From f84a710d2cb31c1ac89016d4a4129e8f2843f56b Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Tue, 14 Sep 2010 19:10:37 +0000 Subject: [PATCH] - initial logic to allow client side controlled AI --- source/glest_game/ai/ai.cpp | 9 ++++-- source/glest_game/ai/ai_interface.cpp | 29 ++++++++--------- source/glest_game/ai/ai_interface.h | 1 + source/glest_game/ai/ai_rule.cpp | 11 ++++--- source/glest_game/game/game.cpp | 31 ++++++++++++------- source/glest_game/game/game_constants.h | 15 +++++++-- source/glest_game/main/battle_end.cpp | 13 ++++++++ .../menu/menu_state_connected_game.cpp | 7 +++++ .../menu/menu_state_custom_game.cpp | 7 +++++ source/glest_game/network/network_manager.h | 8 +---- source/glest_game/type_instances/faction.cpp | 24 ++++++++++++-- source/glest_game/type_instances/faction.h | 1 + 12 files changed, 111 insertions(+), 45 deletions(-) diff --git a/source/glest_game/ai/ai.cpp b/source/glest_game/ai/ai.cpp index 8044ae99..24bc21c4 100644 --- a/source/glest_game/ai/ai.cpp +++ b/source/glest_game/ai/ai.cpp @@ -396,7 +396,8 @@ void Ai::massiveAttack(const Vec2i &pos, Field field, bool ultraAttack){ producerWarriorCount++; } - if(aiInterface->getControlType()==ctCpuMega) + if( aiInterface->getControlType() == ctCpuMega || + aiInterface->getControlType() == ctNetworkCpuMega) { if(producerWarriorCount>maxProducerWarriors) { @@ -434,11 +435,13 @@ void Ai::massiveAttack(const Vec2i &pos, Field field, bool ultraAttack){ } } - if(aiInterface->getControlType()==ctCpuEasy) + if( aiInterface->getControlType() == ctCpuEasy || + aiInterface->getControlType() == ctNetworkCpuEasy) { minWarriors+= 1; } - else if(aiInterface->getControlType()==ctCpuMega) + else if(aiInterface->getControlType() == ctCpuMega || + aiInterface->getControlType() == ctNetworkCpuMega) { minWarriors+= 3; if(minWarriors>maxMinWarriors-1 || randomMinWarriorsReached) diff --git a/source/glest_game/ai/ai_interface.cpp b/source/glest_game/ai/ai_interface.cpp index db910505..4bbce0be 100644 --- a/source/glest_game/ai/ai_interface.cpp +++ b/source/glest_game/ai/ai_interface.cpp @@ -1,7 +1,7 @@ // ============================================================== // This file is part of Glest (www.glest.org) // -// Copyright (C) 2001-2008 Martiņo Figueroa +// Copyright (C) 2001-2008 Martio Figueroa // // You can redistribute this code and/or modify it under // the terms of the GNU General Public License as published @@ -92,13 +92,18 @@ void AiInterface::printLog(int logLevel, const string &s){ // ==================== interaction ==================== +bool AiInterface::executeCommandOverNetwork() { + bool enableServerControlledAI = gameSettings->getEnableServerControlledAI(); + bool isNetworkGame = gameSettings->isNetworkGame(); + NetworkRole role = NetworkManager::getInstance().getNetworkRole(); + Faction *faction = world->getFaction(factionIndex); + return faction->getCpuControl(enableServerControlledAI,isNetworkGame,role); +} + CommandResult AiInterface::giveCommand(int unitIndex, CommandClass commandClass, const Vec2i &pos){ assert(this->gameSettings != NULL); - if(this->gameSettings->getEnableServerControlledAI() == true && - this->gameSettings->isNetworkGame() == true && - NetworkManager::getInstance().getNetworkRole() == nrServer) { - + if(executeCommandOverNetwork() == true) { //Unit *unit = world->getFaction(factionIndex)->getUnit(unitIndex); const Unit *unit = getMyUnit(unitIndex); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] unitIndex = %d\nunit = [%s]\ncommandClass = [%d]\n",__FILE__,__FUNCTION__,__LINE__,unitIndex,unit->toString().c_str(),commandClass); @@ -150,9 +155,7 @@ CommandResult AiInterface::giveCommand(int unitIndex, const CommandType *command throw runtime_error(sError); } - if(this->gameSettings->getEnableServerControlledAI() == true && - this->gameSettings->isNetworkGame() == true && - NetworkManager::getInstance().getNetworkRole() == nrServer) { + if(executeCommandOverNetwork() == true) { //Unit *unit = world->getFaction(factionIndex)->getUnit(unitIndex); const Unit *unit = getMyUnit(unitIndex); @@ -205,10 +208,7 @@ CommandResult AiInterface::giveCommand(int unitIndex, const CommandType *command throw runtime_error(sError); } - if(this->gameSettings->getEnableServerControlledAI() == true && - this->gameSettings->isNetworkGame() == true && - NetworkManager::getInstance().getNetworkRole() == nrServer) { - + if(executeCommandOverNetwork() == true) { //Unit *unit = world->getFaction(factionIndex)->getUnit(unitIndex); const Unit *unit = getMyUnit(unitIndex); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] unitIndex = %d\nunit = [%s]\ncommandType = %d - [%s]\nut = %p\n",__FILE__,__FUNCTION__,__LINE__,unitIndex,unit->toString().c_str(),commandType->getId(),commandType->toString().c_str(),ut); @@ -261,10 +261,7 @@ CommandResult AiInterface::giveCommand(int unitIndex, const CommandType *command throw runtime_error(sError); } - if(this->gameSettings->getEnableServerControlledAI() == true && - this->gameSettings->isNetworkGame() == true && - NetworkManager::getInstance().getNetworkRole() == nrServer) { - + if(executeCommandOverNetwork() == true) { Unit *targetUnit = u; //Unit *unit = world->getFaction(factionIndex)->getUnit(unitIndex); const Unit *unit = getMyUnit(unitIndex); diff --git a/source/glest_game/ai/ai_interface.h b/source/glest_game/ai/ai_interface.h index 40921845..1e43d256 100644 --- a/source/glest_game/ai/ai_interface.h +++ b/source/glest_game/ai/ai_interface.h @@ -90,6 +90,7 @@ public: private: string getLogFilename() const {return "ai"+intToStr(factionIndex)+".log";} + bool executeCommandOverNetwork(); }; }}//end namespace diff --git a/source/glest_game/ai/ai_rule.cpp b/source/glest_game/ai/ai_rule.cpp index 1ca488e8..e48b27f2 100644 --- a/source/glest_game/ai/ai_rule.cpp +++ b/source/glest_game/ai/ai_rule.cpp @@ -1,7 +1,7 @@ // ============================================================== // This file is part of Glest (www.glest.org) // -// Copyright (C) 2001-2008 Martiņo Figueroa +// Copyright (C) 2001-2008 Martio Figueroa // // You can redistribute this code and/or modify it under // the terms of the GNU General Public License as published @@ -197,7 +197,8 @@ void AiRuleAddTasks::execute(){ ai->addPriorityTask(new ProduceTask(ucWorker)); } else{ - if(ai->getAiInterface()->getControlType()==ctCpuMega) + if( ai->getAiInterface()->getControlType() == ctCpuMega || + ai->getAiInterface()->getControlType() == ctNetworkCpuMega) { //workers if(workerCount<5) ai->addTask(new ProduceTask(ucWorker)); @@ -235,7 +236,8 @@ void AiRuleAddTasks::execute(){ if(upgradeCount==2 && workerCount>15) ai->addTask(new UpgradeTask()); if(ai->isStableBase()) ai->addTask(new UpgradeTask()); } - else if(ai->getAiInterface()->getControlType()==ctCpuEasy) + else if(ai->getAiInterface()->getControlType() == ctCpuEasy || + ai->getAiInterface()->getControlType() == ctNetworkCpuEasy) {// Easy CPU //workers if(workerCountaddTask(new ProduceTask(ucWorker)); @@ -522,7 +524,8 @@ void AiRuleProduce::produceSpecific(const ProduceTask *pt){ //produce from random producer if(!producers.empty()){ - if(aiInterface->getControlType()==ctCpuMega) + if( aiInterface->getControlType() == ctCpuMega || + aiInterface->getControlType() == ctNetworkCpuMega) {// mega cpu trys to balance the commands to the producers int randomstart=ai->getRandom()->randRange(0, producers.size()-1); int lowestCommandCount=1000000; diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index e4f31bc0..83130e5f 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -496,11 +496,16 @@ void Game::init(bool initForPreviewOnly) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] creating AI's\n",__FILE__,__FUNCTION__,__LINE__); - //create IAs + //create AIs + + bool enableServerControlledAI = this->gameSettings.getEnableServerControlledAI(); + bool isNetworkGame = this->gameSettings.isNetworkGame(); + NetworkRole role = networkManager.getNetworkRole(); + aiInterfaces.resize(world.getFactionCount()); for(int i=0; i < world.getFactionCount(); ++i) { Faction *faction= world.getFaction(i); - if(faction->getCpuControl()) { + if(faction->getCpuControl(enableServerControlledAI,isNetworkGame,role) == true) { aiInterfaces[i]= new AiInterface(*this, i, faction->getTeam()); logger.add("Creating AI for faction " + intToStr(i), true); } @@ -627,21 +632,25 @@ void Game::update() { NetworkManager &networkManager= NetworkManager::getInstance(); //update - for(int i=0; igameSettings.getEnableServerControlledAI() == false || - this->gameSettings.isNetworkGame() == false || - (this->gameSettings.isNetworkGame() == true && networkManager.getNetworkRole() == nrServer)) { - //AiInterface - for(int i=0; igetCpuControl() && scriptManager.getPlayerModifiers(i)->getAiEnabled()){ - aiInterfaces[i]->update(); - } + bool enableServerControlledAI = this->gameSettings.getEnableServerControlledAI(); + bool isNetworkGame = this->gameSettings.isNetworkGame(); + NetworkRole role = networkManager.getNetworkRole(); + + //AiInterface + for(int i = 0; i < world.getFactionCount(); ++i) { + Faction *faction = world.getFaction(i); + if( faction->getCpuControl(enableServerControlledAI,isNetworkGame,role) == true && + scriptManager.getPlayerModifiers(i)->getAiEnabled() == true) { + + aiInterfaces[i]->update(); } } + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld [AI updates]\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); if(chrono.getMillis() > 0) chrono.start(); diff --git a/source/glest_game/game/game_constants.h b/source/glest_game/game/game_constants.h index b8719ae6..3fdd7ede 100644 --- a/source/glest_game/game/game_constants.h +++ b/source/glest_game/game/game_constants.h @@ -33,16 +33,27 @@ enum TravelState { tsImpossible }; -enum ControlType{ +enum ControlType { ctClosed, ctCpuEasy, ctCpu, ctCpuUltra, ctCpuMega, ctNetwork, - ctHuman + ctHuman, + + ctNetworkCpuEasy, + ctNetworkCpu, + ctNetworkCpuUltra, + ctNetworkCpuMega + }; +enum NetworkRole { + nrServer, + nrClient, + nrIdle +}; enum FactionPersonalityType { fpt_Normal, diff --git a/source/glest_game/main/battle_end.cpp b/source/glest_game/main/battle_end.cpp index 241318cf..5f16441f 100644 --- a/source/glest_game/main/battle_end.cpp +++ b/source/glest_game/main/battle_end.cpp @@ -120,6 +120,19 @@ void BattleEnd::render(){ controlString= lang.get("Human"); break; + case ctNetworkCpuEasy: + controlString= lang.get("NetworkCpuEasy"); + break; + case ctNetworkCpu: + controlString= lang.get("NetworkCpu"); + break; + case ctNetworkCpuUltra: + controlString= lang.get("NetworkCpuUltra"); + break; + case ctNetworkCpuMega: + controlString= lang.get("NetworkCpuMega"); + break; + default: assert(false); }; diff --git a/source/glest_game/menu/menu_state_connected_game.cpp b/source/glest_game/menu/menu_state_connected_game.cpp index 6fcb803a..6f912c51 100644 --- a/source/glest_game/menu/menu_state_connected_game.cpp +++ b/source/glest_game/menu/menu_state_connected_game.cpp @@ -297,6 +297,13 @@ MenuStateConnectedGame::MenuStateConnectedGame(Program *program, MainMenu *mainM controlItems.push_back(lang.get("Network")); controlItems.push_back(lang.get("Human")); + if(config.getBool("EnableNetworkCpu","false") == true) { + controlItems.push_back(lang.get("NetworkCpuEasy")); + controlItems.push_back(lang.get("NetworkCpu")); + controlItems.push_back(lang.get("NetworkCpuUltra")); + controlItems.push_back(lang.get("NetworkCpuMega")); + } + for(int i = 1; i <= GameConstants::maxPlayers; ++i) { teamItems.push_back(intToStr(i)); } diff --git a/source/glest_game/menu/menu_state_custom_game.cpp b/source/glest_game/menu/menu_state_custom_game.cpp index 17a4aaea..fdf3fcaa 100644 --- a/source/glest_game/menu/menu_state_custom_game.cpp +++ b/source/glest_game/menu/menu_state_custom_game.cpp @@ -413,6 +413,13 @@ MenuStateCustomGame::MenuStateCustomGame(Program *program, MainMenu *mainMenu, b controlItems.push_back(lang.get("Network")); controlItems.push_back(lang.get("Human")); + if(config.getBool("EnableNetworkCpu","false") == true) { + controlItems.push_back(lang.get("NetworkCpuEasy")); + controlItems.push_back(lang.get("NetworkCpu")); + controlItems.push_back(lang.get("NetworkCpuUltra")); + controlItems.push_back(lang.get("NetworkCpuMega")); + } + for(int i = 1; i <= GameConstants::maxPlayers; ++i) { teamItems.push_back(intToStr(i)); } diff --git a/source/glest_game/network/network_manager.h b/source/glest_game/network/network_manager.h index 2c5c1d16..1f231709 100644 --- a/source/glest_game/network/network_manager.h +++ b/source/glest_game/network/network_manager.h @@ -28,13 +28,7 @@ namespace Glest{ namespace Game{ // class NetworkManager // ===================================================== -enum NetworkRole{ - nrServer, - nrClient, - nrIdle -}; - -class NetworkManager{ +class NetworkManager { private: GameNetworkInterface* gameNetworkInterface; NetworkRole networkRole; diff --git a/source/glest_game/type_instances/faction.cpp b/source/glest_game/type_instances/faction.cpp index 8208663b..dca61a6a 100644 --- a/source/glest_game/type_instances/faction.cpp +++ b/source/glest_game/type_instances/faction.cpp @@ -110,8 +110,28 @@ int Faction::getStoreAmount(const ResourceType *rt) const{ return 0; } -bool Faction::getCpuControl() const{ - return control==ctCpuEasy ||control==ctCpu || control==ctCpuUltra|| control==ctCpuMega; +bool Faction::getCpuControl(bool enableServerControlledAI,bool isNetworkGame, NetworkRole role) const { + bool result = false; + if(enableServerControlledAI == false || isNetworkGame == false) { + result = (control == ctCpuEasy ||control == ctCpu || control == ctCpuUltra || control == ctCpuMega); + } + else { + if(isNetworkGame == true) { + if(role == nrServer) { + result = (control == ctCpuEasy ||control == ctCpu || control == ctCpuUltra || control == ctCpuMega); + } + else { + result = (control == ctNetworkCpuEasy ||control == ctNetworkCpu || control == ctNetworkCpuUltra || control == ctNetworkCpuMega); + } + } + } + + return result; +} + +bool Faction::getCpuControl() const { + return control == ctCpuEasy ||control == ctCpu || control == ctCpuUltra || control == ctCpuMega || + control == ctNetworkCpuEasy ||control == ctNetworkCpu || control == ctNetworkCpuUltra || control == ctNetworkCpuMega; } // ==================== upgrade manager ==================== diff --git a/source/glest_game/type_instances/faction.h b/source/glest_game/type_instances/faction.h index f974134d..9c4f4e2d 100644 --- a/source/glest_game/type_instances/faction.h +++ b/source/glest_game/type_instances/faction.h @@ -92,6 +92,7 @@ public: const FactionType *getType() const {return factionType;} int getIndex() const {return index;} int getTeam() const {return teamIndex;} + bool getCpuControl(bool enableServerControlledAI, bool isNetworkGame, NetworkRole role) const; bool getCpuControl() const; bool getCpuEasyControl() const {return control==ctCpuEasy;} bool getCpuUltraControl() const {return control==ctCpuUltra;}