Index: ps/trunk/binaries/data/mods/public/gui/aiconfig/aiconfig.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/aiconfig/aiconfig.js (revision 14495) +++ ps/trunk/binaries/data/mods/public/gui/aiconfig/aiconfig.js (revision 14496) @@ -1,54 +1,54 @@ var g_AIs; // [ {"id": ..., "data": {"name": ..., "description": ..., ...} }, ... ] var g_Callback; // for the OK button function init(settings) { g_Callback = settings.callback; g_AIs = [ {id: "", data: {name: "None", description: "AI will be disabled for this player."}} ].concat(settings.ais); - var aiSelection = getGUIObjectByName("aiSelection"); + var aiSelection = Engine.GetGUIObjectByName("aiSelection"); aiSelection.list = [ 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; - var aiDiff = getGUIObjectByName("aiDifficulty"); + var aiDiff = Engine.GetGUIObjectByName("aiDifficulty"); aiDiff.list = [ "Sandbox", "Easy", "Medium", "Hard", "Very Hard" ]; 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; - getGUIObjectByName("aiDescription").caption = description; + Engine.GetGUIObjectByName("aiDescription").caption = description; } function returnAI() { - var aiSelection = getGUIObjectByName("aiSelection"); + var aiSelection = Engine.GetGUIObjectByName("aiSelection"); var idx = aiSelection.selected; var id = g_AIs[idx].id; var name = g_AIs[idx].data.name; - var difficulty = getGUIObjectByName("aiDifficulty").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.PopGuiPage(); g_Callback({"id": id, "name": name, "difficulty" : difficulty}); } Index: ps/trunk/binaries/data/mods/public/gui/civinfo/civinfo.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/civinfo/civinfo.js (revision 14495) +++ ps/trunk/binaries/data/mods/public/gui/civinfo/civinfo.js (revision 14496) @@ -1,155 +1,155 @@ var g_CivData = {}; var TEXTCOLOR = "white" function init(settings) { // Initialize civ list initCivNameList(); // TODO: Separate control for factions? } // Sort by culture, then by code equals culture and then by name ignoring case function sortByCultureAndName(a, b) { if (a.culture < b.culture) return -1; if (a.culture > b.culture) return 1; // Same culture // First code == culture if (a.code == a.culture) return -1; if (b.code == b.culture) return 1; // Then alphabetically by name (ignoring case) return sortNameIgnoreCase(a, b); } // Initialize the dropdown containing all the available civs function initCivNameList() { // Cache map data g_CivData = loadCivData(); var civList = [ { "name": civ.Name, "culture": civ.Culture, "code": civ.Code } for each (civ in g_CivData) ]; // Alphabetically sort the list, ignoring case civList.sort(sortByCultureAndName); // Indent sub-factions var civListNames = [ ((civ.code == civ.culture)?"":" ")+civ.name for each (civ in civList) ]; var civListCodes = [ civ.code for each (civ in civList) ]; // Set civ control - var civSelection = getGUIObjectByName("civSelection"); + var civSelection = Engine.GetGUIObjectByName("civSelection"); civSelection.list = civListNames; civSelection.list_data = civListCodes; civSelection.selected = 0; } // Function to make first char of string big function bigFirstLetter(str, size) { return '[font="serif-bold-'+(size+6)+'"]' + str[0] + '[/font]' + '[font="serif-bold-'+size+'"]' + str.substring(1) + '[/font]'; } // Heading font - bold and mixed caps function heading(string, size) { var textArray = string.split(" "); for(var i = 0; i < textArray.length; ++i) { var word = textArray[i]; var wordCaps = word.toUpperCase(); // Check if word is capitalized, if so assume it needs a big first letter if (wordCaps[0] == word[0]) textArray[i] = bigFirstLetter(wordCaps, size); else textArray[i] = '[font="serif-bold-'+size+'"]' + wordCaps + '[/font]'; // TODO: Would not be necessary if we could do nested tags } return textArray.join(" "); } // Called when user selects civ from dropdown function selectCiv(code) { var civInfo = g_CivData[code]; if(!civInfo) error("Error loading civ data for \""+code+"\""); // Update civ gameplay display - getGUIObjectByName("civGameplayHeading").caption = heading(civInfo.Name+" Gameplay", 16); + Engine.GetGUIObjectByName("civGameplayHeading").caption = heading(civInfo.Name+" Gameplay", 16); // Bonuses var bonusCaption = heading("Civilization Bonus"+(civInfo.CivBonuses.length == 1 ? "" : "es"), 12) + '\n'; for(var i = 0; i < civInfo.CivBonuses.length; ++i) { bonusCaption += '[color="' + TEXTCOLOR + '"][font="serif-bold-14"]' + civInfo.CivBonuses[i].Name + '[/font] [icon="iconInfo" tooltip="' + civInfo.CivBonuses[i].History + '" tooltip_style="civInfoTooltip"]\n ' + civInfo.CivBonuses[i].Description + '\n[/color]'; } bonusCaption += heading("Team Bonus"+(civInfo.TeamBonuses.length == 1 ? "" : "es"), 12) + '\n'; for(var i = 0; i < civInfo.TeamBonuses.length; ++i) { bonusCaption += '[color="' + TEXTCOLOR + '"][font="serif-bold-14"]' + civInfo.TeamBonuses[i].Name + '[/font] [icon="iconInfo" tooltip="' + civInfo.TeamBonuses[i].History + '" tooltip_style="civInfoTooltip"]\n ' + civInfo.TeamBonuses[i].Description + '\n[/color]'; } - getGUIObjectByName("civBonuses").caption = bonusCaption; + Engine.GetGUIObjectByName("civBonuses").caption = bonusCaption; // Special techs / buildings var techCaption = heading("Special Technologies", 12) + '\n'; for(var i = 0; i < civInfo.Factions.length; ++i) { var faction = civInfo.Factions[i]; for(var j = 0; j < faction.Technologies.length; ++j) { techCaption += '[color="' + TEXTCOLOR + '"][font="serif-bold-14"]' + faction.Technologies[j].Name + '[/font] [icon="iconInfo" tooltip="' + faction.Technologies[j].History + '" tooltip_style="civInfoTooltip"]\n ' + faction.Technologies[j].Description + '\n[/color]'; } } techCaption += heading("Special Building"+(civInfo.Structures.length == 1 ? "" : "s"), 12) + '\n'; for(var i = 0; i < civInfo.Structures.length; ++i) { techCaption += '[color="' + TEXTCOLOR + '"][font="serif-bold-14"]' + civInfo.Structures[i].Name + '[/font][/color] [icon="iconInfo" tooltip="' + civInfo.Structures[i].History + '" tooltip_style="civInfoTooltip"]\n'; } - getGUIObjectByName("civTechs").caption = techCaption; + Engine.GetGUIObjectByName("civTechs").caption = techCaption; // Heroes var heroCaption = heading("Heroes", 12) + '\n'; for(var i = 0; i < civInfo.Factions.length; ++i) { var faction = civInfo.Factions[i]; for(var j = 0; j < faction.Heroes.length; ++j) { heroCaption += '[color="' + TEXTCOLOR + '"][font="serif-bold-14"]' + faction.Heroes[j].Name + '[/font][/color] [icon="iconInfo" tooltip="' + faction.Heroes[j].History + '" tooltip_style="civInfoTooltip"]\n'; } heroCaption += '\n'; } - getGUIObjectByName("civHeroes").caption = heroCaption; + Engine.GetGUIObjectByName("civHeroes").caption = heroCaption; // Update civ history display - getGUIObjectByName("civHistoryHeading").caption = heading("History of the " + civInfo.Name, 16); - getGUIObjectByName("civHistoryText").caption = civInfo.History; + Engine.GetGUIObjectByName("civHistoryHeading").caption = heading("History of the " + civInfo.Name, 16); + Engine.GetGUIObjectByName("civHistoryText").caption = civInfo.History; } Index: ps/trunk/binaries/data/mods/public/gui/common/functions_civinfo.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/common/functions_civinfo.js (revision 14495) +++ ps/trunk/binaries/data/mods/public/gui/common/functions_civinfo.js (revision 14496) @@ -1,23 +1,23 @@ /* DESCRIPTION : Functions related to reading civ info NOTES : */ // ==================================================================== function loadCivData() { // Load all JSON files containing civ data var civData = {}; - var civFiles = buildDirEntList("civs/", "*.json", false); + var civFiles = Engine.BuildDirEntList("civs/", "*.json", false); for each (var filename in civFiles) { // Parse data if valid file var data = parseJSONData(filename); civData[data.Code] = data; } return civData; } // ==================================================================== \ No newline at end of file Index: ps/trunk/binaries/data/mods/public/gui/common/functions_global_object.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/common/functions_global_object.js (revision 14495) +++ ps/trunk/binaries/data/mods/public/gui/common/functions_global_object.js (revision 14496) @@ -1,33 +1,67 @@ /* DESCRIPTION : Contains global GUI functions, which will later be accessible from every GUI script/file. NOTES : So far, only the message box-related functions are implemented. */ // ******************************************* // messageBox // ******************************************* -// @params: int mbWidth, int mbHeight, string mbMessage, string mbTitle, int mbMode, arr mbButtonCaptions, arr mbButtonsCode +// @params: int mbWidth, int mbHeight, string mbMessage, string mbTitle, int mbMode, arr mbButtonCaptions, function mbBtnCode, var mbCallbackArgs // @return: void // @desc: Displays a new modal message box. // ******************************************* -function messageBox (mbWidth, mbHeight, mbMessage, mbTitle, mbMode, mbButtonCaptions, mbButtonsCode) + +// We want to pass callback functions for the different buttons in a convenient way. +// Because passing functions accross compartment boundaries is a pain, we just store them here together with some optional arguments. +// The messageBox page will return the code of the pressed button and the according function will be called. +var g_messageBoxBtnFunctions = []; +var g_messageBoxCallbackArgs = []; + +var g_messageBoxCallbackFunction = function(btnCode) +{ + if (btnCode !== undefined && g_messageBoxBtnFunctions[btnCode]) + { + if (g_messageBoxCallbackArgs[btnCode]) + g_messageBoxBtnFunctions[btnCode](g_messageBoxCallbackArgs[btnCode]); + else + g_messageBoxBtnFunctions[btnCode](); + } + + g_messageBoxBtnFunctions = []; + g_messageBoxCallbackArgs = []; +} + +function messageBox (mbWidth, mbHeight, mbMessage, mbTitle, mbMode, mbButtonCaptions, mbBtnCode, mbCallbackArgs) { + if (g_messageBoxBtnFunctions.length != 0) + { + warn("A messagebox was called when a previous callback function is still set, aborting!"); + return; + } - Engine.PushGuiPage("page_msgbox.xml", { + g_messageBoxBtnFunctions = mbBtnCode; + if (mbCallbackArgs) + g_messageBoxCallbackArgs = mbCallbackArgs; + + var initData = { width: mbWidth, height: mbHeight, message: mbMessage, title: mbTitle, mode: mbMode, buttonCaptions: mbButtonCaptions, - buttonCode: mbButtonsCode - }); + } + if (mbBtnCode) + initData.callback = "g_messageBoxCallbackFunction"; + + + Engine.PushGuiPage("page_msgbox.xml", initData); } // ==================================================================== function updateFPS() { - getGUIObjectByName("fpsCounter").caption = "FPS: " + getFPS(); + Engine.GetGUIObjectByName("fpsCounter").caption = "FPS: " + Engine.GetFPS(); } Index: ps/trunk/binaries/data/mods/public/gui/common/functions_utility.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/common/functions_utility.js (revision 14495) +++ ps/trunk/binaries/data/mods/public/gui/common/functions_utility.js (revision 14496) @@ -1,313 +1,313 @@ /* DESCRIPTION : Generic utility functions. NOTES : */ // ==================================================================== function getRandom(randomMin, randomMax) { // Returns a random whole number in a min..max range. // NOTE: There should probably be an engine function for this, // since we'd need to keep track of random seeds for replays. var randomNum = randomMin + (randomMax-randomMin)*Math.random(); // num is random, from A to B return Math.round(randomNum); } // ==================================================================== // Get list of XML files in pathname with recursion, excepting those starting with _ function getXMLFileList(pathname) { - var files = buildDirEntList(pathname, "*.xml", true); + var files = Engine.BuildDirEntList(pathname, "*.xml", true); var result = []; // Get only subpath from filename and discard extension for (var i = 0; i < files.length; ++i) { var file = files[i]; file = file.substring(pathname.length, file.length-4); // Split path into directories so we can check for beginning _ character var tokens = file.split("/"); if (tokens[tokens.length-1][0] != "_") result.push(file); } return result; } // ==================================================================== // Get list of JSON files in pathname function getJSONFileList(pathname) { - var files = buildDirEntList(pathname, "*.json", false); + var files = Engine.BuildDirEntList(pathname, "*.json", false); // Remove the path and extension from each name, since we just want the filename files = [ n.substring(pathname.length, n.length-5) for each (n in files) ]; return files; } // ==================================================================== // Parse JSON data function parseJSONData(pathname) { var data = {}; - var rawData = readFile(pathname); + var rawData = Engine.ReadFile(pathname); if (!rawData) { error("Failed to read file: "+pathname); } else { try { // Catch nasty errors from JSON parsing // TODO: Need more info from the parser on why it failed: line number, position, etc! data = JSON.parse(rawData); if (!data) error("Failed to parse JSON data in: "+pathname+" (check for valid JSON data)"); } catch(err) { error(err.toString()+": parsing JSON data in "+pathname); } } return data; } // ==================================================================== // A sorting function for arrays of objects with 'name' properties, ignoring case function sortNameIgnoreCase(x, y) { var lowerX = x.name.toLowerCase(); var lowerY = y.name.toLowerCase(); if (lowerX < lowerY) return -1; else if (lowerX > lowerY) return 1; else return 0; } // ==================================================================== // Escape text tags and whitespace, so users can't use special formatting in their chats // Limit string length to 256 characters function escapeText(text) { if (!text) return text; var out = text.replace(/[\[\]]+/g,""); out = out.replace(/\s+/g, " "); return out.substr(0, 255); } // ==================================================================== function toTitleCase (string) { if (!string) return string; // Returns the title-case version of a given string. string = string.toString(); string = string[0].toUpperCase() + string.substring(1).toLowerCase(); return string; } // ==================================================================== // Parse and return JSON data from file in simulation/data/* // returns valid object or undefined on error function parseJSONFromDataFile(filename) { var path = "simulation/data/"+filename; - var rawData = readFile(path); + var rawData = Engine.ReadFile(path); if (!rawData) error("Failed to read file: "+path); try { // Catch nasty errors from JSON parsing // TODO: Need more info from the parser on why it failed: line number, position, etc! var data = JSON.parse(rawData); return data; } catch(err) { error(err.toString()+": parsing JSON data in "+path); } return undefined; } // ==================================================================== // Load default player data, for when it's not otherwise specified function initPlayerDefaults() { var defaults = []; var data = parseJSONFromDataFile("player_defaults.json"); if (!data || !data.PlayerData) error("Failed to parse player defaults in player_defaults.json (check for valid JSON data)"); else defaults = data.PlayerData; return defaults; } // ==================================================================== // Load map size data function initMapSizes() { var sizes = { "shortNames":[], "names":[], "tiles": [], "default": 0 }; var data = parseJSONFromDataFile("map_sizes.json"); if (!data || !data.Sizes) error("Failed to parse map sizes in map_sizes.json (check for valid JSON data)"); else { for (var i = 0; i < data.Sizes.length; ++i) { sizes.shortNames.push(data.Sizes[i].Name); sizes.names.push(data.Sizes[i].LongName); sizes.tiles.push(data.Sizes[i].Tiles); if (data.Sizes[i].Default) sizes["default"] = i; } } return sizes; } // ==================================================================== // Load game speed data function initGameSpeeds() { var gameSpeeds = { "names": [], "speeds": [], "default": 0 }; var data = parseJSONFromDataFile("game_speeds.json"); if (!data || !data.Speeds) error("Failed to parse game speeds in game_speeds.json (check for valid JSON data)"); else { for (var i = 0; i < data.Speeds.length; ++i) { gameSpeeds.names.push(data.Speeds[i].Name); gameSpeeds.speeds.push(data.Speeds[i].Speed); if (data.Speeds[i].Default) gameSpeeds["default"] = i; } } return gameSpeeds; } // ==================================================================== // Convert integer color values to string (for use in GUI objects) function iColorToString(color) { var string = "0 0 0"; if (color && ("r" in color) && ("g" in color) && ("b" in color)) string = color.r + " " + color.g + " " + color.b; return string; } // ==================================================================== /** * Convert time in milliseconds to hh:mm:ss string representation. * @param time Time period in milliseconds (integer) * @return String representing time period */ function timeToString(time) { var hours = Math.floor(time / 1000 / 60 / 60); var minutes = Math.floor(time / 1000 / 60) % 60; var seconds = Math.floor(time / 1000) % 60; return hours + ':' + (minutes < 10 ? '0' + minutes : minutes) + ':' + (seconds < 10 ? '0' + seconds : seconds); } // ==================================================================== function removeDupes(array) { for (var i = 0; i < array.length; i++) { if (array.indexOf(array[i]) < i) { array.splice(i, 1); i--; } } } // ==================================================================== // "Inside-out" implementation of Fisher-Yates shuffle function shuffleArray(source) { if (!source.length) return []; var result = [source[0]]; for (var i = 1; i < source.length; ++i) { var j = Math.floor(Math.random() * i); result[i] = result[j]; result[j] = source[i]; } return result; } // ==================================================================== // Filter out conflicting characters and limit the length of a given name. // @param name Name to be filtered. // @param stripUnicode Whether or not to remove unicode characters. // @param stripSpaces Whether or not to remove whitespace. function sanitizePlayerName(name, stripUnicode, stripSpaces) { // We delete the '[', ']' characters (GUI tags) and delete the ',' characters (player name separators) by default. var sanitizedName = name.replace(/[\[\],]/g, ""); // Optionally strip unicode if (stripUnicode) sanitizedName = sanitizedName.replace(/[^\x20-\x7f]/g, ""); // Optionally strip whitespace if (stripSpaces) sanitizedName = sanitizedName.replace(/\s/g, ""); // Limit the length to 20 characters return sanitizedName.substr(0,20); } Index: ps/trunk/binaries/data/mods/public/gui/common/functions_utility_error.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/common/functions_utility_error.js (revision 14495) +++ ps/trunk/binaries/data/mods/public/gui/common/functions_utility_error.js (revision 14496) @@ -1,29 +1,29 @@ /* DESCRIPTION : Error-handling utility functions. NOTES : */ // ==================================================================== function cancelOnError(msg) { // Delete game objects - endGame(); + Engine.EndGame(); // Return to pregame Engine.SwitchGuiPage("page_pregame.xml"); // Display error dialog if message given if (msg) { Engine.PushGuiPage("page_msgbox.xml", { width: 500, height: 200, message: '[font="serif-bold-18"]' + msg + '[/font]', title: "Loading Aborted", mode: 2 }); } // Reset cursor - setCursor("arrow-default"); + Engine.SetCursor("arrow-default"); } Index: ps/trunk/binaries/data/mods/public/gui/common/functions_utility_list.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/common/functions_utility_list.js (revision 14495) +++ ps/trunk/binaries/data/mods/public/gui/common/functions_utility_list.js (revision 14496) @@ -1,164 +1,164 @@ /* DESCRIPTION : Functions to manipulate objects with a 'list' property (used to handle the items in list, dropdown, etc.) NOTES : To ensure the selection is properly updated, it is important to use these functions and not manually access the list. */ // ==================================================================== // Remove the item at the given index (pos) from the given list object (objectName). function removeItem (objectName, pos) { - if (getGUIObjectByName (objectName) == null) + if (Engine.GetGUIObjectByName (objectName) == null) Engine.Console_Write ("removeItem(): " + objectName + " not found."); - var list = getGUIObjectByName (objectName).list; - var selected = getGUIObjectByName (objectName).selected; + var list = Engine.GetGUIObjectByName (objectName).list; + var selected = Engine.GetGUIObjectByName (objectName).selected; list.splice(pos, 1); - getGUIObjectByName (objectName).list = list; + Engine.GetGUIObjectByName (objectName).list = list; // It's important that we update the selection *after* // we've committed the changes to the list. // Update the selected so the same element remains selected. if (selected == pos) { - getGUIObjectByName (objectName).selected = -1; + Engine.GetGUIObjectByName (objectName).selected = -1; } else if (selected > pos) { - getGUIObjectByName (objectName).selected = selected - 1; + Engine.GetGUIObjectByName (objectName).selected = selected - 1; } } // ==================================================================== // Add the item at the given index (pos) to the given list object (objectName) with the given value (value). function addItem (objectName, pos, value) { - if (getGUIObjectByName (objectName) == null) + if (Engine.GetGUIObjectByName (objectName) == null) Engine.Console_Write ("addItem(): " + objectName + " not found."); - var list = getGUIObjectByName (objectName).list; - var selected = getGUIObjectByName (objectName).selected; + var list = Engine.GetGUIObjectByName (objectName).list; + var selected = Engine.GetGUIObjectByName (objectName).selected; list.splice (pos, 0, value); - getGUIObjectByName (objectName).list = list; + Engine.GetGUIObjectByName (objectName).list = list; // It's important that we update the selection *after* // we've committed the changes to the list. // Update the selected so the same element remains selected. if (selected >= pos) { - getGUIObjectByName (objectName).selected = selected + 1; + Engine.GetGUIObjectByName (objectName).selected = selected + 1; } } // ==================================================================== // Adds an element to the end of the list function pushItem (objectName, value) { - if (getGUIObjectByName (objectName) == null) + if (Engine.GetGUIObjectByName (objectName) == null) Engine.Console_Write ("pushItem(): " + objectName + " not found."); - var list = getGUIObjectByName (objectName).list; + var list = Engine.GetGUIObjectByName (objectName).list; list.push (value); - getGUIObjectByName (objectName).list = list; + Engine.GetGUIObjectByName (objectName).list = list; // Point to the new item. - getGUIObjectByName(objectName).selected = getNumItems(objectName)-1; + Engine.GetGUIObjectByName(objectName).selected = getNumItems(objectName)-1; } // ==================================================================== // Removes the last element function popItem (objectName) { - if (getGUIObjectByName (objectName) == null) + if (Engine.GetGUIObjectByName (objectName) == null) Engine.Console_Write ("popItem(): " + objectName + " not found."); - var selected = getGUIObjectByName (objectName).selected; + var selected = Engine.GetGUIObjectByName (objectName).selected; removeItem(objectName, getNumItems(objectName)-1); if (selected == getNumItems(objectName)-1) { - getGUIObjectByName(objectName).selected = -1; + Engine.GetGUIObjectByName(objectName).selected = -1; } } // ==================================================================== // Retrieves the number of elements in the list function getNumItems (objectName) { - if (getGUIObjectByName (objectName) == null) + if (Engine.GetGUIObjectByName (objectName) == null) Engine.Console_Write ("getNumItems(): " + objectName + " not found."); - var list = getGUIObjectByName(objectName).list; + var list = Engine.GetGUIObjectByName(objectName).list; return list.length; } // ==================================================================== // Retrieves the value of the item at 'pos' function getItemValue (objectName, pos) { - if (getGUIObjectByName (objectName) == null) + if (Engine.GetGUIObjectByName (objectName) == null) Engine.Console_Write ("getItemValue(): " + objectName + " not found."); - var list = getGUIObjectByName(objectName).list; + var list = Engine.GetGUIObjectByName(objectName).list; return list[pos]; } // ==================================================================== // Retrieves the value of the currently selected item function getCurrItemValue (objectName) { - if (getGUIObjectByName (objectName) == null) + if (Engine.GetGUIObjectByName (objectName) == null) Engine.Console_Write ("getCurrItemValue(): " + objectName + " not found."); - if (getGUIObjectByName(objectName).selected == -1) + if (Engine.GetGUIObjectByName(objectName).selected == -1) return ""; - var list = getGUIObjectByName(objectName).list; - return list[getGUIObjectByName(objectName).selected]; + var list = Engine.GetGUIObjectByName(objectName).list; + return list[Engine.GetGUIObjectByName(objectName).selected]; } // ==================================================================== // Sets current item to a given string (which must be one of those // already in the list). function setCurrItemValue (objectName, string) { - if (getGUIObjectByName(objectName) == null) { + if (Engine.GetGUIObjectByName(objectName) == null) { Engine.Console_Write ("setCurrItemValue(): " + objectName + " not found."); return -1; } - if (getGUIObjectByName(objectName).selected == -1) + if (Engine.GetGUIObjectByName(objectName).selected == -1) return -1; // Return -1 if nothing selected. - var list = getGUIObjectByName(objectName).list; + var list = Engine.GetGUIObjectByName(objectName).list; // Seek through list. for (var ctr = 0; ctr < list.length; ctr++) { // If we have found the string in the list, if (list[ctr] == string) { // Point selected to this item. - getGUIObjectByName(objectName).selected = ctr; + Engine.GetGUIObjectByName(objectName).selected = ctr; return ctr; // Return position of item. } } // Return -2 if failed to find value in list. Engine.Console_Write ("Requested string '" + string + "' not found in " + objectName + "'s list."); return -2; } // ==================================================================== Index: ps/trunk/binaries/data/mods/public/gui/common/functions_utility_music.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/common/functions_utility_music.js (revision 14495) +++ ps/trunk/binaries/data/mods/public/gui/common/functions_utility_music.js (revision 14496) @@ -1,171 +1,171 @@ /* DESCRIPTION : Audio functions (eg "pick a random sound from a list", "build a playlist") go here. NOTES : */ // ==================================================================== // Quick run-down of the basic audio commands: // Save the specified audio file to handle "s". // s = new Sound( "audio/music/menu_track.ogg" ); // Play the sound stored at handle "s" one time (it'll be automatically freed when the playback ends): // s.play(); // Play the sound stored at handle "s" continuously: // s.loop(); // Close "s" and free it from memory (use in conjunction with loop()): // s.free(); // Adjust the gain (volume) of a sound (floating point range between 0 (silent) and 1 (max volume)). // s.setGain(value); // ==================================================================== function newRandomSound(soundType, soundSubType, soundPrePath) { // Return a random audio file by category, to be assigned to a handle. var randomSoundPath; switch (soundType) { case "music": randomSoundPath = "audio/music/" break; case "ambient": randomSoundPath = "audio/ambient/" + soundPrePath + "/"; break; case "effect": randomSoundPath = soundPrePath + "/"; break; default: break; } // Get names of sounds (attack, command, select, hit, pain). // or // Get names of "peace", "menu" (theme) and "battle" tracks. - var soundArray = buildDirEntList(randomSoundPath, "*" + soundSubType + "*", false); + var soundArray = Engine.BuildDirEntList(randomSoundPath, "*" + soundSubType + "*", false); if (soundArray.length == 0) { Engine.Console_Write ("Failed to find sounds matching '*"+soundSubType+"*'"); return undefined; } // Get a random number within the sound's range. var randomSound = getRandom (0, soundArray.length-1); // Set name of track. var randomFileName = soundArray[randomSound]; // Build path to random audio file. randomSoundPath = randomFileName; //Engine.Console_Write("Playing " + randomSoundPath + " ..."); switch (soundType) { case "music": return new MusicSound(randomSoundPath); break; case "ambient": return new AmbientSound(randomSoundPath); break; case "effect": Engine.Console_Write("am loading effect '*"+randomSoundPath+"*'"); break; default: break; } return new Sound(randomSoundPath); } // ==================================================================== function fadeOut (soundHandle, fadeDuration) { // Adjust the gain of a sound until it is zero. // The sound is automatically freed when finished fading. soundHandle.fade(-1, 0, fadeDuration) return true; } // ==================================================================== function fadeIn (soundHandle, finalGain, fadeDuration) { // Adjust the gain of a sound from zero up to the given value. soundHandle.fade(0, finalGain, fadeDuration) return true; } // ==================================================================== function crossFade (outHandle, inHandle, fadeDuration) { // Accepts two sound handles. Over the given duration, // fades out the first while fading in the second. // Note that it plays the in and frees the out while it's at it. if (outHandle) fadeOut(outHandle, fadeDuration); if (inHandle) { inHandle.play(); fadeIn(inHandle, Engine.ConfigDB_GetValue("user", "sound.mastergain"), fadeDuration); } return true; } // ==================================================================== //const AMBIENT_SOUND = "audio/ambient/dayscape/day_temperate_gen_03.ogg"; //const AMBIENT_TEMPERATE = "temperate"; //var currentAmbient; //function playRandomAmbient(type) //{ // switch (type) // { // case AMBIENT_TEMPERATE: // // Seem to need the underscore at the end of "temperate" to avoid crash // // (Might be caused by trying to randomly load day_temperate.xml) // currentAmbient = newRandomSound("ambient", "temperate_", "dayscape"); // if (currentAmbient) // { // currentAmbient.loop(); // currentAmbient.setGain(0.8); // } // break; // // default: // Engine.Console_Write("Unrecognized ambient type: " + type); // break; // } //} // //function stopAmbient() //{ // if (currentAmbient) // { // currentAmbient.fade(-1, 0.0, 5.0); // currentAmbient = null; // } //} //const BUTTON_SOUND = "audio/interface/ui/ui_button_longclick.ogg"; //function playButtonSound() //{ // var buttonSound = new Sound(BUTTON_SOUND); // buttonSound.play(); //} Index: ps/trunk/binaries/data/mods/public/gui/common/functions_utility_test.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/common/functions_utility_test.js (revision 14495) +++ ps/trunk/binaries/data/mods/public/gui/common/functions_utility_test.js (revision 14496) @@ -1,44 +1,44 @@ function updateOrbital() { - if( !getGUIObjectByName( 'arena' ).hidden ) + if( !Engine.GetGUIObjectByName( 'arena' ).hidden ) { g_ballx += g_balldx; g_bally += g_balldy; if (g_ballx > 600) { g_balldx *= -0.9; g_ballx = 600-(g_ballx-600); } else if (g_ballx < 0) { g_balldx *= -0.9; g_ballx = -g_ballx; } if (g_bally > 400) { g_balldy *= -0.9; g_bally = 400-(g_bally-400); } else if (g_bally < 0) { g_balldy *= -0.9; g_bally = -g_bally; } // Gravitate towards the mouse var vect_x = g_ballx-g_mousex; var vect_y = g_bally-g_mousey; var dsquared = vect_x*vect_x + vect_y*vect_y; if (dsquared < 1000) dsquared = 1000; var force = 10000.0 / dsquared; var mag = Math.sqrt(dsquared); vect_x /= mag; vect_y /= mag; g_balldx -= force * vect_x; g_balldy -= force * vect_y; - var ball = getGUIObjectByName('ball'); + var ball = Engine.GetGUIObjectByName('ball'); var r=5; ball.size = new GUISize(g_ballx-r, g_bally-r, g_ballx+r, g_bally+r); } } Index: ps/trunk/binaries/data/mods/public/gui/gamesetup/gamesetup.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/gamesetup/gamesetup.js (revision 14495) +++ ps/trunk/binaries/data/mods/public/gui/gamesetup/gamesetup.js (revision 14496) @@ -1,1565 +1,1565 @@ //////////////////////////////////////////////////////////////////////////////////////////////// // Constants const DEFAULT_NETWORKED_MAP = "Acropolis 01"; const DEFAULT_OFFLINE_MAP = "Acropolis 01"; // TODO: Move these somewhere like simulation\data\game_types.json, Atlas needs them too const VICTORY_TEXT = ["Conquest", "Wonder", "None"]; const VICTORY_DATA = ["conquest", "wonder", "endless"]; const VICTORY_DEFAULTIDX = 0; const POPULATION_CAP = ["50", "100", "150", "200", "250", "300", "Unlimited"]; const POPULATION_CAP_DATA = [50, 100, 150, 200, 250, 300, 10000]; const POPULATION_CAP_DEFAULTIDX = 5; const STARTING_RESOURCES = ["Very Low", "Low", "Medium", "High", "Very High", "Deathmatch"]; const STARTING_RESOURCES_DATA = [100, 300, 500, 1000, 3000, 50000]; const STARTING_RESOURCES_DEFAULTIDX = 1; // Max number of players for any map const MAX_PLAYERS = 8; //////////////////////////////////////////////////////////////////////////////////////////////// // Is this is a networked game, or offline var g_IsNetworked; // Is this user in control of game settings (i.e. is a network server, or offline player) var g_IsController; //Server name, if user is a server, connected to the multiplayer lobby var g_ServerName; // Are we currently updating the GUI in response to network messages instead of user input // (and therefore shouldn't send further messages to the network) var g_IsInGuiUpdate; var g_PlayerAssignments = {}; // Default game setup attributes var g_DefaultPlayerData = []; var g_GameAttributes = { settings: {} }; var g_GameSpeeds = {}; var g_MapSizes = {}; var g_AIs = []; var g_ChatMessages = []; // Data caches var g_MapData = {}; var g_CivData = {}; var g_MapFilters = []; // Warn about the AI's nonexistent naval map support. var g_NavalWarning = "\n\n[font=\"serif-bold-12\"][color=\"orange\"]Warning:[/color][/font] \ The AI does not support naval maps and may cause severe performance issues. \ Naval maps are recommended to be played with human opponents only."; // To prevent the display locking up while we load the map metadata, // we'll start with a 'loading' message and switch to the main screen in the // tick handler var g_LoadingState = 0; // 0 = not started, 1 = loading, 2 = loaded //////////////////////////////////////////////////////////////////////////////////////////////// function init(attribs) { switch (attribs.type) { case "offline": g_IsNetworked = false; g_IsController = true; break; case "server": g_IsNetworked = true; g_IsController = true; break; case "client": g_IsNetworked = true; g_IsController = false; break; default: error("Unexpected 'type' in gamesetup init: "+attribs.type); } if (attribs.serverName) g_ServerName = attribs.serverName; // Init the Cancel Button caption and tooltip - var cancelButton = getGUIObjectByName("cancelGame"); + var cancelButton = Engine.GetGUIObjectByName("cancelGame"); if(!Engine.HasXmppClient()) { cancelButton.tooltip = "Return to the main menu." } else { cancelButton.tooltip = "Return to the lobby." } } // Called after the map data is loaded and cached function initMain() { // Load AI list and hide deprecated AIs 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(); for (var i = 0; i < g_DefaultPlayerData.length; i++) g_DefaultPlayerData[i].Civ = "random"; g_GameSpeeds = initGameSpeeds(); g_MapSizes = initMapSizes(); // Init civs initCivNameList(); // Init map types - var mapTypes = getGUIObjectByName("mapTypeSelection"); + var mapTypes = Engine.GetGUIObjectByName("mapTypeSelection"); mapTypes.list = ["Skirmish","Random","Scenario"]; mapTypes.list_data = ["skirmish","random","scenario"]; // Setup map filters - will appear in order they are added addFilter("Default", function(settings) { return settings && !keywordTestOR(settings.Keywords, ["naval", "demo", "hidden"]); }); addFilter("Naval Maps", function(settings) { return settings && keywordTestAND(settings.Keywords, ["naval"]); }); addFilter("Demo Maps", function(settings) { return settings && keywordTestAND(settings.Keywords, ["demo"]); }); addFilter("All Maps", function(settings) { return true; }); // Populate map filters dropdown - var mapFilters = getGUIObjectByName("mapFilterSelection"); + var mapFilters = Engine.GetGUIObjectByName("mapFilterSelection"); mapFilters.list = getFilters(); g_GameAttributes.mapFilter = "Default"; // Setup controls for host only if (g_IsController) { mapTypes.selected = 0; mapFilters.selected = 0; // Create a unique ID for this match, to be used for identifying the same game reports // for the lobby. g_GameAttributes.matchID = Engine.GetMatchID(); initMapNameList(); - var numPlayersSelection = getGUIObjectByName("numPlayersSelection"); + var numPlayersSelection = Engine.GetGUIObjectByName("numPlayersSelection"); var players = []; for (var i = 1; i <= MAX_PLAYERS; ++i) players.push(i); numPlayersSelection.list = players; numPlayersSelection.list_data = players; numPlayersSelection.selected = MAX_PLAYERS - 1; - var gameSpeed = getGUIObjectByName("gameSpeed"); + var gameSpeed = Engine.GetGUIObjectByName("gameSpeed"); gameSpeed.hidden = false; - getGUIObjectByName("gameSpeedText").hidden = true; + Engine.GetGUIObjectByName("gameSpeedText").hidden = true; gameSpeed.list = g_GameSpeeds.names; gameSpeed.list_data = g_GameSpeeds.speeds; gameSpeed.onSelectionChange = function() { // Update attributes so other players can see change if (this.selected != -1) g_GameAttributes.gameSpeed = g_GameSpeeds.speeds[this.selected]; if (!g_IsInGuiUpdate) updateGameAttributes(); } gameSpeed.selected = g_GameSpeeds["default"]; - var populationCaps = getGUIObjectByName("populationCap"); + var populationCaps = Engine.GetGUIObjectByName("populationCap"); populationCaps.list = POPULATION_CAP; populationCaps.list_data = POPULATION_CAP_DATA; populationCaps.selected = POPULATION_CAP_DEFAULTIDX; populationCaps.onSelectionChange = function() { if (this.selected != -1) g_GameAttributes.settings.PopulationCap = POPULATION_CAP_DATA[this.selected]; if (!g_IsInGuiUpdate) updateGameAttributes(); } - var startingResourcesL = getGUIObjectByName("startingResources"); + var startingResourcesL = Engine.GetGUIObjectByName("startingResources"); startingResourcesL.list = STARTING_RESOURCES; startingResourcesL.list_data = STARTING_RESOURCES_DATA; startingResourcesL.selected = STARTING_RESOURCES_DEFAULTIDX; startingResourcesL.onSelectionChange = function() { if (this.selected != -1) g_GameAttributes.settings.StartingResources = STARTING_RESOURCES_DATA[this.selected]; if (!g_IsInGuiUpdate) updateGameAttributes(); } - var victoryConditions = getGUIObjectByName("victoryCondition"); + var victoryConditions = Engine.GetGUIObjectByName("victoryCondition"); victoryConditions.list = VICTORY_TEXT; victoryConditions.list_data = VICTORY_DATA; victoryConditions.onSelectionChange = function() { // Update attributes so other players can see change if (this.selected != -1) g_GameAttributes.settings.GameType = VICTORY_DATA[this.selected]; if (!g_IsInGuiUpdate) updateGameAttributes(); }; victoryConditions.selected = VICTORY_DEFAULTIDX; - var mapSize = getGUIObjectByName("mapSize"); + var mapSize = Engine.GetGUIObjectByName("mapSize"); mapSize.list = g_MapSizes.names; mapSize.list_data = g_MapSizes.tiles; mapSize.onSelectionChange = function() { // Update attributes so other players can see change if (this.selected != -1) g_GameAttributes.settings.Size = g_MapSizes.tiles[this.selected]; if (!g_IsInGuiUpdate) updateGameAttributes(); }; mapSize.selected = 0; - getGUIObjectByName("revealMap").onPress = function() + Engine.GetGUIObjectByName("revealMap").onPress = function() { // Update attributes so other players can see change g_GameAttributes.settings.RevealMap = this.checked; if (!g_IsInGuiUpdate) updateGameAttributes(); }; - getGUIObjectByName("lockTeams").onPress = function() + Engine.GetGUIObjectByName("lockTeams").onPress = function() { // Update attributes so other players can see change g_GameAttributes.settings.LockTeams = this.checked; if (!g_IsInGuiUpdate) updateGameAttributes(); }; - getGUIObjectByName("enableCheats").onPress = function() + Engine.GetGUIObjectByName("enableCheats").onPress = function() { // Update attributes so other players can see change g_GameAttributes.settings.CheatsEnabled = this.checked; if (!g_IsInGuiUpdate) updateGameAttributes(); }; } else { // If we're a network client, disable all the map controls // TODO: make them look visually disabled so it's obvious why they don't work - getGUIObjectByName("mapTypeSelection").hidden = true; - getGUIObjectByName("mapTypeText").hidden = false; - getGUIObjectByName("mapFilterSelection").hidden = true; - getGUIObjectByName("mapFilterText").hidden = false; - getGUIObjectByName("mapSelectionText").hidden = false; - getGUIObjectByName("mapSelection").hidden = true; - getGUIObjectByName("victoryConditionText").hidden = false; - getGUIObjectByName("victoryCondition").hidden = true; - getGUIObjectByName("gameSpeedText").hidden = false; - getGUIObjectByName("gameSpeed").hidden = true; + Engine.GetGUIObjectByName("mapTypeSelection").hidden = true; + Engine.GetGUIObjectByName("mapTypeText").hidden = false; + Engine.GetGUIObjectByName("mapFilterSelection").hidden = true; + Engine.GetGUIObjectByName("mapFilterText").hidden = false; + Engine.GetGUIObjectByName("mapSelectionText").hidden = false; + Engine.GetGUIObjectByName("mapSelection").hidden = true; + Engine.GetGUIObjectByName("victoryConditionText").hidden = false; + Engine.GetGUIObjectByName("victoryCondition").hidden = true; + Engine.GetGUIObjectByName("gameSpeedText").hidden = false; + Engine.GetGUIObjectByName("gameSpeed").hidden = true; // Disable player and game options controls // TODO: Shouldn't players be able to choose their own assignment? for (var i = 0; i < MAX_PLAYERS; ++i) { - getGUIObjectByName("playerAssignment["+i+"]").enabled = false; - getGUIObjectByName("playerCiv["+i+"]").hidden = true; - getGUIObjectByName("playerTeam["+i+"]").hidden = true; + Engine.GetGUIObjectByName("playerAssignment["+i+"]").enabled = false; + Engine.GetGUIObjectByName("playerCiv["+i+"]").hidden = true; + Engine.GetGUIObjectByName("playerTeam["+i+"]").hidden = true; } - getGUIObjectByName("numPlayersSelection").hidden = true; + Engine.GetGUIObjectByName("numPlayersSelection").hidden = true; } // Set up multiplayer/singleplayer bits: if (!g_IsNetworked) { - getGUIObjectByName("chatPanel").hidden = true; - getGUIObjectByName("enableCheats").checked = true; + Engine.GetGUIObjectByName("chatPanel").hidden = true; + Engine.GetGUIObjectByName("enableCheats").checked = true; g_GameAttributes.settings.CheatsEnabled = true; } else { - getGUIObjectByName("enableCheatsDesc").hidden = false; - getGUIObjectByName("enableCheats").checked = false; + Engine.GetGUIObjectByName("enableCheatsDesc").hidden = false; + Engine.GetGUIObjectByName("enableCheats").checked = false; g_GameAttributes.settings.CheatsEnabled = false; if (g_IsController) - getGUIObjectByName("enableCheats").hidden = false; + Engine.GetGUIObjectByName("enableCheats").hidden = false; else - getGUIObjectByName("enableCheatsText").hidden = false; + Engine.GetGUIObjectByName("enableCheatsText").hidden = false; } // Settings for all possible player slots var boxSpacing = 32; for (var i = 0; i < MAX_PLAYERS; ++i) { // Space player boxes - var box = getGUIObjectByName("playerBox["+i+"]"); + var box = Engine.GetGUIObjectByName("playerBox["+i+"]"); var boxSize = box.size; var h = boxSize.bottom - boxSize.top; boxSize.top = i * boxSpacing; boxSize.bottom = i * boxSpacing + h; box.size = boxSize; // Populate team dropdowns - var team = getGUIObjectByName("playerTeam["+i+"]"); + var team = Engine.GetGUIObjectByName("playerTeam["+i+"]"); team.list = ["None", "1", "2", "3", "4"]; team.list_data = [-1, 0, 1, 2, 3]; team.selected = 0; let playerSlot = i; // declare for inner function use team.onSelectionChange = function() { // Update team if (this.selected != -1) g_GameAttributes.settings.PlayerData[playerSlot].Team = this.selected - 1; if (!g_IsInGuiUpdate) updateGameAttributes(); }; // Set events - var civ = getGUIObjectByName("playerCiv["+i+"]"); + var civ = Engine.GetGUIObjectByName("playerCiv["+i+"]"); civ.onSelectionChange = function() { // Update civ if ((this.selected != -1)&&(g_GameAttributes.mapType !== "scenario")) g_GameAttributes.settings.PlayerData[playerSlot].Civ = this.list_data[this.selected]; if (!g_IsInGuiUpdate) updateGameAttributes(); }; } if (g_IsNetworked) { // For multiplayer, focus the chat input box by default - getGUIObjectByName("chatInput").focus(); + Engine.GetGUIObjectByName("chatInput").focus(); } else { // For single-player, focus the map list by default, // to allow easy keyboard selection of maps - getGUIObjectByName("mapSelection").focus(); + Engine.GetGUIObjectByName("mapSelection").focus(); } } function handleNetMessage(message) { log("Net message: "+uneval(message)); switch (message.type) { case "netstatus": switch (message.status) { case "disconnected": cancelSetup(); if (Engine.HasXmppClient()) Engine.SwitchGuiPage("page_lobby.xml"); else Engine.SwitchGuiPage("page_pregame.xml"); reportDisconnect(message.reason); break; default: error("Unrecognised netstatus type "+message.status); break; } break; case "gamesetup": if (message.data) // (the host gets undefined data on first connect, so skip that) g_GameAttributes = message.data; onGameAttributesChange(); break; case "players": // Find and report all joinings/leavings for (var host in message.hosts) if (! g_PlayerAssignments[host]) addChatMessage({ "type": "connect", "username": message.hosts[host].name }); for (var host in g_PlayerAssignments) if (! message.hosts[host]) addChatMessage({ "type": "disconnect", "guid": host }); // Update the player list g_PlayerAssignments = message.hosts; updatePlayerList(); if (g_IsController) sendRegisterGameStanza(); break; case "start": if (g_IsController && Engine.HasXmppClient()) { var players = [ assignment.name for each (assignment in g_PlayerAssignments) ] Engine.SendChangeStateGame(Object.keys(g_PlayerAssignments).length, players.join(", ")); } Engine.SwitchGuiPage("page_loading.xml", { "attribs": g_GameAttributes, "isNetworked" : g_IsNetworked, "playerAssignments": g_PlayerAssignments, "isController": g_IsController }); break; case "chat": addChatMessage({ "type": "message", "guid": message.guid, "text": message.text }); break; default: error("Unrecognised net message type "+message.type); } } // Get display name from map data function getMapDisplayName(map) { var mapData = loadMapData(map); if (!mapData || !mapData.settings || !mapData.settings.Name) { // Give some msg that map format is unsupported log("Map data missing in scenario '"+map+"' - likely unsupported format"); return map; } return mapData.settings.Name; } // Get display name from map data function getMapPreview(map) { var mapData = loadMapData(map); if (!mapData || !mapData.settings || !mapData.settings.Preview) { // Give some msg that map format is unsupported return "nopreview.png"; } return mapData.settings.Preview; } // Get a setting if it exists or return default function getSetting(settings, defaults, property) { if (settings && (property in settings)) return settings[property]; // Use defaults if (defaults && (property in defaults)) return defaults[property]; return undefined; } // Initialize the dropdowns containing all the available civs function initCivNameList() { // Cache civ data g_CivData = loadCivData(); // Extract name/code, and skip civs that are explicitly disabled // (intended for unusable incomplete civs) var civList = [ { "name": civ.Name, "code": civ.Code } for each (civ in g_CivData) if (civ.SelectableInGameSetup !== false) ]; // Alphabetically sort the list, ignoring case civList.sort(sortNameIgnoreCase); var civListNames = [ civ.name for each (civ in civList) ]; var civListCodes = [ civ.code for each (civ in civList) ]; // Add random civ to beginning of list civListNames.unshift("[color=\"orange\"]Random"); civListCodes.unshift("random"); // Update the dropdowns for (var i = 0; i < MAX_PLAYERS; ++i) { - var civ = getGUIObjectByName("playerCiv["+i+"]"); + var civ = Engine.GetGUIObjectByName("playerCiv["+i+"]"); civ.list = civListNames; civ.list_data = civListCodes; civ.selected = 0; } } // Initialise the list control containing all the available maps function initMapNameList() { // Get a list of map filenames // TODO: Should verify these are valid maps before adding to list - var mapSelectionBox = getGUIObjectByName("mapSelection") + var mapSelectionBox = Engine.GetGUIObjectByName("mapSelection") var mapFiles; switch (g_GameAttributes.mapType) { case "scenario": case "skirmish": mapFiles = getXMLFileList(g_GameAttributes.mapPath); break; case "random": mapFiles = getJSONFileList(g_GameAttributes.mapPath); break; default: error("initMapNameList: Unexpected map type '"+g_GameAttributes.mapType+"'"); return; } // Apply map filter, if any defined var mapList = []; for (var i = 0; i < mapFiles.length; ++i) { var file = g_GameAttributes.mapPath + mapFiles[i]; var mapData = loadMapData(file); if (g_GameAttributes.mapFilter && mapData && testFilter(g_GameAttributes.mapFilter, mapData.settings)) mapList.push({ "name": getMapDisplayName(file), "file": file }); } // Alphabetically sort the list, ignoring case mapList.sort(sortNameIgnoreCase); if (g_GameAttributes.mapType == "random") mapList.unshift({ "name": "[color=\"orange\"]Random[/color]", "file": "random" }); var mapListNames = [ map.name for each (map in mapList) ]; var mapListFiles = [ map.file for each (map in mapList) ]; // Select the default map var selected = mapListFiles.indexOf(g_GameAttributes.map); // Default to the first element if list is not empty and we can't find the one we searched for if (selected == -1 && mapList.length) { selected = 0; } // Update the list control mapSelectionBox.list = mapListNames; mapSelectionBox.list_data = mapListFiles; mapSelectionBox.selected = selected; } function loadMapData(name) { if (!name) return undefined; if (!g_MapData[name]) { switch (g_GameAttributes.mapType) { case "scenario": case "skirmish": g_MapData[name] = Engine.LoadMapSettings(name); break; case "random": if (name == "random") g_MapData[name] = {settings : {"Name" : "Random", "Description" : "Randomly selects a map from the list"}}; else g_MapData[name] = parseJSONData(name+".json"); break; default: error("loadMapData: Unexpected map type '"+g_GameAttributes.mapType+"'"); return undefined; } } return g_MapData[name]; } //////////////////////////////////////////////////////////////////////////////////////////////// // GUI event handlers function cancelSetup() { Engine.DisconnectNetworkGame(); if (Engine.HasXmppClient()) { // Set player presence Engine.LobbySetPlayerPresence("available"); // Unregister the game if (g_IsController) Engine.SendUnregisterGame(); } } function onTick() { // First tick happens before first render, so don't load yet if (g_LoadingState == 0) { g_LoadingState++; } else if (g_LoadingState == 1) { - getGUIObjectByName("loadingWindow").hidden = true; - getGUIObjectByName("setupWindow").hidden = false; + Engine.GetGUIObjectByName("loadingWindow").hidden = true; + Engine.GetGUIObjectByName("setupWindow").hidden = false; initMain(); g_LoadingState++; } else if (g_LoadingState == 2) { while (true) { var message = Engine.PollNetworkClient(); if (!message) break; handleNetMessage(message); } } } // Called when user selects number of players function selectNumPlayers(num) { // Avoid recursion if (g_IsInGuiUpdate) return; // Network clients can't change number of players if (g_IsNetworked && !g_IsController) return; // Only meaningful for random maps if (g_GameAttributes.mapType != "random") return; // Update player data var pData = g_GameAttributes.settings.PlayerData; if (pData && num < pData.length) { // Remove extra player data g_GameAttributes.settings.PlayerData = pData.slice(0, num); } else { // Add player data from defaults for (var i = pData.length; i < num; ++i) g_GameAttributes.settings.PlayerData.push(g_DefaultPlayerData[i]); } // Some players may have lost their assigned slot for (var guid in g_PlayerAssignments) { var player = g_PlayerAssignments[guid].player; if (player > num) { if (g_IsNetworked) Engine.AssignNetworkPlayer(player, ""); else g_PlayerAssignments = { "local": { "name": "You", "player": 1, "civ": "", "team": -1} }; } } updateGameAttributes(); } // Called when the user selects a map type from the list function selectMapType(type) { // Avoid recursion if (g_IsInGuiUpdate) return; // Network clients can't change map type if (g_IsNetworked && !g_IsController) return; // Reset game attributes g_GameAttributes.map = ""; g_GameAttributes.mapType = type; // Clear old map data g_MapData = {}; // Select correct path switch (g_GameAttributes.mapType) { case "scenario": // Set a default map // TODO: This should be remembered from the last session g_GameAttributes.mapPath = "maps/scenarios/"; g_GameAttributes.map = g_GameAttributes.mapPath + (g_IsNetworked ? DEFAULT_NETWORKED_MAP : DEFAULT_OFFLINE_MAP); break; case "skirmish": g_GameAttributes.mapPath = "maps/skirmishes/"; g_GameAttributes.settings = { PlayerData: g_DefaultPlayerData.slice(0, 4), Seed: Math.floor(Math.random() * 65536), CheatsEnabled: g_GameAttributes.settings.CheatsEnabled }; break; case "random": g_GameAttributes.mapPath = "maps/random/"; g_GameAttributes.settings = { PlayerData: g_DefaultPlayerData.slice(0, 4), Seed: Math.floor(Math.random() * 65536), CheatsEnabled: g_GameAttributes.settings.CheatsEnabled }; break; default: error("selectMapType: Unexpected map type '"+g_GameAttributes.mapType+"'"); return; } initMapNameList(); updateGameAttributes(); } function selectMapFilter(filterName) { // Avoid recursion if (g_IsInGuiUpdate) return; // Network clients can't change map filter if (g_IsNetworked && !g_IsController) return; g_GameAttributes.mapFilter = filterName; initMapNameList(); updateGameAttributes(); } // Called when the user selects a map from the list function selectMap(name) { // Avoid recursion if (g_IsInGuiUpdate) return; // Network clients can't change map if (g_IsNetworked && !g_IsController) return; // Return if we have no map if (!name) return; var mapData = loadMapData(name); var mapSettings = (mapData && mapData.settings ? deepcopy(mapData.settings) : {}); // Copy any new settings g_GameAttributes.map = name; g_GameAttributes.script = mapSettings.Script; if (mapData !== "Random") for (var prop in mapSettings) g_GameAttributes.settings[prop] = mapSettings[prop]; // Use default AI if the map doesn't specify any explicitly for (var i = 0; i < g_GameAttributes.settings.PlayerData.length; ++i) { if (!('AI' in g_GameAttributes.settings.PlayerData[i])) g_GameAttributes.settings.PlayerData[i].AI = g_DefaultPlayerData[i].AI; if (!('AIDiff' in g_GameAttributes.settings.PlayerData[i])) g_GameAttributes.settings.PlayerData[i].AIDiff = g_DefaultPlayerData[i].AIDiff; } // Reset player assignments on map change if (!g_IsNetworked) { // Slot 1 g_PlayerAssignments = { "local": { "name": "You", "player": 1, "civ": "", "team": -1} }; } else { var numPlayers = (mapSettings.PlayerData ? mapSettings.PlayerData.length : g_GameAttributes.settings.PlayerData.length); for (var guid in g_PlayerAssignments) { // Unassign extra players var player = g_PlayerAssignments[guid].player; if (player <= MAX_PLAYERS && player > numPlayers) Engine.AssignNetworkPlayer(player, ""); } } updateGameAttributes(); } function launchGame() { if (g_IsNetworked && !g_IsController) { error("Only host can start game"); return; } // Check that we have a map if (!g_GameAttributes.map) return; if (g_GameAttributes.map == "random") - selectMap(getGUIObjectByName("mapSelection").list_data[Math.floor(Math.random() * - (getGUIObjectByName("mapSelection").list.length - 1)) + 1]); + selectMap(Engine.GetGUIObjectByName("mapSelection").list_data[Math.floor(Math.random() * + (Engine.GetGUIObjectByName("mapSelection").list.length - 1)) + 1]); g_GameAttributes.settings.mapType = g_GameAttributes.mapType; var numPlayers = g_GameAttributes.settings.PlayerData.length; // Assign random civilizations to players with that choice // (this is synchronized because we're the host) var cultures = []; for each (var civ in g_CivData) if (civ.Culture !== undefined && cultures.indexOf(civ.Culture) < 0 && (civ.SelectableInGameSetup === undefined || civ.SelectableInGameSetup)) cultures.push(civ.Culture); var allcivs = new Array(cultures.length); for (var i = 0; i < allcivs.length; ++i) allcivs[i] = []; for each (var civ in g_CivData) if (civ.Culture !== undefined && (civ.SelectableInGameSetup === undefined || civ.SelectableInGameSetup)) allcivs[cultures.indexOf(civ.Culture)].push(civ.Code); const romanNumbers = [undefined, "I", "II", "III", "IV", "V", "VI", "VII", "VIII"]; for (var i = 0; i < numPlayers; ++i) { var civs = allcivs[Math.floor(Math.random()*allcivs.length)]; if (!g_GameAttributes.settings.PlayerData[i].Civ || g_GameAttributes.settings.PlayerData[i].Civ == "random") g_GameAttributes.settings.PlayerData[i].Civ = civs[Math.floor(Math.random()*civs.length)]; // Setting names for AI players. Check if the player is AI and the match is not a scenario if (g_GameAttributes.mapType !== "scenario" && g_GameAttributes.settings.PlayerData[i].AI) { // Get the civ specific names if (g_CivData[g_GameAttributes.settings.PlayerData[i].Civ].AINames !== undefined) var civAINames = shuffleArray(g_CivData[g_GameAttributes.settings.PlayerData[i].Civ].AINames); else var civAINames = [g_CivData[g_GameAttributes.settings.PlayerData[i].Civ].Name]; // Choose the name var usedName = 0; if (i < civAINames.length) var chosenName = civAINames[i]; else var chosenName = civAINames[Math.floor(Math.random() * civAINames.length)]; for (var j = 0; j < numPlayers; ++j) if (g_GameAttributes.settings.PlayerData[j].Name && g_GameAttributes.settings.PlayerData[j].Name.indexOf(chosenName) !== -1) usedName++; // Assign civ specific names to AI players if (usedName) g_GameAttributes.settings.PlayerData[i].Name = chosenName + " " + romanNumbers[usedName+1]; else g_GameAttributes.settings.PlayerData[i].Name = chosenName; } } if (g_IsNetworked) { Engine.SetNetworkGameAttributes(g_GameAttributes); Engine.StartNetworkGame(); } else { // Find the player ID which the user has been assigned to var numPlayers = g_GameAttributes.settings.PlayerData.length; var playerID = -1; for (var i = 0; i < numPlayers; ++i) { - var assignBox = getGUIObjectByName("playerAssignment["+i+"]"); + var assignBox = Engine.GetGUIObjectByName("playerAssignment["+i+"]"); if (assignBox.list_data[assignBox.selected] == "local") playerID = i+1; } // Remove extra player data g_GameAttributes.settings.PlayerData = g_GameAttributes.settings.PlayerData.slice(0, numPlayers); Engine.StartGame(g_GameAttributes, playerID); Engine.SwitchGuiPage("page_loading.xml", { "attribs": g_GameAttributes, "isNetworked" : g_IsNetworked, "playerAssignments": g_PlayerAssignments }); } } //////////////////////////////////////////////////////////////////////////////////////////////// function onGameAttributesChange() { g_IsInGuiUpdate = true; // Don't set any attributes here, just show the changes in GUI var mapName = g_GameAttributes.map || ""; var mapSettings = g_GameAttributes.settings; var numPlayers = (mapSettings.PlayerData ? mapSettings.PlayerData.length : MAX_PLAYERS); // Update some controls for clients if (!g_IsController) { - getGUIObjectByName("mapFilterText").caption = g_GameAttributes.mapFilter; - var mapTypeSelection = getGUIObjectByName("mapTypeSelection"); + Engine.GetGUIObjectByName("mapFilterText").caption = g_GameAttributes.mapFilter; + var mapTypeSelection = Engine.GetGUIObjectByName("mapTypeSelection"); var idx = mapTypeSelection.list_data.indexOf(g_GameAttributes.mapType); - getGUIObjectByName("mapTypeText").caption = mapTypeSelection.list[idx]; - var mapSelectionBox = getGUIObjectByName("mapSelection"); + Engine.GetGUIObjectByName("mapTypeText").caption = mapTypeSelection.list[idx]; + var mapSelectionBox = Engine.GetGUIObjectByName("mapSelection"); mapSelectionBox.selected = mapSelectionBox.list_data.indexOf(mapName); - getGUIObjectByName("mapSelectionText").caption = getMapDisplayName(mapName); - var populationCapBox = getGUIObjectByName("populationCap"); + Engine.GetGUIObjectByName("mapSelectionText").caption = getMapDisplayName(mapName); + var populationCapBox = Engine.GetGUIObjectByName("populationCap"); populationCapBox.selected = populationCapBox.list_data.indexOf(mapSettings.PopulationCap); - var startingResourcesBox = getGUIObjectByName("startingResources"); + var startingResourcesBox = Engine.GetGUIObjectByName("startingResources"); startingResourcesBox.selected = startingResourcesBox.list_data.indexOf(mapSettings.StartingResources); initMapNameList(); } // Controls common to all map types - var numPlayersSelection = getGUIObjectByName("numPlayersSelection"); - var revealMap = getGUIObjectByName("revealMap"); - var victoryCondition = getGUIObjectByName("victoryCondition"); - var lockTeams = getGUIObjectByName("lockTeams"); - var mapSize = getGUIObjectByName("mapSize"); - var enableCheats = getGUIObjectByName("enableCheats"); - var populationCap = getGUIObjectByName("populationCap"); - var startingResources = getGUIObjectByName("startingResources"); - - var numPlayersText= getGUIObjectByName("numPlayersText"); - var mapSizeDesc = getGUIObjectByName("mapSizeDesc"); - var mapSizeText = getGUIObjectByName("mapSizeText"); - var revealMapText = getGUIObjectByName("revealMapText"); - var victoryConditionText = getGUIObjectByName("victoryConditionText"); - var lockTeamsText = getGUIObjectByName("lockTeamsText"); - var enableCheatsText = getGUIObjectByName("enableCheatsText"); - var populationCapText = getGUIObjectByName("populationCapText"); - var startingResourcesText = getGUIObjectByName("startingResourcesText"); - var gameSpeedText = getGUIObjectByName("gameSpeedText"); + var numPlayersSelection = Engine.GetGUIObjectByName("numPlayersSelection"); + var revealMap = Engine.GetGUIObjectByName("revealMap"); + var victoryCondition = Engine.GetGUIObjectByName("victoryCondition"); + var lockTeams = Engine.GetGUIObjectByName("lockTeams"); + var mapSize = Engine.GetGUIObjectByName("mapSize"); + var enableCheats = Engine.GetGUIObjectByName("enableCheats"); + var populationCap = Engine.GetGUIObjectByName("populationCap"); + var startingResources = Engine.GetGUIObjectByName("startingResources"); + + var numPlayersText= Engine.GetGUIObjectByName("numPlayersText"); + var mapSizeDesc = Engine.GetGUIObjectByName("mapSizeDesc"); + var mapSizeText = Engine.GetGUIObjectByName("mapSizeText"); + var revealMapText = Engine.GetGUIObjectByName("revealMapText"); + var victoryConditionText = Engine.GetGUIObjectByName("victoryConditionText"); + var lockTeamsText = Engine.GetGUIObjectByName("lockTeamsText"); + var enableCheatsText = Engine.GetGUIObjectByName("enableCheatsText"); + var populationCapText = Engine.GetGUIObjectByName("populationCapText"); + var startingResourcesText = Engine.GetGUIObjectByName("startingResourcesText"); + var gameSpeedText = Engine.GetGUIObjectByName("gameSpeedText"); var sizeIdx = (g_MapSizes.tiles.indexOf(mapSettings.Size) != -1 ? g_MapSizes.tiles.indexOf(mapSettings.Size) : g_MapSizes["default"]); var speedIdx = (g_GameAttributes.gameSpeed !== undefined && g_GameSpeeds.speeds.indexOf(g_GameAttributes.gameSpeed) != -1) ? g_GameSpeeds.speeds.indexOf(g_GameAttributes.gameSpeed) : g_GameSpeeds["default"]; var victoryIdx = (VICTORY_DATA.indexOf(mapSettings.GameType) != -1 ? VICTORY_DATA.indexOf(mapSettings.GameType) : VICTORY_DEFAULTIDX); enableCheats.checked = (g_GameAttributes.settings.CheatsEnabled === undefined || !g_GameAttributes.settings.CheatsEnabled ? false : true) enableCheatsText.caption = (enableCheats.checked ? "Yes" : "No"); gameSpeedText.caption = g_GameSpeeds.names[speedIdx]; populationCap.selected = (POPULATION_CAP_DATA.indexOf(mapSettings.PopulationCap) != -1 ? POPULATION_CAP_DATA.indexOf(mapSettings.PopulationCap) : POPULATION_CAP_DEFAULTIDX); populationCapText.caption = POPULATION_CAP[populationCap.selected]; startingResources.selected = (STARTING_RESOURCES_DATA.indexOf(mapSettings.StartingResources) != -1 ? STARTING_RESOURCES_DATA.indexOf(mapSettings.StartingResources) : STARTING_RESOURCES_DEFAULTIDX); startingResourcesText.caption = STARTING_RESOURCES[startingResources.selected]; // Update map preview - getGUIObjectByName("mapPreview").sprite = "cropped:(0.78125,0.5859375)session/icons/mappreview/" + getMapPreview(mapName); + Engine.GetGUIObjectByName("mapPreview").sprite = "cropped:(0.78125,0.5859375)session/icons/mappreview/" + getMapPreview(mapName); // Handle map type specific logic switch (g_GameAttributes.mapType) { case "random": mapSizeDesc.hidden = false; if (g_IsController) { //Host numPlayersSelection.selected = numPlayers - 1; numPlayersSelection.hidden = false; mapSize.hidden = false; revealMap.hidden = false; victoryCondition.hidden = false; lockTeams.hidden = false; populationCap.hidden = false; startingResources.hidden = false; numPlayersText.hidden = true; mapSizeText.hidden = true; revealMapText.hidden = true; victoryConditionText.hidden = true; lockTeamsText.hidden = true; populationCapText.hidden = true; startingResourcesText.hidden = true; mapSizeText.caption = "Map size:"; mapSize.selected = sizeIdx; revealMapText.caption = "Reveal map:"; revealMap.checked = (mapSettings.RevealMap ? true : false); victoryConditionText.caption = "Victory condition:"; victoryCondition.selected = victoryIdx; lockTeamsText.caption = "Teams locked:"; lockTeams.checked = (mapSettings.LockTeams ? true : false); } else { // Client numPlayersText.hidden = false; mapSizeText.hidden = false; revealMapText.hidden = false; victoryConditionText.hidden = false; lockTeamsText.hidden = false; populationCap.hidden = true; populationCapText.hidden = false; startingResources.hidden = true; startingResourcesText.hidden = false; numPlayersText.caption = numPlayers; mapSizeText.caption = g_MapSizes.names[sizeIdx]; revealMapText.caption = (mapSettings.RevealMap ? "Yes" : "No"); victoryConditionText.caption = VICTORY_TEXT[victoryIdx]; lockTeamsText.caption = (mapSettings.LockTeams ? "Yes" : "No"); } break; case "skirmish": mapSizeText.caption = "Default"; numPlayersText.caption = numPlayers; numPlayersSelection.hidden = true; mapSize.hidden = true; mapSizeText.hidden = true; mapSizeDesc.hidden = true; if (g_IsController) { //Host revealMap.hidden = false; victoryCondition.hidden = false; lockTeams.hidden = false; populationCap.hidden = false; startingResources.hidden = false; numPlayersText.hidden = false; revealMapText.hidden = true; victoryConditionText.hidden = true; lockTeamsText.hidden = true; populationCapText.hidden = true; startingResourcesText.hidden = true; revealMapText.caption = "Reveal map:"; revealMap.checked = (mapSettings.RevealMap ? true : false); victoryConditionText.caption = "Victory condition:"; victoryCondition.selected = victoryIdx; lockTeamsText.caption = "Teams locked:"; lockTeams.checked = (mapSettings.LockTeams ? true : false); } else { // Client numPlayersText.hidden = false; revealMapText.hidden = false; victoryConditionText.hidden = false; lockTeamsText.hidden = false; populationCap.hidden = true; populationCapText.hidden = false; startingResources.hidden = true; startingResourcesText.hidden = false; revealMapText.caption = (mapSettings.RevealMap ? "Yes" : "No"); victoryConditionText.caption = VICTORY_TEXT[victoryIdx]; lockTeamsText.caption = (mapSettings.LockTeams ? "Yes" : "No"); } break; case "scenario": // For scenario just reflect settings for the current map numPlayersSelection.hidden = true; mapSize.hidden = true; revealMap.hidden = true; victoryCondition.hidden = true; lockTeams.hidden = true; numPlayersText.hidden = false; mapSizeText.hidden = true; mapSizeDesc.hidden = true; revealMapText.hidden = false; victoryConditionText.hidden = false; lockTeamsText.hidden = false; populationCap.hidden = true; populationCapText.hidden = false; startingResources.hidden = true; startingResourcesText.hidden = false; numPlayersText.caption = numPlayers; mapSizeText.caption = "Default"; revealMapText.caption = (mapSettings.RevealMap ? "Yes" : "No"); victoryConditionText.caption = VICTORY_TEXT[victoryIdx]; lockTeamsText.caption = (mapSettings.LockTeams ? "Yes" : "No"); - getGUIObjectByName("populationCap").selected = POPULATION_CAP_DEFAULTIDX; + Engine.GetGUIObjectByName("populationCap").selected = POPULATION_CAP_DEFAULTIDX; break; default: error("onGameAttributesChange: Unexpected map type '"+g_GameAttributes.mapType+"'"); return; } // Display map name - getGUIObjectByName("mapInfoName").caption = getMapDisplayName(mapName); + Engine.GetGUIObjectByName("mapInfoName").caption = getMapDisplayName(mapName); // Load the description from the map file, if there is one var description = mapSettings.Description || "Sorry, no description available."; if (g_GameAttributes.mapFilter == "Naval Maps") description += g_NavalWarning; // Describe the number of players var playerString = numPlayers + " " + (numPlayers == 1 ? "player" : "players") + ". "; for (var i = 0; i < MAX_PLAYERS; ++i) { // Show only needed player slots - getGUIObjectByName("playerBox["+i+"]").hidden = (i >= numPlayers); + Engine.GetGUIObjectByName("playerBox["+i+"]").hidden = (i >= numPlayers); // Show player data or defaults as necessary if (i >= numPlayers) continue; - var pName = getGUIObjectByName("playerName["+i+"]"); - var pCiv = getGUIObjectByName("playerCiv["+i+"]"); - var pCivText = getGUIObjectByName("playerCivText["+i+"]"); - var pTeam = getGUIObjectByName("playerTeam["+i+"]"); - var pTeamText = getGUIObjectByName("playerTeamText["+i+"]"); - var pColor = getGUIObjectByName("playerColour["+i+"]"); + var pName = Engine.GetGUIObjectByName("playerName["+i+"]"); + var pCiv = Engine.GetGUIObjectByName("playerCiv["+i+"]"); + var pCivText = Engine.GetGUIObjectByName("playerCivText["+i+"]"); + var pTeam = Engine.GetGUIObjectByName("playerTeam["+i+"]"); + var pTeamText = Engine.GetGUIObjectByName("playerTeamText["+i+"]"); + var pColor = Engine.GetGUIObjectByName("playerColour["+i+"]"); // Player data / defaults var pData = mapSettings.PlayerData ? mapSettings.PlayerData[i] : {}; var pDefs = g_DefaultPlayerData ? g_DefaultPlayerData[i] : {}; // Common to all game types var color = iColorToString(getSetting(pData, pDefs, "Colour")); pColor.sprite = "colour:"+color+" 100"; pName.caption = getSetting(pData, pDefs, "Name"); var team = getSetting(pData, pDefs, "Team"); var civ = getSetting(pData, pDefs, "Civ"); // For clients or scenarios, hide some player dropdowns if (!g_IsController || g_GameAttributes.mapType == "scenario") { pCivText.hidden = false; pCiv.hidden = true; pTeamText.hidden = false; pTeam.hidden = true; // Set text values if (civ == "random") pCivText.caption = "[color=\"orange\"]Random"; else pCivText.caption = g_CivData[civ].Name; pTeamText.caption = (team !== undefined && team >= 0) ? team+1 : "-"; } else if (g_GameAttributes.mapType != "scenario") { pCivText.hidden = true; pCiv.hidden = false; pTeamText.hidden = true; pTeam.hidden = false; // Set dropdown values pCiv.selected = (civ ? pCiv.list_data.indexOf(civ) : 0); pTeam.selected = (team !== undefined && team >= 0) ? team+1 : 0; } } - getGUIObjectByName("mapInfoDescription").caption = playerString + description; + Engine.GetGUIObjectByName("mapInfoDescription").caption = playerString + description; g_IsInGuiUpdate = false; // Game attributes include AI settings, so update the player list updatePlayerList(); } function updateGameAttributes() { if (g_IsNetworked) { Engine.SetNetworkGameAttributes(g_GameAttributes); if (g_IsController && g_LoadingState >= 2) sendRegisterGameStanza(); } else { onGameAttributesChange(); } } function updatePlayerList() { g_IsInGuiUpdate = true; var hostNameList = []; var hostGuidList = []; var assignments = []; var aiAssignments = {}; var noAssignment; var assignedCount = 0; for (var guid in g_PlayerAssignments) { var name = g_PlayerAssignments[guid].name; var hostID = hostNameList.length; var player = g_PlayerAssignments[guid].player; hostNameList.push(name); hostGuidList.push(guid); assignments[player] = hostID; if (player != 255) assignedCount++; } // Only enable start button if we have enough assigned players if (g_IsController) - getGUIObjectByName("startGame").enabled = (assignedCount > 0); + Engine.GetGUIObjectByName("startGame").enabled = (assignedCount > 0); for each (var ai in g_AIs) { if (ai.data.hidden) { // If the map uses a hidden AI then don't hide it var usedByMap = false; for (var i = 0; i < MAX_PLAYERS; ++i) if (i < g_GameAttributes.settings.PlayerData.length && g_GameAttributes.settings.PlayerData[i].AI == ai.id) { usedByMap = true; break; } if (!usedByMap) continue; } // Give AI a different color so it stands out aiAssignments[ai.id] = hostNameList.length; hostNameList.push("[color=\"70 150 70 255\"]AI: " + ai.data.name); hostGuidList.push("ai:" + ai.id); } noAssignment = hostNameList.length; hostNameList.push("[color=\"140 140 140 255\"]Unassigned"); hostGuidList.push(""); for (var i = 0; i < MAX_PLAYERS; ++i) { let playerSlot = i; let playerID = i+1; // we don't show Gaia, so first slot is ID 1 var selection = assignments[playerID]; - var configButton = getGUIObjectByName("playerConfig["+i+"]"); + var configButton = Engine.GetGUIObjectByName("playerConfig["+i+"]"); configButton.hidden = true; // Look for valid player slots if (playerSlot >= g_GameAttributes.settings.PlayerData.length) continue; // If no human is assigned, look for an AI instead if (selection === undefined) { var aiId = g_GameAttributes.settings.PlayerData[playerSlot].AI; if (aiId) { // Check for a valid AI if (aiId in aiAssignments) selection = aiAssignments[aiId]; else warn("AI \""+aiId+"\" not present. Defaulting to unassigned."); } if (!selection) 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", { ais: g_AIs, id: g_GameAttributes.settings.PlayerData[playerSlot].AI, difficulty: g_GameAttributes.settings.PlayerData[playerSlot].AIDiff, callback: function(ai) { g_GameAttributes.settings.PlayerData[playerSlot].AI = ai.id; g_GameAttributes.settings.PlayerData[playerSlot].AIDiff = ai.difficulty; if (g_IsNetworked) Engine.SetNetworkGameAttributes(g_GameAttributes); else updatePlayerList(); } }); }; } } // 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 else if (g_IsController && g_GameAttributes.settings.PlayerData[playerSlot].AI && g_GameAttributes.settings.PlayerData[playerSlot].AI != "") { g_GameAttributes.settings.PlayerData[playerSlot].AI = ""; if (g_IsNetworked) Engine.SetNetworkGameAttributes(g_GameAttributes); } - var assignBox = getGUIObjectByName("playerAssignment["+i+"]"); + var assignBox = Engine.GetGUIObjectByName("playerAssignment["+i+"]"); assignBox.list = hostNameList; assignBox.list_data = hostGuidList; if (assignBox.selected != selection) assignBox.selected = selection; if (g_IsNetworked && g_IsController) { assignBox.onselectionchange = function () { if (!g_IsInGuiUpdate) { var guid = hostGuidList[this.selected]; if (guid == "") { // Unassign any host from this player slot Engine.AssignNetworkPlayer(playerID, ""); // Remove AI from this player slot g_GameAttributes.settings.PlayerData[playerSlot].AI = ""; } else if (guid.substr(0, 3) == "ai:") { // Unassign any host from this player slot Engine.AssignNetworkPlayer(playerID, ""); // Set the AI for this player slot g_GameAttributes.settings.PlayerData[playerSlot].AI = guid.substr(3); } else swapPlayers(guid, playerSlot); Engine.SetNetworkGameAttributes(g_GameAttributes); } }; } else if (!g_IsNetworked) { assignBox.onselectionchange = function () { if (!g_IsInGuiUpdate) { var guid = hostGuidList[this.selected]; if (guid == "") { // Remove AI from this player slot g_GameAttributes.settings.PlayerData[playerSlot].AI = ""; } else if (guid.substr(0, 3) == "ai:") { // Set the AI for this player slot g_GameAttributes.settings.PlayerData[playerSlot].AI = guid.substr(3); } else swapPlayers(guid, playerSlot); updatePlayerList(); } }; } } g_IsInGuiUpdate = false; } function swapPlayers(guid, newSlot) { // Player slots are indexed from 0 as Gaia is omitted. var newPlayerID = newSlot + 1; var playerID = g_PlayerAssignments[guid].player; // Attempt to swap the player or AI occupying the target slot, // if any, into the slot this player is currently in. if (playerID != 255) { for (var i in g_PlayerAssignments) { // Move the player in the destination slot into the current slot. if (g_PlayerAssignments[i].player == newPlayerID) { if (g_IsNetworked) Engine.AssignNetworkPlayer(playerID, i); else g_PlayerAssignments[i].player = playerID; break; } } // Transfer the AI from the target slot to the current slot. g_GameAttributes.settings.PlayerData[playerID - 1].AI = g_GameAttributes.settings.PlayerData[newSlot].AI; } if (g_IsNetworked) Engine.AssignNetworkPlayer(newPlayerID, guid); else g_PlayerAssignments[guid].player = newPlayerID; // Remove AI from this player slot g_GameAttributes.settings.PlayerData[newSlot].AI = ""; } function submitChatInput() { - var input = getGUIObjectByName("chatInput"); + var input = Engine.GetGUIObjectByName("chatInput"); var text = input.caption; if (text.length) { Engine.SendNetworkChat(text); input.caption = ""; } } function addChatMessage(msg) { var username = escapeText(msg.username || g_PlayerAssignments[msg.guid].name); var message = escapeText(msg.text); // TODO: Maybe host should have distinct font/color? var color = "white"; if (g_PlayerAssignments[msg.guid] && g_PlayerAssignments[msg.guid].player != 255) { // Valid player who has been assigned - get player colour var player = g_PlayerAssignments[msg.guid].player - 1; var mapName = g_GameAttributes.map; var mapData = loadMapData(mapName); var mapSettings = (mapData && mapData.settings ? mapData.settings : {}); var pData = mapSettings.PlayerData ? mapSettings.PlayerData[player] : {}; var pDefs = g_DefaultPlayerData ? g_DefaultPlayerData[player] : {}; color = iColorToString(getSetting(pData, pDefs, "Colour")); } var formatted; switch (msg.type) { case "connect": formatted = '[font="serif-bold-13"][color="'+ color +'"]' + username + '[/color][/font] [color="gold"]has joined[/color]'; break; case "disconnect": formatted = '[font="serif-bold-13"][color="'+ color +'"]' + username + '[/color][/font] [color="gold"]has left[/color]'; break; case "message": formatted = '[font="serif-bold-13"]<[color="'+ color +'"]' + username + '[/color]>[/font] ' + message; break; default: error("Invalid chat message '" + uneval(msg) + "'"); return; } g_ChatMessages.push(formatted); - getGUIObjectByName("chatText").caption = g_ChatMessages.join("\n"); + Engine.GetGUIObjectByName("chatText").caption = g_ChatMessages.join("\n"); } function toggleMoreOptions() { - getGUIObjectByName("moreOptions").hidden = !getGUIObjectByName("moreOptions").hidden; + Engine.GetGUIObjectByName("moreOptions").hidden = !Engine.GetGUIObjectByName("moreOptions").hidden; } //////////////////////////////////////////////////////////////////////////////////////////////// // Basic map filters API // Add a new map list filter function addFilter(name, filterFunc) { if (filterFunc instanceof Object) { // Basic validity test var newFilter = {}; newFilter.name = name; newFilter.filter = filterFunc; g_MapFilters.push(newFilter); } else { error("Invalid map filter: "+name); } } // Get array of map filter names function getFilters() { var filters = []; for (var i = 0; i < g_MapFilters.length; ++i) filters.push(g_MapFilters[i].name); return filters; } // Test map filter on given map settings object function testFilter(name, mapSettings) { for (var i = 0; i < g_MapFilters.length; ++i) if (g_MapFilters[i].name == name) return g_MapFilters[i].filter(mapSettings); error("Invalid map filter: "+name); return false; } // Test an array of keywords against a match array using AND logic function keywordTestAND(keywords, matches) { if (!keywords || !matches) return false; for (var m = 0; m < matches.length; ++m) if (keywords.indexOf(matches[m]) == -1) return false; return true; } // Test an array of keywords against a match array using OR logic function keywordTestOR(keywords, matches) { if (!keywords || !matches) return false; for (var m = 0; m < matches.length; ++m) if (keywords.indexOf(matches[m]) != -1) return true; return false; } function sendRegisterGameStanza() { if (!Engine.HasXmppClient()) return; - var selectedMapSize = getGUIObjectByName("mapSize").selected; - var selectedVictoryCondition = getGUIObjectByName("victoryCondition").selected; + var selectedMapSize = Engine.GetGUIObjectByName("mapSize").selected; + var selectedVictoryCondition = Engine.GetGUIObjectByName("victoryCondition").selected; // Map sizes only apply to random maps. if (g_GameAttributes.mapType == "random") - var mapSize = getGUIObjectByName("mapSize").list[selectedMapSize]; + var mapSize = Engine.GetGUIObjectByName("mapSize").list[selectedMapSize]; else var mapSize = "Default"; - var victoryCondition = getGUIObjectByName("victoryCondition").list[selectedVictoryCondition]; + var victoryCondition = Engine.GetGUIObjectByName("victoryCondition").list[selectedVictoryCondition]; var numberOfPlayers = Object.keys(g_PlayerAssignments).length; var players = [ assignment.name for each (assignment in g_PlayerAssignments) ].join(", "); var nbp = numberOfPlayers ? numberOfPlayers : 1; var tnbp = g_GameAttributes.settings.PlayerData.length; var gameData = { "name":g_ServerName, "mapName":g_GameAttributes.map, "niceMapName":getMapDisplayName(g_GameAttributes.map), "mapSize":mapSize, "mapType":g_GameAttributes.mapType, "victoryCondition":victoryCondition, "nbp":nbp, "tnbp":tnbp, "players":players }; Engine.SendRegisterGame(gameData); } Index: ps/trunk/binaries/data/mods/public/gui/gamesetup/gamesetup_mp.js =================================================================== --- ps/trunk/binaries/data/mods/public/gui/gamesetup/gamesetup_mp.js (revision 14495) +++ ps/trunk/binaries/data/mods/public/gui/gamesetup/gamesetup_mp.js (revision 14496) @@ -1,229 +1,229 @@ var g_IsConnecting = false; var g_GameType; // "server" or "client" var g_ServerName = ""; var g_IsRejoining = false; var g_GameAttributes; // used when rejoining var g_PlayerAssignments; // used when rejoining function init(attribs) { switch (attribs.multiplayerGameType) { case "join": if(Engine.HasXmppClient()) { if (startJoin(attribs.name, attribs.ip)) switchSetupPage("pageJoin", "pageConnecting"); } else { - getGUIObjectByName("pageJoin").hidden = false; - getGUIObjectByName("pageHost").hidden = true; + Engine.GetGUIObjectByName("pageJoin").hidden = false; + Engine.GetGUIObjectByName("pageHost").hidden = true; } break; case "host": - getGUIObjectByName("pageJoin").hidden = true; - getGUIObjectByName("pageHost").hidden = false; + Engine.GetGUIObjectByName("pageJoin").hidden = true; + Engine.GetGUIObjectByName("pageHost").hidden = false; if(Engine.HasXmppClient()) { - getGUIObjectByName("hostServerNameWrapper").hidden = false; - getGUIObjectByName("hostPlayerName").caption = attribs.name; - getGUIObjectByName("hostServerName").caption = attribs.name + "'s game"; + Engine.GetGUIObjectByName("hostServerNameWrapper").hidden = false; + Engine.GetGUIObjectByName("hostPlayerName").caption = attribs.name; + Engine.GetGUIObjectByName("hostServerName").caption = attribs.name + "'s game"; } else - getGUIObjectByName("hostPlayerNameWrapper").hidden = false; + Engine.GetGUIObjectByName("hostPlayerNameWrapper").hidden = false; break; default: error("Unrecognised multiplayer game type : " + attribs.multiplayerGameType); break; } } function cancelSetup() { if (g_IsConnecting) Engine.DisconnectNetworkGame(); // Set player lobby presence if (Engine.HasXmppClient()) Engine.LobbySetPlayerPresence("available"); Engine.PopGuiPage(); } function startConnectionStatus(type) { g_GameType = type; g_IsConnecting = true; g_IsRejoining = false; - getGUIObjectByName("connectionStatus").caption = "Connecting to server..."; + Engine.GetGUIObjectByName("connectionStatus").caption = "Connecting to server..."; } function onTick() { if (!g_IsConnecting) return; pollAndHandleNetworkClient(); } function pollAndHandleNetworkClient() { while (true) { var message = Engine.PollNetworkClient(); if (!message) break; log("Net message: "+uneval(message)); // If we're rejoining an active game, we don't want to actually display // the game setup screen, so perform similar processing to gamesetup.js // in this screen if (g_IsRejoining) { switch (message.type) { case "netstatus": switch (message.status) { case "disconnected": cancelSetup(); reportDisconnect(message.reason); return; default: error("Unrecognised netstatus type "+message.status); break; } break; case "gamesetup": g_GameAttributes = message.data; break; case "players": g_PlayerAssignments = message.hosts; break; case "start": Engine.SwitchGuiPage("page_loading.xml", { "attribs": g_GameAttributes, "isNetworked" : true, "playerAssignments": g_PlayerAssignments }); break; case "chat": // Ignore, since we have nowhere to display chat messages break; default: error("Unrecognised net message type "+message.type); } } else { // Not rejoining - just trying to connect to server switch (message.type) { case "netstatus": switch (message.status) { case "connected": - getGUIObjectByName("connectionStatus").caption = "Registering with server..."; + Engine.GetGUIObjectByName("connectionStatus").caption = "Registering with server..."; break; case "authenticated": if (message.rejoining) { - getGUIObjectByName("connectionStatus").caption = "Game has already started - rejoining..."; + Engine.GetGUIObjectByName("connectionStatus").caption = "Game has already started - rejoining..."; g_IsRejoining = true; return; // we'll process the game setup messages in the next tick } else { Engine.SwitchGuiPage("page_gamesetup.xml", { "type": g_GameType, "serverName": g_ServerName }); return; // don't process any more messages - leave them for the game GUI loop } case "disconnected": cancelSetup(); reportDisconnect(message.reason); return; default: error("Unrecognised netstatus type "+message.status); break; } break; default: error("Unrecognised net message type "+message.type); break; } } } } function switchSetupPage(oldpage, newpage) { - getGUIObjectByName(oldpage).hidden = true; - getGUIObjectByName(newpage).hidden = false; + Engine.GetGUIObjectByName(oldpage).hidden = true; + Engine.GetGUIObjectByName(newpage).hidden = false; } function startHost(playername, servername) { // Disallow identically named games in the multiplayer lobby if (Engine.HasXmppClient()) { for each (g in Engine.GetGameList()) { if (g.name === servername) { - getGUIObjectByName("hostFeedback").caption = "Game name already in use."; + Engine.GetGUIObjectByName("hostFeedback").caption = "Game name already in use."; return false; } } } try { Engine.StartNetworkHost(playername); } catch (e) { cancelSetup(); messageBox(400, 200, "Cannot host game: " + e.message + ".", "Error", 2); return false; } startConnectionStatus("server"); g_ServerName = servername; // Set player lobby presence if (Engine.HasXmppClient()) Engine.LobbySetPlayerPresence("playing"); return true; } function startJoin(playername, ip) { try { Engine.StartNetworkJoin(playername, ip); } catch (e) { cancelSetup(); messageBox(400, 200, "Cannot join game: " + e.message + ".", "Error", 2); return false; } startConnectionStatus("client"); // Set player lobby presence if (Engine.HasXmppClient()) Engine.LobbySetPlayerPresence("playing"); return true; } Index: ps/trunk/binaries/data/mods/public/gui/gamesetup/gamesetup_mp.xml =================================================================== --- ps/trunk/binaries/data/mods/public/gui/gamesetup/gamesetup_mp.xml (revision 14495) +++ ps/trunk/binaries/data/mods/public/gui/gamesetup/gamesetup_mp.xml (revision 14496) @@ -1,121 +1,121 @@ Engine.PopGuiPage();