Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/simulation/components/UnitAI.js
Show First 20 Lines • Show All 137 Lines • ▼ Show 20 Lines | UnitAI.prototype.UnitFsmSpec = { | ||||
// Default event handlers: | // Default event handlers: | ||||
"MoveCompleted": function() { | "MoveCompleted": function() { | ||||
// ignore spurious movement messages | // ignore spurious movement messages | ||||
// (these can happen when stopping moving at the same time | // (these can happen when stopping moving at the same time | ||||
// as switching states) | // as switching states) | ||||
}, | }, | ||||
"MoveStarted": function() { | |||||
// ignore spurious movement messages | |||||
}, | |||||
"ConstructionFinished": function(msg) { | "ConstructionFinished": function(msg) { | ||||
// ignore uninteresting construction messages | // ignore uninteresting construction messages | ||||
}, | }, | ||||
"LosRangeUpdate": function(msg) { | "LosRangeUpdate": function(msg) { | ||||
// ignore newly-seen units by default | // ignore newly-seen units by default | ||||
}, | }, | ||||
▲ Show 20 Lines • Show All 720 Lines • ▼ Show 20 Lines | "Order.Unpack": function(msg) { | ||||
this.SetNextState("MEMBER"); | this.SetNextState("MEMBER"); | ||||
}, | }, | ||||
"IDLE": { | "IDLE": { | ||||
"enter": function(msg) { | "enter": function(msg) { | ||||
var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | ||||
cmpFormation.SetRearrange(false); | cmpFormation.SetRearrange(false); | ||||
}, | }, | ||||
"MoveStarted": function() { | |||||
let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | |||||
cmpFormation.SetRearrange(true); | |||||
cmpFormation.MoveMembersIntoFormation(true, true); | |||||
} | |||||
}, | }, | ||||
"WALKING": { | "WALKING": { | ||||
"enter": function() { | "enter": function() { | ||||
if (!this.MoveTo(this.order.data)) | if (!this.MoveTo(this.order.data)) | ||||
{ | { | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return true; | return true; | ||||
} | } | ||||
let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | |||||
cmpFormation.SetRearrange(true); | |||||
cmpFormation.MoveMembersIntoFormation(true, true); | |||||
}, | }, | ||||
"leave": function() { | "leave": function() { | ||||
this.StopMoving(); | this.StopMoving(); | ||||
}, | }, | ||||
"MoveStarted": function(msg) { | |||||
var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | |||||
cmpFormation.SetRearrange(true); | |||||
cmpFormation.MoveMembersIntoFormation(true, true); | |||||
}, | |||||
"MoveCompleted": function(msg) { | "MoveCompleted": function(msg) { | ||||
if (this.FinishOrder()) | if (this.FinishOrder()) | ||||
this.CallMemberFunction("ResetFinishOrder", []); | this.CallMemberFunction("ResetFinishOrder", []); | ||||
}, | }, | ||||
}, | }, | ||||
"WALKINGANDFIGHTING": { | "WALKINGANDFIGHTING": { | ||||
"enter": function(msg) { | "enter": function(msg) { | ||||
if (!this.MoveTo(this.order.data)) | if (!this.MoveTo(this.order.data)) | ||||
{ | { | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return true; | return true; | ||||
} | } | ||||
this.StartTimer(0, 1000); | this.StartTimer(0, 1000); | ||||
let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | |||||
cmpFormation.SetRearrange(true); | |||||
cmpFormation.MoveMembersIntoFormation(true, true); | |||||
}, | }, | ||||
"leave": function() { | "leave": function() { | ||||
this.StopMoving(); | this.StopMoving(); | ||||
this.StopTimer(); | |||||
}, | }, | ||||
"Timer": function(msg) { | "Timer": function(msg) { | ||||
// check if there are no enemies to attack | // check if there are no enemies to attack | ||||
this.FindWalkAndFightTargets(); | this.FindWalkAndFightTargets(); | ||||
}, | }, | ||||
"leave": function(msg) { | |||||
this.StopTimer(); | |||||
}, | |||||
"MoveStarted": function(msg) { | |||||
var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | |||||
cmpFormation.SetRearrange(true); | |||||
cmpFormation.MoveMembersIntoFormation(true, true); | |||||
}, | |||||
"MoveCompleted": function(msg) { | "MoveCompleted": function(msg) { | ||||
if (this.FinishOrder()) | if (this.FinishOrder()) | ||||
this.CallMemberFunction("ResetFinishOrder", []); | this.CallMemberFunction("ResetFinishOrder", []); | ||||
}, | }, | ||||
}, | }, | ||||
"PATROL": { | "PATROL": { | ||||
"enter": function(msg) { | "enter": function(msg) { | ||||
Show All 12 Lines | "PATROL": { | ||||
} | } | ||||
if (!this.MoveTo(this.order.data)) | if (!this.MoveTo(this.order.data)) | ||||
{ | { | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return true; | return true; | ||||
} | } | ||||
this.StartTimer(0, 1000); | this.StartTimer(0, 1000); | ||||
let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | |||||
cmpFormation.SetRearrange(true); | |||||
cmpFormation.MoveMembersIntoFormation(true, true); | |||||
}, | }, | ||||
"Timer": function(msg) { | "Timer": function(msg) { | ||||
// Check if there are no enemies to attack | // Check if there are no enemies to attack | ||||
this.FindWalkAndFightTargets(); | this.FindWalkAndFightTargets(); | ||||
}, | }, | ||||
"leave": function(msg) { | "leave": function(msg) { | ||||
this.StopTimer(); | this.StopTimer(); | ||||
this.StopMoving(); | this.StopMoving(); | ||||
delete this.patrolStartPosOrder; | delete this.patrolStartPosOrder; | ||||
}, | }, | ||||
"MoveStarted": function(msg) { | |||||
let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | |||||
cmpFormation.SetRearrange(true); | |||||
cmpFormation.MoveMembersIntoFormation(true, true); | |||||
}, | |||||
"MoveCompleted": function() { | "MoveCompleted": function() { | ||||
/** | /** | ||||
* A-B-A-B-..: | * A-B-A-B-..: | ||||
* if the user only commands one patrol order, the patrol will be between | * if the user only commands one patrol order, the patrol will be between | ||||
* the last position and the defined waypoint | * the last position and the defined waypoint | ||||
* A-B-C-..-A-B-..: | * A-B-C-..-A-B-..: | ||||
* otherwise, the patrol is only between the given patrol commands and the | * otherwise, the patrol is only between the given patrol commands and the | ||||
* last position is not included (last position = the position where the unit | * last position is not included (last position = the position where the unit | ||||
Show All 31 Lines | "GARRISON":{ | ||||
"APPROACHING": { | "APPROACHING": { | ||||
"enter": function() { | "enter": function() { | ||||
if (!this.MoveToGarrisonRange(this.order.data.target)) | if (!this.MoveToGarrisonRange(this.order.data.target)) | ||||
{ | { | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return true; | return true; | ||||
} | } | ||||
let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | |||||
cmpFormation.SetRearrange(true); | |||||
cmpFormation.MoveMembersIntoFormation(true, true); | |||||
}, | }, | ||||
"leave": function() { | "leave": function() { | ||||
this.StopMoving(); | this.StopMoving(); | ||||
}, | }, | ||||
"MoveStarted": function(msg) { | |||||
var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | |||||
cmpFormation.SetRearrange(true); | |||||
cmpFormation.MoveMembersIntoFormation(true, true); | |||||
}, | |||||
"MoveCompleted": function(msg) { | "MoveCompleted": function(msg) { | ||||
this.SetNextState("GARRISONING"); | this.SetNextState("GARRISONING"); | ||||
}, | }, | ||||
}, | }, | ||||
"GARRISONING": { | "GARRISONING": { | ||||
"enter": function() { | "enter": function() { | ||||
// If a pickup has been requested, cancel it as it will be requested by members | // If a pickup has been requested, cancel it as it will be requested by members | ||||
Show All 10 Lines | "FORMATIONCONTROLLER": { | ||||
"FORMING": { | "FORMING": { | ||||
"enter": function() { | "enter": function() { | ||||
if (!this.MoveTo(this.order.data)) | if (!this.MoveTo(this.order.data)) | ||||
{ | { | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return true; | return true; | ||||
} | } | ||||
let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | |||||
cmpFormation.SetRearrange(true); | |||||
cmpFormation.MoveMembersIntoFormation(true, true); | |||||
}, | }, | ||||
"leave": function() { | "leave": function() { | ||||
this.StopMoving(); | this.StopMoving(); | ||||
}, | }, | ||||
"MoveStarted": function(msg) { | |||||
var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | |||||
cmpFormation.SetRearrange(true); | |||||
cmpFormation.MoveMembersIntoFormation(true, false); | |||||
}, | |||||
"MoveCompleted": function(msg) { | "MoveCompleted": function(msg) { | ||||
if (this.FinishOrder()) | if (this.FinishOrder()) | ||||
{ | { | ||||
this.CallMemberFunction("ResetFinishOrder", []); | this.CallMemberFunction("ResetFinishOrder", []); | ||||
return; | return; | ||||
} | } | ||||
var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | ||||
cmpFormation.FindInPosition(); | cmpFormation.FindInPosition(); | ||||
} | } | ||||
}, | }, | ||||
"COMBAT": { | "COMBAT": { | ||||
"APPROACHING": { | "APPROACHING": { | ||||
"enter": function() { | "enter": function() { | ||||
if (!this.MoveTo(this.order.data)) | if (!this.MoveTo(this.order.data)) | ||||
{ | { | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return true; | return true; | ||||
} | } | ||||
let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | |||||
cmpFormation.SetRearrange(true); | |||||
cmpFormation.MoveMembersIntoFormation(true, true); | |||||
}, | }, | ||||
"leave": function() { | "leave": function() { | ||||
this.StopMoving(); | this.StopMoving(); | ||||
}, | }, | ||||
"MoveStarted": function(msg) { | |||||
var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | |||||
cmpFormation.SetRearrange(true); | |||||
cmpFormation.MoveMembersIntoFormation(true, true); | |||||
}, | |||||
"MoveCompleted": function(msg) { | "MoveCompleted": function(msg) { | ||||
var cmpAttack = Engine.QueryInterface(this.entity, IID_Attack); | var cmpAttack = Engine.QueryInterface(this.entity, IID_Attack); | ||||
this.CallMemberFunction("Attack", [this.order.data.target, this.order.data.allowCapture, false]); | this.CallMemberFunction("Attack", [this.order.data.target, this.order.data.allowCapture, false]); | ||||
if (cmpAttack.CanAttackAsFormation()) | if (cmpAttack.CanAttackAsFormation()) | ||||
this.SetNextState("COMBAT.ATTACKING"); | this.SetNextState("COMBAT.ATTACKING"); | ||||
else | else | ||||
this.SetNextState("MEMBER"); | this.SetNextState("MEMBER"); | ||||
}, | }, | ||||
▲ Show 20 Lines • Show All 346 Lines • ▼ Show 20 Lines | "IDLE": { | ||||
this.AttackEntitiesByPreference(msg.data.added); | this.AttackEntitiesByPreference(msg.data.added); | ||||
} | } | ||||
}, | }, | ||||
"LosHealRangeUpdate": function(msg) { | "LosHealRangeUpdate": function(msg) { | ||||
this.RespondToHealableEntities(msg.data.added); | this.RespondToHealableEntities(msg.data.added); | ||||
}, | }, | ||||
"MoveStarted": function() { | |||||
this.SelectAnimation("move"); | |||||
}, | |||||
"MoveCompleted": function() { | "MoveCompleted": function() { | ||||
this.SelectAnimation("idle"); | this.SelectAnimation("idle"); | ||||
}, | }, | ||||
"Timer": function(msg) { | "Timer": function(msg) { | ||||
if (!this.isIdle) | if (!this.isIdle) | ||||
{ | { | ||||
this.isIdle = true; | this.isIdle = true; | ||||
▲ Show 20 Lines • Show All 119 Lines • ▼ Show 20 Lines | "GUARD": { | ||||
"Timer": function(msg) { | "Timer": function(msg) { | ||||
// Check the target is alive | // Check the target is alive | ||||
if (!this.TargetIsAlive(this.isGuardOf)) | if (!this.TargetIsAlive(this.isGuardOf)) | ||||
{ | { | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return; | return; | ||||
} | } | ||||
this.SetHeldPositionOnEntity(this.isGuardOf); | |||||
}, | |||||
"leave": function(msg) { | |||||
this.StopMoving(); | |||||
this.ResetSpeedMultiplier(); | |||||
this.StopTimer(); | |||||
this.SetDefaultAnimationVariant(); | |||||
}, | |||||
"MoveStarted": function(msg) { | |||||
// Adapt the speed to the one of the target if needed | // Adapt the speed to the one of the target if needed | ||||
let cmpObstructionManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_ObstructionManager); | let cmpObstructionManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_ObstructionManager); | ||||
if (cmpObstructionManager.IsInTargetRange(this.entity, this.isGuardOf, 0, 3 * this.guardRange, true)) | if (cmpObstructionManager.IsInTargetRange(this.entity, this.isGuardOf, 0, 3 * this.guardRange, true)) | ||||
{ | { | ||||
var cmpUnitAI = Engine.QueryInterface(this.isGuardOf, IID_UnitAI); | let cmpUnitAI = Engine.QueryInterface(this.isGuardOf, IID_UnitAI); | ||||
if (cmpUnitAI) | if (cmpUnitAI) | ||||
{ | { | ||||
var speed = cmpUnitAI.GetWalkSpeed(); | let speed = cmpUnitAI.GetWalkSpeed(); | ||||
if (speed < this.GetWalkSpeed()) | if (speed < this.GetWalkSpeed()) | ||||
this.SetSpeedMultiplier(speed / this.GetWalkSpeed()); | this.SetSpeedMultiplier(speed / this.GetWalkSpeed()); | ||||
} | } | ||||
} | } | ||||
this.SetHeldPositionOnEntity(this.isGuardOf); | |||||
}, | |||||
"leave": function(msg) { | |||||
this.StopMoving(); | |||||
this.ResetSpeedMultiplier(); | |||||
this.StopTimer(); | |||||
this.SetDefaultAnimationVariant(); | |||||
}, | }, | ||||
"MoveCompleted": function() { | "MoveCompleted": function() { | ||||
this.ResetSpeedMultiplier(); | this.ResetSpeedMultiplier(); | ||||
if (this.CheckTargetRangeExplicit(this.isGuardOf, 0, this.guardRange)) | if (this.CheckTargetRangeExplicit(this.isGuardOf, 0, this.guardRange)) | ||||
this.SetNextState("GUARDING"); | this.SetNextState("GUARDING"); | ||||
}, | }, | ||||
}, | }, | ||||
▲ Show 20 Lines • Show All 2,161 Lines • ▼ Show 20 Lines | UnitAI.prototype.StopTimer = function() | ||||
cmpTimer.CancelTimer(this.timer); | cmpTimer.CancelTimer(this.timer); | ||||
this.timer = undefined; | this.timer = undefined; | ||||
}; | }; | ||||
//// Message handlers ///// | //// Message handlers ///// | ||||
UnitAI.prototype.OnMotionChanged = function(msg) | UnitAI.prototype.OnMotionChanged = function(msg) | ||||
{ | { | ||||
if (msg.starting && !msg.error) | |||||
this.UnitFsm.ProcessMessage(this, {"type": "MoveStarted", "data": msg}); | |||||
else if (!msg.starting || msg.error) | |||||
this.UnitFsm.ProcessMessage(this, {"type": "MoveCompleted", "data": msg}); | this.UnitFsm.ProcessMessage(this, { "type": "MoveCompleted", "data": msg }); | ||||
}; | }; | ||||
UnitAI.prototype.OnGlobalConstructionFinished = function(msg) | UnitAI.prototype.OnGlobalConstructionFinished = function(msg) | ||||
{ | { | ||||
// TODO: This is a bit inefficient since every unit listens to every | // TODO: This is a bit inefficient since every unit listens to every | ||||
// construction message - ideally we could scope it to only the one we're building | // construction message - ideally we could scope it to only the one we're building | ||||
this.UnitFsm.ProcessMessage(this, {"type": "ConstructionFinished", "data": msg}); | this.UnitFsm.ProcessMessage(this, {"type": "ConstructionFinished", "data": msg}); | ||||
▲ Show 20 Lines • Show All 2,085 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator