From aa69a5818bc03c15592ed2fc1d1f0dd06e5bec3c Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Sat, 25 May 2013 20:46:36 +0000 Subject: [PATCH] added some new lua methods to determine day or night and new event --- source/glest_game/game/game.cpp | 4 +- source/glest_game/game/script_manager.cpp | 95 ++++++++++++++++++++ source/glest_game/game/script_manager.h | 18 +++- source/glest_game/graphics/renderer.cpp | 14 +-- source/glest_game/world/world.cpp | 1 + source/shared_lib/include/lua/lua_script.h | 1 + source/shared_lib/sources/lua/lua_script.cpp | 7 ++ 7 files changed, 132 insertions(+), 8 deletions(-) diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index 25376e52..ce8e0d14 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -5158,12 +5158,12 @@ void Game::render2d() { if(Renderer::renderText3DEnabled == true) { renderer.renderText3D( scriptManager.getDisplayText(), coreData.getMenuFontNormal3D(), - Vec3f(fontColor.x,fontColor.y,fontColor.z), 200, 680, false); + Vec3f(fontColor.x,fontColor.y,fontColor.z), 200, 660, false); } else { renderer.renderText( scriptManager.getDisplayText(), coreData.getMenuFontNormal(), - Vec3f(fontColor.x,fontColor.y,fontColor.z), 200, 680, false); + Vec3f(fontColor.x,fontColor.y,fontColor.z), 200, 660, false); } } diff --git a/source/glest_game/game/script_manager.cpp b/source/glest_game/game/script_manager.cpp index be81d98a..9931cf9e 100644 --- a/source/glest_game/game/script_manager.cpp +++ b/source/glest_game/game/script_manager.cpp @@ -212,6 +212,8 @@ ScriptManager::ScriptManager() { rootNode = NULL; currentCellTriggeredEventAreaEntryUnitId = 0; currentCellTriggeredEventAreaExitUnitId = 0; + lastDayNightTriggerStatus = 0; + registeredDayNightEvent = false; } ScriptManager::~ScriptManager() { @@ -381,6 +383,12 @@ void ScriptManager::init(World* world, GameCamera *gameCamera, const XmlNode *ro luaScript.registerFunction(setAttackWarningsEnabled, "setAttackWarningsEnabled"); luaScript.registerFunction(getAttackWarningsEnabled, "getAttackWarningsEnabled"); + luaScript.registerFunction(getIsDayTime, "getIsDayTime"); + luaScript.registerFunction(getIsNightTime, "getIsNightTime"); + luaScript.registerFunction(getTimeOfDay, "getTimeOfDay"); + luaScript.registerFunction(registerDayNightEvent, "registerDayNightEvent"); + luaScript.registerFunction(unregisterDayNightEvent, "unregisterDayNightEvent"); + //load code for(int i= 0; igetScriptCount(); ++i){ const Script* script= scenario->getScript(i); @@ -1784,6 +1792,66 @@ bool ScriptManager::getAttackWarningsEnabled() { return world->getAttackWarningsEnabled(); } + +void ScriptManager::registerDayNightEvent() { + registeredDayNightEvent = true; +} + +void ScriptManager::unregisterDayNightEvent() { + registeredDayNightEvent = false; +} + +void ScriptManager::onDayNightTriggerEvent() { + if(registeredDayNightEvent == true) { + bool isDay = this->getIsDayTime(); + if((lastDayNightTriggerStatus != 1 && isDay == true) || + (lastDayNightTriggerStatus != 2 && isDay == false)) { + if(isDay == true) { + lastDayNightTriggerStatus = 1; + } + else { + lastDayNightTriggerStatus = 2; + } + + printf("Triggering daynight event isDay: %d [%f]\n",isDay,getTimeOfDay()); + + luaScript.beginCall("dayNightTriggerEvent"); + luaScript.endCall(); + } + } +} + +int ScriptManager::getIsDayTime() { + if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + ScriptManager_STREFLOP_Wrapper streflopWrapper; + + const TimeFlow *tf= world->getTimeFlow(); + if(tf == NULL) { + throw megaglest_runtime_error("tf == NULL"); + } + return tf->isDay(); +} +int ScriptManager::getIsNightTime() { + if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + ScriptManager_STREFLOP_Wrapper streflopWrapper; + + const TimeFlow *tf= world->getTimeFlow(); + if(tf == NULL) { + throw megaglest_runtime_error("tf == NULL"); + } + return tf->isNight(); +} +float ScriptManager::getTimeOfDay() { + if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + ScriptManager_STREFLOP_Wrapper streflopWrapper; + + const TimeFlow *tf= world->getTimeFlow(); + if(tf == NULL) { + throw megaglest_runtime_error("tf == NULL"); + } + return tf->getTime(); +} + // ========================== lua callbacks =============================================== int ScriptManager::showMessage(LuaHandle* luaHandle){ @@ -2822,6 +2890,33 @@ int ScriptManager::getAttackWarningsEnabled(LuaHandle* luaHandle) { return luaArguments.getReturnCount(); } +int ScriptManager::getIsDayTime(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + luaArguments.returnInt(thisScriptManager->getIsDayTime()); + return luaArguments.getReturnCount(); +} +int ScriptManager::getIsNightTime(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + luaArguments.returnInt(thisScriptManager->getIsNightTime()); + return luaArguments.getReturnCount(); +} +int ScriptManager::getTimeOfDay(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + luaArguments.returnFloat(thisScriptManager->getTimeOfDay()); + return luaArguments.getReturnCount(); +} + +int ScriptManager::registerDayNightEvent(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + thisScriptManager->registerDayNightEvent(); + return luaArguments.getReturnCount(); +} +int ScriptManager::unregisterDayNightEvent(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + thisScriptManager->unregisterDayNightEvent(); + return luaArguments.getReturnCount(); +} + void ScriptManager::saveGame(XmlNode *rootNode) { std::map mapTagReplacements; XmlNode *scriptManagerNode = rootNode->addChild("ScriptManager"); diff --git a/source/glest_game/game/script_manager.h b/source/glest_game/game/script_manager.h index bab73e23..37a18162 100644 --- a/source/glest_game/game/script_manager.h +++ b/source/glest_game/game/script_manager.h @@ -28,7 +28,6 @@ #include "leak_dumper.h" using std::string; -//using std::queue; using std::list; using Shared::Graphics::Vec2i; using Shared::Lua::LuaScript; @@ -198,6 +197,9 @@ private: bool inCellTriggerEvent; std::vector unRegisterCellTriggerEventList; + bool registeredDayNightEvent; + int lastDayNightTriggerStatus; + RandomGen random; const XmlNode *rootNode; @@ -228,8 +230,10 @@ public: void onUnitAttacked(const Unit* unit); void onUnitAttacking(const Unit* unit); void onGameOver(bool won); + void onCellTriggerEvent(Unit *movingUnit); void onTimerTriggerEvent(); + void onDayNightTriggerEvent(); bool getGameWon() const; bool getIsGameOver() const; @@ -381,6 +385,12 @@ private: void setAttackWarningsEnabled(bool enabled); bool getAttackWarningsEnabled(); + int getIsDayTime(); + int getIsNightTime(); + float getTimeOfDay(); + void registerDayNightEvent(); + void unregisterDayNightEvent(); + //callbacks, commands static int networkShowMessageForFaction(LuaHandle* luaHandle); static int networkShowMessageForTeam(LuaHandle* luaHandle); @@ -528,6 +538,12 @@ private: static int setAttackWarningsEnabled(LuaHandle* luaHandle); static int getAttackWarningsEnabled(LuaHandle* luaHandle); + static int getIsDayTime(LuaHandle* luaHandle); + static int getIsNightTime(LuaHandle* luaHandle); + static int getTimeOfDay(LuaHandle* luaHandle); + static int registerDayNightEvent(LuaHandle* luaHandle); + static int unregisterDayNightEvent(LuaHandle* luaHandle); + }; }}//end namespace diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index b268faa9..35dc0c28 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -2424,7 +2424,7 @@ void Renderer::renderResourceStatus() { glPushAttrib(GL_ENABLE_BIT); - int j= 0; + int resourceCountRendered = 0; for(int i= 0; i < world->getTechTree()->getResourceTypeCount(); ++i) { const ResourceType *rt = world->getTechTree()->getResourceType(i); const Resource *r = thisFaction->getResource(rt); @@ -2475,7 +2475,11 @@ void Renderer::renderResourceStatus() { if(isNegativeConsumableDisplayCycle == false) { glColor3f(1.f, 1.f, 1.f); } - renderQuad(j*100+200, metrics.getVirtualH()-30, 16, 16, rt->getImage()); + const int MAX_RESOURCES_PER_ROW = 6; + int resourceRow = (resourceCountRendered > 0 ? resourceCountRendered / MAX_RESOURCES_PER_ROW : 0); + int resourceCol = resourceCountRendered % MAX_RESOURCES_PER_ROW; + //renderQuad(resourceCountRendered*100+200, metrics.getVirtualH()-30 - (30 * resourceRows), 16, 16, rt->getImage()); + renderQuad(resourceCol * 100 + 200, metrics.getVirtualH()-30 - (30 * resourceRow), 16, 16, rt->getImage()); if(rt->getClass() != rcStatic) { str+= "/" + intToStr(thisFaction->getStoreAmount(rt)); @@ -2494,15 +2498,15 @@ void Renderer::renderResourceStatus() { renderTextShadow3D( str, CoreData::getInstance().getDisplayFontSmall3D(), resourceFontColor, - j*100+220, metrics.getVirtualH()-30, false); + resourceCol * 100 + 220, metrics.getVirtualH()-30 - (30 * resourceRow), false); } else { renderTextShadow( str, CoreData::getInstance().getDisplayFontSmall(), resourceFontColor, - j*100+220, metrics.getVirtualH()-30, false); + resourceCol * 100 + 220, metrics.getVirtualH()-30 - (30 * resourceRow), false); } - ++j; + ++resourceCountRendered; } } diff --git a/source/glest_game/world/world.cpp b/source/glest_game/world/world.cpp index f1f19816..d0fada1c 100644 --- a/source/glest_game/world/world.cpp +++ b/source/glest_game/world/world.cpp @@ -754,6 +754,7 @@ void World::update() { //time timeFlow.update(); + scriptManager->onDayNightTriggerEvent(); if(showPerfStats) { sprintf(perfBuf,"In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chronoPerf.getMillis()); diff --git a/source/shared_lib/include/lua/lua_script.h b/source/shared_lib/include/lua/lua_script.h index 7735d6ce..e0f38ab5 100644 --- a/source/shared_lib/include/lua/lua_script.h +++ b/source/shared_lib/include/lua/lua_script.h @@ -100,6 +100,7 @@ public: int getReturnCount() const {return returnCount;} void returnInt(int value); + void returnFloat(float value); void returnString(const string &value); void returnVec2i(const Vec2i &value); void returnVec4i(const Vec4i &value); diff --git a/source/shared_lib/sources/lua/lua_script.cpp b/source/shared_lib/sources/lua/lua_script.cpp index 6e96bd47..d1ecc90e 100644 --- a/source/shared_lib/sources/lua/lua_script.cpp +++ b/source/shared_lib/sources/lua/lua_script.cpp @@ -930,6 +930,13 @@ void LuaArguments::returnInt(int value){ lua_pushinteger(luaState, value); } +void LuaArguments::returnFloat(float value){ + Lua_STREFLOP_Wrapper streflopWrapper; + + ++returnCount; + lua_pushnumber(luaState, value); +} + void LuaArguments::returnString(const string &value){ Lua_STREFLOP_Wrapper streflopWrapper;