def step(self): self.energy -= 2 self.age = self.age + 1 if self.energy <= 0 or self.age > 300: kill(self.location) return neighbor_space = choose_neighbor(self.location) neighbor = g.creatures.get(neighbor_space) if neighbor: if type(neighbor) == SimPlant: color_similarity = compare_color([self.r, self.g, self.b], [neighbor.r, neighbor.g, neighbor.b]) if random.random() < color_similarity: self.energy = self.energy + neighbor.energy kill(neighbor_space) neighbor = None elif type(neighbor) == self.__class__: might_have_space = True while self.energy > 70 and might_have_space: child_space = find_or_make_empty_space( self.location, victims=[SimPlant]) if child_space: self.energy = self.energy - 20 g.creatures[child_space] = breed( self, neighbor, loc=child_space) else: might_have_space = False if not neighbor: # either was empty or got eaten. old_loc = self.location self.location = neighbor_space g.creatures[neighbor_space] = self del g.creatures[old_loc] self.energy = min(200, self.energy) if self.energy == 200: # SOO MUCH ENERGY! GONNA BUD child_space = find_or_make_empty_space(self.location, victims=[SimPlant]) if child_space: g.creatures[child_space] = bud(self, loc=child_space) self.energy == 100
def step_subclass(self): self.energy -= self.metabolism self.age = self.age + 1 if self.energy <= 0 or self.age > self.max_age: kill(self.location) return neighbor_space = choose_neighbor(self.location) neighbor = g.creatures.get(neighbor_space) if neighbor: if type(neighbor) == SimPlant: color_similarity = compare_color([self.h, self.s, self.l], [neighbor.h, neighbor.s, neighbor.l]) eat_chance = chance_to_eat([self.h, self.s, self.l], [neighbor.h, neighbor.s, neighbor.l]) if random.random() < eat_chance: self.energy = self.energy + neighbor.energy * color_similarity self.energy = min(self.max_energy, self.energy) kill(neighbor_space) neighbor = None elif type(neighbor) == self.__class__: if self.energy > self.min_breed_energy: child_space = find_or_make_empty_space( self.location, victims=[SimPlant]) if child_space: self.energy = self.energy - self.breed_energy_loss g.creatures[child_space] = breed( self, neighbor, loc=child_space) if not neighbor: # either was empty or got eaten. # move there self.move(neighbor_space) if self.energy > self.min_bud_energy: # SOO MUCH ENERGY! GONNA BUD child_space = find_or_make_empty_space(self.location, victims=[SimPlant]) if child_space: g.creatures[child_space] = bud(self, loc=child_space) self.energy == self.energy - self.bud_energy_loss
def chance_to_eat(color1, color2): similarity = compare_color(color1, color2) return 0.4 * (similarity**4 + 0.05) # small constant chance to eat