def __attackCleanup(self, mob): ''' Perform additional cleanup work and logging as a result of an agent having killed the given mob. ''' # Get any items that were immediately picked up itemsPickedUp, _ = self.inventory.sync() # Get any items that were dropped to the ground (register their IDs w/ Inventory class to preserve them) nearbyEntities = self.nearbyEntities() itemsDropped = [x for x in nearbyEntities if Items.All.isMember(x.type)] for item in itemsDropped: for i in range(0, item.quantity): Inventory.registerDropItem(item) # Create the log report for the attack self.__logReports.append(LogUtils.AttackReport(mob, True, itemsDropped, itemsPickedUp)) # Trigger a log report for new closest mobs of this mob's type for all agents allAgents = list(Agent.allAgents.values()) for agent in allAgents: if Mobs.All.isMember(mob.type): agent.closestMob() if Mobs.Peaceful.isMember(mob.type): agent.closestMob(Mobs.Peaceful) if Mobs.Hostile.isMember(mob.type): agent.closestMob(Mobs.Hostile) if Mobs.Food.isMember(mob.type): agent.closestMob(Mobs.Food)
def attackMob(self, mob): ''' Direct this agent to attack a mob using the currently equipped item. Returns true if the agent successfully swung, false otherwise. Preconditions: - The given entity is a mob - The agent is looking at the mob - The agent is at the mob ''' # Check action override if self.__shouldPerformActionOverride(self.attackMob): return self.__actionOverride.function(*self.__actionOverride.args) # Preconditions if not self.__checkPreconditions( Mobs.All.isMember(mob.type), self.__isLookingAt(mob.position), self.__isAt(mob.position)): self.stopMoving() return False # Action oldMobsKilled = self.__mobsKilled() self.__startAttacking() self.stopMoving() time.sleep(0.7) newMobsKilled = self.__mobsKilled() if newMobsKilled > oldMobsKilled: self.__attackCleanup(mob) else: self.__logReports.append(LogUtils.AttackReport(mob, False, [], [])) return True