Skip to content

Commit

Permalink
Add AllowUnitTransport callin
Browse files Browse the repository at this point in the history
  • Loading branch information
ashdnazg committed Jan 27, 2018
1 parent 03d7636 commit 9a97978
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions cont/base/springcontent/LuaGadgets/callins.lua
Expand Up @@ -99,6 +99,7 @@ CALLIN_LIST = {
"AllowUnitCreation",
"AllowUnitTransfer",
"AllowUnitBuildStep",
"AllowUnitTransport",
"AllowFeatureBuildStep",
"AllowFeatureCreation",
"AllowResourceLevel",
Expand Down
12 changes: 12 additions & 0 deletions cont/base/springcontent/LuaGadgets/gadgets.lua
Expand Up @@ -1137,6 +1137,18 @@ function gadgetHandler:AllowUnitBuildStep(builderID, builderTeam,
end


function gadgetHandler:AllowUnitTransport(transporterID, transporterUnitDefID, transporterTeam,
transporteeID, transporteeUnitDefID, transporteeTeam)
for _,g in r_ipairs(self.AllowUnitTransportList) do
if (not g:AllowUnitTransport(transporterID, transporterUnitDefID, transporterTeam,
transporteeID, transporteeUnitDefID, transporteeTeam)) then
return false
end
end
return true
end


function gadgetHandler:AllowFeatureBuildStep(builderID, builderTeam,
featureID, featureDefID, part)
for _,g in r_ipairs(self.AllowFeatureBuildStepList) do
Expand Down
3 changes: 3 additions & 0 deletions doc/changelog.txt
Expand Up @@ -61,6 +61,9 @@ Lua:
them available in `Spring.MoveCtrl.SetMoveDef` but beware the perf cost.
- new TextEditing(utf8, start, length) callin, which holds the IME editing composition (https://wiki.libsdl.org/Tutorials/TextInput)
- new SDLSetTextInputRect(x, y, w, h), SDLStartTextInput() and SDLStopTextInput() functions for controlling IME input from Lua
- Add AllowUnitTransport callin. Called when the engine tests whether unit A can transport unit B. Returning true will let
the engine decide, returning false will force the engine to disallow transportation (for this specific call).


AI:
- reveal unit's captureProgress, buildProgress and paralyzeDamage params through
Expand Down
28 changes: 28 additions & 0 deletions rts/Lua/LuaHandleSynced.cpp
Expand Up @@ -588,6 +588,34 @@ bool CSyncedLuaHandle::AllowUnitBuildStep(const CUnit* builder,
}


bool CSyncedLuaHandle::AllowUnitTransport(const CUnit* transporter,
const CUnit* transportee)
{
LUA_CALL_IN_CHECK(L, true);
luaL_checkstack(L, 8, __func__);

static const LuaHashString cmdStr(__func__);
if (!cmdStr.GetGlobalFunc(L))
return true; // the call is not defined

lua_pushnumber(L, transporter->id);
lua_pushnumber(L, transporter->unitDef->id);
lua_pushnumber(L, transporter->team);
lua_pushnumber(L, transportee->id);
lua_pushnumber(L, transportee->unitDef->id);
lua_pushnumber(L, transportee->team);

// call the function
if (!RunCallIn(L, cmdStr, 6, 1))
return true;

// get the results
const bool retval = luaL_optboolean(L, -1, true);
lua_pop(L, 1);
return retval;
}


bool CSyncedLuaHandle::AllowFeatureCreation(const FeatureDef* featureDef,
int teamID, const float3& pos)
{
Expand Down
1 change: 1 addition & 0 deletions rts/Lua/LuaHandleSynced.h
Expand Up @@ -58,6 +58,7 @@ class CSyncedLuaHandle : public CLuaHandle
bool AllowUnitCreation(const UnitDef* unitDef, const CUnit* builder, const BuildInfo* buildInfo);
bool AllowUnitTransfer(const CUnit* unit, int newTeam, bool capture);
bool AllowUnitBuildStep(const CUnit* builder, const CUnit* unit, float part);
bool AllowUnitTransport(const CUnit* transporter, const CUnit* transportee);
bool AllowFeatureCreation(const FeatureDef* featureDef, int allyTeamID, const float3& pos);
bool AllowFeatureBuildStep(const CUnit* builder, const CFeature* feature, float part);
bool AllowResourceLevel(int teamID, const string& type, float level);
Expand Down
4 changes: 4 additions & 0 deletions rts/Sim/Units/Unit.cpp
Expand Up @@ -2489,6 +2489,10 @@ bool CUnit::CanTransport(const CUnit* unit) const
return false;
if (unit->GetTransporter() != NULL)
return false;

if (!eventHandler.AllowUnitTransport(this, unit))
return false;

if (!unit->unitDef->transportByEnemy && !teamHandler->AlliedTeams(unit->team, team))
return false;
if (transportCapacityUsed >= unitDef->transportCapacity)
Expand Down
1 change: 1 addition & 0 deletions rts/System/EventClient.cpp
Expand Up @@ -50,6 +50,7 @@ bool CEventClient::AllowCommand(const CUnit* unit, const Command& cmd, bool from
bool CEventClient::AllowUnitCreation(const UnitDef* unitDef, const CUnit* builder, const BuildInfo* buildInfo) { return true; }
bool CEventClient::AllowUnitTransfer(const CUnit* unit, int newTeam, bool capture) { return true; }
bool CEventClient::AllowUnitBuildStep(const CUnit* builder, const CUnit* unit, float part) { return true; }
bool CEventClient::AllowUnitTransport(const CUnit* transporter, const CUnit* transportee) { return true; }
bool CEventClient::AllowFeatureCreation(const FeatureDef* featureDef, int allyTeamID, const float3& pos) { return true; }
bool CEventClient::AllowFeatureBuildStep(const CUnit* builder, const CFeature* feature, float part) { return true; }
bool CEventClient::AllowResourceLevel(int teamID, const string& type, float level) { return true; }
Expand Down
1 change: 1 addition & 0 deletions rts/System/EventClient.h
Expand Up @@ -198,6 +198,7 @@ class CEventClient
virtual bool AllowUnitCreation(const UnitDef* unitDef, const CUnit* builder, const BuildInfo* buildInfo);
virtual bool AllowUnitTransfer(const CUnit* unit, int newTeam, bool capture);
virtual bool AllowUnitBuildStep(const CUnit* builder, const CUnit* unit, float part);
virtual bool AllowUnitTransport(const CUnit* transporter, const CUnit* transportee);
virtual bool AllowFeatureCreation(const FeatureDef* featureDef, int allyTeamID, const float3& pos);
virtual bool AllowFeatureBuildStep(const CUnit* builder, const CFeature* feature, float part);
virtual bool AllowResourceLevel(int teamID, const string& type, float level);
Expand Down
6 changes: 6 additions & 0 deletions rts/System/EventHandler.cpp
Expand Up @@ -266,6 +266,12 @@ bool CEventHandler::AllowUnitBuildStep(const CUnit* builder, const CUnit* unit,
}


bool CEventHandler::AllowUnitTransport(const CUnit* transporter, const CUnit* transportee)
{
return ControlIterateDefTrue(listAllowUnitTransport, &CEventClient::AllowUnitTransport, transporter, transportee);
}


bool CEventHandler::AllowFeatureCreation(const FeatureDef* featureDef, int allyTeamID, const float3& pos)
{
return ControlIterateDefTrue(listAllowFeatureCreation, &CEventClient::AllowFeatureCreation, featureDef, allyTeamID, pos);
Expand Down
1 change: 1 addition & 0 deletions rts/System/EventHandler.h
Expand Up @@ -139,6 +139,7 @@ class CEventHandler
bool AllowUnitCreation(const UnitDef* unitDef, const CUnit* builder, const BuildInfo* buildInfo);
bool AllowUnitTransfer(const CUnit* unit, int newTeam, bool capture);
bool AllowUnitBuildStep(const CUnit* builder, const CUnit* unit, float part);
bool AllowUnitTransport(const CUnit* transporter, const CUnit* transportee);
bool AllowFeatureCreation(const FeatureDef* featureDef, int allyTeamID, const float3& pos);
bool AllowFeatureBuildStep(const CUnit* builder, const CFeature* feature, float part);
bool AllowResourceLevel(int teamID, const string& type, float level);
Expand Down
1 change: 1 addition & 0 deletions rts/System/Events.def
Expand Up @@ -164,6 +164,7 @@
SETUP_EVENT(AllowUnitCreation, MANAGED_BIT | CONTROL_BIT)
SETUP_EVENT(AllowUnitTransfer, MANAGED_BIT | CONTROL_BIT)
SETUP_EVENT(AllowUnitBuildStep, MANAGED_BIT | CONTROL_BIT)
SETUP_EVENT(AllowUnitTransport, MANAGED_BIT | CONTROL_BIT)
SETUP_EVENT(AllowFeatureCreation, MANAGED_BIT | CONTROL_BIT)
SETUP_EVENT(AllowFeatureBuildStep, MANAGED_BIT | CONTROL_BIT)
SETUP_EVENT(AllowResourceLevel, MANAGED_BIT | CONTROL_BIT)
Expand Down

0 comments on commit 9a97978

Please sign in to comment.