Ticket #785: gamespeed-minimal-03182013.patch

File gamespeed-minimal-03182013.patch, 18.6 KB (added by historic_bruno, 11 years ago)
  • binaries/data/mods/public/gui/common/functions_utility.js

     
    134134
    135135// ====================================================================
    136136
    137 // Load default player data, for when it's not otherwise specified
    138 function initPlayerDefaults()
     137// Parse and return JSON data from file in simulation/data/*
     138// returns valid object or undefined on error
     139function parseJSONFromDataFile(filename)
    139140{
    140     var filename = "simulation/data/player_defaults.json";
    141     var defaults = [];
    142     var rawData = readFile(filename);
     141    var path = "simulation/data/"+filename;
     142    var rawData = readFile(path);
    143143    if (!rawData)
    144         error("Failed to read player defaults file: "+filename);
    145    
     144        error("Failed to read file: "+path);
     145
    146146    try
    147     {   // Catch nasty errors from JSON parsing
     147    {
     148        // Catch nasty errors from JSON parsing
    148149        // TODO: Need more info from the parser on why it failed: line number, position, etc!
    149150        var data = JSON.parse(rawData);
    150         if (!data || !data.PlayerData)
    151             error("Failed to parse player defaults in: "+filename+" (check for valid JSON data)");
    152        
    153         defaults = data.PlayerData;
     151        return data;
    154152    }
    155153    catch(err)
    156154    {
    157         error(err.toString()+": parsing player defaults in "+filename);
     155        error(err.toString()+": parsing JSON data in "+path);
    158156    }
    159    
     157
     158    return undefined;
     159}
     160
     161// ====================================================================
     162
     163// Load default player data, for when it's not otherwise specified
     164function initPlayerDefaults()
     165{
     166    var defaults = [];
     167
     168    var data = parseJSONFromDataFile("player_defaults.json");
     169    if (!data || !data.PlayerData)
     170        error("Failed to parse player defaults in player_defaults.json (check for valid JSON data)");
     171    else
     172        defaults = data.PlayerData;
     173
    160174    return defaults;
    161175}
    162176
     
    165179// Load map size data
    166180function initMapSizes()
    167181{
    168     var filename = "simulation/data/map_sizes.json";
    169182    var sizes = {
    170         names: [],
    171         tiles: [],
    172         default: 0
     183        "names":[],
     184        "tiles": [],
     185        "default": 0
    173186    };
    174     var rawData = readFile(filename);
    175     if (!rawData)
    176         error("Failed to read map sizes file: "+filename);
    177    
    178     try
    179     {   // Catch nasty errors from JSON parsing
    180         // TODO: Need more info from the parser on why it failed: line number, position, etc!
    181         var data = JSON.parse(rawData);
    182         if (!data || !data.Sizes)
    183             error("Failed to parse map sizes in: "+filename+" (check for valid JSON data)");
    184        
     187
     188    var data = parseJSONFromDataFile("map_sizes.json");
     189    if (!data || !data.Sizes)
     190        error("Failed to parse map sizes in map_sizes.json (check for valid JSON data)");
     191    else
     192    {
    185193        for (var i = 0; i < data.Sizes.length; ++i)
    186194        {
    187195            sizes.names.push(data.Sizes[i].LongName);
    188196            sizes.tiles.push(data.Sizes[i].Tiles);
    189            
     197
    190198            if (data.Sizes[i].Default)
    191                 sizes.default = i;
     199                sizes["default"] = i;
    192200        }
    193201    }
    194     catch(err)
     202
     203    return sizes;
     204}
     205
     206// ====================================================================
     207
     208// Load game speed data
     209function initGameSpeeds()
     210{
     211    var gameSpeeds = {
     212        "names": [],
     213        "speeds": [],
     214        "default": 0
     215    };
     216
     217    var data = parseJSONFromDataFile("game_speeds.json");
     218    if (!data || !data.Speeds)
     219        error("Failed to parse game speeds in game_speeds.json (check for valid JSON data)");
     220    else
    195221    {
    196         error(err.toString()+": parsing map sizes in "+filename);
     222        for (var i = 0; i < data.Speeds.length; ++i)
     223        {
     224            gameSpeeds.names.push(data.Speeds[i].Name);
     225            gameSpeeds.speeds.push(data.Speeds[i].Speed);
     226
     227            if (data.Speeds[i].Default)
     228                gameSpeeds["default"] = i;
     229        }
    197230    }
    198    
    199     return sizes;
     231
     232    return gameSpeeds;
    200233}
    201234
     235
    202236// ====================================================================
    203237
    204238// Convert integer color values to string (for use in GUI objects)
  • binaries/data/mods/public/gui/gamesetup/gamesetup.js

     
    3636    settings: {}
    3737};
    3838
     39var g_GameSpeeds = {};
    3940var g_MapSizes = {};
    4041
    4142var g_AIs = [];
     
    9798    g_DefaultPlayerData.shift();
    9899    for (var i = 0; i < g_DefaultPlayerData.length; i++)
    99100        g_DefaultPlayerData[i].Civ = "random";
    100    
     101
     102    g_GameSpeeds = initGameSpeeds();
    101103    g_MapSizes = initMapSizes();
    102104
    103105    // Init civs
     
    135137        numPlayersSelection.list = players;
    136138        numPlayersSelection.list_data = players;
    137139        numPlayersSelection.selected = MAX_PLAYERS - 1;
     140       
     141        var gameSpeed = getGUIObjectByName("gameSpeed");
     142        gameSpeed.hidden = false;
     143        getGUIObjectByName("gameSpeedText").hidden = true;
     144        gameSpeed.list = g_GameSpeeds.names;
     145        gameSpeed.list_data = g_GameSpeeds.speeds;
     146        gameSpeed.onSelectionChange = function()
     147        {
     148            // Update attributes so other players can see change
     149            if (this.selected != -1)
     150                g_GameAttributes.gameSpeed = g_GameSpeeds.speeds[this.selected];
    138151
     152            if (!g_IsInGuiUpdate)
     153                updateGameAttributes();
     154        }
     155        gameSpeed.selected = g_GameSpeeds["default"];
     156
    139157        var populationCaps = getGUIObjectByName("populationCap");
    140158        populationCaps.list = POPULATION_CAP;
    141159        populationCaps.list_data = POPULATION_CAP_DATA;
     
    245263        getGUIObjectByName("mapSelection").hidden = true;
    246264        getGUIObjectByName("victoryConditionText").hidden = false;
    247265        getGUIObjectByName("victoryCondition").hidden = true;
    248        
     266        getGUIObjectByName("gameSpeedText").hidden = false;
     267        getGUIObjectByName("gameSpeed").hidden = true;
     268
    249269        // Disable player and game options controls
    250270        // TODO: Shouldn't players be able to choose their own assignment?
    251271        for (var i = 0; i < MAX_PLAYERS; ++i)
     
    955975    var enableCheatsText = getGUIObjectByName("enableCheatsText");
    956976    var populationCapText = getGUIObjectByName("populationCapText");
    957977    var startingResourcesText = getGUIObjectByName("startingResourcesText");
    958    
    959     var sizeIdx = (g_MapSizes.tiles.indexOf(mapSettings.Size) != -1 ? g_MapSizes.tiles.indexOf(mapSettings.Size) : g_MapSizes.default);
     978    var gameSpeedText = getGUIObjectByName("gameSpeedText");
     979
     980    var sizeIdx = (g_MapSizes.tiles.indexOf(mapSettings.Size) != -1 ? g_MapSizes.tiles.indexOf(mapSettings.Size) : g_MapSizes["default"]);
     981    var speedIdx = (g_GameAttributes.gameSpeed !== undefined && g_GameSpeeds.names.indexOf(g_GameAttributes.gameSpeed) != -1) ? g_GameSpeeds.names.indexOf(g_GameAttributes.gameSpeed) : g_GameSpeeds["default"];
    960982    var victoryIdx = (VICTORY_DATA.indexOf(mapSettings.GameType) != -1 ? VICTORY_DATA.indexOf(mapSettings.GameType) : VICTORY_DEFAULTIDX);
    961983    enableCheats.checked = (g_GameAttributes.settings.CheatsEnabled === undefined || !g_GameAttributes.settings.CheatsEnabled ? false : true)
    962984    enableCheatsText.caption = (enableCheats.checked ? "Yes" : "No");
     985    gameSpeedText.caption = g_GameSpeeds.names[speedIdx];
    963986    populationCap.selected = (POPULATION_CAP_DATA.indexOf(mapSettings.PopulationCap) != -1 ? POPULATION_CAP_DATA.indexOf(mapSettings.PopulationCap) : POPULATION_CAP_DEFAULTIDX);
    964987    populationCapText.caption = POPULATION_CAP[populationCap.selected];
    965988    startingResources.selected = (STARTING_RESOURCES_DATA.indexOf(mapSettings.StartingResources) != -1 ? STARTING_RESOURCES_DATA.indexOf(mapSettings.StartingResources) : STARTING_RESOURCES_DEFAULTIDX);
    966989    startingResourcesText.caption = STARTING_RESOURCES[startingResources.selected];
     990
    967991    // Handle map type specific logic
    968992    switch (g_GameAttributes.mapType)
    969993    {
  • binaries/data/mods/public/gui/gamesetup/gamesetup.xml

     
    178178        </object>
    179179
    180180        <!-- More Options -->
    181         <object name="moreOptions" type="image" sprite="StoneWindow" size="50%-200 50%-120 50%+200 50%+155" z="70" hidden="true">
     181        <object name="moreOptions" type="image" sprite="StoneWindow" size="50%-200 50%-150 50%+200 50%+155" z="70" hidden="true">
    182182            <object style="TitleText" type="text" size="50%-128 11 50%+128 27">
    183183                More Options
    184184            </object>
    185185           
    186186            <object size="14 38 94% 66">
    187187                <object size="0 0 40% 28">
    188                     <object size="0 0 100% 100%" type="text" style="RightLabelText">Victory condition:</object>
     188                    <object size="0 0 100% 100%" type="text" style="RightLabelText">Game Speed:</object>
    189189                </object>
     190                <object name="gameSpeedText" size="40% 0 100% 100%" type="text" style="LeftLabelText"/>
     191                <object name="gameSpeed" size="40% 0 100% 28" type="dropdown" style="StoneDropDown" hidden="true" tooltip_style="onscreenToolTip" tooltip="Select game speed."/>
     192            </object>
     193
     194            <object size="14 68 94% 96">
     195                <object size="0 0 40% 28">
     196                    <object size="0 0 100% 100%" type="text" style="RightLabelText">Victory Condition:</object>
     197                </object>
    190198                <object name="victoryConditionText" size="40% 0 100% 100%" type="text" style="LeftLabelText"/>
    191199                <object name="victoryCondition" size="40% 0 100% 28" type="dropdown" style="StoneDropDown" hidden="true" tooltip_style="onscreenToolTip" tooltip="Select victory condition."/>
    192200            </object>
    193201           
    194             <object size="14 68 94% 96">
     202            <object size="14 98 94% 126">
    195203                <object size="0 0 40% 28">
    196204                    <object size="0 0 100% 100%" type="text" style="RightLabelText">Population Cap:</object>
    197205                </object>
     
    199207                <object name="populationCap" size="40% 0 100% 28" type="dropdown" style="StoneDropDown" hidden="true" tooltip_style="onscreenToolTip" tooltip="Select population cap."/>
    200208            </object>
    201209           
    202             <object size="14 98 94% 126">
     210            <object size="14 128 94% 156">
    203211                <object size="0 0 40% 28">
    204212                    <object size="0 0 100% 100%" type="text" style="RightLabelText">Starting Resources:</object>
    205213                </object>
     
    207215                <object name="startingResources" size="40% 0 100% 28" type="dropdown" style="StoneDropDown" hidden="true" tooltip_style="onscreenToolTip" tooltip="Select the game's starting resources."/>
    208216            </object>
    209217           
    210             <object size="14 128 94% 216">
     218            <object size="14 158 94% 246">
    211219                <object size="0 0 40% 28">
    212                     <object size="0 0 100% 100%" type="text" style="RightLabelText">Reveal map:</object>
     220                    <object size="0 0 100% 100%" type="text" style="RightLabelText">Reveal Map:</object>
    213221                </object>
    214222                <object size="0 30 40% 58">
    215                     <object size="0 0 100% 100%" type="text" style="RightLabelText">Teams locked:</object>
     223                    <object size="0 0 100% 100%" type="text" style="RightLabelText">Teams Locked:</object>
    216224                </object>
    217225                <object size="0 60 40% 88" name="enableCheatsDesc" hidden="true">
    218226                    <object size="0 0 100% 100%" type="text" style="RightLabelText">Cheats:</object>
     
    237245                name="hideMoreOptions"
    238246                type="button"
    239247                style="StoneButton"
    240                 size="50%-70 218 50%+70 246"
     248                size="50%-70 248 50%+70 274"
    241249                tooltip_style="onscreenToolTip"
    242250                tooltip="Close more game options window"
    243251            >
  • binaries/data/mods/public/gui/session/menu.js

     
    361361        openDiplomacy();
    362362};
    363363
     364function toggleGameSpeed()
     365{
     366    var gameSpeed = getGUIObjectByName("gameSpeed");
     367    gameSpeed.hidden = !gameSpeed.hidden;
     368}
     369
    364370function pauseGame()
    365371{
    366372    getGUIObjectByName("pauseButtonText").caption = RESUME;
  • binaries/data/mods/public/gui/session/session.js

     
    66// Cache the useful civ data
    77var g_CivData = {};
    88
     9var g_GameSpeeds = {};
     10var g_CurrentSpeed;
     11
    912var g_PlayerAssignments = { "local": { "name": "You", "player": 1 } };
    1013
    1114// Cache dev-mode settings that are frequently or widely used
     
    8386        g_Players = getPlayerData(g_PlayerAssignments);
    8487
    8588        if (initData.savedGUIData)
    86         {
    8789            restoreSavedGameData(initData.savedGUIData);
    88         }
     90
     91        getGUIObjectByName("gameSpeedButton").hidden = g_IsNetworked;
    8992    }
    9093    else // Needed for autostart loading option
    9194    {
     
    9699    g_CivData = loadCivData();
    97100    g_CivData["gaia"] = { "Code": "gaia", "Name": "Gaia" };
    98101
     102    g_GameSpeeds = initGameSpeeds();
     103    g_CurrentSpeed = Engine.GetSimRate();
     104    var gameSpeed = getGUIObjectByName("gameSpeed");
     105    gameSpeed.list = g_GameSpeeds.names;
     106    gameSpeed.list_data = g_GameSpeeds.speeds;
     107    var idx = g_GameSpeeds.speeds.indexOf(g_CurrentSpeed);
     108    gameSpeed.selected = idx != -1 ? idx : g_GameSpeeds["default"];
     109    gameSpeed.onSelectionChange = function() { changeGameSpeed(+this.list_data[this.selected]); }
     110
    99111    getGUIObjectByName("civIcon").sprite = "stretched:" + g_CivData[g_Players[Engine.GetPlayerID()].civ].Emblem;
    100112    getGUIObjectByName("civIcon").tooltip = g_CivData[g_Players[Engine.GetPlayerID()].civ].Name;
    101113    initMenuPosition(); // set initial position
     
    347359    }
    348360}
    349361
     362function changeGameSpeed(speed)
     363{
     364    // For non-networked games only
     365    if (!g_IsNetworked)
     366    {
     367        Engine.SetSimRate(speed);
     368        g_CurrentSpeed = speed;
     369    }
     370}
     371
    350372/**
    351373 * Recomputes GUI state that depends on simulation state or selection state. Called directly every simulation
    352374 * update (see session.xml), or from onTick when the selection has changed.
     
    520542
    521543function updateTimeElapsedCounter(simState)
    522544{
     545    var speed = g_CurrentSpeed != 1.0 ? " (" + g_CurrentSpeed + "x)" : "";
    523546    var timeElapsedCounter = getGUIObjectByName("timeElapsedCounter");
    524     timeElapsedCounter.caption = timeToString(simState.timeElapsed);
     547    timeElapsedCounter.caption = timeToString(simState.timeElapsed) + speed;
    525548}
    526549
    527550// Toggles the display of status bars for all of the player's entities.
  • binaries/data/mods/public/gui/session/session.xml

     
    220220    <!-- ================================  ================================ -->
    221221    <!-- Time elapsed counter -->
    222222    <!-- ================================  ================================ -->
    223     <object size="100%-100 50 100%-10 70" type="text" name="timeElapsedCounter" style="SettingsText" hotkey="timeelapsedcounter.toggle" hidden="true">
     223   
     224    <object size="100%-120 45 100%-10 65" type="text" name="timeElapsedCounter" style="SettingsText" hotkey="timeelapsedcounter.toggle" hidden="true">
    224225        <action on="Press">this.hidden = !this.hidden;</action>
    225226    </object>
    226227
     
    513514        <!-- ================================  ================================ -->
    514515
    515516        <!-- Displays Alpha name and number -->
    516         <object size="70%-128 0 70%+128 100%" name="alphaLabel" type="text" style="CenteredLabelText" text_valign="top" ghost="true">
     517        <object size="50%+48 0 100%-226 100%" name="alphaLabel" type="text" style="CenteredLabelText" text_valign="top" ghost="true">
    517518        ALPHA XIII : Magadha<!-- IMPORTANT: remember to update pregame/mainmenu.xml in sync with this -->
    518519
    519520        <!-- Displays build date and revision number-->
     
    522523        </object>
    523524        </object>
    524525
     526        <!-- ================================  ================================ -->
     527        <!-- Game Speed Button -->
    525528        <!-- ================================  ================================ -->
     529        <object type="button"
     530        name="gameSpeedButton"
     531        size="100%-226 4 100%-198 32"
     532        style="iconButton"
     533        tooltip_style="sessionToolTip"
     534        tooltip="Game speed"
     535        >
     536            <object size="5 5 100%-5 100%-5" type="image" sprite="stretched:session/icons/resources/time_small.png" ghost="true"/>
     537            <action on="Press">
     538                toggleGameSpeed();
     539            </action>
     540        </object>
     541
     542        <object size="100%-230 45 100%-140 70" name="gameSpeed" type="dropdown" buffer_zone="5" style="StoneDropDown" hidden="true" tooltip="Choose game speed" tooltip_style="sessionToolTip"/>
     543       
     544        <!-- ================================  ================================ -->
    526545        <!-- Diplomacy Button -->
    527546        <!-- ================================  ================================ -->
    528547        <object type="button"
    529548        name="diplomacyButton1"
    530         size="100%-196 0 100%-164 32"
     549        size="100%-194 4 100%-166 32"
    531550        style="iconButton"
    532551        tooltip_style="sessionToolTip"
    533552        tooltip="Diplomacy"
  • source/gui/scripting/ScriptFunctions.cpp

     
    1 /* Copyright (C) 2012 Wildfire Games.
     1/* Copyright (C) 2013 Wildfire Games.
    22 * This file is part of 0 A.D.
    33 *
    44 * 0 A.D. is free software: you can redistribute it and/or modify
     
    526526    g_Game->SetSimRate(rate);
    527527}
    528528
     529float GetSimRate(void* UNUSED(cbdata))
     530{
     531    return g_Game->GetSimRate();
     532}
     533
    529534void SetTurnLength(void* UNUSED(cbdata), int length)
    530535{
    531536    if (g_NetServer)
     
    680685
    681686    // Development/debugging functions
    682687    scriptInterface.RegisterFunction<void, float, &SetSimRate>("SetSimRate");
     688    scriptInterface.RegisterFunction<float, &GetSimRate>("GetSimRate");
    683689    scriptInterface.RegisterFunction<void, int, &SetTurnLength>("SetTurnLength");
    684690    scriptInterface.RegisterFunction<void, float, float, float, &SetCameraTarget>("SetCameraTarget");
    685691    scriptInterface.RegisterFunction<int, &Crash>("Crash");
  • source/ps/Game.cpp

     
    126126    std::string mapType;
    127127    m_Simulation2->GetScriptInterface().GetProperty(attribs.get(), "mapType", mapType);
    128128
     129    float speed;
     130    if (m_Simulation2->GetScriptInterface().HasProperty(attribs.get(), "gameSpeed") && m_Simulation2->GetScriptInterface().GetProperty(attribs.get(), "gameSpeed", speed))
     131        SetSimRate(speed);
     132
    129133    LDR_BeginRegistering();
    130134
    131135    RegMemFun(m_Simulation2, &CSimulation2::ProgressiveLoad, L"Simulation init", 1000);
  • source/ps/Game.h

     
    145145     *                      clamped to 0.0f.
    146146     **/
    147147    inline void SetSimRate(float simRate)
    148     {    m_SimRate = std::max(simRate, 0.0f); }
     148    {   if (isfinite(simRate)) m_SimRate = std::max(simRate, 0.0f); }
    149149
     150    inline float GetSimRate()
     151    {   return m_SimRate; }
     152
    150153    /**
    151154     * Replace the current turn manager.
    152155     * This class will take ownership of the pointer.