From a0ced5c34e86ca271bbeafd0b57566a04f788461 Mon Sep 17 00:00:00 2001 From: hispidence Date: Tue, 13 Jul 2021 13:21:13 +0100 Subject: [PATCH] Make GameLogicScript use kInventorySlots. Make InvItem a read-only table. This means we do not need to write an enum directly in the Lua source. --- TR5Main/Scripting/GameLogicScript.cpp | 36 +++++++++++++++++++++------ TR5Main/Scripting/GameLogicScript.h | 8 +++--- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/TR5Main/Scripting/GameLogicScript.cpp b/TR5Main/Scripting/GameLogicScript.cpp index e499eb3da..5682f002d 100644 --- a/TR5Main/Scripting/GameLogicScript.cpp +++ b/TR5Main/Scripting/GameLogicScript.cpp @@ -13,6 +13,7 @@ #include "pickup.h" #include "newinv2.h" #include +#include "InventorySlots.h" extern GameFlow* g_GameFlow; GameScript* g_GameScript; @@ -29,6 +30,26 @@ GameScript::GameScript(sol::state* lua) : LuaHandler{ lua } m_lua->set_function("GetInvItemCount", &GameScript::InventoryGetCount); m_lua->set_function("SetInvItemCount", &GameScript::InventorySetCount); + // Put all the data in InvItem's metatable + m_lua->set("InvItemMeta", sol::as_table(kInventorySlots)); + + // Make the metatable's __index refer to itself so that requests + // to InvItem will go through to the metatable (and thus the + // kInventorySlot members) + m_lua->safe_script("InvItemMeta.__index = InvItemMeta"); + + // Don't allow InvItem to have new elements put into it + m_lua->safe_script("InvItemMeta.__newindex = function() error('InvItem is read-only') end"); + + // Protect the metatable + m_lua->safe_script("InvItemMeta.__metatable = 'metatable is protected'"); + + auto tab = m_lua->create_named_table("InvItem"); + m_lua->safe_script("setmetatable(InvItem, InvItemMeta)"); + + // point InvItemMeta array from the table + m_lua->safe_script("InvItemMeta = nil"); + GameScriptItemInfo::Register(m_lua); GameScriptPosition::Register(m_lua); GameScriptRotation::Register(m_lua); @@ -311,26 +332,25 @@ void GameScript::Earthquake(int strength) } // Inventory -void GameScript::InventoryAdd(int slot, sol::optional count) +void GameScript::InventoryAdd(GAME_OBJECT_ID slot, sol::optional count) { - PickedUpObject(static_cast(inventry_objects_list[slot].object_number), count.value_or(0)); + PickedUpObject(slot, count.value_or(0)); } -void GameScript::InventoryRemove(int slot, sol::optional count) +void GameScript::InventoryRemove(GAME_OBJECT_ID slot, sol::optional count) { RemoveObjectFromInventory(static_cast(inventry_objects_list[slot].object_number), count.value_or(0)); } -int GameScript::InventoryGetCount(int slot) +int GameScript::InventoryGetCount(GAME_OBJECT_ID slot) { - return GetInventoryCount(static_cast( inventry_objects_list[slot].object_number)); + return GetInventoryCount(slot); } -void GameScript::InventorySetCount(int slot, int count) +void GameScript::InventorySetCount(GAME_OBJECT_ID slot, int count) { - auto result = static_cast( inventry_objects_list[slot].object_number ); // add the amount we'd need to add to get to count - int currAmt = GetInventoryCount(result); + int currAmt = GetInventoryCount(slot); InventoryAdd(slot, count - currAmt); } diff --git a/TR5Main/Scripting/GameLogicScript.h b/TR5Main/Scripting/GameLogicScript.h index 8d232e7bb..42b4b7c37 100644 --- a/TR5Main/Scripting/GameLogicScript.h +++ b/TR5Main/Scripting/GameLogicScript.h @@ -96,10 +96,10 @@ public: void Earthquake(int strength); // Inventory - static void InventoryAdd(int slot, sol::optional count); - static void InventoryRemove(int slot, sol::optional count); - static int InventoryGetCount(int slot); - static void InventorySetCount(int slot, int count); + static void InventoryAdd(GAME_OBJECT_ID slot, sol::optional count); + static void InventoryRemove(GAME_OBJECT_ID slot, sol::optional count); + static int InventoryGetCount(GAME_OBJECT_ID slot); + static void InventorySetCount(GAME_OBJECT_ID slot, int count); void InventoryCombine(int slot1, int slot2); void InventorySeparate(int slot);