2013-12-09 15:23:56 +01:00
|
|
|
var PlayerID = -1;
|
|
|
|
|
2013-12-30 11:04:59 +01:00
|
|
|
var API3 = (function() {
|
|
|
|
|
|
|
|
var m = {};
|
|
|
|
|
2014-02-01 04:29:55 +01:00
|
|
|
m.DebugEnabled = false;
|
2013-12-30 15:28:30 +01:00
|
|
|
|
2013-12-30 11:04:59 +01:00
|
|
|
m.BaseAI = function(settings)
|
2013-12-09 15:23:56 +01:00
|
|
|
{
|
|
|
|
if (!settings)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.player = settings.player;
|
|
|
|
|
|
|
|
// played turn, in case you don't want the AI to play every turn.
|
|
|
|
this.turn = 0;
|
2013-12-30 11:04:59 +01:00
|
|
|
};
|
2013-12-09 15:23:56 +01:00
|
|
|
|
|
|
|
//Return a simple object (using no classes etc) that will be serialized into saved games
|
2013-12-30 11:04:59 +01:00
|
|
|
m.BaseAI.prototype.Serialize = function()
|
2013-12-09 15:23:56 +01:00
|
|
|
{
|
|
|
|
// TODO: ought to get the AI script subclass to serialize its own state
|
|
|
|
// TODO: actually this is part of a larger reflection on wether AIs should or not.
|
|
|
|
return {};
|
|
|
|
};
|
|
|
|
|
|
|
|
//Called after the constructor when loading a saved game, with 'data' being
|
|
|
|
//whatever Serialize() returned
|
2013-12-30 11:04:59 +01:00
|
|
|
m.BaseAI.prototype.Deserialize = function(data, sharedScript)
|
2013-12-09 15:23:56 +01:00
|
|
|
{
|
|
|
|
// TODO: ought to get the AI script subclass to deserialize its own state
|
|
|
|
// TODO: actually this is part of a larger reflection on wether AIs should or not.
|
2013-12-11 18:10:14 +01:00
|
|
|
this.isDeserialized = true;
|
2013-12-09 15:23:56 +01:00
|
|
|
};
|
|
|
|
|
2013-12-30 11:04:59 +01:00
|
|
|
m.BaseAI.prototype.Init = function(state, playerID, sharedAI)
|
2013-12-09 15:23:56 +01:00
|
|
|
{
|
2013-12-30 11:04:59 +01:00
|
|
|
PlayerID = playerID;
|
2013-12-09 15:23:56 +01:00
|
|
|
// define some references
|
|
|
|
this.entities = sharedAI.entities;
|
|
|
|
this.templates = sharedAI.templates;
|
|
|
|
this.passabilityClasses = sharedAI.passabilityClasses;
|
|
|
|
this.passabilityMap = sharedAI.passabilityMap;
|
|
|
|
this.territoryMap = sharedAI.territoryMap;
|
|
|
|
this.accessibility = sharedAI.accessibility;
|
|
|
|
this.terrainAnalyzer = sharedAI.terrainAnalyzer;
|
|
|
|
|
|
|
|
this.techModifications = sharedAI._techModifications[this.player];
|
|
|
|
this.playerData = sharedAI.playersData[this.player];
|
|
|
|
|
2013-12-30 11:04:59 +01:00
|
|
|
this.gameState = sharedAI.gameState[this.player];
|
2013-12-09 15:23:56 +01:00
|
|
|
this.gameState.ai = this;
|
|
|
|
this.sharedScript = sharedAI;
|
|
|
|
|
|
|
|
this.timeElapsed = sharedAI.timeElapsed;
|
|
|
|
|
2014-04-05 14:18:37 +02:00
|
|
|
this.circularMap = sharedAI.circularMap;
|
|
|
|
|
2014-07-09 23:41:14 +02:00
|
|
|
this.gameType = sharedAI.gameType;
|
|
|
|
|
2014-01-14 20:54:31 +01:00
|
|
|
this.barterPrices = sharedAI.barterPrices;
|
|
|
|
|
2013-12-09 15:23:56 +01:00
|
|
|
this.CustomInit(this.gameState, this.sharedScript);
|
|
|
|
}
|
|
|
|
|
2013-12-30 11:04:59 +01:00
|
|
|
m.BaseAI.prototype.CustomInit = function()
|
2013-12-09 15:23:56 +01:00
|
|
|
{ // AIs override this function
|
|
|
|
};
|
|
|
|
|
2013-12-30 11:04:59 +01:00
|
|
|
m.BaseAI.prototype.HandleMessage = function(state, playerID, sharedAI)
|
2013-12-09 15:23:56 +01:00
|
|
|
{
|
2013-12-30 11:04:59 +01:00
|
|
|
PlayerID = playerID;
|
2014-01-10 02:46:27 +01:00
|
|
|
this.events = sharedAI.events;
|
|
|
|
this.passabilityMap = sharedAI.passabilityMap;
|
|
|
|
this.territoryMap = sharedAI.territoryMap;
|
2013-12-09 15:23:56 +01:00
|
|
|
|
2013-12-11 18:10:14 +01:00
|
|
|
if (this.isDeserialized && this.turn !== 0)
|
|
|
|
{
|
|
|
|
this.isDeserialized = false;
|
2014-01-21 21:24:51 +01:00
|
|
|
this.Init(state, playerID, sharedAI);
|
2013-12-11 18:10:14 +01:00
|
|
|
warn("AIs don't work completely with saved games yet. You may run into idle units and unused buildings.");
|
|
|
|
} else if (this.isDeserialized)
|
|
|
|
return;
|
2013-12-09 15:23:56 +01:00
|
|
|
this.OnUpdate(sharedAI);
|
|
|
|
};
|
|
|
|
|
2013-12-30 11:04:59 +01:00
|
|
|
m.BaseAI.prototype.OnUpdate = function()
|
2013-12-09 15:23:56 +01:00
|
|
|
{ // AIs override this function
|
|
|
|
};
|
|
|
|
|
2013-12-30 11:04:59 +01:00
|
|
|
m.BaseAI.prototype.chat = function(message)
|
2013-12-09 15:23:56 +01:00
|
|
|
{
|
2014-04-25 05:41:01 +02:00
|
|
|
Engine.PostCommand(PlayerID,{"type": "aichat", "message": message});
|
2013-12-09 15:23:56 +01:00
|
|
|
};
|
2013-12-30 11:04:59 +01:00
|
|
|
m.BaseAI.prototype.chatTeam = function(message)
|
2013-12-09 15:23:56 +01:00
|
|
|
{
|
2014-04-25 05:41:01 +02:00
|
|
|
Engine.PostCommand(PlayerID,{"type": "aichat", "message": "/team " +message});
|
2013-12-09 15:23:56 +01:00
|
|
|
};
|
2013-12-30 11:04:59 +01:00
|
|
|
m.BaseAI.prototype.chatEnemies = function(message)
|
2013-12-09 15:23:56 +01:00
|
|
|
{
|
2014-04-25 05:41:01 +02:00
|
|
|
Engine.PostCommand(PlayerID,{"type": "aichat", "message": "/enemy " +message});
|
2013-12-09 15:23:56 +01:00
|
|
|
};
|
|
|
|
|
2013-12-30 11:04:59 +01:00
|
|
|
return m;
|
|
|
|
|
|
|
|
}());
|
|
|
|
|