From fa1646a9bd676a3849d45040b098dd311043db2b Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 18 Dec 2024 00:48:29 +0300 Subject: [PATCH] add '--script', '--version' command line arguments --- src/{TestMainloop.cpp => ServerMainloop.cpp} | 36 +++++++++++++++----- src/{TestMainloop.hpp => ServerMainloop.hpp} | 6 ++-- src/engine.cpp | 4 +-- src/engine.hpp | 3 +- src/logic/scripting/lua/lua_engine.cpp | 2 +- src/util/command_line.cpp | 15 ++++++-- src/world/Level.hpp | 1 - 7 files changed, 48 insertions(+), 19 deletions(-) rename src/{TestMainloop.cpp => ServerMainloop.cpp} (53%) rename src/{TestMainloop.hpp => ServerMainloop.hpp} (73%) diff --git a/src/TestMainloop.cpp b/src/ServerMainloop.cpp similarity index 53% rename from src/TestMainloop.cpp rename to src/ServerMainloop.cpp index 100c0b51..be013e7b 100644 --- a/src/TestMainloop.cpp +++ b/src/ServerMainloop.cpp @@ -1,4 +1,4 @@ -#include "TestMainloop.hpp" +#include "ServerMainloop.hpp" #include "logic/scripting/scripting.hpp" #include "logic/LevelController.hpp" @@ -6,22 +6,27 @@ #include "debug/Logger.hpp" #include "world/Level.hpp" #include "world/World.hpp" +#include "util/platform.hpp" #include "engine.hpp" +#include + +using namespace std::chrono; + static debug::Logger logger("mainloop"); inline constexpr int TPS = 20; -TestMainloop::TestMainloop(Engine& engine) : engine(engine) { +ServerMainloop::ServerMainloop(Engine& engine) : engine(engine) { } -TestMainloop::~TestMainloop() = default; +ServerMainloop::~ServerMainloop() = default; -void TestMainloop::run() { +void ServerMainloop::run() { const auto& coreParams = engine.getCoreParameters(); auto& time = engine.getTime(); - if (coreParams.testFile.empty()) { + if (coreParams.scriptFile.empty()) { logger.info() << "nothing to do"; return; } @@ -29,21 +34,34 @@ void TestMainloop::run() { setLevel(std::move(level)); }); - logger.info() << "starting test " << coreParams.testFile; - auto process = scripting::start_coroutine(coreParams.testFile); + logger.info() << "starting test " << coreParams.scriptFile; + auto process = scripting::start_coroutine(coreParams.scriptFile); + + double targetDelta = 1.0f / static_cast(TPS); + double delta = targetDelta; + auto begin = steady_clock::now(); while (process->isActive()) { - time.step(1.0f / static_cast(TPS)); + time.step(delta); process->update(); if (controller) { float delta = time.getDelta(); controller->getLevel()->getWorld()->updateTimers(delta); controller->update(glm::min(delta, 0.2f), false); } + + if (!coreParams.testMode) { + auto end = steady_clock::now(); + platform::sleep(targetDelta * 1000 - + duration_cast(end - begin).count() / 1000); + end = steady_clock::now(); + delta = duration_cast(end - begin).count() / 1e6; + begin = end; + } } logger.info() << "test finished"; } -void TestMainloop::setLevel(std::unique_ptr level) { +void ServerMainloop::setLevel(std::unique_ptr level) { if (level == nullptr) { controller->onWorldQuit(); engine.getPaths()->setCurrentWorldFolder(fs::path()); diff --git a/src/TestMainloop.hpp b/src/ServerMainloop.hpp similarity index 73% rename from src/TestMainloop.hpp rename to src/ServerMainloop.hpp index 06ffae25..28cfbd9d 100644 --- a/src/TestMainloop.hpp +++ b/src/ServerMainloop.hpp @@ -6,12 +6,12 @@ class Level; class LevelController; class Engine; -class TestMainloop { +class ServerMainloop { Engine& engine; std::unique_ptr controller; public: - TestMainloop(Engine& engine); - ~TestMainloop(); + ServerMainloop(Engine& engine); + ~ServerMainloop(); void run(); diff --git a/src/engine.cpp b/src/engine.cpp index 3c7d4bb5..46fd17c8 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -36,7 +36,7 @@ #include "window/Window.hpp" #include "world/Level.hpp" #include "Mainloop.hpp" -#include "TestMainloop.hpp" +#include "ServerMainloop.hpp" #include #include @@ -173,7 +173,7 @@ void Engine::saveScreenshot() { void Engine::run() { if (params.headless) { - TestMainloop(*this).run(); + ServerMainloop(*this).run(); } else { Mainloop(*this).run(); } diff --git a/src/engine.hpp b/src/engine.hpp index 63a78084..88a60865 100644 --- a/src/engine.hpp +++ b/src/engine.hpp @@ -48,9 +48,10 @@ public: struct CoreParameters { bool headless = false; + bool testMode = false; std::filesystem::path resFolder {"res"}; std::filesystem::path userFolder {"."}; - std::filesystem::path testFile; + std::filesystem::path scriptFile; }; class Engine : public util::ObjectsKeeper { diff --git a/src/logic/scripting/lua/lua_engine.cpp b/src/logic/scripting/lua/lua_engine.cpp index 0a4e2259..b34b87a0 100644 --- a/src/logic/scripting/lua/lua_engine.cpp +++ b/src/logic/scripting/lua/lua_engine.cpp @@ -121,7 +121,7 @@ void lua::initialize(const EnginePaths& paths, const CoreParameters& params) { main_thread = create_state( paths, params.headless ? StateType::TEST : StateType::BASE ); - lua::pushstring(main_thread, params.testFile.stem().u8string()); + lua::pushstring(main_thread, params.scriptFile.stem().u8string()); lua::setglobal(main_thread, "__VC_TEST_NAME"); } diff --git a/src/util/command_line.cpp b/src/util/command_line.cpp index 768c6260..25d7b554 100644 --- a/src/util/command_line.cpp +++ b/src/util/command_line.cpp @@ -19,19 +19,30 @@ static bool perform_keyword( auto token = reader.next(); params.userFolder = fs::u8path(token); } else if (keyword == "--help" || keyword == "-h") { - std::cout << "VoxelEngine command-line arguments:\n"; + std::cout << "VoxelCore v" << ENGINE_VERSION_STRING << "\n\n"; + std::cout << "command-line arguments:\n"; std::cout << " --help - show help\n"; + std::cout << " --version - print engine version\n"; std::cout << " --res - set resources directory\n"; std::cout << " --dir - set userfiles directory\n"; std::cout << " --headless - run in headless mode\n"; std::cout << " --test - test script file\n"; + std::cout << " --script - main script file\n"; std::cout << std::endl; return false; + } else if (keyword == "--version") { + std::cout << ENGINE_VERSION_STRING << std::endl; + return false; } else if (keyword == "--headless") { params.headless = true; } else if (keyword == "--test") { auto token = reader.next(); - params.testFile = fs::u8path(token); + params.testMode = true; + params.scriptFile = fs::u8path(token); + } else if (keyword == "--script") { + auto token = reader.next(); + params.testMode = false; + params.scriptFile = fs::u8path(token); } else { throw std::runtime_error("unknown argument " + keyword); } diff --git a/src/world/Level.hpp b/src/world/Level.hpp index 63a724c9..121fecc1 100644 --- a/src/world/Level.hpp +++ b/src/world/Level.hpp @@ -27,7 +27,6 @@ public: std::unique_ptr chunks; std::unique_ptr inventories; - std::unique_ptr physics; std::unique_ptr events; std::unique_ptr entities;