Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CCmpTerrain.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2013 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "precompiled.h"
19 
21 #include "ICmpTerrain.h"
22 
23 #include "ICmpObstructionManager.h"
24 #include "ICmpRangeManager.h"
26 
27 #include "graphics/Terrain.h"
28 #include "renderer/Renderer.h"
29 #include "renderer/WaterManager.h"
30 #include "maths/Vector3D.h"
31 
32 class CCmpTerrain : public ICmpTerrain
33 {
34 public:
35  static void ClassInit(CComponentManager& UNUSED(componentManager))
36  {
37  }
38 
40 
41  CTerrain* m_Terrain; // not null
42 
43  static std::string GetSchema()
44  {
45  return "<a:component type='system'/><empty/>";
46  }
47 
48  virtual void Init(const CParamNode& UNUSED(paramNode))
49  {
51  }
52 
53  virtual void Deinit()
54  {
55  }
56 
57  virtual void Serialize(ISerializer& UNUSED(serialize))
58  {
59  }
60 
61  virtual void Deserialize(const CParamNode& paramNode, IDeserializer& UNUSED(deserialize))
62  {
63  Init(paramNode);
64  }
65 
66  virtual bool IsLoaded()
67  {
68  return m_Terrain->GetVerticesPerSide() != 0;
69  }
70 
72  {
73  CFixedVector3D normal;
74  m_Terrain->CalcNormalFixed((x / (int)TERRAIN_TILE_SIZE).ToInt_RoundToZero(), (z / (int)TERRAIN_TILE_SIZE).ToInt_RoundToZero(), normal);
75  return normal;
76  }
77 
78  virtual CVector3D CalcExactNormal(float x, float z)
79  {
80  return m_Terrain->CalcExactNormal(x, z);
81  }
82 
84  {
85  // TODO: this can crash if the terrain heightmap isn't initialised yet
86 
87  return m_Terrain->GetExactGroundLevelFixed(x, z);
88  }
89 
90  virtual float GetExactGroundLevel(float x, float z)
91  {
92  return m_Terrain->GetExactGroundLevel(x, z);
93  }
94 
95  virtual u16 GetTilesPerSide()
96  {
98  ENSURE(1 <= tiles && tiles <= 65535);
99  return (u16)tiles;
100  }
101 
103  {
104  ssize_t vertices = m_Terrain->GetVerticesPerSide();
105  ENSURE(1 <= vertices && vertices <= 65535);
106  return (u16)vertices;
107  }
108 
110  {
111  return m_Terrain;
112  }
113 
114  virtual void ReloadTerrain()
115  {
116  // TODO: should refactor this code to be nicer
117 
118  u16 tiles = GetTilesPerSide();
119  u16 vertices = GetVerticesPerSide();
120 
121  CmpPtr<ICmpObstructionManager> cmpObstructionManager(GetSystemEntity());
122  if (cmpObstructionManager)
123  {
124  cmpObstructionManager->SetBounds(entity_pos_t::Zero(), entity_pos_t::Zero(),
126  entity_pos_t::FromInt(tiles*(int)TERRAIN_TILE_SIZE));
127  }
128 
129  CmpPtr<ICmpRangeManager> cmpRangeManager(GetSystemEntity());
130  if (cmpRangeManager)
131  {
132  cmpRangeManager->SetBounds(entity_pos_t::Zero(), entity_pos_t::Zero(),
134  entity_pos_t::FromInt(tiles*(int)TERRAIN_TILE_SIZE),
135  vertices);
136  }
137 
139  g_Renderer.GetWaterManager()->SetMapSize(vertices);
140 
141  MakeDirty(0, 0, tiles+1, tiles+1);
142  }
143 
144  virtual void MakeDirty(i32 i0, i32 j0, i32 i1, i32 j1)
145  {
146  CMessageTerrainChanged msg(i0, j0, i1, j1);
148  }
149 };
150 
An entity initialisation parameter node.
Definition: ParamNode.h:112
virtual bool IsLoaded()
Definition: CCmpTerrain.cpp:66
A simple fixed-point number class.
Definition: Fixed.h:115
virtual u16 GetTilesPerSide()
Returns number of tiles per side on the terrain.
Definition: CCmpTerrain.cpp:95
#define REGISTER_COMPONENT_TYPE(cname)
Definition: Component.h:30
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
virtual CTerrain * GetCTerrain()
static CFixed Zero()
Definition: Fixed.h:127
const ssize_t TERRAIN_TILE_SIZE
metres [world space units] per tile in x and z
Definition: Terrain.h:40
virtual void SetBounds(entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1)=0
Set the bounds of the world.
#define i32
Definition: types.h:36
Serialization interface; see serialization overview.
Definition: ISerializer.h:120
Sent when terrain (texture or elevation) has been changed.
Definition: MessageTypes.h:282
ssize_t GetVerticesPerSide() const
Definition: Terrain.h:65
virtual void Serialize(ISerializer &serialize)
Definition: CCmpTerrain.cpp:57
float GetExactGroundLevel(float x, float z) const
Definition: Terrain.cpp:353
virtual void Deserialize(const CParamNode &paramNode, IDeserializer &deserialize)
Definition: CCmpTerrain.cpp:61
virtual u16 GetVerticesPerSide()
Returns number of vertices per side on the terrain.
#define g_Renderer
Definition: Renderer.h:61
entity_id_t GetEntityId() const
Definition: IComponent.h:48
#define ENSURE(expr)
ensure the expression &lt;expr&gt; evaluates to non-zero.
Definition: debug.h:282
virtual float GetExactGroundLevel(float x, float z)
Definition: CCmpTerrain.cpp:90
virtual void Init(const CParamNode &paramNode)
Definition: CCmpTerrain.cpp:48
ssize_t GetTilesPerSide() const
Definition: Terrain.h:67
virtual entity_pos_t GetGroundLevel(entity_pos_t x, entity_pos_t z)
Definition: CCmpTerrain.cpp:83
virtual CFixedVector3D CalcNormal(entity_pos_t x, entity_pos_t z)
Definition: CCmpTerrain.cpp:71
virtual void MakeDirty(i32 i0, i32 j0, i32 i1, i32 j1)
Indicate that terrain tiles within the given region (inclusive lower bound, exclusive upper bound) ha...
#define DEFAULT_COMPONENT_ALLOCATOR(cname)
Definition: Component.h:44
const CSimContext & GetSimContext() const
Definition: IComponent.h:52
static bool IsInitialised()
Definition: Singleton.h:63
virtual void Deinit()
Definition: CCmpTerrain.cpp:53
A simplified syntax for accessing entity components.
Definition: CmpPtr.h:55
intptr_t ssize_t
Definition: wposix_types.h:82
fixed GetExactGroundLevelFixed(fixed x, fixed z) const
Definition: Terrain.cpp:398
static CFixed FromInt(int n)
Definition: Fixed.h:136
CEntityHandle GetSystemEntity() const
Definition: IComponent.h:50
void CalcNormalFixed(ssize_t i, ssize_t j, CFixedVector3D &normal) const
Definition: Terrain.cpp:191
void PostMessage(entity_id_t ent, const CMessage &msg) const
Send a message, targeted at a particular entity.
#define u16
Definition: types.h:40
CTerrain * m_Terrain
Definition: CCmpTerrain.cpp:41
static void ClassInit(CComponentManager &componentManager)
Definition: CCmpTerrain.cpp:35
virtual CVector3D CalcExactNormal(float x, float z)
Definition: CCmpTerrain.cpp:78
virtual void ReloadTerrain()
Call when the underlying CTerrain has been modified behind our backs.
static std::string GetSchema()
Definition: CCmpTerrain.cpp:43
virtual void SetBounds(entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1, ssize_t vertices)=0
Set the bounds of the world.
CVector3D CalcExactNormal(float x, float z) const
Definition: Terrain.cpp:235
CComponentManager & GetComponentManager() const
Definition: SimContext.cpp:35
CTerrain & GetTerrain() const
Definition: SimContext.cpp:51
Deserialization interface; see serialization overview.
Definition: IDeserializer.h:34