def check_death(self, world): """Check if potassium bacteria should die. :param sandbox.simulate_world.World world: The world object """ x_y_key = utils.get_x_y_key(self.x_position, self.y_position) if world.world_map[x_y_key].potassium > self.DEATH_CONCENTRATION: self._die(world)
def _die(self, world): """Kill the potassium bacteria. :param sandbox.simulation_world.World world: The world object """ x_y_key = utils.get_x_y_key(self.x_position, self.y_position) world.world_map[x_y_key].potassium += 3 world.global_bacteria.remove(self)
def _die(self, world): """Kill the tree. :param sandbox.simulation_world.World world: The world object """ x_y_key = utils.get_x_y_key(self.x_position, self.y_position) world.world_map[x_y_key].tree_matter += 10 world.global_plants.remove(self) world.world_map[x_y_key].beings['tree'].remove(self)
def spawn_tree(self, x_position, y_position): """Spawn a single tree. :param int x_position: The x position of this tree :param int y_position: The y position of this tree """ tree = simulate_plants.TreePlant(x_position, y_position) x_y_key = utils.get_x_y_key(x_position, y_position) self.world.global_plants.append(tree) self.world.world_map[x_y_key].beings['tree'].append(tree)
def spawn_grass_plant(self, x_position, y_position): """Spawn a single grass plant. :param int x_position: The x position of this grass plant :param int y_position: The y position of this grass plant """ plant = simulate_plants.GrassPlant(x_position, y_position) x_y_key = utils.get_x_y_key(x_position, y_position) self.world.global_plants.append(plant) self.world.world_map[x_y_key].beings['grass'].append(plant)
def __init__(self, max_x_size, max_y_size): self.max_x_size = max_x_size self.max_y_size = max_y_size self.time = 0 self.world_map = {} for x_val in range(0, self.max_x_size): for y_val in range(0, self.max_x_size): x_y_key = utils.get_x_y_key(x_val, y_val) self.world_map[x_y_key] = Land(x_val, y_val) self.global_bacteria = [] self.global_plants = []
def reproduce(self, world): """Make a new child grass plant. :param sandbox.simulate_world.World world: The world object :rtype: sandbox.simulate_plants.GrassPlant """ new_x_position, new_y_position = utils.get_new_position( self.x_position, self.y_position, world.max_x_size, world.max_y_size, 2) child = GrassPlant(new_x_position, new_y_position) x_y_key = utils.get_x_y_key(new_x_position, new_y_position) world.global_plants.append(child) world.world_map[x_y_key].beings['grass'].append(child)
def _die(self, world): """Kill the grass plant. :param sandbox.simulation_world.World world: The world object """ x_y_key = utils.get_x_y_key(self.x_position, self.y_position) world.world_map[x_y_key].plant_matter += 1 world.world_map[x_y_key].carbon += 1 self.spawn_bacteria(world) self.spawn_bacteria(world) self.spawn_bacteria(world) world.global_plants.remove(self) world.world_map[x_y_key].beings['grass'].remove(self)
def check_death(self, world): """Check if grass plant should die. :param sandbox.simulate_world.World world: The world object """ x_y_key = utils.get_x_y_key(self.x_position, self.y_position) world.world_map[ x_y_key].nitrogen -= self.DEATH_CONCENTRATION * 2 / self.max_lifetime world.world_map[ x_y_key].phosphorus -= self.DEATH_CONCENTRATION * 1 / self.max_lifetime world.world_map[ x_y_key].potassium -= self.DEATH_CONCENTRATION * 1 / self.max_lifetime if world.world_map[x_y_key].nitrogen < 0 or \ world.world_map[x_y_key].phosphorus < 0 or \ world.world_map[x_y_key].potassium < 0: self._die(world) return if len(world.world_map[x_y_key].beings['grass']) > 10: self._die(world) return
def plot_bacteria(world): """Plot the bacteria in the world. :param sandbox.simulate_world.World world: The world object """ print('*'*100) print(world.time) bacteria_count = {} for bacteria in world.global_bacteria: if str(bacteria) in bacteria_count: bacteria_count[str(bacteria)] += 1 else: bacteria_count[str(bacteria)] = 1 print(bacteria_count) nitrogen = 0 phosphorus = 0 potassium = 0 carbon = 0 plant_matter = 0 tree_matter = 0 grass = 0 trees = 0 for x_val in range(0, world.max_x_size): for y_val in range(0, world.max_y_size): x_y_key = utils.get_x_y_key(x_val, y_val) nitrogen += world.world_map[x_y_key].nitrogen phosphorus += world.world_map[x_y_key].phosphorus potassium += world.world_map[x_y_key].potassium carbon += world.world_map[x_y_key].carbon plant_matter += world.world_map[x_y_key].plant_matter tree_matter += world.world_map[x_y_key].tree_matter grass += len(world.world_map[x_y_key].beings['grass']) trees += len(world.world_map[x_y_key].beings['tree']) print('nitrogen:{} phosphorus:{} potassium:{} carbon:{}' .format(nitrogen, phosphorus, potassium, carbon)) print('plant_matter:{} tree_matter:{}' .format(plant_matter, tree_matter)) print('grass: {} trees:{}'.format(grass, trees))