Colobot
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-2016, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.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 #include "graphics/core/vertex.h"
28 
29 #include "math/const.h"
30 #include "math/point.h"
31 #include "math/vector.h"
32 
33 #include <string>
34 #include <vector>
35 
36 
37 // Graphics module namespace
38 namespace Gfx
39 {
40 
41 class CEngine;
42 class CWater;
43 struct Material;
44 
45 
47 const float TERRAIN_FLATLIMIT = (5.0f*Math::PI/180.0f);
48 
49 
55 {
57  TR_NULL = 0,
59  TR_STONE = 1,
63  TR_POWER = 3,
65 
66  TR_KEY_A = 4,
67  TR_KEY_B = 5,
68  TR_KEY_C = 6,
69  TR_KEY_D = 7
71 };
74 
147 class CTerrain
148 {
149 public:
150  CTerrain();
151  ~CTerrain();
152 
154  bool Generate(int mosaicCount, int brickCountPow2, float brickSize, float vision, int depth, float hardness);
155 
157  bool InitTextures(const std::string& baseName, int* table, int dx, int dy);
158 
160  void FlushMaterials();
162  void AddMaterial(int id, const std::string& texName, const Math::Point& uv,
163  int up, int right, int down, int left, float hardness);
165  bool InitMaterials(int id);
167  bool GenerateMaterials(int *id, float min, float max, float slope, float freq, Math::Vector center, float radius);
168 
170  void FlushRelief();
172  bool LoadRelief(const std::string& fileName, float scaleRelief, bool adjustBorder);
174  bool RandomizeRelief();
175 
177  bool LoadResources(const std::string& fileName);
178 
180  bool CreateObjects();
181 
183  bool Terraform(const Math::Vector& p1, const Math::Vector& p2, float height);
184 
186  void SetWind(Math::Vector speed);
188  Math::Vector GetWind();
190 
192  float GetFineSlope(const Math::Vector& pos);
194  float GetCoarseSlope(const Math::Vector& pos);
196  bool GetNormal(Math::Vector& n, const Math::Vector &p);
198  float GetFloorLevel(const Math::Vector& pos, bool brut=false, bool water=false);
200  float GetHeightToFloor(const Math::Vector& pos, bool brut=false, bool water=false);
202  bool AdjustToFloor(Math::Vector& pos, bool brut=false, bool water=false);
206  bool AdjustToBounds(Math::Vector& pos, float margin);
208  TerrainRes GetResource(const Math::Vector& pos);
209 
211  void FlushBuildingLevel();
213  bool AddBuildingLevel(Math::Vector center, float min, float max, float height, float factor);
215  bool UpdateBuildingLevel(Math::Vector center);
217  bool DeleteBuildingLevel(Math::Vector center);
219  float GetBuildingFactor(const Math::Vector& pos);
221  float GetHardness(const Math::Vector& pos);
222 
224  int GetMosaicCount();
226  int GetBrickCount();
228  float GetBrickSize();
230  float GetReliefScale();
231 
233  void ShowFlatGround(Math::Vector pos);
235  float GetFlatZoneRadius(Math::Vector center, float max);
236 
238  void SetFlyingMaxHeight(float height);
240  float GetFlyingMaxHeight();
242  void FlushFlyingLimit();
245  void AddFlyingLimit(Math::Vector center, float extRadius, float intRadius, float maxHeight);
247  float GetFlyingLimit(Math::Vector pos, bool noLimit);
248 
249 protected:
251  bool AddReliefPoint(Math::Vector pos, float scaleRelief);
253  void AdjustRelief();
255  Math::Vector GetVector(int x, int y);
257  VertexTex2 GetVertex(int x, int y, int step);
259  bool CreateMosaic(int ox, int oy, int step, int objRank, const Material& mat);
261  bool CreateSquare(int x, int y);
262 
263  struct TerrainMaterial;
265  TerrainMaterial* FindMaterial(int id);
267  int FindMaterialByNeighbors(char *mat);
269  void GetTexture(int x, int y, std::string& name, Math::Point& uv);
271  float GetHeight(int x, int y);
273  bool CheckMaterialPoint(int x, int y, float min, float max, float slope);
275  void SetMaterialPoint(int x, int y, int id, char *mat);
277  bool ChangeMaterialPoint(int x, int y, int id);
279  bool CondChangeMaterialPoint(int x, int y, int id, char *mat);
281  void InitMaterialPoints();
283  void FlushMaterialPoints();
284 
287 
288 protected:
289  CEngine* m_engine;
290  CWater* m_water;
291 
293  std::vector<float> m_relief;
295  std::vector<unsigned char> m_resources;
297  std::vector<int> m_textures;
299  std::vector<int> m_objRanks;
300 
308  float m_brickSize;
314  int m_depth;
318  float m_vision;
319 
321  std::string m_texBaseName;
323  std::string m_texBaseExt;
331  {
333  short id = 0;
335  std::string texName;
339  float hardness = 0.0f;
341  char mat[4] = {};
342  };
344  std::vector<TerrainMaterial> m_materials;
345 
351  {
353  short id = 0;
355  char mat[4] = {};
356  };
357 
358 
360  std::vector<TerrainMaterialPoint> m_materialPoints;
361 
368 
374  {
375  Math::Vector center;
376  float factor = 0.0f;
377  float min = 0.0f;
378  float max = 0.0f;
379  float level = 0.0f;
380  float height = 0.0f;
381  float bboxMinX = 0.0f;
382  float bboxMaxX = 0.0f;
383  float bboxMinZ = 0.0f;
384  float bboxMaxZ = 0.0f;
385  };
386  std::vector<BuildingLevel> m_buildingLevels;
387 
390 
393 
398  struct FlyingLimit
399  {
400  Math::Vector center;
401  float extRadius = 0.0f;
402  float intRadius = 0.0f;
403  float maxHeight = 0.0f;
404  };
406  std::vector<FlyingLimit> m_flyingLimits;
407 };
408 
409 
410 } // namespace Gfx
411 
float GetCoarseSlope(const Math::Vector &pos)
Gives the approximate slope of the terrain at 2D (XZ) position.
Definition: terrain.cpp:1388
float GetHeight(int x, int y)
Returns the height of the terrain.
Definition: terrain.cpp:774
void FlushRelief()
Clears the relief, resources and all other associated data.
Definition: terrain.cpp:273
bool GetNormal(Math::Vector &n, const Math::Vector &p)
Gives the normal vector at 2D (XZ) position.
Definition: terrain.cpp:1414
bool InitMaterials(int id)
Initializes all the ground with one material.
Definition: terrain.cpp:1134
float GetFlyingLimit(Math::Vector pos, bool noLimit)
Returns the maximum height of flight.
Definition: terrain.cpp:1863
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:127
float m_scaleRelief
Vertical (relief) scale.
Definition: terrain.h:310
Energy.
Definition: terrain.h:63
Gfx::IntColor ResourceToColor(TerrainRes res)
Converts TerrainRes to color.
Definition: terrain.cpp:206
bool LoadRelief(const std::string &fileName, float scaleRelief, bool adjustBorder)
Load relief from image.
Definition: terrain.cpp:298
int FindMaterialByNeighbors(char *mat)
Seeks a material based on neighbor values.
Definition: terrain.cpp:826
Math::Point uv
UV texture coordinates.
Definition: terrain.h:337
bool CreateMosaic(int ox, int oy, int step, int objRank, const Material &mat)
Creates all objects of a mosaic.
Definition: terrain.cpp:574
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:1509
Math::Vector GetVector(int x, int y)
Calculates a vector of the terrain.
Definition: terrain.cpp:482
Vertex with secondary texture coordinates.
Definition: vertex.h:113
float m_brickSize
Size of single brick (along X and Z axis)
Definition: terrain.h:308
float GetFlatZoneRadius(Math::Vector center, float max)
Calculates the radius of the largest flat area available.
Definition: terrain.cpp:1802
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:918
bool m_useMaterials
True if using terrain material mapping.
Definition: terrain.h:363
std::string m_texBaseName
Base name for single texture.
Definition: terrain.h:321
Color with integer values.
Definition: color.h:101
Point struct and related functions.
bool CreateObjects()
Creates all objects of the terrain within the 3D engine.
Definition: terrain.cpp:1269
int m_materialAutoID
Internal counter for auto generation of material IDs.
Definition: terrain.h:367
float GetFineSlope(const Math::Vector &pos)
Gives the exact slope of the terrain at 2D (XZ) position.
Definition: terrain.cpp:1381
int m_maxMaterialID
Maximum level ID (no ID is >= to this)
Definition: terrain.h:365
float m_flyingMaxHeight
Global flying height limit.
Definition: terrain.h:392
void AddFlyingLimit(Math::Vector center, float extRadius, float intRadius, float maxHeight)
Adds a new flying limit.
Definition: terrain.cpp:1851
TerrainRes GetResource(const Math::Vector &pos)
Returns the resource type available underground at 2D (XZ) position.
Definition: terrain.cpp:256
No resource.
Definition: terrain.h:57
Material of a surface.
Definition: material.h:45
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:1150
bool CreateSquare(int x, int y)
Creates all objects in a mesh square ground.
Definition: terrain.cpp:1250
const float PI
PI.
Definition: const.h:48
void SetWind(Math::Vector speed)
Management of the wind.
Definition: terrain.cpp:1371
void InitMaterialPoints()
Initializes material points array.
Definition: terrain.cpp:1230
void FlushFlyingLimit()
Empty the table of flying limits.
Definition: terrain.cpp:1845
std::string m_texBaseExt
Extension for single texture.
Definition: terrain.h:323
bool ChangeMaterialPoint(int x, int y, int id)
Modifies the state of a point.
Definition: terrain.cpp:974
float m_vision
Vision before a change of resolution.
Definition: terrain.h:318
Uranium.
Definition: terrain.h:61
void AddMaterial(int id, const std::string &texName, 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:164
Material for ground surface.
Definition: terrain.h:330
void FlushMaterials()
Clears all terrain materials.
Definition: terrain.cpp:156
std::vector< FlyingLimit > m_flyingLimits
List of local flight limits.
Definition: terrain.h:406
bool LoadResources(const std::string &fileName)
Load resources from image.
Definition: terrain.cpp:214
std::string texName
Texture.
Definition: terrain.h:335
TerrainMaterial * FindMaterial(int id)
Seeks a material based on its ID.
Definition: terrain.cpp:745
bool AddReliefPoint(Math::Vector pos, float scaleRelief)
Adds a point of elevation in the buffer of relief.
Definition: terrain.cpp:403
void FlushMaterialPoints()
Clears the material points.
Definition: terrain.cpp:1245
int GetMosaicCount()
Returns number of mosaics.
Definition: terrain.cpp:107
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:839
Vertex structs.
bool CheckMaterialPoint(int x, int y, float min, float max, float slope)
Decide whether a point is using the materials.
Definition: terrain.cpp:786
bool Terraform(const Math::Vector &p1, const Math::Vector &p2, float height)
Modifies the terrain&#39;s relief.
Definition: terrain.cpp:1283
bool Generate(int mosaicCount, int brickCountPow2, float brickSize, float vision, int depth, float hardness)
Generates a new flat terrain.
Definition: terrain.cpp:75
Material used for terrain point.
Definition: terrain.h:350
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:756
std::vector< int > m_textures
Texture indices.
Definition: terrain.h:297
2D point
Definition: point.h:50
int GetBrickCount()
Returns number of bricks in mosaic.
Definition: terrain.cpp:112
Spherical limit of flight.
Definition: terrain.h:398
std::vector< TerrainMaterial > m_materials
Terrain materials.
Definition: terrain.h:344
std::vector< TerrainMaterialPoint > m_materialPoints
Material for terrain points.
Definition: terrain.h:360
void FlushBuildingLevel()
Empty the table of elevations.
Definition: terrain.cpp:1619
float GetBuildingFactor(const Math::Vector &pos)
Returns the influence factor whether a position is on a possible rise.
Definition: terrain.cpp:1686
float GetHardness(const Math::Vector &pos)
Returns the hardness of the ground in a given place.
Definition: terrain.cpp:1739
TerrainRes
Underground resource type.
Definition: terrain.h:54
Terrain loader/generator and manager.
Definition: terrain.h:147
int m_brickCount
Number of bricks per mosaic (along one dimension)
Definition: terrain.h:304
Constants used in math functions.
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:423
int m_depth
Number of different resolutions (1,2,3,4)
Definition: terrain.h:314
float m_textureScale
Scale of texture mapping.
Definition: terrain.h:316
Vector struct and related functions.
The graphics engine.
Definition: engine.h:585
Math::Vector m_wind
Wind speed.
Definition: terrain.h:389
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:1437
float GetBrickSize()
Returns brick size.
Definition: terrain.cpp:117
void SetFlyingMaxHeight(float height)
Management of the global max flying height.
Definition: terrain.cpp:1835
bool AddBuildingLevel(Math::Vector center, float min, float max, float height, float factor)
Adds a new elevation for a building.
Definition: terrain.cpp:1624
int m_mosaicCount
Number of mosaics (along one dimension)
Definition: terrain.h:302
Water manager/renderer.
Definition: water.h:72
bool AdjustToStandardBounds(Math::Vector &pos)
Adjusts 3D position so that it is within standard terrain boundaries.
Definition: terrain.cpp:1548
int m_textureSubdivCount
Subdivision of material points in mosaic.
Definition: terrain.h:312
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:1473
3D (3x1) vector
Definition: vector.h:53
VertexTex2 GetVertex(int x, int y, int step)
Calculates a vertex of the terrain.
Definition: terrain.cpp:515
Vault keys.
Definition: terrain.h:66
Flat level for building.
Definition: terrain.h:373
bool AdjustToBounds(Math::Vector &pos, float margin)
Adjusts 3D position so that it is within terrain boundaries and the given margin. ...
Definition: terrain.cpp:1587
std::vector< unsigned char > m_resources
Resources data.
Definition: terrain.h:295
const float TERRAIN_FLATLIMIT
Limit of slope considered a flat piece of land.
Definition: terrain.h:47
bool UpdateBuildingLevel(Math::Vector center)
Updates the elevation for a building when it was moved up (after a terraforming)
Definition: terrain.cpp:1654
float m_defaultHardness
Default hardness for level material.
Definition: terrain.h:325
void AdjustBuildingLevel(Math::Vector &p)
Adjusts a position according to a possible rise.
Definition: terrain.cpp:1703
bool RandomizeRelief()
Load ramdomized relief.
Definition: terrain.cpp:349
bool DeleteBuildingLevel(Math::Vector center)
Removes the elevation for a building when it was destroyed.
Definition: terrain.cpp:1669
Titanium.
Definition: terrain.h:59
void ShowFlatGround(Math::Vector pos)
Shows the flat areas on the ground.
Definition: terrain.cpp:1769
std::vector< float > m_relief
Relief data points.
Definition: terrain.h:293
int m_materialPointCount
Number of terrain material dots (along one dimension)
Definition: terrain.h:306
float GetReliefScale()
Returns the vertical scale of relief.
Definition: terrain.cpp:122
std::vector< int > m_objRanks
Object ranks for mosaic objects.
Definition: terrain.h:299