struct Room * find_next_navigable_room_for_thing_with_capacity_and_closer_than(struct Thing *thing, int prev_room_idx, unsigned char nav_flags, long used, long *neardistance) { long distance; struct Coord3d pos; struct Room *room; unsigned long k; int i; k = 0; i = prev_room_idx; while (i != 0) { room = room_get(i); if (room_is_invalid(room)) { ERRORLOG("Jump to invalid room detected"); break; } i = room->next_of_owner; // Per-room code // Compute simplified distance - without use of mul or div distance = abs(thing->mappos.x.stl.num - (int)room->central_stl_x) + abs(thing->mappos.y.stl.num - (int)room->central_stl_y); if ((*neardistance > distance) && (room->used_capacity >= used)) { if (find_first_valid_position_for_thing_anywhere_in_room(thing, room, &pos)) { TbBool is_connected; switch (thing->class_id) { case TCls_Creature: is_connected = creature_can_navigate_to(thing, &pos, nav_flags); break; default: is_connected = navigation_points_connected(&thing->mappos, &pos); break; } if (is_connected) { *neardistance = distance; return room; } } } // Per-room code ends k++; if (k > ROOMS_COUNT) { ERRORLOG("Infinite loop detected when sweeping rooms list"); break; } } return INVALID_ROOM; }
struct Room * find_nearest_navigable_room_for_thing_with_capacity_and_closer_than(struct Thing *thing, PlayerNumber owner, RoomKind rkind, unsigned char nav_flags, long used, long *neardistance) { struct Dungeon *dungeon; struct Room *nearoom; long distance; struct Coord3d pos; struct Room *room; unsigned long k; int i; SYNCDBG(18,"Searching for %s navigable by %s index %d",room_code_name(rkind),thing_model_name(thing),(int)thing->index); dungeon = get_dungeon(owner); nearoom = INVALID_ROOM; k = 0; i = dungeon->room_kind[rkind]; while (i != 0) { room = room_get(i); if (room_is_invalid(room)) { ERRORLOG("Jump to invalid room detected"); break; } i = room->next_of_owner; // Per-room code // Compute simplified distance - without use of mul or div distance = abs(thing->mappos.x.stl.num - (int)room->central_stl_x) + abs(thing->mappos.y.stl.num - (int)room->central_stl_y); if ((*neardistance > distance) && (room->used_capacity >= used)) { if (find_first_valid_position_for_thing_anywhere_in_room(thing, room, &pos)) { if ((thing->class_id != TCls_Creature) || creature_can_navigate_to(thing, &pos, nav_flags)) { *neardistance = distance; nearoom = room; } } } // Per-room code ends k++; if (k > ROOMS_COUNT) { ERRORLOG("Infinite loop detected when sweeping rooms list"); break; } } return nearoom; }