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);
247  bool RandomizeRelief();
248 
250  bool LoadResources(const std::string& fileName);
251 
253  bool CreateObjects();
254 
256  bool Terraform(const Math::Vector& p1, const Math::Vector& p2, float height);
257 
259 
260  void SetWind(Math::Vector speed);
261  Math::Vector GetWind();
263 
265  float GetFineSlope(const Math::Vector& pos);
267  float GetCoarseSlope(const Math::Vector& pos);
269  bool GetNormal(Math::Vector& n, const Math::Vector &p);
271  float GetFloorLevel(const Math::Vector& pos, bool brut=false, bool water=false);
273  float GetHeightToFloor(const Math::Vector& pos, bool brut=false, bool water=false);
275  bool AdjustToFloor(Math::Vector& pos, bool brut=false, bool water=false);
279  bool AdjustToBounds(Math::Vector& pos, float margin);
281  TerrainRes GetResource(const Math::Vector& pos);
282 
284  void FlushBuildingLevel();
286  bool AddBuildingLevel(Math::Vector center, float min, float max, float height, float factor);
288  bool UpdateBuildingLevel(Math::Vector center);
290  bool DeleteBuildingLevel(Math::Vector center);
292  float GetBuildingFactor(const Math::Vector& pos);
294  float GetHardness(const Math::Vector& pos);
295 
297  int GetMosaicCount();
299  int GetBrickCount();
301  float GetBrickSize();
303  float GetReliefScale();
304 
306  void ShowFlatGround(Math::Vector pos);
308  float GetFlatZoneRadius(Math::Vector center, float max);
309 
311 
312  void SetFlyingMaxHeight(float height);
313  float GetFlyingMaxHeight();
315 
316  void FlushFlyingLimit();
318  void AddFlyingLimit(Math::Vector center, float extRadius, float intRadius, float maxHeight);
320  float GetFlyingLimit(Math::Vector pos, bool noLimit);
321 
322 protected:
324  bool AddReliefPoint(Math::Vector pos, float scaleRelief);
326  void AdjustRelief();
328  Math::Vector GetVector(int x, int y);
330  VertexTex2 GetVertex(int x, int y, int step);
332  bool CreateMosaic(int ox, int oy, int step, int objRank, const Material& mat);
334  bool CreateSquare(int x, int y);
335 
337  TerrainMaterial* FindMaterial(int id);
339  int FindMaterialByNeighbors(char *mat);
341  void GetTexture(int x, int y, std::string& name, Math::Point& uv);
343  float GetHeight(int x, int y);
345  bool CheckMaterialPoint(int x, int y, float min, float max, float slope);
347  void SetMaterialPoint(int x, int y, int id, char *mat);
349  bool ChangeMaterialPoint(int x, int y, int id);
351  bool CondChangeMaterialPoint(int x, int y, int id, char *mat);
353  void InitMaterialPoints();
355  void FlushMaterialPoints();
356 
359 
360 protected:
361  CEngine* m_engine;
362  CWater* m_water;
363 
365  std::vector<float> m_relief;
367  std::vector<unsigned char> m_resources;
369  std::vector<int> m_textures;
371  std::vector<int> m_objRanks;
372 
380  float m_brickSize;
386  int m_depth;
390  float m_vision;
391 
393  std::string m_texBaseName;
395  std::string m_texBaseExt;
398 
402  std::vector<TerrainMaterial> m_materials;
404  std::vector<TerrainMaterialPoint> m_materialPoints;
409 
410  std::vector<BuildingLevel> m_buildingLevels;
411 
414 
418  std::vector<FlyingLimit> m_flyingLimits;
419 };
420 
421 
422 } // namespace Gfx
423