Ticket #4121: fixFields.diff

File fixFields.diff, 10.3 KB (added by sanderd17, 8 years ago)
  • binaries/data/mods/public/simulation/components/Foundation.js

     
    251251            }
    252252
    253253            var cmpFoundationPosition = Engine.QueryInterface(this.entity, IID_Position);
    254             var pos = cmpFoundationPosition.GetPosition();
     254            var pos = cmpFoundationPosition.GetPosition2D();
    255255            var rot = cmpFoundationPosition.GetRotation();
    256256            cmpPreviewPosition.SetYRotation(rot.y);
    257257            cmpPreviewPosition.SetXZRotation(rot.x, rot.z);
    258             cmpPreviewPosition.JumpTo(pos.x, pos.z);
     258            cmpPreviewPosition.JumpTo(pos.x, pos.y);
    259259        }
    260260
    261261        this.committed = true;
     
    333333        if (cmpTerritoryDecay && cmpTerritoryDecay.HasTerritoryOwnership())
    334334        {
    335335            let cmpTerritoryManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TerritoryManager);
    336             owner = cmpTerritoryManager.GetOwner(pos.x, pos.z);
     336            owner = cmpTerritoryManager.GetOwner(pos.x, pos.y);
    337337        }
    338338        else
    339339        {
  • binaries/data/mods/public/simulation/components/tests/test_Foundation.js

     
    88Engine.LoadComponentScript("interfaces/Trigger.js");
    99Engine.LoadComponentScript("Foundation.js");
    1010
    11 AddMock(SYSTEM_ENTITY, IID_Trigger, {
    12     "CallEvent": () => {},
    13 });
    14 
    1511let player = 1;
    1612let playerEnt = 3;
     13let foundationEnt = 20;
     14let previewEnt = 21;
     15let newEnt = 22;
    1716
    18 AddMock(SYSTEM_ENTITY, IID_PlayerManager, {
    19     "GetPlayerByID": () => playerEnt,
    20 });
     17function testFoundation(...mocks)
     18{
     19    ResetState();
    2120
    22 AddMock(playerEnt, IID_StatisticsTracker, {
    23     "IncreaseConstructedBuildingsCounter": ent => {
    24         TS_ASSERT_EQUALS(ent, newEnt);
    25     },
    26 });
     21    AddMock(SYSTEM_ENTITY, IID_Trigger, {
     22        "CallEvent": () => {},
     23    });
    2724
    28 let foundationEnt = 20;
    29 let newEnt = 21;
    30 let finalTemplate = "structures/athen_civil_centre.xml";
    31 let foundationHP = 1;
    32 let maxHP = 100;
     25    AddMock(SYSTEM_ENTITY, IID_PlayerManager, {
     26        "GetPlayerByID": () => playerEnt,
     27    });
    3328
    34 AddMock(foundationEnt, IID_Cost, {
    35     "GetBuildTime": () => 50,
    36     "GetResourceCosts": () => ({ "wood": 100 }),
    37 });
     29    AddMock(SYSTEM_ENTITY, IID_TerritoryManager, {
     30        "GetOwner": (x, y) => {
     31            TS_ASSERT_EQUALS(x, pos.x);
     32            TS_ASSERT_EQUALS(y, pos.y);
     33            return player;
     34        },
     35    });
    3836
    39 AddMock(foundationEnt, IID_Health, {
    40     "GetHitpoints": () => foundationHP,
    41     "GetMaxHitpoints": () => maxHP,
    42     "Increase": hp => {
    43         foundationHP = Math.min(foundationHP + hp, maxHP);
    44         cmpFoundation.OnHealthChanged();
    45     },
    46 });
     37    Engine.RegisterGlobal("PlaySound", (name, source) => {
     38        TS_ASSERT_EQUALS(name, "constructed");
     39        TS_ASSERT_EQUALS(source, newEnt);
     40    });
     41    Engine.RegisterGlobal("MT_EntityRenamed", "entityRenamed");
    4742
    48 AddMock(foundationEnt, IID_Obstruction, {
    49     "GetBlockMovementFlag": () => true,
    50     "GetUnitCollisions": () => [],
    51     "SetDisableBlockMovementPathfinding": () => {},
    52 });
     43    let finalTemplate = "structures/athen_civil_centre.xml";
     44    let foundationHP = 1;
     45    let maxHP = 100;
     46    let rot = new Vector3D(1, 2, 3);
     47    let pos = new Vector2D(4, 5);
    5348
    54 AddMock(foundationEnt, IID_Ownership, {
    55     "GetOwner": () => player,
    56 });
     49    AddMock(foundationEnt, IID_Cost, {
     50        "GetBuildTime": () => 50,
     51        "GetResourceCosts": () => ({ "wood": 100 }),
     52    });
    5753
    58 AddMock(foundationEnt, IID_Position, {
    59     "GetPosition2D": () => new Vector2D(1, 2),
    60     "GetRotation": () => new Vector3D(1, 2, 3),
    61     "IsInWorld": () => true,
    62 });
     54    AddMock(foundationEnt, IID_Health, {
     55        "GetHitpoints": () => foundationHP,
     56        "GetMaxHitpoints": () => maxHP,
     57        "Increase": hp => {
     58            foundationHP = Math.min(foundationHP + hp, maxHP);
     59            cmpFoundation.OnHealthChanged();
     60        },
     61    });
    6362
    64 Engine.AddEntity = function(template)
    65 {
    66     AddMock(newEnt, IID_Position, {
    67         "JumpTo": (x, y) => { TS_ASSERT_EQUALS(x, 1); TS_ASSERT_EQUALS(y, 2); },
    68         "SetYRotation": r => { TS_ASSERT_EQUALS(r, 2); },
     63    AddMock(foundationEnt, IID_Obstruction, {
     64        "GetBlockMovementFlag": () => true,
     65        "GetUnitCollisions": () => [],
     66        "SetDisableBlockMovementPathfinding": () => {},
     67    });
     68
     69    AddMock(foundationEnt, IID_Ownership, {
     70        "GetOwner": () => player,
     71    });
     72
     73    AddMock(foundationEnt, IID_Position, {
     74        "GetPosition2D": () => pos,
     75        "GetRotation": () => rot,
     76        "SetConstructionProgress": () => {},
     77        "IsInWorld": () => true,
     78    });
     79
     80    AddMock(previewEnt, IID_Ownership, {
     81        "SetOwner": owner => { TS_ASSERT_EQUALS(owner, player); },
     82    });
     83
     84    AddMock(previewEnt, IID_Position, {
     85        "JumpTo": (x, y) => {
     86            TS_ASSERT_EQUALS(x, pos.x);
     87            TS_ASSERT_EQUALS(y, pos.y);
     88        },
     89        "SetConstructionProgress": p => {},
     90        "SetYRotation": r => { TS_ASSERT_EQUALS(r, rot.y); },
    6991        "SetXZRotation": (rx, rz) => {
    70             TS_ASSERT_EQUALS(rx, 1);
    71             TS_ASSERT_EQUALS(rz, 3);
     92            TS_ASSERT_EQUALS(rx, rot.x);
     93            TS_ASSERT_EQUALS(rz, rot.z);
    7294        },
    7395    });
     96
    7497    AddMock(newEnt, IID_Ownership, {
    7598        "SetOwner": owner => { TS_ASSERT_EQUALS(owner, player); },
    7699    });
    77     return newEnt;
    78 };
    79100
    80 function PlaySound(name, source)
    81 {
    82     TS_ASSERT_EQUALS(name, "constructed");
    83     TS_ASSERT_EQUALS(source, newEnt);
    84 };
    85 Engine.RegisterGlobal("PlaySound", PlaySound);
    86 Engine.RegisterGlobal("MT_EntityRenamed", "entityRenamed");
     101    AddMock(newEnt, IID_Position, {
     102        "JumpTo": (x, y) => {
     103            TS_ASSERT_EQUALS(x, pos.x);
     104            TS_ASSERT_EQUALS(y, pos.y);
     105        },
     106        "SetYRotation": r => { TS_ASSERT_EQUALS(r, rot.y); },
     107        "SetXZRotation": (rx, rz) => {
     108            TS_ASSERT_EQUALS(rx, rot.x);
     109            TS_ASSERT_EQUALS(rz, rot.z);
     110        },
     111    });
    87112
    88 let cmpFoundation = ConstructComponent(foundationEnt, "Foundation", {});
     113    for (let mock of mocks)
     114        AddMock(...mock);
    89115
    90 // INITIALISE
    91 cmpFoundation.InitialiseConstruction(player, finalTemplate);
     116    // INITIALISE
     117    Engine.AddEntity = function(template) {
     118        TS_ASSERT_EQUALS(template, "construction|" + finalTemplate);
     119        return previewEnt;
     120    };
     121    let cmpFoundation = ConstructComponent(foundationEnt, "Foundation", {});
     122    cmpFoundation.InitialiseConstruction(player, finalTemplate);
    92123
    93 TS_ASSERT_EQUALS(cmpFoundation.owner, player);
    94 TS_ASSERT_EQUALS(cmpFoundation.finalTemplateName, finalTemplate);
    95 TS_ASSERT_EQUALS(cmpFoundation.maxProgress, 0);
    96 TS_ASSERT_EQUALS(cmpFoundation.initialised, true);
     124    TS_ASSERT_EQUALS(cmpFoundation.owner, player);
     125    TS_ASSERT_EQUALS(cmpFoundation.finalTemplateName, finalTemplate);
     126    TS_ASSERT_EQUALS(cmpFoundation.maxProgress, 0);
     127    TS_ASSERT_EQUALS(cmpFoundation.initialised, true);
    97128
    98 // BUILDER COUNT
    99 let twoBuilderMultiplier = Math.pow(2, cmpFoundation.buildTimePenalty) / 2;
    100 TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 0);
    101 cmpFoundation.AddBuilder(10);
    102 TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 1);
    103 TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, 1);
    104 cmpFoundation.AddBuilder(11);
    105 TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 2);
    106 TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, twoBuilderMultiplier);
    107 cmpFoundation.AddBuilder(11);
    108 TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 2);
    109 TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, twoBuilderMultiplier);
    110 cmpFoundation.RemoveBuilder(11);
    111 TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 1);
    112 TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, 1);
    113 cmpFoundation.RemoveBuilder(11);
    114 TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 1);
    115 TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, 1);
    116 // with cmpVisual
    117 AddMock(foundationEnt, IID_Visual, {
     129    // BUILDER COUNT
     130    let twoBuilderMultiplier = Math.pow(2, cmpFoundation.buildTimePenalty) / 2;
     131    TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 0);
     132    cmpFoundation.AddBuilder(10);
     133    TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 1);
     134    TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, 1);
     135    cmpFoundation.AddBuilder(11);
     136    TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 2);
     137    TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, twoBuilderMultiplier);
     138    cmpFoundation.AddBuilder(11);
     139    TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 2);
     140    TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, twoBuilderMultiplier);
     141    cmpFoundation.RemoveBuilder(11);
     142    TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 1);
     143    TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, 1);
     144    cmpFoundation.RemoveBuilder(11);
     145    TS_ASSERT_EQUALS(cmpFoundation.GetNumBuilders(), 1);
     146    TS_ASSERT_EQUALS(cmpFoundation.buildMultiplier, 1);
     147
     148    // COMMIT FOUNDATION
     149    TS_ASSERT_EQUALS(cmpFoundation.committed, false);
     150    let work = 5;
     151    cmpFoundation.Build(10, work);
     152    TS_ASSERT_EQUALS(cmpFoundation.committed, true);
     153    TS_ASSERT_EQUALS(foundationHP, 1 + work * cmpFoundation.GetBuildRate() * cmpFoundation.buildMultiplier);
     154    TS_ASSERT_EQUALS(cmpFoundation.maxProgress, foundationHP / maxHP);
     155
     156    // FINISH CONSTRUCTION
     157    Engine.AddEntity = function(template) {
     158        TS_ASSERT_EQUALS(template, finalTemplate);
     159        return newEnt;
     160    };
     161    cmpFoundation.Build(10, 1000);
     162    TS_ASSERT_EQUALS(cmpFoundation.maxProgress, 1);
     163    TS_ASSERT_EQUALS(foundationHP, maxHP);
     164}
     165
     166testFoundation();
     167
     168testFoundation([foundationEnt, IID_Visual, {
    118169    "SetVariable": (key, num) => {
    119170        TS_ASSERT_EQUALS(key, "numbuilders");
    120         TS_ASSERT_EQUALS(num, 2);
     171        TS_ASSERT(num == 1 || num == 2);
    121172    },
    122 });
    123 cmpFoundation.AddBuilder(11);
    124 DeleteMock(foundationEnt, IID_Visual);
    125 cmpFoundation.RemoveBuilder(11);
     173    "SelectAnimation": () => {},
     174    "HasConstructionPreview": () => true,
     175}]);
    126176
    127 // COMMIT FOUNDATION
    128 TS_ASSERT_EQUALS(cmpFoundation.committed, false);
    129 let work = 5;
    130 cmpFoundation.Build(10, work);
    131 TS_ASSERT_EQUALS(cmpFoundation.committed, true);
    132 TS_ASSERT_EQUALS(foundationHP, 1 + work * cmpFoundation.GetBuildRate() * cmpFoundation.buildMultiplier);
    133 TS_ASSERT_EQUALS(cmpFoundation.maxProgress, foundationHP / maxHP);
     177testFoundation([newEnt, IID_TerritoryDecay, {
     178    "HasTerritoryOwnership": () => true,
     179}]);
    134180
    135 // FINISH CONSTRUCTION
    136 cmpFoundation.Build(10, 1000);
    137 TS_ASSERT_EQUALS(cmpFoundation.maxProgress, 1);
    138 TS_ASSERT_EQUALS(foundationHP, maxHP);
     181testFoundation([playerEnt, IID_StatisticsTracker, {
     182    "IncreaseConstructedBuildingsCounter": ent => {
     183        TS_ASSERT_EQUALS(ent, newEnt);
     184    },
     185}]);
    139186