Ticket #1878: 0002-Do-some-more-caching-in-the-gui.patch

File 0002-Do-some-more-caching-in-the-gui.patch, 11.7 KB (added by sbte, 11 years ago)
  • binaries/data/mods/public/gui/session/input.js

    From af09dcc9bfb8fdf65326d006725f99c677596f23 Mon Sep 17 00:00:00 2001
    From: "Sven (Sbte)" <svenb.linux@gmail.com>
    Date: Sat, 6 Apr 2013 14:58:11 +0200
    Subject: Do some more caching in the gui
    
    ---
     binaries/data/mods/public/gui/session/input.js     | 43 +++++++++++-----------
     binaries/data/mods/public/gui/session/session.js   | 41 ++++++++++++++-------
     .../data/mods/public/gui/session/unit_commands.js  |  2 +-
     .../mods/public/gui/session/utility_functions.js   |  2 +-
     4 files changed, 51 insertions(+), 37 deletions(-)
    
    diff --git a/binaries/data/mods/public/gui/session/input.js b/binaries/data/mods/public/gui/session/input.js
    index 8fd2e57..8b57250 100644
    a b function findGatherType(gatherer, supply)  
    159159    return undefined;
    160160}
    161161
    162 function getActionInfo(action, target, simState)
     162function getActionInfo(action, target)
    163163{
     164    var simState = GetSimState();
    164165    var selection = g_Selection.toList();
    165166
    166167    // If the selection doesn't exist, no action
    function determineAction(x, y, fromMinimap)  
    457458        target = targets[0];
    458459    }
    459460
    460     var simState = Engine.GuiInterfaceCall("GetSimulationState");
    461 
    462461    if (preSelectedAction != ACTION_NONE)
    463462    {
    464463        switch (preSelectedAction)
    465464        {
    466465        case ACTION_GARRISON:
    467             if (getActionInfo("garrison", target, simState).possible)
     466            if (getActionInfo("garrison", target).possible)
    468467                return {"type": "garrison", "cursor": "action-garrison", "target": target};
    469468            else
    470469                return  {"type": "none", "cursor": "action-garrison-disabled", "target": undefined};
    471470            break;
    472471        case ACTION_REPAIR:
    473             if (getActionInfo("repair", target, simState).possible)
     472            if (getActionInfo("repair", target).possible)
    474473                return {"type": "repair", "cursor": "action-repair", "target": target};
    475474            else
    476475                return {"type": "none", "cursor": "action-repair-disabled", "target": undefined};
    477476            break;
    478477        }
    479478    }
    480     else if (Engine.HotkeyIsPressed("session.attack") && getActionInfo("attack", target, simState).possible)
     479    else if (Engine.HotkeyIsPressed("session.attack") && getActionInfo("attack", target).possible)
    481480    {
    482481        return {"type": "attack", "cursor": "action-attack", "target": target};
    483482    }
    484     else if (Engine.HotkeyIsPressed("session.garrison") && getActionInfo("garrison", target, simState).possible)
     483    else if (Engine.HotkeyIsPressed("session.garrison") && getActionInfo("garrison", target).possible)
    485484    {
    486485        return {"type": "garrison", "cursor": "action-garrison", "target": target};
    487486    }
    488     else if (Engine.HotkeyIsPressed("session.attackmove") && getActionInfo("attack-move", target, simState).possible)
     487    else if (Engine.HotkeyIsPressed("session.attackmove") && getActionInfo("attack-move", target).possible)
    489488    {
    490489            return {"type": "attack-move", "cursor": "action-attack-move"};
    491490    }
    492491    else
    493492    {
    494493        var actionInfo = undefined;
    495         if ((actionInfo = getActionInfo("setup-trade-route", target, simState)).possible)
     494        if ((actionInfo = getActionInfo("setup-trade-route", target)).possible)
    496495            return {"type": "setup-trade-route", "cursor": "action-setup-trade-route", "tooltip": actionInfo.tooltip, "target": target};
    497         else if ((actionInfo = getActionInfo("gather", target, simState)).possible)
     496        else if ((actionInfo = getActionInfo("gather", target)).possible)
    498497            return {"type": "gather", "cursor": actionInfo.cursor, "target": target};
    499         else if ((actionInfo = getActionInfo("returnresource", target, simState)).possible)
     498        else if ((actionInfo = getActionInfo("returnresource", target)).possible)
    500499            return {"type": "returnresource", "cursor": actionInfo.cursor, "target": target};
    501         else if (getActionInfo("build", target, simState).possible)
     500        else if (getActionInfo("build", target).possible)
    502501            return {"type": "build", "cursor": "action-build", "target": target};
    503         else if (getActionInfo("repair", target, simState).possible)
     502        else if (getActionInfo("repair", target).possible)
    504503            return {"type": "build", "cursor": "action-repair", "target": target};
    505         else if ((actionInfo = getActionInfo("set-rallypoint", target, simState)).possible)
     504        else if ((actionInfo = getActionInfo("set-rallypoint", target)).possible)
    506505            return {"type": "set-rallypoint", "cursor": actionInfo.cursor, "data": actionInfo.data, "tooltip": actionInfo.tooltip, "position": actionInfo.position};
    507         else if (getActionInfo("heal", target, simState).possible)
     506        else if (getActionInfo("heal", target).possible)
    508507            return {"type": "heal", "cursor": "action-heal", "target": target};
    509         else if (getActionInfo("attack", target, simState).possible)
     508        else if (getActionInfo("attack", target).possible)
    510509            return {"type": "attack", "cursor": "action-attack", "target": target};
    511         else if (getActionInfo("unset-rallypoint", target, simState).possible)
     510        else if (getActionInfo("unset-rallypoint", target).possible)
    512511            return {"type": "unset-rallypoint"};
    513         else if (getActionInfo("move", target, simState).possible)
     512        else if (getActionInfo("move", target).possible)
    514513            return {"type": "move"};
    515514    }
    516515    return {"type": type, "cursor": cursor, "target": target};
    function handleInputAfterGui(ev)  
    11701169                    {
    11711170                        // If double click hasn't already occurred, this is a double click.
    11721171                        // Select similar units regardless of rank
    1173                         templateToMatch = Engine.GuiInterfaceCall("GetEntityState", selectedEntity).identity.selectionGroupName;
     1172                        templateToMatch = GetEntityState(selectedEntity).identity.selectionGroupName;
    11741173                        if (templateToMatch)
    11751174                        {
    11761175                            matchRank = false;
    11771176                        }
    11781177                        else
    11791178                        {   // No selection group name defined, so fall back to exact match
    1180                             templateToMatch = Engine.GuiInterfaceCall("GetEntityState", selectedEntity).template;
     1179                            templateToMatch = GetEntityState(selectedEntity).template;
    11811180                        }
    11821181
    11831182                        doubleClicked = true;
    function handleInputAfterGui(ev)  
    11881187                    {
    11891188                        // Double click has already occurred, so this is a triple click.
    11901189                        // Select units matching exact template name (same rank)
    1191                         templateToMatch = Engine.GuiInterfaceCall("GetEntityState", selectedEntity).template;
     1190                        templateToMatch = GetEntityState(selectedEntity).template;
    11921191                    }
    11931192
    11941193                    // TODO: Should we handle "control all units" here as well?
    function getEntityLimitAndCount(playerState, entType)  
    15571556// Add the unit shown at position to the training queue for all entities in the selection
    15581557function addTrainingByPosition(position)
    15591558{
    1560     var simState = Engine.GuiInterfaceCall("GetSimulationState");
     1559    var simState = GetSimState();
    15611560    var playerState = simState.players[Engine.GetPlayerID()];
    15621561    var selection = g_Selection.toList();
    15631562
  • binaries/data/mods/public/gui/session/session.js

    diff --git a/binaries/data/mods/public/gui/session/session.js b/binaries/data/mods/public/gui/session/session.js
    index f919321..4efbab7 100644
    a b var g_CivData = {};  
    88
    99var g_GameSpeeds = {};
    1010var g_CurrentSpeed;
     11var g_SimState;
    1112
    1213var g_PlayerAssignments = { "local": { "name": "You", "player": 1 } };
    1314
    function GetEntityState(entId)  
    4647    return g_EntityStates[entId];
    4748}
    4849
     50function GetSimState()
     51{
     52    if (!g_SimState)
     53    {
     54        g_SimState = Engine.GuiInterfaceCall("GetSimulationState");
     55    }
     56
     57    return g_SimState;
     58}
     59
    4960// Cache TemplateData
    5061var g_TemplateData = {}; // {id:template}
    5162
    function reportPerformance(time)  
    159170
    160171function resignGame()
    161172{
    162     var simState = Engine.GuiInterfaceCall("GetSimulationState");
     173    var simState = GetSimState();
    163174
    164175    // Players can't resign if they've already won or lost.
    165176    if (simState.players[Engine.GetPlayerID()].state != "active" || g_Disconnected)
    function onTick()  
    301312
    302313function checkPlayerState()
    303314{
    304     var simState = Engine.GuiInterfaceCall("GetSimulationState");
     315    var simState = GetSimState();
    305316    var playerState = simState.players[Engine.GetPlayerID()];
    306317
    307318    if (!g_GameEnded)
    function onSimulationUpdate()  
    386397    g_TemplateData = {};
    387398    g_TechnologyData = {};
    388399
    389     var simState = Engine.GuiInterfaceCall("GetSimulationState");
     400    g_SimState = Engine.GuiInterfaceCall("GetSimulationState");
    390401
    391402    // If we're called during init when the game is first loading, there will be no simulation yet, so do nothing
    392     if (!simState)
     403    if (!g_SimState)
    393404        return;
    394405
    395406    handleNotifications();
    function onSimulationUpdate()  
    397408    if (g_ShowAllStatusBars)
    398409        recalculateStatusBarDisplay();
    399410
    400     updateHero(simState);
     411    updateHero();
    401412    updateGroups();
    402     updateDebug(simState);
    403     updatePlayerDisplay(simState);
     413    updateDebug();
     414    updatePlayerDisplay();
    404415    updateSelectionDetails();
    405416    updateResearchDisplay();
    406417    updateBuildingPlacementPreview();
    407     updateTimeElapsedCounter(simState);
     418    updateTimeElapsedCounter();
    408419
    409420    // Update music state on basis of battle state.
    410421    var battleState = Engine.GuiInterfaceCall("GetBattleState", Engine.GetPlayerID());
    function onSimulationUpdate()  
    412423        global.music.setState(global.music.states[battleState]);
    413424}
    414425
    415 function updateHero(simState)
     426function updateHero()
    416427{
     428    var simState = GetSimState();
    417429    var playerState = simState.players[Engine.GetPlayerID()];
    418430    var heroButton = getGUIObjectByName("unitHeroButton");
    419431
    function updateHero(simState)  
    424436    }
    425437
    426438    var heroImage = getGUIObjectByName("unitHeroImage");
    427     var heroState = Engine.GuiInterfaceCall("GetEntityState", playerState.heroes[0]);
     439    var heroState = GetEntityState(playerState.heroes[0]);
    428440    var template = GetTemplateData(heroState.template);
    429441    heroImage.sprite = "stretched:session/portraits/" + template.icon;
    430442
    function updateGroups()  
    471483        layoutButtonRow(i, guiName, buttonSideLength, buttonSpacer, rowLength*i, rowLength*(i+1) );
    472484}
    473485
    474 function updateDebug(simState)
     486function updateDebug()
    475487{
     488    var simState = GetSimState();
    476489    var debug = getGUIObjectByName("debug");
    477490
    478491    if (getGUIObjectByName("devDisplayState").checked)
    function updateDebug(simState)  
    506519    debug.caption = text;
    507520}
    508521
    509 function updatePlayerDisplay(simState)
     522function updatePlayerDisplay()
    510523{
     524    var simState = GetSimState();
    511525    var playerState = simState.players[Engine.GetPlayerID()];
    512526    if (!playerState)
    513527        return;
    function updateResearchDisplay()  
    582596        getGUIObjectByName("researchStartedButton[" + i + "]").hidden = true;
    583597}
    584598
    585 function updateTimeElapsedCounter(simState)
     599function updateTimeElapsedCounter()
    586600{
     601    var simState = GetSimState();
    587602    var speed = g_CurrentSpeed != 1.0 ? " (" + g_CurrentSpeed + "x)" : "";
    588603    var timeElapsedCounter = getGUIObjectByName("timeElapsedCounter");
    589604    timeElapsedCounter.caption = timeToString(simState.timeElapsed) + speed;
  • binaries/data/mods/public/gui/session/unit_commands.js

    diff --git a/binaries/data/mods/public/gui/session/unit_commands.js b/binaries/data/mods/public/gui/session/unit_commands.js
    index 316117d..2361253 100644
    a b function updateUnitCommands(entState, supplementalDetailsPanel, commandsPanel, s  
    10071007    {
    10081008        // Get player state to check some constraints
    10091009        // e.g. presence of a hero or build limits
    1010         var simState = Engine.GuiInterfaceCall("GetSimulationState");
     1010        var simState = GetSimState();
    10111011        var playerState = simState.players[player];
    10121012
    10131013        if (selection.length > 1)
  • binaries/data/mods/public/gui/session/utility_functions.js

    diff --git a/binaries/data/mods/public/gui/session/utility_functions.js b/binaries/data/mods/public/gui/session/utility_functions.js
    index 1660479..6dde038 100644
    a b function getPlayerData(playerAssignments)  
    3737{
    3838    var players = [];
    3939
    40     var simState = Engine.GuiInterfaceCall("GetSimulationState");
     40    var simState = GetSimState();
    4141    if (!simState)
    4242        return players;
    4343