diff --git a/source/glest_game/game/script_manager.cpp b/source/glest_game/game/script_manager.cpp index be852d094..c9ca1bdeb 100644 --- a/source/glest_game/game/script_manager.cpp +++ b/source/glest_game/game/script_manager.cpp @@ -23,6 +23,25 @@ using namespace Shared::Util; namespace Glest{ namespace Game{ +// +// This class wraps streflop for the Lua ScriptMAnager. We need to toggle the data type +// for streflop to use when calling into glest from LUA as streflop may corrupt some +// numeric values passed from Lua otherwise +// +class ScriptManager_STREFLOP_Wrapper { +public: + ScriptManager_STREFLOP_Wrapper() { +#ifdef USE_STREFLOP + streflop_init(); +#endif + } + ~ScriptManager_STREFLOP_Wrapper() { +#ifdef USE_STREFLOP + streflop_init(); +#endif + } +}; + // ===================================================== // class PlayerModifiers // ===================================================== @@ -131,6 +150,7 @@ void ScriptManager::onUnitDied(const Unit* unit){ // ========================== lua wrappers =============================================== string ScriptManager::wrapString(const string &str, int wrapCount){ + ScriptManager_STREFLOP_Wrapper streflopWrapper; string returnString; @@ -151,6 +171,8 @@ string ScriptManager::wrapString(const string &str, int wrapCount){ } void ScriptManager::showMessage(const string &text, const string &header){ + ScriptManager_STREFLOP_Wrapper streflopWrapper; + Lang &lang= Lang::getInstance(); messageQueue.push(ScriptManagerMessage(text, header)); @@ -164,88 +186,108 @@ void ScriptManager::clearDisplayText(){ } void ScriptManager::setDisplayText(const string &text){ + ScriptManager_STREFLOP_Wrapper streflopWrapper; displayText= wrapString(Lang::getInstance().getScenarioString(text), displayTextWrapCount); } void ScriptManager::setCameraPosition(const Vec2i &pos){ + ScriptManager_STREFLOP_Wrapper streflopWrapper; gameCamera->centerXZ(pos.x, pos.y); } void ScriptManager::createUnit(const string &unitName, int factionIndex, Vec2i pos){ - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] unit [%s] factionIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,unitName.c_str(),factionIndex); + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] unit [%s] factionIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,unitName.c_str(),factionIndex); + ScriptManager_STREFLOP_Wrapper streflopWrapper; world->createUnit(unitName, factionIndex, pos); } void ScriptManager::giveResource(const string &resourceName, int factionIndex, int amount){ + ScriptManager_STREFLOP_Wrapper streflopWrapper; world->giveResource(resourceName, factionIndex, amount); } void ScriptManager::givePositionCommand(int unitId, const string &commandName, const Vec2i &pos){ + ScriptManager_STREFLOP_Wrapper streflopWrapper; world->givePositionCommand(unitId, commandName, pos); } void ScriptManager::giveProductionCommand(int unitId, const string &producedName){ + ScriptManager_STREFLOP_Wrapper streflopWrapper; world->giveProductionCommand(unitId, producedName); } void ScriptManager::giveUpgradeCommand(int unitId, const string &producedName){ + ScriptManager_STREFLOP_Wrapper streflopWrapper; world->giveUpgradeCommand(unitId, producedName); } void ScriptManager::disableAi(int factionIndex){ + ScriptManager_STREFLOP_Wrapper streflopWrapper; if(factionIndexgetStartLocation(factionIndex); } Vec2i ScriptManager::getUnitPosition(int unitId){ + ScriptManager_STREFLOP_Wrapper streflopWrapper; return world->getUnitPosition(unitId); } int ScriptManager::getUnitFaction(int unitId){ + ScriptManager_STREFLOP_Wrapper streflopWrapper; return world->getUnitFactionIndex(unitId); } int ScriptManager::getResourceAmount(const string &resourceName, int factionIndex){ + ScriptManager_STREFLOP_Wrapper streflopWrapper; return world->getResourceAmount(resourceName, factionIndex); } const string &ScriptManager::getLastCreatedUnitName(){ + ScriptManager_STREFLOP_Wrapper streflopWrapper; return lastCreatedUnitName; } int ScriptManager::getLastCreatedUnitId(){ + ScriptManager_STREFLOP_Wrapper streflopWrapper; return lastCreatedUnitId; } const string &ScriptManager::getLastDeadUnitName(){ + ScriptManager_STREFLOP_Wrapper streflopWrapper; return lastDeadUnitName; } int ScriptManager::getLastDeadUnitId(){ + ScriptManager_STREFLOP_Wrapper streflopWrapper; return lastDeadUnitId; } int ScriptManager::getUnitCount(int factionIndex){ + ScriptManager_STREFLOP_Wrapper streflopWrapper; return world->getUnitCount(factionIndex); } int ScriptManager::getUnitCountOfType(int factionIndex, const string &typeName){ + ScriptManager_STREFLOP_Wrapper streflopWrapper; return world->getUnitCountOfType(factionIndex, typeName); } diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index a8fa395e7..8076ed52f 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -24,10 +24,7 @@ #include "opengl.h" #include "faction.h" #include "factory_repository.h" - -#ifndef WIN32 - #include -#endif +#include #include "leak_dumper.h" @@ -541,8 +538,8 @@ void Renderer::renderMouse2d(int x, int y, int anim, float fade){ anim= anim*2-maxMouse2dAnim; - color2= (abs(anim*fadeFactor)/static_cast(maxMouse2dAnim))/2.f+0.4f; - color1= (abs(anim*fadeFactor)/static_cast(maxMouse2dAnim))/2.f+0.8f; + color2= (abs(anim*(int)fadeFactor)/static_cast(maxMouse2dAnim))/2.f+0.4f; + color1= (abs(anim*(int)fadeFactor)/static_cast(maxMouse2dAnim))/2.f+0.8f; glPushAttrib(GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT | GL_LINE_BIT); glEnable(GL_BLEND); diff --git a/source/glest_game/main/program.cpp b/source/glest_game/main/program.cpp index 345fb7469..dafd2ea5e 100644 --- a/source/glest_game/main/program.cpp +++ b/source/glest_game/main/program.cpp @@ -121,7 +121,6 @@ Program::Program() { programState= NULL; singleton = this; soundThreadManager = NULL; - loopThreadManager = NULL; } void Program::initNormal(WindowGl *window){ @@ -167,10 +166,6 @@ Program::~Program(){ BaseThread::shutdownAndWait(soundThreadManager); delete soundThreadManager; soundThreadManager = NULL; - - BaseThread::shutdownAndWait(loopThreadManager); - delete loopThreadManager; - loopThreadManager = NULL; } void Program::keyDown(char key){ @@ -191,12 +186,7 @@ void Program::simpleTask() { } void Program::loop() { - if(loopThreadManager == NULL) { - loopWorker(); - } - else { - loopThreadManager->setTaskSignalled(true); - } + loopWorker(); } void Program::loopWorker() { @@ -407,11 +397,6 @@ void Program::init(WindowGl *window, bool initSound, bool toggleFullScreen){ } } - BaseThread::shutdownAndWait(loopThreadManager); - delete loopThreadManager; - //loopThreadManager = new SimpleTaskThread(this,0,5,true); - //loopThreadManager->start(); - NetworkInterface::setAllowGameDataSynchCheck(Config::getInstance().getBool("AllowGameDataSynchCheck","false")); NetworkInterface::setAllowDownloadDataSynch(Config::getInstance().getBool("AllowDownloadDataSynch","false")); diff --git a/source/glest_game/main/program.h b/source/glest_game/main/program.h index a83df1e1b..7acee1689 100644 --- a/source/glest_game/main/program.h +++ b/source/glest_game/main/program.h @@ -110,7 +110,6 @@ private: WindowGl *window; static Program *singleton; - SimpleTaskThread *loopThreadManager; public: Program(); diff --git a/source/glest_game/network/network_interface.cpp b/source/glest_game/network/network_interface.cpp index a97b73652..7e2e2008d 100644 --- a/source/glest_game/network/network_interface.cpp +++ b/source/glest_game/network/network_interface.cpp @@ -121,17 +121,18 @@ GameNetworkInterface::GameNetworkInterface(){ } void GameNetworkInterface::requestCommand(const NetworkCommand *networkCommand, bool insertAtStart) { - Mutex *mutex = getServerSynchAccessor(); + assert(networkCommand != NULL); + //Mutex *mutex = getServerSynchAccessor(); if(insertAtStart == false) { - if(mutex != NULL) mutex->p(); + //if(mutex != NULL) mutex->p(); requestedCommands.push_back(*networkCommand); - if(mutex != NULL) mutex->v(); + //if(mutex != NULL) mutex->v(); } else { - if(mutex != NULL) mutex->p(); + //if(mutex != NULL) mutex->p(); requestedCommands.insert(requestedCommands.begin(),*networkCommand); - if(mutex != NULL) mutex->v(); + //if(mutex != NULL) mutex->v(); } } diff --git a/source/glest_game/network/server_interface.cpp b/source/glest_game/network/server_interface.cpp index c5734f077..bf8483c93 100644 --- a/source/glest_game/network/server_interface.cpp +++ b/source/glest_game/network/server_interface.cpp @@ -41,7 +41,6 @@ ServerInterface::ServerInterface(){ for(int i= 0; istart(); - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] END\n",__FILE__,__FUNCTION__); } @@ -108,12 +100,6 @@ bool ServerInterface::switchSlot(int fromPlayerIndex,int toPlayerIndex){ } void ServerInterface::removeSlot(int playerIndex) { - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d playerIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,playerIndex); - - //BaseThread::shutdownAndWait(slotThreads[playerIndex]); - //delete slotThreads[playerIndex]; - //slotThreads[playerIndex] = NULL; - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d playerIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,playerIndex); delete slots[playerIndex]; @@ -650,7 +636,7 @@ bool ServerInterface::launchGame(const GameSettings* gameSettings){ SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); - serverSynchAccessor.p(); + //serverSynchAccessor.p(); for(int i= 0; iisConnected() == false) - { + for(int i= 0; iisConnected() == false); + serverSynchAccessor.v(); + + if(isSlotOpen == true) { ++openSlotCount; } } + serverSynchAccessor.p(); serverSocket.listen(openSlotCount); - serverSynchAccessor.v(); } int ServerInterface::getOpenSlotCount() { int openSlotCount= 0; - serverSynchAccessor.p(); + for(int i= 0; iisConnected() == false); + serverSynchAccessor.v(); - for(int i= 0; iisConnected() == false) - { + if(isSlotOpen == true) { ++openSlotCount; } } - serverSynchAccessor.v(); - return openSlotCount; } diff --git a/source/glest_game/type_instances/unit.cpp b/source/glest_game/type_instances/unit.cpp index 1594b5133..4d4dc6ad2 100644 --- a/source/glest_game/type_instances/unit.cpp +++ b/source/glest_game/type_instances/unit.cpp @@ -23,13 +23,7 @@ #include "skill_type.h" #include "core_data.h" #include "renderer.h" - #include "game.h" - -#ifndef WIN32 - #include -#endif - #include "socket.h" #include "leak_dumper.h" diff --git a/source/shared_lib/include/graphics/math_util.h b/source/shared_lib/include/graphics/math_util.h index 414e9f255..dd0ba92dc 100644 --- a/source/shared_lib/include/graphics/math_util.h +++ b/source/shared_lib/include/graphics/math_util.h @@ -12,8 +12,6 @@ #ifndef _SHARED_GRAPHICS_MATHUTIL_H_ #define _SHARED_GRAPHICS_MATHUTIL_H_ -//#include - #include "vec.h" namespace Shared{ namespace Graphics{ diff --git a/source/shared_lib/include/graphics/matrix.h b/source/shared_lib/include/graphics/matrix.h index 3048cb2f8..2f934571b 100644 --- a/source/shared_lib/include/graphics/matrix.h +++ b/source/shared_lib/include/graphics/matrix.h @@ -12,8 +12,6 @@ #ifndef _SHARED_GRAPHICS_MATRIX_H_ #define _SHARED_GRAPHICS_MATRIX_H_ -//#include - #include "vec.h" namespace Shared{ namespace Graphics{ diff --git a/source/shared_lib/sources/lua/lua_script.cpp b/source/shared_lib/sources/lua/lua_script.cpp index 97e3be859..da808a81d 100644 --- a/source/shared_lib/sources/lua/lua_script.cpp +++ b/source/shared_lib/sources/lua/lua_script.cpp @@ -22,11 +22,32 @@ using namespace Shared::Util; namespace Shared{ namespace Lua{ +// +// This class wraps streflop for LuaScript. We need to toggle the data type +// for streflop to use when calling into LUA as streflop may corrupt some +// numeric values passed from Lua otherwise +// +class Lua_STREFLOP_Wrapper { +public: + Lua_STREFLOP_Wrapper() { +#ifdef USE_STREFLOP + streflop_init(); +#endif + } + ~Lua_STREFLOP_Wrapper() { +#ifdef USE_STREFLOP + streflop_init(); +#endif + } +}; + // ===================================================== // class LuaScript // ===================================================== -LuaScript::LuaScript(){ +LuaScript::LuaScript() { + Lua_STREFLOP_Wrapper streflopWrapper; + luaState= luaL_newstate(); luaL_openlibs(luaState); @@ -38,73 +59,51 @@ LuaScript::LuaScript(){ argumentCount= -1; } -LuaScript::~LuaScript(){ +LuaScript::~LuaScript() { + Lua_STREFLOP_Wrapper streflopWrapper; + lua_close(luaState); } void LuaScript::loadCode(const string &code, const string &name){ - -#ifdef USE_STREFLOP - streflop_init(); -#endif + Lua_STREFLOP_Wrapper streflopWrapper; int errorCode= luaL_loadbuffer(luaState, code.c_str(), code.size(), name.c_str()); if(errorCode!=0){ -#ifdef USE_STREFLOP - streflop_init(); -#endif throw runtime_error("Error loading lua code: " + errorToString(errorCode)); } //run code errorCode= lua_pcall(luaState, 0, 0, 0)!=0; if(errorCode!=0){ -#ifdef USE_STREFLOP - streflop_init(); -#endif throw runtime_error("Error initializing lua: " + errorToString(errorCode)); } -#ifdef USE_STREFLOP - streflop_init(); -#endif } void LuaScript::beginCall(const string& functionName){ -#ifdef USE_STREFLOP - streflop_init(); -#endif + Lua_STREFLOP_Wrapper streflopWrapper; + lua_getglobal(luaState, functionName.c_str()); argumentCount= 0; -#ifdef USE_STREFLOP - streflop_init(); -#endif } void LuaScript::endCall(){ -#ifdef USE_STREFLOP - streflop_init(); -#endif + Lua_STREFLOP_Wrapper streflopWrapper; + lua_pcall(luaState, argumentCount, 0, 0); -#ifdef USE_STREFLOP - streflop_init(); -#endif } void LuaScript::registerFunction(LuaFunction luaFunction, const string &functionName){ -#ifdef USE_STREFLOP - streflop_init(); -#endif - lua_pushcfunction(luaState, luaFunction); + Lua_STREFLOP_Wrapper streflopWrapper; + + lua_pushcfunction(luaState, luaFunction); lua_setglobal(luaState, functionName.c_str()); -#ifdef USE_STREFLOP - streflop_init(); -#endif } -string LuaScript::errorToString(int errorCode){ +string LuaScript::errorToString(int errorCode) { + Lua_STREFLOP_Wrapper streflopWrapper; string error; - switch(errorCode){ case LUA_ERRSYNTAX: error+= "Syntax error"; @@ -132,34 +131,25 @@ string LuaScript::errorToString(int errorCode){ // ===================================================== LuaArguments::LuaArguments(lua_State *luaState){ -#ifdef USE_STREFLOP - streflop_init(); -#endif + Lua_STREFLOP_Wrapper streflopWrapper; + this->luaState= luaState; returnCount= 0; -#ifdef USE_STREFLOP - streflop_init(); -#endif } int LuaArguments::getInt(int argumentIndex) const{ -#ifdef USE_STREFLOP - streflop_init(); -#endif - if(!lua_isnumber(luaState, argumentIndex)){ -#ifdef USE_STREFLOP - streflop_init(); -#endif + Lua_STREFLOP_Wrapper streflopWrapper; + + if(!lua_isnumber(luaState, argumentIndex)) { throwLuaError("Can not get int from Lua state"); } int result = luaL_checkint(luaState, argumentIndex); -#ifdef USE_STREFLOP - streflop_init(); -#endif return result; } string LuaArguments::getString(int argumentIndex) const{ + Lua_STREFLOP_Wrapper streflopWrapper; + if(!lua_isstring(luaState, argumentIndex)){ throwLuaError("Can not get string from Lua state"); } @@ -167,22 +157,15 @@ string LuaArguments::getString(int argumentIndex) const{ } Vec2i LuaArguments::getVec2i(int argumentIndex) const{ -#ifdef USE_STREFLOP - streflop_init(); -#endif + Lua_STREFLOP_Wrapper streflopWrapper; + Vec2i v; if(!lua_istable(luaState, argumentIndex)){ -#ifdef USE_STREFLOP - streflop_init(); -#endif throwLuaError("Can not get vec2i from Lua state, value on the stack is not a table"); } if(luaL_getn(luaState, argumentIndex)!=2){ -#ifdef USE_STREFLOP - streflop_init(); -#endif throwLuaError("Can not get vec2i from Lua state, array size not 2"); } @@ -194,32 +177,26 @@ Vec2i LuaArguments::getVec2i(int argumentIndex) const{ v.y= luaL_checkint(luaState, argumentIndex); lua_pop(luaState, 1); -#ifdef USE_STREFLOP - streflop_init(); -#endif return v; } void LuaArguments::returnInt(int value){ -#ifdef USE_STREFLOP - streflop_init(); -#endif + Lua_STREFLOP_Wrapper streflopWrapper; + ++returnCount; lua_pushinteger(luaState, value); -#ifdef USE_STREFLOP - streflop_init(); -#endif } void LuaArguments::returnString(const string &value){ + Lua_STREFLOP_Wrapper streflopWrapper; + ++returnCount; lua_pushstring(luaState, value.c_str()); } void LuaArguments::returnVec2i(const Vec2i &value){ -#ifdef USE_STREFLOP - streflop_init(); -#endif + Lua_STREFLOP_Wrapper streflopWrapper; + ++returnCount; lua_newtable(luaState); @@ -229,15 +206,11 @@ void LuaArguments::returnVec2i(const Vec2i &value){ lua_pushnumber(luaState, value.y); lua_rawseti(luaState, -2, 2); -#ifdef USE_STREFLOP - streflop_init(); -#endif } void LuaArguments::throwLuaError(const string &message) const{ -#ifdef USE_STREFLOP - streflop_init(); -#endif + Lua_STREFLOP_Wrapper streflopWrapper; + string stackString; int stackSize = lua_gettop(luaState); @@ -260,9 +233,6 @@ void LuaArguments::throwLuaError(const string &message) const{ stackString+= "\n"; } -#ifdef USE_STREFLOP - streflop_init(); -#endif throw runtime_error("Lua error: " + message + "\n\nLua Stack:\n" + stackString); }