Fix building of fields + update tests to make sure this doesn't happen again. Refs #4121

This was SVN commit r18575.
This commit is contained in:
sanderd17 2016-08-02 11:54:18 +00:00
parent 69b3c5871b
commit 8af205fd71
2 changed files with 158 additions and 111 deletions

View File

@ -251,11 +251,11 @@ Foundation.prototype.Build = function(builderEnt, work)
}
var cmpFoundationPosition = Engine.QueryInterface(this.entity, IID_Position);
var pos = cmpFoundationPosition.GetPosition();
var pos = cmpFoundationPosition.GetPosition2D();
var rot = cmpFoundationPosition.GetRotation();
cmpPreviewPosition.SetYRotation(rot.y);
cmpPreviewPosition.SetXZRotation(rot.x, rot.z);
cmpPreviewPosition.JumpTo(pos.x, pos.z);
cmpPreviewPosition.JumpTo(pos.x, pos.y);
}
this.committed = true;
@ -333,7 +333,7 @@ Foundation.prototype.Build = function(builderEnt, work)
if (cmpTerritoryDecay && cmpTerritoryDecay.HasTerritoryOwnership())
{
let cmpTerritoryManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TerritoryManager);
owner = cmpTerritoryManager.GetOwner(pos.x, pos.z);
owner = cmpTerritoryManager.GetOwner(pos.x, pos.y);
}
else
{

View File

@ -8,132 +8,179 @@ Engine.LoadComponentScript("interfaces/TerritoryDecay.js");
Engine.LoadComponentScript("interfaces/Trigger.js");
Engine.LoadComponentScript("Foundation.js");
AddMock(SYSTEM_ENTITY, IID_Trigger, {
"CallEvent": () => {},
});
let player = 1;
let playerEnt = 3;
AddMock(SYSTEM_ENTITY, IID_PlayerManager, {
"GetPlayerByID": () => playerEnt,
});
AddMock(playerEnt, IID_StatisticsTracker, {
"IncreaseConstructedBuildingsCounter": ent => {
TS_ASSERT_EQUALS(ent, newEnt);
},
});
let foundationEnt = 20;
let newEnt = 21;
let finalTemplate = "structures/athen_civil_centre.xml";
let foundationHP = 1;
let maxHP = 100;
let previewEnt = 21;
let newEnt = 22;
AddMock(foundationEnt, IID_Cost, {
"GetBuildTime": () => 50,
"GetResourceCosts": () => ({ "wood": 100 }),
});
AddMock(foundationEnt, IID_Health, {
"GetHitpoints": () => foundationHP,
"GetMaxHitpoints": () => maxHP,
"Increase": hp => {
foundationHP = Math.min(foundationHP + hp, maxHP);
cmpFoundation.OnHealthChanged();
},
});
AddMock(foundationEnt, IID_Obstruction, {
"GetBlockMovementFlag": () => true,
"GetUnitCollisions": () => [],
"SetDisableBlockMovementPathfinding": () => {},
});
AddMock(foundationEnt, IID_Ownership, {
"GetOwner": () => player,
});
AddMock(foundationEnt, IID_Position, {
"GetPosition2D": () => new Vector2D(1, 2),
"GetRotation": () => new Vector3D(1, 2, 3),
"IsInWorld": () => true,
});
Engine.AddEntity = function(template)
function testFoundation(...mocks)
{
AddMock(newEnt, IID_Position, {
"JumpTo": (x, y) => { TS_ASSERT_EQUALS(x, 1); TS_ASSERT_EQUALS(y, 2); },
"SetYRotation": r => { TS_ASSERT_EQUALS(r, 2); },
"SetXZRotation": (rx, rz) => {
TS_ASSERT_EQUALS(rx, 1);
TS_ASSERT_EQUALS(rz, 3);
ResetState();
AddMock(SYSTEM_ENTITY, IID_Trigger, {
"CallEvent": () => {},
});
AddMock(SYSTEM_ENTITY, IID_PlayerManager, {
"GetPlayerByID": () => playerEnt,
});
AddMock(SYSTEM_ENTITY, IID_TerritoryManager, {
"GetOwner": (x, y) => {
TS_ASSERT_EQUALS(x, pos.x);
TS_ASSERT_EQUALS(y, pos.y);
return player;
},
});
Engine.RegisterGlobal("PlaySound", (name, source) => {
TS_ASSERT_EQUALS(name, "constructed");
TS_ASSERT_EQUALS(source, newEnt);
});
Engine.RegisterGlobal("MT_EntityRenamed", "entityRenamed");
let finalTemplate = "structures/athen_civil_centre.xml";
let foundationHP = 1;
let maxHP = 100;
let rot = new Vector3D(1, 2, 3);
let pos = new Vector2D(4, 5);
AddMock(foundationEnt, IID_Cost, {
"GetBuildTime": () => 50,
"GetResourceCosts": () => ({ "wood": 100 }),
});
AddMock(foundationEnt, IID_Health, {
"GetHitpoints": () => foundationHP,
"GetMaxHitpoints": () => maxHP,
"Increase": hp => {
foundationHP = Math.min(foundationHP + hp, maxHP);
cmpFoundation.OnHealthChanged();
},
});
AddMock(foundationEnt, IID_Obstruction, {
"GetBlockMovementFlag": () => true,
"GetUnitCollisions": () => [],
"SetDisableBlockMovementPathfinding": () => {},
});
AddMock(foundationEnt, IID_Ownership, {
"GetOwner": () => player,
});
AddMock(foundationEnt, IID_Position, {
"GetPosition2D": () => pos,
"GetRotation": () => rot,
"SetConstructionProgress": () => {},
"IsInWorld": () => true,
});
AddMock(previewEnt, IID_Ownership, {
"SetOwner": owner => { TS_ASSERT_EQUALS(owner, player); },
});
AddMock(previewEnt, IID_Position, {
"JumpTo": (x, y) => {
TS_ASSERT_EQUALS(x, pos.x);
TS_ASSERT_EQUALS(y, pos.y);
},
"SetConstructionProgress": p => {},
"SetYRotation": r => { TS_ASSERT_EQUALS(r, rot.y); },
"SetXZRotation": (rx, rz) => {
TS_ASSERT_EQUALS(rx, rot.x);
TS_ASSERT_EQUALS(rz, rot.z);
},
});
AddMock(newEnt, IID_Ownership, {
"SetOwner": owner => { TS_ASSERT_EQUALS(owner, player); },
});
return newEnt;
};
function PlaySound(name, source)
{
TS_ASSERT_EQUALS(name, "constructed");
TS_ASSERT_EQUALS(source, newEnt);
};
Engine.RegisterGlobal("PlaySound", PlaySound);
Engine.RegisterGlobal("MT_EntityRenamed", "entityRenamed");
AddMock(newEnt, IID_Position, {
"JumpTo": (x, y) => {
TS_ASSERT_EQUALS(x, pos.x);
TS_ASSERT_EQUALS(y, pos.y);
},
"SetYRotation": r => { TS_ASSERT_EQUALS(r, rot.y); },
"SetXZRotation": (rx, rz) => {
TS_ASSERT_EQUALS(rx, rot.x);
TS_ASSERT_EQUALS(rz, rot.z);
},
});
let cmpFoundation = ConstructComponent(foundationEnt, "Foundation", {});
for (let mock of mocks)
AddMock(...mock);
// INITIALISE
cmpFoundation.InitialiseConstruction(player, finalTemplate);
// INITIALISE
Engine.AddEntity = function(template) {
TS_ASSERT_EQUALS(template, "construction|" + finalTemplate);
return previewEnt;
};
let cmpFoundation = ConstructComponent(foundationEnt, "Foundation", {});
cmpFoundation.InitialiseConstruction(player, finalTemplate);
TS_ASSERT_EQUALS(cmpFoundation.owner, player);
TS_ASSERT_EQUALS(cmpFoundation.finalTemplateName, finalTemplate);
TS_ASSERT_EQUALS(cmpFoundation.maxProgress, 0);
TS_ASSERT_EQUALS(cmpFoundation.initialised, true);
TS_ASSERT_EQUALS(cmpFoundation.owner, player);
TS_ASSERT_EQUALS(cmpFoundation.finalTemplateName, finalTemplate);
TS_ASSERT_EQUALS(cmpFoundation.maxProgress, 0);
TS_ASSERT_EQUALS(cmpFoundation.initialised, true);
// BUILDER COUNT
let twoBuilderMultiplier = Math.pow(2, cmpFoundation.buildTimePenalty) / 2;
TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 0);
cmpFoundation.AddBuilder(10);
TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 1);
TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, 1);
cmpFoundation.AddBuilder(11);
TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 2);
TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, twoBuilderMultiplier);
cmpFoundation.AddBuilder(11);
TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 2);
TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, twoBuilderMultiplier);
cmpFoundation.RemoveBuilder(11);
TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 1);
TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, 1);
cmpFoundation.RemoveBuilder(11);
TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 1);
TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, 1);
// with cmpVisual
AddMock(foundationEnt, IID_Visual, {
// BUILDER COUNT
let twoBuilderMultiplier = Math.pow(2, cmpFoundation.buildTimePenalty) / 2;
TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 0);
cmpFoundation.AddBuilder(10);
TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 1);
TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, 1);
cmpFoundation.AddBuilder(11);
TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 2);
TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, twoBuilderMultiplier);
cmpFoundation.AddBuilder(11);
TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 2);
TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, twoBuilderMultiplier);
cmpFoundation.RemoveBuilder(11);
TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 1);
TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, 1);
cmpFoundation.RemoveBuilder(11);
TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 1);
TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, 1);
// COMMIT FOUNDATION
TS_ASSERT_EQUALS(cmpFoundation.committed, false);
let work = 5;
cmpFoundation.Build(10, work);
TS_ASSERT_EQUALS(cmpFoundation.committed, true);
TS_ASSERT_EQUALS(foundationHP, 1 + work * cmpFoundation.GetBuildRate() * cmpFoundation.buildMultiplier);
TS_ASSERT_EQUALS(cmpFoundation.maxProgress, foundationHP / maxHP);
// FINISH CONSTRUCTION
Engine.AddEntity = function(template) {
TS_ASSERT_EQUALS(template, finalTemplate);
return newEnt;
};
cmpFoundation.Build(10, 1000);
TS_ASSERT_EQUALS(cmpFoundation.maxProgress, 1);
TS_ASSERT_EQUALS(foundationHP, maxHP);
}
testFoundation();
testFoundation([foundationEnt, IID_Visual, {
"SetVariable": (key, num) => {
TS_ASSERT_EQUALS(key, "numbuilders");
TS_ASSERT_EQUALS(num, 2);
TS_ASSERT(num == 1 || num == 2);
},
});
cmpFoundation.AddBuilder(11);
DeleteMock(foundationEnt, IID_Visual);
cmpFoundation.RemoveBuilder(11);
"SelectAnimation": () => {},
"HasConstructionPreview": () => true,
}]);
// COMMIT FOUNDATION
TS_ASSERT_EQUALS(cmpFoundation.committed, false);
let work = 5;
cmpFoundation.Build(10, work);
TS_ASSERT_EQUALS(cmpFoundation.committed, true);
TS_ASSERT_EQUALS(foundationHP, 1 + work * cmpFoundation.GetBuildRate() * cmpFoundation.buildMultiplier);
TS_ASSERT_EQUALS(cmpFoundation.maxProgress, foundationHP / maxHP);
testFoundation([newEnt, IID_TerritoryDecay, {
"HasTerritoryOwnership": () => true,
}]);
// FINISH CONSTRUCTION
cmpFoundation.Build(10, 1000);
TS_ASSERT_EQUALS(cmpFoundation.maxProgress, 1);
TS_ASSERT_EQUALS(foundationHP, maxHP);
testFoundation([playerEnt, IID_StatisticsTracker, {
"IncreaseConstructedBuildingsCounter": ent => {
TS_ASSERT_EQUALS(ent, newEnt);
},
}]);