lua: world.get_total_time + set_block_user_bits fix

This commit is contained in:
MihailRis
2024-01-22 09:52:26 +03:00
parent ff7f18fa7b
commit 31eda1abb2
6 changed files with 147 additions and 112 deletions
+7 -4
View File
@@ -489,12 +489,13 @@ void WorldFiles::writeWorldInfo(const World* world) {
versionobj.put("major", ENGINE_VERSION_MAJOR); versionobj.put("major", ENGINE_VERSION_MAJOR);
versionobj.put("minor", ENGINE_VERSION_MINOR); versionobj.put("minor", ENGINE_VERSION_MINOR);
root.put("name", world->name); root.put("name", world->getName());
root.put("seed", world->seed); root.put("seed", world->getSeed());
auto& timeobj = root.putMap("time"); auto& timeobj = root.putMap("time");
timeobj.put("day-time", world->daytime); timeobj.put("day-time", world->daytime);
timeobj.put("day-time-speed", world->daytimeSpeed); timeobj.put("day-time-speed", world->daytimeSpeed);
timeobj.put("total-time", world->totalTime);
files::write_json(getWorldFile(), &root); files::write_json(getWorldFile(), &root);
} }
@@ -507,8 +508,9 @@ bool WorldFiles::readWorldInfo(World* world) {
} }
auto root = files::read_json(file); auto root = files::read_json(file);
root->str("name", world->name);
root->num("seed", world->seed); world->setName(root->getStr("name", world->getName()));
world->setSeed(root->getInt("seed", world->getSeed()));
auto verobj = root->map("version"); auto verobj = root->map("version");
if (verobj) { if (verobj) {
@@ -522,6 +524,7 @@ bool WorldFiles::readWorldInfo(World* world) {
if (timeobj) { if (timeobj) {
timeobj->num("day-time", world->daytime); timeobj->num("day-time", world->daytime);
timeobj->num("day-time-speed", world->daytimeSpeed); timeobj->num("day-time-speed", world->daytimeSpeed);
timeobj->num("total-time", world->totalTime);
} }
return true; return true;
+1 -1
View File
@@ -94,7 +94,7 @@ void HudRenderer::createDebugPanel(Engine* engine) {
L" "+stream.str(); L" "+stream.str();
})); }));
panel->add(create_label([=](){ panel->add(create_label([=](){
return L"seed: "+std::to_wstring(level->world->seed); return L"seed: "+std::to_wstring(level->world->getSeed());
})); }));
for (int ax = 0; ax < 3; ax++){ for (int ax = 0; ax < 3; ax++){
+5 -1
View File
@@ -102,7 +102,11 @@ bool ChunksController::loadVisible(){
chunks->putChunk(chunk); chunks->putChunk(chunk);
if (!chunk->isLoaded()) { if (!chunk->isLoaded()) {
generator->generate(chunk->voxels, chunk->x, chunk->z, level->world->seed); generator->generate(
chunk->voxels,
chunk->x, chunk->z,
level->world->getSeed()
);
chunk->setUnsaved(true); chunk->setUnsaved(true);
} }
+10 -4
View File
@@ -47,6 +47,11 @@ static const luaL_Reg packlib [] = {
}; };
/* == world library == */ /* == world library == */
static int l_world_get_total_time(lua_State* L) {
lua_pushnumber(L, scripting::level->world->totalTime);
return 1;
}
static int l_world_get_day_time(lua_State* L) { static int l_world_get_day_time(lua_State* L) {
lua_pushnumber(L, scripting::level->world->daytime); lua_pushnumber(L, scripting::level->world->daytime);
return 1; return 1;
@@ -59,11 +64,12 @@ static int l_world_set_day_time(lua_State* L) {
} }
static int l_world_get_seed(lua_State* L) { static int l_world_get_seed(lua_State* L) {
lua_pushinteger(L, scripting::level->world->seed); lua_pushinteger(L, scripting::level->world->getSeed());
return 1; return 1;
} }
static const luaL_Reg worldlib [] = { static const luaL_Reg worldlib [] = {
{"get_total_time", l_world_get_total_time},
{"get_day_time", l_world_get_day_time}, {"get_day_time", l_world_get_day_time},
{"set_day_time", l_world_set_day_time}, {"set_day_time", l_world_set_day_time},
{"get_seed", l_world_get_seed}, {"get_seed", l_world_get_seed},
@@ -261,14 +267,14 @@ static int l_set_block_user_bits(lua_State* L) {
int offset = lua_tointeger(L, 4) + VOXEL_USER_BITS_OFFSET; int offset = lua_tointeger(L, 4) + VOXEL_USER_BITS_OFFSET;
int bits = lua_tointeger(L, 5); int bits = lua_tointeger(L, 5);
uint mask = (1 << bits) - 1; uint mask = ((1 << bits) - 1) << offset;
int value = lua_tointeger(L, 6) & mask; int value = (lua_tointeger(L, 6) << offset) & mask;
voxel* vox = scripting::level->chunks->get(x, y, z); voxel* vox = scripting::level->chunks->get(x, y, z);
if (vox == nullptr) { if (vox == nullptr) {
return 0; return 0;
} }
vox->states = (vox->states & (~mask)) | (value << offset); vox->states = (vox->states & (~mask)) | value;
return 0; return 0;
} }
+80 -66
View File
@@ -13,101 +13,115 @@
#include "../objects/Player.h" #include "../objects/Player.h"
#include "../window/Camera.h" #include "../window/Camera.h"
using glm::vec3; world_load_error::world_load_error(std::string message)
using std::unique_ptr; : std::runtime_error(message) {
using std::shared_ptr;
using std::string;
using std::filesystem::path;
namespace fs = std::filesystem;
world_load_error::world_load_error(string message) : std::runtime_error(message) {
} }
World::World(string name, World::World(
path directory, std::string name,
uint64_t seed, fs::path directory,
EngineSettings& settings, uint64_t seed,
const Content* content, EngineSettings& settings,
const std::vector<ContentPack> packs) const Content* content,
: settings(settings), const std::vector<ContentPack> packs)
content(content), : name(name),
packs(packs), seed(seed),
name(name), settings(settings),
seed(seed) { content(content),
wfile = new WorldFiles(directory, settings.debug); packs(packs) {
wfile = new WorldFiles(directory, settings.debug);
} }
World::~World(){ World::~World(){
delete wfile; delete wfile;
} }
void World::updateTimers(float delta) { void World::updateTimers(float delta) {
daytime += delta * daytimeSpeed; daytime += delta * daytimeSpeed;
daytime = fmod(daytime, 1.0f); daytime = fmod(daytime, 1.0f);
totalTime += delta;
} }
void World::write(Level* level) { void World::write(Level* level) {
const Content* content = level->content; const Content* content = level->content;
Chunks* chunks = level->chunks; Chunks* chunks = level->chunks;
for (size_t i = 0; i < chunks->volume; i++) { for (size_t i = 0; i < chunks->volume; i++) {
shared_ptr<Chunk> chunk = chunks->chunks[i]; auto chunk = chunks->chunks[i];
if (chunk == nullptr || !chunk->isLighted()) if (chunk == nullptr || !chunk->isLighted())
continue; continue;
bool lightsUnsaved = !chunk->isLoadedLights() && bool lightsUnsaved = !chunk->isLoadedLights() &&
settings.debug.doWriteLights; settings.debug.doWriteLights;
if (!chunk->isUnsaved() && !lightsUnsaved) if (!chunk->isUnsaved() && !lightsUnsaved)
continue; continue;
wfile->put(chunk.get()); wfile->put(chunk.get());
} }
wfile->write(this, content); wfile->write(this, content);
wfile->writePlayer(level->player); wfile->writePlayer(level->player);
} }
const float DEF_PLAYER_Y = 100.0f; const float DEF_PLAYER_Y = 100.0f;
const float DEF_PLAYER_SPEED = 4.0f; const float DEF_PLAYER_SPEED = 4.0f;
Level* World::create(string name, Level* World::create(std::string name,
path directory, fs::path directory,
uint64_t seed, uint64_t seed,
EngineSettings& settings, EngineSettings& settings,
const Content* content, const Content* content,
const std::vector<ContentPack>& packs) { const std::vector<ContentPack>& packs) {
World* world = new World(name, directory, seed, settings, content, packs); World* world = new World(name, directory, seed, settings, content, packs);
Player* player = new Player(vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED); Player* player = new Player(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED);
return new Level(world, content, player, settings); return new Level(world, content, player, settings);
} }
ContentLUT* World::checkIndices(const path& directory, ContentLUT* World::checkIndices(const fs::path& directory,
const Content* content) { const Content* content) {
path indicesFile = directory/path("indices.json"); fs::path indicesFile = directory/fs::path("indices.json");
if (fs::is_regular_file(indicesFile)) { if (fs::is_regular_file(indicesFile)) {
return ContentLUT::create(indicesFile, content); return ContentLUT::create(indicesFile, content);
} }
return nullptr; return nullptr;
} }
Level* World::load(path directory, Level* World::load(fs::path directory,
EngineSettings& settings, EngineSettings& settings,
const Content* content, const Content* content,
const std::vector<ContentPack>& packs) { const std::vector<ContentPack>& packs) {
unique_ptr<World> world (new World(".", directory, 0, settings, content, packs)); auto world = std::make_unique<World>(
auto& wfile = world->wfile; ".", directory, 0, settings, content, packs
);
auto& wfile = world->wfile;
if (!wfile->readWorldInfo(world.get())) { if (!wfile->readWorldInfo(world.get())) {
throw world_load_error("could not to find world.json"); throw world_load_error("could not to find world.json");
} }
Player* player = new Player(vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED); Player* player = new Player(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED);
Level* level = new Level(world.get(), content, player, settings); Level* level = new Level(world.get(), content, player, settings);
wfile->readPlayer(player); wfile->readPlayer(player);
world.release(); world.release();
return level; return level;
}
void World::setName(const std::string& name) {
this->name = name;
}
void World::setSeed(uint64_t seed) {
this->seed = seed;
}
std::string World::getName() const {
return name;
}
uint64_t World::getSeed() const {
return seed;
} }
const std::vector<ContentPack>& World::getPacks() const { const std::vector<ContentPack>& World::getPacks() const {
return packs; return packs;
} }
+44 -36
View File
@@ -18,53 +18,61 @@ class Level;
class Player; class Player;
class ContentLUT; class ContentLUT;
namespace fs = std::filesystem;
class world_load_error : public std::runtime_error { class world_load_error : public std::runtime_error {
public: public:
world_load_error(std::string message); world_load_error(std::string message);
}; };
class World { class World {
EngineSettings& settings; std::string name;
const Content* const content; uint64_t seed;
std::vector<ContentPack> packs; EngineSettings& settings;
const Content* const content;
std::vector<ContentPack> packs;
public: public:
std::string name; WorldFiles* wfile;
WorldFiles* wfile;
uint64_t seed;
/* Day/night loop timer in range 0..1 /* Day/night loop timer in range 0..1
0.0 - is midnight 0.0 - is midnight
0.5 - is noon 0.5 - is noon
*/ */
float daytime = timeutil::time_value(10, 00, 00); float daytime = timeutil::time_value(10, 00, 00);
float daytimeSpeed = 1.0f/60.0f/24.0f; float daytimeSpeed = 1.0f/60.0f/24.0f;
double totalTime = 0.0;
World(std::string name, World(std::string name,
std::filesystem::path directory, fs::path directory,
uint64_t seed, uint64_t seed,
EngineSettings& settings, EngineSettings& settings,
const Content* content, const Content* content,
std::vector<ContentPack> packs); std::vector<ContentPack> packs);
~World(); ~World();
void updateTimers(float delta); void updateTimers(float delta);
void write(Level* level); void write(Level* level);
static ContentLUT* checkIndices(const std::filesystem::path& directory, static ContentLUT* checkIndices(const fs::path& directory,
const Content* content); const Content* content);
static Level* create(std::string name, static Level* create(std::string name,
std::filesystem::path directory, fs::path directory,
uint64_t seed, uint64_t seed,
EngineSettings& settings, EngineSettings& settings,
const Content* content, const Content* content,
const std::vector<ContentPack>& packs); const std::vector<ContentPack>& packs);
static Level* load(std::filesystem::path directory, static Level* load(fs::path directory,
EngineSettings& settings, EngineSettings& settings,
const Content* content, const Content* content,
const std::vector<ContentPack>& packs); const std::vector<ContentPack>& packs);
const std::vector<ContentPack>& getPacks() const; void setName(const std::string& name);
void setSeed(uint64_t seed);
std::string getName() const;
uint64_t getSeed() const;
const std::vector<ContentPack>& getPacks() const;
}; };
#endif /* WORLD_WORLD_H_ */ #endif /* WORLD_WORLD_H_ */