Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/UnitAI.js
Show First 20 Lines • Show All 3,075 Lines • ▼ Show 20 Lines | "REPAIR": { | ||||
"MovementUpdate": function(msg) { | "MovementUpdate": function(msg) { | ||||
if (msg.likelyFailure || msg.likelySuccess) | if (msg.likelyFailure || msg.likelySuccess) | ||||
this.SetNextState("REPAIRING"); | this.SetNextState("REPAIRING"); | ||||
}, | }, | ||||
}, | }, | ||||
"REPAIRING": { | "REPAIRING": { | ||||
"enter": function() { | "enter": function() { | ||||
let cmpBuilder = Engine.QueryInterface(this.entity, IID_Builder); | |||||
if (!cmpBuilder) | |||||
{ | |||||
this.FinishOrder(); | |||||
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) | ||||
if (this.order.data.force) | if (this.order.data.force) | ||||
this.order.data.autoharvest = true; | this.order.data.autoharvest = true; | ||||
this.order.data.force = false; | this.order.data.force = false; | ||||
// Needed to remove the entity from the builder list when leaving this state. | if (!this.CheckTargetRange(this.order.data.target, IID_Builder)) | ||||
this.repairTarget = this.order.data.target; | |||||
if (!this.CanRepair(this.repairTarget)) | |||||
{ | { | ||||
this.FinishOrder(); | this.ProcessMessage("OutOfRange"); | ||||
return true; | return true; | ||||
} | } | ||||
if (!this.CheckTargetRange(this.repairTarget, IID_Builder)) | let cmpHealth = Engine.QueryInterface(this.order.data.target, IID_Health); | ||||
{ | |||||
this.SetNextState("APPROACHING"); | |||||
return true; | |||||
} | |||||
let cmpHealth = Engine.QueryInterface(this.repairTarget, IID_Health); | |||||
if (cmpHealth && cmpHealth.GetHitpoints() >= cmpHealth.GetMaxHitpoints()) | if (cmpHealth && cmpHealth.GetHitpoints() >= cmpHealth.GetMaxHitpoints()) | ||||
{ | { | ||||
// The building was already finished/fully repaired before we arrived; | // The building was already finished/fully repaired before we arrived; | ||||
// let the ConstructionFinished handler handle this. | // let the ConstructionFinished handler handle this. | ||||
this.ConstructionFinished({ "entity": this.repairTarget, "newentity": this.repairTarget }); | this.ConstructionFinished({ "entity": this.order.data.target, "newentity": this.order.data.target }); | ||||
return true; | return true; | ||||
} | } | ||||
let cmpBuilderList = QueryBuilderListInterface(this.repairTarget); | if (!cmpBuilder.StartRepairing(this.order.data.target, IID_UnitAI)) | ||||
if (cmpBuilderList) | { | ||||
cmpBuilderList.AddBuilder(this.entity); | this.ProcessMessage("TargetInvalidated"); | ||||
return true; | |||||
this.FaceTowardsTarget(this.repairTarget); | } | ||||
this.SelectAnimation("build"); | this.FaceTowardsTarget(this.order.data.target); | ||||
this.StartTimer(1000, 1000); | |||||
return false; | return false; | ||||
}, | }, | ||||
"leave": function() { | "leave": function() { | ||||
let cmpBuilderList = QueryBuilderListInterface(this.repairTarget); | let cmpBuilder = Engine.QueryInterface(this.entity, IID_Builder); | ||||
if (cmpBuilderList) | if (cmpBuilder) | ||||
cmpBuilderList.RemoveBuilder(this.entity); | cmpBuilder.StopRepairing(); | ||||
delete this.repairTarget; | |||||
this.StopTimer(); | |||||
this.ResetAnimation(); | |||||
}, | }, | ||||
"Timer": function(msg) { | "OutOfRange": function(msg) { | ||||
if (!this.CanRepair(this.repairTarget)) | |||||
{ | |||||
this.FinishOrder(); | |||||
return; | |||||
} | |||||
this.FaceTowardsTarget(this.repairTarget); | |||||
let cmpBuilder = Engine.QueryInterface(this.entity, IID_Builder); | |||||
cmpBuilder.PerformBuilding(this.repairTarget); | |||||
// If the building is completed, the leave() function will be called | |||||
// by the ConstructionFinished message. | |||||
// In that case, the repairTarget is deleted, and we can just return. | |||||
if (!this.repairTarget) | |||||
return; | |||||
if (!this.CheckTargetRange(this.repairTarget, IID_Builder)) | |||||
this.SetNextState("APPROACHING"); | this.SetNextState("APPROACHING"); | ||||
}, | }, | ||||
"TargetInvalidated": function(msg) { | |||||
this.FinishOrder(); | |||||
}, | |||||
}, | }, | ||||
"ConstructionFinished": function(msg) { | "ConstructionFinished": function(msg) { | ||||
if (msg.data.entity != this.order.data.target) | if (msg.data.entity != this.order.data.target) | ||||
return; // ignore other buildings | return; // ignore other buildings | ||||
let oldData = this.order.data; | let oldData = this.order.data; | ||||
▲ Show 20 Lines • Show All 3,452 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator