Add AddLuaNameSink, RemoveLuaNameSink, and GetSinkByName.

Make a template that all GetXByName member functions work off, to reduce copypasted functions.
For consistency, rename RemoveLuaName and AddLuaName to RemoveLuaNameItem and AddLuaNameItem.
Fix some documentation comments, order function documentation into sections.
This commit is contained in:
hispidence 2021-07-23 16:02:30 +01:00
parent 40933eef0c
commit f9ae0046c6
3 changed files with 80 additions and 49 deletions

View file

@ -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<int, short>(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<std::string, short>(luaName, itemNumber)).second;
}
@ -218,6 +245,16 @@ bool GameScript::AddLuaNameCamera(std::string const& luaName, LEVEL_CAMERA_INFO&
return m_camerasMapName.insert(std::pair<std::string, LEVEL_CAMERA_INFO&>(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<std::string, SINK_INFO&>(luaName, infoRef)).second;
}
void GameScript::FreeLevelScripts()
{
/*
@ -368,7 +405,6 @@ void GameScript::SetVariables(std::map<std::string, T>& locals, std::map<std::st
template void GameScript::SetVariables<bool>(std::map<std::string, bool>& locals, std::map<std::string, bool>& globals);
template void GameScript::SetVariables<float>(std::map<std::string, float>& locals, std::map<std::string, float>& globals);
template void GameScript::SetVariables<std::string>(std::map<std::string, std::string>& locals, std::map<std::string, std::string>& globals);
std::unique_ptr<GameScriptItemInfo> GameScript::GetItemById(int id)
{
if (m_itemsMapId.find(id) == m_itemsMapId.end())
@ -381,52 +417,41 @@ std::unique_ptr<GameScriptItemInfo> GameScript::GetItemById(int id)
return std::make_unique<GameScriptItemInfo>(m_itemsMapId[id], false);
}
std::unique_ptr<GameScriptItemInfo> GameScript::GetItemByName(std::string const & name)
template <typename T, typename Stored>
std::unique_ptr<T> GetTByName(std::string const & type, std::string const& name, std::map<std::string, Stored> 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<GameScriptItemInfo>(nullptr);
return std::unique_ptr<T>(nullptr);
}
return std::make_unique<GameScriptItemInfo>(m_itemsMapName[name], false);
return std::make_unique<T>(map.at(name), false);
}
std::unique_ptr<GameScriptItemInfo> GameScript::GetItemByName(std::string const & name)
{
return GetTByName<GameScriptItemInfo, short>("Item", name, m_itemsMapName);
}
std::unique_ptr<GameScriptMeshInfo> 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<GameScriptMeshInfo>(nullptr);
}
return std::make_unique<GameScriptMeshInfo>(m_meshesMapName.at(name), false);
return GetTByName<GameScriptMeshInfo, MESH_INFO &>("Mesh", name, m_meshesMapName);
}
std::unique_ptr<GameScriptCameraInfo> 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<GameScriptCameraInfo>(nullptr);
}
return GetTByName<GameScriptCameraInfo, LEVEL_CAMERA_INFO &>("Camera", name, m_camerasMapName);
}
return std::make_unique<GameScriptCameraInfo>(m_camerasMapName.at(name), false);
std::unique_ptr<GameScriptSinkInfo> GameScript::GetSinkByName(std::string const & name)
{
return GetTByName<GameScriptSinkInfo, SINK_INFO &>("Sink", name, m_sinksMapName);
}
void GameScript::PlayAudioTrack(std::string const & trackName, bool looped)