This Trac instance is not used for development anymore!

We migrated our development workflow to git and Gitea.
To test the future redirection, replace trac by ariadne in the page URL.

Ticket #2160: regicide_garrison_option_v2.patch

File regicide_garrison_option_v2.patch, 15.7 KB (added by Sandarac, 8 years ago)

Some fixes after review, and adds tests for Player and GarrisonHolder components.

  • binaries/data/mods/public/gui/gamesetup/gamesetup.js

     
    498498        "ExploreMap": "exploreMap",
    499499        "DisableTreasures": "disableTreasures",
    500500        "LockTeams": "lockTeams",
    501         "LastManStanding" : "lastManStanding",
     501        "LastManStanding": "lastManStanding",
     502        "RegicideGarrison": "regicideGarrison",
    502503        "CheatsEnabled": "enableCheats"
    503504    };
    504505
     
    11831184    {
    11841185        delete g_GameAttributes.settings.WonderDuration;
    11851186        delete g_GameAttributes.settings.LastManStanding;
     1187        delete g_GameAttributes.settings.RegicideGarrison;
    11861188    }
    11871189
    11881190    if (mapSettings.PlayerData)
     
    13801382    setGUIBoolean("revealMap", "revealMapText", !!mapSettings.RevealMap);
    13811383    setGUIBoolean("lockTeams", "lockTeamsText", !!mapSettings.LockTeams);
    13821384    setGUIBoolean("lastManStanding", "lastManStandingText", !!mapSettings.LastManStanding);
     1385    setGUIBoolean("regicideGarrison", "regicideGarrisonText", !!mapSettings.RegicideGarrison);
    13831386    setGUIBoolean("enableRating", "enableRatingText", !!mapSettings.RatingEnabled);
    13841387
    13851388    Engine.GetGUIObjectByName("optionWonderDuration").hidden =
     
    13861389        g_GameAttributes.settings.GameType &&
    13871390        g_GameAttributes.settings.GameType != "wonder";
    13881391    Engine.GetGUIObjectByName("optionLastManStanding").hidden = mapSettings.LockTeams;
     1392    Engine.GetGUIObjectByName("optionRegicideGarrison").hidden =
     1393        g_GameAttributes.settings.GameType &&
     1394        g_GameAttributes.settings.GameType != "regicide";
    13891395
    13901396    Engine.GetGUIObjectByName("cheatWarningText").hidden = !g_IsNetworked || !mapSettings.CheatsEnabled;
    13911397
     
    14031409
    14041410    for (let ctrl of ["victoryCondition", "wonderDuration", "populationCap",
    14051411                      "startingResources", "ceasefire", "revealMap",
    1406                       "exploreMap", "disableTreasures", "lockTeams", "lastManStanding"])
     1412                      "exploreMap", "disableTreasures", "lockTeams",
     1413                      "lastManStanding", "regicideGarrison"])
    14071414        hideControl(ctrl, ctrl + "Text", notScenario);
    14081415
    14091416    Engine.GetGUIObjectByName("civResetButton").hidden = !notScenario;
  • binaries/data/mods/public/gui/gamesetup/gamesetup.xml

     
    335335                    </object>
    336336                </object>
    337337
    338                 <object name="optionPopulationCap" size="14 98 94% 126">
     338                <object name="optionRegicideGarrison" size="14 98 94% 126">
    339339                    <object size="0 0 40% 28" type="text" style="ModernRightLabelText">
     340                        <translatableAttribute id="caption">Hero Garrison:</translatableAttribute>
     341                    </object>
     342                    <object name="regicideGarrisonText" size="40% 0 100% 28" type="text" style="ModernLeftLabelText"/>
     343                    <object name="regicideGarrison" size="40%+10 5 40%+30 100%-5" type="checkbox" style="ModernTickBox" hidden="true" tooltip_style="onscreenToolTip">
     344                        <translatableAttribute id="tooltip">Toggle whether heroes can be garrisoned.</translatableAttribute>
     345                    </object>
     346                </object>
     347
     348                <object name="optionPopulationCap" size="14 128 94% 156">
     349                    <object size="0 0 40% 28" type="text" style="ModernRightLabelText">
    340350                        <translatableAttribute id="caption">Population Cap:</translatableAttribute>
    341351                    </object>
    342352                    <object name="populationCapText" size="40% 0 100% 100%" type="text" style="ModernLeftLabelText"/>
     
    345355                    </object>
    346356                </object>
    347357
    348                 <object name="optionStartingResources" size="14 128 94% 156">
     358                <object name="optionStartingResources" size="14 158 94% 186">
    349359                    <object size="0 0 40% 28" type="text" style="ModernRightLabelText">
    350360                        <translatableAttribute id="caption">Starting Resources:</translatableAttribute>
    351361                    </object>
     
    355365                    </object>
    356366                </object>
    357367
    358                 <object name="optionCeasefire" size="14 158 94% 186">
     368                <object name="optionCeasefire" size="14 188 94% 216">
    359369                    <object size="0 0 40% 28" type="text" style="ModernRightLabelText">
    360370                        <translatableAttribute id="caption">Ceasefire:</translatableAttribute>
    361371                    </object>
     
    440450                    name="hideMoreOptions"
    441451                    type="button"
    442452                    style="StoneButton"
    443                     size="50%-70 428 50%+70 456"
     453                    size="50%-70 458 50%+70 486"
    444454                    tooltip_style="onscreenToolTip"
    445455                    hotkey="cancel"
    446456                >
  • binaries/data/mods/public/maps/scripts/Regicide.js

     
    66
    77Trigger.prototype.InitRegicideGame = function(msg)
    88{
     9    let cmpEndGameManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_EndGameManager);
     10    this.regicideGarrison = cmpEndGameManager.GetGameTypeSettings().regicideGarrison;
     11
    912    let playersCivs = [];
    1013    for (let playerID = 1; playerID < TriggerHelper.GetNumberOfPlayers(); ++playerID)
    1114        playersCivs[playerID] = QueryPlayerIDInterface(playerID).GetCiv();
     
    8992
    9093            hero = hero[0];
    9194
     95            if (!this.regicideGarrison && !isShip)
     96                QueryPlayerIDInterface(playerID).AddDisabledGarrisonClass("Hero");
     97
    9298            if (isShip)
    9399            {
    94100                let cmpUnitAI = Engine.QueryInterface(hero, IID_UnitAI);
     
    102108    return undefined;
    103109};
    104110
     111Trigger.prototype.CheckRegicideHeroGarrison = function(data)
     112{
     113    if (!data.removed.length)
     114        return;
     115
     116    if (this.regicideHeroes.indexOf(data.removed[0]) == -1 || this.regicideGarrison)
     117        return;
     118
     119    QueryOwnerInterface(data.removed[0]).AddDisabledGarrisonClass("Hero");
     120};
     121
    105122let cmpTrigger = Engine.QueryInterface(SYSTEM_ENTITY, IID_Trigger);
    106123cmpTrigger.regicideHeroes = [];
     124cmpTrigger.regicideGarrison = false;
    107125cmpTrigger.DoAfterDelay(0, "InitRegicideGame", {});
    108126cmpTrigger.RegisterTrigger("OnOwnershipChanged", "CheckRegicideDefeat", { "enabled": true });
     127cmpTrigger.RegisterTrigger("OnGarrisonedUnitsChanged", "CheckRegicideHeroGarrison", { "enabled": true });
  • binaries/data/mods/public/simulation/components/GarrisonHolder.js

     
    111111
    112112/**
    113113 * Returns an array of unit classes which can be garrisoned inside this
    114  * particualar entity. Obtained from the entity's template
     114 * particualar entity.
    115115 */
    116116GarrisonHolder.prototype.GetAllowedClasses = function()
    117117{
    118     return this.template.List._string;
     118    let allowedClasses = this.template.List._string.split(/\s+/);
     119    let disabledClasses = QueryOwnerInterface(this.entity).GetDisabledGarrisonClasses();
     120    let classes = [];
     121
     122    for (let className of allowedClasses)
     123    {
     124        for (let disabled of disabledClasses)
     125            className += "+!" + disabled;
     126        classes.push(className);
     127    }
     128
     129    return classes;
    119130};
    120131
    121132/**
     
    200211    if (!cmpIdentity)
    201212        return false;
    202213    var entityClasses = cmpIdentity.GetClassesList();
     214    let cmpPlayer = QueryOwnerInterface(entity);
     215    if (cmpPlayer && entityClasses.some(className => cmpPlayer.GetDisabledGarrisonClasses().indexOf(className) != -1))
     216        return false;
     217
    203218    return MatchesClassList(entityClasses, this.template.List._string);
    204219};
    205220
  • binaries/data/mods/public/simulation/components/interfaces/Trigger.js

     
    1111 * sent from Player component.
    1212 */
    1313Engine.RegisterMessageType("DisabledTechnologiesChanged");
     14
     15/**
     16 * Message of the form {}
     17 * sent from Player component.
     18 */
     19Engine.RegisterMessageType("DisabledGarrisonClassesChanged");
  • binaries/data/mods/public/simulation/components/Player.js

     
    5353    this.disabledTemplates = {};
    5454    this.disabledTechnologies = {};
    5555    this.startingTechnologies = [];
     56    this.disabledGarrisonClasses = [];
    5657};
    5758
    5859Player.prototype.SetPlayerID = function(id)
     
    853854    this.startingTechnologies = techs;
    854855};
    855856
     857Player.prototype.AddDisabledGarrisonClass = function(className)
     858{
     859    if (this.disabledGarrisonClasses.indexOf(className) != -1)
     860        return;
     861    this.disabledGarrisonClasses.push(className);
     862    Engine.BroadcastMessage(MT_DisabledGarrisonClassesChanged, {});
     863};
     864
     865Player.prototype.RemoveDisabledGarrisonClass = function(className)
     866{
     867    this.disabledGarrisonClasses.splice(this.disabledGarrisonClasses.indexOf(className), 1);
     868    Engine.BroadcastMessage(MT_DisabledGarrisonClassesChanged, {});
     869};
     870
     871Player.prototype.SetDisabledGarrisonClasses = function(classes)
     872{
     873    this.disabledGarrisonClasses = classes;
     874    Engine.BroadcastMessage(MT_DisabledGarrisonClassesChanged, {});
     875};
     876
     877Player.prototype.GetDisabledGarrisonClasses = function()
     878{
     879    return this.disabledGarrisonClasses;
     880};
     881
    856882Engine.RegisterComponentType(IID_Player, "Player", Player);
  • binaries/data/mods/public/simulation/components/tests/test_GarrisonHolder.js

     
     1Engine.LoadHelperScript("Player.js");
     2Engine.LoadHelperScript("ValueModification.js");
     3Engine.LoadComponentScript("interfaces/GarrisonHolder.js");
     4Engine.LoadComponentScript("interfaces/Health.js");
     5Engine.LoadComponentScript("interfaces/Trigger.js");
     6Engine.LoadComponentScript("interfaces/Player.js");
     7Engine.LoadComponentScript("GarrisonHolder.js");
     8Engine.LoadComponentScript("Identity.js");
     9Engine.LoadComponentScript("Player.js");
     10Engine.LoadComponentScript("PlayerManager.js");
     11
     12let garrisonEnt = 15;
     13let unitEnt = 33;
     14let entHP = 1000;
     15let playerEnt = 1;
     16
     17AddMock(SYSTEM_ENTITY, IID_PlayerManager, {
     18    "GetPlayerByID": (id) => playerEnt,
     19});
     20
     21AddMock(garrisonEnt, IID_Health, {
     22    "GetHitpoints": () => entHP,
     23    "Decrease": hp => {
     24        entHP = Math.max(entHP - hp, 0);
     25    },
     26});
     27
     28AddMock(playerEnt, IID_Player, {
     29    "IsAlly": (id) =>  id == 1,
     30    "IsEnemy": (id) =>  id != 1,
     31    "GetPlayerID": () =>  1,
     32    "GetDisabledGarrisonClasses": () => [],
     33    "QueryOwnerInterface": (unitEnt) => playerEnt,
     34});
     35
     36AddMock(unitEnt, IID_Identity, {
     37    "GetClassesList": () => ["Hero", "Infantry"],
     38    "GetVisibleClassesList": () => ["Hero", "Infantry"],
     39    "HasClass": () => true,
     40});
     41
     42AddMock(garrisonEnt, IID_Ownership, {
     43    "GetOwner": () => playerEnt,
     44});
     45
     46AddMock(playerEnt, IID_Ownership, {
     47    "GetOwner": () => playerEnt,
     48});
     49
     50let cmpGarrisonHolder = ConstructComponent(garrisonEnt, "GarrisonHolder", {
     51    "LoadingRange": "2.0",
     52    "Max": "20",
     53    "List": { "_string": "Support Infantry Cavalry"},
     54    "entity": garrisonEnt,
     55});
     56
     57let cmpPlayer = ConstructComponent(playerEnt, "Player")
     58
     59TS_ASSERT_EQUALS(cmpGarrisonHolder.GetGarrisonedEntitiesCount(), 0);
     60TS_ASSERT_EQUALS(cmpGarrisonHolder.entities.length, 0);
     61TS_ASSERT_EQUALS(cmpGarrisonHolder.visibleGarrisonPoints.length, 0);
     62TS_ASSERT_EQUALS(+cmpGarrisonHolder.template.Max, 20);
     63TS_ASSERT_EQUALS(cmpGarrisonHolder.GetLoadingRange().max, 2.0);
     64TS_ASSERT_EQUALS(cmpGarrisonHolder.IsGarrisoningAllowed(), true);
     65TS_ASSERT_EQUALS(cmpGarrisonHolder.AllowedToGarrison(unitEnt), true);
     66TS_ASSERT_EQUALS(cmpGarrisonHolder.GetAllowedClasses().length, 3);
     67
     68cmpPlayer.AddDisabledGarrisonClass("Hero");
     69TS_ASSERT(cmpGarrisonHolder.GetAllowedClasses(unitEnt)[0].indexOf("!Hero") != 1);
  • binaries/data/mods/public/simulation/components/tests/test_Player.js

    Property changes on: binaries/data/mods/public/simulation/components/tests/test_GarrisonHolder.js
    ___________________________________________________________________
    Added: svn:eol-style
    ## -0,0 +1 ##
    +native
    \ No newline at end of property
     
    11Engine.LoadHelperScript("ValueModification.js");
    2 Engine.LoadComponentScript("interfaces/EndGameManager.js")
    3 Engine.LoadComponentScript("interfaces/TechnologyManager.js")
    4 Engine.LoadComponentScript("interfaces/Timer.js")
    5 Engine.LoadComponentScript("EndGameManager.js")
     2Engine.LoadComponentScript("interfaces/EndGameManager.js");
     3Engine.LoadComponentScript("interfaces/TechnologyManager.js");
     4Engine.LoadComponentScript("interfaces/Timer.js");
     5Engine.LoadComponentScript("interfaces/Trigger.js");
     6Engine.LoadComponentScript("interfaces/Player.js");
     7Engine.LoadComponentScript("EndGameManager.js");
    68Engine.LoadComponentScript("Player.js");
    7 Engine.LoadComponentScript("Timer.js")
     9Engine.LoadComponentScript("Timer.js");
    810
    911ConstructComponent(SYSTEM_ENTITY, "EndGameManager");
    1012ConstructComponent(SYSTEM_ENTITY, "Timer");
    1113
    12 var cmpPlayer = ConstructComponent(10, "Player");
     14let playerEntity = 1;
     15let cmpPlayer = ConstructComponent(playerEntity, "Player");
    1316
    1417TS_ASSERT_EQUALS(cmpPlayer.GetPopulationCount(), 0);
    1518TS_ASSERT_EQUALS(cmpPlayer.GetPopulationLimit(), 0);
     19TS_ASSERT_EQUALS(cmpPlayer.GetDisabledGarrisonClasses().length, 0);
     20
     21cmpPlayer.AddDisabledGarrisonClass("Champion");
     22TS_ASSERT_EQUALS(cmpPlayer.GetDisabledGarrisonClasses().length, 1);
     23TS_ASSERT_EQUALS(cmpPlayer.GetDisabledGarrisonClasses()[0], "Champion");
     24
     25cmpPlayer.AddDisabledGarrisonClass("Hero");
     26TS_ASSERT_EQUALS(cmpPlayer.GetDisabledGarrisonClasses().length, 2);
     27TS_ASSERT_EQUALS(cmpPlayer.GetDisabledGarrisonClasses()[1], "Hero");
     28
     29cmpPlayer.RemoveDisabledGarrisonClass("Champion");
     30TS_ASSERT_EQUALS(cmpPlayer.GetDisabledGarrisonClasses().length, 1);
     31TS_ASSERT_EQUALS(cmpPlayer.GetDisabledGarrisonClasses()[0], "Hero");
     32TS_ASSERT_EQUALS(cmpPlayer.GetDisabledGarrisonClasses().length, 1);
     33
     34cmpPlayer.SetDisabledGarrisonClasses(["Champion", "Elephant"]);
     35TS_ASSERT_EQUALS(cmpPlayer.GetDisabledGarrisonClasses().indexOf("Hero"), -1);
     36TS_ASSERT_EQUALS(cmpPlayer.GetDisabledGarrisonClasses().length, 2);
     37TS_ASSERT_EQUALS(cmpPlayer.GetDisabledGarrisonClasses()[1], "Elephant");
  • binaries/data/mods/public/simulation/components/Trigger.js

     
    1111    "CinemaPathEnded",
    1212    "CinemaQueueEnded",
    1313    "ConstructionStarted",
     14    "GarrisonedUnitsChanged",
    1415    "Interval",
    1516    "OwnershipChanged",
    1617    "PlayerCommand",
     
    265266    this.CallEvent("PlayerWon", msg);
    266267};
    267268
     269Trigger.prototype.OnGlobalGarrisonedUnitsChanged = function(msg)
     270{
     271    this.CallEvent("GarrisonedUnitsChanged", msg);
     272};
     273
    268274/**
    269275 * Execute a function after a certain delay.
    270276 *
  • binaries/data/mods/public/simulation/helpers/Setup.js

     
    4747    let gameTypeSettings = {};
    4848    if (settings.WonderDuration)
    4949        gameTypeSettings.wonderDuration = settings.WonderDuration * 60 * 1000;
     50    if (settings.GameType == "regicide")
     51        gameTypeSettings.regicideGarrison = settings.RegicideGarrison;
    5052    if (settings.GameType)
    5153        cmpEndGameManager.SetGameType(settings.GameType, gameTypeSettings);
    5254