Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/UnitAI.js
Show First 20 Lines • Show All 2,491 Lines • ▼ Show 20 Lines | "GATHER": { | ||||
"TargetInvalidated": function(msg) { | "TargetInvalidated": function(msg) { | ||||
this.SetNextState("FINDINGNEWTARGET"); | this.SetNextState("FINDINGNEWTARGET"); | ||||
}, | }, | ||||
}, | }, | ||||
"FINDINGNEWTARGET": { | "FINDINGNEWTARGET": { | ||||
"enter": function() { | "enter": function() { | ||||
const previousForced = this.order.data.force; | |||||
Stan: previouslyForced maybe, or currentlyForced.
Why does it need to be cached? | |||||
let previousTarget = this.order.data.target; | let previousTarget = this.order.data.target; | ||||
let resourceTemplate = this.order.data.template; | let resourceTemplate = this.order.data.template; | ||||
let resourceType = this.order.data.type; | let resourceType = this.order.data.type; | ||||
// Give up on this order and try our next queued order | // Give up on this order and try our next queued order | ||||
// but first check what is our next order and, if needed, insert a returnResource order | // but first check what is our next order and, if needed, insert a returnResource order | ||||
let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); | let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); | ||||
if (cmpResourceGatherer.IsCarrying(resourceType.generic) && | if (cmpResourceGatherer.IsCarrying(resourceType.generic) && | ||||
this.orderQueue.length > 1 && this.orderQueue[1] !== "ReturnResource" && | this.orderQueue.length > 1 && this.orderQueue[1] !== "ReturnResource" && | ||||
(this.orderQueue[1].type !== "Gather" || this.orderQueue[1].data.type.generic !== resourceType.generic)) | (this.orderQueue[1].type !== "Gather" || this.orderQueue[1].data.type.generic !== resourceType.generic)) | ||||
{ | { | ||||
let nearestDropsite = this.FindNearestDropsite(resourceType.generic); | let nearestDropsite = this.FindNearestDropsite(resourceType.generic); | ||||
if (nearestDropsite) | if (nearestDropsite) | ||||
this.orderQueue.splice(1, 0, { "type": "ReturnResource", "data": { "target": nearestDropsite, "force": false } }); | this.orderQueue.splice(1, 0, { "type": "ReturnResource", "data": { "target": nearestDropsite, "force": false } }); | ||||
} | } | ||||
// Must go before FinishOrder or this.order will be undefined. | // Must go before FinishOrder or this.order will be undefined. | ||||
let initPos = this.order.data.initPos; | let initPos = this.order.data.initPos; | ||||
if (this.FinishOrder()) | if (this.FinishOrder()) | ||||
Done Inline ActionsThis resets the order. Freagarach: This resets the order. | |||||
return true; | return true; | ||||
// No remaining orders - pick a useful default behaviour | // No remaining orders - pick a useful default behaviour | ||||
let cmpPosition = Engine.QueryInterface(this.entity, IID_Position); | let cmpPosition = Engine.QueryInterface(this.entity, IID_Position); | ||||
if (!cmpPosition || !cmpPosition.IsInWorld()) | if (!cmpPosition || !cmpPosition.IsInWorld()) | ||||
return true; | return true; | ||||
let filter = (ent, type, template) => { | let filter = (ent, type, template) => { | ||||
if (previousTarget == ent) | if (previousTarget == ent) | ||||
return false; | return false; | ||||
// Don't switch to a different type of huntable animal. | // Don't switch to a different type of huntable animal. | ||||
return type.specific == resourceType.specific && | return type.specific == resourceType.specific && | ||||
(type.specific != "meat" || resourceTemplate == template); | (type.specific != "meat" || resourceTemplate == template); | ||||
}; | }; | ||||
// If we had a forced order, go look there for resources, when possible. | |||||
if (previousForced) | |||||
{ | |||||
const previousTargetPosition = Engine.QueryInterface(previousTarget, IID_Position)?.GetPosition2D(); | |||||
if (previousTargetPosition) | |||||
{ | |||||
this.GatherNearPosition(previousTargetPosition.x, previousTargetPosition.y, resourceType, resourceTemplate); | |||||
return true; | |||||
} | |||||
} | |||||
// Current position is often next to a dropsite. | // Current position is often next to a dropsite. | ||||
let pos = cmpPosition.GetPosition(); | let pos = cmpPosition.GetPosition(); | ||||
let nearbyResource = this.FindNearbyResource(Vector2D.from3D(pos), filter); | let nearbyResource = this.FindNearbyResource(Vector2D.from3D(pos), filter); | ||||
// If there is an initPos, search there as well when we haven't found anything. | // If there is an initPos, search there as well when we haven't found anything. | ||||
// Otherwise set initPos to our current pos. | // Otherwise set initPos to our current pos. | ||||
if (!initPos) | if (!initPos) | ||||
initPos = { 'x': pos.X, 'z': pos.Z }; | initPos = { 'x': pos.X, 'z': pos.Z }; | ||||
else if (!nearbyResource) | else if (!nearbyResource) | ||||
nearbyResource = this.FindNearbyResource(new Vector2D(initPos.x, initPos.z), filter); | nearbyResource = this.FindNearbyResource(new Vector2D(initPos.x, initPos.z), filter); | ||||
Done Inline ActionsThe previousForced case now has a wasteful FIndNearbyResource call. wraitii: The previousForced case now has a wasteful FIndNearbyResource call. | |||||
if (nearbyResource) | if (nearbyResource) | ||||
{ | { | ||||
this.PerformGather(nearbyResource, false, false); | this.PerformGather(nearbyResource, false, false); | ||||
return true; | return true; | ||||
} | } | ||||
// Failing that, try to move there and se if we are more lucky: maybe there are resources in FOW. | // Failing that, try to move there and se if we are more lucky: maybe there are resources in FOW. | ||||
▲ Show 20 Lines • Show All 3,886 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
previouslyForced maybe, or currentlyForced.
Why does it need to be cached?