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 3, 2024
1 parent 19b0efc commit 548253e
Show file tree
Hide file tree
Showing 13 changed files with 364 additions and 20 deletions.
1 change: 1 addition & 0 deletions rts/Game/GameHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,7 @@ CGameHelper::BuildSquareStatus CGameHelper::TestUnitBuildSquare(
// const float buildHeight = GetBuildHeight(testPos, buildInfo.def, synced);
// const float modelHeight = (model != nullptr) ? math::fabs(model->height) : 10.0f;

// FIXME this call is probably useless, we can assume that any caller already assigned it correctly
sqrPos.y = GetBuildHeight(testPos, buildInfo.def, synced);

BuildSquareStatus testStatus = BUILDSQUARE_OPEN;
Expand Down
7 changes: 7 additions & 0 deletions rts/Game/UI/CommandColors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,17 @@ CCommandColors::CCommandColors()
SETUP_COLOR(rangeSelfDestruct, 0.8f, 0.1f, 0.1f, 0.7f);
SETUP_COLOR(rangeInterceptorOn, 1.0f, 1.0f, 1.0f, 0.7f);
SETUP_COLOR(rangeInterceptorOff, 0.0f, 0.0f, 0.0f, 0.7f);

SETUP_COLOR(unitBox, 0.0f, 1.0f, 0.0f, 0.9f);
SETUP_COLOR(buildBox, 0.0f, 1.0f, 0.0f, 0.9f);
SETUP_COLOR(allyBuildBox, 0.8f, 0.8f, 0.2f, 0.9f);
SETUP_COLOR(mouseBox, 1.0f, 1.0f, 1.0f, 0.9f);
SETUP_COLOR(buildableSquare, 0.0f, 0.9f, 0.0f, 0.7f);
SETUP_COLOR(unbuildableSquare, 0.9f, 0.8f, 0.0f, 0.7f);
SETUP_COLOR(featureSquare, 0.9f, 0.8f, 0.0f, 0.7f);
SETUP_COLOR(illegalSquare, 0.9f, 0.0f, 0.0f, 0.7f);
SETUP_COLOR(underwaterStart, 0.0f, 0.0f, 1.0f, 0.5f);
SETUP_COLOR(underwaterEnd, 0.0f, 0.5f, 1.0f, 1.0f);
}


Expand Down
12 changes: 12 additions & 0 deletions rts/Game/UI/CommandColors.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ class CCommandColors {
const float* buildBox;
const float* allyBuildBox;
const float* mouseBox;
const float* buildableSquare;
const float* unbuildableSquare;
const float* featureSquare;
const float* illegalSquare;
const float* underwaterStart;
const float* underwaterEnd;

// for command queue rendering
const float* start;
Expand Down Expand Up @@ -156,6 +162,12 @@ class CCommandColors {
buildBoxIndex,
allyBuildBoxIndex,
mouseBoxIndex,
buildableSquareIndex,
unbuildableSquareIndex,
featureSquareIndex,
illegalSquareIndex,
underwaterStartIndex,
underwaterEndIndex,

ColorCount
};
Expand Down
89 changes: 89 additions & 0 deletions rts/Lua/LuaUnsyncedCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ bool LuaUnsyncedCtrl::PushEntries(lua_State* L)
REGISTER_LUA_CFUNC(AddWorldUnit);

REGISTER_LUA_CFUNC(DrawUnitCommands);
REGISTER_LUA_CFUNC(AddBuildSquare);
REGISTER_LUA_CFUNC(RemoveBuildSquare);

REGISTER_LUA_CFUNC(SetTeamColor);

Expand Down Expand Up @@ -3382,6 +3384,93 @@ int LuaUnsyncedCtrl::SetBuildFacing(lua_State* L)
}


// FIXME: move to LuaUtils, but currently it produces error
// "error: undefined reference to 'BuildInfo::BuildInfo(int, float3 const&, int)'"
static BuildInfo ParseBuildInfo(
lua_State* L,
const char* caller,
int idx
) {
int unitDefID = luaL_checkinteger(L, idx);
float3 pos = {
luaL_checkfloat(L, idx+1),
luaL_checkfloat(L, idx+2),
luaL_checkfloat(L, idx+3),
};
int facing = luaL_checkinteger(L, idx+4);

return BuildInfo(unitDefID, pos, facing);
}


static LuaBuildSquareOptions ParseBuildSquareOptions(
lua_State* L,
const char* caller,
const int idx
) {
LuaBuildSquareOptions opts;
if (lua_istable(L, idx)) {
for (lua_pushnil(L); lua_next(L, idx) != 0; lua_pop(L, 1)) {
// "key" = value (table format of CommandNotify)
// ignore the "coded" key; not a boolean value
if (lua_israwstring(L, -2)) {
if (lua_isboolean(L, -1)) {
const bool value = lua_toboolean(L, -1);
switch (hashString(lua_tostring(L, -2))) {
case hashString("unbuildable"):
opts.unbuildable = value;
break;
}
}
}
}
} else if (!lua_isnone(L, idx) && !lua_isnil(L, idx)) {
luaL_error(L, "%s(): bad options-argument type", caller);
}
return opts;
}


/***
*
* @function Spring.AddBuildSquare
* @number unitDefID
* @number x
* @number y
* @number z
* @number facing
* @bool[opt] unbuildable
* @treturn nil
*/
int LuaUnsyncedCtrl::AddBuildSquare(lua_State* L)
{
BuildInfo buildInfo = ParseBuildInfo(L, __func__, 1);
LuaBuildSquareOptions opts = ParseBuildSquareOptions(L, __func__, 6);

unitDrawer->AddLuaBuildSquare(buildInfo, opts);
return 0;
}


/***
*
* @function Spring.RemoveBuildSquare
* @number unitDefID
* @number x
* @number y
* @number z
* @number facing
* @treturn nil
*/
int LuaUnsyncedCtrl::RemoveBuildSquare(lua_State* L)
{
BuildInfo buildInfo = ParseBuildInfo(L, __func__, 1);

unitDrawer->RemoveLuaBuildSquare(buildInfo);
return 0;
}


/******************************************************************************
* UI
* @section ui
Expand Down
2 changes: 2 additions & 0 deletions rts/Lua/LuaUnsyncedCtrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class LuaUnsyncedCtrl {
static int AddWorldUnit(lua_State* L);

static int DrawUnitCommands(lua_State* L);
static int AddBuildSquare(lua_State* L);
static int RemoveBuildSquare(lua_State* L);

static int SetTeamColor(lua_State* L);

Expand Down
1 change: 1 addition & 0 deletions rts/Rendering/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ set(sources_engine_Rendering
"${CMAKE_CURRENT_SOURCE_DIR}/Features/FeatureDrawerData.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Features/FeatureDrawer.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Units/UnitDrawerData.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Units/UnitDrawerCache.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Units/UnitDrawer.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/ModelsDataUploader.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/UniformConstants.cpp"
Expand Down
Loading

0 comments on commit 548253e

Please sign in to comment.