1
0
forked from 0ad/0ad

Remove some variables from serialisation and update this.animations name to reflect what it actually holds [Formations.js]

Needs ReviewPublic

Differential Revision: https://code.wildfiregames.com/D2707
Comments by: elexis, Stan, wraitii, bb
Remove variables were not needed to be serialised or stored inside
component
Create `variablesToSerialize` to hold list of variables, that need to be
Serialized.
Rename `this.template.Animations` to `this.template.AnimationVariants`
(and anything related) as it does not hold animation names but names of
animation variants what is misleading.
Do not create timer when deserializing as that would cause oos and timer
already exists.
this.Init has to be called from Deserialize because it is not called by
default

This was SVN commit r23694.
This commit is contained in:
Angen 2020-05-23 13:03:34 +00:00
parent 21e3eedd2f
commit 9f6d44feb8
7 changed files with 80 additions and 46 deletions

View File

@ -66,17 +66,33 @@ Formation.prototype.Schema =
"<element name='UnitSeparationDepthMultiplier' a:help='Place the units in the formation closer or further to each other. The standard separation is the footprint size.'>" +
"<ref name='nonNegativeDecimal'/>" +
"</element>" +
"<element name='Animations' a:help='Give a list of animation variants to use for the particular formation members, based on their positions'>" +
"<text a:help='example text: \"1..1,1..-1:animation1;2..2,1..-1;animation2\", this will set animation1 for the first row, and animation2 for the second row. The first part of the numbers (1..1 and 2..2) means the row range. Every row between (and including) those values will switch animations. The second part of the numbers (1..-1) denote the columns inside those rows that will be affected. Note that in both cases, you can use -1 for the last row/column, -2 for the second to last, etc.'/>" +
"<element name='AnimationVariants' a:help='Give a list of animation variants to use for the particular formation members, based on their positions'>" +
"<text a:help='example text: \"1..1,1..-1:animationVariant1;2..2,1..-1;animationVariant2\", this will set animationVariant1 for the first row, and animation2 for the second row. The first part of the numbers (1..1 and 2..2) means the row range. Every row between (and including) those values will switch animationvariants. The second part of the numbers (1..-1) denote the columns inside those rows that will be affected. Note that in both cases, you can use -1 for the last row/column, -2 for the second to last, etc.'/>" +
"</element>";
var g_ColumnDistanceThreshold = 128; // distance at which we'll switch between column/box formations
Formation.prototype.Init = function()
Formation.prototype.variablesToSerialize = [
"lastOrderVariant",
"members",
"memberPositions",
"maxRowsUsed",
"maxColumnsUsed",
"inPosition",
"columnar",
"rearrange",
"formationMembersWithAura",
"width",
"depth",
"oldOrientation",
"twinFormations",
"formationSeparation",
"offsets"
];
Formation.prototype.Init = function(deserialized = false)
{
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,
@ -89,26 +105,26 @@ Formation.prototype.Init = function()
this.maxRows = +(this.template.MaxRows || 0);
this.centerGap = +(this.template.CenterGap || 0);
this.animations = [];
if (this.template.Animations)
if (this.template.AnimationVariants)
{
let differentAnimations = this.template.Animations.split(/\s*;\s*/);
// loop over the different rectangulars that will map to different animations
for (var rectAnimation of differentAnimations)
this.animationvariants = [];
let differentAnimationVariants = this.template.AnimationVariants.split(/\s*;\s*/);
// loop over the different rectangulars that will map to different animation variants
for (let rectAnimationVariant of differentAnimationVariants)
{
var rect, replacementAnimationName;
[rect, replacementAnimationName] = rectAnimation.split(/\s*:\s*/);
var rows, columns;
let rect, replacementAnimationVariant;
[rect, replacementAnimationVariant] = rectAnimationVariant.split(/\s*:\s*/);
let rows, columns;
[rows, columns] = rect.split(/\s*,\s*/);
var minRow, maxRow, minColumn, maxColumn;
let minRow, maxRow, minColumn, maxColumn;
[minRow, maxRow] = rows.split(/\s*\.\.\s*/);
[minColumn, maxColumn] = columns.split(/\s*\.\.\s*/);
this.animations.push({
this.animationvariants.push({
"minRow": +minRow,
"maxRow": +maxRow,
"minColumn": +minColumn,
"maxColumn": +maxColumn,
"animation": replacementAnimationName
"name": replacementAnimationVariant
});
}
}
@ -129,10 +145,30 @@ Formation.prototype.Init = function()
this.twinFormations = [];
// distance from which two twin formations will merge into one.
this.formationSeparation = 0;
if (deserialized)
return;
Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer)
.SetInterval(this.entity, IID_Formation, "ShapeUpdate", 1000, 1000, null);
};
Formation.prototype.Serialize = function()
{
let result = {};
for (let key of this.variablesToSerialize)
result[key] = this[key];
return result;
};
Formation.prototype.Deserialize = function(data)
{
this.Init(true);
for (let key in data)
this[key] = data[key];
};
/**
* Set the value from which two twin formations will become one.
*/
@ -203,45 +239,44 @@ Formation.prototype.GetPrimaryMember = function()
};
/**
* Get the formation animation for a certain member of this formation
* Get the formation animation variant for a certain member of this formation
* @param entity The entity ID to get the animation for
* @return The name of the transformed animation as defined in the template
* @return The name of the animation variant as defined in the template
* E.g. "testudo_row1" or undefined if does not exist
*/
Formation.prototype.GetFormationAnimation = function(entity)
Formation.prototype.GetFormationAnimationVariant = function(entity)
{
var animationGroup = this.animations;
if (!animationGroup.length || this.columnar || !this.memberPositions[entity])
if (!this.animationvariants || !this.animationvariants.length || this.columnar || !this.memberPositions[entity])
return undefined;
var row = this.memberPositions[entity].row;
var column = this.memberPositions[entity].column;
for (var i = 0; i < animationGroup.length; ++i)
let row = this.memberPositions[entity].row;
let column = this.memberPositions[entity].column;
for (let i = 0; i < this.animationvariants.length; ++i)
{
var minRow = animationGroup[i].minRow;
let minRow = this.animationvariants[i].minRow;
if (minRow < 0)
minRow += this.maxRowsUsed + 1;
if (row < minRow)
continue;
var maxRow = animationGroup[i].maxRow;
let maxRow = this.animationvariants[i].maxRow;
if (maxRow < 0)
maxRow += this.maxRowsUsed + 1;
if (row > maxRow)
continue;
var minColumn = animationGroup[i].minColumn;
let minColumn = this.animationvariants[i].minColumn;
if (minColumn < 0)
minColumn += this.maxColumnsUsed[row] + 1;
if (column < minColumn)
continue;
var maxColumn = animationGroup[i].maxColumn;
let maxColumn = this.animationvariants[i].maxColumn;
if (maxColumn < 0)
maxColumn += this.maxColumnsUsed[row] + 1;
if (column > maxColumn)
continue;
return animationGroup[i].animation;
return this.animationvariants[i].name;
}
return undefined;
};
@ -636,11 +671,10 @@ Formation.prototype.ComputeFormationOffsets = function(active, positions)
var count = active.length;
var shape = this.formationShape;
let shape = this.template.FormationShape;
var shiftRows = this.shiftRows;
var centerGap = this.centerGap;
var sortingOrder = this.sortingOrder;
let sortingOrder = this.template.SortingOrder;
var offsets = [];
// Choose a sensible size/shape for the various formations, depending on number of units
@ -656,7 +690,7 @@ Formation.prototype.ComputeFormationOffsets = function(active, positions)
}
else
{
var depth = Math.sqrt(count / this.widthDepthRatio);
let 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));
@ -726,8 +760,8 @@ Formation.prototype.ComputeFormationOffsets = function(active, positions)
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;
let r1 = randFloat(-1, 1) * this.sloppyness;
let r2 = randFloat(-1, 1) * this.sloppyness;
offsets.push(new Vector2D(x + r1, z + r2));
offsets[offsets.length - 1].row = r+1;

View File

@ -1312,7 +1312,7 @@ UnitAI.prototype.UnitFsmSpec = {
let cmpFormation = Engine.QueryInterface(this.formationController, IID_Formation);
if (cmpFormation)
{
this.formationAnimationVariant = cmpFormation.GetFormationAnimation(this.entity);
this.formationAnimationVariant = cmpFormation.GetFormationAnimationVariant(this.entity);
if (this.formationAnimationVariant)
this.SetAnimationVariant(this.formationAnimationVariant);
else
@ -1341,7 +1341,7 @@ UnitAI.prototype.UnitFsmSpec = {
{
let cmpFormation = Engine.QueryInterface(this.formationController, IID_Formation);
if (cmpFormation)
this.formationAnimationVariant = cmpFormation.GetFormationAnimation(this.entity);
this.formationAnimationVariant = cmpFormation.GetFormationAnimationVariant(this.entity);
}
if (this.formationAnimationVariant)
this.SetAnimationVariant(this.formationAnimationVariant);

View File

@ -14,10 +14,10 @@
<MaxColumns>8</MaxColumns>
<MaxRows>2</MaxRows>
<WidthDepthRatio>0.8</WidthDepthRatio>
<Animations>
<AnimationVariants>
1..1,1..-1: anti_cavalry_front;
2..2,1..-1: anti_cavalry_back
</Animations>
</AnimationVariants>
</Formation>
<FormationAttack>
<CanAttackAsFormation>false</CanAttackAsFormation>

View File

@ -12,11 +12,11 @@
<MinColumns>5</MinColumns>
<MaxColumns>20</MaxColumns>
<MaxRows>8</MaxRows>
<Animations>
<AnimationVariants>
1..1,1..-1: phalanx_front;
2..2,1..-1: phalanx_mid;
3..-1,1..-1: phalanx_back
</Animations>
</AnimationVariants>
</Formation>
<FormationAttack>
<CanAttackAsFormation>false</CanAttackAsFormation>

View File

@ -14,14 +14,14 @@
<MinColumns>4</MinColumns>
<MaxColumns>8</MaxColumns>
<MaxRows>9</MaxRows>
<Animations>
<AnimationVariants>
1..2,1..-1: syntagma_front;
3..3,1..-1: syntagma_01;
4..4,1..-1: syntagma_02;
5..5,1..-1: syntagma_03;
6..6,1..-1: syntagma_04;
7...-1,1..-1: syntagma_back
</Animations>
</AnimationVariants>
<Sloppyness>0.15</Sloppyness>
</Formation>
<FormationAttack>

View File

@ -13,7 +13,7 @@
<WidthDepthRatio>0.8</WidthDepthRatio>
<MinColumns>8</MinColumns>
<MaxColumns>8</MaxColumns>
<Animations>
<AnimationVariants>
1..1,2..7: testudo_front;
1..1,1..1: testudo_front_left;
1..1,8..8: testudo_front_right;
@ -62,7 +62,7 @@
16..16,8..8: testudo_right;
16..16,2..7: testudo_top;
16..16,1..1: testudo_left
</Animations>
</AnimationVariants>
</Formation>
<FormationAttack>
<CanAttackAsFormation>false</CanAttackAsFormation>

View File

@ -31,7 +31,7 @@
<UnitSeparationDepthMultiplier>1</UnitSeparationDepthMultiplier>
<WidthDepthRatio>1</WidthDepthRatio>
<Sloppyness>0</Sloppyness>
<Animations/>
<AnimationVariants/>
</Formation>
<FormationAttack>
<CanAttackAsFormation>false</CanAttackAsFormation>