#include "rail_manager.h" #include "entity.h" #include "game.h" #include "grid_entity.h" #include "log.h" #include "room.h" constexpr int BACKDROP_ASHPIT = 46; // The row and the columns where an L room's walls step in around its missing quarter. constexpr int L_ROW = 7; constexpr int L_LEFT_COLUMN = 13; constexpr int L_RIGHT_COLUMN = 14; // The rectangle of walls around a room's floor: the grid's edges, or a narrow room's corridor. struct RailWalls { int bottom = 0; int columnEnd = 0; int columnStart = 0; int left = 0; int right = 0; int rowEnd = 0; int rowStart = 0; int top = 0; }; static bool is_narrow(RoomShape shape) { return shape == ROOMSHAPE_IH || shape == ROOMSHAPE_IV || shape == ROOMSHAPE_IIV || shape == ROOMSHAPE_IIH; } // 009e2830 Loads the rail sprite once, takes the mineshaft's or Ashpit's rails or the usual ones, clears the rails // and lines the room's walls with bumpers: above and below each column first, then left and right of each row, an L // room's walls stepping in around its missing quarter. // The backdrop it checks for Ashpit is still the last room's, as LoadBackdropGraphics comes later in Room::Init. void RailManager::init() { Room& room = *g_game->room; if (!sprite.isLoaded) { sprite.Load("gfx/grid/grid_rail.anm2", GRAPHICS_DEFER); sprite.Play(sprite.GetDefaultAnimationName()); } rails.fill(RAIL_NONE); if (g_game->HasAbandonedMineshaft() && g_game->currentDimensionIdx == 1) sprite.ReplaceSpritesheet(0, "gfx/grid/grid_rails_mineshaft.png"); else if (room.backdropType == BACKDROP_ASHPIT) sprite.ReplaceSpritesheet(0, "gfx/grid/grid_rails_ashpit.png"); else sprite.ReplaceSpritesheet(0, "gfx/grid/grid_rails.png"); sprite.LoadGraphics(); int width = room.gridWidth; int height = room.gridHeight; RoomShape shape = room.descriptor && room.descriptor->data ? room.descriptor->data->shape : ROOMSHAPE_NULL; RailWalls walls = {height - 1, width, 0, 0, width - 1, height - 1, 0, 0}; if (is_narrow(shape)) { LRoomTileDesc tiles = room.GetLRoomTileDesc(); walls = {tiles.bottomRightAY + 1, tiles.bottomRightAX + 1, tiles.topLeftAX - 1, tiles.topLeftAX - 1, tiles.bottomRightAX + 1, tiles.bottomRightAY + 1, tiles.topLeftAY - 1, tiles.topLeftAY - 1}; } for (int x = walls.columnStart; x < walls.columnEnd; x++) { int top = walls.top; int bottom = walls.bottom; if ((shape == ROOMSHAPE_LTL && x <= L_LEFT_COLUMN) || (shape == ROOMSHAPE_LTR && x > L_RIGHT_COLUMN)) top = L_ROW; if ((shape == ROOMSHAPE_LBL && x < L_LEFT_COLUMN) || (shape == ROOMSHAPE_LBR && x > L_RIGHT_COLUMN)) bottom = L_ROW + 1; rails[top * width + x] = RAIL_WALL_TOP; rails[bottom * width + x] = RAIL_WALL_BOTTOM; } for (int y = walls.rowStart; y < walls.rowEnd; y++) { int left = walls.left; int right = walls.right; if ((shape == ROOMSHAPE_LTL && y <= L_ROW) || (shape == ROOMSHAPE_LBL && y > L_ROW)) left = L_LEFT_COLUMN; if ((shape == ROOMSHAPE_LTR && y <= L_ROW) || (shape == ROOMSHAPE_LBR && y > L_ROW)) right = L_RIGHT_COLUMN; rails[y * width + left] = RAIL_WALL_LEFT; rails[y * width + right] = RAIL_WALL_RIGHT; } } // 009e2f20 Draws each cell's rail by its piece's frame, but under pits, offset on screen. void RailManager::Render(Vector offset) { Room& room = *g_game->room; sprite.SetFrame("rail", 0); for (int i = 0; i < room.gridHeight * room.gridWidth; i++) { uint8_t rail = rails[i]; GridEntity* entity = room.gridEntities[i]; if (rail & RAIL_HIDDEN || (entity && entity->type == GRID_PIT)) continue; if (sprite.animState.animData) { uint8_t piece = rail & (rail & RAIL_MINESHAFT ? RAIL_MINESHAFT_PIECE_MASK : RAIL_PIECE_MASK); sprite.animState.SetFrameFloat((float)piece); } Vector screen = render_position_get(room.GetGridPosition(i), RENDER_SNAPPED); sprite.Render({.position = vector_make(screen.x + offset.x, screen.y + offset.y)}); } } // 009e38a0 The point of a cell's rail nearest a minecart, for its rail following. void RailManager::get_closest_rail_point() { reconstruction_missing("RailManager::get_closest_rail_point 009e38a0"); }