Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/simulation/components/Formation.js
Show First 20 Lines • Show All 73 Lines • ▼ Show 20 Lines | |||||
var g_ColumnDistanceThreshold = 128; | var g_ColumnDistanceThreshold = 128; | ||||
Formation.prototype.variablesToSerialize = [ | Formation.prototype.variablesToSerialize = [ | ||||
"lastOrderVariant", | "lastOrderVariant", | ||||
"members", | "members", | ||||
"memberPositions", | "memberPositions", | ||||
"maxRowsUsed", | "maxRowsUsed", | ||||
"maxColumnsUsed", | "maxColumnsUsed", | ||||
"waitingOnController", | "finishedEntities", | ||||
"idleEntities", | |||||
"columnar", | "columnar", | ||||
"rearrange", | "rearrange", | ||||
"formationMembersWithAura", | "formationMembersWithAura", | ||||
"width", | "width", | ||||
"depth", | "depth", | ||||
"twinFormations", | "twinFormations", | ||||
"formationSeparation", | "formationSeparation", | ||||
"offsets" | "offsets" | ||||
Show All 39 Lines | Formation.prototype.Init = function(deserialized = false) | ||||
} | } | ||||
this.lastOrderVariant = undefined; | this.lastOrderVariant = undefined; | ||||
// Entity IDs currently belonging to this formation. | // Entity IDs currently belonging to this formation. | ||||
this.members = []; | this.members = []; | ||||
this.memberPositions = {}; | this.memberPositions = {}; | ||||
this.maxRowsUsed = 0; | this.maxRowsUsed = 0; | ||||
this.maxColumnsUsed = []; | this.maxColumnsUsed = []; | ||||
// Entities that are waiting on the controller. | // Entities that have finished the original task. | ||||
this.waitingOnController = []; | this.finishedEntities = new Set(); | ||||
this.idleEntities = new Set(); | |||||
// Whether we're travelling in column (vs box) formation. | // Whether we're travelling in column (vs box) formation. | ||||
this.columnar = false; | this.columnar = false; | ||||
// Whether we should rearrange all formation members. | // Whether we should rearrange all formation members. | ||||
this.rearrange = true; | this.rearrange = true; | ||||
// Members with a formation aura. | // Members with a formation aura. | ||||
this.formationMembersWithAura = []; | this.formationMembersWithAura = []; | ||||
this.width = 0; | this.width = 0; | ||||
this.depth = 0; | this.depth = 0; | ||||
▲ Show 20 Lines • Show All 131 Lines • ▼ Show 20 Lines | for (let i = 0; i < this.animationvariants.length; ++i) | ||||
if (column > maxColumn) | if (column > maxColumn) | ||||
continue; | continue; | ||||
return this.animationvariants[i].name; | return this.animationvariants[i].name; | ||||
} | } | ||||
return undefined; | return undefined; | ||||
}; | }; | ||||
Formation.prototype.SetWaitingOnController = function(ent) | Formation.prototype.SetFinishedEntity = function(ent) | ||||
{ | { | ||||
// Rotate the entity to the right angle. | // Rotate the entity to the correct angle. | ||||
let cmpPosition = Engine.QueryInterface(this.entity, IID_Position); | const cmpPosition = Engine.QueryInterface(this.entity, IID_Position); | ||||
let cmpEntPosition = Engine.QueryInterface(ent, IID_Position); | const cmpEntPosition = Engine.QueryInterface(ent, IID_Position); | ||||
if (cmpEntPosition && cmpEntPosition.IsInWorld() && cmpPosition && cmpPosition.IsInWorld()) | if (cmpEntPosition && cmpEntPosition.IsInWorld() && cmpPosition && cmpPosition.IsInWorld()) | ||||
cmpEntPosition.TurnTo(cmpPosition.GetRotation().y); | cmpEntPosition.TurnTo(cmpPosition.GetRotation().y); | ||||
if (this.waitingOnController.indexOf(ent) !== -1) | this.finishedEntities.add(ent); | ||||
return; | }; | ||||
this.waitingOnController.push(ent); | |||||
Formation.prototype.UnsetFinishedEntity = function(ent) | |||||
{ | |||||
this.finishedEntities.delete(ent); | |||||
}; | |||||
Formation.prototype.ResetFinishedEntities = function() | |||||
{ | |||||
this.finishedEntities.clear(); | |||||
}; | |||||
Formation.prototype.AreAllMembersFinished = function() | |||||
{ | |||||
return this.finishedEntities.size === this.members.length; | |||||
}; | |||||
Formation.prototype.SetIdleEntity = function(ent) | |||||
{ | |||||
this.idleEntities.add(ent); | |||||
}; | }; | ||||
Formation.prototype.UnsetWaitingOnController = function(ent) | Formation.prototype.UnsetIdleEntity = function(ent) | ||||
{ | { | ||||
let ind = this.waitingOnController.indexOf(ent); | this.idleEntities.delete(ent); | ||||
if (ind !== -1) | |||||
this.waitingOnController.splice(ind, 1); | |||||
}; | }; | ||||
Formation.prototype.ResetWaitingEntities = function() | Formation.prototype.ResetIdleEntities = function() | ||||
{ | { | ||||
this.waitingOnController = []; | this.idleEntities.clear(); | ||||
}; | }; | ||||
Formation.prototype.AreAllMembersWaiting = function() | Formation.prototype.AreAllMembersIdle = function() | ||||
{ | { | ||||
return this.waitingOnController.length === this.members.length; | return this.idleEntities.size === this.members.length; | ||||
}; | }; | ||||
/** | /** | ||||
* Set whether we are allowed to rearrange formation members. | * Set whether we are allowed to rearrange formation members. | ||||
*/ | */ | ||||
Formation.prototype.SetRearrange = function(rearrange) | Formation.prototype.SetRearrange = function(rearrange) | ||||
{ | { | ||||
this.rearrange = rearrange; | this.rearrange = rearrange; | ||||
Show All 34 Lines | |||||
* The entities must already be members of this formation. | * The entities must already be members of this formation. | ||||
* @param {boolean} rename - Whether the removal was part of an entity rename | * @param {boolean} rename - Whether the removal was part of an entity rename | ||||
(prevents disbanding of the formation when under the member limit). | (prevents disbanding of the formation when under the member limit). | ||||
*/ | */ | ||||
Formation.prototype.RemoveMembers = function(ents, renamed = false) | Formation.prototype.RemoveMembers = function(ents, renamed = false) | ||||
{ | { | ||||
this.offsets = undefined; | this.offsets = undefined; | ||||
this.members = this.members.filter(ent => ents.indexOf(ent) === -1); | this.members = this.members.filter(ent => ents.indexOf(ent) === -1); | ||||
this.waitingOnController = this.waitingOnController.filter(ent => ents.indexOf(ent) === -1); | |||||
for (let ent of ents) | for (let ent of ents) | ||||
{ | { | ||||
this.finishedEntities.delete(ent); | |||||
let cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); | let cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); | ||||
cmpUnitAI.UpdateWorkOrders(); | cmpUnitAI.UpdateWorkOrders(); | ||||
cmpUnitAI.SetFormationController(INVALID_ENTITY); | cmpUnitAI.SetFormationController(INVALID_ENTITY); | ||||
} | } | ||||
for (let ent of this.formationMembersWithAura) | for (let ent of this.formationMembersWithAura) | ||||
{ | { | ||||
let cmpAuras = Engine.QueryInterface(ent, IID_Auras); | let cmpAuras = Engine.QueryInterface(ent, IID_Auras); | ||||
▲ Show 20 Lines • Show All 71 Lines • ▼ Show 20 Lines | Formation.prototype.Disband = function() | ||||
for (let ent of this.formationMembersWithAura) | for (let ent of this.formationMembersWithAura) | ||||
{ | { | ||||
let cmpAuras = Engine.QueryInterface(ent, IID_Auras); | let cmpAuras = Engine.QueryInterface(ent, IID_Auras); | ||||
cmpAuras.RemoveFormationAura(this.members); | cmpAuras.RemoveFormationAura(this.members); | ||||
} | } | ||||
this.members = []; | this.members = []; | ||||
this.waitingOnController = []; | this.finishedEntities.clear(); | ||||
this.formationMembersWithAura = []; | this.formationMembersWithAura = []; | ||||
this.offsets = undefined; | this.offsets = undefined; | ||||
let cmpUnitAI = Engine.QueryInterface(this.entity, IID_UnitAI); | let cmpUnitAI = Engine.QueryInterface(this.entity, IID_UnitAI); | ||||
// Hack: switch to a clean state to stop timers. | // Hack: switch to a clean state to stop timers. | ||||
cmpUnitAI.UnitFsm.SwitchToNextState(cmpUnitAI, ""); | cmpUnitAI.UnitFsm.SwitchToNextState(cmpUnitAI, ""); | ||||
Engine.DestroyEntity(this.entity); | Engine.DestroyEntity(this.entity); | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | Formation.prototype.MoveMembersIntoFormation = function(moveCenter, force, variant) | ||||
} | } | ||||
let xMax = 0; | let xMax = 0; | ||||
let yMax = 0; | let yMax = 0; | ||||
let xMin = 0; | let xMin = 0; | ||||
let yMin = 0; | let yMin = 0; | ||||
if (force) | if (force) | ||||
// Reset waitingOnController as FormationWalk is called. | // Reset finishedEntities as FormationWalk is called. | ||||
this.ResetWaitingEntities(); | this.ResetFinishedEntities(); | ||||
for (let i = 0; i < this.offsets.length; ++i) | for (let i = 0; i < this.offsets.length; ++i) | ||||
{ | { | ||||
let offset = this.offsets[i]; | let offset = this.offsets[i]; | ||||
let cmpUnitAI = Engine.QueryInterface(offset.ent, IID_UnitAI); | let cmpUnitAI = Engine.QueryInterface(offset.ent, IID_UnitAI); | ||||
if (!cmpUnitAI) | if (!cmpUnitAI) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 422 Lines • ▼ Show 20 Lines | if (this.members.indexOf(msg.entity) != -1) | ||||
this.RemoveMembers([msg.entity]); | this.RemoveMembers([msg.entity]); | ||||
}; | }; | ||||
Formation.prototype.OnGlobalEntityRenamed = function(msg) | Formation.prototype.OnGlobalEntityRenamed = function(msg) | ||||
{ | { | ||||
if (this.members.indexOf(msg.entity) === -1) | if (this.members.indexOf(msg.entity) === -1) | ||||
return; | return; | ||||
let waitingIndex = this.waitingOnController.indexOf(msg.entity); | if (this.finishedEntities.delete(msg.entity)) | ||||
if (waitingIndex !== -1) | this.finishedEntities.add(msg.newentity); | ||||
this.waitingOnController.splice(waitingIndex, 1, msg.newentity); | |||||
// Save rearranging to temporarily set it to false. | // Save rearranging to temporarily set it to false. | ||||
let temp = this.rearrange; | let temp = this.rearrange; | ||||
this.rearrange = false; | this.rearrange = false; | ||||
// First remove the old member to be able to reuse its position. | // First remove the old member to be able to reuse its position. | ||||
this.RemoveMembers([msg.entity], true); | this.RemoveMembers([msg.entity], true); | ||||
this.AddMembers([msg.newentity]); | this.AddMembers([msg.newentity]); | ||||
▲ Show 20 Lines • Show All 62 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator