Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/UnitAI.js
Show First 20 Lines • Show All 2,483 Lines • ▼ Show 20 Lines | "GATHER": { | ||||
"leave": function() { | "leave": function() { | ||||
this.StopMoving(); | this.StopMoving(); | ||||
}, | }, | ||||
"MovementUpdate": function(msg) { | "MovementUpdate": function(msg) { | ||||
// If we failed, the GATHERING timer will handle finding a valid resource. | // If we failed, the GATHERING timer will handle finding a valid resource. | ||||
if (msg.likelyFailure || msg.obstructed && this.RelaxedMaxRangeCheck(this.order.data, this.DefaultRelaxedMaxRange) || | if (msg.likelyFailure || msg.obstructed && this.RelaxedMaxRangeCheck(this.order.data, this.DefaultRelaxedMaxRange) || | ||||
this.CheckRange(this.order.data)) | this.CheckRange(this.order.data)) | ||||
this.SetNextState("GATHERING"); | this.SetNextState("FINDINGNEWTARGET"); | ||||
}, | }, | ||||
}, | }, | ||||
"GATHERING": { | "GATHERING": { | ||||
"enter": function() { | "enter": function() { | ||||
let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); | let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); | ||||
if (!cmpResourceGatherer) | if (!cmpResourceGatherer) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 100 Lines • ▼ Show 20 Lines | "GATHER": { | ||||
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); | ||||
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 1,747 Lines • ▼ Show 20 Lines | UnitAI.prototype.FindNearbyResource = function(position, filter) | ||||
let cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); | let cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); | ||||
let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); | let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); | ||||
// Don't account for entity size, we need to match LOS visibility. | // Don't account for entity size, we need to match LOS visibility. | ||||
let nearby = cmpRangeManager.ExecuteQueryAroundPos(position, 0, range, players, IID_ResourceSupply, false); | let nearby = cmpRangeManager.ExecuteQueryAroundPos(position, 0, range, players, IID_ResourceSupply, false); | ||||
return nearby.find(ent => { | return nearby.find(ent => { | ||||
if (!this.CanGather(ent) || !this.CheckTargetVisible(ent)) | if (!this.CanGather(ent) || !this.CheckTargetVisible(ent)) | ||||
return false; | return false; | ||||
let cmpResourceSupply = Engine.QueryInterface(ent, IID_ResourceSupply); | |||||
let type = cmpResourceSupply.GetType(); | |||||
let amount = cmpResourceSupply.GetCurrentAmount(); | |||||
let template = cmpTemplateManager.GetCurrentTemplateName(ent); | let template = cmpTemplateManager.GetCurrentTemplateName(ent); | ||||
if (template.indexOf("resource|") != -1) | if (template.indexOf("resource|") != -1) | ||||
template = template.slice(9); | template = template.slice(9); | ||||
return amount > 0 && cmpResourceSupply.IsAvailableTo(this.entity) && filter(ent, type, template); | let cmpResourceSupply = Engine.QueryInterface(ent, IID_ResourceSupply); | ||||
let type = cmpResourceSupply.GetType(); | |||||
return cmpResourceSupply.IsAvailableTo(this.entity) && filter(ent, type, template); | |||||
}); | }); | ||||
}; | }; | ||||
/** | /** | ||||
* Returns the entity ID of the nearest resource dropsite that accepts | * Returns the entity ID of the nearest resource dropsite that accepts | ||||
* the given type, or undefined if none can be found. | * the given type, or undefined if none can be found. | ||||
*/ | */ | ||||
UnitAI.prototype.FindNearestDropsite = function(genericType) | UnitAI.prototype.FindNearestDropsite = function(genericType) | ||||
▲ Show 20 Lines • Show All 2,085 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator