]> git.lizzy.rs Git - minetest.git/blobdiff - src/util/numeric.h
Allow rotating entity selectionboxes (#12379)
[minetest.git] / src / util / numeric.h
index bfdff84a0e878101fc066576a17b2ac0c77031e0..265046a63243c4769260e585d18df57ef627c2af 100644 (file)
@@ -20,10 +20,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #pragma once
 
 #include "basic_macros.h"
+#include "constants.h"
 #include "irrlichttypes.h"
 #include "irr_v2d.h"
 #include "irr_v3d.h"
 #include "irr_aabb3d.h"
+#include "SColor.h"
+#include <matrix4.h>
 
 #define rangelim(d, min, max) ((d) < (min) ? (min) : ((d) > (max) ? (max) : (d)))
 #define myfloor(x) ((x) < 0.0 ? (int)(x) - 1 : (int)(x))
@@ -34,6 +37,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
        y = temp; \
 } while (0)
 
+// Maximum radius of a block.  The magic number is
+// sqrt(3.0) / 2.0 in literal form.
+static constexpr const f32 BLOCK_MAX_RADIUS = 0.866025403784f * MAP_BLOCKSIZE * BS;
 
 inline s16 getContainerPos(s16 p, s16 d)
 {
@@ -217,6 +223,8 @@ u32 myrand();
 void mysrand(unsigned int seed);
 void myrand_bytes(void *out, size_t len);
 int myrand_range(int min, int max);
+float myrand_range(float min, float max);
+float myrand_float();
 
 /*
        Miscellaneous functions
@@ -417,3 +425,47 @@ inline void wrappedApproachShortest(T &current, const T target, const T stepsize
                current = target;
        }
 }
+
+void setPitchYawRollRad(core::matrix4 &m, const v3f &rot);
+
+inline void setPitchYawRoll(core::matrix4 &m, const v3f &rot)
+{
+       setPitchYawRollRad(m, rot * core::DEGTORAD64);
+}
+
+v3f getPitchYawRollRad(const core::matrix4 &m);
+
+inline v3f getPitchYawRoll(const core::matrix4 &m)
+{
+       return getPitchYawRollRad(m) * core::RADTODEG64;
+}
+
+// Muliply the RGB value of a color linearly, and clamp to black/white
+inline irr::video::SColor multiplyColorValue(const irr::video::SColor &color, float mod)
+{
+       return irr::video::SColor(color.getAlpha(),
+                       core::clamp<u32>(color.getRed() * mod, 0, 255),
+                       core::clamp<u32>(color.getGreen() * mod, 0, 255),
+                       core::clamp<u32>(color.getBlue() * mod, 0, 255));
+}
+
+template <typename T> inline T numericAbsolute(T v) { return v < 0 ? T(-v) : v;                }
+template <typename T> inline T numericSign(T v)     { return T(v < 0 ? -1 : (v == 0 ? 0 : 1)); }
+
+inline v3f vecAbsolute(v3f v)
+{
+       return v3f(
+               numericAbsolute(v.X),
+               numericAbsolute(v.Y),
+               numericAbsolute(v.Z)
+       );
+}
+
+inline v3f vecSign(v3f v)
+{
+       return v3f(
+               numericSign(v.X),
+               numericSign(v.Y),
+               numericSign(v.Z)
+       );
+}