Ticket #3355: t3355_move_ai_difficulties_v1.patch
File t3355_move_ai_difficulties_v1.patch, 8.1 KB (added by , 9 years ago) |
---|
-
binaries/data/mods/public/gui/aiconfig/aiconfig.js
1 var g_AIs; // [ {"id": ..., "data": {"name": ..., "description": ..., ...} }, ... ]2 1 var g_PlayerSlot; 3 2 3 const 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 4 11 function init(settings) 5 12 { 13 // Remember the player ID that we change the AI settings for 6 14 g_PlayerSlot = settings.playerSlot; 7 15 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 13 16 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); 26 19 27 20 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; 30 22 aiDiff.selected = settings.difficulty; 31 23 } 32 24 33 25 function selectAI(idx) 34 26 { 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; 40 28 } 41 29 42 30 function returnAI() 43 31 { 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; 48 33 49 var difficulty = Engine.GetGUIObjectByName("aiDifficulty").selected;50 51 34 // Pop the page before calling the callback, so the callback runs 52 35 // 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 }); 54 42 } -
binaries/data/mods/public/gui/aiconfig/aiconfig.xml
1 1 <?xml version="1.0" encoding="utf-8"?> 2 2 3 3 <objects> 4 4 5 <script file="gui/common/settings.js"/> 5 6 <script file="gui/aiconfig/aiconfig.js"/> 6 7 7 8 <!-- Add a translucent black background to fade out the menu page --> 8 9 <object type="image" sprite="ModernFade"/> 9 10 -
binaries/data/mods/public/gui/common/settings.js
7 7 /** 8 8 * The maximum number of teams allowed. 9 9 */ 10 10 const g_MaxTeams = 4; 11 11 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 */ 16 15 const g_SettingsDirectory = "simulation/data/settings/"; 17 16 18 17 /** 19 18 * An object containing all values given by setting name. 20 19 * Used by lobby, gamesetup, session, summary screen and replay menu. … … 27 26 * 28 27 * @returns {Object|undefined} 29 28 */ 30 29 function loadSettingsValues() 31 30 { 31 // TODO: move PlayerDefaults and MapSizes from functions_utility.js here 32 32 var settings = { 33 "AIDescriptions": loadAIDescriptions(), 34 "AIDifficulties": loadAIDifficulties(), 33 35 "Ceasefire": loadCeasefire(), 34 36 "GameSpeeds": loadSettingValuesFile("game_speeds.json"), 35 37 "MapTypes": loadMapTypes(), 36 38 "PopulationCapacities": loadPopulationCapacities(), 37 39 "StartingResources": loadSettingValuesFile("starting_resources.json"), … … 66 68 67 69 return json.Data; 68 70 } 69 71 70 72 /** 73 * Loads the descriptions as defined in simulation/ai/.../data.json and loaded by ICmpAIManager.cpp. 74 * 75 * @returns {Array} 76 */ 77 function 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 */ 90 function 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 /** 71 122 * Loads available ceasefire settings. 72 123 * 73 124 * @returns {Array|undefined} 74 125 */ 75 126 function loadCeasefire() -
binaries/data/mods/public/gui/gamesetup/gamesetup.js
46 46 settings: {} 47 47 }; 48 48 49 49 var g_MapSizes = {}; 50 50 51 var g_AIs = [];52 53 51 var g_ChatMessages = []; 54 52 55 53 // Data caches 56 54 var g_MapData = {}; 57 55 var g_CivData = {}; … … 106 104 } 107 105 108 106 // Called after the map data is loaded and cached 109 107 function initMain() 110 108 { 111 // Load AI list112 g_AIs = Engine.GetAIs();113 114 // Sort AIs by displayed name115 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 119 109 // Get default player data - remove gaia 120 110 g_DefaultPlayerData = initPlayerDefaults(); 121 111 g_DefaultPlayerData.shift(); 122 112 for (var i = 0; i < g_DefaultPlayerData.length; ++i) 123 113 g_DefaultPlayerData[i].Civ = "random"; … … 708 698 709 699 // Ensure that cheats are enabled in singleplayer 710 700 if (!g_IsNetworked) 711 701 mapSettings.CheatsEnabled = true; 712 702 713 var aiCodes = [ ai.id for each (ai in g_AIs) ];714 703 var civListCodes = [ civ.Code for each (civ in g_CivData) if (civ.SelectableInGameSetup !== false) ]; 715 704 civListCodes.push("random"); 716 705 717 706 var playerData = mapSettings.PlayerData; 718 707 … … 1534 1523 1535 1524 // Only enable start button if we have enough assigned players 1536 1525 if (g_IsController) 1537 1526 Engine.GetGUIObjectByName("startGame").enabled = (g_AssignedCount > 0); 1538 1527 1539 for each (var ai in g_AIs)1528 for (let ai of g_Settings.AIDescriptions) 1540 1529 { 1541 1530 if (ai.data.hidden) 1542 1531 { 1543 1532 // If the map uses a hidden AI then don't hide it 1544 1533 var usedByMap = false; … … 1600 1589 if (g_IsController) 1601 1590 { 1602 1591 configButton.hidden = false; 1603 1592 configButton.onpress = function() { 1604 1593 Engine.PushGuiPage("page_aiconfig.xml", { 1605 "ais": g_AIs,1606 1594 "id": g_GameAttributes.settings.PlayerData[playerSlot].AI, 1607 1595 "difficulty": g_GameAttributes.settings.PlayerData[playerSlot].AIDiff, 1608 1596 "callback": "AIConfigCallback", 1609 1597 "playerSlot": playerSlot // required by the callback function 1610 1598 });