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,621 Lines • ▼ Show 20 Lines | "GATHER": { | ||||
// 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()) | ||||
return true; | return true; | ||||
// No remaining orders - pick a useful default behaviour | // No remaining orders - pick a useful default behaviour | ||||
// Give up if we're not in the world right now. | |||||
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; | ||||
// If we have no known initial position of our target, look around our own position | let filter = (ent, type, template) => { | ||||
// as a fallback. | |||||
if (!initPos) | |||||
{ | |||||
let pos = cmpPosition.GetPosition(); | |||||
initPos = { 'x': pos.X, 'z': pos.Z }; | |||||
} | |||||
// Try to find a new resource of the same specific type near the initial resource position: | |||||
// Also don't switch to a different type of huntable animal | |||||
let nearbyResource = this.FindNearbyResource(new Vector2D(initPos.x, initPos.z), | |||||
(ent, type, template) => { | |||||
if (previousTarget == ent) | if (previousTarget == ent) | ||||
return false; | return false; | ||||
// 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); | ||||
}); | }; | ||||
// Current position is often next to a dropsite. | |||||
let pos = cmpPosition.GetPosition(); | |||||
let nearbyResource = this.FindNearbyResource(Vector2D.from3D(pos), filter); | |||||
// If there is an initPos, search there as well when we haven't found anything. | |||||
// Otherwise set initPos to our current pos. | |||||
if (!initPos) | |||||
initPos = { 'x': pos.X, 'z': pos.Z }; | |||||
else if (!nearbyResource) | |||||
nearbyResource = this.FindNearbyResource(new Vector2D(initPos.X, initPos.Z), filter); | |||||
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 4,000 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator