Skip to content

Commit

Permalink
Merge remote-tracking branch 'MihailRis/main' into vulkan-support
Browse files Browse the repository at this point in the history
  • Loading branch information
C4e10VeK committed Dec 9, 2023
2 parents 194aa78 + f1353c7 commit 332c2be
Show file tree
Hide file tree
Showing 19 changed files with 389 additions and 141 deletions.
Binary file modified res/textures/menubg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 0 additions & 69 deletions src/content/ContentIndexLUT.cpp

This file was deleted.

33 changes: 0 additions & 33 deletions src/content/ContentIndexLUT.h

This file was deleted.

59 changes: 59 additions & 0 deletions src/content/ContentLUT.cpp
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;
}
}
58 changes: 58 additions & 0 deletions src/content/ContentLUT.h
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_
71 changes: 71 additions & 0 deletions src/files/WorldConverter.cpp
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);
}
26 changes: 26 additions & 0 deletions src/files/WorldConverter.h
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_
37 changes: 34 additions & 3 deletions src/files/WorldFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ WorldRegion* WorldFiles::getOrCreateRegion(
return region;
}

ubyte* WorldFiles::compress(ubyte* src, size_t srclen, size_t& len) {
ubyte* WorldFiles::compress(const ubyte* src, size_t srclen, size_t& len) {
len = extrle::encode(src, srclen, compressionBuffer);
ubyte* data = new ubyte[len];
for (size_t i = 0; i < len; i++) {
Expand All @@ -131,12 +131,27 @@ ubyte* WorldFiles::compress(ubyte* src, size_t srclen, size_t& len) {
return data;
}

ubyte* WorldFiles::decompress(ubyte* src, size_t srclen, size_t dstlen) {
ubyte* WorldFiles::decompress(const ubyte* src, size_t srclen, size_t dstlen) {
ubyte* decompressed = new ubyte[dstlen];
extrle::decode(src, srclen, decompressed);
return decompressed;
}

void WorldFiles::put(int x, int z, const ubyte* voxelData) {
int regionX = floordiv(x, REGION_SIZE);
int regionZ = floordiv(z, REGION_SIZE);
int localX = x - (regionX * REGION_SIZE);
int localZ = z - (regionZ * REGION_SIZE);

/* Writing Voxels */ {
WorldRegion* region = getOrCreateRegion(regions, regionX, regionZ);
region->setUnsaved(true);
size_t compressedSize;
ubyte* data = compress(voxelData, CHUNK_DATA_LEN, compressedSize);
region->put(localX, localZ, data, compressedSize);
}
}

void WorldFiles::put(Chunk* chunk){
assert(chunk != nullptr);

Expand Down Expand Up @@ -176,6 +191,21 @@ path WorldFiles::getRegionFilename(int x, int y) const {
return path(filename);
}

bool WorldFiles::parseRegionFilename(const string& name, int& x, int& y) {
size_t sep = name.find('_');
if (sep == string::npos || sep == 0 || sep == name.length()-1)
return false;
try {
x = std::stoi(name.substr(0, sep));
y = std::stoi(name.substr(sep+1));
} catch (std::invalid_argument& err) {
return false;
} catch (std::out_of_range& err) {
return false;
}
return true;
}

path WorldFiles::getPlayerFile() const {
return directory/path("player.json");
}
Expand Down Expand Up @@ -338,7 +368,8 @@ void WorldFiles::write(const World* world, const Content* content) {
fs::create_directories(directory);
}
}
writeWorldInfo(world);
if (world)
writeWorldInfo(world);
if (generatorTestMode)
return;
writeIndices(content->indices);
Expand Down
Loading

0 comments on commit 332c2be

Please sign in to comment.