Index: ps/trunk/binaries/data/mods/public/globalscripts/vector.js =================================================================== --- ps/trunk/binaries/data/mods/public/globalscripts/vector.js (revision 20705) +++ ps/trunk/binaries/data/mods/public/globalscripts/vector.js (revision 20706) @@ -1,382 +1,380 @@ ///////////////////////////////////////////////////////////////////// // Vector2D // // Class for representing and manipulating 2D vectors // ///////////////////////////////////////////////////////////////////// // TODO: Type errors if v not instanceof Vector classes // TODO: Possibly implement in C++ -function Vector2D(x, y) +function Vector2D(x = 0, y = 0) { - if (arguments.length == 2) - this.set(x, y); - else - this.set(0, 0); + this.set(x, y); } Vector2D.prototype.clone = function() { return new Vector2D(this.x, this.y); }; // Mutating 2D functions // // These functions modify the current object, // and always return this object to allow chaining Vector2D.prototype.set = function(x, y) { this.x = x; this.y = y; return this; }; Vector2D.prototype.add = function(v) { this.x += v.x; this.y += v.y; return this; }; Vector2D.prototype.sub = function(v) { this.x -= v.x; this.y -= v.y; return this; }; Vector2D.prototype.mult = function(f) { this.x *= f; this.y *= f; return this; }; Vector2D.prototype.div = function(f) { this.x /= f; this.y /= f; return this; }; Vector2D.prototype.normalize = function() { let magnitude = this.length(); if (!magnitude) return this; return this.div(magnitude); }; /** * Rotate a radians anti-clockwise */ Vector2D.prototype.rotate = function(angle) { let sin = Math.sin(angle); let cos = Math.cos(angle); return this.set( this.x * cos + this.y * sin, this.y * cos - this.x * sin); }; /** * Rotate radians anti-clockwise around the specified rotation center. */ Vector2D.prototype.rotateAround = function(angle, center) { return this.sub(center).rotate(angle).add(center); }; // Numeric 2D info functions (non-mutating) // // These methods serve to get numeric info on the vector, they don't modify the vector /** - * Return the vector that forms a right angle with this one. + * Returns a vector that forms a right angle with this one. */ Vector2D.prototype.perpendicular = function() { return new Vector2D(-this.y, this.x); }; /** * Computes the scalar product of the two vectors. * Geometrically, this is the product of the length of the two vectors and the cosine of the angle between them. * If the vectors are orthogonal, the product is zero. */ Vector2D.prototype.dot = function(v) { return this.x * v.x + this.y * v.y; }; /** * Computes the non-zero coordinate of the cross product of the two vectors. - * Geometrically, the cross of the vectors is the 3D vector perpendicular to the two 2D vectors. - * This returned length of that vector equals the area of the parallelogram that the vectors span. + * Geometrically, the cross of the vectors is a 3D vector perpendicular to the two 2D vectors. + * The returned number corresponds to the area of the parallelogram with the vectors for sides. */ Vector2D.prototype.cross = function(v) { return this.x * v.y - this.y * v.x; }; Vector2D.prototype.lengthSquared = function() { return this.dot(this); }; Vector2D.prototype.length = function() { return Math.sqrt(this.lengthSquared()); }; /** - * Compare this length to the length of v, + * Compare this length to the length of v. * @return 0 if the lengths are equal * @return 1 if this is longer than v * @return -1 if this is shorter than v * @return NaN if the vectors aren't comparable */ Vector2D.prototype.compareLength = function(v) { return Math.sign(this.lengthSquared() - v.lengthSquared()); }; Vector2D.prototype.distanceToSquared = function(v) { return Math.euclidDistance2DSquared(this.x, this.y, v.x, v.y); }; Vector2D.prototype.distanceTo = function(v) { return Math.euclidDistance2D(this.x, this.y, v.x, v.y); }; /** * Returns the angle going from this position to v. * Angles are between -PI and PI. E.g., north is 0, east is PI/2. */ Vector2D.prototype.angleTo = function(v) { return Math.atan2(v.x - this.x, v.y - this.y); }; // Static 2D functions // // Static functions that return a new vector object. // Note that object creation is slow in JS, so use them only when necessary Vector2D.from3D = function(v) { return new Vector2D(v.x, v.z); }; Vector2D.add = function(v1, v2) { return new Vector2D(v1.x + v2.x, v1.y + v2.y); }; Vector2D.sub = function(v1, v2) { return new Vector2D(v1.x - v2.x, v1.y - v2.y); }; Vector2D.mult = function(v, f) { return new Vector2D(v.x * f, v.y * f); }; Vector2D.div = function(v, f) { return new Vector2D(v.x / f, v.y / f); }; -Vector2D.avg = function(vectorList) +Vector2D.average = function(vectorList) { return Vector2D.sum(vectorList).div(vectorList.length); }; Vector2D.sum = function(vectorList) { - var sum = new Vector2D(); - vectorList.forEach(v => sum.add(v)); + // Do not use for...of nor array functions for performance + let sum = new Vector2D(); + + for (let i = 0; i < vectorList.length; ++i) + sum.add(vectorList[i]); + return sum; }; ///////////////////////////////////////////////////////////////////// // Vector3D // // Class for representing and manipulating 3D vectors // ///////////////////////////////////////////////////////////////////// -function Vector3D(x, y, z) +function Vector3D(x = 0, y = 0, z = 0) { - if (arguments.length == 3) - this.set(x, y, z); - else - this.set(0, 0, 0); + this.set(x, y, z); } Vector3D.prototype.clone = function() { return new Vector3D(this.x, this.y, this.z); }; // Mutating 3D functions // // These functions modify the current object, // and always return this object to allow chaining Vector3D.prototype.set = function(x, y, z) { this.x = x; this.y = y; this.z = z; return this; }; Vector3D.prototype.add = function(v) { this.x += v.x; this.y += v.y; this.z += v.z; return this; }; Vector3D.prototype.sub = function(v) { this.x -= v.x; this.y -= v.y; this.z -= v.z; return this; }; Vector3D.prototype.mult = function(f) { this.x *= f; this.y *= f; this.z *= f; return this; }; Vector3D.prototype.div = function(f) { this.x /= f; this.y /= f; this.z /= f; return this; }; Vector3D.prototype.normalize = function() { let magnitude = this.length(); if (!magnitude) return this; return this.div(magnitude); }; // Numeric 3D info functions (non-mutating) // // These methods serve to get numeric info on the vector, they don't modify the vector Vector3D.prototype.dot = function(v) { return this.x * v.x + this.y * v.y + this.z * v.z; }; /** * Returns a vector perpendicular to the two given vectors. * The length of the returned vector corresponds to the area of the parallelogram with the vectors for sides. */ Vector3D.prototype.cross = function(v) { return new Vector3D( this.y * v.z - this.z * v.y, this.z * v.x - this.x * v.z, this.x * v.y - this.y * v.x); }; Vector3D.prototype.lengthSquared = function() { return this.dot(this); }; Vector3D.prototype.length = function() { return Math.sqrt(this.lengthSquared()); }; /** * Compare this length to the length of v, * @return 0 if the lengths are equal * @return 1 if this is longer than v * @return -1 if this is shorter than v * @return NaN if the vectors aren't comparable */ Vector3D.prototype.compareLength = function(v) { return Math.sign(this.lengthSquared() - v.lengthSquared()); }; Vector3D.prototype.distanceToSquared = function(v) { return Math.euclidDistance3DSquared(this.x, this.y, this.z, v.x, v.y, v.z); }; Vector3D.prototype.distanceTo = function(v) { return Math.euclidDistance3D(this.x, this.y, this.z, v.x, v.y, v.z); }; Vector3D.prototype.horizDistanceToSquared = function(v) { return Math.euclidDistance2DSquared(this.x, this.z, v.x, v.z); }; Vector3D.prototype.horizDistanceTo = function(v) { return Math.sqrt(this.horizDistanceToSquared(v)); }; /** * Returns the angle going from this position to v. */ Vector3D.prototype.horizAngleTo = function(v) { return Math.atan2(v.x - this.x, v.z - this.z); }; // Static 3D functions // // Static functions that return a new vector object. // Note that object creation is slow in JS, so use them only when really necessary Vector3D.add = function(v1, v2) { return new Vector3D(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z); }; Vector3D.sub = function(v1, v2) { return new Vector3D(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z); }; Vector3D.mult = function(v, f) { return new Vector3D(v.x * f, v.y * f, v.z * f); }; Vector3D.div = function(v, f) { return new Vector3D(v.x / f, v.y / f, v.z / f); }; // make the prototypes easily accessible to C++ const Vector2Dprototype = Vector2D.prototype; const Vector3Dprototype = Vector3D.prototype; Index: ps/trunk/binaries/data/mods/public/simulation/components/Formation.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/Formation.js (revision 20705) +++ ps/trunk/binaries/data/mods/public/simulation/components/Formation.js (revision 20706) @@ -1,1010 +1,1010 @@ function Formation() {} Formation.prototype.Schema = "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + ""; var g_ColumnDistanceThreshold = 128; // distance at which we'll switch between column/box formations Formation.prototype.Init = function() { this.formationShape = this.template.FormationShape; this.sortingClasses = this.template.SortingClasses.split(/\s+/g); this.sortingOrder = this.template.SortingOrder; this.shiftRows = this.template.ShiftRows == "true"; this.separationMultiplier = { "width": +this.template.UnitSeparationWidthMultiplier, "depth": +this.template.UnitSeparationDepthMultiplier }; this.sloppyness = +this.template.Sloppyness; this.widthDepthRatio = +this.template.WidthDepthRatio; this.minColumns = +(this.template.MinColumns || 0); this.maxColumns = +(this.template.MaxColumns || 0); this.maxRows = +(this.template.MaxRows || 0); this.centerGap = +(this.template.CenterGap || 0); var animations = this.template.Animations; this.animations = {}; for (var animationName in animations) { var differentAnimations = animations[animationName].split(/\s*;\s*/); this.animations[animationName] = []; // loop over the different rectangulars that will map to different animations for (var rectAnimation of differentAnimations) { var rect, replacementAnimationName; [rect, replacementAnimationName] = rectAnimation.split(/\s*:\s*/); var rows, columns; [rows, columns] = rect.split(/\s*,\s*/); var minRow, maxRow, minColumn, maxColumn; [minRow, maxRow] = rows.split(/\s*\.\.\s*/); [minColumn, maxColumn] = columns.split(/\s*\.\.\s*/); this.animations[animationName].push({ "minRow": +minRow, "maxRow": +maxRow, "minColumn": +minColumn, "maxColumn": +maxColumn, "animation": replacementAnimationName }); } } this.members = []; // entity IDs currently belonging to this formation this.memberPositions = {}; this.maxRowsUsed = 0; this.maxColumnsUsed = []; this.inPosition = []; // entities that have reached their final position this.columnar = false; // whether we're travelling in column (vs box) formation this.rearrange = true; // whether we should rearrange all formation members this.formationMembersWithAura = []; // Members with a formation aura this.width = 0; this.depth = 0; this.oldOrientation = {"sin": 0, "cos": 0}; this.twinFormations = []; // distance from which two twin formations will merge into one. this.formationSeparation = 0; Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer) .SetInterval(this.entity, IID_Formation, "ShapeUpdate", 1000, 1000, null); }; /** * Set the value from which two twin formations will become one. */ Formation.prototype.SetFormationSeparation = function(value) { this.formationSeparation = value; }; Formation.prototype.GetSize = function() { return {"width": this.width, "depth": this.depth}; }; Formation.prototype.GetSpeedMultiplier = function() { return +this.template.SpeedMultiplier; }; Formation.prototype.GetMemberCount = function() { return this.members.length; }; Formation.prototype.GetMembers = function() { return this.members; }; Formation.prototype.GetClosestMember = function(ent, filter) { var cmpEntPosition = Engine.QueryInterface(ent, IID_Position); if (!cmpEntPosition) return INVALID_ENTITY; var entPosition = cmpEntPosition.GetPosition2D(); var closestMember = INVALID_ENTITY; var closestDistance = Infinity; for (var member of this.members) { if (filter && !filter(ent)) continue; var cmpPosition = Engine.QueryInterface(member, IID_Position); if (!cmpPosition || !cmpPosition.IsInWorld()) continue; var pos = cmpPosition.GetPosition2D(); var dist = entPosition.distanceToSquared(pos); if (dist < closestDistance) { closestMember = member; closestDistance = dist; } } return closestMember; }; /** * Returns the 'primary' member of this formation (typically the most * important unit type), for e.g. playing a representative sound. * Returns undefined if no members. * TODO: actually implement something like that; currently this just returns * the arbitrary first one. */ Formation.prototype.GetPrimaryMember = function() { return this.members[0]; }; /** * Get the formation animation for a certain member of this formation * @param entity The entity ID to get the animation for * @param defaultAnimation The name of the default wanted animation for the entity * E.g. "walk", "idle" ... * @return The name of the transformed animation as defined in the template * E.g. "walk_testudo_row1" */ Formation.prototype.GetFormationAnimation = function(entity, defaultAnimation) { var animationGroup = this.animations[defaultAnimation]; if (!animationGroup || this.columnar) return defaultAnimation; var row = this.memberPositions[entity].row; var column = this.memberPositions[entity].column; for (var i = 0; i < animationGroup.length; ++i) { var minRow = animationGroup[i].minRow; if (minRow < 0) minRow += this.maxRowsUsed + 1; if (row < minRow) continue; var maxRow = animationGroup[i].maxRow; if (maxRow < 0) maxRow += this.maxRowsUsed + 1; if (row > maxRow) continue; var minColumn = animationGroup[i].minColumn; if (minColumn < 0) minColumn += this.maxColumnsUsed[row] + 1; if (column < minColumn) continue; var maxColumn = animationGroup[i].maxColumn; if (maxColumn < 0) maxColumn += this.maxColumnsUsed[row] + 1; if (column > maxColumn) continue; return animationGroup[i].animation; } return defaultAnimation; }; /** * Permits formation members to register that they've reached their destination. */ Formation.prototype.SetInPosition = function(ent) { if (this.inPosition.indexOf(ent) != -1) return; // Rotate the entity to the right angle var cmpPosition = Engine.QueryInterface(this.entity, IID_Position); var cmpEntPosition = Engine.QueryInterface(ent, IID_Position); if (cmpEntPosition && cmpEntPosition.IsInWorld() && cmpPosition && cmpPosition.IsInWorld()) cmpEntPosition.TurnTo(cmpPosition.GetRotation().y); this.inPosition.push(ent); }; /** * Called by formation members upon entering non-walking states. */ Formation.prototype.UnsetInPosition = function(ent) { var ind = this.inPosition.indexOf(ent); if (ind != -1) this.inPosition.splice(ind, 1); }; /** * Set whether we should rearrange formation members if * units are removed from the formation. */ Formation.prototype.SetRearrange = function(rearrange) { this.rearrange = rearrange; }; /** * Initialise the members of this formation. * Must only be called once. * All members must implement UnitAI. */ Formation.prototype.SetMembers = function(ents) { this.members = ents; var cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); var templateName = cmpTemplateManager.GetCurrentTemplateName(this.entity); for (var ent of this.members) { var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); cmpUnitAI.SetFormationController(this.entity); cmpUnitAI.SetLastFormationTemplate(templateName); var cmpAuras = Engine.QueryInterface(ent, IID_Auras); if (cmpAuras && cmpAuras.HasFormationAura()) { this.formationMembersWithAura.push(ent); cmpAuras.ApplyFormationBonus(ents); } } this.offsets = undefined; // Locate this formation controller in the middle of its members this.MoveToMembersCenter(); // Compute the speed etc. of the formation this.ComputeMotionParameters(); }; /** * Remove the given list of entities. * The entities must already be members of this formation. */ Formation.prototype.RemoveMembers = function(ents) { this.offsets = undefined; this.members = this.members.filter(function(e) { return ents.indexOf(e) == -1; }); this.inPosition = this.inPosition.filter(function(e) { return ents.indexOf(e) == -1; }); for (var ent of ents) { var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); cmpUnitAI.UpdateWorkOrders(); cmpUnitAI.SetFormationController(INVALID_ENTITY); } for (var ent of this.formationMembersWithAura) { var cmpAuras = Engine.QueryInterface(ent, IID_Auras); cmpAuras.RemoveFormationBonus(ents); // the unit with the aura is also removed from the formation if (ents.indexOf(ent) !== -1) cmpAuras.RemoveFormationBonus(this.members); } this.formationMembersWithAura = this.formationMembersWithAura.filter(function(e) { return ents.indexOf(e) == -1; }); // If there's nobody left, destroy the formation if (this.members.length == 0) { Engine.DestroyEntity(this.entity); return; } if (!this.rearrange) return; this.ComputeMotionParameters(); // Rearrange the remaining members this.MoveMembersIntoFormation(true, true); }; Formation.prototype.AddMembers = function(ents) { this.offsets = undefined; this.inPosition = []; for (var ent of this.formationMembersWithAura) { var cmpAuras = Engine.QueryInterface(ent, IID_Auras); cmpAuras.RemoveFormationBonus(ents); // the unit with the aura is also removed from the formation if (ents.indexOf(ent) !== -1) cmpAuras.RemoveFormationBonus(this.members); } this.members = this.members.concat(ents); for (var ent of this.members) { var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); cmpUnitAI.SetFormationController(this.entity); var cmpAuras = Engine.QueryInterface(ent, IID_Auras); if (cmpAuras && cmpAuras.HasFormationAura()) { this.formationMembersWithAura.push(ent); cmpAuras.ApplyFormationBonus(ents); } } this.MoveMembersIntoFormation(true, true); }; /** * Called when the formation stops moving in order to detect * units that have already reached their final positions. */ Formation.prototype.FindInPosition = function() { for (var i = 0; i < this.members.length; ++i) { var cmpUnitMotion = Engine.QueryInterface(this.members[i], IID_UnitMotion); if (!cmpUnitMotion.IsMoving()) { // Verify that members are stopped in FORMATIONMEMBER.WALKING var cmpUnitAI = Engine.QueryInterface(this.members[i], IID_UnitAI); if (cmpUnitAI.IsWalking()) this.SetInPosition(this.members[i]); } } }; /** * Remove all members and destroy the formation. */ Formation.prototype.Disband = function() { for (var ent of this.members) { var cmpUnitAI = Engine.QueryInterface(ent, IID_UnitAI); cmpUnitAI.SetFormationController(INVALID_ENTITY); } for (var ent of this.formationMembersWithAura) { var cmpAuras = Engine.QueryInterface(ent, IID_Auras); cmpAuras.RemoveFormationBonus(this.members); } this.members = []; this.inPosition = []; this.formationMembersWithAura = []; this.offsets = undefined; Engine.DestroyEntity(this.entity); }; /** * Set all members to form up into the formation shape. * If moveCenter is true, the formation center will be reinitialised * to the center of the units. * If force is true, all individual orders of the formation units are replaced, * otherwise the order to walk into formation is just pushed to the front. */ Formation.prototype.MoveMembersIntoFormation = function(moveCenter, force) { if (!this.members.length) return; var active = []; var positions = []; for (var ent of this.members) { var cmpPosition = Engine.QueryInterface(ent, IID_Position); if (!cmpPosition || !cmpPosition.IsInWorld()) continue; active.push(ent); // query the 2D position as exact hight calculation isn't needed // but bring the position to the right coordinates var pos = cmpPosition.GetPosition2D(); positions.push(pos); } - var avgpos = Vector2D.avg(positions); + var avgpos = Vector2D.average(positions); // Reposition the formation if we're told to or if we don't already have a position var cmpPosition = Engine.QueryInterface(this.entity, IID_Position); var inWorld = cmpPosition.IsInWorld(); if (moveCenter || !inWorld) { cmpPosition.JumpTo(avgpos.x, avgpos.y); // Don't make the formation controller entity show up in range queries if (!inWorld) { var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); cmpRangeManager.SetEntityFlag(this.entity, "normal", false); } } // Switch between column and box if necessary var cmpUnitAI = Engine.QueryInterface(this.entity, IID_UnitAI); var walkingDistance = cmpUnitAI.ComputeWalkingDistance(); var columnar = walkingDistance > g_ColumnDistanceThreshold; if (columnar != this.columnar) { this.columnar = columnar; this.offsets = undefined; } var newOrientation = this.GetEstimatedOrientation(avgpos); var dSin = Math.abs(newOrientation.sin - this.oldOrientation.sin); var dCos = Math.abs(newOrientation.cos - this.oldOrientation.cos); // If the formation existed, only recalculate positions if the turning agle is somewhat biggish if (!this.offsets || dSin > 1 || dCos > 1) this.offsets = this.ComputeFormationOffsets(active, positions); this.oldOrientation = newOrientation; var xMax = 0; var yMax = 0; var xMin = 0; var yMin = 0; for (var i = 0; i < this.offsets.length; ++i) { var offset = this.offsets[i]; var cmpUnitAI = Engine.QueryInterface(offset.ent, IID_UnitAI); if (!cmpUnitAI) continue; var data = { "target": this.entity, "x": offset.x, "z": offset.y }; cmpUnitAI.AddOrder("FormationWalk", data, !force); xMax = Math.max(xMax, offset.x); yMax = Math.max(yMax, offset.y); xMin = Math.min(xMin, offset.x); yMin = Math.min(yMin, offset.y); } this.width = xMax - xMin; this.depth = yMax - yMin; }; Formation.prototype.MoveToMembersCenter = function() { var positions = []; for (var ent of this.members) { var cmpPosition = Engine.QueryInterface(ent, IID_Position); if (!cmpPosition || !cmpPosition.IsInWorld()) continue; positions.push(cmpPosition.GetPosition2D()); } - var avgpos = Vector2D.avg(positions); + var avgpos = Vector2D.average(positions); var cmpPosition = Engine.QueryInterface(this.entity, IID_Position); var inWorld = cmpPosition.IsInWorld(); cmpPosition.JumpTo(avgpos.x, avgpos.y); // Don't make the formation controller show up in range queries if (!inWorld) { var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); cmpRangeManager.SetEntityFlag(this.entity, "normal", false); } }; Formation.prototype.GetAvgFootprint = function(active) { var footprints = []; for (var ent of active) { var cmpFootprint = Engine.QueryInterface(ent, IID_Footprint); if (cmpFootprint) footprints.push(cmpFootprint.GetShape()); } if (!footprints.length) return {"width":1, "depth": 1}; var r = {"width": 0, "depth": 0}; for (var shape of footprints) { if (shape.type == "circle") { r.width += shape.radius * 2; r.depth += shape.radius * 2; } else if (shape.type == "square") { r.width += shape.width; r.depth += shape.depth; } } r.width /= footprints.length; r.depth /= footprints.length; return r; }; Formation.prototype.ComputeFormationOffsets = function(active, positions) { var separation = this.GetAvgFootprint(active); separation.width *= this.separationMultiplier.width; separation.depth *= this.separationMultiplier.depth; if (this.columnar) var sortingClasses = ["Cavalry","Infantry"]; else var sortingClasses = this.sortingClasses.slice(); sortingClasses.push("Unknown"); // the entities will be assigned to positions in the formation in // the same order as the types list is ordered var types = {}; for (var i = 0; i < sortingClasses.length; ++i) types[sortingClasses[i]] = []; for (var i in active) { var cmpIdentity = Engine.QueryInterface(active[i], IID_Identity); var classes = cmpIdentity.GetClassesList(); var done = false; for (var c = 0; c < sortingClasses.length; ++c) { if (classes.indexOf(sortingClasses[c]) > -1) { types[sortingClasses[c]].push({"ent": active[i], "pos": positions[i]}); done = true; break; } } if (!done) types["Unknown"].push({"ent": active[i], "pos": positions[i]}); } var count = active.length; var shape = this.formationShape; var shiftRows = this.shiftRows; var centerGap = this.centerGap; var sortingOrder = this.sortingOrder; var offsets = []; // Choose a sensible size/shape for the various formations, depending on number of units var cols; if (this.columnar) { shape = "square"; cols = Math.min(count,3); shiftRows = false; centerGap = 0; sortingOrder = null; } else { var depth = Math.sqrt(count / this.widthDepthRatio); if (this.maxRows && depth > this.maxRows) depth = this.maxRows; cols = Math.ceil(count / Math.ceil(depth) + (this.shiftRows ? 0.5 : 0)); if (cols < this.minColumns) cols = Math.min(count, this.minColumns); if (this.maxColumns && cols > this.maxColumns && this.maxRows != depth) cols = this.maxColumns; } // define special formations here if (this.template.FormationName == "Scatter") { var width = Math.sqrt(count) * (separation.width + separation.depth) * 2.5; for (var i = 0; i < count; ++i) { var obj = new Vector2D(randFloat(0, width), randFloat(0, width)); obj.row = 1; obj.column = i + 1; offsets.push(obj); } } // For non-special formations, calculate the positions based on the number of entities this.maxColumnsUsed = []; this.maxRowsUsed = 0; if (shape != "special") { offsets = []; var r = 0; var left = count; // while there are units left, start a new row in the formation while (left > 0) { // save the position of the row var z = -r * separation.depth; // switch between the left and right side of the center to have a symmetrical distribution var side = 1; // determine the number of entities in this row of the formation if (shape == "square") { var n = cols; if (shiftRows) n -= r%2; } else if (shape == "triangle") { if (shiftRows) var n = r + 1; else var n = r * 2 + 1; } if (!shiftRows && n > left) n = left; for (var c = 0; c < n && left > 0; ++c) { // switch sides for the next entity side *= -1; if (n%2 == 0) var x = side * (Math.floor(c/2) + 0.5) * separation.width; else var x = side * Math.ceil(c/2) * separation.width; if (centerGap) { if (x == 0) // don't use the center position with a center gap continue; x += side * centerGap / 2; } var column = Math.ceil(n/2) + Math.ceil(c/2) * side; var r1 = randFloat(-1, 1) * this.sloppyness; var r2 = randFloat(-1, 1) * this.sloppyness; offsets.push(new Vector2D(x + r1, z + r2)); offsets[offsets.length - 1].row = r+1; offsets[offsets.length - 1].column = column; left--; } ++r; this.maxColumnsUsed[r] = n; } this.maxRowsUsed = r; } // make sure the average offset is zero, as the formation is centered around that // calculating offset distances without a zero average makes no sense, as the formation // will jump to a different position any time - var avgoffset = Vector2D.avg(offsets); + var avgoffset = Vector2D.average(offsets); offsets.forEach(function (o) {o.sub(avgoffset);}); // sort the available places in certain ways // the places first in the list will contain the heaviest units as defined by the order // of the types list if (this.sortingOrder == "fillFromTheSides") offsets.sort(function(o1, o2) { return Math.abs(o1.x) < Math.abs(o2.x);}); else if (this.sortingOrder == "fillToTheCenter") offsets.sort(function(o1, o2) { return Math.max(Math.abs(o1.x), Math.abs(o1.y)) < Math.max(Math.abs(o2.x), Math.abs(o2.y)); }); // query the 2D position of the formation var cmpPosition = Engine.QueryInterface(this.entity, IID_Position); var formationPos = cmpPosition.GetPosition2D(); // use realistic place assignment, // every soldier searches the closest available place in the formation var newOffsets = []; var realPositions = this.GetRealOffsetPositions(offsets, formationPos); for (var i = sortingClasses.length; i; --i) { var t = types[sortingClasses[i-1]]; if (!t.length) continue; var usedOffsets = offsets.splice(-t.length); var usedRealPositions = realPositions.splice(-t.length); for (var entPos of t) { var closestOffsetId = this.TakeClosestOffset(entPos, usedRealPositions, usedOffsets); usedRealPositions.splice(closestOffsetId, 1); newOffsets.push(usedOffsets.splice(closestOffsetId, 1)[0]); newOffsets[newOffsets.length - 1].ent = entPos.ent; } } return newOffsets; }; /** * Search the closest position in the realPositions list to the given entity * @param ent, the queried entity * @param realPositions, the world coordinates of the available offsets * @return the index of the closest offset position */ Formation.prototype.TakeClosestOffset = function(entPos, realPositions, offsets) { var pos = entPos.pos; var closestOffsetId = -1; var offsetDistanceSq = Infinity; for (var i = 0; i < realPositions.length; i++) { var distSq = pos.distanceToSquared(realPositions[i]); if (distSq < offsetDistanceSq) { offsetDistanceSq = distSq; closestOffsetId = i; } } this.memberPositions[entPos.ent] = {"row": offsets[closestOffsetId].row, "column":offsets[closestOffsetId].column}; return closestOffsetId; }; /** * Get the world positions for a list of offsets in this formation */ Formation.prototype.GetRealOffsetPositions = function(offsets, pos) { var offsetPositions = []; var {sin, cos} = this.GetEstimatedOrientation(pos); // calculate the world positions for (var o of offsets) offsetPositions.push(new Vector2D(pos.x + o.y * sin + o.x * cos, pos.y + o.y * cos - o.x * sin)); return offsetPositions; }; /** * calculate the estimated rotation of the formation * based on the first unitAI target position when ordered to walk, * based on the current rotation in other cases * Return the sine and cosine of the angle */ Formation.prototype.GetEstimatedOrientation = function(pos) { var cmpUnitAI = Engine.QueryInterface(this.entity, IID_UnitAI); var r = {"sin": 0, "cos": 1}; var unitAIState = cmpUnitAI.GetCurrentState(); if (unitAIState == "FORMATIONCONTROLLER.WALKING" || unitAIState == "FORMATIONCONTROLLER.COMBAT.APPROACHING") { var targetPos = cmpUnitAI.GetTargetPositions(); if (!targetPos.length) return r; var d = targetPos[0].sub(pos).normalize(); if (!d.x && !d.y) return r; r.cos = d.y; r.sin = d.x; } else { var cmpPosition = Engine.QueryInterface(this.entity, IID_Position); if (!cmpPosition) return r; var rot = cmpPosition.GetRotation().y; r.sin = Math.sin(rot); r.cos = Math.cos(rot); } return r; }; /** * Set formation controller's speed based on its current members. */ Formation.prototype.ComputeMotionParameters = function() { var maxRadius = 0; var minSpeed = Infinity; for (var ent of this.members) { var cmpUnitMotion = Engine.QueryInterface(ent, IID_UnitMotion); if (cmpUnitMotion) minSpeed = Math.min(minSpeed, cmpUnitMotion.GetWalkSpeed()); } minSpeed *= this.GetSpeedMultiplier(); var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); cmpUnitMotion.SetSpeed(minSpeed); }; Formation.prototype.ShapeUpdate = function() { // Check the distance to twin formations, and merge if when // the formations could collide for (var i = this.twinFormations.length - 1; i >= 0; --i) { // only do the check on one side if (this.twinFormations[i] <= this.entity) continue; var cmpPosition = Engine.QueryInterface(this.entity, IID_Position); var cmpOtherPosition = Engine.QueryInterface(this.twinFormations[i], IID_Position); var cmpOtherFormation = Engine.QueryInterface(this.twinFormations[i], IID_Formation); if (!cmpPosition || !cmpOtherPosition || !cmpOtherFormation) continue; var thisPosition = cmpPosition.GetPosition2D(); var otherPosition = cmpOtherPosition.GetPosition2D(); var dx = thisPosition.x - otherPosition.x; var dy = thisPosition.y - otherPosition.y; var dist = Math.sqrt(dx * dx + dy * dy); var thisSize = this.GetSize(); var otherSize = cmpOtherFormation.GetSize(); var minDist = Math.max(thisSize.width / 2, thisSize.depth / 2) + Math.max(otherSize.width / 2, otherSize.depth / 2) + this.formationSeparation; if (minDist < dist) continue; // merge the members from the twin formation into this one // twin formations should always have exactly the same orders this.AddMembers(cmpOtherFormation.members); Engine.DestroyEntity(this.twinFormations[i]); this.twinFormations.splice(i,1); } // Switch between column and box if necessary var cmpUnitAI = Engine.QueryInterface(this.entity, IID_UnitAI); var walkingDistance = cmpUnitAI.ComputeWalkingDistance(); var columnar = walkingDistance > g_ColumnDistanceThreshold; if (columnar != this.columnar) { this.offsets = undefined; this.columnar = columnar; this.MoveMembersIntoFormation(false, true); // (disable moveCenter so we can't get stuck in a loop of switching // shape causing center to change causing shape to switch back) } }; Formation.prototype.OnGlobalOwnershipChanged = function(msg) { // When an entity is captured or destroyed, it should no longer be // controlled by this formation if (this.members.indexOf(msg.entity) != -1) this.RemoveMembers([msg.entity]); }; Formation.prototype.OnGlobalEntityRenamed = function(msg) { if (this.members.indexOf(msg.entity) != -1) { this.offsets = undefined; var cmpNewUnitAI = Engine.QueryInterface(msg.newentity, IID_UnitAI); if (cmpNewUnitAI) { this.members[this.members.indexOf(msg.entity)] = msg.newentity; this.memberPositions[msg.newentity] = this.memberPositions[msg.entity]; } var cmpOldUnitAI = Engine.QueryInterface(msg.entity, IID_UnitAI); cmpOldUnitAI.SetFormationController(INVALID_ENTITY); if (cmpNewUnitAI) cmpNewUnitAI.SetFormationController(this.entity); // Because the renamed entity might have different characteristics, // (e.g. packed vs. unpacked siege), we need to recompute motion parameters this.ComputeMotionParameters(); } }; Formation.prototype.RegisterTwinFormation = function(entity) { var cmpFormation = Engine.QueryInterface(entity, IID_Formation); if (!cmpFormation) return; this.twinFormations.push(entity); cmpFormation.twinFormations.push(this.entity); }; Formation.prototype.DeleteTwinFormations = function() { for (var ent of this.twinFormations) { var cmpFormation = Engine.QueryInterface(ent, IID_Formation); if (cmpFormation) cmpFormation.twinFormations.splice(cmpFormation.twinFormations.indexOf(this.entity), 1); } this.twinFormations = []; }; Formation.prototype.LoadFormation = function(newTemplate) { // get the old formation info var members = this.members.slice(); var cmpThisUnitAI = Engine.QueryInterface(this.entity, IID_UnitAI); var orders = cmpThisUnitAI.GetOrders().slice(); this.Disband(); var newFormation = Engine.AddEntity(newTemplate); // Apply the info from the old formation to the new one let cmpNewOwnership = Engine.QueryInterface(newFormation, IID_Ownership); let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); if (cmpOwnership && cmpNewOwnership) cmpNewOwnership.SetOwner(cmpOwnership.GetOwner()); var cmpNewPosition = Engine.QueryInterface(newFormation, IID_Position); var cmpPosition = Engine.QueryInterface(this.entity, IID_Position); if (cmpPosition && cmpPosition.IsInWorld() && cmpNewPosition) cmpNewPosition.TurnTo(cmpPosition.GetRotation().y); var cmpFormation = Engine.QueryInterface(newFormation, IID_Formation); var cmpNewUnitAI = Engine.QueryInterface(newFormation, IID_UnitAI); cmpFormation.SetMembers(members); if (orders.length) cmpNewUnitAI.AddOrders(orders); else cmpNewUnitAI.MoveIntoFormation(); Engine.PostMessage(this.entity, MT_EntityRenamed, { "entity": this.entity, "newentity": newFormation }); }; Engine.RegisterComponentType(IID_Formation, "Formation", Formation); Index: ps/trunk/binaries/data/mods/public/simulation/components/tests/test_Vector.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/tests/test_Vector.js (revision 20705) +++ ps/trunk/binaries/data/mods/public/simulation/components/tests/test_Vector.js (revision 20706) @@ -1,182 +1,200 @@ var brokenVector = { "lengthSquared": () => "incompatible vector" }; // Test Vector2D add, mult, distance { let v1 = new Vector2D(); TS_ASSERT_EQUALS(v1.x, 0); TS_ASSERT_EQUALS(v1.y, 0); let v2 = new Vector2D(3, 4); TS_ASSERT_EQUALS(v1.distanceTo(v2), 5); v2.mult(3); TS_ASSERT_EQUALS(v2.x, 9); TS_ASSERT_EQUALS(v2.y, 12); v2.add(new Vector2D(1, 2)); TS_ASSERT_EQUALS(v2.x, 10); TS_ASSERT_EQUALS(v2.y, 14); } // Test Vector2D normalization { let v3 = new Vector2D(0, 5).normalize(); TS_ASSERT_EQUALS(v3.x, 0); TS_ASSERT_EQUALS(v3.y, 1); v3.set(-8, 0).normalize(); TS_ASSERT_EQUALS(v3.x, -1); TS_ASSERT_EQUALS(v3.y, 0); } // Test Vector2D rotation { let v4 = new Vector2D(2, -5).rotate(4 * Math.PI); TS_ASSERT_EQUALS(v4.x, 2); TS_ASSERT_EQUALS(v4.y, -5); v4.rotate(Math.PI); TS_ASSERT_EQUALS(v4.x, -2); TS_ASSERT_EQUALS(v4.y, 5); // Result of rotating (1, 0) let unitCircle = [ { "angle": Math.PI / 2, "x": 0, "y": 1 }, { "angle": Math.PI / 3, "x": 1/2, "y": Math.sqrt(3) / 2 }, { "angle": Math.PI / 4, "x": Math.sqrt(2) / 2, "y": Math.sqrt(2) / 2 }, { "angle": Math.PI / 6, "x": Math.sqrt(3) / 2, "y": 1/2 } ]; let epsilon = 0.00000001; for (let expectedVector of unitCircle) { let computedVector = new Vector2D(1, 0).rotate(-expectedVector.angle); TS_ASSERT_EQUALS_APPROX(computedVector.x, expectedVector.x, epsilon); TS_ASSERT_EQUALS_APPROX(computedVector.y, expectedVector.y, epsilon); } } // Test Vector2D rotation around a center { let epsilon = 0.00000001; let v1 = new Vector2D(-4, 8).rotateAround(Math.PI / 3, new Vector2D(-1, -3)); TS_ASSERT_EQUALS_APPROX(v1.x, 7.02627944, epsilon); TS_ASSERT_EQUALS_APPROX(v1.y, 5.09807617, epsilon); } // Test Vector2D dot product { TS_ASSERT_EQUALS(new Vector2D(2, 3).dot(new Vector2D(4, 5)), 23); } // Test Vector2D cross product { TS_ASSERT_EQUALS(new Vector2D(3, 5).cross(new Vector2D(-4, -1/3)), 19); } // Test Vector2D length and compareLength { TS_ASSERT_EQUALS(new Vector2D(20, 21).length(), 29); let v5 = new Vector2D(10, 20); TS_ASSERT_EQUALS(v5.compareLength(new Vector2D(5, 8)), 1); TS_ASSERT_EQUALS(v5.compareLength(new Vector2D(500, 800)), -1); TS_ASSERT_EQUALS(v5.compareLength(new Vector2D(10, 20)), 0); TS_ASSERT(isNaN(v5.compareLength(brokenVector))); } // Test Vector2D rotation furthermore { let epsilon = 0.00000001; let v5 = new Vector2D(10, 20); let v6 = v5.clone(); TS_ASSERT_EQUALS(v5.x, v6.x); TS_ASSERT_EQUALS(v5.y, v6.y); TS_ASSERT(Math.abs(v5.dot(v6.rotate(Math.PI / 2))) < epsilon); } // Test Vector2D perpendicular { let v7 = new Vector2D(4, 5).perpendicular(); TS_ASSERT_EQUALS(v7.x, -5); TS_ASSERT_EQUALS(v7.y, 4); let v8 = new Vector2D(0, 0).perpendicular(); TS_ASSERT_EQUALS(v8.x, 0); TS_ASSERT_EQUALS(v8.y, 0); } // Test Vector2D angleTo { let v1 = new Vector2D(1, 1); let v2 = new Vector2D(1, 3); let v3 = new Vector2D(3, 1); TS_ASSERT_EQUALS(v1.angleTo(v2), 0); TS_ASSERT_EQUALS(v1.angleTo(v3), Math.PI / 2); TS_ASSERT_EQUALS(v3.angleTo(v2), -Math.PI / 4); } +// Test Vector2D list functions +{ + let list = [ + new Vector2D(), + new Vector2D(-1, 5), + new Vector2D(89, -123), + new Vector2D(55, 66), + ]; + + let sum = Vector2D.sum(list); + TS_ASSERT_EQUALS(sum.x, 143); + TS_ASSERT_EQUALS(sum.y, -52); + + let avg = Vector2D.average(list); + TS_ASSERT_EQUALS(avg.x, 35.75); + TS_ASSERT_EQUALS(avg.y, -13); +} + // Test Vector3D distance and compareLength { let v1 = new Vector3D(2, 5, 14); TS_ASSERT_EQUALS(v1.distanceTo(new Vector3D()), 15); TS_ASSERT(isNaN(v1.compareLength(brokenVector))); } // Test Vector3D mult { let v2 = new Vector3D(2, 5, 14).mult(3); TS_ASSERT_EQUALS(v2.x, 6); TS_ASSERT_EQUALS(v2.y, 15); TS_ASSERT_EQUALS(v2.z, 42); } // Test Vector3D dot product { TS_ASSERT_EQUALS(new Vector3D(1, 2, 3).dot(new Vector3D(4, 5, 6)), 32); } // Test Vector3D clone { let v3 = new Vector3D(9, 10, 11); let v4 = v3.clone(); TS_ASSERT_EQUALS(v3.x, v4.x); TS_ASSERT_EQUALS(v3.y, v4.y); TS_ASSERT_EQUALS(v3.z, v4.z); } // Test Vector3D cross product { let v5 = new Vector3D(1, 2, 3).cross(new Vector3D(4, 5, 6)); TS_ASSERT_EQUALS(v5.x, -3); TS_ASSERT_EQUALS(v5.y, 6); TS_ASSERT_EQUALS(v5.z, -3); } // Test Vector3D horizAngleTo { let v1 = new Vector3D(1, 1, 1); let v2 = new Vector3D(1, 2, 3); let v3 = new Vector3D(3, 10, 1); TS_ASSERT_EQUALS(v1.horizAngleTo(v2), 0); TS_ASSERT_EQUALS(v1.horizAngleTo(v3), Math.PI / 2); TS_ASSERT_EQUALS(v3.horizAngleTo(v2), -Math.PI / 4); }