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>" +
|
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;
|
|
|
|
|
|
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)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"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) {
|
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())
|
|
|
|
|
{
|
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) {
|
2019-07-17 11:11:15 -07:00
|
|
|
let cmpGarrisonHolder = Engine.QueryInterface(this.entity, IID_GarrisonHolder);
|
2013-11-08 15:22:59 -08:00
|
|
|
if (!cmpGarrisonHolder || cmpGarrisonHolder.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
|
|
|
|
2020-08-03 05:02:24 -07:00
|
|
|
let range = cmpGarrisonHolder.GetLoadingRange();
|
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");
|
2020-08-03 05:02:24 -07:00
|
|
|
else
|
|
|
|
|
this.SetNextState("INDIVIDUAL.PICKUP.APPROACHING");
|
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
|
|
|
|
2019-05-28 04:38:18 -07:00
|
|
|
if (!this.CheckTargetRangeExplicit(this.isGuardOf, 0, this.guardRange))
|
2013-11-30 09:30:08 -08:00
|
|
|
this.SetNextState("INDIVIDUAL.GUARD.ESCORTING");
|
|
|
|
|
else
|
|
|
|
|
this.SetNextState("INDIVIDUAL.GUARD.GUARDING");
|
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-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-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-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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the unit is full go to the nearest dropsite instead of trying to gather.
|
|
|
|
|
let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer);
|
2021-03-02 23:47:38 -08:00
|
|
|
if (cmpResourceGatherer && !cmpResourceGatherer.CanCarryMore(msg.data.type.generic))
|
2020-11-12 03:01:42 -08:00
|
|
|
{
|
|
|
|
|
let nearestDropsite = this.FindNearestDropsite(msg.data.type.generic);
|
|
|
|
|
if (nearestDropsite)
|
|
|
|
|
this.PushOrderFront("ReturnResource", {
|
|
|
|
|
"target": nearestDropsite,
|
|
|
|
|
"force": false,
|
|
|
|
|
"type": msg.data.type
|
|
|
|
|
});
|
|
|
|
|
// Players expect the unit to move, so walk to the target instead of trying to gather.
|
|
|
|
|
else if (!this.FinishOrder())
|
|
|
|
|
this.WalkToTarget(msg.data.target, false);
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
|
// Make sure we can attack the target, else we'll get very stuck
|
2021-03-03 00:21:00 -08:00
|
|
|
if (!this.GetBestAttackAgainst(msg.data.target, false))
|
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-03-03 00:21:00 -08:00
|
|
|
this.PushOrderFront("Attack", { "target": msg.data.target, "force": !!msg.data.force, "hunting": true, "allowCapture": false });
|
2021-02-27 22:29:53 -08:00
|
|
|
return ACCEPT_ORDER;
|
2010-10-06 14:37:55 -07:00
|
|
|
}
|
|
|
|
|
|
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");
|
2019-05-28 04:38:18 -07:00
|
|
|
else
|
|
|
|
|
this.SetNextState("INDIVIDUAL.GATHER.APPROACHING");
|
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) {
|
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
|
|
|
|
|
|
|
|
"Order.ReturnResource": function(msg) {
|
2020-06-04 04:01:06 -07:00
|
|
|
let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer);
|
2021-03-03 00:21:00 -08:00
|
|
|
if (this.CheckTargetRange(msg.data.target, IID_ResourceGatherer) &&
|
|
|
|
|
this.CanReturnResource(msg.data.target, true, cmpResourceGatherer))
|
2014-01-01 10:17:47 -08:00
|
|
|
{
|
2021-03-03 00:21:00 -08:00
|
|
|
cmpResourceGatherer.CommitResources(msg.data.target);
|
2020-06-04 04:01:06 -07:00
|
|
|
this.SetDefaultAnimationVariant();
|
2014-01-01 10:17:47 -08:00
|
|
|
|
2020-06-04 04:01:06 -07:00
|
|
|
// Our next order should always be a Gather,
|
|
|
|
|
// so just switch back to that order.
|
|
|
|
|
this.FinishOrder();
|
2014-01-01 10:17:47 -08:00
|
|
|
}
|
2021-02-27 22:29:53 -08:00
|
|
|
else
|
|
|
|
|
this.SetNextState("INDIVIDUAL.RETURNRESOURCE.APPROACHING");
|
|
|
|
|
return ACCEPT_ORDER;
|
2010-11-13 11:15:29 -08:00
|
|
|
},
|
2012-03-08 12:42:28 -08:00
|
|
|
|
|
|
|
|
"Order.Trade": function(msg) {
|
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");
|
2019-05-28 04:38:18 -07:00
|
|
|
else
|
|
|
|
|
this.SetNextState("INDIVIDUAL.REPAIR.APPROACHING");
|
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-03 00:21:00 -08:00
|
|
|
if (this.CheckGarrisonRange(msg.data.target))
|
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) {
|
|
|
|
|
let cmpTreasureCollecter = Engine.QueryInterface(this.entity, IID_TreasureCollecter);
|
|
|
|
|
if (!cmpTreasureCollecter || !cmpTreasureCollecter.CanCollect(msg.data.target))
|
|
|
|
|
return this.FinishOrder();
|
|
|
|
|
|
|
|
|
|
this.SetNextState("COLLECTTREASURE");
|
|
|
|
|
return ACCEPT_ORDER;
|
|
|
|
|
},
|
|
|
|
|
|
2021-03-03 10:20:49 -08:00
|
|
|
"Order.CollectTreasureNearPosition": function(msg) {
|
|
|
|
|
let nearbyTreasure = this.FindNearbyTreasure(msg.data.x, msg.data.z);
|
|
|
|
|
if (nearbyTreasure)
|
|
|
|
|
this.CollectTreasure(nearbyTreasure, oldData.autocontinue, true);
|
|
|
|
|
else
|
|
|
|
|
this.SetNextState("COLLECTTREASURE");
|
|
|
|
|
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) {
|
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) {
|
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) {
|
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-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-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-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) {
|
|
|
|
|
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 allowCapture = msg.data.allowCapture;
|
|
|
|
|
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
|
|
|
{
|
2020-12-05 23:22:51 -08:00
|
|
|
if (this.CanAttack(target) && 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
|
|
|
}
|
2018-01-13 01:39:43 -08:00
|
|
|
this.CallMemberFunction("Attack", [target, 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) {
|
2013-11-11 08:29:44 -08:00
|
|
|
if (!Engine.QueryInterface(msg.data.target, IID_GarrisonHolder))
|
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-11 08:29:44 -08:00
|
|
|
if (!this.CheckGarrisonRange(msg.data.target))
|
2012-05-18 14:31:57 -07:00
|
|
|
{
|
2013-11-11 08:29:44 -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();
|
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
|
|
|
}
|
2019-07-09 12:58:44 -07:00
|
|
|
this.PushOrderFront("Attack", { "target": msg.data.target, "force": !!msg.data.force, "hunting": true, "allowCapture": false, "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
|
|
|
},
|
|
|
|
|
|
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
|
|
|
|
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);
|
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");
|
2013-12-26 08:09:32 -08:00
|
|
|
this.FindWalkAndFightTargets();
|
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);
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"leave": function() {
|
|
|
|
|
this.StopMoving();
|
|
|
|
|
this.StopTimer();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"Timer": function(msg) {
|
|
|
|
|
this.FindWalkAndFightTargets();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"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;
|
|
|
|
|
}
|
|
|
|
|
this.FindWalkAndFightTargets();
|
|
|
|
|
++this.stopSurveying;
|
|
|
|
|
}
|
|
|
|
|
}
|
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-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.MoveToGarrisonRange(this.order.data.target))
|
|
|
|
|
{
|
|
|
|
|
this.FinishOrder();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2019-12-20 12:05:19 -08:00
|
|
|
|
|
|
|
|
// If the garrisonholder should pickup, warn it so it can take needed action.
|
|
|
|
|
let cmpGarrisonHolder = Engine.QueryInterface(this.order.data.target, IID_GarrisonHolder);
|
|
|
|
|
if (cmpGarrisonHolder && cmpGarrisonHolder.CanPickup(this.entity))
|
|
|
|
|
{
|
|
|
|
|
this.pickup = this.order.data.target; // temporary, deleted in "leave"
|
|
|
|
|
Engine.PostMessage(this.pickup, MT_PickupRequested, { "entity": this.entity });
|
|
|
|
|
}
|
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() {
|
2014-01-11 04:19:43 -08:00
|
|
|
this.CallMemberFunction("Garrison", [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) {
|
2020-04-07 10:35:25 -07:00
|
|
|
let target = this.order.data.target;
|
|
|
|
|
let allowCapture = this.order.data.allowCapture;
|
|
|
|
|
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) {
|
2020-04-07 10:35:25 -07:00
|
|
|
let target = this.order.data.target;
|
|
|
|
|
let allowCapture = this.order.data.allowCapture;
|
|
|
|
|
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);
|
|
|
|
|
if (cmpFormation && !cmpFormation.AreAllMembersWaiting())
|
2012-12-01 17:52:27 -08:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (this.FinishOrder())
|
2013-02-25 13:56:24 -08:00
|
|
|
{
|
2013-12-26 08:09:32 -08:00
|
|
|
if (this.IsWalkingAndFighting())
|
|
|
|
|
this.FindWalkAndFightTargets();
|
2012-12-01 17:52:27 -08:00
|
|
|
return;
|
2013-02-25 13:56:24 -08:00
|
|
|
}
|
2020-07-07 03:24:58 -07:00
|
|
|
return;
|
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);
|
2020-07-07 03:24:58 -07:00
|
|
|
if (cmpFormation)
|
|
|
|
|
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();
|
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))
|
2015-04-21 10:58:07 -07:00
|
|
|
this.PushOrderFront("Attack", { "target": msg.data.attacker, "force": false, "allowCapture": true });
|
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
|
|
|
},
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
this.FindWalkAndFightTargets();
|
|
|
|
|
++this.stopSurveying;
|
|
|
|
|
}
|
|
|
|
|
}
|
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.ResetSpeedMultiplier();
|
|
|
|
|
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");
|
|
|
|
|
|
2019-05-13 09:47:51 -07:00
|
|
|
this.SetSpeedMultiplier(this.GetRunMultiplier());
|
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-13 09:47:51 -07:00
|
|
|
this.ResetSpeedMultiplier();
|
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,
|
|
|
|
|
// Force to true - otherwise structures might be attacked instead of captured,
|
|
|
|
|
// which is generally not expected (attacking units usually has allowCapture false).
|
|
|
|
|
"allowCapture": true
|
|
|
|
|
});
|
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;
|
2019-07-01 23:49:27 -07:00
|
|
|
if (!this.CanAttack(target))
|
2014-01-03 04:49:04 -08:00
|
|
|
{
|
2019-07-01 23:49:27 -07:00
|
|
|
this.SetNextState("COMBAT.FINDINGNEWTARGET");
|
|
|
|
|
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
|
|
|
|
|
|
|
|
this.SetNextState("COMBAT.APPROACHING");
|
|
|
|
|
return true;
|
2014-01-03 04:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
2019-07-01 23:49:27 -07:00
|
|
|
let cmpAttack = Engine.QueryInterface(this.entity, IID_Attack);
|
2013-06-26 07:15:03 -07:00
|
|
|
this.attackTimers = cmpAttack.GetTimers(this.order.data.attackType);
|
2010-07-21 09:09:58 -07:00
|
|
|
|
2012-08-28 13:02:03 -07:00
|
|
|
// If the repeat time since the last attack hasn't elapsed,
|
|
|
|
|
// delay this attack to avoid attacking too fast.
|
2019-07-01 23:49:27 -07:00
|
|
|
let prepare = this.attackTimers.prepare;
|
2012-08-28 13:02:03 -07:00
|
|
|
if (this.lastAttacked)
|
|
|
|
|
{
|
2019-07-01 23:49:27 -07:00
|
|
|
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
|
|
|
|
|
let repeatLeft = this.lastAttacked + this.attackTimers.repeat - cmpTimer.GetTime();
|
2012-08-28 13:02:03 -07:00
|
|
|
prepare = Math.max(prepare, repeatLeft);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-10 02:03:15 -08:00
|
|
|
if (!this.formationAnimationVariant)
|
2019-07-08 11:23:44 -07:00
|
|
|
this.SetAnimationVariant("combat");
|
|
|
|
|
|
2015-09-24 14:57:43 -07:00
|
|
|
this.oldAttackType = this.order.data.attackType;
|
2019-07-08 11:23:44 -07:00
|
|
|
this.SelectAnimation("attack_" + this.order.data.attackType.toLowerCase());
|
2012-08-28 13:02:03 -07:00
|
|
|
this.SetAnimationSync(prepare, this.attackTimers.repeat);
|
|
|
|
|
this.StartTimer(prepare, this.attackTimers.repeat);
|
2010-07-21 09:09:58 -07:00
|
|
|
// TODO: we should probably only bother syncing projectile attacks, not melee
|
|
|
|
|
|
2012-08-28 13:02:03 -07:00
|
|
|
// If using a non-default prepare time, re-sync the animation when the timer runs.
|
2019-07-01 23:49:27 -07:00
|
|
|
this.resyncAnimation = prepare != this.attackTimers.prepare;
|
2011-06-06 15:08:26 -07:00
|
|
|
|
|
|
|
|
this.FaceTowardsTarget(this.order.data.target);
|
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);
|
2010-07-21 09:09:58 -07:00
|
|
|
this.StopTimer();
|
2019-07-09 12:56:28 -07:00
|
|
|
this.ResetAnimation();
|
2010-07-21 09:09:58 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"Timer": function(msg) {
|
2019-07-01 23:49:27 -07:00
|
|
|
let target = this.order.data.target;
|
2020-06-05 23:19:15 -07:00
|
|
|
let attackType = this.order.data.attackType;
|
2019-07-01 23:49:27 -07:00
|
|
|
|
|
|
|
|
if (!this.CanAttack(target))
|
2010-07-21 09:09:58 -07:00
|
|
|
{
|
2019-07-01 23:49:27 -07:00
|
|
|
this.SetNextState("COMBAT.FINDINGNEWTARGET");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-09-06 12:47:48 -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);
|
2012-08-28 13:02:03 -07:00
|
|
|
|
2019-07-01 23:49:27 -07:00
|
|
|
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
|
|
|
|
|
this.lastAttacked = cmpTimer.GetTime() - msg.lateness;
|
2016-02-22 09:01:16 -08:00
|
|
|
|
2019-07-01 23:49:27 -07:00
|
|
|
this.FaceTowardsTarget(target);
|
2012-08-28 13:02:03 -07:00
|
|
|
|
2019-07-01 23:49:27 -07:00
|
|
|
// BuildingAI has it's own attack-routine
|
|
|
|
|
let cmpBuildingAI = Engine.QueryInterface(this.entity, IID_BuildingAI);
|
|
|
|
|
if (!cmpBuildingAI)
|
|
|
|
|
{
|
|
|
|
|
let cmpAttack = Engine.QueryInterface(this.entity, IID_Attack);
|
2020-06-05 23:19:15 -07:00
|
|
|
cmpAttack.PerformAttack(attackType, target);
|
2019-07-01 23:49:27 -07:00
|
|
|
}
|
|
|
|
|
|
2020-06-05 23:19:15 -07:00
|
|
|
// PerformAttack might have triggered messages that moved us to another state.
|
2021-02-27 12:13:40 -08:00
|
|
|
// (use 'ends with' to handle formation members copying our state).
|
2020-06-05 23:19:15 -07:00
|
|
|
if (!this.GetCurrentState().endsWith("COMBAT.ATTACKING"))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
2019-07-01 23:49:27 -07:00
|
|
|
// Check we can still reach the target for the next attack
|
2020-06-05 23:19:15 -07:00
|
|
|
if (this.CheckTargetAttackRange(target, attackType))
|
2019-07-01 23:49:27 -07:00
|
|
|
{
|
|
|
|
|
if (this.resyncAnimation)
|
2013-12-30 08:07:19 -08:00
|
|
|
{
|
2019-07-01 23:49:27 -07:00
|
|
|
this.SetAnimationSync(this.attackTimers.repeat, this.attackTimers.repeat);
|
|
|
|
|
this.resyncAnimation = false;
|
2010-07-21 09:09:58 -07:00
|
|
|
}
|
2019-07-01 23:49:27 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2010-10-06 14:37:55 -07:00
|
|
|
|
2019-07-01 23:49:27 -07:00
|
|
|
if (this.ShouldChaseTargetedEntity(target, this.order.data.force))
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
}
|
2019-07-01 23:49:27 -07:00
|
|
|
this.SetNextState("COMBAT.CHASING");
|
|
|
|
|
return;
|
2010-07-21 09:09:58 -07:00
|
|
|
}
|
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
|
|
|
// TODO: respond to target deaths immediately, rather than waiting
|
|
|
|
|
// until the next Timer event
|
|
|
|
|
|
|
|
|
|
"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) {
|
|
|
|
|
if (this.CheckPointRangeExplicit(msg.data.x, msg.data.z, 0, msg.data.max))
|
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
|
|
|
|
2014-01-05 10:29:38 -08:00
|
|
|
var cmpUnitAI = Engine.QueryInterface(this.order.data.target, IID_UnitAI);
|
|
|
|
|
if (cmpUnitAI && cmpUnitAI.IsFleeing())
|
2019-05-13 09:47:51 -07:00
|
|
|
this.SetSpeedMultiplier(this.GetRunMultiplier());
|
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-13 12:40:58 -07:00
|
|
|
this.ResetSpeedMultiplier();
|
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": {
|
2020-02-01 05:46:23 -08:00
|
|
|
"leave": function() {
|
|
|
|
|
// 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".
|
|
|
|
|
|
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);
|
2020-12-10 00:13:46 -08:00
|
|
|
let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer);
|
|
|
|
|
if (cmpResourceGatherer)
|
|
|
|
|
cmpResourceGatherer.AddToPlayerCounter(this.order.data.type.generic);
|
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();
|
|
|
|
|
|
|
|
|
|
if (!this.gatheringTarget)
|
|
|
|
|
return;
|
2014-01-14 16:04:25 -08:00
|
|
|
// don't use ownership because this is called after a conversion/resignation
|
|
|
|
|
// and the ownership would be invalid then.
|
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
|
|
|
|
|
|
|
|
let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer);
|
|
|
|
|
if (cmpResourceGatherer)
|
|
|
|
|
cmpResourceGatherer.RemoveFromPlayerCounter();
|
|
|
|
|
|
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();
|
|
|
|
|
},
|
|
|
|
|
|
2019-06-09 04:16:40 -07:00
|
|
|
"MovementUpdate": function(msg) {
|
|
|
|
|
// If we failed, the GATHERING timer will handle finding a valid resource.
|
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-07-03 10:57:16 -07:00
|
|
|
this.SetNextState("GATHERING");
|
2011-12-16 08:08:26 -08:00
|
|
|
},
|
|
|
|
|
},
|
2010-07-21 09:09:58 -07:00
|
|
|
|
|
|
|
|
"GATHERING": {
|
|
|
|
|
"enter": function() {
|
2019-06-05 13:44:43 -07:00
|
|
|
this.gatheringTarget = this.order.data.target || INVALID_ENTITY; // deleted in "leave".
|
2013-07-23 11:48:25 -07:00
|
|
|
|
2013-07-23 03:37:43 -07:00
|
|
|
// Check if the resource is full.
|
2019-06-05 13:44:43 -07:00
|
|
|
// Will only be added if we're not already in.
|
2020-09-11 03:19:09 -07:00
|
|
|
let cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply);
|
Decay/Regenerate option for resources.
Allows entities to change their resource amount over time, possibly with
some constraint.
This is a not-so-bare minimum, but can certainly be improved and/or
extended later.
Part of: #1973
Original patch by: @smiley
Redone by: @Stan
Standing on the shoulders of giants: @Freagarach
(Revisions: 59; Inlines: 209)
Differential revision: D1718
Comments by: @Angen, @elexis, @Imarok, @Langbart, @nani, @Nescio,
@smiley, @Stan, @wraitii
This was SVN commit r24963.
2021-02-28 12:14:53 -08:00
|
|
|
if (!cmpSupply || !cmpSupply.AddActiveGatherer(this.entity))
|
2013-07-23 11:48:25 -07:00
|
|
|
{
|
2019-09-01 00:35:32 -07:00
|
|
|
this.SetNextState("FINDINGNEWTARGET");
|
|
|
|
|
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
|
|
|
|
2012-04-28 12:25:44 -07:00
|
|
|
// Calculate timing based on gather rates
|
|
|
|
|
// This allows the gather rate to control how often we gather, instead of how much.
|
2020-02-13 12:24:51 -08:00
|
|
|
let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer);
|
|
|
|
|
let rate = cmpResourceGatherer.GetTargetGatherRate(this.gatheringTarget);
|
2012-04-28 12:25:44 -07:00
|
|
|
|
|
|
|
|
if (!rate)
|
|
|
|
|
{
|
|
|
|
|
// Try to find another target if the current one stopped existing
|
2013-03-13 13:10:46 -07:00
|
|
|
if (!Engine.QueryInterface(this.gatheringTarget, IID_Identity))
|
2012-04-28 12:25:44 -07:00
|
|
|
{
|
2019-09-01 00:35:32 -07:00
|
|
|
this.SetNextState("FINDINGNEWTARGET");
|
|
|
|
|
return true;
|
2012-04-28 12:25:44 -07:00
|
|
|
}
|
2013-07-23 11:48:25 -07:00
|
|
|
|
2012-04-28 12:25:44 -07:00
|
|
|
// No rate, give up on gathering
|
|
|
|
|
this.FinishOrder();
|
2012-09-14 13:00:03 -07:00
|
|
|
return true;
|
2012-04-28 12:25:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Scale timing interval based on rate, and start timer
|
|
|
|
|
// The offset should be at least as long as the repeat time so we use the same value for both.
|
2020-02-13 12:24:51 -08:00
|
|
|
let offset = 1000 / rate;
|
|
|
|
|
this.StartTimer(offset, offset);
|
2010-12-08 08:12:04 -08:00
|
|
|
|
|
|
|
|
// We want to start the gather animation as soon as possible,
|
|
|
|
|
// but only if we're actually at the target and it's still alive
|
|
|
|
|
// (else it'll look like we're chopping empty air).
|
|
|
|
|
// (If it's not alive, the Timer handler will deal with sending us
|
|
|
|
|
// off to a different target.)
|
2013-03-13 13:10:46 -07:00
|
|
|
if (this.CheckTargetRange(this.gatheringTarget, IID_ResourceGatherer))
|
2010-12-08 08:12:04 -08:00
|
|
|
{
|
2017-12-10 02:06:08 -08:00
|
|
|
this.SetDefaultAnimationVariant();
|
2019-07-18 12:56:09 -07:00
|
|
|
this.FaceTowardsTarget(this.order.data.target);
|
|
|
|
|
this.SelectAnimation("gather_" + this.order.data.type.specific);
|
2020-12-10 00:13:46 -08:00
|
|
|
cmpResourceGatherer.AddToPlayerCounter(this.order.data.type.generic);
|
2010-12-08 08:12:04 -08:00
|
|
|
}
|
2012-09-14 13:55:49 -07:00
|
|
|
return false;
|
2010-07-21 09:09:58 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"leave": function() {
|
|
|
|
|
this.StopTimer();
|
2013-07-23 11:48:25 -07:00
|
|
|
|
2020-02-13 12:24:51 -08:00
|
|
|
// Don't use ownership because this is called after a conversion/resignation
|
2014-01-14 16:04:25 -08:00
|
|
|
// and the ownership would be invalid then.
|
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
|
|
|
|
|
|
|
|
let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer);
|
|
|
|
|
if (cmpResourceGatherer)
|
|
|
|
|
cmpResourceGatherer.RemoveFromPlayerCounter();
|
|
|
|
|
|
2013-03-13 13:10:46 -07:00
|
|
|
delete this.gatheringTarget;
|
2013-07-23 11:48:25 -07:00
|
|
|
|
2019-07-09 12:56:28 -07:00
|
|
|
this.ResetAnimation();
|
2010-07-21 09:09:58 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"Timer": function(msg) {
|
2019-07-03 10:57:16 -07:00
|
|
|
let resourceTemplate = this.order.data.template;
|
|
|
|
|
let resourceType = this.order.data.type;
|
2013-07-23 11:48:25 -07:00
|
|
|
|
2019-09-01 00:35:32 -07:00
|
|
|
// TODO: we are leaking information here - if the target died in FOW, we'll know it's dead
|
|
|
|
|
// straight away.
|
|
|
|
|
// Seems one would have to listen to ownership changed messages to make it work correctly
|
|
|
|
|
// but that's likely prohibitively expansive performance wise.
|
|
|
|
|
|
2019-07-03 10:57:16 -07:00
|
|
|
let cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply);
|
2019-09-01 00:35:32 -07:00
|
|
|
// If we can't gather from the target, find a new one.
|
2020-09-11 03:19:09 -07:00
|
|
|
if (!cmpSupply || !cmpSupply.IsAvailableTo(this.entity) ||
|
2019-09-01 00:35:32 -07:00
|
|
|
!this.CanGather(this.gatheringTarget))
|
|
|
|
|
{
|
|
|
|
|
this.SetNextState("FINDINGNEWTARGET");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2010-11-13 11:15:29 -08:00
|
|
|
|
2019-09-01 00:35:32 -07:00
|
|
|
if (!this.CheckTargetRange(this.gatheringTarget, IID_ResourceGatherer))
|
|
|
|
|
{
|
|
|
|
|
// Try to follow the target
|
|
|
|
|
if (this.MoveToTargetRange(this.gatheringTarget, 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.gatheringTarget) && 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
|
|
|
|
|
this.SetNextState("FINDINGNEWTARGET");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2010-11-13 11:15:29 -08:00
|
|
|
|
|
|
|
|
|
2019-09-01 00:35:32 -07:00
|
|
|
let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer);
|
2010-11-13 11:15:29 -08:00
|
|
|
|
2019-09-01 00:35:32 -07:00
|
|
|
// If we've already got some resources but they're the wrong type,
|
|
|
|
|
// drop them first to ensure we're only ever carrying one type
|
|
|
|
|
if (cmpResourceGatherer.IsCarryingAnythingExcept(resourceType.generic))
|
|
|
|
|
cmpResourceGatherer.DropResources();
|
2013-07-23 11:48:25 -07:00
|
|
|
|
2019-09-01 00:35:32 -07:00
|
|
|
this.FaceTowardsTarget(this.order.data.target);
|
2013-07-23 11:48:25 -07:00
|
|
|
|
2019-09-01 00:35:32 -07:00
|
|
|
let status = cmpResourceGatherer.PerformGather(this.gatheringTarget);
|
2010-11-13 11:15:29 -08:00
|
|
|
|
2019-09-01 00:35:32 -07:00
|
|
|
if (status.filled)
|
|
|
|
|
{
|
2020-02-13 12:24:51 -08:00
|
|
|
let nearestDropsite = this.FindNearestDropsite(resourceType.generic);
|
|
|
|
|
if (nearestDropsite)
|
2010-07-21 09:09:58 -07:00
|
|
|
{
|
2019-09-01 00:35:32 -07:00
|
|
|
// (Keep this Gather order on the stack so we'll
|
|
|
|
|
// continue gathering after returning)
|
|
|
|
|
// However mark our target as invalid if it's exhausted, so we don't waste time
|
|
|
|
|
// trying to gather from it.
|
|
|
|
|
if (status.exhausted)
|
|
|
|
|
this.order.data.target = INVALID_ENTITY;
|
2020-02-13 12:24:51 -08:00
|
|
|
this.PushOrderFront("ReturnResource", { "target": nearestDropsite, "force": false });
|
2019-09-01 00:35:32 -07:00
|
|
|
return;
|
2010-12-08 08:12:04 -08:00
|
|
|
}
|
2010-07-31 14:21:42 -07:00
|
|
|
|
2019-09-01 00:35:32 -07:00
|
|
|
// Oh no, couldn't find any drop sites. Give up on gathering.
|
|
|
|
|
this.FinishOrder();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (status.exhausted)
|
|
|
|
|
this.SetNextState("FINDINGNEWTARGET");
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"FINDINGNEWTARGET": {
|
|
|
|
|
"enter": function() {
|
|
|
|
|
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.
|
|
|
|
|
let pos = cmpPosition.GetPosition();
|
|
|
|
|
let nearbyResource = this.FindNearbyResource(Vector2D.from3D(pos), filter);
|
|
|
|
|
|
|
|
|
|
// 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 };
|
|
|
|
|
else if (!nearbyResource)
|
|
|
|
|
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.
|
|
|
|
|
// Only move if we are some distance away (TODO: pick the distance better?)
|
|
|
|
|
if (!this.CheckPointRangeExplicit(initPos.x, initPos.z, 0, 10))
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
2012-04-17 13:22:13 -07:00
|
|
|
"HEAL": {
|
|
|
|
|
"Attacked": function(msg) {
|
2012-05-08 16:00:14 -07:00
|
|
|
if (!this.GetStance().respondStandGround && !this.order.data.force)
|
2012-04-17 13:22:13 -07:00
|
|
|
this.Flee(msg.data.attacker, false);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"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() {
|
2019-07-22 11:07:24 -07:00
|
|
|
if (!this.CheckRange(this.order.data, IID_Heal))
|
|
|
|
|
{
|
|
|
|
|
this.SetNextState("APPROACHING");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!this.TargetIsAlive(this.order.data.target) ||
|
|
|
|
|
!this.CanHeal(this.order.data.target))
|
|
|
|
|
{
|
|
|
|
|
this.SetNextState("FINDINGNEWTARGET");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let cmpHeal = Engine.QueryInterface(this.entity, IID_Heal);
|
2012-04-17 13:22:13 -07:00
|
|
|
this.healTimers = cmpHeal.GetTimers();
|
2012-08-28 13:02:03 -07:00
|
|
|
|
|
|
|
|
// If the repeat time since the last heal hasn't elapsed,
|
|
|
|
|
// delay the action to avoid healing too fast.
|
|
|
|
|
var prepare = this.healTimers.prepare;
|
|
|
|
|
if (this.lastHealed)
|
|
|
|
|
{
|
|
|
|
|
var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
|
|
|
|
|
var repeatLeft = this.lastHealed + this.healTimers.repeat - cmpTimer.GetTime();
|
|
|
|
|
prepare = Math.max(prepare, repeatLeft);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-24 10:13:03 -08:00
|
|
|
this.SelectAnimation("heal");
|
2012-08-28 13:02:03 -07:00
|
|
|
this.SetAnimationSync(prepare, this.healTimers.repeat);
|
|
|
|
|
this.StartTimer(prepare, this.healTimers.repeat);
|
|
|
|
|
|
|
|
|
|
// If using a non-default prepare time, re-sync the animation when the timer runs.
|
2019-07-22 11:07:24 -07:00
|
|
|
this.resyncAnimation = prepare != this.healTimers.prepare;
|
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() {
|
2019-07-09 12:56:28 -07:00
|
|
|
this.ResetAnimation();
|
2012-04-17 13:22:13 -07:00
|
|
|
this.StopTimer();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"Timer": function(msg) {
|
2019-07-22 11:07:24 -07:00
|
|
|
let target = this.order.data.target;
|
|
|
|
|
if (!this.TargetIsAlive(target) || !this.CanHeal(target))
|
|
|
|
|
{
|
|
|
|
|
this.SetNextState("FINDINGNEWTARGET");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!this.CheckRange(this.order.data, IID_Heal))
|
2012-04-17 13:22:13 -07:00
|
|
|
{
|
|
|
|
|
if (this.ShouldChaseTargetedEntity(target, this.order.data.force))
|
|
|
|
|
{
|
2018-04-25 16:47:24 -07:00
|
|
|
if (this.CanPack())
|
|
|
|
|
{
|
|
|
|
|
this.PushOrderFront("Pack", { "force": true });
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-05-28 04:38:18 -07:00
|
|
|
this.SetNextState("HEAL.APPROACHING");
|
2012-04-17 13:22:13 -07:00
|
|
|
}
|
2019-07-22 11:07:24 -07:00
|
|
|
else
|
|
|
|
|
this.SetNextState("FINDINGNEWTARGET");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
|
|
|
|
|
this.lastHealed = cmpTimer.GetTime() - msg.lateness;
|
|
|
|
|
|
|
|
|
|
this.FaceTowardsTarget(target);
|
|
|
|
|
let cmpHeal = Engine.QueryInterface(this.entity, IID_Heal);
|
|
|
|
|
cmpHeal.PerformHeal(target);
|
|
|
|
|
|
|
|
|
|
if (this.resyncAnimation)
|
|
|
|
|
{
|
|
|
|
|
this.SetAnimationSync(this.healTimers.repeat, this.healTimers.repeat);
|
|
|
|
|
this.resyncAnimation = false;
|
2012-04-17 13:22:13 -07:00
|
|
|
}
|
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() {
|
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;
|
|
|
|
|
}
|
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) {
|
2011-11-22 20:59:02 -08:00
|
|
|
// Check the dropsite is in range and we can return our resource there
|
2010-12-08 08:12:04 -08:00
|
|
|
// (we didn't get stopped before reaching it)
|
2020-06-04 04:01:06 -07:00
|
|
|
let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer);
|
|
|
|
|
if (this.CheckTargetRange(this.order.data.target, IID_ResourceGatherer) &&
|
|
|
|
|
this.CanReturnResource(this.order.data.target, true, cmpResourceGatherer))
|
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
|
|
|
// Our next order should always be a Gather,
|
|
|
|
|
// so just switch back to that order.
|
|
|
|
|
this.FinishOrder();
|
|
|
|
|
return;
|
2010-11-13 11:15:29 -08:00
|
|
|
}
|
2010-12-08 08:12:04 -08:00
|
|
|
|
2019-07-22 11:07:24 -07:00
|
|
|
if (msg.obstructed)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// If we are here: we are in range but not carrying the right resources (or resources at all),
|
|
|
|
|
// the dropsite was destroyed, or we couldn't reach it, or ownership changed.
|
2010-12-08 08:12:04 -08:00
|
|
|
// Look for a new one.
|
2019-07-22 11:07:24 -07:00
|
|
|
let genericType = cmpResourceGatherer.GetMainCarryingType();
|
|
|
|
|
let nearby = this.FindNearestDropsite(genericType);
|
2010-12-08 08:12:04 -08:00
|
|
|
if (nearby)
|
2010-11-13 11:15:29 -08:00
|
|
|
{
|
2010-12-08 08:12:04 -08:00
|
|
|
this.FinishOrder();
|
2012-05-08 16:00:14 -07:00
|
|
|
this.PushOrderFront("ReturnResource", { "target": nearby, "force": false });
|
2010-12-08 08:12:04 -08:00
|
|
|
return;
|
2010-11-13 11:15:29 -08:00
|
|
|
}
|
|
|
|
|
|
2010-12-08 08:12:04 -08:00
|
|
|
// Oh no, couldn't find any drop sites. Give up on returning.
|
2010-11-13 11:15:29 -08:00
|
|
|
this.FinishOrder();
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
2021-03-02 23:47:38 -08:00
|
|
|
"COLLECTTREASURE": {
|
|
|
|
|
"enter": function() {
|
|
|
|
|
let cmpTreasureCollecter = Engine.QueryInterface(this.entity, IID_TreasureCollecter);
|
|
|
|
|
if (!cmpTreasureCollecter || !cmpTreasureCollecter.CanCollect(this.order.data.target))
|
|
|
|
|
{
|
2021-03-03 10:20:49 -08:00
|
|
|
this.SetNextState("FINDINGNEWTARGET");
|
2021-03-02 23:47:38 -08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (this.CheckTargetRange(this.order.data.target, IID_TreasureCollecter))
|
|
|
|
|
this.SetNextState("COLLECTING");
|
|
|
|
|
else
|
|
|
|
|
this.SetNextState("APPROACHING");
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"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-03-02 23:47:38 -08:00
|
|
|
if (!this.MoveToTargetRange(this.order.data.target, IID_TreasureCollecter))
|
|
|
|
|
{
|
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) {
|
|
|
|
|
if (this.CheckTargetRange(this.order.data.target, IID_TreasureCollecter))
|
|
|
|
|
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() {
|
|
|
|
|
let cmpTreasureCollecter = Engine.QueryInterface(this.entity, IID_TreasureCollecter);
|
|
|
|
|
if (!cmpTreasureCollecter.StartCollecting(this.order.data.target, IID_UnitAI))
|
|
|
|
|
{
|
|
|
|
|
this.ProcessMessage("TargetInvalidated");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
this.FaceTowardsTarget(this.order.data.target);
|
|
|
|
|
this.SelectAnimation("collecting_treasure");
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"leave": function() {
|
|
|
|
|
let cmpTreasureCollecter = Engine.QueryInterface(this.entity, IID_TreasureCollecter);
|
|
|
|
|
if (cmpTreasureCollecter)
|
|
|
|
|
cmpTreasureCollecter.StopCollecting();
|
|
|
|
|
this.ResetAnimation();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"OutOfRange": function(msg) {
|
|
|
|
|
this.SetNextState("APPROACHING");
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"TargetInvalidated": function(msg) {
|
2021-03-03 10:20:49 -08:00
|
|
|
this.SetNextState("FINDINGNEWTARGET");
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"FINDINGNEWTARGET": {
|
|
|
|
|
"enter": function() {
|
|
|
|
|
let oldData = this.order.data;
|
|
|
|
|
|
|
|
|
|
// Switch to the next order (if any).
|
|
|
|
|
if (this.FinishOrder())
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
// If autocontinue explicitly disabled (e.g. by AI)
|
|
|
|
|
// then do nothing automatically.
|
|
|
|
|
if (!oldData.autocontinue)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
let nearbyTreasure = this.FindNearbyTreasure(this.TargetPosOrEntPos(oldData.target));
|
|
|
|
|
if (nearbyTreasure)
|
|
|
|
|
this.CollectTreasure(nearbyTreasure, oldData.autocontinue, true);
|
|
|
|
|
|
|
|
|
|
return true;
|
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
|
|
|
|
|
},
|
|
|
|
|
|
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))
|
2014-06-21 14:03:44 -07:00
|
|
|
this.StopTrading();
|
2014-03-26 02:11:06 -07:00
|
|
|
}
|
2014-01-28 10:56:39 -08:00
|
|
|
else
|
2016-03-09 11:54:12 -08:00
|
|
|
this.PerformTradeAndMoveToNextMarket(this.order.data.target);
|
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();
|
|
|
|
|
this.StopTrading();
|
|
|
|
|
if (otherMarket)
|
|
|
|
|
this.WalkToTarget(otherMarket);
|
|
|
|
|
},
|
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() {
|
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;
|
|
|
|
|
|
2020-01-27 09:14:30 -08:00
|
|
|
// Needed to remove the entity from the builder list when leaving this state.
|
|
|
|
|
this.repairTarget = this.order.data.target;
|
|
|
|
|
|
2014-01-08 05:39:33 -08:00
|
|
|
if (!this.CanRepair(this.repairTarget))
|
2012-08-20 00:30:50 -07:00
|
|
|
{
|
|
|
|
|
this.FinishOrder();
|
2012-09-14 13:00:03 -07:00
|
|
|
return true;
|
2012-08-20 00:30:50 -07:00
|
|
|
}
|
2013-05-21 16:45:20 -07:00
|
|
|
|
2014-01-08 05:39:33 -08:00
|
|
|
if (!this.CheckTargetRange(this.repairTarget, IID_Builder))
|
|
|
|
|
{
|
2019-06-11 13:06:25 -07:00
|
|
|
this.SetNextState("APPROACHING");
|
2014-01-08 05:39:33 -08:00
|
|
|
return true;
|
|
|
|
|
}
|
2020-11-04 09:50:20 -08:00
|
|
|
|
2020-01-27 09:14:30 -08:00
|
|
|
let cmpHealth = Engine.QueryInterface(this.repairTarget, 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.
|
2020-08-03 03:13:54 -07:00
|
|
|
this.ConstructionFinished({ "entity": this.repairTarget, "newentity": this.repairTarget });
|
2013-05-21 16:45:20 -07:00
|
|
|
return true;
|
2012-08-20 00:30:50 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-27 13:31:10 -07:00
|
|
|
let cmpBuilderList = QueryBuilderListInterface(this.repairTarget);
|
|
|
|
|
if (cmpBuilderList)
|
|
|
|
|
cmpBuilderList.AddBuilder(this.entity);
|
2013-05-21 16:45:20 -07:00
|
|
|
|
2020-01-27 09:14:30 -08:00
|
|
|
this.FaceTowardsTarget(this.repairTarget);
|
2019-07-18 12:56:09 -07:00
|
|
|
|
2018-02-24 10:13:03 -08:00
|
|
|
this.SelectAnimation("build");
|
2010-07-21 09:09:58 -07:00
|
|
|
this.StartTimer(1000, 1000);
|
2012-09-14 13:55:49 -07:00
|
|
|
return false;
|
2010-07-21 09:09:58 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"leave": function() {
|
2015-08-27 13:31:10 -07:00
|
|
|
let cmpBuilderList = QueryBuilderListInterface(this.repairTarget);
|
|
|
|
|
if (cmpBuilderList)
|
|
|
|
|
cmpBuilderList.RemoveBuilder(this.entity);
|
2013-07-07 15:13:58 -07:00
|
|
|
delete this.repairTarget;
|
2010-07-21 09:09:58 -07:00
|
|
|
this.StopTimer();
|
2019-07-09 12:56:28 -07:00
|
|
|
this.ResetAnimation();
|
2010-07-21 09:09:58 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
"Timer": function(msg) {
|
2014-01-08 05:39:33 -08:00
|
|
|
if (!this.CanRepair(this.repairTarget))
|
2010-07-21 09:09:58 -07:00
|
|
|
{
|
|
|
|
|
this.FinishOrder();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-02-05 18:11:39 -08:00
|
|
|
|
2020-01-27 09:14:30 -08:00
|
|
|
this.FaceTowardsTarget(this.repairTarget);
|
2019-07-18 12:56:09 -07:00
|
|
|
|
|
|
|
|
let cmpBuilder = Engine.QueryInterface(this.entity, IID_Builder);
|
2013-07-07 15:13:58 -07:00
|
|
|
cmpBuilder.PerformBuilding(this.repairTarget);
|
2020-01-27 09:14:30 -08:00
|
|
|
// If the building is completed, the leave() function will be called
|
|
|
|
|
// by the ConstructionFinished message.
|
|
|
|
|
// In that case, the repairTarget is deleted, and we can just return.
|
2014-01-08 07:39:32 -08:00
|
|
|
if (!this.repairTarget)
|
|
|
|
|
return;
|
2019-06-11 13:06:25 -07:00
|
|
|
if (!this.CheckTargetRange(this.repairTarget, IID_Builder))
|
2014-01-08 05:39:33 -08:00
|
|
|
this.SetNextState("APPROACHING");
|
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
|
|
|
|
|
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-12 11:53:52 -08:00
|
|
|
if (!this.CanGarrison(this.order.data.target))
|
|
|
|
|
{
|
|
|
|
|
this.FinishOrder();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-09 09:59:27 -07:00
|
|
|
if (!this.MoveToGarrisonRange(this.order.data.target))
|
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 });
|
|
|
|
|
|
|
|
|
|
let cmpGarrisonHolder = Engine.QueryInterface(this.order.data.target, IID_GarrisonHolder);
|
|
|
|
|
if (cmpGarrisonHolder && cmpGarrisonHolder.CanPickup(this.entity))
|
|
|
|
|
{
|
2021-03-12 11:53:52 -08:00
|
|
|
this.pickup = this.order.data.target;
|
2019-12-20 12:05:19 -08:00
|
|
|
Engine.PostMessage(this.pickup, MT_PickupRequested, { "entity": this.entity });
|
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
|
|
if (this.CheckGarrisonRange(this.order.data.target))
|
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-12 11:53:52 -08:00
|
|
|
let cmpGarrisonable = Engine.QueryInterface(this.entity, IID_Garrisonable);
|
|
|
|
|
if (!cmpGarrisonable || !cmpGarrisonable.Garrison(target))
|
2014-04-05 20:04:30 -07:00
|
|
|
{
|
2018-01-29 17:47:12 -08:00
|
|
|
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-02-27 21:40:08 -08:00
|
|
|
let cmpGarrisonHolder = Engine.QueryInterface(this.entity, IID_GarrisonHolder);
|
2013-11-08 15:22:59 -08:00
|
|
|
if (!cmpGarrisonHolder || cmpGarrisonHolder.IsFull())
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
|
2012-08-28 13:02:03 -07:00
|
|
|
// For preventing increased action rate due to Stop orders or target death.
|
|
|
|
|
this.lastAttacked = undefined;
|
|
|
|
|
this.lastHealed = 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
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UnitAI.prototype.IsTurret = function()
|
|
|
|
|
{
|
2020-07-20 23:09:19 -07:00
|
|
|
let cmpPosition = Engine.QueryInterface(this.entity, IID_Position);
|
2014-05-31 04:35:07 -07:00
|
|
|
return cmpPosition && cmpPosition.GetTurretParent() != 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);
|
|
|
|
|
};
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2017-07-04 10:57:20 -07:00
|
|
|
UnitAI.prototype.GetGarrisonHolder = function()
|
|
|
|
|
{
|
2021-01-01 23:34:52 -08:00
|
|
|
if (!this.isGarrisoned)
|
|
|
|
|
return INVALID_ENTITY;
|
|
|
|
|
|
|
|
|
|
let cmpGarrisonable = Engine.QueryInterface(this.entity, IID_Garrisonable);
|
|
|
|
|
return cmpGarrisonable ? cmpGarrisonable.HolderID() : INVALID_ENTITY;
|
2017-07-04 10:57:20 -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)
|
|
|
|
|
{
|
|
|
|
|
if (this.isImmobile || this.IsTurret())
|
|
|
|
|
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())
|
2018-04-04 08:05:59 -07:00
|
|
|
return false;
|
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
|
|
|
|
|
{
|
|
|
|
|
let index = this.GetCurrentState().indexOf(".");
|
|
|
|
|
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;
|
|
|
|
|
this.PushOrderAfterForced("PickupUnit", { "target": msg.entity });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-07-23 23:07:27 -07:00
|
|
|
let cmpPlayer = QueryOwnerInterface(this.entity);
|
|
|
|
|
// If we are being destructed (owner == -1), creating a range query is pointless.
|
2015-08-29 15:49:52 -07:00
|
|
|
if (!cmpPlayer)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-01-30 13:13:42 -08:00
|
|
|
let players = cmpPlayer.GetEnemies();
|
|
|
|
|
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
|
|
|
|
2020-07-23 23:07:27 -07:00
|
|
|
let cmpPlayer = QueryOwnerInterface(this.entity);
|
|
|
|
|
// If we are being destructed (owner == -1), creating a range query is pointless.
|
2015-08-29 15:49:52 -07:00
|
|
|
if (!cmpPlayer)
|
|
|
|
|
return;
|
2012-04-17 13:22:13 -07:00
|
|
|
|
2020-07-23 23:07:27 -07:00
|
|
|
let players = cmpPlayer.GetAllies();
|
|
|
|
|
let 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let cmpPlayer = QueryOwnerInterface(this.entity);
|
|
|
|
|
// If we are being destructed (owner == -1), creating a range query is pointless.
|
|
|
|
|
if (!cmpPlayer)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// TODO: How to handle neutral players - Special query to attack military only?
|
|
|
|
|
let players = cmpPlayer.GetEnemies();
|
|
|
|
|
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
|
|
|
|
2018-03-20 11:20:10 -07:00
|
|
|
let cmpPosition = Engine.QueryInterface(this.entity, IID_Position);
|
2021-03-07 23:19:06 -08:00
|
|
|
if (this.orderQueue.length && (this.isGarrisoned || this.IsFormationController() ||
|
2020-07-07 03:24:58 -07:00
|
|
|
cmpPosition && cmpPosition.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
|
2020-06-06 09:07:01 -07:00
|
|
|
let cmpFormation = Engine.QueryInterface(this.formationController, IID_Formation);
|
|
|
|
|
cmpFormation.SetWaitingOnController(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
|
|
|
{
|
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
|
|
|
|
|
});
|
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
|
|
|
{
|
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-03-07 23:19:06 -08:00
|
|
|
if (this.isGarrisoned)
|
2017-07-04 10:57:20 -07:00
|
|
|
{
|
2021-03-07 23:19:06 -08:00
|
|
|
let cmpGarrisonable = Engine.QueryInterface(this.entity, IID_Garrisonable);
|
|
|
|
|
if (!cmpGarrisonable || !cmpGarrisonable.UnGarrison(false))
|
2017-07-04 10:57:20 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2019-07-03 10:57:16 -07:00
|
|
|
let cmpResourceSupply = Engine.QueryInterface(ent, IID_ResourceSupply);
|
|
|
|
|
let type = cmpResourceSupply.GetType();
|
|
|
|
|
let amount = cmpResourceSupply.GetCurrentAmount();
|
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);
|
|
|
|
|
|
2020-09-11 03:19:09 -07:00
|
|
|
return amount > 0 && 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();
|
|
|
|
|
let cmpPlayer = QueryOwnerInterface(this.entity);
|
|
|
|
|
let players = cmpPlayer && cmpPlayer.HasSharedDropsites() ? cmpPlayer.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.)
|
|
|
|
|
return nearby.find(ent => !Engine.QueryInterface(ent, IID_Foundation).IsFinished());
|
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;
|
|
|
|
|
|
|
|
|
|
let cmpTreasureCollecter = Engine.QueryInterface(this.entity, IID_TreasureCollecter);
|
|
|
|
|
if (!cmpTreasureCollecter)
|
|
|
|
|
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);
|
|
|
|
|
return nearby.find(ent => cmpTreasureCollecter.CanCollect(ent));
|
|
|
|
|
};
|
|
|
|
|
|
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()
|
|
|
|
|
{
|
2020-03-20 13:12:09 -07:00
|
|
|
let cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion);
|
|
|
|
|
if (cmpUnitMotion)
|
|
|
|
|
cmpUnitMotion.StopMoving();
|
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;
|
|
|
|
|
|
2020-01-30 13:05:59 -08:00
|
|
|
let range = this.GetRange(iid, type);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 04:40:29 -07:00
|
|
|
let cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion);
|
|
|
|
|
if (!this.AbleToMove(cmpUnitMotion))
|
|
|
|
|
return false;
|
|
|
|
|
|
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);
|
|
|
|
|
|
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
|
|
|
|
2020-01-30 13:05:59 -08:00
|
|
|
let range = this.GetRange(IID_Attack, type);
|
|
|
|
|
if (!range)
|
|
|
|
|
return false;
|
2013-08-03 12:20:20 -07:00
|
|
|
|
2019-07-03 11:05:11 -07:00
|
|
|
let thisCmpPosition = Engine.QueryInterface(this.entity, IID_Position);
|
2014-05-11 02:36:12 -07:00
|
|
|
if (!thisCmpPosition.IsInWorld())
|
|
|
|
|
return false;
|
2019-07-03 11:05:11 -07:00
|
|
|
let s = thisCmpPosition.GetPosition();
|
2013-08-03 12:20:20 -07:00
|
|
|
|
2019-07-03 11:05:11 -07:00
|
|
|
let targetCmpPosition = Engine.QueryInterface(target, IID_Position);
|
2020-11-28 06:06:17 -08:00
|
|
|
if (!targetCmpPosition || !targetCmpPosition.IsInWorld())
|
2015-02-05 18:11:39 -08:00
|
|
|
return false;
|
2013-08-03 12:20:20 -07:00
|
|
|
|
2020-12-17 11:55:22 -08:00
|
|
|
// Parabolic range compuation is the same as in BuildingAI's FireArrows.
|
2019-07-03 11:05:11 -07:00
|
|
|
let t = targetCmpPosition.GetPosition();
|
2013-08-03 12:20:20 -07:00
|
|
|
// h is positive when I'm higher than the target
|
2019-07-03 11:05:11 -07:00
|
|
|
let h = s.y - t.y + range.elevationBonus;
|
2013-08-03 12:20:20 -07:00
|
|
|
|
2019-07-03 11:05:11 -07:00
|
|
|
let parabolicMaxRange = Math.sqrt(Math.square(range.max) + 2 * range.max * h);
|
2013-08-03 12:20:20 -07:00
|
|
|
// No negative roots please
|
2019-07-03 11:05:11 -07:00
|
|
|
if (h <= -range.max / 2)
|
2013-08-03 12:20:20 -07:00
|
|
|
// return false? Or hope you come close enough?
|
2019-07-03 11:05:11 -07:00
|
|
|
parabolicMaxRange = 0;
|
2013-08-03 12:20:20 -07:00
|
|
|
|
2019-07-03 11:05:11 -07:00
|
|
|
// The parabole changes while walking so be cautious:
|
|
|
|
|
let 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
|
|
|
};
|
|
|
|
|
|
2013-11-08 15:22:59 -08:00
|
|
|
UnitAI.prototype.MoveToGarrisonRange = function(target)
|
|
|
|
|
{
|
|
|
|
|
if (!this.CheckTargetVisible(target))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var cmpGarrisonHolder = Engine.QueryInterface(target, IID_GarrisonHolder);
|
|
|
|
|
if (!cmpGarrisonHolder)
|
|
|
|
|
return false;
|
|
|
|
|
var range = cmpGarrisonHolder.GetLoadingRange();
|
|
|
|
|
|
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, range.min, range.max);
|
2013-11-08 15:22:59 -08: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
|
|
|
{
|
2020-01-30 13:05:59 -08:00
|
|
|
let range = this.GetRange(iid, type);
|
|
|
|
|
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
|
|
|
|
2015-02-05 18:11:39 -08:00
|
|
|
if (type != "Ranged")
|
2014-01-11 04:19:43 -08:00
|
|
|
return this.CheckTargetRange(target, IID_Attack, type);
|
2015-02-05 18:11:39 -08:00
|
|
|
|
2019-07-03 11:05:11 -07:00
|
|
|
let targetCmpPosition = Engine.QueryInterface(target, IID_Position);
|
2015-02-05 18:11:39 -08:00
|
|
|
if (!targetCmpPosition || !targetCmpPosition.IsInWorld())
|
|
|
|
|
return false;
|
2013-08-03 12:20:20 -07:00
|
|
|
|
2020-01-30 13:05:59 -08:00
|
|
|
let range = this.GetRange(IID_Attack, type);
|
|
|
|
|
if (!range)
|
|
|
|
|
return false;
|
2013-08-03 12:20:20 -07:00
|
|
|
|
2019-07-03 11:05:11 -07:00
|
|
|
let thisCmpPosition = Engine.QueryInterface(this.entity, IID_Position);
|
2014-05-11 02:36:12 -07:00
|
|
|
if (!thisCmpPosition.IsInWorld())
|
|
|
|
|
return false;
|
|
|
|
|
|
2019-07-03 11:05:11 -07:00
|
|
|
let s = thisCmpPosition.GetPosition();
|
2013-08-03 12:20:20 -07:00
|
|
|
|
2019-07-03 11:05:11 -07:00
|
|
|
let t = targetCmpPosition.GetPosition();
|
2013-08-03 12:20:20 -07:00
|
|
|
|
2019-07-03 11:05:11 -07:00
|
|
|
let h = s.y - t.y + range.elevationBonus;
|
|
|
|
|
let maxRange = Math.sqrt(Math.square(range.max) + 2 * range.max * h);
|
2013-08-03 12:20:20 -07:00
|
|
|
|
2019-07-03 11:05:11 -07:00
|
|
|
if (maxRange < 0)
|
2013-08-03 12:20:20 -07:00
|
|
|
return false;
|
|
|
|
|
|
2019-06-06 12:37:23 -07:00
|
|
|
let cmpObstructionManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_ObstructionManager);
|
2019-07-03 11:05:11 -07:00
|
|
|
return cmpObstructionManager.IsInTargetRange(this.entity, target, range.min, maxRange, false);
|
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);
|
|
|
|
|
};
|
|
|
|
|
|
2012-03-09 15:33:55 -08:00
|
|
|
UnitAI.prototype.CheckGarrisonRange = function(target)
|
|
|
|
|
{
|
2020-08-03 05:02:24 -07:00
|
|
|
let cmpGarrisonHolder = Engine.QueryInterface(target, IID_GarrisonHolder);
|
2013-11-08 15:22:59 -08:00
|
|
|
if (!cmpGarrisonHolder)
|
|
|
|
|
return false;
|
2012-03-09 15:33:55 -08:00
|
|
|
|
2020-08-03 05:02:24 -07:00
|
|
|
let range = cmpGarrisonHolder.GetLoadingRange();
|
|
|
|
|
return this.CheckTargetRangeExplicit(target, range.min, range.max);
|
2012-04-28 12:25:44 -07:00
|
|
|
};
|
2012-03-09 15:33:55 -08:00
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
|
|
|
|
|
if (!cmpOwnership)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
|
|
|
|
|
if (!cmpRangeManager)
|
|
|
|
|
return false;
|
|
|
|
|
|
2014-08-04 15:49:19 -07:00
|
|
|
// Entities that are hidden and miraged are considered visible
|
|
|
|
|
var cmpFogging = Engine.QueryInterface(target, IID_Fogging);
|
|
|
|
|
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
|
|
|
{
|
2020-01-30 13:05:59 -08:00
|
|
|
let range = this.GetRange(iid, type);
|
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
|
|
|
|
2015-04-21 06:21:19 -07:00
|
|
|
UnitAI.prototype.GetBestAttackAgainst = function(target, allowCapture)
|
2012-05-01 15:20:08 -07:00
|
|
|
{
|
|
|
|
|
var cmpAttack = Engine.QueryInterface(this.entity, IID_Attack);
|
|
|
|
|
if (!cmpAttack)
|
|
|
|
|
return undefined;
|
2015-04-21 06:21:19 -07:00
|
|
|
return cmpAttack.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;
|
|
|
|
|
|
2017-12-23 07:53:00 -08:00
|
|
|
this.PushOrderFront("Attack", { "target": target, "force": false, "allowCapture": true });
|
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;
|
|
|
|
|
|
2017-12-23 07:53:00 -08:00
|
|
|
this.PushOrderFront("Attack", { "target": target, "force": false, "allowCapture": true });
|
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;
|
|
|
|
|
|
|
|
|
|
// Set obstruction group, so we can walk through members
|
|
|
|
|
// of our own formation (or ourself if not in formation)
|
|
|
|
|
var cmpObstruction = Engine.QueryInterface(this.entity, IID_Obstruction);
|
|
|
|
|
if (cmpObstruction)
|
|
|
|
|
{
|
|
|
|
|
if (ent == INVALID_ENTITY)
|
|
|
|
|
cmpObstruction.SetControlGroup(this.entity);
|
|
|
|
|
else
|
|
|
|
|
cmpObstruction.SetControlGroup(ent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we were removed from a formation, let the FSM switch back to INDIVIDUAL
|
|
|
|
|
if (ent == INVALID_ENTITY)
|
2014-05-27 00:24:07 -07:00
|
|
|
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":
|
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
|
|
|
|
2010-10-02 12:40:30 -07:00
|
|
|
default:
|
2013-12-04 05:14:31 -08:00
|
|
|
error("GetTargetPositions: Unrecognised order type '"+order.type+"'");
|
|
|
|
|
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
|
2017-07-10 09:10:50 -07:00
|
|
|
{
|
2017-07-10 09:19:13 -07:00
|
|
|
// May happen if an order arrives on the same turn the unit is garrisoned
|
2021-03-17 07:53:09 -07:00
|
|
|
// in that case, just forget the order as this will lead to an infinite loop.
|
|
|
|
|
// ToDo: Fix that by checking for the ability to move on orders that need that.
|
2021-03-07 23:19:06 -08:00
|
|
|
if (this.isGarrisoned && !this.IsTurret() && type != "Ungarrison")
|
2017-07-10 09:10:50 -07:00
|
|
|
return;
|
2010-07-21 09:09:58 -07:00
|
|
|
this.ReplaceOrder(type, data);
|
2017-07-10 09:10:50 -07:00
|
|
|
}
|
2010-07-21 09:09:58 -07:00
|
|
|
};
|
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
|
|
|
};
|
|
|
|
|
|
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
|
|
|
*/
|
2021-03-05 22:12:07 -08:00
|
|
|
UnitAI.prototype.WalkAndFight = function(x, z, targetClasses, allowCapture = true, 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
|
|
|
|
2021-03-05 22:12:07 -08:00
|
|
|
UnitAI.prototype.Patrol = function(x, z, targetClasses, allowCapture = true, 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.
|
|
|
|
|
*/
|
2021-03-05 22:12:07 -08:00
|
|
|
UnitAI.prototype.Attack = function(target, allowCapture = true, 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
|
|
|
{
|
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-05 22:12:07 -08:00
|
|
|
this.AddOrder("Garrison", { "target": target, "force": 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-03-07 23:19:06 -08:00
|
|
|
if (!this.isGarrisoned)
|
|
|
|
|
return;
|
|
|
|
|
this.AddOrder("Ungarrison", null, false);
|
2011-05-10 21:05:05 -07:00
|
|
|
};
|
|
|
|
|
|
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)
|
|
|
|
|
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-03-03 10:20:49 -08:00
|
|
|
UnitAI.prototype.CollectTreasure = function(target, autocontinue, queued)
|
|
|
|
|
{
|
|
|
|
|
this.AddOrder("CollectTreasure", {
|
|
|
|
|
"target": target,
|
|
|
|
|
"autocontinue": autocontinue,
|
|
|
|
|
"force": true
|
|
|
|
|
}, queued);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds order to collect a treasure to queue, forced by the player.
|
|
|
|
|
*/
|
|
|
|
|
UnitAI.prototype.CollectTreasureNearPosition = function(posX, posZ, autocontinue, queued)
|
2021-03-02 23:47:38 -08:00
|
|
|
{
|
2021-03-03 10:20:49 -08:00
|
|
|
this.AddOrder("CollectTreasureNearPosition", {
|
|
|
|
|
"x": posX,
|
|
|
|
|
"z": posZ,
|
|
|
|
|
"target": target,
|
|
|
|
|
"autocontinue": autocontinue,
|
|
|
|
|
"force": false
|
|
|
|
|
}, queued);
|
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-03-09 11:54:12 -08:00
|
|
|
UnitAI.prototype.PerformTradeAndMoveToNextMarket = function(currentMarket)
|
2012-03-08 12:42:28 -08:00
|
|
|
{
|
|
|
|
|
if (!this.CanTrade(currentMarket))
|
|
|
|
|
{
|
|
|
|
|
this.StopTrading();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-09 11:54:12 -08:00
|
|
|
if (!this.CheckTargetRange(currentMarket, IID_Trader))
|
2012-03-08 12:42:28 -08:00
|
|
|
{
|
2016-03-09 11:54:12 -08:00
|
|
|
if (!this.MoveToMarket(currentMarket)) // If the current market is not reached try again
|
2014-01-03 06:03:12 -08:00
|
|
|
this.StopTrading();
|
2016-03-09 11:54:12 -08:00
|
|
|
return;
|
|
|
|
|
}
|
2014-01-28 10:56:39 -08:00
|
|
|
|
2016-03-09 11:54:12 -08:00
|
|
|
let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
|
2017-02-24 13:59:23 -08:00
|
|
|
let nextMarket = cmpTrader.PerformTrade(currentMarket);
|
2016-03-20 07:00:49 -07:00
|
|
|
let amount = cmpTrader.GetGoods().amount;
|
2017-02-24 13:59:23 -08:00
|
|
|
if (!nextMarket || !amount || !amount.traderGain)
|
2016-03-09 11:54:12 -08:00
|
|
|
{
|
|
|
|
|
this.StopTrading();
|
|
|
|
|
return;
|
2012-03-08 12:42:28 -08:00
|
|
|
}
|
2016-03-09 11:54:12 -08:00
|
|
|
|
|
|
|
|
this.order.data.target = nextMarket;
|
|
|
|
|
|
|
|
|
|
if (this.order.data.route && this.order.data.route.length)
|
2012-03-08 12:42:28 -08:00
|
|
|
{
|
2016-03-09 11:54:12 -08:00
|
|
|
this.waypoints = this.order.data.route.slice();
|
|
|
|
|
if (this.order.data.target == cmpTrader.GetSecondMarket())
|
|
|
|
|
this.waypoints.reverse();
|
2012-03-08 12:42:28 -08:00
|
|
|
}
|
2016-03-09 11:54:12 -08:00
|
|
|
|
2021-01-18 03:00:02 -08:00
|
|
|
this.SetNextState("APPROACHINGMARKET");
|
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-03-08 12:42:28 -08:00
|
|
|
UnitAI.prototype.StopTrading = function()
|
|
|
|
|
{
|
|
|
|
|
this.FinishOrder();
|
|
|
|
|
var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader);
|
|
|
|
|
cmpTrader.StopTrading();
|
2012-04-28 12:25:44 -07:00
|
|
|
};
|
2012-03-08 12:42:28 -08:00
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
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())
|
|
|
|
|
{
|
2021-03-22 06:27:33 -07:00
|
|
|
let cmpUnitAI;
|
|
|
|
|
let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation);
|
|
|
|
|
for (let ent of cmpFormation.members)
|
2013-11-30 10:23:32 -08:00
|
|
|
{
|
2014-08-24 04:51:03 -07:00
|
|
|
if (!(cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI)))
|
2013-12-26 08:09:32 -08:00
|
|
|
continue;
|
2021-03-22 06:27:33 -07:00
|
|
|
if (cmpUnitAI.FindWalkAndFightTargets())
|
2014-08-24 04:51:03 -07:00
|
|
|
return true;
|
2013-11-30 10:23:32 -08:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
this.PushOrderFront("Attack", { "target": v, "force": false, "allowCapture": this?.order?.data?.allowCapture });
|
|
|
|
|
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;
|
|
|
|
|
this.PushOrderFront("Attack", { "target": targ, "force": false, "allowCapture": this?.order?.data?.allowCapture });
|
|
|
|
|
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
|
|
|
/*
|
|
|
|
|
* Make the unit walk at its normal pace.
|
|
|
|
|
*/
|
2019-05-13 09:47:51 -07:00
|
|
|
UnitAI.prototype.ResetSpeedMultiplier = function()
|
2019-04-19 03:04:50 -07:00
|
|
|
{
|
|
|
|
|
let cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion);
|
|
|
|
|
if (cmpUnitMotion)
|
2019-05-13 09:47:51 -07:00
|
|
|
cmpUnitMotion.SetSpeedMultiplier(1);
|
2019-04-19 03:04:50 -07:00
|
|
|
};
|
2012-05-08 16:00:14 -07:00
|
|
|
|
2019-05-13 09:47:51 -07:00
|
|
|
UnitAI.prototype.SetSpeedMultiplier = function(speed)
|
2012-05-08 16:00:14 -07:00
|
|
|
{
|
2019-04-19 03:04:50 -07:00
|
|
|
let cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion);
|
|
|
|
|
if (cmpUnitMotion)
|
2019-05-13 09:47:51 -07:00
|
|
|
cmpUnitMotion.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
|
|
|
|
|
* @param {string} type - [Optional]
|
|
|
|
|
* @return {Object | undefined} - The range in the form
|
|
|
|
|
* { "min": number, "max": number }
|
|
|
|
|
* Object."elevationBonus": number may be present when iid == IID_Attack.
|
|
|
|
|
* Returns undefined when the entity does not have the requested component.
|
|
|
|
|
*/
|
|
|
|
|
UnitAI.prototype.GetRange = function(iid, type)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
|
|
|
return component.GetRange(type);
|
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
|
|
|
|
|
// (then the individual units can make up their own minds)
|
|
|
|
|
if (this.IsFormationController())
|
|
|
|
|
return true;
|
|
|
|
|
|
2020-01-17 01:40:34 -08:00
|
|
|
let cmpGarrisonHolder = Engine.QueryInterface(target, IID_GarrisonHolder);
|
2010-10-23 15:43:15 -07:00
|
|
|
if (!cmpGarrisonHolder)
|
|
|
|
|
return false;
|
2011-02-27 05:34:22 -08:00
|
|
|
|
2020-01-17 01:40:34 -08:00
|
|
|
let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
|
2013-04-13 17:34:14 -07:00
|
|
|
if (!cmpOwnership || !(IsOwnedByPlayer(cmpOwnership.GetOwner(), target) || IsOwnedByMutualAllyOfPlayer(cmpOwnership.GetOwner(), target)))
|
2011-11-21 16:16:35 -08:00
|
|
|
return false;
|
|
|
|
|
|
2010-10-23 15:43:15 -07:00
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
2010-07-21 09:09:58 -07:00
|
|
|
UnitAI.prototype.CanGather = function(target)
|
|
|
|
|
{
|
2014-05-30 07:46:06 -07:00
|
|
|
if (this.IsTurret())
|
|
|
|
|
return false;
|
2020-11-04 09:50:20 -08:00
|
|
|
|
2015-05-01 10:03:37 -07:00
|
|
|
var cmpResourceSupply = QueryMiragedInterface(target, IID_ResourceSupply);
|
|
|
|
|
if (!cmpResourceSupply)
|
2012-08-26 18:35:53 -07:00
|
|
|
return false;
|
|
|
|
|
|
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;
|
|
|
|
|
|
2010-07-21 09:09:58 -07:00
|
|
|
var cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer);
|
|
|
|
|
if (!cmpResourceGatherer)
|
|
|
|
|
return false;
|
2010-02-12 14:46:53 -08:00
|
|
|
|
2010-07-21 09:09:58 -07:00
|
|
|
if (!cmpResourceGatherer.GetTargetGatherRate(target))
|
|
|
|
|
return false;
|
2010-02-12 14:46:53 -08:00
|
|
|
|
2012-04-10 14:09:21 -07:00
|
|
|
// No need to verify ownership as we should be able to gather from
|
|
|
|
|
// a target regardless of ownership.
|
2013-03-13 13:10:46 -07:00
|
|
|
// No need to call "cmpResourceSupply.IsAvailable()" either because that
|
|
|
|
|
// would cause units to walk to full entities instead of choosing another one
|
|
|
|
|
// nearby to gather from, which is undesirable.
|
2010-07-21 09:09:58 -07:00
|
|
|
return true;
|
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
|
|
|
{
|
2014-05-30 07:46:06 -07:00
|
|
|
if (this.IsTurret())
|
|
|
|
|
return false;
|
2010-11-13 11:15:29 -08:00
|
|
|
// Formation controllers should always respond to commands
|
|
|
|
|
// (then the individual units can make up their own minds)
|
|
|
|
|
if (this.IsFormationController())
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
if (!cmpResourceGatherer)
|
2020-06-04 04:01:06 -07:00
|
|
|
{
|
|
|
|
|
cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer);
|
|
|
|
|
if (!cmpResourceGatherer)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2010-11-13 11:15:29 -08:00
|
|
|
|
2020-06-04 04:01:06 -07:00
|
|
|
let cmpResourceDropsite = Engine.QueryInterface(target, IID_ResourceDropsite);
|
2010-11-13 11:15:29 -08:00
|
|
|
if (!cmpResourceDropsite)
|
|
|
|
|
return false;
|
|
|
|
|
|
2011-11-22 20:59:02 -08:00
|
|
|
if (checkCarriedResource)
|
|
|
|
|
{
|
2020-06-04 04:01:06 -07:00
|
|
|
let type = cmpResourceGatherer.GetMainCarryingType();
|
2011-11-22 20:59:02 -08:00
|
|
|
if (!type || !cmpResourceDropsite.AcceptsType(type))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2010-11-13 11:15:29 -08:00
|
|
|
|
2020-06-04 04:01:06 -07:00
|
|
|
let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
|
2016-03-13 06:33:21 -07:00
|
|
|
if (cmpOwnership && IsOwnedByPlayer(cmpOwnership.GetOwner(), target))
|
|
|
|
|
return true;
|
2020-06-04 04:01:06 -07:00
|
|
|
let cmpPlayer = QueryOwnerInterface(this.entity);
|
2016-03-13 06:33:21 -07:00
|
|
|
return cmpPlayer && cmpPlayer.HasSharedDropsites() && cmpResourceDropsite.IsShared() &&
|
|
|
|
|
cmpOwnership && IsOwnedByMutualAllyOfPlayer(cmpOwnership.GetOwner(), target);
|
2010-11-13 11:15:29 -08:00
|
|
|
};
|
|
|
|
|
|
2012-03-08 12:42:28 -08:00
|
|
|
UnitAI.prototype.CanTrade = function(target)
|
|
|
|
|
{
|
2014-05-30 07:46:06 -07:00
|
|
|
if (this.IsTurret())
|
|
|
|
|
return false;
|
2012-03-08 12:42:28 -08:00
|
|
|
// Formation controllers should always respond to commands
|
|
|
|
|
// (then the individual units can make up their own minds)
|
|
|
|
|
if (this.IsFormationController())
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
var 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)
|
|
|
|
|
{
|
2014-05-30 07:46:06 -07:00
|
|
|
if (this.IsTurret())
|
|
|
|
|
return false;
|
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;
|
|
|
|
|
|
2010-09-03 13:18:54 -07:00
|
|
|
// Verify that we're able to respond to Repair (Builder) commands
|
2010-09-03 02:55:14 -07:00
|
|
|
var cmpBuilder = Engine.QueryInterface(this.entity, IID_Builder);
|
2010-09-03 13:04:11 -07:00
|
|
|
if (!cmpBuilder)
|
2010-09-03 02:55:14 -07:00
|
|
|
return false;
|
|
|
|
|
|
2016-03-24 14:48:15 -07:00
|
|
|
var cmpFoundation = QueryMiragedInterface(target, IID_Foundation);
|
2016-01-21 12:49:57 -08:00
|
|
|
var cmpRepairable = Engine.QueryInterface(target, IID_Repairable);
|
|
|
|
|
if (!cmpFoundation && !cmpRepairable)
|
|
|
|
|
return false;
|
|
|
|
|
|
2011-11-21 16:16:35 -08:00
|
|
|
var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
|
2015-12-06 12:51:30 -08:00
|
|
|
return cmpOwnership && IsOwnedByAllyOfPlayer(cmpOwnership.GetOwner(), target);
|
2010-09-03 02:55:14 -07:00
|
|
|
};
|
|
|
|
|
|
2012-11-30 16:34:03 -08:00
|
|
|
UnitAI.prototype.CanPack = function()
|
|
|
|
|
{
|
|
|
|
|
var cmpPack = Engine.QueryInterface(this.entity, IID_Pack);
|
2018-03-27 10:56:32 -07:00
|
|
|
return cmpPack && !cmpPack.IsPacking() && !cmpPack.IsPacked();
|
2012-11-30 16:34:03 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UnitAI.prototype.CanUnpack = function()
|
|
|
|
|
{
|
|
|
|
|
var cmpPack = Engine.QueryInterface(this.entity, IID_Pack);
|
2018-03-27 10:56:32 -07:00
|
|
|
return cmpPack && !cmpPack.IsPacking() && cmpPack.IsPacked();
|
2012-11-30 16:34:03 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UnitAI.prototype.IsPacking = function()
|
|
|
|
|
{
|
|
|
|
|
var 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);
|
|
|
|
|
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.
|
|
|
|
|
* @param resetWaitingEntities - If true, call ResetWaitingEntities first.
|
|
|
|
|
* 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
|
|
|
*/
|
2020-06-06 09:07:01 -07:00
|
|
|
UnitAI.prototype.CallMemberFunction = function(funcname, args, resetWaitingEntities = true)
|
2014-01-11 04:19:43 -08:00
|
|
|
{
|
|
|
|
|
var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation);
|
|
|
|
|
if (!cmpFormation)
|
|
|
|
|
return;
|
2015-08-29 15:49:44 -07:00
|
|
|
|
2020-06-06 09:07:01 -07:00
|
|
|
if (resetWaitingEntities)
|
|
|
|
|
cmpFormation.ResetWaitingEntities();
|
|
|
|
|
|
2015-08-29 15:49:44 -07:00
|
|
|
cmpFormation.GetMembers().forEach(ent => {
|
2020-06-06 09:07:01 -07:00
|
|
|
let cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI);
|
2014-01-11 04:19:43 -08:00
|
|
|
cmpUnitAI[funcname].apply(cmpUnitAI, args);
|
2015-08-29 15:49:44 -07:00
|
|
|
});
|
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);
|