Ticket #1334: shared-los-3.patch

File shared-los-3.patch, 7.9 KB (added by Deiz, 12 years ago)
  • binaries/data/mods/public/simulation/components/Player.js

    diff --git a/binaries/data/mods/public/simulation/components/Player.js b/binaries/data/mods/public/simulation/components/Player.js
    index 56c3a8a..5dc6a5c 100644
    a b Player.prototype.GetDiplomacy = function()  
    221221Player.prototype.SetDiplomacy = function(dipl)
    222222{
    223223    this.diplomacy = dipl;
     224    this.UpdateSharedLos();
     225};
     226
     227Player.prototype.SetDiplomacyIndex = function(ind, value)
     228{
     229    this.diplomacy[ind] = value;
     230    this.UpdateSharedLos();
     231};
     232
     233Player.prototype.UpdateSharedLos = function()
     234{
     235    var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
     236    if (!cmpRangeManager)
     237        return;
     238
     239    var allies = [];
     240    for (var i = 0; i < this.diplomacy.length; ++i)
     241        if (this.IsAlly(i))
     242            allies.push(i);
     243
     244    cmpRangeManager.SetSharedLosMask( this.playerID, allies );
    224245};
    225246
    226247Player.prototype.GetFormations = function()
    Player.prototype.IsAI = function()  
    275296
    276297Player.prototype.SetAlly = function(id)
    277298{
    278     this.diplomacy[id] = 1;
     299    this.SetDiplomacyIndex(id, 1);
    279300};
    280301
    281302/**
    Player.prototype.IsAlly = function(id)  
    288309
    289310Player.prototype.SetEnemy = function(id)
    290311{
    291     this.diplomacy[id] = -1;
     312    this.SetDiplomacyIndex(id, -1);
    292313};
    293314
    294315/**
    Player.prototype.IsEnemy = function(id)  
    301322
    302323Player.prototype.SetNeutral = function(id)
    303324{
    304     this.diplomacy[id] = 0;
     325    this.SetDiplomacyIndex(id, 0);
    305326};
    306327
    307328/**
  • source/graphics/LOSTexture.cpp

    diff --git a/source/graphics/LOSTexture.cpp b/source/graphics/LOSTexture.cpp
    index e89c7d7..5f9ebf7 100644
    a b void CLOSTexture::RecomputeTexture(int unit)  
    178178    if (!cmpRangeManager)
    179179        return;
    180180
    181     ICmpRangeManager::CLosQuerier los (cmpRangeManager->GetLosQuerier(g_Game->GetPlayerID()));
     181    u32 playerMask = cmpRangeManager->GetSharedLosMask(g_Game->GetPlayerID());
     182    ICmpRangeManager::CLosQuerier los (cmpRangeManager->GetLosQuerier(g_Game->GetPlayerID(), playerMask));
    182183
    183184    GenerateBitmap(los, &losData[0], m_MapSize, m_MapSize);
    184185
  • source/simulation2/components/CCmpRangeManager.cpp

    diff --git a/source/simulation2/components/CCmpRangeManager.cpp b/source/simulation2/components/CCmpRangeManager.cpp
    index 44a098d..7d10b3d 100644
    a b  
    2222
    2323#include "simulation2/MessageTypes.h"
    2424#include "simulation2/components/ICmpPosition.h"
     25#include "simulation2/components/ICmpPlayerManager.h"
    2526#include "simulation2/components/ICmpTerritoryManager.h"
    2627#include "simulation2/components/ICmpVision.h"
    2728#include "simulation2/helpers/Render.h"
    public:  
    212213    // (TODO: this is usually a waste of memory)
    213214    std::vector<u32> m_LosStateRevealed;
    214215
     216    // Shared LOS masks, one per player.
     217    std::vector<u32> m_PlayerLosMasks;
     218
    215219    static std::string GetSchema()
    216220    {
    217221        return "<a:component type='system'/><empty/>";
    public:  
    915919            return CLosQuerier(player, m_LosState, m_TerrainVerticesPerSide);
    916920    }
    917921
     922    virtual CLosQuerier GetLosQuerier(player_id_t player, u32 playerMask)
     923    {
     924        if (GetLosRevealAll(player))
     925            return CLosQuerier(playerMask, m_LosStateRevealed, m_TerrainVerticesPerSide);
     926        else
     927            return CLosQuerier(playerMask, m_LosState, m_TerrainVerticesPerSide);
     928    }
     929
    918930    virtual ELosVisibility GetLosVisibility(entity_id_t ent, player_id_t player, bool forceRetainInFog)
    919931    {
     932        return GetLosVisibility(ent, player, 0, forceRetainInFog);
     933    }
     934
     935    virtual ELosVisibility GetLosVisibility(entity_id_t ent, player_id_t player, u32 playerMask, bool forceRetainInFog)
     936    {
    920937        // (We can't use m_EntityData since this needs to handle LOCAL entities too)
    921938
    922939        // Entities not with positions in the world are never visible
    public:  
    939956        }
    940957
    941958        // Visible if within a visible region
     959        if (!playerMask)
     960            playerMask = GetSharedLosMask(player);
    942961
    943         CLosQuerier los(player, m_LosState, m_TerrainVerticesPerSide);
     962        CLosQuerier los(playerMask, m_LosState, m_TerrainVerticesPerSide);
    944963
    945964        if (los.IsVisible(i, j))
    946965            return VIS_VISIBLE;
    public:  
    957976        return VIS_HIDDEN;
    958977    }
    959978
     979    virtual u32 GetLosVisibilityMask(std::vector<player_id_t> players)
     980    {
     981        u32 playerMask = 0;
     982        player_id_t player;
     983
     984        for (size_t i = 0; i < players.size(); i++)
     985        {
     986            player = players[i];
     987            if (player > 0 && player <= 16)
     988                playerMask |= LOS_MASK << (2*(player-1));
     989        }
     990
     991        return playerMask;
     992    }
     993
    960994    virtual void SetLosRevealAll(player_id_t player, bool enabled)
    961995    {
    962996        m_LosRevealAll[player] = enabled;
    public:  
    9911025        return m_LosCircular;
    9921026    }
    9931027
     1028    virtual void SetSharedLosMask(player_id_t player, std::vector<player_id_t> players)
     1029    {
     1030        CmpPtr<ICmpPlayerManager> cmpPlayerManager(GetSimContext(), SYSTEM_ENTITY);
     1031        if (!cmpPlayerManager)
     1032            return;
     1033
     1034        int numPlayers = cmpPlayerManager->GetNumPlayers();
     1035        m_PlayerLosMasks.resize(numPlayers);
     1036
     1037        if (player >= 0  && player <= (int)m_PlayerLosMasks.size())
     1038            m_PlayerLosMasks[player] = GetLosVisibilityMask(players);
     1039    }
     1040
     1041    virtual u32 GetSharedLosMask(player_id_t player)
     1042    {
     1043        if (player < 0 || player >= (int)m_PlayerLosMasks.size())
     1044            return 0;
     1045
     1046        return m_PlayerLosMasks[player];
     1047    }
     1048
    9941049    void UpdateTerritoriesLos()
    9951050    {
    9961051        CmpPtr<ICmpTerritoryManager> cmpTerritoryManager(GetSimContext(), SYSTEM_ENTITY);
  • source/simulation2/components/ICmpRangeManager.cpp

    diff --git a/source/simulation2/components/ICmpRangeManager.cpp b/source/simulation2/components/ICmpRangeManager.cpp
    index 4503ef2..ad18ff2 100644
    a b DEFINE_INTERFACE_METHOD_2("SetLosRevealAll", void, ICmpRangeManager, SetLosRevea  
    4848DEFINE_INTERFACE_METHOD_3("GetLosVisibility", std::string, ICmpRangeManager, GetLosVisibility_wrapper, entity_id_t, player_id_t, bool)
    4949DEFINE_INTERFACE_METHOD_1("SetLosCircular", void, ICmpRangeManager, SetLosCircular, bool)
    5050DEFINE_INTERFACE_METHOD_0("GetLosCircular", bool, ICmpRangeManager, GetLosCircular)
     51DEFINE_INTERFACE_METHOD_2("SetSharedLosMask", void, ICmpRangeManager, SetSharedLosMask, player_id_t, std::vector<player_id_t>)
    5152DEFINE_INTERFACE_METHOD_1("GetPercentMapExplored", i32, ICmpRangeManager, GetPercentMapExplored, player_id_t)
    5253END_INTERFACE_WRAPPER(RangeManager)
  • source/simulation2/components/ICmpRangeManager.h

    diff --git a/source/simulation2/components/ICmpRangeManager.h b/source/simulation2/components/ICmpRangeManager.h
    index 142936e..2ebd6ff 100644
    a b public:  
    194194                m_PlayerMask = 0;
    195195        }
    196196
     197        CLosQuerier(u32 playerMask, const std::vector<u32>& data, ssize_t verticesPerSide) :
     198            m_Data(&data[0]), m_VerticesPerSide(verticesPerSide)
     199        {
     200            m_PlayerMask = playerMask;
     201        }
     202
    197203        const CLosQuerier& operator=(const CLosQuerier&); // not implemented
    198204
    199205    public:
    public:  
    269275     * Returns a CLosQuerier for checking whether vertex positions are visible to the given player.
    270276     */
    271277    virtual CLosQuerier GetLosQuerier(player_id_t player) = 0;
     278    virtual CLosQuerier GetLosQuerier(player_id_t player, u32 playerMask) = 0;
    272279
    273280    /**
    274281     * Returns the visibility status of the given entity, with respect to the given player.
    public:  
    279286     *  see http://trac.wildfiregames.com/ticket/958
    280287     */
    281288    virtual ELosVisibility GetLosVisibility(entity_id_t ent, player_id_t player, bool forceRetainInFog = false) = 0;
     289    virtual u32 GetLosVisibilityMask(std::vector<player_id_t> players) = 0;
    282290
    283291    /**
    284292     * GetLosVisibility wrapped for script calls.
    public:  
    308316    virtual bool GetLosCircular() = 0;
    309317
    310318    /**
     319     * Sets LOS mask for a player based on the supplied array of players.
     320     */
     321    virtual void SetSharedLosMask(player_id_t player, std::vector<player_id_t> players) = 0;
     322    virtual u32 GetSharedLosMask(player_id_t player) = 0;
     323
     324    /**
    311325     * Get percent map explored statistics for specified player.
    312326     */
    313327    virtual i32 GetPercentMapExplored(player_id_t player) = 0;