Move the AI data to the new simulation settings system. Patch by elexis. Refs #3355.

This was SVN commit r17034.
This commit is contained in:
Nicolas Auvray 2015-09-18 16:36:52 +00:00
parent 4655196d6f
commit 4da5d571fc
4 changed files with 77 additions and 49 deletions

View File

@ -1,54 +1,42 @@
var g_AIs; // [ {"id": ..., "data": {"name": ..., "description": ..., ...} }, ... ]
var g_PlayerSlot;
const g_AIDescriptions = [{
"id": "",
"data": {
"name": translateWithContext("ai", "None"),
"description": translate("AI will be disabled for this player.")
}
}].concat(g_Settings.AIDescriptions);
function init(settings)
{
// Remember the player ID that we change the AI settings for
g_PlayerSlot = settings.playerSlot;
translateObjectKeys(settings.ais, ["name", "description"]);
g_AIs = [
{id: "", data: {name: translateWithContext("ai", "None"), description: translate("AI will be disabled for this player.")}}
].concat(settings.ais);
var aiSelection = Engine.GetGUIObjectByName("aiSelection");
aiSelection.list = [ translate(ai.data.name) for each (ai in g_AIs) ];
var selected = 0;
for (var i = 0; i < g_AIs.length; ++i)
{
if (g_AIs[i].id == settings.id)
{
selected = i;
break;
}
}
aiSelection.selected = selected;
aiSelection.list = g_AIDescriptions.map(ai => ai.data.name);
aiSelection.selected = g_AIDescriptions.findIndex(ai => ai.id == settings.id);
var aiDiff = Engine.GetGUIObjectByName("aiDifficulty");
// Translation: AI difficulty level.
aiDiff.list = [translateWithContext("aiDiff", "Sandbox"), translateWithContext("aiDiff", "Very Easy"), translateWithContext("aiDiff", "Easy"), translateWithContext("aiDiff", "Medium"), translateWithContext("aiDiff", "Hard"), translateWithContext("aiDiff", "Very Hard")];
aiDiff.list = prepareForDropdown(g_Settings.AIDifficulties).Title;
aiDiff.selected = settings.difficulty;
}
function selectAI(idx)
{
var id = g_AIs[idx].id;
var name = g_AIs[idx].data.name;
var description = g_AIs[idx].data.description;
Engine.GetGUIObjectByName("aiDescription").caption = description;
Engine.GetGUIObjectByName("aiDescription").caption = g_AIDescriptions[idx].data.description;
}
function returnAI()
{
var aiSelection = Engine.GetGUIObjectByName("aiSelection");
var idx = aiSelection.selected;
var id = g_AIs[idx].id;
var name = g_AIs[idx].data.name;
var idx = Engine.GetGUIObjectByName("aiSelection").selected;
var difficulty = Engine.GetGUIObjectByName("aiDifficulty").selected;
// Pop the page before calling the callback, so the callback runs
// in the parent GUI page's context
Engine.PopGuiPageCB({"id": id, "name": name, "difficulty" : difficulty, "playerSlot" : g_PlayerSlot });
Engine.PopGuiPageCB({
"id": g_AIDescriptions[idx].id,
"name": g_AIDescriptions[idx].data.name,
"difficulty": Engine.GetGUIObjectByName("aiDifficulty").selected,
"playerSlot": g_PlayerSlot
});
}

View File

@ -2,6 +2,7 @@
<objects>
<script file="gui/common/settings.js"/>
<script file="gui/aiconfig/aiconfig.js"/>
<!-- Add a translucent black background to fade out the menu page -->

View File

@ -9,10 +9,9 @@ const g_MaxPlayers = 8;
*/
const g_MaxTeams = 4;
// The following settings will be loaded here:
// AIDifficulties, Ceasefire, GameSpeeds, GameTypes, MapTypes,
// MapSizes, PlayerDefaults, PopulationCapacity, StartingResources
/**
* Directory containing all editable settings.
*/
const g_SettingsDirectory = "simulation/data/settings/";
/**
@ -29,7 +28,10 @@ const g_Settings = loadSettingsValues();
*/
function loadSettingsValues()
{
// TODO: move PlayerDefaults and MapSizes from functions_utility.js here
var settings = {
"AIDescriptions": loadAIDescriptions(),
"AIDifficulties": loadAIDifficulties(),
"Ceasefire": loadCeasefire(),
"GameSpeeds": loadSettingValuesFile("game_speeds.json"),
"MapTypes": loadMapTypes(),
@ -67,6 +69,55 @@ function loadSettingValuesFile(filename)
return json.Data;
}
/**
* Loads the descriptions as defined in simulation/ai/.../data.json and loaded by ICmpAIManager.cpp.
*
* @returns {Array}
*/
function loadAIDescriptions()
{
var ais = Engine.GetAIs();
translateObjectKeys(ais, ["name", "description"]);
return ais.sort((a, b) => a.data.name.localeCompare(b.data.name));
}
/**
* Hardcoded, as modding is not supported without major changes.
* Notice the AI code parses the difficulty level by the index, not by name.
*
* @returns {Array}
*/
function loadAIDifficulties()
{
return [
{
"Name": "sandbox",
"Title": translateWithContext("aiDiff", "Sandbox")
},
{
"Name": "very easy",
"Title": translateWithContext("aiDiff", "Very Easy")
},
{
"Name": "easy",
"Title": translateWithContext("aiDiff", "Easy")
},
{
"Name": "medium",
"Title": translateWithContext("aiDiff", "Medium"),
"Default": true
},
{
"Name": "hard",
"Title": translateWithContext("aiDiff", "Hard")
},
{
"Name": "very hard",
"Title": translateWithContext("aiDiff", "Very Hard")
}
];
}
/**
* Loads available ceasefire settings.
*

View File

@ -48,8 +48,6 @@ var g_GameAttributes = {
var g_MapSizes = {};
var g_AIs = [];
var g_ChatMessages = [];
// Data caches
@ -108,14 +106,6 @@ function init(attribs)
// Called after the map data is loaded and cached
function initMain()
{
// Load AI list
g_AIs = Engine.GetAIs();
// Sort AIs by displayed name
g_AIs.sort(function (a, b) {
return a.data.name < b.data.name ? -1 : b.data.name < a.data.name ? +1 : 0;
});
// Get default player data - remove gaia
g_DefaultPlayerData = initPlayerDefaults();
g_DefaultPlayerData.shift();
@ -710,7 +700,6 @@ function loadGameAttributes()
if (!g_IsNetworked)
mapSettings.CheatsEnabled = true;
var aiCodes = [ ai.id for each (ai in g_AIs) ];
var civListCodes = [ civ.Code for each (civ in g_CivData) if (civ.SelectableInGameSetup !== false) ];
civListCodes.push("random");
@ -1536,7 +1525,7 @@ function updatePlayerList()
if (g_IsController)
Engine.GetGUIObjectByName("startGame").enabled = (g_AssignedCount > 0);
for each (var ai in g_AIs)
for (let ai of g_Settings.AIDescriptions)
{
if (ai.data.hidden)
{
@ -1602,7 +1591,6 @@ function updatePlayerList()
configButton.hidden = false;
configButton.onpress = function() {
Engine.PushGuiPage("page_aiconfig.xml", {
"ais": g_AIs,
"id": g_GameAttributes.settings.PlayerData[playerSlot].AI,
"difficulty": g_GameAttributes.settings.PlayerData[playerSlot].AIDiff,
"callback": "AIConfigCallback",