Ticket #958: territory_fow_building-09122011.patch

File territory_fow_building-09122011.patch, 7.5 KB (added by historic_bruno, 13 years ago)
  • binaries/data/mods/public/simulation/components/BuildRestrictions.js

     
    118118        return false;   // Fail
    119119    }
    120120   
     121    // Check whether it's in a visible region or explored but inside player's territory
     122    var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
     123    if (!cmpRangeManager)
     124    {
     125        return false;   // Fail
     126    }
     127    var losState = cmpRangeManager.GetLosState(this.entity, player);
     128    if (losState != "visible" && !(losState == "explored" && isOwn))
     129    {
     130        return false;   // Fail
     131    }
     132   
    121133    // Check special requirements
    122134    if (this.template.Category == "Dock")
    123135    {
  • binaries/data/mods/public/simulation/components/GuiInterface.js

     
    471471            pos.SetYRotation(cmd.angle);
    472472        }
    473473
    474         // Check whether it's in a visible region
    475         var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
    476         var visible = (cmpRangeManager && cmpRangeManager.GetLosVisibility(ent, player) == "visible");
    477         var validPlacement = false;
     474        // Check whether it's obstructed by other entities or invalid terrain
     475        var cmpBuildRestrictions = Engine.QueryInterface(ent, IID_BuildRestrictions);
     476        if (!cmpBuildRestrictions)
     477            error("cmpBuildRestrictions not defined");
    478478       
    479         if (visible)
    480         {   // Check whether it's obstructed by other entities or invalid terrain
    481             var cmpBuildRestrictions = Engine.QueryInterface(ent, IID_BuildRestrictions);
    482             if (!cmpBuildRestrictions)
    483                 error("cmpBuildRestrictions not defined");
    484            
    485             validPlacement = (cmpBuildRestrictions && cmpBuildRestrictions.CheckPlacement(player));
    486         }
    487 
    488         var ok = (visible && validPlacement);
     479        var validPlacement = (cmpBuildRestrictions && cmpBuildRestrictions.CheckPlacement(player));
    489480       
    490481        // Set it to a red shade if this is an invalid location
    491482        var cmpVisual = Engine.QueryInterface(ent, IID_Visual);
    492483        if (cmpVisual)
    493484        {
    494             if (!ok)
     485            if (!validPlacement)
    495486                cmpVisual.SetShadingColour(1.4, 0.4, 0.4, 1);
    496487            else
    497488                cmpVisual.SetShadingColour(1, 1, 1, 1);
    498489        }
    499490
    500         return ok;
     491        return validPlacement;
    501492    }
    502493
    503494    return false;
  • binaries/data/mods/public/simulation/helpers/Commands.js

     
    168168                Engine.DestroyEntity(ent);
    169169                break;
    170170            }
    171 
    172             // Check whether it's in a visible region
    173             var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
    174             var visible = (cmpRangeManager.GetLosVisibility(ent, player) == "visible");
    175             if (!visible)
    176             {
    177                 // TODO: report error to player (the building site was not visible)
    178                 print("Building site was not visible\n");
    179 
    180                 Engine.DestroyEntity(ent);
    181                 break;
    182             }
    183171        }
    184172       
    185173        var cmpCost = Engine.QueryInterface(ent, IID_Cost);
  • source/simulation2/components/CCmpRangeManager.cpp

     
    866866        return VIS_HIDDEN;
    867867    }
    868868
     869    virtual ELosState GetLosState(entity_id_t ent, player_id_t player)
     870    {
     871        // (We can't use m_EntityData since this needs to handle LOCAL entities too)
     872
     873        // Entities not with positions in the world are never visible
     874        CmpPtr<ICmpPosition> cmpPosition(GetSimContext(), ent);
     875        if (cmpPosition.null() || !cmpPosition->IsInWorld())
     876            return LOS_UNEXPLORED;
     877
     878        CFixedVector2D pos = cmpPosition->GetPosition2D();
     879
     880        int i = (pos.X / (int)CELL_SIZE).ToInt_RoundToNearest();
     881        int j = (pos.Y / (int)CELL_SIZE).ToInt_RoundToNearest();
     882
     883        // Reveal flag makes all positioned entities visible
     884        if (GetLosRevealAll(player))
     885        {
     886            if (LosIsOffWorld(i, j))
     887                return LOS_UNEXPLORED;
     888            else
     889                return LOS_VISIBLE;
     890        }
     891
     892        CLosQuerier los(player, m_LosState, m_TerrainVerticesPerSide);
     893
     894        if (los.IsVisible(i, j))
     895            return LOS_VISIBLE;
     896        else if (los.IsExplored(i, j))
     897            return LOS_EXPLORED;
     898
     899        // Otherwise unexplored
     900        return LOS_UNEXPLORED;
     901    }
     902
    869903    virtual void SetLosRevealAll(player_id_t player, bool enabled)
    870904    {
    871905        m_LosRevealAll[player] = enabled;
  • source/simulation2/components/ICmpRangeManager.cpp

     
    3333    }
    3434}
    3535
     36std::string ICmpRangeManager::GetLosState_wrapper(entity_id_t ent, int player)
     37{
     38    ELosState state = GetLosState(ent, player);
     39    switch (state)
     40    {
     41    case LOS_UNEXPLORED: return "unexplored";
     42    case LOS_EXPLORED: return "explored";
     43    case LOS_VISIBLE: return "visible";
     44    default: return "error"; // should never happen
     45    }
     46}
     47
    3648BEGIN_INTERFACE_WRAPPER(RangeManager)
    3749DEFINE_INTERFACE_METHOD_5("ExecuteQuery", std::vector<entity_id_t>, ICmpRangeManager, ExecuteQuery, entity_id_t, entity_pos_t, entity_pos_t, std::vector<int>, int)
    3850DEFINE_INTERFACE_METHOD_5("CreateActiveQuery", ICmpRangeManager::tag_t, ICmpRangeManager, CreateActiveQuery, entity_id_t, entity_pos_t, entity_pos_t, std::vector<int>, int)
     
    4456DEFINE_INTERFACE_METHOD_1("SetDebugOverlay", void, ICmpRangeManager, SetDebugOverlay, bool)
    4557DEFINE_INTERFACE_METHOD_2("SetLosRevealAll", void, ICmpRangeManager, SetLosRevealAll, player_id_t, bool)
    4658DEFINE_INTERFACE_METHOD_2("GetLosVisibility", std::string, ICmpRangeManager, GetLosVisibility_wrapper, entity_id_t, player_id_t)
     59DEFINE_INTERFACE_METHOD_2("GetLosState", std::string, ICmpRangeManager, GetLosState_wrapper, entity_id_t, player_id_t)
    4760DEFINE_INTERFACE_METHOD_1("SetLosCircular", void, ICmpRangeManager, SetLosCircular, bool)
    4861DEFINE_INTERFACE_METHOD_0("GetLosCircular", bool, ICmpRangeManager, GetLosCircular)
    4962DEFINE_INTERFACE_METHOD_1("GetPercentMapExplored", i32, ICmpRangeManager, GetPercentMapExplored, player_id_t)
  • source/simulation2/components/ICmpRangeManager.h

     
    270270    std::string GetLosVisibility_wrapper(entity_id_t ent, player_id_t player);
    271271
    272272    /**
     273     * Returns the LOS state of the given entity, with respect to the given player.
     274     * Returns LOS_UNEXPLORED if the entity doesn't exist or is not in the world.
     275     * This respects the GetLosRevealAll flag.
     276     * (Note this is subtly different than GetLosVisibility, which considers rendering concepts).
     277     */
     278    virtual ELosState GetLosState(entity_id_t ent, player_id_t player) = 0;
     279
     280    /**
     281     * GetLosState wrapped for script calls.
     282     * Returns "unexplored", "explored" or "visible".
     283     */
     284    std::string GetLosState_wrapper(entity_id_t ent, player_id_t player);
     285
     286    /**
    273287     * Set whether the whole map should be made visible to the given player.
    274288     * If player is -1, the map will be made visible to all players.
    275289     */