3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "basic_macros.h"
23 #include "constants.h"
24 #include "irrlichttypes.h"
27 #include "irr_aabb3d.h"
31 #define rangelim(d, min, max) ((d) < (min) ? (min) : ((d) > (max) ? (max) : (d)))
32 #define myfloor(x) ((x) < 0.0 ? (int)(x) - 1 : (int)(x))
33 // The naive swap performs better than the xor version
34 #define SWAP(t, x, y) do { \
40 // Maximum radius of a block. The magic number is
41 // sqrt(3.0) / 2.0 in literal form.
42 static constexpr const f32 BLOCK_MAX_RADIUS = 0.866025403784f * MAP_BLOCKSIZE * BS;
44 inline s16 getContainerPos(s16 p, s16 d)
46 return (p >= 0 ? p : p - d + 1) / d;
49 inline v2s16 getContainerPos(v2s16 p, s16 d)
52 getContainerPos(p.X, d),
53 getContainerPos(p.Y, d)
57 inline v3s16 getContainerPos(v3s16 p, s16 d)
60 getContainerPos(p.X, d),
61 getContainerPos(p.Y, d),
62 getContainerPos(p.Z, d)
66 inline v2s16 getContainerPos(v2s16 p, v2s16 d)
69 getContainerPos(p.X, d.X),
70 getContainerPos(p.Y, d.Y)
74 inline v3s16 getContainerPos(v3s16 p, v3s16 d)
77 getContainerPos(p.X, d.X),
78 getContainerPos(p.Y, d.Y),
79 getContainerPos(p.Z, d.Z)
83 inline void getContainerPosWithOffset(s16 p, s16 d, s16 &container, s16 &offset)
85 container = (p >= 0 ? p : p - d + 1) / d;
89 inline void getContainerPosWithOffset(const v2s16 &p, s16 d, v2s16 &container, v2s16 &offset)
91 getContainerPosWithOffset(p.X, d, container.X, offset.X);
92 getContainerPosWithOffset(p.Y, d, container.Y, offset.Y);
95 inline void getContainerPosWithOffset(const v3s16 &p, s16 d, v3s16 &container, v3s16 &offset)
97 getContainerPosWithOffset(p.X, d, container.X, offset.X);
98 getContainerPosWithOffset(p.Y, d, container.Y, offset.Y);
99 getContainerPosWithOffset(p.Z, d, container.Z, offset.Z);
103 inline bool isInArea(v3s16 p, s16 d)
106 p.X >= 0 && p.X < d &&
107 p.Y >= 0 && p.Y < d &&
112 inline bool isInArea(v2s16 p, s16 d)
115 p.X >= 0 && p.X < d &&
120 inline bool isInArea(v3s16 p, v3s16 d)
123 p.X >= 0 && p.X < d.X &&
124 p.Y >= 0 && p.Y < d.Y &&
125 p.Z >= 0 && p.Z < d.Z
129 inline void sortBoxVerticies(v3s16 &p1, v3s16 &p2) {
131 SWAP(s16, p1.X, p2.X);
133 SWAP(s16, p1.Y, p2.Y);
135 SWAP(s16, p1.Z, p2.Z);
138 inline v3s16 componentwise_min(const v3s16 &a, const v3s16 &b)
140 return v3s16(MYMIN(a.X, b.X), MYMIN(a.Y, b.Y), MYMIN(a.Z, b.Z));
143 inline v3s16 componentwise_max(const v3s16 &a, const v3s16 &b)
145 return v3s16(MYMAX(a.X, b.X), MYMAX(a.Y, b.Y), MYMAX(a.Z, b.Z));
149 /** Returns \p f wrapped to the range [-360, 360]
151 * See test.cpp for example cases.
153 * \note This is also used in cases where degrees wrapped to the range [0, 360]
154 * is innapropriate (e.g. pitch needs negative values)
156 * \internal functionally equivalent -- although precision may vary slightly --
157 * to fmodf((f), 360.0f) however empirical tests indicate that this approach is
160 inline float modulo360f(float f)
175 fraction = f - whole;
178 return sign * (whole + fraction);
182 /** Returns \p f wrapped to the range [0, 360]
184 inline float wrapDegrees_0_360(float f)
186 float value = modulo360f(f);
187 return value < 0 ? value + 360 : value;
191 /** Returns \p v3f wrapped to the range [0, 360]
193 inline v3f wrapDegrees_0_360_v3f(v3f v)
196 value_v3f.X = modulo360f(v.X);
197 value_v3f.Y = modulo360f(v.Y);
198 value_v3f.Z = modulo360f(v.Z);
200 // Now that values are wrapped, use to get values for certain ranges
201 value_v3f.X = value_v3f.X < 0 ? value_v3f.X + 360 : value_v3f.X;
202 value_v3f.Y = value_v3f.Y < 0 ? value_v3f.Y + 360 : value_v3f.Y;
203 value_v3f.Z = value_v3f.Z < 0 ? value_v3f.Z + 360 : value_v3f.Z;
208 /** Returns \p f wrapped to the range [-180, 180]
210 inline float wrapDegrees_180(float f)
212 float value = modulo360f(f + 180);
219 Pseudo-random (VC++ rand() sucks)
221 #define MYRAND_RANGE 0xffffffff
223 void mysrand(unsigned int seed);
224 void myrand_bytes(void *out, size_t len);
225 int myrand_range(int min, int max);
228 Miscellaneous functions
231 inline u32 get_bits(u32 x, u32 pos, u32 len)
233 u32 mask = (1 << len) - 1;
234 return (x >> pos) & mask;
237 inline void set_bits(u32 *x, u32 pos, u32 len, u32 val)
239 u32 mask = (1 << len) - 1;
240 *x &= ~(mask << pos);
241 *x |= (val & mask) << pos;
244 inline u32 calc_parity(u32 v)
250 return (0x6996 >> v) & 1;
253 u64 murmur_hash_64_ua(const void *key, int len, unsigned int seed);
255 bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
256 f32 camera_fov, f32 range, f32 *distance_ptr=NULL);
258 s16 adjustDist(s16 dist, float zoom_fov);
261 Returns nearest 32-bit integer for given floating point number.
262 <cmath> and <math.h> in VC++ don't provide round().
264 inline s32 myround(f32 f)
266 return (s32)(f < 0.f ? (f - 0.5f) : (f + 0.5f));
269 inline constexpr f32 sqr(f32 f)
275 Returns integer position of node in given floating point position
277 inline v3s16 floatToInt(v3f p, f32 d)
280 (p.X + (p.X > 0 ? d / 2 : -d / 2)) / d,
281 (p.Y + (p.Y > 0 ? d / 2 : -d / 2)) / d,
282 (p.Z + (p.Z > 0 ? d / 2 : -d / 2)) / d);
286 Returns integer position of node in given double precision position
288 inline v3s16 doubleToInt(v3d p, double d)
291 (p.X + (p.X > 0 ? d / 2 : -d / 2)) / d,
292 (p.Y + (p.Y > 0 ? d / 2 : -d / 2)) / d,
293 (p.Z + (p.Z > 0 ? d / 2 : -d / 2)) / d);
297 Returns floating point position of node in given integer position
299 inline v3f intToFloat(v3s16 p, f32 d)
308 // Random helper. Usually d=BS
309 inline aabb3f getNodeBox(v3s16 p, float d)
312 (float)p.X * d - 0.5f * d,
313 (float)p.Y * d - 0.5f * d,
314 (float)p.Z * d - 0.5f * d,
315 (float)p.X * d + 0.5f * d,
316 (float)p.Y * d + 0.5f * d,
317 (float)p.Z * d + 0.5f * d
322 class IntervalLimiter
325 IntervalLimiter() = default;
328 dtime: time from last call to this method
329 wanted_interval: interval wanted
331 true: action should be skipped
332 false: action should be done
334 bool step(float dtime, float wanted_interval)
336 m_accumulator += dtime;
337 if (m_accumulator < wanted_interval)
339 m_accumulator -= wanted_interval;
344 float m_accumulator = 0.0f;
349 Splits a list into "pages". For example, the list [1,2,3,4,5] split
350 into two pages would be [1,2,3],[4,5]. This function computes the
351 minimum and maximum indices of a single page.
353 length: Length of the list that should be split
354 page: Page number, 1 <= page <= pagecount
355 pagecount: The number of pages, >= 1
356 minindex: Receives the minimum index (inclusive).
357 maxindex: Receives the maximum index (exclusive).
359 Ensures 0 <= minindex <= maxindex <= length.
361 inline void paging(u32 length, u32 page, u32 pagecount, u32 &minindex, u32 &maxindex)
363 if (length < 1 || pagecount < 1 || page < 1 || page > pagecount) {
364 // Special cases or invalid parameters
365 minindex = maxindex = 0;
366 } else if(pagecount <= length) {
367 // Less pages than entries in the list:
368 // Each page contains at least one entry
369 minindex = (length * (page-1) + (pagecount-1)) / pagecount;
370 maxindex = (length * page + (pagecount-1)) / pagecount;
372 // More pages than entries in the list:
373 // Make sure the empty pages are at the end
384 inline float cycle_shift(float value, float by = 0, float max = 1)
386 if (value + by < 0) return value + by + max;
387 if (value + by > max) return value + by - max;
391 inline bool is_power_of_two(u32 n)
393 return n != 0 && (n & (n - 1)) == 0;
396 // Compute next-higher power of 2 efficiently, e.g. for power-of-2 texture sizes.
397 // Public Domain: https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
398 inline u32 npot2(u32 orig) {
408 // Gradual steps towards the target value in a wrapped (circular) system
409 // using the shorter of both ways
411 inline void wrappedApproachShortest(T ¤t, const T target, const T stepsize,
414 T delta = target - current;
418 if (delta > stepsize && maximum - delta > stepsize) {
419 current += (delta < maximum / 2) ? stepsize : -stepsize;
420 if (current >= maximum)
427 void setPitchYawRollRad(core::matrix4 &m, const v3f &rot);
429 inline void setPitchYawRoll(core::matrix4 &m, const v3f &rot)
431 setPitchYawRollRad(m, rot * core::DEGTORAD64);
434 v3f getPitchYawRollRad(const core::matrix4 &m);
436 inline v3f getPitchYawRoll(const core::matrix4 &m)
438 return getPitchYawRollRad(m) * core::RADTODEG64;
441 // Muliply the RGB value of a color linearly, and clamp to black/white
442 inline irr::video::SColor multiplyColorValue(const irr::video::SColor &color, float mod)
444 return irr::video::SColor(color.getAlpha(),
445 core::clamp<u32>(color.getRed() * mod, 0, 255),
446 core::clamp<u32>(color.getGreen() * mod, 0, 255),
447 core::clamp<u32>(color.getBlue() * mod, 0, 255));