def tick(self, tick): if not self.brain.actions: # Lay babies... if random.random() < 1 / 32: corpses = [e for e in self.owner.visible_entities if e.isinstance('Corpse') and e.position != self.owner.position] corpses = sorted(corpses, key=lambda c: utils.math.distance(self.owner.position, c.position)) target = corpses[0] if corpses else None if target: act = movetoaction.MoveToAction(self.owner, target.position) self.brain.add_action(act) # ...or wander. else: batched_action = action.BatchedAction(self.owner) for _ in range(random.randint(1, 3)): idle_action = action.IdleAction(self.owner) idle_action.parent = batched_action self.brain.add_action(idle_action) wander_action = wanderaction.WanderAction(self.owner) wander_action.parent = batched_action self.brain.add_action(wander_action) self.brain.add_action(batched_action)
def perform(self): path = instances.scene_root.level.pathfinder.get_path( *self.performer.position, *self.destination) if self.max_moves: path = path[:int(self.max_moves)] moves = helpers.MoveHelper.path_to_moves(self.performer.position, path) if not moves: return batched_move = action.BatchedAction(self.performer) first_move = None for command in moves: move_action = helpers.MoveHelper.move_to_action( self.performer, command) if move_action: move_action.parent = batched_move self.performer.brain.add_action(move_action) if not first_move: first_move = move_action self.performer.brain.add_action(batched_move) if self.performer.brain.actions[0] is first_move: self.performer.brain.perform_action()
def tick(self, tick): if not self.brain.actions: # Attempt to heal # TODO: Have a more generic way of finding healing items corpses = [ e for e in self.owner.visible_entities if e.isinstance('Corpse') ] corpses = sorted(corpses, key=lambda c: utils.math.distance( self.owner.position, c.position)) target = corpses[0] if corpses else None if target: act = movetoaction.MoveToAction(self.owner, target.position) self.brain.add_action(act) # Wander otherwise else: batched_action = action.BatchedAction(self.owner) for _ in range(random.randint(1, 3)): idle_action = action.IdleAction(self.owner) idle_action.parent = batched_action self.brain.add_action(idle_action) wander_action = wanderaction.WanderAction(self.owner) wander_action.parent = batched_action self.brain.add_action(wander_action) self.brain.add_action(batched_action)
def tick(self, tick): if not self.brain.actions: # Attempt to heal corpses = [e for e in self.owner.visible_entities if e.isinstance('Corpse') and e.position != self.owner.position] corpses = sorted(corpses, key=lambda c: utils.math.distance(self.owner.position, c.position)) target = corpses[0] if corpses else None if not target: weaker = [e for e in self.owner.visible_entities if e.isinstance('Creature') and e.current_health == 1] sorted(weaker, key=lambda w: utils.math.distance(self.owner.position, w.position)) target = weaker[0] if weaker else None if target: act = movetoaction.MoveToAction(self.owner, target.position) self.brain.add_action(act) # Wander otherwise else: batched_action = action.BatchedAction(self.owner) for _ in range(random.randint(1, 3)): idle_action = action.IdleAction(self.owner) idle_action.parent = batched_action self.brain.add_action(idle_action) wander_action = wanderaction.WanderAction(self.owner) wander_action.parent = batched_action self.brain.add_action(wander_action) self.brain.add_action(batched_action)
def perform(self): moves = (1, 0), (-1, 0), (0, 1), (0, -1) number_of_moves = random.randint(1, 3) batched_move = action.BatchedAction(self.performer) for _ in range(number_of_moves): move_action = moveaction.MoveAction(self.performer, moves[random.randint(0, 3)]) move_action.parent = batched_move self.performer.brain.add_action(move_action) self.performer.brain.add_action(batched_move)
def tick(self, tick): # Idle behavior. Wait and wander. if not self.brain.actions: batched_action = action.BatchedAction(self.owner) for _ in range(random.randint(1, 3)): idle_action = action.IdleAction(self.owner) idle_action.parent = batched_action self.brain.add_action(idle_action) wander_action = wanderaction.WanderAction(self.owner) wander_action.parent = batched_action self.brain.add_action(wander_action) self.brain.add_action(batched_action)
def queue_batched_move(self, moves): moves = [m for m in moves if m in helpers.DirectionHelper.valid_moves] if not moves: return batched_move = action.BatchedAction(self) for command in moves: move_action = helpers.MoveHelper.move_to_action(self, command) if move_action: move_action.parent = batched_move self.brain.add_action(move_action) self.brain.add_action(batched_move)
def tick(self, tick): # Idle behavior. Wait and wander. if not self.brain.actions: batched_action = action.BatchedAction(self.owner) for _ in range(random.randint(1, 3)): idle_action = action.IdleAction(self.owner) idle_action.parent = batched_action self.brain.add_action(idle_action) wander_action = wanderaction.WanderAction(self.owner) wander_action.parent = batched_action self.brain.add_action(wander_action) self.brain.add_action(batched_action) self.turns_util_sleep -= 1 if self.turns_util_sleep <= 0 and random.random() < 1 / 32: self.brain.set_state(creature.CreatureSleepState)
def handle_events(self, event): super().handle_events(event) if self.state == "EXITED": return if event.type == 'TWITCHCHATMESSAGE': if event.nickname == self.name: commands = event.message.split(' ') if commands[0].upper() == '!MOVE' or commands[0].upper( ) == '!MV': # Moves can be either a series of moves (eg. ULDR) or a # players name moves = ''.join(commands[1:]) if moves and moves[0] == '@': moves = moves[1:] players = instances.scene_root.players target = [p for p in players if p.name == moves.lower()] target = target[0] if target else None if target and target is not self: path = instances.scene_root.level.player_pathfinder.get_path( *self.position, *target.position)[:-1] moves = helpers.MoveHelper.path_to_moves( self.position, path) self.queue_batched_move(moves) elif commands[0].upper() == '!ATTACK' or commands[0].upper( ) == '!AT': moves = ''.join(commands[1:]) moves = [ helpers.DirectionHelper.get_direction(m) for m in moves if m in helpers.DirectionHelper.valid_moves ] if moves: batched_attack = action.BatchedAction(self) for attack_dir in moves: #act = attackaction.AttackAction(self, direction=attack_dir) act = self.weapon.Action(self, direction=attack_dir) act.parent = batched_attack self.brain.add_action(act) self.brain.add_action(batched_attack) self._has_taken_action = True elif commands[0].upper() == '!DROP': self.drop_weapon() self._has_taken_action = True elif commands[0].upper() == '!THROW': direction = commands[1] if len(commands) > 1 else None direction = helpers.DirectionHelper.get_direction( direction[0]) if not direction: return dest = utils.math.add(self.position, direction) for target_entity in instances.scene_root.get_entity_at( *dest): act = throwaction.ThrowAction(self, target_entity) if act.prerequisite(): self.brain.add_action(act) break if not self.weapon.isinstance('Fist'): w = self.weapon w.remove() w.position = dest instances.scene_root.append(w) self.equip_weapon(fist.Fist()) act = throwaction.ThrowAction(self, w) self.brain.add_action(act) elif commands[0].upper() == '!STAIRS' and game.Game.args.debug: stair = instances.scene_root.downward_stair path = instances.scene_root.level.player_pathfinder.get_path( self.x, self.y, stair.x, stair.y) moves = helpers.MoveHelper.path_to_moves( self.position, path) self.queue_batched_move(moves) elif commands[0].upper() == '!STOP': next_action = self.brain.actions[ 0] if self.brain.actions else None if next_action and next_action.isinstance('MoveAction'): next_action.fail() elif event.type == 'HALF-TICK': self.half_tick()