mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-21 09:59:44 +00:00
add world.get_logic, set_logic 'block_ticks', 'random_ticks' runtime world parameters
This commit is contained in:
parent
bccc6e154f
commit
d0803fce0c
5 changed files with 110 additions and 9 deletions
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include <glm/glm.hpp>
|
||||
#include <memory>
|
||||
#include <unordered_set>
|
||||
#include <utility>
|
||||
|
||||
#include "content/Content.hpp"
|
||||
|
|
@ -14,6 +15,7 @@
|
|||
#include "objects/Player.hpp"
|
||||
#include "objects/Players.hpp"
|
||||
#include "settings.hpp"
|
||||
#include "util/stringutil.hpp"
|
||||
#include "voxels/Chunk.hpp"
|
||||
#include "voxels/Chunks.hpp"
|
||||
#include "voxels/GlobalChunks.hpp"
|
||||
|
|
@ -24,6 +26,7 @@
|
|||
static debug::Logger logger("world");
|
||||
const float DAYIME_SPECIFIC_SPEED = 1.0f / 1440.0f; // 1.0f/60.0f/24.0f;
|
||||
|
||||
|
||||
world_load_error::world_load_error(const std::string& message)
|
||||
: std::runtime_error(message) {
|
||||
}
|
||||
|
|
@ -38,8 +41,7 @@ World::World(
|
|||
packs(packs),
|
||||
wfile(std::move(worldFiles)) {}
|
||||
|
||||
World::~World() {
|
||||
}
|
||||
World::~World() = default;
|
||||
|
||||
void World::updateTimers(float delta) {
|
||||
info.daytime += delta * info.daytimeSpeed * DAYIME_SPECIFIC_SPEED;
|
||||
|
|
@ -76,6 +78,49 @@ void World::write(Level& level) {
|
|||
writeResources(content);
|
||||
}
|
||||
|
||||
dv::value WorldInfo::getLogicValue(const std::string& name,
|
||||
const dv::value& def) const {
|
||||
if (logic.isObject()) {
|
||||
if (auto field = logic.at(name)) {
|
||||
return *field;
|
||||
}
|
||||
}
|
||||
const auto& defaults = defaultLogic();
|
||||
if (defaults.isObject()) {
|
||||
if (auto field = defaults.at(name)) {
|
||||
return *field;
|
||||
}
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
void WorldInfo::setLogicValue(const std::string& name, dv::value value) {
|
||||
if (!WorldInfo::isLogicKey(name)) {
|
||||
throw std::runtime_error("unknown logic parameter " + util::quote(name));
|
||||
}
|
||||
if (!logic.isObject()) {
|
||||
logic = dv::object();
|
||||
}
|
||||
logic[name] = std::move(value);
|
||||
}
|
||||
|
||||
bool WorldInfo::getLogicFlag(const std::string& name, bool def) const {
|
||||
auto value = getLogicValue(name, dv::value(def));
|
||||
if (value.isBoolean()) {
|
||||
return value.asBoolean();
|
||||
}
|
||||
if (value.isInteger()) {
|
||||
return value.asInteger() != 0;
|
||||
}
|
||||
if (value.isNumber()) {
|
||||
return value.asNumber() != 0.0;
|
||||
}
|
||||
if (value.isString()) {
|
||||
return !value.asString().empty();
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
std::unique_ptr<Level> World::create(
|
||||
const std::string& name,
|
||||
const std::string& environment,
|
||||
|
|
@ -216,6 +261,14 @@ void WorldInfo::deserialize(const dv::value& root) {
|
|||
daytimeSpeed = timeobj["day-time-speed"].asNumber();
|
||||
totalTime = timeobj["total-time"].asNumber();
|
||||
}
|
||||
if (root.has("logic")) {
|
||||
logic = root["logic"];
|
||||
} else {
|
||||
logic = dv::object({
|
||||
{"random_ticks", true},
|
||||
{"block_ticks", true}
|
||||
});
|
||||
}
|
||||
nextInventoryId = root["next-inventory-id"].asInteger(2);
|
||||
nextEntityId = root["next-entity-id"].asInteger(1);
|
||||
root.at("next-player-id").get(nextPlayerId);
|
||||
|
|
@ -244,5 +297,6 @@ dv::value WorldInfo::serialize() const {
|
|||
root["next-inventory-id"] = nextInventoryId;
|
||||
root["next-entity-id"] = nextEntityId;
|
||||
root["next-player-id"] = nextPlayerId;
|
||||
root["logic"] = logic;
|
||||
return root;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,6 +22,18 @@ public:
|
|||
};
|
||||
|
||||
struct WorldInfo : public Serializable {
|
||||
static const dv::value& defaultLogic() {
|
||||
static const dv::value logic = dv::object({
|
||||
{"random_ticks", true},
|
||||
{"block_ticks", true}
|
||||
});
|
||||
return logic;
|
||||
}
|
||||
|
||||
static bool isLogicKey(const std::string& name) {
|
||||
return defaultLogic().has(name);
|
||||
}
|
||||
|
||||
std::string name;
|
||||
std::string environment;
|
||||
std::string explicitGenerator;
|
||||
|
|
@ -29,6 +41,8 @@ struct WorldInfo : public Serializable {
|
|||
int64_t nextInventoryId = 1;
|
||||
int64_t nextPlayerId = 0;
|
||||
|
||||
dv::value logic = defaultLogic();
|
||||
|
||||
/// @brief Day/night loop timer in range 0..1 where
|
||||
/// 0.0 - is midnight and
|
||||
/// 0.5 - is noon
|
||||
|
|
@ -45,6 +59,12 @@ struct WorldInfo : public Serializable {
|
|||
|
||||
bool isLoaded = false;
|
||||
|
||||
dv::value getLogicValue(
|
||||
const std::string& name, const dv::value& def = nullptr
|
||||
) const;
|
||||
void setLogicValue(const std::string& name, dv::value value);
|
||||
bool getLogicFlag(const std::string& name, bool def = true) const;
|
||||
|
||||
dv::value serialize() const override;
|
||||
void deserialize(const dv::value& src) override;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue