Skip to content

Commit

Permalink
Feature: register new lua functions AddBuildSquare and `RemoveBuild…
Browse files Browse the repository at this point in the history
…Square` to show build squares on custom building commands
  • Loading branch information
Aurelien Lambert committed Nov 29, 2024
1 parent c8482c2 commit 0f2c725
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
32 changes: 29 additions & 3 deletions rts/Lua/LuaUnsyncedCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3556,12 +3556,25 @@ static BuildInfo ParseBuildInfo(
luaL_checkfloat(L, idx+2),
luaL_checkfloat(L, idx+3),
};
int facing = luaL_checkinteger(L, idx+4);
int facing = LuaUtils::ParseFacing(L, __func__, idx+4);

return BuildInfo(unitDefID, pos, facing);
BuildInfo buildInfo = BuildInfo(unitDefID, pos, facing);
if (buildInfo.def == nullptr) {
luaL_error(L, "%s(): invalid unitDefID", caller);
}
return buildInfo;
}


/*** Build Square Options params
*
* @table buildSquareOptions
*
* @bool[opt] unbuildable Show the building as unbuildable
* @number[opt] cacheValidity How long to keep in cache after removal for faster reuse (in seconds)
*/


static LuaBuildSquareOptions ParseBuildSquareOptions(
lua_State* L,
const char* caller,
Expand Down Expand Up @@ -3596,12 +3609,20 @@ static LuaBuildSquareOptions ParseBuildSquareOptions(
* @number y
* @number z
* @number facing
* @bool[opt] unbuildable
* @tparam[opt] buildSquareOptions options
* @treturn nil
*
* x and z are assumed to be valid building positions
* y is assumed to be Spring.GroundHeight(x, z)
* Unit build squares are cached using (unitDefID, x, z, facing) key
* Passing a different y will force to compute the squares again
*
*/
int LuaUnsyncedCtrl::AddUnitBuildSquare(lua_State* L)
{
BuildInfo buildInfo = ParseBuildInfo(L, __func__, 1);
if (buildInfo.def == nullptr) return 0;

LuaBuildSquareOptions opts = ParseBuildSquareOptions(L, __func__, 6);

unitDrawer->AddLuaBuildSquare(buildInfo, opts);
Expand All @@ -3618,10 +3639,15 @@ int LuaUnsyncedCtrl::AddUnitBuildSquare(lua_State* L)
* @number z
* @number facing
* @treturn nil
*
* Unit build squares are cached using (unitDefID, x, z, facing) key
* y is ignored when removing unit build squares
*
*/
int LuaUnsyncedCtrl::RemoveUnitBuildSquare(lua_State* L)
{
BuildInfo buildInfo = ParseBuildInfo(L, __func__, 1);
if (buildInfo.def == nullptr) return 0;

unitDrawer->RemoveLuaBuildSquare(buildInfo);
return 0;
Expand Down
6 changes: 4 additions & 2 deletions rts/Rendering/Units/UnitDrawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1357,8 +1357,9 @@ void CUnitDrawerGLSL::RemoveLuaBuildSquare(const BuildInfo& buildInfo) {

LuaBuildSquareTaskKey buildKey(buildInfo);
auto it = luaBuildSquareTasks.find(buildKey);
if (it == luaBuildSquareTasks.end()) return;

LuaBuildSquareTask& task = it->second;

if (task.cacheUntil < 0) {
// nothing computed yet, no point keeping it in cache
luaBuildSquareTasks.erase(it);
Expand Down Expand Up @@ -1402,7 +1403,8 @@ void CUnitDrawerGLSL::ShowLuaBuildSquare() {
for (auto it = luaBuildSquareTasks.begin(); it != luaBuildSquareTasks.end(); ) {
LuaBuildSquareTask& task = it->second;
// should be uncached
if (task.cacheUntil > gs->frameNum) {
if (task.cacheUntil > 0 && task.cacheUntil < gs->frameNum) {

it = luaBuildSquareTasks.erase(it);
continue;
}
Expand Down

0 comments on commit 0f2c725

Please sign in to comment.