1
0
Fork 0
Univerxel/src/client/config.hpp

252 lines
11 KiB
C++
Raw Normal View History

2020-07-06 19:18:29 +00:00
#pragma once
#include "core/utils/toml.hpp"
2020-09-14 16:03:21 +00:00
#include <glm/vec4.hpp>
2020-09-20 16:41:54 +00:00
#include <limits.h>
#include <iomanip>
#include "world/Universe.hpp"
2020-09-26 22:05:43 +00:00
#include "render/Renderer.hpp"
#include "render/Window.hpp"
2020-10-04 13:32:28 +00:00
#include "contouring/Abstract.hpp"
2020-07-25 16:45:03 +00:00
#include "control/Camera.hpp"
2020-09-14 16:03:21 +00:00
2020-09-20 16:41:54 +00:00
namespace config::client {
2020-07-06 19:18:29 +00:00
2020-07-31 17:09:44 +00:00
inline glm::vec4 fromHex(const std::string& str) {
2020-08-04 15:44:53 +00:00
std::array<int, 3> rgb = {UCHAR_MAX};
2020-07-12 13:46:51 +00:00
sscanf(str.c_str() + 1, "%02X%02X%02X", (unsigned int *)&rgb[0], (unsigned int *)&rgb[1], (unsigned int *)&rgb[2]);
2020-07-31 17:09:44 +00:00
return glm::vec4(rgb[0] * 1.f / UCHAR_MAX, rgb[1] * 1.f / UCHAR_MAX, rgb[2] * 1.f / UCHAR_MAX, 1);
2020-07-12 13:46:51 +00:00
}
2020-07-31 17:09:44 +00:00
inline std::string toHexa(const glm::vec4& rgb) {
2020-08-04 15:44:53 +00:00
std::ostringstream sstr;
2020-10-06 14:32:05 +00:00
sstr << '#' << std::hex << std::setw(2) << std::setfill('0') <<
static_cast<int>(rgb.x * UCHAR_MAX) << static_cast<int>(rgb.y * UCHAR_MAX) << static_cast<int>(rgb.z * UCHAR_MAX);
2020-08-04 15:44:53 +00:00
return sstr.str();
2020-07-12 13:46:51 +00:00
}
2020-11-04 21:04:37 +00:00
/// Client side configuration
2020-07-06 19:18:29 +00:00
struct options {
2020-09-20 16:41:54 +00:00
public:
options(toml::node_view<toml::node> config) {
assert(config["enabled"] && "probably a broken config file");
2020-09-20 16:41:54 +00:00
window.targetFPS = config["window"]["target_fps"].value_or(window.targetFPS);
2020-10-18 15:34:27 +00:00
window.sampling = config["window"]["sampling"].value_or(window.sampling);
2020-09-20 16:41:54 +00:00
window.fullscreen = config["window"]["fullscreen"].value_or(window.fullscreen);
2020-09-26 22:05:43 +00:00
renderer.inFlightFrames = config["window"]["parallel_frames"].value_or(renderer.inFlightFrames);
2020-07-12 13:46:51 +00:00
2020-09-20 16:41:54 +00:00
preferVulkan = config["render"]["prefer_vulkan"].value_or(preferVulkan);
2020-07-12 13:46:51 +00:00
renderer.textures = config["render"]["textures"].value_or(renderer.textures);
2020-10-17 12:49:00 +00:00
renderer.textureQuality = config["render"]["texture_quality"].value_or(renderer.textureQuality);
renderer.textureSharpness = config["render"]["texture_angular_quality"].value_or(renderer.textureSharpness);
2020-08-30 16:35:45 +00:00
renderer.voxel.pbr = config["render"]["pbr"].value_or(renderer.voxel.pbr);
2020-12-03 18:46:47 +00:00
renderer.voxel.planar = static_cast<render::Planar>(config["render"]["planar"].value_or(static_cast<int>(renderer.voxel.planar)));
renderer.voxel.stochastic = config["render"]["stochastic"].value_or(renderer.voxel.stochastic);
2020-08-30 16:35:45 +00:00
renderer.voxel.geometry = config["render"]["geometry"].value_or(renderer.voxel.geometry);
renderer.voxel.blend = config["render"]["blend"].value_or(renderer.voxel.blend);
renderer.voxel.fog = config["render"]["fog"].value_or(renderer.voxel.fog);
2020-07-13 16:45:08 +00:00
const std::string fog = config["render"]["fog_color"].value_or(std::string{"#000000"});
2020-07-12 13:46:51 +00:00
renderer.clear_color = fromHex(fog);
2020-07-13 16:45:08 +00:00
renderer.skybox = config["render"]["skybox"].value_or(renderer.skybox);
renderer.voxel.curvature = config["render"]["curvature"].value_or(renderer.voxel.curvature);
renderer.voxel.curv_depth = config["render"]["curvature_depth"].value_or(renderer.voxel.curv_depth);
2020-10-22 16:11:33 +00:00
renderer.voxel.transparency = config["render"]["transparency"].value_or(renderer.voxel.transparency);
2020-09-20 16:41:54 +00:00
culling = config["render"]["culling"].value_or(culling);
2020-07-12 13:46:51 +00:00
2020-10-04 13:32:28 +00:00
contouring = config["contouring"].value_or(std::string(""));
2020-07-12 13:46:51 +00:00
2020-11-11 19:25:03 +00:00
camera.far_dist = config["camera"]["far"].value_or(camera.far_dist);
camera.near_dist = config["camera"]["near"].value_or(camera.near_dist);
2020-07-12 13:46:51 +00:00
camera.fov = config["camera"]["fov"].value_or(camera.fov);
2020-08-08 13:03:20 +00:00
control.sensibility = config["control"]["sensibility"].value_or(control.sensibility);
control.speed = config["control"]["speed"].value_or(control.speed);
control.collide = config["control"]["collide"].value_or(control.collide);
2020-07-12 13:46:51 +00:00
2020-09-20 16:41:54 +00:00
world.loadDistance = config["world"]["load_distance"].value_or(world.loadDistance);
world.keepDistance = config["world"]["keep_distance"].value_or(world.keepDistance);
2020-11-06 16:32:00 +00:00
world.useAverages = config["world"]["use_averages"].value_or(world.useAverages);
world.trustMajorant = config["world"]["trust_majorant"].value_or(world.trustMajorant);
2020-11-07 21:16:48 +00:00
world.editPrediction = config["world"]["edit_prediction"].value_or(world.editPrediction);
world.editHandling = config["world"]["edit_handling"].value_or(world.editHandling);
world.movePrediction = config["world"]["move_prediction"].value_or(world.movePrediction);
world.moveCollision = config["world"]["move_collision"].value_or(world.moveCollision);
2020-07-12 13:46:51 +00:00
#ifndef LIGHT_CLIENT
const auto useLocal = config["connection"]["use_local"].value_or(true);
#else
const auto useLocal = false;
#endif
if(!useLocal) {
2020-09-22 20:37:09 +00:00
world::client::Universe::connection ct;
2020-11-03 22:04:43 +00:00
ct.host = config["connection"]["host"].value_or(ct.host);
2020-09-22 20:37:09 +00:00
ct.port = config["connection"]["port"].value_or(ct.port);
connection = ct;
}
2020-09-20 16:41:54 +00:00
editor.visible = config["editor"]["visible"].value_or(editor.visible);
2020-10-23 20:50:00 +00:00
editor.tool.shape = static_cast<world::action::Shape>(config["editor"]["tool"]["shape"].value_or(static_cast<int>(editor.tool.shape)));
2020-09-20 16:41:54 +00:00
editor.tool.radius = config["editor"]["tool"]["radius"].value_or(editor.tool.radius);
editor.tool.material = config["editor"]["tool"]["material"].value_or(editor.tool.material);
editor.tool.emptyAir = config["editor"]["tool"]["empty_air"].value_or(editor.tool.emptyAir);
2020-07-25 14:29:05 +00:00
2020-10-21 17:55:32 +00:00
console.visible = config["console"]["visible"].value_or(console.visible);
console.scroll = config["console"]["scroll"].value_or(console.scroll);
console.opacity = config["console"]["opacity"].value_or(console.opacity);
2020-09-20 16:41:54 +00:00
debugMenu.bar = config["debug_menu"]["bar"].value_or(debugMenu.bar);
debugMenu.render = config["debug_menu"]["render"].value_or(debugMenu.render);
debugMenu.world = config["debug_menu"]["world"].value_or(debugMenu.world);
debugMenu.contouring = config["debug_menu"]["contouring"].value_or(debugMenu.contouring);
debugMenu.controls = config["debug_menu"]["controls"].value_or(debugMenu.controls);
overlay.visible = config["overlay"]["visible"].value_or(overlay.visible);
overlay.corner = config["overlay"]["corner"].value_or(overlay.corner);
if (config["keys"].is_table()) {
for(auto entry: *config["keys"].as_table()) {
keys.emplace_back(std::stoi(entry.first), entry.second.value_or(-1));
}
}
2020-07-12 13:46:51 +00:00
}
2020-09-20 16:41:54 +00:00
toml::table save() {
2020-07-12 13:46:51 +00:00
auto config = toml::table();
2020-09-20 16:41:54 +00:00
config.insert_or_assign("enabled", true);
2020-07-12 13:46:51 +00:00
config.insert_or_assign("window", toml::table({
2020-09-20 16:41:54 +00:00
{"target_fps", window.targetFPS},
2020-10-18 15:34:27 +00:00
{"sampling", window.sampling},
2020-09-26 22:05:43 +00:00
{"fullscreen", window.fullscreen},
{"parallel_frames", renderer.inFlightFrames}
2020-07-12 13:46:51 +00:00
}));
config.insert_or_assign("render", toml::table({
2020-09-20 16:41:54 +00:00
{"prefer_vulkan", preferVulkan},
2020-07-12 13:46:51 +00:00
{"textures", renderer.textures},
2020-10-17 12:49:00 +00:00
{"texture_quality", renderer.textureQuality},
{"texture_angular_quality", renderer.textureSharpness},
2020-08-30 16:35:45 +00:00
{"pbr", renderer.voxel.pbr},
2020-12-03 18:46:47 +00:00
{"planar", static_cast<int>(renderer.voxel.planar)},
{"stochastic", renderer.voxel.stochastic},
2020-08-30 16:35:45 +00:00
{"geometry", renderer.voxel.geometry},
{"blend", renderer.voxel.blend},
{"fog", renderer.voxel.fog},
2020-07-12 13:46:51 +00:00
{"fog_color", toHexa(renderer.clear_color)},
{"skybox", renderer.skybox},
{"curvature", renderer.voxel.curvature},
2020-09-20 16:41:54 +00:00
{"curvature_depth", renderer.voxel.curv_depth},
2020-10-22 16:11:33 +00:00
{"transparency", renderer.voxel.transparency},
2020-09-20 16:41:54 +00:00
{"culling", culling}
2020-07-12 13:46:51 +00:00
}));
2020-10-04 13:32:28 +00:00
config.insert_or_assign("contouring", contouring);
2020-07-12 13:46:51 +00:00
config.insert_or_assign("camera", toml::table({
2020-11-11 19:25:03 +00:00
{"far", camera.far_dist},
{"near", camera.near_dist},
2020-08-08 13:03:20 +00:00
{"fov", camera.fov}
}));
config.insert_or_assign("control", toml::table({
{"sensibility", control.sensibility},
{"speed", control.speed},
{"collide", control.collide}
2020-07-12 13:46:51 +00:00
}));
2020-09-20 16:41:54 +00:00
config.insert_or_assign("world", toml::table({
{"load_distance", world.loadDistance},
{"keep_distance", world.keepDistance},
2020-11-06 16:32:00 +00:00
{"use_averages", world.useAverages},
{"trust_majorant", world.trustMajorant},
2020-11-07 21:16:48 +00:00
{"edit_prediction", world.editPrediction},
{"edit_handling", world.editHandling},
{"move_prediction", world.movePrediction},
{"move_collision", world.moveCollision}
2020-07-12 13:46:51 +00:00
}));
2020-09-22 20:37:09 +00:00
if(connection.has_value()) {
const auto &ct = connection.value();
config.insert_or_assign("connection", toml::table({
#ifndef LIGHT_CLIENT
2020-09-22 20:37:09 +00:00
{"use_local", false},
#endif
2020-11-03 22:04:43 +00:00
{"host", ct.host},
{"port", ct.port}
2020-09-22 20:37:09 +00:00
}));
} else {
config.insert_or_assign("connection", toml::table({{"use_local", true}}));
}
2020-07-25 14:29:05 +00:00
config.insert_or_assign("editor", toml::table({
2020-09-20 16:41:54 +00:00
{"visible", editor.visible},
2020-07-25 14:29:05 +00:00
{"tool", toml::table({
2020-10-23 20:50:00 +00:00
{"shape", static_cast<int>(editor.tool.shape)},
2020-09-20 16:41:54 +00:00
{"radius", editor.tool.radius},
{"material", editor.tool.material},
{"empty_air", editor.tool.emptyAir}
2020-07-25 14:29:05 +00:00
})}
}));
2020-10-21 17:55:32 +00:00
config.insert_or_assign("console", toml::table({
{"visible", console.visible},
{"scroll", console.scroll},
{"opacity", console.opacity}
}));
2020-09-20 16:41:54 +00:00
config.insert_or_assign("debug_menu", toml::table({
{"bar", debugMenu.bar},
{"render", debugMenu.render},
{"world", debugMenu.world},
{"contouring", debugMenu.contouring},
{"controls", debugMenu.controls}
}));
config.insert_or_assign("overlay", toml::table({
{"visible", overlay.visible},
{"corner", overlay.corner}
}));
toml::table key_table;
for (const auto& key: keys)
key_table.insert_or_assign(std::to_string(key.first), key.second);
config.insert_or_assign("keys", key_table);
2020-09-20 16:41:54 +00:00
return config;
2020-07-12 13:46:51 +00:00
}
2020-09-20 16:41:54 +00:00
world::client::Universe::options world;
struct {
bool bar = false;
bool render = false;
bool world = false;
bool contouring = false;
bool controls = false;
} debugMenu;
2020-10-18 20:41:56 +00:00
windowOptions window;
2020-09-20 16:41:54 +00:00
2020-10-22 16:11:33 +00:00
bool preferVulkan = false;
2020-09-26 22:05:43 +00:00
render::renderOptions renderer;
2020-08-09 17:23:45 +00:00
int culling = 0;
2020-07-06 19:18:29 +00:00
2020-10-04 13:32:28 +00:00
std::string contouring;
2020-09-20 16:41:54 +00:00
2020-08-08 13:03:20 +00:00
Controllable::options control;
2020-07-10 19:37:49 +00:00
Camera::options camera;
2020-07-06 19:18:29 +00:00
2020-09-20 16:41:54 +00:00
struct {
bool visible = false;
struct {
2020-10-23 20:50:00 +00:00
world::action::Shape shape = world::action::Shape::Cube;
2020-09-20 16:41:54 +00:00
int radius = 2;
unsigned short material = 5;
bool emptyAir = true;
} tool;
} editor;
2020-10-21 17:55:32 +00:00
struct {
bool visible = true;
bool scroll = true;
float opacity = .8f;
} console;
struct overlay_t {
2020-09-20 16:41:54 +00:00
bool visible = true;
int corner = 3;
} overlay;
2020-09-22 20:37:09 +00:00
2020-09-26 22:05:43 +00:00
std::optional<world::client::Universe::connection> connection = {};
std::vector<std::pair<int, int>> keys;
2020-07-06 19:18:29 +00:00
};
2020-09-14 16:03:21 +00:00
}