Ticket #3355: t3355_move_ai_difficulties_v1.patch

File t3355_move_ai_difficulties_v1.patch, 8.1 KB (added by elexis, 9 years ago)
  • binaries/data/mods/public/gui/aiconfig/aiconfig.js

     
    1 var g_AIs; // [ {"id": ..., "data": {"name": ..., "description": ..., ...} }, ... ]
    21var g_PlayerSlot;
    32
     3const g_AIDescriptions = [{
     4    "id": "",
     5    "data": {
     6        "name": translateWithContext("ai", "None"),
     7        "description": translate("AI will be disabled for this player.")
     8    }
     9}].concat(g_Settings.AIDescriptions);
     10
    411function init(settings)
    512{
     13    // Remember the player ID that we change the AI settings for
    614    g_PlayerSlot = settings.playerSlot;
    715
    8     translateObjectKeys(settings.ais, ["name", "description"]);
    9     g_AIs = [
    10         {id: "", data: {name: translateWithContext("ai", "None"), description: translate("AI will be disabled for this player.")}}
    11     ].concat(settings.ais);
    12 
    1316    var aiSelection = Engine.GetGUIObjectByName("aiSelection");
    14     aiSelection.list = [ translate(ai.data.name) for each (ai in g_AIs) ];
    15 
    16     var selected = 0;
    17     for (var i = 0; i < g_AIs.length; ++i)
    18     {
    19         if (g_AIs[i].id == settings.id)
    20         {
    21             selected = i;
    22             break;
    23         }
    24     }
    25     aiSelection.selected = selected;
     17    aiSelection.list = g_AIDescriptions.map(ai => ai.data.name);
     18    aiSelection.selected = g_AIDescriptions.findIndex(ai => ai.id == settings.id);
    2619
    2720    var aiDiff = Engine.GetGUIObjectByName("aiDifficulty");
    28     // Translation: AI difficulty level.
    29     aiDiff.list = [translateWithContext("aiDiff", "Sandbox"), translateWithContext("aiDiff", "Very Easy"), translateWithContext("aiDiff", "Easy"), translateWithContext("aiDiff", "Medium"), translateWithContext("aiDiff", "Hard"), translateWithContext("aiDiff", "Very Hard")];
     21    aiDiff.list = prepareForDropdown(g_Settings.AIDifficulties).Title;
    3022    aiDiff.selected = settings.difficulty;
    3123}
    3224
    3325function selectAI(idx)
    3426{
    35     var id = g_AIs[idx].id;
    36     var name = g_AIs[idx].data.name;
    37     var description = g_AIs[idx].data.description;
    38 
    39     Engine.GetGUIObjectByName("aiDescription").caption = description;
     27    Engine.GetGUIObjectByName("aiDescription").caption = g_AIDescriptions[idx].data.description;
    4028}
    4129
    4230function returnAI()
    4331{
    44     var aiSelection = Engine.GetGUIObjectByName("aiSelection");
    45     var idx = aiSelection.selected;
    46     var id = g_AIs[idx].id;
    47     var name = g_AIs[idx].data.name;
     32    var idx = Engine.GetGUIObjectByName("aiSelection").selected;
    4833
    49     var difficulty = Engine.GetGUIObjectByName("aiDifficulty").selected;
    50    
    5134    // Pop the page before calling the callback, so the callback runs
    5235    // in the parent GUI page's context
    53     Engine.PopGuiPageCB({"id": id, "name": name, "difficulty" : difficulty, "playerSlot" : g_PlayerSlot });
     36    Engine.PopGuiPageCB({
     37        "id": g_AIDescriptions[idx].id,
     38        "name": g_AIDescriptions[idx].data.name,
     39        "difficulty": Engine.GetGUIObjectByName("aiDifficulty").selected,
     40        "playerSlot": g_PlayerSlot
     41    });
    5442}
  • binaries/data/mods/public/gui/aiconfig/aiconfig.xml

     
    11<?xml version="1.0" encoding="utf-8"?>
    22
    33<objects>
    44
     5    <script file="gui/common/settings.js"/>
    56    <script file="gui/aiconfig/aiconfig.js"/>
    67
    78    <!-- Add a translucent black background to fade out the menu page -->
    89    <object type="image" sprite="ModernFade"/>
    910
  • binaries/data/mods/public/gui/common/settings.js

     
    77/**
    88 * The maximum number of teams allowed.
    99 */
    1010const g_MaxTeams = 4;
    1111
    12 // The following settings will be loaded here:
    13 // AIDifficulties, Ceasefire, GameSpeeds, GameTypes, MapTypes,
    14 // MapSizes, PlayerDefaults, PopulationCapacity, StartingResources
    15 
     12/**
     13 * Directory containing all editable settings.
     14 */
    1615const g_SettingsDirectory = "simulation/data/settings/";
    1716
    1817/**
    1918 * An object containing all values given by setting name.
    2019 * Used by lobby, gamesetup, session, summary screen and replay menu.
     
    2726 *
    2827 * @returns {Object|undefined}
    2928 */
    3029function loadSettingsValues()
    3130{
     31    // TODO: move PlayerDefaults and MapSizes from functions_utility.js here
    3232    var settings = {
     33        "AIDescriptions": loadAIDescriptions(),
     34        "AIDifficulties": loadAIDifficulties(),
    3335        "Ceasefire": loadCeasefire(),
    3436        "GameSpeeds": loadSettingValuesFile("game_speeds.json"),
    3537        "MapTypes": loadMapTypes(),
    3638        "PopulationCapacities": loadPopulationCapacities(),
    3739        "StartingResources": loadSettingValuesFile("starting_resources.json"),
     
    6668
    6769    return json.Data;
    6870}
    6971
    7072/**
     73 * Loads the descriptions as defined in simulation/ai/.../data.json and loaded by ICmpAIManager.cpp.
     74 *
     75 * @returns {Array}
     76 */
     77function loadAIDescriptions()
     78{
     79    var ais = Engine.GetAIs();
     80    translateObjectKeys(ais, ["name", "description"]);
     81    return ais.sort((a, b) => a.data.name.localeCompare(b.data.name));
     82}
     83
     84/**
     85 * Hardcoded, as modding is not supported without major changes.
     86 * Notice the AI code parses the difficulty level by the index, not by name.
     87 *
     88 * @returns {Array}
     89 */
     90function loadAIDifficulties()
     91{
     92    return [
     93        {
     94            "Name": "sandbox",
     95            "Title": translateWithContext("aiDiff", "Sandbox")
     96        },
     97        {
     98            "Name": "very easy",
     99            "Title": translateWithContext("aiDiff", "Very Easy")
     100        },
     101        {
     102            "Name": "easy",
     103            "Title": translateWithContext("aiDiff", "Easy")
     104        },
     105        {
     106            "Name": "medium",
     107            "Title": translateWithContext("aiDiff", "Medium"),
     108            "Default": true
     109        },
     110        {
     111            "Name": "hard",
     112            "Title": translateWithContext("aiDiff", "Hard")
     113        },
     114        {
     115            "Name": "very hard",
     116            "Title": translateWithContext("aiDiff", "Very Hard")
     117        }
     118    ];
     119}
     120
     121/**
    71122 * Loads available ceasefire settings.
    72123 *
    73124 * @returns {Array|undefined}
    74125 */
    75126function loadCeasefire()
  • binaries/data/mods/public/gui/gamesetup/gamesetup.js

     
    4646    settings: {}
    4747};
    4848
    4949var g_MapSizes = {};
    5050
    51 var g_AIs = [];
    52 
    5351var g_ChatMessages = [];
    5452
    5553// Data caches
    5654var g_MapData = {};
    5755var g_CivData = {};
     
    106104}
    107105
    108106// Called after the map data is loaded and cached
    109107function initMain()
    110108{
    111     // Load AI list
    112     g_AIs = Engine.GetAIs();
    113 
    114     // Sort AIs by displayed name
    115     g_AIs.sort(function (a, b) {
    116         return a.data.name < b.data.name ? -1 : b.data.name < a.data.name ? +1 : 0;
    117     });
    118 
    119109    // Get default player data - remove gaia
    120110    g_DefaultPlayerData = initPlayerDefaults();
    121111    g_DefaultPlayerData.shift();
    122112    for (var i = 0; i < g_DefaultPlayerData.length; ++i)
    123113        g_DefaultPlayerData[i].Civ = "random";
     
    708698
    709699    // Ensure that cheats are enabled in singleplayer
    710700    if (!g_IsNetworked)
    711701        mapSettings.CheatsEnabled = true;
    712702
    713     var aiCodes = [ ai.id for each (ai in g_AIs) ];
    714703    var civListCodes = [ civ.Code for each (civ in g_CivData) if (civ.SelectableInGameSetup !== false) ];
    715704    civListCodes.push("random");
    716705
    717706    var playerData = mapSettings.PlayerData;
    718707
     
    15341523
    15351524    // Only enable start button if we have enough assigned players
    15361525    if (g_IsController)
    15371526        Engine.GetGUIObjectByName("startGame").enabled = (g_AssignedCount > 0);
    15381527
    1539     for each (var ai in g_AIs)
     1528    for (let ai of g_Settings.AIDescriptions)
    15401529    {
    15411530        if (ai.data.hidden)
    15421531        {
    15431532            // If the map uses a hidden AI then don't hide it
    15441533            var usedByMap = false;
     
    16001589            if (g_IsController)
    16011590            {
    16021591                configButton.hidden = false;
    16031592                configButton.onpress = function() {
    16041593                    Engine.PushGuiPage("page_aiconfig.xml", {
    1605                         "ais": g_AIs,
    16061594                        "id": g_GameAttributes.settings.PlayerData[playerSlot].AI,
    16071595                        "difficulty": g_GameAttributes.settings.PlayerData[playerSlot].AIDiff,
    16081596                        "callback": "AIConfigCallback",
    16091597                        "playerSlot": playerSlot // required by the callback function
    16101598                    });