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

View File

@ -489,12 +489,13 @@ void WorldFiles::writeWorldInfo(const World* world) {
versionobj.put("major", ENGINE_VERSION_MAJOR);
versionobj.put("minor", ENGINE_VERSION_MINOR);
root.put("name", world->name);
root.put("seed", world->seed);
root.put("name", world->getName());
root.put("seed", world->getSeed());
auto& timeobj = root.putMap("time");
timeobj.put("day-time", world->daytime);
timeobj.put("day-time-speed", world->daytimeSpeed);
timeobj.put("total-time", world->totalTime);
files::write_json(getWorldFile(), &root);
}
@ -507,8 +508,9 @@ bool WorldFiles::readWorldInfo(World* world) {
}
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");
if (verobj) {
@ -522,6 +524,7 @@ bool WorldFiles::readWorldInfo(World* world) {
if (timeobj) {
timeobj->num("day-time", world->daytime);
timeobj->num("day-time-speed", world->daytimeSpeed);
timeobj->num("total-time", world->totalTime);
}
return true;

View File

@ -94,7 +94,7 @@ void HudRenderer::createDebugPanel(Engine* engine) {
L" "+stream.str();
}));
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++){

View File

@ -102,7 +102,11 @@ bool ChunksController::loadVisible(){
chunks->putChunk(chunk);
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);
}

View File

@ -47,6 +47,11 @@ static const luaL_Reg packlib [] = {
};
/* == 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) {
lua_pushnumber(L, scripting::level->world->daytime);
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) {
lua_pushinteger(L, scripting::level->world->seed);
lua_pushinteger(L, scripting::level->world->getSeed());
return 1;
}
static const luaL_Reg worldlib [] = {
{"get_total_time", l_world_get_total_time},
{"get_day_time", l_world_get_day_time},
{"set_day_time", l_world_set_day_time},
{"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 bits = lua_tointeger(L, 5);
uint mask = (1 << bits) - 1;
int value = lua_tointeger(L, 6) & mask;
uint mask = ((1 << bits) - 1) << offset;
int value = (lua_tointeger(L, 6) << offset) & mask;
voxel* vox = scripting::level->chunks->get(x, y, z);
if (vox == nullptr) {
return 0;
}
vox->states = (vox->states & (~mask)) | (value << offset);
vox->states = (vox->states & (~mask)) | value;
return 0;
}

View File

@ -13,101 +13,115 @@
#include "../objects/Player.h"
#include "../window/Camera.h"
using glm::vec3;
using std::unique_ptr;
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_load_error::world_load_error(std::string message)
: std::runtime_error(message) {
}
World::World(string name,
path directory,
uint64_t seed,
EngineSettings& settings,
const Content* content,
const std::vector<ContentPack> packs)
: settings(settings),
content(content),
packs(packs),
name(name),
seed(seed) {
wfile = new WorldFiles(directory, settings.debug);
World::World(
std::string name,
fs::path directory,
uint64_t seed,
EngineSettings& settings,
const Content* content,
const std::vector<ContentPack> packs)
: name(name),
seed(seed),
settings(settings),
content(content),
packs(packs) {
wfile = new WorldFiles(directory, settings.debug);
}
World::~World(){
delete wfile;
delete wfile;
}
void World::updateTimers(float delta) {
daytime += delta * daytimeSpeed;
daytime = fmod(daytime, 1.0f);
daytime += delta * daytimeSpeed;
daytime = fmod(daytime, 1.0f);
totalTime += delta;
}
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++) {
shared_ptr<Chunk> chunk = chunks->chunks[i];
if (chunk == nullptr || !chunk->isLighted())
continue;
bool lightsUnsaved = !chunk->isLoadedLights() &&
settings.debug.doWriteLights;
if (!chunk->isUnsaved() && !lightsUnsaved)
continue;
wfile->put(chunk.get());
}
for (size_t i = 0; i < chunks->volume; i++) {
auto chunk = chunks->chunks[i];
if (chunk == nullptr || !chunk->isLighted())
continue;
bool lightsUnsaved = !chunk->isLoadedLights() &&
settings.debug.doWriteLights;
if (!chunk->isUnsaved() && !lightsUnsaved)
continue;
wfile->put(chunk.get());
}
wfile->write(this, content);
wfile->writePlayer(level->player);
wfile->write(this, content);
wfile->writePlayer(level->player);
}
const float DEF_PLAYER_Y = 100.0f;
const float DEF_PLAYER_SPEED = 4.0f;
Level* World::create(string name,
path directory,
uint64_t seed,
EngineSettings& settings,
const Content* content,
const std::vector<ContentPack>& packs) {
World* world = new World(name, directory, seed, settings, content, packs);
Player* player = new Player(vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED);
return new Level(world, content, player, settings);
Level* World::create(std::string name,
fs::path directory,
uint64_t seed,
EngineSettings& settings,
const Content* content,
const std::vector<ContentPack>& packs) {
World* world = new World(name, directory, seed, settings, content, packs);
Player* player = new Player(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED);
return new Level(world, content, player, settings);
}
ContentLUT* World::checkIndices(const path& directory,
const Content* content) {
path indicesFile = directory/path("indices.json");
if (fs::is_regular_file(indicesFile)) {
return ContentLUT::create(indicesFile, content);
}
return nullptr;
ContentLUT* World::checkIndices(const fs::path& directory,
const Content* content) {
fs::path indicesFile = directory/fs::path("indices.json");
if (fs::is_regular_file(indicesFile)) {
return ContentLUT::create(indicesFile, content);
}
return nullptr;
}
Level* World::load(path directory,
Level* World::load(fs::path directory,
EngineSettings& settings,
const Content* content,
const std::vector<ContentPack>& packs) {
unique_ptr<World> world (new World(".", directory, 0, settings, content, packs));
auto& wfile = world->wfile;
const std::vector<ContentPack>& packs) {
auto world = std::make_unique<World>(
".", directory, 0, settings, content, packs
);
auto& wfile = world->wfile;
if (!wfile->readWorldInfo(world.get())) {
throw world_load_error("could not to find world.json");
}
if (!wfile->readWorldInfo(world.get())) {
throw world_load_error("could not to find world.json");
}
Player* player = new Player(vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED);
Level* level = new Level(world.get(), content, player, settings);
wfile->readPlayer(player);
Player* player = new Player(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED);
Level* level = new Level(world.get(), content, player, settings);
wfile->readPlayer(player);
world.release();
return level;
world.release();
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 {
return packs;
return packs;
}

View File

@ -18,53 +18,61 @@ class Level;
class Player;
class ContentLUT;
namespace fs = std::filesystem;
class world_load_error : public std::runtime_error {
public:
world_load_error(std::string message);
world_load_error(std::string message);
};
class World {
EngineSettings& settings;
const Content* const content;
std::vector<ContentPack> packs;
std::string name;
uint64_t seed;
EngineSettings& settings;
const Content* const content;
std::vector<ContentPack> packs;
public:
std::string name;
WorldFiles* wfile;
uint64_t seed;
WorldFiles* wfile;
/* Day/night loop timer in range 0..1
0.0 - is midnight
0.5 - is noon
*/
float daytime = timeutil::time_value(10, 00, 00);
float daytimeSpeed = 1.0f/60.0f/24.0f;
/* Day/night loop timer in range 0..1
0.0 - is midnight
0.5 - is noon
*/
float daytime = timeutil::time_value(10, 00, 00);
float daytimeSpeed = 1.0f/60.0f/24.0f;
double totalTime = 0.0;
World(std::string name,
std::filesystem::path directory,
uint64_t seed,
EngineSettings& settings,
const Content* content,
std::vector<ContentPack> packs);
~World();
World(std::string name,
fs::path directory,
uint64_t seed,
EngineSettings& settings,
const Content* content,
std::vector<ContentPack> packs);
~World();
void updateTimers(float delta);
void write(Level* level);
void updateTimers(float delta);
void write(Level* level);
static ContentLUT* checkIndices(const std::filesystem::path& directory,
const Content* content);
static ContentLUT* checkIndices(const fs::path& directory,
const Content* content);
static Level* create(std::string name,
std::filesystem::path directory,
uint64_t seed,
EngineSettings& settings,
const Content* content,
const std::vector<ContentPack>& packs);
static Level* load(std::filesystem::path directory,
EngineSettings& settings,
const Content* content,
const std::vector<ContentPack>& packs);
const std::vector<ContentPack>& getPacks() const;
static Level* create(std::string name,
fs::path directory,
uint64_t seed,
EngineSettings& settings,
const Content* content,
const std::vector<ContentPack>& packs);
static Level* load(fs::path directory,
EngineSettings& settings,
const Content* content,
const std::vector<ContentPack>& packs);
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_ */