File: util_worldgen.hpp

package info (click to toggle)
libtcod 1.18.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,464 kB
  • sloc: ansic: 48,535; cpp: 11,905; python: 4,840; makefile: 44; sh: 25
file content (148 lines) | stat: -rw-r--r-- 5,270 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * Copyright (c) 2009 Jice
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * The name of Jice may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Jice ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Jice BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include <vector>

// size of the heightmap
#define HM_WIDTH 400
#define HM_HEIGHT 400

// biome and climate list. based on Whittaker Biome Diagram
enum EClimate { ARCTIC_ALPINE, COLD, TEMPERATE, WARM, TROPICAL, NB_CLIMATES };

// grassland : might be either grassland, shrubland or woodland depending on the vegetation level
// savanna : might be either savanna or thorn forest depending on the vegetation level
enum EBiome {
  TUNDRA,
  COLD_DESERT,
  GRASSLAND,
  BOREAL_FOREST,
  TEMPERATE_FOREST,
  TROPICAL_MONTANE_FOREST,
  HOT_DESERT,
  SAVANNA,
  TROPICAL_DRY_FOREST,
  TROPICAL_EVERGREEN_FOREST,
  THORN_FOREST,
  NB_BIOMES
};

class WorldGenerator {
 public:
  // altitude->color map
  TCODColor mapGradient[256];
  // world height map (0.0 - 1.0)
  TCODHeightMap* hm;
  // height map without erosion
  TCODHeightMap* hm2;
  // complete world map (not shaded)
  TCODImage* worldmap;
  // temperature map (in °C)
  TCODHeightMap* temperature;
  // precipitation map (0.0 - 1.0)
  TCODHeightMap* precipitation;
  // biome map
  EBiome* biomeMap;

  void generate(TCODRandom* wRng);

  // getters
  int getWidth() const;
  int getHeight() const;
  float getAltitude(int x, int y) const;  // heightmap. between 0 and 1
  float getInterpolatedAltitude(float x, float y) const;
  float getSandHeight() const;
  bool isOnSea(float x, float y) const;
  float getCloudThickness(float x, float y) const;
  void getInterpolatedNormal(float x, float y, float n[3]) const;
  TCODColor getInterpolatedColor(float worldX, float worldY);
  float getInterpolatedIntensity(float worldX, float worldY);

  // update
  void updateClouds(float elapsedTime);
  void computeSunLight(float lightDir[3]);

  // data
  float getRealAltitude(float x, float y) const;    // altitude in meters
  float getPrecipitations(float x, float y) const;  // in centimeter/m²/year
  float getTemperature(float x, float y) const;     // in °C
  EBiome getBiome(float x, float y) const;

  // map generators
  void saveBiomeMap(const char* filename = NULL);
  void saveAltitudeMap(const char* filename = NULL);
  void saveTemperatureMap(const char* filename = NULL);
  void savePrecipitationMap(const char* filename = NULL);

 protected:
  friend class RiverPathCbk;
  TCODNoise* noise;
  // cloud thickness
  float clouds[HM_WIDTH][HM_HEIGHT];
  float cloudDx;  // horizontal offset for smooth scrolling
  float cloudTotalDx;
  // world light intensity map (shadow map)
  float* worldint;
  typedef struct {
    float slope;
    // number of cells flowing into this cell
    uint32_t area;
    // direction of lowest neighbour
    uint8_t flowDir;
    // inverse flow direction
    uint8_t upDir;
    uint8_t inFlags;  // incoming flows
    uint8_t riverId;
    int riverLength;
  } map_data_t;
  map_data_t* mapData;
  typedef struct {
    std::vector<int> coords;
    std::vector<int> strength;
  } river_t;
  std::vector<river_t> rivers;
  TCODRandom* wgRng;

  void addHill(int nbHill, float baseRadius, float radiusVar, float height);
  void buildBaseMap();
  void erodeMap();
  void smoothMap();
  // compute the ground color from the heightmap
  TCODColor getMapColor(float h);
  // get sun light intensity on a point of the map
  float getMapIntensity(float worldX, float worldY, float lightDir[3]);
  TCODColor getInterpolatedColor(TCODImage* img, float x, float y);
  float getInterpolatedFloat(float* arr, float x, float y, int width, int height);
  void generateRivers();
  void smoothPrecipitations();
  int getRiverStrength(int riverId);
  void setLandMass(float percent, float waterLevel);
  void computeTemperaturesAndBiomes();
  TCODColor getBiomeColor(EBiome biome, int x, int y);
  void computePrecipitations();
  void computeColors();
  void drawCoasts(TCODImage* img);
  EClimate getClimateFromTemp(float temp);
};