diff --git a/TR5Main/Scripting/GameLogicScript.cpp b/TR5Main/Scripting/GameLogicScript.cpp index 13c4e1d33..dec14f09c 100644 --- a/TR5Main/Scripting/GameLogicScript.cpp +++ b/TR5Main/Scripting/GameLogicScript.cpp @@ -32,6 +32,10 @@ extern bool const WarningsAsErrors = true; GameScript::GameScript(sol::state* lua) : LuaHandler{ lua }, m_itemsMapId{}, m_itemsMapName{}, m_meshesMapName{} { +/*** Ambience and music +@section Music +*/ + /*** Set the named track as the ambient track, and start playing it @function SetAmbientTrack @@ -47,26 +51,30 @@ Start playing the named track. */ m_lua->set_function("PlayAudioTrack", &GameScript::PlayAudioTrack); +/*** Player inventory management +@section Inventory +*/ + /*** Add x of a certain item to the inventory. @function GiveInvItem -@tparam @{InvItem}) item the item to be added -@tparam int the number of items to add +@tparam @{InvItem} item the item to be added +@tparam int count the number of items to add */ m_lua->set_function("GiveInvItem", &GameScript::InventoryAdd); /*** Remove x of a certain item from the inventory. @function TakeInvItem -@tparam @{InvItem}) item the item to be removed -@tparam int the number of items to remove +@tparam @{InvItem} item the item to be removed +@tparam int count the number of items to remove */ m_lua->set_function("TakeInvItem", &GameScript::InventoryRemove); /*** Get the amount the player holds of an item. @function GetInvItemCount -@tparam @{InvItem}) item the item to check +@tparam @{InvItem} item the item to check @return the amount of the item the player has in the inventory */ m_lua->set_function("GetInvItemCount", &GameScript::InventoryGetCount); @@ -75,14 +83,21 @@ Get the amount the player holds of an item. Set the amount of a certain item the player has in the inventory. Similar to @{GiveInvItem} but replaces with the new amount instead of adding it. @function SetInvItemCount -@tparam @{InvItem}) item the item to be set -@tparam int the number of items the player will have +@tparam @{InvItem} item the item to be set +@tparam int count the number of items the player will have */ m_lua->set_function("SetInvItemCount", &GameScript::InventorySetCount); +/*** Game entity getters. +All Lua variables created with these functions will be non-owning. +This means that the actual in-game entity (object/camera/sink/whatever) +will _not_ be removed from the game if the Lua variable goes out of scope +or is destroyed in some other way. +@section getters +*/ + /*** -Get an ItemInfo by its name. This is non-owning, meaning the actual item will not -be removed from the game if the ItemInfo object is destroyed. +Get an ItemInfo by its name. @function GetItemByName @tparam string name the unique name of the item as set in, or generated by, Tomb Editor @return a non-owning ItemInfo referencing the item. @@ -99,8 +114,7 @@ Not sure if we're using this. m_lua->set_function("GetItemByID", &GameScript::GetItemById, this); /*** -Get a MeshInfo by its name. This is non-owning, meaning the actual mesh will not -be removed from the game if the MeshInfo object is destroyed. +Get a MeshInfo by its name. @function GetMeshByName @tparam string name the unique name of the mesh as set in, or generated by, Tomb Editor @return a non-owning MeshInfo referencing the mesh. @@ -108,14 +122,21 @@ be removed from the game if the MeshInfo object is destroyed. m_lua->set_function("GetMeshByName", &GameScript::GetMeshByName, this); /*** -Get a CameraInfo by its name. This is non-owning, meaning the actual camera will not -be removed from the game if the CameraInfo object is destroyed. +Get a CameraInfo by its name. @function GetCameraByName @tparam string name the unique name of the camera as set in, or generated by, Tomb Editor @return a non-owning CameraInfo referencing the camera. */ m_lua->set_function("GetCameraByName", &GameScript::GetCameraByName, this); +/*** +Get a SinkInfo by its name. +@function GetSinkByName +@tparam string name the unique name of the sink as set in, or generated by, Tomb Editor +@return a non-owning CameraInfo referencing the sink. +*/ + m_lua->set_function("GetSinkByName", &GameScript::GetSinkByName, this); + auto makeReadOnlyTable = [this](std::string const & tableName, auto const& container) { auto mt = tableName + "Meta"; @@ -145,8 +166,8 @@ auto makeReadOnlyTable = [this](std::string const & tableName, auto const& conta GameScriptItemInfo::Register(m_lua); GameScriptItemInfo::SetNameCallbacks( - [this](std::string const& str, short num) { return AddLuaName(str, num); }, - [this](std::string const& str) { return RemoveLuaName(str); } + [this](std::string const& str, short num) { return AddLuaNameItem(str, num); }, + [this](std::string const& str) { return RemoveLuaNameItem(str); } ); GameScriptMeshInfo::Register(m_lua); @@ -161,6 +182,12 @@ auto makeReadOnlyTable = [this](std::string const & tableName, auto const& conta [this](std::string const& str) { return RemoveLuaNameCamera(str); } ); + GameScriptSinkInfo::Register(m_lua); + GameScriptSinkInfo::SetNameCallbacks( + [this](std::string const& str, SINK_INFO & info) { return AddLuaNameSink(str, info); }, + [this](std::string const& str) { return RemoveLuaNameSink(str); } + ); + GameScriptPosition::Register(m_lua); GameScriptRotation::Register(m_lua); GameScriptColor::Register(m_lua); @@ -188,12 +215,12 @@ void GameScript::AddLuaId(int luaId, short itemNumber) m_itemsMapId.insert(std::pair(luaId, itemNumber)); } -bool GameScript::RemoveLuaName(std::string const & luaName) +bool GameScript::RemoveLuaNameItem(std::string const & luaName) { return m_itemsMapName.erase(luaName); } -bool GameScript::AddLuaName(std::string const & luaName, short itemNumber) +bool GameScript::AddLuaNameItem(std::string const & luaName, short itemNumber) { return m_itemsMapName.insert(std::pair(luaName, itemNumber)).second; } @@ -218,6 +245,16 @@ bool GameScript::AddLuaNameCamera(std::string const& luaName, LEVEL_CAMERA_INFO& return m_camerasMapName.insert(std::pair(luaName, infoRef)).second; } +bool GameScript::RemoveLuaNameSink(std::string const & luaName) +{ + return m_sinksMapName.erase(luaName); +} + +bool GameScript::AddLuaNameSink(std::string const& luaName, SINK_INFO& infoRef) +{ + return m_sinksMapName.insert(std::pair(luaName, infoRef)).second; +} + void GameScript::FreeLevelScripts() { /* @@ -368,7 +405,6 @@ void GameScript::SetVariables(std::map& locals, std::map(std::map& locals, std::map& globals); template void GameScript::SetVariables(std::map& locals, std::map& globals); template void GameScript::SetVariables(std::map& locals, std::map& globals); - std::unique_ptr GameScript::GetItemById(int id) { if (m_itemsMapId.find(id) == m_itemsMapId.end()) @@ -381,52 +417,41 @@ std::unique_ptr GameScript::GetItemById(int id) return std::make_unique(m_itemsMapId[id], false); } -std::unique_ptr GameScript::GetItemByName(std::string const & name) +template +std::unique_ptr GetTByName(std::string const & type, std::string const& name, std::map const & map) { - if (m_itemsMapName.find(name) == m_itemsMapName.end()) + if (map.find(name) == map.end()) { if (WarningsAsErrors) { - std::string error{ "Item name not found: " }; + std::string error = type + " name not found: "; error += name; throw std::runtime_error{error}; } - return std::unique_ptr(nullptr); + return std::unique_ptr(nullptr); } - return std::make_unique(m_itemsMapName[name], false); + return std::make_unique(map.at(name), false); +} + +std::unique_ptr GameScript::GetItemByName(std::string const & name) +{ + return GetTByName("Item", name, m_itemsMapName); } std::unique_ptr GameScript::GetMeshByName(std::string const & name) { - if (m_meshesMapName.find(name) == m_meshesMapName.end()) - { - if (WarningsAsErrors) - { - std::string error{ "Mesh name not found: " }; - error += name; - throw std::runtime_error{error}; - } - return std::unique_ptr(nullptr); - } - - return std::make_unique(m_meshesMapName.at(name), false); + return GetTByName("Mesh", name, m_meshesMapName); } std::unique_ptr GameScript::GetCameraByName(std::string const & name) { - if (m_camerasMapName.find(name) == m_camerasMapName.end()) - { - if (WarningsAsErrors) - { - std::string error{ "Camera name not found: " }; - error += name; - throw std::runtime_error{error}; - } - return std::unique_ptr(nullptr); - } + return GetTByName("Camera", name, m_camerasMapName); +} - return std::make_unique(m_camerasMapName.at(name), false); +std::unique_ptr GameScript::GetSinkByName(std::string const & name) +{ + return GetTByName("Sink", name, m_sinksMapName); } void GameScript::PlayAudioTrack(std::string const & trackName, bool looped) diff --git a/TR5Main/Scripting/GameLogicScript.h b/TR5Main/Scripting/GameLogicScript.h index eb1e610de..9271527fe 100644 --- a/TR5Main/Scripting/GameLogicScript.h +++ b/TR5Main/Scripting/GameLogicScript.h @@ -8,6 +8,7 @@ #include "GameScriptRotation.h" #include "GameScriptItemInfo.h" #include "GameScriptMeshInfo.h" +#include "GameScriptSinkInfo.h" #include "GameScriptCameraInfo.h" struct LuaFunction { @@ -52,6 +53,7 @@ private: std::map m_itemsMapName; std::map m_meshesMapName; std::map m_camerasMapName; + std::map m_sinksMapName; std::vector m_triggers; sol::protected_function m_onStart; sol::protected_function m_onLoad; @@ -64,12 +66,14 @@ public: void FreeLevelScripts(); void AddTrigger(LuaFunction* function); void AddLuaId(int luaId, short itemNumber); - bool AddLuaName(std::string const & luaName, short itemNumber); - bool RemoveLuaName(std::string const& luaName); + bool AddLuaNameItem(std::string const & luaName, short itemNumber); + bool RemoveLuaNameItem(std::string const& luaName); bool AddLuaNameMesh(std::string const & luaName, MESH_INFO &); bool RemoveLuaNameMesh(std::string const& luaName); bool AddLuaNameCamera(std::string const & luaName, LEVEL_CAMERA_INFO &); bool RemoveLuaNameCamera(std::string const& luaName); + bool AddLuaNameSink(std::string const & luaName, SINK_INFO &); + bool RemoveLuaNameSink(std::string const& luaName); void AssignItemsAndLara(); @@ -81,6 +85,7 @@ public: std::unique_ptr GetItemByName(std::string const & name); std::unique_ptr GetMeshByName(std::string const & name); std::unique_ptr GetCameraByName(std::string const & name); + std::unique_ptr GetSinkByName(std::string const & name); // Variables template diff --git a/TR5Main/Specific/level.cpp b/TR5Main/Specific/level.cpp index 73b111113..b62f3b25b 100644 --- a/TR5Main/Specific/level.cpp +++ b/TR5Main/Specific/level.cpp @@ -124,7 +124,7 @@ int LoadItems() ReadBytes(buffer, numBytes); item->luaName = std::string((const char*)buffer); - g_GameScript->AddLuaName(item->luaName, i); + g_GameScript->AddLuaNameItem(item->luaName, i); memcpy(&item->startPos, &item->pos, sizeof(PHD_3DPOS)); } @@ -413,6 +413,7 @@ void LoadCameras() sink.luaName = std::string((const char*)buffer); g_Level.Sinks.push_back(sink); + g_GameScript->AddLuaNameSink(sink.luaName, g_Level.Sinks.back()); } }