From bbf92d2ffa6fbb2e5c2604417d8b3d5fae6d9836 Mon Sep 17 00:00:00 2001 From: Rogier Date: Tue, 6 May 2014 16:20:22 +0200 Subject: [PATCH] Split struct BlockPos into own header --- BlockPos.h | 69 +++++++++++++++++++++++++++++++++++++++++++++ TileGenerator.h | 48 +------------------------------ minetest-database.h | 69 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+), 47 deletions(-) create mode 100644 BlockPos.h create mode 100644 minetest-database.h diff --git a/BlockPos.h b/BlockPos.h new file mode 100644 index 0000000..ef10381 --- /dev/null +++ b/BlockPos.h @@ -0,0 +1,69 @@ + +#ifndef BLOCKPOS_H +#define BLOCKPOS_H + +#include +#include + +struct BlockPos { + int x; + int y; + int z; +#if SIZE_MAX > (1LL << 36) + std::size_t hash(void) const { return databasePos(); } +#else + std::size_t hash(void) const { return databasePos() % SIZE_MAX; } +#endif + BlockPos() : x(0), y(0), z(0) {} + BlockPos(int _x, int _y, int _z) : x(_x), y(_y), z(_z) {} + BlockPos(int64_t i) { setFromDBPos(i); } + int64_t databasePos(void) const { return getDBPos(); } + + bool operator<(const BlockPos& p) const; + bool operator==(const BlockPos& p) const; + void operator=(const BlockPos &p) { x = p.x; y = p.y; z = p.z; } + void operator=(int64_t i) { setFromDBPos(i); } + +protected: +// Include code copied from the minetest codebase. +// Defines (at least) the following functions: +// setFromDBPos(int64_t i); +// int64_t getDBPos(void) const; +#include "minetest-database.h" + +}; + +// operator< should order the positions in the +// order the corresponding pixels are generated: +// First (most significant): z coordinate, descending (i.e. reversed) +// Then : x coordinate, ascending +// Last (least significant): y coordinate, descending (i.e. reversed) +inline bool BlockPos::operator<(const BlockPos& p) const +{ + if (z > p.z) + return true; + if (z < p.z) + return false; + if (x < p.x) + return true; + if (x > p.x) + return false; + if (y > p.y) + return true; + if (y < p.y) + return false; + return false; +} + +inline bool BlockPos::operator==(const BlockPos& p) const +{ + if (z != p.z) + return false; + if (y != p.y) + return false; + if (x != p.x) + return false; + return true; +} + +#endif // BLOCKPOS_H diff --git a/TileGenerator.h b/TileGenerator.h index bec0130..fe6be08 100644 --- a/TileGenerator.h +++ b/TileGenerator.h @@ -21,6 +21,7 @@ #include #include "types.h" #include "PixelAttributes.h" +#include "BlockPos.h" #include "Color.h" #include "db.h" @@ -30,53 +31,6 @@ #define TILECENTER_IS_WORLDCENTER INT_MAX #define TILECENTER_IS_MAPCENTER INT_MIN -struct BlockPos { - int x; - int y; - int z; - // operator< should order the positions in the - // order the corresponding pixels are generated: - // First (most significant): z coordinate, descending (i.e. reversed) - // Then : x coordinate, ascending - // Last (least significant): y coordinate, descending (i.e. reversed) - bool operator<(const BlockPos& p) const - { - if (z > p.z) { - return true; - } - if (z < p.z) { - return false; - } - if (x < p.x) { - return true; - } - if (x > p.x) { - return false; - } - if (y > p.y) { - return true; - } - if (y < p.y) { - return false; - } - return false; - } - bool operator==(const BlockPos& p) const - { - if (z != p.z) { - return false; - } - if (y != p.y) { - return false; - } - if (x != p.x) { - return false; - } - return true; - } -}; - - class TileGenerator { private: diff --git a/minetest-database.h b/minetest-database.h new file mode 100644 index 0000000..2c84829 --- /dev/null +++ b/minetest-database.h @@ -0,0 +1,69 @@ +/* +Minetest +Copyright (C) 2013 celeron55, Perttu Ahola + +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. +*/ + +// This file was copied from the minetest code & minimally modified to suit +// this codebase +// Original filename: src/database.cpp +// +// For minetestmapper, this file should be included by BlockPos.h *only* + + +/**************** + * Black magic! * + **************** + * The position hashing is very messed up. + * It's a lot more complicated than it looks. + */ + +static inline int unsigned_to_signed(unsigned i, unsigned max_positive) +{ + if (i < max_positive) { + return i; + } else { + return i - (max_positive * 2); + } +} + + +// Modulo of a negative number does not work consistently in C +static inline int64_t pythonmodulo(int64_t i, int16_t mod) +{ + if (i >= 0) { + return i % mod; + } + return mod - ((-i) % mod); +} + +inline int64_t getDBPos(void) const +{ + return (uint64_t) z * 0x1000000 + + (uint64_t) y * 0x1000 + + (uint64_t) x; +} + + +inline void setFromDBPos(int64_t i) +{ + x = unsigned_to_signed(pythonmodulo(i, 4096), 2048); + i = (i - x) / 4096; + y = unsigned_to_signed(pythonmodulo(i, 4096), 2048); + i = (i - y) / 4096; + z = unsigned_to_signed(pythonmodulo(i, 4096), 2048); +} +