Skip to content

Commit

Permalink
Lua: replace Game.gameID by synced callin GameID(string gameID)
Browse files Browse the repository at this point in the history
  • Loading branch information
rt committed May 7, 2012
1 parent beb2305 commit 0e3bafa
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 55 deletions.
1 change: 1 addition & 0 deletions cont/base/springcontent/LuaGadgets/callins.lua
Expand Up @@ -30,6 +30,7 @@ CallInsList = {
"GamePreload",
"GameStart",
"GameOver",
"GameID",
"TeamDied",

"UnitCreated",
Expand Down
8 changes: 8 additions & 0 deletions cont/base/springcontent/LuaGadgets/gadgets.lua
Expand Up @@ -90,6 +90,7 @@ local callInLists = {
"GamePreload",
"GameStart",
"GameOver",
"GameID",
"TeamDied",

"GameFrame",
Expand Down Expand Up @@ -1044,6 +1045,13 @@ function gadgetHandler:GameOver()
return
end

function gadgetHandler:GameID(gameID)
for _,g in ipairs(self.GameIDList) do
g:GameID(gameID)
end
return
end


function gadgetHandler:TeamDied(teamID)
for _,g in ipairs(self.TeamDiedList) do
Expand Down
4 changes: 3 additions & 1 deletion cont/examples/Widgets/minimap_startbox.lua
Expand Up @@ -300,7 +300,7 @@ function widget:DrawWorld()
-- show the team start positions
for _, teamID in ipairs(Spring.GetTeamList()) do
local _,leader = Spring.GetTeamInfo(teamID)
local _,_,spec = Spring.GetPlayerInfo(leader)
local name,_,spec = Spring.GetPlayerInfo(leader)
if ((not spec) and (teamID ~= gaiaTeamID)) then
local newx, newy, newz = Spring.GetTeamStartPosition(teamID)

Expand All @@ -315,6 +315,8 @@ function widget:DrawWorld()

if (newx ~= oldx or newy ~= oldy or newz ~= oldz) then
Spring.PlaySoundFile("MapPoint")
Spring.MarkerErasePosition(oldx, oldy, oldz)
Spring.MarkerAddPoint(newx, newy, newz, "Start " .. teamID .. " (" .. name .. ")", 1)
teamStartPositions[teamID][1] = newx
teamStartPositions[teamID][2] = newy
teamStartPositions[teamID][3] = newz
Expand Down
2 changes: 1 addition & 1 deletion rts/Game/NetCommands.cpp
Expand Up @@ -20,7 +20,6 @@
#include "ExternalAI/EngineOutHandler.h"
#include "ExternalAI/SkirmishAIHandler.h"
#include "Lua/LuaRules.h"
#include "Lua/LuaUI.h"
#include "UI/GameSetupDrawer.h"
#include "UI/MouseHandler.h"
#include "Rendering/GlobalRendering.h"
Expand Down Expand Up @@ -350,6 +349,7 @@ void CGame::ClientReadNet()
p[ 0], p[ 1], p[ 2], p[ 3], p[ 4], p[ 5], p[ 6], p[ 7],
p[ 8], p[ 9], p[10], p[11], p[12], p[13], p[14], p[15]);
AddTraffic(-1, packetCode, dataLength);
eventHandler.GameID(gameID, sizeof(gameID));
break;
}

Expand Down
87 changes: 40 additions & 47 deletions rts/Lua/LuaConstGame.cpp
Expand Up @@ -43,13 +43,8 @@ bool LuaConstGame::PushEntries(lua_State* L)
// FIXME -- this is getting silly, convert to userdata?

const float gravity = -(mapInfo->map.gravity * GAME_SPEED * GAME_SPEED);
const bool ghostedBuildings = gameSetup ? gameSetup->ghostedBuildings : false;
const int startPosType = gameSetup ? gameSetup->startPosType : 0;

// FIXME -- loaded too early - not set yet (another reason to use userdata)
lua_pushliteral(L, "gameID");
lua_pushlstring(L, (const char*)game->gameID, sizeof(game->gameID));
lua_rawset(L, -3);
const bool ghostedBuildings = gameSetup->ghostedBuildings;
const int startPosType = gameSetup->startPosType;

LuaPushNamedString(L, "version", SpringVersion::GetFull());

Expand All @@ -63,54 +58,52 @@ bool LuaConstGame::PushEntries(lua_State* L)

LuaPushNamedBool(L, "ghostedBuildings", ghostedBuildings);

