def _move_player(self, direction): oldpos = self._mobile.position if oldpos.direction == direction: x, y = oldpos.x, oldpos.y if direction == NORTH: y -= 1 elif direction == NORTH_EAST: x += 1 y -= 1 elif direction == EAST: x += 1 elif direction == SOUTH_EAST: x += 1 y += 1 elif direction == SOUTH: y += 1 elif direction == SOUTH_WEST: x -= 1 y += 1 elif direction == WEST: x -= 1 elif direction == NORTH_WEST: x -= 1 y -= 1 self._mobile.position = Position(x, y, oldpos.z, direction) else: self._mobile.position = Position(oldpos.x, oldpos.y, oldpos.z, direction)
def PathFindWalkRectangle(client, map, rectangle): assert rectangle[0] <= rectangle[2] assert rectangle[1] <= rectangle[3] destinations = [] for x in range(rectangle[0], rectangle[2] + 1): destinations.append(Position(x, rectangle[1])) destinations.append(Position(x, rectangle[3])) for y in range(rectangle[1] + 1, rectangle[3]): destinations.append(Position(rectangle[0], y)) destinations.append(Position(rectangle[2], y)) return PathFindWalkAny(client, map, destinations)
def _walk(self): position = self.player.position if position is None: self._failure() return self.trees = find_tree(self.map, self.exhaust_db, position) if self.trees is None: self._failure() return positions = set() for resource in self.trees: for p in passable_positions_around(self.map, resource.x, resource.y, resource.z, 2): positions.add(p) if len(positions) == 0: # no reachable position; try again resource = self.trees[0] self.exhaust_db.set_exhausted(resource.x / 8, resource.y / 8) reactor.callLater(0.1, self._walk) return nearest = nearest_position( (self.player.position.x, self.player.position.y), positions) position = Position(nearest[0], nearest[1]) client = self._client d = PathFindWalk(self._client, self.map, position).deferred d.addCallbacks(self._walked, self._walk_failed)
def _walk(self): forge = nearest_forge(self._client.world, self._client.world.player.position) print "forge", forge self._map.flush_cache() p = Position(forge[0], forge[1]) d = PathFindWalkNear(self._client, self._map, p, 1).deferred d.addCallbacks(self._walked, self._walk_failed)
def _walk(self): position = self.player.position if position is None: self._failure() return self.trees = find_tree(self.map, self.exhaust_db, position) if self.trees is None: self._failure() return positions = set() for resource in self.trees: for p in passable_positions_around(self.map, resource.x, resource.y, resource.z, 2): positions.add(Position(p[0], p[1])) self.map.flush_cache() d = PathFindWalkAny(self._client, self.map, positions).deferred d.addCallbacks(self._walked, self._walk_failed)
def _try(self): destinations = [] for x in range(self.destination.x - self.distance, self.destination.x + self.distance + 1): for y in range(self.destination.y - self.distance, self.destination.y + self.distance + 1): p = Position(x, y) if self.destination.manhattan_distance(p) <= self.distance: destinations.append(p) destination = choose_destination(self.map, self.player.position, destinations) if destination is None: self._failure(Unreachable()) return self.__walk = d = PathFindWalkFragile(self._client, self.map, destination).deferred self.__walk.addCallbacks(self._success, self._walk_failed)
def PathFindWalkRectangle(client, map, rectangle): assert rectangle[0] <= rectangle[2] assert rectangle[1] <= rectangle[3] wx = rectangle[2] - rectangle[0] wy = rectangle[3] - rectangle[1] player = client.world.player.position x, y = player.x + random.randint(-8, 8), player.y + random.randint(-8, 8) x += random.randint(-wx / 2, wx / 2) y += random.randint(-wy / 2, wy / 2) if x < rectangle[0]: x = rectangle[0] elif x > rectangle[2]: x = rectangle[2] if y < rectangle[1]: y = rectangle[1] elif y > rectangle[3]: y = rectangle[3] return PathFindWalk(client, map, Position(x, y))
def _walk(self): position = self.player.position if position is None: self._failure() return self.tree = find_tree(self.map, self.exhaust_db, position) if self.tree is None: self._failure() return x, y = self.tree.x, self.tree.y if position.x < self.tree.x: x -= 1 elif position.x > self.tree.x: x += 1 if position.y < self.tree.y: y -= 1 elif position.y > self.tree.y: y += 1 position = Position(x, y) d = PathFindWalk(self._client, self.map, position).deferred d.addCallbacks(self._walked, self._walk_failed)
def find_tree(map, exhaust_db, position): item_id, x, y, z = find_resource(map, position, TREES, exhaust_db) print("tree:", item_id, x, y, z) return Position(x, y)
def find_tree(map, exhaust_db, position): center = Position((position.x * 7 + BANK[4]) / 8, (position.y * 7 + BANK[5]) / 8) return find_statics_resource_block(map, center, TREES, exhaust_db)
def is_passable(self, x, y, z): from gemuo.path import Position return self.map.is_passable(x, y, z) and Position(x, y) not in self.bad
def _opened(self, result): d = DirectWalk(self._client, Position(HOUSE[0], HOUSE[1] - 2)).deferred d.addCallbacks(self._walked2, self._walk2_failed)
def _walk(self): self._map.flush_cache() d = PathFindWalk(self._client, self._map, Position(HOUSE[0], HOUSE[1])).deferred d.addCallbacks(self._walked, self._walk_failed)
def add_bad(self, x, y): from gemuo.path import Position self.bad.append(Position(x, y))
def walk_reject(self, seq, x, y, z, direction): self._queue.reject(seq) self._mobile.position = Position(x, y, z, direction)
def _opened2(self, result): d = DirectWalk(self._client, Position(HOUSE[0], HOUSE[1])).deferred d.addCallbacks(self._success, self._failure)
def on_packet(self, packet): if isinstance(packet, p.MobileStatus): mobile = self._make_mobile(packet.serial) mobile.name = packet.name mobile.hits = BoundedValue(packet.hits, packet.hits_max) mobile.female = packet.female mobile.stats = packet.stats mobile.stamina = BoundedValue(packet.stamina, packet.stamina_max) mobile.mana = BoundedValue(packet.mana, packet.mana_max) mobile.gold = packet.gold mobile.armor = packet.armor mobile.mass = packet.mass mobile.stat_cap = packet.stat_cap self._signal('on_mobile_status', mobile) elif isinstance(packet, p.WorldItem): item = self._new_item(packet.serial) item.item_id = packet.item_id item.amount = packet.amount item.hue = packet.hue item.flags = packet.flags item.position = Position(packet.x, packet.y, packet.z, packet.direction) self._signal('on_world_item', item) elif isinstance(packet, p.LoginConfirm): self._clear() self.map_width = packet.map_width self.map_height = packet.map_height self.player = self._make_mobile(packet.serial) self.player.body = packet.body self.player.position = Position(packet.x, packet.y, packet.z, packet.direction) self.walk = Walk(self.player) elif isinstance(packet, p.Delete): self._delete_entity(packet.serial) elif isinstance(packet, p.MobileUpdate): mobile = self._make_mobile(packet.serial) mobile.body = packet.body mobile.hue = packet.hue mobile.flags = packet.flags mobile.position = Position(packet.x, packet.y, packet.z, packet.direction) self._signal('on_mobile_update', mobile) elif isinstance(packet, p.WalkReject): if self.walk is not None: self.walk.walk_reject(packet.seq, packet.x, packet.y, packet.z, packet.direction) self._signal('on_walk_reject') elif isinstance(packet, p.WalkAck): if self.walk is not None: self.walk.walk_ack(packet.seq, packet.notoriety) self._signal('on_walk_ack') elif isinstance(packet, p.OpenContainer): if packet.serial in self.entities: container = self.entities[packet.serial] container.gump_id = packet.gump_id self._signal('on_open_container', container) elif isinstance(packet, p.ContainerItem): item = self._new_item(packet.serial) item.item_id = packet.item_id item.hue = packet.hue item.parent_serial = packet.parent_serial item.amount = packet.amount item.position = Position(packet.x, packet.y) self._signal('on_container_item', item) elif isinstance(packet, p.EquipItem): item = self._new_item(packet.serial) item.item_id = packet.item_id item.hue = packet.hue item.parent_serial = packet.parent_serial item.layer = packet.layer self._signal('on_equip_item', item) elif isinstance(packet, p.SkillUpdate): if self.player is not None: self.player.update_skills(packet.skills) self._signal('on_skill_update', self.player.skills) elif isinstance(packet, p.ContainerContent): containers = set() for x in packet.items: self.on_packet(x) containers.add(x.parent_serial) for x in containers: if x in self.entities: self._signal('on_container_content', self.entities[x]) elif isinstance(packet, p.MobileIncoming): mobile = self._make_mobile(packet.serial) mobile.body = packet.body mobile.hue = packet.hue mobile.flags = packet.flags mobile.position = Position(packet.x, packet.y, packet.z, packet.direction) mobile.notoriety = packet.notoriety for x in packet.items: item = self._new_item(x.serial) item.parent_serial = mobile.serial item.item_id = x.item_id item.layer = x.layer item.hue = x.hue self._signal('on_mobile_incoming', mobile) elif isinstance(packet, p.MovePlayer): if self.walk is not None: self.walk.move_player(packet.direction) self._signal('on_move_player') elif isinstance(packet, p.MobileMoving): mobile = self._make_mobile(packet.serial) oldpos = mobile.position mobile.body = packet.body mobile.hue = packet.hue mobile.flags = packet.flags mobile.position = Position(packet.x, packet.y, packet.z, packet.direction) mobile.notoriety = packet.notoriety self._signal('on_mobile_moving', mobile, oldpos) elif isinstance(packet, p.MobileHits): mobile = self._make_mobile(packet.serial) mobile.hits = BoundedValue(packet.hits, packet.hits_max) self._signal('on_mobile_hits', mobile) elif isinstance(packet, p.MobileMana): mobile = self._make_mobile(packet.serial) mobile.mana = BoundedValue(packet.mana, packet.mana_max) self._signal('on_mobile_mana', mobile) elif isinstance(packet, p.MobileStamina): mobile = self._make_mobile(packet.serial) mobile.stamina = BoundedValue(packet.stamina, packet.stamina_max) self._signal('on_mobile_stamina', mobile) elif isinstance(packet, p.Extended): if packet.extended == 0x0019 and packet.extended2 == 2: # statlock info mobile = self._make_mobile(packet.serial) mobile.stat_locks = packet.stat_locks elif isinstance(packet, p.AsciiMessage): if packet.serial in self.entities: if packet.type == 0x06 or len(packet.name) > 0: entity = self.entities[packet.serial] entity.name = packet.text elif isinstance(packet, p.ChangeCombatant): if packet.serial != 0: self.combatant = self._make_mobile(packet.serial) else: self.combatant = None
def _walk_to_healer(self): if self.walk is not None: return self.walk = PathFindWalkNear(self._client, self.map, Position(1281, 1326), 3) self.walk.deferred.addCallbacks(self._walked, self._walk_failed)