Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/simulation/components/UnitAI.js
Show First 20 Lines • Show All 295 Lines • ▼ Show 20 Lines | if (cmpFormation) | ||||
// Triggers FormationLeave, which ultimately will FinishOrder, | // Triggers FormationLeave, which ultimately will FinishOrder, | ||||
// discarding this order. | // discarding this order. | ||||
cmpFormation.RemoveMembers([this.entity]); | cmpFormation.RemoveMembers([this.entity]); | ||||
cmpFormation.SetRearrange(true); | cmpFormation.SetRearrange(true); | ||||
} | } | ||||
}, | }, | ||||
"Order.Stop": function(msg) { | "Order.Stop": function(msg) { | ||||
this.StopMoving(); | |||||
this.FinishOrder(); | this.FinishOrder(); | ||||
if (this.IsFormationMember()) | if (this.IsFormationMember()) | ||||
this.SetNextState("FORMATIONMEMBER.IDLE"); | this.SetNextState("FORMATIONMEMBER.IDLE"); | ||||
else | else | ||||
this.SetNextState("INDIVIDUAL.IDLE"); | this.SetNextState("INDIVIDUAL.IDLE"); | ||||
}, | }, | ||||
▲ Show 20 Lines • Show All 443 Lines • ▼ Show 20 Lines | "Order.Guard": function(msg) { | ||||
cmpFormation.Disband(); | cmpFormation.Disband(); | ||||
}, | }, | ||||
"Order.Stop": function(msg) { | "Order.Stop": function(msg) { | ||||
let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | ||||
cmpFormation.ResetOrderVariant(); | cmpFormation.ResetOrderVariant(); | ||||
if (!this.IsAttackingAsFormation()) | if (!this.IsAttackingAsFormation()) | ||||
this.CallMemberFunction("Stop", [false]); | this.CallMemberFunction("Stop", [false]); | ||||
this.StopMoving(); | |||||
this.FinishOrder(); | this.FinishOrder(); | ||||
// Don't move the members back into formation, | // Don't move the members back into formation, | ||||
// as the formation then resets and it looks odd when walk-stopping. | // as the formation then resets and it looks odd when walk-stopping. | ||||
// TODO: this should be improved in the formation reshaping code. | // TODO: this should be improved in the formation reshaping code. | ||||
}, | }, | ||||
"Order.Attack": function(msg) { | "Order.Attack": function(msg) { | ||||
let target = msg.data.target; | let target = msg.data.target; | ||||
▲ Show 20 Lines • Show All 530 Lines • ▼ Show 20 Lines | "MEMBER": { | ||||
let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | ||||
if (cmpFormation) | if (cmpFormation) | ||||
cmpFormation.SetRearrange(false); | cmpFormation.SetRearrange(false); | ||||
// While waiting on members, the formation is more like | // While waiting on members, the formation is more like | ||||
// a group of unit and does not have a well-defined position, | // a group of unit and does not have a well-defined position, | ||||
// so move the controller out of the world to enforce that. | // so move the controller out of the world to enforce that. | ||||
let cmpPosition = Engine.QueryInterface(this.entity, IID_Position); | let cmpPosition = Engine.QueryInterface(this.entity, IID_Position); | ||||
if (cmpPosition && cmpPosition.IsInWorld()) | if (cmpPosition && cmpPosition.IsInWorld()) | ||||
{ | |||||
this.StopMoving(); | |||||
cmpPosition.MoveOutOfWorld(); | cmpPosition.MoveOutOfWorld(); | ||||
} | |||||
this.StartTimer(1000, 1000); | this.StartTimer(1000, 1000); | ||||
return false; | return false; | ||||
}, | }, | ||||
"Timer": function(msg) { | "Timer": function(msg) { | ||||
let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | ||||
if (cmpFormation && !cmpFormation.AreAllMembersWaiting()) | if (cmpFormation && !cmpFormation.AreAllMembersWaiting()) | ||||
▲ Show 20 Lines • Show All 133 Lines • ▼ Show 20 Lines | "WALKINGTOPOINT": { | ||||
if (!this.MoveTo(this.order.data)) | if (!this.MoveTo(this.order.data)) | ||||
{ | { | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return true; | return true; | ||||
} | } | ||||
return false; | return false; | ||||
}, | }, | ||||
"leave": function() { | |||||
this.StopMoving(); | |||||
}, | |||||
"MovementUpdate": function() { | "MovementUpdate": function() { | ||||
if (!this.CheckRange(this.order.data)) | if (!this.CheckRange(this.order.data)) | ||||
return; | return; | ||||
this.StopMoving(); | |||||
this.FinishOrder(); | this.FinishOrder(); | ||||
}, | }, | ||||
}, | }, | ||||
}, | }, | ||||
// States for entities not part of a formation: | // States for entities not part of a formation: | ||||
"INDIVIDUAL": { | "INDIVIDUAL": { | ||||
▲ Show 20 Lines • Show All 354 Lines • ▼ Show 20 Lines | "PATROL": { | ||||
this.FindWalkAndFightTargets(); | this.FindWalkAndFightTargets(); | ||||
++this.stopSurveying; | ++this.stopSurveying; | ||||
} | } | ||||
} | } | ||||
}, | }, | ||||
"GUARD": { | "GUARD": { | ||||
"RemoveGuard": function() { | "RemoveGuard": function() { | ||||
this.StopMoving(); | |||||
this.FinishOrder(); | this.FinishOrder(); | ||||
}, | }, | ||||
"ESCORTING": { | "ESCORTING": { | ||||
"enter": function() { | "enter": function() { | ||||
if (!this.MoveToTargetRangeExplicit(this.isGuardOf, 0, this.guardRange)) | if (!this.MoveToTargetRangeExplicit(this.isGuardOf, 0, this.guardRange)) | ||||
{ | { | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
Show All 32 Lines | "GUARD": { | ||||
"MovementUpdate": function(msg) { | "MovementUpdate": function(msg) { | ||||
if (msg.likelyFailure || this.CheckTargetRangeExplicit(this.isGuardOf, 0, this.guardRange)) | if (msg.likelyFailure || this.CheckTargetRangeExplicit(this.isGuardOf, 0, this.guardRange)) | ||||
this.SetNextState("GUARDING"); | this.SetNextState("GUARDING"); | ||||
}, | }, | ||||
}, | }, | ||||
"GUARDING": { | "GUARDING": { | ||||
"enter": function() { | "enter": function() { | ||||
this.StopMoving(); | |||||
this.StartTimer(1000, 1000); | this.StartTimer(1000, 1000); | ||||
this.SetHeldPositionOnEntity(this.entity); | this.SetHeldPositionOnEntity(this.entity); | ||||
this.SetAnimationVariant("combat"); | this.SetAnimationVariant("combat"); | ||||
this.FaceTowardsTarget(this.order.data.target); | this.FaceTowardsTarget(this.order.data.target); | ||||
return false; | return false; | ||||
}, | }, | ||||
"LosAttackRangeUpdate": function(msg) { | "LosAttackRangeUpdate": function(msg) { | ||||
▲ Show 20 Lines • Show All 118 Lines • ▼ Show 20 Lines | "COMBAT": { | ||||
"leave": function() { | "leave": function() { | ||||
this.StopMoving(); | this.StopMoving(); | ||||
this.StopTimer(); | this.StopTimer(); | ||||
}, | }, | ||||
"Timer": function(msg) { | "Timer": function(msg) { | ||||
if (this.ShouldAbandonChase(this.order.data.target, this.order.data.force, IID_Attack, this.order.data.attackType)) | if (this.ShouldAbandonChase(this.order.data.target, this.order.data.force, IID_Attack, this.order.data.attackType)) | ||||
{ | { | ||||
this.StopMoving(); | |||||
this.FinishOrder(); | this.FinishOrder(); | ||||
if (this.GetStance().respondHoldGround) | if (this.GetStance().respondHoldGround) | ||||
this.WalkToHeldPosition(); | this.WalkToHeldPosition(); | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
this.RememberTargetPosition(); | this.RememberTargetPosition(); | ||||
▲ Show 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | "COMBAT": { | ||||
this.PushOrderFront("Pack", { "force": true }); | this.PushOrderFront("Pack", { "force": true }); | ||||
return true; | return true; | ||||
} | } | ||||
this.SetNextState("COMBAT.APPROACHING"); | this.SetNextState("COMBAT.APPROACHING"); | ||||
return true; | return true; | ||||
} | } | ||||
this.StopMoving(); | |||||
let cmpAttack = Engine.QueryInterface(this.entity, IID_Attack); | let cmpAttack = Engine.QueryInterface(this.entity, IID_Attack); | ||||
this.attackTimers = cmpAttack.GetTimers(this.order.data.attackType); | this.attackTimers = cmpAttack.GetTimers(this.order.data.attackType); | ||||
// If the repeat time since the last attack hasn't elapsed, | // If the repeat time since the last attack hasn't elapsed, | ||||
// delay this attack to avoid attacking too fast. | // delay this attack to avoid attacking too fast. | ||||
let prepare = this.attackTimers.prepare; | let prepare = this.attackTimers.prepare; | ||||
if (this.lastAttacked) | if (this.lastAttacked) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 157 Lines • ▼ Show 20 Lines | "COMBAT": { | ||||
return true; | return true; | ||||
}, | }, | ||||
}, | }, | ||||
"CHASING": { | "CHASING": { | ||||
"Order.MoveToChasingPoint": function(msg) { | "Order.MoveToChasingPoint": function(msg) { | ||||
if (this.CheckPointRangeExplicit(msg.data.x, msg.data.z, 0, msg.data.max)) | if (this.CheckPointRangeExplicit(msg.data.x, msg.data.z, 0, msg.data.max)) | ||||
{ | { | ||||
this.StopMoving(); | |||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return; | return; | ||||
} | } | ||||
this.order.data.relaxed = true; | this.order.data.relaxed = true; | ||||
this.StopTimer(); | this.StopTimer(); | ||||
this.SetNextState("MOVINGTOPOINT"); | this.SetNextState("MOVINGTOPOINT"); | ||||
}, | }, | ||||
"enter": function() { | "enter": function() { | ||||
Show All 11 Lines | "COMBAT": { | ||||
this.SetSpeedMultiplier(this.GetRunMultiplier()); | this.SetSpeedMultiplier(this.GetRunMultiplier()); | ||||
this.StartTimer(1000, 1000); | this.StartTimer(1000, 1000); | ||||
return false; | return false; | ||||
}, | }, | ||||
"leave": function() { | "leave": function() { | ||||
this.ResetSpeedMultiplier(); | this.ResetSpeedMultiplier(); | ||||
this.StopMoving(); | this.StopMoving(); | ||||
this.StopTimer(); | this.StopTimer(); | ||||
}, | }, | ||||
"Timer": function(msg) { | "Timer": function(msg) { | ||||
if (this.ShouldAbandonChase(this.order.data.target, this.order.data.force, IID_Attack, this.order.data.attackType)) | if (this.ShouldAbandonChase(this.order.data.target, this.order.data.force, IID_Attack, this.order.data.attackType)) | ||||
{ | { | ||||
this.StopMoving(); | |||||
this.FinishOrder(); | this.FinishOrder(); | ||||
if (this.GetStance().respondHoldGround) | if (this.GetStance().respondHoldGround) | ||||
this.WalkToHeldPosition(); | this.WalkToHeldPosition(); | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
this.RememberTargetPosition(); | this.RememberTargetPosition(); | ||||
▲ Show 20 Lines • Show All 202 Lines • ▼ Show 20 Lines | "GATHER": { | ||||
// We want to start the gather animation as soon as possible, | // 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 | // but only if we're actually at the target and it's still alive | ||||
// (else it'll look like we're chopping empty air). | // (else it'll look like we're chopping empty air). | ||||
// (If it's not alive, the Timer handler will deal with sending us | // (If it's not alive, the Timer handler will deal with sending us | ||||
// off to a different target.) | // off to a different target.) | ||||
if (this.CheckTargetRange(this.gatheringTarget, IID_ResourceGatherer)) | if (this.CheckTargetRange(this.gatheringTarget, IID_ResourceGatherer)) | ||||
{ | { | ||||
this.StopMoving(); | |||||
this.SetDefaultAnimationVariant(); | this.SetDefaultAnimationVariant(); | ||||
this.FaceTowardsTarget(this.order.data.target); | this.FaceTowardsTarget(this.order.data.target); | ||||
this.SelectAnimation("gather_" + this.order.data.type.specific); | this.SelectAnimation("gather_" + this.order.data.type.specific); | ||||
cmpResourceGatherer.AddToPlayerCounter(this.order.data.type.generic); | cmpResourceGatherer.AddToPlayerCounter(this.order.data.type.generic); | ||||
} | } | ||||
return false; | return false; | ||||
}, | }, | ||||
▲ Show 20 Lines • Show All 467 Lines • ▼ Show 20 Lines | "REPAIR": { | ||||
if (cmpHealth && cmpHealth.GetHitpoints() >= cmpHealth.GetMaxHitpoints()) | if (cmpHealth && cmpHealth.GetHitpoints() >= cmpHealth.GetMaxHitpoints()) | ||||
{ | { | ||||
// The building was already finished/fully repaired before we arrived; | // The building was already finished/fully repaired before we arrived; | ||||
// let the ConstructionFinished handler handle this. | // let the ConstructionFinished handler handle this. | ||||
this.ConstructionFinished({ "entity": this.repairTarget, "newentity": this.repairTarget }); | this.ConstructionFinished({ "entity": this.repairTarget, "newentity": this.repairTarget }); | ||||
return true; | return true; | ||||
} | } | ||||
this.StopMoving(); | |||||
let cmpBuilderList = QueryBuilderListInterface(this.repairTarget); | let cmpBuilderList = QueryBuilderListInterface(this.repairTarget); | ||||
if (cmpBuilderList) | if (cmpBuilderList) | ||||
cmpBuilderList.AddBuilder(this.entity); | cmpBuilderList.AddBuilder(this.entity); | ||||
this.FaceTowardsTarget(this.repairTarget); | this.FaceTowardsTarget(this.repairTarget); | ||||
this.SelectAnimation("build"); | this.SelectAnimation("build"); | ||||
this.StartTimer(1000, 1000); | this.StartTimer(1000, 1000); | ||||
▲ Show 20 Lines • Show All 283 Lines • ▼ Show 20 Lines | "CHEERING": { | ||||
"Timer": function(msg) { | "Timer": function(msg) { | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
}, | }, | ||||
}, | }, | ||||
"PACKING": { | "PACKING": { | ||||
"enter": function() { | "enter": function() { | ||||
this.StopMoving(); | let cmpPack = Engine.QueryInterface(this.entity, IID_Pack); | ||||
var cmpPack = Engine.QueryInterface(this.entity, IID_Pack); | |||||
cmpPack.Pack(); | cmpPack.Pack(); | ||||
return false; | return false; | ||||
}, | }, | ||||
"PackFinished": function(msg) { | "PackFinished": function(msg) { | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
}, | }, | ||||
"leave": function() { | "leave": function() { | ||||
let cmpPack = Engine.QueryInterface(this.entity, IID_Pack); | let cmpPack = Engine.QueryInterface(this.entity, IID_Pack); | ||||
cmpPack.CancelPack(); | cmpPack.CancelPack(); | ||||
}, | }, | ||||
"Attacked": function(msg) { | "Attacked": function(msg) { | ||||
// Ignore attacks while packing | // Ignore attacks while packing | ||||
}, | }, | ||||
}, | }, | ||||
"UNPACKING": { | "UNPACKING": { | ||||
"enter": function() { | "enter": function() { | ||||
this.StopMoving(); | let cmpPack = Engine.QueryInterface(this.entity, IID_Pack); | ||||
var cmpPack = Engine.QueryInterface(this.entity, IID_Pack); | |||||
cmpPack.Unpack(); | cmpPack.Unpack(); | ||||
return false; | return false; | ||||
}, | }, | ||||
"PackFinished": function(msg) { | "PackFinished": function(msg) { | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
}, | }, | ||||
Show All 23 Lines | "PICKUP": { | ||||
}, | }, | ||||
"MovementUpdate": function(msg) { | "MovementUpdate": function(msg) { | ||||
if (msg.likelyFailure || msg.likelySuccess) | if (msg.likelyFailure || msg.likelySuccess) | ||||
this.SetNextState("LOADING"); | this.SetNextState("LOADING"); | ||||
}, | }, | ||||
"PickupCanceled": function() { | "PickupCanceled": function() { | ||||
this.StopMoving(); | |||||
this.FinishOrder(); | this.FinishOrder(); | ||||
}, | }, | ||||
}, | }, | ||||
"LOADING": { | "LOADING": { | ||||
"enter": function() { | "enter": function() { | ||||
this.StopMoving(); | let cmpGarrisonHolder = Engine.QueryInterface(this.entity, IID_GarrisonHolder); | ||||
var cmpGarrisonHolder = Engine.QueryInterface(this.entity, IID_GarrisonHolder); | |||||
if (!cmpGarrisonHolder || cmpGarrisonHolder.IsFull()) | if (!cmpGarrisonHolder || cmpGarrisonHolder.IsFull()) | ||||
{ | { | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return true; | return true; | ||||
} | } | ||||
return false; | return false; | ||||
}, | }, | ||||
▲ Show 20 Lines • Show All 2,414 Lines • ▼ Show 20 Lines | |||||
UnitAI.prototype.MarketRemoved = function(market) | UnitAI.prototype.MarketRemoved = function(market) | ||||
{ | { | ||||
if (this.order && this.order.data && this.order.data.target && this.order.data.target == market) | if (this.order && this.order.data && this.order.data.target && this.order.data.target == market) | ||||
this.UnitFsm.ProcessMessage(this, { "type": "TradingCanceled", "market": market }); | this.UnitFsm.ProcessMessage(this, { "type": "TradingCanceled", "market": market }); | ||||
}; | }; | ||||
UnitAI.prototype.StopTrading = function() | UnitAI.prototype.StopTrading = function() | ||||
{ | { | ||||
this.StopMoving(); | |||||
this.FinishOrder(); | this.FinishOrder(); | ||||
var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); | var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); | ||||
cmpTrader.StopTrading(); | cmpTrader.StopTrading(); | ||||
}; | }; | ||||
/** | /** | ||||
* Adds repair/build order to the queue, forced by the player | * Adds repair/build order to the queue, forced by the player | ||||
* until the target is reached | * until the target is reached | ||||
▲ Show 20 Lines • Show All 738 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator