Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Spring.SetAllyTeamStartBox synced control #958

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ Lua:
matches in subdirs, recursively.
- `gadget:AllowUnitCreation` now accepts a second return value that controls whether to drop the order.
If set to false, the builder or factory will keep retrying. Defaults to true (drops order; current behavior).
- add `Spring.SetAllyTeamStartBox(allyTeamID, xMin, zMin, xMax, zMax) -> nil`, allowing to set
start box (in elmos) for an ally team.


Game Setup:
Expand Down
3 changes: 1 addition & 2 deletions rts/Game/UI/StartPosSelecter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ void CStartPosSelecter::DrawStartBox(GL::RenderDataBufferC* buffer, Shader::IPro
{
glAttribStatePtr->EnableDepthTest();

const std::vector<AllyTeam>& allyStartData = CGameSetup::GetAllyStartingData();
const AllyTeam& myStartData = allyStartData[gu->myAllyTeam];
const AllyTeam& myStartData = teamHandler.GetAllyTeam(gu->myAllyTeam);

const float by = myStartData.startRectTop * mapDims.mapy * SQUARE_SIZE;
const float bx = myStartData.startRectLeft * mapDims.mapx * SQUARE_SIZE;
Expand Down
33 changes: 33 additions & 0 deletions rts/Lua/LuaSyncedCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ bool LuaSyncedCtrl::PushEntries(lua_State* L)


REGISTER_LUA_CFUNC(SetAlly);
REGISTER_LUA_CFUNC(SetAllyTeamStartBox);
REGISTER_LUA_CFUNC(KillTeam);
REGISTER_LUA_CFUNC(AssignPlayerToTeam);
REGISTER_LUA_CFUNC(GameOver);
Expand Down Expand Up @@ -800,6 +801,38 @@ int LuaSyncedCtrl::SetAlly(lua_State* L)
}


/*** Changes the start box position of an allyTeam.
*
* @function Spring.SetAllyTeamStartBox
* @number allyTeamID
* @number xMin left start box boundary (elmos)
* @number zMin top start box boundary (elmos)
* @number xMax right start box boundary (elmos)
* @number zMax bottom start box boundary (elmos)
* @treturn nil
*/
int LuaSyncedCtrl::SetAllyTeamStartBox(lua_State* L)
{
const unsigned int allyTeamID = luaL_checkint(L, 1);
const float xMin = luaL_checkfloat(L, 2);
const float zMin = luaL_checkfloat(L, 3);
const float xMax = luaL_checkfloat(L, 4);
const float zMax = luaL_checkfloat(L, 5);

if (!teamHandler.IsValidAllyTeam(allyTeamID)) {
return 0;
}

nbusseneau marked this conversation as resolved.
Show resolved Hide resolved
const float startRectLeft = xMin / (mapDims.mapx * SQUARE_SIZE);
const float startRectTop = zMin / (mapDims.mapy * SQUARE_SIZE);
const float startRectRight = xMax / (mapDims.mapx * SQUARE_SIZE);
const float startRectBottom = zMax / (mapDims.mapy * SQUARE_SIZE);

teamHandler.SetAllyTeamStartBox(allyTeamID, startRectLeft, startRectTop, startRectRight, startRectBottom);
return 0;
}


/*** Assigns a player to a team.
*
* @function Spring.AssignPlayerToTeam
Expand Down
1 change: 1 addition & 0 deletions rts/Lua/LuaSyncedCtrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class LuaSyncedCtrl
private:
// all LuaHandleSynced
static int SetAlly(lua_State* L);
static int SetAllyTeamStartBox(lua_State* L);
static int KillTeam(lua_State* L);
static int AssignPlayerToTeam(lua_State* L);
static int GameOver(lua_State* L);
Expand Down
14 changes: 7 additions & 7 deletions rts/Lua/LuaSyncedRead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1462,16 +1462,16 @@ int LuaSyncedRead::GetGaiaTeamID(lua_State* L)
*/
int LuaSyncedRead::GetAllyTeamStartBox(lua_State* L)
{
const std::vector<AllyTeam>& allyData = CGameSetup::GetAllyStartingData();
const unsigned int allyTeam = luaL_checkint(L, 1);
const unsigned int allyTeamID = luaL_checkint(L, 1);

if (allyTeam >= allyData.size())
if (!teamHandler.IsValidAllyTeam(allyTeamID))
return 0;

const float xmin = (mapDims.mapx * SQUARE_SIZE) * allyData[allyTeam].startRectLeft;
const float zmin = (mapDims.mapy * SQUARE_SIZE) * allyData[allyTeam].startRectTop;
const float xmax = (mapDims.mapx * SQUARE_SIZE) * allyData[allyTeam].startRectRight;
const float zmax = (mapDims.mapy * SQUARE_SIZE) * allyData[allyTeam].startRectBottom;
const AllyTeam& allyTeam = teamHandler.GetAllyTeam(allyTeamID);
const float xmin = (mapDims.mapx * SQUARE_SIZE) * allyTeam.startRectLeft;
const float zmin = (mapDims.mapy * SQUARE_SIZE) * allyTeam.startRectTop;
const float xmax = (mapDims.mapx * SQUARE_SIZE) * allyTeam.startRectRight;
const float zmax = (mapDims.mapy * SQUARE_SIZE) * allyTeam.startRectBottom;

lua_pushnumber(L, xmin);
lua_pushnumber(L, zmin);
Expand Down
7 changes: 2 additions & 5 deletions rts/Sim/Misc/Team.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,10 @@ CTeam::CTeam():
void CTeam::SetDefaultStartPos()
{
const int allyTeam = teamHandler.AllyTeam(teamNum);
const std::vector<AllyTeam>& allyStartData = CGameSetup::GetAllyStartingData();

assert(!allyStartData.empty());
assert(allyTeam == teamAllyteam);

const AllyTeam& allyTeamData = allyStartData[allyTeam];
const AllyTeam& allyTeamData = teamHandler.GetAllyTeam(allyTeam);
// pick a spot near the center of our startbox
const float xmin = (mapDims.mapx * SQUARE_SIZE) * allyTeamData.startRectLeft;
const float zmin = (mapDims.mapy * SQUARE_SIZE) * allyTeamData.startRectTop;
Expand All @@ -102,8 +100,7 @@ void CTeam::SetDefaultStartPos()
void CTeam::ClampStartPosInStartBox(float3* pos) const
{
const int allyTeam = teamHandler.AllyTeam(teamNum);
const std::vector<AllyTeam>& allyStartData = CGameSetup::GetAllyStartingData();
const AllyTeam& allyTeamData = allyStartData[allyTeam];
const AllyTeam& allyTeamData = teamHandler.GetAllyTeam(allyTeam);
const SRectangle rect(
allyTeamData.startRectLeft * mapDims.mapx * SQUARE_SIZE,
allyTeamData.startRectTop * mapDims.mapy * SQUARE_SIZE,
Expand Down
17 changes: 17 additions & 0 deletions rts/Sim/Misc/TeamHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,23 @@ class CTeamHandler
*/
void SetAlly(int allyteamA, int allyteamB, bool allied) { allyTeams[allyteamA].allies[allyteamB] = allied; }

/**
* @brief set ally team start box
* @param allyteam ally team to edit
* @param startRectLeft left start box boundary (range: [0.0f, 1.0f])
* @param startRectTop top start box boundary (range: [0.0f, 1.0f])
* @param startRectRight right start box boundary (range: [0.0f, 1.0f])
* @param startRectBottom bottom start box boundary (range: [0.0f, 1.0f])
*
* Sets start box for an ally team
*/
void SetAllyTeamStartBox(int allyteam, float startRectLeft, float startRectTop, float startRectRight, float startRectBottom) {
allyTeams[allyteam].startRectLeft = std::clamp(startRectLeft, 0.0f, 1.0f);
allyTeams[allyteam].startRectTop = std::clamp(startRectTop, 0.0f, 1.0f);
allyTeams[allyteam].startRectRight = std::clamp(startRectRight, 0.0f, 1.0f);
allyTeams[allyteam].startRectBottom = std::clamp(startRectBottom, 0.0f, 1.0f);
}

// accessors
int GaiaTeamID() const { return gaiaTeamID; }
int GaiaAllyTeamID() const { return gaiaAllyTeamID; }
Expand Down