diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index 49722518..0b90e9ac 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -2112,6 +2112,97 @@ void Game::removeUnitFromSelection(const Unit *unit) { } } +bool Game::addUnitToSelection(Unit *unit) { + bool result = false; + try { + Selection *selection= gui.getSelectionPtr(); + if(selection != NULL) { + result = selection->select(unit); + } + } + catch(const exception &ex) { + char szBuf[4096]=""; + sprintf(szBuf,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); + + SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,szBuf); + + if(errorMessageBox.getEnabled() == false) { + ErrorDisplayMessage(ex.what(),true); + } + + //abort(); + } + + return result; +} + +void Game::addUnitToGroupSelection(Unit *unit,int groupIndex) { + try { + Selection *selection= gui.getSelectionPtr(); + if(selection != NULL) { + selection->addUnitToGroup(groupIndex,unit); + } + } + catch(const exception &ex) { + char szBuf[4096]=""; + sprintf(szBuf,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); + + SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,szBuf); + + if(errorMessageBox.getEnabled() == false) { + ErrorDisplayMessage(ex.what(),true); + } + + //abort(); + } +} + +void Game::removeUnitFromGroupSelection(int unitId,int groupIndex) { + try { + Selection *selection= gui.getSelectionPtr(); + if(selection != NULL) { + selection->removeUnitFromGroup(groupIndex,unitId); + } + } + catch(const exception &ex) { + char szBuf[4096]=""; + sprintf(szBuf,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); + + SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,szBuf); + + if(errorMessageBox.getEnabled() == false) { + ErrorDisplayMessage(ex.what(),true); + } + + //abort(); + } +} + +void Game::recallGroupSelection(int groupIndex) { + try { + Selection *selection= gui.getSelectionPtr(); + if(selection != NULL) { + selection->recallGroup(groupIndex); + } + } + catch(const exception &ex) { + char szBuf[4096]=""; + sprintf(szBuf,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); + + SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,szBuf); + + if(errorMessageBox.getEnabled() == false) { + ErrorDisplayMessage(ex.what(),true); + } + + //abort(); + } +} + void Game::tick() { ProgramState::tick(); diff --git a/source/glest_game/game/game.h b/source/glest_game/game/game.h index 45788c93..4b874160 100644 --- a/source/glest_game/game/game.h +++ b/source/glest_game/game/game.h @@ -232,6 +232,10 @@ public: Program *getProgram() {return program;} void removeUnitFromSelection(const Unit *unit); + bool addUnitToSelection(Unit *unit); + void addUnitToGroupSelection(Unit *unit,int groupIndex); + void removeUnitFromGroupSelection(int unitId,int groupIndex); + void recallGroupSelection(int groupIndex); Uint64 getTickCount() {return tickCount;} bool getPaused(); diff --git a/source/glest_game/game/script_manager.cpp b/source/glest_game/game/script_manager.cpp index 1ddc7915..5c62df9c 100644 --- a/source/glest_game/game/script_manager.cpp +++ b/source/glest_game/game/script_manager.cpp @@ -374,6 +374,15 @@ void ScriptManager::init(World* world, GameCamera *gameCamera, const XmlNode *ro luaScript.registerFunction(highlightUnit, "highlightUnit"); luaScript.registerFunction(unhighlightUnit, "unhighlightUnit"); + luaScript.registerFunction(giveStopCommand, "giveStopCommand"); + luaScript.registerFunction(selectUnit, "selectUnit"); + luaScript.registerFunction(unselectUnit, "unselectUnit"); + luaScript.registerFunction(addUnitToGroupSelection, "addUnitToGroupSelection"); + luaScript.registerFunction(recallGroupSelection, "recallGroupSelection"); + luaScript.registerFunction(removeUnitFromGroupSelection, "removeUnitFromGroupSelection"); + luaScript.registerFunction(setAttackWarningsEnabled, "setAttackWarningsEnabled"); + luaScript.registerFunction(getAttackWarningsEnabled, "getAttackWarningsEnabled"); + //load code for(int i= 0; igetScriptCount(); ++i){ const Script* script= scenario->getScript(i); @@ -1730,6 +1739,52 @@ void ScriptManager::unhighlightUnit(int unitId) { world->unhighlightUnit(unitId); } +void ScriptManager::giveStopCommand(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->giveStopCommand(unitId); +} + +bool ScriptManager::selectUnit(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; + return world->selectUnit(unitId); +} + +void ScriptManager::unselectUnit(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->unselectUnit(unitId); +} + +void ScriptManager::addUnitToGroupSelection(int unitId,int groupIndex) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + ScriptManager_STREFLOP_Wrapper streflopWrapper; + world->addUnitToGroupSelection(unitId,groupIndex); +} +void ScriptManager::recallGroupSelection(int groupIndex) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + ScriptManager_STREFLOP_Wrapper streflopWrapper; + world->recallGroupSelection(groupIndex); +} +void ScriptManager::removeUnitFromGroupSelection(int unitId,int groupIndex) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + ScriptManager_STREFLOP_Wrapper streflopWrapper; + world->removeUnitFromGroupSelection(unitId,groupIndex); +} + +void ScriptManager::setAttackWarningsEnabled(bool enabled) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + ScriptManager_STREFLOP_Wrapper streflopWrapper; + world->setAttackWarningsEnabled(enabled); +} + +bool ScriptManager::getAttackWarningsEnabled() { + 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->getAttackWarningsEnabled(); +} + // ========================== lua callbacks =============================================== int ScriptManager::showMessage(LuaHandle* luaHandle){ @@ -2721,6 +2776,53 @@ int ScriptManager::unhighlightUnit(LuaHandle* luaHandle) { return luaArguments.getReturnCount(); } +int ScriptManager::giveStopCommand(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + thisScriptManager->giveStopCommand(luaArguments.getInt(-1)); + return luaArguments.getReturnCount(); +} + +int ScriptManager::selectUnit(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + luaArguments.returnInt(thisScriptManager->selectUnit(luaArguments.getInt(-1))); + return luaArguments.getReturnCount(); +} + +int ScriptManager::unselectUnit(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + thisScriptManager->unselectUnit(luaArguments.getInt(-1)); + return luaArguments.getReturnCount(); +} + +int ScriptManager::addUnitToGroupSelection(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + thisScriptManager->addUnitToGroupSelection(luaArguments.getInt(-2),luaArguments.getInt(-1)); + return luaArguments.getReturnCount(); +} + +int ScriptManager::recallGroupSelection(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + thisScriptManager->recallGroupSelection(luaArguments.getInt(-1)); + return luaArguments.getReturnCount(); +} + +int ScriptManager::removeUnitFromGroupSelection(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + thisScriptManager->removeUnitFromGroupSelection(luaArguments.getInt(-2),luaArguments.getInt(-1)); + return luaArguments.getReturnCount(); +} + +int ScriptManager::setAttackWarningsEnabled(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + thisScriptManager->setAttackWarningsEnabled(luaArguments.getInt(-1)); + return luaArguments.getReturnCount(); +} +int ScriptManager::getAttackWarningsEnabled(LuaHandle* luaHandle) { + LuaArguments luaArguments(luaHandle); + luaArguments.returnInt(thisScriptManager->getAttackWarningsEnabled()); + 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 fdc9083e..bab73e23 100644 --- a/source/glest_game/game/script_manager.h +++ b/source/glest_game/game/script_manager.h @@ -279,6 +279,7 @@ private: void giveProductionCommand(int unitId, const string &producedName); void giveAttackCommand(int unitId, int unitToAttackId); void giveUpgradeCommand(int unitId, const string &upgradeName); + void giveStopCommand(int unitId); void disableAi(int factionIndex); void enableAi(int factionIndex); @@ -372,6 +373,14 @@ private: void highlightUnit(int unitId, float radius, float thickness, Vec4f color); void unhighlightUnit(int unitId); + bool selectUnit(int unitId); + void unselectUnit(int unitId); + void addUnitToGroupSelection(int unitId,int groupIndex); + void recallGroupSelection(int groupIndex); + void removeUnitFromGroupSelection(int unitId,int group); + void setAttackWarningsEnabled(bool enabled); + bool getAttackWarningsEnabled(); + //callbacks, commands static int networkShowMessageForFaction(LuaHandle* luaHandle); static int networkShowMessageForTeam(LuaHandle* luaHandle); @@ -509,6 +518,16 @@ private: static int highlightUnit(LuaHandle* luaHandle); static int unhighlightUnit(LuaHandle* luaHandle); + + static int giveStopCommand(LuaHandle* luaHandle); + static int selectUnit(LuaHandle* luaHandle); + static int unselectUnit(LuaHandle* luaHandle); + static int addUnitToGroupSelection(LuaHandle* luaHandle); + static int recallGroupSelection(LuaHandle* luaHandle); + static int removeUnitFromGroupSelection(LuaHandle* luaHandle); + static int setAttackWarningsEnabled(LuaHandle* luaHandle); + static int getAttackWarningsEnabled(LuaHandle* luaHandle); + }; }}//end namespace diff --git a/source/glest_game/gui/selection.cpp b/source/glest_game/gui/selection.cpp index f2767c9a..609fc534 100644 --- a/source/glest_game/gui/selection.cpp +++ b/source/glest_game/gui/selection.cpp @@ -37,33 +37,34 @@ Selection::~Selection(){ clear(); } -void Selection::select(Unit *unit) { +bool Selection::select(Unit *unit) { + bool result = false; //check size //if(selectedUnits.size() >= maxUnits){ if(selectedUnits.size() >= Config::getInstance().getInt("MaxUnitSelectCount",intToStr(maxUnits).c_str())) { - return; + return result; } //check if already selected for(int i=0; i < selectedUnits.size(); ++i) { if(selectedUnits[i ]== unit) { - return; + return true; } } //check if dead if(unit->isDead()) { - return; + return false; } //check if multisel if(!unit->getType()->getMultiSelect() && !isEmpty()) { - return; + return false; } //check if enemy if(unit->getFactionIndex() != factionIndex && !isEmpty()) { - return; + return false; } //check existing enemy @@ -80,7 +81,10 @@ void Selection::select(Unit *unit) { unit->addObserver(this); selectedUnits.push_back(unit); + result = true; gui->onSelectionChanged(); + + return result; } void Selection::select(const UnitContainer &units){ @@ -169,24 +173,68 @@ bool Selection::hasUnit(const Unit* unit) const{ return find(selectedUnits.begin(), selectedUnits.end(), unit)!=selectedUnits.end(); } -void Selection::assignGroup(int groupIndex){ +void Selection::assignGroup(int groupIndex,const UnitContainer *pUnits) { + if(groupIndex < 0 || groupIndex >= maxGroups) { + throw megaglest_runtime_error("Invalid value for groupIndex = " + intToStr(groupIndex)); + } + //clear group groups[groupIndex].clear(); //assign new group - for(int i=0; isize(); ++i) { + groups[groupIndex].push_back((*addUnits)[i]); } } +void Selection::addUnitToGroup(int groupIndex,Unit *unit) { + if(groupIndex < 0 || groupIndex >= maxGroups) { + throw megaglest_runtime_error("Invalid value for groupIndex = " + intToStr(groupIndex)); + } + + if(unit != NULL) { + const UnitContainer &addUnits = selectedUnits; + groups[groupIndex].push_back(unit); + } +} + +void Selection::removeUnitFromGroup(int groupIndex,int unitId) { + if(groupIndex < 0 || groupIndex >= maxGroups) { + throw megaglest_runtime_error("Invalid value for groupIndex = " + intToStr(groupIndex)); + } + + for(unsigned int i = 0; i < groups[groupIndex].size(); ++i) { + Unit *unit = groups[groupIndex][i]; + if(unit != NULL && unit->getId() == unitId) { + groups[groupIndex].erase(groups[groupIndex].begin() + i); + break; + } + } +} + +vector Selection::getUnitsForGroup(int groupIndex) { + if(groupIndex < 0 || groupIndex >= maxGroups) { + throw megaglest_runtime_error("Invalid value for groupIndex = " + intToStr(groupIndex)); + } + return groups[groupIndex]; +} + void Selection::recallGroup(int groupIndex){ + if(groupIndex < 0 || groupIndex >= maxGroups) { + throw megaglest_runtime_error("Invalid value for groupIndex = " + intToStr(groupIndex)); + } + clear(); - for(int i=0; i getUnitsForGroup(int groupIndex); virtual void unitEvent(UnitObserver::Event event, const Unit *unit); diff --git a/source/glest_game/world/unit_updater.cpp b/source/glest_game/world/unit_updater.cpp index 7cabe871..b6aa1d3b 100644 --- a/source/glest_game/world/unit_updater.cpp +++ b/source/glest_game/world/unit_updater.cpp @@ -2330,13 +2330,17 @@ bool UnitUpdater::unitOnRange(Unit *unit, int range, Unit **rangedPtr, // add new attack if(nearest == NULL) { // no else! + AttackWarningData* awd= new AttackWarningData(); awd->lastFrameCount=world->getFrameCount(); awd->attackPosition.x=enemyFloatCenter.x; awd->attackPosition.y=enemyFloatCenter.y; attackWarnings.push_back(awd); - SoundRenderer::getInstance().playFx(CoreData::getInstance().getAttentionSound()); - world->addAttackEffects(enemyUnit); + + if(world->getAttackWarningsEnabled() == true) { + SoundRenderer::getInstance().playFx(CoreData::getInstance().getAttentionSound()); + world->addAttackEffects(enemyUnit); + } } } } diff --git a/source/glest_game/world/world.cpp b/source/glest_game/world/world.cpp index 99389450..8acbc619 100644 --- a/source/glest_game/world/world.cpp +++ b/source/glest_game/world/world.cpp @@ -80,6 +80,7 @@ World::World() { perfTimerEnabled=false; queuedScenarioName=""; queuedScenarioKeepFactions=false; + disableAttackEffects = false; loadWorldNode = NULL; @@ -788,8 +789,7 @@ void World::moveUnitCells(Unit *unit) { scriptManager->onCellTriggerEvent(unit); } -void World::addAttackEffects(const Unit *unit) -{ +void World::addAttackEffects(const Unit *unit) { attackEffects.addWaterSplash( Vec2f(unit->getPos().x, unit->getPos().y),1); } @@ -1055,6 +1055,63 @@ void World::givePositionCommand(int unitId, const string &commandName, const Vec } } +void World::giveStopCommand(int unitId) { + Unit* unit= findUnitById(unitId); + if(unit != NULL) { + const CommandType *ct = unit->getType()->getFirstCtOfClass(ccStop); + if(ct != NULL) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugUnitCommands).enabled) SystemFlags::OutputDebug(SystemFlags::debugUnitCommands,"In [%s::%s Line: %d] Unit [%s] will stop\n",__FILE__,__FUNCTION__,__LINE__,unit->getFullName().c_str()); + unit->giveCommand(new Command(ct)); + + if(SystemFlags::getSystemSettingType(SystemFlags::debugUnitCommands).enabled) SystemFlags::OutputDebug(SystemFlags::debugUnitCommands,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + } + else { + throw megaglest_runtime_error("Invalid ct in giveStopCommand: " + intToStr(unitId)); + } + } + else { + throw megaglest_runtime_error("Invalid unitId index in giveStopCommand: " + intToStr(unitId)); + } +} + +bool World::selectUnit(int unitId) { + bool result = false; + Unit* unit= findUnitById(unitId); + if(unit != NULL) { + result = game->addUnitToSelection(unit); + } + + return result; +} + +void World::unselectUnit(int unitId) { + Unit* unit= findUnitById(unitId); + if(unit != NULL) { + game->removeUnitFromSelection(unit); + } +} + +void World::addUnitToGroupSelection(int unitId,int groupIndex) { + Unit* unit= findUnitById(unitId); + if(unit != NULL) { + game->addUnitToGroupSelection(unit,groupIndex); + } +} + +void World::removeUnitFromGroupSelection(int unitId,int groupIndex) { + game->removeUnitFromGroupSelection(unitId,groupIndex); +} + +void World::recallGroupSelection(int groupIndex) { + game->recallGroupSelection(groupIndex); +} + +void World::setAttackWarningsEnabled(bool enabled) { + this->disableAttackEffects = !enabled; +} +bool World::getAttackWarningsEnabled() { + return (this->disableAttackEffects == false); +} void World::giveAttackCommand(int unitId, int unitToAttackId) { Unit* unit= findUnitById(unitId); @@ -1612,6 +1669,10 @@ void World::initFactionTypes(GameSettings *gs) { queuedScenarioName = loadWorldNode->getAttribute("queuedScenarioName")->getValue(); // bool queuedScenarioKeepFactions; queuedScenarioKeepFactions = loadWorldNode->getAttribute("queuedScenarioKeepFactions")->getIntValue() != 0; + + if(loadWorldNode->hasAttribute("disableAttackEffects") == true) { + disableAttackEffects = loadWorldNode->getAttribute("disableAttackEffects")->getIntValue() != 0; + } } if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -2416,6 +2477,8 @@ void World::saveGame(XmlNode *rootNode) { worldNode->addAttribute("queuedScenarioName",queuedScenarioName, mapTagReplacements); // bool queuedScenarioKeepFactions; worldNode->addAttribute("queuedScenarioKeepFactions",intToStr(queuedScenarioKeepFactions), mapTagReplacements); + + worldNode->addAttribute("disableAttackEffects",intToStr(disableAttackEffects), mapTagReplacements); } void World::loadGame(const XmlNode *rootNode) { diff --git a/source/glest_game/world/world.h b/source/glest_game/world/world.h index 1d8848ed..002396db 100644 --- a/source/glest_game/world/world.h +++ b/source/glest_game/world/world.h @@ -148,6 +148,8 @@ private: string queuedScenarioName; bool queuedScenarioKeepFactions; + bool disableAttackEffects; + const XmlNode *loadWorldNode; public: @@ -261,6 +263,17 @@ public: void highlightUnit(int unitId,float radius, float thickness, Vec4f color); void unhighlightUnit(int unitId); + void giveStopCommand(int unitId); + + bool selectUnit(int unitId); + void unselectUnit(int unitId); + void addUnitToGroupSelection(int unitId,int groupIndex); + void removeUnitFromGroupSelection(int unitId,int groupIndex); + void recallGroupSelection(int groupIndex); + void setAttackWarningsEnabled(bool enabled); + bool getAttackWarningsEnabled(); + + inline Game * getGame() { return game; } const GameSettings * getGameSettings() const;