const CMapInfo* mi = mapInfo;

LuaPushNamedBool(L, "mapDamage", !mapDamage->disabled);
LuaPushNamedNumber(L, "gravity", gravity);
LuaPushNamedNumber(L, "windMin", wind.GetMinWind());
LuaPushNamedNumber(L, "windMax", wind.GetMaxWind());
LuaPushNamedString(L, "mapName", mi->map.name);
LuaPushNamedString(L, "mapHumanName", mi->map.description); //! deprecated
LuaPushNamedString(L, "mapDescription", mi->map.description);
LuaPushNamedString(L, "mapName", mapInfo->map.name);
LuaPushNamedString(L, "mapHumanName", mapInfo->map.description); //! deprecated
LuaPushNamedString(L, "mapDescription", mapInfo->map.description);
LuaPushNamedNumber(L, "mapX", readmap->width / 64);
LuaPushNamedNumber(L, "mapY", readmap->height / 64);
LuaPushNamedNumber(L, "mapSizeX", readmap->width * SQUARE_SIZE);
LuaPushNamedNumber(L, "mapSizeZ", readmap->height * SQUARE_SIZE);
LuaPushNamedNumber(L, "extractorRadius", mi->map.extractorRadius);
LuaPushNamedNumber(L, "tidal", mi->map.tidalStrength);

LuaPushNamedNumber(L, "waterDamage", mi->water.damage);
LuaPushNamedString(L, "waterTexture", mi->water.texture);
LuaPushNamedNumber(L, "waterRepeatX", mi->water.repeatX);
LuaPushNamedNumber(L, "waterRepeatY", mi->water.repeatY);
LuaPushNamedString(L, "waterFoamTexture", mi->water.foamTexture);
LuaPushNamedString(L, "waterNormalTexture", mi->water.normalTexture);
LuaPushNamedNumber(L, "waterNumTiles", mi->water.numTiles);
LuaPushNamedBool(L, "waterVoid", mi->map.voidWater);
LuaPushNamedBool(L, "waterHasWaterPlane", mi->water.hasWaterPlane);
LuaPushNamedBool(L, "waterForceRendering", mi->water.forceRendering);
LuaPushNamedColor(L, "waterAbsorb", mi->water.absorb);
LuaPushNamedColor(L, "waterBaseColor", mi->water.baseColor);
LuaPushNamedColor(L, "waterMinColor", mi->water.minColor);
LuaPushNamedColor(L, "waterSurfaceColor", mi->water.surfaceColor);
LuaPushNamedNumber(L, "waterSurfaceAlpha", mi->water.surfaceAlpha);
LuaPushNamedColor(L, "waterDiffuseColor", mi->water.diffuseColor);
LuaPushNamedNumber(L, "waterDiffuseFactor", mi->water.diffuseFactor);
LuaPushNamedColor(L, "waterSpecularColor", mi->water.specularColor);
LuaPushNamedNumber(L, "waterSpecularFactor", mi->water.specularFactor);
LuaPushNamedNumber(L, "waterAmbientFactor", mi->water.ambientFactor);
LuaPushNamedColor(L, "waterPlaneColor", mi->water.planeColor);
LuaPushNamedNumber(L, "waterFresnelMin", mi->water.fresnelMin);
LuaPushNamedNumber(L, "waterFresnelMax", mi->water.fresnelMax);
LuaPushNamedNumber(L, "waterFresnelPower", mi->water.fresnelPower);
LuaPushNamedNumber(L, "waterReflectionDistortion", mi->water.reflDistortion);

LuaPushNamedColor(L, "fogColor", mi->atmosphere.fogColor);
LuaPushNamedColor(L, "groundAmbientColor", mi->light.groundAmbientColor);
LuaPushNamedColor(L, "groundSpecularColor", mi->light.groundSpecularColor);
LuaPushNamedColor(L, "groundSunColor", mi->light.groundSunColor);

const vector<string>& causticTexs = mi->water.causticTextures;
LuaPushNamedNumber(L, "extractorRadius", mapInfo->map.extractorRadius);
LuaPushNamedNumber(L, "tidal", mapInfo->map.tidalStrength);

LuaPushNamedNumber(L, "waterDamage", mapInfo->water.damage);
LuaPushNamedString(L, "waterTexture", mapInfo->water.texture);
LuaPushNamedNumber(L, "waterRepeatX", mapInfo->water.repeatX);
LuaPushNamedNumber(L, "waterRepeatY", mapInfo->water.repeatY);
LuaPushNamedString(L, "waterFoamTexture", mapInfo->water.foamTexture);
LuaPushNamedString(L, "waterNormalTexture", mapInfo->water.normalTexture);
LuaPushNamedNumber(L, "waterNumTiles", mapInfo->water.numTiles);
LuaPushNamedBool(L, "waterVoid", mapInfo->map.voidWater);
LuaPushNamedBool(L, "waterHasWaterPlane", mapInfo->water.hasWaterPlane);
LuaPushNamedBool(L, "waterForceRendering", mapInfo->water.forceRendering);
LuaPushNamedColor(L, "waterAbsorb", mapInfo->water.absorb);
LuaPushNamedColor(L, "waterBaseColor", mapInfo->water.baseColor);
LuaPushNamedColor(L, "waterMinColor", mapInfo->water.minColor);
LuaPushNamedColor(L, "waterSurfaceColor", mapInfo->water.surfaceColor);
LuaPushNamedNumber(L, "waterSurfaceAlpha", mapInfo->water.surfaceAlpha);
LuaPushNamedColor(L, "waterDiffuseColor", mapInfo->water.diffuseColor);
LuaPushNamedNumber(L, "waterDiffuseFactor", mapInfo->water.diffuseFactor);
LuaPushNamedColor(L, "waterSpecularColor", mapInfo->water.specularColor);
LuaPushNamedNumber(L, "waterSpecularFactor", mapInfo->water.specularFactor);
LuaPushNamedNumber(L, "waterAmbientFactor", mapInfo->water.ambientFactor);
LuaPushNamedColor(L, "waterPlaneColor", mapInfo->water.planeColor);
LuaPushNamedNumber(L, "waterFresnelMin", mapInfo->water.fresnelMin);
LuaPushNamedNumber(L, "waterFresnelMax", mapInfo->water.fresnelMax);
LuaPushNamedNumber(L, "waterFresnelPower", mapInfo->water.fresnelPower);
LuaPushNamedNumber(L, "waterReflectionDistortion", mapInfo->water.reflDistortion);

LuaPushNamedColor(L, "fogColor", mapInfo->atmosphere.fogColor);
LuaPushNamedColor(L, "groundAmbientColor", mapInfo->light.groundAmbientColor);
LuaPushNamedColor(L, "groundSpecularColor", mapInfo->light.groundSpecularColor);
LuaPushNamedColor(L, "groundSunColor", mapInfo->light.groundSunColor);

const vector<string>& causticTexs = mapInfo->water.causticTextures;
lua_pushliteral(L, "waterCausticTextures");
lua_newtable(L);
for (int i = 0; i < (int)causticTexs.size(); i++) {
Expand Down
23 changes: 22 additions & 1 deletion rts/Lua/LuaHandle.cpp
Expand Up @@ -626,7 +626,7 @@ void CLuaHandle::GameFrame(int frameNum)

LUA_FRAME_BATCH_PUSH(frameNum);
LUA_CALL_IN_CHECK(L);
if(CopyExportTable())
if (CopyExportTable())
DelayRecvFromSynced(L, 0); // Copy _G.EXPORT --> SYNCED.EXPORT once a game frame
lua_checkstack(L, 4);

Expand All @@ -646,6 +646,27 @@ void CLuaHandle::GameFrame(int frameNum)
}


void CLuaHandle::GameID(const unsigned char* gameID, unsigned int numBytes)
{
LUA_CALL_IN_CHECK(L);
lua_checkstack(L, 4);

const LuaHashString cmdStr("GameID");
const int errFunc = SetupTraceback(L);

if (!cmdStr.GetGlobalFunc(L)) {
if (errFunc != 0) {
lua_pop(L, 1);
}
return;
}

lua_pushlstring(L, reinterpret_cast<const char*>(gameID), numBytes);

RunCallInTraceback(cmdStr, 1, 0, errFunc);
}


void CLuaHandle::TeamDied(int teamID)
{
LUA_CALL_IN_CHECK(L);
Expand Down
7 changes: 4 additions & 3 deletions rts/Lua/LuaHandle.h
Expand Up @@ -126,8 +126,9 @@ class CLuaHandle : public CEventClient
CLuaDisplayLists& GetDisplayLists(const lua_State* L = NULL) { return GET_CONTEXT_DATA(displayLists); }

public: // call-ins
virtual bool HasCallIn(lua_State* L, const string& name) { return false; } // FIXME
bool WantsEvent(const string& name) { return HasCallIn(GetActiveState(), name); } // FIXME

virtual bool HasCallIn(lua_State* L, const string& name) { return false; } // FIXME
virtual bool SyncedUpdateCallIn(lua_State* L, const string& name) { return false; }
virtual bool UnsyncedUpdateCallIn(lua_State* L, const string& name) { return false; }

Expand All @@ -140,6 +141,8 @@ class CLuaHandle : public CEventClient
void GameOver(const std::vector<unsigned char>& winningAllyTeams);
void GamePaused(int playerID, bool paused);
void GameFrame(int frameNum);
void GameID(const unsigned char* gameID, unsigned int numBytes);

void TeamDied(int teamID);
void TeamChanged(int teamID);
void PlayerChanged(int playerID);
Expand Down Expand Up @@ -248,8 +251,6 @@ class CLuaHandle : public CEventClient
void DrawScreen();
void DrawInMiniMap();

/// @brief this UNSYNCED event is generated every gameProgressFrameInterval ( defined in gameserver.cpp ), skips network queuing and caching and it's useful
/// to calculate the current fast-forwarding % compared to the real game
void GameProgress(int frameNum);

public: // custom call-in (inter-script calls)
Expand Down
1 change: 1 addition & 0 deletions rts/System/EventClient.h
Expand Up @@ -80,6 +80,7 @@ class CEventClient
virtual void GameOver(const std::vector<unsigned char>& winningAllyTeams) {}
virtual void GamePaused(int playerID, bool paused) {}
virtual void GameFrame(int gameFrame) {}
virtual void GameID(const unsigned char* gameID, unsigned int numBytes) {}

virtual void TeamDied(int teamID) {}
virtual void TeamChanged(int teamID) {}
Expand Down
12 changes: 12 additions & 0 deletions rts/System/EventHandler.cpp
Expand Up @@ -46,6 +46,8 @@ CEventHandler::CEventHandler()
SETUP_EVENT(GameOver, MANAGED_BIT);
SETUP_EVENT(GamePaused, MANAGED_BIT);
SETUP_EVENT(GameFrame, MANAGED_BIT);
SETUP_EVENT(GameID, MANAGED_BIT);

SETUP_EVENT(TeamDied, MANAGED_BIT);
SETUP_EVENT(TeamChanged, MANAGED_BIT);
SETUP_EVENT(PlayerChanged, MANAGED_BIT);
Expand Down Expand Up @@ -388,6 +390,16 @@ void CEventHandler::GameFrame(int gameFrame)
}


void CEventHandler::GameID(const unsigned char* gameID, unsigned int numBytes)
{
const int count = listGameID.size();
for (int i = 0; i < count; i++) {
CEventClient* ec = listGameID[i];
ec->GameID(gameID, numBytes);
}
}


void CEventHandler::TeamDied(int teamID)
{
const int count = listTeamDied.size();
Expand Down
7 changes: 5 additions & 2 deletions rts/System/EventHandler.h
Expand Up @@ -47,6 +47,7 @@ class CEventHandler
void GameOver(const std::vector<unsigned char>& winningAllyTeams);
void GamePaused(int playerID, bool paused);
void GameFrame(int gameFrame);
void GameID(const unsigned char* gameID, unsigned int numBytes);

void TeamDied(int teamID);
void TeamChanged(int teamID);
Expand Down Expand Up @@ -192,8 +193,9 @@ class CEventHandler
void DrawScreen();
void DrawInMiniMap();

/// @brief this UNSYNCED event is generated every gameProgressFrameInterval ( defined in gameserver.cpp ), skips network queuing and caching and it's useful
/// to calculate the current fast-forwarding % compared to the real game
/// @brief this UNSYNCED event is generated every GameServer::gameProgressFrameInterval
/// it skips network queuing and caching and can be used to calculate the current catchup
/// percentage when reconnecting to a running game
void GameProgress(int gameFrame);
/// @}

Expand Down Expand Up @@ -250,6 +252,7 @@ class CEventHandler
EventClientList listGameOver;
EventClientList listGamePaused;
EventClientList listGameFrame;
EventClientList listGameID;
EventClientList listTeamDied;
EventClientList listTeamChanged;
EventClientList listPlayerChanged;
Expand Down

0 comments on commit 0e3bafa

Please sign in to comment.