Ticket #4138: test_visibilities.patch

File test_visibilities.patch, 5.7 KB (added by elexis, 8 years ago)
  • binaries/data/mods/public/simulation/components/Visibility.js

    Visibility.prototype.IsActivated = funct  
    5858 * isVisible: true if the entity is in the vision range of a unit, false otherwise
    5959 * isExplored: true if the entity is in explored territory, false otherwise
    6060 */
    6161Visibility.prototype.GetVisibility = function(player, isVisible, isExplored)
    6262{
    63     if (this.preview)
    64     {
    65         // For the owner only, mock the "RetainInFog" behavior
    66         let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
    67         if (cmpOwnership && cmpOwnership.GetOwner() == player && isExplored)
    68             return isVisible ? VIS_VISIBLE : VIS_FOGGED;
     63    if (!this.preview && !this.corpse)
     64        return VIS_VISIBLE;
    6965
    70         // For others, hide the preview
    71         return VIS_HIDDEN;
    72     }
    73     else if (this.corpse)
    74     {
    75         // For the owner only, mock the "RetainInFog" behavior
    76         let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
    77         if (cmpOwnership && cmpOwnership.GetOwner() == player && isExplored)
    78             return isVisible ? VIS_VISIBLE : VIS_FOGGED;
     66    // For the owner, mock the "RetainInFog" behavior
     67    let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
     68    if (cmpOwnership && cmpOwnership.GetOwner() == player && isExplored)
     69        return isVisible ? VIS_VISIBLE : VIS_FOGGED;
    7970
    80         // For others, regular displaying
    81         return isVisible ? VIS_VISIBLE : VIS_HIDDEN;
    82     }
    83 
    84     return VIS_VISIBLE;
     71    // Hide preview from other players
     72    return isVisible && !this.preview ? VIS_VISIBLE : VIS_HIDDEN;
    8573};
    8674
    8775Visibility.prototype.GetRetainInFog = function()
    8876{
    8977    return this.retainInFog;
  • binaries/data/mods/public/simulation/components/tests/test_Attack.js

    function attackComponentTest(defenderCla  
    3232        "IsInWorld": () => true,
    3333        "GetHeightOffset": () => 5
    3434    });
    3535
    3636    AddMock(attacker, IID_Ownership, {
    37         "GetOwner": owner => 1
     37        "GetOwner": () => 1
    3838    });
    3939
    4040    let cmpAttack = ConstructComponent(attacker, "Attack", {
    4141        "Melee" : {
    4242            "Hack": 11,
  • binaries/data/mods/public/simulation/components/tests/test_Visibility.js

     
     1Engine.LoadComponentScript("Visibility.js");
     2
     3const VIS_HIDDEN = 0;
     4const VIS_FOGGED = 1;
     5const VIS_VISIBLE = 2;
     6
     7let maxPlayers = 8;
     8let owner = 3;
     9
     10function testVisbility(template, test)
     11{
     12    ResetState();
     13
     14    AddMock(SYSTEM_ENTITY, IID_RangeManager, {
     15        "ActivateScriptedVisibility": (entity, status) => {}
     16    });
     17
     18    let entityID = 804;
     19
     20    AddMock(entityID, IID_Ownership, {
     21        "GetOwner": () => owner
     22    });
     23
     24    let cmpVisibility = ConstructComponent(entityID, "Visibility", template);
     25
     26    TS_ASSERT_EQUALS(cmpVisibility.GetRetainInFog(), template.RetainInFog == "true");
     27    TS_ASSERT_EQUALS(cmpVisibility.GetAlwaysVisible(), template.AlwaysVisible == "true");
     28
     29    TS_ASSERT_EQUALS(cmpVisibility.IsActivated(), template.Corpse == "true" || template.Preview == "true");
     30    for (let value of [true, false, true])
     31    {
     32        cmpVisibility.SetActivated(value);
     33        TS_ASSERT_EQUALS(cmpVisibility.IsActivated(), value);
     34    }
     35
     36    test(cmpVisibility);
     37}
     38
     39// Test corpse
     40testVisbility({
     41    "RetainInFog": "true",
     42    "AlwaysVisible": "false",
     43    "Corpse": "true",
     44    "Preview": "false"
     45}, cmpVisibility => {
     46
     47    for (let [isVisible, isExplored] of [[true, true], [false, false], [false, true]])
     48        for (let playerID = 0; playerID <= maxPlayers; ++playerID)
     49            TS_ASSERT_EQUALS(
     50                cmpVisibility.GetVisibility(playerID, isVisible, isExplored),
     51                isVisible ? VIS_VISIBLE :
     52                    isExplored && playerID == owner ? VIS_FOGGED: VIS_HIDDEN
     53            );
     54});
     55
     56// Preview should be visible only to the owner
     57testVisbility({
     58    "RetainInFog": "true",
     59    "AlwaysVisible": "false",
     60    "Corpse": "false",
     61    "Preview": "true"
     62}, cmpVisibility => {
     63
     64    for (let [isVisible, isExplored] of [[true, true], [false, false], [false, true]])
     65        for (let playerID = 0; playerID <= maxPlayers; ++playerID)
     66            TS_ASSERT_EQUALS(
     67                cmpVisibility.GetVisibility(playerID, isVisible, isExplored),
     68                playerID == owner ?
     69                        (isVisible ?
     70                            VIS_VISIBLE :
     71                            isExplored ?
     72                                VIS_FOGGED :
     73                                VIS_HIDDEN) :
     74                        VIS_HIDDEN);
     75});
     76
     77// Test regular entity
     78// Notice GetVisibility returns VIS_VISIBLE even if isVisible and isExplored are false
     79// TODO: can [false, false] occur even or should an argument be removed?
     80testVisbility({
     81    "RetainInFog": "true",
     82    "AlwaysVisible": "false",
     83    "Corpse": "false",
     84    "Preview": "false"
     85}, cmpVisibility => {
     86
     87    for (let [isVisible, isExplored] of [[true, true], [false, false], [false, true]])
     88        for (let playerID = 0; playerID <= maxPlayers; ++playerID)
     89            TS_ASSERT_EQUALS(cmpVisibility.GetVisibility(playerID, isVisible, isExplored), VIS_VISIBLE);
     90});
     91
     92// Test GetAlwaysVisible
     93testVisbility({
     94    "RetainInFog": "true",
     95    "AlwaysVisible": "true",
     96    "Corpse": "false",
     97    "Preview": "false"
     98}, function() {});