Ticket #3970: lastmanstanding_v3.patch

File lastmanstanding_v3.patch, 8.4 KB (added by Sandarac, 8 years ago)

Thank you for the tips, this should do it all, but the code could be cleaner

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

     
    494494        "ExploreMap": "exploreMap",
    495495        "DisableTreasures": "disableTreasures",
    496496        "LockTeams": "lockTeams",
     497        "LastManStanding" : "lastManStanding",
    497498        "CheatsEnabled": "enableCheats"
    498499    };
    499500
     
    511512        Engine.GetGUIObjectByName("lockTeams").enabled = !this.checked;
    512513        updateGameAttributes();
    513514    };
     515    Engine.GetGUIObjectByName("lockTeams").onPress = function() {
     516        g_GameAttributes.settings.LockTeams = this.checked;
     517        g_GameAttributes.settings.LastManStanding = !this.checked;
     518        updateGameAttributes();
     519    };
    514520}
    515521
    516522/**
     
    11511157    }
    11521158
    11531159    if (g_GameAttributes.mapType == "scenario")
     1160    {
    11541161        delete g_GameAttributes.settings.WonderDuration;
     1162        delete g_GameAttributes.settings.LastManStanding;
     1163    }
    11551164
    11561165    if (mapSettings.PlayerData)
    11571166        sanitizePlayerData(mapSettings.PlayerData);
     
    13621371    setGUIBoolean("exploreMap", "exploreMapText", !!mapSettings.ExploreMap);
    13631372    setGUIBoolean("revealMap", "revealMapText", !!mapSettings.RevealMap);
    13641373    setGUIBoolean("lockTeams", "lockTeamsText", !!mapSettings.LockTeams);
     1374    setGUIBoolean("lastManStanding", "lastManStandingText", !!mapSettings.LastManStanding);
    13651375    setGUIBoolean("enableRating", "enableRatingText", !!mapSettings.RatingEnabled);
    13661376
    13671377    Engine.GetGUIObjectByName("optionWonderDuration").hidden =
     
    13721382
    13731383    Engine.GetGUIObjectByName("enableCheats").enabled = !mapSettings.RatingEnabled;
    13741384    Engine.GetGUIObjectByName("lockTeams").enabled = !mapSettings.RatingEnabled;
     1385    Engine.GetGUIObjectByName("optionLastManStanding").hidden = mapSettings.LockTeams;
    13751386
    13761387    // Mapsize completely hidden for non-random maps
    13771388    let isRandom = g_GameAttributes.mapType == "random";
     
    13841395
    13851396    for (let ctrl of ["victoryCondition", "wonderDuration", "populationCap",
    13861397                      "startingResources", "ceasefire", "revealMap",
    1387                       "exploreMap", "disableTreasures", "lockTeams"])
     1398                      "exploreMap", "disableTreasures", "lockTeams", "lastManStanding"])
    13881399        hideControl(ctrl, ctrl + "Text", notScenario);
    13891400
    13901401    Engine.GetGUIObjectByName("civResetButton").hidden = !notScenario;
  • binaries/data/mods/public/gui/gamesetup/gamesetup.xml

     
    386386                    </object>
    387387                </object>
    388388
    389                 <object name="optionCheats" size="14 338 94% 366" hidden="true">
     389                <object name="optionLastManStanding" size="14 338 94% 366">
    390390                    <object size="0 0 40% 28" type="text" style="ModernRightLabelText">
     391                        <translatableAttribute id="caption">Last Man Standing:</translatableAttribute>
     392                    </object>
     393                    <object name="lastManStandingText" size="40% 0 100% 28" type="text" style="ModernLeftLabelText"/>
     394                    <object name="lastManStanding" size="40%+10 5 40%+30 100%-5" type="checkbox" style="ModernTickBox" hidden="true" tooltip_style="onscreenToolTip">
     395                        <translatableAttribute id="tooltip">Toggle if only one player can win (disables allied victory).</translatableAttribute>
     396                    </object>
     397                </object>
     398
     399                <object name="optionCheats" size="14 368 94% 396" hidden="true">
     400                    <object size="0 0 40% 28" type="text" style="ModernRightLabelText">
    391401                        <translatableAttribute id="caption">Cheats:</translatableAttribute>
    392402                    </object>
    393403                    <object name="enableCheatsText" size="40% 0 100% 28" type="text" style="ModernLeftLabelText"/>
     
    396406                    </object>
    397407                </object>
    398408
    399                 <object name="optionRating" size="14 368 94% 396" hidden="true">
     409                <object name="optionRating" size="14 398 94% 426" hidden="true">
    400410                    <object size="0 0 40% 28" hidden="false" type="text" style="ModernRightLabelText">
    401411                        <translatableAttribute id="caption">Rated Game:</translatableAttribute>
    402412                    </object>
  • binaries/data/mods/public/simulation/components/EndGameManager.js

     
    1919    this.wonderDuration = 10 * 60 * 1000;
    2020
    2121    // Allied victory means allied players can win if victory conditions are met for each of them
    22     // Would be false for a "last man standing" game (when diplomacy is fully implemented)
     22    // False for a "last man standing" game
    2323    this.alliedVictory = true;
     24    this.disabledAlliedVictoryMessages = {};
    2425};
    2526
    2627EndGameManager.prototype.GetGameType = function()
     
    7576    this.alliedVictory = flag;
    7677};
    7778
     79function DisableAlliedVictoryCheck(msg)
     80{
     81    var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
     82    var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
     83    var cmpEndGameManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_EndGameManager)
     84    var cmpPlayers = [];
     85    var messages = cmpEndGameManager.disabledAlliedVictoryMessages || {};
     86    var allies = [];
     87    var onlyAlliesLeft = true;
     88
     89    var numPlayers = cmpPlayerManager.GetNumPlayers();
     90
     91    for (var i = 1; i < numPlayers; ++i)
     92    {
     93        cmpPlayers[i] = QueryPlayerIDInterface(i);
     94        if (cmpPlayers[i].GetState() != "active" || i == msg.playerId)
     95            continue;
     96
     97        if (!allies.length || cmpPlayers[allies[0]].IsMutualAlly(i))
     98            allies.push(i);
     99        else
     100            onlyAlliesLeft = false;
     101    }
     102    if (!cmpEndGameManager.alliedVictory && onlyAlliesLeft)
     103    {
     104        if (messages.noAlliesLeftMessage)
     105            cmpGuiInterface.DeleteTimeNotification(messages.noAlliesLeftMessage);
     106        messages.noAlliesLeftMessage = cmpGuiInterface.AddTimeNotification({
     107            "message": markForTranslation("Last player remaining wins"),
     108            "translateMessage": true,
     109            }, 10 * 60 * 9000);
     110    }
     111    else if (!cmpEndGameManager.alliedVictory && !onlyAlliesLeft)
     112        if (messages.noAlliesLeftMessage)
     113            cmpGuiInterface.DeleteTimeNotification(messages.noAlliesLeftMessage);
     114
     115    cmpEndGameManager.disabledAlliedVictoryMessages = messages;
     116};
     117
     118EndGameManager.prototype.OnInitGame = function(msg)
     119{
     120    DisableAlliedVictoryCheck(msg);
     121};
     122
     123EndGameManager.prototype.OnGlobalDiplomacyChanged = function(msg)
     124{
     125    DisableAlliedVictoryCheck(msg);
     126};
     127
    78128EndGameManager.prototype.OnGlobalPlayerDefeated = function(msg)
    79129{
    80130    if (msg.skip)
     
    81131        return;
    82132
    83133    var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
     134    var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
    84135    var cmpPlayers = [];
    85136
     137    var messages = this.disabledAlliedVictoryMessages || {};
    86138    var allies = [];
    87139    var onlyAlliesLeft = true;
    88140
     
    100152            onlyAlliesLeft = false;
    101153    }
    102154
     155    if (!this.alliedVictory && onlyAlliesLeft)
     156    {
     157        if (messages.noAlliesLeftMessage)
     158            cmpGuiInterface.DeleteTimeNotification(messages.noAlliesLeftMessage);
     159        messages.noAlliesLeftMessage = cmpGuiInterface.AddTimeNotification({
     160            "message": markForTranslation("Last player remaining wins"),
     161            "translateMessage": true,
     162            }, 10 * 60 * 9000);
     163    }
     164    this.disabledAlliedVictoryMessages = messages;
     165
    103166    // check if there are winners, or the game needs to continue
    104     if (!allies.length || !onlyAlliesLeft || !this.alliedVictory)
    105         return;
     167    if (!allies.length || !onlyAlliesLeft ||
     168        !this.alliedVictory && cmpPlayers.filter(pData => pData.GetState() == "active").length != 1)
     169        return;
     170    if (messages.noAlliesLeftMessage)
     171        cmpGuiInterface.DeleteTimeNotification(messages.noAlliesLeftMessage);
    106172
    107173    for (var p of allies)
    108174        cmpPlayers[p].SetState("won");
  • binaries/data/mods/public/simulation/helpers/Setup.js

     
    5151        cmpEndGameManager.SetGameType(settings.GameType);
    5252    if (settings.WonderDuration)
    5353        cmpEndGameManager.SetWonderDuration(settings.WonderDuration * 60 * 1000);
     54    if (settings.LastManStanding)
     55        cmpEndGameManager.SetAlliedVictory(false);
    5456
    5557    if (settings.Garrison)
    5658    {