1
0
forked from 0ad/0ad
0ad/binaries/data/mods/public/simulation/ai/testbot/plan.js
Ykkrosh b8925fbbc9 # Support AI construction of buildings.
Pass terrain passability data to AI scripts.
Expand pathfinder passability data to 16 bits per tile, to allow more
classes.
Support 16-bit ints in serializer.
Partially support JS typed arrays.
Allow foundations to be placed on top of units (fixes #499).
Stop farms and fishes blocking movement (fixes #534).
Add obstruction flags to allow finer control over what they block.
Associate entity IDs with obstruction shapes, to allow finding colliding
entities.
Support moving to the edge of a target entity with inactive obstruction.
Support foundation entities in AI.
Support playing as non-hele civs.

This was SVN commit r8899.
2011-02-10 16:06:28 +00:00

68 lines
1.3 KiB
JavaScript

/**
* All plan classes must implement this interface.
*/
var IPlan = Class({
_init: function() { /* ... */ },
canExecute: function(gameState) { /* ... */ },
execute: function(gameState) { /* ... */ },
getCost: function() { /* ... */ },
});
/**
* Represents a prioritised collection of plans.
*/
var PlanGroup = Class({
_init: function()
{
this.escrow = new Resources({});
this.plans = [];
},
addPlan: function(priority, plan)
{
this.plans.push({"priority": priority, "plan": plan});
},
/**
* Executes all plans that we can afford, ordered by priority,
* and returns the highest-priority plan we couldn't afford (or null
* if none).
*/
executePlans: function(gameState)
{
// Ignore impossible plans
var plans = this.plans.filter(function(p) { return p.plan.canExecute(gameState); });
// Sort by decreasing priority
plans.sort(function(a, b) { return b.priority - a.priority; });
// Execute as many plans as we can afford
while (plans.length && this.escrow.canAfford(plans[0].plan.getCost()))
{
var plan = plans.shift().plan;
this.escrow.subtract(plan.getCost());
plan.execute(gameState);
}
if (plans.length)
return plans[0];
else
return null;
},
resetPlans: function()
{
this.plans = [];
},
getEscrow: function()
{
return this.escrow;
},
});