2011-01-12 13:29:00 +01:00
|
|
|
function AIInterface() {}
|
|
|
|
|
|
|
|
AIInterface.prototype.Schema =
|
|
|
|
"<a:component type='system'/><empty/>";
|
|
|
|
|
|
|
|
AIInterface.prototype.Init = function()
|
|
|
|
{
|
2014-01-10 02:46:27 +01:00
|
|
|
this.events = {};
|
|
|
|
this.events["Create"] = [];
|
|
|
|
this.events["Destroy"] = [];
|
|
|
|
this.events["Attacked"] = [];
|
|
|
|
this.events["RangeUpdate"] = [];
|
|
|
|
this.events["ConstructionFinished"] = [];
|
|
|
|
this.events["TrainingFinished"] = [];
|
|
|
|
this.events["AIMetadata"] = [];
|
|
|
|
this.events["PlayerDefeated"] = [];
|
|
|
|
this.events["EntityRenamed"] = [];
|
|
|
|
this.events["OwnershipChanged"] = [];
|
|
|
|
|
2011-03-03 01:16:14 +01:00
|
|
|
this.changedEntities = {};
|
2011-01-12 13:29:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
AIInterface.prototype.GetRepresentation = function()
|
|
|
|
{
|
|
|
|
var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
|
|
|
|
|
|
|
|
// Return the same game state as the GUI uses
|
2013-03-05 23:52:48 +01:00
|
|
|
var state = cmpGuiInterface.GetExtendedSimulationState(-1);
|
2011-01-12 13:29:00 +01:00
|
|
|
|
|
|
|
// Add some extra AI-specific data
|
2014-01-10 02:46:27 +01:00
|
|
|
state.events = {};
|
|
|
|
state.events["Create"] = this.events["Create"];
|
|
|
|
state.events["Destroy"] = this.events["Destroy"];
|
|
|
|
state.events["Attacked"] = this.events["Attacked"];
|
|
|
|
state.events["RangeUpdate"] = this.events["RangeUpdate"];
|
|
|
|
state.events["ConstructionFinished"] = this.events["ConstructionFinished"];
|
|
|
|
state.events["TrainingFinished"] = this.events["TrainingFinished"];
|
|
|
|
state.events["AIMetadata"] = this.events["AIMetadata"];
|
|
|
|
state.events["PlayerDefeated"] = this.events["PlayerDefeated"];
|
|
|
|
state.events["EntityRenamed"] = this.events["EntityRenamed"];
|
|
|
|
state.events["OwnershipChanged"] = this.events["OwnershipChanged"];
|
2011-01-12 13:29:00 +01:00
|
|
|
|
|
|
|
// Reset the event list for the next turn
|
2014-01-10 02:46:27 +01:00
|
|
|
this.events["Create"] = [];
|
|
|
|
this.events["Destroy"] = [];
|
|
|
|
this.events["Attacked"] = [];
|
|
|
|
this.events["RangeUpdate"] = [];
|
|
|
|
this.events["ConstructionFinished"] = [];
|
|
|
|
this.events["TrainingFinished"] = [];
|
|
|
|
this.events["AIMetadata"] = [];
|
|
|
|
this.events["PlayerDefeated"] = [];
|
|
|
|
this.events["EntityRenamed"] = [];
|
|
|
|
this.events["OwnershipChanged"] = [];
|
|
|
|
|
2011-01-16 00:35:20 +01:00
|
|
|
// Add entity representations
|
2011-03-03 01:16:14 +01:00
|
|
|
Engine.ProfileStart("proxy representations");
|
2011-01-16 00:35:20 +01:00
|
|
|
state.entities = {};
|
2011-03-03 01:16:14 +01:00
|
|
|
for (var id in this.changedEntities)
|
2011-01-16 00:35:20 +01:00
|
|
|
{
|
2011-03-03 01:16:14 +01:00
|
|
|
var aiProxy = Engine.QueryInterface(+id, IID_AIProxy);
|
|
|
|
if (aiProxy)
|
|
|
|
state.entities[id] = aiProxy.GetRepresentation();
|
2011-01-16 00:35:20 +01:00
|
|
|
}
|
2011-03-03 01:16:14 +01:00
|
|
|
this.changedEntities = {};
|
|
|
|
Engine.ProfileStop();
|
2011-01-16 00:35:20 +01:00
|
|
|
|
2011-01-12 13:29:00 +01:00
|
|
|
return state;
|
|
|
|
};
|
2013-03-05 23:52:48 +01:00
|
|
|
// Intended to be called first, during the map initialization: no caching
|
2013-12-04 17:52:44 +01:00
|
|
|
AIInterface.prototype.GetFullRepresentation = function(flushEvents)
|
2013-03-05 23:52:48 +01:00
|
|
|
{
|
|
|
|
var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
|
|
|
|
|
|
|
|
// Return the same game state as the GUI uses
|
2013-07-25 10:57:07 +02:00
|
|
|
var state = cmpGuiInterface.GetExtendedSimulationState(-1);
|
2013-03-05 23:52:48 +01:00
|
|
|
|
|
|
|
// Add some extra AI-specific data
|
2014-01-10 02:46:27 +01:00
|
|
|
state.events = {};
|
|
|
|
state.events["Create"] = this.events["Create"];
|
|
|
|
state.events["Destroy"] = this.events["Destroy"];
|
|
|
|
state.events["Attacked"] = this.events["Attacked"];
|
|
|
|
state.events["RangeUpdate"] = this.events["RangeUpdate"];
|
|
|
|
state.events["ConstructionFinished"] = this.events["ConstructionFinished"];
|
|
|
|
state.events["TrainingFinished"] = this.events["TrainingFinished"];
|
|
|
|
state.events["AIMetadata"] = this.events["AIMetadata"];
|
|
|
|
state.events["PlayerDefeated"] = this.events["PlayerDefeated"];
|
|
|
|
state.events["EntityRenamed"] = this.events["EntityRenamed"];
|
|
|
|
state.events["OwnershipChanged"] = this.events["OwnershipChanged"];
|
|
|
|
|
|
|
|
|
2013-12-04 17:52:44 +01:00
|
|
|
if (flushEvents)
|
|
|
|
{
|
2014-01-10 02:46:27 +01:00
|
|
|
state.events["Create"] = [];
|
|
|
|
state.events["Destroy"] = [];
|
|
|
|
state.events["Attacked"] = [];
|
|
|
|
state.events["RangeUpdate"] = [];
|
|
|
|
state.events["ConstructionFinished"] = [];
|
|
|
|
state.events["TrainingFinished"] = [];
|
|
|
|
state.events["AIMetadata"] = [];
|
|
|
|
state.events["PlayerDefeated"] = [];
|
|
|
|
state.events["EntityRenamed"] = [];
|
|
|
|
state.events["OwnershipChanged"] = [];
|
2013-12-04 17:52:44 +01:00
|
|
|
}
|
2014-01-10 02:46:27 +01:00
|
|
|
|
|
|
|
// Reset the event list for the next turn
|
|
|
|
this.events["Create"] = [];
|
|
|
|
this.events["Destroy"] = [];
|
|
|
|
this.events["Attacked"] = [];
|
|
|
|
this.events["RangeUpdate"] = [];
|
|
|
|
this.events["ConstructionFinished"] = [];
|
|
|
|
this.events["TrainingFinished"] = [];
|
|
|
|
this.events["AIMetadata"] = [];
|
|
|
|
this.events["PlayerDefeated"] = [];
|
|
|
|
this.events["EntityRenamed"] = [];
|
|
|
|
this.events["OwnershipChanged"] = [];
|
|
|
|
|
|
|
|
|
2013-03-05 23:52:48 +01:00
|
|
|
// Add entity representations
|
|
|
|
Engine.ProfileStart("proxy representations");
|
|
|
|
state.entities = {};
|
|
|
|
// all entities are changed in the initial state.
|
|
|
|
for (var id in this.changedEntities)
|
|
|
|
{
|
|
|
|
var aiProxy = Engine.QueryInterface(+id, IID_AIProxy);
|
|
|
|
if (aiProxy)
|
|
|
|
state.entities[id] = aiProxy.GetFullRepresentation();
|
|
|
|
}
|
|
|
|
Engine.ProfileStop();
|
|
|
|
|
|
|
|
return state;
|
|
|
|
};
|
2011-01-12 13:29:00 +01:00
|
|
|
|
2011-03-03 01:16:14 +01:00
|
|
|
AIInterface.prototype.ChangedEntity = function(ent)
|
|
|
|
{
|
|
|
|
this.changedEntities[ent] = 1;
|
|
|
|
};
|
|
|
|
|
2011-02-05 21:35:34 +01:00
|
|
|
// AIProxy sets up a load of event handlers to capture interesting things going on
|
|
|
|
// in the world, which we will report to AI. Handle those, and add a few more handlers
|
|
|
|
// for events that AIProxy won't capture.
|
2011-01-12 13:29:00 +01:00
|
|
|
|
2011-02-05 21:35:34 +01:00
|
|
|
AIInterface.prototype.PushEvent = function(type, msg)
|
2011-01-12 13:29:00 +01:00
|
|
|
{
|
2014-01-10 02:46:27 +01:00
|
|
|
if (this.events[type] === undefined)
|
|
|
|
warn("Tried to push unknown event type " + type +", please add it to AIInterface.js");
|
|
|
|
this.events[type].push(msg);
|
2011-01-12 13:29:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
AIInterface.prototype.OnGlobalPlayerDefeated = function(msg)
|
|
|
|
{
|
2014-01-10 02:46:27 +01:00
|
|
|
this.events["PlayerDefeated"].push(msg);
|
2011-01-12 13:29:00 +01:00
|
|
|
};
|
|
|
|
|
2013-12-04 17:52:44 +01:00
|
|
|
AIInterface.prototype.OnGlobalEntityRenamed = function(msg)
|
|
|
|
{
|
2014-01-10 02:46:27 +01:00
|
|
|
this.events["EntityRenamed"].push(msg);
|
2013-12-04 17:52:44 +01:00
|
|
|
};
|
|
|
|
|
2011-01-12 13:29:00 +01:00
|
|
|
Engine.RegisterComponentType(IID_AIInterface, "AIInterface", AIInterface);
|