Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/UnitAI.js
Show First 20 Lines • Show All 343 Lines • ▼ Show 20 Lines | "Order.WalkToTarget": function(msg) { | ||||
{ | { | ||||
// 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.StopMoving(); | this.StopMoving(); | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
} | } | ||||
}, | }, | ||||
"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()) | let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | ||||
Stan: I believe we don't check for system components (here cmptimer) as we want to know immediately… | |||||
Done Inline ActionsOK. I will hold off on that until I get more comments so that I don't have to make and test too many tiny changes. causative: OK. I will hold off on that until I get more comments so that I don't have to make and test… | |||||
Done Inline ActionsOK causative: OK | |||||
if (!cmpTimer || !cmpGarrisonHolder || cmpGarrisonHolder.IsFull() | |||||
Not Done Inline ActionsThis seems only useful in case we didn't have to move, in which case we FinishOrder... But that's broken anyways, because we still want to pickup that first unit, and the second unit might be far away, so we'll start moving, and we're stuck. Simply remove the 2 FinishOrder below, and always go in INDIVIDUAL.PICKUP.LOADING, and this timing issue goes away. wraitii: This seems only useful in case we didn't have to move, in which case we FinishOrder... But… | |||||
Done Inline ActionsThe purpose of this is so that when you load 100 units into the ship with one command, it doesn't do an expensive CheckNearShore() call for each unit. causative: The purpose of this is so that when you load 100 units into the ship with one command, it… | |||||
|| this.lastPickupTime && this.lastPickupTime + 100 > cmpTimer.GetTime()) | |||||
{ | { | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return; | return; | ||||
} | } | ||||
this.lastPickupTime = cmpTimer.GetTime(); // for performance, don't repeat pickup for each unit | |||||
// 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; | let range = cmpGarrisonHolder.GetLoadingRange(); | ||||
var cmpPosition = Engine.QueryInterface(this.entity, IID_Position); | let needToMove = true; | ||||
Not Done Inline ActionsThis can go below the range check. Freagarach: This can go below the range check. | |||||
Done Inline ActionsI don't think it can because it changes the range check? wraitii: I don't think it can because it changes the range check? | |||||
Not Done Inline ActionsYou're right :) Freagarach: You're right :) | |||||
if (this.lastShorelinePosition && cmpPosition && (this.lastShorelinePosition.x == cmpPosition.GetPosition().x) | if (Engine.QueryInterface(SYSTEM_ENTITY, IID_Pathfinder).CheckNearShore(this.entity, this.order.data.target, range.max)) | ||||
&& (this.lastShorelinePosition.z == cmpPosition.GetPosition().z)) | |||||
{ | |||||
// we were already on the shoreline, and have not moved since | |||||
if (DistanceBetweenEntities(this.entity, this.order.data.target) < 50) | |||||
needToMove = false; | needToMove = false; | ||||
} | |||||
// TODO: what if the units are on a cliff ? the ship will go below the cliff | // TODO: what if the units are on a cliff ? the ship will go below the cliff | ||||
// and the units won't be able to garrison. Should go to the nearest (accessible) shore | // and the units won't be able to garrison. Should go to the nearest (accessible) shore | ||||
if (needToMove && this.MoveToTarget(this.order.data.target)) | if (needToMove && this.MoveToTarget(this.order.data.target)) | ||||
{ | { | ||||
this.SetNextState("INDIVIDUAL.PICKUP.APPROACHING"); | this.SetNextState("INDIVIDUAL.PICKUP.APPROACHING"); | ||||
} | } | ||||
else | else | ||||
▲ Show 20 Lines • Show All 777 Lines • ▼ Show 20 Lines | "PATROL": { | ||||
this.PushOrder("Patrol", this.patrolStartPosOrder); | this.PushOrder("Patrol", this.patrolStartPosOrder); | ||||
this.PushOrder(this.order.type, this.order.data); | this.PushOrder(this.order.type, this.order.data); | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
}, | }, | ||||
}, | }, | ||||
"GARRISON":{ | "GARRISON":{ | ||||
"enter": function() { | |||||
// If the garrisonholder should pickup, warn it so it can take needed action | |||||
var 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 }); | |||||
} | |||||
}, | |||||
"leave": function() { | "leave": function() { | ||||
// If a pickup has been requested and not yet canceled, cancel it | // If a pickup has been requested and not yet canceled, cancel it | ||||
if (this.pickup) | if (this.pickup) | ||||
{ | { | ||||
Engine.PostMessage(this.pickup, MT_PickupCanceled, { "entity": this.entity }); | Engine.PostMessage(this.pickup, MT_PickupCanceled, { "entity": this.entity }); | ||||
delete this.pickup; | delete this.pickup; | ||||
} | } | ||||
}, | }, | ||||
"APPROACHING": { | "APPROACHING": { | ||||
"MoveStarted": function(msg) { | "MoveStarted": function(msg) { | ||||
var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | ||||
cmpFormation.SetRearrange(true); | cmpFormation.SetRearrange(true); | ||||
cmpFormation.MoveMembersIntoFormation(true, true); | cmpFormation.MoveMembersIntoFormation(true, true); | ||||
}, | }, | ||||
"MoveCompleted": function(msg) { | "MoveCompleted": function(msg) { | ||||
this.SetNextState("GARRISONING"); | this.SetNextState("GARRISONING"); | ||||
}, | }, | ||||
}, | }, | ||||
"GARRISONING": { | "GARRISONING": { | ||||
"enter": function() { | "enter": function() { | ||||
// If a pickup has been requested, cancel it as it will be requested by members | |||||
if (this.pickup) | |||||
{ | |||||
Engine.PostMessage(this.pickup, MT_PickupCanceled, { "entity": this.entity }); | |||||
delete this.pickup; | |||||
} | |||||
this.CallMemberFunction("Garrison", [this.order.data.target, false]); | this.CallMemberFunction("Garrison", [this.order.data.target, false]); | ||||
this.SetNextStateAlwaysEntering("MEMBER"); | this.SetNextStateAlwaysEntering("MEMBER"); | ||||
}, | }, | ||||
}, | }, | ||||
}, | }, | ||||
"FORMING": { | "FORMING": { | ||||
"MoveStarted": function(msg) { | "MoveStarted": function(msg) { | ||||
▲ Show 20 Lines • Show All 1,710 Lines • ▼ Show 20 Lines | "REPAIR": { | ||||
{ | { | ||||
// We're already walking to the given point, so add this as a order. | // We're already walking to the given point, so add this as a order. | ||||
this.WalkToTarget(msg.data.newentity, true); | this.WalkToTarget(msg.data.newentity, true); | ||||
} | } | ||||
}, | }, | ||||
}, | }, | ||||
"GARRISON": { | "GARRISON": { | ||||
"enter": function() { | |||||
// If the garrisonholder should pickup, warn it so it can take needed action | |||||
var 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 }); | |||||
} | |||||
}, | |||||
"leave": function() { | "leave": function() { | ||||
// If a pickup has been requested and not yet canceled, cancel it | // If a pickup has been requested and not yet canceled, cancel it | ||||
if (this.pickup) | if (this.pickup) | ||||
{ | { | ||||
Engine.PostMessage(this.pickup, MT_PickupCanceled, { "entity": this.entity }); | Engine.PostMessage(this.pickup, MT_PickupCanceled, { "entity": this.entity }); | ||||
delete this.pickup; | delete this.pickup; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 84 Lines • ▼ Show 20 Lines | "GARRISON": { | ||||
cmpResourceGatherer.CommitResources(dropsiteTypes); | cmpResourceGatherer.CommitResources(dropsiteTypes); | ||||
this.SetGathererAnimationOverride(); | this.SetGathererAnimationOverride(); | ||||
} | } | ||||
} | } | ||||
// If a pickup has been requested, remove it | // If a pickup has been requested, remove it | ||||
if (this.pickup) | if (this.pickup) | ||||
{ | { | ||||
var cmpHolderPosition = Engine.QueryInterface(target, IID_Position); | |||||
var cmpHolderUnitAI = Engine.QueryInterface(target, IID_UnitAI); | |||||
if (cmpHolderUnitAI && cmpHolderPosition) | |||||
cmpHolderUnitAI.lastShorelinePosition = cmpHolderPosition.GetPosition(); | |||||
Engine.PostMessage(this.pickup, MT_PickupCanceled, { "entity": this.entity }); | Engine.PostMessage(this.pickup, MT_PickupCanceled, { "entity": this.entity }); | ||||
delete this.pickup; | delete this.pickup; | ||||
} | } | ||||
if (this.IsTurret()) | if (this.IsTurret()) | ||||
this.SetNextState("IDLE"); | this.SetNextState("IDLE"); | ||||
return false; | return false; | ||||
▲ Show 20 Lines • Show All 1,423 Lines • ▼ Show 20 Lines | UnitAI.prototype.MoveToGarrisonRange = function(target) | ||||
if (!this.CheckTargetVisible(target)) | if (!this.CheckTargetVisible(target)) | ||||
return false; | return false; | ||||
var cmpGarrisonHolder = Engine.QueryInterface(target, IID_GarrisonHolder); | var cmpGarrisonHolder = Engine.QueryInterface(target, IID_GarrisonHolder); | ||||
if (!cmpGarrisonHolder) | if (!cmpGarrisonHolder) | ||||
return false; | return false; | ||||
var range = cmpGarrisonHolder.GetLoadingRange(); | var range = cmpGarrisonHolder.GetLoadingRange(); | ||||
// If a pickup has been requested earlier, cancel it | |||||
if (this.pickup) | |||||
{ | |||||
Engine.PostMessage(this.pickup, MT_PickupCanceled, { "entity": this.entity }); | |||||
delete this.pickup; | |||||
} | |||||
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 }); | |||||
} | |||||
var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | ||||
return cmpUnitMotion.MoveToTargetRange(target, range.min, range.max); | return cmpUnitMotion.MoveToTargetRange(target, range.min, range.max); | ||||
}; | }; | ||||
UnitAI.prototype.CheckPointRangeExplicit = function(x, z, min, max) | UnitAI.prototype.CheckPointRangeExplicit = function(x, z, min, max) | ||||
{ | { | ||||
var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | ||||
return cmpUnitMotion.IsInPointRange(x, z, min, max); | return cmpUnitMotion.IsInPointRange(x, z, min, max); | ||||
▲ Show 20 Lines • Show All 1,621 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
I believe we don't check for system components (here cmptimer) as we want to know immediately if something goes wrong with them