Changeset View
Standalone View
binaries/data/mods/public/simulation/components/Formation.js
Show First 20 Lines • Show All 302 Lines • ▼ Show 20 Lines | |||||
Formation.prototype.UnsetInPosition = function(ent) | Formation.prototype.UnsetInPosition = function(ent) | ||||
{ | { | ||||
var ind = this.inPosition.indexOf(ent); | var ind = this.inPosition.indexOf(ent); | ||||
if (ind != -1) | if (ind != -1) | ||||
this.inPosition.splice(ind, 1); | this.inPosition.splice(ind, 1); | ||||
}; | }; | ||||
/** | /** | ||||
* Set whether we should rearrange formation members if | * Set whether we are allowed to rearrange formation members. | ||||
* units are removed from the formation. | |||||
*/ | */ | ||||
Formation.prototype.SetRearrange = function(rearrange) | Formation.prototype.SetRearrange = function(rearrange) | ||||
{ | { | ||||
this.rearrange = rearrange; | this.rearrange = rearrange; | ||||
}; | }; | ||||
/** | /** | ||||
* Initialise the members of this formation. | * Initialise the members of this formation. | ||||
▲ Show 20 Lines • Show All 585 Lines • ▼ Show 20 Lines | Formation.prototype.ComputeMotionParameters = function() | ||||
minSpeed *= this.GetSpeedMultiplier(); | minSpeed *= this.GetSpeedMultiplier(); | ||||
var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | ||||
cmpUnitMotion.SetSpeedMultiplier(minSpeed / cmpUnitMotion.GetWalkSpeed()); | cmpUnitMotion.SetSpeedMultiplier(minSpeed / cmpUnitMotion.GetWalkSpeed()); | ||||
}; | }; | ||||
Formation.prototype.ShapeUpdate = function() | Formation.prototype.ShapeUpdate = function() | ||||
{ | { | ||||
if (!this.rearrange) | |||||
return; | |||||
// Check the distance to twin formations, and merge if when | // Check the distance to twin formations, and merge if when | ||||
// the formations could collide | // the formations could collide | ||||
for (var i = this.twinFormations.length - 1; i >= 0; --i) | for (let i = this.twinFormations.length - 1; i >= 0; --i) | ||||
{ | { | ||||
// only do the check on one side | // only do the check on one side | ||||
if (this.twinFormations[i] <= this.entity) | if (this.twinFormations[i] <= this.entity) | ||||
continue; | continue; | ||||
let cmpPosition = Engine.QueryInterface(this.entity, IID_Position); | |||||
var cmpPosition = Engine.QueryInterface(this.entity, IID_Position); | let cmpOtherPosition = Engine.QueryInterface(this.twinFormations[i], IID_Position); | ||||
var cmpOtherPosition = Engine.QueryInterface(this.twinFormations[i], IID_Position); | let cmpOtherFormation = Engine.QueryInterface(this.twinFormations[i], IID_Formation); | ||||
var cmpOtherFormation = Engine.QueryInterface(this.twinFormations[i], IID_Formation); | if (!cmpPosition || !cmpOtherPosition || !cmpOtherFormation || | ||||
if (!cmpPosition || !cmpOtherPosition || !cmpOtherFormation) | !cmpPosition.IsInWorld() || !cmpOtherPosition.IsInWorld()) | ||||
Stan: I wonder whether it's better to early continue for each, or query all of them then early… | |||||
continue; | continue; | ||||
var thisPosition = cmpPosition.GetPosition2D(); | let thisPosition = cmpPosition.GetPosition2D(); | ||||
var otherPosition = cmpOtherPosition.GetPosition2D(); | let otherPosition = cmpOtherPosition.GetPosition2D(); | ||||
Not Done Inline Actionscareful, Silier: careful,
this can be out of world if got to member state before this one | |||||
Done Inline ActionsIt can't because this.rearrange is set to false. wraitii: It can't because this.rearrange is set to false. | |||||
Not Done Inline Actionshow? But this formation is not yet in member state. Are you saying that while formation is moving it has always rearrange to false? If so, that is in contradiction of usage of this function no? Silier: how?
suppose other Formation reached destination and went to member state so out of world. | |||||
Not Done Inline ActionsCould this be the reason of the error? Freagarach: Could this be the reason of the error? | |||||
Done Inline ActionsThe trick is that FORMATION.WALKING and others are not in MEMBER state. Formations only get moved to MEMBER state when they give out individual orders to sub-units, thus breaking the formation (thus making this function useless). I'm not saying there's not an edge case or an oddity in there, but I can't reproduce something breaking and this ought to be safe wraitii: The trick is that FORMATION.WALKING and others are not in MEMBER state. Formations only get… | |||||
var dx = thisPosition.x - otherPosition.x; | |||||
var dy = thisPosition.y - otherPosition.y; | let dx = thisPosition.x - otherPosition.x; | ||||
var dist = Math.sqrt(dx * dx + dy * dy); | let dy = thisPosition.y - otherPosition.y; | ||||
let dist = Math.sqrt(dx * dx + dy * dy); | |||||
StanUnsubmitted Not Done Inline ActionsMaybe we can use the square for performance Stan: Maybe we can use the square for performance | |||||
StanUnsubmitted Not Done Inline ActionsCan we? Stan: Can we? | |||||
wraitiiAuthorUnsubmitted Done Inline ActionsI didn't particularly bother changing this, but yes we can it seems. wraitii: I didn't particularly bother changing this, but yes we can it seems. | |||||
var thisSize = this.GetSize(); | |||||
var otherSize = cmpOtherFormation.GetSize(); | let thisSize = this.GetSize(); | ||||
var minDist = Math.max(thisSize.width / 2, thisSize.depth / 2) + | let otherSize = cmpOtherFormation.GetSize(); | ||||
let minDist = Math.max(thisSize.width / 2, thisSize.depth / 2) + | |||||
Math.max(otherSize.width / 2, otherSize.depth / 2) + | Math.max(otherSize.width / 2, otherSize.depth / 2) + | ||||
this.formationSeparation; | this.formationSeparation; | ||||
if (minDist < dist) | if (minDist < dist) | ||||
continue; | continue; | ||||
// merge the members from the twin formation into this one | // merge the members from the twin formation into this one | ||||
// twin formations should always have exactly the same orders | // twin formations should always have exactly the same orders | ||||
let otherMembers = cmpOtherFormation.members; | let otherMembers = cmpOtherFormation.members; | ||||
cmpOtherFormation.RemoveMembers(otherMembers); | cmpOtherFormation.RemoveMembers(otherMembers); | ||||
this.AddMembers(otherMembers); | this.AddMembers(otherMembers); | ||||
Engine.DestroyEntity(this.twinFormations[i]); | Engine.DestroyEntity(this.twinFormations[i]); | ||||
this.twinFormations.splice(i,1); | this.twinFormations.splice(i, 1); | ||||
} | } | ||||
// Switch between column and box if necessary | // Switch between column and box if necessary | ||||
var cmpUnitAI = Engine.QueryInterface(this.entity, IID_UnitAI); | let cmpUnitAI = Engine.QueryInterface(this.entity, IID_UnitAI); | ||||
var walkingDistance = cmpUnitAI.ComputeWalkingDistance(); | let walkingDistance = cmpUnitAI.ComputeWalkingDistance(); | ||||
var columnar = walkingDistance > g_ColumnDistanceThreshold; | let columnar = walkingDistance > g_ColumnDistanceThreshold; | ||||
if (columnar != this.columnar) | if (columnar != this.columnar) | ||||
{ | { | ||||
this.offsets = undefined; | this.offsets = undefined; | ||||
this.columnar = columnar; | this.columnar = columnar; | ||||
this.MoveMembersIntoFormation(false, true, this.lastOrderVariant); | this.MoveMembersIntoFormation(false, true, this.lastOrderVariant); | ||||
// (disable moveCenter so we can't get stuck in a loop of switching | // (disable moveCenter so we can't get stuck in a loop of switching | ||||
// shape causing center to change causing shape to switch back) | // shape causing center to change causing shape to switch back) | ||||
} | } | ||||
▲ Show 20 Lines • Show All 88 Lines • Show Last 20 Lines |
I wonder whether it's better to early continue for each, or query all of them then early continue. Likely out of the scope of this patch