def tick(self, world: World, elapsed: float, location: Location, position: Position): self.age += elapsed * ModularAnt.AGE_SCALE self.current_wander_behaviour.set_age(self.age) self.current_wander_behaviour.set_seeking_food(not self.carrying_food) if random.random() > self.hold_position_chance * (1 + self.interacting_ticks): self.current_wander_behaviour.do(world, elapsed, location, position, self) if self.age < ModularAnt.FORAGING_AGE or random.random() < ModularAnt.\ FORAGING_PHEROMONE_OVERRIDE_CHANCE: location.add_pheromones(ModularAnt.PHEROMONES_PER_TICK) else: if location.get_brood_pheromone_count() == 0 and location.get_pheromone_count() == 0: location.add_foraging_pheromones(ModularAnt.FORAGING_PHEROMONES_PER_TICK) # If this ant is in the foraging area, it will pick up food readily. if not self.carrying_food: if location.get_ground().get_id() == ForageGrounds.get_id() or \ random.random() < ModularAnt.ALT_PICKUP_CHANCE: foods: [Object] = location.get_objects(Kind.FOOD) if len(foods) > 0: self.carrying_food = True location.remove_object(foods[-1]) elif location.get_ground().get_id() == Nest.get_id(): if random.random() < ModularAnt.FOOD_DROP_CHANCE or sum(map(lambda l: len(l.get_objects(Kind.FOOD)), world.get_adjacent_locations(position.x, position.y, 1, False))) > 0: location.add_object(Food()) self.carrying_food = False # Measure interactions: others: [int] = [] for loc in world.get_adjacent_locations(position.x, position.y, ModularAnt.INTERACTION_RADIUS, False): if loc.get_actor() is None: continue if isinstance(loc.get_actor(), ModularAnt): other: ModularAnt = loc.get_actor() if ModularAnt.can_interact(self, other): others.append(other.get_ant_id()) if self.interacting_with_id in others: self.interacting_ticks += 1 if self.interacting_ticks > ModularAnt.INTERACTING_TICKS_THRESHOLD: self.interact(self.interacting_with_id) elif len(others) > 0: self.interacting_with_id = others[0] self.interacting_ticks = 0
def tick(self, world: World, elapsed: float, location: Location, position: Position): if self.brood_position is None: self.brood_position = (position.x, position.y) self.current_wander_behaviour.set_brood_position(self.brood_position) # location.add_brood_pheromones(ModularQueen.BROOD_PHEROMONES_PER_TICK) for loc in world.get_adjacent_locations(position.x, position.y, 1, False): loc.add_brood_pheromones(ModularQueen.BROOD_PHEROMONES_PER_TICK) self.current_wander_behaviour.set_age(ModularQueen.QUEEN_LOGICAL_AGE) super().tick(world, elapsed, location, position)
def tick(self, world: World, elapsed: float, location: Location, position: Position): try: # COMPLETELY RANDOM CHOICES FOR WALKS (no preserving of direction). to: Location = Random().choice( world.get_adjacent_locations(position.x, position.y, 1, True)) self.move(location, to) self.food_interaction(to, world, position) except IndexError: # The ant can't find anywhere to move... location.add_object(TestObject())
def food_interaction(self, loc: Location, world: World, position: Position): if self.carrying is not None and Random().random( ) < StigmergyAnt.DROP_CHANCE: for l in world.get_adjacent_locations(position.x, position.y, 1, False): if len(l.get_objects(Kind.FOOD)) > 0: loc.add_object(self.carrying) self.carrying = None break else: food_items: [Object] = loc.get_objects(Kind.FOOD) if len(food_items) > 0: self.carrying = food_items[0] loc.remove_object(self.carrying)
def tick(self, world: World, elapsed: float, location: Location, position: Position): if random.random() < FoodGenerator.CHANCE: for loc in world.get_adjacent_locations(position.x, position.y, FoodGenerator.RADIUS, False): if len(loc.get_objects(Kind.FOOD)) == 0: loc.add_object(Food())