771d9b9629cb75562a9c71261a03e637367835bf
[oweals/minetest.git] / src / database.cpp
1 #include "map.h"
2 #include "mapsector.h"
3 #include "mapblock.h"
4 #include "main.h"
5 #include "filesys.h"
6 #include "voxel.h"
7 #include "porting.h"
8 #include "mapgen.h"
9 #include "nodemetadata.h"
10 #include "settings.h"
11 #include "log.h"
12 #include "profiler.h"
13 #include "nodedef.h"
14 #include "gamedef.h"
15 #include "util/directiontables.h"
16 #include "rollback_interface.h"
17
18
19 #include "database.h"
20
21 static s32 unsignedToSigned(s32 i, s32 max_positive)
22 {
23         if(i < max_positive)
24                 return i;
25         else
26                 return i - 2*max_positive;
27 }
28
29 // modulo of a negative number does not work consistently in C
30 static s64 pythonmodulo(s64 i, s64 mod)
31 {
32         if(i >= 0)
33                 return i % mod;
34         return mod - ((-i) % mod);
35 }
36
37 long long Database::getBlockAsInteger(const v3s16 pos) {
38         return (unsigned long long)pos.Z*16777216 +
39                 (unsigned long long)pos.Y*4096 + 
40                 (unsigned long long)pos.X;
41 }
42
43 v3s16 Database::getIntegerAsBlock(long long i) {
44         s32 x = unsignedToSigned(pythonmodulo(i, 4096), 2048);
45         i = (i - x) / 4096;
46         s32 y = unsignedToSigned(pythonmodulo(i, 4096), 2048);
47         i = (i - y) / 4096;
48         s32 z = unsignedToSigned(pythonmodulo(i, 4096), 2048);
49         return v3s16(x,y,z);
50 }