2012-06-17 00:29:13 +02:00
|
|
|
/*
|
2013-02-24 18:40:43 +01:00
|
|
|
Minetest
|
2013-02-24 19:38:45 +01:00
|
|
|
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2012-06-17 00:29:13 +02:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2017-08-17 22:19:39 +02:00
|
|
|
#pragma once
|
2012-06-17 00:29:13 +02:00
|
|
|
|
2015-10-31 03:06:36 +01:00
|
|
|
#include "basic_macros.h"
|
2012-06-17 00:29:13 +02:00
|
|
|
#include "../irrlichttypes.h"
|
2012-06-17 03:00:31 +02:00
|
|
|
#include "../irr_v2d.h"
|
|
|
|
#include "../irr_v3d.h"
|
|
|
|
#include "../irr_aabb3d.h"
|
2012-06-17 00:29:13 +02:00
|
|
|
|
2015-07-06 18:53:30 +02:00
|
|
|
#define rangelim(d, min, max) ((d) < (min) ? (min) : ((d) > (max) ? (max) : (d)))
|
2015-10-16 18:52:24 +02:00
|
|
|
#define myfloor(x) ((x) < 0.0 ? (int)(x) - 1 : (int)(x))
|
2015-07-06 18:53:30 +02:00
|
|
|
// The naive swap performs better than the xor version
|
|
|
|
#define SWAP(t, x, y) do { \
|
|
|
|
t temp = x; \
|
|
|
|
x = y; \
|
|
|
|
y = temp; \
|
|
|
|
} while (0)
|
2015-02-15 17:30:38 +01:00
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
|
|
|
|
inline s16 getContainerPos(s16 p, s16 d)
|
|
|
|
{
|
2015-07-06 18:53:30 +02:00
|
|
|
return (p >= 0 ? p : p - d + 1) / d;
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline v2s16 getContainerPos(v2s16 p, s16 d)
|
|
|
|
{
|
|
|
|
return v2s16(
|
|
|
|
getContainerPos(p.X, d),
|
|
|
|
getContainerPos(p.Y, d)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline v3s16 getContainerPos(v3s16 p, s16 d)
|
|
|
|
{
|
|
|
|
return v3s16(
|
|
|
|
getContainerPos(p.X, d),
|
|
|
|
getContainerPos(p.Y, d),
|
|
|
|
getContainerPos(p.Z, d)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline v2s16 getContainerPos(v2s16 p, v2s16 d)
|
|
|
|
{
|
|
|
|
return v2s16(
|
|
|
|
getContainerPos(p.X, d.X),
|
|
|
|
getContainerPos(p.Y, d.Y)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline v3s16 getContainerPos(v3s16 p, v3s16 d)
|
|
|
|
{
|
|
|
|
return v3s16(
|
|
|
|
getContainerPos(p.X, d.X),
|
|
|
|
getContainerPos(p.Y, d.Y),
|
|
|
|
getContainerPos(p.Z, d.Z)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-01-13 15:19:54 +01:00
|
|
|
inline void getContainerPosWithOffset(s16 p, s16 d, s16 &container, s16 &offset)
|
|
|
|
{
|
|
|
|
container = (p >= 0 ? p : p - d + 1) / d;
|
|
|
|
offset = p & (d - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void getContainerPosWithOffset(const v2s16 &p, s16 d, v2s16 &container, v2s16 &offset)
|
|
|
|
{
|
|
|
|
getContainerPosWithOffset(p.X, d, container.X, offset.X);
|
|
|
|
getContainerPosWithOffset(p.Y, d, container.Y, offset.Y);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void getContainerPosWithOffset(const v3s16 &p, s16 d, v3s16 &container, v3s16 &offset)
|
|
|
|
{
|
|
|
|
getContainerPosWithOffset(p.X, d, container.X, offset.X);
|
|
|
|
getContainerPosWithOffset(p.Y, d, container.Y, offset.Y);
|
|
|
|
getContainerPosWithOffset(p.Z, d, container.Z, offset.Z);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
inline bool isInArea(v3s16 p, s16 d)
|
|
|
|
{
|
|
|
|
return (
|
|
|
|
p.X >= 0 && p.X < d &&
|
|
|
|
p.Y >= 0 && p.Y < d &&
|
|
|
|
p.Z >= 0 && p.Z < d
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isInArea(v2s16 p, s16 d)
|
|
|
|
{
|
|
|
|
return (
|
|
|
|
p.X >= 0 && p.X < d &&
|
|
|
|
p.Y >= 0 && p.Y < d
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isInArea(v3s16 p, v3s16 d)
|
|
|
|
{
|
|
|
|
return (
|
|
|
|
p.X >= 0 && p.X < d.X &&
|
|
|
|
p.Y >= 0 && p.Y < d.Y &&
|
|
|
|
p.Z >= 0 && p.Z < d.Z
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-06-22 06:29:44 +02:00
|
|
|
inline void sortBoxVerticies(v3s16 &p1, v3s16 &p2) {
|
|
|
|
if (p1.X > p2.X)
|
|
|
|
SWAP(s16, p1.X, p2.X);
|
|
|
|
if (p1.Y > p2.Y)
|
|
|
|
SWAP(s16, p1.Y, p2.Y);
|
|
|
|
if (p1.Z > p2.Z)
|
|
|
|
SWAP(s16, p1.Z, p2.Z);
|
|
|
|
}
|
|
|
|
|
2017-02-23 14:04:39 +01:00
|
|
|
inline v3s16 componentwise_min(const v3s16 &a, const v3s16 &b)
|
|
|
|
{
|
|
|
|
return v3s16(MYMIN(a.X, b.X), MYMIN(a.Y, b.Y), MYMIN(a.Z, b.Z));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline v3s16 componentwise_max(const v3s16 &a, const v3s16 &b)
|
|
|
|
{
|
|
|
|
return v3s16(MYMAX(a.X, b.X), MYMAX(a.Y, b.Y), MYMAX(a.Z, b.Z));
|
|
|
|
}
|
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
|
2015-02-23 07:25:14 +01:00
|
|
|
/** Returns \p f wrapped to the range [-360, 360]
|
|
|
|
*
|
|
|
|
* See test.cpp for example cases.
|
|
|
|
*
|
|
|
|
* \note This is also used in cases where degrees wrapped to the range [0, 360]
|
|
|
|
* is innapropriate (e.g. pitch needs negative values)
|
|
|
|
*
|
|
|
|
* \internal functionally equivalent -- although precision may vary slightly --
|
|
|
|
* to fmodf((f), 360.0f) however empirical tests indicate that this approach is
|
|
|
|
* faster.
|
|
|
|
*/
|
|
|
|
inline float modulo360f(float f)
|
|
|
|
{
|
|
|
|
int sign;
|
|
|
|
int whole;
|
|
|
|
float fraction;
|
|
|
|
|
|
|
|
if (f < 0) {
|
|
|
|
f = -f;
|
|
|
|
sign = -1;
|
|
|
|
} else {
|
|
|
|
sign = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
whole = f;
|
|
|
|
|
|
|
|
fraction = f - whole;
|
|
|
|
whole %= 360;
|
|
|
|
|
|
|
|
return sign * (whole + fraction);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Returns \p f wrapped to the range [0, 360]
|
|
|
|
*/
|
2012-06-17 00:29:13 +02:00
|
|
|
inline float wrapDegrees_0_360(float f)
|
|
|
|
{
|
2015-02-23 07:25:14 +01:00
|
|
|
float value = modulo360f(f);
|
|
|
|
return value < 0 ? value + 360 : value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Returns \p f wrapped to the range [-180, 180]
|
|
|
|
*/
|
2012-06-17 00:29:13 +02:00
|
|
|
inline float wrapDegrees_180(float f)
|
|
|
|
{
|
2015-02-23 07:25:14 +01:00
|
|
|
float value = modulo360f(f + 180);
|
|
|
|
if (value < 0)
|
|
|
|
value += 360;
|
|
|
|
return value - 180;
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Pseudo-random (VC++ rand() sucks)
|
|
|
|
*/
|
2015-03-22 05:01:46 +01:00
|
|
|
#define MYRAND_RANGE 0xffffffff
|
|
|
|
u32 myrand();
|
|
|
|
void mysrand(unsigned int seed);
|
|
|
|
void myrand_bytes(void *out, size_t len);
|
2012-06-17 00:29:13 +02:00
|
|
|
int myrand_range(int min, int max);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Miscellaneous functions
|
|
|
|
*/
|
|
|
|
|
2015-03-31 05:40:35 +02:00
|
|
|
inline u32 get_bits(u32 x, u32 pos, u32 len)
|
|
|
|
{
|
|
|
|
u32 mask = (1 << len) - 1;
|
|
|
|
return (x >> pos) & mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void set_bits(u32 *x, u32 pos, u32 len, u32 val)
|
|
|
|
{
|
|
|
|
u32 mask = (1 << len) - 1;
|
2015-04-01 05:30:44 +02:00
|
|
|
*x &= ~(mask << pos);
|
2015-03-31 05:40:35 +02:00
|
|
|
*x |= (val & mask) << pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline u32 calc_parity(u32 v)
|
|
|
|
{
|
|
|
|
v ^= v >> 16;
|
|
|
|
v ^= v >> 8;
|
|
|
|
v ^= v >> 4;
|
|
|
|
v &= 0xf;
|
|
|
|
return (0x6996 >> v) & 1;
|
|
|
|
}
|
|
|
|
|
2013-09-17 08:57:10 +02:00
|
|
|
u64 murmur_hash_64_ua(const void *key, int len, unsigned int seed);
|
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
|
|
|
|
f32 camera_fov, f32 range, f32 *distance_ptr=NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Returns nearest 32-bit integer for given floating point number.
|
|
|
|
<cmath> and <math.h> in VC++ don't provide round().
|
|
|
|
*/
|
|
|
|
inline s32 myround(f32 f)
|
|
|
|
{
|
2015-04-29 19:28:25 +02:00
|
|
|
return (s32)(f < 0.f ? (f - 0.5f) : (f + 0.5f));
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Returns integer position of node in given floating point position
|
|
|
|
*/
|
|
|
|
inline v3s16 floatToInt(v3f p, f32 d)
|
|
|
|
{
|
2015-07-06 18:53:30 +02:00
|
|
|
return v3s16(
|
|
|
|
(p.X + (p.X > 0 ? d / 2 : -d / 2)) / d,
|
|
|
|
(p.Y + (p.Y > 0 ? d / 2 : -d / 2)) / d,
|
|
|
|
(p.Z + (p.Z > 0 ? d / 2 : -d / 2)) / d);
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Returns floating point position of node in given integer position
|
|
|
|
*/
|
|
|
|
inline v3f intToFloat(v3s16 p, f32 d)
|
|
|
|
{
|
2015-07-06 18:53:30 +02:00
|
|
|
return v3f(
|
2012-06-17 00:29:13 +02:00
|
|
|
(f32)p.X * d,
|
|
|
|
(f32)p.Y * d,
|
|
|
|
(f32)p.Z * d
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Random helper. Usually d=BS
|
2016-02-11 15:21:21 +01:00
|
|
|
inline aabb3f getNodeBox(v3s16 p, float d)
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2016-02-11 15:21:21 +01:00
|
|
|
return aabb3f(
|
2015-07-06 18:53:30 +02:00
|
|
|
(float)p.X * d - 0.5 * d,
|
|
|
|
(float)p.Y * d - 0.5 * d,
|
|
|
|
(float)p.Z * d - 0.5 * d,
|
|
|
|
(float)p.X * d + 0.5 * d,
|
|
|
|
(float)p.Y * d + 0.5 * d,
|
|
|
|
(float)p.Z * d + 0.5 * d
|
2012-06-17 00:29:13 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-07-06 18:53:30 +02:00
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
class IntervalLimiter
|
|
|
|
{
|
|
|
|
public:
|
2017-06-19 23:54:58 +02:00
|
|
|
IntervalLimiter() {}
|
2012-06-17 00:29:13 +02:00
|
|
|
/*
|
|
|
|
dtime: time from last call to this method
|
|
|
|
wanted_interval: interval wanted
|
|
|
|
return value:
|
|
|
|
true: action should be skipped
|
|
|
|
false: action should be done
|
|
|
|
*/
|
|
|
|
bool step(float dtime, float wanted_interval)
|
|
|
|
{
|
|
|
|
m_accumulator += dtime;
|
2015-07-06 18:53:30 +02:00
|
|
|
if (m_accumulator < wanted_interval)
|
2012-06-17 00:29:13 +02:00
|
|
|
return false;
|
|
|
|
m_accumulator -= wanted_interval;
|
|
|
|
return true;
|
|
|
|
}
|
2015-07-06 18:53:30 +02:00
|
|
|
|
|
|
|
private:
|
2017-06-19 23:54:58 +02:00
|
|
|
float m_accumulator = 0.0f;
|
2012-06-17 00:29:13 +02:00
|
|
|
};
|
|
|
|
|
2015-07-06 18:53:30 +02:00
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
/*
|
|
|
|
Splits a list into "pages". For example, the list [1,2,3,4,5] split
|
|
|
|
into two pages would be [1,2,3],[4,5]. This function computes the
|
|
|
|
minimum and maximum indices of a single page.
|
|
|
|
|
|
|
|
length: Length of the list that should be split
|
|
|
|
page: Page number, 1 <= page <= pagecount
|
|
|
|
pagecount: The number of pages, >= 1
|
|
|
|
minindex: Receives the minimum index (inclusive).
|
|
|
|
maxindex: Receives the maximum index (exclusive).
|
|
|
|
|
|
|
|
Ensures 0 <= minindex <= maxindex <= length.
|
|
|
|
*/
|
|
|
|
inline void paging(u32 length, u32 page, u32 pagecount, u32 &minindex, u32 &maxindex)
|
|
|
|
{
|
2015-07-06 18:53:30 +02:00
|
|
|
if (length < 1 || pagecount < 1 || page < 1 || page > pagecount) {
|
2012-06-17 00:29:13 +02:00
|
|
|
// Special cases or invalid parameters
|
|
|
|
minindex = maxindex = 0;
|
2015-07-06 18:53:30 +02:00
|
|
|
} else if(pagecount <= length) {
|
2012-06-17 00:29:13 +02:00
|
|
|
// Less pages than entries in the list:
|
|
|
|
// Each page contains at least one entry
|
|
|
|
minindex = (length * (page-1) + (pagecount-1)) / pagecount;
|
|
|
|
maxindex = (length * page + (pagecount-1)) / pagecount;
|
2015-07-06 18:53:30 +02:00
|
|
|
} else {
|
2012-06-17 00:29:13 +02:00
|
|
|
// More pages than entries in the list:
|
|
|
|
// Make sure the empty pages are at the end
|
2015-07-06 18:53:30 +02:00
|
|
|
if (page < length) {
|
2012-06-17 00:29:13 +02:00
|
|
|
minindex = page-1;
|
|
|
|
maxindex = page;
|
2015-07-06 18:53:30 +02:00
|
|
|
} else {
|
2012-06-17 00:29:13 +02:00
|
|
|
minindex = 0;
|
|
|
|
maxindex = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-27 20:34:30 +02:00
|
|
|
inline float cycle_shift(float value, float by = 0, float max = 1)
|
|
|
|
{
|
2015-07-06 18:53:30 +02:00
|
|
|
if (value + by < 0) return value + by + max;
|
2013-07-27 20:34:30 +02:00
|
|
|
if (value + by > max) return value + by - max;
|
|
|
|
return value + by;
|
|
|
|
}
|
|
|
|
|
2014-11-02 03:47:43 +01:00
|
|
|
inline bool is_power_of_two(u32 n)
|
|
|
|
{
|
2015-07-06 18:53:30 +02:00
|
|
|
return n != 0 && (n & (n - 1)) == 0;
|
2014-11-02 03:47:43 +01:00
|
|
|
}
|
|
|
|
|
2015-03-09 14:32:11 +01:00
|
|
|
// Compute next-higher power of 2 efficiently, e.g. for power-of-2 texture sizes.
|
|
|
|
// Public Domain: https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
|
|
|
|
inline u32 npot2(u32 orig) {
|
|
|
|
orig--;
|
|
|
|
orig |= orig >> 1;
|
|
|
|
orig |= orig >> 2;
|
|
|
|
orig |= orig >> 4;
|
|
|
|
orig |= orig >> 8;
|
|
|
|
orig |= orig >> 16;
|
|
|
|
return orig + 1;
|
|
|
|
}
|