2011-01-20 15:56:30 +00:00
|
|
|
// ==============================================================
|
|
|
|
// This file is part of Glest (www.glest.org)
|
|
|
|
//
|
2011-12-14 07:40:48 +00:00
|
|
|
// Copyright (C) 2001-2008 Martiño Figueroa
|
2011-01-20 15:56:30 +00:00
|
|
|
//
|
|
|
|
// You can redistribute this code and/or modify it under
|
|
|
|
// the terms of the GNU General Public License as published
|
|
|
|
// by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version
|
|
|
|
// ==============================================================
|
|
|
|
|
|
|
|
#ifndef _GLEST_GAME_AIINTERFACE_H_
|
|
|
|
#define _GLEST_GAME_AIINTERFACE_H_
|
|
|
|
|
|
|
|
#include "world.h"
|
|
|
|
#include "commander.h"
|
|
|
|
#include "command.h"
|
|
|
|
#include "conversion.h"
|
|
|
|
#include "ai.h"
|
|
|
|
#include "game_settings.h"
|
|
|
|
#include <map>
|
|
|
|
#include "leak_dumper.h"
|
|
|
|
|
|
|
|
using Shared::Util::intToStr;
|
|
|
|
|
|
|
|
namespace Glest{ namespace Game{
|
|
|
|
|
|
|
|
// =====================================================
|
|
|
|
// class AiInterface
|
|
|
|
//
|
|
|
|
/// The AI will interact with the game through this interface
|
|
|
|
// =====================================================
|
|
|
|
|
2013-01-10 21:16:28 +00:00
|
|
|
class AiInterfaceThread : public BaseThread, public SlaveThreadControllerInterface {
|
2013-01-01 18:55:26 +00:00
|
|
|
protected:
|
|
|
|
|
|
|
|
AiInterface *aiIntf;
|
|
|
|
Semaphore semTaskSignalled;
|
|
|
|
Mutex *triggerIdMutex;
|
|
|
|
std::pair<int,bool> frameIndex;
|
2013-01-10 21:16:28 +00:00
|
|
|
MasterSlaveThreadController *masterController;
|
2013-01-01 18:55:26 +00:00
|
|
|
|
|
|
|
virtual void setQuitStatus(bool value);
|
|
|
|
virtual void setTaskCompleted(int frameIndex);
|
|
|
|
|
|
|
|
public:
|
|
|
|
AiInterfaceThread(AiInterface *aiIntf);
|
|
|
|
virtual ~AiInterfaceThread();
|
|
|
|
virtual void execute();
|
|
|
|
void signal(int frameIndex);
|
|
|
|
bool isSignalCompleted(int frameIndex);
|
2013-01-10 21:16:28 +00:00
|
|
|
|
|
|
|
virtual void setMasterController(MasterSlaveThreadController *master) { masterController = master; }
|
|
|
|
virtual void signalSlave(void *userdata) { signal(*((int *)(userdata))); }
|
|
|
|
|
2013-01-27 07:26:35 +00:00
|
|
|
virtual void signalQuit();
|
2013-02-16 10:07:36 +00:00
|
|
|
virtual bool canShutdown(bool deleteSelfIfShutdownDelayed=false);
|
2013-01-01 18:55:26 +00:00
|
|
|
};
|
|
|
|
|
2011-01-20 15:56:30 +00:00
|
|
|
class AiInterface {
|
|
|
|
private:
|
|
|
|
World *world;
|
|
|
|
Commander *commander;
|
|
|
|
Console *console;
|
|
|
|
GameSettings *gameSettings;
|
|
|
|
|
|
|
|
Ai ai;
|
|
|
|
|
|
|
|
int timer;
|
|
|
|
int factionIndex;
|
|
|
|
int teamIndex;
|
|
|
|
|
|
|
|
//config
|
|
|
|
bool redir;
|
|
|
|
int logLevel;
|
2012-03-17 19:22:03 +00:00
|
|
|
std::string aiLogFile;
|
|
|
|
FILE *fp;
|
|
|
|
|
2011-01-20 15:56:30 +00:00
|
|
|
std::map<const ResourceType *,int> cacheUnitHarvestResourceLookup;
|
|
|
|
|
2013-01-01 18:55:26 +00:00
|
|
|
Mutex *aiMutex;
|
|
|
|
|
|
|
|
AiInterfaceThread *workerThread;
|
2013-01-12 01:55:13 +00:00
|
|
|
std::vector<Vec2i> enemyWarningPositionList;
|
2013-01-01 18:55:26 +00:00
|
|
|
|
2011-01-20 15:56:30 +00:00
|
|
|
public:
|
2011-01-11 22:09:46 +00:00
|
|
|
AiInterface(Game &game, int factionIndex, int teamIndex, int useStartLocation=-1);
|
2011-01-20 15:56:30 +00:00
|
|
|
~AiInterface();
|
|
|
|
|
2013-05-17 05:01:23 +00:00
|
|
|
AiInterface(const AiInterface& obj) {
|
2013-05-17 05:21:14 +00:00
|
|
|
init();
|
2013-05-17 05:01:23 +00:00
|
|
|
throw megaglest_runtime_error("class AiInterface is NOT safe to copy!");
|
|
|
|
}
|
|
|
|
AiInterface & operator=(const AiInterface& obj) {
|
2013-05-17 05:21:14 +00:00
|
|
|
init();
|
2013-05-17 05:01:23 +00:00
|
|
|
throw megaglest_runtime_error("class AiInterface is NOT safe to assign!");
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:56:30 +00:00
|
|
|
//main
|
|
|
|
void update();
|
|
|
|
|
2013-01-12 01:55:13 +00:00
|
|
|
std::vector<Vec2i> getEnemyWarningPositionList() const { return enemyWarningPositionList; }
|
|
|
|
void removeEnemyWarningPositionFromList(Vec2i &checkPos);
|
|
|
|
|
2013-01-01 18:55:26 +00:00
|
|
|
inline Mutex * getMutex() {return aiMutex;}
|
|
|
|
|
|
|
|
void signalWorkerThread(int frameIndex);
|
|
|
|
bool isWorkerThreadSignalCompleted(int frameIndex);
|
2013-01-10 21:16:28 +00:00
|
|
|
AiInterfaceThread *getWorkerThread() { return workerThread; }
|
2013-01-01 18:55:26 +00:00
|
|
|
|
2013-01-12 01:11:53 +00:00
|
|
|
bool isLogLevelEnabled(int level);
|
|
|
|
|
2011-01-20 15:56:30 +00:00
|
|
|
//get
|
|
|
|
int getTimer() const {return timer;}
|
|
|
|
int getFactionIndex() const {return factionIndex;}
|
|
|
|
|
|
|
|
//misc
|
|
|
|
void printLog(int logLevel, const string &s);
|
|
|
|
|
|
|
|
//interact
|
2012-11-10 19:39:55 +00:00
|
|
|
std::pair<CommandResult,string> giveCommand(int unitIndex, CommandClass commandClass, const Vec2i &pos=Vec2i(0));
|
|
|
|
std::pair<CommandResult,string> giveCommand(int unitIndex, const CommandType *commandType, const Vec2i &pos, const UnitType* unitType);
|
|
|
|
std::pair<CommandResult,string> giveCommand(int unitIndex, const CommandType *commandType, const Vec2i &pos, int unitGroupCommandId);
|
|
|
|
std::pair<CommandResult,string> giveCommand(int unitIndex, const CommandType *commandType, Unit *u= NULL);
|
|
|
|
std::pair<CommandResult,string> giveCommand(const Unit *unit, const CommandType *commandType, const Vec2i &pos, int unitGroupCommandId);
|
2011-01-20 15:56:30 +00:00
|
|
|
|
2012-11-10 19:39:55 +00:00
|
|
|
std::pair<CommandResult,string> giveCommandSwitchTeamVote(const Faction* faction, SwitchTeamVote *vote);
|
2011-09-21 06:51:28 +00:00
|
|
|
|
2011-01-20 15:56:30 +00:00
|
|
|
//get data
|
|
|
|
const ControlType getControlType();
|
|
|
|
int getMapMaxPlayers();
|
|
|
|
Vec2i getHomeLocation();
|
|
|
|
Vec2i getStartLocation(int locationIndex);
|
|
|
|
int getFactionCount();
|
|
|
|
int getMyUnitCount() const;
|
|
|
|
int getMyUpgradeCount() const;
|
|
|
|
int onSightUnitCount();
|
|
|
|
const Resource *getResource(const ResourceType *rt);
|
|
|
|
const Unit *getMyUnit(int unitIndex);
|
2011-05-01 05:36:04 +00:00
|
|
|
Unit *getMyUnitPtr(int unitIndex);
|
2011-01-20 15:56:30 +00:00
|
|
|
const Unit *getOnSightUnit(int unitIndex);
|
|
|
|
const FactionType *getMyFactionType();
|
|
|
|
Faction *getMyFaction();
|
|
|
|
const TechTree *getTechTree();
|
2011-06-25 18:14:20 +00:00
|
|
|
bool isResourceInRegion(const Vec2i &pos, const ResourceType *rt, Vec2i &resourcePos, int range) const;
|
2011-01-20 15:56:30 +00:00
|
|
|
bool isResourceNear(const Vec2i &pos, const ResourceType *rt, Vec2i &resourcePos, Faction *faction, bool fallbackToPeersHarvestingSameResource) const;
|
|
|
|
bool getNearestSightedResource(const ResourceType *rt, const Vec2i &pos, Vec2i &resultPos, bool usableResourceTypeOnly);
|
|
|
|
bool isAlly(const Unit *unit) const;
|
|
|
|
bool isAlly(int factionIndex) const;
|
|
|
|
bool reqsOk(const RequirableType *rt);
|
|
|
|
bool reqsOk(const CommandType *ct);
|
2012-03-27 03:23:03 +00:00
|
|
|
bool checkCosts(const ProducibleType *pt, const CommandType *ct);
|
2011-01-20 15:56:30 +00:00
|
|
|
bool isFreeCells(const Vec2i &pos, int size, Field field);
|
2011-02-04 06:34:32 +00:00
|
|
|
const Unit *getFirstOnSightEnemyUnit(Vec2i &pos, Field &field, int radius);
|
2011-02-25 16:32:27 +00:00
|
|
|
Map * getMap();
|
2011-03-28 21:04:47 +00:00
|
|
|
World * getWorld() { return world; }
|
2011-01-20 15:56:30 +00:00
|
|
|
|
2011-04-06 15:44:33 +00:00
|
|
|
bool factionUsesResourceType(const FactionType *factionType, const ResourceType *rt);
|
|
|
|
|
2012-03-10 03:27:25 +00:00
|
|
|
void saveGame(XmlNode *rootNode) const;
|
2012-03-13 15:21:25 +00:00
|
|
|
void loadGame(const XmlNode *rootNode, Faction *faction);
|
2012-03-10 03:27:25 +00:00
|
|
|
|
2011-01-20 15:56:30 +00:00
|
|
|
private:
|
|
|
|
string getLogFilename() const {return "ai"+intToStr(factionIndex)+".log";}
|
|
|
|
bool executeCommandOverNetwork();
|
2013-05-17 05:21:14 +00:00
|
|
|
|
|
|
|
void init();
|
2011-01-20 15:56:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}}//end namespace
|
|
|
|
|
|
|
|
#endif
|