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)
|
159 | 159 | return undefined; |
160 | 160 | } |
161 | 161 | |
162 | | function getActionInfo(action, target, simState) |
| 162 | function getActionInfo(action, target) |
163 | 163 | { |
| 164 | var simState = GetSimState(); |
164 | 165 | var selection = g_Selection.toList(); |
165 | 166 | |
166 | 167 | // If the selection doesn't exist, no action |
… |
… |
function determineAction(x, y, fromMinimap)
|
457 | 458 | target = targets[0]; |
458 | 459 | } |
459 | 460 | |
460 | | var simState = Engine.GuiInterfaceCall("GetSimulationState"); |
461 | | |
462 | 461 | if (preSelectedAction != ACTION_NONE) |
463 | 462 | { |
464 | 463 | switch (preSelectedAction) |
465 | 464 | { |
466 | 465 | case ACTION_GARRISON: |
467 | | if (getActionInfo("garrison", target, simState).possible) |
| 466 | if (getActionInfo("garrison", target).possible) |
468 | 467 | return {"type": "garrison", "cursor": "action-garrison", "target": target}; |
469 | 468 | else |
470 | 469 | return {"type": "none", "cursor": "action-garrison-disabled", "target": undefined}; |
471 | 470 | break; |
472 | 471 | case ACTION_REPAIR: |
473 | | if (getActionInfo("repair", target, simState).possible) |
| 472 | if (getActionInfo("repair", target).possible) |
474 | 473 | return {"type": "repair", "cursor": "action-repair", "target": target}; |
475 | 474 | else |
476 | 475 | return {"type": "none", "cursor": "action-repair-disabled", "target": undefined}; |
477 | 476 | break; |
478 | 477 | } |
479 | 478 | } |
480 | | else if (Engine.HotkeyIsPressed("session.attack") && getActionInfo("attack", target, simState).possible) |
| 479 | else if (Engine.HotkeyIsPressed("session.attack") && getActionInfo("attack", target).possible) |
481 | 480 | { |
482 | 481 | return {"type": "attack", "cursor": "action-attack", "target": target}; |
483 | 482 | } |
484 | | else if (Engine.HotkeyIsPressed("session.garrison") && getActionInfo("garrison", target, simState).possible) |
| 483 | else if (Engine.HotkeyIsPressed("session.garrison") && getActionInfo("garrison", target).possible) |
485 | 484 | { |
486 | 485 | return {"type": "garrison", "cursor": "action-garrison", "target": target}; |
487 | 486 | } |
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) |
489 | 488 | { |
490 | 489 | return {"type": "attack-move", "cursor": "action-attack-move"}; |
491 | 490 | } |
492 | 491 | else |
493 | 492 | { |
494 | 493 | var actionInfo = undefined; |
495 | | if ((actionInfo = getActionInfo("setup-trade-route", target, simState)).possible) |
| 494 | if ((actionInfo = getActionInfo("setup-trade-route", target)).possible) |
496 | 495 | 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) |
498 | 497 | 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) |
500 | 499 | return {"type": "returnresource", "cursor": actionInfo.cursor, "target": target}; |
501 | | else if (getActionInfo("build", target, simState).possible) |
| 500 | else if (getActionInfo("build", target).possible) |
502 | 501 | return {"type": "build", "cursor": "action-build", "target": target}; |
503 | | else if (getActionInfo("repair", target, simState).possible) |
| 502 | else if (getActionInfo("repair", target).possible) |
504 | 503 | 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) |
506 | 505 | 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) |
508 | 507 | return {"type": "heal", "cursor": "action-heal", "target": target}; |
509 | | else if (getActionInfo("attack", target, simState).possible) |
| 508 | else if (getActionInfo("attack", target).possible) |
510 | 509 | 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) |
512 | 511 | return {"type": "unset-rallypoint"}; |
513 | | else if (getActionInfo("move", target, simState).possible) |
| 512 | else if (getActionInfo("move", target).possible) |
514 | 513 | return {"type": "move"}; |
515 | 514 | } |
516 | 515 | return {"type": type, "cursor": cursor, "target": target}; |
… |
… |
function handleInputAfterGui(ev)
|
1170 | 1169 | { |
1171 | 1170 | // If double click hasn't already occurred, this is a double click. |
1172 | 1171 | // Select similar units regardless of rank |
1173 | | templateToMatch = Engine.GuiInterfaceCall("GetEntityState", selectedEntity).identity.selectionGroupName; |
| 1172 | templateToMatch = GetEntityState(selectedEntity).identity.selectionGroupName; |
1174 | 1173 | if (templateToMatch) |
1175 | 1174 | { |
1176 | 1175 | matchRank = false; |
1177 | 1176 | } |
1178 | 1177 | else |
1179 | 1178 | { // No selection group name defined, so fall back to exact match |
1180 | | templateToMatch = Engine.GuiInterfaceCall("GetEntityState", selectedEntity).template; |
| 1179 | templateToMatch = GetEntityState(selectedEntity).template; |
1181 | 1180 | } |
1182 | 1181 | |
1183 | 1182 | doubleClicked = true; |
… |
… |
function handleInputAfterGui(ev)
|
1188 | 1187 | { |
1189 | 1188 | // Double click has already occurred, so this is a triple click. |
1190 | 1189 | // Select units matching exact template name (same rank) |
1191 | | templateToMatch = Engine.GuiInterfaceCall("GetEntityState", selectedEntity).template; |
| 1190 | templateToMatch = GetEntityState(selectedEntity).template; |
1192 | 1191 | } |
1193 | 1192 | |
1194 | 1193 | // TODO: Should we handle "control all units" here as well? |
… |
… |
function getEntityLimitAndCount(playerState, entType)
|
1557 | 1556 | // Add the unit shown at position to the training queue for all entities in the selection |
1558 | 1557 | function addTrainingByPosition(position) |
1559 | 1558 | { |
1560 | | var simState = Engine.GuiInterfaceCall("GetSimulationState"); |
| 1559 | var simState = GetSimState(); |
1561 | 1560 | var playerState = simState.players[Engine.GetPlayerID()]; |
1562 | 1561 | var selection = g_Selection.toList(); |
1563 | 1562 | |
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 = {};
|
8 | 8 | |
9 | 9 | var g_GameSpeeds = {}; |
10 | 10 | var g_CurrentSpeed; |
| 11 | var g_SimState; |
11 | 12 | |
12 | 13 | var g_PlayerAssignments = { "local": { "name": "You", "player": 1 } }; |
13 | 14 | |
… |
… |
function GetEntityState(entId)
|
46 | 47 | return g_EntityStates[entId]; |
47 | 48 | } |
48 | 49 | |
| 50 | function GetSimState() |
| 51 | { |
| 52 | if (!g_SimState) |
| 53 | { |
| 54 | g_SimState = Engine.GuiInterfaceCall("GetSimulationState"); |
| 55 | } |
| 56 | |
| 57 | return g_SimState; |
| 58 | } |
| 59 | |
49 | 60 | // Cache TemplateData |
50 | 61 | var g_TemplateData = {}; // {id:template} |
51 | 62 | |
… |
… |
function reportPerformance(time)
|
159 | 170 | |
160 | 171 | function resignGame() |
161 | 172 | { |
162 | | var simState = Engine.GuiInterfaceCall("GetSimulationState"); |
| 173 | var simState = GetSimState(); |
163 | 174 | |
164 | 175 | // Players can't resign if they've already won or lost. |
165 | 176 | if (simState.players[Engine.GetPlayerID()].state != "active" || g_Disconnected) |
… |
… |
function onTick()
|
301 | 312 | |
302 | 313 | function checkPlayerState() |
303 | 314 | { |
304 | | var simState = Engine.GuiInterfaceCall("GetSimulationState"); |
| 315 | var simState = GetSimState(); |
305 | 316 | var playerState = simState.players[Engine.GetPlayerID()]; |
306 | 317 | |
307 | 318 | if (!g_GameEnded) |
… |
… |
function onSimulationUpdate()
|
386 | 397 | g_TemplateData = {}; |
387 | 398 | g_TechnologyData = {}; |
388 | 399 | |
389 | | var simState = Engine.GuiInterfaceCall("GetSimulationState"); |
| 400 | g_SimState = Engine.GuiInterfaceCall("GetSimulationState"); |
390 | 401 | |
391 | 402 | // 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) |
393 | 404 | return; |
394 | 405 | |
395 | 406 | handleNotifications(); |
… |
… |
function onSimulationUpdate()
|
397 | 408 | if (g_ShowAllStatusBars) |
398 | 409 | recalculateStatusBarDisplay(); |
399 | 410 | |
400 | | updateHero(simState); |
| 411 | updateHero(); |
401 | 412 | updateGroups(); |
402 | | updateDebug(simState); |
403 | | updatePlayerDisplay(simState); |
| 413 | updateDebug(); |
| 414 | updatePlayerDisplay(); |
404 | 415 | updateSelectionDetails(); |
405 | 416 | updateResearchDisplay(); |
406 | 417 | updateBuildingPlacementPreview(); |
407 | | updateTimeElapsedCounter(simState); |
| 418 | updateTimeElapsedCounter(); |
408 | 419 | |
409 | 420 | // Update music state on basis of battle state. |
410 | 421 | var battleState = Engine.GuiInterfaceCall("GetBattleState", Engine.GetPlayerID()); |
… |
… |
function onSimulationUpdate()
|
412 | 423 | global.music.setState(global.music.states[battleState]); |
413 | 424 | } |
414 | 425 | |
415 | | function updateHero(simState) |
| 426 | function updateHero() |
416 | 427 | { |
| 428 | var simState = GetSimState(); |
417 | 429 | var playerState = simState.players[Engine.GetPlayerID()]; |
418 | 430 | var heroButton = getGUIObjectByName("unitHeroButton"); |
419 | 431 | |
… |
… |
function updateHero(simState)
|
424 | 436 | } |
425 | 437 | |
426 | 438 | var heroImage = getGUIObjectByName("unitHeroImage"); |
427 | | var heroState = Engine.GuiInterfaceCall("GetEntityState", playerState.heroes[0]); |
| 439 | var heroState = GetEntityState(playerState.heroes[0]); |
428 | 440 | var template = GetTemplateData(heroState.template); |
429 | 441 | heroImage.sprite = "stretched:session/portraits/" + template.icon; |
430 | 442 | |
… |
… |
function updateGroups()
|
471 | 483 | layoutButtonRow(i, guiName, buttonSideLength, buttonSpacer, rowLength*i, rowLength*(i+1) ); |
472 | 484 | } |
473 | 485 | |
474 | | function updateDebug(simState) |
| 486 | function updateDebug() |
475 | 487 | { |
| 488 | var simState = GetSimState(); |
476 | 489 | var debug = getGUIObjectByName("debug"); |
477 | 490 | |
478 | 491 | if (getGUIObjectByName("devDisplayState").checked) |
… |
… |
function updateDebug(simState)
|
506 | 519 | debug.caption = text; |
507 | 520 | } |
508 | 521 | |
509 | | function updatePlayerDisplay(simState) |
| 522 | function updatePlayerDisplay() |
510 | 523 | { |
| 524 | var simState = GetSimState(); |
511 | 525 | var playerState = simState.players[Engine.GetPlayerID()]; |
512 | 526 | if (!playerState) |
513 | 527 | return; |
… |
… |
function updateResearchDisplay()
|
582 | 596 | getGUIObjectByName("researchStartedButton[" + i + "]").hidden = true; |
583 | 597 | } |
584 | 598 | |
585 | | function updateTimeElapsedCounter(simState) |
| 599 | function updateTimeElapsedCounter() |
586 | 600 | { |
| 601 | var simState = GetSimState(); |
587 | 602 | var speed = g_CurrentSpeed != 1.0 ? " (" + g_CurrentSpeed + "x)" : ""; |
588 | 603 | var timeElapsedCounter = getGUIObjectByName("timeElapsedCounter"); |
589 | 604 | timeElapsedCounter.caption = timeToString(simState.timeElapsed) + speed; |
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
|
1007 | 1007 | { |
1008 | 1008 | // Get player state to check some constraints |
1009 | 1009 | // e.g. presence of a hero or build limits |
1010 | | var simState = Engine.GuiInterfaceCall("GetSimulationState"); |
| 1010 | var simState = GetSimState(); |
1011 | 1011 | var playerState = simState.players[player]; |
1012 | 1012 | |
1013 | 1013 | if (selection.length > 1) |
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)
|
37 | 37 | { |
38 | 38 | var players = []; |
39 | 39 | |
40 | | var simState = Engine.GuiInterfaceCall("GetSimulationState"); |
| 40 | var simState = GetSimState(); |
41 | 41 | if (!simState) |
42 | 42 | return players; |
43 | 43 | |