Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/UnitAI.js
Show First 20 Lines • Show All 552 Lines • ▼ Show 20 Lines | if (!this.AbleToMove()) | ||||
return this.FinishOrder(); | return this.FinishOrder(); | ||||
this.SetNextState("INDIVIDUAL.GATHER.WALKING"); | this.SetNextState("INDIVIDUAL.GATHER.WALKING"); | ||||
msg.data.initPos = { 'x': msg.data.x, 'z': msg.data.z }; | msg.data.initPos = { 'x': msg.data.x, 'z': msg.data.z }; | ||||
msg.data.relaxed = true; | msg.data.relaxed = true; | ||||
return ACCEPT_ORDER; | return ACCEPT_ORDER; | ||||
}, | }, | ||||
"Order.ReturnResource": function(msg) { | "Order.ReturnResource": function(msg) { | ||||
let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); | if (this.CheckTargetRange(msg.data.target, IID_ResourceGatherer)) | ||||
if (this.CheckTargetRange(msg.data.target, IID_ResourceGatherer) && | this.SetNextState("RETURNRESOURCE.DROPPINGRESOURCES"); | ||||
this.CanReturnResource(msg.data.target, true, cmpResourceGatherer)) | |||||
{ | |||||
cmpResourceGatherer.CommitResources(msg.data.target); | |||||
this.SetDefaultAnimationVariant(); | |||||
// Our next order should always be a Gather, | |||||
// so just switch back to that order. | |||||
this.FinishOrder(); | |||||
} | |||||
else if (this.AbleToMove()) | else if (this.AbleToMove()) | ||||
this.SetNextState("INDIVIDUAL.RETURNRESOURCE.APPROACHING"); | this.SetNextState("RETURNRESOURCE.APPROACHING"); | ||||
else | else | ||||
return this.FinishOrder(); | return this.FinishOrder(); | ||||
return ACCEPT_ORDER; | return ACCEPT_ORDER; | ||||
}, | }, | ||||
"Order.Trade": function(msg) { | "Order.Trade": function(msg) { | ||||
if (!this.AbleToMove()) | if (!this.AbleToMove()) | ||||
return this.FinishOrder(); | return this.FinishOrder(); | ||||
▲ Show 20 Lines • Show All 1,853 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 153 Lines • ▼ Show 20 Lines | "RETURNRESOURCE": { | ||||
let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); | let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); | ||||
if (this.CanReturnResource(this.order.data.target, true, cmpResourceGatherer)) | if (this.CanReturnResource(this.order.data.target, true, cmpResourceGatherer)) | ||||
{ | { | ||||
cmpResourceGatherer.CommitResources(this.order.data.target); | cmpResourceGatherer.CommitResources(this.order.data.target); | ||||
// Stop showing the carried resource animation. | // Stop showing the carried resource animation. | ||||
this.SetDefaultAnimationVariant(); | this.SetDefaultAnimationVariant(); | ||||
// Our next order should always be a Gather, | |||||
// so just switch back to that order. | |||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return true; | return true; | ||||
} | } | ||||
let nearby = this.FindNearestDropsite(cmpResourceGatherer.GetMainCarryingType()); | let nearby = this.FindNearestDropsite(cmpResourceGatherer.GetMainCarryingType()); | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
if (nearby) | if (nearby) | ||||
this.PushOrderFront("ReturnResource", { "target": nearby, "force": false }); | this.PushOrderFront("ReturnResource", { "target": nearby, "force": false }); | ||||
▲ Show 20 Lines • Show All 1,573 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,060 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator