Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/components/CCmpObstructionManager.cpp
Show All 14 Lines | |||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
#include "precompiled.h" | #include "precompiled.h" | ||||
#include "simulation2/system/Component.h" | #include "simulation2/system/Component.h" | ||||
#include "ICmpObstructionManager.h" | #include "ICmpObstructionManager.h" | ||||
#include "ICmpTerrain.h" | |||||
#include "ICmpPosition.h" | #include "ICmpPosition.h" | ||||
#include "simulation2/MessageTypes.h" | #include "simulation2/MessageTypes.h" | ||||
#include "simulation2/helpers/Geometry.h" | #include "simulation2/helpers/Geometry.h" | ||||
#include "simulation2/helpers/Grid.h" | #include "simulation2/helpers/Grid.h" | ||||
#include "simulation2/helpers/Rasterize.h" | #include "simulation2/helpers/Rasterize.h" | ||||
#include "simulation2/helpers/Render.h" | #include "simulation2/helpers/Render.h" | ||||
#include "simulation2/helpers/Spatial.h" | #include "simulation2/helpers/Spatial.h" | ||||
#include "simulation2/serialization/SerializedTypes.h" | #include "simulation2/serialization/SerializedTypes.h" | ||||
#include "graphics/Overlay.h" | #include "graphics/Overlay.h" | ||||
#include "graphics/Terrain.h" | |||||
#include "maths/MathUtil.h" | #include "maths/MathUtil.h" | ||||
#include "ps/Profile.h" | #include "ps/Profile.h" | ||||
#include "renderer/Scene.h" | #include "renderer/Scene.h" | ||||
#include "ps/CLogger.h" | #include "ps/CLogger.h" | ||||
// Externally, tags are opaque non-zero positive integers. | // Externally, tags are opaque non-zero positive integers. | ||||
// Internally, they are tagged (by shape) indexes into shape lists. | // Internally, they are tagged (by shape) indexes into shape lists. | ||||
// idx must be non-zero. | // idx must be non-zero. | ||||
▲ Show 20 Lines • Show All 98 Lines • ▼ Show 20 Lines | public: | ||||
entity_pos_t m_MaxClearance; | entity_pos_t m_MaxClearance; | ||||
bool m_PassabilityCircular; | bool m_PassabilityCircular; | ||||
entity_pos_t m_WorldX0; | entity_pos_t m_WorldX0; | ||||
entity_pos_t m_WorldZ0; | entity_pos_t m_WorldZ0; | ||||
entity_pos_t m_WorldX1; | entity_pos_t m_WorldX1; | ||||
entity_pos_t m_WorldZ1; | entity_pos_t m_WorldZ1; | ||||
u16 m_TerrainTiles; | |||||
static std::string GetSchema() | static std::string GetSchema() | ||||
{ | { | ||||
return "<a:component type='system'/><empty/>"; | return "<a:component type='system'/><empty/>"; | ||||
} | } | ||||
virtual void Init(const CParamNode& UNUSED(paramNode)) | virtual void Init(const CParamNode& UNUSED(paramNode)) | ||||
{ | { | ||||
m_DebugOverlayEnabled = false; | m_DebugOverlayEnabled = false; | ||||
m_DebugOverlayDirty = true; | m_DebugOverlayDirty = true; | ||||
m_UnitShapeNext = 1; | m_UnitShapeNext = 1; | ||||
m_StaticShapeNext = 1; | m_StaticShapeNext = 1; | ||||
m_UpdateInformations.dirty = true; | m_UpdateInformations.dirty = true; | ||||
m_UpdateInformations.globallyDirty = true; | m_UpdateInformations.globallyDirty = true; | ||||
m_PassabilityCircular = false; | m_PassabilityCircular = false; | ||||
m_WorldX0 = m_WorldZ0 = m_WorldX1 = m_WorldZ1 = entity_pos_t::Zero(); | m_WorldX0 = m_WorldZ0 = m_WorldX1 = m_WorldZ1 = entity_pos_t::Zero(); | ||||
m_TerrainTiles = 0; | |||||
// Initialise with bogus values (these will get replaced when | // Initialise with bogus values (these will get replaced when | ||||
// SetBounds is called) | // SetBounds is called) | ||||
ResetSubdivisions(entity_pos_t::FromInt(1024), entity_pos_t::FromInt(1024)); | ResetSubdivisions(entity_pos_t::FromInt(1024), entity_pos_t::FromInt(1024)); | ||||
} | } | ||||
virtual void Deinit() | virtual void Deinit() | ||||
{ | { | ||||
Show All 13 Lines | void SerializeCommon(S& serialize) | ||||
serialize.NumberU32_Unbounded("static shape next", m_StaticShapeNext); | serialize.NumberU32_Unbounded("static shape next", m_StaticShapeNext); | ||||
serialize.Bool("circular", m_PassabilityCircular); | serialize.Bool("circular", m_PassabilityCircular); | ||||
serialize.NumberFixed_Unbounded("world x0", m_WorldX0); | serialize.NumberFixed_Unbounded("world x0", m_WorldX0); | ||||
serialize.NumberFixed_Unbounded("world z0", m_WorldZ0); | serialize.NumberFixed_Unbounded("world z0", m_WorldZ0); | ||||
serialize.NumberFixed_Unbounded("world x1", m_WorldX1); | serialize.NumberFixed_Unbounded("world x1", m_WorldX1); | ||||
serialize.NumberFixed_Unbounded("world z1", m_WorldZ1); | serialize.NumberFixed_Unbounded("world z1", m_WorldZ1); | ||||
serialize.NumberU16_Unbounded("terrain tiles", m_TerrainTiles); | |||||
} | } | ||||
virtual void Serialize(ISerializer& serialize) | virtual void Serialize(ISerializer& serialize) | ||||
{ | { | ||||
// TODO: this could perhaps be optimised by not storing all the obstructions, | // TODO: this could perhaps be optimised by not storing all the obstructions, | ||||
// and instead regenerating them from the other entities on Deserialize | // and instead regenerating them from the other entities on Deserialize | ||||
SerializeCommon(serialize); | SerializeCommon(serialize); | ||||
} | } | ||||
virtual void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize) | virtual void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize) | ||||
{ | { | ||||
Init(paramNode); | Init(paramNode); | ||||
SerializeCommon(deserialize); | SerializeCommon(deserialize); | ||||
m_UpdateInformations.dirtinessGrid = Grid<u8>(m_TerrainTiles*Pathfinding::NAVCELLS_PER_TILE, m_TerrainTiles*Pathfinding::NAVCELLS_PER_TILE); | i32 size = ((m_WorldX1-m_WorldX0)/Pathfinding::NAVCELL_SIZE_INT).ToInt_RoundToInfinity(); | ||||
m_UpdateInformations.dirtinessGrid = Grid<u8>(size, size); | |||||
} | } | ||||
virtual void HandleMessage(const CMessage& msg, bool UNUSED(global)) | virtual void HandleMessage(const CMessage& msg, bool UNUSED(global)) | ||||
{ | { | ||||
switch (msg.GetType()) | switch (msg.GetType()) | ||||
{ | { | ||||
case MT_RenderSubmit: | case MT_RenderSubmit: | ||||
{ | { | ||||
Show All 13 Lines | virtual void SetBounds(entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1) | ||||
m_WorldX1 = x1; | m_WorldX1 = x1; | ||||
m_WorldZ1 = z1; | m_WorldZ1 = z1; | ||||
MakeDirtyAll(); | MakeDirtyAll(); | ||||
// Subdivision system bounds: | // Subdivision system bounds: | ||||
ENSURE(x0.IsZero() && z0.IsZero()); // don't bother implementing non-zero offsets yet | ENSURE(x0.IsZero() && z0.IsZero()); // don't bother implementing non-zero offsets yet | ||||
ResetSubdivisions(x1, z1); | ResetSubdivisions(x1, z1); | ||||
CmpPtr<ICmpTerrain> cmpTerrain(GetSystemEntity()); | i32 size = ((m_WorldX1-m_WorldX0)/Pathfinding::NAVCELL_SIZE_INT).ToInt_RoundToInfinity(); | ||||
if (!cmpTerrain) | m_UpdateInformations.dirtinessGrid = Grid<u8>(size, size); | ||||
return; | |||||
m_TerrainTiles = cmpTerrain->GetTilesPerSide(); | |||||
m_UpdateInformations.dirtinessGrid = Grid<u8>(m_TerrainTiles*Pathfinding::NAVCELLS_PER_TILE, m_TerrainTiles*Pathfinding::NAVCELLS_PER_TILE); | |||||
CmpPtr<ICmpPathfinder> cmpPathfinder(GetSystemEntity()); | CmpPtr<ICmpPathfinder> cmpPathfinder(GetSystemEntity()); | ||||
if (cmpPathfinder) | if (cmpPathfinder) | ||||
m_MaxClearance = cmpPathfinder->GetMaximumClearance(); | m_MaxClearance = cmpPathfinder->GetMaximumClearance(); | ||||
} | } | ||||
void ResetSubdivisions(entity_pos_t x1, entity_pos_t z1) | void ResetSubdivisions(entity_pos_t x1, entity_pos_t z1) | ||||
{ | { | ||||
// Use 8x8 tile subdivisions | // Use 8x8 tile subdivisions | ||||
// (TODO: find the optimal number instead of blindly guessing) | // (TODO: find the optimal number instead of blindly guessing) | ||||
m_UnitSubdivision.Reset(x1, z1, entity_pos_t::FromInt(8*TERRAIN_TILE_SIZE)); | m_UnitSubdivision.Reset(x1, z1, entity_pos_t::FromInt(32)); | ||||
m_StaticSubdivision.Reset(x1, z1, entity_pos_t::FromInt(8*TERRAIN_TILE_SIZE)); | m_StaticSubdivision.Reset(x1, z1, entity_pos_t::FromInt(32)); | ||||
Stan: Can we have a constant for that ? Wonder if the compiler is smart enough to not do from int… | |||||
for (std::map<u32, UnitShape>::iterator it = m_UnitShapes.begin(); it != m_UnitShapes.end(); ++it) | for (std::map<u32, UnitShape>::iterator it = m_UnitShapes.begin(); it != m_UnitShapes.end(); ++it) | ||||
{ | { | ||||
CFixedVector2D center(it->second.x, it->second.z); | CFixedVector2D center(it->second.x, it->second.z); | ||||
CFixedVector2D halfSize(it->second.clearance, it->second.clearance); | CFixedVector2D halfSize(it->second.clearance, it->second.clearance); | ||||
m_UnitSubdivision.Add(it->first, center - halfSize, center + halfSize); | m_UnitSubdivision.Add(it->first, center - halfSize, center + halfSize); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 278 Lines • ▼ Show 20 Lines | private: | ||||
inline void MarkDirtinessGrid(const entity_pos_t& x, const entity_pos_t& z, const entity_pos_t& r) | inline void MarkDirtinessGrid(const entity_pos_t& x, const entity_pos_t& z, const entity_pos_t& r) | ||||
{ | { | ||||
MarkDirtinessGrid(x, z, CFixedVector2D(r, r)); | MarkDirtinessGrid(x, z, CFixedVector2D(r, r)); | ||||
} | } | ||||
inline void MarkDirtinessGrid(const entity_pos_t& x, const entity_pos_t& z, const CFixedVector2D& hbox) | inline void MarkDirtinessGrid(const entity_pos_t& x, const entity_pos_t& z, const CFixedVector2D& hbox) | ||||
{ | { | ||||
ENSURE(m_UpdateInformations.dirtinessGrid.m_W == m_TerrainTiles*Pathfinding::NAVCELLS_PER_TILE && | if (m_UpdateInformations.dirtinessGrid.m_W == 0) | ||||
Not Done Inline ActionsWhat was wrong with the ensure? Stan: What was wrong with the ensure? | |||||
Done Inline ActionsMostly I've removed m_TerrainTiles so I can't have it anymore, and also I think it as largely useless (the check happens elsewhere). wraitii: Mostly I've removed `m_TerrainTiles` so I can't have it anymore, and also I think it as largely… | |||||
m_UpdateInformations.dirtinessGrid.m_H == m_TerrainTiles*Pathfinding::NAVCELLS_PER_TILE); | |||||
if (m_TerrainTiles == 0) | |||||
return; | return; | ||||
u16 j0, j1, i0, i1; | u16 j0, j1, i0, i1; | ||||
Pathfinding::NearestNavcell(x - hbox.X, z - hbox.Y, i0, j0, m_UpdateInformations.dirtinessGrid.m_W, m_UpdateInformations.dirtinessGrid.m_H); | Pathfinding::NearestNavcell(x - hbox.X, z - hbox.Y, i0, j0, m_UpdateInformations.dirtinessGrid.m_W, m_UpdateInformations.dirtinessGrid.m_H); | ||||
Pathfinding::NearestNavcell(x + hbox.X, z + hbox.Y, i1, j1, m_UpdateInformations.dirtinessGrid.m_W, m_UpdateInformations.dirtinessGrid.m_H); | Pathfinding::NearestNavcell(x + hbox.X, z + hbox.Y, i1, j1, m_UpdateInformations.dirtinessGrid.m_W, m_UpdateInformations.dirtinessGrid.m_H); | ||||
for (int j = j0; j < j1; ++j) | for (int j = j0; j < j1; ++j) | ||||
for (int i = i0; i < i1; ++i) | for (int i = i0; i < i1; ++i) | ||||
▲ Show 20 Lines • Show All 780 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
Can we have a constant for that ? Wonder if the compiler is smart enough to not do from int twice.