#include "room_path.h" #include "game.h" #include "grid_entity_pit.h" #include "grid_entity_teleporter.h" #include "entity_pickup.h" #include "entity_effect.h" #include "log.h" #include #include #include constexpr int PATH_NEIGHBORS_MAX = 32; constexpr int PATH_CARVE_COST = 448; constexpr int PATH_BLOCKED = 1000; constexpr float PATH_HEURISTIC_SCALE = 10; constexpr uint16_t PATH_NO_CELL = 0xffff; // The sides CanSpawnObstacleAtPosition keeps joined around a new obstacle, and its mark for a door beside it. constexpr std::array, 4> OBSTACLE_SIDES = {{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}}; constexpr int OBSTACLE_BESIDE_DOOR = -2; // The obstacles a carving search may cross and break: the rocks but blocks and pillars, and the pits it fills. static bool is_carvable(GridEntityType type) { switch (type) { case GRID_ROCK: case GRID_ROCKT: case GRID_ROCK_BOMB: case GRID_ROCK_ALT: case GRID_ROCK_SS: case GRID_ROCK_SPIKED: case GRID_ROCK_ALT2: case GRID_ROCK_GOLD: case GRID_PIT: return true; default: return false; } } // The distance from a cell to the goal in cells, ten times over, as the search's estimate. static uint32_t path_estimate(const Room& room, int cell, int goal) { float columns = (float)(goal % room.gridWidth) - (float)(cell % room.gridWidth); float rows = (float)(goal / room.gridWidth) - (float)(cell / room.gridWidth); return (uint32_t)(std::sqrt(rows * rows + columns * columns) * PATH_HEURISTIC_SCALE); } // The cells a search steps to from one: left, right, up and down, then the other teleporters of the variant of a // working teleporter it stands on. The scan for those starts past it and stops two cells short of a full turn. static int path_neighbors_get(Room& room, int cell, uint32_t flags, std::array& neighbors) { int column = cell % room.gridWidth; int row = cell / room.gridWidth; int count = 0; neighbors[count++] = room.GetGridIndexByTile(column - 1, row); neighbors[count++] = room.GetGridIndexByTile(column + 1, row); neighbors[count++] = room.GetGridIndexByTile(column, row - 1); neighbors[count++] = room.GetGridIndexByTile(column, row + 1); if (!(flags & GRID_PATH_TELEPORTERS)) return count; GridEntity* teleporter = room.gridEntities[cell]; if (!teleporter || teleporter->type != GRID_TELEPORTER || teleporter->state == TELEPORTER_STATE_OFF) return count; int cells = room.gridWidth * room.gridHeight; int scan = teleporter->gridIndex; for (int step = 0; step < cells - 2; step++) { scan = (scan + 1) % cells; GridEntity* other = scan < ROOM_GRID_CELLS ? room.gridEntities[scan] : nullptr; bool isPartner = other && other->type == GRID_TELEPORTER && other->variant == teleporter->variant; if (isPartner && count < PATH_NEIGHBORS_MAX) neighbors[count++] = scan; } return count; } // What stepping onto a cell costs: 1 onto open ground, the carve cost onto an obstacle a carving search breaks, or // nothing (0) when it is closed. Spikes and cells marked by the paths are solid. static uint32_t path_step_cost(Room& room, int cell, uint32_t flags) { GridEntity* grid = room.gridEntities[cell]; int collision = grid ? (grid->type == GRID_SPIKES ? COLLISION_SOLID : grid->collisionClass) : COLLISION_NONE; if (room.gridPaths[cell] >= PATH_BLOCKED) collision = COLLISION_SOLID; if ((flags & GRID_PATH_PLAYER_WALLS) && collision == COLLISION_WALL_EXCEPT_PLAYER) collision = COLLISION_NONE; if ((flags & GRID_PATH_CARVE) && collision == COLLISION_SOLID) return grid && is_carvable(grid->type) ? PATH_CARVE_COST : 0; return collision == COLLISION_NONE ? 1 : 0; } // The carve of a found path, from the goal back to the start: rocks break (lifted off on the room's first frame) and // empty pits fill. static void path_carve(Room& room, const std::array& cameFrom, int goal) { for (int cell = goal; cell != PATH_NO_CELL; cell = cameFrom[cell]) { GridEntity* grid = room.gridEntities[cell]; if (!grid || !is_carvable(grid->type)) continue; if (grid->type == GRID_PIT) { if (grid->state != PIT_STATE_EMPTY) continue; grid->state = PIT_STATE_FILLED; static_cast(grid)->UpdateCollision(); } else if (room.enterFrame == g_game->frameCount) reconstruction_missing("Room::PickupGridEntity 00825c50"); else grid->Destroy(true, EntityRef()); } } // 00821fc0 Whether a path joins two cells, by an A* search that expands the open cell of lowest estimate, the first in // cell order on a tie. Estimates are compared in 16 bits as the original's are. A carving search clears its path. bool is_grid_path_valid(Room& room, const GridPath& path) { std::array costs; std::array estimates; std::array cameFrom; std::array neighbors; std::set closed; std::set open = {path.start}; costs.fill(UINT32_MAX); estimates.fill(UINT32_MAX); cameFrom.fill(PATH_NO_CELL); costs[path.start] = 0; estimates[path.start] = path_estimate(room, path.start, path.goal) & PATH_NO_CELL; while (!open.empty()) { int current = *std::ranges::min_element(open, {}, [&](int cell) { return (uint16_t)estimates[cell]; }); if (current == path.goal) { if (path.flags & GRID_PATH_CARVE) path_carve(room, cameFrom, current); return true; } open.erase(current); closed.insert(current); int count = path_neighbors_get(room, current, path.flags, neighbors); for (int i = 0; i < count; i++) { int next = neighbors[i]; if (next < 0) continue; uint32_t step = path_step_cost(room, next, path.flags); if (step == 0 || closed.contains(next)) continue; uint32_t cost = costs[current] + step; if (open.contains(next) && cost >= costs[next]) continue; open.insert(next); costs[next] = cost; cameFrom[next] = (uint16_t)current; estimates[next] = path_estimate(room, next, path.goal) + cost; } } return false; } // 00823be0 Whether a position can walk to the room's entrance or to the player: its cell must be open (or a wall only // players pass), then a path must join them through those walls and the teleporters. bool is_reachable_from_player(Room& room, const Reach& reach) { Entity_Player* player = reach.player ? reach.player : g_game->GetPlayer(0); int goal = room.GetGridIndex(reach.goal == REACH_PLAYER ? player->pos : player->GetEnterPosition()); int start = room.GetGridIndex(reach.position); if (start < 0) return false; uint32_t collision = room.GetGridCollision(start); bool isStartOpen = collision == COLLISION_NONE || collision == COLLISION_WALL_EXCEPT_PLAYER; return goal >= 0 && isStartOpen && is_grid_path_valid(room, {start, goal, GRID_PATH_PLAYER_WALLS | GRID_PATH_TELEPORTERS}); } // Whether two cells lie less than two steps apart along the grid. static bool is_cell_near(Room& room, int cell, int other) { return std::abs(cell % room.gridWidth - other % room.gridWidth) + std::abs(cell / room.gridWidth - other / room.gridWidth) < 2; } // Whether a cell beside a new obstacle is one the paths around it must still join: a free cell, or a grid entity a // walker passes (TNT and poop always) but a trapdoor, stairs, plate or teleporter. A door beside it forbids it (-2). static int obstacle_neighbor_get(Room& room, int cell) { GridEntity* grid = room.gridEntities[cell]; if (!grid) return room.gridPaths[cell] < PATH_BLOCKED ? cell : -1; if (grid->type == GRID_DOOR) return OBSTACLE_BESIDE_DOOR; bool isPassable = (uint32_t)grid->gridIndex >= ROOM_GRID_CELLS || room.gridPaths[grid->gridIndex] < PATH_BLOCKED || grid->type == GRID_TNT || grid->type == GRID_POOP; return isPassable && !is_grid_floor_special(grid->type) ? cell : -1; } // 00822820 Whether an obstacle may go on a cell: inside the room (not a crawlspace), on a free or walkable cell that is // not floor special, under no grounded player (unless forced), two steps from the doors, the Stairway's ladder and the // shop items, and leaving the cells around it joined. bool Room::CanSpawnObstacleAtPosition(int gridIndex, bool isForced) { if ((uint32_t)gridIndex >= ROOM_GRID_CELLS || !IsPositionInRoom(GetGridPosition(gridIndex), 0) || roomType == ROOM_DUNGEON) return false; GridEntity* grid = gridEntities[gridIndex]; bool isWalkable = grid && (grid->IsEasyCrushableOrWalkable() || grid->IsDangerousCrushableOrWalkable()); if (grid && (!isWalkable || is_grid_floor_special(grid->type))) return false; for (Entity_Player* player : g_game->playerManager.playerList) { bool isGrounded = player->entityGridCollisionClass > GRIDCOLL_WALLS; if (!isForced && isGrounded && GetGridIndex(player->pos) == gridIndex) return false; } for (int slot = 0; slot < DOOR_SLOT_COUNT; slot++) { bool isDoor = descriptor->data && descriptor->data->doors & (1u << slot); if (isDoor && is_cell_near(*this, doorGridPositions[slot], gridIndex)) return false; } for (Entity* entity : entityList.updateEL) { bool isKept = (entity->type == ENTITY_EFFECT && entity->variant == EFFECT_TALL_LADDER) || (entity->type == ENTITY_PICKUP && static_cast(entity)->price != 0); int cell = isKept ? GetGridIndex(entity->pos) : -1; if (cell >= 0 && is_cell_near(*this, cell, gridIndex)) return false; } std::vector neighbors; for (auto [column, row] : OBSTACLE_SIDES) { int cell = GetGridIndexByTile(gridIndex % gridWidth + column, gridIndex / gridWidth + row); int neighbor = cell >= 0 ? obstacle_neighbor_get(*this, cell) : -1; if (neighbor == OBSTACLE_BESIDE_DOOR) return false; if (neighbor >= 0) neighbors.push_back(neighbor); } int savedPath = gridPaths[gridIndex]; bool isJoined = true; gridPaths[gridIndex] = PATH_BLOCKED; for (size_t i = 0; i + 1 < neighbors.size() && isJoined; i++) for (size_t j = i + 1; j < neighbors.size() && isJoined; j++) isJoined = is_grid_path_valid(*this, {neighbors[i], neighbors[j], 0}); gridPaths[gridIndex] = savedPath; return isJoined; }