#pragma once #include "classes.h" #include "room_config.h" #include #include #include #include #include constexpr int LEVEL_GRID_WIDTH = 13; constexpr int LEVEL_GRID_CELLS = LEVEL_GRID_WIDTH * LEVEL_GRID_WIDTH; constexpr int DIRECTION_COUNT = 4; constexpr uint32_t ROOM_SHAPES_ALL = (1 << ROOMSHAPE_ANY) - 1; // The door slots of a room's door mask, the second of each side being the far half of a big room. enum DoorSlot : int { DOOR_NONE = -1, DOOR_LEFT0, DOOR_UP0, DOOR_RIGHT0, DOOR_DOWN0, DOOR_LEFT1, DOOR_UP1, DOOR_RIGHT1, DOOR_DOWN1, DOOR_SLOT_COUNT }; // Whether a door slot a corridor shape lacks still counts, as it does when a room checks what touches it. enum ThinShapes { THIN_SHAPES_RESTRICTED, THIN_SHAPES_IGNORED }; // Which shapes a candidate room may take: the floor's own shapes at their odds, or any allowed shape. enum CandidateShapes { CANDIDATE_SHAPES_ROLLED, CANDIDATE_SHAPES_ANY }; // Whether placing a boss room looks for a new dead end, or takes the room an XL floor's first boss room left beside it. enum BossRoomPick { BOSS_ROOM_NEXT, BOSS_ROOM_DETERMINE }; // A cell of the 13x13 floor grid, (-1, -1) for none. struct XY { int x = -1; int y = -1; bool operator==(const XY&) const = default; XY operator+(XY other) const { return {x + other.x, y + other.y}; } XY operator-(XY other) const { return {x - other.x, y - other.y}; } int ToGridIdx() const; static XY NewFromGridIdx(int gridIdx); }; constexpr XY XY_NONE{}; // A room of a layout being grown: its top left cell and shape, the doors it has toward its neighbours, and how it // joined the layout (the parent's door slot and side, the cell it entered through and its distance from the start). // A secret room is never counted as a neighbour when an end room grows. // Construction (007381b0), copies and destruction (007507d0, 007527b0, 007529c0, 00752e40, 009b1e40, 009b1f00) are // the defaults. struct LevelGenerator_Room { std::set neighbors; XY linkPos; XY position{0, 0}; RoomShape shape = ROOMSHAPE_1x1; int connectDirection = -1; int connectDoorSlot = DOOR_NONE; int distanceFromStart = 0; int generationIndex = 0; uint32_t doors = 0; bool isDeadEnd = false; bool isSecret = false; }; // How a layout grows: the rooms to place past the start, the dead ends it must end with, the shapes the floor's rooms // come in, and the floor's kind (XL, the Dark Room and Chest's chapter, the Void). struct GenerationSettings { uint32_t allowedShapes = ROOM_SHAPES_ALL; int deadEndCount = 0; int roomCount = 0; bool isChapter6 = false; bool isVoid = false; bool isXL = false; }; // Grows a floor's layout on the 13x13 grid from the start room, deterministically from its seed: rooms branch out by // a queue of candidates, dead ends are sorted farthest first, then the special rooms take dead ends (grown to their // shapes) or the best empty cells. class LevelGenerator { public: std::array roomMap{}; std::array blockedPositions{}; std::vector rooms; std::vector deadEnds; std::vector nonDeadEnds; RNG rng{}; uint32_t allowedShapes = ROOM_SHAPES_ALL; int nextBossRoomIdx = -1; int numBossRooms = 0; bool isChapter6 = false; bool isVoid = false; bool isXL = false; explicit LevelGenerator(uint32_t seed); void Generate(const GenerationSettings& settings, const LevelGenerator_Room& startRoom); LevelGenerator_Room* CreateRoom(const LevelGenerator_Room& room); LevelGenerator_Room* try_add_dead_end(const std::set* excludedCells); bool add_dead_end(); LevelGenerator_Room* GetNewEndRoom(RoomShape shape, uint32_t possibleDoors); void DetermineBossRoom(RoomShape shape, uint32_t possibleDoors); LevelGenerator_Room* place_boss_room(RoomShape shape, uint32_t possibleDoors, BossRoomPick pick); LevelGenerator_Room* place_secret_room(const std::set& excludedCells); LevelGenerator_Room* try_place_room(const std::set* doorCells); std::vector get_all_rooms(); void block_cell(XY position); void block_room_area(const LevelGenerator_Room& room); void block_cells_near_rooms(const LevelGenerator& other); void block_unused_door_cells(const LevelGenerator_Room& room, uint32_t doorMask); static bool has_shape_slot(RoomShape shape, int doorSlot, ThinShapes thinShapes); static XY get_door_source_position(XY position, RoomShape shape, int doorSlot); static XY get_door_target_position(XY position, RoomShape shape, int doorSlot, ThinShapes thinShapes); static std::span get_room_placement_offsets(RoomShape shape); private: void generate_rooms(int roomCount, const LevelGenerator_Room& startRoom); std::vector get_neighbor_candidates(int generationIndex, CandidateShapes candidateShapes); LevelGenerator_Room& place_room(const LevelGenerator_Room& room); bool can_place_room(XY position, RoomShape shape); bool is_placement_valid(XY position, RoomShape shape); int count_neighbor_rooms(XY position); bool try_resize_room(LevelGenerator_Room& room, RoomShape shape, uint32_t possibleDoors); void update_room_neighbors(LevelGenerator_Room& room); void mark_dead_ends(); void update_dead_end_lists(); };