Ticket #2913: cpp_activation.2.patch

File cpp_activation.2.patch, 7.3 KB (added by Itms, 9 years ago)

Tweaks to the previous patch

  • binaries/data/mods/public/simulation/components/Visibility.js

     
    2121    this.alwaysVisible = this.template.AlwaysVisible == "true";
    2222    this.preview = this.template.Preview == "true";
    2323
    24     this.activated = false;
    25 
    2624    if (this.preview)
    27         this.activated = true;
     25        this.SetActivated(true);
    2826};
    2927
    3028/**
    31  * If this function returns true, the range manager will call the GetVisibility function
    32  * instead of computing the visibility.
     29 * Sets the range manager scriptedVisibility flag for this entity.
    3330 */
    34 Visibility.prototype.IsActivated = function()
     31Visibility.prototype.SetActivated = function(status)
    3532{
    36     return this.activated;
     33    let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
     34    cmpRangeManager.ActivateScriptedVisibility(this.entity, status);
    3735};
    3836
    3937/**
    40  * This function is called if IsActivated returns true.
     38 * This function is called if the range manager scriptedVisibility flag is set to true for this entity.
    4139 * If so, the return value supersedes the visibility computed by the range manager.
    4240 * isVisible: true if the entity is in the vision range of a unit, false otherwise
    4341 * isExplored: true if the entity is in explored territory, false otherwise
     
    4442 */
    4543Visibility.prototype.GetVisibility = function(player, isVisible, isExplored)
    4644{
    47     if (!this.activated)
    48         warn("The Visibility component was asked to provide a superseding visibility while not activated, this should not happen");
    49 
    5045    if (this.preview)
    5146    {
    5247        // For the owner only, mock the "RetainInFog" behaviour
  • source/simulation2/components/CCmpRangeManager.cpp

     
    174174 */
    175175struct EntityData
    176176{
    177     EntityData() : visibilities(0), retainInFog(0), owner(-1), inWorld(0), flags(1) { }
     177    EntityData() : visibilities(0), retainInFog(0), owner(-1), inWorld(0), flags(1), scriptedVisibility(0) { }
    178178    entity_pos_t x, z;
    179179    entity_pos_t visionRange;
    180180    u32 visibilities; // 2-bit visibility, per player
     
    182182    i8 owner;
    183183    u8 inWorld; // boolean
    184184    u8 flags; // See GetEntityFlagMask
     185    u8 scriptedVisibility; // boolean, see ComputeLosVisibility
    185186};
    186187
    187 cassert(sizeof(EntityData) == 20);
     188cassert(sizeof(EntityData) == 24);
    188189
    189190/**
    190191 * Serialization helper template for Query
     
    14151416            return CLosQuerier(GetSharedLosMask(player), m_LosState, m_TerrainVerticesPerSide);
    14161417    }
    14171418
     1419    virtual void ActivateScriptedVisibility(entity_id_t ent, bool status)
     1420    {
     1421        EntityMap<EntityData>::iterator it = m_EntityData.find(ent);
     1422        if (it != m_EntityData.end())
     1423            it->second.scriptedVisibility = status ? 1 : 0;
     1424    }
     1425
    14181426    ELosVisibility ComputeLosVisibility(CEntityHandle ent, player_id_t player)
    14191427    {
    14201428        // Entities not with positions in the world are never visible
     
    14451453        // Get visible regions
    14461454        CLosQuerier los(GetSharedLosMask(player), m_LosState, m_TerrainVerticesPerSide);
    14471455
    1448         // Ask the Visibility component about special situations
    14491456        CmpPtr<ICmpVisibility> cmpVisibility(ent);
    1450         if (cmpVisibility && cmpVisibility->IsActivated())
    1451             return cmpVisibility->GetVisibility(player, los.IsVisible(i, j), los.IsExplored(i, j));
    14521457
     1458        // Possibly ask the scripted Visibility component
     1459        EntityMap<EntityData>::iterator it = m_EntityData.find(ent.GetId());
     1460        if (it != m_EntityData.end())
     1461        {
     1462            if (it->second.scriptedVisibility == 1 && cmpVisibility)
     1463                return cmpVisibility->GetVisibility(player, los.IsVisible(i, j), los.IsExplored(i, j));
     1464        }
     1465
    14531466        // Else, default behavior
    14541467
    14551468        if (los.IsVisible(i, j))
     
    14641477            return VIS_HIDDEN;
    14651478
    14661479        // Invisible if the 'retain in fog' flag is not set, and in a non-visible explored region
    1467         if (!(cmpVisibility && cmpVisibility->GetRetainInFog()))
    1468             return VIS_HIDDEN;
     1480        // Try using the 'retainInFog' flag in m_EntityData to save a script call
     1481        if (it != m_EntityData.end())
     1482        {
     1483            if (it->second.retainInFog != 1)
     1484                return VIS_HIDDEN;
     1485        }
     1486        else
     1487        {
     1488            if (!(cmpVisibility && cmpVisibility->GetRetainInFog()))
     1489                return VIS_HIDDEN;
     1490        }
    14691491
    14701492        if (cmpMirage)
    14711493            return VIS_FOGGED;
  • source/simulation2/components/ICmpRangeManager.cpp

     
    5151DEFINE_INTERFACE_METHOD_2("SetLosRevealAll", void, ICmpRangeManager, SetLosRevealAll, player_id_t, bool)
    5252DEFINE_INTERFACE_METHOD_1("GetLosRevealAll", bool, ICmpRangeManager, GetLosRevealAll, player_id_t)
    5353DEFINE_INTERFACE_METHOD_5("GetElevationAdaptedRange", entity_pos_t, ICmpRangeManager, GetElevationAdaptedRange, CFixedVector3D, CFixedVector3D, entity_pos_t, entity_pos_t, entity_pos_t)
     54DEFINE_INTERFACE_METHOD_2("ActivateScriptedVisibility", void, ICmpRangeManager, ActivateScriptedVisibility, entity_id_t, bool)
    5455DEFINE_INTERFACE_METHOD_2("GetLosVisibility", std::string, ICmpRangeManager, GetLosVisibility_wrapper, entity_id_t, player_id_t)
    5556DEFINE_INTERFACE_METHOD_1("RequestVisibilityUpdate", void, ICmpRangeManager, RequestVisibilityUpdate, entity_id_t)
    5657DEFINE_INTERFACE_METHOD_1("SetLosCircular", void, ICmpRangeManager, SetLosCircular, bool)
  • source/simulation2/components/ICmpRangeManager.h

     
    314314     *  (or other players it shares LOS with).
    315315     */
    316316    virtual CLosQuerier GetLosQuerier(player_id_t player) = 0;
     317   
     318    /**
     319     * Toggle the scripted Visibility component activation for entity ent.
     320     */
     321    virtual void ActivateScriptedVisibility(entity_id_t ent, bool status) = 0;
    317322
    318323    /**
    319324     * Returns the visibility status of the given entity, with respect to the given player.
  • source/simulation2/components/ICmpVisibility.cpp

     
    3030public:
    3131    DEFAULT_SCRIPT_WRAPPER(VisibilityScripted)
    3232
    33     virtual bool IsActivated()
    34     {
    35         return m_Script.Call<bool>("IsActivated");
    36     }
    37 
    3833    virtual ICmpRangeManager::ELosVisibility GetVisibility(player_id_t player, bool isVisible, bool isExplored)
    3934    {
    4035        int visibility = m_Script.Call<int, player_id_t, bool, bool>("GetVisibility", player, isVisible, isExplored);
  • source/simulation2/components/ICmpVisibility.h

     
    3535class ICmpVisibility : public IComponent
    3636{
    3737public:
    38     virtual bool IsActivated() = 0;
    39 
    4038    virtual ICmpRangeManager::ELosVisibility GetVisibility(player_id_t player, bool isVisible, bool isExplored) = 0;
    4139
    4240    virtual bool GetRetainInFog() = 0;