From e63e634e1e2e13282f2e900f455874f5588033d4 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Sat, 13 Oct 2012 07:33:33 +0000 Subject: [PATCH] - added a bit more lua and updated capture enemy flag --- source/glest_game/game/game.cpp | 16 ++ source/glest_game/game/game.h | 6 + source/glest_game/game/script_manager.cpp | 84 ++++++++-- source/glest_game/game/script_manager.h | 6 + source/glest_game/graphics/renderer.cpp | 54 +++++++ source/glest_game/graphics/renderer.h | 1 + source/glest_game/gui/selection.h | 7 + source/glest_game/world/world.cpp | 17 +++ source/glest_game/world/world.h | 3 + source/shared_lib/include/lua/lua_script.h | 10 ++ source/shared_lib/sources/lua/lua_script.cpp | 153 +++++++++++++++++++ 11 files changed, 346 insertions(+), 11 deletions(-) diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index 8369875f..81dbdc44 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -3725,6 +3725,20 @@ void Game::exitGameState(Program *program, Stats &endStats) { // ==================== PRIVATE ==================== +void Game::highlightUnit(int unitId,float radius, float thickness, Vec4f color) { + HighlightSpecialUnitInfo info; + info.radius = radius; + info.thickness = thickness; + info.color = color; + unitHighlightList[unitId] = info; +} + +void Game::unhighlightUnit(int unitId) { + if(unitHighlightList.find(unitId) != unitHighlightList.end()) { + unitHighlightList.erase(unitId); + } +} + // ==================== render ==================== void Game::render3d(){ @@ -3781,6 +3795,8 @@ void Game::render3d(){ if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); } + renderer.renderSpecialHighlightUnits(unitHighlightList); + // renderTeamColorCircle renderer.renderMorphEffects(); if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderObjects]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); diff --git a/source/glest_game/game/game.h b/source/glest_game/game/game.h index 695030a3..45788c93 100644 --- a/source/glest_game/game/game.h +++ b/source/glest_game/game/game.h @@ -28,6 +28,7 @@ #include "game_settings.h" #include "network_interface.h" #include "data_types.h" +#include "selection.h" #include "leak_dumper.h" using std::vector; @@ -193,6 +194,8 @@ private: Unit *currentCameraFollowUnit; + std::map unitHighlightList; + public: Game(); Game(Program *program, const GameSettings *gameSettings, bool masterserverMode); @@ -304,6 +307,9 @@ public: void removeCellMarker(Vec2i surfaceCellPos, const Faction *faction); void showMarker(Vec2i cellPos, MarkedCell cellData); + void highlightUnit(int unitId,float radius, float thickness, Vec4f color); + void unhighlightUnit(int unitId); + private: //render void render3d(); diff --git a/source/glest_game/game/script_manager.cpp b/source/glest_game/game/script_manager.cpp index 4d6c8653..1ddc7915 100644 --- a/source/glest_game/game/script_manager.cpp +++ b/source/glest_game/game/script_manager.cpp @@ -371,6 +371,9 @@ void ScriptManager::init(World* world, GameCamera *gameCamera, const XmlNode *ro luaScript.registerFunction(getHumanFactionId, "humanFaction"); + luaScript.registerFunction(highlightUnit, "highlightUnit"); + luaScript.registerFunction(unhighlightUnit, "unhighlightUnit"); + //load code for(int i= 0; igetScriptCount(); ++i){ const Script* script= scenario->getScript(i); @@ -683,14 +686,30 @@ void ScriptManager::onCellTriggerEvent(Unit *movingUnit) { { 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); + + // Cache area lookup so for each unitsize and pos its done only once + bool foundInCache = false; + std::map >::iterator iterFind1 = event.eventLookupCache.find(movingUnit->getType()->getSize()); + if(iterFind1 != event.eventLookupCache.end()) { + std::map::iterator iterFind2 = iterFind1->second.find(movingUnit->getPos()); + if(iterFind2 != iterFind1->second.end()) { + foundInCache = true; + srcInDst = iterFind2->second; } } + if(foundInCache == 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); + } + } + + event.eventLookupCache[movingUnit->getType()->getSize()][movingUnit->getPos()] = srcInDst; + } + if(srcInDst == true) { if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } @@ -750,15 +769,32 @@ void ScriptManager::onCellTriggerEvent(Unit *movingUnit) { //if(event.sourceId == 1) printf("ctet_FactionPos event.destPos = [%s], movingUnit->getPos() [%s] Unit id = %d\n",event.destPos.getString().c_str(),movingUnit->getPos().getString().c_str(),movingUnit->getId()); 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__); - } + // Cache area lookup so for each unitsize and pos its done only once + bool foundInCache = false; + std::map >::iterator iterFind1 = event.eventLookupCache.find(movingUnit->getType()->getSize()); + if(iterFind1 != event.eventLookupCache.end()) { + std::map::iterator iterFind2 = iterFind1->second.find(movingUnit->getPos()); + if(iterFind2 != iterFind1->second.end()) { + foundInCache = true; + srcInDst = iterFind2->second; } } + + if(foundInCache == 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__); + } + } + } + + event.eventLookupCache[movingUnit->getType()->getSize()][movingUnit->getPos()] = srcInDst; + } + triggerEvent = srcInDst; if(triggerEvent == true) { //printf("!!!UNIT IN AREA!!! Faction area pos, moving unit faction= %d, trigger faction = %d, unit id = %d\n",movingUnit->getFactionIndex(),event.sourceId,movingUnit->getId()); @@ -1680,6 +1716,20 @@ int ScriptManager::getHumanFactionId() { return this->world->getThisFactionIndex(); } +void ScriptManager::highlightUnit(int unitId, float radius, float thickness, Vec4f color) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + ScriptManager_STREFLOP_Wrapper streflopWrapper; + + world->highlightUnit(unitId, radius, thickness, color); +} + +void ScriptManager::unhighlightUnit(int unitId) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + ScriptManager_STREFLOP_Wrapper streflopWrapper; + + world->unhighlightUnit(unitId); +} + // ========================== lua callbacks =============================================== int ScriptManager::showMessage(LuaHandle* luaHandle){ @@ -2659,6 +2709,18 @@ int ScriptManager::getHumanFactionId(LuaHandle* luaHandle) { return luaArguments.getReturnCount(); } +int ScriptManager::highlightUnit(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + thisScriptManager->highlightUnit(luaArguments.getInt(-4), luaArguments.getFloat(-3), luaArguments.getFloat(-2), luaArguments.getVec4f(-1)); + return luaArguments.getReturnCount(); +} + +int ScriptManager::unhighlightUnit(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + thisScriptManager->unhighlightUnit(luaArguments.getInt(-1)); + 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 c7f0b390..fdc9083e 100644 --- a/source/glest_game/game/script_manager.h +++ b/source/glest_game/game/script_manager.h @@ -119,6 +119,8 @@ public: std::map eventStateInfo; + std::map > eventLookupCache; + void saveGame(XmlNode *rootNode); void loadGame(const XmlNode *rootNode); }; @@ -367,6 +369,8 @@ private: int isFreeCells(int unitSize, int field,Vec2i pos); int getHumanFactionId(); + void highlightUnit(int unitId, float radius, float thickness, Vec4f color); + void unhighlightUnit(int unitId); //callbacks, commands static int networkShowMessageForFaction(LuaHandle* luaHandle); @@ -503,6 +507,8 @@ private: static int getHumanFactionId(LuaHandle* luaHandle); + static int highlightUnit(LuaHandle* luaHandle); + static int unhighlightUnit(LuaHandle* luaHandle); }; }}//end namespace diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index 9f915782..b18e7e85 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -4817,6 +4817,60 @@ void Renderer::renderTeamColorCircle(){ } } +void Renderer::renderSpecialHighlightUnits(std::map unitHighlightList) { + if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true || unitHighlightList.empty() == true) { + return; + } + + VisibleQuadContainerCache &qCache = getQuadCache(); + if(qCache.visibleQuadUnitList.empty() == false) { + + glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT); + glDisable(GL_LIGHTING); + glDisable(GL_TEXTURE_2D); + glDepthFunc(GL_ALWAYS); + glDisable(GL_STENCIL_TEST); + glDisable(GL_CULL_FACE); + glEnable(GL_BLEND); + glLineWidth(2.f); + + for(int visibleUnitIndex = 0; + visibleUnitIndex < qCache.visibleQuadUnitList.size(); ++visibleUnitIndex) { + Unit *unit = qCache.visibleQuadUnitList[visibleUnitIndex]; + + std::map::iterator iterFindSpecialUnit = unitHighlightList.find(unit->getId()); + if(iterFindSpecialUnit != unitHighlightList.end()) { + Vec3f color=unit->getFaction()->getTexture()->getPixmapConst()->getPixel3f(0,0); + float radius = 1.0f; + float thickness = 0.1f; + float alpha = 0.65f; + + HighlightSpecialUnitInfo &specialInfo = iterFindSpecialUnit->second; + if(specialInfo.color.x >= 0) { + color.x = specialInfo.color.x; + color.y = specialInfo.color.y; + color.z = specialInfo.color.z; + } + if(specialInfo.color.w >= 0) { + alpha = specialInfo.color.w; + } + if(specialInfo.radius > 0) { + radius = specialInfo.radius; + } + if(specialInfo.thickness > 0) { + thickness = specialInfo.thickness; + } + + glColor4f(color.x, color.y, color.z, alpha); + + Vec3f currVec= unit->getCurrVectorFlat(); + renderSelectionCircle(currVec, unit->getType()->getSize(), radius, thickness); + } + } + glPopAttrib(); + } +} + void Renderer::renderTeamColorPlane(){ if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { return; diff --git a/source/glest_game/graphics/renderer.h b/source/glest_game/graphics/renderer.h index 5f3381b5..c8d21aa0 100644 --- a/source/glest_game/graphics/renderer.h +++ b/source/glest_game/graphics/renderer.h @@ -559,6 +559,7 @@ public: inline Quad2i getVisibleQuad() const {return visibleQuad;} inline Quad2i getVisibleQuadFromCamera() const {return visibleQuadFromCamera;} void renderTeamColorPlane(); + void renderSpecialHighlightUnits(std::map unitHighlightList); void renderTeamColorCircle(); void renderMorphEffects(); diff --git a/source/glest_game/gui/selection.h b/source/glest_game/gui/selection.h index f145eebe..3c65bf0d 100644 --- a/source/glest_game/gui/selection.h +++ b/source/glest_game/gui/selection.h @@ -28,6 +28,13 @@ namespace Glest{ namespace Game{ class Gui; class World; +class HighlightSpecialUnitInfo { +public: + float radius; + float thickness; + Vec4f color; +}; + // ===================================================== // class Selection // diff --git a/source/glest_game/world/world.cpp b/source/glest_game/world/world.cpp index 8744164a..57226d31 100644 --- a/source/glest_game/world/world.cpp +++ b/source/glest_game/world/world.cpp @@ -1367,6 +1367,23 @@ void World::showMarker(Vec2i pos, int factionIndex, const string ¬e, const st game->showMarker(surfaceCellPos, mc); } +void World::highlightUnit(int unitId,float radius, float thickness, Vec4f color) { + Unit* unit= findUnitById(unitId); + if(unit == NULL) { + throw megaglest_runtime_error("Can not find unit to set highlight unitId = " + intToStr(unitId)); + } + game->highlightUnit(unitId,radius, thickness, color); +} + +void World::unhighlightUnit(int unitId) { + Unit* unit= findUnitById(unitId); + if(unit == NULL) { + throw megaglest_runtime_error("Can not find unit to set highlight unitId = " + intToStr(unitId)); + } + game->unhighlightUnit(unitId); +} + + int World::getUnitFactionIndex(int unitId) { Unit* unit= findUnitById(unitId); if(unit == NULL) { diff --git a/source/glest_game/world/world.h b/source/glest_game/world/world.h index d0e26118..1d8848ed 100644 --- a/source/glest_game/world/world.h +++ b/source/glest_game/world/world.h @@ -258,6 +258,9 @@ public: const string getSystemMacroValue(const string key); const string getPlayerName(int factionIndex); + void highlightUnit(int unitId,float radius, float thickness, Vec4f color); + void unhighlightUnit(int unitId); + inline Game * getGame() { return game; } const GameSettings * getGameSettings() const; diff --git a/source/shared_lib/include/lua/lua_script.h b/source/shared_lib/include/lua/lua_script.h index fd956181..7735d6ce 100644 --- a/source/shared_lib/include/lua/lua_script.h +++ b/source/shared_lib/include/lua/lua_script.h @@ -22,6 +22,10 @@ using std::string; using Shared::Graphics::Vec2i; using Shared::Graphics::Vec4i; +using Shared::Graphics::Vec2f; +using Shared::Graphics::Vec3f; +using Shared::Graphics::Vec4f; + using Shared::Xml::XmlNode; namespace Shared { namespace Lua { @@ -87,6 +91,12 @@ public: void * getGenericData(int argumentIndex) const; Vec2i getVec2i(int argumentIndex) const; Vec4i getVec4i(int argumentIndex) const; + + float getFloat(int argumentIndex) const; + Vec2f getVec2f(int argumentIndex) const; + Vec3f getVec3f(int argumentIndex) const; + Vec4f getVec4f(int argumentIndex) const; + int getReturnCount() const {return returnCount;} void returnInt(int value); diff --git a/source/shared_lib/sources/lua/lua_script.cpp b/source/shared_lib/sources/lua/lua_script.cpp index 8d274733..fc665792 100644 --- a/source/shared_lib/sources/lua/lua_script.cpp +++ b/source/shared_lib/sources/lua/lua_script.cpp @@ -635,6 +635,159 @@ int LuaArguments::getInt(int argumentIndex) const{ return result; } +float LuaArguments::getFloat(int argumentIndex) const { + Lua_STREFLOP_Wrapper streflopWrapper; + + if(!lua_isnumber(luaState, argumentIndex)) { + throwLuaError("Can not get int from Lua state"); + } + float result = luaL_checknumber(luaState, argumentIndex); + return result; +} +Vec2f LuaArguments::getVec2f(int argumentIndex) const { + Lua_STREFLOP_Wrapper streflopWrapper; + + Vec2f v; + + if(!lua_istable(luaState, argumentIndex)){ + throwLuaError("Can not get vec2f from Lua state, value on the stack is not a table"); + } + +#if LUA_VERSION_NUM > 501 + if(lua_rawlen(luaState, argumentIndex)!=2){ +#else + if(luaL_getn(luaState, argumentIndex)!=2){ +#endif + throwLuaError("Can not get vec2f from Lua state, array size not 2"); + } + + //string stackString = getStackText(); + //printf("Lua Stack:\n%s\n",stackString.c_str()); + + lua_rawgeti(luaState, argumentIndex, 1); + //printf("xa = %s argumentIndex = %d\n",lua_tostring(luaState, argumentIndex),argumentIndex); + + //v.x= luaL_checkint(luaState, argumentIndex); + v.x= lua_tonumber(luaState, argumentIndex); + lua_pop(luaState, 1); + + //printf("X = %d\n",v.x); + + lua_rawgeti(luaState, argumentIndex, 2); + //printf("ya = %s\n",lua_tostring(luaState, argumentIndex)); + + //v.y= luaL_checkint(luaState, argumentIndex); + v.y= lua_tonumber(luaState, argumentIndex); + lua_pop(luaState, 1); + + //printf("Y = %d\n",v.y); + + return v; +} + +Vec3f LuaArguments::getVec3f(int argumentIndex) const { + Lua_STREFLOP_Wrapper streflopWrapper; + + Vec3f v; + + if(!lua_istable(luaState, argumentIndex)){ + throwLuaError("Can not get vec3f from Lua state, value on the stack is not a table"); + } + +#if LUA_VERSION_NUM > 501 + if(lua_rawlen(luaState, argumentIndex)!=3){ +#else + if(luaL_getn(luaState, argumentIndex)!=3){ +#endif + throwLuaError("Can not get vec3f from Lua state, array size not 3"); + } + + //string stackString = getStackText(); + //printf("Lua Stack:\n%s\n",stackString.c_str()); + + lua_rawgeti(luaState, argumentIndex, 1); + //printf("xa = %s argumentIndex = %d\n",lua_tostring(luaState, argumentIndex),argumentIndex); + + //v.x= luaL_checkint(luaState, argumentIndex); + v.x= lua_tonumber(luaState, argumentIndex); + lua_pop(luaState, 1); + + //printf("X = %d\n",v.x); + + lua_rawgeti(luaState, argumentIndex, 2); + //printf("ya = %s\n",lua_tostring(luaState, argumentIndex)); + + //v.y= luaL_checkint(luaState, argumentIndex); + v.y= lua_tonumber(luaState, argumentIndex); + lua_pop(luaState, 1); + + //printf("Y = %d\n",v.y); + + lua_rawgeti(luaState, argumentIndex, 3); + //printf("ya = %s\n",lua_tostring(luaState, argumentIndex)); + + //v.y= luaL_checkint(luaState, argumentIndex); + v.z= lua_tonumber(luaState, argumentIndex); + lua_pop(luaState, 1); + + return v; +} + +Vec4f LuaArguments::getVec4f(int argumentIndex) const { + Lua_STREFLOP_Wrapper streflopWrapper; + + Vec4f v; + + if(!lua_istable(luaState, argumentIndex)){ + throwLuaError("Can not get vec4f 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 vec4f from Lua state, array size not 4"); + } + + //string stackString = getStackText(); + //printf("Lua Stack:\n%s\n",stackString.c_str()); + + lua_rawgeti(luaState, argumentIndex, 1); + //printf("xa = %s argumentIndex = %d\n",lua_tostring(luaState, argumentIndex),argumentIndex); + + //v.x= luaL_checkint(luaState, argumentIndex); + v.x= lua_tonumber(luaState, argumentIndex); + lua_pop(luaState, 1); + + //printf("X = %d\n",v.x); + + lua_rawgeti(luaState, argumentIndex, 2); + //printf("ya = %s\n",lua_tostring(luaState, argumentIndex)); + + //v.y= luaL_checkint(luaState, argumentIndex); + v.y= lua_tonumber(luaState, argumentIndex); + lua_pop(luaState, 1); + + //printf("Y = %d\n",v.y); + + lua_rawgeti(luaState, argumentIndex, 3); + //printf("ya = %s\n",lua_tostring(luaState, argumentIndex)); + + //v.y= luaL_checkint(luaState, argumentIndex); + v.z= lua_tonumber(luaState, argumentIndex); + lua_pop(luaState, 1); + + lua_rawgeti(luaState, argumentIndex, 4); + //printf("ya = %s\n",lua_tostring(luaState, argumentIndex)); + + //v.y= luaL_checkint(luaState, argumentIndex); + v.w= lua_tonumber(luaState, argumentIndex); + lua_pop(luaState, 1); + + return v; +} + string LuaArguments::getString(int argumentIndex) const{ Lua_STREFLOP_Wrapper streflopWrapper;