#pragma once #include "items.h" #include "room.h" #include "LuaHandler.h" #include "trmath.h" #include #include "GameScriptColor.h" #include "GameScriptPosition.h" #include "GameScriptRotation.h" #include "GameScriptItemInfo.h" #include "GameScriptMeshInfo.h" #include "GameScriptSinkInfo.h" #include "GameScriptAIObject.h" #include "GameScriptSoundSourceInfo.h" #include "GameScriptCameraInfo.h" #include "GameScriptDisplayString.h" struct LuaFunction { std::string Name; std::string Code; bool Executed; }; struct GameScriptVector3 { float x; float y; float z; }; class LuaVariables { public: std::unordered_map variables; sol::object GetVariable(sol::table tab, std::string key); void SetVariable(sol::table tab, std::string key, sol::object value); }; struct LuaVariable { bool IsGlobal; std::string Name; int Type; float FloatValue; int IntValue; std::string StringValue; bool BoolValue; }; using CallbackDrawString = std::function; using DisplayStringMap = std::unordered_map; using VarMapVal = std::variant< short, std::reference_wrapper, std::reference_wrapper, std::reference_wrapper, std::reference_wrapper, std::reference_wrapper>; class GameScript : public LuaHandler { private: LuaVariables m_globals{}; LuaVariables m_locals{}; DisplayStringMap m_userDisplayStrings{}; std::unordered_map m_nameMap{}; std::unordered_map m_itemsMapName{}; std::unordered_map m_levelFuncs{}; sol::protected_function m_onStart{}; sol::protected_function m_onLoad{}; sol::protected_function m_onControlPhase{}; sol::protected_function m_onSave{}; sol::protected_function m_onEnd{}; void ResetLevelTables(); CallbackDrawString m_callbackDrawSring; public: GameScript(sol::state* lua); void FreeLevelScripts(); bool SetDisplayString(DisplayStringIDType id, UserDisplayString const & ds); std::optional> GetDisplayString(DisplayStringIDType id); bool ScheduleRemoveDisplayString(DisplayStringIDType id); bool SetLevelFunc(sol::table tab, std::string const& luaName, sol::object obj); sol::protected_function GetLevelFunc(sol::table tab, std::string const& luaName); void AssignItemsAndLara(); void ExecuteFunction(std::string const & name); void MakeItemInvisible(short id); template std::unique_ptr GetByName(std::string const& name) { ScriptAssertF(m_nameMap.find(name) != m_nameMap.end(), "{} name not found: {}", S, name); return std::make_unique(std::get(m_nameMap.at(name)), false); } template bool AddName(std::string const& key, Value&& val) { auto p = std::pair{ key, val }; return m_nameMap.insert(p).second; } bool RemoveName(std::string const& key) { return m_nameMap.erase(key); } // Variables template void GetVariables(std::map& locals, std::map& globals); template void SetVariables(std::map& locals, std::map& globals); void ResetVariables(); void SetCallbackDrawString(CallbackDrawString cb); void ShowString(GameScriptDisplayString const&, sol::optional nSeconds); void ProcessDisplayStrings(float dt); void InitCallbacks(); void OnStart(); void OnLoad(); void OnControlPhase(float dt); void OnSave(); void OnEnd(); };