def __init__(self, star, orbit_number): self.star = star self.orbit_number = orbit_number self.name = Planet.names[orbit_number] self.type = utilities.choose_from_probability(Planet.star_type_probability_table[self.star.type]) self.size = utilities.choose_from_probability(Planet.size_probability_table) self.richness = utilities.choose_from_probability(Planet.star_richness_probability_table[self.star.type]) self.gravity = Planet.size_richness_gravity_table[self.size][self.richness] logger.debug("%s: %s %s %s %s", self.name, self.size, self.type, self.richness, self.gravity)
def add(self, star, orbit_number, type=None): if type is None: type = utilities.choose_from_probability(Orbitals.star_orbital_probability_table[star.type]) if self.star_orbits.has_key(star): if orbit_number in self.star_orbits[star]: raise Exception, "already added orbit %d for star %s"%(orbit_number, star.name) else: self.star_orbits[star] = [] self.star_orbits[star].append(orbit_number) if type == 'nothing': object = Empty(star, orbit_number) elif type == 'planet': object = Planet(star, orbit_number) self.planets.append(object) elif type == 'asteroid belt': object = AsteroidBelt(star, orbit_number) self.asteroid_belts.append(object) elif type == 'gas giant': object = GasGiant(star, orbit_number) self.gas_giants.append(object) else: raise Exception, 'Unknown orbital type: %s'%type return object
def generate_stars_and_black_holes(self, config): # generate coordinates for stars AND black holes in one pass # since they may not overlap object_coordinates = utilities.random_dispersed_coordinates( config.limits[0], config.limits[1], config.limits[2], config.limits[3], amount=config.star_count, dispersion=config.dispersion ) self.stars = stars.Stars(self.orbitals) self.black_holes = black_holes.BlackHoles() for coordinate in object_coordinates: object_type = utilities.choose_from_probability(config.object_pool) if object_type == 'black hole': self.black_holes.add(coordinate) else: # object_type is color self.stars.add(coordinate, object_type)
def test_zeroed_key(self): "If a key has value zero, it should never be the return value." table = {'a': 0, 'b': 1} self.assertEqual(utilities.choose_from_probability(table), 'b')
def test_total_0(self): "If probabilities total zero, result should be the (alpha-sort) first probability." table = {'c': 0, 'a':0, 'b': 0} self.assertEqual(utilities.choose_from_probability(table), 'a')