Colobot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
terrain.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsiteс.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
25 #pragma once
26 
27 
28 #include "graphics/engine/engine.h"
29 
30 
31 // Graphics module namespace
32 namespace Gfx {
33 
34 class CEngine;
35 class CWater;
36 
37 
39 const float TERRAIN_FLATLIMIT = (5.0f*Math::PI/180.0f);
40 
41 
47 {
49  TR_NULL = 0,
51  TR_STONE = 1,
55  TR_POWER = 3,
57 
58  TR_KEY_A = 4,
59  TR_KEY_B = 5,
60  TR_KEY_C = 6,
61  TR_KEY_D = 7
63 };
64 
70 {
71  Math::Vector center;
72  float factor;
73  float min;
74  float max;
75  float level;
76  float height;
77  float bboxMinX;
78  float bboxMaxX;
79  float bboxMinZ;
80  float bboxMaxZ;
81 
83  {
84  factor = min = max = level = height = 0.0f;
85  bboxMinX = bboxMaxX = bboxMinZ = bboxMaxZ = 0.0f;
86  }
87 };
88 
94 {
96  short id;
98  std::string texName;
102  float hardness;
104  char mat[4];
105 
107  {
108  id = 0;
109  hardness = 0.0f;
110  mat[0] = mat[1] = mat[2] = mat[3] = 0;
111  }
112 };
113 
119 {
121  short id;
123  char mat[4];
124 
126  {
127  id = 0;
128  mat[0] = mat[1] = mat[2] = mat[3] = 0;
129  }
130 };
131 
137 {
138  Math::Vector center;
139  float extRadius;
140  float intRadius;
141  float maxHeight;
142 
143  FlyingLimit()
144  {
145  extRadius = intRadius = maxHeight = 0.0f;
146  }
147 };
148 
149 
222 class CTerrain
223 {
224 public:
225  CTerrain();
226  ~CTerrain();
227 
229  bool Generate(int mosaicCount, int brickCountPow2, float brickSize, float vision, int depth, float hardness);
230 
232  bool InitTextures(const std::string& baseName, int* table, int dx, int dy);
233 
235  void FlushMaterials();
237  void AddMaterial(int id, const std::string& baseName, const Math::Point& uv,
238  int up, int right, int down, int left, float hardness);
240  bool InitMaterials(int id);
242  bool GenerateMaterials(int *id, float min, float max, float slope, float freq, Math::Vector center, float radius);
243 
245  void FlushRelief();
247  bool LoadRelief(const std::string& fileName, float scaleRelief, bool adjustBorder);
249  bool RandomizeRelief();
250 
252  bool LoadResources(const std::string& fileName);
253 
255  bool CreateObjects();
256 
258  bool Terraform(const Math::Vector& p1, const Math::Vector& p2, float height);
259 
261  void SetWind(Math::Vector speed);
263  Math::Vector GetWind();
265 
267  float GetFineSlope(const Math::Vector& pos);
269  float GetCoarseSlope(const Math::Vector& pos);
271  bool GetNormal(Math::Vector& n, const Math::Vector &p);
273  float GetFloorLevel(const Math::Vector& pos, bool brut=false, bool water=false);
275  float GetHeightToFloor(const Math::Vector& pos, bool brut=false, bool water=false);
277  bool AdjustToFloor(Math::Vector& pos, bool brut=false, bool water=false);
281  bool AdjustToBounds(Math::Vector& pos, float margin);
283  TerrainRes GetResource(const Math::Vector& pos);
284 
286  void FlushBuildingLevel();
288  bool AddBuildingLevel(Math::Vector center, float min, float max, float height, float factor);
290  bool UpdateBuildingLevel(Math::Vector center);
292  bool DeleteBuildingLevel(Math::Vector center);
294  float GetBuildingFactor(const Math::Vector& pos);
296  float GetHardness(const Math::Vector& pos);
297 
299  int GetMosaicCount();
301  int GetBrickCount();
303  float GetBrickSize();
305  float GetReliefScale();
306 
308  void ShowFlatGround(Math::Vector pos);
310  float GetFlatZoneRadius(Math::Vector center, float max);
311 
313  void SetFlyingMaxHeight(float height);
315  float GetFlyingMaxHeight();
317  void FlushFlyingLimit();
320  void AddFlyingLimit(Math::Vector center, float extRadius, float intRadius, float maxHeight);
322  float GetFlyingLimit(Math::Vector pos, bool noLimit);
323 
324 protected:
326  bool AddReliefPoint(Math::Vector pos, float scaleRelief);
328  void AdjustRelief();
330  Math::Vector GetVector(int x, int y);
332  VertexTex2 GetVertex(int x, int y, int step);
334  bool CreateMosaic(int ox, int oy, int step, int objRank, const Material& mat);
336  bool CreateSquare(int x, int y);
337 
339  TerrainMaterial* FindMaterial(int id);
341  int FindMaterialByNeighbors(char *mat);
343  void GetTexture(int x, int y, std::string& name, Math::Point& uv);
345  float GetHeight(int x, int y);
347  bool CheckMaterialPoint(int x, int y, float min, float max, float slope);
349  void SetMaterialPoint(int x, int y, int id, char *mat);
351  bool ChangeMaterialPoint(int x, int y, int id);
353  bool CondChangeMaterialPoint(int x, int y, int id, char *mat);
355  void InitMaterialPoints();
357  void FlushMaterialPoints();
358 
361 
362 protected:
363  CEngine* m_engine;
364  CWater* m_water;
365 
367  std::vector<float> m_relief;
369  std::vector<unsigned char> m_resources;
371  std::vector<int> m_textures;
373  std::vector<int> m_objRanks;
374 
382  float m_brickSize;
388  int m_depth;
392  float m_vision;
393 
395  std::string m_texBaseName;
397  std::string m_texBaseExt;
400 
404  std::vector<TerrainMaterial> m_materials;
406  std::vector<TerrainMaterialPoint> m_materialPoints;
411 
412  std::vector<BuildingLevel> m_buildingLevels;
413 
416 
420  std::vector<FlyingLimit> m_flyingLimits;
421 };
422 
423 
424 } // namespace Gfx
425 
float GetCoarseSlope(const Math::Vector &pos)
Gives the approximate slope of the terrain at 2D (XZ) position.
Definition: terrain.cpp:1376
float GetHeight(int x, int y)
Returns the height of the terrain.
Definition: terrain.cpp:762
void FlushRelief()
Clears the relief, resources and all other associated data.
Definition: terrain.cpp:261
bool GetNormal(Math::Vector &n, const Math::Vector &p)
Gives the normal vector at 2D (XZ) position.
Definition: terrain.cpp:1402
bool InitMaterials(int id)
Initializes all the ground with one material.
Definition: terrain.cpp:1122
float GetFlyingLimit(Math::Vector pos, bool noLimit)
Returns the maximum height of flight.
Definition: terrain.cpp:1851
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:121
float m_scaleRelief
Vertical (relief) scale.
Definition: terrain.h:384
Energy.
Definition: terrain.h:55
Main graphics engine - CEngine class.
bool LoadRelief(const std::string &fileName, float scaleRelief, bool adjustBorder)
Load relief from image.
Definition: terrain.cpp:286
int FindMaterialByNeighbors(char *mat)
Seeks a material based on neighbor values.
Definition: terrain.cpp:814
bool CreateMosaic(int ox, int oy, int step, int objRank, const Material &mat)
Creates all objects of a mosaic.
Definition: terrain.cpp:562
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:1497
Math::Vector GetVector(int x, int y)
Calculates a vector of the terrain.
Definition: terrain.cpp:470
Vertex with secondary texture coordinates.
Definition: vertex.h:112
float m_brickSize
Size of single brick (along X and Z axis)
Definition: terrain.h:382
float GetFlatZoneRadius(Math::Vector center, float max)
Calculates the radius of the largest flat area available.
Definition: terrain.cpp:1790
TerrainMaterial * FindMaterial(int id)
Seeks a material based on its ID.
Definition: terrain.cpp:733
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:906
bool m_useMaterials
True if using terrain material mapping.
Definition: terrain.h:402
std::string m_texBaseName
Base name for single texture.
Definition: terrain.h:395
Material for ground surface.
Definition: terrain.h:93
bool CreateObjects()
Creates all objects of the terrain within the 3D engine.
Definition: terrain.cpp:1257
int m_materialAutoID
Internal counter for auto generation of material IDs.
Definition: terrain.h:410
float GetFineSlope(const Math::Vector &pos)
Gives the exact slope of the terrain at 2D (XZ) position.
Definition: terrain.cpp:1369
int m_maxMaterialID
Maximum level ID (no ID is >= to this)
Definition: terrain.h:408
float m_flyingMaxHeight
Global flying height limit.
Definition: terrain.h:418
void AddFlyingLimit(Math::Vector center, float extRadius, float intRadius, float maxHeight)
Adds a new flying limit.
Definition: terrain.cpp:1839
TerrainRes GetResource(const Math::Vector &pos)
Returns the resource type available underground at 2D (XZ) position.
Definition: terrain.cpp:244
No resource.
Definition: terrain.h:49
Material of a surface.
Definition: material.h:44
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:1138
bool CreateSquare(int x, int y)
Creates all objects in a mesh square ground.
Definition: terrain.cpp:1238
char mat[4]
IDs of neighbor materials: up, right, down, left.
Definition: terrain.h:123
const float PI
PI.
Definition: const.h:47
void SetWind(Math::Vector speed)
Management of the wind.
Definition: terrain.cpp:1359
void InitMaterialPoints()
Initializes material points array.
Definition: terrain.cpp:1218
void FlushFlyingLimit()
Empty the table of flying limits.
Definition: terrain.cpp:1833
std::string m_texBaseExt
Extension for single texture.
Definition: terrain.h:397
bool ChangeMaterialPoint(int x, int y, int id)
Modifies the state of a point.
Definition: terrain.cpp:962
float m_vision
Vision before a change of resolution.
Definition: terrain.h:392
std::string texName
Texture.
Definition: terrain.h:98
Uranium.
Definition: terrain.h:53
void FlushMaterials()
Clears all terrain materials.
Definition: terrain.cpp:150
float hardness
Terrain hardness (defines e.g. sound of walking)
Definition: terrain.h:102
std::vector< FlyingLimit > m_flyingLimits
List of local flight limits.
Definition: terrain.h:420
bool LoadResources(const std::string &fileName)
Load resources from image.
Definition: terrain.cpp:192
bool AddReliefPoint(Math::Vector pos, float scaleRelief)
Adds a point of elevation in the buffer of relief.
Definition: terrain.cpp:391
void FlushMaterialPoints()
Clears the material points.
Definition: terrain.cpp:1233
int GetMosaicCount()
Returns number of mosaics.
Definition: terrain.cpp:101
char mat[4]
IDs of neighbor materials: up, right, down, left.
Definition: terrain.h:104
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:827
bool CheckMaterialPoint(int x, int y, float min, float max, float slope)
Decide whether a point is using the materials.
Definition: terrain.cpp:774
bool Terraform(const Math::Vector &p1, const Math::Vector &p2, float height)
Modifies the terrain's relief.
Definition: terrain.cpp:1271
bool Generate(int mosaicCount, int brickCountPow2, float brickSize, float vision, int depth, float hardness)
Generates a new flat terrain.
Definition: terrain.cpp:69
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:744
std::vector< int > m_textures
Texture indices.
Definition: terrain.h:371
2D point
Definition: point.h:49
int GetBrickCount()
Returns number of bricks in mosaic.
Definition: terrain.cpp:106
Material used for terrain point.
Definition: terrain.h:118
std::vector< TerrainMaterial > m_materials
Terrain materials.
Definition: terrain.h:404
short id
Unique ID.
Definition: terrain.h:96
std::vector< TerrainMaterialPoint > m_materialPoints
Material for terrain points.
Definition: terrain.h:406
void FlushBuildingLevel()
Empty the table of elevations.
Definition: terrain.cpp:1607
float GetBuildingFactor(const Math::Vector &pos)
Returns the influence factor whether a position is on a possible rise.
Definition: terrain.cpp:1674
float GetHardness(const Math::Vector &pos)
Returns the hardness of the ground in a given place.
Definition: terrain.cpp:1727
short id
ID of material.
Definition: terrain.h:121
TerrainRes
Underground resource type.
Definition: terrain.h:46
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:158
Math::Point uv
UV texture coordinates.
Definition: terrain.h:100
Terrain loader/generator and manager.
Definition: terrain.h:222
int m_brickCount
Number of bricks per mosaic (along one dimension)
Definition: terrain.h:378
Namespace for (new) graphics code.
Definition: app.h:49
void AdjustRelief()
Adjust the edges of each mosaic to be compatible with all lower resolutions.
Definition: terrain.cpp:411
int m_depth
Number of different resolutions (1,2,3,4)
Definition: terrain.h:388
float m_textureScale
Scale of texture mapping.
Definition: terrain.h:390
The graphics engine.
Definition: engine.h:684
Math::Vector m_wind
Wind speed.
Definition: terrain.h:415
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:1425
float GetBrickSize()
Returns brick size.
Definition: terrain.cpp:111
Spherical limit of flight.
Definition: terrain.h:136
void SetFlyingMaxHeight(float height)
Management of the global max flying height.
Definition: terrain.cpp:1823
bool AddBuildingLevel(Math::Vector center, float min, float max, float height, float factor)
Adds a new elevation for a building.
Definition: terrain.cpp:1612
int m_mosaicCount
Number of mosaics (along one dimension)
Definition: terrain.h:376
Water manager/renderer.
Definition: water.h:118
bool AdjustToStandardBounds(Math::Vector &pos)
Adjusts 3D position so that it is within standard terrain boundaries.
Definition: terrain.cpp:1536
int m_textureSubdivCount
Subdivision of material points in mosaic.
Definition: terrain.h:386
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:1461
3D (3x1) vector
Definition: vector.h:52
VertexTex2 GetVertex(int x, int y, int step)
Calculates a vertex of the terrain.
Definition: terrain.cpp:503
Vault keys.
Definition: terrain.h:58
bool AdjustToBounds(Math::Vector &pos, float margin)
Adjusts 3D position so that it is within terrain boundaries and the given margin. ...
Definition: terrain.cpp:1575
Flat level for building.
Definition: terrain.h:69
std::vector< unsigned char > m_resources
Resources data.
Definition: terrain.h:369
const float TERRAIN_FLATLIMIT
Limit of slope considered a flat piece of land.
Definition: terrain.h:39
bool UpdateBuildingLevel(Math::Vector center)
Updates the elevation for a building when it was moved up (after a terraforming)
Definition: terrain.cpp:1642
float m_defaultHardness
Default hardness for level material.
Definition: terrain.h:399
void AdjustBuildingLevel(Math::Vector &p)
Adjusts a position according to a possible rise.
Definition: terrain.cpp:1691
bool RandomizeRelief()
Load ramdomized relief.
Definition: terrain.cpp:337
bool DeleteBuildingLevel(Math::Vector center)
Removes the elevation for a building when it was destroyed.
Definition: terrain.cpp:1657
Titanium.
Definition: terrain.h:51
void ShowFlatGround(Math::Vector pos)
Shows the flat areas on the ground.
Definition: terrain.cpp:1757
std::vector< float > m_relief
Relief data points.
Definition: terrain.h:367
int m_materialPointCount
Number of terrain material dots (along one dimension)
Definition: terrain.h:380
float GetReliefScale()
Returns the vertical scale of relief.
Definition: terrain.cpp:116
std::vector< int > m_objRanks
Object ranks for mosaic objects.
Definition: terrain.h:373