From 8a3d208ba81a7413607b0feb3e1414b03a3dde11 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Fri, 15 Feb 2019 22:24:04 +0500 Subject: Added lua-support --- src/AssetManager.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/AssetManager.hpp | 5 +++++ 2 files changed, 55 insertions(+) (limited to 'src') diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 71800d7..1ae9929 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -11,6 +11,7 @@ #define STB_IMAGE_IMPLEMENTATION #define STBI_ONLY_PNG #include +#include #include "Utility.hpp" @@ -24,10 +25,12 @@ std::map blockIdToBlockName; std::unique_ptr assetTree; std::unique_ptr atlas; std::map blockIdToBlockFaces; +sol::state lua; void LoadIds(); void LoadAssets(); void LoadTextures(); +void LoadScripts(); void WalkDirEntry(const fs::directory_entry &dirEntry, AssetTreeNode *node); void ParseAsset(AssetTreeNode &node); @@ -35,6 +38,7 @@ void ParseAssetTexture(AssetTreeNode &node); void ParseAssetBlockModel(AssetTreeNode &node); void ParseAssetBlockState(AssetTreeNode &node); void ParseAssetShader(AssetTreeNode &node); +void ParseAssetScript(AssetTreeNode &node); void ParseBlockModels(); @@ -58,6 +62,7 @@ void AssetManager::InitAssetManager() LoadIds(); ParseBlockModels(); + LoadScripts(); } void LoadIds() { @@ -96,6 +101,38 @@ void LoadTextures() { atlas = std::make_unique(textureData); } +void LoadScripts() { + lua.open_libraries(sol::lib::base, sol::lib::table); + + LOG(INFO) << "Loading lua-init-scripts"; + std::vector loadedScripts; + std::vector failedScripts; + + AssetTreeNode *node = AssetManager::GetAssetByAssetName("/"); + for (auto &it : node->childs) { + for (auto &child : it->childs) { + if (child->name == "init") { + AssetScript *asset = dynamic_cast(child->asset.get()); + if (!asset) { + LOG(ERROR) << "Unrecognised script file /" << it->name; + continue; + } + try { + lua.script(asset->code); + } + catch (sol::error &e) { + LOG(ERROR) << "LUA init-script " << child->name << " failed: " << e.what(); + failedScripts.push_back(it->name); + continue; + } + loadedScripts.push_back(it->name); + } + } + } + + LOG(INFO) << "Lua loaded: " << loadedScripts.size() << " failed: " << failedScripts.size(); +} + void WalkDirEntry(const fs::directory_entry &dirEntry, AssetTreeNode *node) { for (auto &file : fs::directory_iterator(dirEntry)) { node->childs.push_back(std::make_unique()); @@ -138,6 +175,11 @@ void ParseAsset(AssetTreeNode &node) { ParseAssetShader(node); return; } + + if (node.name == "init") { + ParseAssetScript(node); + return; + } } void ParseAssetTexture(AssetTreeNode &node) { @@ -380,6 +422,14 @@ void ParseAssetShader(AssetTreeNode &node) { } } +void ParseAssetScript(AssetTreeNode &node) { + node.asset = std::make_unique(); + AssetScript *asset = dynamic_cast(node.asset.get()); + asset->code = std::string((char*)node.data.data(), (char*)node.data.data() + node.data.size()); + node.data.clear(); + node.data.shrink_to_fit(); +} + void ParseBlockModels() { std::string textureName; diff --git a/src/AssetManager.hpp b/src/AssetManager.hpp index de3881e..97e1d63 100644 --- a/src/AssetManager.hpp +++ b/src/AssetManager.hpp @@ -10,6 +10,7 @@ #include #include #include +#include #include "Vector.hpp" #include "Block.hpp" @@ -167,6 +168,10 @@ struct AssetShader : Asset { std::unique_ptr shader; }; +struct AssetScript : Asset { + std::string code; +}; + namespace AssetManager { void InitAssetManager(); -- cgit v1.2.3 From 404b04570452fe24676b0294fdd77878806a904b Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sun, 17 Feb 2019 13:11:08 +0500 Subject: Minor lua-api improvement --- src/AssetManager.cpp | 53 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 1ae9929..361e2ef 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -103,34 +103,55 @@ void LoadTextures() { void LoadScripts() { lua.open_libraries(sol::lib::base, sol::lib::table); + lua["AC"] = lua.create_table(); + lua["plugins"] = lua.create_table(); + lua["AC"]["RegisterPlugin"].set_function([&](sol::table &self, sol::table &plugin) { + std::string pluginName; + try { + pluginName = plugin["name"]; + lua["plugins"][pluginName] = plugin; + LOG(INFO) << "Loading plugin " << (lua["plugins"][pluginName]["displayName"] ? lua["plugins"][pluginName]["displayName"] : pluginName); + if (lua["plugins"][pluginName]["onLoad"]) + lua["plugins"][pluginName]["onLoad"].call(lua["plugins"][pluginName]); + } catch (sol::error &e) { + if (pluginName.empty()) + return; + + LOG(ERROR) << "Plugin " << pluginName << " loading failed: " << e.what(); + lua["plugins"][pluginName] = sol::lua_nil; + } + }); - LOG(INFO) << "Loading lua-init-scripts"; + LOG(INFO) << "Loading Lua..."; std::vector loadedScripts; std::vector failedScripts; AssetTreeNode *node = AssetManager::GetAssetByAssetName("/"); for (auto &it : node->childs) { for (auto &child : it->childs) { - if (child->name == "init") { - AssetScript *asset = dynamic_cast(child->asset.get()); - if (!asset) { - LOG(ERROR) << "Unrecognised script file /" << it->name; - continue; - } - try { - lua.script(asset->code); - } - catch (sol::error &e) { - LOG(ERROR) << "LUA init-script " << child->name << " failed: " << e.what(); - failedScripts.push_back(it->name); - continue; + if (child->name == "scripts") { + for (auto &script : child->childs) + { + AssetScript *asset = dynamic_cast(script->asset.get()); + if (!asset) { + LOG(ERROR) << "Unrecognised script file /" << it->name; + continue; + } + try { + lua.script(asset->code); + } + catch (sol::error &e) { + LOG(ERROR) << "LUA script " << it->name << "/" << child->name << "/" << script->name << " parsing failed: " << e.what(); + failedScripts.push_back(it->name); + continue; + } + loadedScripts.push_back(it->name); } - loadedScripts.push_back(it->name); } } } - LOG(INFO) << "Lua loaded: " << loadedScripts.size() << " failed: " << failedScripts.size(); + LOG(INFO) << "Lua loaded: " << loadedScripts.size() << " failed: " << failedScripts.size(); } void WalkDirEntry(const fs::directory_entry &dirEntry, AssetTreeNode *node) { -- cgit v1.2.3 From a2fd708de4ede7427589125e680f3fb339926f4e Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sun, 17 Feb 2019 21:24:52 +0500 Subject: Refactored lua-api --- src/AssetManager.cpp | 42 ++++----------------------- src/AssetManager.hpp | 1 - src/Plugin.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/Plugin.hpp | 11 +++++++ src/Render.cpp | 17 ++++++++++- 5 files changed, 113 insertions(+), 39 deletions(-) create mode 100644 src/Plugin.cpp create mode 100644 src/Plugin.hpp (limited to 'src') diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 361e2ef..66e7767 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -11,9 +11,9 @@ #define STB_IMAGE_IMPLEMENTATION #define STBI_ONLY_PNG #include -#include #include "Utility.hpp" +#include "Plugin.hpp" namespace fs = std::experimental::filesystem::v1; @@ -25,7 +25,6 @@ std::map blockIdToBlockName; std::unique_ptr assetTree; std::unique_ptr atlas; std::map blockIdToBlockFaces; -sol::state lua; void LoadIds(); void LoadAssets(); @@ -62,6 +61,8 @@ void AssetManager::InitAssetManager() LoadIds(); ParseBlockModels(); + + PluginSystem::Init(); LoadScripts(); } @@ -102,30 +103,6 @@ void LoadTextures() { } void LoadScripts() { - lua.open_libraries(sol::lib::base, sol::lib::table); - lua["AC"] = lua.create_table(); - lua["plugins"] = lua.create_table(); - lua["AC"]["RegisterPlugin"].set_function([&](sol::table &self, sol::table &plugin) { - std::string pluginName; - try { - pluginName = plugin["name"]; - lua["plugins"][pluginName] = plugin; - LOG(INFO) << "Loading plugin " << (lua["plugins"][pluginName]["displayName"] ? lua["plugins"][pluginName]["displayName"] : pluginName); - if (lua["plugins"][pluginName]["onLoad"]) - lua["plugins"][pluginName]["onLoad"].call(lua["plugins"][pluginName]); - } catch (sol::error &e) { - if (pluginName.empty()) - return; - - LOG(ERROR) << "Plugin " << pluginName << " loading failed: " << e.what(); - lua["plugins"][pluginName] = sol::lua_nil; - } - }); - - LOG(INFO) << "Loading Lua..."; - std::vector loadedScripts; - std::vector failedScripts; - AssetTreeNode *node = AssetManager::GetAssetByAssetName("/"); for (auto &it : node->childs) { for (auto &child : it->childs) { @@ -137,21 +114,12 @@ void LoadScripts() { LOG(ERROR) << "Unrecognised script file /" << it->name; continue; } - try { - lua.script(asset->code); - } - catch (sol::error &e) { - LOG(ERROR) << "LUA script " << it->name << "/" << child->name << "/" << script->name << " parsing failed: " << e.what(); - failedScripts.push_back(it->name); - continue; - } - loadedScripts.push_back(it->name); + PluginSystem::Execute(asset->code); } } } } - - LOG(INFO) << "Lua loaded: " << loadedScripts.size() << " failed: " << failedScripts.size(); + LOG(INFO) << "Scripts loaded"; } void WalkDirEntry(const fs::directory_entry &dirEntry, AssetTreeNode *node) { diff --git a/src/AssetManager.hpp b/src/AssetManager.hpp index 97e1d63..bf948b3 100644 --- a/src/AssetManager.hpp +++ b/src/AssetManager.hpp @@ -10,7 +10,6 @@ #include #include #include -#include #include "Vector.hpp" #include "Block.hpp" diff --git a/src/Plugin.cpp b/src/Plugin.cpp new file mode 100644 index 0000000..40ff82f --- /dev/null +++ b/src/Plugin.cpp @@ -0,0 +1,81 @@ +#include "Plugin.hpp" + +#include + +#include +#include + +struct Plugin { + const std::string name; + const std::string displayName; + const std::function onLoad; + const std::function onUnload; + const std::function onChangeState; +}; + + +std::vector plugins; +sol::state lua; + + +namespace PluginApi { + + void RegisterPlugin(sol::table &self, sol::table &plugin) { + Plugin nativePlugin { + plugin["name"].get_or("75"), + plugin["displayName"].get_or(""), + plugin["onLoad"].get_or(std::function()), + plugin["onUnload"].get_or(std::function()), + plugin["onChangeState"].get_or(std::function()) + }; + plugins.push_back(nativePlugin); + nativePlugin.onLoad(); + + LOG(INFO) << "Loaded plugin " << (!nativePlugin.displayName.empty() ? nativePlugin.displayName : nativePlugin.name); + } + + void LogWarning(sol::table &self, std::string text) { + LOG(WARNING) << text; + } + +} + +void PluginSystem::Init() +{ + LOG(INFO) << "Initializing plugin system"; + for (Plugin &plugin : plugins) { + if (plugin.onUnload) + plugin.onUnload(); + } + + plugins.clear(); + lua = sol::state(); + lua.open_libraries(); + + sol::table apiTable = lua["AC"].get_or_create(); + + apiTable["RegisterPlugin"] = PluginApi::RegisterPlugin; + apiTable["LogWarning"] = PluginApi::LogWarning; +} + +void PluginSystem::Execute(const std::string &luaCode) +{ + try { + lua.safe_script(luaCode); + } catch (sol::error &e) { + LOG(ERROR) << e.what(); + } +} + +void PluginSystem::CallOnChangeState(std::string newState) +{ + for (Plugin &plugin : plugins) { + if (plugin.onChangeState) + try { + plugin.onChangeState(newState); + } + catch (sol::error &e) { + LOG(ERROR) << e.what(); + } + } +} diff --git a/src/Plugin.hpp b/src/Plugin.hpp new file mode 100644 index 0000000..a5f75e1 --- /dev/null +++ b/src/Plugin.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace PluginSystem { + void Init(); + + void Execute(const std::string &luaCode); + + void CallOnChangeState(std::string newState); +} \ No newline at end of file diff --git a/src/Render.cpp b/src/Render.cpp index cf108e4..f86e5ec 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -14,6 +14,7 @@ #include "RendererWorld.hpp" #include "Settings.hpp" #include "Framebuffer.hpp" +#include "Plugin.hpp" Render::Render(unsigned int windowWidth, unsigned int windowHeight, std::string windowTitle) @@ -71,7 +72,9 @@ Render::~Render() { Settings::WriteDouble("brightness", fieldBrightness); Settings::WriteDouble("resolutionScale", fieldResolutionScale); Settings::Save(); - + + PluginSystem::Init(); + framebuffer.reset(); ImGui_ImplSdlGL3_Shutdown(); SDL_GL_DeleteContext(glContext); @@ -717,13 +720,25 @@ void Render::InitEvents() { switch (GlobalState::GetState()) { case State::Playing: SetMouseCapture(true); + PluginSystem::CallOnChangeState("Playing"); break; case State::InitialLoading: + PluginSystem::CallOnChangeState("InitialLoading"); + break; case State::MainMenu: + PluginSystem::CallOnChangeState("MainMenu"); + break; case State::Loading: + PluginSystem::CallOnChangeState("Loading"); + break; case State::Paused: + PluginSystem::CallOnChangeState("Paused"); + break; case State::Inventory: + PluginSystem::CallOnChangeState("Inventory"); + break; case State::Chat: + PluginSystem::CallOnChangeState("Chat"); SetMouseCapture(false); break; } -- cgit v1.2.3 From 93ab4aa89d8d81e5dd042201b75165f8db4968bd Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Mon, 18 Feb 2019 09:03:05 +0500 Subject: Trying to fix build in linux --- src/Plugin.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/Plugin.cpp b/src/Plugin.cpp index 40ff82f..41c690c 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -20,7 +20,7 @@ sol::state lua; namespace PluginApi { - void RegisterPlugin(sol::table &self, sol::table &plugin) { + void RegisterPlugin(sol::table self, sol::table plugin) { Plugin nativePlugin { plugin["name"].get_or("75"), plugin["displayName"].get_or(""), @@ -34,7 +34,7 @@ namespace PluginApi { LOG(INFO) << "Loaded plugin " << (!nativePlugin.displayName.empty() ? nativePlugin.displayName : nativePlugin.name); } - void LogWarning(sol::table &self, std::string text) { + void LogWarning(sol::table self, std::string text) { LOG(WARNING) << text; } -- cgit v1.2.3 From d4e9d6bbc0abe66acb149a358a25c9a0f385ce74 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sun, 21 Apr 2019 18:06:55 +0500 Subject: Renamed GlobalState to Game --- src/Game.cpp | 238 +++++++++++++++++++++++++++++++++++++++++++++++++ src/Game.hpp | 25 ++++++ src/GlobalState.cpp | 238 ------------------------------------------------- src/GlobalState.hpp | 25 ------ src/Render.cpp | 2 +- src/RendererEntity.cpp | 2 +- src/main.cpp | 2 +- 7 files changed, 266 insertions(+), 266 deletions(-) create mode 100644 src/Game.cpp create mode 100644 src/Game.hpp delete mode 100644 src/GlobalState.cpp delete mode 100644 src/GlobalState.hpp (limited to 'src') diff --git a/src/Game.cpp b/src/Game.cpp new file mode 100644 index 0000000..46785b1 --- /dev/null +++ b/src/Game.cpp @@ -0,0 +1,238 @@ +#include "Game.hpp" + +#include "NetworkClient.hpp" +#include "GameState.hpp" +#include "Render.hpp" +#include "DebugInfo.hpp" +#include "Event.hpp" + +//Global game variables +std::unique_ptr nc; +std::unique_ptr gs; +std::shared_ptr gsReadOnly; +std::unique_ptr render; +bool isRunning; +bool isPhysRunning; +EventListener listener; +bool isMoving[5] = { 0,0,0,0,0 }; +std::thread threadPhys; +State state; +std::mutex gsCopyMutex; + +void PhysExec(); + +void InitEvents() { + /* + * Network Events + */ + + listener.RegisterHandler("Exit", [](const Event&) { + isRunning = false; + }); + + listener.RegisterHandler("ConnectToServer", [](const Event& eventData) { + auto data = eventData.get >(); //address,port,username + if (std::get<0>(data) == "" || std::get<1>(data) == 0) + LOG(FATAL) << "NOT VALID CONNECT-TO-SERVER EVENT"; + if (nc != nullptr) { + LOG(ERROR) << "Already connected"; + return; + } + LOG(INFO) << "Connecting to server at address " + std::get<0>(data) + ":" + std::to_string(std::get<1>(data)) + " as " + std::get<2>(data); + PUSH_EVENT("Connecting",0); + gs = std::make_unique(); + isPhysRunning = true; + threadPhys = std::thread(&PhysExec); + try { + nc = std::make_unique(std::get<0>(data), + std::get<1>(data), + std::get<2>(data)); + } catch (std::exception &e) { + LOG(WARNING) << "Connection failed"; + PUSH_EVENT("ConnectionFailed", std::string(e.what())); + isPhysRunning = false; + threadPhys.join(); + gs.reset(); + return; + } + LOG(INFO) << "Connected to server"; + PUSH_EVENT("ConnectionSuccessfull", 0); + }); + + listener.RegisterHandler("Disconnect", [](const Event& eventData) { + auto data = eventData.get(); + PUSH_EVENT("Disconnected", data); + LOG(INFO) << "Disconnected: " << data; + nc.reset(); + }); + + listener.RegisterHandler("NetworkClientException", [](const Event& eventData) { + auto data = eventData.get < std::string>(); + PUSH_EVENT("Disconnect", data); + }); + + /* + * GameState Events + */ + + listener.RegisterHandler("Exit", [](const Event&) { + isRunning = false; + }); + + listener.RegisterHandler("Disconnected", [](const Event&) { + if (!gs) + return; + isPhysRunning = false; + threadPhys.join(); + gs.reset(); + }); + + listener.RegisterHandler("SendChatMessage", [](const Event& eventData) { + auto message = eventData.get(); + auto packet = std::static_pointer_cast(std::make_shared(message)); + PUSH_EVENT("SendPacket",packet); + }); +} + +void PhysExec() { + EventListener listener; + + listener.RegisterHandler("KeyPressed", [](const Event& eventData) { + if (!gs) + return; + switch (eventData.get()) { + case SDL_SCANCODE_W: + isMoving[GameState::FORWARD] = true; + break; + case SDL_SCANCODE_A: + isMoving[GameState::LEFT] = true; + break; + case SDL_SCANCODE_S: + isMoving[GameState::BACKWARD] = true; + break; + case SDL_SCANCODE_D: + isMoving[GameState::RIGHT] = true; + break; + case SDL_SCANCODE_SPACE: + isMoving[GameState::JUMP] = true; + break; + default: + break; + } + }); + + listener.RegisterHandler("KeyReleased", [](const Event& eventData) { + if (!gs) + return; + switch (eventData.get()) { + case SDL_SCANCODE_W: + isMoving[GameState::FORWARD] = false; + break; + case SDL_SCANCODE_A: + isMoving[GameState::LEFT] = false; + break; + case SDL_SCANCODE_S: + isMoving[GameState::BACKWARD] = false; + break; + case SDL_SCANCODE_D: + isMoving[GameState::RIGHT] = false; + break; + case SDL_SCANCODE_SPACE: + isMoving[GameState::JUMP] = false; + break; + default: + break; + } + }); + + listener.RegisterHandler("MouseMove", [](const Event& eventData) { + if (!gs) + return; + auto data = eventData.get>(); + gs->HandleRotation(std::get<0>(data),std::get<1>(data)); + }); + + listener.RegisterHandler("ReceivedPacket", [](const Event& eventData) { + std::shared_ptr packet = eventData.get>(); + gs->UpdatePacket(packet); + }); + + listener.RegisterHandler("LmbPressed",[](const Event& eventData) { + gs->StartDigging(); + }); + + listener.RegisterHandler("LmbReleased",[](const Event& eventData) { + gs->CancelDigging(); + }); + + listener.RegisterHandler("RmbPressed", [](const Event& eventData) { + gs->PlaceBlock(); + }); + + listener.RegisterHandler("SelectedBlockChanged", [](const Event& eventData) { + //TODO: + //gs->CancelDigging(); + }); + + LoopExecutionTimeController timer(std::chrono::milliseconds(8)); + + while (isPhysRunning) { + DebugInfo::gameThreadTime = timer.GetRealDeltaS() * 1000'00.0f; + + if (state == State::Playing) { + if (isMoving[GameState::FORWARD]) + gs->HandleMovement(GameState::FORWARD, timer.GetRealDeltaS()); + if (isMoving[GameState::BACKWARD]) + gs->HandleMovement(GameState::BACKWARD, timer.GetRealDeltaS()); + if (isMoving[GameState::LEFT]) + gs->HandleMovement(GameState::LEFT, timer.GetRealDeltaS()); + if (isMoving[GameState::RIGHT]) + gs->HandleMovement(GameState::RIGHT, timer.GetRealDeltaS()); + if (isMoving[GameState::JUMP]) + gs->HandleMovement(GameState::JUMP, timer.GetRealDeltaS()); + } + + gs->Update(timer.GetRealDeltaS()); + + listener.HandleAllEvents(); + + gsCopyMutex.lock(); + gsReadOnly = std::make_shared(*gs.get()); + gsCopyMutex.unlock(); + + timer.Update(); + } +} + +void GlobalState::Exec() { + render = std::make_unique(900, 480, "AltCraft"); + isRunning = true; + InitEvents(); + GlobalState::SetState(State::MainMenu); + while (isRunning) { + render->Update(); + listener.HandleAllEvents(); + } + PUSH_EVENT("Exit", 0); + isRunning = false; + render.reset(); +} + +std::shared_ptr GlobalState::GetGameState() { + std::lock_guard guard(gsCopyMutex); + return gsReadOnly; +} + +Render *GlobalState::GetRender() { + return render.get(); +} + +State GlobalState::GetState() { + return state; +} + +void GlobalState::SetState(const State &newState) { + if (newState != state) + PUSH_EVENT("StateUpdated", 0); + state = newState; +} diff --git a/src/Game.hpp b/src/Game.hpp new file mode 100644 index 0000000..bc7224f --- /dev/null +++ b/src/Game.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include + +class NetworkClient; +class GameState; +class Render; + +enum class State { + InitialLoading, + MainMenu, + Loading, + Playing, + Paused, + Inventory, + Chat, +}; + +struct GlobalState { + static std::shared_ptr GetGameState(); + static Render *GetRender(); + static void Exec(); + static State GetState(); + static void SetState(const State &newState); +}; \ No newline at end of file diff --git a/src/GlobalState.cpp b/src/GlobalState.cpp deleted file mode 100644 index afa1a56..0000000 --- a/src/GlobalState.cpp +++ /dev/null @@ -1,238 +0,0 @@ -#include "GlobalState.hpp" - -#include "NetworkClient.hpp" -#include "GameState.hpp" -#include "Render.hpp" -#include "DebugInfo.hpp" -#include "Event.hpp" - -//Global game variables -std::unique_ptr nc; -std::unique_ptr gs; -std::shared_ptr gsReadOnly; -std::unique_ptr render; -bool isRunning; -bool isPhysRunning; -EventListener listener; -bool isMoving[5] = { 0,0,0,0,0 }; -std::thread threadPhys; -State state; -std::mutex gsCopyMutex; - -void PhysExec(); - -void InitEvents() { - /* - * Network Events - */ - - listener.RegisterHandler("Exit", [](const Event&) { - isRunning = false; - }); - - listener.RegisterHandler("ConnectToServer", [](const Event& eventData) { - auto data = eventData.get >(); //address,port,username - if (std::get<0>(data) == "" || std::get<1>(data) == 0) - LOG(FATAL) << "NOT VALID CONNECT-TO-SERVER EVENT"; - if (nc != nullptr) { - LOG(ERROR) << "Already connected"; - return; - } - LOG(INFO) << "Connecting to server at address " + std::get<0>(data) + ":" + std::to_string(std::get<1>(data)) + " as " + std::get<2>(data); - PUSH_EVENT("Connecting",0); - gs = std::make_unique(); - isPhysRunning = true; - threadPhys = std::thread(&PhysExec); - try { - nc = std::make_unique(std::get<0>(data), - std::get<1>(data), - std::get<2>(data)); - } catch (std::exception &e) { - LOG(WARNING) << "Connection failed"; - PUSH_EVENT("ConnectionFailed", std::string(e.what())); - isPhysRunning = false; - threadPhys.join(); - gs.reset(); - return; - } - LOG(INFO) << "Connected to server"; - PUSH_EVENT("ConnectionSuccessfull", 0); - }); - - listener.RegisterHandler("Disconnect", [](const Event& eventData) { - auto data = eventData.get(); - PUSH_EVENT("Disconnected", data); - LOG(INFO) << "Disconnected: " << data; - nc.reset(); - }); - - listener.RegisterHandler("NetworkClientException", [](const Event& eventData) { - auto data = eventData.get < std::string>(); - PUSH_EVENT("Disconnect", data); - }); - - /* - * GameState Events - */ - - listener.RegisterHandler("Exit", [](const Event&) { - isRunning = false; - }); - - listener.RegisterHandler("Disconnected", [](const Event&) { - if (!gs) - return; - isPhysRunning = false; - threadPhys.join(); - gs.reset(); - }); - - listener.RegisterHandler("SendChatMessage", [](const Event& eventData) { - auto message = eventData.get(); - auto packet = std::static_pointer_cast(std::make_shared(message)); - PUSH_EVENT("SendPacket",packet); - }); -} - -void PhysExec() { - EventListener listener; - - listener.RegisterHandler("KeyPressed", [](const Event& eventData) { - if (!gs) - return; - switch (eventData.get()) { - case SDL_SCANCODE_W: - isMoving[GameState::FORWARD] = true; - break; - case SDL_SCANCODE_A: - isMoving[GameState::LEFT] = true; - break; - case SDL_SCANCODE_S: - isMoving[GameState::BACKWARD] = true; - break; - case SDL_SCANCODE_D: - isMoving[GameState::RIGHT] = true; - break; - case SDL_SCANCODE_SPACE: - isMoving[GameState::JUMP] = true; - break; - default: - break; - } - }); - - listener.RegisterHandler("KeyReleased", [](const Event& eventData) { - if (!gs) - return; - switch (eventData.get()) { - case SDL_SCANCODE_W: - isMoving[GameState::FORWARD] = false; - break; - case SDL_SCANCODE_A: - isMoving[GameState::LEFT] = false; - break; - case SDL_SCANCODE_S: - isMoving[GameState::BACKWARD] = false; - break; - case SDL_SCANCODE_D: - isMoving[GameState::RIGHT] = false; - break; - case SDL_SCANCODE_SPACE: - isMoving[GameState::JUMP] = false; - break; - default: - break; - } - }); - - listener.RegisterHandler("MouseMove", [](const Event& eventData) { - if (!gs) - return; - auto data = eventData.get>(); - gs->HandleRotation(std::get<0>(data),std::get<1>(data)); - }); - - listener.RegisterHandler("ReceivedPacket", [](const Event& eventData) { - std::shared_ptr packet = eventData.get>(); - gs->UpdatePacket(packet); - }); - - listener.RegisterHandler("LmbPressed",[](const Event& eventData) { - gs->StartDigging(); - }); - - listener.RegisterHandler("LmbReleased",[](const Event& eventData) { - gs->CancelDigging(); - }); - - listener.RegisterHandler("RmbPressed", [](const Event& eventData) { - gs->PlaceBlock(); - }); - - listener.RegisterHandler("SelectedBlockChanged", [](const Event& eventData) { - //TODO: - //gs->CancelDigging(); - }); - - LoopExecutionTimeController timer(std::chrono::milliseconds(8)); - - while (isPhysRunning) { - DebugInfo::gameThreadTime = timer.GetRealDeltaS() * 1000'00.0f; - - if (state == State::Playing) { - if (isMoving[GameState::FORWARD]) - gs->HandleMovement(GameState::FORWARD, timer.GetRealDeltaS()); - if (isMoving[GameState::BACKWARD]) - gs->HandleMovement(GameState::BACKWARD, timer.GetRealDeltaS()); - if (isMoving[GameState::LEFT]) - gs->HandleMovement(GameState::LEFT, timer.GetRealDeltaS()); - if (isMoving[GameState::RIGHT]) - gs->HandleMovement(GameState::RIGHT, timer.GetRealDeltaS()); - if (isMoving[GameState::JUMP]) - gs->HandleMovement(GameState::JUMP, timer.GetRealDeltaS()); - } - - gs->Update(timer.GetRealDeltaS()); - - listener.HandleAllEvents(); - - gsCopyMutex.lock(); - gsReadOnly = std::make_shared(*gs.get()); - gsCopyMutex.unlock(); - - timer.Update(); - } -} - -void GlobalState::Exec() { - render = std::make_unique(900, 480, "AltCraft"); - isRunning = true; - InitEvents(); - GlobalState::SetState(State::MainMenu); - while (isRunning) { - render->Update(); - listener.HandleAllEvents(); - } - PUSH_EVENT("Exit", 0); - isRunning = false; - render.reset(); -} - -std::shared_ptr GlobalState::GetGameState() { - std::lock_guard guard(gsCopyMutex); - return gsReadOnly; -} - -Render *GlobalState::GetRender() { - return render.get(); -} - -State GlobalState::GetState() { - return state; -} - -void GlobalState::SetState(const State &newState) { - if (newState != state) - PUSH_EVENT("StateUpdated", 0); - state = newState; -} diff --git a/src/GlobalState.hpp b/src/GlobalState.hpp deleted file mode 100644 index bc7224f..0000000 --- a/src/GlobalState.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include - -class NetworkClient; -class GameState; -class Render; - -enum class State { - InitialLoading, - MainMenu, - Loading, - Playing, - Paused, - Inventory, - Chat, -}; - -struct GlobalState { - static std::shared_ptr GetGameState(); - static Render *GetRender(); - static void Exec(); - static State GetState(); - static void SetState(const State &newState); -}; \ No newline at end of file diff --git a/src/Render.cpp b/src/Render.cpp index f86e5ec..a20ce21 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -8,7 +8,7 @@ #include "AssetManager.hpp" #include "Event.hpp" #include "DebugInfo.hpp" -#include "GlobalState.hpp" +#include "Game.hpp" #include "World.hpp" #include "GameState.hpp" #include "RendererWorld.hpp" diff --git a/src/RendererEntity.cpp b/src/RendererEntity.cpp index 25403be..ed1e854 100644 --- a/src/RendererEntity.cpp +++ b/src/RendererEntity.cpp @@ -7,7 +7,7 @@ #include "GameState.hpp" #include "Renderer.hpp" #include "AssetManager.hpp" -#include "GlobalState.hpp" +#include "Game.hpp" const GLfloat vertices[] = { -0.5f, 0.5f, 0.5f, diff --git a/src/main.cpp b/src/main.cpp index cb2daa8..100b7bc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,6 @@ #include "Event.hpp" #include "Utility.hpp" -#include "GlobalState.hpp" +#include "Game.hpp" #include -- cgit v1.2.3 From 9d9a415b9a6e4a9ad75256e10f68e4ce55dd5f95 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sun, 21 Apr 2019 19:04:53 +0500 Subject: Basic single-threaded implementation --- src/Game.cpp | 352 ++++++++++++++++++++++--------------------------- src/Game.hpp | 36 ++--- src/Render.cpp | 59 ++++----- src/RendererEntity.cpp | 1 - src/RendererWorld.cpp | 54 ++++---- src/RendererWorld.hpp | 9 +- src/main.cpp | 2 +- 7 files changed, 231 insertions(+), 282 deletions(-) (limited to 'src') diff --git a/src/Game.cpp b/src/Game.cpp index 46785b1..927cdc2 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -1,238 +1,200 @@ #include "Game.hpp" -#include "NetworkClient.hpp" -#include "GameState.hpp" +#include + #include "Render.hpp" -#include "DebugInfo.hpp" -#include "Event.hpp" +#include "GameState.hpp" +#include "NetworkClient.hpp" -//Global game variables +bool isRunning = true; +bool isMoving[5] = { 0,0,0,0,0 }; +State state; std::unique_ptr nc; std::unique_ptr gs; -std::shared_ptr gsReadOnly; std::unique_ptr render; -bool isRunning; -bool isPhysRunning; EventListener listener; -bool isMoving[5] = { 0,0,0,0,0 }; -std::thread threadPhys; -State state; -std::mutex gsCopyMutex; - -void PhysExec(); void InitEvents() { - /* - * Network Events - */ + /* + * Network Events + */ - listener.RegisterHandler("Exit", [](const Event&) { - isRunning = false; - }); - - listener.RegisterHandler("ConnectToServer", [](const Event& eventData) { + listener.RegisterHandler("ConnectToServer", [](const Event & eventData) { auto data = eventData.get >(); //address,port,username - if (std::get<0>(data) == "" || std::get<1>(data) == 0) - LOG(FATAL) << "NOT VALID CONNECT-TO-SERVER EVENT"; - if (nc != nullptr) { - LOG(ERROR) << "Already connected"; - return; - } + if (std::get<0>(data) == "" || std::get<1>(data) == 0) + LOG(FATAL) << "NOT VALID CONNECT-TO-SERVER EVENT"; + if (nc != nullptr) { + LOG(ERROR) << "Already connected"; + return; + } LOG(INFO) << "Connecting to server at address " + std::get<0>(data) + ":" + std::to_string(std::get<1>(data)) + " as " + std::get<2>(data); PUSH_EVENT("Connecting",0); - gs = std::make_unique(); - isPhysRunning = true; - threadPhys = std::thread(&PhysExec); - try { - nc = std::make_unique(std::get<0>(data), - std::get<1>(data), - std::get<2>(data)); - } catch (std::exception &e) { - LOG(WARNING) << "Connection failed"; + gs = std::make_unique(); + try { + nc = std::make_unique(std::get<0>(data), + std::get<1>(data), + std::get<2>(data)); + } catch (std::exception &e) { + LOG(WARNING) << "Connection failed"; PUSH_EVENT("ConnectionFailed", std::string(e.what())); - isPhysRunning = false; - threadPhys.join(); - gs.reset(); - return; - } - LOG(INFO) << "Connected to server"; + gs.reset(); + return; + } + LOG(INFO) << "Connected to server"; PUSH_EVENT("ConnectionSuccessfull", 0); - }); + }); - listener.RegisterHandler("Disconnect", [](const Event& eventData) { + listener.RegisterHandler("Disconnect", [](const Event& eventData) { auto data = eventData.get(); PUSH_EVENT("Disconnected", data); - LOG(INFO) << "Disconnected: " << data; - nc.reset(); - }); + LOG(INFO) << "Disconnected: " << data; + nc.reset(); + }); - listener.RegisterHandler("NetworkClientException", [](const Event& eventData) { + listener.RegisterHandler("NetworkClientException", [](const Event& eventData) { auto data = eventData.get < std::string>(); PUSH_EVENT("Disconnect", data); - }); + }); - /* - * GameState Events - */ + /* + * GameState Events + */ - listener.RegisterHandler("Exit", [](const Event&) { - isRunning = false; - }); + listener.RegisterHandler("Exit", [](const Event&) { + isRunning = false; + }); - listener.RegisterHandler("Disconnected", [](const Event&) { - if (!gs) - return; - isPhysRunning = false; - threadPhys.join(); - gs.reset(); - }); + listener.RegisterHandler("Disconnected", [](const Event&) { + if (!gs) + return; + gs.reset(); + }); - listener.RegisterHandler("SendChatMessage", [](const Event& eventData) { + listener.RegisterHandler("SendChatMessage", [](const Event& eventData) { auto message = eventData.get(); - auto packet = std::static_pointer_cast(std::make_shared(message)); - PUSH_EVENT("SendPacket",packet); - }); + auto packet = std::static_pointer_cast(std::make_shared(message)); + PUSH_EVENT("SendPacket",packet); + }); + + /* + * Phys Events + */ + + listener.RegisterHandler("KeyPressed", [](const Event& eventData) { + if (!gs) + return; + switch (eventData.get()) { + case SDL_SCANCODE_W: + isMoving[GameState::FORWARD] = true; + break; + case SDL_SCANCODE_A: + isMoving[GameState::LEFT] = true; + break; + case SDL_SCANCODE_S: + isMoving[GameState::BACKWARD] = true; + break; + case SDL_SCANCODE_D: + isMoving[GameState::RIGHT] = true; + break; + case SDL_SCANCODE_SPACE: + isMoving[GameState::JUMP] = true; + break; + default: + break; + } + }); + + listener.RegisterHandler("KeyReleased", [](const Event& eventData) { + if (!gs) + return; + switch (eventData.get()) { + case SDL_SCANCODE_W: + isMoving[GameState::FORWARD] = false; + break; + case SDL_SCANCODE_A: + isMoving[GameState::LEFT] = false; + break; + case SDL_SCANCODE_S: + isMoving[GameState::BACKWARD] = false; + break; + case SDL_SCANCODE_D: + isMoving[GameState::RIGHT] = false; + break; + case SDL_SCANCODE_SPACE: + isMoving[GameState::JUMP] = false; + break; + default: + break; + } + }); + + listener.RegisterHandler("MouseMove", [](const Event& eventData) { + if (!gs) + return; + auto data = eventData.get>(); + gs->HandleRotation(std::get<0>(data),std::get<1>(data)); + }); + + listener.RegisterHandler("ReceivedPacket", [](const Event& eventData) { + std::shared_ptr packet = eventData.get>(); + gs->UpdatePacket(packet); + }); + + listener.RegisterHandler("LmbPressed",[](const Event& eventData) { + gs->StartDigging(); + }); + + listener.RegisterHandler("LmbReleased",[](const Event& eventData) { + gs->CancelDigging(); + }); + + listener.RegisterHandler("RmbPressed", [](const Event& eventData) { + gs->PlaceBlock(); + }); + + listener.RegisterHandler("SelectedBlockChanged", [](const Event& eventData) { + //TODO: + //gs->CancelDigging(); + }); } -void PhysExec() { - EventListener listener; - - listener.RegisterHandler("KeyPressed", [](const Event& eventData) { - if (!gs) - return; - switch (eventData.get()) { - case SDL_SCANCODE_W: - isMoving[GameState::FORWARD] = true; - break; - case SDL_SCANCODE_A: - isMoving[GameState::LEFT] = true; - break; - case SDL_SCANCODE_S: - isMoving[GameState::BACKWARD] = true; - break; - case SDL_SCANCODE_D: - isMoving[GameState::RIGHT] = true; - break; - case SDL_SCANCODE_SPACE: - isMoving[GameState::JUMP] = true; - break; - default: - break; - } - }); - - listener.RegisterHandler("KeyReleased", [](const Event& eventData) { - if (!gs) - return; - switch (eventData.get()) { - case SDL_SCANCODE_W: - isMoving[GameState::FORWARD] = false; - break; - case SDL_SCANCODE_A: - isMoving[GameState::LEFT] = false; - break; - case SDL_SCANCODE_S: - isMoving[GameState::BACKWARD] = false; - break; - case SDL_SCANCODE_D: - isMoving[GameState::RIGHT] = false; - break; - case SDL_SCANCODE_SPACE: - isMoving[GameState::JUMP] = false; - break; - default: - break; - } - }); - - listener.RegisterHandler("MouseMove", [](const Event& eventData) { - if (!gs) - return; - auto data = eventData.get>(); - gs->HandleRotation(std::get<0>(data),std::get<1>(data)); - }); - - listener.RegisterHandler("ReceivedPacket", [](const Event& eventData) { - std::shared_ptr packet = eventData.get>(); - gs->UpdatePacket(packet); - }); - - listener.RegisterHandler("LmbPressed",[](const Event& eventData) { - gs->StartDigging(); - }); - - listener.RegisterHandler("LmbReleased",[](const Event& eventData) { - gs->CancelDigging(); - }); - - listener.RegisterHandler("RmbPressed", [](const Event& eventData) { - gs->PlaceBlock(); - }); - - listener.RegisterHandler("SelectedBlockChanged", [](const Event& eventData) { - //TODO: - //gs->CancelDigging(); - }); - - LoopExecutionTimeController timer(std::chrono::milliseconds(8)); - - while (isPhysRunning) { - DebugInfo::gameThreadTime = timer.GetRealDeltaS() * 1000'00.0f; - - if (state == State::Playing) { - if (isMoving[GameState::FORWARD]) - gs->HandleMovement(GameState::FORWARD, timer.GetRealDeltaS()); - if (isMoving[GameState::BACKWARD]) - gs->HandleMovement(GameState::BACKWARD, timer.GetRealDeltaS()); - if (isMoving[GameState::LEFT]) - gs->HandleMovement(GameState::LEFT, timer.GetRealDeltaS()); - if (isMoving[GameState::RIGHT]) - gs->HandleMovement(GameState::RIGHT, timer.GetRealDeltaS()); - if (isMoving[GameState::JUMP]) - gs->HandleMovement(GameState::JUMP, timer.GetRealDeltaS()); - } - - gs->Update(timer.GetRealDeltaS()); +void RunGame() { + InitEvents(); - listener.HandleAllEvents(); + render = std::make_unique(900, 480, "AltCraft"); + + SetState(State::MainMenu); + LoopExecutionTimeController time(std::chrono::milliseconds(16)); - gsCopyMutex.lock(); - gsReadOnly = std::make_shared(*gs.get()); - gsCopyMutex.unlock(); + while (isRunning) { + listener.HandleAllEvents(); + if (gs) + gs->Update(time.GetDeltaS()); + render->Update(); + time.Update(); + } - timer.Update(); - } + render.reset(); } -void GlobalState::Exec() { - render = std::make_unique(900, 480, "AltCraft"); - isRunning = true; - InitEvents(); - GlobalState::SetState(State::MainMenu); - while (isRunning) { - render->Update(); - listener.HandleAllEvents(); - } - PUSH_EVENT("Exit", 0); - isRunning = false; - render.reset(); +State GetState() { + return state; } -std::shared_ptr GlobalState::GetGameState() { - std::lock_guard guard(gsCopyMutex); - return gsReadOnly; +void SetState(State newState) { + if (newState != state) + PUSH_EVENT("StateUpdated", 0); + state = newState; } -Render *GlobalState::GetRender() { - return render.get(); +GameState *GetGameState() { + return gs.get(); } -State GlobalState::GetState() { - return state; +Render *GetRender() { + return render.get(); } -void GlobalState::SetState(const State &newState) { - if (newState != state) - PUSH_EVENT("StateUpdated", 0); - state = newState; +NetworkClient *GetNetworkClient() { + return nc.get(); } diff --git a/src/Game.hpp b/src/Game.hpp index bc7224f..633585f 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -1,25 +1,27 @@ #pragma once -#include - -class NetworkClient; class GameState; class Render; +class NetworkClient; enum class State { - InitialLoading, - MainMenu, - Loading, - Playing, - Paused, - Inventory, - Chat, + InitialLoading, + MainMenu, + Loading, + Playing, + Paused, + Inventory, + Chat, }; -struct GlobalState { - static std::shared_ptr GetGameState(); - static Render *GetRender(); - static void Exec(); - static State GetState(); - static void SetState(const State &newState); -}; \ No newline at end of file +void RunGame(); + +State GetState(); + +void SetState(State newState); + +GameState* GetGameState(); + +Render* GetRender(); + +NetworkClient* GetNetworkClient(); \ No newline at end of file diff --git a/src/Render.cpp b/src/Render.cpp index a20ce21..2e69369 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -8,13 +8,13 @@ #include "AssetManager.hpp" #include "Event.hpp" #include "DebugInfo.hpp" -#include "Game.hpp" #include "World.hpp" #include "GameState.hpp" #include "RendererWorld.hpp" #include "Settings.hpp" #include "Framebuffer.hpp" #include "Plugin.hpp" +#include "Game.hpp" Render::Render(unsigned int windowWidth, unsigned int windowHeight, std::string windowTitle) @@ -228,11 +228,11 @@ void Render::HandleEvents() { case SDL_WINDOWEVENT_FOCUS_LOST: { HasFocus = false; - auto state = GlobalState::GetState(); + State state = GetState(); if (state == State::Inventory || state == State::Playing || state == State::Chat) { - GlobalState::SetState(State::Paused); + SetState(State::Paused); } break; } @@ -244,13 +244,13 @@ void Render::HandleEvents() { case SDL_KEYDOWN: { switch (event.key.keysym.scancode) { case SDL_SCANCODE_ESCAPE: { - auto state = GlobalState::GetState(); + auto state = GetState(); if (state == State::Playing) { - GlobalState::SetState(State::Paused); + SetState(State::Paused); } else if (state == State::Paused || state == State::Inventory || state == State::Chat) { - GlobalState::SetState(State::Playing); + SetState(State::Playing); } else if (state == State::MainMenu) { LOG(INFO) << "Received close event by esc"; PUSH_EVENT("Exit", 0); @@ -260,11 +260,11 @@ void Render::HandleEvents() { } case SDL_SCANCODE_E: { - auto state = GlobalState::GetState(); + auto state = GetState(); if (state == State::Playing) { - GlobalState::SetState(State::Inventory); + SetState(State::Inventory); } else if (state == State::Inventory) { - GlobalState::SetState(State::Playing); + SetState(State::Playing); } break; @@ -273,11 +273,11 @@ void Render::HandleEvents() { case SDL_SCANCODE_SLASH: case SDL_SCANCODE_T: { if (!ImGui::GetIO().WantCaptureKeyboard) { - auto state = GlobalState::GetState(); + auto state = GetState(); if (state == State::Playing) { - GlobalState::SetState(State::Chat); + SetState(State::Chat); } else if (state == State::Chat) { - GlobalState::SetState(State::Playing); + SetState(State::Playing); } } @@ -351,11 +351,8 @@ void Render::SetMouseCapture(bool IsCaptured) { } void Render::Update() { - if (world) - world->UpdateGameState(GlobalState::GetGameState()); - HandleEvents(); - if (HasFocus && GlobalState::GetState() == State::Playing) UpdateKeyboard(); + if (HasFocus && GetState() == State::Playing) UpdateKeyboard(); if (isMouseCaptured) HandleMouseCapture(); glCheckError(); @@ -388,9 +385,9 @@ void Render::RenderGui() { ImGui::Text("FPS: %.1f (%.3fms)", ImGui::GetIO().Framerate, 1000.0f / ImGui::GetIO().Framerate); float gameTime = DebugInfo::gameThreadTime / 100.0f; if (world) { - Entity *playerPtr = world->GameStatePtr()->GetPlayer(); - SelectionStatus selectionStatus = world->GameStatePtr()->GetSelectionStatus(); - const World *worldPtr = &world->GameStatePtr()->GetWorld(); + Entity *playerPtr = GetGameState()->GetPlayer(); + SelectionStatus selectionStatus = GetGameState()->GetSelectionStatus(); + const World *worldPtr = &GetGameState()->GetWorld(); ImGui::Text("TPS: %.1f (%.2fms)", 1000.0f / gameTime, gameTime); ImGui::Text("Sections loaded: %d", (int) DebugInfo::totalSections); @@ -431,7 +428,7 @@ void Render::RenderGui() { ImGui::Text( "Player health: %.1f/%.1f", - world->GameStatePtr()->GetPlayerStatus().health, 20.0f); + GetGameState()->GetPlayerStatus().health, 20.0f); ImGui::Text( "Selected block: %d %d %d : %.1f", @@ -456,7 +453,7 @@ void Render::RenderGui() { ImGui::End(); - switch (GlobalState::GetState()) { + switch (GetState()) { case State::MainMenu: { ImGui::SetNextWindowPosCenter(); ImGui::Begin("Menu", 0, windowFlags); @@ -513,7 +510,7 @@ void Render::RenderGui() { }; ImGui::SetNextWindowPosCenter(); ImGui::Begin("Inventory", 0, windowFlags); - const Window& inventory = world->GameStatePtr()->GetInventory(); + const Window& inventory = GetGameState()->GetInventory(); //Hand and drop slots if (renderSlot(inventory.handSlot, -1)) { @@ -600,7 +597,7 @@ void Render::RenderGui() { ImGui::SetNextWindowPosCenter(); ImGui::Begin("Pause Menu", 0, windowFlags); if (ImGui::Button("Continue")) { - GlobalState::SetState(State::Playing); + SetState(State::Playing); } ImGui::Separator(); @@ -629,7 +626,7 @@ void Render::RenderGui() { if (fieldSensetivity != sensetivity) sensetivity = fieldSensetivity; - world->GameStatePtr()->GetPlayer()->isFlying = fieldFlight; + GetGameState()->GetPlayer()->isFlying = fieldFlight; isWireframe = fieldWireframe; timer.SetDelayLength(std::chrono::duration(1.0 / fieldTargetFps * 1000.0)); @@ -675,7 +672,7 @@ void Render::InitEvents() { listener.RegisterHandler("PlayerConnected", [this](const Event&) { stateString = "Loading terrain..."; - world = std::make_unique(GlobalState::GetGameState()); + world = std::make_unique(); world->MaxRenderingDistance = fieldDistance; PUSH_EVENT("UpdateSectionsRender", 0); }); @@ -683,9 +680,9 @@ void Render::InitEvents() { listener.RegisterHandler("RemoveLoadingScreen", [this](const Event&) { stateString = "Playing"; renderWorld = true; - GlobalState::SetState(State::Playing); + SetState(State::Playing); glClearColor(0, 0, 0, 1.0f); - world->GameStatePtr()->GetPlayer()->isFlying = this->fieldFlight; + GetGameState()->GetPlayer()->isFlying = this->fieldFlight; PUSH_EVENT("SetMinLightLevel", fieldBrightness); }); @@ -693,7 +690,7 @@ void Render::InitEvents() { stateString = "Connection failed: " + eventData.get (); renderWorld = false; world.reset(); - GlobalState::SetState(State::MainMenu); + SetState(State::MainMenu); glClearColor(0.8, 0.8, 0.8, 1.0f); }); @@ -701,13 +698,13 @@ void Render::InitEvents() { stateString = "Disconnected: " + eventData.get(); renderWorld = false; world.reset(); - GlobalState::SetState(State::MainMenu); + SetState(State::MainMenu); glClearColor(0.8, 0.8, 0.8, 1.0f); }); listener.RegisterHandler("Connecting", [this](const Event&) { stateString = "Connecting to the server..."; - GlobalState::SetState(State::Loading); + SetState(State::Loading); }); listener.RegisterHandler("ChatMessageReceived", [this](const Event& eventData) { @@ -717,7 +714,7 @@ void Render::InitEvents() { }); listener.RegisterHandler("StateUpdated", [this](const Event& eventData) { - switch (GlobalState::GetState()) { + switch (GetState()) { case State::Playing: SetMouseCapture(true); PluginSystem::CallOnChangeState("Playing"); diff --git a/src/RendererEntity.cpp b/src/RendererEntity.cpp index ed1e854..fc5e1a2 100644 --- a/src/RendererEntity.cpp +++ b/src/RendererEntity.cpp @@ -7,7 +7,6 @@ #include "GameState.hpp" #include "Renderer.hpp" #include "AssetManager.hpp" -#include "Game.hpp" const GLfloat vertices[] = { -0.5f, 0.5f, 0.5f, diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 7f7c850..bcb6f92 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -12,6 +12,7 @@ #include "GameState.hpp" #include "Section.hpp" #include "RendererSectionData.hpp" +#include "Game.hpp" void RendererWorld::WorkerFunction(size_t workerId) { EventListener tasksListener; @@ -52,13 +53,13 @@ void RendererWorld::ParseQueueUpdate() { vec.y -= 4500; } - parsing[id].data.section = gs->GetWorld().GetSection(vec); - parsing[id].data.north = gs->GetWorld().GetSection(vec + Vector(0, 0, 1)); - parsing[id].data.south = gs->GetWorld().GetSection(vec + Vector(0, 0, -1)); - parsing[id].data.west = gs->GetWorld().GetSection(vec + Vector(1, 0, 0)); - parsing[id].data.east = gs->GetWorld().GetSection(vec + Vector(-1, 0, 0)); - parsing[id].data.bottom = gs->GetWorld().GetSection(vec + Vector(0, -1, 0)); - parsing[id].data.top = gs->GetWorld().GetSection(vec + Vector(0, 1, 0)); + parsing[id].data.section = GetGameState()->GetWorld().GetSection(vec); + parsing[id].data.north = GetGameState()->GetWorld().GetSection(vec + Vector(0, 0, 1)); + parsing[id].data.south = GetGameState()->GetWorld().GetSection(vec + Vector(0, 0, -1)); + parsing[id].data.west = GetGameState()->GetWorld().GetSection(vec + Vector(1, 0, 0)); + parsing[id].data.east = GetGameState()->GetWorld().GetSection(vec + Vector(-1, 0, 0)); + parsing[id].data.bottom = GetGameState()->GetWorld().GetSection(vec + Vector(0, -1, 0)); + parsing[id].data.top = GetGameState()->GetWorld().GetSection(vec + Vector(0, 1, 0)); parsing[id].parsing = true; @@ -86,7 +87,7 @@ void RendererWorld::ParseQeueueRemoveUnnecessary() { if (std::find(elements.begin(), elements.end(), vec) != elements.end()) continue; - const Section& section = gs->GetWorld().GetSection(vec); + const Section& section = GetGameState()->GetWorld().GetSection(vec); bool skip = false; @@ -112,10 +113,10 @@ void RendererWorld::ParseQeueueRemoveUnnecessary() { } void RendererWorld::UpdateAllSections(VectorF playerPos) { - Vector playerChunk(std::floor(gs->GetPlayer()->pos.x / 16), 0, std::floor(gs->GetPlayer()->pos.z / 16)); + Vector playerChunk(std::floor(GetGameState()->GetPlayer()->pos.x / 16), 0, std::floor(GetGameState()->GetPlayer()->pos.z / 16)); std::vector suitableChunks; - auto chunks = gs->GetWorld().GetSectionsList(); + auto chunks = GetGameState()->GetWorld().GetSectionsList(); for (auto& it : chunks) { double distance = (Vector(it.x, 0, it.z) - playerChunk).GetLength(); if (distance > MaxRenderingDistance) @@ -134,7 +135,7 @@ void RendererWorld::UpdateAllSections(VectorF playerPos) { PUSH_EVENT("DeleteSectionRender", it); } - playerChunk.y = std::floor(gs->GetPlayer()->pos.y / 16.0); + playerChunk.y = std::floor(GetGameState()->GetPlayer()->pos.y / 16.0); std::sort(suitableChunks.begin(), suitableChunks.end(), [playerChunk](Vector lhs, Vector rhs) { double leftLengthToPlayer = (playerChunk - lhs).GetLength(); double rightLengthToPlayer = (playerChunk - rhs).GetLength(); @@ -146,8 +147,7 @@ void RendererWorld::UpdateAllSections(VectorF playerPos) { } } -RendererWorld::RendererWorld(std::shared_ptr ptr) { - gs = ptr; +RendererWorld::RendererWorld() { MaxRenderingDistance = 2; numOfWorkers = _max(1, (signed int) std::thread::hardware_concurrency() - 2); @@ -186,7 +186,7 @@ RendererWorld::RendererWorld(std::shared_ptr ptr) { listener->RegisterHandler("EntityChanged", [this](const Event& eventData) { auto data = eventData.get(); - for (unsigned int entityId : gs->GetWorld().GetEntitiesList()) { + for (unsigned int entityId : GetGameState()->GetWorld().GetEntitiesList()) { if (entityId == data) { entities.push_back(RendererEntity(entityId)); } @@ -198,7 +198,7 @@ RendererWorld::RendererWorld(std::shared_ptr ptr) { if (vec == Vector()) return; - Vector playerChunk(std::floor(gs->GetPlayer()->pos.x / 16), 0, std::floor(gs->GetPlayer()->pos.z / 16)); + Vector playerChunk(std::floor(GetGameState()->GetPlayer()->pos.x / 16), 0, std::floor(GetGameState()->GetPlayer()->pos.z / 16)); double distanceToChunk = (Vector(vec.x, 0, vec.z) - playerChunk).GetLength(); if (MaxRenderingDistance != 1000 && distanceToChunk > MaxRenderingDistance) { @@ -215,7 +215,7 @@ RendererWorld::RendererWorld(std::shared_ptr ptr) { if (vec == Vector()) return; - Vector playerChunk(std::floor(gs->GetPlayer()->pos.x / 16), 0, std::floor(gs->GetPlayer()->pos.z / 16)); + Vector playerChunk(std::floor(GetGameState()->GetPlayer()->pos.x / 16), 0, std::floor(GetGameState()->GetPlayer()->pos.z / 16)); double distanceToChunk = (Vector(vec.x, 0, vec.z) - playerChunk).GetLength(); if (MaxRenderingDistance != 1000 && distanceToChunk > MaxRenderingDistance) { @@ -230,7 +230,7 @@ RendererWorld::RendererWorld(std::shared_ptr ptr) { }); listener->RegisterHandler("UpdateSectionsRender", [this](const Event&) { - UpdateAllSections(gs->GetPlayer()->pos); + UpdateAllSections(GetGameState()->GetPlayer()->pos); }); listener->RegisterHandler("PlayerPosChanged", [this](const Event& eventData) { @@ -276,7 +276,7 @@ void RendererWorld::Render(RenderState & renderState) { glm::radians(70.0f), (float) renderState.WindowWidth / (float) renderState.WindowHeight, 0.1f, 10000000.0f ); - glm::mat4 view = gs->GetViewMatrix(); + glm::mat4 view = GetGameState()->GetViewMatrix(); glm::mat4 projView = projection * view; //Render Entities @@ -289,11 +289,11 @@ void RendererWorld::Render(RenderState & renderState) { renderState.SetActiveVao(RendererEntity::GetVao()); for (auto& it : entities) { - it.Render(renderState, &gs->GetWorld()); + it.Render(renderState, &GetGameState()->GetWorld()); } //Render selected block - Vector selectedBlock = gs->GetSelectionStatus().selectedBlock; + Vector selectedBlock = GetGameState()->GetSelectionStatus().selectedBlock; if (selectedBlock != Vector()) { glLineWidth(2.0f); { @@ -311,7 +311,7 @@ void RendererWorld::Render(RenderState & renderState) { //Render raycast hit const bool renderHit = false; if (renderHit) { - VectorF hit = gs->GetSelectionStatus().raycastHit; + VectorF hit = GetGameState()->GetSelectionStatus().raycastHit; glLineWidth(2.0f); { glm::mat4 model; @@ -331,16 +331,16 @@ void RendererWorld::Render(RenderState & renderState) { glCheckError(); //Render sky - renderState.TimeOfDay = gs->GetTimeStatus().timeOfDay; + renderState.TimeOfDay = GetGameState()->GetTimeStatus().timeOfDay; Shader *skyShader = AssetManager::GetAsset("/altcraft/shaders/sky")->shader.get(); skyShader->Activate(); skyShader->SetUniform("projection", projection); skyShader->SetUniform("view", view); glm::mat4 model = glm::mat4(1.0); - model = glm::translate(model, gs->GetPlayer()->pos.glm()); + model = glm::translate(model, GetGameState()->GetPlayer()->pos.glm()); const float scale = 1000000.0f; model = glm::scale(model, glm::vec3(scale, scale, scale)); - float shift = gs->GetTimeStatus().interpolatedTimeOfDay / 24000.0f; + float shift = GetGameState()->GetTimeStatus().interpolatedTimeOfDay / 24000.0f; if (shift < 0) shift *= -1.0f; model = glm::rotate(model, glm::radians(90.0f), glm::vec3(0, 1.0f, 0.0f)); @@ -357,7 +357,7 @@ void RendererWorld::Render(RenderState & renderState) { const float moonriseLength = moonriseMax - moonriseMin; float mixLevel = 0; - float dayTime = gs->GetTimeStatus().interpolatedTimeOfDay; + float dayTime = GetGameState()->GetTimeStatus().interpolatedTimeOfDay; if (dayTime < 0) dayTime *= -1; while (dayTime > 24000) @@ -451,7 +451,3 @@ void RendererWorld::Update(double timeToUpdate) { DebugInfo::readyRenderer = parseQueue.size(); DebugInfo::renderSections = sections.size(); } - -GameState* RendererWorld::GameStatePtr() { - return gs.get(); -} diff --git a/src/RendererWorld.hpp b/src/RendererWorld.hpp index 913e510..d031179 100644 --- a/src/RendererWorld.hpp +++ b/src/RendererWorld.hpp @@ -27,7 +27,6 @@ class RendererWorld { }; //General - std::shared_ptr gs; std::unique_ptr listener; size_t numOfWorkers; size_t currentWorker = 0; @@ -51,7 +50,7 @@ class RendererWorld { Texture *skyTexture; RendererSky rendererSky; public: - RendererWorld(std::shared_ptr ptr); + RendererWorld(); ~RendererWorld(); void Render(RenderState& renderState); @@ -61,11 +60,5 @@ public: void Update(double timeToUpdate); - GameState *GameStatePtr(); - int culledSections = 0; - - inline void UpdateGameState(std::shared_ptr newPtr) { - gs = newPtr; - } }; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 100b7bc..2ed2b85 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -48,7 +48,7 @@ int main(int argc, char** argv) { return -1; } - GlobalState::Exec(); + RunGame(); return 0; } \ No newline at end of file -- cgit v1.2.3 From 0d78332f2b82438a4cc47df4a4f923c2b23a7a38 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Mon, 22 Apr 2019 00:34:40 +0500 Subject: Second iteration of changing to single-threaded model --- src/Game.cpp | 41 ++++++++++++++++++++++++++++++++++++----- src/Game.hpp | 5 ++++- src/GameState.cpp | 2 +- src/GameState.hpp | 2 +- src/Render.cpp | 21 ++++++++++++--------- src/Render.hpp | 1 - 6 files changed, 54 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/Game.cpp b/src/Game.cpp index 927cdc2..a3f2b50 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -12,6 +12,7 @@ State state; std::unique_ptr nc; std::unique_ptr gs; std::unique_ptr render; +std::unique_ptr timer; EventListener listener; void InitEvents() { @@ -126,6 +127,7 @@ void InitEvents() { default: break; } + LOG(INFO) << "Changed key"; }); listener.RegisterHandler("MouseMove", [](const Event& eventData) { @@ -136,23 +138,33 @@ void InitEvents() { }); listener.RegisterHandler("ReceivedPacket", [](const Event& eventData) { + if (!gs) + return; std::shared_ptr packet = eventData.get>(); gs->UpdatePacket(packet); }); listener.RegisterHandler("LmbPressed",[](const Event& eventData) { + if (!gs) + return; gs->StartDigging(); }); listener.RegisterHandler("LmbReleased",[](const Event& eventData) { + if (!gs) + return; gs->CancelDigging(); }); listener.RegisterHandler("RmbPressed", [](const Event& eventData) { + if (!gs) + return; gs->PlaceBlock(); }); listener.RegisterHandler("SelectedBlockChanged", [](const Event& eventData) { + if (!gs) + return; //TODO: //gs->CancelDigging(); }); @@ -161,17 +173,32 @@ void InitEvents() { void RunGame() { InitEvents(); + timer = std::make_unique(std::chrono::milliseconds(16)); + render = std::make_unique(900, 480, "AltCraft"); - SetState(State::MainMenu); - LoopExecutionTimeController time(std::chrono::milliseconds(16)); + SetState(State::MainMenu); while (isRunning) { listener.HandleAllEvents(); - if (gs) - gs->Update(time.GetDeltaS()); + if (gs) { + if (GetState() == State::Playing) { + if (isMoving[GameState::FORWARD]) + gs->HandleMovement(GameState::FORWARD, timer->GetRealDeltaS()); + if (isMoving[GameState::BACKWARD]) + gs->HandleMovement(GameState::BACKWARD, timer->GetRealDeltaS()); + if (isMoving[GameState::LEFT]) + gs->HandleMovement(GameState::LEFT, timer->GetRealDeltaS()); + if (isMoving[GameState::RIGHT]) + gs->HandleMovement(GameState::RIGHT, timer->GetRealDeltaS()); + if (isMoving[GameState::JUMP]) + gs->HandleMovement(GameState::JUMP, timer->GetRealDeltaS()); + } + + gs->Update(timer->GetRealDeltaS()); + } render->Update(); - time.Update(); + timer->Update(); } render.reset(); @@ -198,3 +225,7 @@ Render *GetRender() { NetworkClient *GetNetworkClient() { return nc.get(); } + +LoopExecutionTimeController* GetTime() { + return timer.get(); +} diff --git a/src/Game.hpp b/src/Game.hpp index 633585f..f7efd11 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -3,6 +3,7 @@ class GameState; class Render; class NetworkClient; +class LoopExecutionTimeController; enum class State { InitialLoading, @@ -24,4 +25,6 @@ GameState* GetGameState(); Render* GetRender(); -NetworkClient* GetNetworkClient(); \ No newline at end of file +NetworkClient* GetNetworkClient(); + +LoopExecutionTimeController *GetTime(); \ No newline at end of file diff --git a/src/GameState.cpp b/src/GameState.cpp index ace2488..9e92c1f 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -6,7 +6,7 @@ #include "Event.hpp" #include "Packet.hpp" -void GameState::Update(float deltaTime) { +void GameState::Update(double deltaTime) { if (!gameStatus.isGameStarted) return; diff --git a/src/GameState.hpp b/src/GameState.hpp index 8318c8a..5489ac6 100644 --- a/src/GameState.hpp +++ b/src/GameState.hpp @@ -70,7 +70,7 @@ class GameState { std::vector openedWindows; public: - void Update(float deltaTime); + void Update(double deltaTime); void UpdatePacket(std::shared_ptr ptr); diff --git a/src/Render.cpp b/src/Render.cpp index 2e69369..00daf99 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -8,17 +8,16 @@ #include "AssetManager.hpp" #include "Event.hpp" #include "DebugInfo.hpp" +#include "Game.hpp" #include "World.hpp" #include "GameState.hpp" #include "RendererWorld.hpp" #include "Settings.hpp" #include "Framebuffer.hpp" #include "Plugin.hpp" -#include "Game.hpp" Render::Render(unsigned int windowWidth, unsigned int windowHeight, - std::string windowTitle) - : timer(std::chrono::milliseconds(16)) { + std::string windowTitle) { InitEvents(); Settings::Load(); @@ -48,9 +47,9 @@ Render::Render(unsigned int windowWidth, unsigned int windowHeight, if (fieldSensetivity != sensetivity) sensetivity = fieldSensetivity; isWireframe = fieldWireframe; - timer.SetDelayLength(std::chrono::duration(1.0 / fieldTargetFps * 1000.0)); + GetTime()->SetDelayLength(std::chrono::duration(1.0 / fieldTargetFps * 1000.0)); if (fieldVsync) { - timer.SetDelayLength(std::chrono::milliseconds(0)); + GetTime()->SetDelayLength(std::chrono::milliseconds(0)); SDL_GL_SetSwapInterval(1); } else @@ -191,7 +190,7 @@ void Render::RenderFrame() { RenderGui(); if (world) { - world->Update(timer.RemainTimeMs()); + world->Update(GetTime()->RemainTimeMs()); } SDL_GL_SwapWindow(window); @@ -358,7 +357,6 @@ void Render::Update() { RenderFrame(); listener.HandleAllEvents(); - timer.Update(); } void Render::RenderGui() { @@ -629,9 +627,9 @@ void Render::RenderGui() { GetGameState()->GetPlayer()->isFlying = fieldFlight; isWireframe = fieldWireframe; - timer.SetDelayLength(std::chrono::duration(1.0 / fieldTargetFps * 1000.0)); + GetTime()->SetDelayLength(std::chrono::duration(1.0 / fieldTargetFps * 1000.0)); if (fieldVsync) { - timer.SetDelayLength(std::chrono::milliseconds(0)); + GetTime()->SetDelayLength(std::chrono::milliseconds(0)); SDL_GL_SetSwapInterval(1); } else SDL_GL_SetSwapInterval(0); @@ -721,18 +719,23 @@ void Render::InitEvents() { break; case State::InitialLoading: PluginSystem::CallOnChangeState("InitialLoading"); + SetMouseCapture(false); break; case State::MainMenu: PluginSystem::CallOnChangeState("MainMenu"); + SetMouseCapture(false); break; case State::Loading: PluginSystem::CallOnChangeState("Loading"); + SetMouseCapture(false); break; case State::Paused: PluginSystem::CallOnChangeState("Paused"); + SetMouseCapture(false); break; case State::Inventory: PluginSystem::CallOnChangeState("Inventory"); + SetMouseCapture(false); break; case State::Chat: PluginSystem::CallOnChangeState("Chat"); diff --git a/src/Render.hpp b/src/Render.hpp index eea5450..8e2e233 100644 --- a/src/Render.hpp +++ b/src/Render.hpp @@ -25,7 +25,6 @@ class Render { std::unique_ptr world; bool renderWorld = false; RenderState renderState; - LoopExecutionTimeController timer; std::map isKeyPressed; bool HasFocus=true; float sensetivity = 0.1f; -- cgit v1.2.3 From 868ba6279a20e4d1412c2d576c67400167de6694 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Tue, 30 Apr 2019 16:12:35 +0500 Subject: Integrated Optick profiler --- src/AssetManager.cpp | 2 ++ src/Event.cpp | 5 +++++ src/Framebuffer.cpp | 12 ++++++++---- src/Game.cpp | 5 ++++- src/GameState.cpp | 2 ++ src/Render.cpp | 8 +++++++- src/Renderer.cpp | 3 +++ src/RendererEntity.cpp | 2 ++ src/RendererSection.cpp | 4 ++++ src/RendererSectionData.cpp | 5 +++-- src/RendererSky.cpp | 9 +++++---- src/RendererWorld.cpp | 16 +++++++++++++++- src/Utility.cpp | 2 ++ src/World.cpp | 6 ++++++ 14 files changed, 68 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 66e7767..ba2b4f4 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -11,6 +11,7 @@ #define STB_IMAGE_IMPLEMENTATION #define STBI_ONLY_PNG #include +#include #include "Utility.hpp" #include "Plugin.hpp" @@ -661,6 +662,7 @@ std::string AssetManager::GetAssetNameByBlockId(BlockId block) { } Asset *AssetManager::GetAssetPtr(const std::string & assetName) { + OPTICK_EVENT(); AssetTreeNode *node; if (assetName[0] != '/') node = GetAssetByAssetName('/' + assetName); diff --git a/src/Event.cpp b/src/Event.cpp index 5c126bb..ea09af3 100644 --- a/src/Event.cpp +++ b/src/Event.cpp @@ -1,5 +1,7 @@ #include "Event.hpp" +#include + std::list EventSystem::listeners; std::recursive_mutex EventSystem::listenersMutex; @@ -14,6 +16,7 @@ EventListener::~EventListener() { } void EventListener::HandleEvent() { + OPTICK_EVENT(); if (!NotEmpty()) return; @@ -27,6 +30,7 @@ void EventListener::HandleEvent() { } void EventListener::HandleAllEvents() { + OPTICK_EVENT(); if (!NotEmpty()) return; @@ -54,6 +58,7 @@ void EventListener::RegisterHandler(size_t eventId, const EventListener::Handler } void EventListener::PollEvents() { + OPTICK_EVENT(); std::lock_guard rawLock (rawEventsMutex); if (rawEvents.empty()) return; diff --git a/src/Framebuffer.cpp b/src/Framebuffer.cpp index fb7ebc7..d81b0e0 100644 --- a/src/Framebuffer.cpp +++ b/src/Framebuffer.cpp @@ -3,11 +3,13 @@ #include #include "Utility.hpp" #include "AssetManager.hpp" +#include const GLuint magic = 316784; GLuint quadVao = magic, quadVbo = magic; Framebuffer::Framebuffer(unsigned int width, unsigned int height, bool createDepthStencilBuffer) : width(width), height(height) { + OPTICK_EVENT(); if (quadVao == magic) { float quadVertices[] = { // positions // texCoords @@ -70,11 +72,13 @@ Framebuffer::~Framebuffer() { } void Framebuffer::Activate() { + OPTICK_EVENT(); glViewport(0, 0, width, height); glBindFramebuffer(GL_FRAMEBUFFER, fbo); } void Framebuffer::RenderTo(Framebuffer &target) { + OPTICK_EVENT(); glBindFramebuffer(GL_FRAMEBUFFER, target.fbo); glViewport(0, 0, target.width, target.height); AssetManager::GetAsset("/altcraft/shaders/fbo")->shader->Activate(); @@ -96,8 +100,8 @@ void Framebuffer::Resize(unsigned int newWidth, unsigned int newHeight) { } } -Framebuffer &Framebuffer::GetDefault() -{ +Framebuffer &Framebuffer::GetDefault() { + OPTICK_EVENT(); static char fboDefaultData[sizeof(Framebuffer)]; static Framebuffer *fboDefault = nullptr; if (fboDefault == nullptr) { @@ -111,8 +115,8 @@ Framebuffer &Framebuffer::GetDefault() return *fboDefault; } -void Framebuffer::Clear(bool color, bool depth, bool stencil) -{ +void Framebuffer::Clear(bool color, bool depth, bool stencil) { + OPTICK_EVENT(); Activate(); GLbitfield clearBits = 0; if (color) diff --git a/src/Game.cpp b/src/Game.cpp index a3f2b50..d67072e 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -2,6 +2,8 @@ #include +#include + #include "Render.hpp" #include "GameState.hpp" #include "NetworkClient.hpp" @@ -127,7 +129,6 @@ void InitEvents() { default: break; } - LOG(INFO) << "Changed key"; }); listener.RegisterHandler("MouseMove", [](const Event& eventData) { @@ -171,6 +172,7 @@ void InitEvents() { } void RunGame() { + OPTICK_THREAD("Main"); InitEvents(); timer = std::make_unique(std::chrono::milliseconds(16)); @@ -180,6 +182,7 @@ void RunGame() { SetState(State::MainMenu); while (isRunning) { + OPTICK_FRAME("MainThread"); listener.HandleAllEvents(); if (gs) { if (GetState() == State::Playing) { diff --git a/src/GameState.cpp b/src/GameState.cpp index 9e92c1f..e4278ec 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -2,11 +2,13 @@ #include #include +#include #include "Event.hpp" #include "Packet.hpp" void GameState::Update(double deltaTime) { + OPTICK_EVENT(); if (!gameStatus.isGameStarted) return; diff --git a/src/Render.cpp b/src/Render.cpp index 00daf99..6218740 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -2,6 +2,7 @@ #include #include +#include #include "imgui_impl_sdl_gl3.h" #include "Shader.hpp" @@ -173,6 +174,7 @@ void Render::UpdateKeyboard() { } void Render::RenderFrame() { + OPTICK_EVENT(); framebuffer->Clear(); Framebuffer::GetDefault().Clear(); @@ -193,7 +195,9 @@ void Render::RenderFrame() { world->Update(GetTime()->RemainTimeMs()); } - SDL_GL_SwapWindow(window); + + OPTICK_EVENT("VSYNC"); + SDL_GL_SwapWindow(window); } void Render::HandleEvents() { @@ -350,6 +354,7 @@ void Render::SetMouseCapture(bool IsCaptured) { } void Render::Update() { + OPTICK_EVENT(); HandleEvents(); if (HasFocus && GetState() == State::Playing) UpdateKeyboard(); if (isMouseCaptured) HandleMouseCapture(); @@ -360,6 +365,7 @@ void Render::Update() { } void Render::RenderGui() { + OPTICK_EVENT(); ImGui_ImplSdlGL3_NewFrame(window); if (isMouseCaptured) { diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 0db23db..947fd6f 100644 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -1,6 +1,9 @@ #include "Renderer.hpp" +#include + void RenderState::SetActiveVao(GLuint Vao) { + OPTICK_EVENT(); glBindVertexArray(Vao); ActiveVao = Vao; } diff --git a/src/RendererEntity.cpp b/src/RendererEntity.cpp index fc5e1a2..fcbf79a 100644 --- a/src/RendererEntity.cpp +++ b/src/RendererEntity.cpp @@ -2,6 +2,7 @@ #include #include +#include #include "Entity.hpp" #include "GameState.hpp" @@ -123,6 +124,7 @@ RendererEntity::~RendererEntity() { } void RendererEntity::Render(RenderState& renderState, const World *world) { + OPTICK_EVENT(); glm::mat4 model = glm::mat4(1.0); const Entity &entity = world->GetEntity(entityId); model = glm::translate(model, entity.pos.glm()); diff --git a/src/RendererSection.cpp b/src/RendererSection.cpp index 1521c6f..d797771 100644 --- a/src/RendererSection.cpp +++ b/src/RendererSection.cpp @@ -1,6 +1,7 @@ #include "RendererSection.hpp" #include +#include #include "Utility.hpp" #include "Renderer.hpp" @@ -31,6 +32,7 @@ GLuint RendererSection::VboVertices = magicUniqueConstant; GLuint RendererSection::VboUvs = magicUniqueConstant; RendererSection::RendererSection(const RendererSectionData &data) { + OPTICK_EVENT(); if (VboVertices == magicUniqueConstant) { glGenBuffers(1, &VboVertices); glGenBuffers(1, &VboUvs); @@ -155,6 +157,7 @@ void swap(RendererSection & lhs, RendererSection & rhs) { } void RendererSection::Render(RenderState &renderState) { + OPTICK_EVENT(); renderState.SetActiveVao(Vao); glDrawArraysInstanced(GL_TRIANGLES, 0, 6, numOfFaces); glCheckError(); @@ -169,6 +172,7 @@ size_t RendererSection::GetHash() { } void RendererSection::UpdateData(const RendererSectionData & data) { + OPTICK_EVENT(); glBindBuffer(GL_ARRAY_BUFFER, Vbo[TEXTURES]); glBufferData(GL_ARRAY_BUFFER, data.textures.size() * sizeof(glm::vec4), data.textures.data(), GL_DYNAMIC_DRAW); diff --git a/src/RendererSectionData.cpp b/src/RendererSectionData.cpp index 3b51809..69ed665 100644 --- a/src/RendererSectionData.cpp +++ b/src/RendererSectionData.cpp @@ -3,6 +3,7 @@ #include #include +#include #include "World.hpp" @@ -101,8 +102,8 @@ std::array SetBlockIdData(const SectionsData §ions) { return blockIdData; } -RendererSectionData ParseSection(const SectionsData §ions) -{ +RendererSectionData ParseSection(const SectionsData §ions) { + OPTICK_EVENT(); RendererSectionData data; std::vector> idModels; diff --git a/src/RendererSky.cpp b/src/RendererSky.cpp index d0e9518..1eab369 100644 --- a/src/RendererSky.cpp +++ b/src/RendererSky.cpp @@ -1,5 +1,7 @@ #include "RendererSky.hpp" +#include + #include "Renderer.hpp" #include "Utility.hpp" @@ -127,16 +129,15 @@ RendererSky::RendererSky() { glCheckError(); } -RendererSky::~RendererSky() -{ +RendererSky::~RendererSky() { glDeleteBuffers(1, &VboVert); glDeleteBuffers(1, &VboUv); glDeleteVertexArrays(1, &Vao); //glCheckError(); } -void RendererSky::Render(RenderState &renderState) -{ +void RendererSky::Render(RenderState &renderState) { + OPTICK_EVENT(); renderState.SetActiveVao(Vao); glDrawArrays(GL_TRIANGLES, 0, 36); glCheckError(); diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index bcb6f92..ebb049d 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -2,6 +2,7 @@ #include #include +#include #include "DebugInfo.hpp" #include "Frustum.hpp" @@ -15,9 +16,11 @@ #include "Game.hpp" void RendererWorld::WorkerFunction(size_t workerId) { + OPTICK_THREAD("Worker"); EventListener tasksListener; tasksListener.RegisterHandler("ParseSection", [&](const Event &eventData) { + OPTICK_EVENT("EV_ParseSection"); auto data = eventData.get>(); if (std::get<0>(data) != workerId) return; @@ -37,6 +40,7 @@ void RendererWorld::WorkerFunction(size_t workerId) { } void RendererWorld::ParseQueueUpdate() { + OPTICK_EVENT(); while (!parseQueue.empty()) { size_t id = 0; for (; id < RendererWorld::parsingBufferSize && parsing[id].parsing; ++id) {} @@ -70,6 +74,7 @@ void RendererWorld::ParseQueueUpdate() { } void RendererWorld::ParseQeueueRemoveUnnecessary() { + OPTICK_EVENT(); size_t size = parseQueue.size(); static std::vector elements; elements.clear(); @@ -113,6 +118,7 @@ void RendererWorld::ParseQeueueRemoveUnnecessary() { } void RendererWorld::UpdateAllSections(VectorF playerPos) { + OPTICK_EVENT(); Vector playerChunk(std::floor(GetGameState()->GetPlayer()->pos.x / 16), 0, std::floor(GetGameState()->GetPlayer()->pos.z / 16)); std::vector suitableChunks; @@ -148,6 +154,7 @@ void RendererWorld::UpdateAllSections(VectorF playerPos) { } RendererWorld::RendererWorld() { + OPTICK_EVENT(); MaxRenderingDistance = 2; numOfWorkers = _max(1, (signed int) std::thread::hardware_concurrency() - 2); @@ -158,6 +165,7 @@ RendererWorld::RendererWorld() { PrepareRender(); listener->RegisterHandler("DeleteSectionRender", [this](const Event& eventData) { + OPTICK_EVENT("EV_DeleteSectionRender"); auto vec = eventData.get(); auto it = sections.find(vec); if (it == sections.end()) @@ -166,6 +174,7 @@ RendererWorld::RendererWorld() { }); listener->RegisterHandler("SectionParsed",[this](const Event &eventData) { + OPTICK_EVENT("EV_SectionParsed"); auto id = eventData.get(); parsing[id].parsing = false; @@ -185,6 +194,7 @@ RendererWorld::RendererWorld() { }); listener->RegisterHandler("EntityChanged", [this](const Event& eventData) { + OPTICK_EVENT("EV_EntityChanged"); auto data = eventData.get(); for (unsigned int entityId : GetGameState()->GetWorld().GetEntitiesList()) { if (entityId == data) { @@ -194,6 +204,7 @@ RendererWorld::RendererWorld() { }); listener->RegisterHandler("ChunkChanged", [this](const Event& eventData) { + OPTICK_EVENT("EV_ChunkChanged"); auto vec = eventData.get(); if (vec == Vector()) return; @@ -211,6 +222,7 @@ RendererWorld::RendererWorld() { }); listener->RegisterHandler("ChunkChangedForce", [this](const Event& eventData) { + OPTICK_EVENT("EV_ChunkChangedForce"); auto vec = eventData.get(); if (vec == Vector()) return; @@ -271,6 +283,7 @@ RendererWorld::~RendererWorld() { } void RendererWorld::Render(RenderState & renderState) { + OPTICK_EVENT(); //Common glm::mat4 projection = glm::perspective( glm::radians(70.0f), (float) renderState.WindowWidth / (float) renderState.WindowHeight, @@ -286,7 +299,7 @@ void RendererWorld::Render(RenderState & renderState) { entityShader->SetUniform("projection", projection); entityShader->SetUniform("view", view); glCheckError(); - + renderState.SetActiveVao(RendererEntity::GetVao()); for (auto& it : entities) { it.Render(renderState, &GetGameState()->GetWorld()); @@ -434,6 +447,7 @@ void RendererWorld::PrepareRender() { } void RendererWorld::Update(double timeToUpdate) { + OPTICK_EVENT(); static auto timeSincePreviousUpdate = std::chrono::steady_clock::now(); if (parseQueueNeedRemoveUnnecessary) diff --git a/src/Utility.cpp b/src/Utility.cpp index 848ee02..09696fd 100644 --- a/src/Utility.cpp +++ b/src/Utility.cpp @@ -2,9 +2,11 @@ #include +#include #include GLenum glCheckError_(const char *file, int line) { + OPTICK_EVENT(); GLenum errorCode; while ((errorCode = glGetError()) != GL_NO_ERROR) { std::string error; diff --git a/src/World.cpp b/src/World.cpp index da0a33b..fa281f1 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -2,6 +2,7 @@ #include #include +#include #include "Event.hpp" #include "DebugInfo.hpp" @@ -131,6 +132,7 @@ const Section &World::GetSection(Vector sectionPos) const { // TODO: skip liquid blocks RaycastResult World::Raycast(glm::vec3 position, glm::vec3 direction) const { + OPTICK_EVENT(); const float maxLen = 5.0; const float step = 0.01; glm::vec3 pos = glm::vec3(0.0); @@ -150,6 +152,7 @@ RaycastResult World::Raycast(glm::vec3 position, glm::vec3 direction) const { } void World::UpdatePhysics(float delta) { + OPTICK_EVENT(); struct CollisionResult { bool isCollide; //Vector block; @@ -158,6 +161,7 @@ void World::UpdatePhysics(float delta) { }; auto testCollision = [this](double width, double height, VectorF pos)->CollisionResult { + OPTICK_EVENT("testCollision"); int blockXBegin = pos.x - width - 1.0; int blockXEnd = pos.x + width + 0.5; int blockYBegin = pos.y - 0.5; @@ -177,6 +181,7 @@ void World::UpdatePhysics(float delta) { for (int y = blockYBegin; y <= blockYEnd; y++) { for (int z = blockZBegin; z <= blockZEnd; z++) { for (int x = blockXBegin; x <= blockXEnd; x++) { + OPTICK_EVENT("testCollision"); BlockId block = this->GetBlockId(Vector(x, y, z)); if (block.id == 0 || block.id == 31 || block.id == 37 || block.id == 38 || block.id == 175 || block.id == 78) continue; @@ -191,6 +196,7 @@ void World::UpdatePhysics(float delta) { }; for (auto& it : entities) { + OPTICK_EVENT("Foreach entities"); if (it.isFlying) { VectorF newPos = it.pos + VectorF(it.vel.x, it.vel.y, it.vel.z) * delta; auto coll = testCollision(it.width, it.height, newPos); -- cgit v1.2.3 From 8c5320a94b4c91f2801c05766f6a1747de42a2e5 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sat, 18 May 2019 18:12:56 +0500 Subject: Implemented more scripting APIs --- src/AssetManager.cpp | 7 ++++- src/Game.cpp | 13 ++++++--- src/Plugin.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++++------- src/Plugin.hpp | 4 ++- 4 files changed, 84 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index ba2b4f4..eb3186a 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -115,7 +115,12 @@ void LoadScripts() { LOG(ERROR) << "Unrecognised script file /" << it->name; continue; } - PluginSystem::Execute(asset->code); + try { + PluginSystem::Execute(asset->code, true); + } + catch (std::exception& e) { + LOG(ERROR) << "Failed loading script '" << script->name << "' in '" << it->name << "'"; + } } } } diff --git a/src/Game.cpp b/src/Game.cpp index d67072e..c69ff5d 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -7,6 +7,7 @@ #include "Render.hpp" #include "GameState.hpp" #include "NetworkClient.hpp" +#include "Plugin.hpp" bool isRunning = true; bool isMoving[5] = { 0,0,0,0,0 }; @@ -75,6 +76,12 @@ void InitEvents() { listener.RegisterHandler("SendChatMessage", [](const Event& eventData) { auto message = eventData.get(); + if (message[0] == '!' && message[1] != '!') { + PluginSystem::Execute(message.substr(1)); + return; + } + if (message[0] == '!') + message = message.substr(1); auto packet = std::static_pointer_cast(std::make_shared(message)); PUSH_EVENT("SendPacket",packet); }); @@ -182,8 +189,9 @@ void RunGame() { SetState(State::MainMenu); while (isRunning) { - OPTICK_FRAME("MainThread"); + OPTICK_FRAME("MainThread"); listener.HandleAllEvents(); + PluginSystem::CallOnTick(timer->GetRealDeltaS()); if (gs) { if (GetState() == State::Playing) { if (isMoving[GameState::FORWARD]) @@ -196,8 +204,7 @@ void RunGame() { gs->HandleMovement(GameState::RIGHT, timer->GetRealDeltaS()); if (isMoving[GameState::JUMP]) gs->HandleMovement(GameState::JUMP, timer->GetRealDeltaS()); - } - + } gs->Update(timer->GetRealDeltaS()); } render->Update(); diff --git a/src/Plugin.cpp b/src/Plugin.cpp index 41c690c..9641e0f 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -4,6 +4,11 @@ #include #include +#include + +#include "GameState.hpp" +#include "Game.hpp" + struct Plugin { const std::string name; @@ -11,6 +16,7 @@ struct Plugin { const std::function onLoad; const std::function onUnload; const std::function onChangeState; + const std::function onTick; }; @@ -20,13 +26,14 @@ sol::state lua; namespace PluginApi { - void RegisterPlugin(sol::table self, sol::table plugin) { + void RegisterPlugin(sol::table plugin) { Plugin nativePlugin { - plugin["name"].get_or("75"), + plugin["name"].get_or(""), plugin["displayName"].get_or(""), plugin["onLoad"].get_or(std::function()), plugin["onUnload"].get_or(std::function()), - plugin["onChangeState"].get_or(std::function()) + plugin["onChangeState"].get_or(std::function()), + plugin["onTick"].get_or(std::function()) }; plugins.push_back(nativePlugin); nativePlugin.onLoad(); @@ -34,14 +41,25 @@ namespace PluginApi { LOG(INFO) << "Loaded plugin " << (!nativePlugin.displayName.empty() ? nativePlugin.displayName : nativePlugin.name); } - void LogWarning(sol::table self, std::string text) { + void LogWarning(std::string text) { LOG(WARNING) << text; } + void LogInfo(std::string text) { + LOG(INFO) << text; + } + + void LogError(std::string text) { + LOG(ERROR) << text; + } + + GameState *GetGameState() { + return ::GetGameState(); + } } -void PluginSystem::Init() -{ +void PluginSystem::Init() { + OPTICK_EVENT(); LOG(INFO) << "Initializing plugin system"; for (Plugin &plugin : plugins) { if (plugin.onUnload) @@ -52,23 +70,47 @@ void PluginSystem::Init() lua = sol::state(); lua.open_libraries(); + lua.new_usertype("Entity", + "pos", &Entity::pos); + + lua.new_usertype("GameState", + "GetPlayer", &GameState::GetPlayer, + "GetWorld", &GameState::GetWorld); + + lua.new_usertype("World"); + + lua.new_usertype("Vector", + "x", &Vector::x, + "y", &Vector::y, + "z", &Vector::z); + + lua.new_usertype("VectorF", + "x", &VectorF::x, + "y", &VectorF::y, + "z", &VectorF::z); + sol::table apiTable = lua["AC"].get_or_create(); apiTable["RegisterPlugin"] = PluginApi::RegisterPlugin; apiTable["LogWarning"] = PluginApi::LogWarning; + apiTable["LogInfo"] = PluginApi::LogInfo; + apiTable["LogError"] = PluginApi::LogError; + apiTable["GetGameState"] = PluginApi::GetGameState; } -void PluginSystem::Execute(const std::string &luaCode) -{ +void PluginSystem::Execute(const std::string &luaCode, bool except) { + OPTICK_EVENT(); try { lua.safe_script(luaCode); } catch (sol::error &e) { LOG(ERROR) << e.what(); + if (except) + throw; } } -void PluginSystem::CallOnChangeState(std::string newState) -{ +void PluginSystem::CallOnChangeState(std::string newState) { + OPTICK_EVENT(); for (Plugin &plugin : plugins) { if (plugin.onChangeState) try { @@ -79,3 +121,16 @@ void PluginSystem::CallOnChangeState(std::string newState) } } } + +void PluginSystem::CallOnTick(double deltaTime) { + OPTICK_EVENT(); + for (Plugin& plugin : plugins) { + if (plugin.onTick) + try { + plugin.onTick(deltaTime); + } + catch (sol::error &e) { + LOG(ERROR) << e.what(); + } + } +} diff --git a/src/Plugin.hpp b/src/Plugin.hpp index a5f75e1..3b61011 100644 --- a/src/Plugin.hpp +++ b/src/Plugin.hpp @@ -5,7 +5,9 @@ namespace PluginSystem { void Init(); - void Execute(const std::string &luaCode); + void Execute(const std::string &luaCode, bool except = false); void CallOnChangeState(std::string newState); + + void CallOnTick(double deltaTime); } \ No newline at end of file -- cgit v1.2.3 From 1d965bebefe11f1fd5e34c686058fa932bb6b82e Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sun, 19 May 2019 13:20:44 +0500 Subject: Implemented GameState lua-api --- src/Plugin.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 65 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/Plugin.cpp b/src/Plugin.cpp index 9641e0f..4ef1240 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -8,9 +8,11 @@ #include "GameState.hpp" #include "Game.hpp" +#include "Event.hpp" struct Plugin { + int errors; const std::string name; const std::string displayName; const std::function onLoad; @@ -28,6 +30,7 @@ namespace PluginApi { void RegisterPlugin(sol::table plugin) { Plugin nativePlugin { + 0, plugin["name"].get_or(""), plugin["displayName"].get_or(""), plugin["onLoad"].get_or(std::function()), @@ -62,7 +65,7 @@ void PluginSystem::Init() { OPTICK_EVENT(); LOG(INFO) << "Initializing plugin system"; for (Plugin &plugin : plugins) { - if (plugin.onUnload) + if (plugin.onUnload && plugin.errors < 10) plugin.onUnload(); } @@ -75,16 +78,70 @@ void PluginSystem::Init() { lua.new_usertype("GameState", "GetPlayer", &GameState::GetPlayer, - "GetWorld", &GameState::GetWorld); - - lua.new_usertype("World"); + "GetWorld", &GameState::GetWorld, + "GetTimeStatus", &GameState::GetTimeStatus, + "GetGameStatus", &GameState::GetGameStatus, + "GetPlayerStatus", &GameState::GetPlayerStatus, + "GetSelectionStatus", &GameState::GetSelectionStatus, + "GetInventory", &GameState::GetInventory); + + lua.new_usertype("TimeStatus", + "interpolatedTimeOfDay", &TimeStatus::interpolatedTimeOfDay, + "worldAge", &TimeStatus::worldAge, + "timeOfDay", &TimeStatus::timeOfDay, + "doDaylightCycle", &TimeStatus::doDaylightCycle); + + lua.new_usertype("GameStatus", + "levelType", &GameStatus::levelType, + "spawnPosition", &GameStatus::spawnPosition, + "gamemode", &GameStatus::gamemode, + "dimension", &GameStatus::dimension, + "difficulty", &GameStatus::difficulty, + "maxPlayers", &GameStatus::maxPlayers, + "isGameStarted", &GameStatus::isGameStarted, + "reducedDebugInfo", &GameStatus::reducedDebugInfo); + + lua.new_usertype("SelectionStatus", + "raycastHit", &SelectionStatus::raycastHit, + "selectedBlock", &SelectionStatus::selectedBlock, + "distanceToSelectedBlock", &SelectionStatus::distanceToSelectedBlock, + "isBlockSelected", &SelectionStatus::isBlockSelected); + + lua.new_usertype("PlayerStatus", + "uid", &PlayerStatus::uid, + "name", &PlayerStatus::name, + "flyingSpeed", &PlayerStatus::flyingSpeed, + "fovModifier", &PlayerStatus::fovModifier, + "health", &PlayerStatus::health, + "eid", &PlayerStatus::eid, + "invulnerable", &PlayerStatus::invulnerable, + "flying", &PlayerStatus::flying, + "allowFlying", &PlayerStatus::allowFlying, + "creativeMode", &PlayerStatus::creativeMode); + + lua.new_usertype("World", + "GetEntitiesList", &World::GetEntitiesList, + "GetEntity",&World::GetEntityPtr, + "Raycast", &World::Raycast, + "GetBlockId", &World::GetBlockId, + "SetBlockId", &World::SetBlockId); + + lua.new_usertype("BlockId", + "id", sol::property( + [](BlockId & bid) { return bid.id; }, + [](BlockId & bid, unsigned short id) { bid.id = id; }), + "state", sol::property( + [](BlockId & bid) { return bid.state; }, + [](BlockId & bid, unsigned char state) { bid.state = state; })); lua.new_usertype("Vector", + sol::constructors(), "x", &Vector::x, "y", &Vector::y, "z", &Vector::z); lua.new_usertype("VectorF", + sol::constructors(), "x", &VectorF::x, "y", &VectorF::y, "z", &VectorF::z); @@ -112,12 +169,13 @@ void PluginSystem::Execute(const std::string &luaCode, bool except) { void PluginSystem::CallOnChangeState(std::string newState) { OPTICK_EVENT(); for (Plugin &plugin : plugins) { - if (plugin.onChangeState) + if (plugin.onChangeState && plugin.errors < 10) try { plugin.onChangeState(newState); } catch (sol::error &e) { LOG(ERROR) << e.what(); + plugin.errors++; } } } @@ -125,12 +183,13 @@ void PluginSystem::CallOnChangeState(std::string newState) { void PluginSystem::CallOnTick(double deltaTime) { OPTICK_EVENT(); for (Plugin& plugin : plugins) { - if (plugin.onTick) + if (plugin.onTick && plugin.errors < 10) try { plugin.onTick(deltaTime); } catch (sol::error &e) { LOG(ERROR) << e.what(); + plugin.errors++; } } } -- cgit v1.2.3 From 35e786c2b4632f92518c8881db650ba63beecd5c Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sun, 19 May 2019 15:25:03 +0500 Subject: Implemented lua's "require" for AM --- src/AssetManager.cpp | 9 +++++++-- src/Plugin.cpp | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index eb3186a..be69dd0 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -110,6 +110,9 @@ void LoadScripts() { if (child->name == "scripts") { for (auto &script : child->childs) { + if (script->name != "init") + continue; + AssetScript *asset = dynamic_cast(script->asset.get()); if (!asset) { LOG(ERROR) << "Unrecognised script file /" << it->name; @@ -118,9 +121,11 @@ void LoadScripts() { try { PluginSystem::Execute(asset->code, true); } - catch (std::exception& e) { + catch (std::exception & e) { LOG(ERROR) << "Failed loading script '" << script->name << "' in '" << it->name << "'"; } + + break; } } } @@ -171,7 +176,7 @@ void ParseAsset(AssetTreeNode &node) { return; } - if (node.name == "init") { + if (node.parent->name == "scripts") { ParseAssetScript(node); return; } diff --git a/src/Plugin.cpp b/src/Plugin.cpp index 4ef1240..6acfb08 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -9,6 +9,7 @@ #include "GameState.hpp" #include "Game.hpp" #include "Event.hpp" +#include "AssetManager.hpp" struct Plugin { @@ -61,6 +62,24 @@ namespace PluginApi { } } +int LoadFileRequire(lua_State* L) { + std::string path = sol::stack::get(L); + + std::string package = path.substr(0, path.find('/')); + std::string script = path.substr(path.find('/') + 1); + + std::string scriptPath = "/" + package + "/scripts/" + script; + + AssetScript *asset = AssetManager::GetAsset(scriptPath); + if (!asset) { + sol::stack::push(L, "Module '" + scriptPath + "' not found"); + return 1; + } + + luaL_loadbuffer(L, asset->code.data(), asset->code.size(), path.c_str()); + return 1; +} + void PluginSystem::Init() { OPTICK_EVENT(); LOG(INFO) << "Initializing plugin system"; @@ -73,6 +92,10 @@ void PluginSystem::Init() { lua = sol::state(); lua.open_libraries(); + lua["package"]["searchers"] = lua.create_table_with( + 1, LoadFileRequire + ); + lua.new_usertype("Entity", "pos", &Entity::pos); -- cgit v1.2.3 From 646f77ec6bc27af231b6ff8974e631b86188beb6 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sun, 19 May 2019 23:03:48 +0500 Subject: Implemented block-api --- src/AssetManager.cpp | 8 ++++---- src/Block.cpp | 19 +++++++++++++++++++ src/Block.hpp | 14 +++++++++++++- src/Plugin.cpp | 45 ++++++++++++++++++++++++++++++++++++++++++++- src/Plugin.hpp | 6 ++++++ src/World.cpp | 6 +++--- 6 files changed, 89 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index be69dd0..19cd452 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -613,16 +613,16 @@ BlockFaces &AssetManager::GetBlockModelByBlockId(BlockId block) { return blockIdToBlockFaces.find(block)->second; } - auto blockStateName = TransformBlockIdToBlockStateName(block); - AssetBlockState *asset = GetAsset("/minecraft/blockstates/" + blockStateName.first); + BlockInfo blockInfo = GetBlockInfo(block); + AssetBlockState *asset = GetAsset("/minecraft/blockstates/" + blockInfo.blockstate); if (!asset) return GetBlockModelByBlockId(BlockId{ 7788,0 }); BlockState &blockState = asset->blockState; - if (blockState.variants.find(blockStateName.second) == blockState.variants.end()) + if (blockState.variants.find(blockInfo.variant) == blockState.variants.end()) return GetBlockModelByBlockId(BlockId{ 7788,0 }); - BlockStateVariant &variant = blockState.variants[blockStateName.second]; + BlockStateVariant &variant = blockState.variants[blockInfo.variant]; if (variant.models.empty()) return GetBlockModelByBlockId(BlockId{ 7788,0 }); diff --git a/src/Block.cpp b/src/Block.cpp index a12db9d..421cb5d 100644 --- a/src/Block.cpp +++ b/src/Block.cpp @@ -1,5 +1,9 @@ #include "Block.hpp" +#include + +#include "Plugin.hpp" + std::pair TransformBlockIdToBlockStateName(BlockId blockId) { switch (blockId.id) { case 1: { @@ -523,3 +527,18 @@ std::pair TransformBlockIdToBlockStateName(BlockId blo return std::make_pair("", ""); } + +std::map staticBlockInfo; + +void RegisterStaticBlockInfo(BlockId blockId, BlockInfo blockInfo) { + staticBlockInfo[blockId] = blockInfo; +} + +BlockInfo GetBlockInfo(BlockId blockId, Vector blockPos) { + auto it = staticBlockInfo.find(blockId); + if (it != staticBlockInfo.end()) + return it->second; + if (blockPos == Vector()) + return BlockInfo{ true, "", "" }; + return PluginSystem::RequestBlockInfo(blockPos); +} diff --git a/src/Block.hpp b/src/Block.hpp index fa8b51a..d8a78a8 100644 --- a/src/Block.hpp +++ b/src/Block.hpp @@ -3,6 +3,8 @@ #include #include +#include "Vector.hpp" + struct BlockId { unsigned short id : 13; unsigned char state : 4; @@ -40,4 +42,14 @@ namespace std { } //returns name of blockstate and name of variant -std::pair TransformBlockIdToBlockStateName(BlockId blockId); \ No newline at end of file +std::pair TransformBlockIdToBlockStateName(BlockId blockId); + +struct BlockInfo { + bool collides; + std::string blockstate; + std::string variant; +}; + +void RegisterStaticBlockInfo(BlockId blockId, BlockInfo blockInfo); + +BlockInfo GetBlockInfo(BlockId blockId, Vector blockPos = Vector(0,0,0)); \ No newline at end of file diff --git a/src/Plugin.cpp b/src/Plugin.cpp index 6acfb08..8d2de94 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -20,6 +20,7 @@ struct Plugin { const std::function onUnload; const std::function onChangeState; const std::function onTick; + const std::function onRequestBlockInfo; }; @@ -37,7 +38,8 @@ namespace PluginApi { plugin["onLoad"].get_or(std::function()), plugin["onUnload"].get_or(std::function()), plugin["onChangeState"].get_or(std::function()), - plugin["onTick"].get_or(std::function()) + plugin["onTick"].get_or(std::function()), + plugin["onRequestBlockInfo"].get_or(std::function()), }; plugins.push_back(nativePlugin); nativePlugin.onLoad(); @@ -60,6 +62,14 @@ namespace PluginApi { GameState *GetGameState() { return ::GetGameState(); } + + void RegisterBlock(BlockId blockId, bool collides, std::string blockstate, std::string variant) { + RegisterStaticBlockInfo(blockId, BlockInfo{ + collides, + blockstate, + variant + }); + } } int LoadFileRequire(lua_State* L) { @@ -149,7 +159,16 @@ void PluginSystem::Init() { "GetBlockId", &World::GetBlockId, "SetBlockId", &World::SetBlockId); + auto bidFactory1 = []() { + return BlockId{ 0,0 }; + }; + auto bidFactory2 = [](unsigned short id, unsigned char state) { + return BlockId{ id,state }; + }; + lua.new_usertype("BlockId", + "new", sol::factories([]() {return BlockId{ 0,0 };}, + [](unsigned short id, unsigned char state) {return BlockId{ id, state };}), "id", sol::property( [](BlockId & bid) { return bid.id; }, [](BlockId & bid, unsigned short id) { bid.id = id; }), @@ -169,6 +188,11 @@ void PluginSystem::Init() { "y", &VectorF::y, "z", &VectorF::z); + lua.new_usertype("BlockInfo", + "collides", &BlockInfo::collides, + "blockstate", &BlockInfo::blockstate, + "variant", &BlockInfo::variant); + sol::table apiTable = lua["AC"].get_or_create(); apiTable["RegisterPlugin"] = PluginApi::RegisterPlugin; @@ -176,6 +200,7 @@ void PluginSystem::Init() { apiTable["LogInfo"] = PluginApi::LogInfo; apiTable["LogError"] = PluginApi::LogError; apiTable["GetGameState"] = PluginApi::GetGameState; + apiTable["RegisterBlock"] = PluginApi::RegisterBlock; } void PluginSystem::Execute(const std::string &luaCode, bool except) { @@ -216,3 +241,21 @@ void PluginSystem::CallOnTick(double deltaTime) { } } } + +BlockInfo PluginSystem::RequestBlockInfo(Vector blockPos) { + OPTICK_EVENT(); + BlockInfo ret; + for (Plugin& plugin : plugins) { + if (plugin.onRequestBlockInfo && plugin.errors < 10) + try { + ret = plugin.onRequestBlockInfo(blockPos); + if (!ret.blockstate.empty()) + break; + } + catch (sol::error & e) { + LOG(ERROR) << e.what(); + plugin.errors++; + } + } + return ret; +} diff --git a/src/Plugin.hpp b/src/Plugin.hpp index 3b61011..a849f5c 100644 --- a/src/Plugin.hpp +++ b/src/Plugin.hpp @@ -2,6 +2,10 @@ #include +#include "Vector.hpp" + +class BlockInfo; + namespace PluginSystem { void Init(); @@ -10,4 +14,6 @@ namespace PluginSystem { void CallOnChangeState(std::string newState); void CallOnTick(double deltaTime); + + BlockInfo RequestBlockInfo(Vector blockPos); } \ No newline at end of file diff --git a/src/World.cpp b/src/World.cpp index fa281f1..00a1a19 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -98,7 +98,7 @@ bool World::isPlayerCollides(double X, double Y, double Z) const { for (int y = 0; y < 16; y++) { for (int z = 0; z < 16; z++) { BlockId block = section.GetBlockId(Vector(x, y, z)); - if (block.id == 0 || block.id == 31 || block.id == 37 || block.id == 38 || block.id == 175) + if (!GetBlockInfo(block).collides) continue; AABB blockColl{ (x + it.x * 16.0), (y + it.y * 16.0), @@ -183,8 +183,8 @@ void World::UpdatePhysics(float delta) { for (int x = blockXBegin; x <= blockXEnd; x++) { OPTICK_EVENT("testCollision"); BlockId block = this->GetBlockId(Vector(x, y, z)); - if (block.id == 0 || block.id == 31 || block.id == 37 || block.id == 38 || block.id == 175 || block.id == 78) - continue; + if (block.id == 0 || !GetBlockInfo(block).collides) + continue; AABB blockColl{ x,y,z,1.0,1.0,1.0 }; if (TestCollision(entityCollBox, blockColl)) { return { true }; -- cgit v1.2.3 From 37fbb814cb277fe5ce53240a3d02aa996000be1d Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Mon, 20 May 2019 14:06:25 +0500 Subject: Completely removed hardcoded list of blockstates --- src/Block.cpp | 524 --------------------------------------------------------- src/Block.hpp | 3 - src/Render.cpp | 4 +- 3 files changed, 2 insertions(+), 529 deletions(-) (limited to 'src') diff --git a/src/Block.cpp b/src/Block.cpp index 421cb5d..48c099d 100644 --- a/src/Block.cpp +++ b/src/Block.cpp @@ -4,530 +4,6 @@ #include "Plugin.hpp" -std::pair TransformBlockIdToBlockStateName(BlockId blockId) { - switch (blockId.id) { - case 1: { - if (blockId.state > 6) - break; - static const std::pair ids[] = { - std::pair("stone", "normal"), - std::pair("granite", "normal"), - std::pair("smooth_granite", "normal"), - std::pair("diorite", "normal"), - std::pair("smooth_diorite", "normal"), - std::pair("andesite", "normal"), - std::pair("smooth_andesite", "normal"), - }; - return ids[blockId.state]; - } - case 2: { - return std::make_pair("grass", "snowy=false"); - } - case 3: { - if (blockId.state > 2) - break; - static const std::pair ids[] = { - std::pair("dirt", "normal"), - std::pair("coarse_dirt", "normal"), - std::pair("podzol", "snowy=false"), - }; - return ids[blockId.state]; - } - case 4: { - return std::make_pair("cobblestone", "normal"); - } - case 5: { - if (blockId.state > 5) - break; - static const std::pair ids[] = { - std::pair("oak_planks", "normal"), - std::pair("spruce_planks", "normal"), - std::pair("birch_planks", "normal"), - std::pair("jungle_planks", "normal"), - std::pair("acacia_planks", "normal"), - std::pair("dark_oak_planks", "normal"), - }; - return ids[blockId.state]; - } - case 7: { - return std::make_pair("bedrock", "normal"); - } - case 8: - case 9: { - return std::make_pair("water", "normal"); - } - case 10: - case 11: { - return std::make_pair("lava", "normal"); - } - case 12: { - if (blockId.state > 1) - break; - static const std::pair ids[] = { - std::pair("sand", "normal"), - std::pair("red_sand", "normal"), - }; - return ids[blockId.state]; - } - case 13: { - return std::make_pair("gravel", "normal"); - } - case 14: { - return std::make_pair("gold_ore", "normal"); - } - case 15: { - return std::make_pair("iron_ore", "normal"); - } - case 16: { - return std::make_pair("coal_ore", "normal"); - } - case 17: { - unsigned char type = blockId.state & 0x3; - unsigned char dir = (blockId.state & 0xC) >> 2; - static const std::string types[] = { - "oak_log", - "spruce_log", - "birch_log", - "jungle_log", - }; - static const std::string dirs[] = { - "axis=y", - "axis=x", - "axis=z", - "axis=none", - }; - return std::make_pair(types[type], dirs[dir]); - } - case 18: { - static const std::pair ids[] = { - std::pair("oak_leaves", "normal"), - std::pair("spruce_leaves", "normal"), - std::pair("birch_leaves", "normal"), - std::pair("jungle_leaves", "normal"), - }; - return ids[blockId.state & 0x3]; - } - case 20: { - return std::make_pair("glass", "normal"); - } - case 21: { - return std::make_pair("lapis_ore", "normal"); - } - case 22: { - return std::make_pair("lapis_block", "normal"); - } - case 24: { - if (blockId.state > 2) - break; - static const std::pair ids[] = { - std::pair("sandstone", "normal"), - std::pair("chiseled_sandstone", "normal"), - std::pair("smooth_sandstone", "normal"), - }; - return ids[blockId.state]; - } - case 30: { - return std::make_pair("web", "normal"); - } - case 31: { - if (blockId.state > 2) - break; - static const std::pair ids[] = { - std::pair("dead_bush", "normal"), - std::pair("tall_grass", "normal"), - std::pair("fern", "normal"), - }; - return ids[blockId.state]; - } - case 32: { - return std::make_pair("dead_bush", "normal"); - } - case 35: { - static const std::pair ids[] = { - std::pair("white_wool", "normal"), - std::pair("orange_wool", "normal"), - std::pair("magenta_wool", "normal"), - std::pair("light_blue_wool", "normal"), - std::pair("yellow_wool", "normal"), - std::pair("lime_wool", "normal"), - std::pair("pink_wool", "normal"), - std::pair("gray_wool", "normal"), - std::pair("silver_wool", "normal"), - std::pair("cyan_wool", "normal"), - std::pair("purple_wool", "normal"), - std::pair("blue_wool", "normal"), - std::pair("brown_wool", "normal"), - std::pair("green_wool", "normal"), - std::pair("red_wool", "normal"), - std::pair("black_wool", "normal"), - }; - return ids[blockId.state]; - } - case 37: { - return std::make_pair("dandelion", "normal"); - } - case 38: { - if (blockId.state > 8) - break; - static const std::pair ids[] = { - std::pair("poppy", "normal"), - std::pair("blue_orchid", "normal"), - std::pair("allium", "normal"), - std::pair("houstonia", "normal"), - std::pair("red_tulip", "normal"), - std::pair("orange_tulip", "normal"), - std::pair("white_tulip", "normal"), - std::pair("pink_tulip", "normal"), - std::pair("oxeye_daisy", "normal"), - }; - return ids[blockId.state]; - } - case 39: { - return std::make_pair("brown_mushroom","normal"); - } - case 40: { - return std::make_pair("red_mushroom", "normal"); - } - case 41: { - return std::make_pair("gold_block", "normal"); - } - case 44: { - return std::make_pair("stone_slab", "half=" + std::string(!(blockId.state >> 3) ? "bottom" : "top")); - } - case 45: { - return std::make_pair("brick_block", "normal"); - } - case 46: { - return std::make_pair("tnt", "normal"); - } - case 47: { - return std::make_pair("bookshelf", "normal"); - } - case 48: { - return std::make_pair("mossy_cobblestone", "normal"); - } - case 49: { - return std::make_pair("obsidian", "normal"); - } - case 50: { - if (blockId.state > 5) - break; - static const std::pair ids[] = { - std::pair("", ""), - std::pair("torch", "facing=east"), - std::pair("torch", "facing=west"), - std::pair("torch", "facing=south"), - std::pair("torch", "facing=north"), - std::pair("torch", "facing=up"), - }; - return ids[blockId.state]; - } - case 53: { - bool isUp = blockId.state >> 2; - unsigned char dir = blockId.state & 0x3; - static const std::string dirs[] = { - "facing=east,half=", - "facing=west,half=", - "facing=south,half=", - "facing=north,half=", - }; - - return std::make_pair("oak_stairs", dirs[dir] + (isUp? "top" : "bottom") +",shape=straight"); - } - case 56: { - return std::make_pair("diamond_ore", "normal"); - } - case 57: { - return std::make_pair("diamond_block", "normal"); - } - case 59: { - return std::make_pair("wheat", "age=" + std::to_string(blockId.state)); - } - case 60: { - return std::make_pair("farmland", "moisture=" + std::to_string(7 - blockId.state)); - } - case 61: { - static const std::string dirs[] = { - "", - "", - "facing=north", - "facing=south", - "facing=west", - "facing=east", - }; - return std::make_pair("furnace", dirs[blockId.state]); - } - case 64: { - bool isUp = !(blockId.state >> 3); - bool hingeIsLeft = true; - bool isOpen = 0; - unsigned char dir = 0; - if (isUp) - hingeIsLeft = blockId.state & 0x1; - else { - isOpen = (blockId.state >> 1) & 0x1; - dir = blockId.state >> 2; - } - static const std::string dirs[] = { - "east", - "south", - "west", - "north", - }; - - return std::make_pair("wooden_door", "facing=" + dirs[dir] + ",half=" + (isUp ? "upper" : "lower") + ",hinge=" + (hingeIsLeft ? "left" : "right") + ",open=" + (isOpen ? "true" : "false")); - } - case 67: { - bool isUp = blockId.state >> 2; - unsigned char dir = blockId.state & 0x3; - static const std::string dirs[] = { - "facing=east,half=", - "facing=west,half=", - "facing=south,half=", - "facing=north,half=", - }; - - return std::make_pair("stone_stairs", dirs[dir] + (isUp ? "top" : "bottom") + ",shape=straight"); - } - case 69: { - bool isActive = blockId.state >> 3; - static const std::string types[] = { - "facing=down_x", - "facing=east", - "facing=west", - "facing=south", - "facing=north", - "facing=up_z", - "facing=up_x", - "facing=down_z", - }; - - return std::make_pair("lever", types[blockId.state & 0x7] + ",powered=" + (isActive ? "true" : "false")); - } - case 73: { - return std::make_pair("redstone_ore", "normal"); - } - case 74: { - return std::make_pair("redstone_ore", "normal"); - } - case 78: { - if (blockId.state > 7) - break; - static const std::pair ids[] = { - std::pair("snow_layer", "layers=1"), - std::pair("snow_layer", "layers=2"), - std::pair("snow_layer", "layers=3"), - std::pair("snow_layer", "layers=4"), - std::pair("snow_layer", "layers=5"), - std::pair("snow_layer", "layers=6"), - std::pair("snow_layer", "layers=7"), - std::pair("snow_layer", "layers=8"), - }; - return ids[blockId.state]; - } - case 79: { - return std::make_pair("ice", "normal"); - } - case 80: { - return std::make_pair("snow", "normal"); - } - case 81: { - return std::make_pair("cactus", "normal"); - } - case 82: { - return std::make_pair("clay", "normal"); - } - case 83: { - return std::make_pair("reeds", "normal"); - } - case 86: { - if (blockId.state > 3) - break; - static const std::pair ids[] = { - std::pair("pumpkin", "facing=south"), - std::pair("pumpkin", "facing=west"), - std::pair("pumpkin", "facing=north"), - std::pair("pumpkin", "facing=east"), - }; - return ids[blockId.state]; - } - case 87: { - return std::make_pair("netherrack", "normal"); - } - case 88: { - return std::make_pair("soul_sand", "normal"); - } - case 89: { - return std::make_pair("glowstone", "normal"); - } - case 90: { - return std::make_pair("portal", blockId.state == 1 ? "axis=x" : "axis=z"); - } - case 93: { - static const std::string dirs[] = { - "east", - "south", - "west", - "north", - }; - unsigned char dir = blockId.state & 0x3; - unsigned char delay = (blockId.state >> 2) + 1; - return std::make_pair("unpowered_repeater", "delay=" + std::to_string(delay) + ",facing=" + dirs[dir] + ",locked=false"); - } - case 94: { - static const std::string dirs[] = { - "east", - "south", - "west", - "north", - }; - unsigned char dir = blockId.state & 0x3; - unsigned char delay = (blockId.state >> 2) + 1; - return std::make_pair("powered_repeater", "delay=" + std::to_string(delay) + ",facing=" + dirs[dir] + ",locked=false"); - } - case 99: { - static const std::string variants[] = { - "variant=all_inside", - "variant=north_west", - "variant=north", - "variant=north_east", - "variant=west", - "variant=center", - "variant=east", - "variant=south_west", - "variant=south", - "variant=south_east", - "variant=stem", - "variant=all_outside", - "variant=all_stem", - }; - return std::make_pair("brown_mushroom_block", variants[blockId.state]); - } - case 100: { - static const std::string variants[] = { - "variant=all_inside", - "variant=north_west", - "variant=north", - "variant=north_east", - "variant=west", - "variant=center", - "variant=east", - "variant=south_west", - "variant=south", - "variant=south_east", - "variant=stem", - "variant=all_outside", - "variant=all_stem", - }; - return std::make_pair("red_mushroom_block", variants[blockId.state]); - } - case 103: { - return std::make_pair("melon_block", "normal"); - } - case 106: { - static const std::string values[] = { - "false", - "true", - }; - return std::make_pair("vine", "east=" + values[(blockId.state >> 3) & 0x1] + ",north=" + values[(blockId.state >> 2) & 0x1] + ",south=" + values[blockId.state & 0x1] + ",up=" + values[blockId.state == 0] + ",west=" + values[(blockId.state >> 1) & 0x1]); - } - case 111: { - return std::make_pair("waterlily", "normal"); - } - case 112: { - return std::make_pair("nether_brick", "normal"); - } - case 121: { - return std::make_pair("end_stone", "normal"); - } - case 129: { - return std::make_pair("emerald_ore", "normal"); - } - case 133: { - return std::make_pair("emerald_block", "normal"); - } - case 141: { - return std::make_pair("carrots", "age=" + std::to_string(blockId.state)); - } - case 142: { - return std::make_pair("potatoes", "age=" + std::to_string(blockId.state)); - } - case 149: { - static const std::string dirs[] = { - "east", - "south", - "west", - "north", - }; - unsigned char dir = blockId.state & 0x3; - bool substractMode = (blockId.state >> 2) & 0x1; - bool isPowered = blockId.state >> 3; - return std::make_pair("unpowered_comparator", "facing=" + dirs[dir] + ",mode=" + (substractMode ? "subtract" : "compare") + ",powered=" + (isPowered ? "true" : "false")); - } - case 153: { - return std::make_pair("quartz_ore", "normal"); - } - case 155: { - return std::make_pair("quartz_block", "normal"); - } - case 161: { - if ((blockId.state & 0x3) > 2) - break; - static const std::pair ids[] = { - std::pair("acacia_leaves", "normal"), - std::pair("dark_oak_leaves", "normal"), - }; - return ids[blockId.state & 0x3]; - } - case 162: { - unsigned char type = blockId.state & 0x3; - if (type > 2) - break; - unsigned char dir = (blockId.state & 0xC) >> 2; - static const std::string types[] = { - "acacia_log", - "dark_oak_log", - }; - static const std::string dirs[] = { - "axis=y", - "axis=x", - "axis=z", - "axis=none", - }; - return std::make_pair(types[type], dirs[dir]); - } - case 175: { - bool high = ((blockId.state >> 3) & 0x1); - unsigned char type = blockId.state & 0x7; - - static const std::string types[] = { - "sunflower", - "syringa", - "double_grass", - "double_fern", - "double_rose", - "paeonia", - }; - - static const std::string isHigh[] = { - "half=lower", - "half=upper", - }; - return std::make_pair(types[type], isHigh[high]); - } - case 207: { - return std::make_pair("beetroots", "age=" + std::to_string(blockId.state)); - } - case 208: { - return std::make_pair("grass_path", "normal"); - } - default: - break; - } - - return std::make_pair("", ""); -} - std::map staticBlockInfo; void RegisterStaticBlockInfo(BlockId blockId, BlockInfo blockInfo) { diff --git a/src/Block.hpp b/src/Block.hpp index d8a78a8..fbeeaeb 100644 --- a/src/Block.hpp +++ b/src/Block.hpp @@ -41,9 +41,6 @@ namespace std { }; } -//returns name of blockstate and name of variant -std::pair TransformBlockIdToBlockStateName(BlockId blockId); - struct BlockInfo { bool collides; std::string blockstate; diff --git a/src/Render.cpp b/src/Render.cpp index 6218740..128c877 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -451,8 +451,8 @@ void Render::RenderGui() { AssetManager::GetAssetNameByBlockId(BlockId{ worldPtr->GetBlockId(selectionStatus.selectedBlock).id,0 }).c_str()); ImGui::Text("Selected block variant: %s:%s", - TransformBlockIdToBlockStateName(worldPtr->GetBlockId(selectionStatus.selectedBlock)).first.c_str(), - TransformBlockIdToBlockStateName(worldPtr->GetBlockId(selectionStatus.selectedBlock)).second.c_str()); + GetBlockInfo(worldPtr->GetBlockId(selectionStatus.selectedBlock)).blockstate.c_str(), + GetBlockInfo(worldPtr->GetBlockId(selectionStatus.selectedBlock)).variant.c_str()); } ImGui::End(); -- cgit v1.2.3