- Some initial changes in prep for support server controlled AI

This commit is contained in:
Mark Vejvoda 2010-06-01 16:54:44 +00:00
parent 288bd3a287
commit 1c9b85418a
7 changed files with 146 additions and 52 deletions

View File

@ -30,10 +30,15 @@ using namespace Shared::Graphics;
namespace Glest{ namespace Game{
bool AiInterface::enableServerControlledAI = false;
AiInterface::AiInterface(Game &game, int factionIndex, int teamIndex){
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
this->world= game.getWorld();
this->commander= game.getCommander();
this->console= game.getConsole();
this->gameSettings = game.getGameSettings();
this->factionIndex= factionIndex;
this->teamIndex= teamIndex;
@ -55,6 +60,7 @@ AiInterface::AiInterface(Game &game, int factionIndex, int teamIndex){
fprintf(f, "%s", "Glest AI log file\n\n");
fclose(f);
}
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}
// ==================== main ====================
@ -88,37 +94,100 @@ void AiInterface::printLog(int logLevel, const string &s){
// ==================== interaction ====================
CommandResult AiInterface::giveCommand(int unitIndex, CommandClass commandClass, const Vec2i &pos){
Command *c= new Command (world->getFaction(factionIndex)->getUnit(unitIndex)->getType()->getFirstCtOfClass(commandClass), pos);
assert(this->gameSettings != NULL);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
CommandResult result = world->getFaction(factionIndex)->getUnit(unitIndex)->giveCommand(c);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
if(enableServerControlledAI == true && this->gameSettings->isNetworkGame() == true) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
return result;
CommandResult result = commander->tryGiveCommand(world->getFaction(factionIndex)->getUnit(unitIndex), world->getFaction(factionIndex)->getUnit(unitIndex)->getType()->getFirstCtOfClass(commandClass), pos, world->getFaction(factionIndex)->getUnit(unitIndex)->getType(),CardinalDir::NORTH);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
return result;
}
else {
Command *c= new Command (world->getFaction(factionIndex)->getUnit(unitIndex)->getType()->getFirstCtOfClass(commandClass), pos);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
CommandResult result = world->getFaction(factionIndex)->getUnit(unitIndex)->giveCommand(c);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
return result;
}
}
CommandResult AiInterface::giveCommand(int unitIndex, const CommandType *commandType, const Vec2i &pos){
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
CommandResult result = world->getFaction(factionIndex)->getUnit(unitIndex)->giveCommand(new Command(commandType, pos));
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
assert(this->gameSettings != NULL);
return result;
if(enableServerControlledAI == true && this->gameSettings->isNetworkGame() == true) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
CommandResult result = commander->tryGiveCommand(world->getFaction(factionIndex)->getUnit(unitIndex), commandType, pos, world->getFaction(factionIndex)->getUnit(unitIndex)->getType(),CardinalDir::NORTH);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
return result;
}
else {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
CommandResult result = world->getFaction(factionIndex)->getUnit(unitIndex)->giveCommand(new Command(commandType, pos));
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
return result;
}
}
CommandResult AiInterface::giveCommand(int unitIndex, const CommandType *commandType, const Vec2i &pos, const UnitType *ut){
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
CommandResult result = world->getFaction(factionIndex)->getUnit(unitIndex)->giveCommand(new Command(commandType, pos, ut, CardinalDir::NORTH));
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
assert(this->gameSettings != NULL);
return result;
if(enableServerControlledAI == true && this->gameSettings->isNetworkGame() == true) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
CommandResult result = commander->tryGiveCommand(world->getFaction(factionIndex)->getUnit(unitIndex), commandType, pos, ut,CardinalDir::NORTH);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
return result;
}
else {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
CommandResult result = world->getFaction(factionIndex)->getUnit(unitIndex)->giveCommand(new Command(commandType, pos, ut, CardinalDir::NORTH));
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
return result;
}
}
CommandResult AiInterface::giveCommand(int unitIndex, const CommandType *commandType, Unit *u){
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
CommandResult result = world->getFaction(factionIndex)->getUnit(unitIndex)->giveCommand(new Command(commandType, u));
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
assert(this->gameSettings != NULL);
assert(this->commander != NULL);
return result;
if(enableServerControlledAI == true && this->gameSettings->isNetworkGame() == true) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
assert(u != NULL);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
CommandResult result = commander->tryGiveCommand(u, commandType, Vec2i(0), u->getType(),CardinalDir::NORTH);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
return result;
}
else {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
CommandResult result = world->getFaction(factionIndex)->getUnit(unitIndex)->giveCommand(new Command(commandType, u));
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
return result;
}
}
// ==================== get data ====================

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
@ -17,6 +17,7 @@
#include "command.h"
#include "conversion.h"
#include "ai.h"
#include "game_settings.h"
using Shared::Util::intToStr;
@ -33,6 +34,8 @@ private:
World *world;
Commander *commander;
Console *console;
GameSettings *gameSettings;
Ai ai;
int timer;
@ -43,6 +46,8 @@ private:
bool redir;
int logLevel;
static bool enableServerControlledAI;
public:
AiInterface(Game &game, int factionIndex, int teamIndex);
@ -84,6 +89,8 @@ public:
bool checkCosts(const ProducibleType *pt);
bool isFreeCells(const Vec2i &pos, int size, Field field);
static bool getEnableServerControlledAI() { return enableServerControlledAI; }
private:
string getLogFilename() const {return "ai"+intToStr(factionIndex)+".log";}
};

View File

@ -41,12 +41,19 @@ void Commander::init(World *world){
}
CommandResult Commander::tryGiveCommand(const Unit* unit, const CommandType *commandType, const Vec2i &pos, const UnitType* unitType, CardinalDir facing, bool tryQueue) const {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
assert(this->world != NULL);
assert(unit != NULL);
assert(commandType != NULL);
assert(unitType != NULL);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
NetworkCommand networkCommand(this->world,nctGiveCommand, unit->getId(), commandType->getId(), pos, unitType->getId(), -1, facing, tryQueue);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
return pushNetworkCommand(&networkCommand);
}

View File

@ -43,12 +43,16 @@ Game *thisGamePtr = NULL;
Game::Game(Program *program, const GameSettings *gameSettings):
ProgramState(program), lastMousePos(0)
{
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
originalDisplayMsgCallback = NULL;
thisGamePtr = this;
this->gameSettings= *gameSettings;
scrollSpeed = Config::getInstance().getFloat("UiScrollSpeed","1.5");
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
mouseX=0;
mouseY=0;
mouse2d= 0;
@ -63,10 +67,13 @@ Game::Game(Program *program, const GameSettings *gameSettings):
renderNetworkStatus= false;
speed= sNormal;
showFullConsole= false;
render3DThreadManager = NULL;
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}
Game::~Game(){
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
Logger &logger= Logger::getInstance();
Renderer &renderer= Renderer::getInstance();
@ -74,25 +81,26 @@ Game::~Game(){
logger.setState(Lang::getInstance().get("Deleting"));
logger.add("Game", true);
BaseThread::shutdownAndWait(render3DThreadManager);
delete render3DThreadManager;
render3DThreadManager = NULL;
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
renderer.endGame();
SoundRenderer::getInstance().stopAllSounds();
deleteValues(aiInterfaces.begin(), aiInterfaces.end());
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
gui.end(); //selection must be cleared before deleting units
world.end(); //must die before selection because of referencers
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
thisGamePtr = NULL;
if(originalDisplayMsgCallback != NULL) {
NetworkInterface::setDisplayMessageFunction(originalDisplayMsgCallback);
}
}
// ==================== init and load ====================
int Game::ErrorDisplayMessage(const char *msg, bool exitApp) {
@ -346,7 +354,7 @@ void Game::init()
}
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] Waiting for network\n",__FILE__,__FUNCTION__);
logger.add("Waiting for network", true);
logger.add("Waiting for network players", true);
networkManager.getGameNetworkInterface()->waitUntilReady(&checksum);
std::string worldLog = world.DumpWorldToLog(true);
@ -358,18 +366,9 @@ void Game::init()
logger.add("Launching game");
BaseThread::shutdownAndWait(render3DThreadManager);
delete render3DThreadManager;
//render3DThreadManager = new SimpleTaskThread(this,0,5,true);
//render3DThreadManager->start();
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}
void Game::simpleTask() {
renderWorker();
}
// ==================== update ====================
//update
@ -394,15 +393,20 @@ void Game::update(){
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
NetworkManager &networkManager= NetworkManager::getInstance();
//update
for(int i=0; i<updateLoops; ++i){
Renderer &renderer= Renderer::getInstance();
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
//AiInterface
for(int i=0; i<world.getFactionCount(); ++i){
if(world.getFaction(i)->getCpuControl() && scriptManager.getPlayerModifiers(i)->getAiEnabled()){
aiInterfaces[i]->update();
if( AiInterface::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();
}
}
}
@ -469,12 +473,7 @@ void Game::render() {
//SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d renderFps = %d\n",__FILE__,__FUNCTION__,__LINE__,renderFps);
renderFps++;
if(render3DThreadManager == NULL) {
renderWorker();
}
else {
render3DThreadManager->setTaskSignalled(true);
}
renderWorker();
//SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d renderFps = %d\n",__FILE__,__FUNCTION__,__LINE__,renderFps);
}

View File

@ -22,7 +22,7 @@
#include "chat_manager.h"
#include "script_manager.h"
#include "game_settings.h"
#include "simple_threads.h"
//#include "simple_threads.h"
#include "network_interface.h"
using std::vector;
@ -38,7 +38,8 @@ class GraphicMessageBox;
// Main game class
// =====================================================
class Game: public ProgramState, public SimpleTaskCallbackInterface {
//class Game: public ProgramState, public SimpleTaskCallbackInterface {
class Game: public ProgramState {
public:
enum Speed{
sFast,
@ -60,7 +61,6 @@ private:
Console console;
ChatManager chatManager;
ScriptManager scriptManager;
SimpleTaskThread *render3DThreadManager;
//misc
Checksum checksum;
@ -123,8 +123,6 @@ public:
void quitGame();
virtual void simpleTask();
private:
//render
void render3d();

View File

@ -64,9 +64,19 @@ public:
const string &getScenario() const {return scenario;}
const string &getScenarioDir() const {return scenarioDir;}
const string &getFactionTypeName(int factionIndex) const {return factionTypeNames[factionIndex];}
const string &getNetworkPlayerName(int factionIndex) const {return networkPlayerNames[factionIndex];}
const string &getNetworkPlayerName(int factionIndex) const {return networkPlayerNames[factionIndex];}
ControlType getFactionControl(int factionIndex) const {return factionControls[factionIndex];}
bool isNetworkGame() const {
bool result = false;
for(int idx = 0; idx < GameConstants::maxPlayers; ++idx) {
if(factionControls[idx] == ctNetwork) {
result = true;
break;
}
}
return result;
}
int getThisFactionIndex() const {return thisFactionIndex;}
int getFactionCount() const {return factionCount;}
int getTeam(int factionIndex) const {return teams[factionIndex];}

View File

@ -584,15 +584,19 @@ void MenuStateConnectedGame::update()
//launch
if(clientInterface->getLaunchGame())
{
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] clientInterface->getLaunchGame() - A\n",__FILE__,__FUNCTION__);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
//servers.save(serversSavedFile);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] clientInterface->getLaunchGame() - B\n",__FILE__,__FUNCTION__);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
assert(clientInterface != NULL);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
program->setState(new Game(program, clientInterface->getGameSettings()));
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] clientInterface->getLaunchGame() - C\n",__FILE__,__FUNCTION__);
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__);