18 #include "precompiled.h"
80 return "<a:component type='system'/><empty/>";
128 int impassableCost = externalParamNode.
GetChild(
"TerritoryManager").
GetChild(
"ImpassableCost").
ToInt();
129 ENSURE(0 <= impassableCost && impassableCost <= 255);
171 if (msgData.
component == L
"TerritoryInfluence")
214 if (cmpTerritoryInfluence)
265 void Interpolate(
float frameTime,
float frameOffset);
283 Grid<u32>& grid, OpenQueue& queue, const
Grid<
u8>& costGrid)
285 u32 dg = falloff * costGrid.get(i, j);
287 dg = (dg * 362) / 256;
291 if (pg <= grid.get(i, j) + dg)
306 while (!openTiles.
empty())
311 u16 x = tile.
id.first;
312 u16 z = tile.
id.second;
323 if (x > 0 && z < tilesH-1)
325 if (x < tilesW-1 && z > 0)
327 if (x < tilesW-1 && z < tilesH-1)
337 PROFILE(
"CalculateTerritories");
352 Grid<u8> influenceGrid(tilesW, tilesH);
359 for (
u16 j = 0; j < tilesH; ++j)
361 for (
u16 i = 0; i < tilesW; ++i)
363 u16 g = passGrid.
get(i, j);
365 if (g & passClassUnrestricted)
367 else if (g & passClassDefault)
371 influenceGrid.
set(i, j, cost);
382 std::map<player_id_t, std::vector<entity_id_t> > influenceEntities;
383 std::vector<entity_id_t> rootInfluenceEntities;
384 for (CComponentManager::InterfaceList::iterator it = influences.begin(); it != influences.end(); ++it)
388 if (cmpTerritoryInfluence->
GetWeight() == 0 || cmpTerritoryInfluence->
GetRadius() == 0)
406 if (!cmpPosition || !cmpPosition->
IsInWorld())
409 influenceEntities[owner].push_back(it->first);
411 if (cmpTerritoryInfluence->
IsRoot())
412 rootInfluenceEntities.push_back(it->first);
416 std::vector<std::pair<player_id_t, Grid<u32> > > playerGrids;
420 for (std::map<
player_id_t, std::vector<entity_id_t> >::iterator it = influenceEntities.begin(); it != influenceEntities.end(); ++it)
424 std::vector<entity_id_t>& ents = it->second;
425 for (std::vector<entity_id_t>::iterator eit = ents.begin(); eit != ents.end(); ++eit)
439 u32 falloff = weight / radius;
445 entityGrid.
set(i, j, weight);
448 openTiles.
push(tile);
451 FloodFill(entityGrid, influenceGrid, openTiles, falloff);
454 for (
u16 j = 0; j < entityGrid.
m_H; ++j)
455 for (
u16 i = 0; i < entityGrid.
m_W; ++i)
456 playerGrid.
set(i, j, playerGrid.
get(i, j) + entityGrid.
get(i, j));
459 playerGrids.push_back(std::make_pair(it->first, playerGrid));
463 for (
u16 j = 0; j < tilesH; ++j)
465 for (
u16 i = 0; i < tilesW; ++i)
468 for (
size_t k = 0; k < playerGrids.size(); ++k)
470 u32 w = playerGrids[k].second.get(i, j);
483 for (std::vector<entity_id_t>::iterator it = rootInfluenceEntities.begin(); it != rootInfluenceEntities.end(); ++it)
506 std::vector<std::pair<u16, u16> > tileStack;
508 #define MARK_AND_PUSH(i, j) STMT(grid.set(i, j, owner | TERRITORY_CONNECTED_MASK); tileStack.push_back(std::make_pair(i, j)); )
511 while (!tileStack.empty())
513 int ti = tileStack.back().first;
514 int tj = tileStack.back().second;
515 tileStack.pop_back();
517 if (ti > 0 && grid.
get(ti-1, tj) == owner)
519 if (ti < maxi && grid.
get(ti+1, tj) == owner)
521 if (tj > 0 && grid.
get(ti, tj-1) == owner)
523 if (tj < maxj && grid.
get(ti, tj+1) == owner)
526 if (ti > 0 && tj > 0 && grid.
get(ti-1, tj-1) == owner)
528 if (ti > 0 && tj < maxj && grid.
get(ti-1, tj+1) == owner)
530 if (ti < maxi && tj > 0 && grid.
get(ti+1, tj-1) == owner)
532 if (ti < maxi && tj < maxj && grid.
get(ti+1, tj+1) == owner)
563 for (CComponentManager::InterfaceList::iterator it = infls.begin(); it != infls.end(); ++it)
585 for (
u16 j = j0; j <= j1; ++j)
587 for (
u16 i = i0; i <= i1; ++i)
592 grid.
set(i, j, (
u8)cost);
611 PROFILE(
"update boundary lines");
622 texturePropsBase.
SetWrap(GL_CLAMP_TO_BORDER, GL_CLAMP_TO_EDGE);
627 texturePropsMask.
SetWrap(GL_CLAMP_TO_BORDER, GL_CLAMP_TO_EDGE);
632 if (!cmpPlayerManager)
635 for (
size_t i = 0; i < boundaries.size(); ++i)
637 if (boundaries[i].points.empty())
643 color = cmpPlayer->GetColour();
659 for (
size_t j = 0; j < boundaries[i].points.size(); ++j)
661 points.push_back(boundaries[i].points[j].
X);
662 points.push_back(boundaries[i].points[j].
Y);
666 const size_t numHighlightNodes = 7;
668 if (j > boundaries[i].points.size() - 1 - numHighlightNodes)
670 else if (j < numHighlightNodes)
738 :
TerrainOverlay(manager.GetSimContext()), m_TerritoryManager(manager)
virtual void HandleMessage(const CMessage &msg, bool global)
An entity initialisation parameter node.
virtual pass_class_t GetPassabilityClass(const std::string &name)=0
Get the tag for a given passability class name.
virtual player_id_t GetOwner(entity_pos_t x, entity_pos_t z)
Get owner of territory at given position.
void SubscribeToMessageType(MessageTypeId mtid)
Subscribe the current component type to the given message type.
virtual void ProcessTile(ssize_t i, ssize_t j)
Override to perform processing of each tile.
A simple fixed-point number class.
virtual const Grid< u16 > & GetPassabilityGrid()=0
#define REGISTER_COMPONENT_TYPE(cname)
std::vector< std::pair< entity_id_t, IComponent * > > InterfaceList
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
CCmpTerritoryManager & m_TerritoryManager
Line-based overlay, with world-space coordinates, rendered in the world potentially behind other obje...
virtual bool IsConnected(entity_pos_t x, entity_pos_t z)
Get whether territory at given position is connected to a root object (civ center etc) owned by that ...
const ssize_t TERRAIN_TILE_SIZE
metres [world space units] per tile in x and z
Serialization interface; see serialization overview.
Represents the filename and GL parameters of a texture, for passing to CTextureManager::CreateTexture...
Add renderable objects to the scene collector.
Priority queue implemented as a binary heap.
void InterpolatePointsRNS(std::vector< CVector2D > &points, bool closed, float offset, int segmentSamples=4)
Updates the given points to include intermediate points interpolating between the original control po...
virtual bool GetObstructionSquare(ICmpObstructionManager::ObstructionSquare &out)=0
Gets the square corresponding to this obstruction shape.
std::vector< SOverlayLine > m_DebugBoundaryLineNodes
int ToInt() const
Parses the content of this node as an integer.
virtual u32 GetRadius()=0
Base class for (relatively) simple drawing of data onto terrain tiles, intended for debugging purpose...
virtual bool IsInWorld()=0
Returns true if the entity currently exists at a defined position in the world.
void UpdateBoundaryLines()
float deltaSimTime
Elapsed simulation time since previous interpolate, in seconds.
static void ProcessNeighbour(u32 falloff, u16 i, u16 j, u32 pg, bool diagonal, Grid< u32 > &grid, OpenQueue &queue, const Grid< u8 > &costGrid)
int32_t player_id_t
valid player IDs are non-negative (see ICmpOwnership)
This interface accepts renderable objects.
void RenderTile(const CColor &colour, bool draw_hidden)
Draw a filled quad on top of the current tile.
bool m_EnableLineDebugOverlays
Enable node debugging overlays for boundary lines?
fixed ToFixed() const
Parses the content of this node as a fixed-point number.
static void LoadXML(CParamNode &ret, const XMBFile &file, const wchar_t *sourceIdentifier=NULL)
Loads the XML data specified by file into the node ret.
static void TileCenter(u16 i, u16 j, entity_pos_t &x, entity_pos_t &z)
Returns the position of the center of the given tile.
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
TerritoryOverlay * m_DebugOverlay
void ConstructCircleOnGround(const CSimContext &context, float x, float z, float radius, SOverlayLine &overlay, bool floating, float heightOffset=0.25f)
Constructs overlay line as a circle with given center and radius, conforming to terrain.
virtual int GetType() const =0
std::vector< SBoundaryLine > m_BoundaryLines
InterfaceList GetEntitiesWithInterface(InterfaceId iid) const
const CParamNode & GetChild(const char *name) const
Returns the (unique) child node with the given name, or a node with IsOk() == false if there is none...
Textured line overlay, with world-space coordinates, rendered in the world onto the terrain...
void Interpolate(float frameTime, float frameOffset)
static void ClassInit(CComponentManager &componentManager)
NONCOPYABLE(TerritoryOverlay)
virtual void Init(const CParamNode ¶mNode)
virtual void Serialize(ISerializer &serialize)
virtual entity_id_t GetPlayerByID(int32_t id)=0
float ToFloat() const
Convert to float. May be lossy - float can't represent all values.
static std::string GetSchema()
virtual CFixedVector2D GetPosition2D()=0
Returns the current x,z position (no interpolation).
static void FloodFill(Grid< u32 > &grid, Grid< u8 > &costGrid, OpenQueue &openTiles, u32 falloff)
#define SAFE_DELETE(p)
delete memory ensuing from new and set the pointer to zero (thus making double-frees safe / a no-op) ...
virtual player_id_t GetOwner()=0
float offset
Range [0, 1] (inclusive); fractional time of current frame between previous/next simulation turns...
virtual const Grid< u8 > & GetTerritoryGrid()
For each tile, the TERRITORY_PLAYER_MASK bits are player ID; TERRITORY_CONNECTED_MASK is set if the t...
virtual bool NeedUpdate(size_t *dirtyID)
void CalculateTerritories()
#define DEFAULT_COMPONENT_ALLOCATOR(cname)
virtual void Deserialize(const CParamNode ¶mNode, IDeserializer &deserialize)
const CSimContext & GetSimContext() const
static const int TERRITORY_CONNECTED_MASK
static bool IsInitialised()
void SubscribeGloballyToMessageType(MessageTypeId mtid)
Subscribe the current component type to all messages of the given message type.
SOverlayTexturedLine overlay
Grid< u8 > * m_Territories
void RasteriseInfluences(CComponentManager::InterfaceList &infls, Grid< u8 > &grid)
Updates grid based on the obstruction shapes of all entities with a TerritoryInfluence component...
A simplified syntax for accessing entity components.
std::vector< STerritoryBoundary > ComputeBoundaries()
static CFixed FromInt(int n)
CEntityHandle GetSystemEntity() const
Helper functions related to rendering.
#define MARK_AND_PUSH(i, j)
void SetMaxAnisotropy(float aniso)
Set maximum anisotropy value.
void MakeDirtyIfRelevantEntity(entity_id_t ent)
static const int TERRITORY_PLAYER_MASK
SceneCollector & collector
void RenderSubmit(SceneCollector &collector)
Sent by technology manager when a technology is researched that modifies a component.
void BroadcastMessage(const CMessage &msg) const
Send a message, not targeted at any particular entity.
T & get(int i, int j) const
virtual bool IsLoaded()=0
static void NearestTile(entity_pos_t x, entity_pos_t z, u16 &i, u16 &j, u16 w, u16 h)
Compute the tile indexes on the grid nearest to a given point.
void SmoothPointsAverage(std::vector< CVector2D > &points, bool closed)
Updates the given points so each point is averaged with its neighbours, resulting in a somewhat smoot...
static std::vector< STerritoryBoundary > ComputeBoundaries(const Grid< u8 > *territories)
Computes and returns all territory boundaries on the provided territory map (see STerritoryBoundary f...
virtual u16 GetTilesPerSide()=0
Returns number of tiles per side on the terrain.
Sent when territory assignments have changed.
Prepare for rendering a new frame (set up model positions etc).
bool PointIsInSquare(CFixedVector2D point, CFixedVector2D u, CFixedVector2D v, CFixedVector2D halfSize)
Checks if a point is inside the given rotated square or rectangle.
TerritoryOverlay(CCmpTerritoryManager &manager)
virtual u32 GetWeight()=0
bool m_BoundaryLinesDirty
void SetWrap(GLint wrap)
Set wrapping mode (typically GL_REPEAT, GL_CLAMP_TO_EDGE, etc).
u32 entity_id_t
Entity ID type.
virtual void Submit(CPatch *patch)=0
Submit a terrain patch that is part of the scene.
void push(const Item &item)
virtual void StartRender()
Override to perform processing at the start of the overlay rendering, before the ProcessTile calls...
T clamp(T value, T min, T max)
Standard representation for all types of shapes, for use with geometry processing code...
Helper functions related to geometry algorithms.
void set(int i, int j, const T &value)
CComponentManager & GetComponentManager() const
CFixedVector2D GetHalfBoundingBox(CFixedVector2D u, CFixedVector2D v, CFixedVector2D halfSize)
Deserialization interface; see serialization overview.
virtual i32 GetCost()=0
Returns either -1 to indicate no special terrain cost, or a value in [0, 255] to indicate overriding ...
shared_ptr< CTexture > CTexturePtr