- 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++;
}
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)

View File

@ -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);

View File

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

View File

@ -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(workerCount<buildingCount+2) ai->addTask(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;

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__);
//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; i<updateLoops; ++i){
for(int i = 0; i < updateLoops; ++i) {
chrono.start();
Renderer &renderer= Renderer::getInstance();
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
if( this->gameSettings.getEnableServerControlledAI() == false ||
this->gameSettings.isNetworkGame() == false ||
(this->gameSettings.isNetworkGame() == true && networkManager.getNetworkRole() == nrServer)) {
//AiInterface
for(int i=0; i<world.getFactionCount(); ++i){
if(world.getFaction(i)->getCpuControl() && 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();

View File

@ -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,

View File

@ -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);
};

View File

@ -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));
}

View File

@ -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));
}

View File

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

View File

@ -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 ====================

View File

@ -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;}