Ticket #2285: damage.diff

File damage.diff, 7.6 KB (added by sanderd17, 10 years ago)
  • binaries/data/mods/public/simulation/helpers/Damage.js

     
    9999    // If there is insufficient data return an empty array.
    100100    if (!origin || !radius)
    101101        return [];
    102     // Create/recycle the dummy entity used for range calculations.
    103     if (!Damage.dummyTargetEntity || dummyPath != Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager).GetCurrentTemplateName(Damage.dummyTargetEntity))
    104         Damage.dummyTargetEntity = Engine.AddEntity(dummyPath);
    105     // Move the dummy entity to the origin of the query.
    106     var cmpDummyPosition = Engine.QueryInterface(Damage.dummyTargetEntity, IID_Position);
    107     if (!cmpDummyPosition)
    108         return [];
    109     cmpDummyPosition.JumpTo(origin.x, origin.z);
    110102
    111103    // If the players parameter is not specified use all players.
    112104    if (!players)
     
    119111
    120112    // Call RangeManager with dummy entity and return the result.
    121113    var rangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
    122     var rangeQuery = rangeManager.ExecuteQuery(Damage.dummyTargetEntity, 0, radius, players, IID_DamageReceiver);
     114    var rangeQuery = rangeManager.ExecuteQueryAroundPos({"x": origin.x, "y": origin.z}, 0, radius, players, IID_DamageReceiver);
    123115    return rangeQuery;
    124116};
    125117
  • source/simulation2/components/CCmpRangeManager.cpp

     
    3333#include "graphics/Overlay.h"
    3434#include "graphics/Terrain.h"
    3535#include "lib/timer.h"
    36 #include "maths/FixedVector2D.h"
    3736#include "ps/CLogger.h"
    3837#include "ps/Overlay.h"
    3938#include "ps/Profile.h"
     
    729728        q.enabled = false;
    730729    }
    731730
     731    virtual std::vector<entity_id_t> ExecuteQueryAroundPos(CFixedVector2D pos,
     732        entity_pos_t minRange, entity_pos_t maxRange,
     733        std::vector<int> owners, int requiredInterface)
     734    {
     735        Query q = ConstructQuery(INVALID_ENTITY, minRange, maxRange, owners, requiredInterface, GetEntityFlagMask("normal"));
     736        std::vector<entity_id_t> r;
     737        PerformQuery(q, r, pos);
     738
     739        // Return the list sorted by distance from the entity
     740        std::stable_sort(r.begin(), r.end(), EntityDistanceOrdering(m_EntityData, pos));
     741
     742        return r;
     743    }
     744
    732745    virtual std::vector<entity_id_t> ExecuteQuery(entity_id_t source,
    733746        entity_pos_t minRange, entity_pos_t maxRange,
    734747        std::vector<int> owners, int requiredInterface)
     
    746759            return r;
    747760        }
    748761
    749         PerformQuery(q, r);
     762        CFixedVector2D pos = cmpSourcePosition->GetPosition2D();
     763        PerformQuery(q, r, pos);
    750764
    751765        // Return the list sorted by distance from the entity
    752         CFixedVector2D pos = cmpSourcePosition->GetPosition2D();
    753766        std::stable_sort(r.begin(), r.end(), EntityDistanceOrdering(m_EntityData, pos));
    754767
    755768        return r;
     
    779792            return r;
    780793        }
    781794
    782         PerformQuery(q, r);
     795        CFixedVector2D pos = cmpSourcePosition->GetPosition2D();
     796        PerformQuery(q, r, pos);
    783797
    784798        q.lastMatch = r;
    785799
    786800        // Return the list sorted by distance from the entity
    787         CFixedVector2D pos = cmpSourcePosition->GetPosition2D();
    788801        std::stable_sort(r.begin(), r.end(), EntityDistanceOrdering(m_EntityData, pos));
    789802
    790803        return r;
     
    841854
    842855            results.clear();
    843856            results.reserve(query.lastMatch.size());
    844             PerformQuery(query, results);
     857            CFixedVector2D pos = cmpSourcePosition->GetPosition2D();
     858            PerformQuery(query, results, pos);
    845859
    846860            // Compute the changes vs the last match
    847861            added.clear();
     
    902916    /**
    903917     * Returns a list of distinct entity IDs that match the given query, sorted by ID.
    904918     */
    905     void PerformQuery(const Query& q, std::vector<entity_id_t>& r)
     919    void PerformQuery(const Query& q, std::vector<entity_id_t>& r, CFixedVector2D pos)
    906920    {
    907         CmpPtr<ICmpPosition> cmpSourcePosition(q.source);
    908         if (!cmpSourcePosition || !cmpSourcePosition->IsInWorld())
    909             return;
    910         CFixedVector2D pos = cmpSourcePosition->GetPosition2D();
    911921
    912922        // Special case: range -1.0 means check all entities ignoring distance
    913923        if (q.maxRange == entity_pos_t::FromInt(-1))
     
    924934        else if (q.parabolic)
    925935        {
    926936            // elevationBonus is part of the 3D position, as the source is really that much heigher
     937            CmpPtr<ICmpPosition> cmpSourcePosition(q.source);
    927938            CFixedVector3D pos3d = cmpSourcePosition->GetPosition()+
    928939                CFixedVector3D(entity_pos_t::Zero(), q.elevationBonus, entity_pos_t::Zero()) ;
    929940            // Get a quick list of entities that are potentially in range, with a cutoff of 2*maxRange
     
    9921003        }
    9931004    }
    9941005
    995 
    9961006    virtual entity_pos_t GetElevationAdaptedRange(CFixedVector3D pos, CFixedVector3D rot, entity_pos_t range, entity_pos_t elevationBonus, entity_pos_t angle)
    9971007    {
    9981008        entity_pos_t r = entity_pos_t::Zero() ;
  • source/simulation2/components/ICmpRangeManager.cpp

     
    3535
    3636BEGIN_INTERFACE_WRAPPER(RangeManager)
    3737DEFINE_INTERFACE_METHOD_5("ExecuteQuery", std::vector<entity_id_t>, ICmpRangeManager, ExecuteQuery, entity_id_t, entity_pos_t, entity_pos_t, std::vector<int>, int)
     38DEFINE_INTERFACE_METHOD_5("ExecuteQueryAroundPos", std::vector<entity_id_t>, ICmpRangeManager, ExecuteQueryAroundPos, CFixedVector2D, entity_pos_t, entity_pos_t, std::vector<int>, int)
    3839DEFINE_INTERFACE_METHOD_6("CreateActiveQuery", ICmpRangeManager::tag_t, ICmpRangeManager, CreateActiveQuery, entity_id_t, entity_pos_t, entity_pos_t, std::vector<int>, int, u8)
    3940DEFINE_INTERFACE_METHOD_7("CreateActiveParabolicQuery", ICmpRangeManager::tag_t, ICmpRangeManager, CreateActiveParabolicQuery, entity_id_t, entity_pos_t, entity_pos_t, entity_pos_t, std::vector<int>, int, u8)
    4041DEFINE_INTERFACE_METHOD_1("DestroyActiveQuery", void, ICmpRangeManager, DestroyActiveQuery, ICmpRangeManager::tag_t)
  • source/simulation2/components/ICmpRangeManager.h

     
    1919#define INCLUDED_ICMPRANGEMANAGER
    2020
    2121#include "maths/FixedVector3D.h"
     22#include "maths/FixedVector2D.h"
    2223
    2324#include "simulation2/system/Interface.h"
    2425#include "simulation2/helpers/Position.h"
     
    9293        entity_pos_t minRange, entity_pos_t maxRange, std::vector<int> owners, int requiredInterface) = 0;
    9394
    9495    /**
     96     * Execute a passive query.
     97     * @param pos the position around which the range will be computed.
     98     * @param minRange non-negative minimum distance in metres (inclusive).
     99     * @param maxRange non-negative maximum distance in metres (inclusive); or -1.0 to ignore distance.
     100     * @param owners list of player IDs that matching entities may have; -1 matches entities with no owner.
     101     * @param requiredInterface if non-zero, an interface ID that matching entities must implement.
     102     * @return list of entities matching the query, ordered by increasing distance from the source entity.
     103     */
     104    virtual std::vector<entity_id_t> ExecuteQueryAroundPos(CFixedVector2D pos,
     105        entity_pos_t minRange, entity_pos_t maxRange, std::vector<int> owners, int requiredInterface) = 0;
     106
     107    /**
    95108     * Construct an active query. The query will be disabled by default.
    96109     * @param source the entity around which the range will be computed.
    97110     * @param minRange non-negative minimum distance in metres (inclusive).