Ticket #1707: range_manager_optimisation.diff

File range_manager_optimisation.diff, 4.6 KB (added by Jonathan Waller, 11 years ago)

WIP patch, currently works but is done in parallel and doesn't serialize so won't survive saving.

  • source/simulation2/components/CCmpRangeManager.cpp

     
    211211    tag_t m_QueryNext; // next allocated id
    212212    std::map<tag_t, Query> m_Queries;
    213213    std::map<entity_id_t, EntityData> m_EntityData;
     214    std::vector<EntityData> m_EntityDataArray;
    214215    SpatialSubdivision<entity_id_t> m_Subdivision; // spatial index of m_EntityData
    215216
    216217    // LOS state:
     
    285286        serialize.NumberU32_Unbounded("query next", m_QueryNext);
    286287        SerializeMap<SerializeU32_Unbounded, SerializeQuery>()(serialize, "queries", m_Queries);
    287288        SerializeMap<SerializeU32_Unbounded, SerializeEntityData>()(serialize, "entity data", m_EntityData);
     289        //SerializeVector<SerializeEntityData>(serialize, "entity data array", m_EntityDataArray);
    288290
    289291        SerializeMap<SerializeI32_Unbounded, SerializeBool>()(serialize, "los reveal all", m_LosRevealAll);
    290292        serialize.Bool("los circular", m_LosCircular);
     
    336338            // (any initialisation of those values will happen later), so we can just
    337339            // use the default-constructed EntityData here
    338340            EntityData entdata;
     341           
    339342
    340343            // Store the LOS data, if any
    341344            CmpPtr<ICmpVision> cmpVision(GetSimContext(), ent);
     
    347350
    348351            // Remember this entity
    349352            m_EntityData.insert(std::make_pair(ent, entdata));
     353            if (ent >= m_EntityDataArray.size()) {
     354                m_EntityDataArray.resize(ent+1);
     355            }
     356            m_EntityDataArray[ent] = entdata;
    350357
    351358            break;
    352359        }
     
    358365            std::map<entity_id_t, EntityData>::iterator it = m_EntityData.find(ent);
    359366
    360367            // Ignore if we're not already tracking this entity
    361             if (it == m_EntityData.end())
     368            if (it == m_EntityData.end() || ent >= m_EntityDataArray.size())
    362369                break;
    363370
    364371            if (msgData.inWorld)
     
    380387                it->second.inWorld = 1;
    381388                it->second.x = msgData.x;
    382389                it->second.z = msgData.z;
     390               
     391                EntityData& entData = m_EntityDataArray[ent];
     392                entData.inWorld = 1;
     393                entData.x = msgData.x;
     394                entData.z = msgData.z;
    383395            }
    384396            else
    385397            {
     
    393405                it->second.inWorld = 0;
    394406                it->second.x = entity_pos_t::Zero();
    395407                it->second.z = entity_pos_t::Zero();
     408               
     409                EntityData& entData = m_EntityDataArray[ent];
     410                entData.inWorld = 0;
     411                entData.x = entity_pos_t::Zero();
     412                entData.z = entity_pos_t::Zero();
    396413            }
    397414
    398415            break;
     
    417434
    418435            ENSURE(-128 <= msgData.to && msgData.to <= 127);
    419436            it->second.owner = (i8)msgData.to;
     437           
     438            m_EntityDataArray[ent].owner = (i8)msgData.to;
    420439
    421440            break;
    422441        }
     
    467486                LosRemove(it->second.owner, oldRange, pos);
    468487
    469488            it->second.visionRange = newRange;
     489            m_EntityDataArray[ent].visionRange = newRange;
    470490
    471491            if (it->second.inWorld)
    472492                LosAdd(it->second.owner, newRange, pos);
     
    643663            return r;
    644664        }
    645665
     666        PROFILE_START("perform");
    646667        PerformQuery(q, r);
     668        PROFILE_END("perform");
    647669
    648670        // Return the list sorted by distance from the entity
    649671        CFixedVector2D pos = cmpSourcePosition->GetPosition2D();
     
    815837        else
    816838        {
    817839            // Get a quick list of entities that are potentially in range
    818             std::vector<entity_id_t> ents = m_Subdivision.GetNear(pos, q.maxRange);
     840            std::vector<entity_id_t> ents;
     841            PROFILE_START("GetNear");
     842            ents = m_Subdivision.GetNear(pos, q.maxRange);
     843            PROFILE_END("GetNear");
    819844
    820845            for (size_t i = 0; i < ents.size(); ++i)
    821846            {
    822                 std::map<entity_id_t, EntityData>::const_iterator it = m_EntityData.find(ents[i]);
    823                 ENSURE(it != m_EntityData.end());
     847                //std::map<entity_id_t, EntityData>::const_iterator it = m_EntityData.find(ents[i]);
     848                //ENSURE(it != m_EntityData.end());
     849                const EntityData& entData = m_EntityDataArray[ents[i]];
     850               
     851                //ENSURE(entData.x == it->second.x);
     852               
     853                //if (entData.x != it->second.x) LOGWARNING(L"hi");
    824854
    825                 if (!TestEntityQuery(q, it->first, it->second))
     855                if (!TestEntityQuery(q, ents[i], entData))
    826856                    continue;
    827857
    828858                // Restrict based on precise distance
    829                 int distVsMax = (CFixedVector2D(it->second.x, it->second.z) - pos).CompareLength(q.maxRange);
     859                int distVsMax = (CFixedVector2D(entData.x, entData.z) - pos).CompareLength(q.maxRange);
    830860                if (distVsMax > 0)
    831861                    continue;
    832862
    833863                if (!q.minRange.IsZero())
    834864                {
    835                     int distVsMin = (CFixedVector2D(it->second.x, it->second.z) - pos).CompareLength(q.minRange);
     865                    int distVsMin = (CFixedVector2D(entData.x, entData.z) - pos).CompareLength(q.minRange);
    836866                    if (distVsMin < 0)
    837867                        continue;
    838868                }
    839869
    840                 r.push_back(it->first);
     870                r.push_back(ents[i]);
    841871            }
    842872        }
    843873    }