libtcod
Loading...
Searching...
No Matches
heightmap.h
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_H_
36#define TCOD_HEIGHTMAP_H_
37
38#include <stdint.h>
39
40#include "mersenne_types.h"
41#include "noise.h"
42#include "portability.h"
43#ifdef __cplusplus
44extern "C" {
45#endif
48
50typedef struct {
51 int w;
52 int h;
55 float* __restrict values;
57
61static inline bool TCOD_heightmap_is_valid(const TCOD_heightmap_t* heightmap) {
62 return heightmap && heightmap->values && heightmap->w >= 0 && heightmap->h >= 0;
63}
64
69static inline bool TCOD_heightmap_in_bounds(const TCOD_heightmap_t* heightmap, int x, int y) {
70 return TCOD_heightmap_is_valid(heightmap) && 0 <= x && 0 <= y && x < heightmap->w && y < heightmap->h;
71}
72
77static inline float TCOD_heightmap_get_value(const TCOD_heightmap_t* heightmap, int x, int y) {
78 if (!TCOD_heightmap_in_bounds(heightmap, x, y)) return 0.0;
79 return heightmap->values[y * heightmap->w + x];
80}
81
86static inline void TCOD_heightmap_set_value(TCOD_heightmap_t* heightmap, int x, int y, float value) {
87 if (TCOD_heightmap_in_bounds(heightmap, x, y)) heightmap->values[y * heightmap->w + x] = value;
88}
89
90TCODLIB_API TCOD_heightmap_t* TCOD_heightmap_new(int w, int h);
91TCODLIB_API void TCOD_heightmap_delete(TCOD_heightmap_t* hm);
92
93TCODLIB_API float TCOD_heightmap_get_interpolated_value(const TCOD_heightmap_t* hm, float x, float y);
94TCODLIB_API float TCOD_heightmap_get_slope(const TCOD_heightmap_t* hm, int x, int y);
95TCODLIB_API void TCOD_heightmap_get_normal(
96 const TCOD_heightmap_t* __restrict hm, float x, float y, float n[3], float waterLevel);
97TCODLIB_API int TCOD_heightmap_count_cells(const TCOD_heightmap_t* hm, float min, float max);
98TCODLIB_API bool TCOD_heightmap_has_land_on_border(const TCOD_heightmap_t* hm, float waterLevel);
99
107 const TCOD_heightmap_t* __restrict heightmap, float* __restrict min_out, float* __restrict max_out);
108
109TCODLIB_API void TCOD_heightmap_copy(
110 const TCOD_heightmap_t* __restrict hm_source, TCOD_heightmap_t* __restrict hm_dest);
111TCODLIB_API void TCOD_heightmap_add(TCOD_heightmap_t* hm, float value);
112TCODLIB_API void TCOD_heightmap_scale(TCOD_heightmap_t* hm, float value);
113TCODLIB_API void TCOD_heightmap_clamp(TCOD_heightmap_t* hm, float min, float max);
114TCODLIB_API void TCOD_heightmap_normalize(TCOD_heightmap_t* hm, float min, float max);
115TCODLIB_API void TCOD_heightmap_clear(TCOD_heightmap_t* __restrict hm);
116TCODLIB_API void TCOD_heightmap_lerp_hm(
117 const TCOD_heightmap_t* __restrict hm1,
118 const TCOD_heightmap_t* __restrict hm2,
119 TCOD_heightmap_t* __restrict out,
120 float coef);
121TCODLIB_API void TCOD_heightmap_add_hm(
122 const TCOD_heightmap_t* __restrict hm1, const TCOD_heightmap_t* __restrict hm2, TCOD_heightmap_t* __restrict out);
123TCODLIB_API void TCOD_heightmap_multiply_hm(
124 const TCOD_heightmap_t* __restrict hm1, const TCOD_heightmap_t* __restrict hm2, TCOD_heightmap_t* __restrict out);
125
126TCODLIB_API void TCOD_heightmap_add_hill(TCOD_heightmap_t* hm, float hx, float hy, float h_radius, float h_height);
127TCODLIB_API void TCOD_heightmap_dig_hill(TCOD_heightmap_t* hm, float hx, float hy, float h_radius, float h_height);
128TCODLIB_API void TCOD_heightmap_dig_bezier(
129 TCOD_heightmap_t* __restrict hm,
130 int px[4],
131 int py[4],
132 float startRadius,
133 float startDepth,
134 float endRadius,
135 float endDepth);
136TCODLIB_API void TCOD_heightmap_rain_erosion(
137 TCOD_heightmap_t* __restrict hm,
138 int nbDrops,
139 float erosionCoef,
140 float sedimentationCoef,
141 TCOD_Random* __restrict rnd);
142/* TCODLIB_API void TCOD_heightmap_heat_erosion(TCOD_heightmap_t *hm, int nbPass,float minSlope,float erosionCoef,float
143 * sedimentationCoef,TCOD_Random* rnd); */
144TCODLIB_API void TCOD_heightmap_kernel_transform(
145 TCOD_heightmap_t* __restrict hm,
146 int kernel_size,
147 const int* __restrict dx,
148 const int* __restrict dy,
149 const float* __restrict weight,
150 float minLevel,
151 float maxLevel);
167 const TCOD_heightmap_t* __restrict hm, uint8_t* __restrict mask, float minLevel, float maxLevel);
194 const TCOD_heightmap_t* __restrict hm_src,
195 TCOD_heightmap_t* __restrict hm_dst,
196 int kernel_size,
197 const int* __restrict dx,
198 const int* __restrict dy,
199 const float* __restrict weight,
200 const uint8_t* __restrict mask);
201TCODLIB_API void TCOD_heightmap_add_voronoi(
202 TCOD_heightmap_t* __restrict hm,
203 int nbPoints,
204 int nbCoef,
205 const float* __restrict coef,
206 TCOD_Random* __restrict rnd);
207TCODLIB_API void TCOD_heightmap_mid_point_displacement(
208 TCOD_heightmap_t* __restrict hm, TCOD_Random* __restrict rnd, float roughness);
209TCODLIB_API void TCOD_heightmap_add_fbm(
210 TCOD_heightmap_t* __restrict hm,
211 TCOD_Noise* __restrict noise,
212 float mul_x,
213 float mul_y,
214 float add_x,
215 float add_y,
216 float octaves,
217 float delta,
218 float scale);
219TCODLIB_API void TCOD_heightmap_scale_fbm(
220 TCOD_heightmap_t* __restrict hm,
221 TCOD_Noise* __restrict noise,
222 float mul_x,
223 float mul_y,
224 float add_x,
225 float add_y,
226 float octaves,
227 float delta,
228 float scale);
229TCOD_DEPRECATED("This function does nothing and will be removed.")
230TCODLIB_API void TCOD_heightmap_islandify(TCOD_heightmap_t* __restrict hm, float seaLevel, TCOD_Random* __restrict rnd);
232#ifdef __cplusplus
233} // extern "C"
234#endif
235#endif // TCOD_HEIGHTMAP_H_
static void TCOD_heightmap_set_value(TCOD_heightmap_t *heightmap, int x, int y, float value)
Set x,y on heightmap to value, out-of-bounds coordinates are silently ignored.
Definition heightmap.h:86
void TCOD_heightmap_kernel_transform_out(const TCOD_heightmap_t *hm_src, TCOD_heightmap_t *hm_dst, int kernel_size, const int *dx, const int *dy, const float *weight, const uint8_t *mask)
Apply a sparse kernel convolution from source to destination heightmap.
static bool TCOD_heightmap_is_valid(const TCOD_heightmap_t *heightmap)
Return true if heightmap is valid.
Definition heightmap.h:61
static float TCOD_heightmap_get_value(const TCOD_heightmap_t *heightmap, int x, int y)
Return the value at x,y from heightmap, or 0.0 for out-of-bounds coordinates.
Definition heightmap.h:77
void TCOD_heightmap_get_minmax(const TCOD_heightmap_t *heightmap, float *min_out, float *max_out)
Output the highest and lowest values in heightmap to min and max
static bool TCOD_heightmap_in_bounds(const TCOD_heightmap_t *heightmap, int x, int y)
Return true if x,y are valid and accessible coordinates for this heightmap
Definition heightmap.h:69
void TCOD_heightmap_threshold_mask(const TCOD_heightmap_t *hm, uint8_t *mask, float minLevel, float maxLevel)
Generate a mask from heightmap values within a threshold range.
Random number generator types.
Texture noise generator module.
Miscellaneous tools needed across platforms.
Definition noise.h:49
A contigious 2D array of float values.
Definition heightmap.h:50
float * values
Contigious 2D array of float values.
Definition heightmap.h:55
int w
Width of this heightmap.
Definition heightmap.h:51
int h
Height of this heightmap.
Definition heightmap.h:52
Pseudorandom number generator toolkit, all attributes are private.
Definition mersenne_types.h:87