Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/simulation/components/UnitAI.js
Show First 20 Lines • Show All 636 Lines • ▼ Show 20 Lines | UnitAI.prototype.UnitFsmSpec = { | ||||
"Order.MoveToChasingPoint": function(msg) { | "Order.MoveToChasingPoint": function(msg) { | ||||
// Overriden by the CHASING state. | // Overriden by the CHASING state. | ||||
// Can however happen outside of it when renaming... | // Can however happen outside of it when renaming... | ||||
// TODO: don't use an order for that behaviour. | // TODO: don't use an order for that behaviour. | ||||
return this.FinishOrder(); | return this.FinishOrder(); | ||||
}, | }, | ||||
"Order.CollectTreasure": function(msg) { | "Order.CollectTreasure": function(msg) { | ||||
let cmpTreasureCollecter = Engine.QueryInterface(this.entity, IID_TreasureCollecter); | if (this.CheckTargetRange(msg.data.target, IID_TreasureCollecter)) | ||||
if (!cmpTreasureCollecter || !cmpTreasureCollecter.CanCollect(msg.data.target)) | this.SetNextState("INDIVIDUAL.COLLECTTREASURE.COLLECTING"); | ||||
else if (this.AbleToMove()) | |||||
this.SetNextState("INDIVIDUAL.COLLECTTREASURE.APPROACHING"); | |||||
else | |||||
return this.FinishOrder(); | return this.FinishOrder(); | ||||
this.SetNextState("COLLECTTREASURE"); | |||||
return ACCEPT_ORDER; | return ACCEPT_ORDER; | ||||
}, | }, | ||||
"Order.CollectTreasureNearPosition": function(msg) { | "Order.CollectTreasureNearPosition": function(msg) { | ||||
let nearbyTreasure = this.FindNearbyTreasure(msg.data.x, msg.data.z); | if (!this.AbleToMove()) | ||||
if (nearbyTreasure) | return this.FinishOrder(); | ||||
this.CollectTreasure(nearbyTreasure, oldData.autocontinue, true); | this.SetNextState("INDIVIDUAL.COLLECTTREASURE.WALKING"); | ||||
else | msg.data.initPos = { 'x': msg.data.x, 'z': msg.data.z }; | ||||
this.SetNextState("COLLECTTREASURE"); | msg.data.relaxed = true; | ||||
return ACCEPT_ORDER; | return ACCEPT_ORDER; | ||||
}, | }, | ||||
// States for the special entity representing a group of units moving in formation: | // States for the special entity representing a group of units moving in formation: | ||||
"FORMATIONCONTROLLER": { | "FORMATIONCONTROLLER": { | ||||
"Order.Walk": function(msg) { | "Order.Walk": function(msg) { | ||||
if (!this.AbleToMove()) | if (!this.AbleToMove()) | ||||
▲ Show 20 Lines • Show All 2,100 Lines • ▼ Show 20 Lines | "RETURNRESOURCE": { | ||||
}, | }, | ||||
"leave": function() { | "leave": function() { | ||||
}, | }, | ||||
}, | }, | ||||
}, | }, | ||||
"COLLECTTREASURE": { | "COLLECTTREASURE": { | ||||
"enter": function() { | |||||
let cmpTreasureCollecter = Engine.QueryInterface(this.entity, IID_TreasureCollecter); | |||||
if (!cmpTreasureCollecter || !cmpTreasureCollecter.CanCollect(this.order.data.target)) | |||||
{ | |||||
this.SetNextState("FINDINGNEWTARGET"); | |||||
return true; | |||||
} | |||||
if (this.CheckTargetRange(this.order.data.target, IID_TreasureCollecter)) | |||||
this.SetNextState("COLLECTING"); | |||||
else | |||||
this.SetNextState("APPROACHING"); | |||||
return true; | |||||
}, | |||||
"leave": function() { | "leave": function() { | ||||
}, | }, | ||||
"APPROACHING": { | "APPROACHING": { | ||||
"enter": function() { | "enter": function() { | ||||
// If we can't move, assume we'll fail any subsequent order | // If we can't move, assume we'll fail any subsequent order | ||||
// and finish the order entirely to avoid an infinite loop. | // and finish the order entirely to avoid an infinite loop. | ||||
if (!this.AbleToMove()) | if (!this.AbleToMove()) | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | "COLLECTTREASURE": { | ||||
"TargetInvalidated": function(msg) { | "TargetInvalidated": function(msg) { | ||||
this.SetNextState("FINDINGNEWTARGET"); | this.SetNextState("FINDINGNEWTARGET"); | ||||
}, | }, | ||||
}, | }, | ||||
"FINDINGNEWTARGET": { | "FINDINGNEWTARGET": { | ||||
"enter": function() { | "enter": function() { | ||||
let oldData = this.order.data; | let oldTarget = this.order.data.target || INVALID_ENTITY; | ||||
// Switch to the next order (if any). | // Switch to the next order (if any). | ||||
if (this.FinishOrder()) | if (this.FinishOrder()) | ||||
return true; | return true; | ||||
// If autocontinue explicitly disabled (e.g. by AI) | let nearbyTreasure = this.FindNearbyTreasure(this.TargetPosOrEntPos(oldTarget)); | ||||
// then do nothing automatically. | |||||
if (!oldData.autocontinue) | |||||
return false; | |||||
let nearbyTreasure = this.FindNearbyTreasure(this.TargetPosOrEntPos(oldData.target)); | |||||
if (nearbyTreasure) | if (nearbyTreasure) | ||||
this.CollectTreasure(nearbyTreasure, oldData.autocontinue, true); | this.CollectTreasure(nearbyTreasure, true); | ||||
return true; | return true; | ||||
}, | }, | ||||
}, | }, | ||||
// Walking to a good place to collect treasures near, used by CollectTreasureNearPosition. | |||||
"WALKING": { | |||||
"enter": function() { | |||||
if (!this.MoveTo(this.order.data)) | |||||
{ | |||||
this.FinishOrder(); | |||||
return true; | |||||
} | |||||
return false; | |||||
}, | |||||
"leave": function() { | |||||
this.StopMoving(); | |||||
}, | |||||
"MovementUpdate": function(msg) { | |||||
if (msg.likelyFailure || msg.obstructed && this.RelaxedMaxRangeCheck(this.order.data, this.DefaultRelaxedMaxRange) || | |||||
this.CheckRange(this.order.data)) | |||||
this.SetNextState("FINDINGNEWTARGET"); | |||||
}, | |||||
}, | |||||
}, | }, | ||||
"TRADE": { | "TRADE": { | ||||
"Attacked": function(msg) { | "Attacked": function(msg) { | ||||
// Ignore attack | // Ignore attack | ||||
// TODO: Inform player | // TODO: Inform player | ||||
}, | }, | ||||
▲ Show 20 Lines • Show All 2,724 Lines • ▼ Show 20 Lines | UnitAI.prototype.ReturnResource = function(target, queued, pushFront) | ||||
} | } | ||||
this.AddOrder("ReturnResource", { "target": target, "force": true }, queued, pushFront); | this.AddOrder("ReturnResource", { "target": target, "force": true }, queued, pushFront); | ||||
}; | }; | ||||
/** | /** | ||||
* Adds order to collect a treasure to queue, forced by the player. | * Adds order to collect a treasure to queue, forced by the player. | ||||
*/ | */ | ||||
UnitAI.prototype.CollectTreasure = function(target, autocontinue, queued) | UnitAI.prototype.CollectTreasure = function(target, queued, pushFront) | ||||
{ | { | ||||
this.AddOrder("CollectTreasure", { | this.AddOrder("CollectTreasure", { | ||||
"target": target, | "target": target, | ||||
"autocontinue": autocontinue, | |||||
"force": true | "force": true | ||||
}, queued); | }, queued, pushFront); | ||||
}; | }; | ||||
/** | /** | ||||
* Adds order to collect a treasure to queue, forced by the player. | * Adds order to collect a treasure to queue, forced by the player. | ||||
*/ | */ | ||||
UnitAI.prototype.CollectTreasureNearPosition = function(posX, posZ, autocontinue, queued) | UnitAI.prototype.CollectTreasureNearPosition = function(posX, posZ, queued, pushFront) | ||||
{ | { | ||||
this.AddOrder("CollectTreasureNearPosition", { | this.AddOrder("CollectTreasureNearPosition", { | ||||
"x": posX, | "x": posX, | ||||
"z": posZ, | "z": posZ, | ||||
"target": target, | "force": true | ||||
"autocontinue": autocontinue, | }, queued, pushFront); | ||||
"force": false | |||||
}, queued); | |||||
}; | }; | ||||
UnitAI.prototype.CancelSetupTradeRoute = function(target) | UnitAI.prototype.CancelSetupTradeRoute = function(target) | ||||
{ | { | ||||
let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); | let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); | ||||
if (!cmpTrader) | if (!cmpTrader) | ||||
return; | return; | ||||
cmpTrader.RemoveTargetMarket(target); | cmpTrader.RemoveTargetMarket(target); | ||||
▲ Show 20 Lines • Show All 789 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator