libtcod
Loading...
Searching...
No Matches
heightmap.hpp
Go to the documentation of this file.
1/* BSD 3-Clause License
2 *
3 * Copyright © 2008-2026, Jice and the libtcod contributors.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * 3. Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
34#pragma once
35#ifndef TCOD_HEIGHTMAP_HPP_
36#define TCOD_HEIGHTMAP_HPP_
37
38#include <stddef.h>
39
40#include <vector>
41
42#include "heightmap.h"
43#include "noise.hpp"
46
47// clang-format off
56class TCODLIB_API TCODHeightMap {
57 public:
58 TCODHeightMap() = default;
59
83 TCODHeightMap(int width, int height) : w{width}, h{height}, values{new float[width * height]{}} {}
84
85 // clang-format on
86
87 TCODHeightMap(const TCODHeightMap& rhs) : TCODHeightMap{rhs.w, rhs.h} { *this = rhs; }
88 TCODHeightMap& operator=(const TCODHeightMap& rhs) {
89 if (w == rhs.w && h == rhs.h) {
90 for (ptrdiff_t i = 0; i < w * h; ++i) values[i] = rhs.values[i];
91 } else {
92 *this = TCODHeightMap(rhs);
93 }
94 return *this;
95 }
96 TCODHeightMap(TCODHeightMap&& rhs) { swap(rhs); }
97 TCODHeightMap& operator=(TCODHeightMap&& rhs) {
98 swap(rhs);
99 return *this;
100 }
101
102 ~TCODHeightMap() { delete[] values; }
103
104 void swap(TCODHeightMap& rhs) noexcept {
105 std::swap(w, rhs.w);
106 std::swap(h, rhs.h);
107 std::swap(values, rhs.values);
108 }
109 // clang-format off
128 inline void setValue(int x, int y, float v) {
129 values[w * y + x] = v;
130 }
131
142 void add(float f);
143
154 void scale(float f);
155
165 void clear(); // resets all values to 0.0
166
178 void clamp(float min, float max);
179
191 [[deprecated("Copy by value instead: 'x = y;'")]]
192 void copy(const TCODHeightMap *source) {
193 if (source) *this = *source;
194 }
195
210 void normalize(float newMin=0.0f, float newMax=1.0f); // scales the values to the range [newMin;newMax]
211
225 void lerp(const TCODHeightMap *a, const TCODHeightMap *b,float coef);
226
238 void add(const TCODHeightMap *a, const TCODHeightMap *b);
239
251 void multiply(const TCODHeightMap *a, const TCODHeightMap *b);
252
271 void addHill(float x, float y, float radius, float height); // adds a hill (half sphere) at given position
272
289 void digHill(float hx, float hy, float h_radius, float height);
290
305 void rainErosion(int nbDrops,float erosionCoef,float sedimentationCoef,TCODRandom *rnd);
306
336 void kernelTransform(int kernelSize, const int *dx, const int *dy, const float *weight, float minLevel,float maxLevel);
337
353 void addVoronoi(int nbPoints, int nbCoef, const float *coef,TCODRandom *rnd);
354
371 void addFbm(TCODNoise *noise,float mul_x, float mul_y, float add_x, float add_y, float octaves, float delta, float scale);
372
382 void scaleFbm(TCODNoise *noise,float mul_x, float mul_y, float add_x, float add_y, float octaves, float delta, float scale);
383
401 void digBezier(int px[4], int py[4], float startRadius, float startDepth, float endRadius, float endDepth);
402
419 inline float getValue(int x, int y) const {
420 return values[w * y + x];
421 }
422
436 float getInterpolatedValue(float x, float y) const;
437
451 float getSlope(int x, int y) const; // returns the slope in radian between 0 and PI/2
452
468 void getNormal(float x, float y,float n[3], float waterLevel=0.0f) const; // returns the surface normal or (0,0,1) if beyond water level.
469
481 int countCells(float min,float max) const;
482
494 bool hasLandOnBorder(float waterLevel) const;
495
507 void getMinMax(float *min, float *max) const;
508
509// void heatErosion(int nbPass,float minSlope,float erosionCoef,float sedimentationCoef,TCODRandom *rnd);
525 void midPointDisplacement(TCODRandom *rnd = NULL, float roughness=0.45f);
526 void islandify(float seaLevel,TCODRandom *rnd); // lowers the terrain near the heightmap borders
527 // TODO : checks island connectivity with floodfill
528
529 // clang-format on
530
531 int w{};
532 int h{};
533 float* values{};
534
535 private:
536};
537
538#endif // TCOD_HEIGHTMAP_HPP_
Definition heightmap.hpp:56
void scaleFbm(TCODNoise *noise, float mul_x, float mul_y, float add_x, float add_y, float octaves, float delta, float scale)
This function works exactly as the previous one, but it multiplies the resulting value instead of add...
float getSlope(int x, int y) const
This function returns the slope between 0 and PI/2 at given coordinates.
void addFbm(TCODNoise *noise, float mul_x, float mul_y, float add_x, float add_y, float octaves, float delta, float scale)
This function adds values from a simplex fbm function to the map.
float getInterpolatedValue(float x, float y) const
This function returns the interpolated height at non integer coordinates.
void addHill(float x, float y, float radius, float height)
This function adds a hill (a half spheroid) at given position.
void digBezier(int px[4], int py[4], float startRadius, float startDepth, float endRadius, float endDepth)
This function carve a path along a cubic Bezier curve using the digHill function.
void scale(float f)
void midPointDisplacement(TCODRandom *rnd=NULL, float roughness=0.45f)
This algorithm generates a realistic fractal heightmap using the diamond-square (or random midpoint d...
void lerp(const TCODHeightMap *a, const TCODHeightMap *b, float coef)
TCODHeightMap(int width, int height)
As with other modules, you have to create a heightmap object first : Note that whereas most other mod...
Definition heightmap.hpp:83
void setValue(int x, int y, float v)
Once the heightmap has been created, you can do some basic operations on the values inside it....
Definition heightmap.hpp:128
void rainErosion(int nbDrops, float erosionCoef, float sedimentationCoef, TCODRandom *rnd)
This function simulates the effect of rain drops on the terrain, resulting in erosion patterns.
bool hasLandOnBorder(float waterLevel) const
This function checks if the cells on the map border are below a certain height.
float getValue(int x, int y) const
This function returns the height value of a map cell.
Definition heightmap.hpp:419
void multiply(const TCODHeightMap *a, const TCODHeightMap *b)
void digHill(float hx, float hy, float h_radius, float height)
This function takes the highest value (if height > 0) or the lowest (if height < 0) between the map a...
void kernelTransform(int kernelSize, const int *dx, const int *dy, const float *weight, float minLevel, float maxLevel)
This function allows you to apply a generic transformation on the map, so that each resulting cell va...
void getMinMax(float *min, float *max) const
This function calculates the min and max of all values inside the map.
void getNormal(float x, float y, float n[3], float waterLevel=0.0f) const
This function returns the map normal at given coordinates.
void add(float f)
void addVoronoi(int nbPoints, int nbCoef, const float *coef, TCODRandom *rnd)
This function adds values from a Voronoi diagram to the map.
void clamp(float min, float max)
int countCells(float min, float max) const
This function returns the number of map cells which value is between min and max.
void add(const TCODHeightMap *a, const TCODHeightMap *b)
void copy(const TCODHeightMap *source)
Definition heightmap.hpp:192
void normalize(float newMin=0.0f, float newMax=1.0f)
void TCODHeightMap::normalize() void TCODHeightMap::normalize(float min) void TCODHeightMap::normaliz...
Usage example: 1D noise : the variation of a torch intensity 2D fbm : heightfield generation or cloud...
Definition noise.hpp:79
Definition mersenne.hpp:94
Terrain heightmap module.
Texture noise generator module.