Ticket #3215: t3215_fix_observer_statusbars_v3.patch

File t3215_fix_observer_statusbars_v3.patch, 17.2 KB (added by elexis, 9 years ago)

(1) Renames functions from ...AllPlayerEntities to ...NonGaiaEntities (2) Removes the loop when computing the ownership mask (3) Uses for (const entity_id_t& ent : ents) for a cleaner loop (4) Removes an unused player argument in the GuiInterface

  • binaries/data/mods/public/gui/session/input.js

     
    159159    else if (placementSupport.mode === "wall")
    160160    {
    161161        if (placementSupport.wallSet && placementSupport.position)
    162162        {
    163163            // Fetch an updated list of snapping candidate entities
    164             placementSupport.wallSnapEntities = Engine.PickSimilarFriendlyEntities(
     164            placementSupport.wallSnapEntities = Engine.PickSimilarPlayerEntities(
    165165                placementSupport.wallSet.templates.tower,
    166166                placementSupport.wallSnapEntitiesIncludeOffscreen,
    167167                true, // require exact template match
    168168                true  // include foundations
    169169            );
     
    549549        switch (ev.type)
    550550        {
    551551        case "mousemotion":
    552552            var rect = updateBandbox(bandbox, ev, false);
    553553
    554             var ents = Engine.PickFriendlyEntitiesInRect(rect[0], rect[1], rect[2], rect[3], Engine.GetPlayerID());
     554            var ents = Engine.PickPlayerEntitiesInRect(rect[0], rect[1], rect[2], rect[3], Engine.GetPlayerID());
    555555            var preferredEntities = getPreferredEntities(ents);
    556556            g_Selection.setHighlightList(preferredEntities);
    557557
    558558            return false;
    559559
     
    561561            if (ev.button == SDL_BUTTON_LEFT)
    562562            {
    563563                var rect = updateBandbox(bandbox, ev, true);
    564564
    565565                // Get list of entities limited to preferred entities
    566                 var ents = getPreferredEntities(Engine.PickFriendlyEntitiesInRect(rect[0], rect[1], rect[2], rect[3], Engine.GetPlayerID()));
     566                var ents = getPreferredEntities(Engine.PickPlayerEntitiesInRect(rect[0], rect[1], rect[2], rect[3], Engine.GetPlayerID()));
    567567
    568568                // Remove the bandbox hover highlighting
    569569                g_Selection.setHighlightList([]);
    570570
    571571                // Update the list of selected units
     
    10431043                        // Select units matching exact template name (same rank)
    10441044                        templateToMatch = GetEntityState(selectedEntity).template;
    10451045                    }
    10461046
    10471047                    // TODO: Should we handle "control all units" here as well?
    1048                     ents = Engine.PickSimilarFriendlyEntities(templateToMatch, showOffscreen, matchRank, false);
     1048                    ents = Engine.PickSimilarPlayerEntities(templateToMatch, showOffscreen, matchRank, false);
    10491049                }
    10501050                else
    10511051                {
    10521052                    // It's single click right now but it may become double or triple click
    10531053                    doubleClicked = false;
  • binaries/data/mods/public/gui/session/session.js

     
    820820
    821821// Toggles the display of status bars for all of the player's entities.
    822822function recalculateStatusBarDisplay()
    823823{
    824824    if (g_ShowAllStatusBars)
    825         var entities = Engine.PickFriendlyEntitiesOnScreen(Engine.GetPlayerID());
     825        var entities = g_IsObserver ? Engine.PickNonGaiaEntitiesOnScreen() : Engine.PickPlayerEntitiesOnScreen(Engine.GetPlayerID());
    826826    else
    827827    {
    828828        var selected = g_Selection.toList();
    829829        for each (var ent in g_Selection.highlighted)
    830830            selected.push(ent);
    831 
     831       
    832832        // Remove selected entities from the 'all entities' array, to avoid disabling their status bars.
    833         var entities = Engine.GuiInterfaceCall("GetPlayerEntities").filter(
    834                 function(idx) { return (selected.indexOf(idx) == -1); }
    835         );
     833        var entities = Engine.GuiInterfaceCall(g_IsObserver ? "GetNonGaiaEntities" : "GetPlayerEntities").filter(idx => selected.indexOf(idx) == -1);
    836834    }
    837835
    838836    Engine.GuiInterfaceCall("SetStatusBars", { "entities": entities, "enabled": g_ShowAllStatusBars });
    839837}
    840 
    841838// Update the additional list of entities to be highlighted.
    842839function updateAdditionalHighlight()
    843840{
    844841    var entsAdd = [];    // list of entities units to be highlighted
    845842    var entsRemove = [];
  • binaries/data/mods/public/simulation/components/GuiInterface.js

     
    891891{
    892892    var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
    893893    return cmpRangeManager.GetEntitiesByPlayer(player);
    894894};
    895895
     896GuiInterface.prototype.GetNonGaiaEntities = function()
     897{
     898    var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
     899    return cmpRangeManager.GetNonGaiaEntities();
     900};
     901
    896902/**
    897903 * Displays the rally points of a given list of entities (carried in cmd.entities).
    898904 *
    899905 * The 'cmd' object may carry its own x/z coordinate pair indicating the location where the rally point should
    900906 * be rendered, in order to support instantaneously rendering a rally point marker at a specified location
     
    18831889
    18841890    "SetSelectionHighlight": 1,
    18851891    "GetAllBuildableEntities": 1,
    18861892    "SetStatusBars": 1,
    18871893    "GetPlayerEntities": 1,
     1894    "GetNonGaiaEntities": 1,
    18881895    "DisplayRallyPoint": 1,
    18891896    "SetBuildingPlacementPreview": 1,
    18901897    "SetWallPlacementPreview": 1,
    18911898    "GetFoundationSnapData": 1,
    18921899    "PlaySound": 1,
  • source/gui/scripting/ScriptFunctions.cpp

     
    5959#include "renderer/scripting/JSInterface_Renderer.h"
    6060#include "simulation2/Simulation2.h"
    6161#include "simulation2/components/ICmpAIManager.h"
    6262#include "simulation2/components/ICmpCommandQueue.h"
    6363#include "simulation2/components/ICmpGuiInterface.h"
     64#include "simulation2/components/ICmpPlayerManager.h"
    6465#include "simulation2/components/ICmpRangeManager.h"
    6566#include "simulation2/components/ICmpSelectable.h"
    6667#include "simulation2/components/ICmpTemplateManager.h"
    6768#include "simulation2/helpers/Selection.h"
    6869#include "soundmanager/SoundManager.h"
     
    151152entity_id_t PickEntityAtPoint(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int x, int y)
    152153{
    153154    return EntitySelection::PickEntityAtPoint(*g_Game->GetSimulation2(), *g_Game->GetView()->GetCamera(), x, y, g_Game->GetPlayerID(), false);
    154155}
    155156
    156 std::vector<entity_id_t> PickFriendlyEntitiesInRect(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int x0, int y0, int x1, int y1, int player)
     157std::vector<entity_id_t> PickPlayerEntitiesInRect(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int x0, int y0, int x1, int y1, int player)
    157158{
    158159    return EntitySelection::PickEntitiesInRect(*g_Game->GetSimulation2(), *g_Game->GetView()->GetCamera(), x0, y0, x1, y1, player, false);
    159160}
    160161
    161 std::vector<entity_id_t> PickFriendlyEntitiesOnScreen(ScriptInterface::CxPrivate* pCxPrivate, int player)
     162std::vector<entity_id_t> PickPlayerEntitiesOnScreen(ScriptInterface::CxPrivate* pCxPrivate, int player)
    162163{
    163     return PickFriendlyEntitiesInRect(pCxPrivate, 0, 0, g_xres, g_yres, player);
     164    return PickPlayerEntitiesInRect(pCxPrivate, 0, 0, g_xres, g_yres, player);
    164165}
    165166
    166 std::vector<entity_id_t> PickSimilarFriendlyEntities(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::string templateName, bool includeOffScreen, bool matchRank, bool allowFoundations)
     167std::vector<entity_id_t> PickNonGaiaEntitiesOnScreen(ScriptInterface::CxPrivate* pCxPrivate)
     168{
     169    std::vector<entity_id_t> entities;
     170
     171    CmpPtr<ICmpPlayerManager> cmpPlayerManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY);
     172
     173    if (!cmpPlayerManager)
     174        return entities;
     175
     176    i32 numPlayers = cmpPlayerManager->GetNumPlayers();
     177    for (i32 player = 1; player < numPlayers; ++player)
     178    {
     179        std::vector<entity_id_t> ents = PickPlayerEntitiesOnScreen(pCxPrivate, player);
     180        entities.insert(entities.end(), ents.begin(), ents.end());
     181    }
     182    return entities;
     183}
     184
     185std::vector<entity_id_t> PickSimilarPlayerEntities(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), std::string templateName, bool includeOffScreen, bool matchRank, bool allowFoundations)
    167186{
    168187    return EntitySelection::PickSimilarEntities(*g_Game->GetSimulation2(), *g_Game->GetView()->GetCamera(), templateName, g_Game->GetPlayerID(), includeOffScreen, matchRank, false, allowFoundations);
    169188}
    170189
    171190CFixedVector3D GetTerrainAtScreenPoint(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int x, int y)
     
    942961    scriptInterface.RegisterFunction<JS::Value, std::wstring, JS::HandleValue, &GuiInterfaceCall>("GuiInterfaceCall");
    943962    scriptInterface.RegisterFunction<void, JS::HandleValue, &PostNetworkCommand>("PostNetworkCommand");
    944963
    945964    // Entity picking
    946965    scriptInterface.RegisterFunction<entity_id_t, int, int, &PickEntityAtPoint>("PickEntityAtPoint");
    947     scriptInterface.RegisterFunction<std::vector<entity_id_t>, int, int, int, int, int, &PickFriendlyEntitiesInRect>("PickFriendlyEntitiesInRect");
    948     scriptInterface.RegisterFunction<std::vector<entity_id_t>, int, &PickFriendlyEntitiesOnScreen>("PickFriendlyEntitiesOnScreen");
    949     scriptInterface.RegisterFunction<std::vector<entity_id_t>, std::string, bool, bool, bool, &PickSimilarFriendlyEntities>("PickSimilarFriendlyEntities");
     966    scriptInterface.RegisterFunction<std::vector<entity_id_t>, int, int, int, int, int, &PickPlayerEntitiesInRect>("PickPlayerEntitiesInRect");
     967    scriptInterface.RegisterFunction<std::vector<entity_id_t>, int, &PickPlayerEntitiesOnScreen>("PickPlayerEntitiesOnScreen");
     968    scriptInterface.RegisterFunction<std::vector<entity_id_t>, &PickNonGaiaEntitiesOnScreen>("PickNonGaiaEntitiesOnScreen");
     969    scriptInterface.RegisterFunction<std::vector<entity_id_t>, std::string, bool, bool, bool, &PickSimilarPlayerEntities>("PickSimilarPlayerEntities");
    950970    scriptInterface.RegisterFunction<CFixedVector3D, int, int, &GetTerrainAtScreenPoint>("GetTerrainAtScreenPoint");
    951971
    952972    // Network / game setup functions
    953973    scriptInterface.RegisterFunction<void, &StartNetworkGame>("StartNetworkGame");
    954974    scriptInterface.RegisterFunction<void, JS::HandleValue, int, &StartGame>("StartGame");
  • source/simulation2/components/CCmpRangeManager.cpp

     
    905905        }
    906906
    907907        return entities;
    908908    }
    909909
     910    virtual std::vector<entity_id_t> GetNonGaiaEntities()
     911    {
     912        // Compute ownership mask for all players except gaia as in CalcOwnerMask
     913        u32 ownerMask = ((1 << MAX_LOS_PLAYER_ID) - 1) << 1;
     914
     915        // Get the list of entities
     916        std::vector<entity_id_t> entities;
     917        for (EntityMap<EntityData>::const_iterator it = m_EntityData.begin(); it != m_EntityData.end(); ++it)
     918        {
     919            // Check owner and add to list if it matches
     920            if (CalcOwnerMask(it->second.owner) & ownerMask)
     921                entities.push_back(it->first);
     922        }
     923
     924        return entities;
     925    }
     926
    910927    virtual void SetDebugOverlay(bool enabled)
    911928    {
    912929        m_DebugOverlayEnabled = enabled;
    913930        m_DebugOverlayDirty = true;
    914931        if (!enabled)
  • source/simulation2/components/ICmpRangeManager.cpp

     
    4343DEFINE_INTERFACE_METHOD_1("DisableActiveQuery", void, ICmpRangeManager, DisableActiveQuery, ICmpRangeManager::tag_t)
    4444DEFINE_INTERFACE_METHOD_1("ResetActiveQuery", std::vector<entity_id_t>, ICmpRangeManager, ResetActiveQuery, ICmpRangeManager::tag_t)
    4545DEFINE_INTERFACE_METHOD_3("SetEntityFlag", void, ICmpRangeManager, SetEntityFlag, entity_id_t, std::string, bool)
    4646DEFINE_INTERFACE_METHOD_1("GetEntityFlagMask", u8, ICmpRangeManager, GetEntityFlagMask, std::string)
    4747DEFINE_INTERFACE_METHOD_1("GetEntitiesByPlayer", std::vector<entity_id_t>, ICmpRangeManager, GetEntitiesByPlayer, player_id_t)
     48DEFINE_INTERFACE_METHOD_0("GetNonGaiaEntities", std::vector<entity_id_t>, ICmpRangeManager, GetNonGaiaEntities)
    4849DEFINE_INTERFACE_METHOD_1("SetDebugOverlay", void, ICmpRangeManager, SetDebugOverlay, bool)
    4950DEFINE_INTERFACE_METHOD_1("ExploreAllTiles", void, ICmpRangeManager, ExploreAllTiles, player_id_t)
    5051DEFINE_INTERFACE_METHOD_0("ExploreTerritories", void, ICmpRangeManager, ExploreTerritories)
    5152DEFINE_INTERFACE_METHOD_2("SetLosRevealAll", void, ICmpRangeManager, SetLosRevealAll, player_id_t, bool)
    5253DEFINE_INTERFACE_METHOD_1("GetLosRevealAll", bool, ICmpRangeManager, GetLosRevealAll, player_id_t)
  • source/simulation2/components/ICmpRangeManager.h

     
    177177     * @return list of entities matching the query, ordered by increasing distance from the source entity.
    178178     */
    179179    virtual std::vector<entity_id_t> ResetActiveQuery(tag_t tag) = 0;
    180180
    181181    /**
    182      * Returns list of all entities for specific player.
     182     * Returns a list of all entities for specific player.
    183183     * (This is on this interface because it shares a lot of the implementation.
    184184     * Maybe it should be extended to be more like ExecuteQuery without
    185185     * the range parameter.)
    186186     */
    187187    virtual std::vector<entity_id_t> GetEntitiesByPlayer(player_id_t player) = 0;
    188188
    189189    /**
     190     * Returns a list of all entities of all players except gaia.
     191     */
     192    virtual std::vector<entity_id_t> GetNonGaiaEntities() = 0;
     193
     194    /**
    190195     * Toggle the rendering of debug info.
    191196     */
    192197    virtual void SetDebugOverlay(bool enabled) = 0;
    193198
    194199    /**
  • source/simulation2/helpers/Selection.cpp

     
    142142
    143143    if (owner != INVALID_PLAYER)
    144144    {
    145145        CComponentManager& componentManager = simulation.GetSimContext().GetComponentManager();
    146146        std::vector<entity_id_t> ents = cmpRangeManager->GetEntitiesByPlayer(owner);
    147         for (std::vector<entity_id_t>::iterator it = ents.begin(); it != ents.end(); ++it)
     147        for (const entity_id_t& ent : ents)
    148148        {
    149             if (CheckEntityVisibleAndInRect(componentManager.LookupEntityHandle(*it), cmpRangeManager, camera, sx0, sy0, sx1, sy1, owner, allowEditorSelectables))
    150                 hitEnts.push_back(*it);
     149            if (CheckEntityVisibleAndInRect(componentManager.LookupEntityHandle(ent), cmpRangeManager, camera, sx0, sy0, sx1, sy1, owner, allowEditorSelectables))
     150                hitEnts.push_back(ent);
    151151        }
    152152    }
    153153    else // owner == INVALID_PLAYER; Used when selecting units in Atlas or other mods that allow all kinds of selectables to be selected.
    154154    {
    155155        const CSimulation2::InterfaceListUnordered& selectableEnts = simulation.GetEntitiesWithInterfaceUnordered(IID_Selectable);
     
    161161    }
    162162
    163163    return hitEnts;
    164164}
    165165
     166std::vector<entity_id_t> EntitySelection::PickNonGaiaEntitiesInRect(CSimulation2& simulation, const CCamera& camera, int sx0, int sy0, int sx1, int sy1, bool allowEditorSelectables)
     167{
     168    PROFILE2("PickNonGaiaEntitiesInRect");
     169    // Make sure sx0 <= sx1, and sy0 <= sy1
     170    if (sx0 > sx1)
     171        std::swap(sx0, sx1);
     172    if (sy0 > sy1)
     173        std::swap(sy0, sy1);
     174
     175    CmpPtr<ICmpRangeManager> cmpRangeManager(simulation, SYSTEM_ENTITY);
     176    ENSURE(cmpRangeManager);
     177
     178    std::vector<entity_id_t> hitEnts;
     179
     180    CComponentManager& componentManager = simulation.GetSimContext().GetComponentManager();
     181
     182    std::vector<entity_id_t> ents = cmpRangeManager->GetNonGaiaEntities();
     183    for (const entity_id_t& ent : ents)
     184    {
     185        CmpPtr<ICmpOwnership> cmpOwnership(simulation.GetSimContext(), ent);
     186        if (CheckEntityVisibleAndInRect(componentManager.LookupEntityHandle(ent), cmpRangeManager, camera, sx0, sy0, sx1, sy1, cmpOwnership->GetOwner(), allowEditorSelectables))
     187            hitEnts.push_back(ent);
     188    }
     189
     190    return hitEnts;
     191}
     192
    166193std::vector<entity_id_t> EntitySelection::PickSimilarEntities(CSimulation2& simulation, const CCamera& camera,
    167194    const std::string& templateName, player_id_t owner, bool includeOffScreen, bool matchRank,
    168195    bool allowEditorSelectables, bool allowFoundations)
    169196{
    170197    PROFILE2("PickSimilarEntities");
  • source/simulation2/helpers/Selection.h

     
    6363 * @return unordered list of selected entities.
    6464 */
    6565std::vector<entity_id_t> PickEntitiesInRect(CSimulation2& simulation, const CCamera& camera, int sx0, int sy0, int sx1, int sy1, player_id_t owner, bool allowEditorSelectables);
    6666
    6767/**
     68 * Finds all selectable entities within the given screen coordinate rectangle,
     69 * belonging to any player.
     70 *
     71 * @param camera use this view to convert screen to world coordinates.
     72 * @param sx0,sy0,sx1,sy1 diagonally opposite corners of the rectangle in 2D screen coordinates.
     73 * @param owner player whose entities we are selecting. Ownership is ignored if
     74 *  INVALID_PLAYER is used.
     75 * @param allowEditorSelectables if true, all entities with the ICmpSelectable interface
     76 *  will be selected (including decorative actors), else only those selectable ingame.
     77 *
     78 * @return unordered list of selected entities.
     79 */
     80std::vector<entity_id_t> PickNonGaiaEntitiesInRect(CSimulation2& simulation, const CCamera& camera, int sx0, int sy0, int sx1, int sy1, bool allowEditorSelectables);
     81
     82/**
    6883 * Finds all entities with the given entity template name, belonging to the given player.
    6984 *
    7085 * @param camera use this view to convert screen to world coordinates.
    7186 * @param templateName the name of the template to match, or the selection group name
    7287 *  for similar matching.