-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'MihailRis/main' into vulkan-support
- Loading branch information
Showing
19 changed files
with
389 additions
and
141 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include "ContentLUT.h" | ||
|
||
#include <memory> | ||
|
||
#include "Content.h" | ||
#include "../constants.h" | ||
#include "../files/files.h" | ||
#include "../coders/json.h" | ||
#include "../voxels/Block.h" | ||
|
||
using std::string; | ||
using std::unique_ptr; | ||
using std::make_unique; | ||
using std::filesystem::path; | ||
|
||
#include <iostream> | ||
|
||
ContentLUT::ContentLUT(size_t blocksCount, const Content* content) { | ||
ContentIndices* indices = content->indices; | ||
for (size_t i = 0; i < blocksCount; i++) { | ||
blocks.push_back(i); | ||
} | ||
for (size_t i = 0; i < indices->countBlockDefs(); i++) { | ||
blockNames.push_back(indices->getBlockDef(i)->name); | ||
} | ||
|
||
for (size_t i = indices->countBlockDefs(); i < blocksCount; i++) { | ||
blockNames.push_back(""); | ||
} | ||
} | ||
|
||
ContentLUT* ContentLUT::create(const path& filename, | ||
const Content* content) { | ||
unique_ptr<json::JObject> root(files::read_json(filename)); | ||
json::JArray* blocksarr = root->arr("blocks"); | ||
|
||
auto& indices = content->indices; | ||
size_t blocks_c = blocksarr | ||
? std::max(blocksarr->size(), indices->countBlockDefs()) | ||
: indices->countBlockDefs(); | ||
|
||
auto lut = make_unique<ContentLUT>(blocks_c, content); | ||
if (blocksarr) { | ||
for (size_t i = 0; i < blocksarr->size(); i++) { | ||
string name = blocksarr->str(i); | ||
Block* def = content->findBlock(name); | ||
if (def) { | ||
lut->setBlock(i, name, def->rt.id); | ||
} else { | ||
lut->setBlock(i, name, BLOCK_VOID); | ||
} | ||
} | ||
} | ||
if (lut->hasContentReorder() || lut->hasMissingContent()) { | ||
return lut.release(); | ||
} else { | ||
return nullptr; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#ifndef CONTENT_CONTENT_LUT_H_ | ||
#define CONTENT_CONTENT_LUT_H_ | ||
|
||
#include <string> | ||
#include <vector> | ||
#include <filesystem> | ||
|
||
#include "../typedefs.h" | ||
#include "../constants.h" | ||
|
||
class Content; | ||
|
||
/* Content indices lookup table or report | ||
used to convert world with different indices | ||
Building with indices.json */ | ||
class ContentLUT { | ||
std::vector<blockid_t> blocks; | ||
std::vector<std::string> blockNames; | ||
|
||
bool reorderContent = false; | ||
bool missingContent = false; | ||
public: | ||
ContentLUT(size_t blocks, const Content* content); | ||
|
||
inline const std::string& getBlockName(blockid_t index) const { | ||
return blockNames[index]; | ||
} | ||
|
||
inline blockid_t getBlockId(blockid_t index) const { | ||
return blocks[index]; | ||
} | ||
|
||
inline void setBlock(blockid_t index, std::string name, blockid_t id) { | ||
blocks[index] = id; | ||
blockNames[index] = name; | ||
if (id == BLOCK_VOID) { | ||
missingContent = true; | ||
} else if (index != id) { | ||
reorderContent = true; | ||
} | ||
} | ||
|
||
static ContentLUT* create(const std::filesystem::path& filename, | ||
const Content* content); | ||
|
||
inline bool hasContentReorder() const { | ||
return reorderContent; | ||
} | ||
inline bool hasMissingContent() const { | ||
return missingContent; | ||
} | ||
|
||
inline size_t countBlocks() const { | ||
return blocks.size(); | ||
} | ||
}; | ||
|
||
#endif // CONTENT_CONTENT_LUT_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include "WorldConverter.h" | ||
|
||
#include <memory> | ||
#include <iostream> | ||
#include <stdexcept> | ||
#include "WorldFiles.h" | ||
#include "../voxels/Chunk.h" | ||
#include "../content/ContentLUT.h" | ||
|
||
namespace fs = std::filesystem; | ||
using std::string; | ||
using std::unique_ptr; | ||
using fs::path; | ||
|
||
WorldConverter::WorldConverter(path folder, | ||
const Content* content, | ||
const ContentLUT* lut) | ||
: lut(lut), content(content) { | ||
DebugSettings settings; | ||
wfile = new WorldFiles(folder, settings); | ||
|
||
path regionsFolder = wfile->getRegionsFolder(); | ||
if (!fs::is_directory(regionsFolder)) { | ||
std::cerr << "nothing to convert" << std::endl; | ||
return; | ||
} | ||
for (auto file : fs::directory_iterator(regionsFolder)) { | ||
regions.push(file.path()); | ||
} | ||
} | ||
|
||
WorldConverter::~WorldConverter() { | ||
delete wfile; | ||
} | ||
|
||
bool WorldConverter::hasNext() const { | ||
return !regions.empty(); | ||
} | ||
|
||
void WorldConverter::convertNext() { | ||
if (!hasNext()) { | ||
throw std::runtime_error("no more regions to convert"); | ||
} | ||
path regfile = regions.front(); | ||
regions.pop(); | ||
if (!fs::is_regular_file(regfile)) | ||
return; | ||
int x, y; | ||
string name = regfile.stem().string(); | ||
if (!WorldFiles::parseRegionFilename(name, x, y)) { | ||
std::cerr << "could not parse name " << name << std::endl; | ||
return; | ||
} | ||
std::cout << "converting region " << name << std::endl; | ||
for (uint cz = 0; cz < REGION_SIZE; cz++) { | ||
for (uint cx = 0; cx < REGION_SIZE; cx++) { | ||
int gx = cx + x * REGION_SIZE; | ||
int gz = cz + y * REGION_SIZE; | ||
unique_ptr<ubyte[]> data (wfile->getChunk(gx, gz)); | ||
if (data == nullptr) | ||
continue; | ||
Chunk::convert(data.get(), lut); | ||
wfile->put(gx, gz, data.get()); | ||
} | ||
} | ||
} | ||
|
||
void WorldConverter::write() { | ||
std::cout << "writing world" << std::endl; | ||
wfile->write(nullptr, content); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef FILES_WORLD_CONVERTER_H_ | ||
#define FILES_WORLD_CONVERTER_H_ | ||
|
||
#include <queue> | ||
#include <filesystem> | ||
|
||
class Content; | ||
class ContentLUT; | ||
class WorldFiles; | ||
|
||
class WorldConverter { | ||
WorldFiles* wfile; | ||
const ContentLUT* const lut; | ||
const Content* const content; | ||
std::queue<std::filesystem::path> regions; | ||
public: | ||
WorldConverter(std::filesystem::path folder, const Content* content, const ContentLUT* lut); | ||
~WorldConverter(); | ||
|
||
bool hasNext() const; | ||
void convertNext(); | ||
|
||
void write(); | ||
}; | ||
|
||
#endif // FILES_WORLD_CONVERTER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.