From b120404141ee572d69a89e1312ae3bb282a36cca Mon Sep 17 00:00:00 2001 From: elexis Date: Thu, 21 Apr 2016 20:07:39 +0000 Subject: [PATCH] Reveal AI settings in the gamesetup to all players. Based on patch by Imarok, fixes #3911. This was SVN commit r18078. --- .../data/mods/public/gui/aiconfig/aiconfig.js | 19 +++++-- .../mods/public/gui/aiconfig/aiconfig.xml | 6 ++- .../mods/public/gui/gamesetup/gamesetup.js | 49 ++++++++++++++----- 3 files changed, 56 insertions(+), 18 deletions(-) diff --git a/binaries/data/mods/public/gui/aiconfig/aiconfig.js b/binaries/data/mods/public/gui/aiconfig/aiconfig.js index 079e00605d..153d341023 100644 --- a/binaries/data/mods/public/gui/aiconfig/aiconfig.js +++ b/binaries/data/mods/public/gui/aiconfig/aiconfig.js @@ -13,13 +13,23 @@ function init(settings) // Remember the player ID that we change the AI settings for g_PlayerSlot = settings.playerSlot; - var aiSelection = Engine.GetGUIObjectByName("aiSelection"); + let aiSelection = Engine.GetGUIObjectByName("aiSelection"); aiSelection.list = g_AIDescriptions.map(ai => ai.data.name); aiSelection.selected = g_AIDescriptions.findIndex(ai => ai.id == settings.id); + aiSelection.hidden = !settings.isController; - var aiDiff = Engine.GetGUIObjectByName("aiDifficulty"); + let aiSelectionText = Engine.GetGUIObjectByName("aiSelectionText"); + aiSelectionText.caption = aiSelection.list[aiSelection.selected]; + aiSelectionText.hidden = settings.isController; + + let aiDiff = Engine.GetGUIObjectByName("aiDifficulty"); aiDiff.list = prepareForDropdown(g_Settings.AIDifficulties).Title; aiDiff.selected = settings.difficulty; + aiDiff.hidden = !settings.isController; + + let aiDiffText = Engine.GetGUIObjectByName("aiDifficultyText"); + aiDiffText.caption = aiDiff.list[aiDiff.selected]; + aiDiffText.hidden = settings.isController; } function selectAI(idx) @@ -27,13 +37,14 @@ function selectAI(idx) Engine.GetGUIObjectByName("aiDescription").caption = g_AIDescriptions[idx].data.description; } -function returnAI() +function returnAI(save = true) { - var idx = Engine.GetGUIObjectByName("aiSelection").selected; + let idx = Engine.GetGUIObjectByName("aiSelection").selected; // Pop the page before calling the callback, so the callback runs // in the parent GUI page's context Engine.PopGuiPageCB({ + "save": save, "id": g_AIDescriptions[idx].id, "name": g_AIDescriptions[idx].data.name, "difficulty": Engine.GetGUIObjectByName("aiDifficulty").selected, diff --git a/binaries/data/mods/public/gui/aiconfig/aiconfig.xml b/binaries/data/mods/public/gui/aiconfig/aiconfig.xml index c5fbb9d78f..9d0c194bce 100644 --- a/binaries/data/mods/public/gui/aiconfig/aiconfig.xml +++ b/binaries/data/mods/public/gui/aiconfig/aiconfig.xml @@ -24,6 +24,7 @@ selectAI(this.selected); + AI Difficulty: @@ -31,18 +32,19 @@ + Cancel - Engine.PopGuiPage(); + returnAI(false); OK - returnAI(); + returnAI(true); diff --git a/binaries/data/mods/public/gui/gamesetup/gamesetup.js b/binaries/data/mods/public/gui/gamesetup/gamesetup.js index 8e83693b0a..fbf47d5459 100644 --- a/binaries/data/mods/public/gui/gamesetup/gamesetup.js +++ b/binaries/data/mods/public/gui/gamesetup/gamesetup.js @@ -192,6 +192,11 @@ var g_LoadingState = 0; // 0 = not started, 1 = loading, 2 = loaded */ var g_LastGameStanza; +/** + * Remembers if the current player viewed the AI settings of some playerslot. + */ +var g_LastViewedAIPlayer = -1; + /** * Initializes some globals without touching the GUI. * @@ -1420,6 +1425,13 @@ function updateGUIObjects() // We should have everyone confirm that the new settings are acceptable. resetReadyData(); + + // Refresh AI config page + if (g_LastViewedAIPlayer != -1) + { + Engine.PopGuiPage(); + openAIConfig(g_LastViewedAIPlayer); + } } /** @@ -1480,8 +1492,29 @@ function updateGameAttributes() updateGUIObjects(); } +function openAIConfig(playerSlot) +{ + g_LastViewedAIPlayer = playerSlot; + + Engine.PushGuiPage("page_aiconfig.xml", { + "callback": "AIConfigCallback", + "isController": g_IsController, + "playerSlot": playerSlot, + "id": g_GameAttributes.settings.PlayerData[playerSlot].AI, + "difficulty": g_GameAttributes.settings.PlayerData[playerSlot].AIDiff + }); +} + +/** + * Called after closing the dialog. + */ function AIConfigCallback(ai) { + g_LastViewedAIPlayer = -1; + + if (!ai.save || !g_IsController) + return; + g_GameAttributes.settings.PlayerData[ai.playerSlot].AI = ai.id; g_GameAttributes.settings.PlayerData[ai.playerSlot].AIDiff = ai.difficulty; @@ -1570,18 +1603,10 @@ function updatePlayerList() selection = noAssignment; // Since no human is assigned, show the AI config button - if (g_IsController) - { - configButton.hidden = false; - configButton.onpress = function() { - Engine.PushGuiPage("page_aiconfig.xml", { - "id": g_GameAttributes.settings.PlayerData[playerSlot].AI, - "difficulty": g_GameAttributes.settings.PlayerData[playerSlot].AIDiff, - "callback": "AIConfigCallback", - "playerSlot": playerSlot // required by the callback function - }); - }; - } + configButton.hidden = false; + configButton.onpress = function() { + openAIConfig(playerSlot); + }; } // There was a human, so make sure we don't have any AI left // over in their slot, if we're in charge of the attributes