summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-rw-r--r--src/util/basic_macros.h12
-rw-r--r--src/util/cpp11.h32
-rw-r--r--src/util/cpp11_container.h16
-rw-r--r--src/util/directiontables.h56
-rw-r--r--src/util/hex.h13
-rw-r--r--src/util/mathconstants.h7
-rw-r--r--src/util/numeric.cpp94
-rw-r--r--src/util/numeric.h104
-rw-r--r--src/util/pointedthing.cpp75
-rw-r--r--src/util/pointedthing.h40
-rw-r--r--src/util/serialize.cpp49
-rw-r--r--src/util/serialize.h7
-rw-r--r--src/util/sha1.h33
-rw-r--r--src/util/sha2.h107
-rw-r--r--src/util/sha256.c5
-rw-r--r--src/util/srp.cpp2
-rw-r--r--src/util/string.cpp8
-rw-r--r--src/util/string.h38
-rw-r--r--src/util/thread.h21
-rw-r--r--src/util/timetaker.cpp27
-rw-r--r--src/util/timetaker.h14
21 files changed, 427 insertions, 333 deletions
diff --git a/src/util/basic_macros.h b/src/util/basic_macros.h
index c100b4f25..687d7cf85 100644
--- a/src/util/basic_macros.h
+++ b/src/util/basic_macros.h
@@ -20,14 +20,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#ifndef BASICMACROS_HEADER
#define BASICMACROS_HEADER
-#include <algorithm>
-
#define ARRLEN(x) (sizeof(x) / sizeof((x)[0]))
#define MYMIN(a, b) ((a) < (b) ? (a) : (b))
#define MYMAX(a, b) ((a) > (b) ? (a) : (b))
+// Requires <algorithm>
#define CONTAINS(c, v) (std::find((c).begin(), (c).end(), (v)) != (c).end())
// To disable copy constructors and assignment operations for some class
@@ -50,4 +49,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define STATIC_ASSERT(expr, msg) \
UNUSED_ATTRIBUTE typedef char msg[!!(expr) * 2 - 1]
+// Macros to facilitate writing position vectors to a stream
+// Usage:
+// v3s16 pos(1,2,3);
+// mystream << "message " << PP(pos) << std::endl;
+
+#define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
+
+#define PP2(x) "("<<(x).X<<","<<(x).Y<<")"
+
#endif
diff --git a/src/util/cpp11.h b/src/util/cpp11.h
new file mode 100644
index 000000000..14913cb86
--- /dev/null
+++ b/src/util/cpp11.h
@@ -0,0 +1,32 @@
+/*
+Minetest
+Copyright (C) 2016 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
+
+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.
+*/
+
+#ifndef MT_CPP11_HEADER
+#define MT_CPP11_HEADER
+
+#if __cplusplus < 201103L || _MSC_VER < 1600
+#define USE_CPP11_FAKE_KEYWORD
+#endif
+
+#ifdef USE_CPP11_FAKE_KEYWORD
+#define constexpr const
+#define nullptr NULL
+#endif
+
+#endif
diff --git a/src/util/cpp11_container.h b/src/util/cpp11_container.h
index 88317c9c4..0194385fc 100644
--- a/src/util/cpp11_container.h
+++ b/src/util/cpp11_container.h
@@ -29,15 +29,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#endif
#ifdef USE_UNORDERED_CONTAINERS
- #include <unordered_map>
- #include <unordered_set>
- #define UNORDERED_MAP std::unordered_map
- #define UNORDERED_SET std::unordered_set
+#include <unordered_map>
+#include <unordered_set>
+#define UNORDERED_MAP std::unordered_map
+#define UNORDERED_SET std::unordered_set
#else
- #include <map>
- #include <set>
- #define UNORDERED_MAP std::map
- #define UNORDERED_SET std::set
+#include <map>
+#include <set>
+#define UNORDERED_MAP std::map
+#define UNORDERED_SET std::set
#endif
#endif
diff --git a/src/util/directiontables.h b/src/util/directiontables.h
index 0dd3aab09..3cfe0fb3e 100644
--- a/src/util/directiontables.h
+++ b/src/util/directiontables.h
@@ -30,5 +30,59 @@ extern const v3s16 g_26dirs[26];
// 26th is (0,0,0)
extern const v3s16 g_27dirs[27];
-#endif
+/// Direction in the 6D format. g_27dirs contains corresponding vectors.
+/// Here P means Positive, N stands for Negative.
+enum Direction6D {
+// 0
+ D6D_ZP,
+ D6D_YP,
+ D6D_XP,
+ D6D_ZN,
+ D6D_YN,
+ D6D_XN,
+// 6
+ D6D_XN_YP,
+ D6D_XP_YP,
+ D6D_YP_ZP,
+ D6D_YP_ZN,
+ D6D_XN_ZP,
+ D6D_XP_ZP,
+ D6D_XN_ZN,
+ D6D_XP_ZN,
+ D6D_XN_YN,
+ D6D_XP_YN,
+ D6D_YN_ZP,
+ D6D_YN_ZN,
+// 18
+ D6D_XN_YP_ZP,
+ D6D_XP_YP_ZP,
+ D6D_XN_YP_ZN,
+ D6D_XP_YP_ZN,
+ D6D_XN_YN_ZP,
+ D6D_XP_YN_ZP,
+ D6D_XN_YN_ZN,
+ D6D_XP_YN_ZN,
+// 26
+ D6D,
+
+// aliases
+ D6D_BACK = D6D_ZP,
+ D6D_TOP = D6D_YP,
+ D6D_RIGHT = D6D_XP,
+ D6D_FRONT = D6D_ZN,
+ D6D_BOTTOM = D6D_YN,
+ D6D_LEFT = D6D_XN,
+};
+/// Direction in the wallmounted format.
+/// P is Positive, N is Negative.
+enum DirectionWallmounted {
+ DWM_YP,
+ DWM_YN,
+ DWM_XP,
+ DWM_XN,
+ DWM_ZP,
+ DWM_ZN,
+};
+
+#endif
diff --git a/src/util/hex.h b/src/util/hex.h
index 6f00a79bf..c205d01da 100644
--- a/src/util/hex.h
+++ b/src/util/hex.h
@@ -30,9 +30,8 @@ static inline std::string hex_encode(const char *data, unsigned int data_size)
char buf2[3];
buf2[2] = '\0';
- for(unsigned int i = 0; i < data_size; i++)
- {
- unsigned char c = (unsigned char) data[i];
+ for (unsigned int i = 0; i < data_size; i++) {
+ unsigned char c = (unsigned char)data[i];
buf2[0] = hex_chars[(c & 0xf0) >> 4];
buf2[1] = hex_chars[c & 0x0f];
ret.append(buf2);
@@ -43,16 +42,16 @@ static inline std::string hex_encode(const char *data, unsigned int data_size)
static inline std::string hex_encode(const std::string &data)
{
- return hex_encode(data.c_str(), data.size());
+ return hex_encode(data.c_str(), data.size());
}
static inline bool hex_digit_decode(char hexdigit, unsigned char &value)
{
- if(hexdigit >= '0' && hexdigit <= '9')
+ if (hexdigit >= '0' && hexdigit <= '9')
value = hexdigit - '0';
- else if(hexdigit >= 'A' && hexdigit <= 'F')
+ else if (hexdigit >= 'A' && hexdigit <= 'F')
value = hexdigit - 'A' + 10;
- else if(hexdigit >= 'a' && hexdigit <= 'f')
+ else if (hexdigit >= 'a' && hexdigit <= 'f')
value = hexdigit - 'a' + 10;
else
return false;
diff --git a/src/util/mathconstants.h b/src/util/mathconstants.h
deleted file mode 100644
index 1b478aa95..000000000
--- a/src/util/mathconstants.h
+++ /dev/null
@@ -1,7 +0,0 @@
-#include <math.h>
-
-// MSVC doesn't seem to define this
-#ifndef M_PI
- #define M_PI 3.1415926535
-#endif
-
diff --git a/src/util/numeric.cpp b/src/util/numeric.cpp
index a9e7ae584..e6a9cb75e 100644
--- a/src/util/numeric.cpp
+++ b/src/util/numeric.cpp
@@ -18,107 +18,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/
#include "numeric.h"
-#include "mathconstants.h"
#include "log.h"
#include "../constants.h" // BS, MAP_BLOCKSIZE
#include "../noise.h" // PseudoRandom, PcgRandom
#include "../threading/mutex_auto_lock.h"
#include <string.h>
-#include <iostream>
-UNORDERED_MAP<u16, std::vector<v3s16> > FacePositionCache::m_cache;
-Mutex FacePositionCache::m_cache_mutex;
-// Calculate the borders of a "d-radius" cube
-// TODO: Make it work without mutex and data races, probably thread-local
-std::vector<v3s16> FacePositionCache::getFacePositions(u16 d)
-{
- MutexAutoLock cachelock(m_cache_mutex);
- if (m_cache.find(d) != m_cache.end())
- return m_cache[d];
-
- generateFacePosition(d);
- return m_cache[d];
-
-}
-
-void FacePositionCache::generateFacePosition(u16 d)
-{
- m_cache[d] = std::vector<v3s16>();
- if(d == 0) {
- m_cache[d].push_back(v3s16(0,0,0));
- return;
- }
- if(d == 1) {
- /*
- This is an optimized sequence of coordinates.
- */
- m_cache[d].push_back(v3s16( 0, 1, 0)); // top
- m_cache[d].push_back(v3s16( 0, 0, 1)); // back
- m_cache[d].push_back(v3s16(-1, 0, 0)); // left
- m_cache[d].push_back(v3s16( 1, 0, 0)); // right
- m_cache[d].push_back(v3s16( 0, 0,-1)); // front
- m_cache[d].push_back(v3s16( 0,-1, 0)); // bottom
- // 6
- m_cache[d].push_back(v3s16(-1, 0, 1)); // back left
- m_cache[d].push_back(v3s16( 1, 0, 1)); // back right
- m_cache[d].push_back(v3s16(-1, 0,-1)); // front left
- m_cache[d].push_back(v3s16( 1, 0,-1)); // front right
- m_cache[d].push_back(v3s16(-1,-1, 0)); // bottom left
- m_cache[d].push_back(v3s16( 1,-1, 0)); // bottom right
- m_cache[d].push_back(v3s16( 0,-1, 1)); // bottom back
- m_cache[d].push_back(v3s16( 0,-1,-1)); // bottom front
- m_cache[d].push_back(v3s16(-1, 1, 0)); // top left
- m_cache[d].push_back(v3s16( 1, 1, 0)); // top right
- m_cache[d].push_back(v3s16( 0, 1, 1)); // top back
- m_cache[d].push_back(v3s16( 0, 1,-1)); // top front
- // 18
- m_cache[d].push_back(v3s16(-1, 1, 1)); // top back-left
- m_cache[d].push_back(v3s16( 1, 1, 1)); // top back-right
- m_cache[d].push_back(v3s16(-1, 1,-1)); // top front-left
- m_cache[d].push_back(v3s16( 1, 1,-1)); // top front-right
- m_cache[d].push_back(v3s16(-1,-1, 1)); // bottom back-left
- m_cache[d].push_back(v3s16( 1,-1, 1)); // bottom back-right
- m_cache[d].push_back(v3s16(-1,-1,-1)); // bottom front-left
- m_cache[d].push_back(v3s16( 1,-1,-1)); // bottom front-right
- // 26
- return;
- }
- // Take blocks in all sides, starting from y=0 and going +-y
- for(s16 y=0; y<=d-1; y++) {
- // Left and right side, including borders
- for(s16 z=-d; z<=d; z++) {
- m_cache[d].push_back(v3s16(d,y,z));
- m_cache[d].push_back(v3s16(-d,y,z));
- if(y != 0) {
- m_cache[d].push_back(v3s16(d,-y,z));
- m_cache[d].push_back(v3s16(-d,-y,z));
- }
- }
- // Back and front side, excluding borders
- for(s16 x=-d+1; x<=d-1; x++) {
- m_cache[d].push_back(v3s16(x,y,d));
- m_cache[d].push_back(v3s16(x,y,-d));
- if(y != 0) {
- m_cache[d].push_back(v3s16(x,-y,d));
- m_cache[d].push_back(v3s16(x,-y,-d));
- }
- }
- }
-
- // Take the bottom and top face with borders
- // -d<x<d, y=+-d, -d<z<d
- for(s16 x=-d; x<=d; x++)
- for(s16 z=-d; z<=d; z++) {
- m_cache[d].push_back(v3s16(x,-d,z));
- m_cache[d].push_back(v3s16(x,d,z));
- }
-}
-
-/*
- myrand
-*/
+// myrand
PcgRandom g_pcgrand;
diff --git a/src/util/numeric.h b/src/util/numeric.h
index 4cdc254c3..4a27f657d 100644
--- a/src/util/numeric.h
+++ b/src/util/numeric.h
@@ -26,28 +26,20 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "../irr_v3d.h"
#include "../irr_aabb3d.h"
#include "../threading/mutex.h"
-#include "cpp11_container.h"
-#include <list>
-#include <vector>
+#define rangelim(d, min, max) ((d) < (min) ? (min) : ((d) > (max) ? (max) : (d)))
+#define myfloor(x) ((x) < 0.0 ? (int)(x) - 1 : (int)(x))
+// The naive swap performs better than the xor version
+#define SWAP(t, x, y) do { \
+ t temp = x; \
+ x = y; \
+ y = temp; \
+} while (0)
-/*
- * This class permits to cache getFacePosition call results
- * This reduces CPU usage and vector calls
- */
-class FacePositionCache
-{
-public:
- static std::vector<v3s16> getFacePositions(u16 d);
-private:
- static void generateFacePosition(u16 d);
- static UNORDERED_MAP<u16, std::vector<v3s16> > m_cache;
- static Mutex m_cache_mutex;
-};
inline s16 getContainerPos(s16 p, s16 d)
{
- return (p>=0 ? p : p-d+1) / d;
+ return (p >= 0 ? p : p - d + 1) / d;
}
inline v2s16 getContainerPos(v2s16 p, s16 d)
@@ -130,16 +122,6 @@ inline bool isInArea(v3s16 p, v3s16 d)
);
}
-#define rangelim(d, min, max) ((d) < (min) ? (min) : ((d)>(max)?(max):(d)))
-#define myfloor(x) ((x) > 0.0 ? (int)(x) : (int)(x) - 1)
-
-// The naive swap performs better than the xor version
-#define SWAP(t, x, y) do { \
- t temp = x; \
- x = y; \
- y = temp; \
-} while (0)
-
inline void sortBoxVerticies(v3s16 &p1, v3s16 &p2) {
if (p1.X > p2.X)
SWAP(s16, p1.X, p2.X);
@@ -149,6 +131,16 @@ inline void sortBoxVerticies(v3s16 &p1, v3s16 &p2) {
SWAP(s16, p1.Z, p2.Z);
}
+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));
+}
+
/** Returns \p f wrapped to the range [-360, 360]
*
@@ -256,11 +248,10 @@ inline s32 myround(f32 f)
*/
inline v3s16 floatToInt(v3f p, f32 d)
{
- v3s16 p2(
- (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);
- return p2;
+ 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);
}
/*
@@ -268,34 +259,31 @@ inline v3s16 floatToInt(v3f p, f32 d)
*/
inline v3f intToFloat(v3s16 p, f32 d)
{
- v3f p2(
+ return v3f(
(f32)p.X * d,
(f32)p.Y * d,
(f32)p.Z * d
);
- return p2;
}
// Random helper. Usually d=BS
inline aabb3f getNodeBox(v3s16 p, float d)
{
return aabb3f(
- (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
+ (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
);
}
+
class IntervalLimiter
{
public:
- IntervalLimiter():
- m_accumulator(0)
- {
- }
+ IntervalLimiter() : m_accumulator(0) {}
/*
dtime: time from last call to this method
wanted_interval: interval wanted
@@ -306,15 +294,17 @@ public:
bool step(float dtime, float wanted_interval)
{
m_accumulator += dtime;
- if(m_accumulator < wanted_interval)
+ if (m_accumulator < wanted_interval)
return false;
m_accumulator -= wanted_interval;
return true;
}
-protected:
+
+private:
float m_accumulator;
};
+
/*
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
@@ -330,29 +320,21 @@ protected:
*/
inline void paging(u32 length, u32 page, u32 pagecount, u32 &minindex, u32 &maxindex)
{
- if(length < 1 || pagecount < 1 || page < 1 || page > pagecount)
- {
+ if (length < 1 || pagecount < 1 || page < 1 || page > pagecount) {
// Special cases or invalid parameters
minindex = maxindex = 0;
- }
- else if(pagecount <= length)
- {
+ } else if(pagecount <= length) {
// 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;
- }
- else
- {
+ } else {
// More pages than entries in the list:
// Make sure the empty pages are at the end
- if(page < length)
- {
+ if (page < length) {
minindex = page-1;
maxindex = page;
- }
- else
- {
+ } else {
minindex = 0;
maxindex = 0;
}
@@ -361,14 +343,14 @@ inline void paging(u32 length, u32 page, u32 pagecount, u32 &minindex, u32 &maxi
inline float cycle_shift(float value, float by = 0, float max = 1)
{
- if (value + by < 0) return max + by + value;
+ if (value + by < 0) return value + by + max;
if (value + by > max) return value + by - max;
return value + by;
}
inline bool is_power_of_two(u32 n)
{
- return n != 0 && (n & (n-1)) == 0;
+ return n != 0 && (n & (n - 1)) == 0;
}
// Compute next-higher power of 2 efficiently, e.g. for power-of-2 texture sizes.
diff --git a/src/util/pointedthing.cpp b/src/util/pointedthing.cpp
index cd13000b5..ed3d4bb67 100644
--- a/src/util/pointedthing.cpp
+++ b/src/util/pointedthing.cpp
@@ -27,29 +27,25 @@ PointedThing::PointedThing():
type(POINTEDTHING_NOTHING),
node_undersurface(0,0,0),
node_abovesurface(0,0,0),
+ node_real_undersurface(0,0,0),
+ intersection_point(0,0,0),
+ intersection_normal(0,0,0),
object_id(-1)
{}
std::string PointedThing::dump() const
{
std::ostringstream os(std::ios::binary);
- if(type == POINTEDTHING_NOTHING)
- {
+ if (type == POINTEDTHING_NOTHING) {
os<<"[nothing]";
- }
- else if(type == POINTEDTHING_NODE)
- {
+ } else if (type == POINTEDTHING_NODE) {
const v3s16 &u = node_undersurface;
const v3s16 &a = node_abovesurface;
os<<"[node under="<<u.X<<","<<u.Y<<","<<u.Z
<< " above="<<a.X<<","<<a.Y<<","<<a.Z<<"]";
- }
- else if(type == POINTEDTHING_OBJECT)
- {
+ } else if (type == POINTEDTHING_OBJECT) {
os<<"[object "<<object_id<<"]";
- }
- else
- {
+ } else {
os<<"[unknown PointedThing]";
}
return os.str();
@@ -59,61 +55,56 @@ void PointedThing::serialize(std::ostream &os) const
{
writeU8(os, 0); // version
writeU8(os, (u8)type);
- if(type == POINTEDTHING_NOTHING)
- {
- // nothing
- }
- else if(type == POINTEDTHING_NODE)
- {
+ switch (type) {
+ case POINTEDTHING_NOTHING:
+ break;
+ case POINTEDTHING_NODE:
writeV3S16(os, node_undersurface);
writeV3S16(os, node_abovesurface);
- }
- else if(type == POINTEDTHING_OBJECT)
- {
+ break;
+ case POINTEDTHING_OBJECT:
writeS16(os, object_id);
+ break;
}
}
void PointedThing::deSerialize(std::istream &is)
{
int version = readU8(is);
- if(version != 0) throw SerializationError(
+ if (version != 0) throw SerializationError(
"unsupported PointedThing version");
type = (PointedThingType) readU8(is);
- if(type == POINTEDTHING_NOTHING)
- {
- // nothing
- }
- else if(type == POINTEDTHING_NODE)
- {
+ switch (type) {
+ case POINTEDTHING_NOTHING:
+ break;
+ case POINTEDTHING_NODE:
node_undersurface = readV3S16(is);
node_abovesurface = readV3S16(is);
- }
- else if(type == POINTEDTHING_OBJECT)
- {
+ break;
+ case POINTEDTHING_OBJECT:
object_id = readS16(is);
- }
- else
- {
- throw SerializationError(
- "unsupported PointedThingType");
+ break;
+ default:
+ throw SerializationError("unsupported PointedThingType");
}
}
bool PointedThing::operator==(const PointedThing &pt2) const
{
- if(type != pt2.type)
+ if (type != pt2.type)
+ {
return false;
- if(type == POINTEDTHING_NODE)
+ }
+ if (type == POINTEDTHING_NODE)
{
- if(node_undersurface != pt2.node_undersurface)
- return false;
- if(node_abovesurface != pt2.node_abovesurface)
+ if ((node_undersurface != pt2.node_undersurface)
+ || (node_abovesurface != pt2.node_abovesurface)
+ || (node_real_undersurface != pt2.node_real_undersurface))
return false;
}
- else if(type == POINTEDTHING_OBJECT)
+ else if (type == POINTEDTHING_OBJECT)
{
- if(object_id != pt2.object_id)
+ if (object_id != pt2.object_id)
return false;
}
return true;
diff --git a/src/util/pointedthing.h b/src/util/pointedthing.h
index 2b2703e98..f695a4ebd 100644
--- a/src/util/pointedthing.h
+++ b/src/util/pointedthing.h
@@ -32,17 +32,57 @@ enum PointedThingType
POINTEDTHING_OBJECT
};
+//! An active object or node which is selected by a ray on the map.
struct PointedThing
{
+ //! The type of the pointed object.
PointedThingType type;
+ /*!
+ * Only valid if type is POINTEDTHING_NODE.
+ * The coordinates of the node which owns the
+ * nodebox that the ray hits first.
+ * This may differ from node_real_undersurface if
+ * a nodebox exceeds the limits of its node.
+ */
v3s16 node_undersurface;
+ /*!
+ * Only valid if type is POINTEDTHING_NODE.
+ * The coordinates of the last node the ray intersects
+ * before node_undersurface. Same as node_undersurface
+ * if the ray starts in a nodebox.
+ */
v3s16 node_abovesurface;
+ /*!
+ * Only valid if type is POINTEDTHING_NODE.
+ * The coordinates of the node which contains the
+ * point of the collision and the nodebox of the node.
+ */
+ v3s16 node_real_undersurface;
+ /*!
+ * Only valid if type isn't POINTEDTHING_NONE.
+ * First intersection point of the ray and the nodebox.
+ */
+ v3f intersection_point;
+ /*!
+ * Only valid if type isn't POINTEDTHING_NONE.
+ * Normal vector of the intersection.
+ * This is perpendicular to the face the ray hits,
+ * points outside of the box and it's length is 1.
+ */
+ v3s16 intersection_normal;
+ /*!
+ * Only valid if type is POINTEDTHING_OBJECT.
+ * The ID of the object the ray hit.
+ */
s16 object_id;
PointedThing();
std::string dump() const;
void serialize(std::ostream &os) const;
void deSerialize(std::istream &is);
+ /*!
+ * This function ignores the intersection point and normal.
+ */
bool operator==(const PointedThing &pt2) const;
bool operator!=(const PointedThing &pt2) const;
};
diff --git a/src/util/serialize.cpp b/src/util/serialize.cpp
index 99cb990f1..61d369bc4 100644
--- a/src/util/serialize.cpp
+++ b/src/util/serialize.cpp
@@ -354,6 +354,55 @@ std::string deSerializeJsonString(std::istream &is)
return os.str();
}
+std::string serializeJsonStringIfNeeded(const std::string &s)
+{
+ for (size_t i = 0; i < s.size(); ++i) {
+ if (s[i] <= 0x1f || s[i] >= 0x7f || s[i] == ' ' || s[i] == '\"')
+ return serializeJsonString(s);
+ }
+ return s;
+}
+
+std::string deSerializeJsonStringIfNeeded(std::istream &is)
+{
+ std::ostringstream tmp_os;
+ bool expect_initial_quote = true;
+ bool is_json = false;
+ bool was_backslash = false;
+ for (;;) {
+ char c = is.get();
+ if (is.eof())
+ break;
+
+ if (expect_initial_quote && c == '"') {
+ tmp_os << c;
+ is_json = true;
+ } else if(is_json) {
+ tmp_os << c;
+ if (was_backslash)
+ was_backslash = false;
+ else if (c == '\\')
+ was_backslash = true;
+ else if (c == '"')
+ break; // Found end of string
+ } else {
+ if (c == ' ') {
+ // Found end of word
+ is.unget();
+ break;
+ } else {
+ tmp_os << c;
+ }
+ }
+ expect_initial_quote = false;
+ }
+ if (is_json) {
+ std::istringstream tmp_is(tmp_os.str(), std::ios::binary);
+ return deSerializeJsonString(tmp_is);
+ } else
+ return tmp_os.str();
+}
+
////
//// String/Struct conversions
////
diff --git a/src/util/serialize.h b/src/util/serialize.h
index 36324a675..e22434191 100644
--- a/src/util/serialize.h
+++ b/src/util/serialize.h
@@ -405,6 +405,13 @@ std::string serializeJsonString(const std::string &plain);
// Reads a string encoded in JSON format
std::string deSerializeJsonString(std::istream &is);
+// If the string contains spaces, quotes or control characters, encodes as JSON.
+// Else returns the string unmodified.
+std::string serializeJsonStringIfNeeded(const std::string &s);
+
+// Parses a string serialized by serializeJsonStringIfNeeded.
+std::string deSerializeJsonStringIfNeeded(std::istream &is);
+
// Creates a string consisting of the hexadecimal representation of `data`
std::string serializeHexString(const std::string &data, bool insert_spaces=false);
diff --git a/src/util/sha1.h b/src/util/sha1.h
index c04947373..0ac08c67b 100644
--- a/src/util/sha1.h
+++ b/src/util/sha1.h
@@ -29,22 +29,23 @@ typedef unsigned int Uint32;
class SHA1
{
- private:
- // fields
- Uint32 H0, H1, H2, H3, H4;
- unsigned char bytes[64];
- int unprocessedBytes;
- Uint32 size;
- void process();
- public:
- SHA1();
- ~SHA1();
- void addBytes( const char* data, int num );
- unsigned char* getDigest();
- // utility methods
- static Uint32 lrot( Uint32 x, int bits );
- static void storeBigEndianUint32( unsigned char* byte, Uint32 num );
- static void hexPrinter( unsigned char* c, int l );
+private:
+ // fields
+ Uint32 H0, H1, H2, H3, H4;
+ unsigned char bytes[64];
+ int unprocessedBytes;
+ Uint32 size;
+ void process();
+
+public:
+ SHA1();
+ ~SHA1();
+ void addBytes(const char *data, int num);
+ unsigned char *getDigest();
+ // utility methods
+ static Uint32 lrot(Uint32 x, int bits);
+ static void storeBigEndianUint32(unsigned char *byte, Uint32 num);
+ static void hexPrinter(unsigned char *c, int l);
};
#define SHA1_HEADER
diff --git a/src/util/sha2.h b/src/util/sha2.h
index 6ac045feb..233c85a81 100644
--- a/src/util/sha2.h
+++ b/src/util/sha2.h
@@ -57,21 +57,21 @@
*/
#ifndef HEADER_SHA_H
-# define HEADER_SHA_H
+#define HEADER_SHA_H
-# include <stddef.h>
+#include <stddef.h>
-#ifdef __cplusplus
+#ifdef __cplusplus
extern "C" {
#endif
-# if defined(OPENSSL_NO_SHA) || (defined(OPENSSL_NO_SHA0) && defined(OPENSSL_NO_SHA1))
-# error SHA is disabled.
-# endif
+#if defined(OPENSSL_NO_SHA) || (defined(OPENSSL_NO_SHA0) && defined(OPENSSL_NO_SHA1))
+#error SHA is disabled.
+#endif
-# if defined(OPENSSL_FIPS)
-# define FIPS_SHA_SIZE_T size_t
-# endif
+#if defined(OPENSSL_FIPS)
+#define FIPS_SHA_SIZE_T size_t
+#endif
/*
Compat stuff from OpenSSL land
@@ -79,11 +79,10 @@ extern "C" {
/* crypto.h */
-# define fips_md_init(alg) fips_md_init_ctx(alg, alg)
+#define fips_md_init(alg) fips_md_init_ctx(alg, alg)
-# define fips_md_init_ctx(alg, cx) \
- int alg##_Init(cx##_CTX *c)
-# define fips_cipher_abort(alg) while(0)
+#define fips_md_init_ctx(alg, cx) int alg##_Init(cx##_CTX *c)
+#define fips_cipher_abort(alg) while (0)
/*-
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@@ -92,47 +91,51 @@ extern "C" {
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/
-# if defined(__LP32__)
-# define SHA_LONG unsigned long
-# elif defined(__ILP64__)
-# define SHA_LONG unsigned long
-# define SHA_LONG_LOG2 3
-# else
-# define SHA_LONG unsigned int
-# endif
-
-# define SHA_LBLOCK 16
-# define SHA_CBLOCK (SHA_LBLOCK*4)/* SHA treats input data as a
- * contiguous array of 32 bit wide
- * big-endian values. */
-# define SHA_LAST_BLOCK (SHA_CBLOCK-8)
-# define SHA_DIGEST_LENGTH 20
-
-typedef struct SHAstate_st {
- SHA_LONG h0, h1, h2, h3, h4;
- SHA_LONG Nl, Nh;
- SHA_LONG data[SHA_LBLOCK];
- unsigned int num;
+#if defined(__LP32__)
+#define SHA_LONG unsigned long
+#elif defined(__ILP64__)
+#define SHA_LONG unsigned long
+#define SHA_LONG_LOG2 3
+#else
+#define SHA_LONG unsigned int
+#endif
+
+#define SHA_LBLOCK 16
+#define SHA_CBLOCK \
+ (SHA_LBLOCK * 4) /* SHA treats input data as a \
+ * contiguous array of 32 bit wide \
+ * big-endian values. */
+#define SHA_LAST_BLOCK (SHA_CBLOCK - 8)
+#define SHA_DIGEST_LENGTH 20
+
+typedef struct SHAstate_st
+{
+ SHA_LONG h0, h1, h2, h3, h4;
+ SHA_LONG Nl, Nh;
+ SHA_LONG data[SHA_LBLOCK];
+ unsigned int num;
} SHA_CTX;
-# define SHA256_CBLOCK (SHA_LBLOCK*4)/* SHA-256 treats input data as a
- * contiguous array of 32 bit wide
- * big-endian values. */
-# define SHA224_DIGEST_LENGTH 28
-# define SHA256_DIGEST_LENGTH 32
-
-typedef struct SHA256state_st {
- SHA_LONG h[8];
- SHA_LONG Nl, Nh;
- SHA_LONG data[SHA_LBLOCK];
- unsigned int num, md_len;
+#define SHA256_CBLOCK \
+ (SHA_LBLOCK * 4) /* SHA-256 treats input data as a \
+ * contiguous array of 32 bit wide \
+ * big-endian values. */
+#define SHA224_DIGEST_LENGTH 28
+#define SHA256_DIGEST_LENGTH 32
+
+typedef struct SHA256state_st
+{
+ SHA_LONG h[8];
+ SHA_LONG Nl, Nh;
+ SHA_LONG data[SHA_LBLOCK];
+ unsigned int num, md_len;
} SHA256_CTX;
-# ifndef OPENSSL_NO_SHA256
-# ifdef OPENSSL_FIPS
+#ifndef OPENSSL_NO_SHA256
+#ifdef OPENSSL_FIPS
int private_SHA224_Init(SHA256_CTX *c);
int private_SHA256_Init(SHA256_CTX *c);
-# endif
+#endif
int SHA224_Init(SHA256_CTX *c);
int SHA224_Update(SHA256_CTX *c, const void *data, size_t len);
int SHA224_Final(unsigned char *md, SHA256_CTX *c);
@@ -142,12 +145,12 @@ int SHA256_Update(SHA256_CTX *c, const void *data, size_t len);
int SHA256_Final(unsigned char *md, SHA256_CTX *c);
unsigned char *SHA256(const unsigned char *d, size_t n, unsigned char *md);
void SHA256_Transform(SHA256_CTX *c, const unsigned char *data);
-# endif
+#endif
-# define SHA384_DIGEST_LENGTH 48
-# define SHA512_DIGEST_LENGTH 64
+#define SHA384_DIGEST_LENGTH 48
+#define SHA512_DIGEST_LENGTH 64
-#ifdef __cplusplus
+#ifdef __cplusplus
}
#endif
diff --git a/src/util/sha256.c b/src/util/sha256.c
index 4c2bb71a8..4241f31f3 100644
--- a/src/util/sha256.c
+++ b/src/util/sha256.c
@@ -30,11 +30,6 @@ static void OPENSSL_cleanse(void *ptr, size_t len)
cleanse_ctr = (unsigned char)ctr;
}
-# define fips_md_init(alg) fips_md_init_ctx(alg, alg)
-# define fips_md_init_ctx(alg, cx) \
- int alg##_Init(cx##_CTX *c)
-# define fips_cipher_abort(alg) while(0)
-
fips_md_init_ctx(SHA224, SHA256)
{
memset(c, 0, sizeof(*c));
diff --git a/src/util/srp.cpp b/src/util/srp.cpp
index 77c1816e8..430ba1137 100644
--- a/src/util/srp.cpp
+++ b/src/util/srp.cpp
@@ -44,7 +44,7 @@
#if USE_SYSTEM_GMP || defined (__ANDROID__) || defined (ANDROID)
#include <gmp.h>
#else
- #include <gmp/mini-gmp.h>
+ #include <mini-gmp.h>
#endif
#include <util/sha2.h>
diff --git a/src/util/string.cpp b/src/util/string.cpp
index 94064ef93..d41b91f24 100644
--- a/src/util/string.cpp
+++ b/src/util/string.cpp
@@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "hex.h"
#include "../porting.h"
+#include <algorithm>
#include <sstream>
#include <iomanip>
#include <map>
@@ -561,6 +562,7 @@ ColorContainer::ColorContainer()
colors["darkgoldenrod"] = 0xb8860b;
colors["darkgray"] = 0xa9a9a9;
colors["darkgreen"] = 0x006400;
+ colors["darkgrey"] = 0xa9a9a9;
colors["darkkhaki"] = 0xbdb76b;
colors["darkmagenta"] = 0x8b008b;
colors["darkolivegreen"] = 0x556b2f;
@@ -571,11 +573,13 @@ ColorContainer::ColorContainer()
colors["darkseagreen"] = 0x8fbc8f;
colors["darkslateblue"] = 0x483d8b;
colors["darkslategray"] = 0x2f4f4f;
+ colors["darkslategrey"] = 0x2f4f4f;
colors["darkturquoise"] = 0x00ced1;
colors["darkviolet"] = 0x9400d3;
colors["deeppink"] = 0xff1493;
colors["deepskyblue"] = 0x00bfff;
colors["dimgray"] = 0x696969;
+ colors["dimgrey"] = 0x696969;
colors["dodgerblue"] = 0x1e90ff;
colors["firebrick"] = 0xb22222;
colors["floralwhite"] = 0xfffaf0;
@@ -588,6 +592,7 @@ ColorContainer::ColorContainer()
colors["gray"] = 0x808080;
colors["green"] = 0x008000;
colors["greenyellow"] = 0xadff2f;
+ colors["grey"] = 0x808080;
colors["honeydew"] = 0xf0fff0;
colors["hotpink"] = 0xff69b4;
colors["indianred"] = 0xcd5c5c;
@@ -604,11 +609,13 @@ ColorContainer::ColorContainer()
colors["lightgoldenrodyellow"] = 0xfafad2;
colors["lightgray"] = 0xd3d3d3;
colors["lightgreen"] = 0x90ee90;
+ colors["lightgrey"] = 0xd3d3d3;
colors["lightpink"] = 0xffb6c1;
colors["lightsalmon"] = 0xffa07a;
colors["lightseagreen"] = 0x20b2aa;
colors["lightskyblue"] = 0x87cefa;
colors["lightslategray"] = 0x778899;
+ colors["lightslategrey"] = 0x778899;
colors["lightsteelblue"] = 0xb0c4de;
colors["lightyellow"] = 0xffffe0;
colors["lime"] = 0x00ff00;
@@ -661,6 +668,7 @@ ColorContainer::ColorContainer()
colors["skyblue"] = 0x87ceeb;
colors["slateblue"] = 0x6a5acd;
colors["slategray"] = 0x708090;
+ colors["slategrey"] = 0x708090;
colors["snow"] = 0xfffafa;
colors["springgreen"] = 0x00ff7f;
colors["steelblue"] = 0x4682b4;
diff --git a/src/util/string.h b/src/util/string.h
index 572c37150..cc278da13 100644
--- a/src/util/string.h
+++ b/src/util/string.h
@@ -232,7 +232,7 @@ inline std::vector<std::basic_string<T> > str_split(
*/
inline std::string lowercase(const std::string &str)
{
- std::string s2;
+ std::string s2 = "";
s2.reserve(str.size());
@@ -423,6 +423,18 @@ inline void str_replace(std::string &str, const std::string &pattern,
}
/**
+ * Escapes characters [ ] \ , ; that can not be used in formspecs
+ */
+inline void str_formspec_escape(std::string &str)
+{
+ str_replace(str, "\\", "\\\\");
+ str_replace(str, "]", "\\]");
+ str_replace(str, "[", "\\[");
+ str_replace(str, ";", "\\;");
+ str_replace(str, ",", "\\,");
+}
+
+/**
* Replace all occurrences of the character \p from in \p str with \p to.
*
* @param str The string to (potentially) modify.
@@ -614,4 +626,28 @@ inline const char *bool_to_cstr(bool val)
return val ? "true" : "false";
}
+inline const std::string duration_to_string(int sec)
+{
+ int min = sec / 60;
+ sec %= 60;
+ int hour = min / 60;
+ min %= 60;
+
+ std::stringstream ss;
+ if (hour > 0) {
+ ss << hour << "h ";
+ }
+
+ if (min > 0) {
+ ss << min << "m ";
+ }
+
+ if (sec > 0) {
+ ss << sec << "s ";
+ }
+
+ return ss.str();
+}
+
+
#endif
diff --git a/src/util/thread.h b/src/util/thread.h
index 5ed63544c..b96f302f6 100644
--- a/src/util/thread.h
+++ b/src/util/thread.h
@@ -26,11 +26,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "../threading/mutex_auto_lock.h"
#include "porting.h"
#include "log.h"
+#include "container.h"
template<typename T>
-class MutexedVariable {
+class MutexedVariable
+{
public:
- MutexedVariable(T value):
+ MutexedVariable(const T &value):
m_value(value)
{}
@@ -40,21 +42,14 @@ public:
return m_value;
}
- void set(T value)
+ void set(const T &value)
{
MutexAutoLock lock(m_mutex);
m_value = value;
}
- // You'll want to grab this in a SharedPtr
- MutexAutoLock *getLock()
- {
- return new MutexAutoLock(m_mutex);
- }
-
// You pretty surely want to grab the lock when accessing this
T m_value;
-
private:
Mutex m_mutex;
};
@@ -88,8 +83,8 @@ public:
GetRequest() {}
~GetRequest() {}
- GetRequest(Key a_key) {
- key = a_key;
+ GetRequest(const Key &a_key): key(a_key)
+ {
}
Key key;
@@ -111,7 +106,7 @@ public:
return m_queue.empty();
}
- void add(Key key, Caller caller, CallerData callerdata,
+ void add(const Key &key, Caller caller, CallerData callerdata,
ResultQueue<Key, T, Caller, CallerData> *dest)
{
typename std::deque<GetRequest<Key, T, Caller, CallerData> >::iterator i;
diff --git a/src/util/timetaker.cpp b/src/util/timetaker.cpp
index dcf07dc0d..ac686c3a3 100644
--- a/src/util/timetaker.cpp
+++ b/src/util/timetaker.cpp
@@ -19,31 +19,26 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "timetaker.h"
-#include "../gettime.h"
+#include "../porting.h"
#include "../log.h"
#include <ostream>
-TimeTaker::TimeTaker(const char *name, u32 *result, TimePrecision prec)
+TimeTaker::TimeTaker(const std::string &name, u64 *result, TimePrecision prec)
{
m_name = name;
m_result = result;
m_running = true;
m_precision = prec;
- m_time1 = getTime(prec);
+ m_time1 = porting::getTime(prec);
}
-u32 TimeTaker::stop(bool quiet)
+u64 TimeTaker::stop(bool quiet)
{
- if(m_running)
- {
- u32 time2 = getTime(m_precision);
- u32 dtime = time2 - m_time1;
- if(m_result != NULL)
- {
+ if (m_running) {
+ u64 dtime = porting::getTime(m_precision) - m_time1;
+ if (m_result != NULL) {
(*m_result) += dtime;
- }
- else
- {
+ } else {
if (!quiet) {
static const char* const units[] = {
"s" /* PRECISION_SECONDS */,
@@ -62,10 +57,8 @@ u32 TimeTaker::stop(bool quiet)
return 0;
}
-u32 TimeTaker::getTimerTime()
+u64 TimeTaker::getTimerTime()
{
- u32 time2 = getTime(m_precision);
- u32 dtime = time2 - m_time1;
- return dtime;
+ return porting::getTime(m_precision) - m_time1;
}
diff --git a/src/util/timetaker.h b/src/util/timetaker.h
index 5512c205f..c10f4f535 100644
--- a/src/util/timetaker.h
+++ b/src/util/timetaker.h
@@ -30,24 +30,24 @@ with this program; if not, write to the Free Software Foundation, Inc.,
class TimeTaker
{
public:
- TimeTaker(const char *name, u32 *result=NULL,
- TimePrecision=PRECISION_MILLI);
+ TimeTaker(const std::string &name, u64 *result=NULL,
+ TimePrecision prec=PRECISION_MILLI);
~TimeTaker()
{
stop();
}
- u32 stop(bool quiet=false);
+ u64 stop(bool quiet=false);
- u32 getTimerTime();
+ u64 getTimerTime();
private:
- const char *m_name;
- u32 m_time1;
+ std::string m_name;
+ u64 m_time1;
bool m_running;
TimePrecision m_precision;
- u32 *m_result;
+ u64 *m_result;
};
#endif