Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/simulation/components/UnitAI.js
Show First 20 Lines • Show All 2,078 Lines • ▼ Show 20 Lines | "COMBAT": { | ||||
if (this.GetStance().respondHoldGround) | if (this.GetStance().respondHoldGround) | ||||
this.WalkToHeldPosition(); | this.WalkToHeldPosition(); | ||||
return true; | return true; | ||||
}, | }, | ||||
}, | }, | ||||
"CHASING": { | "CHASING": { | ||||
"Order.MoveToChasingPoint": function(msg) { | |||||
if (this.CheckPointRangeExplicit(msg.data.x, msg.data.z, 0, msg.data.max)) | |||||
{ | |||||
this.StopMoving(); | |||||
this.FinishOrder(); | |||||
return; | |||||
} | |||||
this.order.data.relaxed = true; | |||||
this.StopTimer(); | |||||
this.SetNextState("MOVINGTOPOINT"); | |||||
}, | |||||
"enter": function() { | "enter": function() { | ||||
if (!this.MoveToTargetAttackRange(this.order.data.target, this.order.data.attackType)) | if (!this.MoveToTargetAttackRange(this.order.data.target, this.order.data.attackType)) | ||||
{ | { | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return true; | return true; | ||||
} | } | ||||
if (!this.formationAnimationVariant) | if (!this.formationAnimationVariant) | ||||
this.SetAnimationVariant("combat"); | this.SetAnimationVariant("combat"); | ||||
var cmpUnitAI = Engine.QueryInterface(this.order.data.target, IID_UnitAI); | var cmpUnitAI = Engine.QueryInterface(this.order.data.target, IID_UnitAI); | ||||
if (cmpUnitAI && cmpUnitAI.IsFleeing()) | if (cmpUnitAI && cmpUnitAI.IsFleeing()) | ||||
{ | |||||
// Run after a fleeing target | |||||
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.StopMoving(); | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
// Return to our original position | // Return to our original position | ||||
if (this.GetStance().respondHoldGround) | if (this.GetStance().respondHoldGround) | ||||
this.WalkToHeldPosition(); | this.WalkToHeldPosition(); | ||||
} | } | ||||
else | |||||
{ | |||||
this.RememberTargetPosition(); | |||||
if (this.order.data.hunting && this.orderQueue.length > 1 && | |||||
this.orderQueue[1].type === "Gather") | |||||
this.RememberTargetPosition(this.orderQueue[1].data); | |||||
} | |||||
}, | }, | ||||
"MovementUpdate": function(msg) { | "MovementUpdate": function(msg) { | ||||
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; | |||||
} | |||||
} | |||||
if (this.CheckTargetAttackRange(this.order.data.target, this.order.data.attackType)) | if (this.CheckTargetAttackRange(this.order.data.target, this.order.data.attackType)) | ||||
{ | { | ||||
// If the unit needs to unpack, do so | // If the unit needs to unpack, do so | ||||
if (this.CanUnpack()) | if (this.CanUnpack()) | ||||
{ | { | ||||
this.PushOrderFront("Unpack", { "force": true }); | this.PushOrderFront("Unpack", { "force": true }); | ||||
return; | return; | ||||
} | } | ||||
this.SetNextState("ATTACKING"); | this.SetNextState("ATTACKING"); | ||||
} | } | ||||
else if (msg.likelySuccess) | else if (msg.likelySuccess) | ||||
// Try moving again, | // Try moving again, | ||||
// attack range uses a height-related formula and our actual max range might have changed. | // 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)) | if (!this.MoveToTargetAttackRange(this.order.data.target, this.order.data.attackType)) | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
}, | }, | ||||
"MOVINGTOPOINT": { | |||||
"enter": function() { | |||||
if (!this.MoveTo(this.order.data)) | |||||
{ | |||||
this.FinishOrder(); | |||||
return true; | |||||
} | |||||
return false; | |||||
}, | |||||
"leave": function() { | |||||
this.StopMoving(); | |||||
}, | |||||
"MovementUpdate": function(msg) { | |||||
// 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. | |||||
if (msg.likelyFailure || | |||||
msg.obstructed && this.RelaxedMaxRangeCheck(this.order.data, this.order.data.max + this.DefaultRelaxedMaxRange) || | |||||
!msg.obstructed && this.CheckRange(this.order.data)) | |||||
this.FinishOrder(); | |||||
}, | |||||
}, | |||||
}, | }, | ||||
}, | }, | ||||
"GATHER": { | "GATHER": { | ||||
"leave": function() { | "leave": function() { | ||||
// Show the carried resource, if we've gathered anything. | // Show the carried resource, if we've gathered anything. | ||||
this.SetDefaultAnimationVariant(); | this.SetDefaultAnimationVariant(); | ||||
}, | }, | ||||
▲ Show 20 Lines • Show All 4,117 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator