Content packs indices auto build + WorldGenerator fix
This commit is contained in:
parent
d2a40d31a4
commit
76aeb8e4df
@ -238,6 +238,10 @@ JObject& JArray::putObj() {
|
||||
return *obj;
|
||||
}
|
||||
|
||||
void JArray::remove(size_t index) {
|
||||
values.erase(values.begin() + index);
|
||||
}
|
||||
|
||||
JObject::~JObject() {
|
||||
for (auto entry : map) {
|
||||
delete entry.second;
|
||||
|
||||
@ -68,6 +68,8 @@ namespace json {
|
||||
|
||||
JArray& putArray();
|
||||
JObject& putObj();
|
||||
|
||||
void remove(size_t index);
|
||||
};
|
||||
|
||||
class JObject {
|
||||
|
||||
@ -3,9 +3,11 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <algorithm>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "Content.h"
|
||||
#include "../util/listutil.h"
|
||||
#include "../voxels/Block.h"
|
||||
#include "../files/files.h"
|
||||
#include "../coders/json.h"
|
||||
@ -19,6 +21,61 @@ namespace fs = std::filesystem;
|
||||
ContentLoader::ContentLoader(ContentPack* pack) : pack(pack) {
|
||||
}
|
||||
|
||||
void ContentLoader::fixPackIndices() {
|
||||
auto folder = pack->folder;
|
||||
auto indexFile = pack->getContentFile();
|
||||
auto blocksFolder = folder/ContentPack::BLOCKS_FOLDER;
|
||||
std::unique_ptr<json::JObject> root;
|
||||
if (fs::is_regular_file(indexFile)) {
|
||||
root.reset(files::read_json(indexFile));
|
||||
} else {
|
||||
root.reset(new json::JObject());
|
||||
}
|
||||
|
||||
std::vector<std::string> detectedBlocks;
|
||||
std::vector<std::string> indexedBlocks;
|
||||
if (fs::is_directory(blocksFolder)) {
|
||||
for (auto entry : fs::directory_iterator(blocksFolder)) {
|
||||
fs::path file = entry.path();
|
||||
if (fs::is_regular_file(file) && file.extension() == ".json") {
|
||||
std::string name = file.stem();
|
||||
if (name[0] == '_')
|
||||
continue;
|
||||
detectedBlocks.push_back(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool modified = false;
|
||||
if (!root->has("blocks")) {
|
||||
root->putArray("blocks");
|
||||
}
|
||||
json::JArray* blocksarr = root->arr("blocks");
|
||||
if (blocksarr) {
|
||||
for (uint i = 0; i < blocksarr->size(); i++) {
|
||||
std::string name = blocksarr->str(i);
|
||||
if (!util::contains(detectedBlocks, name)) {
|
||||
blocksarr->remove(i);
|
||||
i--;
|
||||
modified = true;
|
||||
continue;
|
||||
}
|
||||
indexedBlocks.push_back(name);
|
||||
}
|
||||
}
|
||||
for (auto name : detectedBlocks) {
|
||||
if (!util::contains(indexedBlocks, name)) {
|
||||
blocksarr->put(name);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
if (modified){
|
||||
// rewrite modified json
|
||||
std::cout << indexFile << std::endl;
|
||||
files::write_string(indexFile, json::stringify(root.get(), true, " "));
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: add basic validation and logging
|
||||
Block* ContentLoader::loadBlock(std::string name, fs::path file) {
|
||||
std::unique_ptr<json::JObject> root(files::read_json(file));
|
||||
@ -95,8 +152,10 @@ Block* ContentLoader::loadBlock(std::string name, fs::path file) {
|
||||
void ContentLoader::load(ContentBuilder* builder) {
|
||||
std::cout << "-- loading pack [" << pack->id << "]" << std::endl;
|
||||
|
||||
fixPackIndices();
|
||||
|
||||
auto folder = pack->folder;
|
||||
auto root = files::read_json(pack->getContentFile());
|
||||
std::unique_ptr<json::JObject> root (files::read_json(pack->getContentFile()));
|
||||
|
||||
json::JArray* blocksarr = root->arr("blocks");
|
||||
if (blocksarr) {
|
||||
|
||||
@ -13,6 +13,7 @@ class ContentLoader {
|
||||
public:
|
||||
ContentLoader(ContentPack* pack);
|
||||
|
||||
void fixPackIndices();
|
||||
Block* loadBlock(std::string name, std::filesystem::path file);
|
||||
void load(ContentBuilder* builder);
|
||||
};
|
||||
|
||||
@ -10,6 +10,7 @@ namespace fs = std::filesystem;
|
||||
|
||||
const std::string ContentPack::PACKAGE_FILENAME = "package.json";
|
||||
const std::string ContentPack::CONTENT_FILENAME = "content.json";
|
||||
const fs::path ContentPack::BLOCKS_FOLDER = "blocks";
|
||||
|
||||
contentpack_error::contentpack_error(
|
||||
std::string packId,
|
||||
|
||||
@ -30,6 +30,8 @@ struct ContentPack {
|
||||
|
||||
static const std::string PACKAGE_FILENAME;
|
||||
static const std::string CONTENT_FILENAME;
|
||||
static const std::filesystem::path BLOCKS_FOLDER;
|
||||
|
||||
static bool is_pack(std::filesystem::path folder);
|
||||
static ContentPack read(std::filesystem::path folder);
|
||||
static void scan(std::filesystem::path folder,
|
||||
|
||||
13
src/util/listutil.h
Normal file
13
src/util/listutil.h
Normal file
@ -0,0 +1,13 @@
|
||||
#ifndef UTIL_LISTUTIL_H_
|
||||
#define UTIL_LISTUTIL_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace util {
|
||||
template<class T>
|
||||
bool contains(std::vector<T> vec, const T& value) {
|
||||
return std::find(vec.begin(), vec.end(), value) != vec.end();
|
||||
}
|
||||
}
|
||||
|
||||
#endif // UTIL_LISTUTIL_H_
|
||||
@ -128,7 +128,9 @@ int generate_tree(fnl_state *noise,
|
||||
int cur_x,
|
||||
int cur_y,
|
||||
int cur_z,
|
||||
int tileSize){
|
||||
int tileSize,
|
||||
blockid_t idWood,
|
||||
blockid_t idLeaves){
|
||||
const int tileX = floordiv(cur_x, tileSize);
|
||||
const int tileZ = floordiv(cur_z, tileSize);
|
||||
|
||||
@ -152,9 +154,9 @@ int generate_tree(fnl_state *noise,
|
||||
int ly = cur_y - height - 3 * radius;
|
||||
int lz = cur_z - centerZ;
|
||||
if (lx == 0 && lz == 0 && cur_y - height < (3*radius + radius/2))
|
||||
return 6;
|
||||
return idWood;
|
||||
if (lx*lx+ly*ly/2+lz*lz < radius*radius)
|
||||
return 7;
|
||||
return idLeaves;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -210,7 +212,7 @@ void WorldGenerator::generate(voxel* voxels, int cx, int cz, int seed){
|
||||
} else if (cur_y < height){
|
||||
id = idDirt;
|
||||
} else {
|
||||
int tree = generate_tree(&noise, &randomtree, heights, cur_x, cur_y, cur_z, treesTile);
|
||||
int tree = generate_tree(&noise, &randomtree, heights, cur_x, cur_y, cur_z, treesTile, idWood, idLeaves);
|
||||
if (tree) {
|
||||
id = tree;
|
||||
states = BLOCK_DIR_UP;
|
||||
@ -220,7 +222,7 @@ void WorldGenerator::generate(voxel* voxels, int cx, int cz, int seed){
|
||||
if (
|
||||
((height - (1.1 - 0.2 * pow(height - 54, 4))
|
||||
+
|
||||
(5*send)) < cur_y + (height - 0.01- (int)height))
|
||||
(5*send)) < cur_y + (height - 0.01- (int)height))
|
||||
&& (cur_y < height)){
|
||||
id = idSand;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user