def drop_weapon(self): if not self.weapon.isinstance('Fist'): i = self.weapon i.remove() i.hidden = False i.position = self.position instances.scene_root.append(i) self.equip_weapon(fist.Fist()) instances.console.describe( self, '{} drops {}'.format(self.display_string, i.display_string), 'Something clatters to the ground')
def __init__(self, char, position=(0, 0), fg=palette.BRIGHT_WHITE, bg=palette.BLACK): super().__init__(char, position, fg, bg) self.brain = brain.Brain(self) self.statuses = [] self.name = 'Creature' self.current_health = 10 self.max_health = 10 self.weapon = None self.visible_tiles = set() self.sight_radius = 7.5 self.equip_weapon(fist.Fist())
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()
def tick(self, tick): if self.owner.weapon.isinstance('Spear'): # Are we lined up for a throw? if self.current_target.x == self.owner.x or self.current_target.y == self.owner.y: # Are we in range? dist = utils.math.distance(self.owner.position, self.current_target.position) if dist <= self.owner.weapon.throw_distance and dist > 1: dir = utils.math.sub(self.current_target.position, self.owner.position) dir = utils.math.normalize(dir) dir = tuple(map(lambda i: int(i), dir)) s = self.owner.weapon s.remove() s.position = utils.math.add(self.owner.position, dir) instances.scene_root.append(s) self.owner.equip_weapon(fist.Fist()) act = throwaction.ThrowAction(self.owner, s) self.brain.add_action(act) self.brain.add_action(action.IdleAction(self.owner)) return # Attempt to line up a throw elif not self.brain.actions: dir = utils.math.sub(self.current_target.position, self.owner.position) if abs(dir[0]) < abs(dir[1]): dir = dir[0], 0 else: dir = 0, dir[1] dir = utils.math.normalize(dir) dir = tuple(map(lambda i: int(i), dir)) act = moveaction.MoveAction(self.owner, dir) self.brain.add_action(act) return # Attack enemy if self.aggro_counter <= 0: self.brain.add_action( movetoaction.MoveToAction(self.owner, self.current_target.position, self.brain.owner.sight_radius)) self.aggro_counter = self.aggro_cooldown self.aggro_counter -= 1 return # Go get our spear elif not self.brain.actions: s = [ e for e in self.owner.visible_entities if e.isinstance('Spear') ] if s: s = sorted(s, key=lambda t: utils.math.distance( self.owner.position, t.position))[0] act = movetoaction.MoveToAction(self.owner, s.position) self.brain.add_action(act) self.brain.add_action(action.IdleAction(self.owner)) return # Attack enemy if self.aggro_counter <= 0: self.brain.add_action( movetoaction.MoveToAction(self.owner, self.current_target.position, self.brain.owner.sight_radius)) self.aggro_counter = self.aggro_cooldown self.aggro_counter -= 1