Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/UnitAI.js
Show First 20 Lines • Show All 2,210 Lines • ▼ Show 20 Lines | "GATHER": { | ||||
// but only if we're actually at the target and it's still alive | // but only if we're actually at the target and it's still alive | ||||
// (else it'll look like we're chopping empty air). | // (else it'll look like we're chopping empty air). | ||||
// (If it's not alive, the Timer handler will deal with sending us | // (If it's not alive, the Timer handler will deal with sending us | ||||
// off to a different target.) | // off to a different target.) | ||||
if (this.CheckTargetRange(this.gatheringTarget, IID_ResourceGatherer)) | if (this.CheckTargetRange(this.gatheringTarget, IID_ResourceGatherer)) | ||||
{ | { | ||||
this.StopMoving(); | this.StopMoving(); | ||||
this.SetDefaultAnimationVariant(); | this.SetDefaultAnimationVariant(); | ||||
if (cmpSupply.IsMine()) | |||||
{ | |||||
let cmpGarrisonHolder = Engine.QueryInterface(this.gatheringTarget, IID_GarrisonHolder); | |||||
if (!cmpGarrisonHolder || !cmpGarrisonHolder.Garrison(this.entity)) | |||||
{ | |||||
this.FinishOrder(); | |||||
return true; | |||||
} | |||||
this.isGarrisoned = true; | |||||
} | |||||
this.FaceTowardsTarget(this.order.data.target); | this.FaceTowardsTarget(this.order.data.target); | ||||
this.SelectAnimation("gather_" + this.order.data.type.specific); | this.SelectAnimation("gather_" + this.order.data.type.specific); | ||||
} | } | ||||
return false; | return false; | ||||
}, | }, | ||||
"leave": function() { | "leave": function() { | ||||
this.StopTimer(); | this.StopTimer(); | ||||
// don't use ownership because this is called after a conversion/resignation | // don't use ownership because this is called after a conversion/resignation | ||||
// and the ownership would be invalid then. | // and the ownership would be invalid then. | ||||
var cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); | var cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); | ||||
if (cmpSupply) | if (cmpSupply) | ||||
cmpSupply.RemoveGatherer(this.entity); | cmpSupply.RemoveGatherer(this.entity); | ||||
delete this.gatheringTarget; | delete this.gatheringTarget; | ||||
// Show the carried resource, if we've gathered anything. | // Show the carried resource, if we've gathered anything. | ||||
this.ResetAnimation(); | this.ResetAnimation(); | ||||
this.SetDefaultAnimationVariant(); | this.SetDefaultAnimationVariant(); | ||||
}, | }, | ||||
"Timer": function(msg) { | "Timer": function(msg) { | ||||
let orderData = this.order.data; | |||||
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); | let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); | ||||
if (!cmpOwnership) | if (!cmpOwnership) | ||||
return; | 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.IsAvailable(cmpOwnership.GetOwner(), 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) && !cmpSupply.IsMine()) | ||||
{ | { | ||||
// Try to follow the target | // Try to follow the target | ||||
if (this.MoveToTargetRange(this.gatheringTarget, IID_ResourceGatherer)) | if (this.MoveToTargetRange(this.gatheringTarget, IID_ResourceGatherer)) | ||||
this.SetNextState("APPROACHING"); | this.SetNextState("APPROACHING"); | ||||
// Our target is no longer visible - go to its last known position first | // Our target is no longer visible - go to its last known position first | ||||
// and then hopefully it will become visible. | // and then hopefully it will become visible. | ||||
else if (!this.CheckTargetVisible(this.gatheringTarget) && this.order.data.lastPos) | else if (!this.CheckTargetVisible(this.gatheringTarget) && this.order.data.lastPos) | ||||
this.PushOrderFront("Walk", { | this.PushOrderFront("Walk", { | ||||
Show All 23 Lines | "GATHER": { | ||||
// Collect from the target | // Collect from the target | ||||
let status = cmpResourceGatherer.PerformGather(this.gatheringTarget); | let status = cmpResourceGatherer.PerformGather(this.gatheringTarget); | ||||
// If we've collected as many resources as possible, | // If we've collected as many resources as possible, | ||||
// return to the nearest dropsite | // return to the nearest dropsite | ||||
if (status.filled) | if (status.filled) | ||||
{ | { | ||||
if (cmpSupply.IsMine()) | |||||
{ | |||||
let cmpGarrisonHolder = Engine.QueryInterface(this.gatheringTarget, IID_GarrisonHolder); | |||||
if (cmpGarrisonHolder && !cmpGarrisonHolder.Unload(this.entity)) | |||||
return; | |||||
} | |||||
let nearby = this.FindNearestDropsite(resourceType.generic); | let nearby = this.FindNearestDropsite(resourceType.generic); | ||||
if (nearby) | if (nearby) | ||||
{ | { | ||||
// (Keep this Gather order on the stack so we'll | // (Keep this Gather order on the stack so we'll | ||||
// continue gathering after returning) | // continue gathering after returning) | ||||
// However mark our target as invalid if it's exhausted, so we don't waste time | // However mark our target as invalid if it's exhausted, so we don't waste time | ||||
// trying to gather from it. | // trying to gather from it. | ||||
if (status.exhausted) | if (status.exhausted) | ||||
this.order.data.target = INVALID_ENTITY; | this.order ? this.order.data.target = INVALID_ENTITY : orderData.target = INVALID_ENTITY; | ||||
this.PushOrderFront("ReturnResource", { "target": nearby, "force": false }); | this.PushOrderFront("ReturnResource", { "target": nearby, "force": false }); | ||||
// Needed because the ungarrison order clears the order queue. | |||||
if (cmpSupply.IsMine()) | |||||
this.PushOrder("Gather", orderData); | |||||
return; | return; | ||||
} | } | ||||
// Oh no, couldn't find any drop sites. Give up on gathering. | // Oh no, couldn't find any drop sites. Give up on gathering. | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return; | return; | ||||
} | } | ||||
// Find a new target if the current one is exhausted | // Find a new target if the current one is exhausted | ||||
if (status.exhausted) | if (status.exhausted) | ||||
{ | |||||
if (cmpSupply.IsMine()) | |||||
{ | |||||
let cmpGarrisonHolder = Engine.QueryInterface(this.gatheringTarget, IID_GarrisonHolder); | |||||
if (cmpGarrisonHolder && !cmpGarrisonHolder.Unload(this.entity)) | |||||
return; | |||||
orderData.target = INVALID_ENTITY; | |||||
this.PushOrder("Gather", orderData); | |||||
} | |||||
this.SetNextState("FINDINGNEWTARGET"); | this.SetNextState("FINDINGNEWTARGET"); | ||||
} | |||||
}, | }, | ||||
}, | }, | ||||
"FINDINGNEWTARGET": { | "FINDINGNEWTARGET": { | ||||
"enter": function() { | "enter": function() { | ||||
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; | ||||
▲ Show 20 Lines • Show All 616 Lines • ▼ Show 20 Lines | "GARRISON": { | ||||
var cmpHolderPosition = Engine.QueryInterface(target, IID_Position); | var cmpHolderPosition = Engine.QueryInterface(target, IID_Position); | ||||
var cmpHolderUnitAI = Engine.QueryInterface(target, IID_UnitAI); | var cmpHolderUnitAI = Engine.QueryInterface(target, IID_UnitAI); | ||||
if (cmpHolderUnitAI && cmpHolderPosition) | if (cmpHolderUnitAI && cmpHolderPosition) | ||||
cmpHolderUnitAI.lastShorelinePosition = cmpHolderPosition.GetPosition(); | cmpHolderUnitAI.lastShorelinePosition = cmpHolderPosition.GetPosition(); | ||||
Engine.PostMessage(this.pickup, MT_PickupCanceled, { "entity": this.entity }); | Engine.PostMessage(this.pickup, MT_PickupCanceled, { "entity": this.entity }); | ||||
delete this.pickup; | delete this.pickup; | ||||
} | } | ||||
if (this.IsTurret()) | //if (this.IsTurret()) | ||||
Stan: dead code ? | |||||
{ | //{ | ||||
this.SetNextState("IDLE"); | this.FinishOrder(); | ||||
return true; | return true; | ||||
} | //} | ||||
return false; | //return false; | ||||
} | } | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
// Unable to reach the target, try again (or follow if it is a moving target) | // Unable to reach the target, try again (or follow if it is a moving target) | ||||
// except if the does not exits anymore or its orders have changed | // except if the does not exits anymore or its orders have changed | ||||
if (this.pickup) | if (this.pickup) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 2,806 Lines • ▼ Show 20 Lines | UnitAI.prototype.CanGarrison = function(target) | ||||
if (this.IsAnimal()) | if (this.IsAnimal()) | ||||
return false; | return false; | ||||
return true; | return true; | ||||
}; | }; | ||||
UnitAI.prototype.CanGather = function(target) | UnitAI.prototype.CanGather = function(target) | ||||
{ | { | ||||
if (this.IsTurret()) | |||||
return false; | |||||
// The target must be a valid resource supply, or the mirage of one. | // The target must be a valid resource supply, or the mirage of one. | ||||
var cmpResourceSupply = QueryMiragedInterface(target, IID_ResourceSupply); | let cmpResourceSupply = QueryMiragedInterface(target, IID_ResourceSupply); | ||||
if (!cmpResourceSupply) | if (!cmpResourceSupply) | ||||
return false; | return false; | ||||
// Formation controllers should always respond to commands | // Formation controllers should always respond to commands. | ||||
// (then the individual units can make up their own minds) | // (then the individual units can make up their own minds) | ||||
if (this.IsFormationController()) | if (this.IsFormationController()) | ||||
return true; | return true; | ||||
// Verify that we're able to respond to Gather commands | // Verify that we're able to respond to Gather commands. | ||||
var cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); | let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); | ||||
if (!cmpResourceGatherer) | if (!cmpResourceGatherer) | ||||
return false; | return false; | ||||
// Verify that we can gather from this target | // Verify that we can gather from this target. | ||||
if (!cmpResourceGatherer.GetTargetGatherRate(target)) | if (!cmpResourceGatherer.GetTargetGatherRate(target)) | ||||
return false; | return false; | ||||
// If we're a turret and can gather from our parent its okay. | |||||
if (this.IsTurret()) | |||||
{ | |||||
let cmpPosition = Engine.QueryInterface(this.entity, IID_Position); | |||||
return cmpPosition && cmpPosition.GetTurretParent() == target; | |||||
} | |||||
// No need to verify ownership as we should be able to gather from | // No need to verify ownership as we should be able to gather from | ||||
// a target regardless of ownership. | // a target regardless of ownership. | ||||
// No need to call "cmpResourceSupply.IsAvailable()" either because that | // No need to call "cmpResourceSupply.IsAvailable()" either because that | ||||
// would cause units to walk to full entities instead of choosing another one | // would cause units to walk to full entities instead of choosing another one | ||||
// nearby to gather from, which is undesirable. | // nearby to gather from, which is undesirable. | ||||
return true; | return true; | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 272 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
dead code ?