Skip to content

Commit

Permalink
Document some build square assumptions and expose build grid resoluti…
Browse files Browse the repository at this point in the history
…on to Lua. (#1865)
  • Loading branch information
sprunk authored Jan 8, 2025
1 parent 71a2a4d commit cec8d5c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
5 changes: 4 additions & 1 deletion rts/Game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,11 @@ void CGame::LoadMap(const std::string& mapFileName)
helper->Init();
readMap = CReadMap::LoadMap(mapFileName);

// half size; building positions are snapped to multiples of BUILD_SQUARE_SIZE
/* Uses half-size grid because it *incorrectly* assumes
* building positions are always snapped to the build grid. */
static_assert(BUILD_GRID_RESOLUTION == 2);
buildingMaskMap.Init(mapDims.hmapx * mapDims.hmapy);

groundBlockingObjectMap.Init(mapDims.mapSquares);
yardmapStatusEffectsMap.InitNewYardmapStatusEffectsMap();
}
Expand Down
7 changes: 7 additions & 0 deletions rts/Game/GameHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,8 @@ float3 CGameHelper::Pos2BuildPos(const BuildInfo& buildInfo, bool synced)
RECOIL_DETAILED_TRACY_ZONE;
float3 pos;

static_assert(BUILD_GRID_RESOLUTION == 2);

// snap build-positions to 16-elmo grid
if (buildInfo.GetXSize() & 2)
pos.x = math::floor((buildInfo.pos.x ) / BUILD_SQUARE_SIZE) * BUILD_SQUARE_SIZE + SQUARE_SIZE;
Expand Down Expand Up @@ -1125,6 +1127,11 @@ float3 CGameHelper::ClosestBuildPos(
const int xsize = bi.GetXSize();
const int zsize = bi.GetZSize();

/* I'm not sure which of these two the `2` below represents,
* maybe it even assumes both are the same? */
static_assert(BUILD_GRID_RESOLUTION == 2);
static_assert(SPRING_FOOTPRINT_SCALE == 2);

int xmin = std::max( 0, xsqr - (xsize ) / 2 - minDistance);
int zmin = std::max( 0, zsqr - (zsize ) / 2 - minDistance);
int xmax = std::min(mapDims.mapx, xsqr + (xsize + 1) / 2 + minDistance);
Expand Down
1 change: 1 addition & 0 deletions rts/Lua/LuaConstGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ bool LuaConstGame::PushEntries(lua_State* L)
LuaPushNamedNumber(L, "squareSize", SQUARE_SIZE);
LuaPushNamedNumber(L, "metalMapSquareSize", METAL_MAP_SQUARE_SIZE);
LuaPushNamedNumber(L, "buildSquareSize", BUILD_SQUARE_SIZE);
LuaPushNamedNumber(L, "buildGridResolution", BUILD_GRID_RESOLUTION);
LuaPushNamedNumber(L, "footprintScale", SPRING_FOOTPRINT_SCALE);
}

Expand Down
18 changes: 16 additions & 2 deletions rts/Sim/Misc/GlobalConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,22 @@ static constexpr int SPRING_FOOTPRINT_SCALE = 2;
*
* Defines the size of 1 heightmap square as 8 elmos.
*/
static constexpr int SQUARE_SIZE = 8;
static constexpr int BUILD_SQUARE_SIZE = SQUARE_SIZE * 2;
static constexpr int SQUARE_SIZE = 8;

/**
* @brief Building grid resolution
*
* Defines the resolution of the native build placement GUI, and maybe
* other related things.
*
* This probably makes the most sense to equal `SPRING_FOOTPRINT_SCALE`,
* but it doesn't have to be. It would be good to make it controllable
* by games at some point (modrule?), but there may be many assumptions
* elsewhere that this is equal to `SPRING_FOOTPRINT_SCALE` or to 2.
* Some are marked via static asserts but coverage is NOT comprehensive. */
static constexpr int BUILD_GRID_RESOLUTION = SPRING_FOOTPRINT_SCALE;

static constexpr int BUILD_SQUARE_SIZE = SQUARE_SIZE * BUILD_GRID_RESOLUTION;


/**
Expand Down

0 comments on commit cec8d5c

Please sign in to comment.