Skip to content

Commit 41bc244

Browse files
committedApr 23, 2014
Revert binary database block position encoding
This reverts commits a2003b0 and 54ffe2e. These weren't correct. Add a black magic warning instead.
1 parent 9ec281c commit 41bc244

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed
 

‎src/database.cpp

+30-9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2020
#include "database.h"
2121
#include "irrlichttypes.h"
2222

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+
2331
static inline s16 unsigned_to_signed(u16 i, u16 max_positive)
2432
{
2533
if (i < max_positive) {
@@ -30,19 +38,32 @@ static inline s16 unsigned_to_signed(u16 i, u16 max_positive)
3038
}
3139

3240

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+
3351
s64 Database::getBlockAsInteger(const v3s16 pos) const
3452
{
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;
3856
}
3957

40-
v3s16 Database::getIntegerAsBlock(const s64 i) const
58+
59+
v3s16 Database::getIntegerAsBlock(s64 i) const
4160
{
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;
4768
}
4869

‎src/database.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Database
3535
virtual void saveBlock(MapBlock *block)=0;
3636
virtual MapBlock* loadBlock(v3s16 blockpos)=0;
3737
s64 getBlockAsInteger(const v3s16 pos) const;
38-
v3s16 getIntegerAsBlock(const s64 i) const;
38+
v3s16 getIntegerAsBlock(s64 i) const;
3939
virtual void listAllLoadableBlocks(std::list<v3s16> &dst)=0;
4040
virtual int Initialized(void)=0;
4141
virtual ~Database() {};

2 commit comments

Comments
 (2)

Megaf commented on Apr 25, 2014

@Megaf
Contributor

This fixed the #1243 indeed.

sfan5 commented on Apr 25, 2014

@sfan5
Collaborator

This probably explains why migrating to leveldb did not work...

Please sign in to comment.