Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/simulation/components/UnitAI.js
Show First 20 Lines • Show All 1,352 Lines • ▼ Show 20 Lines | "MEMBER": { | ||||
cmpPosition.MoveOutOfWorld(); | cmpPosition.MoveOutOfWorld(); | ||||
this.StartTimer(1000, 1000); | this.StartTimer(1000, 1000); | ||||
return false; | return false; | ||||
}, | }, | ||||
"Timer": function(msg) { | "Timer": function(msg) { | ||||
let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | ||||
if (cmpFormation && !cmpFormation.AreAllMembersWaiting()) | if (cmpFormation && !cmpFormation.AreAllMembersFinished()) | ||||
return; | return; | ||||
if (this.FinishOrder()) | if (this.FinishOrder()) | ||||
{ | { | ||||
if (this.IsWalkingAndFighting()) | if (this.IsWalkingAndFighting()) | ||||
this.FindWalkAndFightTargets(); | this.FindWalkAndFightTargets(); | ||||
return; | return; | ||||
} | } | ||||
return; | return; | ||||
}, | }, | ||||
"leave": function(msg) { | "leave": function(msg) { | ||||
this.StopTimer(); | this.StopTimer(); | ||||
// Reform entirely as members might be all over the place now. | // Reform entirely as members might be all over the place now. | ||||
let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | ||||
if (cmpFormation) | if (cmpFormation && (cmpFormation.AreAllMembersIdle() || this.orderQueue.length)) | ||||
cmpFormation.MoveMembersIntoFormation(true); | cmpFormation.MoveMembersIntoFormation(true); | ||||
// Update the held position so entities respond to orders. | // Update the held position so entities respond to orders. | ||||
let cmpPosition = Engine.QueryInterface(this.entity, IID_Position); | let cmpPosition = Engine.QueryInterface(this.entity, IID_Position); | ||||
if (cmpPosition && cmpPosition.IsInWorld()) | if (cmpPosition && cmpPosition.IsInWorld()) | ||||
{ | { | ||||
let pos = cmpPosition.GetPosition2D(); | let pos = cmpPosition.GetPosition2D(); | ||||
this.CallMemberFunction("SetHeldPosition", [pos.x, pos.y]); | this.CallMemberFunction("SetHeldPosition", [pos.x, pos.y]); | ||||
▲ Show 20 Lines • Show All 218 Lines • ▼ Show 20 Lines | "IDLE": { | ||||
cmpRangeManager.DisableActiveQuery(this.losHealRangeQuery); | cmpRangeManager.DisableActiveQuery(this.losHealRangeQuery); | ||||
if (this.losAttackRangeQuery) | if (this.losAttackRangeQuery) | ||||
cmpRangeManager.DisableActiveQuery(this.losAttackRangeQuery); | cmpRangeManager.DisableActiveQuery(this.losAttackRangeQuery); | ||||
this.StopTimer(); | this.StopTimer(); | ||||
if (this.isIdle) | if (this.isIdle) | ||||
{ | { | ||||
if (this.IsFormationMember()) | |||||
Engine.QueryInterface(this.formationController, IID_Formation).UnsetIdleEntity(this.entity); | |||||
this.isIdle = false; | this.isIdle = false; | ||||
Engine.PostMessage(this.entity, MT_UnitIdleChanged, { "idle": this.isIdle }); | Engine.PostMessage(this.entity, MT_UnitIdleChanged, { "idle": this.isIdle }); | ||||
} | } | ||||
}, | }, | ||||
"Attacked": function(msg) { | "Attacked": function(msg) { | ||||
if (this.isIdle && (this.GetStance().targetAttackersAlways || !this.order || !this.order.data || !this.order.data.force)) | if (this.isIdle && (this.GetStance().targetAttackersAlways || !this.order || !this.order.data || !this.order.data.force)) | ||||
this.RespondToTargetedEntities([msg.data.attacker]); | this.RespondToTargetedEntities([msg.data.attacker]); | ||||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | "IDLE": { | ||||
this.WalkToHeldPosition()) | this.WalkToHeldPosition()) | ||||
return; | return; | ||||
if (this.IsFormationMember()) | if (this.IsFormationMember()) | ||||
{ | { | ||||
let cmpFormationAI = Engine.QueryInterface(this.formationController, IID_UnitAI); | let cmpFormationAI = Engine.QueryInterface(this.formationController, IID_UnitAI); | ||||
if (!cmpFormationAI || !cmpFormationAI.IsIdle()) | if (!cmpFormationAI || !cmpFormationAI.IsIdle()) | ||||
return; | return; | ||||
Engine.QueryInterface(this.formationController, IID_Formation).SetIdleEntity(this.entity); | |||||
} | } | ||||
this.isIdle = true; | this.isIdle = true; | ||||
Engine.PostMessage(this.entity, MT_UnitIdleChanged, { "idle": this.isIdle }); | Engine.PostMessage(this.entity, MT_UnitIdleChanged, { "idle": this.isIdle }); | ||||
} | } | ||||
// Go linger first to prevent all roaming entities | // Go linger first to prevent all roaming entities | ||||
// to move all at the same time on map init. | // to move all at the same time on map init. | ||||
▲ Show 20 Lines • Show All 2,202 Lines • ▼ Show 20 Lines | UnitAI.prototype.FinishOrder = function() | ||||
// Check if there are queued formation orders | // Check if there are queued formation orders | ||||
if (this.IsFormationMember()) | if (this.IsFormationMember()) | ||||
{ | { | ||||
this.SetNextState("FORMATIONMEMBER.IDLE"); | this.SetNextState("FORMATIONMEMBER.IDLE"); | ||||
let cmpUnitAI = Engine.QueryInterface(this.formationController, IID_UnitAI); | let cmpUnitAI = Engine.QueryInterface(this.formationController, IID_UnitAI); | ||||
if (cmpUnitAI) | if (cmpUnitAI) | ||||
{ | { | ||||
// Inform the formation controller that we finished this task | // Inform the formation controller that we finished this task | ||||
let cmpFormation = Engine.QueryInterface(this.formationController, IID_Formation); | Engine.QueryInterface(this.formationController, IID_Formation). | ||||
cmpFormation.SetWaitingOnController(this.entity); | SetFinishedEntity(this.entity); | ||||
// We don't want to carry out the default order | // We don't want to carry out the default order | ||||
// if there are still queued formation orders left | // if there are still queued formation orders left | ||||
if (cmpUnitAI.GetOrders().length > 1) | if (cmpUnitAI.GetOrders().length > 1) | ||||
return true; | return true; | ||||
} | } | ||||
} | } | ||||
return false; | return false; | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 2,537 Lines • ▼ Show 20 Lines | for (let pref of preferences) | ||||
return true; | return true; | ||||
} | } | ||||
return this.RespondToTargetedEntities(entsWithoutPref); | return this.RespondToTargetedEntities(entsWithoutPref); | ||||
}; | }; | ||||
/** | /** | ||||
* Call UnitAI.funcname(args) on all formation members. | * Call UnitAI.funcname(args) on all formation members. | ||||
* @param resetWaitingEntities - If true, call ResetWaitingEntities first. | * @param resetFinishedEntities - If true, call ResetFinishedEntities first. | ||||
* If the controller wants to wait on its members to finish their order, | * If the controller wants to wait on its members to finish their order, | ||||
* this needs to be reset before sending new orders (in case they instafail) | * this needs to be reset before sending new orders (in case they instafail) | ||||
* so it makes sense to do it here. | * so it makes sense to do it here. | ||||
* Only set this to false if you're sure it's safe. | * Only set this to false if you're sure it's safe. | ||||
*/ | */ | ||||
UnitAI.prototype.CallMemberFunction = function(funcname, args, resetWaitingEntities = true) | UnitAI.prototype.CallMemberFunction = function(funcname, args, resetFinishedEntities = true) | ||||
{ | { | ||||
var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); | ||||
if (!cmpFormation) | if (!cmpFormation) | ||||
return; | return; | ||||
if (resetWaitingEntities) | if (resetFinishedEntities) | ||||
cmpFormation.ResetWaitingEntities(); | cmpFormation.ResetFinishedEntities(); | ||||
cmpFormation.GetMembers().forEach(ent => { | cmpFormation.GetMembers().forEach(ent => { | ||||
let cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); | let cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); | ||||
cmpUnitAI[funcname].apply(cmpUnitAI, args); | cmpUnitAI[funcname].apply(cmpUnitAI, args); | ||||
}); | }); | ||||
}; | }; | ||||
/** | /** | ||||
Show All 35 Lines |
Wildfire Games · Phabricator