Colobot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
terrain.h
Go to the documentation of this file.
1 // * This file is part of the COLOBOT source code
2 // * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch
3 // * Copyright (C) 2012, Polish Portal of Colobot (PPC)
4 // *
5 // * This program is free software: you can redistribute it and/or modify
6 // * it under the terms of the GNU General Public License as published by
7 // * the Free Software Foundation, either version 3 of the License, or
8 // * (at your option) any later version.
9 // *
10 // * This program is distributed in the hope that it will be useful,
11 // * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // * GNU General Public License for more details.
14 // *
15 // * You should have received a copy of the GNU General Public License
16 // * along with this program. If not, see http://www.gnu.org/licenses/.
17 
23 #pragma once
24 
25 
26 #include "graphics/engine/engine.h"
27 
28 
29 // Graphics module namespace
30 namespace Gfx {
31 
32 class CEngine;
33 class CWater;
34 
35 
37 const float TERRAIN_FLATLIMIT = (5.0f*Math::PI/180.0f);
38 
39 
45 {
47  TR_NULL = 0,
49  TR_STONE = 1,
53  TR_POWER = 3,
55 
56  TR_KEY_A = 4,
57  TR_KEY_B = 5,
58  TR_KEY_C = 6,
59  TR_KEY_D = 7
61 };
62 
68 {
69  Math::Vector center;
70  float factor;
71  float min;
72  float max;
73  float level;
74  float height;
75  float bboxMinX;
76  float bboxMaxX;
77  float bboxMinZ;
78  float bboxMaxZ;
79 
81  {
82  factor = min = max = level = height = 0.0f;
83  bboxMinX = bboxMaxX = bboxMinZ = bboxMaxZ = 0.0f;
84  }
85 };
86 
92 {
94  short id;
96  std::string texName;
100  float hardness;
102  char mat[4];
103 
105  {
106  id = 0;
107  hardness = 0.0f;
108  mat[0] = mat[1] = mat[2] = mat[3] = 0;
109  }
110 };
111 
117 {
119  short id;
121  char mat[4];
122 
124  {
125  id = 0;
126  mat[0] = mat[1] = mat[2] = mat[3] = 0;
127  }
128 };
129 
135 {
136  Math::Vector center;
137  float extRadius;
138  float intRadius;
139  float maxHeight;
140 
141  FlyingLimit()
142  {
143  extRadius = intRadius = maxHeight = 0.0f;
144  }
145 };
146 
147 
220 class CTerrain
221 {
222 public:
223  CTerrain();
224  ~CTerrain();
225 
227  bool Generate(int mosaicCount, int brickCountPow2, float brickSize, float vision, int depth, float hardness);
228 
230  bool InitTextures(const std::string& baseName, int* table, int dx, int dy);
231 
233  void FlushMaterials();
235  void AddMaterial(int id, const std::string& baseName, const Math::Point& uv,
236  int up, int right, int down, int left, float hardness);
238  bool InitMaterials(int id);
240  bool GenerateMaterials(int *id, float min, float max, float slope, float freq, Math::Vector center, float radius);
241 
243  void FlushRelief();
245  bool LoadRelief(const std::string& fileName, float scaleRelief, bool adjustBorder);
246 
248  bool LoadResources(const std::string& fileName);
249 
251  bool CreateObjects();
252 
254  bool Terraform(const Math::Vector& p1, const Math::Vector& p2, float height);
255 
257  void SetWind(Math::Vector speed);
259  Math::Vector GetWind();
261 
263  float GetFineSlope(const Math::Vector& pos);
265  float GetCoarseSlope(const Math::Vector& pos);
267  bool GetNormal(Math::Vector& n, const Math::Vector &p);
269  float GetFloorLevel(const Math::Vector& pos, bool brut=false, bool water=false);
271  float GetHeightToFloor(const Math::Vector& pos, bool brut=false, bool water=false);
273  bool AdjustToFloor(Math::Vector& pos, bool brut=false, bool water=false);
277  bool AdjustToBounds(Math::Vector& pos, float margin);
279  TerrainRes GetResource(const Math::Vector& pos);
280 
282  void FlushBuildingLevel();
284  bool AddBuildingLevel(Math::Vector center, float min, float max, float height, float factor);
286  bool UpdateBuildingLevel(Math::Vector center);
288  bool DeleteBuildingLevel(Math::Vector center);
290  float GetBuildingFactor(const Math::Vector& pos);
292  float GetHardness(const Math::Vector& pos);
293 
295  int GetMosaicCount();
297  int GetBrickCount();
299  float GetBrickSize();
301  float GetReliefScale();
302 
304  void ShowFlatGround(Math::Vector pos);
306  float GetFlatZoneRadius(Math::Vector center, float max);
307 
309  void SetFlyingMaxHeight(float height);
311  float GetFlyingMaxHeight();
313  void FlushFlyingLimit();
316  void AddFlyingLimit(Math::Vector center, float extRadius, float intRadius, float maxHeight);
318  float GetFlyingLimit(Math::Vector pos, bool noLimit);
319 
320 protected:
322  bool AddReliefPoint(Math::Vector pos, float scaleRelief);
324  void AdjustRelief();
326  Math::Vector GetVector(int x, int y);
328  VertexTex2 GetVertex(int x, int y, int step);
330  bool CreateMosaic(int ox, int oy, int step, int objRank, const Material& mat);
332  bool CreateSquare(int x, int y);
333 
335  TerrainMaterial* FindMaterial(int id);
337  int FindMaterialByNeighbors(char *mat);
339  void GetTexture(int x, int y, std::string& name, Math::Point& uv);
341  float GetHeight(int x, int y);
343  bool CheckMaterialPoint(int x, int y, float min, float max, float slope);
345  void SetMaterialPoint(int x, int y, int id, char *mat);
347  bool ChangeMaterialPoint(int x, int y, int id);
349  bool CondChangeMaterialPoint(int x, int y, int id, char *mat);
351  void InitMaterialPoints();
353  void FlushMaterialPoints();
354 
357 
358 protected:
359  CEngine* m_engine;
360  CWater* m_water;
361 
363  std::vector<float> m_relief;
365  std::vector<unsigned char> m_resources;
367  std::vector<int> m_textures;
369  std::vector<int> m_objRanks;
370 
378  float m_brickSize;
384  int m_depth;
388  float m_vision;
389 
391  std::string m_texBaseName;
393  std::string m_texBaseExt;
396 
400  std::vector<TerrainMaterial> m_materials;
402  std::vector<TerrainMaterialPoint> m_materialPoints;
407 
408  std::vector<BuildingLevel> m_buildingLevels;
409 
412 
416  std::vector<FlyingLimit> m_flyingLimits;
417 };
418 
419 
420 } // namespace Gfx
421 
float GetCoarseSlope(const Math::Vector &pos)
Gives the approximate slope of the terrain at 2D (XZ) position.
Definition: terrain.cpp:1315
float GetHeight(int x, int y)
Returns the height of the terrain.
Definition: terrain.cpp:701
void FlushRelief()
Clears the relief, resources and all other associated data.
Definition: terrain.cpp:258
bool GetNormal(Math::Vector &n, const Math::Vector &p)
Gives the normal vector at 2D (XZ) position.
Definition: terrain.cpp:1341
bool InitMaterials(int id)
Initializes all the ground with one material.
Definition: terrain.cpp:1061
float GetFlyingLimit(Math::Vector pos, bool noLimit)
Returns the maximum height of flight.
Definition: terrain.cpp:1790
bool InitTextures(const std::string &baseName, int *table, int dx, int dy)
Initializes the names of textures to use for the land.
Definition: terrain.cpp:119
float m_scaleRelief
Vertical (relief) scale.
Definition: terrain.h:380
Energy.
Definition: terrain.h:53
Main graphics engine - CEngine class.
bool LoadRelief(const std::string &fileName, float scaleRelief, bool adjustBorder)
Load relief from image.
Definition: terrain.cpp:283
int FindMaterialByNeighbors(char *mat)
Seeks a material based on neighbor values.
Definition: terrain.cpp:753
bool CreateMosaic(int ox, int oy, int step, int objRank, const Material &mat)
Creates all objects of a mosaic.
Definition: terrain.cpp:505
bool AdjustToFloor(Math::Vector &pos, bool brut=false, bool water=false)
Modifies the Y coordinate of 3D position to rest on the ground floor.
Definition: terrain.cpp:1436
Math::Vector GetVector(int x, int y)
Calculates a vector of the terrain.
Definition: terrain.cpp:413
Vertex with secondary texture coordinates.
Definition: vertex.h:107
float m_brickSize
Size of single brick (along X and Z axis)
Definition: terrain.h:378
float GetFlatZoneRadius(Math::Vector center, float max)
Calculates the radius of the largest flat area available.
Definition: terrain.cpp:1729
TerrainMaterial * FindMaterial(int id)
Seeks a material based on its ID.
Definition: terrain.cpp:672
bool CondChangeMaterialPoint(int x, int y, int id, char *mat)
Tests if a material can give a place, according to its four neighbors. If yes, puts the point...
Definition: terrain.cpp:845
bool m_useMaterials
True if using terrain material mapping.
Definition: terrain.h:398
std::string m_texBaseName
Base name for single texture.
Definition: terrain.h:391
Material for ground surface.
Definition: terrain.h:91
bool CreateObjects()
Creates all objects of the terrain within the 3D engine.
Definition: terrain.cpp:1196
int m_materialAutoID
Internal counter for auto generation of material IDs.
Definition: terrain.h:406
float GetFineSlope(const Math::Vector &pos)
Gives the exact slope of the terrain at 2D (XZ) position.
Definition: terrain.cpp:1308
int m_maxMaterialID
Maximum level ID (no ID is >= to this)
Definition: terrain.h:404
float m_flyingMaxHeight
Global flying height limit.
Definition: terrain.h:414
void AddFlyingLimit(Math::Vector center, float extRadius, float intRadius, float maxHeight)
Adds a new flying limit.
Definition: terrain.cpp:1778
TerrainRes GetResource(const Math::Vector &pos)
Returns the resource type available underground at 2D (XZ) position.
Definition: terrain.cpp:241
No resource.
Definition: terrain.h:47
Material of a surface.
Definition: material.h:41
bool GenerateMaterials(int *id, float min, float max, float slope, float freq, Math::Vector center, float radius)
Generates a level in the terrain.
Definition: terrain.cpp:1077
bool CreateSquare(int x, int y)
Creates all objects in a mesh square ground.
Definition: terrain.cpp:1177
char mat[4]
IDs of neighbor materials: up, right, down, left.
Definition: terrain.h:121
const float PI
PI.
Definition: const.h:44
void SetWind(Math::Vector speed)
Management of the wind.
Definition: terrain.cpp:1298
void InitMaterialPoints()
Initializes material points array.
Definition: terrain.cpp:1157
void FlushFlyingLimit()
Empty the table of flying limits.
Definition: terrain.cpp:1772
std::string m_texBaseExt
Extension for single texture.
Definition: terrain.h:393
bool ChangeMaterialPoint(int x, int y, int id)
Modifies the state of a point.
Definition: terrain.cpp:901
float m_vision
Vision before a change of resolution.
Definition: terrain.h:388
std::string texName
Texture.
Definition: terrain.h:96
Uranium.
Definition: terrain.h:51
void FlushMaterials()
Clears all terrain materials.
Definition: terrain.cpp:147
float hardness
Terrain hardness (defines e.g. sound of walking)
Definition: terrain.h:100
std::vector< FlyingLimit > m_flyingLimits
List of local flight limits.
Definition: terrain.h:416
bool LoadResources(const std::string &fileName)
Load resources from image.
Definition: terrain.cpp:189
bool AddReliefPoint(Math::Vector pos, float scaleRelief)
Adds a point of elevation in the buffer of relief.
Definition: terrain.cpp:334
void FlushMaterialPoints()
Clears the material points.
Definition: terrain.cpp:1172
int GetMosaicCount()
Returns number of mosaics.
Definition: terrain.cpp:99
char mat[4]
IDs of neighbor materials: up, right, down, left.
Definition: terrain.h:102
void SetMaterialPoint(int x, int y, int id, char *mat)
Modifies the state of a point and its four neighbors, without testing if possible.
Definition: terrain.cpp:766
bool CheckMaterialPoint(int x, int y, float min, float max, float slope)
Decide whether a point is using the materials.
Definition: terrain.cpp:713
bool Terraform(const Math::Vector &p1, const Math::Vector &p2, float height)
Modifies the terrain's relief.
Definition: terrain.cpp:1210
bool Generate(int mosaicCount, int brickCountPow2, float brickSize, float vision, int depth, float hardness)
Generates a new flat terrain.
Definition: terrain.cpp:67
void GetTexture(int x, int y, std::string &name, Math::Point &uv)
Returns the texture name and UV coords to use for a given square.
Definition: terrain.cpp:683
std::vector< int > m_textures
Texture indices.
Definition: terrain.h:367
2D point
Definition: point.h:46
int GetBrickCount()
Returns number of bricks in mosaic.
Definition: terrain.cpp:104
Material used for terrain point.
Definition: terrain.h:116
std::vector< TerrainMaterial > m_materials
Terrain materials.
Definition: terrain.h:400
short id
Unique ID.
Definition: terrain.h:94
std::vector< TerrainMaterialPoint > m_materialPoints
Material for terrain points.
Definition: terrain.h:402
void FlushBuildingLevel()
Empty the table of elevations.
Definition: terrain.cpp:1546
float GetBuildingFactor(const Math::Vector &pos)
Returns the influence factor whether a position is on a possible rise.
Definition: terrain.cpp:1613
float GetHardness(const Math::Vector &pos)
Returns the hardness of the ground in a given place.
Definition: terrain.cpp:1666
short id
ID of material.
Definition: terrain.h:119
TerrainRes
Underground resource type.
Definition: terrain.h:44
void AddMaterial(int id, const std::string &baseName, const Math::Point &uv, int up, int right, int down, int left, float hardness)
Adds a terrain material the names of textures to use for the land.
Definition: terrain.cpp:155
Math::Point uv
UV texture coordinates.
Definition: terrain.h:98
Terrain loader/generator and manager.
Definition: terrain.h:220
int m_brickCount
Number of bricks per mosaic (along one dimension)
Definition: terrain.h:374
void AdjustRelief()
Adjust the edges of each mosaic to be compatible with all lower resolutions.
Definition: terrain.cpp:354
int m_depth
Number of different resolutions (1,2,3,4)
Definition: terrain.h:384
float m_textureScale
Scale of texture mapping.
Definition: terrain.h:386
The graphics engine.
Definition: engine.h:681
Math::Vector m_wind
Wind speed.
Definition: terrain.h:411
float GetFloorLevel(const Math::Vector &pos, bool brut=false, bool water=false)
Returns the height of the ground level at 2D (XZ) position.
Definition: terrain.cpp:1364
float GetBrickSize()
Returns brick size.
Definition: terrain.cpp:109
Spherical limit of flight.
Definition: terrain.h:134
void SetFlyingMaxHeight(float height)
Management of the global max flying height.
Definition: terrain.cpp:1762
bool AddBuildingLevel(Math::Vector center, float min, float max, float height, float factor)
Adds a new elevation for a building.
Definition: terrain.cpp:1551
int m_mosaicCount
Number of mosaics (along one dimension)
Definition: terrain.h:372
Water manager/renderer.
Definition: water.h:116
bool AdjustToStandardBounds(Math::Vector &pos)
Adjusts 3D position so that it is within standard terrain boundaries.
Definition: terrain.cpp:1475
int m_textureSubdivCount
Subdivision of material points in mosaic.
Definition: terrain.h:382
float GetHeightToFloor(const Math::Vector &pos, bool brut=false, bool water=false)
Returns the distance to the ground level from 3D position.
Definition: terrain.cpp:1400
3D (3x1) vector
Definition: vector.h:49
VertexTex2 GetVertex(int x, int y, int step)
Calculates a vertex of the terrain.
Definition: terrain.cpp:446
Vault keys.
Definition: terrain.h:56
bool AdjustToBounds(Math::Vector &pos, float margin)
Adjusts 3D position so that it is within terrain boundaries and the given margin. ...
Definition: terrain.cpp:1514
Flat level for building.
Definition: terrain.h:67
std::vector< unsigned char > m_resources
Resources data.
Definition: terrain.h:365
const float TERRAIN_FLATLIMIT
Limit of slope considered a flat piece of land.
Definition: terrain.h:37
bool UpdateBuildingLevel(Math::Vector center)
Updates the elevation for a building when it was moved up (after a terraforming)
Definition: terrain.cpp:1581
float m_defaultHardness
Default hardness for level material.
Definition: terrain.h:395
void AdjustBuildingLevel(Math::Vector &p)
Adjusts a position according to a possible rise.
Definition: terrain.cpp:1630
bool DeleteBuildingLevel(Math::Vector center)
Removes the elevation for a building when it was destroyed.
Definition: terrain.cpp:1596
Titanium.
Definition: terrain.h:49
void ShowFlatGround(Math::Vector pos)
Shows the flat areas on the ground.
Definition: terrain.cpp:1696
std::vector< float > m_relief
Relief data points.
Definition: terrain.h:363
int m_materialPointCount
Number of terrain material dots (along one dimension)
Definition: terrain.h:376
float GetReliefScale()
Returns the vertical scale of relief.
Definition: terrain.cpp:114
std::vector< int > m_objRanks
Object ranks for mosaic objects.
Definition: terrain.h:369