Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/UnitAI.js
Show First 20 Lines • Show All 2,200 Lines • ▼ Show 20 Lines | "GATHER": { | ||||
this.SetDefaultAnimationVariant(); | this.SetDefaultAnimationVariant(); | ||||
}, | }, | ||||
"APPROACHING": { | "APPROACHING": { | ||||
"enter": function() { | "enter": function() { | ||||
this.gatheringTarget = this.order.data.target; // temporary, deleted in "leave". | this.gatheringTarget = this.order.data.target; // temporary, deleted in "leave". | ||||
// Check that we can gather from the resource we're supposed to gather from. | // Check that we can gather from the resource we're supposed to gather from. | ||||
let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); | |||||
let cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); | let cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); | ||||
let cmpMirage = Engine.QueryInterface(this.gatheringTarget, IID_Mirage); | let cmpMirage = Engine.QueryInterface(this.gatheringTarget, IID_Mirage); | ||||
if ((!cmpMirage || !cmpMirage.Mirages(IID_ResourceSupply)) && | if ((!cmpMirage || !cmpMirage.Mirages(IID_ResourceSupply)) && | ||||
(!cmpSupply || !cmpSupply.AddGatherer(cmpOwnership.GetOwner(), this.entity)) || | (!cmpSupply || !cmpSupply.AddGatherer(this.entity)) || | ||||
!this.MoveTo(this.order.data, IID_ResourceGatherer)) | !this.MoveTo(this.order.data, IID_ResourceGatherer)) | ||||
{ | { | ||||
// If the target's last known position is in FOW, try going there | // If the target's last known position is in FOW, try going there | ||||
// and hope that we might find it then. | // and hope that we might find it then. | ||||
let lastPos = this.order.data.lastPos; | let lastPos = this.order.data.lastPos; | ||||
if (this.gatheringTarget != INVALID_ENTITY && | if (this.gatheringTarget != INVALID_ENTITY && | ||||
lastPos && !this.CheckPositionVisible(lastPos.x, lastPos.z)) | lastPos && !this.CheckPositionVisible(lastPos.x, lastPos.z)) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | "GATHER": { | ||||
}, | }, | ||||
"GATHERING": { | "GATHERING": { | ||||
"enter": function() { | "enter": function() { | ||||
this.gatheringTarget = this.order.data.target || INVALID_ENTITY; // deleted in "leave". | this.gatheringTarget = this.order.data.target || INVALID_ENTITY; // deleted in "leave". | ||||
// Check if the resource is full. | // Check if the resource is full. | ||||
// Will only be added if we're not already in. | // Will only be added if we're not already in. | ||||
let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); | let cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); | ||||
let cmpSupply; | if (!cmpSupply || !cmpSupply.AddGatherer(this.entity)) | ||||
if (cmpOwnership) | |||||
cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); | |||||
if (!cmpSupply || !cmpSupply.AddGatherer(cmpOwnership.GetOwner(), this.entity)) | |||||
{ | { | ||||
this.SetNextState("FINDINGNEWTARGET"); | this.SetNextState("FINDINGNEWTARGET"); | ||||
return true; | return true; | ||||
} | } | ||||
// If this order was forced, the player probably gave it, but now we've reached the target | // If this order was forced, the player probably gave it, but now we've reached the target | ||||
// switch to an unforced order (can be interrupted by attacks) | // switch to an unforced order (can be interrupted by attacks) | ||||
this.order.data.force = false; | this.order.data.force = false; | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | "GATHER": { | ||||
this.ResetAnimation(); | this.ResetAnimation(); | ||||
}, | }, | ||||
"Timer": function(msg) { | "Timer": function(msg) { | ||||
let resourceTemplate = this.order.data.template; | let resourceTemplate = this.order.data.template; | ||||
let resourceType = this.order.data.type; | let resourceType = this.order.data.type; | ||||
let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); | |||||
if (!cmpOwnership) | |||||
return; | |||||
// TODO: we are leaking information here - if the target died in FOW, we'll know it's dead | // TODO: we are leaking information here - if the target died in FOW, we'll know it's dead | ||||
// straight away. | // straight away. | ||||
// Seems one would have to listen to ownership changed messages to make it work correctly | // Seems one would have to listen to ownership changed messages to make it work correctly | ||||
// but that's likely prohibitively expansive performance wise. | // but that's likely prohibitively expansive performance wise. | ||||
let cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); | let cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); | ||||
// If we can't gather from the target, find a new one. | // If we can't gather from the target, find a new one. | ||||
if (!cmpSupply || !cmpSupply.IsAvailable(cmpOwnership.GetOwner(), this.entity) || | if (!cmpSupply || !cmpSupply.IsAvailableTo(this.entity) || | ||||
!this.CanGather(this.gatheringTarget)) | !this.CanGather(this.gatheringTarget)) | ||||
{ | { | ||||
this.SetNextState("FINDINGNEWTARGET"); | this.SetNextState("FINDINGNEWTARGET"); | ||||
return; | return; | ||||
} | } | ||||
if (!this.CheckTargetRange(this.gatheringTarget, IID_ResourceGatherer)) | if (!this.CheckTargetRange(this.gatheringTarget, IID_ResourceGatherer)) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 1,888 Lines • ▼ Show 20 Lines | |||||
* "Nearest" is nearest from @param position. | * "Nearest" is nearest from @param position. | ||||
* TODO: extend this to exclude resources that already have lots of gatherers. | * TODO: extend this to exclude resources that already have lots of gatherers. | ||||
*/ | */ | ||||
UnitAI.prototype.FindNearbyResource = function(position, filter) | UnitAI.prototype.FindNearbyResource = function(position, filter) | ||||
{ | { | ||||
if (!position) | if (!position) | ||||
return undefined; | return undefined; | ||||
let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); | let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); | ||||
if (!cmpOwnership || cmpOwnership.GetOwner() == INVALID_PLAYER) | if (!cmpOwnership || cmpOwnership.GetOwner() == INVALID_PLAYER) | ||||
return undefined; | return undefined; | ||||
bb: Why keep all this? Shouldn't happen often so probably causes more performance downtime than it… | |||||
wraitiiUnsubmitted Done Inline ActionsAgreed, seems useless. wraitii: Agreed, seems useless. | |||||
let owner = cmpOwnership.GetOwner(); | |||||
// We accept resources owned by Gaia or any player | // We accept resources owned by Gaia or any player | ||||
let players = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetAllPlayers(); | let players = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetAllPlayers(); | ||||
let range = 64; // TODO: what's a sensible number? | let range = 64; // TODO: what's a sensible number? | ||||
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); | ||||
let nearby = cmpRangeManager.ExecuteQueryAroundPos(position, 0, range, players, IID_ResourceSupply); | let nearby = cmpRangeManager.ExecuteQueryAroundPos(position, 0, range, players, IID_ResourceSupply); | ||||
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 cmpResourceSupply = Engine.QueryInterface(ent, IID_ResourceSupply); | ||||
let type = cmpResourceSupply.GetType(); | let type = cmpResourceSupply.GetType(); | ||||
let amount = cmpResourceSupply.GetCurrentAmount(); | let amount = cmpResourceSupply.GetCurrentAmount(); | ||||
let template = cmpTemplateManager.GetCurrentTemplateName(ent); | let template = cmpTemplateManager.GetCurrentTemplateName(ent); | ||||
// Remove "resource|" prefix from template names, if present. | // Remove "resource|" prefix from template names, if present. | ||||
if (template.indexOf("resource|") != -1) | if (template.indexOf("resource|") != -1) | ||||
template = template.slice(9); | template = template.slice(9); | ||||
return amount > 0 && cmpResourceSupply.IsAvailable(owner, this.entity) && filter(ent, type, template); | return amount > 0 && 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,090 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
Why keep all this? Shouldn't happen often so probably causes more performance downtime than it gains