Split struct BlockPos into own header

This commit is contained in:
Rogier 2014-05-06 16:20:22 +02:00
parent 902f3b45d7
commit bbf92d2ffa
3 changed files with 139 additions and 47 deletions

69
BlockPos.h Normal file
View File

@ -0,0 +1,69 @@
#ifndef BLOCKPOS_H
#define BLOCKPOS_H
#include <cstdlib>
#include <limits>
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

View File

@ -21,6 +21,7 @@
#include <string>
#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:

69
minetest-database.h Normal file
View File

@ -0,0 +1,69 @@
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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);
}