Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/UnitAI.js
Show First 20 Lines • Show All 1,212 Lines • ▼ Show 20 Lines | "FormationLeave": function(msg) { | ||||
if (this.orderQueue.length) | if (this.orderQueue.length) | ||||
{ | { | ||||
// We're leaving the formation, so stop our FormationWalk order | // We're leaving the formation, so stop our FormationWalk order | ||||
if (this.FinishOrder()) | if (this.FinishOrder()) | ||||
return; | return; | ||||
} | } | ||||
// No orders left, we're an individual now | // No orders left, we're an individual now | ||||
if (this.IsAnimal()) | |||||
this.SetNextState("ANIMAL.IDLE"); | |||||
else | |||||
this.SetNextState("INDIVIDUAL.IDLE"); | this.SetNextState("INDIVIDUAL.IDLE"); | ||||
}, | }, | ||||
// Override the LeaveFoundation order since we're not doing | // Override the LeaveFoundation order since we're not doing | ||||
// anything more important (and we might be stuck in the WALKING | // anything more important (and we might be stuck in the WALKING | ||||
// state forever and need to get out of foundations in that case) | // state forever and need to get out of foundations in that case) | ||||
"Order.LeaveFoundation": function(msg) { | "Order.LeaveFoundation": function(msg) { | ||||
// If foundation is not ally of entity, or if entity is unpacked siege, | // If foundation is not ally of entity, or if entity is unpacked siege, | ||||
// ignore the order | // ignore the order | ||||
Show All 13 Lines | "Order.LeaveFoundation": function(msg) { | ||||
else | else | ||||
{ | { | ||||
this.order.data.min = range; | this.order.data.min = range; | ||||
this.SetNextState("WALKINGTOPOINT"); | this.SetNextState("WALKINGTOPOINT"); | ||||
} | } | ||||
}, | }, | ||||
"enter": function() { | "enter": function() { | ||||
if (this.IsAnimal()) | |||||
{ | |||||
// Animals can't go in formation. | |||||
warn("Entity " + this.entity + " was put in FORMATIONMEMBER state but is an animal"); | |||||
this.FinishOrder(); | |||||
this.SetNextState("ANIMAL.IDLE"); | |||||
return true; | |||||
} | |||||
let cmpFormation = Engine.QueryInterface(this.formationController, IID_Formation); | let cmpFormation = Engine.QueryInterface(this.formationController, IID_Formation); | ||||
if (cmpFormation) | if (cmpFormation) | ||||
this.SetAnimationVariant(cmpFormation.GetFormationAnimation(this.entity)); | this.SetAnimationVariant(cmpFormation.GetFormationAnimation(this.entity)); | ||||
}, | }, | ||||
"leave": function() { | "leave": function() { | ||||
this.SetDefaultAnimationVariant(); | this.SetDefaultAnimationVariant(); | ||||
}, | }, | ||||
"IDLE": { | "IDLE": { | ||||
"enter": function() { | // Formation members do nothing while Idle, but we need the state | ||||
if (this.IsAnimal()) | // so that they keep the formation variant. | ||||
this.SetNextState("ANIMAL.IDLE"); | |||||
else | |||||
this.SetNextState("INDIVIDUAL.IDLE"); | |||||
return true; | |||||
}, | |||||
}, | }, | ||||
"WALKING": { | "WALKING": { | ||||
"enter": function() { | "enter": function() { | ||||
let cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | let cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | ||||
cmpUnitMotion.MoveToFormationOffset(this.order.data.target, this.order.data.x, this.order.data.z); | cmpUnitMotion.MoveToFormationOffset(this.order.data.target, this.order.data.x, this.order.data.z); | ||||
}, | }, | ||||
"leave": function() { | |||||
this.StopMoving(); | |||||
}, | |||||
// Occurs when the unit has reached its destination and the controller | // Occurs when the unit has reached its destination and the controller | ||||
// is done moving. The controller is notified. | // is done moving. The controller is notified. | ||||
"MovementUpdate": function(msg) { | "MovementUpdate": function(msg) { | ||||
// We can only finish this order if the move was really completed. | // We can only finish this order if the move was really completed. | ||||
if (!this.CheckRange(this.order.data) || msg.error) | let cmpPosition = Engine.QueryInterface(this.formationController, IID_Position); | ||||
let atDestination = cmpPosition && cmpPosition.IsInWorld(); | |||||
if (!atDestination && cmpPosition) | |||||
{ | |||||
let pos = cmpPosition.GetPosition2D(); | |||||
atDestination = this.CheckPointRangeExplicit(pos.X + this.order.data.x, pos.Y + this.order.data.z, 0, 0); | |||||
} | |||||
if (!atDestination && !msg.error) | |||||
return; | return; | ||||
if (this.FinishOrder()) | if (this.FinishOrder()) | ||||
return; | return; | ||||
let cmpFormation = Engine.QueryInterface(this.formationController, IID_Formation); | let cmpFormation = Engine.QueryInterface(this.formationController, IID_Formation); | ||||
if (cmpFormation) | if (cmpFormation) | ||||
cmpFormation.SetInPosition(this.entity); | cmpFormation.SetInPosition(this.entity); | ||||
▲ Show 20 Lines • Show All 92 Lines • ▼ Show 20 Lines | "GuardedAttacked": function(msg) { | ||||
} | } | ||||
}, | }, | ||||
"IDLE": { | "IDLE": { | ||||
"enter": function() { | "enter": function() { | ||||
// Switch back to idle animation to guarantee we won't | // Switch back to idle animation to guarantee we won't | ||||
// get stuck with an incorrect animation | // get stuck with an incorrect animation | ||||
var animationName = "idle"; | var animationName = "idle"; | ||||
if (this.IsFormationMember()) | |||||
{ | |||||
var cmpFormation = Engine.QueryInterface(this.formationController, IID_Formation); | |||||
if (cmpFormation) | |||||
animationName = cmpFormation.GetFormationAnimation(this.entity, animationName); | |||||
} | |||||
this.SelectAnimation(animationName); | this.SelectAnimation(animationName); | ||||
// Idle is the default state. If units try, from the IDLE.enter sub-state, to | // Idle is the default state. If units try, from the IDLE.enter sub-state, to | ||||
// begin another order, and that order fails (calling FinishOrder), they might | // begin another order, and that order fails (calling FinishOrder), they might | ||||
// end up in an infinite loop. To avoid this, all methods that could put the unit in | // end up in an infinite loop. To avoid this, all methods that could put the unit in | ||||
// a new state are done on the next turn. | // a new state are done on the next turn. | ||||
// Further, the GUI and AI want to know when a unit is idle, | // Further, the GUI and AI want to know when a unit is idle, | ||||
// but sending this info in Idle.enter will send spurious messages. | // but sending this info in Idle.enter will send spurious messages. | ||||
▲ Show 20 Lines • Show All 4,506 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator