diff --git a/source/glest_game/game/script_manager.cpp b/source/glest_game/game/script_manager.cpp index 40692344e..d86af5715 100644 --- a/source/glest_game/game/script_manager.cpp +++ b/source/glest_game/game/script_manager.cpp @@ -133,6 +133,9 @@ void CellTriggerEvent::saveGame(XmlNode *rootNode) { cellTriggerEventNode->addAttribute("destPos",destPos.getString(), mapTagReplacements); // int triggerCount; cellTriggerEventNode->addAttribute("triggerCount",intToStr(triggerCount), mapTagReplacements); + +// Vec2i destPosEnd; + cellTriggerEventNode->addAttribute("destPosEnd",destPosEnd.getString(), mapTagReplacements); } void CellTriggerEvent::loadGame(const XmlNode *rootNode) { @@ -143,6 +146,10 @@ void CellTriggerEvent::loadGame(const XmlNode *rootNode) { destId = cellTriggerEventNode->getAttribute("destId")->getIntValue(); destPos = Vec2i::strToVec2(cellTriggerEventNode->getAttribute("destPos")->getValue()); triggerCount = cellTriggerEventNode->getAttribute("triggerCount")->getIntValue(); + + if(cellTriggerEventNode->hasAttribute("destPosEnd") == true) { + destPosEnd = Vec2i::strToVec2(cellTriggerEventNode->getAttribute("destPosEnd")->getValue()); + } } TimerTriggerEvent::TimerTriggerEvent() { @@ -184,9 +191,9 @@ void TimerTriggerEvent::loadGame(const XmlNode *rootNode) { // ===================================================== // class ScriptManager // ===================================================== -ScriptManager* ScriptManager::thisScriptManager= NULL; -const int ScriptManager::messageWrapCount= 30; -const int ScriptManager::displayTextWrapCount= 64; +ScriptManager* ScriptManager::thisScriptManager = NULL; +const int ScriptManager::messageWrapCount = 30; +const int ScriptManager::displayTextWrapCount = 64; ScriptManager::ScriptManager() { world = NULL; @@ -278,6 +285,10 @@ void ScriptManager::init(World* world, GameCamera *gameCamera, const XmlNode *ro luaScript.registerFunction(registerCellTriggerEventForUnitToLocation, "registerCellTriggerEventForUnitToLocation"); luaScript.registerFunction(registerCellTriggerEventForFactionToUnit, "registerCellTriggerEventForFactionToUnit"); luaScript.registerFunction(registerCellTriggerEventForFactionToLocation, "registerCellTriggerEventForFactionToLocation"); + + luaScript.registerFunction(registerCellAreaTriggerEventForUnitToLocation, "registerCellAreaTriggerEventForUnitToLocation"); + luaScript.registerFunction(registerCellAreaTriggerEventForFactionToLocation, "registerCellAreaTriggerEventForFactionToLocation"); + luaScript.registerFunction(getCellTriggerEventCount, "getCellTriggerEventCount"); luaScript.registerFunction(unregisterCellTriggerEvent, "unregisterCellTriggerEvent"); luaScript.registerFunction(startTimerEvent, "startTimerEvent"); @@ -595,6 +606,26 @@ void ScriptManager::onCellTriggerEvent(Unit *movingUnit) { } break; + case ctet_UnitAreaPos: + { + if(movingUnit->getId() == event.sourceId) { + bool srcInDst = false; + for(int x = event.destPos.x; srcInDst == false && x <= event.destPosEnd.x; ++x) { + for(int y = event.destPos.y; srcInDst == false && y <= event.destPosEnd.y; ++y) { + srcInDst = world->getMap()->isInUnitTypeCells(movingUnit->getType(), Vec2i(x,y),movingUnit->getPos()); + if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] movingUnit = %d, event.type = %d, movingUnit->getPos() = %s, event.sourceId = %d, event.destId = %d, event.destPos = %s, srcInDst = %d\n", + __FILE__,__FUNCTION__,__LINE__,movingUnit->getId(),event.type,movingUnit->getPos().getString().c_str(),event.sourceId,event.destId,Vec2i(x,y).getString().c_str(),srcInDst); + } + } + + if(srcInDst == true) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + } + triggerEvent = srcInDst; + } + } + break; + case ctet_Faction: { Unit *destUnit = world->findUnitById(event.destId); @@ -631,6 +662,26 @@ void ScriptManager::onCellTriggerEvent(Unit *movingUnit) { } break; + case ctet_FactionAreaPos: + { + if(movingUnit->getFactionIndex() == event.sourceId) { + //printf("ctet_FactionPos event.destPos = [%s], movingUnit->getPos() [%s]\n",event.destPos.getString().c_str(),movingUnit->getPos().getString().c_str()); + + bool srcInDst = false; + for(int x = event.destPos.x; srcInDst == false && x <= event.destPosEnd.x; ++x) { + for(int y = event.destPos.y; srcInDst == false && y <= event.destPosEnd.y; ++y) { + + srcInDst = world->getMap()->isInUnitTypeCells(movingUnit->getType(), Vec2i(x,y),movingUnit->getPos()); + if(srcInDst == true) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + } + } + } + triggerEvent = srcInDst; + } + } + break; + } if(triggerEvent == true) { @@ -958,6 +1009,23 @@ int ScriptManager::registerCellTriggerEventForUnitToLocation(int sourceUnitId, c return eventId; } +int ScriptManager::registerCellAreaTriggerEventForUnitToLocation(int sourceUnitId, const Vec4i &pos) { + CellTriggerEvent trigger; + trigger.type = ctet_UnitAreaPos; + trigger.sourceId = sourceUnitId; + trigger.destPos.x = pos.x; + trigger.destPos.y = pos.y; + trigger.destPosEnd.x = pos.z; + trigger.destPosEnd.y = pos.w; + + int eventId = currentEventId++; + CellTriggerEventList[eventId] = trigger; + + if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] Unit: %d will trigger cell event when reaching pos: %s, eventId = %d\n",__FILE__,__FUNCTION__,__LINE__,sourceUnitId,pos.getString().c_str(),eventId); + + return eventId; +} + int ScriptManager::registerCellTriggerEventForFactionToUnit(int sourceFactionId, int destUnitId) { CellTriggerEvent trigger; trigger.type = ctet_Faction; @@ -986,6 +1054,23 @@ int ScriptManager::registerCellTriggerEventForFactionToLocation(int sourceFactio return eventId; } +int ScriptManager::registerCellAreaTriggerEventForFactionToLocation(int sourceFactionId, const Vec4i &pos) { + CellTriggerEvent trigger; + trigger.type = ctet_FactionAreaPos; + trigger.sourceId = sourceFactionId; + trigger.destPos.x = pos.x; + trigger.destPos.y = pos.y; + trigger.destPosEnd.x = pos.z; + trigger.destPosEnd.y = pos.w; + + int eventId = currentEventId++; + CellTriggerEventList[eventId] = trigger; + + if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]Faction: %d will trigger cell event when reaching pos: %s, eventId = %d\n",__FILE__,__FUNCTION__,__LINE__,sourceFactionId,pos.getString().c_str(),eventId); + + return eventId; +} + int ScriptManager::getCellTriggerEventCount(int eventId) { int result = 0; if(CellTriggerEventList.find(eventId) != CellTriggerEventList.end()) { @@ -1563,6 +1648,13 @@ int ScriptManager::registerCellTriggerEventForUnitToLocation(LuaHandle* luaHandl return luaArguments.getReturnCount(); } +int ScriptManager::registerCellAreaTriggerEventForUnitToLocation(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + int result = thisScriptManager->registerCellAreaTriggerEventForUnitToLocation(luaArguments.getInt(-2),luaArguments.getVec4i(-1)); + luaArguments.returnInt(result); + return luaArguments.getReturnCount(); +} + int ScriptManager::registerCellTriggerEventForFactionToUnit(LuaHandle* luaHandle) { LuaArguments luaArguments(luaHandle); int result = thisScriptManager->registerCellTriggerEventForFactionToUnit(luaArguments.getInt(-2),luaArguments.getInt(-1)); @@ -1577,6 +1669,13 @@ int ScriptManager::registerCellTriggerEventForFactionToLocation(LuaHandle* luaHa return luaArguments.getReturnCount(); } +int ScriptManager::registerCellAreaTriggerEventForFactionToLocation(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + int result = thisScriptManager->registerCellAreaTriggerEventForFactionToLocation(luaArguments.getInt(-2),luaArguments.getVec4i(-1)); + luaArguments.returnInt(result); + return luaArguments.getReturnCount(); +} + int ScriptManager::getCellTriggerEventCount(LuaHandle* luaHandle) { LuaArguments luaArguments(luaHandle); int result = thisScriptManager->getCellTriggerEventCount(luaArguments.getInt(-1)); diff --git a/source/glest_game/game/script_manager.h b/source/glest_game/game/script_manager.h index df59ebbcb..8d0d78eae 100644 --- a/source/glest_game/game/script_manager.h +++ b/source/glest_game/game/script_manager.h @@ -100,7 +100,9 @@ enum CellTriggerEventType { ctet_Unit, ctet_UnitPos, ctet_Faction, - ctet_FactionPos + ctet_FactionPos, + ctet_UnitAreaPos, + ctet_FactionAreaPos }; class CellTriggerEvent { @@ -110,6 +112,7 @@ public: int sourceId; int destId; Vec2i destPos; + Vec2i destPosEnd; int triggerCount; @@ -268,6 +271,10 @@ private: int registerCellTriggerEventForUnitToLocation(int sourceUnitId, const Vec2i &pos); int registerCellTriggerEventForFactionToUnit(int sourceFactionId, int destUnitId); int registerCellTriggerEventForFactionToLocation(int sourceFactionId, const Vec2i &pos); + + int registerCellAreaTriggerEventForUnitToLocation(int sourceUnitId, const Vec4i &pos); + int registerCellAreaTriggerEventForFactionToLocation(int sourceFactionId, const Vec4i &pos); + int getCellTriggerEventCount(int eventId); void unregisterCellTriggerEvent(int eventId); int startTimerEvent(); @@ -372,6 +379,10 @@ private: static int registerCellTriggerEventForUnitToLocation(LuaHandle* luaHandle); static int registerCellTriggerEventForFactionToUnit(LuaHandle* luaHandle); static int registerCellTriggerEventForFactionToLocation(LuaHandle* luaHandle); + + static int registerCellAreaTriggerEventForUnitToLocation(LuaHandle* luaHandle); + static int registerCellAreaTriggerEventForFactionToLocation(LuaHandle* luaHandle); + static int getCellTriggerEventCount(LuaHandle* luaHandle); static int unregisterCellTriggerEvent(LuaHandle* luaHandle); static int startTimerEvent(LuaHandle* luaHandle); diff --git a/source/shared_lib/include/lua/lua_script.h b/source/shared_lib/include/lua/lua_script.h index e75c6e722..f6228ea75 100644 --- a/source/shared_lib/include/lua/lua_script.h +++ b/source/shared_lib/include/lua/lua_script.h @@ -14,16 +14,17 @@ #include #include -#include +#include "vec.h" #include "xml_parser.h" #include "leak_dumper.h" using std::string; using Shared::Graphics::Vec2i; +using Shared::Graphics::Vec4i; using Shared::Xml::XmlNode; -namespace Shared{ namespace Lua{ +namespace Shared { namespace Lua { typedef lua_State LuaHandle; typedef int(*LuaFunction)(LuaHandle*); @@ -32,7 +33,7 @@ typedef int(*LuaFunction)(LuaHandle*); // class LuaScript // ===================================================== -class LuaScript{ +class LuaScript { private: LuaHandle *luaState; int argumentCount; @@ -63,7 +64,7 @@ private: // class LuaArguments // ===================================================== -class LuaArguments{ +class LuaArguments { private: lua_State *luaState; int returnCount; @@ -75,11 +76,13 @@ public: string getString(int argumentIndex) const; void * getGenericData(int argumentIndex) const; Vec2i getVec2i(int argumentIndex) const; + Vec4i getVec4i(int argumentIndex) const; int getReturnCount() const {return returnCount;} void returnInt(int value); void returnString(const string &value); void returnVec2i(const Vec2i &value); + void returnVec4i(const Vec4i &value); void returnVectorInt(const vector &value); private: diff --git a/source/shared_lib/sources/lua/lua_script.cpp b/source/shared_lib/sources/lua/lua_script.cpp index c723f907b..17e491657 100644 --- a/source/shared_lib/sources/lua/lua_script.cpp +++ b/source/shared_lib/sources/lua/lua_script.cpp @@ -20,7 +20,7 @@ using namespace std; using namespace Shared::Util; -namespace Shared{ namespace Lua{ +namespace Shared { namespace Lua { // // This class wraps streflop for LuaScript. We need to toggle the data type @@ -590,6 +590,42 @@ Vec2i LuaArguments::getVec2i(int argumentIndex) const{ return v; } +Vec4i LuaArguments::getVec4i(int argumentIndex) const { + Lua_STREFLOP_Wrapper streflopWrapper; + + Vec4i v; + + if(!lua_istable(luaState, argumentIndex)){ + throwLuaError("Can not get vec4i from Lua state, value on the stack is not a table"); + } + +#if LUA_VERSION_NUM > 501 + if(lua_rawlen(luaState, argumentIndex) != 4 ) { +#else + if(luaL_getn(luaState, argumentIndex) != 4) { +#endif + throwLuaError("Can not get vec4i from Lua state, array size not 4"); + } + + lua_rawgeti(luaState, argumentIndex, 1); + v.x= luaL_checkint(luaState, argumentIndex); + lua_pop(luaState, 1); + + lua_rawgeti(luaState, argumentIndex, 2); + v.y= luaL_checkint(luaState, argumentIndex); + lua_pop(luaState, 1); + + lua_rawgeti(luaState, argumentIndex, 3); + v.z= luaL_checkint(luaState, argumentIndex); + lua_pop(luaState, 1); + + lua_rawgeti(luaState, argumentIndex, 4); + v.w= luaL_checkint(luaState, argumentIndex); + lua_pop(luaState, 1); + + return v; +} + void LuaArguments::returnInt(int value){ Lua_STREFLOP_Wrapper streflopWrapper; @@ -618,6 +654,27 @@ void LuaArguments::returnVec2i(const Vec2i &value){ lua_rawseti(luaState, -2, 2); } +void LuaArguments::returnVec4i(const Vec4i &value) { + //Lua_STREFLOP_Wrapper streflopWrapper; + + ++returnCount; + + lua_newtable(luaState); + + lua_pushnumber(luaState, value.x); + lua_rawseti(luaState, -2, 1); + + lua_pushnumber(luaState, value.y); + lua_rawseti(luaState, -2, 2); + + lua_pushnumber(luaState, value.z); + lua_rawseti(luaState, -2, 3); + + lua_pushnumber(luaState, value.w); + lua_rawseti(luaState, -2, 4); + +} + void LuaArguments::returnVectorInt(const vector &value) { //Lua_STREFLOP_Wrapper streflopWrapper;