Ticket #2913: conditional-fogging.patch

File conditional-fogging.patch, 14.8 KB (added by Itms, 9 years ago)
  • binaries/data/mods/public/gui/session/selection_details.js

     
    164164        Engine.GetGUIObjectByName("resourceCarryingIcon").tooltip = sprintf(translate("Gain: %(amount)s"), { amount: getTradingTooltip(entState.trader.goods.amount) });
    165165    }
    166166    // And for number of workers
    167     else if (entState.foundation && !entState.mirage)
     167    else if (entState.foundation && entState.visibility == "visible")
    168168    {
    169169        Engine.GetGUIObjectByName("resourceCarryingIcon").hidden = false;
    170170        Engine.GetGUIObjectByName("resourceCarryingText").hidden = false;
     
    172172        Engine.GetGUIObjectByName("resourceCarryingText").caption = entState.foundation.numBuilders + "    ";
    173173        Engine.GetGUIObjectByName("resourceCarryingIcon").tooltip = sprintf(translate("Number of builders.\nTasking another to this foundation would speed construction up by %(numb)s%%"), { numb : Math.round((Math.pow((entState.foundation.numBuilders+1)/entState.foundation.numBuilders, 0.7) - 1.0)*100) });
    174174    }
    175     else if (entState.resourceSupply && (!entState.resourceSupply.killBeforeGather || !entState.hitpoints) && !entState.mirage)
     175    else if (entState.resourceSupply && (!entState.resourceSupply.killBeforeGather || !entState.hitpoints) && entState.visibility == "visible")
    176176    {
    177177        Engine.GetGUIObjectByName("resourceCarryingIcon").hidden = false;
    178178        Engine.GetGUIObjectByName("resourceCarryingText").hidden = false;
  • binaries/data/mods/public/simulation/components/Fogging.js

     
    55function Fogging() {}
    66
    77Fogging.prototype.Schema =
    8     "<a:help>Allows this entity to be replaced by mirages entities in the fog-of-war.</a:help>" +
     8    "<a:help>Allows this entity to be replaced by mirage entities in the fog-of-war.</a:help>" +
    99    "<empty/>";
    1010
    1111Fogging.prototype.Init = function()
    1212{
     13    this.activated = false;
    1314    this.mirages = [];
    1415    this.miraged = [];
    1516    this.seen = [];
    1617
    17     var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
    18     for (var player = 0; player < cmpPlayerManager.GetNumPlayers(); ++player)
     18    let cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
     19    for (let player = 0; player < cmpPlayerManager.GetNumPlayers(); ++player)
    1920    {
    2021        this.mirages.push(INVALID_ENTITY);
    2122        this.miraged.push(false);
     
    2324    }
    2425};
    2526
     27Fogging.prototype.Activate = function()
     28{
     29    let mustUpdate = !this.activated;
     30    this.activated = true;
     31
     32    if (mustUpdate)
     33    {
     34        // Load a mirage for each player who has already seen the entity
     35        let cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
     36        for (let player = 0; player < cmpPlayerManager.GetNumPlayers(); ++player)
     37        {
     38            if (this.seen[player])
     39                this.LoadMirage(player);
     40        }
     41    }
     42};
     43
     44Fogging.prototype.IsActivated = function()
     45{
     46    return this.activated;
     47}
     48
    2649Fogging.prototype.LoadMirage = function(player)
    2750{
     51    if (!this.activated)
     52    {
     53        error("LoadMirage called for an entity with fogging deactivated");
     54        return;
     55    }
     56
    2857    this.miraged[player] = true;
    2958
    3059    if (this.mirages[player] == INVALID_ENTITY)
     
    108137    // Notify the GUI the entity has been replaced by a mirage, in case it is selected at this moment
    109138    var cmpGuiInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
    110139    cmpGuiInterface.AddMiragedEntity(player, this.entity, this.mirages[player]);
     140
     141    // Notify the range manager the visibility of this entity must be updated
     142    let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
     143    cmpRangeManager.RequestVisibilityUpdate(this.entity);
    111144};
    112145
    113146Fogging.prototype.ForceMiraging = function(player)
    114147{
     148    if (!this.activated)
     149        return;
     150
    115151    this.seen[player] = true;
    116152    this.LoadMirage(player);
    117153};
     
    140176    return this.seen[player];
    141177};
    142178
    143 Fogging.prototype.OnVisibilityChanged = function(msg)
    144 {
    145     if (msg.player >= this.mirages.length)
    146         return;
    147 
    148     if (msg.newVisibility == VIS_VISIBLE)
    149     {
    150         this.miraged[msg.player] = false;
    151         this.seen[msg.player] = true;
    152     }
    153 
    154     if (msg.newVisibility == VIS_FOGGED)
    155         this.LoadMirage(msg.player);
    156 };
    157 
    158179Fogging.prototype.OnDestroy = function(msg)
    159180{
    160181    for (var player = 0; player < this.mirages.length; ++player)
     
    172193    }
    173194};
    174195
     196Fogging.prototype.OnOwnershipChanged = function(msg)
     197{
     198    // Always activate fogging for non-Gaia entities
     199    if (msg.to > 0)
     200        this.Activate();
     201};
     202
     203Fogging.prototype.OnVisibilityChanged = function(msg)
     204{
     205    if (msg.player >= this.mirages.length)
     206        return;
     207
     208    if (msg.newVisibility == VIS_VISIBLE)
     209    {
     210        this.miraged[msg.player] = false;
     211        this.seen[msg.player] = true;
     212    }
     213
     214    if (msg.newVisibility == VIS_FOGGED && this.activated)
     215        this.LoadMirage(msg.player);
     216};
     217
    175218Engine.RegisterComponentType(IID_Fogging, "Fogging", Fogging);
  • binaries/data/mods/public/simulation/components/Health.js

     
    7070    if (this.hitpoints == 0)
    7171        return;
    7272
     73    // Before changing the value, activate Fogging if necessary to hide changes
     74    let cmpFogging = Engine.QueryInterface(this.entity, IID_Fogging);
     75    if (cmpFogging)
     76        cmpFogging.Activate();
     77
    7378    var old = this.hitpoints;
    7479    this.hitpoints = Math.max(1, Math.min(this.GetMaxHitpoints(), value));
    7580   
     
    150155 */
    151156Health.prototype.Reduce = function(amount)
    152157{
     158    // Before changing the value, activate Fogging if necessary to hide changes
     159    let cmpFogging = Engine.QueryInterface(this.entity, IID_Fogging);
     160    if (cmpFogging)
     161        cmpFogging.Activate();
     162
    153163    var state = { "killed": false };
    154164    if (amount >= 0 && this.hitpoints == this.GetMaxHitpoints())
    155165    {
     
    208218
    209219Health.prototype.Increase = function(amount)
    210220{
     221    // Before changing the value, activate Fogging if necessary to hide changes
     222    let cmpFogging = Engine.QueryInterface(this.entity, IID_Fogging);
     223    if (cmpFogging)
     224        cmpFogging.Activate();
     225
    211226    if (this.hitpoints == this.GetMaxHitpoints())
    212227        return {"old": this.hitpoints, "new":this.hitpoints};
    213228
  • binaries/data/mods/public/simulation/components/ResourceSupply.js

     
    9797
    9898ResourceSupply.prototype.TakeResources = function(rate)
    9999{
     100    // Before changing the amount, activate Fogging if necessary to hide changes
     101    let cmpFogging = Engine.QueryInterface(this.entity, IID_Fogging);
     102    if (cmpFogging)
     103        cmpFogging.Activate();
     104
    100105    if (this.infinite)
    101106        return { "amount": rate, "exhausted": false };
    102107
  • binaries/data/mods/public/simulation/components/StatusBars.js

     
    101101        yoffset -= height * 1.2;
    102102    };
    103103
     104    var cmpMirage = Engine.QueryInterface(this.entity, IID_Mirage);
     105
    104106    var cmpPack = Engine.QueryInterface(this.entity, IID_Pack);
    105107    if (cmpPack && cmpPack.IsPacking())
    106     {
    107108        AddBar("pack", cmpPack.GetProgress());
    108     }
    109109
    110110    var cmpHealth = Engine.QueryInterface(this.entity, IID_Health);
    111111    if (cmpHealth && cmpHealth.GetHitpoints() > 0)
    112     {
    113112        AddBar("health", cmpHealth.GetHitpoints() / cmpHealth.GetMaxHitpoints());
    114     }
     113    else if (cmpMirage && cmpMirage.Health())
     114        AddBar("health", cmpMirage.GetHitpoints() / cmpMirage.GetMaxHitpoints());
    115115
    116116    var cmpResourceSupply = Engine.QueryInterface(this.entity, IID_ResourceSupply);
    117117    if (cmpResourceSupply)
    118     {
    119118        AddBar("supply", cmpResourceSupply.IsInfinite() ? 1 : cmpResourceSupply.GetCurrentAmount() / cmpResourceSupply.GetMaxAmount());
    120     }
     119    else if (cmpMirage && cmpMirage.ResourceSupply())
     120        AddBar("supply", cmpMirage.IsInfinite() ? 1 : cmpMirage.GetAmount() / cmpMirage.GetMaxAmount());
    121121
    122122    /*
    123123    // Rank icon disabled for now - see discussion around
  • source/ps/TemplateLoader.cpp

     
    433433    permittedComponentTypes.insert("Footprint");
    434434    permittedComponentTypes.insert("Minimap");
    435435    permittedComponentTypes.insert("Ownership");
     436    permittedComponentTypes.insert("OverlayRenderer");
    436437    permittedComponentTypes.insert("Position");
    437438    permittedComponentTypes.insert("Selectable");
     439    permittedComponentTypes.insert("StatusBars");
     440    permittedComponentTypes.insert("Visibility");
    438441    permittedComponentTypes.insert("VisualActor");
    439442
    440443    CParamNode::LoadXMLString(out, "<Entity/>");
     
    456459
    457460    // Set the entity as mirage entity
    458461    CParamNode::LoadXMLString(out, "<Entity><Mirage/></Entity>");
    459     CParamNode::LoadXMLString(out, "<Entity><Visibility><RetainInFog>true</RetainInFog><AlwaysVisible>false</AlwaysVisible><Preview>false</Preview></Visibility></Entity>");
    460462}
    461463
    462464void CTemplateLoader::CopyFoundationSubset(CParamNode& out, const CParamNode& in)
  • source/simulation2/components/CCmpRangeManager.cpp

     
    14541454            return VIS_HIDDEN;
    14551455        }
    14561456
    1457         // Fogged entities must not disappear while the mirage is not ready
     1457        // Fogged entities are hidden in two cases:
     1458        // - They were not scouted
     1459        // - A mirage replaces them
    14581460        CmpPtr<ICmpFogging> cmpFogging(ent);
    1459         if (cmpFogging && cmpFogging->WasSeen(player) && !cmpFogging->IsMiraged(player))
    1460             return VIS_FOGGED;
     1461        if (cmpFogging && cmpFogging->IsActivated() &&
     1462            (!cmpFogging->WasSeen(player) || cmpFogging->IsMiraged(player)))
     1463            return VIS_HIDDEN;
    14611464
    1462         return VIS_HIDDEN;
     1465        return VIS_FOGGED;
    14631466    }
    14641467
    14651468    ELosVisibility ComputeLosVisibility(entity_id_t ent, player_id_t player)
     
    15571560
    15581561        for (std::vector<entity_id_t>::iterator it = m_ModifiedEntities.begin(); it != m_ModifiedEntities.end(); ++it)
    15591562        {
    1560             UpdateVisibility(*it);
     1563            // Don't bother updating if we already did it in a global update
     1564            if (!m_GlobalVisibilityUpdate)
     1565                UpdateVisibility(*it);
    15611566        }
    15621567        m_ModifiedEntities.clear();
    15631568
     
    15641569        m_GlobalVisibilityUpdate = false;
    15651570    }
    15661571
     1572    virtual void RequestVisibilityUpdate(entity_id_t ent)
     1573    {
     1574        if (std::find(m_ModifiedEntities.begin(), m_ModifiedEntities.end(), ent) == m_ModifiedEntities.end())
     1575            m_ModifiedEntities.push_back(ent);
     1576    }
     1577
    15671578    void UpdateVisibility(entity_id_t ent)
    15681579    {
    15691580        // Warning: Code related to fogging (like posting VisibilityChanged messages)
     
    15941605        {
    15951606            if (oldVisibilities[player-1] == newVisibilities[player-1])
    15961607                continue;
    1597            
    1598             // Another visibility update can be necessary to take in account new mirages
    1599             if (std::find(m_ModifiedEntities.begin(), m_ModifiedEntities.end(), ent) == m_ModifiedEntities.end())
    1600                 m_ModifiedEntities.push_back(ent);
    16011608
    16021609            CMessageVisibilityChanged msg(player, ent, oldVisibilities[player-1], newVisibilities[player-1]);
    16031610            GetSimContext().GetComponentManager().PostMessage(ent, msg);
  • source/simulation2/components/ICmpFogging.cpp

     
    1 /* Copyright (C) 2014 Wildfire Games.
     1/* Copyright (C) 2015 Wildfire Games.
    22 * This file is part of 0 A.D.
    33 *
    44 * 0 A.D. is free software: you can redistribute it and/or modify
     
    3030public:
    3131    DEFAULT_SCRIPT_WRAPPER(FoggingScripted)
    3232
     33    virtual bool IsActivated()
     34    {
     35        return m_Script.Call<bool>("IsActivated");
     36    }
     37
    3338    virtual bool WasSeen(player_id_t player)
    3439    {
    3540        return m_Script.Call<bool>("WasSeen", player);
  • source/simulation2/components/ICmpFogging.h

     
    1 /* Copyright (C) 2014 Wildfire Games.
     1/* Copyright (C) 2015 Wildfire Games.
    22 * This file is part of 0 A.D.
    33 *
    44 * 0 A.D. is free software: you can redistribute it and/or modify
     
    3030class ICmpFogging : public IComponent
    3131{
    3232public:
     33    virtual bool IsActivated() = 0;
    3334    virtual bool WasSeen(player_id_t player) = 0;
    3435    virtual bool IsMiraged(player_id_t player) = 0;
    3536    virtual void ForceMiraging(player_id_t player) = 0;
  • source/simulation2/components/ICmpRangeManager.cpp

     
    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)
    5454DEFINE_INTERFACE_METHOD_2("GetLosVisibility", std::string, ICmpRangeManager, GetLosVisibility_wrapper, entity_id_t, player_id_t)
     55DEFINE_INTERFACE_METHOD_1("RequestVisibilityUpdate", void, ICmpRangeManager, RequestVisibilityUpdate, entity_id_t)
    5556DEFINE_INTERFACE_METHOD_1("SetLosCircular", void, ICmpRangeManager, SetLosCircular, bool)
    5657DEFINE_INTERFACE_METHOD_0("GetLosCircular", bool, ICmpRangeManager, GetLosCircular)
    5758DEFINE_INTERFACE_METHOD_2("SetSharedLos", void, ICmpRangeManager, SetSharedLos, player_id_t, std::vector<player_id_t>)
  • source/simulation2/components/ICmpRangeManager.h

     
    323323    virtual ELosVisibility GetLosVisibility(CEntityHandle ent, player_id_t player) = 0;
    324324    virtual ELosVisibility GetLosVisibility(entity_id_t ent, player_id_t player) = 0;
    325325
     326    /**
     327     * Request the update of the visibility cache of ent at next turn.
     328     * Typically used for fogging.
     329     */
     330    virtual void RequestVisibilityUpdate(entity_id_t ent) = 0;
    326331
     332
    327333    /**
    328334     * GetLosVisibility wrapped for script calls.
    329335     * Returns "hidden", "fogged" or "visible".