diff --git a/TileGenerator.cpp b/TileGenerator.cpp index cd8b14d..cfc2619 100644 --- a/TileGenerator.cpp +++ b/TileGenerator.cpp @@ -22,13 +22,17 @@ TileGenerator::TileGenerator(): m_drawOrigin(false), m_drawPlayers(false), m_drawScale(false), - m_drawUnderground(false) - + m_drawUnderground(false), + m_db(0) { } TileGenerator::~TileGenerator() { + if (m_db != 0) { + sqlite3_close(m_db); + m_db = 0; + } } void TileGenerator::setBgColor(const std::string &bgColor) @@ -71,10 +75,6 @@ void TileGenerator::setDrawUnderground(bool drawUnderground) m_drawUnderground = drawUnderground; } -void TileGenerator::generate(const std::string &/*input*/, const std::string &/*output*/) -{ -} - void TileGenerator::parseColorsFile(const std::string &fileName) { ifstream in; @@ -108,3 +108,61 @@ void TileGenerator::parseColorsFile(const std::string &fileName) } } +void TileGenerator::generate(const std::string &input, const std::string &/*output*/) +{ + openDb(input); + loadBlocks(); +} + +void TileGenerator::openDb(const std::string &input) +{ + string db_name = input + "map.sqlite"; + if (sqlite3_open(db_name.c_str(), &m_db) != SQLITE_OK) { + throw DbError(); + } +} + +void TileGenerator::loadBlocks() +{ + sqlite3_stmt *statement; + string sql = "SELECT pos FROM blocks"; + if (sqlite3_prepare_v2(m_db, sql.c_str(), sql.length(), &statement, 0) == SQLITE_OK) { + //int cols = sqlite3_column_count(statement); + int result = 0; + while (true) { + result = sqlite3_step(statement); + if(result == SQLITE_ROW) { + sqlite3_int64 blocknum = sqlite3_column_int64(statement, 0); + decodeBlockPos(blocknum); + } + else { + break; + } + } + } + else { + throw DbError(); + } +} + +inline BlockPos TileGenerator::decodeBlockPos(sqlite3_int64 blockId) +{ + BlockPos pos; + pos.x = unsignedToSigned(blockId % 4096, 2048); + blockId = blockId / 4096; + pos.y = unsignedToSigned(blockId % 4096, 2048); + blockId = blockId / 4096; + pos.z = unsignedToSigned(blockId % 4096, 2048); + return pos; +} + +inline int TileGenerator::unsignedToSigned(long i, long max_positive) +{ + if (i < max_positive) { + return i; + } + else { + return i - 2l * max_positive; + } +} + diff --git a/TileGenerator.h b/TileGenerator.h index 346feb8..5ce5bfc 100644 --- a/TileGenerator.h +++ b/TileGenerator.h @@ -10,9 +10,10 @@ #ifndef TILEGENERATOR_H_JJNUCARH #define TILEGENERATOR_H_JJNUCARH +#include +#include #include #include -#include struct Color { uint8_t r; @@ -20,6 +21,15 @@ struct Color { uint8_t b; }; +struct BlockPos { + int x; + int y; + int z; +}; + +class DbError { +}; + class TileGenerator { private: @@ -36,8 +46,14 @@ public: void setDrawPlayers(bool drawPlayers); void setDrawScale(bool drawScale); void setDrawUnderground(bool drawUnderground); - void generate(const std::string &input, const std::string &output); void parseColorsFile(const std::string &fileName); + void generate(const std::string &input, const std::string &output); + +private: + void openDb(const std::string &input); + void loadBlocks(); + BlockPos decodeBlockPos(sqlite3_int64 blockId); + int unsignedToSigned(long i, long max_positive); private: std::string m_bgColor; @@ -48,6 +64,7 @@ private: bool m_drawPlayers; bool m_drawScale; bool m_drawUnderground; + sqlite3 *m_db; ColorMap m_colors; }; /* ----- end of class TileGenerator ----- */ diff --git a/mapper.cpp b/mapper.cpp index 848cb5d..8a7cae7 100644 --- a/mapper.cpp +++ b/mapper.cpp @@ -105,4 +105,5 @@ int main(int argc, char *argv[]) abort(); } } + generator.generate(input, output); }