0ad/binaries/data/mods/public/simulation/components/tests/test_Garrisoning.js
Dunedan 93ce94655d
Use @stylistic/brace-style for eslint
Up to now `eslint-plugin-brace-rules` was used to enforce a common brace
style for JavaScript code. This plugin was however updated the last time
over 9 years ago and will be incompatible with ESLint v10, as that
[removes `context.getSourceCode()`][1], the plugin relies on.

To keep the eslint config working with ESLint v10, this replaces
`eslint-plugin-brace-rules` with the [`@stylistic/brace-style`][2] rule
from `@stylistic/eslint-plugin`, a package we already use.

While `@stylistic/brace-style` doesn't offer an option to format braces
in exactly the same way as before, the "allman" style seems to be the
one closest to the existing code.

[1]: https://eslint.org/blog/2025/11/eslint-v10.0.0-alpha.0-released/#removed-deprecated-rule-context-members
[2]: https://eslint.style/rules/brace-style
2026-01-12 21:33:52 +01:00

142 lines
3.9 KiB
JavaScript

Engine.LoadHelperScript("ValueModification.js");
Engine.LoadHelperScript("Player.js");
Engine.LoadHelperScript("Position.js");
Engine.LoadComponentScript("interfaces/Diplomacy.js");
Engine.LoadComponentScript("interfaces/Garrisonable.js");
Engine.LoadComponentScript("interfaces/GarrisonHolder.js");
Engine.LoadComponentScript("interfaces/Health.js");
Engine.LoadComponentScript("interfaces/ModifiersManager.js");
Engine.LoadComponentScript("interfaces/Timer.js");
Engine.LoadComponentScript("interfaces/UnitAI.js");
Engine.LoadComponentScript("Garrisonable.js");
Engine.LoadComponentScript("GarrisonHolder.js");
const player = 1;
const enemyPlayer = 2;
const friendlyPlayer = 3;
const garrison = 10;
const holder = 11;
const createGarrisonCmp = entity =>
{
AddMock(entity, IID_Identity, {
"GetClassesList": () => ["Ranged"],
"GetSelectionGroupName": () => "mace_infantry_archer_a"
});
AddMock(entity, IID_Ownership, {
"GetOwner": () => player
});
AddMock(entity, IID_Position, {
"GetHeightOffset": () => 0,
"GetPosition": () => new Vector3D(4, 3, 25),
"GetRotation": () => new Vector3D(4, 0, 6),
"JumpTo": (posX, posZ) => {},
"MoveOutOfWorld": () => {},
"SetHeightOffset": height => {},
"SetYRotation": angle => {}
});
return ConstructComponent(entity, "Garrisonable", {
"Size": "1"
});
};
AddMock(holder, IID_Footprint, {
"PickSpawnPointBothPass": entity => new Vector3D(4, 3, 30),
"PickSpawnPoint": entity => new Vector3D(4, 3, 30)
});
AddMock(holder, IID_Ownership, {
"GetOwner": () => player
});
AddMock(player, IID_Diplomacy, {
"IsAlly": id => id != enemyPlayer,
"IsMutualAlly": id => id != enemyPlayer,
});
AddMock(friendlyPlayer, IID_Diplomacy, {
"IsAlly": id => true,
"IsMutualAlly": id => true,
});
AddMock(SYSTEM_ENTITY, IID_Timer, {
"SetInterval": (ent, iid, funcname, time, data) => 1
});
AddMock(SYSTEM_ENTITY, IID_PlayerManager, {
"GetPlayerByID": id => id
});
AddMock(garrison, IID_Identity, {
"GetClassesList": () => ["Ranged"],
"GetSelectionGroupName": () => "mace_infantry_archer_a"
});
AddMock(garrison, IID_Ownership, {
"GetOwner": () => player
});
AddMock(garrison, IID_Position, {
"GetHeightOffset": () => 0,
"GetPosition": () => new Vector3D(4, 3, 25),
"GetRotation": () => new Vector3D(4, 0, 6),
"JumpTo": (posX, posZ) => {},
"MoveOutOfWorld": () => {},
"SetHeightOffset": height => {},
"SetYRotation": angle => {}
});
const cmpGarrisonable = ConstructComponent(garrison, "Garrisonable", {
"Size": "1"
});
const cmpGarrisonHolder = ConstructComponent(holder, "GarrisonHolder", {
"Max": "10",
"List": { "_string": "Ranged" },
"EjectHealth": "0.1",
"EjectClassesOnDestroy": { "_string": "Infantry" },
"BuffHeal": "1",
"LoadingRange": "2.1",
"Pickup": "false"
});
TS_ASSERT(cmpGarrisonable.Garrison(holder));
TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetEntities(), [garrison]);
cmpGarrisonable.OnEntityRenamed({
"entity": garrison,
"newentity": -1
});
TS_ASSERT_EQUALS(cmpGarrisonHolder.GetGarrisonedEntitiesCount(), 0);
TS_ASSERT(cmpGarrisonable.Garrison(holder));
TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetEntities(), [garrison]);
// Can't garrison twice.
TS_ASSERT(!cmpGarrisonable.Garrison(holder));
TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetEntities(), [garrison]);
TS_ASSERT(cmpGarrisonHolder.Unload(garrison));
TS_ASSERT_EQUALS(cmpGarrisonHolder.GetGarrisonedEntitiesCount(), 0);
// Test initGarrison.
const entities = [21, 22, 23, 24];
for (const entity of entities)
createGarrisonCmp(entity);
cmpGarrisonHolder.SetInitGarrison(entities);
cmpGarrisonHolder.OnGlobalInitGame();
TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetEntities(), entities);
// They turned against us!
AddMock(entities[0], IID_Ownership, {
"GetOwner": () => enemyPlayer
});
cmpGarrisonHolder.OnDiplomacyChanged();
TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetGarrisonedEntitiesCount(), entities.length - 1);
TS_ASSERT(cmpGarrisonHolder.UnloadAll());
TS_ASSERT_UNEVAL_EQUALS(cmpGarrisonHolder.GetEntities(), []);