Ticket #2012: percent-explored.patch

File percent-explored.patch, 7.3 KB (added by scroogie, 11 years ago)
  • source/simulation2/components/CCmpRangeManager.cpp

     
    238238    // Shared LOS masks, one per player.
    239239    std::map<player_id_t, u32> m_SharedLosMasks;
    240240
     241    u32 m_OverallVisibleVertices;
     242    // Number of explored vertices per player.
     243    std::vector<u32> m_ExploredVertices;
     244
    241245    static std::string GetSchema()
    242246    {
    243247        return "<a:component type='system'/><empty/>";
     
    514518        SpatialSubdivision<entity_id_t> oldSubdivision = m_Subdivision;
    515519
    516520        ResetDerivedData(true);
    517        
     521
    518522        if (oldPlayerCounts != m_LosPlayerCounts)
    519523        {
    520524            for (size_t i = 0; i < oldPlayerCounts.size(); ++i)
     
    547551
    548552        m_LosPlayerCounts.clear();
    549553        m_LosPlayerCounts.resize(MAX_LOS_PLAYER_ID+1);
    550         if (!skipLosState)
     554        m_ExploredVertices.clear();
     555        m_ExploredVertices.resize(MAX_LOS_PLAYER_ID+1, 0);
     556        if (skipLosState) {
     557            // recalc current exploration stats.
     558            for (i32 j = 0; j < m_TerrainVerticesPerSide; j++)
     559            {
     560                for (i32 i = 0; i < m_TerrainVerticesPerSide; i++)
     561                {
     562                    if (!LosIsOffWorld(i, j))
     563                    {
     564                        for (u8 k = 1; k < MAX_LOS_PLAYER_ID+1; ++k) {
     565                            m_ExploredVertices.at(k) += ((m_LosState[j*m_TerrainVerticesPerSide + i] & (LOS_EXPLORED << (2*(k-1)))) > 0);
     566                        }
     567                    }
     568                }
     569            }
     570        }
     571        else
    551572        {
    552573            m_LosState.clear();
    553574            m_LosState.resize(m_TerrainVerticesPerSide*m_TerrainVerticesPerSide);
     
    561582                LosAdd(it->second.owner, it->second.visionRange, CFixedVector2D(it->second.x, it->second.z));
    562583        }
    563584
     585        m_OverallVisibleVertices = 0;
    564586        for (ssize_t j = 0; j < m_TerrainVerticesPerSide; ++j)
    565587            for (ssize_t i = 0; i < m_TerrainVerticesPerSide; ++i)
    566                 m_LosStateRevealed[i + j*m_TerrainVerticesPerSide] = LosIsOffWorld(i, j) ? 0 : 0xFFFFFFFFu;
     588            {
     589                if (LosIsOffWorld(i,j))
     590                {
     591                    m_LosStateRevealed[i + j*m_TerrainVerticesPerSide] = 0;
     592                }
     593                else
     594                {
     595                    m_LosStateRevealed[i + j*m_TerrainVerticesPerSide] = 0xFFFFFFFFu;
     596                    m_OverallVisibleVertices++;
     597                }
     598            }
    567599    }
    568600
    569601    void ResetSubdivisions(entity_pos_t x1, entity_pos_t z1)
     
    931963        for (size_t i = 0; i < m_DebugOverlayLines.size(); ++i)
    932964            collector.Submit(&m_DebugOverlayLines[i]);
    933965    }
    934    
     966
    935967    virtual u8 GetEntityFlagMask(std::string identifier)
    936968    {
    937969        if (identifier == "normal")
     
    942974        LOGWARNING(L"CCmpRangeManager: Invalid flag identifier %hs", identifier.c_str());
    943975        return 0;
    944976    }
    945    
     977
    946978    virtual void SetEntityFlag(entity_id_t ent, std::string identifier, bool value)
    947979    {
    948980        std::map<entity_id_t, EntityData>::iterator it = m_EntityData.find(ent);
     
    10851117                u8 p = grid.get(i, j) & ICmpTerritoryManager::TERRITORY_PLAYER_MASK;
    10861118                if (p > 0 && p <= MAX_LOS_PLAYER_ID)
    10871119                {
     1120                    u32 &explored = m_ExploredVertices.at(p);
     1121                    explored += !(m_LosState[i + j*m_TerrainVerticesPerSide] & (LOS_EXPLORED << (2*(p-1))));
    10881122                    m_LosState[i + j*m_TerrainVerticesPerSide] |= (LOS_EXPLORED << (2*(p-1)));
     1123                    explored += !(m_LosState[i+1 + j*m_TerrainVerticesPerSide] & (LOS_EXPLORED << (2*(p-1))));
    10891124                    m_LosState[i+1 + j*m_TerrainVerticesPerSide] |= (LOS_EXPLORED << (2*(p-1)));
     1125                    explored += !(m_LosState[i + (j+1)*m_TerrainVerticesPerSide] & (LOS_EXPLORED << (2*(p-1))));
    10901126                    m_LosState[i + (j+1)*m_TerrainVerticesPerSide] |= (LOS_EXPLORED << (2*(p-1)));
     1127                    explored += !(m_LosState[i+1 + (j+1)*m_TerrainVerticesPerSide] & (LOS_EXPLORED << (2*(p-1))));
    10911128                    m_LosState[i+1 + (j+1)*m_TerrainVerticesPerSide] |= (LOS_EXPLORED << (2*(p-1)));
    10921129                }
    10931130            }
     
    11351172
    11361173        i32 idx0 = j*m_TerrainVerticesPerSide + i0;
    11371174        i32 idx1 = j*m_TerrainVerticesPerSide + i1;
    1138 
     1175        u32 &explored = m_ExploredVertices.at(owner);
    11391176        for (i32 idx = idx0; idx <= idx1; ++idx)
    11401177        {
    11411178            // Increasing from zero to non-zero - move from unexplored/explored to visible+explored
    11421179            if (counts[idx] == 0)
    11431180            {
    11441181                i32 i = i0 + idx - idx0;
    1145                 if (!LosIsOffWorld(i, j))
     1182                if (!LosIsOffWorld(i, j)) {
     1183                    explored += !(m_LosState[idx] & (LOS_EXPLORED << (2*(owner-1))));
    11461184                    m_LosState[idx] |= ((LOS_VISIBLE | LOS_EXPLORED) << (2*(owner-1)));
     1185                }
    11471186            }
    11481187
    11491188            ASSERT(counts[idx] < 65535);
     
    11611200
    11621201        i32 idx0 = j*m_TerrainVerticesPerSide + i0;
    11631202        i32 idx1 = j*m_TerrainVerticesPerSide + i1;
    1164 
    11651203        for (i32 idx = idx0; idx <= idx1; ++idx)
    11661204        {
    11671205            ASSERT(counts[idx] > 0);
     
    14321470        }
    14331471    }
    14341472
    1435     virtual i32 GetPercentMapExplored(player_id_t player)
     1473    virtual u8 GetPercentMapExplored(player_id_t player)
    14361474    {
    1437         i32 exploredVertices = 0;
    1438         i32 overallVisibleVertices = 0;
    1439         CLosQuerier los(CalcPlayerLosMask(player), m_LosState, m_TerrainVerticesPerSide);
    1440 
    1441         for (i32 j = 0; j < m_TerrainVerticesPerSide; j++)
    1442         {
    1443             for (i32 i = 0; i < m_TerrainVerticesPerSide; i++)
    1444             {
    1445                 if (!LosIsOffWorld(i, j))
    1446                 {
    1447                     overallVisibleVertices++;
    1448                     exploredVertices += (i32)los.IsExplored_UncheckedRange(i, j);
    1449                 }
    1450             }
    1451         }
    1452 
    1453         if (overallVisibleVertices == 0) // avoid divide-by-zero
    1454             return 0;
    1455 
    1456         return exploredVertices * 100 / overallVisibleVertices;
     1475        return m_ExploredVertices.at((u8)player) * 100 / m_OverallVisibleVertices;
    14571476    }
    14581477};
    14591478
  • source/simulation2/components/ICmpRangeManager.cpp

     
    4949DEFINE_INTERFACE_METHOD_1("SetLosCircular", void, ICmpRangeManager, SetLosCircular, bool)
    5050DEFINE_INTERFACE_METHOD_0("GetLosCircular", bool, ICmpRangeManager, GetLosCircular)
    5151DEFINE_INTERFACE_METHOD_2("SetSharedLos", void, ICmpRangeManager, SetSharedLos, player_id_t, std::vector<player_id_t>)
    52 DEFINE_INTERFACE_METHOD_1("GetPercentMapExplored", i32, ICmpRangeManager, GetPercentMapExplored, player_id_t)
     52DEFINE_INTERFACE_METHOD_1("GetPercentMapExplored", u8, ICmpRangeManager, GetPercentMapExplored, player_id_t)
    5353END_INTERFACE_WRAPPER(RangeManager)
  • source/simulation2/components/ICmpRangeManager.h

     
    317317    /**
    318318     * Get percent map explored statistics for specified player.
    319319     */
    320     virtual i32 GetPercentMapExplored(player_id_t player) = 0;
     320    virtual u8 GetPercentMapExplored(player_id_t player) = 0;
    321321
    322322
    323323    /**
  • binaries/data/mods/public/simulation/components/StatisticsTracker.js

     
    225225{
    226226    var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
    227227    var cmpPlayer = Engine.QueryInterface(this.entity, IID_Player);
    228     return cmpRangeManager.GetPercentMapExplored(cmpPlayer.GetPlayerID());
     228    if (cmpPlayer.GetPlayerID() == 0) return 0;
     229    return cmpRangeManager.GetPercentMapExplored(cmpPlayer.GetPlayerID()); 
    229230};
    230231
    231232Engine.RegisterComponentType(IID_StatisticsTracker, "StatisticsTracker", StatisticsTracker);