Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/UnitAI.js
Show First 20 Lines • Show All 203 Lines • ▼ Show 20 Lines | UnitAI.prototype.UnitFsmSpec = { | ||||
"FormationLeave": function(msg) { | "FormationLeave": function(msg) { | ||||
// ignore when we're not in FORMATIONMEMBER | // ignore when we're not in FORMATIONMEMBER | ||||
}, | }, | ||||
// Called when being told to walk as part of a formation | // Called when being told to walk as part of a formation | ||||
"Order.FormationWalk": function(msg) { | "Order.FormationWalk": function(msg) { | ||||
// Let players move captured domestic animals around | // Let players move captured domestic animals around | ||||
if (this.IsAnimal() && !this.IsDomestic() || !this.AbleToMove()) | if (this.IsAnimal() && !this.IsDomestic() || !this.AbleToMove() || this.IsUpgrading()) | ||||
{ | { | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return; | return; | ||||
} | } | ||||
// For packable units: | // For packable units: | ||||
// 1. If packed, we can move. | // 1. If packed, we can move. | ||||
// 2. If unpacked, we first need to pack, then follow case 1. | // 2. If unpacked, we first need to pack, then follow case 1. | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | else if (this.IsFormationMember()) | ||||
this.SetNextState("FORMATIONMEMBER.IDLE"); | this.SetNextState("FORMATIONMEMBER.IDLE"); | ||||
else | else | ||||
this.SetNextState("INDIVIDUAL.IDLE"); | this.SetNextState("INDIVIDUAL.IDLE"); | ||||
}, | }, | ||||
"Order.Walk": function(msg) { | "Order.Walk": function(msg) { | ||||
// Let players move captured domestic animals around | // Let players move captured domestic animals around | ||||
if (this.IsAnimal() && !this.IsDomestic() || !this.AbleToMove()) | if (this.IsAnimal() && !this.IsDomestic() || !this.AbleToMove() || this.IsUpgrading()) | ||||
{ | { | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return; | return; | ||||
} | } | ||||
// For packable units: | // For packable units: | ||||
// 1. If packed, we can move. | // 1. If packed, we can move. | ||||
// 2. If unpacked, we first need to pack, then follow case 1. | // 2. If unpacked, we first need to pack, then follow case 1. | ||||
Show All 9 Lines | "Order.Walk": function(msg) { | ||||
if (this.IsAnimal()) | if (this.IsAnimal()) | ||||
this.SetNextState("ANIMAL.WALKING"); | this.SetNextState("ANIMAL.WALKING"); | ||||
else | else | ||||
this.SetNextState("INDIVIDUAL.WALKING"); | this.SetNextState("INDIVIDUAL.WALKING"); | ||||
}, | }, | ||||
"Order.WalkAndFight": function(msg) { | "Order.WalkAndFight": function(msg) { | ||||
// Let players move captured domestic animals around | // Let players move captured domestic animals around | ||||
if (this.IsAnimal() && !this.IsDomestic() || !this.AbleToMove()) | if (this.IsAnimal() && !this.IsDomestic() || !this.AbleToMove() || this.IsUpgrading()) | ||||
{ | { | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return; | return; | ||||
} | } | ||||
// For packable units: | // For packable units: | ||||
// 1. If packed, we can move. | // 1. If packed, we can move. | ||||
// 2. If unpacked, we first need to pack, then follow case 1. | // 2. If unpacked, we first need to pack, then follow case 1. | ||||
Show All 10 Lines | if (this.IsAnimal()) | ||||
this.SetNextState("ANIMAL.WALKING"); // WalkAndFight not applicable for animals | this.SetNextState("ANIMAL.WALKING"); // WalkAndFight not applicable for animals | ||||
else | else | ||||
this.SetNextState("INDIVIDUAL.WALKINGANDFIGHTING"); | this.SetNextState("INDIVIDUAL.WALKINGANDFIGHTING"); | ||||
}, | }, | ||||
"Order.WalkToTarget": function(msg) { | "Order.WalkToTarget": function(msg) { | ||||
// Let players move captured domestic animals around | // Let players move captured domestic animals around | ||||
if (this.IsAnimal() && !this.IsDomestic() || !this.AbleToMove()) | if (this.IsAnimal() && !this.IsDomestic() || !this.AbleToMove() || this.IsUpgrading()) | ||||
{ | { | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return; | return; | ||||
} | } | ||||
// For packable units: | // For packable units: | ||||
// 1. If packed, we can move. | // 1. If packed, we can move. | ||||
// 2. If unpacked, we first need to pack, then follow case 1. | // 2. If unpacked, we first need to pack, then follow case 1. | ||||
▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | "Order.PickupUnit": function(msg) { | ||||
if (needToMove) | if (needToMove) | ||||
this.SetNextState("INDIVIDUAL.PICKUP.APPROACHING"); | this.SetNextState("INDIVIDUAL.PICKUP.APPROACHING"); | ||||
else | else | ||||
this.SetNextState("INDIVIDUAL.PICKUP.LOADING"); | this.SetNextState("INDIVIDUAL.PICKUP.LOADING"); | ||||
}, | }, | ||||
"Order.Guard": function(msg) { | "Order.Guard": function(msg) { | ||||
if (!this.AddGuard(this.order.data.target)) | if (this.IsUpgrading() || !this.AddGuard(this.order.data.target)) | ||||
{ | { | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return; | return; | ||||
} | } | ||||
if (!this.CheckTargetRangeExplicit(this.isGuardOf, 0, this.guardRange)) | if (!this.CheckTargetRangeExplicit(this.isGuardOf, 0, this.guardRange)) | ||||
this.SetNextState("INDIVIDUAL.GUARD.ESCORTING"); | this.SetNextState("INDIVIDUAL.GUARD.ESCORTING"); | ||||
else | else | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | if (this.CheckTargetAttackRange(this.order.data.target, this.order.data.attackType)) | ||||
this.SetNextState("ANIMAL.COMBAT.ATTACKING"); | this.SetNextState("ANIMAL.COMBAT.ATTACKING"); | ||||
else | else | ||||
this.SetNextState("INDIVIDUAL.COMBAT.ATTACKING"); | this.SetNextState("INDIVIDUAL.COMBAT.ATTACKING"); | ||||
return; | return; | ||||
} | } | ||||
// If we can't reach the target, but are standing ground, then abandon this attack order. | // If we can't reach the target, but are standing ground, then abandon this attack order. | ||||
// Unless we're hunting, that's a special case where we should continue attacking our target. | // Unless we're hunting, that's a special case where we should continue attacking our target. | ||||
if (this.GetStance().respondStandGround && !this.order.data.force && !this.order.data.hunting || !this.AbleToMove()) | if (this.GetStance().respondStandGround && !this.order.data.force && !this.order.data.hunting || !this.AbleToMove() || this.IsUpgrading()) | ||||
{ | { | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return; | return; | ||||
} | } | ||||
// For packable units out of attack range: | // For packable units out of attack range: | ||||
// 1. If packed, we need to move to attack range and then unpack. | // 1. If packed, we need to move to attack range and then unpack. | ||||
// 2. If unpacked, we first need to pack, then follow case 1. | // 2. If unpacked, we first need to pack, then follow case 1. | ||||
Show All 9 Lines | "Order.Attack": function(msg) { | ||||
if (this.IsAnimal()) | if (this.IsAnimal()) | ||||
this.SetNextState("ANIMAL.COMBAT.APPROACHING"); | this.SetNextState("ANIMAL.COMBAT.APPROACHING"); | ||||
else | else | ||||
this.SetNextState("INDIVIDUAL.COMBAT.APPROACHING"); | this.SetNextState("INDIVIDUAL.COMBAT.APPROACHING"); | ||||
}, | }, | ||||
"Order.Patrol": function(msg) { | "Order.Patrol": function(msg) { | ||||
if (this.IsAnimal() || !this.AbleToMove()) | if (this.IsAnimal() || !this.AbleToMove() || this.IsUpgrading()) | ||||
{ | { | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return; | return; | ||||
} | } | ||||
if (this.CanPack()) | if (this.CanPack()) | ||||
{ | { | ||||
this.PushOrderFront("Pack", { "force": true }); | this.PushOrderFront("Pack", { "force": true }); | ||||
▲ Show 20 Lines • Show All 136 Lines • ▼ Show 20 Lines | "Order.Repair": function(msg) { | ||||
// Try to move within range | // Try to move within range | ||||
if (this.CheckTargetRange(this.order.data.target, IID_Builder)) | if (this.CheckTargetRange(this.order.data.target, IID_Builder)) | ||||
this.SetNextState("INDIVIDUAL.REPAIR.REPAIRING"); | this.SetNextState("INDIVIDUAL.REPAIR.REPAIRING"); | ||||
else | else | ||||
this.SetNextState("INDIVIDUAL.REPAIR.APPROACHING"); | this.SetNextState("INDIVIDUAL.REPAIR.APPROACHING"); | ||||
}, | }, | ||||
"Order.Garrison": function(msg) { | "Order.Garrison": function(msg) { | ||||
if (!this.AbleToMove()) | if (!this.AbleToMove() || this.IsUpgrading()) | ||||
{ | { | ||||
this.SetNextState("IDLE"); | this.SetNextState("IDLE"); | ||||
return; | return; | ||||
} | } | ||||
else if (this.IsGarrisoned()) | else if (this.IsGarrisoned()) | ||||
{ | { | ||||
if (this.IsAnimal()) | if (this.IsAnimal()) | ||||
this.SetNextState("ANIMAL.GARRISON.GARRISONED"); | this.SetNextState("ANIMAL.GARRISON.GARRISONED"); | ||||
▲ Show 20 Lines • Show All 1,559 Lines • ▼ Show 20 Lines | "COMBAT": { | ||||
return false; | return false; | ||||
}, | }, | ||||
"leave": function() { | "leave": function() { | ||||
this.StopMoving(); | this.StopMoving(); | ||||
}, | }, | ||||
"MovementUpdate": function(msg) { | "MovementUpdate": function(msg) { | ||||
// If it looks like the path is failing, and we are close enough (3 tiles) from wanted range | // If it looks like the path is failing, and we are close enough (3 tiles) from wanted range | ||||
// stop anyways. This avoids pathing for an unreachable goal and reduces lag considerably. | // stop anyways. This avoids pathing for an unreachable goal and reduces lag considerably. | ||||
if (msg.likelyFailure || | if (msg.likelyFailure || | ||||
msg.obstructed && this.RelaxedMaxRangeCheck(this.order.data, this.order.data.max + this.DefaultRelaxedMaxRange) || | msg.obstructed && this.RelaxedMaxRangeCheck(this.order.data, this.order.data.max + this.DefaultRelaxedMaxRange) || | ||||
!msg.obstructed && this.CheckRange(this.order.data)) | !msg.obstructed && this.CheckRange(this.order.data)) | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
}, | }, | ||||
}, | }, | ||||
}, | }, | ||||
}, | }, | ||||
▲ Show 20 Lines • Show All 1,288 Lines • ▼ Show 20 Lines | if (this.isImmobile || this.IsTurret()) | ||||
return false; | return false; | ||||
if (!cmpUnitMotion) | if (!cmpUnitMotion) | ||||
cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | ||||
return !!cmpUnitMotion; | return !!cmpUnitMotion; | ||||
}; | }; | ||||
UnitAI.prototype.IsUpgrading = function() | |||||
bb: function has super weird name and is used only once. inline | |||||
{ | |||||
let cmpUpgrade = Engine.QueryInterface(this.entity, IID_Upgrade) | |||||
return cmpUpgrade && cmpUpgrade.IsUpgrading(); | |||||
}; | |||||
UnitAI.prototype.IsFleeing = function() | UnitAI.prototype.IsFleeing = function() | ||||
{ | { | ||||
var state = this.GetCurrentState().split(".").pop(); | var state = this.GetCurrentState().split(".").pop(); | ||||
return (state == "FLEEING"); | return (state == "FLEEING"); | ||||
}; | }; | ||||
UnitAI.prototype.IsWalking = function() | UnitAI.prototype.IsWalking = function() | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 421 Lines • ▼ Show 20 Lines | |||||
}; | }; | ||||
UnitAI.prototype.WillMoveFromFoundation = function(target, checkPacking = true) | UnitAI.prototype.WillMoveFromFoundation = function(target, checkPacking = true) | ||||
{ | { | ||||
// If foundation is not ally of entity, or if entity is unpacked siege, | // If foundation is not ally of entity, or if entity is unpacked siege, | ||||
// ignore the order. | // ignore the order. | ||||
if (!IsOwnedByAllyOfEntity(this.entity, target) && | if (!IsOwnedByAllyOfEntity(this.entity, target) && | ||||
!Engine.QueryInterface(SYSTEM_ENTITY, IID_CeasefireManager).IsCeasefireActive() || | !Engine.QueryInterface(SYSTEM_ENTITY, IID_CeasefireManager).IsCeasefireActive() || | ||||
checkPacking && this.IsPacking() || this.CanPack() || !this.AbleToMove()) | checkPacking && this.IsPacking() || this.CanPack() || !this.AbleToMove() || this.IsUpgrading()) | ||||
return false; | return false; | ||||
// Move a tile outside the building. | // Move a tile outside the building. | ||||
return !this.CheckTargetRangeExplicit(target, g_LeaveFoundationRange, -1); | return !this.CheckTargetRangeExplicit(target, g_LeaveFoundationRange, -1); | ||||
}; | }; | ||||
UnitAI.prototype.ReplaceOrder = function(type, data) | UnitAI.prototype.ReplaceOrder = function(type, data) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 1,624 Lines • ▼ Show 20 Lines | UnitAI.prototype.SetupTradeRoute = function(target, source, route, queued) | ||||
} | } | ||||
// AI has currently no access to BackToWork | // AI has currently no access to BackToWork | ||||
let cmpPlayer = QueryOwnerInterface(this.entity); | let cmpPlayer = QueryOwnerInterface(this.entity); | ||||
if (cmpPlayer && cmpPlayer.IsAI() && !this.IsFormationController() && | if (cmpPlayer && cmpPlayer.IsAI() && !this.IsFormationController() && | ||||
this.workOrders.length && this.workOrders[0].type == "Trade") | this.workOrders.length && this.workOrders[0].type == "Trade") | ||||
{ | { | ||||
let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); | let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); | ||||
if (cmpTrader.HasBothMarkets() && | if (cmpTrader.HasBothMarkets() && | ||||
(cmpTrader.GetFirstMarket() == target && cmpTrader.GetSecondMarket() == source || | (cmpTrader.GetFirstMarket() == target && cmpTrader.GetSecondMarket() == source || | ||||
cmpTrader.GetFirstMarket() == source && cmpTrader.GetSecondMarket() == target)) | cmpTrader.GetFirstMarket() == source && cmpTrader.GetSecondMarket() == target)) | ||||
{ | { | ||||
this.BackToWork(); | this.BackToWork(); | ||||
return; | return; | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 812 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
function has super weird name and is used only once. inline