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 --- cwd/assets/altcraft/scripts/init.lua | 12 ++++-- src/Plugin.cpp | 71 +++++++++++++++++++++++++++++++++--- 2 files changed, 74 insertions(+), 9 deletions(-) diff --git a/cwd/assets/altcraft/scripts/init.lua b/cwd/assets/altcraft/scripts/init.lua index 2137464..de58b90 100644 --- a/cwd/assets/altcraft/scripts/init.lua +++ b/cwd/assets/altcraft/scripts/init.lua @@ -20,9 +20,15 @@ function plugin.onUnload () end function plugin.onTick (deltaTime) - if AC.GetGameState() and AC.GetGameState():GetPlayer() then - local player = AC.GetGameState():GetPlayer() - player.pos.x = player.pos.x + deltaTime * 0.5 + if AC.GetGameState() and AC.GetGameState():GetPlayer() and AC.GetGameState():GetTimeStatus().worldAge > 0 then + -- local player = AC.GetGameState():GetPlayer() + -- player.pos.x = player.pos.x + deltaTime * 0.5 + + -- local playerPos = AC.GetGameState():GetPlayer().pos + -- local wrld = AC.GetGameState():GetWorld() + -- playerPosV = Vector.new(playerPos.x, playerPos.y - 1, playerPos.z) + -- bid = wrld:GetBlockId(playerPosV) + -- print(bid.id..":"..bid.state) end end 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