2010-02-05 14:00:39 -08:00
function UnitAI ( ) { }
2010-04-23 09:09:03 -07:00
UnitAI . prototype . Schema =
"<a:help>Controls the unit's movement, attacks, etc, in response to commands from the player.</a:help>" +
"<a:example/>" +
2011-03-04 06:36:41 -08:00
"<element name='DefaultStance'>" +
"<choice>" +
2011-06-24 05:35:15 -07:00
"<value>violent</value>" +
2011-03-04 06:36:41 -08:00
"<value>aggressive</value>" +
2011-06-17 15:13:39 -07:00
"<value>defensive</value>" +
"<value>passive</value>" +
2012-03-21 09:45:02 -07:00
"<value>standground</value>" +
2021-02-27 12:13:40 -08:00
"<value>skittish</value>" +
"<value>passive-defensive</value>" +
2011-03-04 06:36:41 -08:00
"</choice>" +
"</element>" +
2010-09-03 02:55:14 -07:00
"<element name='FormationController'>" +
"<data type='boolean'/>" +
2011-02-27 05:34:22 -08:00
"</element>" +
2011-03-04 06:36:41 -08:00
"<element name='FleeDistance'>" +
"<ref name='positiveDecimal'/>" +
"</element>" +
2022-01-22 23:23:44 -08:00
"<optional>" +
"<element name='Formations' a:help='Optional list of space-separated formations this unit is allowed to use. Choices include: Scatter, Box, ColumnClosed, LineClosed, ColumnOpen, LineOpen, Flank, Skirmish, Wedge, Testudo, Phalanx, Syntagma, BattleLine.'>" +
"<attribute name='datatype'>" +
"<value>tokens</value>" +
"</attribute>" +
"<text/>" +
"</element>" +
"</optional>" +
2013-11-30 09:30:08 -08:00
"<element name='CanGuard'>" +
"<data type='boolean'/>" +
"</element>" +
2017-04-25 04:22:41 -07:00
"<element name='CanPatrol'>" +
"<data type='boolean'/>" +
"</element>" +
2020-12-10 01:18:13 -08:00
"<element name='PatrolWaitTime' a:help='Number of seconds to wait in between patrol waypoints.'>" +
"<data type='nonNegativeInteger'/>" +
"</element>" +
2020-11-16 06:47:41 -08:00
"<optional>" +
"<element name='CheeringTime'>" +
"<data type='nonNegativeInteger'/>" +
"</element>" +
"</optional>" +
2011-02-27 05:34:22 -08:00
"<optional>" +
"<interleave>" +
"<element name='RoamDistance'>" +
"<ref name='positiveDecimal'/>" +
"</element>" +
"<element name='RoamTimeMin'>" +
"<ref name='positiveDecimal'/>" +
"</element>" +
"<element name='RoamTimeMax'>" +
"<ref name='positiveDecimal'/>" +
"</element>" +
"<element name='FeedTimeMin'>" +
"<ref name='positiveDecimal'/>" +
"</element>" +
"<element name='FeedTimeMax'>" +
"<ref name='positiveDecimal'/>" +
"</element>" +
"</interleave>" +
"</optional>" ;
2010-04-23 09:09:03 -07:00
2011-06-24 05:35:15 -07:00
// Unit stances.
2011-03-04 06:36:41 -08:00
// There some targeting options:
// targetVisibleEnemies: anything in vision range is a viable target
2012-05-08 16:00:14 -07:00
// targetAttackersAlways: anything that hurts us is a viable target,
// possibly overriding user orders!
2011-03-04 06:36:41 -08:00
// There are some response options, triggered when targets are detected:
// respondFlee: run away
2021-02-27 12:13:40 -08:00
// respondFleeOnSight: run away when an enemy is sighted
2011-03-04 06:36:41 -08:00
// respondChase: start chasing after the enemy
2011-06-24 05:35:15 -07:00
// respondChaseBeyondVision: start chasing, and don't stop even if it's out
// of this unit's vision range (though still visible to the player)
// respondStandGround: attack enemy but don't move at all
// respondHoldGround: attack enemy but don't move far from current position
// TODO: maybe add targetAggressiveEnemies (don't worry about lone scouts,
// do worry around armies slaughtering the guy standing next to you), etc.
2010-08-04 14:15:41 -07:00
var g _Stances = {
2011-06-17 15:13:39 -07:00
"violent" : {
2018-03-10 11:12:23 -08:00
"targetVisibleEnemies" : true ,
"targetAttackersAlways" : true ,
"respondFlee" : false ,
2021-02-27 12:13:40 -08:00
"respondFleeOnSight" : false ,
2018-03-10 11:12:23 -08:00
"respondChase" : true ,
"respondChaseBeyondVision" : true ,
"respondStandGround" : false ,
"respondHoldGround" : false ,
"selectable" : true
2010-08-04 14:15:41 -07:00
} ,
2011-06-17 15:13:39 -07:00
"aggressive" : {
2018-03-10 11:12:23 -08:00
"targetVisibleEnemies" : true ,
"targetAttackersAlways" : false ,
"respondFlee" : false ,
2021-02-27 12:13:40 -08:00
"respondFleeOnSight" : false ,
2018-03-10 11:12:23 -08:00
"respondChase" : true ,
"respondChaseBeyondVision" : false ,
"respondStandGround" : false ,
"respondHoldGround" : false ,
"selectable" : true
2011-03-04 06:36:41 -08:00
} ,
2011-06-17 15:13:39 -07:00
"defensive" : {
2018-03-10 11:12:23 -08:00
"targetVisibleEnemies" : true ,
"targetAttackersAlways" : false ,
"respondFlee" : false ,
2021-02-27 12:13:40 -08:00
"respondFleeOnSight" : false ,
2018-03-10 11:12:23 -08:00
"respondChase" : false ,
"respondChaseBeyondVision" : false ,
"respondStandGround" : false ,
"respondHoldGround" : true ,
"selectable" : true
2011-06-17 15:13:39 -07:00
} ,
"passive" : {
2018-03-10 11:12:23 -08:00
"targetVisibleEnemies" : false ,
"targetAttackersAlways" : false ,
"respondFlee" : true ,
2021-02-27 12:13:40 -08:00
"respondFleeOnSight" : false ,
2018-03-10 11:12:23 -08:00
"respondChase" : false ,
"respondChaseBeyondVision" : false ,
"respondStandGround" : false ,
"respondHoldGround" : false ,
"selectable" : true
2011-06-17 15:13:39 -07:00
} ,
2012-03-21 09:45:02 -07:00
"standground" : {
2018-03-10 11:12:23 -08:00
"targetVisibleEnemies" : true ,
"targetAttackersAlways" : false ,
"respondFlee" : false ,
2021-02-27 12:13:40 -08:00
"respondFleeOnSight" : false ,
2018-03-10 11:12:23 -08:00
"respondChase" : false ,
"respondChaseBeyondVision" : false ,
"respondStandGround" : true ,
"respondHoldGround" : false ,
"selectable" : true
2010-08-04 14:15:41 -07:00
} ,
2021-02-27 12:13:40 -08:00
"skittish" : {
"targetVisibleEnemies" : false ,
"targetAttackersAlways" : false ,
"respondFlee" : true ,
"respondFleeOnSight" : true ,
"respondChase" : false ,
"respondChaseBeyondVision" : false ,
"respondStandGround" : false ,
"respondHoldGround" : false ,
"selectable" : false
} ,
"passive-defensive" : {
"targetVisibleEnemies" : false ,
"targetAttackersAlways" : false ,
"respondFlee" : false ,
"respondFleeOnSight" : false ,
"respondChase" : false ,
"respondChaseBeyondVision" : false ,
"respondStandGround" : false ,
"respondHoldGround" : true ,
"selectable" : false
} ,
2018-03-10 11:12:23 -08:00
"none" : {
// Only to be used by AI or trigger scripts
"targetVisibleEnemies" : false ,
"targetAttackersAlways" : false ,
"respondFlee" : false ,
2021-02-27 12:13:40 -08:00
"respondFleeOnSight" : false ,
2018-03-10 11:12:23 -08:00
"respondChase" : false ,
"respondChaseBeyondVision" : false ,
"respondStandGround" : false ,
"respondHoldGround" : false ,
"selectable" : false
}
2010-08-04 14:15:41 -07:00
} ;
2019-12-28 03:40:24 -08:00
// These orders always require a packed unit, so if a unit that is unpacking is given one of these orders,
// it will immediately cancel unpacking.
var g _OrdersCancelUnpacking = new Set ( [
"FormationWalk" ,
"Walk" ,
"WalkAndFight" ,
"WalkToTarget" ,
"Patrol" ,
2019-12-28 05:43:18 -08:00
"Garrison"
2019-12-28 03:40:24 -08:00
] ) ;
// When leaving a foundation, we want to be clear of it by this distance.
var g _LeaveFoundationRange = 4 ;
2020-09-10 09:37:14 -07:00
UnitAI . prototype . notifyToCheerInRange = 30 ;
2023-01-09 06:29:06 -08:00
UnitAI . prototype . DEFAULT _CAPTURE = false ;
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
// To reject an order, use 'return this.FinishOrder();'
2021-02-27 22:29:53 -08:00
const ACCEPT _ORDER = true ;
2011-02-27 05:34:22 -08:00
// See ../helpers/FSM.js for some documentation of this FSM specification syntax
2014-05-27 00:24:07 -07:00
UnitAI . prototype . UnitFsmSpec = {
2010-07-21 09:09:58 -07:00
2010-09-03 02:55:14 -07:00
// Default event handlers:
2020-12-17 04:56:21 -08:00
"MovementUpdate" : function ( msg ) {
// ignore spurious movement messages
// (these can happen when stopping moving at the same time
// as switching states)
} ,
2025-01-01 02:33:07 -08:00
"ReenterIdle" : function ( msg ) {
// Ignore, intended for IDLE state only.
} ,
2020-12-17 04:56:21 -08:00
"ConstructionFinished" : function ( msg ) {
// ignore uninteresting construction messages
} ,
2010-09-03 02:55:14 -07:00
"LosRangeUpdate" : function ( msg ) {
2020-07-23 23:07:27 -07:00
// Ignore newly-seen units by default.
2010-09-03 02:55:14 -07:00
} ,
2012-05-18 14:31:57 -07:00
2012-04-17 13:22:13 -07:00
"LosHealRangeUpdate" : function ( msg ) {
2020-07-23 23:07:27 -07:00
// Ignore newly-seen injured units by default.
} ,
"LosAttackRangeUpdate" : function ( msg ) {
// Ignore newly-seen enemy units by default.
2012-04-17 13:22:13 -07:00
} ,
2010-09-03 02:55:14 -07:00
"Attacked" : function ( msg ) {
// ignore attacker
} ,
2020-12-17 04:56:21 -08:00
"PackFinished" : function ( msg ) {
// ignore
} ,
2013-11-08 15:22:59 -08:00
"PickupCanceled" : function ( msg ) {
// ignore
} ,
2020-12-17 04:56:21 -08:00
"TradingCanceled" : function ( msg ) {
// ignore
} ,
2013-11-30 09:30:08 -08:00
"GuardedAttacked" : function ( msg ) {
// ignore
} ,
2020-06-05 23:19:15 -07:00
"OrderTargetRenamed" : function ( ) {
// By default, trigger an exit-reenter
// so that state preconditions are checked against the new entity
// (there is no reason to assume the target is still valid).
this . SetNextState ( this . GetCurrentState ( ) ) ;
} ,
2010-09-03 02:55:14 -07:00
// Formation handlers:
"FormationLeave" : function ( msg ) {
2021-01-23 00:07:56 -08:00
// Overloaded by FORMATIONMEMBER
// We end up here if LeaveFormation was called when the entity
// was executing an order in an individual state, so we must
// discard the order now that it has been executed.
if ( this . order && this . order . type === "LeaveFormation" )
this . FinishOrder ( ) ;
2010-09-03 02:55:14 -07:00
} ,
2010-10-03 10:58:49 -07:00
// Called when being told to walk as part of a formation
2010-09-03 02:55:14 -07:00
"Order.FormationWalk" : function ( msg ) {
2021-08-02 09:48:00 -07:00
if ( ! this . IsFormationMember ( ) || ! this . AbleToMove ( ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2011-02-27 05:34:22 -08:00
2012-11-30 16:34:03 -08:00
if ( this . CanPack ( ) )
{
2021-02-13 02:08:10 -08:00
// If the controller is IDLE, this is just the regular reformation timer.
// In that case we don't actually want to move, as that would unpack us.
let cmpControllerAI = Engine . QueryInterface ( this . GetFormationController ( ) , IID _UnitAI ) ;
if ( cmpControllerAI . IsIdle ( ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2012-11-30 16:34:03 -08:00
this . PushOrderFront ( "Pack" , { "force" : true } ) ;
}
2021-02-27 22:29:53 -08:00
else
this . SetNextState ( "FORMATIONMEMBER.WALKING" ) ;
return ACCEPT _ORDER ;
2010-09-03 02:55:14 -07:00
} ,
2011-02-10 08:06:28 -08:00
// Special orders:
// (these will be overridden by various states)
"Order.LeaveFoundation" : function ( msg ) {
2019-12-28 03:40:24 -08:00
if ( ! this . WillMoveFromFoundation ( msg . data . target ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2021-03-03 00:21:00 -08:00
msg . data . min = g _LeaveFoundationRange ;
2019-12-28 03:40:24 -08:00
this . SetNextState ( "INDIVIDUAL.WALKING" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2011-02-10 08:06:28 -08:00
} ,
2010-09-03 02:55:14 -07:00
// Individual orders:
Allow picking a default formation for walk (and walk-like) orders.
This allows choosing a "default formation", which is activated
automatically for units given walk orders (and attack-walk etc.).
Conversely, units in formation that are given a gather/build/... order
are taken out of formation and given the order individually.
The default formation can be selected by right-clicking on any formation
icon.
This leverages formations for walking, where they are quite efficient
(in fact, perhaps too efficient), while circumventing issues with
various orders.
Choosing the "null formation" as the default formation de-activates the
behaviour entirely, and plays out exactly like SVN.
This makes it possible to queue a formation-order then a
noformation-order (i.e. walk then repair), though the behaviour isn't
very flexible.
For modders, it should be relatively easy to change the setup for each
order, and/or to force deactivating/activating formations in general.
Tested by: Freagarach, Angen
Refs #3479, #1791.
Makes #3478 mostly invalid.
Differential Revision: https://code.wildfiregames.com/D2764
This was SVN commit r24480.
2020-12-31 02:04:58 -08:00
"Order.LeaveFormation" : function ( ) {
if ( ! this . IsFormationMember ( ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
Allow picking a default formation for walk (and walk-like) orders.
This allows choosing a "default formation", which is activated
automatically for units given walk orders (and attack-walk etc.).
Conversely, units in formation that are given a gather/build/... order
are taken out of formation and given the order individually.
The default formation can be selected by right-clicking on any formation
icon.
This leverages formations for walking, where they are quite efficient
(in fact, perhaps too efficient), while circumventing issues with
various orders.
Choosing the "null formation" as the default formation de-activates the
behaviour entirely, and plays out exactly like SVN.
This makes it possible to queue a formation-order then a
noformation-order (i.e. walk then repair), though the behaviour isn't
very flexible.
For modders, it should be relatively easy to change the setup for each
order, and/or to force deactivating/activating formations in general.
Tested by: Freagarach, Angen
Refs #3479, #1791.
Makes #3478 mostly invalid.
Differential Revision: https://code.wildfiregames.com/D2764
This was SVN commit r24480.
2020-12-31 02:04:58 -08:00
let cmpFormation = Engine . QueryInterface ( this . formationController , IID _Formation ) ;
if ( cmpFormation )
{
cmpFormation . SetRearrange ( false ) ;
2021-01-23 00:07:56 -08:00
// Triggers FormationLeave, which ultimately will FinishOrder,
// discarding this order.
Allow picking a default formation for walk (and walk-like) orders.
This allows choosing a "default formation", which is activated
automatically for units given walk orders (and attack-walk etc.).
Conversely, units in formation that are given a gather/build/... order
are taken out of formation and given the order individually.
The default formation can be selected by right-clicking on any formation
icon.
This leverages formations for walking, where they are quite efficient
(in fact, perhaps too efficient), while circumventing issues with
various orders.
Choosing the "null formation" as the default formation de-activates the
behaviour entirely, and plays out exactly like SVN.
This makes it possible to queue a formation-order then a
noformation-order (i.e. walk then repair), though the behaviour isn't
very flexible.
For modders, it should be relatively easy to change the setup for each
order, and/or to force deactivating/activating formations in general.
Tested by: Freagarach, Angen
Refs #3479, #1791.
Makes #3478 mostly invalid.
Differential Revision: https://code.wildfiregames.com/D2764
This was SVN commit r24480.
2020-12-31 02:04:58 -08:00
cmpFormation . RemoveMembers ( [ this . entity ] ) ;
cmpFormation . SetRearrange ( true ) ;
}
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
Allow picking a default formation for walk (and walk-like) orders.
This allows choosing a "default formation", which is activated
automatically for units given walk orders (and attack-walk etc.).
Conversely, units in formation that are given a gather/build/... order
are taken out of formation and given the order individually.
The default formation can be selected by right-clicking on any formation
icon.
This leverages formations for walking, where they are quite efficient
(in fact, perhaps too efficient), while circumventing issues with
various orders.
Choosing the "null formation" as the default formation de-activates the
behaviour entirely, and plays out exactly like SVN.
This makes it possible to queue a formation-order then a
noformation-order (i.e. walk then repair), though the behaviour isn't
very flexible.
For modders, it should be relatively easy to change the setup for each
order, and/or to force deactivating/activating formations in general.
Tested by: Freagarach, Angen
Refs #3479, #1791.
Makes #3478 mostly invalid.
Differential Revision: https://code.wildfiregames.com/D2764
This was SVN commit r24480.
2020-12-31 02:04:58 -08:00
} ,
2010-09-03 02:55:14 -07:00
2012-08-07 22:32:53 -07:00
"Order.Stop" : function ( msg ) {
this . FinishOrder ( ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2012-08-07 22:32:53 -07:00
} ,
2010-09-03 02:55:14 -07:00
"Order.Walk" : function ( msg ) {
2020-11-09 00:38:09 -08:00
if ( ! this . AbleToMove ( ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2011-02-27 05:34:22 -08:00
2012-11-30 16:34:03 -08:00
if ( this . CanPack ( ) )
{
this . PushOrderFront ( "Pack" , { "force" : true } ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2012-11-30 16:34:03 -08:00
}
2021-03-03 00:21:00 -08:00
this . SetHeldPosition ( msg . data . x , msg . data . z ) ;
msg . data . relaxed = true ;
2021-02-27 12:13:40 -08:00
this . SetNextState ( "INDIVIDUAL.WALKING" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2010-09-03 02:55:14 -07:00
} ,
2013-02-25 13:56:24 -08:00
"Order.WalkAndFight" : function ( msg ) {
2020-11-09 00:38:09 -08:00
if ( ! this . AbleToMove ( ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2013-02-25 13:56:24 -08:00
if ( this . CanPack ( ) )
{
this . PushOrderFront ( "Pack" , { "force" : true } ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2013-02-25 13:56:24 -08:00
}
2021-03-03 00:21:00 -08:00
this . SetHeldPosition ( msg . data . x , msg . data . z ) ;
msg . data . relaxed = true ;
2021-02-27 12:13:40 -08:00
this . SetNextState ( "INDIVIDUAL.WALKINGANDFIGHTING" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2013-02-25 13:56:24 -08:00
} ,
2010-09-03 02:55:14 -07:00
"Order.WalkToTarget" : function ( msg ) {
2020-11-09 00:38:09 -08:00
if ( ! this . AbleToMove ( ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2011-02-27 05:34:22 -08:00
2012-11-30 16:34:03 -08:00
if ( this . CanPack ( ) )
{
this . PushOrderFront ( "Pack" , { "force" : true } ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2012-11-30 16:34:03 -08:00
}
2019-05-28 04:38:18 -07:00
2021-03-03 00:21:00 -08:00
if ( this . CheckRange ( msg . data ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2019-05-28 04:38:18 -07:00
2021-03-03 00:21:00 -08:00
msg . data . relaxed = true ;
2021-02-27 12:13:40 -08:00
this . SetNextState ( "INDIVIDUAL.WALKING" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2010-09-03 02:55:14 -07:00
} ,
2013-11-08 15:22:59 -08:00
"Order.PickupUnit" : function ( msg ) {
2021-03-30 04:24:08 -07:00
let cmpHolder = Engine . QueryInterface ( this . entity , msg . data . iid ) ;
if ( ! cmpHolder || cmpHolder . IsFull ( ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2013-11-08 15:22:59 -08:00
2021-04-09 00:08:33 -07:00
let range = cmpHolder . LoadingRange ( ) ;
2021-03-03 00:21:00 -08:00
msg . data . min = range . min ;
msg . data . max = range . max ;
if ( this . CheckRange ( msg . data ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2021-02-27 22:29:53 -08:00
2019-07-17 11:11:15 -07:00
// Check if we need to move
2020-08-03 05:02:24 -07:00
// If the target can reach us and we are reasonably close, don't move.
// TODO: it would be slightly more optimal to check for real, not bird-flight distance.
2021-03-03 00:21:00 -08:00
let cmpPassengerMotion = Engine . QueryInterface ( msg . data . target , IID _UnitMotion ) ;
2020-08-03 05:02:24 -07:00
if ( cmpPassengerMotion &&
cmpPassengerMotion . IsTargetRangeReachable ( this . entity , range . min , range . max ) &&
2021-03-03 00:21:00 -08:00
PositionHelper . DistanceBetweenEntities ( this . entity , msg . data . target ) < 200 )
2013-11-08 15:22:59 -08:00
this . SetNextState ( "INDIVIDUAL.PICKUP.LOADING" ) ;
2021-03-30 08:50:03 -07:00
else if ( this . AbleToMove ( ) )
2020-08-03 05:02:24 -07:00
this . SetNextState ( "INDIVIDUAL.PICKUP.APPROACHING" ) ;
2021-03-30 08:50:03 -07:00
else
return this . FinishOrder ( ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2013-11-08 15:22:59 -08:00
} ,
2013-11-30 09:30:08 -08:00
"Order.Guard" : function ( msg ) {
2021-03-03 00:21:00 -08:00
if ( ! this . AddGuard ( msg . data . target ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2013-11-30 09:30:08 -08:00
2021-03-30 08:50:03 -07:00
if ( this . CheckTargetRangeExplicit ( this . isGuardOf , 0 , this . guardRange ) )
this . SetNextState ( "INDIVIDUAL.GUARD.GUARDING" ) ;
else if ( this . AbleToMove ( ) )
2013-11-30 09:30:08 -08:00
this . SetNextState ( "INDIVIDUAL.GUARD.ESCORTING" ) ;
else
2021-03-30 08:50:03 -07:00
return this . FinishOrder ( ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2013-11-30 09:30:08 -08:00
} ,
2011-03-04 06:36:41 -08:00
"Order.Flee" : function ( msg ) {
2021-03-30 08:50:03 -07:00
if ( ! this . AbleToMove ( ) )
return this . FinishOrder ( ) ;
2021-02-27 12:13:40 -08:00
this . SetNextState ( "INDIVIDUAL.FLEEING" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2011-03-04 06:36:41 -08:00
} ,
2010-09-03 02:55:14 -07:00
"Order.Attack" : function ( msg ) {
2021-03-03 00:21:00 -08:00
let type = this . GetBestAttackAgainst ( msg . data . target , msg . data . allowCapture ) ;
2010-09-03 02:55:14 -07:00
if ( ! type )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2021-02-27 22:29:53 -08:00
2021-03-03 00:21:00 -08:00
msg . data . attackType = type ;
2010-09-03 02:55:14 -07:00
2019-07-01 23:49:27 -07:00
this . RememberTargetPosition ( ) ;
2021-03-03 00:21:00 -08:00
if ( msg . data . hunting && this . orderQueue . length > 1 && this . orderQueue [ 1 ] . type === "Gather" )
2019-07-24 08:55:07 -07:00
this . RememberTargetPosition ( this . orderQueue [ 1 ] . data ) ;
2019-07-01 23:49:27 -07:00
2021-03-03 00:21:00 -08:00
if ( this . CheckTargetAttackRange ( msg . data . target , msg . data . attackType ) )
2011-06-17 15:13:39 -07:00
{
2012-11-30 16:34:03 -08:00
if ( this . CanUnpack ( ) )
{
this . PushOrderFront ( "Unpack" , { "force" : true } ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2012-11-30 16:34:03 -08:00
}
2019-12-28 03:40:24 -08:00
// Cancel any current packing order.
2021-02-27 22:29:53 -08:00
if ( this . EnsureCorrectPackStateForAttack ( false ) )
this . SetNextState ( "INDIVIDUAL.COMBAT.ATTACKING" ) ;
2019-12-28 03:40:24 -08:00
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2011-06-24 05:35:15 -07:00
}
2020-11-04 09:50:20 -08:00
// If we're hunting, that's a special case where we should continue attacking our target.
2021-03-03 00:21:00 -08:00
if ( this . GetStance ( ) . respondStandGround && ! msg . data . force && ! msg . data . hunting || ! this . AbleToMove ( ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2018-04-25 16:47:24 -07:00
2018-03-27 10:56:32 -07:00
if ( this . CanPack ( ) )
2012-11-30 16:34:03 -08:00
{
2018-03-27 10:56:32 -07:00
this . PushOrderFront ( "Pack" , { "force" : true } ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2012-11-30 16:34:03 -08:00
}
2019-12-28 03:40:24 -08:00
// If we're currently packing/unpacking, make sure we are packed, so we can move.
2021-02-27 22:29:53 -08:00
if ( this . EnsureCorrectPackStateForAttack ( true ) )
this . SetNextState ( "INDIVIDUAL.COMBAT.APPROACHING" ) ;
return ACCEPT _ORDER ;
2010-09-03 02:55:14 -07:00
} ,
2016-09-25 14:33:05 -07:00
"Order.Patrol" : function ( msg ) {
2021-02-27 12:13:40 -08:00
if ( ! this . AbleToMove ( ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2016-09-25 14:33:05 -07:00
if ( this . CanPack ( ) )
{
this . PushOrderFront ( "Pack" , { "force" : true } ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2016-09-25 14:33:05 -07:00
}
2021-03-03 00:21:00 -08:00
msg . data . relaxed = true ;
2019-07-22 11:07:24 -07:00
2020-12-10 01:18:13 -08:00
this . SetNextState ( "INDIVIDUAL.PATROL.PATROLLING" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2016-09-25 14:33:05 -07:00
} ,
2012-04-17 13:22:13 -07:00
"Order.Heal" : function ( msg ) {
2021-03-03 00:21:00 -08:00
if ( ! this . TargetIsAlive ( msg . data . target ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2012-04-17 13:22:13 -07:00
2012-08-12 19:56:51 -07:00
// Healers can't heal themselves.
2021-03-03 00:21:00 -08:00
if ( msg . data . target == this . entity )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2012-08-12 19:56:51 -07:00
2021-03-03 00:21:00 -08:00
if ( this . CheckTargetRange ( msg . data . target , IID _Heal ) )
2012-04-17 13:22:13 -07:00
{
this . SetNextState ( "INDIVIDUAL.HEAL.HEALING" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2012-04-17 13:22:13 -07:00
}
2021-03-30 08:50:03 -07:00
if ( ! this . AbleToMove ( ) )
return this . FinishOrder ( ) ;
2012-04-17 13:22:13 -07:00
2021-03-03 00:21:00 -08:00
if ( this . GetStance ( ) . respondStandGround && ! msg . data . force )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2012-04-17 13:22:13 -07:00
2019-05-28 04:38:18 -07:00
this . SetNextState ( "INDIVIDUAL.HEAL.APPROACHING" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2012-04-17 13:22:13 -07:00
} ,
2010-09-03 02:55:14 -07:00
"Order.Gather" : function ( msg ) {
2021-04-29 23:24:13 -07:00
let cmpResourceGatherer = Engine . QueryInterface ( this . entity , IID _ResourceGatherer ) ;
if ( ! cmpResourceGatherer )
return this . FinishOrder ( ) ;
// We were given the order to gather while we were still gathering.
// This is needed because we don't re-enter the GATHER-state.
2021-09-23 23:33:53 -07:00
const taskedResourceType = cmpResourceGatherer . GetTaskedResourceType ( ) ;
if ( taskedResourceType && msg . data . type . generic != taskedResourceType )
2021-04-29 23:24:13 -07:00
this . UnitFsm . SwitchToNextState ( this , "INDIVIDUAL.GATHER" ) ;
2021-03-03 00:21:00 -08:00
if ( ! this . CanGather ( msg . data . target ) )
2020-11-12 03:01:42 -08:00
{
this . SetNextState ( "INDIVIDUAL.GATHER.FINDINGNEWTARGET" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2020-11-12 03:01:42 -08:00
}
2021-03-03 00:21:00 -08:00
if ( this . MustKillGatherTarget ( msg . data . target ) )
2010-10-06 14:37:55 -07:00
{
2021-12-09 08:35:03 -08:00
const bestAttack = Engine . QueryInterface ( this . entity , IID _Attack ) ? . GetBestAttackAgainst ( msg . data . target , false ) ;
2010-10-06 14:37:55 -07:00
// Make sure we can attack the target, else we'll get very stuck
2021-12-09 08:35:03 -08:00
if ( ! bestAttack )
2010-10-06 14:37:55 -07:00
{
// Oops, we can't attack at all - give up
// TODO: should do something so the player knows why this failed
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2010-10-06 14:37:55 -07:00
}
2013-01-28 14:19:06 -08:00
// The target was visible when this order was issued,
// but could now be invisible again.
2021-03-03 00:21:00 -08:00
if ( ! this . CheckTargetVisible ( msg . data . target ) )
2013-01-28 14:19:06 -08:00
{
2021-03-03 00:21:00 -08:00
if ( msg . data . secondTry === undefined )
2013-01-28 14:19:06 -08:00
{
2021-03-03 00:21:00 -08:00
msg . data . secondTry = true ;
this . PushOrderFront ( "Walk" , msg . data . lastPos ) ;
2013-01-28 14:19:06 -08:00
}
2019-07-28 02:48:23 -07:00
// We couldn't move there, or the target moved away
2021-03-03 00:21:00 -08:00
else if ( ! this . FinishOrder ( ) )
this . PushOrderFront ( "GatherNearPosition" , {
"x" : msg . data . lastPos . x ,
"z" : msg . data . lastPos . z ,
"type" : msg . data . type ,
"template" : msg . data . template
} ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2013-01-28 14:19:06 -08:00
}
2010-10-06 14:37:55 -07:00
2021-12-09 08:35:03 -08:00
if ( ! this . AbleToMove ( ) && ! this . CheckTargetRange ( msg . data . target , IID _Attack , bestAttack ) )
return this . FinishOrder ( ) ;
2023-01-09 06:29:06 -08:00
this . PushOrderFront ( "Attack" , { "target" : msg . data . target , "force" : ! ! msg . data . force , "hunting" : true } ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2010-10-06 14:37:55 -07:00
}
2021-06-04 01:44:15 -07:00
// If the unit is full go to the nearest dropsite instead of trying to gather.
if ( ! cmpResourceGatherer . CanCarryMore ( msg . data . type . generic ) )
{
this . SetNextState ( "INDIVIDUAL.GATHER.RETURNINGRESOURCE" ) ;
return ACCEPT _ORDER ;
}
2019-07-01 23:49:27 -07:00
this . RememberTargetPosition ( ) ;
2021-03-03 00:21:00 -08:00
if ( ! msg . data . initPos )
msg . data . initPos = msg . data . lastPos ;
2019-07-01 23:49:27 -07:00
2021-03-03 00:21:00 -08:00
if ( this . CheckTargetRange ( msg . data . target , IID _ResourceGatherer ) )
2019-01-05 01:01:51 -08:00
this . SetNextState ( "INDIVIDUAL.GATHER.GATHERING" ) ;
2021-03-30 08:50:03 -07:00
else if ( this . AbleToMove ( ) )
2019-05-28 04:38:18 -07:00
this . SetNextState ( "INDIVIDUAL.GATHER.APPROACHING" ) ;
2021-03-30 08:50:03 -07:00
else
return this . FinishOrder ( ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2010-09-03 02:55:14 -07:00
} ,
2012-05-18 14:31:57 -07:00
2012-03-01 15:16:01 -08:00
"Order.GatherNearPosition" : function ( msg ) {
2021-03-30 08:50:03 -07:00
if ( ! this . AbleToMove ( ) )
return this . FinishOrder ( ) ;
2011-12-16 08:08:26 -08:00
this . SetNextState ( "INDIVIDUAL.GATHER.WALKING" ) ;
2021-03-03 00:21:00 -08:00
msg . data . initPos = { 'x' : msg . data . x , 'z' : msg . data . z } ;
msg . data . relaxed = true ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2011-12-16 08:08:26 -08:00
} ,
2010-11-13 11:15:29 -08:00
2021-08-27 22:52:37 -07:00
"Order.DropAtNearestDropSite" : function ( msg ) {
const cmpResourceGatherer = Engine . QueryInterface ( this . entity , IID _ResourceGatherer ) ;
if ( ! cmpResourceGatherer )
return this . FinishOrder ( ) ;
const nearby = this . FindNearestDropsite ( cmpResourceGatherer . GetMainCarryingType ( ) ) ;
if ( ! nearby )
return this . FinishOrder ( ) ;
this . ReturnResource ( nearby , false , true ) ;
return ACCEPT _ORDER ;
} ,
2010-11-13 11:15:29 -08:00
"Order.ReturnResource" : function ( msg ) {
2021-04-12 05:50:17 -07:00
if ( this . CheckTargetRange ( msg . data . target , IID _ResourceGatherer ) )
2021-05-07 00:05:57 -07:00
this . SetNextState ( "INDIVIDUAL.RETURNRESOURCE.DROPPINGRESOURCES" ) ;
2021-03-30 08:50:03 -07:00
else if ( this . AbleToMove ( ) )
2021-05-07 00:05:57 -07:00
this . SetNextState ( "INDIVIDUAL.RETURNRESOURCE.APPROACHING" ) ;
2021-03-30 08:50:03 -07:00
else
return this . FinishOrder ( ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2010-11-13 11:15:29 -08:00
} ,
2012-03-08 12:42:28 -08:00
"Order.Trade" : function ( msg ) {
2021-03-30 08:50:03 -07:00
if ( ! this . AbleToMove ( ) )
return this . FinishOrder ( ) ;
2021-02-27 22:29:53 -08:00
// We must check if this trader has both markets in case it was a back-to-work order.
let cmpTrader = Engine . QueryInterface ( this . entity , IID _Trader ) ;
2016-05-14 05:27:48 -07:00
if ( ! cmpTrader || ! cmpTrader . HasBothMarkets ( ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2013-11-06 13:07:19 -08:00
2021-01-18 03:00:02 -08:00
this . waypoints = [ ] ;
2019-05-28 04:38:18 -07:00
this . SetNextState ( "TRADE.APPROACHINGMARKET" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2012-03-08 12:42:28 -08:00
} ,
2010-09-03 02:55:14 -07:00
"Order.Repair" : function ( msg ) {
2021-03-03 00:21:00 -08:00
if ( this . CheckTargetRange ( msg . data . target , IID _Builder ) )
2019-01-05 01:01:51 -08:00
this . SetNextState ( "INDIVIDUAL.REPAIR.REPAIRING" ) ;
2021-03-30 08:50:03 -07:00
else if ( this . AbleToMove ( ) )
2019-05-28 04:38:18 -07:00
this . SetNextState ( "INDIVIDUAL.REPAIR.APPROACHING" ) ;
2021-03-30 08:50:03 -07:00
else
return this . FinishOrder ( ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2010-09-03 02:55:14 -07:00
} ,
2012-05-18 14:31:57 -07:00
2010-10-23 15:43:15 -07:00
"Order.Garrison" : function ( msg ) {
2020-06-02 04:40:29 -07:00
if ( ! this . AbleToMove ( ) )
2021-03-17 07:53:09 -07:00
return this . FinishOrder ( ) ;
2015-10-08 14:59:52 -07:00
2021-01-27 07:11:57 -08:00
// Also pack when we are in range.
2012-12-03 14:00:19 -08:00
if ( this . CanPack ( ) )
{
this . PushOrderFront ( "Pack" , { "force" : true } ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2012-12-03 14:00:19 -08:00
}
2021-03-30 04:17:46 -07:00
if ( this . CheckTargetRange ( msg . data . target , msg . data . garrison ? IID _Garrisonable : IID _Turretable ) )
2021-03-17 07:53:09 -07:00
this . SetNextState ( "INDIVIDUAL.GARRISON.GARRISONING" ) ;
2021-02-27 22:29:53 -08:00
else
this . SetNextState ( "INDIVIDUAL.GARRISON.APPROACHING" ) ;
return ACCEPT _ORDER ;
2010-10-23 15:43:15 -07:00
} ,
2012-05-18 14:31:57 -07:00
2021-03-17 07:53:09 -07:00
"Order.Ungarrison" : function ( msg ) {
2021-03-07 23:19:06 -08:00
// Note that this order MUST succeed, or we break
// the assumptions done in garrisonable/garrisonHolder,
// especially in Unloading in the latter. (For user feedback.)
// ToDo: This can be fixed by not making that assumption :)
2015-10-12 10:12:36 -07:00
this . FinishOrder ( ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2015-10-12 10:12:36 -07:00
} ,
2020-09-10 09:37:14 -07:00
"Order.Cheer" : function ( msg ) {
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2011-05-02 08:03:01 -07:00
} ,
2010-09-03 02:55:14 -07:00
2012-11-30 16:34:03 -08:00
"Order.Pack" : function ( msg ) {
2021-02-27 22:29:53 -08:00
if ( ! this . CanPack ( ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2021-02-27 22:29:53 -08:00
this . SetNextState ( "INDIVIDUAL.PACKING" ) ;
return ACCEPT _ORDER ;
2012-11-30 16:34:03 -08:00
} ,
"Order.Unpack" : function ( msg ) {
2021-02-27 22:29:53 -08:00
if ( ! this . CanUnpack ( ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2021-02-27 22:29:53 -08:00
this . SetNextState ( "INDIVIDUAL.UNPACKING" ) ;
return ACCEPT _ORDER ;
2012-11-30 16:34:03 -08:00
} ,
2021-02-10 00:14:38 -08:00
"Order.MoveToChasingPoint" : function ( msg ) {
// Overriden by the CHASING state.
// Can however happen outside of it when renaming...
// TODO: don't use an order for that behaviour.
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2021-02-10 00:14:38 -08:00
} ,
2021-03-02 23:47:38 -08:00
"Order.CollectTreasure" : function ( msg ) {
2021-05-18 09:51:44 -07:00
if ( this . CheckTargetRange ( msg . data . target , IID _TreasureCollector ) )
2021-05-11 00:41:40 -07:00
this . SetNextState ( "INDIVIDUAL.COLLECTTREASURE.COLLECTING" ) ;
else if ( this . AbleToMove ( ) )
this . SetNextState ( "INDIVIDUAL.COLLECTTREASURE.APPROACHING" ) ;
else
2021-03-02 23:47:38 -08:00
return this . FinishOrder ( ) ;
return ACCEPT _ORDER ;
} ,
2021-03-03 10:20:49 -08:00
"Order.CollectTreasureNearPosition" : function ( msg ) {
2021-05-11 00:41:40 -07:00
if ( ! this . AbleToMove ( ) )
return this . FinishOrder ( ) ;
this . SetNextState ( "INDIVIDUAL.COLLECTTREASURE.WALKING" ) ;
msg . data . initPos = { 'x' : msg . data . x , 'z' : msg . data . z } ;
msg . data . relaxed = true ;
2021-03-03 10:20:49 -08:00
return ACCEPT _ORDER ;
} ,
2010-09-03 02:55:14 -07:00
// States for the special entity representing a group of units moving in formation:
"FORMATIONCONTROLLER" : {
"Order.Walk" : function ( msg ) {
2021-03-30 08:50:03 -07:00
if ( ! this . AbleToMove ( ) )
return this . FinishOrder ( ) ;
2014-01-11 04:19:43 -08:00
this . CallMemberFunction ( "SetHeldPosition" , [ msg . data . x , msg . data . z ] ) ;
2010-09-03 02:55:14 -07:00
this . SetNextState ( "WALKING" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2010-09-03 02:55:14 -07:00
} ,
2013-02-25 13:56:24 -08:00
"Order.WalkAndFight" : function ( msg ) {
2021-03-30 08:50:03 -07:00
if ( ! this . AbleToMove ( ) )
return this . FinishOrder ( ) ;
2014-01-11 04:19:43 -08:00
this . CallMemberFunction ( "SetHeldPosition" , [ msg . data . x , msg . data . z ] ) ;
2013-02-25 13:56:24 -08:00
this . SetNextState ( "WALKINGANDFIGHTING" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2013-02-25 13:56:24 -08:00
} ,
2015-02-05 18:11:39 -08:00
2012-12-01 17:52:27 -08:00
"Order.MoveIntoFormation" : function ( msg ) {
2021-03-30 08:50:03 -07:00
if ( ! this . AbleToMove ( ) )
return this . FinishOrder ( ) ;
2014-01-11 04:19:43 -08:00
this . CallMemberFunction ( "SetHeldPosition" , [ msg . data . x , msg . data . z ] ) ;
2012-12-01 17:52:27 -08:00
this . SetNextState ( "FORMING" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2012-12-01 17:52:27 -08:00
} ,
2010-09-03 02:55:14 -07:00
2021-02-27 22:29:53 -08:00
// Only used by other orders to walk there in formation.
2012-05-18 14:31:57 -07:00
"Order.WalkToTargetRange" : function ( msg ) {
2021-03-03 00:21:00 -08:00
if ( this . CheckRange ( msg . data ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2021-03-30 08:50:03 -07:00
if ( ! this . AbleToMove ( ) )
return this . FinishOrder ( ) ;
2021-02-27 22:29:53 -08:00
this . SetNextState ( "WALKING" ) ;
return ACCEPT _ORDER ;
2012-12-02 09:25:23 -08:00
} ,
2014-01-28 10:56:39 -08:00
"Order.WalkToTarget" : function ( msg ) {
2021-03-03 00:21:00 -08:00
if ( this . CheckRange ( msg . data ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2021-03-30 08:50:03 -07:00
if ( ! this . AbleToMove ( ) )
return this . FinishOrder ( ) ;
2021-02-27 22:29:53 -08:00
this . SetNextState ( "WALKING" ) ;
return ACCEPT _ORDER ;
2014-01-28 10:56:39 -08:00
} ,
2012-12-02 09:25:23 -08:00
"Order.WalkToPointRange" : function ( msg ) {
2021-03-03 00:21:00 -08:00
if ( this . CheckRange ( msg . data ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2021-03-30 08:50:03 -07:00
if ( ! this . AbleToMove ( ) )
return this . FinishOrder ( ) ;
2021-02-27 22:29:53 -08:00
this . SetNextState ( "WALKING" ) ;
return ACCEPT _ORDER ;
2012-05-18 14:31:57 -07:00
} ,
2016-09-25 14:33:05 -07:00
"Order.Patrol" : function ( msg ) {
2021-03-30 08:50:03 -07:00
if ( ! this . AbleToMove ( ) )
return this . FinishOrder ( ) ;
2016-09-25 14:33:05 -07:00
this . CallMemberFunction ( "SetHeldPosition" , [ msg . data . x , msg . data . z ] ) ;
2020-12-10 01:18:13 -08:00
this . SetNextState ( "PATROL.PATROLLING" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2016-09-25 14:33:05 -07:00
} ,
2013-11-30 09:30:08 -08:00
"Order.Guard" : function ( msg ) {
2014-01-11 04:19:43 -08:00
this . CallMemberFunction ( "Guard" , [ msg . data . target , false ] ) ;
2021-02-27 22:29:53 -08:00
Engine . QueryInterface ( this . entity , IID _Formation ) . Disband ( ) ;
return ACCEPT _ORDER ;
2013-11-30 09:30:08 -08:00
} ,
2012-08-07 22:32:53 -07:00
"Order.Stop" : function ( msg ) {
2020-01-10 02:03:15 -08:00
let cmpFormation = Engine . QueryInterface ( this . entity , IID _Formation ) ;
cmpFormation . ResetOrderVariant ( ) ;
2015-10-05 13:40:14 -07:00
if ( ! this . IsAttackingAsFormation ( ) )
this . CallMemberFunction ( "Stop" , [ false ] ) ;
2014-01-22 05:20:55 -08:00
this . FinishOrder ( ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2020-07-07 03:24:58 -07:00
// Don't move the members back into formation,
// as the formation then resets and it looks odd when walk-stopping.
// TODO: this should be improved in the formation reshaping code.
2012-08-07 22:32:53 -07:00
} ,
2010-09-03 02:55:14 -07:00
"Order.Attack" : function ( msg ) {
2020-04-07 10:35:25 -07:00
let target = msg . data . target ;
let cmpTargetUnitAI = Engine . QueryInterface ( target , IID _UnitAI ) ;
2014-01-21 08:50:58 -08:00
if ( cmpTargetUnitAI && cmpTargetUnitAI . IsFormationMember ( ) )
target = cmpTargetUnitAI . GetFormationController ( ) ;
2020-04-07 10:35:25 -07:00
if ( ! this . CheckFormationTargetAttackRange ( target ) )
2012-12-01 17:52:27 -08:00
{
2021-03-30 08:50:03 -07:00
if ( this . AbleToMove ( ) && this . CheckTargetVisible ( target ) )
2014-01-11 04:19:43 -08:00
{
2019-05-28 04:38:18 -07:00
this . SetNextState ( "COMBAT.APPROACHING" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2014-01-11 04:19:43 -08:00
}
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2012-12-01 17:52:27 -08:00
}
2023-01-09 06:29:06 -08:00
this . CallMemberFunction ( "Attack" , [ target , msg . data . allowCapture , false ] ) ;
2020-04-07 10:35:25 -07:00
let cmpAttack = Engine . QueryInterface ( this . entity , IID _Attack ) ;
if ( cmpAttack && cmpAttack . CanAttackAsFormation ( ) )
2014-05-04 13:22:31 -07:00
this . SetNextState ( "COMBAT.ATTACKING" ) ;
2014-01-22 07:13:07 -08:00
else
this . SetNextState ( "MEMBER" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2012-04-17 13:22:13 -07:00
} ,
2012-12-01 17:52:27 -08:00
"Order.Garrison" : function ( msg ) {
2021-03-26 03:18:30 -07:00
if ( ! Engine . QueryInterface ( msg . data . target ,
msg . data . garrison ? IID _GarrisonHolder : IID _TurretHolder ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2021-03-30 04:17:46 -07:00
if ( this . CheckTargetRange ( msg . data . target , msg . data . garrison ? IID _Garrisonable : IID _Turretable ) )
2012-05-18 14:31:57 -07:00
{
2021-03-30 08:50:03 -07:00
if ( ! this . AbleToMove ( ) || ! this . CheckTargetVisible ( msg . data . target ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2012-05-18 14:31:57 -07:00
2021-02-27 22:29:53 -08:00
this . SetNextState ( "GARRISON.APPROACHING" ) ;
}
else
this . SetNextState ( "GARRISON.GARRISONING" ) ;
return ACCEPT _ORDER ;
2010-09-03 02:55:14 -07:00
} ,
"Order.Gather" : function ( msg ) {
2013-01-28 14:19:06 -08:00
if ( this . MustKillGatherTarget ( msg . data . target ) )
2012-12-02 15:11:55 -08:00
{
2013-01-28 14:19:06 -08:00
// The target was visible when this order was given,
// but could now be invisible.
if ( ! this . CheckTargetVisible ( msg . data . target ) )
{
if ( msg . data . secondTry === undefined )
{
msg . data . secondTry = true ;
this . PushOrderFront ( "Walk" , msg . data . lastPos ) ;
}
2019-07-28 02:48:23 -07:00
// We couldn't move there, or the target moved away
2013-01-28 14:19:06 -08:00
else
{
2019-07-28 02:48:23 -07:00
let data = msg . data ;
if ( ! this . FinishOrder ( ) )
this . PushOrderFront ( "GatherNearPosition" , {
"x" : data . lastPos . x ,
"z" : data . lastPos . z ,
"type" : data . type ,
"template" : data . template
} ) ;
2013-01-28 14:19:06 -08:00
}
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2013-01-28 14:19:06 -08:00
}
2023-01-09 06:29:06 -08:00
this . PushOrderFront ( "Attack" , { "target" : msg . data . target , "force" : ! ! msg . data . force , "hunting" : true , "min" : 0 , "max" : 10 } ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2012-12-02 15:11:55 -08:00
}
2012-12-01 17:52:27 -08:00
// TODO: on what should we base this range?
if ( ! this . CheckTargetRangeExplicit ( msg . data . target , 0 , 10 ) )
{
2012-12-10 15:35:09 -08:00
if ( ! this . CanGather ( msg . data . target ) || ! this . CheckTargetVisible ( msg . data . target ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2012-12-01 17:52:27 -08:00
// TODO: Should we issue a gather-near-position order
// if the target isn't gatherable/doesn't exist anymore?
2021-02-28 02:11:30 -08:00
if ( ! msg . data . secondTry )
{
msg . data . secondTry = true ;
this . PushOrderFront ( "WalkToTargetRange" , { "target" : msg . data . target , "min" : 0 , "max" : 10 } ) ;
return ACCEPT _ORDER ;
}
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2012-12-01 17:52:27 -08:00
}
2012-09-10 17:56:23 -07:00
2014-01-11 04:19:43 -08:00
this . CallMemberFunction ( "Gather" , [ msg . data . target , false ] ) ;
2012-09-10 17:56:23 -07:00
2019-01-05 01:01:51 -08:00
this . SetNextState ( "MEMBER" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2010-09-03 02:55:14 -07:00
} ,
2012-05-18 14:31:57 -07:00
2012-03-01 15:16:01 -08:00
"Order.GatherNearPosition" : function ( msg ) {
2012-12-01 17:52:27 -08:00
// TODO: on what should we base this range?
2012-12-02 09:25:23 -08:00
if ( ! this . CheckPointRangeExplicit ( msg . data . x , msg . data . z , 0 , 20 ) )
2012-12-01 17:52:27 -08:00
{
2012-12-02 09:25:23 -08:00
// Out of range; move there in formation
this . PushOrderFront ( "WalkToPointRange" , { "x" : msg . data . x , "z" : msg . data . z , "min" : 0 , "max" : 20 } ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2012-12-01 17:52:27 -08:00
}
2014-01-11 04:19:43 -08:00
this . CallMemberFunction ( "GatherNearPosition" , [ msg . data . x , msg . data . z , msg . data . type , msg . data . template , false ] ) ;
2012-12-01 17:52:27 -08:00
2019-01-05 01:01:51 -08:00
this . SetNextState ( "MEMBER" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2011-12-16 08:08:26 -08:00
} ,
2010-09-03 02:55:14 -07:00
2012-12-01 17:52:27 -08:00
"Order.Heal" : function ( msg ) {
// TODO: on what should we base this range?
if ( ! this . CheckTargetRangeExplicit ( msg . data . target , 0 , 10 ) )
{
2012-12-10 15:55:16 -08:00
if ( ! this . TargetIsAlive ( msg . data . target ) || ! this . CheckTargetVisible ( msg . data . target ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2021-02-27 22:29:53 -08:00
2021-02-28 02:11:30 -08:00
if ( ! msg . data . secondTry )
{
msg . data . secondTry = true ;
this . PushOrderFront ( "WalkToTargetRange" , { "target" : msg . data . target , "min" : 0 , "max" : 10 } ) ;
return ACCEPT _ORDER ;
}
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2012-12-01 17:52:27 -08:00
}
2014-01-11 04:19:43 -08:00
this . CallMemberFunction ( "Heal" , [ msg . data . target , false ] ) ;
2012-12-01 17:52:27 -08:00
2019-01-05 01:01:51 -08:00
this . SetNextState ( "MEMBER" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2010-11-13 11:15:29 -08:00
} ,
2021-08-03 09:42:56 -07:00
"Order.CollectTreasure" : function ( msg ) {
// TODO: on what should we base this range?
if ( this . CheckTargetRangeExplicit ( msg . data . target , 0 , 20 ) )
{
this . CallMemberFunction ( "CollectTreasure" , [ msg . data . target , false , false ] ) ;
this . SetNextState ( "MEMBER" ) ;
return ACCEPT _ORDER ;
}
if ( msg . data . secondTry || ! this . CheckTargetVisible ( msg . data . target ) )
return this . FinishOrder ( ) ;
msg . data . secondTry = true ;
this . PushOrderFront ( "WalkToTargetRange" , { "target" : msg . data . target , "min" : 0 , "max" : 20 } ) ;
return ACCEPT _ORDER ;
} ,
"Order.CollectTreasureNearPosition" : function ( msg ) {
// TODO: on what should we base this range?
if ( ! this . CheckPointRangeExplicit ( msg . data . x , msg . data . z , 0 , 20 ) )
{
this . PushOrderFront ( "WalkToPointRange" , { "x" : msg . data . x , "z" : msg . data . z , "min" : 0 , "max" : 20 } ) ;
return ACCEPT _ORDER ;
}
this . CallMemberFunction ( "CollectTreasureNearPosition" , [ msg . data . x , msg . data . z , false , false ] ) ;
this . SetNextState ( "MEMBER" ) ;
return ACCEPT _ORDER ;
} ,
2012-12-01 17:52:27 -08:00
"Order.Repair" : function ( msg ) {
// TODO: on what should we base this range?
if ( ! this . CheckTargetRangeExplicit ( msg . data . target , 0 , 10 ) )
{
2012-12-10 15:55:16 -08:00
if ( ! this . TargetIsAlive ( msg . data . target ) || ! this . CheckTargetVisible ( msg . data . target ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2021-02-27 22:29:53 -08:00
2021-02-28 02:11:30 -08:00
if ( ! msg . data . secondTry )
{
msg . data . secondTry = true ;
this . PushOrderFront ( "WalkToTargetRange" , { "target" : msg . data . target , "min" : 0 , "max" : 10 } ) ;
return ACCEPT _ORDER ;
}
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2012-12-01 17:52:27 -08:00
}
2014-01-11 04:19:43 -08:00
this . CallMemberFunction ( "Repair" , [ msg . data . target , msg . data . autocontinue , false ] ) ;
2012-12-01 17:52:27 -08:00
2019-01-05 01:01:51 -08:00
this . SetNextState ( "MEMBER" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2010-11-30 19:01:17 -08:00
} ,
2012-12-01 17:52:27 -08:00
"Order.ReturnResource" : function ( msg ) {
// TODO: on what should we base this range?
if ( ! this . CheckTargetRangeExplicit ( msg . data . target , 0 , 10 ) )
{
2020-12-05 23:22:51 -08:00
if ( ! this . CheckTargetVisible ( msg . data . target ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2021-02-27 22:29:53 -08:00
2021-02-28 02:11:30 -08:00
if ( ! msg . data . secondTry )
{
msg . data . secondTry = true ;
this . PushOrderFront ( "WalkToTargetRange" , { "target" : msg . data . target , "min" : 0 , "max" : 10 } ) ;
return ACCEPT _ORDER ;
}
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2012-12-01 17:52:27 -08:00
}
2014-01-11 04:19:43 -08:00
this . CallMemberFunction ( "ReturnResource" , [ msg . data . target , false ] ) ;
2012-12-01 17:52:27 -08:00
2019-01-05 01:01:51 -08:00
this . SetNextState ( "MEMBER" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2012-12-01 17:52:27 -08:00
} ,
2012-11-30 16:34:03 -08:00
"Order.Pack" : function ( msg ) {
2014-01-11 04:19:43 -08:00
this . CallMemberFunction ( "Pack" , [ false ] ) ;
2012-12-01 17:52:27 -08:00
2019-01-05 01:01:51 -08:00
this . SetNextState ( "MEMBER" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2012-11-30 16:34:03 -08:00
} ,
2012-12-01 17:52:27 -08:00
2012-11-30 16:34:03 -08:00
"Order.Unpack" : function ( msg ) {
2014-01-11 04:19:43 -08:00
this . CallMemberFunction ( "Unpack" , [ false ] ) ;
2012-12-01 17:52:27 -08:00
2019-01-05 01:01:51 -08:00
this . SetNextState ( "MEMBER" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2012-11-30 16:34:03 -08:00
} ,
2012-05-18 14:31:57 -07:00
2021-08-27 22:52:37 -07:00
"Order.DropAtNearestDropSite" : function ( msg ) {
this . CallMemberFunction ( "DropAtNearestDropSite" , [ false , false ] ) ;
this . SetNextState ( "MEMBER" ) ;
return ACCEPT _ORDER ;
} ,
2010-09-03 02:55:14 -07:00
"IDLE" : {
2014-01-22 05:20:55 -08:00
"enter" : function ( msg ) {
2021-02-10 01:59:39 -08:00
// Turn rearrange off. Otherwise, if the formation is idle
// but individual units go off to fight,
// any death will rearrange the formation, which looks odd.
2020-12-29 02:08:55 -08:00
// Instead, move idle units in formation on a timer.
let cmpFormation = Engine . QueryInterface ( this . entity , IID _Formation ) ;
cmpFormation . SetRearrange ( false ) ;
2021-02-10 01:59:39 -08:00
// Start the timer on the next turn to catch up with potential stragglers.
this . StartTimer ( 100 , 2000 ) ;
2021-01-13 07:27:18 -08:00
this . isIdle = true ;
this . CallMemberFunction ( "ResetIdle" ) ;
2019-12-28 05:43:18 -08:00
return false ;
2014-01-22 05:20:55 -08:00
} ,
2020-12-29 02:08:55 -08:00
"leave" : function ( ) {
2021-01-13 07:27:18 -08:00
this . isIdle = false ;
2020-12-29 02:08:55 -08:00
this . StopTimer ( ) ;
} ,
"Timer" : function ( msg ) {
let cmpFormation = Engine . QueryInterface ( this . entity , IID _Formation ) ;
if ( ! cmpFormation )
return ;
if ( this . TestAllMemberFunction ( "IsIdle" ) )
cmpFormation . MoveMembersIntoFormation ( false , false ) ;
} ,
2010-09-03 02:55:14 -07:00
} ,
"WALKING" : {
2019-05-28 04:38:18 -07:00
"enter" : function ( ) {
2021-02-28 02:11:30 -08:00
let cmpFormation = Engine . QueryInterface ( this . entity , IID _Formation ) ;
cmpFormation . SetRearrange ( true ) ;
cmpFormation . MoveMembersIntoFormation ( true , true ) ;
2019-05-28 04:38:18 -07:00
if ( ! this . MoveTo ( this . order . data ) )
{
this . FinishOrder ( ) ;
return true ;
}
2019-12-28 05:43:18 -08:00
return false ;
2019-05-28 04:38:18 -07:00
} ,
"leave" : function ( ) {
2021-01-05 02:12:47 -08:00
this . StopTimer ( ) ;
2019-05-28 04:38:18 -07:00
this . StopMoving ( ) ;
} ,
2019-06-09 04:16:40 -07:00
"MovementUpdate" : function ( msg ) {
2021-01-05 02:12:47 -08:00
if ( msg . veryObstructed && ! this . timer )
{
// It's possible that the controller (with large clearance)
// is stuck, but not the individual units.
// Ask them to move individually for a little while.
this . CallMemberFunction ( "MoveTo" , [ this . order . data ] ) ;
this . StartTimer ( 3000 ) ;
return ;
}
else if ( this . timer )
return ;
2019-07-22 11:07:24 -07:00
if ( msg . likelyFailure || this . CheckRange ( this . order . data ) )
this . FinishOrder ( ) ;
2014-01-11 04:19:43 -08:00
} ,
2021-01-05 02:12:47 -08:00
"Timer" : function ( ) {
// Reenter to reset the pathfinder state.
this . SetNextState ( "WALKING" ) ;
}
2014-01-11 04:19:43 -08:00
} ,
2013-02-25 13:56:24 -08:00
"WALKINGANDFIGHTING" : {
"enter" : function ( msg ) {
2021-02-28 02:11:30 -08:00
let cmpFormation = Engine . QueryInterface ( this . entity , IID _Formation ) ;
cmpFormation . SetRearrange ( true ) ;
cmpFormation . MoveMembersIntoFormation ( true , true , "combat" ) ;
2019-05-28 04:38:18 -07:00
if ( ! this . MoveTo ( this . order . data ) )
{
this . FinishOrder ( ) ;
return true ;
}
2013-02-25 13:56:24 -08:00
this . StartTimer ( 0 , 1000 ) ;
2022-03-31 22:46:13 -07:00
this . order . data . returningState = "WALKINGANDFIGHTING" ;
2019-12-28 05:43:18 -08:00
return false ;
2013-02-25 13:56:24 -08:00
} ,
2019-05-28 04:38:18 -07:00
"leave" : function ( ) {
this . StopMoving ( ) ;
2019-06-08 05:53:28 -07:00
this . StopTimer ( ) ;
2019-05-28 04:38:18 -07:00
} ,
2013-02-25 13:56:24 -08:00
"Timer" : function ( msg ) {
2021-03-22 06:27:33 -07:00
Engine . ProfileStart ( "FindWalkAndFightTargets" ) ;
2022-03-31 22:46:13 -07:00
if ( this . FindWalkAndFightTargets ( ) )
this . SetNextState ( "MEMBER" ) ;
2021-03-22 06:27:33 -07:00
Engine . ProfileStop ( ) ;
2013-02-25 13:56:24 -08:00
} ,
2019-06-09 04:16:40 -07:00
"MovementUpdate" : function ( msg ) {
2019-07-22 11:07:24 -07:00
if ( msg . likelyFailure || this . CheckRange ( this . order . data ) )
this . FinishOrder ( ) ;
2013-02-25 13:56:24 -08:00
} ,
} ,
2016-09-25 14:33:05 -07:00
"PATROL" : {
2020-12-10 01:18:13 -08:00
"enter" : function ( ) {
2016-09-25 14:33:05 -07:00
let cmpPosition = Engine . QueryInterface ( this . entity , IID _Position ) ;
if ( ! cmpPosition || ! cmpPosition . IsInWorld ( ) )
{
this . FinishOrder ( ) ;
2019-12-28 05:43:18 -08:00
return true ;
2016-09-25 14:33:05 -07:00
}
2020-11-04 09:50:20 -08:00
// Memorize the origin position in case that we want to go back.
2016-09-25 14:33:05 -07:00
if ( ! this . patrolStartPosOrder )
{
this . patrolStartPosOrder = cmpPosition . GetPosition ( ) ;
2016-11-22 04:46:46 -08:00
this . patrolStartPosOrder . targetClasses = this . order . data . targetClasses ;
2017-12-19 11:24:48 -08:00
this . patrolStartPosOrder . allowCapture = this . order . data . allowCapture ;
2016-09-25 14:33:05 -07:00
}
2020-12-10 01:18:13 -08:00
this . SetAnimationVariant ( "combat" ) ;
2019-06-08 05:53:28 -07:00
2019-12-28 05:43:18 -08:00
return false ;
2016-09-25 14:33:05 -07:00
} ,
2020-12-10 01:18:13 -08:00
"leave" : function ( ) {
2016-09-25 14:33:05 -07:00
delete this . patrolStartPosOrder ;
2020-12-10 01:18:13 -08:00
this . SetDefaultAnimationVariant ( ) ;
2016-09-25 14:33:05 -07:00
} ,
2020-12-10 01:18:13 -08:00
"PATROLLING" : {
"enter" : function ( ) {
2021-02-28 02:11:30 -08:00
let cmpFormation = Engine . QueryInterface ( this . entity , IID _Formation ) ;
cmpFormation . SetRearrange ( true ) ;
cmpFormation . MoveMembersIntoFormation ( true , true , "combat" ) ;
2020-12-10 01:18:13 -08:00
let cmpPosition = Engine . QueryInterface ( this . entity , IID _Position ) ;
if ( ! cmpPosition || ! cmpPosition . IsInWorld ( ) ||
! this . MoveTo ( this . order . data ) )
{
this . FinishOrder ( ) ;
return true ;
}
this . StartTimer ( 0 , 1000 ) ;
2022-03-31 22:46:13 -07:00
this . order . data . returningState = "PATROL.PATROLLING" ;
2020-12-10 01:18:13 -08:00
return false ;
} ,
"leave" : function ( ) {
this . StopMoving ( ) ;
this . StopTimer ( ) ;
} ,
"Timer" : function ( msg ) {
2022-03-31 22:46:13 -07:00
if ( this . FindWalkAndFightTargets ( ) )
this . SetNextState ( "MEMBER" ) ;
2020-12-10 01:18:13 -08:00
} ,
"MovementUpdate" : function ( msg ) {
if ( ! msg . likelyFailure && ! msg . likelySuccess && ! this . RelaxedMaxRangeCheck ( this . order . data , this . DefaultRelaxedMaxRange ) )
return ;
if ( this . orderQueue . length == 1 )
this . PushOrder ( "Patrol" , this . patrolStartPosOrder ) ;
this . PushOrder ( this . order . type , this . order . data ) ;
this . SetNextState ( "CHECKINGWAYPOINT" ) ;
} ,
2016-09-25 14:33:05 -07:00
} ,
2020-12-10 01:18:13 -08:00
"CHECKINGWAYPOINT" : {
"enter" : function ( ) {
this . StartTimer ( 0 , 1000 ) ;
this . stopSurveying = 0 ;
// TODO: pick a proper animation
return false ;
} ,
"leave" : function ( ) {
this . StopTimer ( ) ;
delete this . stopSurveying ;
} ,
"Timer" : function ( msg ) {
if ( this . stopSurveying >= + this . template . PatrolWaitTime )
{
this . FinishOrder ( ) ;
return ;
}
2022-03-31 22:46:13 -07:00
if ( this . FindWalkAndFightTargets ( ) )
this . SetNextState ( "MEMBER" ) ;
else
2021-06-17 00:40:24 -07:00
++ this . stopSurveying ;
2020-12-10 01:18:13 -08:00
}
}
2016-09-25 14:33:05 -07:00
} ,
2020-12-10 01:18:13 -08:00
"GARRISON" : {
2013-11-08 15:22:59 -08:00
"APPROACHING" : {
2019-05-28 04:38:18 -07:00
"enter" : function ( ) {
2021-03-30 04:17:46 -07:00
if ( ! this . MoveToTargetRange ( this . order . data . target , this . order . data . garrison ? IID _Garrisonable : IID _Turretable ) )
2019-05-28 04:38:18 -07:00
{
this . FinishOrder ( ) ;
return true ;
}
2019-12-20 12:05:19 -08:00
2021-03-26 03:18:30 -07:00
let cmpFormation = Engine . QueryInterface ( this . entity , IID _Formation ) ;
cmpFormation . SetRearrange ( true ) ;
cmpFormation . MoveMembersIntoFormation ( true , true ) ;
// If the holder should pickup, warn it so it can take needed action.
let cmpHolder = Engine . QueryInterface ( this . order . data . target , this . order . data . garrison ? IID _GarrisonHolder : IID _TurretHolder ) ;
if ( cmpHolder && cmpHolder . CanPickup ( this . entity ) )
2019-12-20 12:05:19 -08:00
{
this . pickup = this . order . data . target ; // temporary, deleted in "leave"
2021-03-30 04:24:08 -07:00
Engine . PostMessage ( this . pickup , MT _PickupRequested , { "entity" : this . entity , "iid" : this . order . data . garrison ? IID _GarrisonHolder : IID _TurretHolder } ) ;
2019-12-20 12:05:19 -08:00
}
2019-12-28 05:43:18 -08:00
return false ;
2019-05-28 04:38:18 -07:00
} ,
"leave" : function ( ) {
this . StopMoving ( ) ;
2019-12-20 12:05:19 -08:00
if ( this . pickup )
{
Engine . PostMessage ( this . pickup , MT _PickupCanceled , { "entity" : this . entity } ) ;
delete this . pickup ;
}
2019-05-28 04:38:18 -07:00
} ,
2019-06-09 04:16:40 -07:00
"MovementUpdate" : function ( msg ) {
2019-07-22 11:07:24 -07:00
if ( msg . likelyFailure || msg . likelySuccess )
this . SetNextState ( "GARRISONING" ) ;
2013-11-08 15:22:59 -08:00
} ,
} ,
"GARRISONING" : {
"enter" : function ( ) {
2021-03-26 03:18:30 -07:00
this . CallMemberFunction ( this . order . data . garrison ? "Garrison" : "OccupyTurret" , [ this . order . data . target , false ] ) ;
2021-02-21 01:18:05 -08:00
// We might have been disbanded due to the lack of members.
if ( Engine . QueryInterface ( this . entity , IID _Formation ) . GetMemberCount ( ) )
this . SetNextState ( "MEMBER" ) ;
2019-12-28 05:43:18 -08:00
return true ;
2013-11-08 15:22:59 -08:00
} ,
} ,
} ,
2012-12-01 17:52:27 -08:00
"FORMING" : {
2019-05-28 04:38:18 -07:00
"enter" : function ( ) {
2021-02-28 02:11:30 -08:00
let cmpFormation = Engine . QueryInterface ( this . entity , IID _Formation ) ;
cmpFormation . SetRearrange ( true ) ;
cmpFormation . MoveMembersIntoFormation ( true , true ) ;
2019-05-28 04:38:18 -07:00
if ( ! this . MoveTo ( this . order . data ) )
{
this . FinishOrder ( ) ;
return true ;
}
2019-12-28 05:43:18 -08:00
return false ;
2019-05-28 04:38:18 -07:00
} ,
"leave" : function ( ) {
this . StopMoving ( ) ;
} ,
2019-06-09 04:16:40 -07:00
"MovementUpdate" : function ( msg ) {
2019-07-22 11:07:24 -07:00
if ( ! msg . likelyFailure && ! this . CheckRange ( this . order . data ) )
2019-06-09 04:16:40 -07:00
return ;
2012-05-18 14:31:57 -07:00
2020-06-06 09:07:01 -07:00
this . FinishOrder ( ) ;
2012-12-01 17:52:27 -08:00
}
} ,
2014-04-28 06:05:47 -07:00
"COMBAT" : {
"APPROACHING" : {
2019-05-28 04:38:18 -07:00
"enter" : function ( ) {
2021-02-28 02:11:30 -08:00
let cmpFormation = Engine . QueryInterface ( this . entity , IID _Formation ) ;
cmpFormation . SetRearrange ( true ) ;
cmpFormation . MoveMembersIntoFormation ( true , true , "combat" ) ;
2020-04-07 10:35:25 -07:00
if ( ! this . MoveFormationToTargetAttackRange ( this . order . data . target ) )
2019-05-28 04:38:18 -07:00
{
this . FinishOrder ( ) ;
return true ;
}
2019-12-28 05:43:18 -08:00
return false ;
2019-05-28 04:38:18 -07:00
} ,
"leave" : function ( ) {
this . StopMoving ( ) ;
} ,
2019-06-09 04:16:40 -07:00
"MovementUpdate" : function ( msg ) {
2020-04-12 03:37:41 -07:00
let target = this . order . data . target ;
let cmpTargetUnitAI = Engine . QueryInterface ( target , IID _UnitAI ) ;
if ( cmpTargetUnitAI && cmpTargetUnitAI . IsFormationMember ( ) )
target = cmpTargetUnitAI . GetFormationController ( ) ;
2019-06-09 04:16:40 -07:00
let cmpAttack = Engine . QueryInterface ( this . entity , IID _Attack ) ;
2020-04-12 03:37:41 -07:00
this . CallMemberFunction ( "Attack" , [ target , this . order . data . allowCapture , false ] ) ;
2014-04-28 06:05:47 -07:00
if ( cmpAttack . CanAttackAsFormation ( ) )
this . SetNextState ( "COMBAT.ATTACKING" ) ;
else
this . SetNextState ( "MEMBER" ) ;
} ,
} ,
"ATTACKING" : {
// Wait for individual members to finish
"enter" : function ( msg ) {
2023-01-09 06:29:06 -08:00
const target = this . order . data . target ;
2020-04-07 10:35:25 -07:00
if ( ! this . CheckFormationTargetAttackRange ( target ) )
2014-01-22 05:20:55 -08:00
{
2020-12-05 23:22:51 -08:00
if ( this . CanAttack ( target ) && this . CheckTargetVisible ( target ) )
2014-04-28 06:05:47 -07:00
{
2020-09-10 00:51:44 -07:00
this . SetNextState ( "COMBAT.APPROACHING" ) ;
2015-10-05 13:40:14 -07:00
return true ;
2014-04-28 06:05:47 -07:00
}
this . FinishOrder ( ) ;
2015-10-05 13:40:14 -07:00
return true ;
2014-01-22 05:20:55 -08:00
}
2020-04-07 10:35:25 -07:00
let cmpFormation = Engine . QueryInterface ( this . entity , IID _Formation ) ;
2015-10-05 13:40:14 -07:00
// TODO fix the rearranging while attacking as formation
cmpFormation . SetRearrange ( ! this . IsAttackingAsFormation ( ) ) ;
2020-01-09 12:53:13 -08:00
cmpFormation . MoveMembersIntoFormation ( false , false , "combat" ) ;
2015-10-05 13:40:14 -07:00
this . StartTimer ( 200 , 200 ) ;
return false ;
2014-04-28 06:05:47 -07:00
} ,
2014-01-22 05:20:55 -08:00
2014-04-28 06:05:47 -07:00
"Timer" : function ( msg ) {
2023-01-09 06:29:06 -08:00
const target = this . order . data . target ;
2020-04-07 10:35:25 -07:00
if ( ! this . CheckFormationTargetAttackRange ( target ) )
2014-01-22 05:20:55 -08:00
{
2020-12-05 23:22:51 -08:00
if ( this . CanAttack ( target ) && this . CheckTargetVisible ( target ) )
2014-04-28 06:05:47 -07:00
{
2020-09-10 00:51:44 -07:00
this . SetNextState ( "COMBAT.APPROACHING" ) ;
2014-04-28 06:05:47 -07:00
return ;
}
this . FinishOrder ( ) ;
2014-01-22 05:20:55 -08:00
return ;
}
2014-04-28 06:05:47 -07:00
} ,
2014-01-22 05:20:55 -08:00
2014-04-28 06:05:47 -07:00
"leave" : function ( msg ) {
this . StopTimer ( ) ;
2015-10-05 13:40:14 -07:00
var cmpFormation = Engine . QueryInterface ( this . entity , IID _Formation ) ;
if ( cmpFormation )
cmpFormation . SetRearrange ( true ) ;
2014-04-28 06:05:47 -07:00
} ,
2014-01-22 05:20:55 -08:00
} ,
} ,
2020-07-07 03:24:58 -07:00
// Wait for individual members to finish
2012-12-01 17:52:27 -08:00
"MEMBER" : {
2020-07-07 03:24:58 -07:00
"OrderTargetRenamed" : function ( msg ) {
// In general, don't react - we don't want to send spurious messages to members.
2021-01-05 02:12:47 -08:00
// This looks odd for hunting however because we wait for all
// entities to have clumped around the dead resource before proceeding
2020-07-07 03:24:58 -07:00
// so explicitly handle this case.
if ( this . order && this . order . data && this . order . data . hunting &&
this . order . data . target == msg . data . newentity &&
this . orderQueue . length > 1 )
this . FinishOrder ( ) ;
} ,
2012-12-01 17:52:27 -08:00
"enter" : function ( msg ) {
2020-07-07 03:24:58 -07:00
// Don't rearrange the formation, as that forces all units to stop
// what they're doing.
let cmpFormation = Engine . QueryInterface ( this . entity , IID _Formation ) ;
if ( cmpFormation )
cmpFormation . SetRearrange ( false ) ;
// While waiting on members, the formation is more like
// a group of unit and does not have a well-defined position,
// so move the controller out of the world to enforce that.
let cmpPosition = Engine . QueryInterface ( this . entity , IID _Position ) ;
2020-08-06 01:40:14 -07:00
if ( cmpPosition && cmpPosition . IsInWorld ( ) )
2020-07-07 03:24:58 -07:00
cmpPosition . MoveOutOfWorld ( ) ;
2012-12-01 17:52:27 -08:00
this . StartTimer ( 1000 , 1000 ) ;
2019-12-28 05:43:18 -08:00
return false ;
2012-12-01 17:52:27 -08:00
} ,
"Timer" : function ( msg ) {
2020-06-06 09:07:01 -07:00
let cmpFormation = Engine . QueryInterface ( this . entity , IID _Formation ) ;
2021-10-02 22:59:54 -07:00
if ( cmpFormation && ! cmpFormation . AreAllMembersFinished ( ) )
2012-12-01 17:52:27 -08:00
return ;
2022-03-31 22:46:13 -07:00
if ( this . order ? . data ? . returningState )
this . SetNextState ( this . order . data . returningState ) ;
else
this . FinishOrder ( ) ;
2012-05-18 14:31:57 -07:00
} ,
2012-12-01 17:52:27 -08:00
"leave" : function ( msg ) {
this . StopTimer ( ) ;
2020-07-07 03:24:58 -07:00
// Reform entirely as members might be all over the place now.
2018-04-11 17:27:03 -07:00
let cmpFormation = Engine . QueryInterface ( this . entity , IID _Formation ) ;
2021-10-02 22:59:54 -07:00
if ( cmpFormation && ( cmpFormation . AreAllMembersIdle ( ) || this . orderQueue . length ) )
2020-07-07 03:24:58 -07:00
cmpFormation . MoveMembersIntoFormation ( true ) ;
// Update the held position so entities respond to orders.
let cmpPosition = Engine . QueryInterface ( this . entity , IID _Position ) ;
if ( cmpPosition && cmpPosition . IsInWorld ( ) )
{
let pos = cmpPosition . GetPosition2D ( ) ;
this . CallMemberFunction ( "SetHeldPosition" , [ pos . x , pos . y ] ) ;
}
2012-12-01 17:52:27 -08:00
} ,
2012-05-18 14:31:57 -07:00
} ,
2010-09-03 02:55:14 -07:00
} ,
2010-07-21 09:09:58 -07:00
2010-09-03 02:55:14 -07:00
// States for entities moving as part of a formation:
"FORMATIONMEMBER" : {
"FormationLeave" : function ( msg ) {
2010-11-30 04:31:54 -08:00
// Stop moving as soon as the formation disbands
2020-07-19 03:49:18 -07:00
// Keep current rotation
let facePointAfterMove = this . GetFacePointAfterMove ( ) ;
this . SetFacePointAfterMove ( false ) ;
2010-11-30 04:31:54 -08:00
this . StopMoving ( ) ;
2020-07-19 03:49:18 -07:00
this . SetFacePointAfterMove ( facePointAfterMove ) ;
2010-11-30 04:31:54 -08:00
2012-08-29 12:54:29 -07:00
// If the controller handled an order but some members rejected it,
// they will have no orders and be in the FORMATIONMEMBER.IDLE state.
if ( this . orderQueue . length )
{
// We're leaving the formation, so stop our FormationWalk order
if ( this . FinishOrder ( ) )
return ;
}
2010-11-22 12:12:04 -08:00
2020-01-09 12:53:13 -08:00
this . formationAnimationVariant = undefined ;
2019-07-09 12:58:44 -07:00
this . SetNextState ( "INDIVIDUAL.IDLE" ) ;
2010-07-21 09:09:58 -07:00
} ,
2011-05-13 13:32:41 -07:00
// Override the LeaveFoundation order since we're not doing
// anything more important (and we might be stuck in the WALKING
// state forever and need to get out of foundations in that case)
"Order.LeaveFoundation" : function ( msg ) {
2019-12-28 03:40:24 -08:00
if ( ! this . WillMoveFromFoundation ( msg . data . target ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2021-03-03 00:21:00 -08:00
msg . data . min = g _LeaveFoundationRange ;
2019-12-28 03:40:24 -08:00
this . SetNextState ( "WALKINGTOPOINT" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2011-05-13 13:32:41 -07:00
} ,
2019-07-08 11:23:44 -07:00
"enter" : function ( ) {
let cmpFormation = Engine . QueryInterface ( this . formationController , IID _Formation ) ;
if ( cmpFormation )
2020-01-09 12:53:13 -08:00
{
2020-05-23 06:03:34 -07:00
this . formationAnimationVariant = cmpFormation . GetFormationAnimationVariant ( this . entity ) ;
2020-01-09 12:53:13 -08:00
if ( this . formationAnimationVariant )
this . SetAnimationVariant ( this . formationAnimationVariant ) ;
else
this . SetDefaultAnimationVariant ( ) ;
}
2019-12-28 05:43:18 -08:00
return false ;
2019-07-08 11:23:44 -07:00
} ,
"leave" : function ( ) {
this . SetDefaultAnimationVariant ( ) ;
2020-01-10 02:03:15 -08:00
this . formationAnimationVariant = undefined ;
2019-07-08 11:23:44 -07:00
} ,
2012-08-18 01:13:47 -07:00
2019-07-18 12:59:37 -07:00
"IDLE" : "INDIVIDUAL.IDLE" ,
2010-07-29 13:39:23 -07:00
2019-12-14 07:47:26 -08:00
"CHEERING" : "INDIVIDUAL.CHEERING" ,
2010-09-03 02:55:14 -07:00
"WALKING" : {
2018-03-10 11:12:23 -08:00
"enter" : function ( ) {
2019-07-08 11:23:44 -07:00
let cmpUnitMotion = Engine . QueryInterface ( this . entity , IID _UnitMotion ) ;
2019-05-28 04:38:18 -07:00
cmpUnitMotion . MoveToFormationOffset ( this . order . data . target , this . order . data . x , this . order . data . z ) ;
2019-12-14 02:10:26 -08:00
if ( this . order . data . offsetsChanged )
{
let cmpFormation = Engine . QueryInterface ( this . formationController , IID _Formation ) ;
if ( cmpFormation )
2020-05-23 06:03:34 -07:00
this . formationAnimationVariant = cmpFormation . GetFormationAnimationVariant ( this . entity ) ;
2019-12-14 02:10:26 -08:00
}
2020-01-09 12:53:13 -08:00
if ( this . formationAnimationVariant )
this . SetAnimationVariant ( this . formationAnimationVariant ) ;
else if ( this . order . data . variant )
this . SetAnimationVariant ( this . order . data . variant ) ;
else
this . SetDefaultAnimationVariant ( ) ;
2025-01-01 02:33:07 -08:00
if ( cmpUnitMotion . PossiblyAtDestination ( ) )
{
this . FinishOrder ( ) ;
return true ;
}
2019-12-28 05:43:18 -08:00
return false ;
2010-09-03 02:55:14 -07:00
} ,
2011-05-13 13:32:41 -07:00
2019-07-09 12:58:44 -07:00
"leave" : function ( ) {
2020-07-07 03:24:58 -07:00
// Don't use the logic from unitMotion, as SetInPosition
// has already given us a custom rotation
// (or we failed to move and thus don't care.)
2020-07-19 03:42:45 -07:00
let facePointAfterMove = this . GetFacePointAfterMove ( ) ;
2020-07-07 03:24:58 -07:00
this . SetFacePointAfterMove ( false ) ;
2019-07-09 12:58:44 -07:00
this . StopMoving ( ) ;
2020-07-19 03:42:45 -07:00
this . SetFacePointAfterMove ( facePointAfterMove ) ;
2019-07-09 12:58:44 -07:00
} ,
2012-08-31 01:20:36 -07:00
// Occurs when the unit has reached its destination and the controller
2014-01-08 01:47:27 -08:00
// is done moving. The controller is notified.
2019-06-09 04:16:40 -07:00
"MovementUpdate" : function ( msg ) {
2020-07-22 02:31:08 -07:00
// When walking in formation, we'll only get notified in case of failure
// if the formation controller has stopped walking.
// Formations can start lagging a lot if many entities request short path
// so prefer to finish order early than retry pathing.
// (see https://code.wildfiregames.com/rP23806)
// (if the message is likelyFailure of likelySuccess, we also want to stop).
2020-07-07 03:24:58 -07:00
this . FinishOrder ( ) ;
2012-08-31 01:20:36 -07:00
} ,
2010-09-03 02:55:14 -07:00
} ,
2012-09-04 16:27:06 -07:00
// Special case used by Order.LeaveFoundation
"WALKINGTOPOINT" : {
"enter" : function ( ) {
2019-05-28 04:38:18 -07:00
if ( ! this . MoveTo ( this . order . data ) )
{
this . FinishOrder ( ) ;
return true ;
}
2019-12-28 05:43:18 -08:00
return false ;
2012-09-04 16:27:06 -07:00
} ,
2021-02-27 21:40:08 -08:00
"leave" : function ( ) {
this . StopMoving ( ) ;
} ,
2019-06-09 04:16:40 -07:00
"MovementUpdate" : function ( ) {
if ( ! this . CheckRange ( this . order . data ) )
return ;
2012-09-04 16:27:06 -07:00
this . FinishOrder ( ) ;
} ,
} ,
2010-09-03 02:55:14 -07:00
} ,
// States for entities not part of a formation:
"INDIVIDUAL" : {
2010-07-21 09:09:58 -07:00
"Attacked" : function ( msg ) {
2017-12-03 02:10:13 -08:00
if ( this . GetStance ( ) . targetAttackersAlways || ! this . order || ! this . order . data || ! this . order . data . force )
2011-03-04 06:36:41 -08:00
this . RespondToTargetedEntities ( [ msg . data . attacker ] ) ;
2010-07-21 09:09:58 -07:00
} ,
2013-11-30 09:30:08 -08:00
"GuardedAttacked" : function ( msg ) {
// do nothing if we have a forced order in queue before the guard order
for ( var i = 0 ; i < this . orderQueue . length ; ++ i )
{
if ( this . orderQueue [ i ] . type == "Guard" )
break ;
if ( this . orderQueue [ i ] . data && this . orderQueue [ i ] . data . force )
return ;
}
// if we already are targeting another unit still alive, finish with it first
2013-12-09 10:15:24 -08:00
if ( this . order && ( this . order . type == "WalkAndFight" || this . order . type == "Attack" ) )
2020-12-05 23:22:51 -08:00
if ( this . order . data . target != msg . data . attacker && this . CanAttack ( msg . data . attacker ) )
2013-11-30 09:30:08 -08:00
return ;
var cmpIdentity = Engine . QueryInterface ( this . entity , IID _Identity ) ;
var cmpHealth = Engine . QueryInterface ( this . isGuardOf , IID _Health ) ;
if ( cmpIdentity && cmpIdentity . HasClass ( "Support" ) &&
2019-05-25 06:11:46 -07:00
cmpHealth && cmpHealth . IsInjured ( ) )
2013-11-30 09:30:08 -08:00
{
if ( this . CanHeal ( this . isGuardOf ) )
this . PushOrderFront ( "Heal" , { "target" : this . isGuardOf , "force" : false } ) ;
2016-01-21 12:49:57 -08:00
else if ( this . CanRepair ( this . isGuardOf ) )
2013-11-30 09:30:08 -08:00
this . PushOrderFront ( "Repair" , { "target" : this . isGuardOf , "autocontinue" : false , "force" : false } ) ;
return ;
}
2013-12-09 10:15:24 -08:00
var cmpBuildingAI = Engine . QueryInterface ( msg . data . attacker , IID _BuildingAI ) ;
2016-01-21 12:49:57 -08:00
if ( cmpBuildingAI && this . CanRepair ( this . isGuardOf ) )
2013-12-09 10:15:24 -08:00
{
this . PushOrderFront ( "Repair" , { "target" : this . isGuardOf , "autocontinue" : false , "force" : false } ) ;
2013-11-30 09:30:08 -08:00
return ;
2013-12-09 10:15:24 -08:00
}
if ( this . CheckTargetVisible ( msg . data . attacker ) )
2023-01-09 06:29:06 -08:00
this . PushOrderFront ( "Attack" , { "target" : msg . data . attacker , "force" : false } ) ;
2013-12-09 10:15:24 -08:00
else
{
var cmpPosition = Engine . QueryInterface ( msg . data . attacker , IID _Position ) ;
if ( ! cmpPosition || ! cmpPosition . IsInWorld ( ) )
return ;
var pos = cmpPosition . GetPosition ( ) ;
this . PushOrderFront ( "WalkAndFight" , { "x" : pos . x , "z" : pos . z , "target" : msg . data . attacker , "force" : false } ) ;
// if we already had a WalkAndFight, keep only the most recent one in case the target has moved
if ( this . orderQueue [ 1 ] && this . orderQueue [ 1 ] . type == "WalkAndFight" )
2015-12-20 13:07:47 -08:00
{
2013-12-09 10:15:24 -08:00
this . orderQueue . splice ( 1 , 1 ) ;
2015-12-20 13:07:47 -08:00
Engine . PostMessage ( this . entity , MT _UnitAIOrderDataChanged , { "to" : this . GetOrderData ( ) } ) ;
}
2013-12-09 10:15:24 -08:00
}
2013-11-30 09:30:08 -08:00
} ,
2010-07-21 09:09:58 -07:00
"IDLE" : {
2020-09-10 09:37:14 -07:00
"Order.Cheer" : function ( ) {
2021-01-23 07:07:15 -08:00
// Do not cheer if there is no cheering time and we are not idle yet.
if ( ! this . cheeringTime || ! this . isIdle )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2020-11-16 06:47:41 -08:00
2020-09-10 09:37:14 -07:00
this . SetNextState ( "CHEERING" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2020-09-10 09:37:14 -07:00
} ,
2025-01-01 02:33:07 -08:00
"ReenterIdle" : function ( ) {
// Reset us to the idle state for the GUI.
if ( this . IsFormationMember ( ) )
{
let cmpFormationAI = Engine . QueryInterface ( this . formationController , IID _UnitAI ) ;
if ( ! cmpFormationAI || ! cmpFormationAI . IsIdle ( ) )
return ;
Engine . QueryInterface ( this . formationController , IID _Formation ) . SetIdleEntity ( this . entity ) ;
}
this . isIdle = true ;
Engine . PostMessage ( this . entity , MT _UnitIdleChanged , { "idle" : this . isIdle } ) ;
// TODO: figure out if we can move more things from Timer here/call timer directly
// without triggering infinite loops.
} ,
2010-07-21 09:09:58 -07:00
"enter" : function ( ) {
2010-11-22 12:12:04 -08:00
// Switch back to idle animation to guarantee we won't
// get stuck with an incorrect animation
2019-07-14 04:40:27 -07:00
this . SelectAnimation ( "idle" ) ;
2015-11-28 03:09:29 -08:00
2019-07-14 04:40:27 -07:00
// 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
// 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.
// This wastes a turn but avoids infinite loops.
// Further, the GUI and AI want to know when a unit is idle,
// but sending this info in Idle.enter will send spurious messages.
// Pick 100 to execute on the next turn in SP and MP.
this . StartTimer ( 100 ) ;
2019-12-28 05:43:18 -08:00
return false ;
2010-07-21 09:09:58 -07:00
} ,
2010-07-29 13:39:23 -07:00
"leave" : function ( ) {
2020-07-23 23:07:27 -07:00
let cmpRangeManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _RangeManager ) ;
2013-06-26 11:41:06 -07:00
if ( this . losRangeQuery )
2015-08-29 15:49:44 -07:00
cmpRangeManager . DisableActiveQuery ( this . losRangeQuery ) ;
2012-04-17 13:22:13 -07:00
if ( this . losHealRangeQuery )
2015-08-29 15:49:44 -07:00
cmpRangeManager . DisableActiveQuery ( this . losHealRangeQuery ) ;
2020-07-23 23:07:27 -07:00
if ( this . losAttackRangeQuery )
cmpRangeManager . DisableActiveQuery ( this . losAttackRangeQuery ) ;
2011-02-05 12:35:34 -08:00
this . StopTimer ( ) ;
if ( this . isIdle )
{
2021-10-02 22:59:54 -07:00
if ( this . IsFormationMember ( ) )
Engine . QueryInterface ( this . formationController , IID _Formation ) . UnsetIdleEntity ( this . entity ) ;
2011-02-05 12:35:34 -08:00
this . isIdle = false ;
Engine . PostMessage ( this . entity , MT _UnitIdleChanged , { "idle" : this . isIdle } ) ;
}
2010-07-29 13:39:23 -07:00
} ,
2021-02-01 11:58:09 -08:00
"Attacked" : function ( msg ) {
if ( this . isIdle && ( this . GetStance ( ) . targetAttackersAlways || ! this . order || ! this . order . data || ! this . order . data . force ) )
this . RespondToTargetedEntities ( [ msg . data . attacker ] ) ;
} ,
2020-07-23 23:07:27 -07:00
// On the range updates:
// We check for idleness to prevent an entity to react only to newly seen entities
// when receiving a Los*RangeUpdate on the same turn as the entity becomes idle
// since this.FindNew*Targets is called in the timer.
2010-07-29 13:39:23 -07:00
"LosRangeUpdate" : function ( msg ) {
2020-07-23 23:07:27 -07:00
if ( this . isIdle && msg && msg . data && msg . data . added && msg . data . added . length )
this . RespondToSightedEntities ( msg . data . added ) ;
2010-07-29 13:39:23 -07:00
} ,
2012-05-01 15:20:08 -07:00
2012-04-17 13:22:13 -07:00
"LosHealRangeUpdate" : function ( msg ) {
2020-07-23 23:07:27 -07:00
if ( this . isIdle && msg && msg . data && msg . data . added && msg . data . added . length )
2019-12-07 05:01:42 -08:00
this . RespondToHealableEntities ( msg . data . added ) ;
2012-04-17 13:22:13 -07:00
} ,
2011-02-05 12:35:34 -08:00
2020-07-23 23:07:27 -07:00
"LosAttackRangeUpdate" : function ( msg ) {
if ( this . isIdle && msg && msg . data && msg . data . added && msg . data . added . length && this . GetStance ( ) . targetVisibleEnemies )
this . AttackEntitiesByPreference ( msg . data . added ) ;
} ,
2011-02-05 12:35:34 -08:00
"Timer" : function ( msg ) {
2019-07-14 04:40:27 -07:00
if ( this . isGuardOf )
{
this . Guard ( this . isGuardOf , false ) ;
return ;
}
// If a unit can heal and attack we first want to heal wounded units,
// so check if we are a healer and find whether there's anybody nearby to heal.
// (If anyone approaches later it'll be handled via LosHealRangeUpdate.)
// If anyone in sight gets hurt that will be handled via LosHealRangeUpdate.
if ( this . IsHealer ( ) && this . FindNewHealTargets ( ) )
2019-12-07 05:01:42 -08:00
return ;
2019-07-14 04:40:27 -07:00
// If we entered the idle state we must have nothing better to do,
// so immediately check whether there's anybody nearby to attack.
2020-07-23 23:07:27 -07:00
// (If anyone approaches later, it'll be handled via LosAttackRangeUpdate.)
2019-07-14 04:40:27 -07:00
if ( this . FindNewTargets ( ) )
2019-12-07 05:01:42 -08:00
return ;
2019-07-14 04:40:27 -07:00
2020-07-23 23:07:27 -07:00
if ( this . FindSightedEnemies ( ) )
return ;
2011-02-05 12:35:34 -08:00
if ( ! this . isIdle )
{
2020-07-07 03:24:58 -07:00
// Move back to the held position if we drifted away.
// (only if not a formation member).
if ( ! this . IsFormationMember ( ) &&
this . GetStance ( ) . respondHoldGround && this . heldPosition &&
! this . CheckPointRangeExplicit ( this . heldPosition . x , this . heldPosition . z , 0 , 10 ) &&
this . WalkToHeldPosition ( ) )
return ;
2021-01-13 07:27:18 -08:00
if ( this . IsFormationMember ( ) )
{
let cmpFormationAI = Engine . QueryInterface ( this . formationController , IID _UnitAI ) ;
if ( ! cmpFormationAI || ! cmpFormationAI . IsIdle ( ) )
return ;
2021-10-02 22:59:54 -07:00
Engine . QueryInterface ( this . formationController , IID _Formation ) . SetIdleEntity ( this . entity ) ;
2021-01-13 07:27:18 -08:00
}
2011-02-05 12:35:34 -08:00
this . isIdle = true ;
Engine . PostMessage ( this . entity , MT _UnitIdleChanged , { "idle" : this . isIdle } ) ;
}
2021-02-27 12:13:40 -08:00
// Go linger first to prevent all roaming entities
// to move all at the same time on map init.
if ( this . template . RoamDistance )
this . SetNextState ( "LINGERING" ) ;
} ,
"ROAMING" : {
"enter" : function ( ) {
this . SetFacePointAfterMove ( false ) ;
this . MoveRandomly ( + this . template . RoamDistance ) ;
this . StartTimer ( randIntInclusive ( + this . template . RoamTimeMin , + this . template . RoamTimeMax ) ) ;
return false ;
} ,
"leave" : function ( ) {
this . StopMoving ( ) ;
this . StopTimer ( ) ;
this . SetFacePointAfterMove ( true ) ;
} ,
"Timer" : function ( msg ) {
this . SetNextState ( "LINGERING" ) ;
} ,
"MovementUpdate" : function ( ) {
this . MoveRandomly ( + this . template . RoamDistance ) ;
} ,
} ,
"LINGERING" : {
"enter" : function ( ) {
// ToDo: rename animations?
this . SelectAnimation ( "feeding" ) ;
this . StartTimer ( randIntInclusive ( + this . template . FeedTimeMin , + this . template . FeedTimeMax ) ) ;
return false ;
} ,
"leave" : function ( ) {
this . ResetAnimation ( ) ;
this . StopTimer ( ) ;
} ,
"Timer" : function ( msg ) {
this . SetNextState ( "ROAMING" ) ;
} ,
2011-02-05 12:35:34 -08:00
} ,
2010-07-29 13:39:23 -07:00
} ,
2010-07-21 09:09:58 -07:00
"WALKING" : {
2018-03-10 11:12:23 -08:00
"enter" : function ( ) {
2019-05-28 04:38:18 -07:00
if ( ! this . MoveTo ( this . order . data ) )
{
this . FinishOrder ( ) ;
2019-06-05 10:27:12 -07:00
return true ;
2019-05-28 04:38:18 -07:00
}
2019-12-28 05:43:18 -08:00
return false ;
2010-07-21 09:09:58 -07:00
} ,
2019-07-22 11:07:24 -07:00
"leave" : function ( ) {
2019-05-28 04:38:18 -07:00
this . StopMoving ( ) ;
} ,
2019-07-13 08:53:21 -07:00
"MovementUpdate" : function ( msg ) {
2020-11-04 09:50:20 -08:00
// If it looks like the path is failing, and we are close enough stop anyways.
// This avoids pathing for an unreachable goal and reduces lag considerably.
2019-07-24 12:05:12 -07:00
if ( msg . likelyFailure || msg . obstructed && this . RelaxedMaxRangeCheck ( this . order . data , this . DefaultRelaxedMaxRange ) ||
2019-07-22 11:07:24 -07:00
this . CheckRange ( this . order . data ) )
2019-06-09 04:16:40 -07:00
this . FinishOrder ( ) ;
2010-07-21 09:09:58 -07:00
} ,
} ,
2013-02-25 13:56:24 -08:00
"WALKINGANDFIGHTING" : {
2018-03-10 11:12:23 -08:00
"enter" : function ( ) {
2019-05-28 04:38:18 -07:00
if ( ! this . MoveTo ( this . order . data ) )
{
this . FinishOrder ( ) ;
2019-06-05 10:27:12 -07:00
return true ;
2019-05-28 04:38:18 -07:00
}
2013-11-22 09:43:26 -08:00
// Show weapons rather than carried resources.
2017-12-10 02:06:08 -08:00
this . SetAnimationVariant ( "combat" ) ;
2013-11-22 09:43:26 -08:00
2013-02-25 13:56:24 -08:00
this . StartTimer ( 0 , 1000 ) ;
2019-12-28 05:43:18 -08:00
return false ;
2013-02-25 13:56:24 -08:00
} ,
"Timer" : function ( msg ) {
2013-12-26 08:09:32 -08:00
this . FindWalkAndFightTargets ( ) ;
2013-02-25 13:56:24 -08:00
} ,
"leave" : function ( msg ) {
2019-05-28 04:38:18 -07:00
this . StopMoving ( ) ;
2013-02-25 13:56:24 -08:00
this . StopTimer ( ) ;
2018-02-23 12:20:57 -08:00
this . SetDefaultAnimationVariant ( ) ;
2013-02-25 13:56:24 -08:00
} ,
2019-07-13 08:53:21 -07:00
"MovementUpdate" : function ( msg ) {
2020-11-04 09:50:20 -08:00
// If it looks like the path is failing, and we are close enough stop anyways.
// This avoids pathing for an unreachable goal and reduces lag considerably.
2019-07-24 12:05:12 -07:00
if ( msg . likelyFailure || msg . obstructed && this . RelaxedMaxRangeCheck ( this . order . data , this . DefaultRelaxedMaxRange ) ||
2019-07-22 11:07:24 -07:00
this . CheckRange ( this . order . data ) )
2019-06-09 04:16:40 -07:00
this . FinishOrder ( ) ;
2013-02-25 13:56:24 -08:00
} ,
} ,
2016-09-25 14:33:05 -07:00
"PATROL" : {
2018-03-10 11:12:23 -08:00
"enter" : function ( ) {
2016-09-25 14:33:05 -07:00
let cmpPosition = Engine . QueryInterface ( this . entity , IID _Position ) ;
2020-12-10 01:18:13 -08:00
if ( ! cmpPosition || ! cmpPosition . IsInWorld ( ) )
2016-09-25 14:33:05 -07:00
{
this . FinishOrder ( ) ;
2019-06-05 10:27:12 -07:00
return true ;
2016-09-25 14:33:05 -07:00
}
2019-05-28 04:38:18 -07:00
2020-11-04 09:50:20 -08:00
// Memorize the origin position in case that we want to go back.
2016-09-25 14:33:05 -07:00
if ( ! this . patrolStartPosOrder )
{
this . patrolStartPosOrder = cmpPosition . GetPosition ( ) ;
2016-11-22 04:46:46 -08:00
this . patrolStartPosOrder . targetClasses = this . order . data . targetClasses ;
2017-12-19 11:24:48 -08:00
this . patrolStartPosOrder . allowCapture = this . order . data . allowCapture ;
2016-09-25 14:33:05 -07:00
}
2018-02-23 12:20:57 -08:00
this . SetAnimationVariant ( "combat" ) ;
2020-12-10 01:18:13 -08:00
2019-12-28 05:43:18 -08:00
return false ;
2016-09-25 14:33:05 -07:00
} ,
"leave" : function ( ) {
delete this . patrolStartPosOrder ;
2018-02-23 12:20:57 -08:00
this . SetDefaultAnimationVariant ( ) ;
2016-09-25 14:33:05 -07:00
} ,
2020-12-10 01:18:13 -08:00
"PATROLLING" : {
"enter" : function ( ) {
let cmpPosition = Engine . QueryInterface ( this . entity , IID _Position ) ;
if ( ! cmpPosition || ! cmpPosition . IsInWorld ( ) ||
! this . MoveTo ( this . order . data ) )
{
this . FinishOrder ( ) ;
return true ;
}
this . StartTimer ( 0 , 1000 ) ;
return false ;
} ,
2016-09-25 14:33:05 -07:00
2020-12-10 01:18:13 -08:00
"leave" : function ( ) {
this . StopMoving ( ) ;
this . StopTimer ( ) ;
} ,
2019-06-09 04:16:40 -07:00
2020-12-10 01:18:13 -08:00
"Timer" : function ( msg ) {
this . FindWalkAndFightTargets ( ) ;
} ,
2016-09-25 14:33:05 -07:00
2020-12-10 01:18:13 -08:00
"MovementUpdate" : function ( msg ) {
if ( ! msg . likelyFailure && ! msg . likelySuccess && ! this . RelaxedMaxRangeCheck ( this . order . data , this . DefaultRelaxedMaxRange ) )
return ;
if ( this . orderQueue . length == 1 )
this . PushOrder ( "Patrol" , this . patrolStartPosOrder ) ;
this . PushOrder ( this . order . type , this . order . data ) ;
this . SetNextState ( "CHECKINGWAYPOINT" ) ;
} ,
2016-09-25 14:33:05 -07:00
} ,
2020-12-10 01:18:13 -08:00
"CHECKINGWAYPOINT" : {
"enter" : function ( ) {
this . StartTimer ( 0 , 1000 ) ;
this . stopSurveying = 0 ;
// TODO: pick a proper animation
return false ;
} ,
"leave" : function ( ) {
this . StopTimer ( ) ;
delete this . stopSurveying ;
} ,
"Timer" : function ( msg ) {
if ( this . stopSurveying >= + this . template . PatrolWaitTime )
{
this . FinishOrder ( ) ;
return ;
}
2021-06-17 00:40:24 -07:00
if ( ! this . FindWalkAndFightTargets ( ) )
++ this . stopSurveying ;
2020-12-10 01:18:13 -08:00
}
}
2016-09-25 14:33:05 -07:00
} ,
2013-11-30 09:30:08 -08:00
"GUARD" : {
"RemoveGuard" : function ( ) {
this . FinishOrder ( ) ;
} ,
"ESCORTING" : {
2018-03-10 11:12:23 -08:00
"enter" : function ( ) {
2019-05-28 04:38:18 -07:00
if ( ! this . MoveToTargetRangeExplicit ( this . isGuardOf , 0 , this . guardRange ) )
{
this . FinishOrder ( ) ;
2019-06-05 10:27:12 -07:00
return true ;
2019-05-28 04:38:18 -07:00
}
2013-11-30 09:30:08 -08:00
// Show weapons rather than carried resources.
2017-12-10 02:06:08 -08:00
this . SetAnimationVariant ( "combat" ) ;
2013-11-30 09:30:08 -08:00
this . StartTimer ( 0 , 1000 ) ;
this . SetHeldPositionOnEntity ( this . isGuardOf ) ;
return false ;
} ,
"Timer" : function ( msg ) {
2020-12-05 23:22:51 -08:00
if ( ! this . ShouldGuard ( this . isGuardOf ) )
2013-11-30 09:30:08 -08:00
{
this . FinishOrder ( ) ;
return ;
}
2019-06-06 12:37:23 -07:00
let cmpObstructionManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _ObstructionManager ) ;
2019-06-30 12:05:04 -07:00
if ( cmpObstructionManager . IsInTargetRange ( this . entity , this . isGuardOf , 0 , 3 * this . guardRange , false ) )
2020-05-20 12:39:36 -07:00
this . TryMatchTargetSpeed ( this . isGuardOf , false ) ;
2019-06-08 05:53:28 -07:00
this . SetHeldPositionOnEntity ( this . isGuardOf ) ;
} ,
"leave" : function ( msg ) {
this . StopMoving ( ) ;
this . StopTimer ( ) ;
this . SetDefaultAnimationVariant ( ) ;
2013-11-30 09:30:08 -08:00
} ,
2019-07-01 12:51:21 -07:00
"MovementUpdate" : function ( msg ) {
2019-07-22 11:07:24 -07:00
if ( msg . likelyFailure || this . CheckTargetRangeExplicit ( this . isGuardOf , 0 , this . guardRange ) )
2013-11-30 09:30:08 -08:00
this . SetNextState ( "GUARDING" ) ;
} ,
} ,
"GUARDING" : {
2018-03-10 11:12:23 -08:00
"enter" : function ( ) {
2013-11-30 09:30:08 -08:00
this . StartTimer ( 1000 , 1000 ) ;
this . SetHeldPositionOnEntity ( this . entity ) ;
2018-02-23 12:20:57 -08:00
this . SetAnimationVariant ( "combat" ) ;
2019-07-18 12:56:09 -07:00
this . FaceTowardsTarget ( this . order . data . target ) ;
2013-11-30 09:30:08 -08:00
return false ;
} ,
2020-07-23 23:07:27 -07:00
"LosAttackRangeUpdate" : function ( msg ) {
2013-11-30 09:30:08 -08:00
if ( this . GetStance ( ) . targetVisibleEnemies )
this . AttackEntitiesByPreference ( msg . data . added ) ;
} ,
"Timer" : function ( msg ) {
2020-12-05 23:22:51 -08:00
if ( ! this . ShouldGuard ( this . isGuardOf ) )
2013-11-30 09:30:08 -08:00
{
this . FinishOrder ( ) ;
return ;
}
2019-05-28 04:38:18 -07:00
// TODO: find out what to do if we cannot move.
if ( ! this . CheckTargetRangeExplicit ( this . isGuardOf , 0 , this . guardRange ) &&
this . MoveToTargetRangeExplicit ( this . isGuardOf , 0 , this . guardRange ) )
2013-11-30 09:30:08 -08:00
this . SetNextState ( "ESCORTING" ) ;
else
{
2019-07-18 12:56:09 -07:00
this . FaceTowardsTarget ( this . order . data . target ) ;
2013-11-30 09:30:08 -08:00
var cmpHealth = Engine . QueryInterface ( this . isGuardOf , IID _Health ) ;
2019-05-25 06:11:46 -07:00
if ( cmpHealth && cmpHealth . IsInjured ( ) )
2013-11-30 09:30:08 -08:00
{
if ( this . CanHeal ( this . isGuardOf ) )
this . PushOrderFront ( "Heal" , { "target" : this . isGuardOf , "force" : false } ) ;
2016-01-21 12:49:57 -08:00
else if ( this . CanRepair ( this . isGuardOf ) )
2013-11-30 09:30:08 -08:00
this . PushOrderFront ( "Repair" , { "target" : this . isGuardOf , "autocontinue" : false , "force" : false } ) ;
}
}
} ,
"leave" : function ( msg ) {
this . StopTimer ( ) ;
2018-02-23 12:20:57 -08:00
this . SetDefaultAnimationVariant ( ) ;
2013-11-30 09:30:08 -08:00
} ,
} ,
} ,
2011-03-04 06:36:41 -08:00
"FLEEING" : {
"enter" : function ( ) {
2019-05-28 04:38:18 -07:00
// We use the distance between the entities to account for ranged attacks
2020-11-04 10:56:45 -08:00
this . order . data . distanceToFlee = PositionHelper . DistanceBetweenEntities ( this . entity , this . order . data . target ) + ( + this . template . FleeDistance ) ;
2019-05-28 04:38:18 -07:00
let cmpUnitMotion = Engine . QueryInterface ( this . entity , IID _UnitMotion ) ;
// Use unit motion directly to ignore the visibility check. TODO: change this if we add LOS to fauna.
2019-06-11 13:06:25 -07:00
if ( this . CheckTargetRangeExplicit ( this . order . data . target , this . order . data . distanceToFlee , - 1 ) ||
! cmpUnitMotion || ! cmpUnitMotion . MoveToTargetRange ( this . order . data . target , this . order . data . distanceToFlee , - 1 ) )
2019-05-28 04:38:18 -07:00
{
this . FinishOrder ( ) ;
2019-06-05 10:27:12 -07:00
return true ;
2019-05-28 04:38:18 -07:00
}
2011-03-04 06:36:41 -08:00
this . PlaySound ( "panic" ) ;
2023-06-14 23:56:41 -07:00
this . Run ( ) ;
2019-12-28 05:43:18 -08:00
return false ;
2011-03-04 06:36:41 -08:00
} ,
2020-06-05 23:19:15 -07:00
"OrderTargetRenamed" : function ( msg ) {
// To avoid replaying the panic sound, handle this explicitly.
let cmpUnitMotion = Engine . QueryInterface ( this . entity , IID _UnitMotion ) ;
if ( this . CheckTargetRangeExplicit ( this . order . data . target , this . order . data . distanceToFlee , - 1 ) ||
! cmpUnitMotion || ! cmpUnitMotion . MoveToTargetRange ( this . order . data . target , this . order . data . distanceToFlee , - 1 ) )
this . FinishOrder ( ) ;
} ,
2020-12-26 23:21:18 -08:00
"Attacked" : function ( msg ) {
if ( msg . data . attacker == this . order . data . target )
return ;
let cmpObstructionManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _ObstructionManager ) ;
if ( cmpObstructionManager . DistanceToTarget ( this . entity , msg . data . target ) > cmpObstructionManager . DistanceToTarget ( this . entity , this . order . data . target ) )
return ;
if ( this . GetStance ( ) . targetAttackersAlways || ! this . order || ! this . order . data || ! this . order . data . force )
this . RespondToTargetedEntities ( [ msg . data . attacker ] ) ;
} ,
2011-03-04 06:36:41 -08:00
"leave" : function ( ) {
2019-05-28 04:38:18 -07:00
this . StopMoving ( ) ;
2011-03-04 06:36:41 -08:00
} ,
2019-07-01 12:51:21 -07:00
"MovementUpdate" : function ( msg ) {
2019-07-22 11:07:24 -07:00
if ( msg . likelyFailure || this . CheckTargetRangeExplicit ( this . order . data . target , this . order . data . distanceToFlee , - 1 ) )
2019-06-09 04:16:40 -07:00
this . FinishOrder ( ) ;
2011-03-04 06:36:41 -08:00
} ,
} ,
2010-07-21 09:09:58 -07:00
"COMBAT" : {
2012-08-18 01:13:47 -07:00
"Order.LeaveFoundation" : function ( msg ) {
// Ignore the order as we're busy.
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2012-08-18 01:13:47 -07:00
} ,
2010-07-21 09:09:58 -07:00
"Attacked" : function ( msg ) {
2017-12-03 02:10:13 -08:00
// If we're already in combat mode, ignore anyone else who's attacking us
// unless it's a melee attack since they may be blocking our way to the target
if ( msg . data . type == "Melee" && ( this . GetStance ( ) . targetAttackersAlways || ! this . order . data . force ) )
this . RespondToTargetedEntities ( [ msg . data . attacker ] ) ;
2010-07-21 09:09:58 -07:00
} ,
2020-01-10 02:03:15 -08:00
"leave" : function ( ) {
if ( ! this . formationAnimationVariant )
this . SetDefaultAnimationVariant ( ) ;
} ,
2010-07-21 09:09:58 -07:00
"APPROACHING" : {
2018-03-10 11:12:23 -08:00
"enter" : function ( ) {
2019-05-28 04:38:18 -07:00
if ( ! this . MoveToTargetAttackRange ( this . order . data . target , this . order . data . attackType ) )
{
this . FinishOrder ( ) ;
2019-06-05 10:27:12 -07:00
return true ;
2019-05-28 04:38:18 -07:00
}
2020-01-10 02:03:15 -08:00
if ( ! this . formationAnimationVariant )
this . SetAnimationVariant ( "combat" ) ;
2012-12-06 11:46:13 -08:00
2011-06-17 15:13:39 -07:00
this . StartTimer ( 1000 , 1000 ) ;
2019-12-28 05:43:18 -08:00
return false ;
2011-06-17 15:13:39 -07:00
} ,
"leave" : function ( ) {
2019-05-28 04:38:18 -07:00
this . StopMoving ( ) ;
2011-06-17 15:13:39 -07:00
this . StopTimer ( ) ;
} ,
"Timer" : function ( msg ) {
2014-03-07 00:27:13 -08:00
if ( this . ShouldAbandonChase ( this . order . data . target , this . order . data . force , IID _Attack , this . order . data . attackType ) )
2011-06-17 15:13:39 -07:00
{
this . FinishOrder ( ) ;
if ( this . GetStance ( ) . respondHoldGround )
this . WalkToHeldPosition ( ) ;
}
2019-07-01 23:49:27 -07:00
else
2019-07-24 08:55:07 -07:00
{
2019-07-01 23:49:27 -07:00
this . RememberTargetPosition ( ) ;
2019-07-24 08:55:07 -07:00
if ( this . order . data . hunting && this . orderQueue . length > 1 &&
this . orderQueue [ 1 ] . type === "Gather" )
this . RememberTargetPosition ( this . orderQueue [ 1 ] . data ) ;
}
2010-07-21 09:09:58 -07:00
} ,
2019-07-01 23:49:27 -07:00
"MovementUpdate" : function ( msg ) {
2019-07-22 11:07:24 -07:00
if ( msg . likelyFailure )
2019-07-01 23:49:27 -07:00
{
// This also handles hunting.
if ( this . orderQueue . length > 1 )
{
this . FinishOrder ( ) ;
return ;
}
2020-08-18 00:46:29 -07:00
else if ( ! this . order . data . force || ! this . order . data . lastPos )
2019-07-01 23:49:27 -07:00
{
this . SetNextState ( "COMBAT.FINDINGNEWTARGET" ) ;
return ;
}
2021-02-10 05:49:13 -08:00
// If the order was forced, try moving to the target position,
// under the assumption that this is desirable if the target
// was somewhat far away - we'll likely end up closer to where
// the player hoped we would.
2019-07-01 23:49:27 -07:00
let lastPos = this . order . data . lastPos ;
2021-02-10 05:49:13 -08:00
this . PushOrder ( "WalkAndFight" , {
"x" : lastPos . x , "z" : lastPos . z ,
"force" : false ,
} ) ;
2019-07-01 23:49:27 -07:00
return ;
}
2019-07-22 11:07:24 -07:00
if ( this . CheckTargetAttackRange ( this . order . data . target , this . order . data . attackType ) )
2019-07-03 11:05:11 -07:00
{
2019-07-22 11:07:24 -07:00
if ( this . CanUnpack ( ) )
{
this . PushOrderFront ( "Unpack" , { "force" : true } ) ;
return ;
}
this . SetNextState ( "ATTACKING" ) ;
}
else if ( msg . likelySuccess )
2019-07-03 11:05:11 -07:00
// Try moving again,
// attack range uses a height-related formula and our actual max range might have changed.
if ( ! this . MoveToTargetAttackRange ( this . order . data . target , this . order . data . attackType ) )
this . FinishOrder ( ) ;
2010-07-21 09:09:58 -07:00
} ,
} ,
"ATTACKING" : {
"enter" : function ( ) {
2019-07-01 23:49:27 -07:00
let target = this . order . data . target ;
let cmpFormation = Engine . QueryInterface ( target , IID _Formation ) ;
2014-01-21 08:50:58 -08:00
if ( cmpFormation )
{
this . order . data . formationTarget = target ;
target = cmpFormation . GetClosestMember ( this . entity ) ;
this . order . data . target = target ;
}
2019-07-01 23:49:27 -07:00
2020-09-10 09:37:14 -07:00
this . shouldCheer = false ;
2021-04-11 23:51:39 -07:00
let cmpAttack = Engine . QueryInterface ( this . entity , IID _Attack ) ;
if ( ! cmpAttack )
2014-01-03 04:49:04 -08:00
{
2021-04-11 23:51:39 -07:00
this . FinishOrder ( ) ;
2019-07-01 23:49:27 -07:00
return true ;
}
2019-06-11 13:06:25 -07:00
2019-07-01 23:49:27 -07:00
if ( ! this . CheckTargetAttackRange ( target , this . order . data . attackType ) )
{
if ( this . CanPack ( ) )
{
this . PushOrderFront ( "Pack" , { "force" : true } ) ;
2019-06-11 13:06:25 -07:00
return true ;
2014-01-03 04:49:04 -08:00
}
2019-07-01 23:49:27 -07:00
2021-04-11 23:51:39 -07:00
this . ProcessMessage ( "OutOfRange" ) ;
2019-07-01 23:49:27 -07:00
return true ;
2014-01-03 04:49:04 -08:00
}
2020-01-10 02:03:15 -08:00
if ( ! this . formationAnimationVariant )
2019-07-08 11:23:44 -07:00
this . SetAnimationVariant ( "combat" ) ;
2021-04-11 23:51:39 -07:00
this . FaceTowardsTarget ( this . order . data . target ) ;
2010-07-21 09:09:58 -07:00
2021-04-11 23:51:39 -07:00
this . RememberTargetPosition ( ) ;
if ( this . order . data . hunting && this . orderQueue . length > 1 && this . orderQueue [ 1 ] . type === "Gather" )
this . RememberTargetPosition ( this . orderQueue [ 1 ] . data ) ;
2011-06-06 15:08:26 -07:00
2021-04-11 23:51:39 -07:00
if ( ! cmpAttack . StartAttacking ( this . order . data . target , this . order . data . attackType , IID _UnitAI ) )
{
this . ProcessMessage ( "TargetInvalidated" ) ;
return true ;
}
2015-04-25 05:54:34 -07:00
2019-07-01 23:49:27 -07:00
let cmpBuildingAI = Engine . QueryInterface ( this . entity , IID _BuildingAI ) ;
2015-04-25 05:54:34 -07:00
if ( cmpBuildingAI )
2020-09-10 09:37:14 -07:00
{
2015-04-25 05:54:34 -07:00
cmpBuildingAI . SetUnitAITarget ( this . order . data . target ) ;
2020-09-10 09:37:14 -07:00
return false ;
}
let cmpUnitAI = Engine . QueryInterface ( this . order . data . target , IID _UnitAI ) ;
2020-11-16 06:47:41 -08:00
// Units with no cheering time do not cheer.
this . shouldCheer = cmpUnitAI && ( ! cmpUnitAI . IsAnimal ( ) || cmpUnitAI . IsDangerousAnimal ( ) ) && this . cheeringTime > 0 ;
2020-09-10 09:37:14 -07:00
2019-12-28 05:43:18 -08:00
return false ;
2010-07-21 09:09:58 -07:00
} ,
"leave" : function ( ) {
2019-07-01 23:49:27 -07:00
let cmpBuildingAI = Engine . QueryInterface ( this . entity , IID _BuildingAI ) ;
2015-04-25 05:54:34 -07:00
if ( cmpBuildingAI )
cmpBuildingAI . SetUnitAITarget ( 0 ) ;
2021-04-11 23:51:39 -07:00
let cmpAttack = Engine . QueryInterface ( this . entity , IID _Attack ) ;
if ( cmpAttack )
cmpAttack . StopAttacking ( ) ;
2010-07-21 09:09:58 -07:00
} ,
2021-04-11 23:51:39 -07:00
"OutOfRange" : function ( ) {
if ( this . ShouldChaseTargetedEntity ( this . order . data . target , this . order . data . force ) )
2019-07-01 23:49:27 -07:00
{
if ( this . CanPack ( ) )
2010-07-21 09:09:58 -07:00
{
2019-07-01 23:49:27 -07:00
this . PushOrderFront ( "Pack" , { "force" : true } ) ;
2019-06-11 13:06:25 -07:00
return ;
2010-07-21 09:09:58 -07:00
}
2021-04-11 23:51:39 -07:00
this . SetNextState ( "CHASING" ) ;
2019-07-01 23:49:27 -07:00
return ;
2010-07-21 09:09:58 -07:00
}
2021-04-11 23:51:39 -07:00
this . SetNextState ( "FINDINGNEWTARGET" ) ;
} ,
2019-07-28 03:39:27 -07:00
2021-04-11 23:51:39 -07:00
"TargetInvalidated" : function ( ) {
2019-07-28 03:39:27 -07:00
this . SetNextState ( "FINDINGNEWTARGET" ) ;
2019-07-01 23:49:27 -07:00
} ,
2010-10-06 14:37:55 -07:00
2019-07-01 23:49:27 -07:00
"Attacked" : function ( msg ) {
2021-03-20 02:02:22 -07:00
if ( this . order . data . attackType == "Capture" && ( this . GetStance ( ) . targetAttackersAlways || ! this . order . data . force ) &&
this . order . data . target != msg . data . attacker && this . GetBestAttackAgainst ( msg . data . attacker , true ) != "Capture" )
2019-07-01 23:49:27 -07:00
this . RespondToTargetedEntities ( [ msg . data . attacker ] ) ;
} ,
} ,
"FINDINGNEWTARGET" : {
2020-09-10 09:37:14 -07:00
"Order.Cheer" : function ( ) {
2020-11-16 06:47:41 -08:00
if ( ! this . cheeringTime )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2020-11-16 06:47:41 -08:00
2020-09-10 09:37:14 -07:00
this . SetNextState ( "CHEERING" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2020-09-10 09:37:14 -07:00
} ,
2019-07-01 23:49:27 -07:00
"enter" : function ( ) {
2019-07-28 03:39:27 -07:00
// Try to find the formation the target was a part of.
let cmpFormation = Engine . QueryInterface ( this . order . data . target , IID _Formation ) ;
if ( ! cmpFormation )
cmpFormation = Engine . QueryInterface ( this . order . data . formationTarget || INVALID _ENTITY , IID _Formation ) ;
// If the target is a formation, pick closest member.
if ( cmpFormation )
2014-01-21 08:50:58 -08:00
{
2019-07-28 03:39:27 -07:00
let filter = ( t ) => this . CanAttack ( t ) ;
this . order . data . formationTarget = this . order . data . target ;
let target = cmpFormation . GetClosestMember ( this . entity , filter ) ;
this . order . data . target = target ;
this . SetNextState ( "COMBAT.ATTACKING" ) ;
return true ;
2014-01-21 08:50:58 -08:00
}
2011-11-21 16:16:35 -08:00
// Can't reach it, no longer owned by enemy, or it doesn't exist any more - give up
2019-07-01 23:49:27 -07:00
// except if in WalkAndFight mode where we look for more enemies around before moving again.
2011-06-17 15:13:39 -07:00
if ( this . FinishOrder ( ) )
2013-02-25 13:56:24 -08:00
{
2013-12-26 08:09:32 -08:00
if ( this . IsWalkingAndFighting ( ) )
2021-03-22 06:27:33 -07:00
{
Engine . ProfileStart ( "FindWalkAndFightTargets" ) ;
2013-12-26 08:09:32 -08:00
this . FindWalkAndFightTargets ( ) ;
2021-03-22 06:27:33 -07:00
Engine . ProfileStop ( ) ;
}
2019-07-01 23:49:27 -07:00
return true ;
2013-02-25 13:56:24 -08:00
}
2011-06-17 15:13:39 -07:00
2011-06-24 05:35:15 -07:00
if ( this . FindNewTargets ( ) )
2019-07-01 23:49:27 -07:00
return true ;
2011-02-27 05:34:22 -08:00
2011-06-17 15:13:39 -07:00
if ( this . GetStance ( ) . respondHoldGround )
2011-06-24 05:35:15 -07:00
this . WalkToHeldPosition ( ) ;
2011-06-25 19:03:36 -07:00
2020-09-10 09:37:14 -07:00
if ( this . shouldCheer )
{
this . Cheer ( ) ;
this . CallPlayerOwnedEntitiesFunctionInRange ( "Cheer" , [ ] , this . notifyToCheerInRange ) ;
}
2019-07-01 23:49:27 -07:00
return true ;
2011-06-25 19:03:36 -07:00
} ,
2010-07-21 09:09:58 -07:00
} ,
"CHASING" : {
2020-04-03 11:08:45 -07:00
"Order.MoveToChasingPoint" : function ( msg ) {
2021-03-30 08:50:03 -07:00
if ( this . CheckPointRangeExplicit ( msg . data . x , msg . data . z , 0 , msg . data . max ) || ! this . AbleToMove ( ) )
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return this . FinishOrder ( ) ;
2021-03-03 00:21:00 -08:00
msg . data . relaxed = true ;
2020-04-03 11:08:45 -07:00
this . StopTimer ( ) ;
this . SetNextState ( "MOVINGTOPOINT" ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2020-04-03 11:08:45 -07:00
} ,
2018-03-10 11:12:23 -08:00
"enter" : function ( ) {
2019-06-11 13:06:25 -07:00
if ( ! this . MoveToTargetAttackRange ( this . order . data . target , this . order . data . attackType ) )
2019-05-28 04:38:18 -07:00
{
this . FinishOrder ( ) ;
2019-06-05 10:27:12 -07:00
return true ;
2019-05-28 04:38:18 -07:00
}
2020-01-10 02:03:15 -08:00
if ( ! this . formationAnimationVariant )
this . SetAnimationVariant ( "combat" ) ;
2012-12-06 11:46:13 -08:00
2023-06-14 23:56:41 -07:00
if ( Engine . QueryInterface ( this . order . data . target , IID _UnitAI ) ? . IsFleeing ( ) )
this . Run ( ) ;
2020-04-03 11:08:45 -07:00
2011-06-17 15:13:39 -07:00
this . StartTimer ( 1000 , 1000 ) ;
2019-12-28 05:43:18 -08:00
return false ;
2010-07-21 09:09:58 -07:00
} ,
2011-06-17 15:13:39 -07:00
"leave" : function ( ) {
2019-05-28 04:38:18 -07:00
this . StopMoving ( ) ;
2011-06-17 15:13:39 -07:00
this . StopTimer ( ) ;
} ,
"Timer" : function ( msg ) {
2014-03-07 00:27:13 -08:00
if ( this . ShouldAbandonChase ( this . order . data . target , this . order . data . force , IID _Attack , this . order . data . attackType ) )
2011-06-17 15:13:39 -07:00
{
this . FinishOrder ( ) ;
if ( this . GetStance ( ) . respondHoldGround )
this . WalkToHeldPosition ( ) ;
}
2020-04-03 11:08:45 -07:00
else
{
this . RememberTargetPosition ( ) ;
if ( this . order . data . hunting && this . orderQueue . length > 1 &&
this . orderQueue [ 1 ] . type === "Gather" )
this . RememberTargetPosition ( this . orderQueue [ 1 ] . data ) ;
}
2011-06-17 15:13:39 -07:00
} ,
2019-07-22 11:07:24 -07:00
"MovementUpdate" : function ( msg ) {
2020-04-03 11:08:45 -07:00
if ( msg . likelyFailure )
{
// This also handles hunting.
if ( this . orderQueue . length > 1 )
{
this . FinishOrder ( ) ;
return ;
}
else if ( ! this . order . data . force )
{
this . SetNextState ( "COMBAT.FINDINGNEWTARGET" ) ;
return ;
}
else if ( this . order . data . lastPos )
{
let lastPos = this . order . data . lastPos ;
let cmpAttack = Engine . QueryInterface ( this . entity , IID _Attack ) ;
this . PushOrder ( "MoveToChasingPoint" , {
"x" : lastPos . x ,
"z" : lastPos . z ,
"max" : cmpAttack . GetRange ( this . order . data . attackType ) . max ,
"force" : true
} ) ;
return ;
}
}
2019-07-22 11:07:24 -07:00
if ( this . CheckTargetAttackRange ( this . order . data . target , this . order . data . attackType ) )
{
if ( this . CanUnpack ( ) )
{
this . PushOrderFront ( "Unpack" , { "force" : true } ) ;
return ;
}
this . SetNextState ( "ATTACKING" ) ;
}
else if ( msg . likelySuccess )
// Try moving again,
// attack range uses a height-related formula and our actual max range might have changed.
if ( ! this . MoveToTargetAttackRange ( this . order . data . target , this . order . data . attackType ) )
this . FinishOrder ( ) ;
2010-07-21 09:09:58 -07:00
} ,
2020-04-03 11:08:45 -07:00
"MOVINGTOPOINT" : {
"enter" : function ( ) {
if ( ! this . MoveTo ( this . order . data ) )
{
this . FinishOrder ( ) ;
return true ;
}
return false ;
} ,
"leave" : function ( ) {
this . StopMoving ( ) ;
} ,
"MovementUpdate" : function ( msg ) {
2020-11-04 09:50:20 -08:00
// If it looks like the path is failing, and we are close enough from wanted range
2020-04-03 11:08:45 -07:00
// stop anyways. This avoids pathing for an unreachable goal and reduces lag considerably.
2020-11-04 09:50:20 -08:00
if ( msg . likelyFailure ||
2020-04-03 11:08:45 -07:00
msg . obstructed && this . RelaxedMaxRangeCheck ( this . order . data , this . order . data . max + this . DefaultRelaxedMaxRange ) ||
! msg . obstructed && this . CheckRange ( this . order . data ) )
this . FinishOrder ( ) ;
} ,
} ,
2010-07-21 09:09:58 -07:00
} ,
} ,
"GATHER" : {
2021-04-29 23:24:13 -07:00
"enter" : function ( ) {
let cmpResourceGatherer = Engine . QueryInterface ( this . entity , IID _ResourceGatherer ) ;
if ( cmpResourceGatherer )
cmpResourceGatherer . AddToPlayerCounter ( this . order . data . type . generic ) ;
return false ;
} ,
2020-02-01 05:46:23 -08:00
"leave" : function ( ) {
2021-04-29 23:24:13 -07:00
let cmpResourceGatherer = Engine . QueryInterface ( this . entity , IID _ResourceGatherer ) ;
if ( cmpResourceGatherer )
cmpResourceGatherer . RemoveFromPlayerCounter ( ) ;
2020-02-01 05:46:23 -08:00
// Show the carried resource, if we've gathered anything.
this . SetDefaultAnimationVariant ( ) ;
} ,
2010-07-21 09:09:58 -07:00
"APPROACHING" : {
2010-12-08 08:12:04 -08:00
"enter" : function ( ) {
2013-03-13 13:10:46 -07:00
this . gatheringTarget = this . order . data . target ; // temporary, deleted in "leave".
2023-02-17 05:43:06 -08:00
if ( this . CheckRange ( this . order . data , IID _ResourceGatherer ) )
{
this . SetNextState ( "GATHERING" ) ;
return true ;
}
2013-03-13 13:10:46 -07:00
2021-03-18 02:01:19 -07:00
// If we can't move, assume we'll fail any subsequent order
// and finish the order entirely to avoid an infinite loop.
if ( ! this . AbleToMove ( ) )
{
this . FinishOrder ( ) ;
return true ;
}
2019-09-01 00:35:32 -07:00
let cmpSupply = Engine . QueryInterface ( this . gatheringTarget , IID _ResourceSupply ) ;
let cmpMirage = Engine . QueryInterface ( this . gatheringTarget , IID _Mirage ) ;
2015-05-01 10:03:37 -07:00
if ( ( ! cmpMirage || ! cmpMirage . Mirages ( IID _ResourceSupply ) ) &&
2020-09-11 03:19:09 -07:00
( ! cmpSupply || ! cmpSupply . AddGatherer ( this . entity ) ) ||
2019-06-05 13:44:43 -07:00
! this . MoveTo ( this . order . data , IID _ResourceGatherer ) )
2013-03-13 13:10:46 -07:00
{
2019-09-01 00:35:32 -07:00
// If the target's last known position is in FOW, try going there
// and hope that we might find it then.
let lastPos = this . order . data . lastPos ;
if ( this . gatheringTarget != INVALID _ENTITY &&
lastPos && ! this . CheckPositionVisible ( lastPos . x , lastPos . z ) )
{
this . PushOrderFront ( "Walk" , {
"x" : lastPos . x , "z" : lastPos . z ,
"force" : this . order . data . force
} ) ;
return true ;
}
this . SetNextState ( "FINDINGNEWTARGET" ) ;
2019-05-28 04:38:18 -07:00
return true ;
}
2020-01-12 05:21:09 -08:00
this . SetAnimationVariant ( "approach_" + this . order . data . type . specific ) ;
2013-03-13 13:10:46 -07:00
return false ;
2010-07-21 09:09:58 -07:00
} ,
2010-09-03 02:55:14 -07:00
2019-06-09 04:16:40 -07:00
"MovementUpdate" : function ( msg ) {
2019-07-03 10:57:16 -07:00
// The GATHERING timer will handle finding a valid resource.
2019-09-01 00:35:32 -07:00
if ( msg . likelyFailure )
this . SetNextState ( "FINDINGNEWTARGET" ) ;
else if ( this . CheckRange ( this . order . data , IID _ResourceGatherer ) )
2019-07-22 11:07:24 -07:00
this . SetNextState ( "GATHERING" ) ;
2010-07-21 09:09:58 -07:00
} ,
2015-02-05 18:11:39 -08:00
2013-03-13 13:10:46 -07:00
"leave" : function ( ) {
2019-05-28 04:38:18 -07:00
this . StopMoving ( ) ;
2023-02-17 05:43:06 -08:00
this . SetDefaultAnimationVariant ( ) ;
2019-05-28 04:38:18 -07:00
if ( ! this . gatheringTarget )
return ;
2021-04-29 23:24:13 -07:00
2020-02-13 12:24:51 -08:00
let cmpSupply = Engine . QueryInterface ( this . gatheringTarget , IID _ResourceSupply ) ;
2014-01-14 16:04:25 -08:00
if ( cmpSupply )
2013-03-13 13:10:46 -07:00
cmpSupply . RemoveGatherer ( this . entity ) ;
2020-12-10 00:13:46 -08:00
2013-03-13 13:10:46 -07:00
delete this . gatheringTarget ;
} ,
2010-07-21 09:09:58 -07:00
} ,
2015-02-05 18:11:39 -08:00
2012-03-01 15:16:01 -08:00
// Walking to a good place to gather resources near, used by GatherNearPosition
2011-12-16 08:08:26 -08:00
"WALKING" : {
"enter" : function ( ) {
2019-05-28 04:38:18 -07:00
if ( ! this . MoveTo ( this . order . data ) )
{
this . FinishOrder ( ) ;
2019-06-05 10:27:12 -07:00
return true ;
2019-05-28 04:38:18 -07:00
}
2020-01-12 05:21:09 -08:00
this . SetAnimationVariant ( "approach_" + this . order . data . type . specific ) ;
2019-12-28 05:43:18 -08:00
return false ;
2011-12-16 08:08:26 -08:00
} ,
2019-05-28 04:38:18 -07:00
"leave" : function ( ) {
this . StopMoving ( ) ;
2023-02-17 05:43:06 -08:00
this . SetDefaultAnimationVariant ( ) ;
2019-05-28 04:38:18 -07:00
} ,
2019-06-09 04:16:40 -07:00
"MovementUpdate" : function ( msg ) {
2019-07-24 12:05:12 -07:00
if ( msg . likelyFailure || msg . obstructed && this . RelaxedMaxRangeCheck ( this . order . data , this . DefaultRelaxedMaxRange ) ||
2019-07-22 11:07:24 -07:00
this . CheckRange ( this . order . data ) )
2021-04-12 05:50:17 -07:00
this . SetNextState ( "FINDINGNEWTARGET" ) ;
2011-12-16 08:08:26 -08:00
} ,
} ,
2010-07-21 09:09:58 -07:00
"GATHERING" : {
"enter" : function ( ) {
2021-04-07 22:31:34 -07:00
let cmpResourceGatherer = Engine . QueryInterface ( this . entity , IID _ResourceGatherer ) ;
if ( ! cmpResourceGatherer )
{
this . FinishOrder ( ) ;
return true ;
}
2013-07-23 11:48:25 -07:00
2021-04-07 22:31:34 -07:00
if ( ! this . CheckTargetRange ( this . order . data . target , IID _ResourceGatherer ) )
2013-07-23 11:48:25 -07:00
{
2021-04-07 22:31:34 -07:00
this . ProcessMessage ( "OutOfRange" ) ;
2019-09-01 00:35:32 -07:00
return true ;
2013-07-23 03:37:43 -07:00
}
2013-07-23 11:48:25 -07:00
2012-05-08 16:00:14 -07:00
// If this order was forced, the player probably gave it, but now we've reached the target
// switch to an unforced order (can be interrupted by attacks)
this . order . data . force = false ;
2012-08-29 19:23:36 -07:00
this . order . data . autoharvest = true ;
2012-05-08 16:00:14 -07:00
2023-02-17 05:43:06 -08:00
this . FaceTowardsTarget ( this . order . data . target ) ;
2021-04-07 22:31:34 -07:00
if ( ! cmpResourceGatherer . StartGathering ( this . order . data . target , IID _UnitAI ) )
2012-04-28 12:25:44 -07:00
{
2021-04-07 22:31:34 -07:00
this . ProcessMessage ( "TargetInvalidated" ) ;
2012-09-14 13:00:03 -07:00
return true ;
2012-04-28 12:25:44 -07:00
}
2012-09-14 13:55:49 -07:00
return false ;
2010-07-21 09:09:58 -07:00
} ,
"leave" : function ( ) {
2020-12-10 00:13:46 -08:00
let cmpResourceGatherer = Engine . QueryInterface ( this . entity , IID _ResourceGatherer ) ;
if ( cmpResourceGatherer )
2021-04-07 22:31:34 -07:00
cmpResourceGatherer . StopGathering ( ) ;
2010-07-21 09:09:58 -07:00
} ,
2021-04-07 22:31:34 -07:00
"InventoryFilled" : function ( msg ) {
2021-04-29 23:24:13 -07:00
this . SetNextState ( "RETURNINGRESOURCE" ) ;
2021-04-07 22:31:34 -07:00
} ,
2019-09-01 00:35:32 -07:00
2021-04-07 22:31:34 -07:00
"OutOfRange" : function ( msg ) {
if ( this . MoveToTargetRange ( this . order . data . target , IID _ResourceGatherer ) )
this . SetNextState ( "APPROACHING" ) ;
// Our target is no longer visible - go to its last known position first
// and then hopefully it will become visible.
else if ( ! this . CheckTargetVisible ( this . order . data . target ) && this . order . data . lastPos )
this . PushOrderFront ( "Walk" , {
"x" : this . order . data . lastPos . x ,
"z" : this . order . data . lastPos . z ,
"force" : this . order . data . force
} ) ;
else
2019-09-01 00:35:32 -07:00
this . SetNextState ( "FINDINGNEWTARGET" ) ;
2021-04-07 22:31:34 -07:00
} ,
2019-09-01 00:35:32 -07:00
2021-04-07 22:31:34 -07:00
"TargetInvalidated" : function ( msg ) {
this . SetNextState ( "FINDINGNEWTARGET" ) ;
2019-09-01 00:35:32 -07:00
} ,
} ,
"FINDINGNEWTARGET" : {
"enter" : function ( ) {
2021-06-11 22:47:06 -07:00
const previousForced = this . order . data . force ;
2019-09-01 00:35:32 -07:00
let previousTarget = this . order . data . target ;
let resourceTemplate = this . order . data . template ;
let resourceType = this . order . data . type ;
2010-07-31 14:21:42 -07:00
2012-05-10 15:02:59 -07:00
// Give up on this order and try our next queued order
2015-12-20 13:30:34 -08:00
// but first check what is our next order and, if needed, insert a returnResource order
2019-07-03 10:57:16 -07:00
let cmpResourceGatherer = Engine . QueryInterface ( this . entity , IID _ResourceGatherer ) ;
2015-12-20 13:30:34 -08:00
if ( cmpResourceGatherer . IsCarrying ( resourceType . generic ) &&
this . orderQueue . length > 1 && this . orderQueue [ 1 ] !== "ReturnResource" &&
( this . orderQueue [ 1 ] . type !== "Gather" || this . orderQueue [ 1 ] . data . type . generic !== resourceType . generic ) )
{
2020-02-13 12:24:51 -08:00
let nearestDropsite = this . FindNearestDropsite ( resourceType . generic ) ;
if ( nearestDropsite )
this . orderQueue . splice ( 1 , 0 , { "type" : "ReturnResource" , "data" : { "target" : nearestDropsite , "force" : false } } ) ;
2015-12-20 13:30:34 -08:00
}
2019-07-04 12:55:09 -07:00
// Must go before FinishOrder or this.order will be undefined.
let initPos = this . order . data . initPos ;
2012-05-10 15:02:59 -07:00
if ( this . FinishOrder ( ) )
2019-09-01 00:35:32 -07:00
return true ;
2010-07-31 14:21:42 -07:00
2012-05-10 15:02:59 -07:00
// No remaining orders - pick a useful default behaviour
2010-07-31 14:21:42 -07:00
2020-08-02 04:40:46 -07:00
let cmpPosition = Engine . QueryInterface ( this . entity , IID _Position ) ;
if ( ! cmpPosition || ! cmpPosition . IsInWorld ( ) )
return true ;
2021-03-03 10:24:16 -08:00
let filter = ( ent , type , template ) => {
if ( previousTarget == ent )
return false ;
2010-12-08 08:12:04 -08:00
2021-03-03 10:24:16 -08:00
// Don't switch to a different type of huntable animal.
return type . specific == resourceType . specific &&
( type . specific != "meat" || resourceTemplate == template ) ;
} ;
2019-07-04 12:55:09 -07:00
2021-03-03 10:24:16 -08:00
// Current position is often next to a dropsite.
2021-06-11 22:47:06 -07:00
// But don't use that on forced orders, as the order may want us to go
// to the other side of the map on purpose.
2021-03-03 10:24:16 -08:00
let pos = cmpPosition . GetPosition ( ) ;
2021-06-11 22:47:06 -07:00
let nearbyResource ;
if ( ! previousForced )
nearbyResource = this . FindNearbyResource ( Vector2D . from3D ( pos ) , filter ) ;
2021-03-03 10:24:16 -08:00
// If there is an initPos, search there as well when we haven't found anything.
// Otherwise set initPos to our current pos.
if ( ! initPos )
initPos = { 'x' : pos . X , 'z' : pos . Z } ;
2021-06-11 22:47:06 -07:00
else if ( ! nearbyResource || previousForced )
2021-04-12 05:50:17 -07:00
nearbyResource = this . FindNearbyResource ( new Vector2D ( initPos . x , initPos . z ) , filter ) ;
2020-08-02 04:40:46 -07:00
if ( nearbyResource )
{
this . PerformGather ( nearbyResource , false , false ) ;
return true ;
}
// Failing that, try to move there and se if we are more lucky: maybe there are resources in FOW.
2021-04-12 23:48:13 -07:00
// Only move if we are some distance away (TODO: pick the distance better?).
// Using the default relaxed range check since that is used in the WALKING-state.
if ( ! this . CheckPointRangeExplicit ( initPos . x , initPos . z , 0 , this . DefaultRelaxedMaxRange ) )
2020-08-02 04:40:46 -07:00
{
this . GatherNearPosition ( initPos . x , initPos . z , resourceType , resourceTemplate ) ;
return true ;
2015-09-26 14:41:35 -07:00
}
2013-09-06 12:47:48 -07:00
2012-05-10 15:02:59 -07:00
// Nothing else to gather - if we're carrying anything then we should
// drop it off, and if not then we might as well head to the dropsite
// anyway because that's a nice enough place to congregate and idle
2010-12-08 08:12:04 -08:00
2020-02-13 12:24:51 -08:00
let nearestDropsite = this . FindNearestDropsite ( resourceType . generic ) ;
if ( nearestDropsite )
2012-05-10 15:02:59 -07:00
{
2020-02-13 12:24:51 -08:00
this . PushOrderFront ( "ReturnResource" , { "target" : nearestDropsite , "force" : false } ) ;
2019-09-01 00:35:32 -07:00
return true ;
2010-07-21 09:09:58 -07:00
}
2019-09-01 00:35:32 -07:00
// No dropsites - just give up.
return true ;
2010-07-21 09:09:58 -07:00
} ,
} ,
2021-04-29 23:24:13 -07:00
"RETURNINGRESOURCE" : {
"enter" : function ( ) {
let nearestDropsite = this . FindNearestDropsite ( this . order . data . type . generic ) ;
if ( ! nearestDropsite )
{
// The player expects the unit to move upon failure.
let formerTarget = this . order . data . target ;
if ( ! this . FinishOrder ( ) )
this . WalkToTarget ( formerTarget ) ;
return true ;
}
this . order . data . formerTarget = this . order . data . target ;
this . order . data . target = nearestDropsite ;
2021-07-25 11:14:39 -07:00
if ( this . CheckTargetRange ( this . order . data . target , IID _ResourceGatherer ) )
{
this . SetNextState ( "DROPPINGRESOURCES" ) ;
return true ;
}
2021-04-29 23:24:13 -07:00
this . SetNextState ( "APPROACHING" ) ;
return true ;
} ,
"leave" : function ( ) {
} ,
"APPROACHING" : "INDIVIDUAL.RETURNRESOURCE.APPROACHING" ,
"DROPPINGRESOURCES" : {
"enter" : function ( ) {
let cmpResourceGatherer = Engine . QueryInterface ( this . entity , IID _ResourceGatherer ) ;
2022-03-10 22:39:25 -08:00
if ( this . CanReturnResource ( this . order . data . target , true , cmpResourceGatherer ) &&
cmpResourceGatherer . IsTargetInRange ( this . order . data . target ) )
2021-04-29 23:24:13 -07:00
{
cmpResourceGatherer . CommitResources ( this . order . data . target ) ;
2023-02-17 05:43:06 -08:00
// Stop showing the carried resource animation.
this . SetDefaultAnimationVariant ( ) ;
2021-04-29 23:24:13 -07:00
this . SetNextState ( "GATHER.APPROACHING" ) ;
}
else
this . SetNextState ( "RETURNINGRESOURCE" ) ;
this . order . data . target = this . order . data . formerTarget ;
return true ;
} ,
"leave" : function ( ) {
} ,
} ,
} ,
2010-07-21 09:09:58 -07:00
} ,
2012-04-17 13:22:13 -07:00
"HEAL" : {
"APPROACHING" : {
2018-03-10 11:12:23 -08:00
"enter" : function ( ) {
2019-07-22 11:07:24 -07:00
if ( this . CheckRange ( this . order . data , IID _Heal ) )
2019-05-28 04:38:18 -07:00
{
this . SetNextState ( "HEALING" ) ;
return true ;
}
if ( ! this . MoveTo ( this . order . data , IID _Heal ) )
{
2021-03-18 02:01:19 -07:00
this . FinishOrder ( ) ;
2019-05-28 04:38:18 -07:00
return true ;
}
2012-04-17 13:22:13 -07:00
this . StartTimer ( 1000 , 1000 ) ;
2019-12-28 05:43:18 -08:00
return false ;
2012-04-17 13:22:13 -07:00
} ,
"leave" : function ( ) {
2019-05-28 04:38:18 -07:00
this . StopMoving ( ) ;
2012-04-17 13:22:13 -07:00
this . StopTimer ( ) ;
} ,
"Timer" : function ( msg ) {
2014-03-07 00:27:13 -08:00
if ( this . ShouldAbandonChase ( this . order . data . target , this . order . data . force , IID _Heal , null ) )
2019-07-22 11:07:24 -07:00
this . SetNextState ( "FINDINGNEWTARGET" ) ;
2012-04-17 13:22:13 -07:00
} ,
2019-07-22 11:07:24 -07:00
"MovementUpdate" : function ( msg ) {
if ( msg . likelyFailure || this . CheckRange ( this . order . data , IID _Heal ) )
this . SetNextState ( "HEALING" ) ;
2012-04-17 13:22:13 -07:00
} ,
} ,
"HEALING" : {
"enter" : function ( ) {
2021-04-07 22:40:49 -07:00
let cmpHeal = Engine . QueryInterface ( this . entity , IID _Heal ) ;
if ( ! cmpHeal )
2019-07-22 11:07:24 -07:00
{
2021-04-07 22:40:49 -07:00
this . FinishOrder ( ) ;
2019-07-22 11:07:24 -07:00
return true ;
}
2021-04-07 22:40:49 -07:00
if ( ! this . CheckRange ( this . order . data , IID _Heal ) )
2019-07-22 11:07:24 -07:00
{
2021-04-07 22:40:49 -07:00
this . ProcessMessage ( "OutOfRange" ) ;
2019-07-22 11:07:24 -07:00
return true ;
}
2021-04-07 22:40:49 -07:00
if ( ! cmpHeal . StartHealing ( this . order . data . target , IID _UnitAI ) )
2012-08-28 13:02:03 -07:00
{
2021-04-07 22:40:49 -07:00
this . ProcessMessage ( "TargetInvalidated" ) ;
return true ;
2012-08-28 13:02:03 -07:00
}
2012-04-17 13:22:13 -07:00
this . FaceTowardsTarget ( this . order . data . target ) ;
2019-12-28 05:43:18 -08:00
return false ;
2012-04-17 13:22:13 -07:00
} ,
"leave" : function ( ) {
2021-04-07 22:40:49 -07:00
let cmpHeal = Engine . QueryInterface ( this . entity , IID _Heal ) ;
if ( cmpHeal )
cmpHeal . StopHealing ( ) ;
2012-04-17 13:22:13 -07:00
} ,
2021-04-07 22:40:49 -07:00
"OutOfRange" : function ( msg ) {
if ( this . ShouldChaseTargetedEntity ( this . order . data . target , this . order . data . force ) )
2012-04-17 13:22:13 -07:00
{
2021-04-07 22:40:49 -07:00
if ( this . CanPack ( ) )
this . PushOrderFront ( "Pack" , { "force" : true } ) ;
2019-07-22 11:07:24 -07:00
else
2021-04-07 22:40:49 -07:00
this . SetNextState ( "APPROACHING" ) ;
2019-07-22 11:07:24 -07:00
}
2021-04-07 22:40:49 -07:00
else
this . SetNextState ( "FINDINGNEWTARGET" ) ;
} ,
2019-07-22 11:07:24 -07:00
2021-04-07 22:40:49 -07:00
"TargetInvalidated" : function ( msg ) {
this . SetNextState ( "FINDINGNEWTARGET" ) ;
2019-07-22 11:07:24 -07:00
} ,
} ,
"FINDINGNEWTARGET" : {
"enter" : function ( ) {
// If we have another order, do that instead.
2012-04-17 13:22:13 -07:00
if ( this . FinishOrder ( ) )
2019-07-22 11:07:24 -07:00
return true ;
2012-04-17 13:22:13 -07:00
if ( this . FindNewHealTargets ( ) )
2019-07-22 11:07:24 -07:00
return true ;
2015-02-05 18:11:39 -08:00
2012-04-17 13:22:13 -07:00
if ( this . GetStance ( ) . respondHoldGround )
this . WalkToHeldPosition ( ) ;
2019-07-22 11:07:24 -07:00
// We quit this state right away.
return true ;
2012-04-17 13:22:13 -07:00
} ,
} ,
} ,
2010-11-13 11:15:29 -08:00
// Returning to dropsite
"RETURNRESOURCE" : {
"APPROACHING" : {
2018-03-10 11:12:23 -08:00
"enter" : function ( ) {
2023-02-17 05:43:06 -08:00
if ( this . CheckTargetRange ( this . order . data . target , IID _ResourceGatherer ) )
{
this . SetNextState ( "DROPPINGRESOURCES" ) ;
return true ;
}
2019-06-15 08:07:27 -07:00
if ( ! this . MoveTo ( this . order . data , IID _ResourceGatherer ) )
2019-05-28 04:38:18 -07:00
{
this . FinishOrder ( ) ;
return true ;
}
2023-02-17 05:43:06 -08:00
this . SetDefaultAnimationVariant ( ) ;
2019-12-28 05:43:18 -08:00
return false ;
2010-11-13 11:15:29 -08:00
} ,
2019-05-28 04:38:18 -07:00
"leave" : function ( ) {
2019-07-09 12:56:28 -07:00
this . StopMoving ( ) ;
2019-05-28 04:38:18 -07:00
} ,
2010-12-08 08:12:04 -08:00
2019-07-22 11:07:24 -07:00
"MovementUpdate" : function ( msg ) {
2021-04-08 23:25:47 -07:00
if ( msg . likelyFailure || this . CheckTargetRange ( this . order . data . target , IID _ResourceGatherer ) )
this . SetNextState ( "DROPPINGRESOURCES" ) ;
} ,
} ,
"DROPPINGRESOURCES" : {
"enter" : function ( ) {
2020-06-04 04:01:06 -07:00
let cmpResourceGatherer = Engine . QueryInterface ( this . entity , IID _ResourceGatherer ) ;
2022-03-10 22:39:25 -08:00
if ( this . CanReturnResource ( this . order . data . target , true , cmpResourceGatherer ) &&
cmpResourceGatherer . IsTargetInRange ( this . order . data . target ) )
2010-11-13 11:15:29 -08:00
{
2020-06-04 04:01:06 -07:00
cmpResourceGatherer . CommitResources ( this . order . data . target ) ;
2010-11-13 11:15:29 -08:00
2020-06-04 04:01:06 -07:00
// Stop showing the carried resource animation.
this . SetDefaultAnimationVariant ( ) ;
2012-12-06 11:46:13 -08:00
2020-06-04 04:01:06 -07:00
this . FinishOrder ( ) ;
2021-04-08 23:25:47 -07:00
return true ;
2010-11-13 11:15:29 -08:00
}
2021-04-08 23:25:47 -07:00
let nearby = this . FindNearestDropsite ( cmpResourceGatherer . GetMainCarryingType ( ) ) ;
this . FinishOrder ( ) ;
2010-12-08 08:12:04 -08:00
if ( nearby )
2012-05-08 16:00:14 -07:00
this . PushOrderFront ( "ReturnResource" , { "target" : nearby , "force" : false } ) ;
2010-11-13 11:15:29 -08:00
2021-04-08 23:25:47 -07:00
return true ;
} ,
"leave" : function ( ) {
2010-11-13 11:15:29 -08:00
} ,
} ,
} ,
2021-03-02 23:47:38 -08:00
"COLLECTTREASURE" : {
"leave" : function ( ) {
} ,
"APPROACHING" : {
"enter" : function ( ) {
2021-03-18 02:01:19 -07:00
// If we can't move, assume we'll fail any subsequent order
// and finish the order entirely to avoid an infinite loop.
if ( ! this . AbleToMove ( ) )
{
this . FinishOrder ( ) ;
return true ;
}
2021-05-18 09:51:44 -07:00
if ( ! this . MoveToTargetRange ( this . order . data . target , IID _TreasureCollector ) )
2021-03-02 23:47:38 -08:00
{
2021-03-03 10:20:49 -08:00
this . SetNextState ( "FINDINGNEWTARGET" ) ;
2021-03-02 23:47:38 -08:00
return true ;
}
return false ;
} ,
"leave" : function ( ) {
this . StopMoving ( ) ;
} ,
"MovementUpdate" : function ( msg ) {
2021-05-18 09:51:44 -07:00
if ( this . CheckTargetRange ( this . order . data . target , IID _TreasureCollector ) )
2021-03-02 23:47:38 -08:00
this . SetNextState ( "COLLECTING" ) ;
else if ( msg . likelyFailure )
2021-03-03 10:20:49 -08:00
this . SetNextState ( "FINDINGNEWTARGET" ) ;
2021-03-02 23:47:38 -08:00
} ,
} ,
"COLLECTING" : {
"enter" : function ( ) {
2021-05-18 09:51:44 -07:00
let cmpTreasureCollector = Engine . QueryInterface ( this . entity , IID _TreasureCollector ) ;
if ( ! cmpTreasureCollector . StartCollecting ( this . order . data . target , IID _UnitAI ) )
2021-03-02 23:47:38 -08:00
{
this . ProcessMessage ( "TargetInvalidated" ) ;
return true ;
}
this . FaceTowardsTarget ( this . order . data . target ) ;
return false ;
} ,
"leave" : function ( ) {
2021-05-18 09:51:44 -07:00
let cmpTreasureCollector = Engine . QueryInterface ( this . entity , IID _TreasureCollector ) ;
if ( cmpTreasureCollector )
cmpTreasureCollector . StopCollecting ( ) ;
2021-03-02 23:47:38 -08:00
} ,
"OutOfRange" : function ( msg ) {
this . SetNextState ( "APPROACHING" ) ;
} ,
"TargetInvalidated" : function ( msg ) {
2021-03-03 10:20:49 -08:00
this . SetNextState ( "FINDINGNEWTARGET" ) ;
} ,
} ,
"FINDINGNEWTARGET" : {
"enter" : function ( ) {
2021-05-11 00:41:40 -07:00
let oldTarget = this . order . data . target || INVALID _ENTITY ;
2021-03-03 10:20:49 -08:00
// Switch to the next order (if any).
if ( this . FinishOrder ( ) )
return true ;
2021-05-11 00:41:40 -07:00
let nearbyTreasure = this . FindNearbyTreasure ( this . TargetPosOrEntPos ( oldTarget ) ) ;
2021-03-03 10:20:49 -08:00
if ( nearbyTreasure )
2021-05-11 00:41:40 -07:00
this . CollectTreasure ( nearbyTreasure , true ) ;
2021-03-03 10:20:49 -08:00
return true ;
2021-03-02 23:47:38 -08:00
} ,
} ,
2021-05-11 00:41:40 -07:00
// 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" ) ;
} ,
} ,
2021-03-02 23:47:38 -08:00
} ,
2012-03-08 12:42:28 -08:00
"TRADE" : {
"Attacked" : function ( msg ) {
// Ignore attack
// TODO: Inform player
} ,
2021-04-08 23:29:49 -07:00
"leave" : function ( ) {
} ,
2016-03-09 11:54:12 -08:00
"APPROACHINGMARKET" : {
2018-03-10 11:12:23 -08:00
"enter" : function ( ) {
2019-05-28 04:38:18 -07:00
if ( ! this . MoveToMarket ( this . order . data . target ) )
{
this . FinishOrder ( ) ;
2019-06-05 10:27:12 -07:00
return true ;
2019-05-28 04:38:18 -07:00
}
2019-12-28 05:43:18 -08:00
return false ;
2012-03-08 12:42:28 -08:00
} ,
2019-05-28 04:38:18 -07:00
"leave" : function ( ) {
this . StopMoving ( ) ;
} ,
2019-07-01 12:51:21 -07:00
"MovementUpdate" : function ( msg ) {
2021-01-18 03:00:02 -08:00
if ( ! msg . likelyFailure && ! this . CheckRange ( this . order . data . nextTarget , IID _Trader ) )
2019-06-09 04:16:40 -07:00
return ;
2014-01-28 10:56:39 -08:00
if ( this . waypoints && this . waypoints . length )
2014-03-26 02:11:06 -07:00
{
2016-03-09 11:54:12 -08:00
if ( ! this . MoveToMarket ( this . order . data . target ) )
2021-04-08 23:29:49 -07:00
this . FinishOrder ( ) ;
2014-03-26 02:11:06 -07:00
}
2014-01-28 10:56:39 -08:00
else
2021-04-08 23:29:49 -07:00
this . SetNextState ( "TRADING" ) ;
} ,
} ,
"TRADING" : {
"enter" : function ( ) {
if ( ! this . CanTrade ( this . order . data . target ) )
{
this . FinishOrder ( ) ;
return true ;
}
if ( ! this . CheckTargetRange ( this . order . data . target , IID _Trader ) )
{
this . SetNextState ( "APPROACHINGMARKET" ) ;
return true ;
}
let cmpTrader = Engine . QueryInterface ( this . entity , IID _Trader ) ;
let nextMarket = cmpTrader . PerformTrade ( this . order . data . target ) ;
let amount = cmpTrader . GetGoods ( ) . amount ;
if ( ! nextMarket || ! amount || ! amount . traderGain )
{
this . FinishOrder ( ) ;
return true ;
}
this . order . data . target = nextMarket ;
if ( this . order . data . route && this . order . data . route . length )
{
this . waypoints = this . order . data . route . slice ( ) ;
if ( this . order . data . target == cmpTrader . GetSecondMarket ( ) )
this . waypoints . reverse ( ) ;
}
this . SetNextState ( "APPROACHINGMARKET" ) ;
return true ;
} ,
"leave" : function ( ) {
2012-03-08 12:42:28 -08:00
} ,
} ,
2016-05-14 05:27:48 -07:00
"TradingCanceled" : function ( msg ) {
if ( msg . market != this . order . data . target )
return ;
let cmpTrader = Engine . QueryInterface ( this . entity , IID _Trader ) ;
let otherMarket = cmpTrader && cmpTrader . GetFirstMarket ( ) ;
if ( otherMarket )
this . WalkToTarget ( otherMarket ) ;
2021-04-08 23:29:49 -07:00
else
this . FinishOrder ( ) ;
2016-05-14 05:27:48 -07:00
} ,
2012-03-08 12:42:28 -08:00
} ,
2010-07-21 09:09:58 -07:00
"REPAIR" : {
"APPROACHING" : {
2018-03-10 11:12:23 -08:00
"enter" : function ( ) {
2019-06-11 13:06:25 -07:00
if ( ! this . MoveTo ( this . order . data , IID _Builder ) )
2019-05-28 04:38:18 -07:00
{
this . FinishOrder ( ) ;
return true ;
}
2019-12-28 05:43:18 -08:00
return false ;
2010-07-21 09:09:58 -07:00
} ,
2015-02-05 18:11:39 -08:00
2019-05-28 04:38:18 -07:00
"leave" : function ( ) {
this . StopMoving ( ) ;
} ,
2019-07-22 11:07:24 -07:00
"MovementUpdate" : function ( msg ) {
if ( msg . likelyFailure || msg . likelySuccess )
this . SetNextState ( "REPAIRING" ) ;
2010-07-21 09:09:58 -07:00
} ,
} ,
"REPAIRING" : {
"enter" : function ( ) {
2021-04-07 22:50:18 -07:00
let cmpBuilder = Engine . QueryInterface ( this . entity , IID _Builder ) ;
if ( ! cmpBuilder )
{
this . FinishOrder ( ) ;
return true ;
}
2012-05-08 16:00:14 -07:00
// If this order was forced, the player probably gave it, but now we've reached the target
// switch to an unforced order (can be interrupted by attacks)
2012-08-29 19:23:36 -07:00
if ( this . order . data . force )
this . order . data . autoharvest = true ;
2012-05-08 16:00:14 -07:00
this . order . data . force = false ;
2021-04-07 22:50:18 -07:00
if ( ! this . CheckTargetRange ( this . order . data . target , IID _Builder ) )
2012-08-20 00:30:50 -07:00
{
2021-04-07 22:50:18 -07:00
this . ProcessMessage ( "OutOfRange" ) ;
2014-01-08 05:39:33 -08:00
return true ;
}
2020-11-04 09:50:20 -08:00
2021-04-07 22:50:18 -07:00
let cmpHealth = Engine . QueryInterface ( this . order . data . target , IID _Health ) ;
2013-05-21 16:45:20 -07:00
if ( cmpHealth && cmpHealth . GetHitpoints ( ) >= cmpHealth . GetMaxHitpoints ( ) )
2012-08-20 00:30:50 -07:00
{
2013-05-21 16:45:20 -07:00
// The building was already finished/fully repaired before we arrived;
// let the ConstructionFinished handler handle this.
2021-04-07 22:50:18 -07:00
this . ConstructionFinished ( { "entity" : this . order . data . target , "newentity" : this . order . data . target } ) ;
2013-05-21 16:45:20 -07:00
return true ;
2012-08-20 00:30:50 -07:00
}
2021-04-07 22:50:18 -07:00
if ( ! cmpBuilder . StartRepairing ( this . order . data . target , IID _UnitAI ) )
{
this . ProcessMessage ( "TargetInvalidated" ) ;
return true ;
}
2019-07-18 12:56:09 -07:00
2021-04-07 22:50:18 -07:00
this . FaceTowardsTarget ( this . order . data . target ) ;
2012-09-14 13:55:49 -07:00
return false ;
2010-07-21 09:09:58 -07:00
} ,
"leave" : function ( ) {
2021-04-07 22:50:18 -07:00
let cmpBuilder = Engine . QueryInterface ( this . entity , IID _Builder ) ;
if ( cmpBuilder )
cmpBuilder . StopRepairing ( ) ;
2010-07-21 09:09:58 -07:00
} ,
2021-04-07 22:50:18 -07:00
"OutOfRange" : function ( msg ) {
this . SetNextState ( "APPROACHING" ) ;
} ,
2019-07-18 12:56:09 -07:00
2021-04-07 22:50:18 -07:00
"TargetInvalidated" : function ( msg ) {
this . FinishOrder ( ) ;
2010-07-21 09:09:58 -07:00
} ,
} ,
"ConstructionFinished" : function ( msg ) {
if ( msg . data . entity != this . order . data . target )
return ; // ignore other buildings
2019-07-03 10:57:16 -07:00
let oldData = this . order . data ;
2011-02-10 08:06:28 -08:00
2012-05-06 14:17:29 -07:00
// Save the current state so we can continue walking if necessary
// FinishOrder() below will switch to IDLE if there's no order, which sets the idle animation.
// Idle animation while moving towards finished construction looks weird (ghosty).
2019-07-03 10:57:16 -07:00
let oldState = this . GetCurrentState ( ) ;
2012-05-06 07:38:03 -07:00
2019-07-03 10:57:16 -07:00
let cmpResourceGatherer = Engine . QueryInterface ( this . entity , IID _ResourceGatherer ) ;
2020-06-04 04:01:06 -07:00
let canReturnResources = this . CanReturnResource ( msg . data . newentity , true , cmpResourceGatherer ) ;
if ( this . CheckTargetRange ( msg . data . newentity , IID _Builder ) && canReturnResources )
2016-02-24 10:49:28 -08:00
{
2020-06-04 04:01:06 -07:00
cmpResourceGatherer . CommitResources ( msg . data . newentity ) ;
2017-12-10 02:06:08 -08:00
this . SetDefaultAnimationVariant ( ) ;
2016-05-21 09:20:27 -07:00
}
2016-02-24 10:49:28 -08:00
2010-07-21 09:09:58 -07:00
// Switch to the next order (if any)
if ( this . FinishOrder ( ) )
2015-04-26 01:35:10 -07:00
{
2020-06-04 04:01:06 -07:00
if ( canReturnResources )
2015-08-29 15:49:44 -07:00
{
2020-06-04 04:01:06 -07:00
// We aren't in range, but we can still return resources there: always do so.
2017-12-10 02:06:08 -08:00
this . SetDefaultAnimationVariant ( ) ;
2015-08-29 15:49:44 -07:00
this . PushOrderFront ( "ReturnResource" , { "target" : msg . data . newentity , "force" : false } ) ;
2015-04-26 01:35:10 -07:00
}
2010-07-21 09:09:58 -07:00
return ;
2015-04-26 01:35:10 -07:00
}
2010-07-21 09:09:58 -07:00
2020-06-04 04:01:06 -07:00
if ( canReturnResources )
{
// We aren't in range, but we can still return resources there: always do so.
this . SetDefaultAnimationVariant ( ) ;
this . PushOrderFront ( "ReturnResource" , { "target" : msg . data . newentity , "force" : false } ) ;
}
2010-07-21 09:09:58 -07:00
// No remaining orders - pick a useful default behaviour
2011-02-10 08:06:28 -08:00
// If autocontinue explicitly disabled (e.g. by AI) then
// do nothing automatically
2012-08-29 19:23:36 -07:00
if ( ! oldData . autocontinue )
2011-02-10 08:06:28 -08:00
return ;
2019-04-07 09:06:10 -07:00
// If this building was e.g. a farm of ours, the entities that received
2012-08-29 19:23:36 -07:00
// the build command should start gathering from it
2023-03-05 23:24:47 -08:00
if ( ( oldData . force || oldData . autoharvest ) && this . CanGather ( msg . data . newentity ) )
2010-07-21 09:09:58 -07:00
{
2012-05-08 16:00:14 -07:00
this . PerformGather ( msg . data . newentity , true , false ) ;
2010-12-08 08:12:04 -08:00
return ;
2010-07-21 09:09:58 -07:00
}
2010-12-08 08:12:04 -08:00
2012-08-29 19:23:36 -07:00
// If this building was e.g. a farmstead of ours, entities that received
// the build command should look for nearby resources to gather
2020-06-04 04:01:06 -07:00
if ( ( oldData . force || oldData . autoharvest ) &&
this . CanReturnResource ( msg . data . newentity , false , cmpResourceGatherer ) )
2010-07-21 09:09:58 -07:00
{
2020-06-04 04:01:06 -07:00
let cmpResourceDropsite = Engine . QueryInterface ( msg . data . newentity , IID _ResourceDropsite ) ;
2019-07-03 10:57:16 -07:00
let types = cmpResourceDropsite . GetTypes ( ) ;
2012-02-12 13:18:50 -08:00
// TODO: Slightly undefined behavior here, we don't know what type of resource will be collected,
// may cause problems for AIs (especially hunting fast animals), but avoid ugly hacks to fix that!
2020-06-04 04:06:27 -07:00
let nearby = this . FindNearbyResource ( this . TargetPosOrEntPos ( msg . data . newentity ) ,
( ent , type , template ) => types . indexOf ( type . generic ) != - 1 ) ;
2019-07-03 10:57:16 -07:00
2010-12-08 08:12:04 -08:00
if ( nearby )
{
2012-05-08 16:00:14 -07:00
this . PerformGather ( nearby , true , false ) ;
2010-12-08 08:12:04 -08:00
return ;
}
2010-07-21 09:09:58 -07:00
}
2010-12-08 08:12:04 -08:00
2020-06-04 04:06:27 -07:00
let nearbyFoundation = this . FindNearbyFoundation ( this . TargetPosOrEntPos ( msg . data . newentity ) ) ;
2011-03-05 09:38:15 -08:00
if ( nearbyFoundation )
{
2012-08-29 19:23:36 -07:00
this . AddOrder ( "Repair" , { "target" : nearbyFoundation , "autocontinue" : oldData . autocontinue , "force" : false } , true ) ;
2011-03-05 09:38:15 -08:00
return ;
}
2012-05-06 07:38:03 -07:00
2012-05-06 14:17:29 -07:00
// Unit was approaching and there's nothing to do now, so switch to walking
2020-06-05 23:19:15 -07:00
if ( oldState . endsWith ( "REPAIR.APPROACHING" ) )
2012-05-06 14:21:04 -07:00
// We're already walking to the given point, so add this as a order.
2012-05-06 14:17:29 -07:00
this . WalkToTarget ( msg . data . newentity , true ) ;
2010-07-21 09:09:58 -07:00
} ,
} ,
2010-10-23 15:43:15 -07:00
"GARRISON" : {
"APPROACHING" : {
"enter" : function ( ) {
2021-03-26 03:18:30 -07:00
if ( this . order . data . garrison ? ! this . CanGarrison ( this . order . data . target ) :
! this . CanOccupyTurret ( this . order . data . target ) )
2021-03-12 11:53:52 -08:00
{
this . FinishOrder ( ) ;
return true ;
}
2021-03-30 04:17:46 -07:00
if ( ! this . MoveToTargetRange ( this . order . data . target , this . order . data . garrison ? IID _Garrisonable : IID _Turretable ) )
2019-05-28 04:38:18 -07:00
{
this . FinishOrder ( ) ;
return true ;
}
2019-12-20 12:05:19 -08:00
if ( this . pickup )
Engine . PostMessage ( this . pickup , MT _PickupCanceled , { "entity" : this . entity } ) ;
2021-03-26 03:18:30 -07:00
let cmpHolder = Engine . QueryInterface ( this . order . data . target , this . order . data . garrison ? IID _GarrisonHolder : IID _TurretHolder ) ;
if ( cmpHolder && cmpHolder . CanPickup ( this . entity ) )
2019-12-20 12:05:19 -08:00
{
2021-03-12 11:53:52 -08:00
this . pickup = this . order . data . target ;
2021-03-30 04:24:08 -07:00
Engine . PostMessage ( this . pickup , MT _PickupRequested , { "entity" : this . entity , "iid" : this . order . data . garrison ? IID _GarrisonHolder : IID _TurretHolder } ) ;
2019-12-20 12:05:19 -08:00
}
2019-12-28 05:43:18 -08:00
return false ;
2010-10-23 15:43:15 -07:00
} ,
2019-05-28 04:38:18 -07:00
"leave" : function ( ) {
2021-03-12 11:53:52 -08:00
if ( this . pickup )
{
Engine . PostMessage ( this . pickup , MT _PickupCanceled , { "entity" : this . entity } ) ;
delete this . pickup ;
}
2019-05-28 04:38:18 -07:00
this . StopMoving ( ) ;
} ,
2019-07-22 11:07:24 -07:00
"MovementUpdate" : function ( msg ) {
2021-03-12 11:53:52 -08:00
if ( ! msg . likelyFailure && ! msg . likelySuccess )
return ;
2021-03-30 04:17:46 -07:00
if ( this . CheckTargetRange ( this . order . data . target , this . order . data . garrison ? IID _Garrisonable : IID _Turretable ) )
2021-03-17 07:53:09 -07:00
this . SetNextState ( "GARRISONING" ) ;
2021-03-12 11:53:52 -08:00
else
{
// Unable to reach the target, try again (or follow if it is a moving target)
// except if the target does not exist anymore or its orders have changed.
if ( this . pickup )
{
let cmpUnitAI = Engine . QueryInterface ( this . pickup , IID _UnitAI ) ;
if ( ! cmpUnitAI || ( ! cmpUnitAI . HasPickupOrder ( this . entity ) && ! cmpUnitAI . IsIdle ( ) ) )
this . FinishOrder ( ) ;
}
}
2010-10-23 15:43:15 -07:00
} ,
} ,
2021-03-17 07:53:09 -07:00
"GARRISONING" : {
2010-10-23 15:43:15 -07:00
"enter" : function ( ) {
2019-07-08 11:06:37 -07:00
let target = this . order . data . target ;
2021-03-26 03:18:30 -07:00
if ( this . order . data . garrison )
2014-04-05 20:04:30 -07:00
{
2021-03-26 03:18:30 -07:00
let cmpGarrisonable = Engine . QueryInterface ( this . entity , IID _Garrisonable ) ;
if ( ! cmpGarrisonable || ! cmpGarrisonable . Garrison ( target ) )
{
this . FinishOrder ( ) ;
return true ;
}
}
else
{
let cmpTurretable = Engine . QueryInterface ( this . entity , IID _Turretable ) ;
if ( ! cmpTurretable || ! cmpTurretable . OccupyTurret ( target ) )
{
this . FinishOrder ( ) ;
return true ;
}
2013-12-26 02:43:51 -08:00
}
2021-03-12 11:53:52 -08:00
if ( this . formationController )
2021-01-07 07:30:25 -08:00
{
2021-03-12 11:53:52 -08:00
let cmpFormation = Engine . QueryInterface ( this . formationController , IID _Formation ) ;
if ( cmpFormation )
2021-01-07 07:30:25 -08:00
{
2021-03-12 11:53:52 -08:00
let rearrange = cmpFormation . rearrange ;
cmpFormation . SetRearrange ( false ) ;
cmpFormation . RemoveMembers ( [ this . entity ] ) ;
cmpFormation . SetRearrange ( rearrange ) ;
2021-01-07 07:30:25 -08:00
}
}
2018-03-26 16:20:34 -07:00
2021-03-12 11:53:52 -08:00
let cmpResourceGatherer = Engine . QueryInterface ( this . entity , IID _ResourceGatherer ) ;
if ( this . CanReturnResource ( target , true , cmpResourceGatherer ) )
{
cmpResourceGatherer . CommitResources ( target ) ;
this . SetDefaultAnimationVariant ( ) ;
}
2013-11-08 15:22:59 -08:00
2021-03-17 07:53:09 -07:00
this . FinishOrder ( ) ;
return true ;
2011-05-10 21:05:05 -07:00
} ,
2015-02-05 18:11:39 -08:00
2010-10-23 15:43:15 -07:00
"leave" : function ( ) {
2021-03-12 11:53:52 -08:00
} ,
2010-10-23 15:43:15 -07:00
} ,
} ,
2011-05-02 08:03:01 -07:00
"CHEERING" : {
"enter" : function ( ) {
this . SelectAnimation ( "promotion" ) ;
2020-11-16 06:47:41 -08:00
this . StartTimer ( this . cheeringTime ) ;
2011-05-02 08:03:01 -07:00
return false ;
} ,
"leave" : function ( ) {
2021-01-23 06:08:41 -08:00
// PushOrderFront preserves the cheering order,
// which can lead to very bad behaviour, so make
// sure to delete any queued ones.
for ( let i = 1 ; i < this . orderQueue . length ; ++ i )
if ( this . orderQueue [ i ] . type == "Cheer" )
this . orderQueue . splice ( i -- , 1 ) ;
2011-05-02 08:03:01 -07:00
this . StopTimer ( ) ;
2019-07-09 12:56:28 -07:00
this . ResetAnimation ( ) ;
2020-09-10 09:37:14 -07:00
} ,
"LosRangeUpdate" : function ( msg ) {
if ( msg && msg . data && msg . data . added && msg . data . added . length )
this . RespondToSightedEntities ( msg . data . added ) ;
} ,
"LosHealRangeUpdate" : function ( msg ) {
if ( msg && msg . data && msg . data . added && msg . data . added . length )
this . RespondToHealableEntities ( msg . data . added ) ;
} ,
"LosAttackRangeUpdate" : function ( msg ) {
if ( msg && msg . data && msg . data . added && msg . data . added . length && this . GetStance ( ) . targetVisibleEnemies )
this . AttackEntitiesByPreference ( msg . data . added ) ;
2011-05-02 08:03:01 -07:00
} ,
"Timer" : function ( msg ) {
this . FinishOrder ( ) ;
} ,
} ,
2012-11-30 16:34:03 -08:00
"PACKING" : {
"enter" : function ( ) {
2021-02-27 21:40:08 -08:00
let cmpPack = Engine . QueryInterface ( this . entity , IID _Pack ) ;
2012-11-30 16:34:03 -08:00
cmpPack . Pack ( ) ;
2019-12-28 05:43:18 -08:00
return false ;
2012-11-30 16:34:03 -08:00
} ,
2021-02-27 21:51:55 -08:00
"Order.CancelPack" : function ( msg ) {
this . FinishOrder ( ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2021-02-27 21:51:55 -08:00
} ,
2012-11-30 16:34:03 -08:00
"PackFinished" : function ( msg ) {
this . FinishOrder ( ) ;
} ,
"leave" : function ( ) {
2020-11-16 01:33:09 -08:00
let cmpPack = Engine . QueryInterface ( this . entity , IID _Pack ) ;
cmpPack . CancelPack ( ) ;
2012-11-30 16:34:03 -08:00
} ,
2013-03-18 17:53:47 -07:00
"Attacked" : function ( msg ) {
// Ignore attacks while packing
} ,
2012-11-30 16:34:03 -08:00
} ,
"UNPACKING" : {
"enter" : function ( ) {
2021-02-27 21:40:08 -08:00
let cmpPack = Engine . QueryInterface ( this . entity , IID _Pack ) ;
2012-11-30 16:34:03 -08:00
cmpPack . Unpack ( ) ;
2019-12-28 05:43:18 -08:00
return false ;
2012-11-30 16:34:03 -08:00
} ,
2021-02-27 21:51:55 -08:00
"Order.CancelUnpack" : function ( msg ) {
this . FinishOrder ( ) ;
2021-02-27 22:29:53 -08:00
return ACCEPT _ORDER ;
2021-02-27 21:51:55 -08:00
} ,
2012-11-30 16:34:03 -08:00
"PackFinished" : function ( msg ) {
this . FinishOrder ( ) ;
} ,
"leave" : function ( ) {
2020-11-16 01:33:09 -08:00
let cmpPack = Engine . QueryInterface ( this . entity , IID _Pack ) ;
cmpPack . CancelPack ( ) ;
2012-11-30 16:34:03 -08:00
} ,
2013-03-18 17:53:47 -07:00
"Attacked" : function ( msg ) {
// Ignore attacks while unpacking
} ,
2012-11-30 16:34:03 -08:00
} ,
2013-11-08 15:22:59 -08:00
"PICKUP" : {
"APPROACHING" : {
"enter" : function ( ) {
2019-05-28 04:38:18 -07:00
if ( ! this . MoveTo ( this . order . data ) )
{
this . FinishOrder ( ) ;
return true ;
}
2019-12-28 05:43:18 -08:00
return false ;
2013-11-08 15:22:59 -08:00
} ,
2019-05-28 04:38:18 -07:00
"leave" : function ( ) {
this . StopMoving ( ) ;
} ,
2019-07-22 11:07:24 -07:00
"MovementUpdate" : function ( msg ) {
if ( msg . likelyFailure || msg . likelySuccess )
this . SetNextState ( "LOADING" ) ;
2013-11-08 15:22:59 -08:00
} ,
"PickupCanceled" : function ( ) {
this . FinishOrder ( ) ;
} ,
} ,
"LOADING" : {
"enter" : function ( ) {
2021-03-30 04:24:08 -07:00
let cmpHolder = Engine . QueryInterface ( this . entity , this . order . data . iid ) ;
if ( ! cmpHolder || cmpHolder . IsFull ( ) )
2013-11-08 15:22:59 -08:00
{
this . FinishOrder ( ) ;
return true ;
}
return false ;
} ,
"PickupCanceled" : function ( ) {
this . FinishOrder ( ) ;
} ,
} ,
} ,
2010-07-21 09:09:58 -07:00
} ,
} ;
2010-02-05 14:00:39 -08:00
UnitAI . prototype . Init = function ( )
{
2010-07-21 09:09:58 -07:00
this . orderQueue = [ ] ; // current order is at the front of the list
this . order = undefined ; // always == this.orderQueue[0]
2010-09-03 02:55:14 -07:00
this . formationController = INVALID _ENTITY ; // entity with IID_Formation that we belong to
2011-02-05 12:35:34 -08:00
this . isIdle = false ;
2015-02-05 18:11:39 -08:00
2014-04-14 18:12:48 -07:00
this . heldPosition = undefined ;
2013-11-01 15:00:06 -07:00
// Queue of remembered works
this . workOrders = [ ] ;
2010-08-04 14:15:41 -07:00
2013-11-30 09:30:08 -08:00
this . isGuardOf = undefined ;
2020-01-09 12:53:13 -08:00
this . formationAnimationVariant = undefined ;
2020-11-16 06:47:41 -08:00
this . cheeringTime = + ( this . template . CheeringTime || 0 ) ;
2011-03-04 06:36:41 -08:00
this . SetStance ( this . template . DefaultStance ) ;
2014-05-30 07:46:06 -07:00
} ;
2021-12-09 08:35:03 -08:00
/ * *
* @ param { cmpTurretable } cmpTurretable - Optionally the component to save a query here .
* @ return { boolean } - Whether we are occupying a turret point .
* /
UnitAI . prototype . IsTurret = function ( cmpTurretable )
2014-05-30 07:46:06 -07:00
{
2021-12-09 08:35:03 -08:00
if ( ! cmpTurretable )
cmpTurretable = Engine . QueryInterface ( this . entity , IID _Turretable ) ;
2021-03-26 03:18:30 -07:00
return cmpTurretable && cmpTurretable . HolderID ( ) != INVALID _ENTITY ;
2010-07-21 09:09:58 -07:00
} ;
2010-02-05 14:00:39 -08:00
2010-09-03 02:55:14 -07:00
UnitAI . prototype . IsFormationController = function ( )
{
return ( this . template . FormationController == "true" ) ;
} ;
2012-12-01 17:52:27 -08:00
UnitAI . prototype . IsFormationMember = function ( )
{
return ( this . formationController != INVALID _ENTITY ) ;
} ;
2022-01-22 23:23:44 -08:00
UnitAI . prototype . GetFormationsList = function ( )
{
return this . template . Formations ? . _string ? . split ( /\s+/ ) || [ ] ;
} ;
UnitAI . prototype . CanUseFormation = function ( formation )
{
return this . GetFormationsList ( ) . includes ( formation ) ;
} ;
2021-02-27 12:13:40 -08:00
/ * *
* For now , entities with a RoamDistance are animals .
* /
2011-02-27 05:34:22 -08:00
UnitAI . prototype . IsAnimal = function ( )
{
2021-02-27 12:13:40 -08:00
return ! ! this . template . RoamDistance ;
2011-02-27 05:34:22 -08:00
} ;
2021-02-27 12:13:40 -08:00
/ * *
* ToDo : Make this not needed by fixing gaia
* range queries in BuildingAI and UnitAI regarding
* animals and other gaia entities .
* /
2012-08-07 22:32:53 -07:00
UnitAI . prototype . IsDangerousAnimal = function ( )
{
2021-02-27 12:13:40 -08:00
return this . IsAnimal ( ) && this . GetStance ( ) . targetVisibleEnemies && ! ! Engine . QueryInterface ( this . entity , IID _Attack ) ;
2012-08-07 22:32:53 -07:00
} ;
2012-04-17 13:22:13 -07:00
UnitAI . prototype . IsHealer = function ( )
{
return Engine . QueryInterface ( this . entity , IID _Heal ) ;
} ;
2011-02-05 12:35:34 -08:00
UnitAI . prototype . IsIdle = function ( )
{
return this . isIdle ;
} ;
2021-01-13 07:27:18 -08:00
/ * *
* Used by formation controllers to toggle the idleness of their members .
* /
UnitAI . prototype . ResetIdle = function ( )
{
let shouldBeIdle = this . GetCurrentState ( ) . endsWith ( ".IDLE" ) ;
if ( this . isIdle == shouldBeIdle )
return ;
this . isIdle = shouldBeIdle ;
Engine . PostMessage ( this . entity , MT _UnitIdleChanged , { "idle" : this . isIdle } ) ;
} ;
2021-03-07 23:19:06 -08:00
UnitAI . prototype . SetGarrisoned = function ( )
2011-05-10 21:05:05 -07:00
{
2021-03-07 23:19:06 -08:00
// UnitAI caches its own garrisoned state for performance.
this . isGarrisoned = true ;
this . SetImmobile ( ) ;
2011-05-10 21:05:05 -07:00
} ;
2021-03-07 23:19:06 -08:00
UnitAI . prototype . UnsetGarrisoned = function ( )
2015-10-09 11:14:53 -07:00
{
2021-03-07 23:19:06 -08:00
delete this . isGarrisoned ;
this . SetMobile ( ) ;
2015-10-09 11:14:53 -07:00
} ;
2018-02-23 20:35:26 -08:00
UnitAI . prototype . ShouldRespondToEndOfAlert = function ( )
2018-01-29 17:47:12 -08:00
{
2018-02-23 20:35:26 -08:00
return ! this . orderQueue . length || this . orderQueue [ 0 ] . type == "Garrison" ;
2018-01-29 17:47:12 -08:00
} ;
2021-03-07 23:19:06 -08:00
UnitAI . prototype . SetImmobile = function ( )
2020-06-02 04:40:29 -07:00
{
2021-03-07 23:19:06 -08:00
if ( this . isImmobile )
return ;
this . isImmobile = true ;
Engine . PostMessage ( this . entity , MT _UnitAbleToMoveChanged , {
"entity" : this . entity ,
"ableToMove" : this . AbleToMove ( )
} ) ;
} ;
UnitAI . prototype . SetMobile = function ( )
{
if ( ! this . isImmobile )
return ;
delete this . isImmobile ;
2020-06-02 04:40:29 -07:00
Engine . PostMessage ( this . entity , MT _UnitAbleToMoveChanged , {
"entity" : this . entity ,
"ableToMove" : this . AbleToMove ( )
} ) ;
} ;
/ * *
* @ param cmpUnitMotion - optionally pass unitMotion to avoid querying it here
* @ returns true if the entity can move , i . e . has UnitMotion and isn ' t immobile .
* /
UnitAI . prototype . AbleToMove = function ( cmpUnitMotion )
{
2021-12-09 08:35:03 -08:00
if ( this . isImmobile )
2020-06-02 04:40:29 -07:00
return false ;
if ( ! cmpUnitMotion )
cmpUnitMotion = Engine . QueryInterface ( this . entity , IID _UnitMotion ) ;
return ! ! cmpUnitMotion ;
} ;
2014-01-05 10:29:38 -08:00
UnitAI . prototype . IsFleeing = function ( )
{
var state = this . GetCurrentState ( ) . split ( "." ) . pop ( ) ;
return ( state == "FLEEING" ) ;
} ;
2012-09-04 16:27:06 -07:00
UnitAI . prototype . IsWalking = function ( )
{
var state = this . GetCurrentState ( ) . split ( "." ) . pop ( ) ;
return ( state == "WALKING" ) ;
} ;
2013-12-26 08:09:32 -08:00
/ * *
2018-03-30 17:21:34 -07:00
* Return true if the current order is WalkAndFight or Patrol .
2013-12-26 08:09:32 -08:00
* /
UnitAI . prototype . IsWalkingAndFighting = function ( )
{
if ( this . IsFormationMember ( ) )
2022-03-31 22:46:13 -07:00
return Engine . QueryInterface ( this . formationController , IID _UnitAI ) . IsWalkingAndFighting ( ) ;
2013-12-26 08:09:32 -08:00
2018-03-30 17:21:34 -07:00
return this . orderQueue . length > 0 && ( this . orderQueue [ 0 ] . type == "WalkAndFight" || this . orderQueue [ 0 ] . type == "Patrol" ) ;
2013-12-26 08:09:32 -08:00
} ;
2010-07-21 09:09:58 -07:00
UnitAI . prototype . OnCreate = function ( )
{
2021-02-27 12:13:40 -08:00
if ( this . IsFormationController ( ) )
2014-05-27 00:24:07 -07:00
this . UnitFsm . Init ( this , "FORMATIONCONTROLLER.IDLE" ) ;
2010-09-03 02:55:14 -07:00
else
2014-05-27 00:24:07 -07:00
this . UnitFsm . Init ( this , "INDIVIDUAL.IDLE" ) ;
2015-06-04 12:00:42 -07:00
this . isIdle = true ;
2012-04-28 12:25:44 -07:00
} ;
2012-04-04 13:23:41 -07:00
2012-10-23 09:31:07 -07:00
UnitAI . prototype . OnDiplomacyChanged = function ( msg )
{
2016-12-07 10:08:26 -08:00
let cmpOwnership = Engine . QueryInterface ( this . entity , IID _Ownership ) ;
2012-10-23 09:31:07 -07:00
if ( cmpOwnership && cmpOwnership . GetOwner ( ) == msg . player )
2015-10-08 14:59:52 -07:00
this . SetupRangeQueries ( ) ;
2016-12-07 10:08:26 -08:00
if ( this . isGuardOf && ! IsOwnedByMutualAllyOfEntity ( this . entity , this . isGuardOf ) )
this . RemoveGuard ( ) ;
2012-10-23 09:31:07 -07:00
} ;
2010-07-29 13:39:23 -07:00
UnitAI . prototype . OnOwnershipChanged = function ( msg )
{
2012-08-07 22:32:53 -07:00
this . SetupRangeQueries ( ) ;
2018-01-21 17:02:29 -08:00
if ( this . isGuardOf && ( msg . to == INVALID _PLAYER || ! IsOwnedByMutualAllyOfEntity ( this . entity , this . isGuardOf ) ) )
2016-12-07 10:08:26 -08:00
this . RemoveGuard ( ) ;
2015-08-02 02:39:08 -07:00
// If the unit isn't being created or dying, reset stance and clear orders
2018-01-21 17:02:29 -08:00
if ( msg . to != INVALID _PLAYER && msg . from != INVALID _PLAYER )
2012-08-07 22:32:53 -07:00
{
2014-02-01 09:55:27 -08:00
// Switch to a virgin state to let states execute their leave handlers.
2021-03-17 07:53:09 -07:00
// Except if (un)packing, in which case we only clear the order queue.
if ( this . IsPacking ( ) )
2015-12-20 13:07:47 -08:00
{
2015-08-02 02:39:08 -07:00
this . orderQueue . length = Math . min ( this . orderQueue . length , 1 ) ;
2015-12-20 13:07:47 -08:00
Engine . PostMessage ( this . entity , MT _UnitAIOrderDataChanged , { "to" : this . GetOrderData ( ) } ) ;
}
2015-08-02 02:39:08 -07:00
else
{
2021-06-07 04:38:16 -07:00
const state = this . GetCurrentState ( ) ;
// Special "will be destroyed soon" mode - do nothing.
if ( state === "" )
return ;
const index = state . indexOf ( "." ) ;
2015-08-02 02:39:08 -07:00
if ( index != - 1 )
2021-03-20 02:02:22 -07:00
this . UnitFsm . SwitchToNextState ( this , this . GetCurrentState ( ) . slice ( 0 , index ) ) ;
2015-08-02 02:39:08 -07:00
this . Stop ( false ) ;
}
2014-01-14 16:04:25 -08:00
2016-09-23 09:11:26 -07:00
this . workOrders = [ ] ;
let cmpTrader = Engine . QueryInterface ( this . entity , IID _Trader ) ;
if ( cmpTrader )
cmpTrader . StopTrading ( ) ;
2012-08-07 22:32:53 -07:00
this . SetStance ( this . template . DefaultStance ) ;
2015-08-02 02:39:08 -07:00
if ( this . IsTurret ( ) )
2015-08-02 10:24:04 -07:00
this . SetTurretStance ( ) ;
2012-08-07 22:32:53 -07:00
}
2010-07-29 13:39:23 -07:00
} ;
2015-08-02 10:24:04 -07:00
2010-07-29 13:39:23 -07:00
UnitAI . prototype . OnDestroy = function ( )
{
2013-06-25 14:49:54 -07:00
// Switch to an empty state to let states execute their leave handlers.
2014-05-27 00:24:07 -07:00
this . UnitFsm . SwitchToNextState ( this , "" ) ;
2010-07-29 13:39:23 -07:00
2020-07-23 23:07:27 -07:00
let cmpRangeManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _RangeManager ) ;
2010-07-29 13:39:23 -07:00
if ( this . losRangeQuery )
2015-08-29 15:49:44 -07:00
cmpRangeManager . DestroyActiveQuery ( this . losRangeQuery ) ;
2012-04-17 13:22:13 -07:00
if ( this . losHealRangeQuery )
2015-08-29 15:49:44 -07:00
cmpRangeManager . DestroyActiveQuery ( this . losHealRangeQuery ) ;
2020-07-23 23:07:27 -07:00
if ( this . losAttackRangeQuery )
cmpRangeManager . DestroyActiveQuery ( this . losAttackRangeQuery ) ;
2010-07-29 13:39:23 -07:00
} ;
2012-09-24 15:27:32 -07:00
UnitAI . prototype . OnVisionRangeChanged = function ( msg )
{
if ( this . entity == msg . entity )
this . SetupRangeQueries ( ) ;
} ;
2013-11-08 15:22:59 -08:00
UnitAI . prototype . HasPickupOrder = function ( entity )
{
2015-08-29 15:49:44 -07:00
return this . orderQueue . some ( order => order . type == "PickupUnit" && order . data . target == entity ) ;
2013-11-08 15:22:59 -08:00
} ;
UnitAI . prototype . OnPickupRequested = function ( msg )
{
if ( this . HasPickupOrder ( msg . entity ) )
return ;
2021-03-30 04:24:08 -07:00
this . PushOrderAfterForced ( "PickupUnit" , { "target" : msg . entity , "iid" : msg . iid } ) ;
2013-11-08 15:22:59 -08:00
} ;
UnitAI . prototype . OnPickupCanceled = function ( msg )
{
2015-12-20 13:07:47 -08:00
for ( let i = 0 ; i < this . orderQueue . length ; ++ i )
2013-11-08 15:22:59 -08:00
{
2015-12-20 13:07:47 -08:00
if ( this . orderQueue [ i ] . type != "PickupUnit" || this . orderQueue [ i ] . data . target != msg . entity )
continue ;
if ( i == 0 )
2021-03-20 02:02:22 -07:00
this . UnitFsm . ProcessMessage ( this , { "type" : "PickupCanceled" , "data" : msg } ) ;
2015-12-20 13:07:47 -08:00
else
this . orderQueue . splice ( i , 1 ) ;
Engine . PostMessage ( this . entity , MT _UnitAIOrderDataChanged , { "to" : this . GetOrderData ( ) } ) ;
break ;
2013-11-08 15:22:59 -08:00
}
} ;
2020-07-23 23:07:27 -07:00
/ * *
* Wrapper function that sets up the LOS , healer and attack range queries .
* This should be called whenever our ownership changes .
* /
2012-08-07 22:32:53 -07:00
UnitAI . prototype . SetupRangeQueries = function ( )
{
2021-02-27 12:13:40 -08:00
if ( this . GetStance ( ) . respondFleeOnSight )
2020-07-23 23:07:27 -07:00
this . SetupLOSRangeQuery ( ) ;
2012-08-07 22:32:53 -07:00
if ( this . IsHealer ( ) )
this . SetupHealRangeQuery ( ) ;
2020-07-23 23:07:27 -07:00
if ( Engine . QueryInterface ( this . entity , IID _Attack ) )
this . SetupAttackRangeQuery ( ) ;
2015-08-29 15:49:44 -07:00
} ;
2012-08-07 22:32:53 -07:00
2015-09-27 05:23:40 -07:00
UnitAI . prototype . UpdateRangeQueries = function ( )
{
2020-07-23 23:07:27 -07:00
let cmpRangeManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _RangeManager ) ;
2015-09-27 05:23:40 -07:00
if ( this . losRangeQuery )
2020-07-23 23:07:27 -07:00
this . SetupLOSRangeQuery ( cmpRangeManager . IsActiveQueryEnabled ( this . losRangeQuery ) ) ;
2016-05-21 09:20:27 -07:00
2020-07-23 23:07:27 -07:00
if ( this . losHealRangeQuery )
2015-09-27 05:23:40 -07:00
this . SetupHealRangeQuery ( cmpRangeManager . IsActiveQueryEnabled ( this . losHealRangeQuery ) ) ;
2020-07-23 23:07:27 -07:00
if ( this . losAttackRangeQuery )
this . SetupAttackRangeQuery ( cmpRangeManager . IsActiveQueryEnabled ( this . losAttackRangeQuery ) ) ;
2015-09-27 05:23:40 -07:00
} ;
2020-07-23 23:07:27 -07:00
/ * *
* Set up a range query for all enemy units within LOS range .
* @ param { boolean } enable - Optional parameter whether to enable the query .
* /
UnitAI . prototype . SetupLOSRangeQuery = function ( enable = true )
2010-07-29 13:39:23 -07:00
{
2020-07-23 23:07:27 -07:00
let cmpRangeManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _RangeManager ) ;
2010-07-29 13:39:23 -07:00
if ( this . losRangeQuery )
2012-08-07 22:32:53 -07:00
{
2015-08-29 15:49:52 -07:00
cmpRangeManager . DestroyActiveQuery ( this . losRangeQuery ) ;
2012-08-07 22:32:53 -07:00
this . losRangeQuery = undefined ;
}
2010-07-29 13:39:23 -07:00
2023-06-18 23:33:33 -07:00
const cmpDiplomacy = QueryOwnerInterface ( this . entity , IID _Diplomacy ) ;
if ( ! cmpDiplomacy )
2015-08-29 15:49:52 -07:00
return ;
2023-06-18 23:33:33 -07:00
const players = cmpDiplomacy . GetEnemies ( ) ;
2020-01-30 13:13:42 -08:00
if ( ! players . length )
return ;
2020-07-23 23:07:27 -07:00
let range = this . GetQueryRange ( IID _Vision ) ;
2021-01-23 10:57:46 -08:00
// Do not compensate for entity sizes: LOS doesn't, and UnitAI relies on that.
this . losRangeQuery = cmpRangeManager . CreateActiveQuery ( this . entity ,
range . min , range . max , players , IID _Identity ,
cmpRangeManager . GetEntityFlagMask ( "normal" ) , false ) ;
2015-02-05 18:11:39 -08:00
2015-09-27 05:23:40 -07:00
if ( enable )
cmpRangeManager . EnableActiveQuery ( this . losRangeQuery ) ;
2011-06-24 05:35:15 -07:00
} ;
2010-07-29 13:39:23 -07:00
2020-07-23 23:07:27 -07:00
/ * *
* Set up a range query for all own or ally units within LOS range
* which can be healed .
* @ param { boolean } enable - Optional parameter whether to enable the query .
* /
2015-09-27 05:23:40 -07:00
UnitAI . prototype . SetupHealRangeQuery = function ( enable = true )
2012-04-17 13:22:13 -07:00
{
2020-07-23 23:07:27 -07:00
let cmpRangeManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _RangeManager ) ;
2012-04-17 13:22:13 -07:00
if ( this . losHealRangeQuery )
2015-08-29 15:49:52 -07:00
{
cmpRangeManager . DestroyActiveQuery ( this . losHealRangeQuery ) ;
this . losHealRangeQuery = undefined ;
}
2012-04-17 13:22:13 -07:00
2023-06-18 23:33:33 -07:00
const cmpDiplomacy = QueryOwnerInterface ( this . entity , IID _Diplomacy ) ;
if ( ! cmpDiplomacy )
2015-08-29 15:49:52 -07:00
return ;
2012-04-17 13:22:13 -07:00
2023-06-18 23:33:33 -07:00
const players = cmpDiplomacy . GetAllies ( ) ;
const range = this . GetQueryRange ( IID _Heal ) ;
2012-04-17 13:22:13 -07:00
2021-01-23 10:57:46 -08:00
// Do not compensate for entity sizes: LOS doesn't, and UnitAI relies on that.
this . losHealRangeQuery = cmpRangeManager . CreateActiveQuery ( this . entity ,
range . min , range . max , players , IID _Health ,
cmpRangeManager . GetEntityFlagMask ( "injured" ) , false ) ;
2015-09-27 05:23:40 -07:00
if ( enable )
cmpRangeManager . EnableActiveQuery ( this . losHealRangeQuery ) ;
2012-04-17 13:22:13 -07:00
} ;
2020-07-23 23:07:27 -07:00
/ * *
* Set up a range query for all enemy and gaia units within range
* which can be attacked .
* @ param { boolean } enable - Optional parameter whether to enable the query .
* /
UnitAI . prototype . SetupAttackRangeQuery = function ( enable = true )
{
let cmpRangeManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _RangeManager ) ;
if ( this . losAttackRangeQuery )
{
cmpRangeManager . DestroyActiveQuery ( this . losAttackRangeQuery ) ;
this . losAttackRangeQuery = undefined ;
}
2023-06-18 23:33:33 -07:00
const cmpDiplomacy = QueryOwnerInterface ( this . entity , IID _Diplomacy ) ;
if ( ! cmpDiplomacy )
2020-07-23 23:07:27 -07:00
return ;
// TODO: How to handle neutral players - Special query to attack military only?
2023-06-18 23:33:33 -07:00
const players = cmpDiplomacy . GetEnemies ( ) ;
2020-07-23 23:07:27 -07:00
if ( ! players . length )
return ;
let range = this . GetQueryRange ( IID _Attack ) ;
2021-01-23 10:57:46 -08:00
// Do not compensate for entity sizes: LOS doesn't, and UnitAI relies on that.
this . losAttackRangeQuery = cmpRangeManager . CreateActiveQuery ( this . entity ,
range . min , range . max , players , IID _Resistance ,
cmpRangeManager . GetEntityFlagMask ( "normal" ) , false ) ;
2020-07-23 23:07:27 -07:00
if ( enable )
cmpRangeManager . EnableActiveQuery ( this . losAttackRangeQuery ) ;
} ;
2012-08-07 22:32:53 -07:00
2021-03-20 02:02:22 -07:00
// FSM linkage functions
2010-07-29 13:39:23 -07:00
2019-01-05 01:01:51 -08:00
// Setting the next state to the current state will leave/re-enter the top-most substate.
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
// Must be called from inside the FSM.
2010-07-21 09:09:58 -07:00
UnitAI . prototype . SetNextState = function ( state )
2010-02-05 14:00:39 -08:00
{
2014-05-27 00:24:07 -07:00
this . UnitFsm . SetNextState ( this , state ) ;
2010-02-05 14:00:39 -08:00
} ;
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
// Must be called from inside the FSM.
2010-07-21 09:09:58 -07:00
UnitAI . prototype . DeferMessage = function ( msg )
2010-07-08 13:08:08 -07:00
{
2014-05-27 00:24:07 -07:00
this . UnitFsm . DeferMessage ( this , msg ) ;
2010-07-21 09:09:58 -07:00
} ;
2010-07-08 13:08:08 -07:00
2012-04-04 13:23:41 -07:00
UnitAI . prototype . GetCurrentState = function ( )
{
2014-05-27 00:24:07 -07:00
return this . UnitFsm . GetCurrentState ( this ) ;
2012-04-04 13:23:41 -07:00
} ;
2012-05-09 05:10:01 -07:00
UnitAI . prototype . FsmStateNameChanged = function ( state )
{
Engine . PostMessage ( this . entity , MT _UnitAIStateChanged , { "to" : state } ) ;
} ;
2010-07-21 09:09:58 -07:00
/ * *
* Call when the current order has been completed ( or failed ) .
* Removes the current order from the queue , and processes the
* next one ( if any ) . Returns false and defaults to IDLE
2018-03-20 11:20:10 -07:00
* if there are no remaining orders or if the unit is not
* inWorld and not garrisoned ( thus usually waiting to be destroyed ) .
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
* Must be called from inside the FSM .
2010-07-21 09:09:58 -07:00
* /
UnitAI . prototype . FinishOrder = function ( )
2010-02-05 14:00:39 -08:00
{
2010-07-21 09:09:58 -07:00
if ( ! this . orderQueue . length )
2012-08-25 14:55:53 -07:00
{
2018-03-20 11:20:10 -07:00
let stack = new Error ( ) . stack . trimRight ( ) . replace ( /^/mg , ' ' ) ; // indent each line
let cmpTemplateManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _TemplateManager ) ;
let template = cmpTemplateManager . GetCurrentTemplateName ( this . entity ) ;
2012-08-25 14:55:53 -07:00
error ( "FinishOrder called for entity " + this . entity + " (" + template + ") when order queue is empty\n" + stack ) ;
}
2010-02-05 14:00:39 -08:00
2013-11-19 13:27:23 -08:00
this . orderQueue . shift ( ) ;
2010-07-21 09:09:58 -07:00
this . order = this . orderQueue [ 0 ] ;
2010-02-12 14:46:53 -08:00
2021-03-07 23:19:06 -08:00
if ( this . orderQueue . length && ( this . isGarrisoned || this . IsFormationController ( ) ||
2021-12-09 08:35:03 -08:00
Engine . QueryInterface ( this . entity , IID _Position ) ? . IsInWorld ( ) ) )
2010-07-08 13:08:08 -07:00
{
2021-03-20 02:02:22 -07:00
let ret = this . UnitFsm . ProcessMessage ( this , {
"type" : "Order." + this . order . type ,
"data" : this . order . data
} ) ;
2012-05-09 05:10:01 -07:00
Engine . PostMessage ( this . entity , MT _UnitAIOrderDataChanged , { "to" : this . GetOrderData ( ) } ) ;
2011-05-15 16:51:51 -07:00
Partial revert of d038b3c4f2 / REJECT_ORDER with FinishOrder()
Fixes d038b3c4f2, by partially reverting it.
If the new order is rejected, PushOrder() will call FinishOrder. If
there are no more orders on the queue, this calls SetNextState("idle"),
with the intention os switching the unit to IDLE. However, this only
works from within an FSM call, and thus does nothing if a new command
gets rejected.
The problem is that this.order/this.orderQueue is already replaced by
the point the order is rejected when called via ReplaceOrder. Ideally,
this would not happen (but doing so isn't trivial).
The current code avoids having 2 different ways to reject an order, thus
isn't a complete revert of d038b3c4f2. It triggers an IDLE re-entry that
wasn't there before if the unit is IDLE when it receives the rejected
order, which at the moment basically never happens.
Refs #5771 (reopened)
Reported by: gameboy
Comments by: Freagarach, Angen
Differential Revision: https://code.wildfiregames.com/D3618
This was SVN commit r24978.
2021-03-02 07:18:39 -08:00
return ret ;
2010-07-08 13:08:08 -07:00
}
2012-12-01 17:52:27 -08:00
2018-04-09 09:36:41 -07:00
this . orderQueue = [ ] ;
this . order = undefined ;
2019-01-18 23:44:40 -08:00
2019-07-14 04:40:27 -07:00
// Switch to IDLE as a default state.
this . SetNextState ( "IDLE" ) ;
2014-03-11 15:42:41 -07:00
2018-03-20 11:20:10 -07:00
Engine . PostMessage ( this . entity , MT _UnitAIOrderDataChanged , { "to" : this . GetOrderData ( ) } ) ;
// Check if there are queued formation orders
if ( this . IsFormationMember ( ) )
{
2020-01-10 01:31:22 -08:00
this . SetNextState ( "FORMATIONMEMBER.IDLE" ) ;
2018-03-20 11:20:10 -07:00
let cmpUnitAI = Engine . QueryInterface ( this . formationController , IID _UnitAI ) ;
if ( cmpUnitAI )
2012-12-01 17:52:27 -08:00
{
2018-03-20 11:20:10 -07:00
// Inform the formation controller that we finished this task
2021-10-02 22:59:54 -07:00
Engine . QueryInterface ( this . formationController , IID _Formation ) .
SetFinishedEntity ( this . entity ) ;
2018-03-20 11:20:10 -07:00
// We don't want to carry out the default order
// if there are still queued formation orders left
if ( cmpUnitAI . GetOrders ( ) . length > 1 )
return true ;
2012-12-01 17:52:27 -08:00
}
2010-04-29 16:36:05 -07:00
}
2018-03-20 11:20:10 -07:00
return false ;
2010-02-12 14:46:53 -08:00
} ;
2010-02-05 14:00:39 -08:00
2010-07-21 09:09:58 -07:00
/ * *
* Add an order onto the back of the queue ,
* and execute it if we didn ' t already have an order .
* /
UnitAI . prototype . PushOrder = function ( type , data )
2010-03-12 13:41:40 -08:00
{
2025-01-01 02:33:07 -08:00
let wasIdle = this . isIdle && ! this . order ;
2010-07-21 09:09:58 -07:00
var order = { "type" : type , "data" : data } ;
this . orderQueue . push ( order ) ;
2010-03-12 13:41:40 -08:00
2010-07-21 09:09:58 -07:00
if ( this . orderQueue . length == 1 )
2010-04-29 16:36:05 -07:00
{
2010-07-21 09:09:58 -07:00
this . order = order ;
2021-03-20 02:02:22 -07:00
this . UnitFsm . ProcessMessage ( this , {
"type" : "Order." + this . order . type ,
"data" : this . order . data
} ) ;
2025-01-01 02:33:07 -08:00
// Check if the order appears to have been rejected
2025-05-03 08:30:55 -07:00
if ( wasIdle && ! this . order )
Fix eslint rule 'brace-on-same-line'
eslint --no-config-lookup --fix --plugin eslint-plugin-brace-rules \
--rule '"brace-rules/brace-on-same-line": [
"warn",
{
"FunctionDeclaration": "never",
"FunctionExpression": "ignore",
"ArrowFunctionExpression": "always",
"IfStatement": "never",
"TryStatement": "ignore",
"CatchClause": "ignore",
"DoWhileStatement": "never",
"WhileStatement": "never",
"ForStatement": "never",
"ForInStatement": "never",
"ForOfStatement": "never",
"SwitchStatement": "never",
},
{
"allowSingleLine": true,
}
]'
Ref: #7812
Signed-off-by: Ralph Sennhauser <ralph.sennhauser@gmail.com>
2025-05-03 07:13:38 -07:00
{
2025-01-01 02:33:07 -08:00
this . UnitFsm . ProcessMessage ( this , {
"type" : "ReenterIdle" ,
} ) ;
}
2010-04-29 16:36:05 -07:00
}
2015-12-20 13:07:47 -08:00
Engine . PostMessage ( this . entity , MT _UnitAIOrderDataChanged , { "to" : this . GetOrderData ( ) } ) ;
2010-03-12 13:41:40 -08:00
} ;
2010-07-21 09:09:58 -07:00
/ * *
* Add an order onto the front of the queue ,
* and execute it immediately .
* /
2019-12-28 03:40:24 -08:00
UnitAI . prototype . PushOrderFront = function ( type , data , ignorePacking = false )
2010-02-12 14:46:53 -08:00
{
2025-01-01 02:33:07 -08:00
if ( ! this . order )
return this . PushOrder ( type , data ) ;
2010-07-21 09:09:58 -07:00
var order = { "type" : type , "data" : data } ;
2020-09-10 09:37:14 -07:00
// If current order is packing/unpacking then add new order after it.
if ( ! ignorePacking && this . order && this . IsPacking ( ) )
2013-12-26 08:09:32 -08:00
{
var packingOrder = this . orderQueue . shift ( ) ;
2014-06-15 05:43:52 -07:00
this . orderQueue . unshift ( packingOrder , order ) ;
2013-12-26 08:09:32 -08:00
}
2011-05-02 08:03:01 -07:00
else
{
this . orderQueue . unshift ( order ) ;
this . order = order ;
2021-03-20 02:02:22 -07:00
this . UnitFsm . ProcessMessage ( this , {
"type" : "Order." + this . order . type ,
"data" : this . order . data
} ) ;
2011-05-02 08:03:01 -07:00
}
2015-12-20 13:07:47 -08:00
Engine . PostMessage ( this . entity , MT _UnitAIOrderDataChanged , { "to" : this . GetOrderData ( ) } ) ;
2010-02-10 11:28:46 -08:00
} ;
2010-02-05 14:00:39 -08:00
2013-11-08 15:22:59 -08:00
/ * *
* Insert an order after the last forced order onto the queue
* and after the other orders of the same type
* /
UnitAI . prototype . PushOrderAfterForced = function ( type , data )
{
if ( ! this . order || ( ( ! this . order . data || ! this . order . data . force ) && this . order . type != type ) )
this . PushOrderFront ( type , data ) ;
else
{
2015-12-20 13:07:47 -08:00
for ( let i = 1 ; i < this . orderQueue . length ; ++ i )
2013-11-08 15:22:59 -08:00
{
if ( this . orderQueue [ i ] . data && this . orderQueue [ i ] . data . force )
continue ;
if ( this . orderQueue [ i ] . type == type )
continue ;
2021-03-20 02:02:22 -07:00
this . orderQueue . splice ( i , 0 , { "type" : type , "data" : data } ) ;
2015-12-20 13:07:47 -08:00
Engine . PostMessage ( this . entity , MT _UnitAIOrderDataChanged , { "to" : this . GetOrderData ( ) } ) ;
2013-11-08 15:22:59 -08:00
return ;
}
this . PushOrder ( type , data ) ;
}
2015-12-20 13:07:47 -08:00
Engine . PostMessage ( this . entity , MT _UnitAIOrderDataChanged , { "to" : this . GetOrderData ( ) } ) ;
2013-11-08 15:22:59 -08:00
} ;
2019-12-28 03:40:24 -08:00
/ * *
* For a unit that is packing and trying to attack something ,
* either cancel packing or continue with packing , as appropriate .
* Precondition : if the unit is packing / unpacking , then orderQueue
* should have the Attack order at index 0 ,
* and the Pack / Unpack order at index 1.
* This precondition holds because if we are packing while processing "Order.Attack" ,
* then we must have come from ReplaceOrder , which guarantees it .
*
* @ param { boolean } requirePacked - true if the unit needs to be packed to continue attacking ,
* false if it needs to be unpacked .
* @ return { boolean } true if the unit can attack now , false if it must continue packing ( or unpacking ) first .
* /
UnitAI . prototype . EnsureCorrectPackStateForAttack = function ( requirePacked )
{
let cmpPack = Engine . QueryInterface ( this . entity , IID _Pack ) ;
if ( ! cmpPack ||
! cmpPack . IsPacking ( ) ||
this . orderQueue . length != 2 ||
this . orderQueue [ 0 ] . type != "Attack" ||
this . orderQueue [ 1 ] . type != "Pack" &&
this . orderQueue [ 1 ] . type != "Unpack" )
return true ;
if ( cmpPack . IsPacked ( ) == requirePacked )
{
// The unit is already in the packed/unpacked state we want.
// Delete the packing order.
this . orderQueue . splice ( 1 , 1 ) ;
cmpPack . CancelPack ( ) ;
Engine . PostMessage ( this . entity , MT _UnitAIOrderDataChanged , { "to" : this . GetOrderData ( ) } ) ;
// Continue with the attack order.
return true ;
}
// Move the attack order behind the unpacking order, to continue unpacking.
let tmp = this . orderQueue [ 0 ] ;
this . orderQueue [ 0 ] = this . orderQueue [ 1 ] ;
this . orderQueue [ 1 ] = tmp ;
Engine . PostMessage ( this . entity , MT _UnitAIOrderDataChanged , { "to" : this . GetOrderData ( ) } ) ;
return false ;
} ;
UnitAI . prototype . WillMoveFromFoundation = function ( target , checkPacking = true )
{
2021-02-27 12:13:40 -08:00
let cmpUnitAI = Engine . QueryInterface ( target , IID _UnitAI ) ;
if ( ! IsOwnedByAllyOfEntity ( this . entity , target ) && cmpUnitAI && ! cmpUnitAI . IsAnimal ( ) &&
2020-06-02 04:40:29 -07:00
! Engine . QueryInterface ( SYSTEM _ENTITY , IID _CeasefireManager ) . IsCeasefireActive ( ) ||
checkPacking && this . IsPacking ( ) || this . CanPack ( ) || ! this . AbleToMove ( ) )
2019-12-28 03:40:24 -08:00
return false ;
return ! this . CheckTargetRangeExplicit ( target , g _LeaveFoundationRange , - 1 ) ;
} ;
2010-07-21 09:09:58 -07:00
UnitAI . prototype . ReplaceOrder = function ( type , data )
2010-02-10 11:28:46 -08:00
{
2013-11-01 15:00:06 -07:00
// Remember the previous work orders to be able to go back to them later if required
if ( data && data . force )
{
if ( this . IsFormationController ( ) )
2014-01-11 04:19:43 -08:00
this . CallMemberFunction ( "UpdateWorkOrders" , [ type ] ) ;
2013-11-01 15:00:06 -07:00
else
this . UpdateWorkOrders ( type ) ;
}
2020-09-10 09:37:14 -07:00
// Do not replace packing/unpacking unless it is cancel order.
2012-11-30 16:34:03 -08:00
// TODO: maybe a better way of doing this would be to use priority levels
2020-11-16 01:33:09 -08:00
if ( this . IsPacking ( ) && type != "CancelPack" && type != "CancelUnpack" && type != "Stop" )
2012-11-30 16:34:03 -08:00
{
var order = { "type" : type , "data" : data } ;
var packingOrder = this . orderQueue . shift ( ) ;
2019-12-28 03:40:24 -08:00
if ( type == "Attack" )
{
// The Attack order is able to handle a packing unit, while other orders can't.
this . orderQueue = [ packingOrder ] ;
this . PushOrderFront ( type , data , true ) ;
}
else if ( packingOrder . type == "Unpack" && g _OrdersCancelUnpacking . has ( type ) )
{
// Immediately cancel unpacking before processing an order that demands a packed unit.
let cmpPack = Engine . QueryInterface ( this . entity , IID _Pack ) ;
cmpPack . CancelPack ( ) ;
this . orderQueue = [ ] ;
this . PushOrder ( type , data ) ;
}
else
this . orderQueue = [ packingOrder , order ] ;
2012-11-30 16:34:03 -08:00
}
2021-02-10 11:23:39 -08:00
else if ( this . IsFormationMember ( ) )
{
// Don't replace orders after a LeaveFormation order
// (this is needed to support queued no-formation orders).
let idx = this . orderQueue . findIndex ( o => o . type == "LeaveFormation" ) ;
if ( idx === - 1 )
2021-02-13 02:08:10 -08:00
{
2021-02-10 11:23:39 -08:00
this . orderQueue = [ ] ;
2021-02-13 02:08:10 -08:00
this . order = undefined ;
}
2021-02-10 11:23:39 -08:00
else
this . orderQueue . splice ( 0 , idx ) ;
this . PushOrderFront ( type , data ) ;
}
2011-05-02 08:03:01 -07:00
else
{
this . orderQueue = [ ] ;
this . PushOrder ( type , data ) ;
}
2017-07-04 10:57:20 -07:00
2015-12-20 13:07:47 -08:00
Engine . PostMessage ( this . entity , MT _UnitAIOrderDataChanged , { "to" : this . GetOrderData ( ) } ) ;
2010-02-10 11:28:46 -08:00
} ;
2011-05-02 08:03:01 -07:00
UnitAI . prototype . GetOrders = function ( )
{
return this . orderQueue . slice ( ) ;
2012-04-28 12:25:44 -07:00
} ;
2011-05-02 08:03:01 -07:00
UnitAI . prototype . AddOrders = function ( orders )
{
2015-08-29 15:49:44 -07:00
orders . forEach ( order => this . PushOrder ( order . type , order . data ) ) ;
2012-04-28 12:25:44 -07:00
} ;
2011-05-02 08:03:01 -07:00
2012-04-04 13:23:41 -07:00
UnitAI . prototype . GetOrderData = function ( )
{
2012-08-10 09:33:58 -07:00
var orders = [ ] ;
2015-09-09 11:21:17 -07:00
for ( let order of this . orderQueue )
if ( order . data )
2017-09-06 13:58:27 -07:00
orders . push ( clone ( order . data ) ) ;
2015-09-09 11:21:17 -07:00
2012-08-10 09:33:58 -07:00
return orders ;
2012-04-28 12:25:44 -07:00
} ;
2012-04-04 13:23:41 -07:00
2013-11-01 15:00:06 -07:00
UnitAI . prototype . UpdateWorkOrders = function ( type )
{
2015-08-29 15:49:44 -07:00
var isWorkType = type => type == "Gather" || type == "Trade" || type == "Repair" || type == "ReturnResource" ;
2013-11-01 15:00:06 -07:00
if ( isWorkType ( type ) )
{
this . workOrders = [ ] ;
return ;
}
2014-04-05 20:04:30 -07:00
2013-11-01 15:00:06 -07:00
if ( this . workOrders . length )
return ;
2014-04-05 20:04:30 -07:00
2013-11-01 15:00:06 -07:00
if ( this . IsFormationMember ( ) )
{
var cmpUnitAI = Engine . QueryInterface ( this . formationController , IID _UnitAI ) ;
2013-11-03 09:18:43 -08:00
if ( cmpUnitAI )
2013-11-01 15:00:06 -07:00
{
2013-11-03 09:18:43 -08:00
for ( var i = 0 ; i < cmpUnitAI . orderQueue . length ; ++ i )
2013-11-01 15:00:06 -07:00
{
2013-11-03 09:18:43 -08:00
if ( isWorkType ( cmpUnitAI . orderQueue [ i ] . type ) )
{
this . workOrders = cmpUnitAI . orderQueue . slice ( i ) ;
return ;
}
2013-11-01 15:00:06 -07:00
}
}
}
// If nothing found, take the unit orders
for ( var i = 0 ; i < this . orderQueue . length ; ++ i )
{
if ( isWorkType ( this . orderQueue [ i ] . type ) )
{
this . workOrders = this . orderQueue . slice ( i ) ;
return ;
}
}
} ;
UnitAI . prototype . BackToWork = function ( )
2014-04-05 20:04:30 -07:00
{
2013-11-01 15:00:06 -07:00
if ( this . workOrders . length == 0 )
return false ;
2014-04-05 20:04:30 -07:00
2021-12-09 08:35:03 -08:00
if ( this . isGarrisoned && ! Engine . QueryInterface ( this . entity , IID _Garrisonable ) ? . UnGarrison ( false ) )
return false ;
const cmpTurretable = Engine . QueryInterface ( this . entity , IID _Turretable ) ;
if ( this . IsTurret ( cmpTurretable ) && ! cmpTurretable . LeaveTurret ( ) )
return false ;
2017-07-04 10:57:20 -07:00
2020-09-10 09:37:14 -07:00
this . orderQueue = [ ] ;
2014-04-05 20:04:30 -07:00
2013-11-01 15:00:06 -07:00
this . AddOrders ( this . workOrders ) ;
2015-12-20 13:07:47 -08:00
Engine . PostMessage ( this . entity , MT _UnitAIOrderDataChanged , { "to" : this . GetOrderData ( ) } ) ;
2014-04-05 20:04:30 -07:00
2013-11-01 15:00:06 -07:00
if ( this . IsFormationMember ( ) )
2013-11-03 09:18:43 -08:00
{
var cmpFormation = Engine . QueryInterface ( this . formationController , IID _Formation ) ;
if ( cmpFormation )
cmpFormation . RemoveMembers ( [ this . entity ] ) ;
}
2014-04-05 20:04:30 -07:00
2013-11-01 15:00:06 -07:00
this . workOrders = [ ] ;
return true ;
} ;
UnitAI . prototype . HasWorkOrders = function ( )
{
return this . workOrders . length > 0 ;
} ;
UnitAI . prototype . GetWorkOrders = function ( )
2013-10-11 12:11:07 -07:00
{
2013-11-01 15:00:06 -07:00
return this . workOrders ;
2013-10-11 12:11:07 -07:00
} ;
2013-11-01 15:00:06 -07:00
UnitAI . prototype . SetWorkOrders = function ( orders )
2013-10-11 12:11:07 -07:00
{
2013-11-01 15:00:06 -07:00
this . workOrders = orders ;
2013-10-11 12:11:07 -07:00
} ;
2010-07-21 09:09:58 -07:00
UnitAI . prototype . TimerHandler = function ( data , lateness )
2010-02-10 11:28:46 -08:00
{
2010-07-21 09:09:58 -07:00
// Reset the timer
2011-02-05 12:35:34 -08:00
if ( data . timerRepeat === undefined )
this . timer = undefined ;
2010-03-12 13:41:40 -08:00
2021-03-20 02:02:22 -07:00
this . UnitFsm . ProcessMessage ( this , { "type" : "Timer" , "data" : data , "lateness" : lateness } ) ;
2010-07-21 09:09:58 -07:00
} ;
2010-02-12 14:46:53 -08:00
2010-12-08 08:12:04 -08:00
/ * *
* Set up the UnitAI timer to run after 'offset' msecs , and then
* every 'repeat' msecs until StopTimer is called . A "Timer" message
* will be sent each time the timer runs .
* /
2010-07-21 09:09:58 -07:00
UnitAI . prototype . StartTimer = function ( offset , repeat )
{
if ( this . timer )
error ( "Called StartTimer when there's already an active timer" ) ;
2010-02-12 14:46:53 -08:00
2010-12-08 08:12:04 -08:00
var data = { "timerRepeat" : repeat } ;
2010-07-21 09:09:58 -07:00
var cmpTimer = Engine . QueryInterface ( SYSTEM _ENTITY , IID _Timer ) ;
2012-05-09 05:14:22 -07:00
if ( repeat === undefined )
this . timer = cmpTimer . SetTimeout ( this . entity , IID _UnitAI , "TimerHandler" , offset , data ) ;
else
this . timer = cmpTimer . SetInterval ( this . entity , IID _UnitAI , "TimerHandler" , offset , repeat , data ) ;
2010-02-07 12:06:16 -08:00
} ;
2010-12-08 08:12:04 -08:00
/ * *
* Stop the current UnitAI timer .
* /
2010-07-21 09:09:58 -07:00
UnitAI . prototype . StopTimer = function ( )
2010-04-29 16:36:05 -07:00
{
2010-07-21 09:09:58 -07:00
if ( ! this . timer )
return ;
2010-04-29 16:36:05 -07:00
var cmpTimer = Engine . QueryInterface ( SYSTEM _ENTITY , IID _Timer ) ;
2010-07-21 09:09:58 -07:00
cmpTimer . CancelTimer ( this . timer ) ;
this . timer = undefined ;
} ;
2010-04-29 16:36:05 -07:00
2019-07-22 11:07:24 -07:00
UnitAI . prototype . OnMotionUpdate = function ( msg )
2010-02-12 14:46:53 -08:00
{
2021-01-05 02:12:47 -08:00
if ( msg . veryObstructed )
msg . obstructed = true ;
2019-07-22 11:07:24 -07:00
this . UnitFsm . ProcessMessage ( this , Object . assign ( { "type" : "MovementUpdate" } , msg ) ) ;
2010-04-29 16:36:05 -07:00
} ;
2010-02-12 14:46:53 -08:00
2020-08-03 03:13:54 -07:00
/ * *
* Called directly by cmpFoundation and cmpRepairable to
* inform builders that repairing has finished .
* This not done by listening to a global message due to performance .
* /
UnitAI . prototype . ConstructionFinished = function ( msg )
2010-02-12 14:46:53 -08:00
{
2020-08-03 03:13:54 -07:00
this . UnitFsm . ProcessMessage ( this , { "type" : "ConstructionFinished" , "data" : msg } ) ;
2010-04-29 16:36:05 -07:00
} ;
2010-02-12 14:46:53 -08:00
2011-05-02 08:03:01 -07:00
UnitAI . prototype . OnGlobalEntityRenamed = function ( msg )
{
2015-12-20 13:07:47 -08:00
let changed = false ;
2020-06-05 23:19:15 -07:00
let currentOrderChanged = false ;
for ( let i = 0 ; i < this . orderQueue . length ; ++ i )
2014-01-22 05:20:55 -08:00
{
2020-06-05 23:19:15 -07:00
let order = this . orderQueue [ i ] ;
2013-04-10 15:48:02 -07:00
if ( order . data && order . data . target && order . data . target == msg . entity )
2015-12-20 13:07:47 -08:00
{
changed = true ;
2020-06-05 23:19:15 -07:00
if ( i == 0 )
currentOrderChanged = true ;
2013-04-10 15:48:02 -07:00
order . data . target = msg . newentity ;
2015-12-20 13:07:47 -08:00
}
2014-01-22 05:20:55 -08:00
if ( order . data && order . data . formationTarget && order . data . formationTarget == msg . entity )
2015-12-20 13:07:47 -08:00
{
changed = true ;
2020-06-05 23:19:15 -07:00
if ( i == 0 )
currentOrderChanged = true ;
2014-01-22 05:20:55 -08:00
order . data . formationTarget = msg . newentity ;
2015-12-20 13:07:47 -08:00
}
2014-01-22 05:20:55 -08:00
}
2020-06-05 23:19:15 -07:00
if ( ! changed )
return ;
2020-05-29 10:00:50 -07:00
2020-06-05 23:19:15 -07:00
if ( currentOrderChanged )
this . UnitFsm . ProcessMessage ( this , { "type" : "OrderTargetRenamed" , "data" : msg } ) ;
Engine . PostMessage ( this . entity , MT _UnitAIOrderDataChanged , { "to" : this . GetOrderData ( ) } ) ;
2012-04-28 12:25:44 -07:00
} ;
2011-05-02 08:03:01 -07:00
2010-07-21 09:09:58 -07:00
UnitAI . prototype . OnAttacked = function ( msg )
2010-02-12 14:46:53 -08:00
{
2020-01-27 08:51:25 -08:00
if ( msg . fromStatusEffect )
return ;
2021-03-20 02:02:22 -07:00
this . UnitFsm . ProcessMessage ( this , { "type" : "Attacked" , "data" : msg } ) ;
2010-07-21 09:09:58 -07:00
} ;
2010-02-12 14:46:53 -08:00
2013-11-30 09:30:08 -08:00
UnitAI . prototype . OnGuardedAttacked = function ( msg )
{
2021-03-20 02:02:22 -07:00
this . UnitFsm . ProcessMessage ( this , { "type" : "GuardedAttacked" , "data" : msg . data } ) ;
2013-11-30 09:30:08 -08:00
} ;
2010-07-29 13:39:23 -07:00
UnitAI . prototype . OnRangeUpdate = function ( msg )
{
if ( msg . tag == this . losRangeQuery )
2020-07-23 23:07:27 -07:00
this . UnitFsm . ProcessMessage ( this , { "type" : "LosRangeUpdate" , "data" : msg } ) ;
2012-04-17 13:22:13 -07:00
else if ( msg . tag == this . losHealRangeQuery )
2020-07-23 23:07:27 -07:00
this . UnitFsm . ProcessMessage ( this , { "type" : "LosHealRangeUpdate" , "data" : msg } ) ;
else if ( msg . tag == this . losAttackRangeQuery )
this . UnitFsm . ProcessMessage ( this , { "type" : "LosAttackRangeUpdate" , "data" : msg } ) ;
2010-07-29 13:39:23 -07:00
} ;
2012-11-30 16:34:03 -08:00
UnitAI . prototype . OnPackFinished = function ( msg )
{
2021-03-20 02:02:22 -07:00
this . UnitFsm . ProcessMessage ( this , { "type" : "PackFinished" , "packed" : msg . packed } ) ;
2012-11-30 16:34:03 -08:00
} ;
2021-03-02 23:47:38 -08:00
/ * *
* A general function to process messages sent from components .
* @ param { string } type - The type of message to process .
* @ param { Object } msg - Optionally extra data to use .
* /
UnitAI . prototype . ProcessMessage = function ( type , msg )
{
this . UnitFsm . ProcessMessage ( this , { "type" : type , "data" : msg } ) ;
} ;
2021-03-20 02:02:22 -07:00
// Helper functions to be called by the FSM
2010-03-12 13:41:40 -08:00
2010-07-21 09:09:58 -07:00
UnitAI . prototype . GetWalkSpeed = function ( )
{
2019-04-19 03:04:50 -07:00
let cmpUnitMotion = Engine . QueryInterface ( this . entity , IID _UnitMotion ) ;
if ( ! cmpUnitMotion )
return 0 ;
2010-11-30 04:31:54 -08:00
return cmpUnitMotion . GetWalkSpeed ( ) ;
2010-02-12 14:46:53 -08:00
} ;
2019-04-19 03:04:50 -07:00
UnitAI . prototype . GetRunMultiplier = function ( )
2010-03-12 13:41:40 -08:00
{
2010-11-30 04:31:54 -08:00
var cmpUnitMotion = Engine . QueryInterface ( this . entity , IID _UnitMotion ) ;
2019-04-19 03:04:50 -07:00
if ( ! cmpUnitMotion )
return 0 ;
2019-05-13 09:47:51 -07:00
return cmpUnitMotion . GetRunMultiplier ( ) ;
2010-07-21 09:09:58 -07:00
} ;
2010-02-10 11:28:46 -08:00
2010-10-13 08:14:15 -07:00
/ * *
* Returns true if the target exists and has non - zero hitpoints .
* /
2010-10-06 14:37:55 -07:00
UnitAI . prototype . TargetIsAlive = function ( ent )
{
2014-01-21 08:50:58 -08:00
var cmpFormation = Engine . QueryInterface ( ent , IID _Formation ) ;
if ( cmpFormation )
return true ;
2015-05-01 10:03:37 -07:00
var cmpHealth = QueryMiragedInterface ( ent , IID _Health ) ;
return cmpHealth && cmpHealth . GetHitpoints ( ) != 0 ;
2010-10-06 14:37:55 -07:00
} ;
2010-10-13 08:14:15 -07:00
/ * *
* Returns true if the target exists and needs to be killed before
* beginning to gather resources from it .
* /
UnitAI . prototype . MustKillGatherTarget = function ( ent )
{
var cmpResourceSupply = Engine . QueryInterface ( ent , IID _ResourceSupply ) ;
if ( ! cmpResourceSupply )
return false ;
if ( ! cmpResourceSupply . GetKillBeforeGather ( ) )
return false ;
return this . TargetIsAlive ( ent ) ;
} ;
2020-06-04 04:06:27 -07:00
/ * *
* Returns the position of target or , if there is none ,
* the entity ' s position , or undefined .
* /
UnitAI . prototype . TargetPosOrEntPos = function ( target )
{
let cmpTargetPosition = Engine . QueryInterface ( target , IID _Position ) ;
if ( cmpTargetPosition && cmpTargetPosition . IsInWorld ( ) )
return cmpTargetPosition . GetPosition2D ( ) ;
let cmpPosition = Engine . QueryInterface ( this . entity , IID _Position ) ;
if ( cmpPosition && cmpPosition . IsInWorld ( ) )
return cmpPosition . GetPosition2D ( ) ;
return undefined ;
} ;
2010-11-30 04:31:54 -08:00
/ * *
2010-12-08 08:12:04 -08:00
* Returns the entity ID of the nearest resource supply where the given
* filter returns true , or undefined if none can be found .
2020-06-04 04:06:27 -07:00
* "Nearest" is nearest from @ param position .
* TODO : extend this to exclude resources that already have lots of gatherers .
2010-11-30 04:31:54 -08:00
* /
2020-06-04 04:06:27 -07:00
UnitAI . prototype . FindNearbyResource = function ( position , filter )
2010-11-30 04:31:54 -08:00
{
2020-06-04 04:06:27 -07:00
if ( ! position )
return undefined ;
2012-04-10 14:09:21 -07:00
// We accept resources owned by Gaia or any player
2019-07-03 10:57:16 -07:00
let players = Engine . QueryInterface ( SYSTEM _ENTITY , IID _PlayerManager ) . GetAllPlayers ( ) ;
2010-11-30 04:31:54 -08:00
2019-07-03 10:57:16 -07:00
let range = 64 ; // TODO: what's a sensible number?
2015-08-29 15:49:46 -07:00
2019-07-03 10:57:16 -07:00
let cmpTemplateManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _TemplateManager ) ;
let cmpRangeManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _RangeManager ) ;
2021-01-23 10:57:46 -08:00
// Don't account for entity size, we need to match LOS visibility.
let nearby = cmpRangeManager . ExecuteQueryAroundPos ( position , 0 , range , players , IID _ResourceSupply , false ) ;
2015-08-29 15:49:46 -07:00
return nearby . find ( ent => {
2018-01-21 18:49:57 -08:00
if ( ! this . CanGather ( ent ) || ! this . CheckTargetVisible ( ent ) )
2015-08-29 15:49:46 -07:00
return false ;
2012-08-19 18:38:39 -07:00
2019-07-03 10:57:16 -07:00
let template = cmpTemplateManager . GetCurrentTemplateName ( ent ) ;
2012-08-19 18:38:39 -07:00
if ( template . indexOf ( "resource|" ) != - 1 )
template = template . slice ( 9 ) ;
2021-04-12 05:50:17 -07:00
let cmpResourceSupply = Engine . QueryInterface ( ent , IID _ResourceSupply ) ;
let type = cmpResourceSupply . GetType ( ) ;
return cmpResourceSupply . IsAvailableTo ( this . entity ) && filter ( ent , type , template ) ;
2015-08-29 15:49:46 -07:00
} ) ;
2010-11-30 04:31:54 -08:00
} ;
2010-12-08 08:12:04 -08:00
/ * *
* Returns the entity ID of the nearest resource dropsite that accepts
* the given type , or undefined if none can be found .
* /
UnitAI . prototype . FindNearestDropsite = function ( genericType )
{
2018-01-21 13:21:21 -08:00
let cmpOwnership = Engine . QueryInterface ( this . entity , IID _Ownership ) ;
2018-01-21 17:02:29 -08:00
if ( ! cmpOwnership || cmpOwnership . GetOwner ( ) == INVALID _PLAYER )
2015-08-29 15:49:46 -07:00
return undefined ;
2019-07-01 04:09:19 -07:00
let cmpPosition = Engine . QueryInterface ( this . entity , IID _Position ) ;
2018-02-19 10:17:00 -08:00
if ( ! cmpPosition || ! cmpPosition . IsInWorld ( ) )
2018-01-21 13:21:21 -08:00
return undefined ;
2010-12-08 08:12:04 -08:00
2018-01-21 13:21:21 -08:00
let pos = cmpPosition . GetPosition2D ( ) ;
let bestDropsite ;
let bestDist = Infinity ;
// Maximum distance a point on an obstruction can be from the center of the obstruction.
let maxDifference = 40 ;
let owner = cmpOwnership . GetOwner ( ) ;
2023-06-18 23:33:33 -07:00
let cmpDiplomacy = QueryOwnerInterface ( this . entity , IID _Diplomacy ) ;
let players = cmpDiplomacy && cmpDiplomacy . HasSharedDropsites ( ) ? cmpDiplomacy . GetMutualAllies ( ) : [ owner ] ;
2021-01-23 10:57:46 -08:00
let nearestDropsites = Engine . QueryInterface ( SYSTEM _ENTITY , IID _RangeManager ) . ExecuteQuery ( this . entity , 0 , - 1 , players , IID _ResourceDropsite , false ) ;
2018-01-21 13:21:21 -08:00
let isShip = Engine . QueryInterface ( this . entity , IID _Identity ) . HasClass ( "Ship" ) ;
let cmpObstructionManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _ObstructionManager ) ;
2020-02-13 12:24:51 -08:00
for ( let dropsite of nearestDropsites )
2018-01-21 13:21:21 -08:00
{
// Ships are unable to reach land dropsites and shouldn't attempt to do so.
if ( isShip && ! Engine . QueryInterface ( dropsite , IID _Identity ) . HasClass ( "Naval" ) )
continue ;
2010-12-08 08:12:04 -08:00
2018-01-21 13:21:21 -08:00
let cmpResourceDropsite = Engine . QueryInterface ( dropsite , IID _ResourceDropsite ) ;
2018-01-21 18:49:57 -08:00
if ( ! cmpResourceDropsite . AcceptsType ( genericType ) || ! this . CheckTargetVisible ( dropsite ) )
2018-01-21 13:21:21 -08:00
continue ;
if ( Engine . QueryInterface ( dropsite , IID _Ownership ) . GetOwner ( ) != owner && ! cmpResourceDropsite . IsShared ( ) )
continue ;
// The range manager sorts entities by the distance to their center,
// but we want the distance to the point where resources will be dropped off.
let dist = cmpObstructionManager . DistanceToPoint ( dropsite , pos . x , pos . y ) ;
if ( dist == - 1 )
continue ;
if ( dist < bestDist )
{
bestDropsite = dropsite ;
bestDist = dist ;
}
else if ( dist > bestDist + maxDifference )
break ;
}
return bestDropsite ;
2010-12-08 08:12:04 -08:00
} ;
2011-03-05 09:38:15 -08:00
/ * *
2020-06-04 04:06:27 -07:00
* Returns the entity ID of the nearest building that needs to be constructed .
* "Nearest" is nearest from @ param position .
2011-03-05 09:38:15 -08:00
* /
2020-06-04 04:06:27 -07:00
UnitAI . prototype . FindNearbyFoundation = function ( position )
2011-03-05 09:38:15 -08:00
{
2020-06-04 04:06:27 -07:00
if ( ! position )
return undefined ;
let cmpOwnership = Engine . QueryInterface ( this . entity , IID _Ownership ) ;
2018-01-21 17:02:29 -08:00
if ( ! cmpOwnership || cmpOwnership . GetOwner ( ) == INVALID _PLAYER )
2015-08-29 15:49:46 -07:00
return undefined ;
2011-03-05 09:38:15 -08:00
2020-06-04 04:06:27 -07:00
let players = [ cmpOwnership . GetOwner ( ) ] ;
2011-03-05 09:38:15 -08:00
2020-06-04 04:06:27 -07:00
let range = 64 ; // TODO: what's a sensible number?
let cmpRangeManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _RangeManager ) ;
2021-01-23 10:57:46 -08:00
// Don't account for entity size, we need to match LOS visibility.
let nearby = cmpRangeManager . ExecuteQueryAroundPos ( position , 0 , range , players , IID _Foundation , false ) ;
2015-08-29 15:49:46 -07:00
// Skip foundations that are already complete. (This matters since
// we process the ConstructionFinished message before the foundation
// we're working on has been deleted.)
2021-10-02 22:43:56 -07:00
return nearby . find ( ent => ! Engine . QueryInterface ( ent , IID _Foundation ) . IsFinished ( ) && this . CheckTargetVisible ( ent ) ) ;
2011-03-05 09:38:15 -08:00
} ;
2021-03-03 10:20:49 -08:00
/ * *
* Returns the entity ID of the nearest treasure .
* "Nearest" is nearest from @ param position .
* /
UnitAI . prototype . FindNearbyTreasure = function ( position )
{
if ( ! position )
return undefined ;
2021-05-18 09:51:44 -07:00
let cmpTreasureCollector = Engine . QueryInterface ( this . entity , IID _TreasureCollector ) ;
if ( ! cmpTreasureCollector )
2021-03-03 10:20:49 -08:00
return undefined ;
let players = Engine . QueryInterface ( SYSTEM _ENTITY , IID _PlayerManager ) . GetAllPlayers ( ) ;
let range = 64 ; // TODO: what's a sensible number?
let cmpRangeManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _RangeManager ) ;
// Don't account for entity size, we need to match LOS visibility.
let nearby = cmpRangeManager . ExecuteQueryAroundPos ( position , 0 , range , players , IID _Treasure , false ) ;
2021-10-02 22:43:56 -07:00
return nearby . find ( ent => cmpTreasureCollector . CanCollect ( ent ) && this . CheckTargetVisible ( ent ) ) ;
2021-03-03 10:20:49 -08:00
} ;
2010-10-03 10:58:49 -07:00
/ * *
* Play a sound appropriate to the current entity .
* /
2010-07-21 09:09:58 -07:00
UnitAI . prototype . PlaySound = function ( name )
{
2010-10-03 10:58:49 -07:00
if ( this . IsFormationController ( ) )
{
var cmpFormation = Engine . QueryInterface ( this . entity , IID _Formation ) ;
var member = cmpFormation . GetPrimaryMember ( ) ;
if ( member )
PlaySound ( name , member ) ;
}
else
{
PlaySound ( name , this . entity ) ;
}
2010-02-10 11:28:46 -08:00
} ;
2017-12-10 02:06:08 -08:00
/ *
* Set a visualActor animation variant .
* By changing the animation variant , you can change animations based on unitAI state .
* If there are no specific variants or the variant doesn ' t exist in the actor ,
* the actor fallbacks to any existing animation .
* @ param type if present , switch to a specific animation variant .
* /
UnitAI . prototype . SetAnimationVariant = function ( type )
2012-12-06 11:46:13 -08:00
{
2017-12-10 02:06:08 -08:00
let cmpVisual = Engine . QueryInterface ( this . entity , IID _Visual ) ;
2012-12-06 11:46:13 -08:00
if ( ! cmpVisual )
return ;
2017-12-10 02:06:08 -08:00
cmpVisual . SetVariant ( "animationVariant" , type ) ;
} ;
/ *
2020-01-09 12:53:13 -08:00
* Reset the animation variant to default behavior .
2017-12-10 02:06:08 -08:00
* Default behavior is to pick a resource - carrying variant if resources are being carried .
* Otherwise pick nothing in particular .
* /
UnitAI . prototype . SetDefaultAnimationVariant = function ( )
{
let cmpResourceGatherer = Engine . QueryInterface ( this . entity , IID _ResourceGatherer ) ;
2020-01-09 12:53:13 -08:00
if ( cmpResourceGatherer )
2012-12-06 11:46:13 -08:00
{
2020-01-09 12:53:13 -08:00
let type = cmpResourceGatherer . GetLastCarriedType ( ) ;
if ( type )
{
let typename = "carry_" + type . generic ;
2012-12-06 11:46:13 -08:00
2020-01-09 12:53:13 -08:00
if ( type . specific == "meat" )
typename = "carry_" + type . specific ;
2012-12-06 11:46:13 -08:00
2020-01-09 12:53:13 -08:00
this . SetAnimationVariant ( typename ) ;
return ;
}
2012-12-06 11:46:13 -08:00
}
2017-12-10 02:06:08 -08:00
this . SetAnimationVariant ( "" ) ;
2015-09-21 14:08:58 -07:00
} ;
2012-12-06 11:46:13 -08:00
2019-07-09 12:56:28 -07:00
UnitAI . prototype . ResetAnimation = function ( )
2010-04-05 16:09:34 -07:00
{
2018-02-24 10:13:03 -08:00
let cmpVisual = Engine . QueryInterface ( this . entity , IID _Visual ) ;
2010-04-05 16:09:34 -07:00
if ( ! cmpVisual )
return ;
2019-07-09 12:56:28 -07:00
cmpVisual . SelectAnimation ( "idle" , false , 1.0 ) ;
} ;
UnitAI . prototype . SelectAnimation = function ( name , once = false , speed = 1.0 )
{
let cmpVisual = Engine . QueryInterface ( this . entity , IID _Visual ) ;
if ( ! cmpVisual )
2010-09-03 02:55:14 -07:00
return ;
2018-02-24 10:13:03 -08:00
cmpVisual . SelectAnimation ( name , once , speed ) ;
2010-04-05 16:09:34 -07:00
} ;
UnitAI . prototype . SetAnimationSync = function ( actiontime , repeattime )
2010-02-07 12:06:16 -08:00
{
var cmpVisual = Engine . QueryInterface ( this . entity , IID _Visual ) ;
if ( ! cmpVisual )
return ;
2010-06-05 15:23:28 -07:00
cmpVisual . SetAnimationSyncRepeat ( repeattime ) ;
cmpVisual . SetAnimationSyncOffset ( actiontime ) ;
2010-02-07 12:06:16 -08:00
} ;
2010-11-30 04:31:54 -08:00
UnitAI . prototype . StopMoving = function ( )
{
2023-06-14 23:56:41 -07:00
const cmpUnitMotion = Engine . QueryInterface ( this . entity , IID _UnitMotion ) ;
if ( ! cmpUnitMotion )
return ;
cmpUnitMotion . StopMoving ( ) ;
2023-07-22 07:04:59 -07:00
// Formations misuse the speed multiplier for adapting its walk speed to their members.
if ( ! this . IsFormationController ( ) )
cmpUnitMotion . SetSpeedMultiplier ( 1 ) ;
2010-11-30 04:31:54 -08:00
} ;
2019-05-28 04:38:18 -07:00
/ * *
* Generic dispatcher for other MoveTo functions .
* @ param iid - Interface ID ( optional ) implementing GetRange
* @ param type - Range type for the interface call
* @ returns whether the move succeeded or failed .
* /
UnitAI . prototype . MoveTo = function ( data , iid , type )
{
2019-06-09 04:16:40 -07:00
if ( data . target )
2019-05-28 04:38:18 -07:00
{
2019-06-09 04:16:40 -07:00
if ( data . min || data . max )
2019-05-28 04:38:18 -07:00
return this . MoveToTargetRangeExplicit ( data . target , data . min || - 1 , data . max || - 1 ) ;
2019-06-09 04:16:40 -07:00
else if ( ! iid )
return this . MoveToTarget ( data . target ) ;
return this . MoveToTargetRange ( data . target , iid , type ) ;
2019-05-28 04:38:18 -07:00
}
2019-06-09 04:16:40 -07:00
else if ( data . min || data . max )
return this . MoveToPointRange ( data . x , data . z , data . min || - 1 , data . max || - 1 ) ;
return this . MoveToPoint ( data . x , data . z ) ;
} ;
2019-05-28 04:38:18 -07:00
2010-07-21 09:09:58 -07:00
UnitAI . prototype . MoveToPoint = function ( x , z )
2010-02-05 14:00:39 -08:00
{
2020-03-20 13:12:09 -07:00
let cmpUnitMotion = Engine . QueryInterface ( this . entity , IID _UnitMotion ) ;
2020-06-02 04:40:29 -07:00
return this . AbleToMove ( cmpUnitMotion ) && cmpUnitMotion . MoveToPointRange ( x , z , 0 , 0 ) ; // For point goals, allow a max range of 0.
2010-07-21 09:09:58 -07:00
} ;
2010-12-08 08:12:04 -08:00
UnitAI . prototype . MoveToPointRange = function ( x , z , rangeMin , rangeMax )
{
2020-03-20 13:12:09 -07:00
let cmpUnitMotion = Engine . QueryInterface ( this . entity , IID _UnitMotion ) ;
2020-06-02 04:40:29 -07:00
return this . AbleToMove ( cmpUnitMotion ) && cmpUnitMotion . MoveToPointRange ( x , z , rangeMin , rangeMax ) ;
2010-12-08 08:12:04 -08:00
} ;
2010-07-21 09:09:58 -07:00
UnitAI . prototype . MoveToTarget = function ( target )
{
2011-06-24 05:35:15 -07:00
if ( ! this . CheckTargetVisible ( target ) )
return false ;
2020-03-20 13:12:09 -07:00
let cmpUnitMotion = Engine . QueryInterface ( this . entity , IID _UnitMotion ) ;
2020-06-02 04:40:29 -07:00
return this . AbleToMove ( cmpUnitMotion ) && cmpUnitMotion . MoveToTargetRange ( target , 0 , 1 ) ;
2010-07-21 09:09:58 -07:00
} ;
UnitAI . prototype . MoveToTargetRange = function ( target , iid , type )
{
2020-06-02 04:40:29 -07:00
if ( ! this . CheckTargetVisible ( target ) )
2011-06-24 05:35:15 -07:00
return false ;
2021-03-30 04:17:46 -07:00
let range = this . GetRange ( iid , type , target ) ;
2020-01-30 13:05:59 -08:00
if ( ! range )
2013-11-11 07:03:22 -08:00
return false ;
2010-07-21 09:09:58 -07:00
2020-01-30 13:05:59 -08:00
let cmpUnitMotion = Engine . QueryInterface ( this . entity , IID _UnitMotion ) ;
2020-06-02 04:40:29 -07:00
return this . AbleToMove ( cmpUnitMotion ) && cmpUnitMotion . MoveToTargetRange ( target , range . min , range . max ) ;
2010-02-05 14:00:39 -08:00
} ;
2013-08-03 12:20:20 -07:00
/ * *
* Move unit so we hope the target is in the attack range
* for melee attacks , this goes straight to the default range checks
2013-08-13 08:11:17 -07:00
* for ranged attacks , the parabolic range is used
2013-08-03 12:20:20 -07:00
* /
2014-01-11 04:19:43 -08:00
UnitAI . prototype . MoveToTargetAttackRange = function ( target , type )
2013-08-03 12:20:20 -07:00
{
2014-01-22 05:20:55 -08:00
// for formation members, the formation will take care of the range check
if ( this . IsFormationMember ( ) )
{
2019-07-03 11:05:11 -07:00
let cmpFormationUnitAI = Engine . QueryInterface ( this . formationController , IID _UnitAI ) ;
2015-10-05 13:40:14 -07:00
if ( cmpFormationUnitAI && cmpFormationUnitAI . IsAttackingAsFormation ( ) )
2014-01-22 05:20:55 -08:00
return false ;
}
2022-02-15 12:31:55 -08:00
const cmpUnitMotion = Engine . QueryInterface ( this . entity , IID _UnitMotion ) ;
2020-06-02 04:40:29 -07:00
if ( ! this . AbleToMove ( cmpUnitMotion ) )
return false ;
2022-02-15 12:31:55 -08:00
const cmpFormation = Engine . QueryInterface ( target , IID _Formation ) ;
2014-01-21 08:50:58 -08:00
if ( cmpFormation )
target = cmpFormation . GetClosestMember ( this . entity ) ;
2015-10-05 13:40:14 -07:00
if ( type != "Ranged" )
2014-01-11 04:19:43 -08:00
return this . MoveToTargetRange ( target , IID _Attack , type ) ;
2015-02-05 18:11:39 -08:00
if ( ! this . CheckTargetVisible ( target ) )
2013-08-03 12:20:20 -07:00
return false ;
2015-02-05 18:11:39 -08:00
Rename "ElevationBonus" and "Delay" to "Origin" and "EffectDelay", respectively.
`ElevationBonus` is vague, as discussions proved. Therefore it is
renamed to `Origin`, which, describes better what the value stands for.
`Delay` is also quite vague, so renamed to `EffectDelay`.
Differential revision: https://code.wildfiregames.com/D2016
Comments by: @bb, @nani, @Nescio, @Silier, @Stan, @wraitii
This was SVN commit r26074.
2021-12-14 23:42:06 -08:00
const cmpAttack = Engine . QueryInterface ( this . entity , IID _Attack ) ;
if ( ! cmpAttack )
2020-01-30 13:05:59 -08:00
return false ;
Rename "ElevationBonus" and "Delay" to "Origin" and "EffectDelay", respectively.
`ElevationBonus` is vague, as discussions proved. Therefore it is
renamed to `Origin`, which, describes better what the value stands for.
`Delay` is also quite vague, so renamed to `EffectDelay`.
Differential revision: https://code.wildfiregames.com/D2016
Comments by: @bb, @nani, @Nescio, @Silier, @Stan, @wraitii
This was SVN commit r26074.
2021-12-14 23:42:06 -08:00
const range = cmpAttack . GetRange ( type ) ;
2013-08-03 12:20:20 -07:00
2022-02-15 12:31:55 -08:00
// In case the range returns negative, we are probably too high compared to the target. Hope we come close enough.
const parabolicMaxRange = Math . max ( 0 , Engine . QueryInterface ( SYSTEM _ENTITY , IID _RangeManager ) . GetEffectiveParabolicRange ( this . entity , target , range . max , cmpAttack . GetAttackYOrigin ( type ) ) ) ;
2013-08-03 12:20:20 -07:00
2019-07-03 11:05:11 -07:00
// The parabole changes while walking so be cautious:
2022-02-15 12:31:55 -08:00
const guessedMaxRange = parabolicMaxRange > range . max ? ( range . max + parabolicMaxRange ) / 2 : parabolicMaxRange ;
2013-08-13 08:11:17 -07:00
2020-03-20 13:12:09 -07:00
return cmpUnitMotion && cmpUnitMotion . MoveToTargetRange ( target , range . min , guessedMaxRange ) ;
2013-08-03 12:20:20 -07:00
} ;
2011-02-10 08:06:28 -08:00
UnitAI . prototype . MoveToTargetRangeExplicit = function ( target , min , max )
{
2011-06-24 05:35:15 -07:00
if ( ! this . CheckTargetVisible ( target ) )
return false ;
2020-03-20 13:12:09 -07:00
let cmpUnitMotion = Engine . QueryInterface ( this . entity , IID _UnitMotion ) ;
2020-06-02 04:40:29 -07:00
return this . AbleToMove ( cmpUnitMotion ) && cmpUnitMotion . MoveToTargetRange ( target , min , max ) ;
2011-02-10 08:06:28 -08:00
} ;
2020-04-07 10:35:25 -07:00
/ * *
* Move unit so we hope the target is in the attack range of the formation .
*
* @ param { number } target - The target entity ID to attack .
* @ return { boolean } - Whether the order to move has succeeded .
* /
UnitAI . prototype . MoveFormationToTargetAttackRange = function ( target )
{
let cmpTargetFormation = Engine . QueryInterface ( target , IID _Formation ) ;
if ( cmpTargetFormation )
target = cmpTargetFormation . GetClosestMember ( this . entity ) ;
2020-06-02 04:40:29 -07:00
if ( ! this . CheckTargetVisible ( target ) )
2020-04-07 10:35:25 -07:00
return false ;
let cmpFormationAttack = Engine . QueryInterface ( this . entity , IID _Attack ) ;
if ( ! cmpFormationAttack )
return false ;
let range = cmpFormationAttack . GetRange ( target ) ;
let cmpUnitMotion = Engine . QueryInterface ( this . entity , IID _UnitMotion ) ;
2020-06-02 04:40:29 -07:00
return this . AbleToMove ( cmpUnitMotion ) && cmpUnitMotion . MoveToTargetRange ( target , range . min , range . max ) ;
2020-04-07 10:35:25 -07:00
} ;
2019-06-09 04:16:40 -07:00
/ * *
* Generic dispatcher for other Check ... Range functions .
* @ param iid - Interface ID ( optional ) implementing GetRange
* @ param type - Range type for the interface call
* /
UnitAI . prototype . CheckRange = function ( data , iid , type )
{
if ( data . target )
{
if ( data . min || data . max )
return this . CheckTargetRangeExplicit ( data . target , data . min || - 1 , data . max || - 1 ) ;
else if ( ! iid )
2019-07-17 11:11:15 -07:00
return this . CheckTargetRangeExplicit ( data . target , 0 , 1 ) ;
2019-06-09 04:16:40 -07:00
return this . CheckTargetRange ( data . target , iid , type ) ;
}
else if ( data . min || data . max )
return this . CheckPointRangeExplicit ( data . x , data . z , data . min || - 1 , data . max || - 1 ) ;
return this . CheckPointRangeExplicit ( data . x , data . z , 0 , 0 ) ;
} ;
2012-12-02 09:25:23 -08:00
UnitAI . prototype . CheckPointRangeExplicit = function ( x , z , min , max )
{
2019-06-06 12:37:23 -07:00
let cmpObstructionManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _ObstructionManager ) ;
2019-06-30 12:05:04 -07:00
return cmpObstructionManager . IsInPointRange ( this . entity , x , z , min , max , false ) ;
2012-12-02 09:25:23 -08:00
} ;
2010-07-21 09:09:58 -07:00
UnitAI . prototype . CheckTargetRange = function ( target , iid , type )
2010-02-05 14:00:39 -08:00
{
2021-03-30 04:17:46 -07:00
let range = this . GetRange ( iid , type , target ) ;
2020-01-30 13:05:59 -08:00
if ( ! range )
2013-11-11 07:03:22 -08:00
return false ;
2010-02-05 14:00:39 -08:00
2019-06-06 12:37:23 -07:00
let cmpObstructionManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _ObstructionManager ) ;
2019-06-30 12:05:04 -07:00
return cmpObstructionManager . IsInTargetRange ( this . entity , target , range . min , range . max , false ) ;
2010-07-21 09:09:58 -07:00
} ;
2010-02-05 14:00:39 -08:00
2013-08-03 12:20:20 -07:00
/ * *
2015-02-05 18:11:39 -08:00
* Check if the target is inside the attack range
2013-08-03 12:20:20 -07:00
* For melee attacks , this goes straigt to the regular range calculation
* For ranged attacks , the parabolic formula is used to accout for bigger ranges
* when the target is lower , and smaller ranges when the target is higher
2015-02-05 18:11:39 -08:00
* /
2014-01-11 04:19:43 -08:00
UnitAI . prototype . CheckTargetAttackRange = function ( target , type )
2013-08-03 12:20:20 -07:00
{
2014-01-22 05:20:55 -08:00
// for formation members, the formation will take care of the range check
if ( this . IsFormationMember ( ) )
{
2019-07-03 11:05:11 -07:00
let cmpFormationUnitAI = Engine . QueryInterface ( this . formationController , IID _UnitAI ) ;
if ( cmpFormationUnitAI && cmpFormationUnitAI . IsAttackingAsFormation ( ) &&
cmpFormationUnitAI . order . data . target == target )
2014-01-22 05:20:55 -08:00
return true ;
}
2019-07-03 11:05:11 -07:00
let cmpFormation = Engine . QueryInterface ( target , IID _Formation ) ;
2014-01-21 08:50:58 -08:00
if ( cmpFormation )
target = cmpFormation . GetClosestMember ( this . entity ) ;
2013-08-03 12:20:20 -07:00
2021-04-11 23:51:39 -07:00
let cmpAttack = Engine . QueryInterface ( this . entity , IID _Attack ) ;
return cmpAttack && cmpAttack . IsTargetInRange ( target , type ) ;
2013-08-03 12:20:20 -07:00
} ;
2012-05-18 14:31:57 -07:00
UnitAI . prototype . CheckTargetRangeExplicit = function ( target , min , max )
{
2019-06-06 12:37:23 -07:00
let cmpObstructionManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _ObstructionManager ) ;
2019-06-30 12:05:04 -07:00
return cmpObstructionManager . IsInTargetRange ( this . entity , target , min , max , false ) ;
2012-05-18 14:31:57 -07:00
} ;
2020-04-07 10:35:25 -07:00
/ * *
* Check if the target is inside the attack range of the formation .
*
* @ param { number } target - The target entity ID to attack .
* @ return { boolean } - Whether the entity is within attacking distance .
* /
UnitAI . prototype . CheckFormationTargetAttackRange = function ( target )
{
let cmpTargetFormation = Engine . QueryInterface ( target , IID _Formation ) ;
if ( cmpTargetFormation )
target = cmpTargetFormation . GetClosestMember ( this . entity ) ;
let cmpFormationAttack = Engine . QueryInterface ( this . entity , IID _Attack ) ;
if ( ! cmpFormationAttack )
return false ;
let range = cmpFormationAttack . GetRange ( target ) ;
let cmpObstructionManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _ObstructionManager ) ;
return cmpObstructionManager . IsInTargetRange ( this . entity , target , range . min , range . max , false ) ;
} ;
2011-06-24 05:35:15 -07:00
/ * *
* Returns true if the target entity is visible through the FoW / SoD .
* /
UnitAI . prototype . CheckTargetVisible = function ( target )
{
2021-12-09 08:35:03 -08:00
if ( this . isGarrisoned )
return false ;
const cmpOwnership = Engine . QueryInterface ( this . entity , IID _Ownership ) ;
2011-06-24 05:35:15 -07:00
if ( ! cmpOwnership )
return false ;
2021-12-09 08:35:03 -08:00
const cmpRangeManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _RangeManager ) ;
2011-06-24 05:35:15 -07:00
if ( ! cmpRangeManager )
return false ;
2014-08-04 15:49:19 -07:00
// Entities that are hidden and miraged are considered visible
2021-12-09 08:35:03 -08:00
const cmpFogging = Engine . QueryInterface ( target , IID _Fogging ) ;
2014-08-04 15:49:19 -07:00
if ( cmpFogging && cmpFogging . IsMiraged ( cmpOwnership . GetOwner ( ) ) )
return true ;
2015-01-30 07:28:06 -08:00
if ( cmpRangeManager . GetLosVisibility ( target , cmpOwnership . GetOwner ( ) ) == "hidden" )
2011-06-24 05:35:15 -07:00
return false ;
// Either visible directly, or visible in fog
return true ;
} ;
2019-09-01 00:35:32 -07:00
/ * *
* Returns true if the given position is currentl visible ( not in FoW / SoD ) .
* /
UnitAI . prototype . CheckPositionVisible = function ( x , z )
{
let cmpOwnership = Engine . QueryInterface ( this . entity , IID _Ownership ) ;
if ( ! cmpOwnership )
return false ;
let cmpRangeManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _RangeManager ) ;
if ( ! cmpRangeManager )
return false ;
return cmpRangeManager . GetLosVisibilityPosition ( x , z , cmpOwnership . GetOwner ( ) ) == "visible" ;
} ;
2019-07-24 12:05:12 -07:00
/ * *
* How close to our goal do we consider it ' s OK to stop if the goal appears unreachable .
* Currently 3 terrain tiles as that ' s relatively close but helps pathfinding .
* /
UnitAI . prototype . DefaultRelaxedMaxRange = 12 ;
2019-07-22 11:07:24 -07:00
/ * *
* @ returns true if the unit is in the relaxed - range from the target .
* /
UnitAI . prototype . RelaxedMaxRangeCheck = function ( data , relaxedRange )
{
if ( ! data . relaxed )
return false ;
let ndata = data ;
ndata . min = 0 ;
ndata . max = relaxedRange ;
return this . CheckRange ( ndata ) ;
} ;
2019-07-01 05:32:57 -07:00
/ * *
* Let an entity face its target .
* @ param { number } target - The entity - ID of the target .
* /
2011-06-06 15:08:26 -07:00
UnitAI . prototype . FaceTowardsTarget = function ( target )
{
2019-07-01 05:32:57 -07:00
let cmpTargetPosition = Engine . QueryInterface ( target , IID _Position ) ;
2011-06-06 15:08:26 -07:00
if ( ! cmpTargetPosition || ! cmpTargetPosition . IsInWorld ( ) )
2013-01-28 14:19:06 -08:00
return ;
2019-07-01 05:32:57 -07:00
let targetPosition = cmpTargetPosition . GetPosition2D ( ) ;
// Use cmpUnitMotion for units that support that, otherwise try cmpPosition (e.g. turrets)
let cmpUnitMotion = Engine . QueryInterface ( this . entity , IID _UnitMotion ) ;
if ( cmpUnitMotion )
2011-06-06 15:08:26 -07:00
{
2019-07-01 05:32:57 -07:00
cmpUnitMotion . FaceTowardsPoint ( targetPosition . x , targetPosition . y ) ;
return ;
2011-06-06 15:08:26 -07:00
}
2019-07-01 05:32:57 -07:00
let cmpPosition = Engine . QueryInterface ( this . entity , IID _Position ) ;
if ( cmpPosition && cmpPosition . IsInWorld ( ) )
cmpPosition . TurnTo ( cmpPosition . GetPosition2D ( ) . angleTo ( targetPosition ) ) ;
2012-04-28 12:25:44 -07:00
} ;
2011-06-06 15:08:26 -07:00
2012-04-18 11:26:49 -07:00
UnitAI . prototype . CheckTargetDistanceFromHeldPosition = function ( target , iid , type )
2011-06-17 15:13:39 -07:00
{
2021-03-30 04:17:46 -07:00
let range = this . GetRange ( iid , type , target ) ;
2020-01-31 12:00:06 -08:00
if ( ! range )
return false ;
2011-06-17 15:13:39 -07:00
2020-01-30 13:05:59 -08:00
let cmpPosition = Engine . QueryInterface ( target , IID _Position ) ;
2011-06-17 15:13:39 -07:00
if ( ! cmpPosition || ! cmpPosition . IsInWorld ( ) )
return false ;
2020-01-30 13:05:59 -08:00
let cmpVision = Engine . QueryInterface ( this . entity , IID _Vision ) ;
2011-06-17 15:13:39 -07:00
if ( ! cmpVision )
return false ;
2020-01-30 13:05:59 -08:00
let halfvision = cmpVision . GetRange ( ) / 2 ;
2011-06-17 15:13:39 -07:00
2020-01-30 13:05:59 -08:00
let pos = cmpPosition . GetPosition ( ) ;
let heldPosition = this . heldPosition ;
2012-12-02 16:37:58 -08:00
if ( heldPosition === undefined )
2018-03-10 11:12:23 -08:00
heldPosition = { "x" : pos . x , "z" : pos . z } ;
2011-06-17 15:13:39 -07:00
2017-10-22 13:46:41 -07:00
return Math . euclidDistance2D ( pos . x , pos . z , heldPosition . x , heldPosition . z ) < halfvision + range . max ;
2011-06-17 15:13:39 -07:00
} ;
UnitAI . prototype . CheckTargetIsInVisionRange = function ( target )
{
2020-01-30 13:05:59 -08:00
let cmpVision = Engine . QueryInterface ( this . entity , IID _Vision ) ;
2011-06-17 15:13:39 -07:00
if ( ! cmpVision )
return false ;
2020-01-30 13:05:59 -08:00
let range = cmpVision . GetRange ( ) ;
2020-11-04 10:56:45 -08:00
let distance = PositionHelper . DistanceBetweenEntities ( this . entity , target ) ;
2011-06-17 15:13:39 -07:00
return distance < range ;
2011-06-24 05:35:15 -07:00
} ;
2011-06-17 15:13:39 -07:00
2023-01-09 06:29:06 -08:00
UnitAI . prototype . GetBestAttackAgainst = function ( target , allowCapture = this . DEFAULT _CAPTURE )
2012-05-01 15:20:08 -07:00
{
2023-01-09 06:29:06 -08:00
return Engine . QueryInterface ( this . entity , IID _Attack ) ? . GetBestAttackAgainst ( target , allowCapture ) ;
2012-05-01 15:20:08 -07:00
} ;
2010-07-29 13:39:23 -07:00
/ * *
* Try to find one of the given entities which can be attacked ,
* and start attacking it .
* Returns true if it found something to attack .
* /
2017-12-23 07:53:00 -08:00
UnitAI . prototype . AttackVisibleEntity = function ( ents )
2010-07-29 13:39:23 -07:00
{
2017-12-23 07:53:00 -08:00
var target = ents . find ( target => this . CanAttack ( target ) ) ;
2015-08-29 15:49:44 -07:00
if ( ! target )
return false ;
2023-01-09 06:29:06 -08:00
this . PushOrderFront ( "Attack" , { "target" : target , "force" : false } ) ;
2015-08-29 15:49:44 -07:00
return true ;
2011-06-17 15:13:39 -07:00
} ;
/ * *
2011-06-24 05:35:15 -07:00
* Try to find one of the given entities which can be attacked
* and which is close to the hold position , and start attacking it .
2011-06-17 15:13:39 -07:00
* Returns true if it found something to attack .
* /
2017-12-23 07:53:00 -08:00
UnitAI . prototype . AttackEntityInZone = function ( ents )
2011-06-17 15:13:39 -07:00
{
2015-08-29 15:49:44 -07:00
var target = ents . find ( target =>
2021-03-20 02:02:22 -07:00
this . CanAttack ( target ) &&
this . CheckTargetDistanceFromHeldPosition ( target , IID _Attack , this . GetBestAttackAgainst ( target , true ) ) &&
( this . GetStance ( ) . respondChaseBeyondVision || this . CheckTargetIsInVisionRange ( target ) )
2015-08-29 15:49:44 -07:00
) ;
if ( ! target )
return false ;
2023-01-09 06:29:06 -08:00
this . PushOrderFront ( "Attack" , { "target" : target , "force" : false } ) ;
2015-08-29 15:49:44 -07:00
return true ;
2010-07-29 13:39:23 -07:00
} ;
2010-02-05 14:00:39 -08:00
2011-03-04 06:36:41 -08:00
/ * *
* Try to respond appropriately given our current stance ,
* given a list of entities that match our stance ' s target criteria .
* Returns true if it responded .
* /
UnitAI . prototype . RespondToTargetedEntities = function ( ents )
{
if ( ! ents . length )
return false ;
if ( this . GetStance ( ) . respondChase )
2017-12-23 07:53:00 -08:00
return this . AttackVisibleEntity ( ents ) ;
2011-03-04 06:36:41 -08:00
2011-06-17 15:13:39 -07:00
if ( this . GetStance ( ) . respondStandGround )
2017-12-23 07:53:00 -08:00
return this . AttackVisibleEntity ( ents ) ;
2011-06-17 15:13:39 -07:00
if ( this . GetStance ( ) . respondHoldGround )
2017-12-23 07:53:00 -08:00
return this . AttackEntityInZone ( ents ) ;
2011-06-17 15:13:39 -07:00
2011-03-04 06:36:41 -08:00
if ( this . GetStance ( ) . respondFlee )
{
2020-05-07 10:29:35 -07:00
if ( this . order && this . order . type == "Flee" )
this . orderQueue . shift ( ) ;
2012-05-08 16:00:14 -07:00
this . PushOrderFront ( "Flee" , { "target" : ents [ 0 ] , "force" : false } ) ;
2011-03-04 06:36:41 -08:00
return true ;
}
return false ;
} ;
2020-07-23 23:07:27 -07:00
/ * *
* @ param { number } ents - An array of the IDs of the spotted entities .
* @ return { boolean } - Whether we responded .
* /
UnitAI . prototype . RespondToSightedEntities = function ( ents )
{
if ( ! ents || ! ents . length )
return false ;
2021-02-27 12:13:40 -08:00
if ( this . GetStance ( ) . respondFleeOnSight )
2020-07-23 23:07:27 -07:00
{
this . Flee ( ents [ 0 ] , false ) ;
return true ;
}
return false ;
} ;
2012-04-17 13:22:13 -07:00
/ * *
* Try to respond to healable entities .
* Returns true if it responded .
* /
UnitAI . prototype . RespondToHealableEntities = function ( ents )
{
2020-07-23 23:07:27 -07:00
let ent = ents . find ( ent => this . CanHeal ( ent ) ) ;
2015-08-29 15:49:44 -07:00
if ( ! ent )
2012-04-17 13:22:13 -07:00
return false ;
2015-08-29 15:49:44 -07:00
this . PushOrderFront ( "Heal" , { "target" : ent , "force" : false } ) ;
return true ;
2012-04-17 13:22:13 -07:00
} ;
2011-06-24 05:35:15 -07:00
/ * *
* Returns true if we should stop following the target entity .
* /
2014-03-07 00:27:13 -08:00
UnitAI . prototype . ShouldAbandonChase = function ( target , force , iid , type )
2011-06-24 05:35:15 -07:00
{
2020-09-02 09:10:08 -07:00
if ( ! this . CheckTargetVisible ( target ) )
return true ;
2012-08-24 16:32:26 -07:00
// Forced orders shouldn't be interrupted.
if ( force )
return false ;
2013-11-30 09:30:08 -08:00
// If we are guarding/escorting, don't abandon as long as the guarded unit is in target range of the attacker
if ( this . isGuardOf )
{
2020-09-02 09:10:08 -07:00
let cmpUnitAI = Engine . QueryInterface ( target , IID _UnitAI ) ;
let cmpAttack = Engine . QueryInterface ( target , IID _Attack ) ;
2016-07-03 13:41:03 -07:00
if ( cmpUnitAI && cmpAttack &&
cmpAttack . GetAttackTypes ( ) . some ( type => cmpUnitAI . CheckTargetAttackRange ( this . isGuardOf , type ) ) )
2021-03-20 02:02:22 -07:00
return false ;
2013-11-30 09:30:08 -08:00
}
2011-06-24 05:35:15 -07:00
if ( this . GetStance ( ) . respondHoldGround )
2014-03-07 00:27:13 -08:00
if ( ! this . CheckTargetDistanceFromHeldPosition ( target , iid , type ) )
2011-06-24 05:35:15 -07:00
return true ;
2020-09-02 09:10:08 -07:00
// Stop if it's left our vision range, unless we're especially persistent.
2012-08-24 16:32:26 -07:00
if ( ! this . GetStance ( ) . respondChaseBeyondVision )
2011-06-24 05:35:15 -07:00
if ( ! this . CheckTargetIsInVisionRange ( target ) )
return true ;
return false ;
} ;
2011-06-17 15:13:39 -07:00
/ *
2011-06-24 05:35:15 -07:00
* Returns whether we should chase the targeted entity ,
* given our current stance .
2011-06-17 15:13:39 -07:00
* /
2011-06-24 05:35:15 -07:00
UnitAI . prototype . ShouldChaseTargetedEntity = function ( target , force )
{
2020-06-02 04:40:29 -07:00
if ( ! this . AbleToMove ( ) )
2014-05-30 07:46:06 -07:00
return false ;
2011-06-24 05:35:15 -07:00
if ( this . GetStance ( ) . respondChase )
return true ;
2011-06-17 15:13:39 -07:00
2013-11-30 09:30:08 -08:00
// If we are guarding/escorting, chase at least as long as the guarded unit is in target range of the attacker
if ( this . isGuardOf )
{
2021-03-20 02:02:22 -07:00
let cmpUnitAI = Engine . QueryInterface ( target , IID _UnitAI ) ;
2018-03-27 10:56:32 -07:00
let cmpAttack = Engine . QueryInterface ( target , IID _Attack ) ;
2016-07-03 13:41:03 -07:00
if ( cmpUnitAI && cmpAttack &&
cmpAttack . GetAttackTypes ( ) . some ( type => cmpUnitAI . CheckTargetAttackRange ( this . isGuardOf , type ) ) )
return true ;
2013-11-30 09:30:08 -08:00
}
2020-09-06 02:17:13 -07:00
return force ;
2011-06-24 05:35:15 -07:00
} ;
2011-06-17 15:13:39 -07:00
2021-03-20 02:02:22 -07:00
// External interface functions
2010-02-10 11:28:46 -08:00
Allow picking a default formation for walk (and walk-like) orders.
This allows choosing a "default formation", which is activated
automatically for units given walk orders (and attack-walk etc.).
Conversely, units in formation that are given a gather/build/... order
are taken out of formation and given the order individually.
The default formation can be selected by right-clicking on any formation
icon.
This leverages formations for walking, where they are quite efficient
(in fact, perhaps too efficient), while circumventing issues with
various orders.
Choosing the "null formation" as the default formation de-activates the
behaviour entirely, and plays out exactly like SVN.
This makes it possible to queue a formation-order then a
noformation-order (i.e. walk then repair), though the behaviour isn't
very flexible.
For modders, it should be relatively easy to change the setup for each
order, and/or to force deactivating/activating formations in general.
Tested by: Freagarach, Angen
Refs #3479, #1791.
Makes #3478 mostly invalid.
Differential Revision: https://code.wildfiregames.com/D2764
This was SVN commit r24480.
2020-12-31 02:04:58 -08:00
/ * *
* Order a unit to leave the formation it is in .
* Used to handle queued no - formation orders for units in formation .
* /
UnitAI . prototype . LeaveFormation = function ( queued = true )
{
// If queued, add the order even if we're not in formation,
// maybe we will be later.
if ( ! queued && ! this . IsFormationMember ( ) )
return ;
if ( queued )
this . AddOrder ( "LeaveFormation" , { "force" : true } , queued ) ;
else
this . PushOrderFront ( "LeaveFormation" , { "force" : true } ) ;
} ;
2010-09-03 02:55:14 -07:00
UnitAI . prototype . SetFormationController = function ( ent )
{
this . formationController = ent ;
2022-06-29 23:04:05 -07:00
// Set obstruction group, so we can walk through members of our own formation.
Engine . QueryInterface ( this . entity , IID _Obstruction ) ? . SetControlGroup ( ent ) ;
Engine . QueryInterface ( this . entity , IID _UnitMotion ) ? . SetMemberOfFormation ( ent ) ;
} ;
2021-06-06 08:25:52 -07:00
2022-06-29 23:04:05 -07:00
UnitAI . prototype . UnsetFormationController = function ( )
{
this . formationController = INVALID _ENTITY ;
Engine . QueryInterface ( this . entity , IID _Obstruction ) ? . SetControlGroup ( this . entity ) ;
Engine . QueryInterface ( this . entity , IID _UnitMotion ) ? . SetMemberOfFormation ( this . formationController ) ;
this . UnitFsm . ProcessMessage ( this , { "type" : "FormationLeave" } ) ;
2010-09-03 02:55:14 -07:00
} ;
UnitAI . prototype . GetFormationController = function ( )
{
return this . formationController ;
} ;
2018-02-17 09:54:24 -08:00
UnitAI . prototype . GetFormationTemplate = function ( )
2011-05-01 13:40:53 -07:00
{
Allow picking a default formation for walk (and walk-like) orders.
This allows choosing a "default formation", which is activated
automatically for units given walk orders (and attack-walk etc.).
Conversely, units in formation that are given a gather/build/... order
are taken out of formation and given the order individually.
The default formation can be selected by right-clicking on any formation
icon.
This leverages formations for walking, where they are quite efficient
(in fact, perhaps too efficient), while circumventing issues with
various orders.
Choosing the "null formation" as the default formation de-activates the
behaviour entirely, and plays out exactly like SVN.
This makes it possible to queue a formation-order then a
noformation-order (i.e. walk then repair), though the behaviour isn't
very flexible.
For modders, it should be relatively easy to change the setup for each
order, and/or to force deactivating/activating formations in general.
Tested by: Freagarach, Angen
Refs #3479, #1791.
Makes #3478 mostly invalid.
Differential Revision: https://code.wildfiregames.com/D2764
This was SVN commit r24480.
2020-12-31 02:04:58 -08:00
return Engine . QueryInterface ( SYSTEM _ENTITY , IID _TemplateManager ) . GetCurrentTemplateName ( this . formationController ) || NULL _FORMATION ;
2011-05-01 13:40:53 -07:00
} ;
2012-12-01 17:52:27 -08:00
UnitAI . prototype . MoveIntoFormation = function ( cmd )
{
var cmpFormation = Engine . QueryInterface ( this . entity , IID _Formation ) ;
if ( ! cmpFormation )
return ;
var cmpPosition = Engine . QueryInterface ( this . entity , IID _Position ) ;
if ( ! cmpPosition || ! cmpPosition . IsInWorld ( ) )
return ;
var pos = cmpPosition . GetPosition ( ) ;
this . PushOrderFront ( "MoveIntoFormation" , { "x" : pos . x , "z" : pos . z , "force" : true } ) ;
} ;
2013-12-04 05:14:31 -08:00
UnitAI . prototype . GetTargetPositions = function ( )
2010-10-02 12:40:30 -07:00
{
2013-12-04 05:14:31 -08:00
var targetPositions = [ ] ;
2010-10-02 12:40:30 -07:00
for ( var i = 0 ; i < this . orderQueue . length ; ++ i )
{
var order = this . orderQueue [ i ] ;
switch ( order . type )
{
case "Walk" :
2013-02-25 13:56:24 -08:00
case "WalkAndFight" :
2012-12-02 09:25:23 -08:00
case "WalkToPointRange" :
2012-12-01 17:52:27 -08:00
case "MoveIntoFormation" :
2011-12-16 08:08:26 -08:00
case "GatherNearPosition" :
2016-09-25 14:33:05 -07:00
case "Patrol" :
2014-01-27 04:34:59 -08:00
targetPositions . push ( new Vector2D ( order . data . x , order . data . z ) ) ;
2010-10-02 12:40:30 -07:00
break ; // and continue the loop
case "WalkToTarget" :
2012-05-18 14:31:57 -07:00
case "WalkToTargetRange" : // This doesn't move to the target (just into range), but a later order will.
2013-11-30 09:30:08 -08:00
case "Guard" :
2011-03-04 06:36:41 -08:00
case "Flee" :
2011-02-10 08:06:28 -08:00
case "LeaveFoundation" :
2010-10-02 12:40:30 -07:00
case "Attack" :
2012-04-17 13:22:13 -07:00
case "Heal" :
2010-10-02 12:40:30 -07:00
case "Gather" :
2010-11-13 11:15:29 -08:00
case "ReturnResource" :
2010-10-02 12:40:30 -07:00
case "Repair" :
2010-11-30 19:01:17 -08:00
case "Garrison" :
2021-08-03 09:42:56 -07:00
case "CollectTreasure" :
2010-10-02 12:40:30 -07:00
var cmpTargetPosition = Engine . QueryInterface ( order . data . target , IID _Position ) ;
if ( ! cmpTargetPosition || ! cmpTargetPosition . IsInWorld ( ) )
2013-12-04 05:14:31 -08:00
return targetPositions ;
2014-01-27 04:34:59 -08:00
targetPositions . push ( cmpTargetPosition . GetPosition2D ( ) ) ;
2013-12-04 05:14:31 -08:00
return targetPositions ;
2010-10-02 12:40:30 -07:00
2012-08-07 22:32:53 -07:00
case "Stop" :
2013-12-04 05:14:31 -08:00
return [ ] ;
2012-08-07 22:32:53 -07:00
2021-08-27 22:52:37 -07:00
case "DropAtNearestDropSite" :
break ;
2010-10-02 12:40:30 -07:00
default :
2025-04-10 21:24:32 -07:00
error ( "GetTargetPositions: Unrecognized order type '" + order . type + "'" ) ;
2013-12-04 05:14:31 -08:00
return [ ] ;
2010-10-02 12:40:30 -07:00
}
}
2013-12-04 05:14:31 -08:00
return targetPositions ;
} ;
/ * *
* Returns the estimated distance that this unit will travel before either
* finishing all of its orders , or reaching a non - walk target ( attack , gather , etc ) .
* Intended for Formation to switch to column layout on long walks .
* /
UnitAI . prototype . ComputeWalkingDistance = function ( )
{
var distance = 0 ;
var cmpPosition = Engine . QueryInterface ( this . entity , IID _Position ) ;
if ( ! cmpPosition || ! cmpPosition . IsInWorld ( ) )
return 0 ;
// Keep track of the position at the start of each order
2014-01-27 04:34:59 -08:00
var pos = cmpPosition . GetPosition2D ( ) ;
2013-12-04 05:14:31 -08:00
var targetPositions = this . GetTargetPositions ( ) ;
2015-08-29 15:49:44 -07:00
for ( var i = 0 ; i < targetPositions . length ; ++ i )
2013-12-04 05:14:31 -08:00
{
2014-01-27 04:34:59 -08:00
distance += pos . distanceTo ( targetPositions [ i ] ) ;
2013-12-04 05:14:31 -08:00
// Remember this as the start position for the next order
pos = targetPositions [ i ] ;
}
2010-10-02 12:40:30 -07:00
return distance ;
} ;
2021-03-05 22:12:07 -08:00
UnitAI . prototype . AddOrder = function ( type , data , queued , pushFront )
2010-07-21 09:09:58 -07:00
{
2014-01-28 10:56:39 -08:00
if ( this . expectedRoute )
this . expectedRoute = undefined ;
2021-03-05 22:12:07 -08:00
if ( pushFront )
this . PushOrderFront ( type , data ) ;
else if ( queued )
2010-07-21 09:09:58 -07:00
this . PushOrder ( type , data ) ;
else
this . ReplaceOrder ( type , data ) ;
} ;
2010-02-10 11:28:46 -08:00
2013-11-30 09:30:08 -08:00
/ * *
* Adds guard / escort order to the queue , forced by the player .
* /
2021-03-05 22:12:07 -08:00
UnitAI . prototype . Guard = function ( target , queued , pushFront )
2013-11-30 09:30:08 -08:00
{
if ( ! this . CanGuard ( ) )
{
this . WalkToTarget ( target , queued ) ;
return ;
}
2020-09-10 01:01:25 -07:00
if ( target === this . entity )
return ;
2013-11-30 09:30:08 -08:00
if ( this . isGuardOf )
{
if ( this . isGuardOf == target && this . order && this . order . type == "Guard" )
return ;
2021-03-20 02:02:22 -07:00
this . RemoveGuard ( ) ;
2013-11-30 09:30:08 -08:00
}
2021-03-05 22:12:07 -08:00
this . AddOrder ( "Guard" , { "target" : target , "force" : false } , queued , pushFront ) ;
2013-11-30 09:30:08 -08:00
} ;
2020-12-05 23:22:51 -08:00
/ * *
* @ return { boolean } - Whether it makes sense to guard the given entity .
* /
UnitAI . prototype . ShouldGuard = function ( target )
{
return this . TargetIsAlive ( target ) ||
Engine . QueryInterface ( target , IID _Capturable ) ||
Engine . QueryInterface ( target , IID _StatusEffectsReceiver ) ;
} ;
2013-11-30 09:30:08 -08:00
UnitAI . prototype . AddGuard = function ( target )
{
if ( ! this . CanGuard ( ) )
return false ;
var cmpGuard = Engine . QueryInterface ( target , IID _Guard ) ;
if ( ! cmpGuard )
return false ;
this . isGuardOf = target ;
this . guardRange = cmpGuard . GetRange ( this . entity ) ;
cmpGuard . AddGuard ( this . entity ) ;
return true ;
} ;
UnitAI . prototype . RemoveGuard = function ( )
{
2016-12-07 10:08:26 -08:00
if ( ! this . isGuardOf )
return ;
let cmpGuard = Engine . QueryInterface ( this . isGuardOf , IID _Guard ) ;
if ( cmpGuard )
cmpGuard . RemoveGuard ( this . entity ) ;
this . guardRange = undefined ;
this . isGuardOf = undefined ;
2013-11-30 09:30:08 -08:00
if ( ! this . order )
return ;
if ( this . order . type == "Guard" )
2018-04-09 09:36:41 -07:00
this . UnitFsm . ProcessMessage ( this , { "type" : "RemoveGuard" } ) ;
2013-11-30 09:30:08 -08:00
else
2016-12-07 10:08:26 -08:00
for ( let i = 1 ; i < this . orderQueue . length ; ++ i )
2013-11-30 09:30:08 -08:00
if ( this . orderQueue [ i ] . type == "Guard" )
this . orderQueue . splice ( i , 1 ) ;
2015-12-20 13:07:47 -08:00
Engine . PostMessage ( this . entity , MT _UnitAIOrderDataChanged , { "to" : this . GetOrderData ( ) } ) ;
2013-11-30 09:30:08 -08:00
} ;
UnitAI . prototype . IsGuardOf = function ( )
{
return this . isGuardOf ;
} ;
UnitAI . prototype . SetGuardOf = function ( entity )
{
// entity may be undefined
this . isGuardOf = entity ;
} ;
UnitAI . prototype . CanGuard = function ( )
{
// Formation controllers should always respond to commands
// (then the individual units can make up their own minds)
if ( this . IsFormationController ( ) )
return true ;
2017-04-25 04:22:41 -07:00
return this . template . CanGuard == "true" ;
} ;
UnitAI . prototype . CanPatrol = function ( )
{
// Formation controllers should always respond to commands
// (then the individual units can make up their own minds)
return this . IsFormationController ( ) || this . template . CanPatrol == "true" ;
2013-11-30 09:30:08 -08:00
} ;
2012-05-08 16:00:14 -07:00
/ * *
* Adds walk order to queue , forced by the player .
* /
2021-03-05 22:12:07 -08:00
UnitAI . prototype . Walk = function ( x , z , queued , pushFront )
2010-07-21 09:09:58 -07:00
{
2021-03-05 22:12:07 -08:00
if ( ! pushFront && this . expectedRoute && queued )
2014-01-28 10:56:39 -08:00
this . expectedRoute . push ( { "x" : x , "z" : z } ) ;
else
2021-03-05 22:12:07 -08:00
this . AddOrder ( "Walk" , { "x" : x , "z" : z , "force" : true } , queued , pushFront ) ;
2010-02-05 14:00:39 -08:00
} ;
2014-05-25 03:00:55 -07:00
/ * *
* Adds walk to point range order to queue , forced by the player .
* /
2021-03-05 22:12:07 -08:00
UnitAI . prototype . WalkToPointRange = function ( x , z , min , max , queued , pushFront )
2014-05-25 03:00:55 -07:00
{
2021-03-05 22:12:07 -08:00
this . AddOrder ( "Walk" , { "x" : x , "z" : z , "min" : min , "max" : max , "force" : true } , queued , pushFront ) ;
2014-05-25 03:00:55 -07:00
} ;
2012-08-07 22:32:53 -07:00
/ * *
* Adds stop order to queue , forced by the player .
* /
2021-03-05 22:12:07 -08:00
UnitAI . prototype . Stop = function ( queued , pushFront )
2012-08-07 22:32:53 -07:00
{
2021-03-05 22:12:07 -08:00
this . AddOrder ( "Stop" , { "force" : true } , queued , pushFront ) ;
2012-08-07 22:32:53 -07:00
} ;
2021-08-27 22:52:37 -07:00
/ * *
* The unit will drop all resources at the closest dropsite . If this unit is no gatherer or
* no dropsite is available , it will do nothing .
* /
UnitAI . prototype . DropAtNearestDropSite = function ( queued , pushFront )
{
this . AddOrder ( "DropAtNearestDropSite" , { "force" : true } , queued , pushFront ) ;
} ;
2012-05-08 16:00:14 -07:00
/ * *
* Adds walk - to - target order to queue , this only occurs in response
* to a player order , and so is forced .
* /
2021-03-05 22:12:07 -08:00
UnitAI . prototype . WalkToTarget = function ( target , queued , pushFront )
2010-03-12 13:41:40 -08:00
{
2021-03-05 22:12:07 -08:00
this . AddOrder ( "WalkToTarget" , { "target" : target , "force" : true } , queued , pushFront ) ;
2013-02-25 13:56:24 -08:00
} ;
/ * *
* Adds walk - and - fight order to queue , this only occurs in response
* to a player order , and so is forced .
2014-08-24 04:51:03 -07:00
* If targetClasses is given , only entities matching the targetClasses can be attacked .
2013-02-25 13:56:24 -08:00
* /
2023-01-09 06:29:06 -08:00
UnitAI . prototype . WalkAndFight = function ( x , z , targetClasses , allowCapture = this . DEFAULT _CAPTURE , queued = false , pushFront = false )
2013-02-25 13:56:24 -08:00
{
2021-03-05 22:12:07 -08:00
this . AddOrder ( "WalkAndFight" , { "x" : x , "z" : z , "targetClasses" : targetClasses , "allowCapture" : allowCapture , "force" : true } , queued , pushFront ) ;
2010-07-21 09:09:58 -07:00
} ;
2010-03-12 13:41:40 -08:00
2023-01-09 06:29:06 -08:00
UnitAI . prototype . Patrol = function ( x , z , targetClasses , allowCapture = this . DEFAULT _CAPTURE , queued = false , pushFront = false )
2016-09-25 14:33:05 -07:00
{
2017-04-25 04:22:41 -07:00
if ( ! this . CanPatrol ( ) )
{
this . Walk ( x , z , queued ) ;
return ;
}
2021-03-05 22:12:07 -08:00
this . AddOrder ( "Patrol" , { "x" : x , "z" : z , "targetClasses" : targetClasses , "allowCapture" : allowCapture , "force" : true } , queued , pushFront ) ;
2016-09-25 14:33:05 -07:00
} ;
2012-05-08 16:00:14 -07:00
/ * *
* Adds leave foundation order to queue , treated as forced .
* /
2011-02-10 08:06:28 -08:00
UnitAI . prototype . LeaveFoundation = function ( target )
{
2011-03-05 14:18:29 -08:00
// If we're already being told to leave a foundation, then
// ignore this new request so we don't end up being too indecisive
2020-06-02 04:40:29 -07:00
// to ever actually move anywhere.
2019-12-28 03:40:24 -08:00
if ( this . order && ( this . order . type == "LeaveFoundation" || ( this . order . type == "Flee" && this . order . data . target == target ) ) )
return ;
if ( this . orderQueue . length && this . orderQueue [ 0 ] . type == "Unpack" && this . WillMoveFromFoundation ( target , false ) )
{
let cmpPack = Engine . QueryInterface ( this . entity , IID _Pack ) ;
if ( cmpPack )
cmpPack . CancelPack ( ) ;
}
if ( this . IsPacking ( ) )
2011-03-05 14:18:29 -08:00
return ;
2012-05-08 16:00:14 -07:00
this . PushOrderFront ( "LeaveFoundation" , { "target" : target , "force" : true } ) ;
2011-02-10 08:06:28 -08:00
} ;
2012-05-08 16:00:14 -07:00
/ * *
* Adds attack order to the queue , forced by the player .
* /
2023-01-09 06:29:06 -08:00
UnitAI . prototype . Attack = function ( target , allowCapture = this . DEFAULT _CAPTURE , queued = false , pushFront = false )
2010-07-21 09:09:58 -07:00
{
if ( ! this . CanAttack ( target ) )
{
2012-04-17 13:22:13 -07:00
// We don't want to let healers walk to the target unit so they can be easily killed.
// Instead we just let them get into healing range.
if ( this . IsHealer ( ) )
this . MoveToTargetRange ( target , IID _Heal ) ;
else
2021-03-05 22:12:07 -08:00
this . WalkToTarget ( target , queued , pushFront ) ;
2010-03-12 13:41:40 -08:00
return ;
2010-07-21 09:09:58 -07:00
}
2019-07-01 23:49:27 -07:00
let order = {
"target" : target ,
"force" : true ,
"allowCapture" : allowCapture ,
} ;
this . RememberTargetPosition ( order ) ;
2021-02-08 02:20:34 -08:00
if ( this . order && this . order . type == "Attack" &&
this . order . data &&
this . order . data . target === order . target &&
this . order . data . allowCapture === order . allowCapture )
{
this . order . data . lastPos = order . lastPos ;
this . order . data . force = order . force ;
2021-03-22 04:36:50 -07:00
if ( order . force )
this . orderQueue = [ this . order ] ;
2021-02-08 02:20:34 -08:00
return ;
}
2021-03-05 22:12:07 -08:00
this . AddOrder ( "Attack" , order , queued , pushFront ) ;
2010-07-21 09:09:58 -07:00
} ;
2010-03-12 13:41:40 -08:00
2012-05-08 16:00:14 -07:00
/ * *
* Adds garrison order to the queue , forced by the player .
* /
2021-03-05 22:12:07 -08:00
UnitAI . prototype . Garrison = function ( target , queued , pushFront )
2010-10-23 15:43:15 -07:00
{
2021-04-12 05:31:29 -07:00
// Not allowed to garrison when occupying a turret, at the moment.
2021-12-09 08:35:03 -08:00
if ( this . isGarrisoned || this . IsTurret ( ) )
2021-04-12 05:31:29 -07:00
return ;
2014-05-10 15:21:24 -07:00
if ( target == this . entity )
return ;
2010-10-23 15:43:15 -07:00
if ( ! this . CanGarrison ( target ) )
{
this . WalkToTarget ( target , queued ) ;
return ;
}
2021-03-26 03:18:30 -07:00
this . AddOrder ( "Garrison" , { "target" : target , "force" : true , "garrison" : true } , queued , pushFront ) ;
2010-10-23 15:43:15 -07:00
} ;
2012-05-08 16:00:14 -07:00
/ * *
* Adds ungarrison order to the queue .
* /
2011-05-10 21:05:05 -07:00
UnitAI . prototype . Ungarrison = function ( )
{
2021-12-09 08:35:03 -08:00
if ( ! this . isGarrisoned && ! this . IsTurret ( ) )
2021-03-07 23:19:06 -08:00
return ;
this . AddOrder ( "Ungarrison" , null , false ) ;
2011-05-10 21:05:05 -07:00
} ;
2021-03-26 03:18:30 -07:00
/ * *
* Adds garrison order to the queue , forced by the player .
* /
UnitAI . prototype . OccupyTurret = function ( target , queued , pushFront )
{
if ( target == this . entity )
return ;
if ( ! this . CanOccupyTurret ( target ) )
{
this . WalkToTarget ( target , queued ) ;
return ;
}
this . AddOrder ( "Garrison" , { "target" : target , "force" : true , "garrison" : false } , queued , pushFront ) ;
} ;
2012-05-08 16:00:14 -07:00
/ * *
2015-02-05 18:11:39 -08:00
* Adds gather order to the queue , forced by the player
* until the target is reached
2012-05-08 16:00:14 -07:00
* /
2021-03-05 22:12:07 -08:00
UnitAI . prototype . Gather = function ( target , queued , pushFront )
2012-05-08 16:00:14 -07:00
{
2021-03-05 22:12:07 -08:00
this . PerformGather ( target , queued , true , pushFront ) ;
2012-05-08 16:00:14 -07:00
} ;
/ * *
* Internal function to abstract the force parameter .
* /
2021-03-05 22:12:07 -08:00
UnitAI . prototype . PerformGather = function ( target , queued , force , pushFront = false )
2010-07-21 09:09:58 -07:00
{
if ( ! this . CanGather ( target ) )
{
this . WalkToTarget ( target , queued ) ;
return ;
}
2010-07-31 14:21:42 -07:00
// Save the resource type now, so if the resource gets destroyed
// before we process the order then we still know what resource
// type to look for more of
2014-08-04 15:49:19 -07:00
var type ;
2015-05-01 10:03:37 -07:00
var cmpResourceSupply = QueryMiragedInterface ( target , IID _ResourceSupply ) ;
2014-08-04 15:49:19 -07:00
if ( cmpResourceSupply )
type = cmpResourceSupply . GetType ( ) ;
else
error ( "CanGather allowed gathering from invalid entity" ) ;
2010-07-31 14:21:42 -07:00
2012-02-12 13:18:50 -08:00
// Also save the target entity's template, so that if it's an animal,
// we won't go from hunting slow safe animals to dangerous fast ones
var cmpTemplateManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _TemplateManager ) ;
var template = cmpTemplateManager . GetCurrentTemplateName ( target ) ;
2012-08-19 18:38:39 -07:00
if ( template . indexOf ( "resource|" ) != - 1 )
template = template . slice ( 9 ) ;
2019-07-01 23:49:27 -07:00
let order = {
"target" : target ,
"type" : type ,
"template" : template ,
"force" : force ,
} ;
2010-12-08 08:12:04 -08:00
2019-07-01 23:49:27 -07:00
this . RememberTargetPosition ( order ) ;
2019-07-03 10:57:16 -07:00
order . initPos = order . lastPos ;
2019-07-01 23:49:27 -07:00
2021-02-08 02:20:34 -08:00
if ( this . order &&
( this . order . type == "Gather" || this . order . type == "Attack" ) &&
this . order . data &&
this . order . data . target === order . target )
{
this . order . data . lastPos = order . lastPos ;
this . order . data . force = order . force ;
2021-03-22 04:36:50 -07:00
if ( order . force )
2021-05-21 22:27:51 -07:00
{
if ( this . orderQueue [ 1 ] ? . type === "Gather" )
this . orderQueue = [ this . order , this . orderQueue [ 1 ] ] ;
else
this . orderQueue = [ this . order ] ;
}
2021-02-08 02:20:34 -08:00
return ;
}
2021-03-05 22:12:07 -08:00
this . AddOrder ( "Gather" , order , queued , pushFront ) ;
2010-07-21 09:09:58 -07:00
} ;
2010-03-12 13:41:40 -08:00
2012-05-08 16:00:14 -07:00
/ * *
* Adds gather - near - position order to the queue , not forced , so it can be
* interrupted by attacks .
2015-02-06 08:50:22 -08:00
* /
2021-03-05 22:12:07 -08:00
UnitAI . prototype . GatherNearPosition = function ( x , z , type , template , queued , pushFront )
2011-12-16 08:08:26 -08:00
{
2012-08-30 01:18:43 -07:00
if ( template . indexOf ( "resource|" ) != - 1 )
template = template . slice ( 9 ) ;
2015-02-05 18:11:39 -08:00
if ( this . IsFormationController ( ) || Engine . QueryInterface ( this . entity , IID _ResourceGatherer ) )
2021-03-05 22:12:07 -08:00
this . AddOrder ( "GatherNearPosition" , { "type" : type , "template" : template , "x" : x , "z" : z , "force" : false } , queued , pushFront ) ;
2015-02-05 18:11:39 -08:00
else
2021-03-05 22:12:07 -08:00
this . AddOrder ( "Walk" , { "x" : x , "z" : z , "force" : false } , queued , pushFront ) ;
2012-04-28 12:25:44 -07:00
} ;
2011-12-16 08:08:26 -08:00
2012-05-08 16:00:14 -07:00
/ * *
* Adds heal order to the queue , forced by the player .
* /
2021-03-05 22:12:07 -08:00
UnitAI . prototype . Heal = function ( target , queued , pushFront )
2012-04-17 13:22:13 -07:00
{
if ( ! this . CanHeal ( target ) )
{
this . WalkToTarget ( target , queued ) ;
return ;
}
2015-02-05 18:11:39 -08:00
2021-02-08 02:20:34 -08:00
if ( this . order && this . order . type == "Heal" &&
this . order . data &&
this . order . data . target === target )
{
this . order . data . force = true ;
2021-03-22 04:36:50 -07:00
this . orderQueue = [ this . order ] ;
2021-02-08 02:20:34 -08:00
return ;
}
2021-03-05 22:12:07 -08:00
this . AddOrder ( "Heal" , { "target" : target , "force" : true } , queued , pushFront ) ;
2012-04-17 13:22:13 -07:00
} ;
2012-05-08 16:00:14 -07:00
/ * *
* Adds return resource order to the queue , forced by the player .
* /
2021-03-05 22:12:07 -08:00
UnitAI . prototype . ReturnResource = function ( target , queued , pushFront )
2010-11-13 11:15:29 -08:00
{
2011-11-22 20:59:02 -08:00
if ( ! this . CanReturnResource ( target , true ) )
2010-11-13 11:15:29 -08:00
{
this . WalkToTarget ( target , queued ) ;
return ;
}
2021-03-05 22:12:07 -08:00
this . AddOrder ( "ReturnResource" , { "target" : target , "force" : true } , queued , pushFront ) ;
2010-11-13 11:15:29 -08:00
} ;
2021-03-02 23:47:38 -08:00
/ * *
* Adds order to collect a treasure to queue , forced by the player .
* /
2021-05-11 00:41:40 -07:00
UnitAI . prototype . CollectTreasure = function ( target , queued , pushFront )
2021-03-03 10:20:49 -08:00
{
this . AddOrder ( "CollectTreasure" , {
"target" : target ,
"force" : true
2021-05-11 00:41:40 -07:00
} , queued , pushFront ) ;
2021-03-03 10:20:49 -08:00
} ;
/ * *
* Adds order to collect a treasure to queue , forced by the player .
* /
2021-05-11 00:41:40 -07:00
UnitAI . prototype . CollectTreasureNearPosition = function ( posX , posZ , queued , pushFront )
2021-03-02 23:47:38 -08:00
{
2021-03-03 10:20:49 -08:00
this . AddOrder ( "CollectTreasureNearPosition" , {
"x" : posX ,
"z" : posZ ,
2021-05-11 00:41:40 -07:00
"force" : true
} , queued , pushFront ) ;
2021-03-02 23:47:38 -08:00
} ;
2020-03-07 03:35:25 -08:00
UnitAI . prototype . CancelSetupTradeRoute = function ( target )
{
let cmpTrader = Engine . QueryInterface ( this . entity , IID _Trader ) ;
if ( ! cmpTrader )
return ;
cmpTrader . RemoveTargetMarket ( target ) ;
if ( this . IsFormationController ( ) )
this . CallMemberFunction ( "CancelSetupTradeRoute" , [ target ] ) ;
2021-01-18 03:00:02 -08:00
} ;
2012-05-08 16:00:14 -07:00
/ * *
* Adds trade order to the queue . Either walk to the first market , or
* start a new route . Not forced , so it can be interrupted by attacks .
2014-01-28 10:56:39 -08:00
* The possible route may be given directly as a SetupTradeRoute argument
* if coming from a RallyPoint , or through this . expectedRoute if a user command .
2012-05-08 16:00:14 -07:00
* /
2021-03-05 22:12:07 -08:00
UnitAI . prototype . SetupTradeRoute = function ( target , source , route , queued , pushFront )
2012-03-08 12:42:28 -08:00
{
if ( ! this . CanTrade ( target ) )
{
this . WalkToTarget ( target , queued ) ;
return ;
}
2018-02-21 09:37:52 -08:00
// AI has currently no access to BackToWork
let cmpPlayer = QueryOwnerInterface ( this . entity ) ;
if ( cmpPlayer && cmpPlayer . IsAI ( ) && ! this . IsFormationController ( ) &&
this . workOrders . length && this . workOrders [ 0 ] . type == "Trade" )
{
let cmpTrader = Engine . QueryInterface ( this . entity , IID _Trader ) ;
2020-11-12 03:01:42 -08:00
if ( cmpTrader . HasBothMarkets ( ) &&
2018-02-21 09:37:52 -08:00
( cmpTrader . GetFirstMarket ( ) == target && cmpTrader . GetSecondMarket ( ) == source ||
cmpTrader . GetFirstMarket ( ) == source && cmpTrader . GetSecondMarket ( ) == target ) )
{
this . BackToWork ( ) ;
return ;
}
}
2014-01-28 10:56:39 -08:00
var marketsChanged = this . SetTargetMarket ( target , source ) ;
2015-08-29 15:49:44 -07:00
if ( ! marketsChanged )
return ;
2014-01-28 10:56:39 -08:00
2015-08-29 15:49:44 -07:00
var cmpTrader = Engine . QueryInterface ( this . entity , IID _Trader ) ;
if ( cmpTrader . HasBothMarkets ( ) )
{
2016-03-09 11:54:12 -08:00
let data = {
"target" : cmpTrader . GetFirstMarket ( ) ,
"route" : route ,
"force" : false
} ;
2014-01-28 10:56:39 -08:00
2015-08-29 15:49:44 -07:00
if ( this . expectedRoute )
{
if ( ! route && this . expectedRoute . length )
data . route = this . expectedRoute . slice ( ) ;
this . expectedRoute = undefined ;
2014-01-28 10:56:39 -08:00
}
2015-08-29 15:49:44 -07:00
if ( this . IsFormationController ( ) )
2014-01-28 10:56:39 -08:00
{
2015-08-29 15:49:44 -07:00
this . CallMemberFunction ( "AddOrder" , [ "Trade" , data , queued ] ) ;
2018-02-21 09:37:52 -08:00
let cmpFormation = Engine . QueryInterface ( this . entity , IID _Formation ) ;
2015-08-29 15:49:44 -07:00
if ( cmpFormation )
cmpFormation . Disband ( ) ;
2014-01-28 10:56:39 -08:00
}
2015-08-29 15:49:44 -07:00
else
2021-03-05 22:12:07 -08:00
this . AddOrder ( "Trade" , data , queued , pushFront ) ;
2015-08-29 15:49:44 -07:00
}
else
{
if ( this . IsFormationController ( ) )
2021-03-05 22:12:07 -08:00
this . CallMemberFunction ( "WalkToTarget" , [ cmpTrader . GetFirstMarket ( ) , queued , pushFront ] ) ;
2015-08-29 15:49:44 -07:00
else
2021-03-05 22:12:07 -08:00
this . WalkToTarget ( cmpTrader . GetFirstMarket ( ) , queued , pushFront ) ;
2015-08-29 15:49:44 -07:00
this . expectedRoute = [ ] ;
2012-03-08 12:42:28 -08:00
}
2012-04-28 12:25:44 -07:00
} ;
2012-03-08 12:42:28 -08:00
2014-01-28 10:56:39 -08:00
UnitAI . prototype . SetTargetMarket = function ( target , source )
{
2015-02-05 18:11:39 -08:00
var cmpTrader = Engine . QueryInterface ( this . entity , IID _Trader ) ;
2014-01-28 10:56:39 -08:00
if ( ! cmpTrader )
return false ;
var marketsChanged = cmpTrader . SetTargetMarket ( target , source ) ;
if ( this . IsFormationController ( ) )
this . CallMemberFunction ( "SetTargetMarket" , [ target , source ] ) ;
return marketsChanged ;
} ;
2016-05-14 05:27:48 -07:00
UnitAI . prototype . SwitchMarketOrder = function ( oldMarket , newMarket )
{
2016-09-23 09:11:26 -07:00
if ( this . order && this . order . data && this . order . data . target && this . order . data . target == oldMarket )
2016-05-26 10:58:08 -07:00
this . order . data . target = newMarket ;
2016-05-14 05:27:48 -07:00
} ;
2012-03-08 12:42:28 -08:00
UnitAI . prototype . MoveToMarket = function ( targetMarket )
{
2021-01-18 03:00:02 -08:00
let nextTarget ;
if ( this . waypoints && this . waypoints . length >= 1 )
nextTarget = this . waypoints . pop ( ) ;
else
nextTarget = { "target" : targetMarket } ;
this . order . data . nextTarget = nextTarget ;
return this . MoveTo ( this . order . data . nextTarget , IID _Trader ) ;
2012-04-28 12:25:44 -07:00
} ;
2012-03-08 12:42:28 -08:00
2016-05-14 05:27:48 -07:00
UnitAI . prototype . MarketRemoved = function ( market )
{
2016-05-14 11:25:35 -07:00
if ( this . order && this . order . data && this . order . data . target && this . order . data . target == market )
2016-05-14 05:27:48 -07:00
this . UnitFsm . ProcessMessage ( this , { "type" : "TradingCanceled" , "market" : market } ) ;
} ;
2012-05-08 16:00:14 -07:00
/ * *
* Adds repair / build order to the queue , forced by the player
* until the target is reached
* /
2021-03-05 22:12:07 -08:00
UnitAI . prototype . Repair = function ( target , autocontinue , queued , pushFront )
2010-07-21 09:09:58 -07:00
{
2010-09-03 02:55:14 -07:00
if ( ! this . CanRepair ( target ) )
2010-03-12 13:41:40 -08:00
{
2010-07-21 09:09:58 -07:00
this . WalkToTarget ( target , queued ) ;
2010-03-12 13:41:40 -08:00
return ;
}
2021-02-08 02:20:34 -08:00
if ( this . order && this . order . type == "Repair" &&
this . order . data &&
this . order . data . target === target &&
this . order . data . autocontinue === autocontinue )
{
this . order . data . force = true ;
2021-03-22 04:36:50 -07:00
this . orderQueue = [ this . order ] ;
2021-02-08 02:20:34 -08:00
return ;
}
2021-03-05 22:12:07 -08:00
this . AddOrder ( "Repair" , { "target" : target , "autocontinue" : autocontinue , "force" : true } , queued , pushFront ) ;
2010-03-12 13:41:40 -08:00
} ;
2012-05-08 16:00:14 -07:00
/ * *
* Adds flee order to the queue , not forced , so it can be
* interrupted by attacks .
* /
2021-03-05 22:12:07 -08:00
UnitAI . prototype . Flee = function ( target , queued , pushFront )
2011-05-17 09:26:47 -07:00
{
2021-03-05 22:12:07 -08:00
this . AddOrder ( "Flee" , { "target" : target , "force" : false } , queued , pushFront ) ;
2011-05-17 09:26:47 -07:00
} ;
2011-05-02 08:03:01 -07:00
UnitAI . prototype . Cheer = function ( )
{
2020-09-10 09:37:14 -07:00
this . PushOrderFront ( "Cheer" , { "force" : false } ) ;
2011-05-02 08:03:01 -07:00
} ;
2021-03-05 22:12:07 -08:00
UnitAI . prototype . Pack = function ( queued , pushFront )
2012-11-30 16:34:03 -08:00
{
if ( this . CanPack ( ) )
2021-03-05 22:12:07 -08:00
this . AddOrder ( "Pack" , { "force" : true } , queued , pushFront ) ;
2012-11-30 16:34:03 -08:00
} ;
2021-03-05 22:12:07 -08:00
UnitAI . prototype . Unpack = function ( queued , pushFront )
2012-11-30 16:34:03 -08:00
{
if ( this . CanUnpack ( ) )
2021-03-05 22:12:07 -08:00
this . AddOrder ( "Unpack" , { "force" : true } , queued , pushFront ) ;
2012-11-30 16:34:03 -08:00
} ;
2021-03-05 22:12:07 -08:00
UnitAI . prototype . CancelPack = function ( queued , pushFront )
2012-11-30 16:34:03 -08:00
{
var cmpPack = Engine . QueryInterface ( this . entity , IID _Pack ) ;
if ( cmpPack && cmpPack . IsPacking ( ) && ! cmpPack . IsPacked ( ) )
2021-03-05 22:12:07 -08:00
this . AddOrder ( "CancelPack" , { "force" : true } , queued , pushFront ) ;
2012-11-30 16:34:03 -08:00
} ;
2021-03-05 22:12:07 -08:00
UnitAI . prototype . CancelUnpack = function ( queued , pushFront )
2012-11-30 16:34:03 -08:00
{
var cmpPack = Engine . QueryInterface ( this . entity , IID _Pack ) ;
if ( cmpPack && cmpPack . IsPacking ( ) && cmpPack . IsPacked ( ) )
2021-03-05 22:12:07 -08:00
this . AddOrder ( "CancelUnpack" , { "force" : true } , queued , pushFront ) ;
2012-11-30 16:34:03 -08:00
} ;
2010-08-04 14:15:41 -07:00
UnitAI . prototype . SetStance = function ( stance )
{
if ( g _Stances [ stance ] )
2017-05-19 13:28:21 -07:00
{
2010-08-04 14:15:41 -07:00
this . stance = stance ;
2017-05-19 13:28:21 -07:00
Engine . PostMessage ( this . entity , MT _UnitStanceChanged , { "to" : this . stance } ) ;
}
2010-08-04 14:15:41 -07:00
else
error ( "UnitAI: Setting to invalid stance '" + stance + "'" ) ;
2012-04-17 13:22:13 -07:00
} ;
2011-06-17 15:13:39 -07:00
UnitAI . prototype . SwitchToStance = function ( stance )
{
var cmpPosition = Engine . QueryInterface ( this . entity , IID _Position ) ;
if ( ! cmpPosition || ! cmpPosition . IsInWorld ( ) )
return ;
var pos = cmpPosition . GetPosition ( ) ;
this . SetHeldPosition ( pos . x , pos . z ) ;
this . SetStance ( stance ) ;
2012-08-07 22:32:53 -07:00
// Reset the range queries, since the range depends on stance.
this . SetupRangeQueries ( ) ;
2012-04-17 13:22:13 -07:00
} ;
2011-06-17 15:13:39 -07:00
2015-02-11 10:34:06 -08:00
UnitAI . prototype . SetTurretStance = function ( )
{
2021-12-09 08:35:03 -08:00
this . SetImmobile ( ) ;
2015-02-11 10:34:06 -08:00
this . previousStance = undefined ;
if ( this . GetStance ( ) . respondStandGround )
return ;
for ( let stance in g _Stances )
{
if ( ! g _Stances [ stance ] . respondStandGround )
continue ;
this . previousStance = this . GetStanceName ( ) ;
this . SwitchToStance ( stance ) ;
return ;
}
} ;
UnitAI . prototype . ResetTurretStance = function ( )
{
2021-12-09 08:35:03 -08:00
this . SetMobile ( ) ;
2015-02-11 10:34:06 -08:00
if ( ! this . previousStance )
return ;
this . SwitchToStance ( this . previousStance ) ;
this . previousStance = undefined ;
} ;
2011-06-24 05:35:15 -07:00
/ * *
2020-07-23 23:07:27 -07:00
* Resets the losRangeQuery .
* @ return { boolean } - Whether there are targets in range that we ought to react upon .
* /
UnitAI . prototype . FindSightedEnemies = function ( )
{
if ( ! this . losRangeQuery )
return false ;
let cmpRangeManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _RangeManager ) ;
return this . RespondToSightedEntities ( cmpRangeManager . ResetActiveQuery ( this . losRangeQuery ) ) ;
} ;
/ * *
* Resets losHealRangeQuery , and if there are some targets in range that we can heal
* then we start healing and this returns true ; otherwise , returns false .
* /
UnitAI . prototype . FindNewHealTargets = function ( )
{
if ( ! this . losHealRangeQuery )
return false ;
let cmpRangeManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _RangeManager ) ;
return this . RespondToHealableEntities ( cmpRangeManager . ResetActiveQuery ( this . losHealRangeQuery ) ) ;
} ;
/ * *
* Resets losAttackRangeQuery , and if there are some targets in range that we can
2011-06-24 05:35:15 -07:00
* attack then we start attacking and this returns true ; otherwise , returns false .
* /
UnitAI . prototype . FindNewTargets = function ( )
2013-12-26 08:09:32 -08:00
{
2020-07-23 23:07:27 -07:00
if ( ! this . losAttackRangeQuery )
2013-12-26 08:09:32 -08:00
return false ;
if ( ! this . GetStance ( ) . targetVisibleEnemies )
return false ;
2020-07-23 23:07:27 -07:00
let cmpRangeManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _RangeManager ) ;
return this . AttackEntitiesByPreference ( cmpRangeManager . ResetActiveQuery ( this . losAttackRangeQuery ) ) ;
2013-12-26 08:09:32 -08:00
} ;
UnitAI . prototype . FindWalkAndFightTargets = function ( )
2011-06-17 15:13:39 -07:00
{
2013-11-30 10:23:32 -08:00
if ( this . IsFormationController ( ) )
2022-03-31 22:46:13 -07:00
return this . CallMemberFunction ( "FindWalkAndFightTargets" , null ) ;
2013-11-30 10:23:32 -08:00
2021-03-22 06:27:33 -07:00
let cmpAttack = Engine . QueryInterface ( this . entity , IID _Attack ) ;
let entities ;
if ( ! this . losAttackRangeQuery || ! this . GetStance ( ) . targetVisibleEnemies || ! cmpAttack )
entities = [ ] ;
else
2013-12-26 08:09:32 -08:00
{
2021-03-22 06:27:33 -07:00
let cmpRangeManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _RangeManager ) ;
entities = cmpRangeManager . ResetActiveQuery ( this . losAttackRangeQuery ) ;
}
let attackfilter = e => {
if ( this ? . order ? . data ? . targetClasses )
2014-08-28 01:59:45 -07:00
{
2021-03-22 06:27:33 -07:00
let cmpIdentity = Engine . QueryInterface ( e , IID _Identity ) ;
let targetClasses = this . order . data . targetClasses ;
2021-03-20 02:02:22 -07:00
if ( cmpIdentity && targetClasses . attack &&
! MatchesClassList ( cmpIdentity . GetClassesList ( ) , targetClasses . attack ) )
2021-03-22 06:27:33 -07:00
return false ;
2021-03-20 02:02:22 -07:00
if ( cmpIdentity && targetClasses . avoid &&
MatchesClassList ( cmpIdentity . GetClassesList ( ) , targetClasses . avoid ) )
2021-03-22 06:27:33 -07:00
return false ;
2015-01-20 10:31:57 -08:00
// Only used by the AIs to prevent some choices of targets
2021-03-22 06:27:33 -07:00
if ( targetClasses . vetoEntities && targetClasses . vetoEntities [ e ] )
return false ;
2014-08-28 01:59:45 -07:00
}
2020-07-23 23:07:27 -07:00
let cmpOwnership = Engine . QueryInterface ( e , IID _Ownership ) ;
2014-01-06 11:35:51 -08:00
if ( cmpOwnership && cmpOwnership . GetOwner ( ) > 0 )
2014-01-03 12:24:02 -08:00
return true ;
2020-07-23 23:07:27 -07:00
let cmpUnitAI = Engine . QueryInterface ( e , IID _UnitAI ) ;
2014-01-03 12:24:02 -08:00
return cmpUnitAI && ( ! cmpUnitAI . IsAnimal ( ) || cmpUnitAI . IsDangerousAnimal ( ) ) ;
2014-01-02 06:17:46 -08:00
} ;
2022-03-31 22:46:13 -07:00
const attack = target => {
const order = {
"target" : target ,
"force" : false ,
2023-01-09 06:29:06 -08:00
"allowCapture" : this . order ? . data ? . allowCapture || this . DEFAULT _CAPTURE
2022-03-31 22:46:13 -07:00
} ;
if ( this . IsFormationMember ( ) )
this . ReplaceOrder ( "Attack" , order ) ;
else
this . PushOrderFront ( "Attack" , order ) ;
} ;
2021-03-22 06:27:33 -07:00
let prefs = { } ;
let bestPref ;
let targets = [ ] ;
let pref ;
for ( let v of entities )
{
if ( this . CanAttack ( v ) && attackfilter ( v ) )
{
pref = cmpAttack . GetPreference ( v ) ;
if ( pref === 0 )
{
2022-03-31 22:46:13 -07:00
attack ( v ) ;
2021-03-22 06:27:33 -07:00
return true ;
}
targets . push ( v ) ;
}
prefs [ v ] = pref ;
if ( pref !== undefined && ( bestPref === undefined || pref < bestPref ) )
bestPref = pref ;
}
for ( let targ of targets )
{
if ( prefs [ targ ] !== bestPref )
continue ;
2022-03-31 22:46:13 -07:00
attack ( targ ) ;
2021-03-22 06:27:33 -07:00
return true ;
}
2012-08-07 22:32:53 -07:00
2021-03-22 06:27:33 -07:00
// healers on a walk-and-fight order should heal injured units
if ( this . IsHealer ( ) )
return this . FindNewHealTargets ( ) ;
return false ;
2011-06-17 15:13:39 -07:00
} ;
2012-04-17 13:22:13 -07:00
UnitAI . prototype . GetQueryRange = function ( iid )
2011-06-17 15:13:39 -07:00
{
2020-01-30 13:05:59 -08:00
let ret = { "min" : 0 , "max" : 0 } ;
let cmpVision = Engine . QueryInterface ( this . entity , IID _Vision ) ;
if ( ! cmpVision )
return ret ;
let visionRange = cmpVision . GetRange ( ) ;
2020-07-23 23:07:27 -07:00
if ( iid === IID _Vision )
{
ret . max = visionRange ;
return ret ;
}
2011-06-17 15:13:39 -07:00
if ( this . GetStance ( ) . respondStandGround )
{
2020-01-30 13:05:59 -08:00
let range = this . GetRange ( iid ) ;
if ( ! range )
2018-03-18 11:49:06 -07:00
return ret ;
2011-06-17 15:13:39 -07:00
ret . min = range . min ;
2020-01-30 13:05:59 -08:00
ret . max = Math . min ( range . max , visionRange ) ;
2011-06-17 15:13:39 -07:00
}
else if ( this . GetStance ( ) . respondChase )
2020-01-30 13:05:59 -08:00
ret . max = visionRange ;
2011-06-17 15:13:39 -07:00
else if ( this . GetStance ( ) . respondHoldGround )
{
2020-01-30 13:05:59 -08:00
let range = this . GetRange ( iid ) ;
2020-01-31 12:00:06 -08:00
if ( ! range )
return ret ;
2020-01-30 13:05:59 -08:00
ret . max = Math . min ( range . max + visionRange / 2 , visionRange ) ;
2011-06-17 15:13:39 -07:00
}
2012-04-17 13:22:13 -07:00
// We probably have stance 'passive' and we wouldn't have a range,
// but as it is the default for healers we need to set it to something sane.
else if ( iid === IID _Heal )
2020-01-30 13:05:59 -08:00
ret . max = visionRange ;
2011-06-17 15:13:39 -07:00
return ret ;
2010-08-04 14:15:41 -07:00
} ;
UnitAI . prototype . GetStance = function ( )
{
return g _Stances [ this . stance ] ;
} ;
2018-03-10 11:12:23 -08:00
UnitAI . prototype . GetSelectableStances = function ( )
2014-06-16 11:34:27 -07:00
{
2015-02-11 10:34:06 -08:00
if ( this . IsTurret ( ) )
return [ ] ;
2018-03-10 11:12:23 -08:00
return Object . keys ( g _Stances ) . filter ( key => g _Stances [ key ] . selectable ) ;
2014-06-16 11:34:27 -07:00
} ;
2011-06-17 15:13:39 -07:00
UnitAI . prototype . GetStanceName = function ( )
{
return this . stance ;
} ;
2019-04-19 03:04:50 -07:00
/ *
2023-06-14 23:56:41 -07:00
* Make the unit run .
2019-04-19 03:04:50 -07:00
* /
2023-06-14 23:56:41 -07:00
UnitAI . prototype . Run = function ( )
2019-04-19 03:04:50 -07:00
{
2023-06-14 23:56:41 -07:00
this . SetSpeedMultiplier ( this . GetRunMultiplier ( ) ) ;
2019-04-19 03:04:50 -07:00
} ;
2012-05-08 16:00:14 -07:00
2023-06-14 23:56:41 -07:00
/ * *
* @ param { number } speed - The multiplier to set the speed to .
* /
2019-05-13 09:47:51 -07:00
UnitAI . prototype . SetSpeedMultiplier = function ( speed )
2012-05-08 16:00:14 -07:00
{
2023-06-14 23:56:41 -07:00
Engine . QueryInterface ( this . entity , IID _UnitMotion ) ? . SetSpeedMultiplier ( speed ) ;
2012-05-08 16:00:14 -07:00
} ;
2020-05-20 12:39:36 -07:00
/ * *
* Try to match the targets current movement speed .
*
* @ param { number } target - The entity ID of the target to match .
* @ param { boolean } mayRun - Whether the entity is allowed to run to match the speed .
* /
UnitAI . prototype . TryMatchTargetSpeed = function ( target , mayRun = true )
{
let cmpUnitMotionTarget = Engine . QueryInterface ( target , IID _UnitMotion ) ;
if ( cmpUnitMotionTarget )
{
let targetSpeed = cmpUnitMotionTarget . GetCurrentSpeed ( ) ;
2020-05-20 12:47:40 -07:00
if ( targetSpeed )
this . SetSpeedMultiplier ( Math . min ( mayRun ? this . GetRunMultiplier ( ) : 1 , targetSpeed / this . GetWalkSpeed ( ) ) ) ;
2020-05-20 12:39:36 -07:00
}
} ;
2019-07-01 23:49:27 -07:00
/ *
* Remember the position of the target ( in lastPos ) , if any , in case it disappears later
* and we want to head to its last known position .
* @ param orderData - The order data to set this on . Defaults to this . order . data
* /
UnitAI . prototype . RememberTargetPosition = function ( orderData )
{
if ( ! orderData )
orderData = this . order . data ;
let cmpPosition = Engine . QueryInterface ( orderData . target , IID _Position ) ;
if ( cmpPosition && cmpPosition . IsInWorld ( ) )
orderData . lastPos = cmpPosition . GetPosition ( ) ;
} ;
2012-05-08 16:00:14 -07:00
UnitAI . prototype . SetHeldPosition = function ( x , z )
{
2021-03-20 02:02:22 -07:00
this . heldPosition = { "x" : x , "z" : z } ;
2012-05-08 16:00:14 -07:00
} ;
2013-11-30 09:30:08 -08:00
UnitAI . prototype . SetHeldPositionOnEntity = function ( entity )
{
var cmpPosition = Engine . QueryInterface ( this . entity , IID _Position ) ;
if ( ! cmpPosition || ! cmpPosition . IsInWorld ( ) )
return ;
var pos = cmpPosition . GetPosition ( ) ;
this . SetHeldPosition ( pos . x , pos . z ) ;
} ;
2014-04-14 18:12:48 -07:00
UnitAI . prototype . GetHeldPosition = function ( )
2012-05-08 16:00:14 -07:00
{
return this . heldPosition ;
} ;
UnitAI . prototype . WalkToHeldPosition = function ( )
{
if ( this . heldPosition )
{
2021-03-05 22:12:07 -08:00
this . AddOrder ( "Walk" , { "x" : this . heldPosition . x , "z" : this . heldPosition . z , "force" : false } , false , false ) ;
2012-05-08 16:00:14 -07:00
return true ;
}
return false ;
} ;
2021-03-20 02:02:22 -07:00
// Helper functions
2010-07-21 09:09:58 -07:00
2020-01-30 13:05:59 -08:00
/ * *
* General getter for ranges .
*
* @ param { number } iid
2021-03-30 04:17:46 -07:00
* @ param { number } target - [ Optional ]
2020-01-30 13:05:59 -08:00
* @ param { string } type - [ Optional ]
* @ return { Object | undefined } - The range in the form
* { "min" : number , "max" : number }
* Returns undefined when the entity does not have the requested component .
* /
2021-03-30 04:17:46 -07:00
UnitAI . prototype . GetRange = function ( iid , type , target )
2020-01-30 13:05:59 -08:00
{
let component = Engine . QueryInterface ( this . entity , iid ) ;
if ( ! component )
2020-01-31 12:00:06 -08:00
return undefined ;
2020-01-30 13:05:59 -08:00
2021-03-30 04:17:46 -07:00
return component . GetRange ( type , target ) ;
2021-03-20 02:02:22 -07:00
} ;
2020-01-30 13:05:59 -08:00
2017-12-23 07:53:00 -08:00
UnitAI . prototype . CanAttack = function ( target )
2010-02-12 14:46:53 -08:00
{
2010-09-03 02:55:14 -07:00
// Formation controllers should always respond to commands
// (then the individual units can make up their own minds)
if ( this . IsFormationController ( ) )
return true ;
2017-12-23 07:53:00 -08:00
let cmpAttack = Engine . QueryInterface ( this . entity , IID _Attack ) ;
return cmpAttack && cmpAttack . CanAttack ( target ) ;
2010-07-21 09:09:58 -07:00
} ;
2010-03-12 13:41:40 -08:00
2010-10-23 15:43:15 -07:00
UnitAI . prototype . CanGarrison = function ( target )
{
2011-11-21 16:16:35 -08:00
// Formation controllers should always respond to commands
2021-03-27 04:22:34 -07:00
// (then the individual units can make up their own minds).
2011-11-21 16:16:35 -08:00
if ( this . IsFormationController ( ) )
return true ;
2021-03-27 04:22:34 -07:00
let cmpGarrisonable = Engine . QueryInterface ( this . entity , IID _Garrisonable ) ;
return cmpGarrisonable && cmpGarrisonable . CanGarrison ( target ) ;
2010-10-23 15:43:15 -07:00
} ;
2010-07-21 09:09:58 -07:00
UnitAI . prototype . CanGather = function ( target )
{
2010-09-03 02:55:14 -07:00
// Formation controllers should always respond to commands
2021-03-27 04:22:34 -07:00
// (then the individual units can make up their own minds).
2010-09-03 02:55:14 -07:00
if ( this . IsFormationController ( ) )
return true ;
2021-03-27 04:22:34 -07:00
let cmpResourceGatherer = Engine . QueryInterface ( this . entity , IID _ResourceGatherer ) ;
return cmpResourceGatherer && cmpResourceGatherer . CanGather ( target ) ;
2010-02-12 14:46:53 -08:00
} ;
2012-04-17 13:22:13 -07:00
UnitAI . prototype . CanHeal = function ( target )
{
// Formation controllers should always respond to commands
// (then the individual units can make up their own minds)
if ( this . IsFormationController ( ) )
return true ;
2020-05-10 14:54:39 -07:00
let cmpHeal = Engine . QueryInterface ( this . entity , IID _Heal ) ;
return cmpHeal && cmpHeal . CanHeal ( target ) ;
2012-04-17 13:22:13 -07:00
} ;
2020-06-04 04:01:06 -07:00
/ * *
* Check if the entity can return carried resources at @ param target
* @ param checkCarriedResource check we are carrying resources
* @ param cmpResourceGatherer if present , use this directly instead of re - querying .
* /
UnitAI . prototype . CanReturnResource = function ( target , checkCarriedResource , cmpResourceGatherer = undefined )
2010-11-13 11:15:29 -08:00
{
// Formation controllers should always respond to commands
2021-03-27 04:22:34 -07:00
// (then the individual units can make up their own minds).
2010-11-13 11:15:29 -08:00
if ( this . IsFormationController ( ) )
return true ;
if ( ! cmpResourceGatherer )
2020-06-04 04:01:06 -07:00
cmpResourceGatherer = Engine . QueryInterface ( this . entity , IID _ResourceGatherer ) ;
2010-11-13 11:15:29 -08:00
2021-03-27 04:22:34 -07:00
return cmpResourceGatherer && cmpResourceGatherer . CanReturnResource ( target , checkCarriedResource ) ;
2010-11-13 11:15:29 -08:00
} ;
2012-03-08 12:42:28 -08:00
UnitAI . prototype . CanTrade = function ( target )
{
// Formation controllers should always respond to commands
2021-03-27 04:22:34 -07:00
// (then the individual units can make up their own minds).
2012-03-08 12:42:28 -08:00
if ( this . IsFormationController ( ) )
return true ;
2021-03-27 04:22:34 -07:00
let cmpTrader = Engine . QueryInterface ( this . entity , IID _Trader ) ;
2015-12-06 12:51:30 -08:00
return cmpTrader && cmpTrader . CanTrade ( target ) ;
2012-04-28 12:25:44 -07:00
} ;
2012-03-08 12:42:28 -08:00
2010-09-03 02:55:14 -07:00
UnitAI . prototype . CanRepair = function ( target )
{
// Formation controllers should always respond to commands
2021-03-27 04:22:34 -07:00
// (then the individual units can make up their own minds).
2010-09-03 02:55:14 -07:00
if ( this . IsFormationController ( ) )
return true ;
2021-03-27 04:22:34 -07:00
let cmpBuilder = Engine . QueryInterface ( this . entity , IID _Builder ) ;
return cmpBuilder && cmpBuilder . CanRepair ( target ) ;
2010-09-03 02:55:14 -07:00
} ;
2021-03-26 03:18:30 -07:00
UnitAI . prototype . CanOccupyTurret = function ( target )
{
// Formation controllers should always respond to commands
// (then the individual units can make up their own minds).
if ( this . IsFormationController ( ) )
return true ;
let cmpTurretable = Engine . QueryInterface ( this . entity , IID _Turretable ) ;
return cmpTurretable && cmpTurretable . CanOccupy ( target ) ;
} ;
2012-11-30 16:34:03 -08:00
UnitAI . prototype . CanPack = function ( )
{
2021-03-27 04:22:34 -07:00
let cmpPack = Engine . QueryInterface ( this . entity , IID _Pack ) ;
return cmpPack && cmpPack . CanPack ( ) ;
2012-11-30 16:34:03 -08:00
} ;
UnitAI . prototype . CanUnpack = function ( )
{
2021-03-27 04:22:34 -07:00
let cmpPack = Engine . QueryInterface ( this . entity , IID _Pack ) ;
return cmpPack && cmpPack . CanUnpack ( ) ;
2012-11-30 16:34:03 -08:00
} ;
UnitAI . prototype . IsPacking = function ( )
{
2021-03-27 04:22:34 -07:00
let cmpPack = Engine . QueryInterface ( this . entity , IID _Pack ) ;
2018-03-27 10:56:32 -07:00
return cmpPack && cmpPack . IsPacking ( ) ;
2012-11-30 16:34:03 -08:00
} ;
2021-03-20 02:02:22 -07:00
// Formation specific functions
2015-10-05 13:40:14 -07:00
UnitAI . prototype . IsAttackingAsFormation = function ( )
{
var cmpAttack = Engine . QueryInterface ( this . entity , IID _Attack ) ;
2021-03-20 02:02:22 -07:00
return cmpAttack && cmpAttack . CanAttackAsFormation ( ) &&
this . GetCurrentState ( ) == "FORMATIONCONTROLLER.COMBAT.ATTACKING" ;
2015-10-05 13:40:14 -07:00
} ;
2011-02-27 05:34:22 -08:00
UnitAI . prototype . MoveRandomly = function ( distance )
{
2021-02-27 12:13:40 -08:00
// To minimize drift all across the map, describe circles
2017-09-24 02:44:59 -07:00
// approximated by polygons.
// And to avoid getting stuck in obstacles or narrow spaces, each side
// of the polygon is obtained by trying to go away from a point situated
// half a meter backwards of the current position, after rotation.
// We also add a fluctuation on the length of each side of the polygon (dist)
// which, in addition to making the move more random, helps escaping narrow spaces
// with bigger values of dist.
let cmpPosition = Engine . QueryInterface ( this . entity , IID _Position ) ;
let cmpUnitMotion = Engine . QueryInterface ( this . entity , IID _UnitMotion ) ;
if ( ! cmpPosition || ! cmpPosition . IsInWorld ( ) || ! cmpUnitMotion )
2011-02-27 05:34:22 -08:00
return ;
2017-09-24 02:44:59 -07:00
let pos = cmpPosition . GetPosition ( ) ;
let ang = cmpPosition . GetRotation ( ) . y ;
2011-02-27 05:34:22 -08:00
2017-09-24 02:44:59 -07:00
if ( ! this . roamAngle )
{
this . roamAngle = ( randBool ( ) ? 1 : - 1 ) * Math . PI / 6 ;
ang -= this . roamAngle / 2 ;
this . startAngle = ang ;
}
else if ( Math . abs ( ( ang - this . startAngle + Math . PI ) % ( 2 * Math . PI ) - Math . PI ) < Math . abs ( this . roamAngle / 2 ) )
this . roamAngle *= randBool ( ) ? 1 : - 1 ;
let halfDelta = randFloat ( this . roamAngle / 4 , this . roamAngle * 3 / 4 ) ;
// First half rotation to decrease the impression of immediate rotation
ang += halfDelta ;
cmpUnitMotion . FaceTowardsPoint ( pos . x + 0.5 * Math . sin ( ang ) , pos . z + 0.5 * Math . cos ( ang ) ) ;
// Then second half of the rotation
ang += halfDelta ;
let dist = randFloat ( 0.5 , 1.5 ) * distance ;
2019-06-30 11:53:25 -07:00
cmpUnitMotion . MoveToPointRange ( pos . x - 0.5 * Math . sin ( ang ) , pos . z - 0.5 * Math . cos ( ang ) , dist , - 1 ) ;
2011-02-27 05:34:22 -08:00
} ;
2013-09-30 16:52:22 -07:00
UnitAI . prototype . SetFacePointAfterMove = function ( val )
{
var cmpMotion = Engine . QueryInterface ( this . entity , IID _UnitMotion ) ;
if ( cmpMotion )
cmpMotion . SetFacePointAfterMove ( val ) ;
} ;
2020-07-19 03:42:45 -07:00
UnitAI . prototype . GetFacePointAfterMove = function ( )
{
let cmpUnitMotion = Engine . QueryInterface ( this . entity , IID _UnitMotion ) ;
return cmpUnitMotion && cmpUnitMotion . GetFacePointAfterMove ( ) ;
2021-03-20 02:02:22 -07:00
} ;
2020-07-19 03:42:45 -07:00
2012-08-08 19:32:56 -07:00
UnitAI . prototype . AttackEntitiesByPreference = function ( ents )
2012-05-01 15:20:08 -07:00
{
2015-11-15 05:19:21 -08:00
if ( ! ents . length )
return false ;
2012-08-08 19:32:56 -07:00
2021-02-27 12:13:40 -08:00
let cmpAttack = Engine . QueryInterface ( this . entity , IID _Attack ) ;
2012-08-08 19:32:56 -07:00
if ( ! cmpAttack )
return false ;
2021-02-27 12:13:40 -08:00
let attackfilter = function ( e ) {
if ( ! cmpAttack . CanAttack ( e ) )
return false ;
let cmpOwnership = Engine . QueryInterface ( e , IID _Ownership ) ;
2014-01-06 11:35:51 -08:00
if ( cmpOwnership && cmpOwnership . GetOwner ( ) > 0 )
2014-01-03 12:24:02 -08:00
return true ;
2021-02-27 12:13:40 -08:00
let cmpUnitAI = Engine . QueryInterface ( e , IID _UnitAI ) ;
2014-01-03 12:24:02 -08:00
return cmpUnitAI && ( ! cmpUnitAI . IsAnimal ( ) || cmpUnitAI . IsDangerousAnimal ( ) ) ;
2012-08-08 19:32:56 -07:00
} ;
2012-08-07 22:32:53 -07:00
2015-11-15 05:19:21 -08:00
let entsByPreferences = { } ;
let preferences = [ ] ;
2015-11-16 10:59:10 -08:00
let entsWithoutPref = [ ] ;
2015-11-15 05:19:21 -08:00
for ( let ent of ents )
{
if ( ! attackfilter ( ent ) )
continue ;
let pref = cmpAttack . GetPreference ( ent ) ;
2023-06-14 00:52:30 -07:00
// If we match our best preference, we can try responding right away.
// This makes some common cases fast, like most soldiers having 'Human' as best preference,
// or ships having 'Ship'. And if there are no such targets, this doesn't do much more work.
if ( pref === 0 )
{
if ( this . RespondToTargetedEntities ( [ ent ] ) )
return true ;
}
else if ( pref === null || pref === undefined )
2015-11-16 10:59:10 -08:00
entsWithoutPref . push ( ent ) ;
else if ( ! entsByPreferences [ pref ] )
2015-11-15 05:19:21 -08:00
{
preferences . push ( pref ) ;
entsByPreferences [ pref ] = [ ent ] ;
}
else
entsByPreferences [ pref ] . push ( ent ) ;
}
2015-11-16 10:59:10 -08:00
if ( preferences . length )
{
preferences . sort ( ( a , b ) => a - b ) ;
for ( let pref of preferences )
if ( this . RespondToTargetedEntities ( entsByPreferences [ pref ] ) )
return true ;
}
return this . RespondToTargetedEntities ( entsWithoutPref ) ;
2012-05-01 15:20:08 -07:00
} ;
2014-01-11 04:19:43 -08:00
/ * *
2020-06-06 09:07:01 -07:00
* Call UnitAI . funcname ( args ) on all formation members .
2021-10-02 22:59:54 -07:00
* @ param resetFinishedEntities - If true , call ResetFinishedEntities first .
2020-06-06 09:07:01 -07:00
* If the controller wants to wait on its members to finish their order ,
* this needs to be reset before sending new orders ( in case they instafail )
* so it makes sense to do it here .
* Only set this to false if you 're sure it' s safe .
2014-01-11 04:19:43 -08:00
* /
2021-10-02 22:59:54 -07:00
UnitAI . prototype . CallMemberFunction = function ( funcname , args , resetFinishedEntities = true )
2014-01-11 04:19:43 -08:00
{
2022-03-31 22:46:13 -07:00
const cmpFormation = Engine . QueryInterface ( this . entity , IID _Formation ) ;
2014-01-11 04:19:43 -08:00
if ( ! cmpFormation )
2022-03-31 22:46:13 -07:00
return false ;
2015-08-29 15:49:44 -07:00
2021-10-02 22:59:54 -07:00
if ( resetFinishedEntities )
cmpFormation . ResetFinishedEntities ( ) ;
2020-06-06 09:07:01 -07:00
2022-03-31 22:46:13 -07:00
let result = false ;
2015-08-29 15:49:44 -07:00
cmpFormation . GetMembers ( ) . forEach ( ent => {
2022-03-31 22:46:13 -07:00
const cmpUnitAI = Engine . QueryInterface ( ent , IID _UnitAI ) ;
if ( cmpUnitAI [ funcname ] . apply ( cmpUnitAI , args ) )
result = true ;
2015-08-29 15:49:44 -07:00
} ) ;
2022-03-31 22:46:13 -07:00
return result ;
2014-01-11 04:19:43 -08:00
} ;
2020-09-10 09:37:14 -07:00
/ * *
* Call obj . funcname ( args ) on UnitAI components owned by player in given range .
* /
UnitAI . prototype . CallPlayerOwnedEntitiesFunctionInRange = function ( funcname , args , range )
{
let cmpOwnership = Engine . QueryInterface ( this . entity , IID _Ownership ) ;
if ( ! cmpOwnership )
return ;
let owner = cmpOwnership . GetOwner ( ) ;
if ( owner == INVALID _PLAYER )
return ;
let cmpRangeManager = Engine . QueryInterface ( SYSTEM _ENTITY , IID _RangeManager ) ;
2021-01-23 10:57:46 -08:00
let nearby = cmpRangeManager . ExecuteQuery ( this . entity , 0 , range , [ owner ] , IID _UnitAI , true ) ;
2020-09-10 09:37:14 -07:00
for ( let i = 0 ; i < nearby . length ; ++ i )
{
let cmpUnitAI = Engine . QueryInterface ( nearby [ i ] , IID _UnitAI ) ;
cmpUnitAI [ funcname ] . apply ( cmpUnitAI , args ) ;
}
} ;
2014-01-11 04:19:43 -08:00
/ * *
* Call obj . functname ( args ) on UnitAI components of all formation members ,
* and return true if all calls return true .
* /
UnitAI . prototype . TestAllMemberFunction = function ( funcname , args )
{
2020-12-29 02:08:55 -08:00
let cmpFormation = Engine . QueryInterface ( this . entity , IID _Formation ) ;
return cmpFormation && cmpFormation . GetMembers ( ) . every ( ent => {
let cmpUnitAI = Engine . QueryInterface ( ent , IID _UnitAI ) ;
2015-08-29 15:49:44 -07:00
return cmpUnitAI [ funcname ] . apply ( cmpUnitAI , args ) ;
} ) ;
2014-01-11 04:19:43 -08:00
} ;
2014-05-27 00:24:07 -07:00
UnitAI . prototype . UnitFsm = new FSM ( UnitAI . prototype . UnitFsmSpec ) ;
2010-02-05 14:00:39 -08:00
Engine . RegisterComponentType ( IID _UnitAI , "UnitAI" , UnitAI ) ;