From e351def1cb92bdf6e64a2c42a31ea33deb0619b9 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Thu, 5 Jan 2012 00:45:17 +0000 Subject: [PATCH] - added new lua function to display playername: getPlayerName(i) --- source/glest_game/game/script_manager.cpp | 13 ++++++ source/glest_game/game/script_manager.h | 2 + source/glest_game/world/scenario.cpp | 55 +++++++++++++++++++++++ source/glest_game/world/scenario.h | 2 + source/glest_game/world/world.cpp | 4 ++ source/glest_game/world/world.h | 1 + 6 files changed, 77 insertions(+) diff --git a/source/glest_game/game/script_manager.cpp b/source/glest_game/game/script_manager.cpp index a45bb94d..3dd32f59 100644 --- a/source/glest_game/game/script_manager.cpp +++ b/source/glest_game/game/script_manager.cpp @@ -176,6 +176,7 @@ void ScriptManager::init(World* world, GameCamera *gameCamera){ luaScript.registerFunction(getGameWon, "gameWon"); luaScript.registerFunction(getSystemMacroValue, "getSystemMacroValue"); + luaScript.registerFunction(getPlayerName, "getPlayerName"); luaScript.registerFunction(loadScenario, "loadScenario"); @@ -1032,6 +1033,12 @@ const string ScriptManager::getSystemMacroValue(const string &key) { return world->getSystemMacroValue(key); } +const string ScriptManager::getPlayerName(int factionIndex) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + ScriptManager_STREFLOP_Wrapper streflopWrapper; + return world->getPlayerName(factionIndex); +} + void ScriptManager::loadScenario(const string &name, bool keepFactions) { //printf("[%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); @@ -1484,6 +1491,12 @@ int ScriptManager::getSystemMacroValue(LuaHandle* luaHandle) { return luaArguments.getReturnCount(); } +int ScriptManager::getPlayerName(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + luaArguments.returnString(thisScriptManager->getPlayerName(luaArguments.getInt(-1))); + return luaArguments.getReturnCount(); +} + int ScriptManager::getUnitCount(LuaHandle* luaHandle){ LuaArguments luaArguments(luaHandle); luaArguments.returnInt(thisScriptManager->getUnitCount(luaArguments.getInt(-1))); diff --git a/source/glest_game/game/script_manager.h b/source/glest_game/game/script_manager.h index 7bd8cc4d..ce5e3919 100644 --- a/source/glest_game/game/script_manager.h +++ b/source/glest_game/game/script_manager.h @@ -272,6 +272,7 @@ private: bool getGameWon(); const string getSystemMacroValue(const string &key); + const string getPlayerName(int factionIndex); void loadScenario(const string &name, bool keepFactions); @@ -359,6 +360,7 @@ private: static int getGameWon(LuaHandle* luaHandle); static int getSystemMacroValue(LuaHandle* luaHandle); + static int getPlayerName(LuaHandle* luaHandle); static int loadScenario(LuaHandle* luaHandle); }; diff --git a/source/glest_game/world/scenario.cpp b/source/glest_game/world/scenario.cpp index 615cd987..226003a4 100644 --- a/source/glest_game/world/scenario.cpp +++ b/source/glest_game/world/scenario.cpp @@ -21,6 +21,7 @@ #include "platform_common.h" #include "properties.h" #include "lang.h" +#include "socket.h" #include "config.h" #include "leak_dumper.h" @@ -299,6 +300,51 @@ ControlType Scenario::strToControllerType(const string &str) { throw std::runtime_error(szBuf); } +string Scenario::controllerTypeToStr(const ControlType &ct) { + string controlString = ""; + + Lang &lang= Lang::getInstance(); + switch(ct) { + case ctCpuEasy: + controlString= lang.get("CpuEasy"); + break; + case ctCpu: + controlString= lang.get("Cpu"); + break; + case ctCpuUltra: + controlString= lang.get("CpuUltra"); + break; + case ctCpuMega: + controlString= lang.get("CpuMega"); + break; + case ctNetwork: + controlString= lang.get("Network"); + break; + case ctHuman: + controlString= lang.get("Human"); + break; + + case ctNetworkCpuEasy: + controlString= lang.get("NetworkCpuEasy"); + break; + case ctNetworkCpu: + controlString= lang.get("NetworkCpu"); + break; + case ctNetworkCpuUltra: + controlString= lang.get("NetworkCpuUltra"); + break; + case ctNetworkCpuMega: + controlString= lang.get("NetworkCpuMega"); + break; + + default: + printf("Error control = %d\n",ct); + //assert(false); + } + + return controlString; +} + void Scenario::loadGameSettings(const vector &dirList, const ScenarioInfo *scenarioInfo, GameSettings *gameSettings, string scenarioDescription) { @@ -318,6 +364,15 @@ void Scenario::loadGameSettings(const vector &dirList, if(ct != ctClosed) { if(ct == ctHuman) { gameSettings->setThisFactionIndex(factionCount); + + if(gameSettings->getNetworkPlayerName(i) == "") { + gameSettings->setNetworkPlayerName(i,Config::getInstance().getString("NetPlayerName",Socket::getHostName().c_str())); + } + } + else { + if(gameSettings->getNetworkPlayerName(i) == "") { + gameSettings->setNetworkPlayerName(i,controllerTypeToStr(ct)); + } } gameSettings->setFactionControl(factionCount, ct); gameSettings->setResourceMultiplierIndex(factionCount, (scenarioInfo->resourceMultipliers[i]-0.5f)/0.1f); diff --git a/source/glest_game/world/scenario.h b/source/glest_game/world/scenario.h index 6081a35d..f75143f6 100644 --- a/source/glest_game/world/scenario.h +++ b/source/glest_game/world/scenario.h @@ -108,6 +108,8 @@ public: static void loadScenarioInfo(string file, ScenarioInfo *scenarioInfo); static ControlType strToControllerType(const string &str); + static string controllerTypeToStr(const ControlType &ct); + static void loadGameSettings(const vector &dirList, const ScenarioInfo *scenarioInfo, GameSettings *gameSettings, string scenarioDescription); private: diff --git a/source/glest_game/world/world.cpp b/source/glest_game/world/world.cpp index 39fbfc87..81099661 100644 --- a/source/glest_game/world/world.cpp +++ b/source/glest_game/world/world.cpp @@ -1100,6 +1100,10 @@ const string World::getSystemMacroValue(const string key) { return result; } +const string World::getPlayerName(int factionIndex) { + return game->getGameSettings()->getNetworkPlayerName(factionIndex); +} + void World::giveUpgradeCommand(int unitId, const string &upgradeName) { Unit *unit= findUnitById(unitId); if(unit != NULL) { diff --git a/source/glest_game/world/world.h b/source/glest_game/world/world.h index bd409f67..21019739 100644 --- a/source/glest_game/world/world.h +++ b/source/glest_game/world/world.h @@ -240,6 +240,7 @@ public: int getUnitCountOfType(int factionIndex, const string &typeName); const string getSystemMacroValue(const string key); + const string getPlayerName(int factionIndex); Game * getGame() { return game; } const GameSettings * getGameSettings() const;