@@ -20,6 +20,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
20
20
#include " database.h"
21
21
#include " irrlichttypes.h"
22
22
23
+
24
+ /* ***************
25
+ * Black magic! *
26
+ ****************
27
+ * The position hashing is very messed up.
28
+ * It's a lot more complicated than it looks.
29
+ */
Has a conversation. Original line has a conversation.
30
+
23
31
static inline s16 unsigned_to_signed (u16 i, u16 max_positive)
24
32
{
25
33
if (i < max_positive) {
@@ -30,19 +38,32 @@ static inline s16 unsigned_to_signed(u16 i, u16 max_positive)
30
38
}
31
39
32
40
41
+ // Modulo of a negative number does not work consistently in C
42
+ static inline s64 pythonmodulo (s64 i, s16 mod)
43
+ {
44
+ if (i >= 0 ) {
45
+ return i % mod;
46
+ }
47
+ return mod - ((-i) % mod);
48
+ }
49
+
50
+
33
51
s64 Database::getBlockAsInteger (const v3s16 pos) const
34
52
{
35
- return ((( u64) pos.Z ) << 24 ) +
36
- ((( u64) pos.Y ) << 12 ) +
37
- (( u64) pos.X ) ;
53
+ return (u64) pos.Z * 0x1000000 +
54
+ (u64) pos.Y * 0x1000 +
55
+ (u64) pos.X ;
38
56
}
39
57
40
- v3s16 Database::getIntegerAsBlock (const s64 i) const
58
+
59
+ v3s16 Database::getIntegerAsBlock (s64 i) const
41
60
{
42
- v3s16 pos;
43
- pos.Z = unsigned_to_signed ((i >> 24 ) & 0xFFF , 0x1000 / 2 );
44
- pos.Y = unsigned_to_signed ((i >> 12 ) & 0xFFF , 0x1000 / 2 );
45
- pos.X = unsigned_to_signed ((i ) & 0xFFF , 0x1000 / 2 );
46
- return pos;
61
+ v3s16 pos;
62
+ pos.X = unsigned_to_signed (pythonmodulo (i, 4096 ), 2048 );
63
+ i = (i - pos.X ) / 4096 ;
64
+ pos.Y = unsigned_to_signed (pythonmodulo (i, 4096 ), 2048 );
65
+ i = (i - pos.Y ) / 4096 ;
66
+ pos.Z = unsigned_to_signed (pythonmodulo (i, 4096 ), 2048 );
67
+ return pos;
47
68
}
48
69