Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/simulation/components/UnitAI.js
Show First 20 Lines • Show All 314 Lines • ▼ Show 20 Lines | "Order.WalkToTarget": function(msg) { | ||||
// 2. If unpacked, we first need to pack, then follow case 1. | // 2. If unpacked, we first need to pack, then follow case 1. | ||||
if (this.CanPack()) | if (this.CanPack()) | ||||
{ | { | ||||
this.PushOrderFront("Pack", { "force": true }); | this.PushOrderFront("Pack", { "force": true }); | ||||
return; | return; | ||||
} | } | ||||
if (this.CheckTargetRangeExplicit(this.order.data.target, 0, 0)) | if (this.CheckRange(this.order.data)) | ||||
{ | { | ||||
// We are already at the target, or can't move at all | // We are already at the target, or can't move at all | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return true; | return true; | ||||
} | } | ||||
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.PickupUnit": function(msg) { | "Order.PickupUnit": function(msg) { | ||||
var cmpGarrisonHolder = Engine.QueryInterface(this.entity, IID_GarrisonHolder); | let cmpGarrisonHolder = Engine.QueryInterface(this.entity, IID_GarrisonHolder); | ||||
if (!cmpGarrisonHolder || cmpGarrisonHolder.IsFull()) | if (!cmpGarrisonHolder || cmpGarrisonHolder.IsFull()) | ||||
{ | { | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return; | return; | ||||
} | } | ||||
if (this.CheckTargetRangeExplicit(this.order.data.target, 0, 0)) | if (this.CheckRange(this.order.data)) | ||||
{ | { | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return; | return; | ||||
} | } | ||||
// Check if we need to move TODO implement a better way to know if we are on the shoreline | // Check if we need to move | ||||
var needToMove = true; | // TODO implement a better way to know if we are on the shoreline | ||||
var cmpPosition = Engine.QueryInterface(this.entity, IID_Position); | let needToMove = true; | ||||
if (this.lastShorelinePosition && cmpPosition && (this.lastShorelinePosition.x == cmpPosition.GetPosition().x) | let cmpPosition = Engine.QueryInterface(this.entity, IID_Position); | ||||
&& (this.lastShorelinePosition.z == cmpPosition.GetPosition().z)) | if (this.lastShorelinePosition && cmpPosition && (this.lastShorelinePosition.x == cmpPosition.GetPosition().x) && | ||||
{ | (this.lastShorelinePosition.z == cmpPosition.GetPosition().z)) | ||||
// we were already on the shoreline, and have not moved since | // we were already on the shoreline, and have not moved since | ||||
if (DistanceBetweenEntities(this.entity, this.order.data.target) < 50) | if (DistanceBetweenEntities(this.entity, this.order.data.target) < 50) | ||||
needToMove = false; | needToMove = false; | ||||
} | |||||
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) { | ||||
▲ Show 20 Lines • Show All 292 Lines • ▼ Show 20 Lines | "FORMATIONCONTROLLER": { | ||||
"Order.MoveIntoFormation": function(msg) { | "Order.MoveIntoFormation": function(msg) { | ||||
this.CallMemberFunction("SetHeldPosition", [msg.data.x, msg.data.z]); | this.CallMemberFunction("SetHeldPosition", [msg.data.x, msg.data.z]); | ||||
this.SetNextState("FORMING"); | this.SetNextState("FORMING"); | ||||
}, | }, | ||||
// Only used by other orders to walk there in formation | // Only used by other orders to walk there in formation | ||||
"Order.WalkToTargetRange": function(msg) { | "Order.WalkToTargetRange": function(msg) { | ||||
if (!this.CheckTargetRangeExplicit(this.order.data.target, this.order.data.min, this.order.data.max)) | if (!this.CheckRange(this.order.data)) | ||||
this.SetNextState("WALKING"); | this.SetNextState("WALKING"); | ||||
else | else | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
}, | }, | ||||
"Order.WalkToTarget": function(msg) { | "Order.WalkToTarget": function(msg) { | ||||
if (!this.CheckTargetRangeExplicit(this.order.data.target, 0, 0)) | if (!this.CheckRange(this.order.data)) | ||||
this.SetNextState("WALKING"); | this.SetNextState("WALKING"); | ||||
else | else | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
}, | }, | ||||
"Order.WalkToPointRange": function(msg) { | "Order.WalkToPointRange": function(msg) { | ||||
if (!this.CheckPointRangeExplicit(this.order.data.x, this.order.data.z, this.order.data.min, this.order.data.max)) | if (!this.CheckRange(this.order.data)) | ||||
this.SetNextState("WALKING"); | this.SetNextState("WALKING"); | ||||
else | else | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
}, | }, | ||||
"Order.Patrol": function(msg) { | "Order.Patrol": function(msg) { | ||||
this.CallMemberFunction("SetHeldPosition", [msg.data.x, msg.data.z]); | this.CallMemberFunction("SetHeldPosition", [msg.data.x, msg.data.z]); | ||||
this.SetNextState("PATROL"); | this.SetNextState("PATROL"); | ||||
▲ Show 20 Lines • Show All 595 Lines • ▼ Show 20 Lines | "WALKING": { | ||||
// is done moving. The controller is notified. | // is done moving. The controller is notified. | ||||
"MovementUpdate": function(msg) { | "MovementUpdate": function(msg) { | ||||
// We can only finish this order if the move was really completed. | // We can only finish this order if the move was really completed. | ||||
let cmpPosition = Engine.QueryInterface(this.formationController, IID_Position); | let cmpPosition = Engine.QueryInterface(this.formationController, IID_Position); | ||||
let atDestination = cmpPosition && cmpPosition.IsInWorld(); | let atDestination = cmpPosition && cmpPosition.IsInWorld(); | ||||
if (!atDestination && cmpPosition) | if (!atDestination && cmpPosition) | ||||
{ | { | ||||
let pos = cmpPosition.GetPosition2D(); | let pos = cmpPosition.GetPosition2D(); | ||||
atDestination = this.CheckPointRangeExplicit(pos.X + this.order.data.x, pos.Y + this.order.data.z, 0, 0); | atDestination = this.CheckPointRangeExplicit(pos.X + this.order.data.x, pos.Y + this.order.data.z, 0, 1); | ||||
} | } | ||||
if (!atDestination && !msg.error) | if (!atDestination && !msg.error) | ||||
return; | return; | ||||
if (this.FinishOrder()) | if (this.FinishOrder()) | ||||
return; | return; | ||||
let cmpFormation = Engine.QueryInterface(this.formationController, IID_Formation); | let cmpFormation = Engine.QueryInterface(this.formationController, IID_Formation); | ||||
▲ Show 20 Lines • Show All 2,839 Lines • ▼ Show 20 Lines | else if (data.min || data.max) | ||||
return this.MoveToPointRange(data.x, data.z, data.min || -1, data.max || -1); | return this.MoveToPointRange(data.x, data.z, data.min || -1, data.max || -1); | ||||
return this.MoveToPoint(data.x, data.z); | return this.MoveToPoint(data.x, data.z); | ||||
}; | }; | ||||
UnitAI.prototype.MoveToPoint = function(x, z) | UnitAI.prototype.MoveToPoint = function(x, z) | ||||
{ | { | ||||
var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | ||||
return cmpUnitMotion.MoveToPointRange(x, z, 0, 0); | return cmpUnitMotion.MoveToPointRange(x, z, 0, 0); // For point goals, allow a max range of 0. | ||||
}; | }; | ||||
UnitAI.prototype.MoveToPointRange = function(x, z, rangeMin, rangeMax) | UnitAI.prototype.MoveToPointRange = function(x, z, rangeMin, rangeMax) | ||||
{ | { | ||||
var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | ||||
return cmpUnitMotion.MoveToPointRange(x, z, rangeMin, rangeMax); | return cmpUnitMotion.MoveToPointRange(x, z, rangeMin, rangeMax); | ||||
}; | }; | ||||
UnitAI.prototype.MoveToTarget = function(target) | UnitAI.prototype.MoveToTarget = function(target) | ||||
{ | { | ||||
if (!this.CheckTargetVisible(target)) | if (!this.CheckTargetVisible(target)) | ||||
return false; | return false; | ||||
var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | ||||
return cmpUnitMotion.MoveToTargetRange(target, 0, 0); | return cmpUnitMotion.MoveToTargetRange(target, 0, 1); | ||||
}; | }; | ||||
UnitAI.prototype.MoveToTargetRange = function(target, iid, type) | UnitAI.prototype.MoveToTargetRange = function(target, iid, type) | ||||
{ | { | ||||
if (!this.CheckTargetVisible(target) || this.IsTurret()) | if (!this.CheckTargetVisible(target) || this.IsTurret()) | ||||
return false; | return false; | ||||
var cmpRanged = Engine.QueryInterface(this.entity, iid); | var cmpRanged = Engine.QueryInterface(this.entity, iid); | ||||
▲ Show 20 Lines • Show All 89 Lines • ▼ Show 20 Lines | |||||
*/ | */ | ||||
UnitAI.prototype.CheckRange = function(data, iid, type) | UnitAI.prototype.CheckRange = function(data, iid, type) | ||||
{ | { | ||||
if (data.target) | if (data.target) | ||||
{ | { | ||||
if (data.min || data.max) | if (data.min || data.max) | ||||
return this.CheckTargetRangeExplicit(data.target, data.min || -1, data.max || -1); | return this.CheckTargetRangeExplicit(data.target, data.min || -1, data.max || -1); | ||||
else if (!iid) | else if (!iid) | ||||
return this.CheckTargetRangeExplicit(data.target, 0, 0); | return this.CheckTargetRangeExplicit(data.target, 0, 1); | ||||
return this.CheckTargetRange(data.target, iid, type); | return this.CheckTargetRange(data.target, iid, type); | ||||
} | } | ||||
else if (data.min || data.max) | 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, data.min || -1, data.max || -1); | ||||
return this.CheckPointRangeExplicit(data.x, data.z, 0, 0); | return this.CheckPointRangeExplicit(data.x, data.z, 0, 0); | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 1,646 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator