- initial logic to allow client side controlled AI

This commit is contained in:
Mark Vejvoda 2010-09-14 19:10:37 +00:00
parent 689b810d46
commit f84a710d2c
12 changed files with 111 additions and 45 deletions

View File

@ -396,7 +396,8 @@ void Ai::massiveAttack(const Vec2i &pos, Field field, bool ultraAttack){
producerWarriorCount++; producerWarriorCount++;
} }
if(aiInterface->getControlType()==ctCpuMega) if( aiInterface->getControlType() == ctCpuMega ||
aiInterface->getControlType() == ctNetworkCpuMega)
{ {
if(producerWarriorCount>maxProducerWarriors) 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; minWarriors+= 1;
} }
else if(aiInterface->getControlType()==ctCpuMega) else if(aiInterface->getControlType() == ctCpuMega ||
aiInterface->getControlType() == ctNetworkCpuMega)
{ {
minWarriors+= 3; minWarriors+= 3;
if(minWarriors>maxMinWarriors-1 || randomMinWarriorsReached) if(minWarriors>maxMinWarriors-1 || randomMinWarriorsReached)

View File

@ -1,7 +1,7 @@
// ============================================================== // ==============================================================
// This file is part of Glest (www.glest.org) // 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 // You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published // the terms of the GNU General Public License as published
@ -92,13 +92,18 @@ void AiInterface::printLog(int logLevel, const string &s){
// ==================== interaction ==================== // ==================== 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){ CommandResult AiInterface::giveCommand(int unitIndex, CommandClass commandClass, const Vec2i &pos){
assert(this->gameSettings != NULL); assert(this->gameSettings != NULL);
if(this->gameSettings->getEnableServerControlledAI() == true && if(executeCommandOverNetwork() == true) {
this->gameSettings->isNetworkGame() == true &&
NetworkManager::getInstance().getNetworkRole() == nrServer) {
//Unit *unit = world->getFaction(factionIndex)->getUnit(unitIndex); //Unit *unit = world->getFaction(factionIndex)->getUnit(unitIndex);
const Unit *unit = getMyUnit(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); 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); throw runtime_error(sError);
} }
if(this->gameSettings->getEnableServerControlledAI() == true && if(executeCommandOverNetwork() == true) {
this->gameSettings->isNetworkGame() == true &&
NetworkManager::getInstance().getNetworkRole() == nrServer) {
//Unit *unit = world->getFaction(factionIndex)->getUnit(unitIndex); //Unit *unit = world->getFaction(factionIndex)->getUnit(unitIndex);
const Unit *unit = getMyUnit(unitIndex); const Unit *unit = getMyUnit(unitIndex);
@ -205,10 +208,7 @@ CommandResult AiInterface::giveCommand(int unitIndex, const CommandType *command
throw runtime_error(sError); throw runtime_error(sError);
} }
if(this->gameSettings->getEnableServerControlledAI() == true && if(executeCommandOverNetwork() == true) {
this->gameSettings->isNetworkGame() == true &&
NetworkManager::getInstance().getNetworkRole() == nrServer) {
//Unit *unit = world->getFaction(factionIndex)->getUnit(unitIndex); //Unit *unit = world->getFaction(factionIndex)->getUnit(unitIndex);
const Unit *unit = getMyUnit(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); 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); throw runtime_error(sError);
} }
if(this->gameSettings->getEnableServerControlledAI() == true && if(executeCommandOverNetwork() == true) {
this->gameSettings->isNetworkGame() == true &&
NetworkManager::getInstance().getNetworkRole() == nrServer) {
Unit *targetUnit = u; Unit *targetUnit = u;
//Unit *unit = world->getFaction(factionIndex)->getUnit(unitIndex); //Unit *unit = world->getFaction(factionIndex)->getUnit(unitIndex);
const Unit *unit = getMyUnit(unitIndex); const Unit *unit = getMyUnit(unitIndex);

View File

@ -90,6 +90,7 @@ public:
private: private:
string getLogFilename() const {return "ai"+intToStr(factionIndex)+".log";} string getLogFilename() const {return "ai"+intToStr(factionIndex)+".log";}
bool executeCommandOverNetwork();
}; };
}}//end namespace }}//end namespace

View File

@ -1,7 +1,7 @@
// ============================================================== // ==============================================================
// This file is part of Glest (www.glest.org) // 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 // You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published // the terms of the GNU General Public License as published
@ -197,7 +197,8 @@ void AiRuleAddTasks::execute(){
ai->addPriorityTask(new ProduceTask(ucWorker)); ai->addPriorityTask(new ProduceTask(ucWorker));
} }
else{ else{
if(ai->getAiInterface()->getControlType()==ctCpuMega) if( ai->getAiInterface()->getControlType() == ctCpuMega ||
ai->getAiInterface()->getControlType() == ctNetworkCpuMega)
{ {
//workers //workers
if(workerCount<5) ai->addTask(new ProduceTask(ucWorker)); 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(upgradeCount==2 && workerCount>15) ai->addTask(new UpgradeTask());
if(ai->isStableBase()) 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 {// Easy CPU
//workers //workers
if(workerCount<buildingCount+2) ai->addTask(new ProduceTask(ucWorker)); if(workerCount<buildingCount+2) ai->addTask(new ProduceTask(ucWorker));
@ -522,7 +524,8 @@ void AiRuleProduce::produceSpecific(const ProduceTask *pt){
//produce from random producer //produce from random producer
if(!producers.empty()){ if(!producers.empty()){
if(aiInterface->getControlType()==ctCpuMega) if( aiInterface->getControlType() == ctCpuMega ||
aiInterface->getControlType() == ctNetworkCpuMega)
{// mega cpu trys to balance the commands to the producers {// mega cpu trys to balance the commands to the producers
int randomstart=ai->getRandom()->randRange(0, producers.size()-1); int randomstart=ai->getRandom()->randRange(0, producers.size()-1);
int lowestCommandCount=1000000; int lowestCommandCount=1000000;

View File

@ -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__); 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()); aiInterfaces.resize(world.getFactionCount());
for(int i=0; i < world.getFactionCount(); ++i) { for(int i=0; i < world.getFactionCount(); ++i) {
Faction *faction= world.getFaction(i); Faction *faction= world.getFaction(i);
if(faction->getCpuControl()) { if(faction->getCpuControl(enableServerControlledAI,isNetworkGame,role) == true) {
aiInterfaces[i]= new AiInterface(*this, i, faction->getTeam()); aiInterfaces[i]= new AiInterface(*this, i, faction->getTeam());
logger.add("Creating AI for faction " + intToStr(i), true); logger.add("Creating AI for faction " + intToStr(i), true);
} }
@ -627,21 +632,25 @@ void Game::update() {
NetworkManager &networkManager= NetworkManager::getInstance(); NetworkManager &networkManager= NetworkManager::getInstance();
//update //update
for(int i=0; i<updateLoops; ++i){ for(int i = 0; i < updateLoops; ++i) {
chrono.start(); chrono.start();
Renderer &renderer= Renderer::getInstance(); Renderer &renderer= Renderer::getInstance();
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
if( this->gameSettings.getEnableServerControlledAI() == false || bool enableServerControlledAI = this->gameSettings.getEnableServerControlledAI();
this->gameSettings.isNetworkGame() == false || bool isNetworkGame = this->gameSettings.isNetworkGame();
(this->gameSettings.isNetworkGame() == true && networkManager.getNetworkRole() == nrServer)) { NetworkRole role = networkManager.getNetworkRole();
//AiInterface
for(int i=0; i<world.getFactionCount(); ++i){ //AiInterface
if(world.getFaction(i)->getCpuControl() && scriptManager.getPlayerModifiers(i)->getAiEnabled()){ for(int i = 0; i < world.getFactionCount(); ++i) {
aiInterfaces[i]->update(); 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) 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(); if(chrono.getMillis() > 0) chrono.start();

View File

@ -33,16 +33,27 @@ enum TravelState {
tsImpossible tsImpossible
}; };
enum ControlType{ enum ControlType {
ctClosed, ctClosed,
ctCpuEasy, ctCpuEasy,
ctCpu, ctCpu,
ctCpuUltra, ctCpuUltra,
ctCpuMega, ctCpuMega,
ctNetwork, ctNetwork,
ctHuman ctHuman,
ctNetworkCpuEasy,
ctNetworkCpu,
ctNetworkCpuUltra,
ctNetworkCpuMega
}; };
enum NetworkRole {
nrServer,
nrClient,
nrIdle
};
enum FactionPersonalityType { enum FactionPersonalityType {
fpt_Normal, fpt_Normal,

View File

@ -120,6 +120,19 @@ void BattleEnd::render(){
controlString= lang.get("Human"); controlString= lang.get("Human");
break; 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: default:
assert(false); assert(false);
}; };

View File

@ -297,6 +297,13 @@ MenuStateConnectedGame::MenuStateConnectedGame(Program *program, MainMenu *mainM
controlItems.push_back(lang.get("Network")); controlItems.push_back(lang.get("Network"));
controlItems.push_back(lang.get("Human")); 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) { for(int i = 1; i <= GameConstants::maxPlayers; ++i) {
teamItems.push_back(intToStr(i)); teamItems.push_back(intToStr(i));
} }

View File

@ -413,6 +413,13 @@ MenuStateCustomGame::MenuStateCustomGame(Program *program, MainMenu *mainMenu, b
controlItems.push_back(lang.get("Network")); controlItems.push_back(lang.get("Network"));
controlItems.push_back(lang.get("Human")); 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) { for(int i = 1; i <= GameConstants::maxPlayers; ++i) {
teamItems.push_back(intToStr(i)); teamItems.push_back(intToStr(i));
} }

View File

@ -28,13 +28,7 @@ namespace Glest{ namespace Game{
// class NetworkManager // class NetworkManager
// ===================================================== // =====================================================
enum NetworkRole{ class NetworkManager {
nrServer,
nrClient,
nrIdle
};
class NetworkManager{
private: private:
GameNetworkInterface* gameNetworkInterface; GameNetworkInterface* gameNetworkInterface;
NetworkRole networkRole; NetworkRole networkRole;

View File

@ -110,8 +110,28 @@ int Faction::getStoreAmount(const ResourceType *rt) const{
return 0; return 0;
} }
bool Faction::getCpuControl() const{ bool Faction::getCpuControl(bool enableServerControlledAI,bool isNetworkGame, NetworkRole role) const {
return control==ctCpuEasy ||control==ctCpu || control==ctCpuUltra|| control==ctCpuMega; 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 ==================== // ==================== upgrade manager ====================

View File

@ -92,6 +92,7 @@ public:
const FactionType *getType() const {return factionType;} const FactionType *getType() const {return factionType;}
int getIndex() const {return index;} int getIndex() const {return index;}
int getTeam() const {return teamIndex;} int getTeam() const {return teamIndex;}
bool getCpuControl(bool enableServerControlledAI, bool isNetworkGame, NetworkRole role) const;
bool getCpuControl() const; bool getCpuControl() const;
bool getCpuEasyControl() const {return control==ctCpuEasy;} bool getCpuEasyControl() const {return control==ctCpuEasy;}
bool getCpuUltraControl() const {return control==ctCpuUltra;} bool getCpuUltraControl() const {return control==ctCpuUltra;}