1
0
forked from 0ad/0ad

Builder component tests and some cleanup. Patch by Stan.

Reviewed By: leper, bb
Differential Revision: https://code.wildfiregames.com/D99
This was SVN commit r19488.
This commit is contained in:
leper 2017-05-01 00:44:08 +00:00
parent 2ad6dc478c
commit c14a7a0b0b
2 changed files with 80 additions and 24 deletions

View File

@ -8,10 +8,10 @@ Builder.prototype.Schema =
"\n structures/{civ}_barracks\n structures/{civ}_civil_centre\n structures/pers_apadana\n " +
"</Entities>" +
"</a:example>" +
"<element name='Rate' a:help='Construction speed multiplier (1.0 is normal speed, higher values are faster)'>" +
"<element name='Rate' a:help='Construction speed multiplier (1.0 is normal speed, higher values are faster).'>" +
"<ref name='positiveDecimal'/>" +
"</element>" +
"<element name='Entities' a:help='Space-separated list of entity template names that this unit can build. The special string \"{civ}\" will be automatically replaced by the unit&apos;s four-character civ code. This element can also be empty, in which case no new foundations may be placed by the unit, but they can still repair existing buildings'>" +
"<element name='Entities' a:help='Space-separated list of entity template names that this unit can build. The special string \"{civ}\" will be automatically replaced by the unit&apos;s four-character civ code. This element can also be empty, in which case no new foundations may be placed by the unit, but they can still repair existing buildings.'>" +
"<attribute name='datatype'>" +
"<value>tokens</value>" +
"</attribute>" +
@ -26,31 +26,29 @@ Builder.prototype.Serialize = null; // we have no dynamic state to save
Builder.prototype.GetEntitiesList = function()
{
var entities = [];
var string = this.template.Entities._string;
if (string)
{
// Replace the "{civ}" codes with this entity's civ ID
var cmpIdentity = Engine.QueryInterface(this.entity, IID_Identity);
if (cmpIdentity)
string = string.replace(/\{civ\}/g, cmpIdentity.GetCiv());
entities = string.split(/\s+/);
let string = this.template.Entities._string;
// Remove disabled entities
var cmpPlayer = QueryOwnerInterface(this.entity, IID_Player);
var disabledEntities = cmpPlayer.GetDisabledTemplates();
if (!string)
return [];
for (var i = entities.length - 1; i >= 0; --i)
if (disabledEntities[entities[i]])
entities.splice(i, 1);
}
return entities;
let cmpIdentity = Engine.QueryInterface(this.entity, IID_Identity);
if (cmpIdentity)
string = string.replace(/\{civ\}/g, cmpIdentity.GetCiv());
let entities = string.split(/\s+/);
let cmpPlayer = QueryOwnerInterface(this.entity);
if (!cmpPlayer)
return entities;
let disabledTemplates = cmpPlayer.GetDisabledTemplates();
return entities.filter(ent => !disabledTemplates[ent]);
};
Builder.prototype.GetRange = function()
{
var cmpObstruction = Engine.QueryInterface(this.entity, IID_Obstruction);
var max = 2;
let max = 2;
let cmpObstruction = Engine.QueryInterface(this.entity, IID_Obstruction);
if (cmpObstruction)
max += cmpObstruction.GetUnitRadius();
@ -60,13 +58,11 @@ Builder.prototype.GetRange = function()
/**
* Build/repair the target entity. This should only be called after a successful range check.
* It should be called at a rate of once per second.
* Returns obj with obj.finished==true if this is a repair and it's fully repaired.
*/
Builder.prototype.PerformBuilding = function(target)
{
let rate = ApplyValueModificationsToEntity("Builder/Rate", +this.template.Rate, this.entity);
// If it's a foundation, then build it
let cmpFoundation = Engine.QueryInterface(target, IID_Foundation);
if (cmpFoundation)
{
@ -74,7 +70,6 @@ Builder.prototype.PerformBuilding = function(target)
return;
}
// Otherwise try to repair it
let cmpRepairable = Engine.QueryInterface(target, IID_Repairable);
if (cmpRepairable)
{

View File

@ -0,0 +1,61 @@
Engine.LoadHelperScript("ValueModification.js");
Engine.LoadHelperScript("Player.js");
Engine.LoadComponentScript("interfaces/Builder.js");
Engine.LoadComponentScript("Builder.js");
const builderId = 6;
const playerId = 1;
let cmpBuilder = ConstructComponent(builderId, "Builder", {
"Rate": 1.0,
"Entities": { "_string": "structures/{civ}_barracks structures/{civ}_civil_centre" }
});
TS_ASSERT_UNEVAL_EQUALS(cmpBuilder.GetEntitiesList(), ["structures/{civ}_barracks", "structures/{civ}_civil_centre"]);
AddMock(builderId, IID_Identity, {
"GetCiv": () => "iber"
});
AddMock(builderId, IID_Player, {
"GetPlayerID": () => playerId
});
TS_ASSERT_UNEVAL_EQUALS(cmpBuilder.GetEntitiesList(), ["structures/iber_barracks", "structures/iber_civil_centre"]);
AddMock(SYSTEM_ENTITY, IID_PlayerManager, {
"GetPlayerByID": (id) => playerId
});
AddMock(1, IID_Player, {
"GetDisabledTemplates": () => ({}),
"GetPlayerID": () => playerId
});
AddMock(builderId, IID_Ownership, {
"GetOwner": () => playerId
});
TS_ASSERT_UNEVAL_EQUALS(cmpBuilder.GetEntitiesList(), ["structures/iber_barracks", "structures/iber_civil_centre"]);
AddMock(1, IID_Player, {
"GetDisabledTemplates": () => ({ "structures/athen_barracks": true }),
"GetPlayerID": () => playerId
});
TS_ASSERT_UNEVAL_EQUALS(cmpBuilder.GetEntitiesList(), ["structures/iber_barracks", "structures/iber_civil_centre"]);
AddMock(1, IID_Player, {
"GetDisabledTemplates": () => ({ "structures/iber_barracks": true }),
"GetPlayerID": () => playerId
});
TS_ASSERT_UNEVAL_EQUALS(cmpBuilder.GetEntitiesList(), ["structures/iber_civil_centre"]);
TS_ASSERT_UNEVAL_EQUALS(cmpBuilder.GetRange(), { "max": 2, "min": 0 });
AddMock(builderId, IID_Obstruction, {
"GetUnitRadius": () => 1.0
});
TS_ASSERT_UNEVAL_EQUALS(cmpBuilder.GetRange(), { "max": 3, "min": 0 });