Skip to content

Commit

Permalink
shop to smart pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
mehah committed Sep 22, 2023
1 parent d40ee19 commit 363e902
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 40 deletions.
48 changes: 13 additions & 35 deletions src/lua/functions/creatures/npc/shop_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,15 @@

int ShopFunctions::luaCreateShop(lua_State* L) {
// Shop() will create a new shop item
Shop* shop = new Shop();
if (shop) {
pushUserdata<Shop>(L, shop);
setMetatable(L, -1, "Shop");
} else {
lua_pushnil(L);
}
pushUserdata<Shop>(L, std::make_shared<Shop>());
setMetatable(L, -1, "Shop");
return 1;
}

int ShopFunctions::luaDeleteShop(lua_State* L) {
// shop:delete() shop:__gc()
Shop** shopPtr = getRawUserdata<Shop>(L, 1);
if (shopPtr && *shopPtr) {
delete *shopPtr;
*shopPtr = nullptr;
}
return 0;
}

int ShopFunctions::luaShopSetId(lua_State* L) {
// shop:setId(id)
Shop* shop = getUserdata<Shop>(L, 1);
if (shop) {

if (const auto &shop = getUserdataShared<Shop>(L, 1)) {
if (isNumber(L, 2)) {
shop->shopBlock.itemId = getNumber<uint16_t>(L, 2);
pushBoolean(L, true);
Expand All @@ -54,7 +39,7 @@ int ShopFunctions::luaShopSetId(lua_State* L) {

int ShopFunctions::luaShopSetIdFromName(lua_State* L) {
// shop:setIdFromName(name)
Shop* shop = getUserdata<Shop>(L, 1);
const auto &shop = getUserdataShared<Shop>(L, 1);
if (shop && isString(L, 2)) {
auto name = getString(L, 2);
auto ids = Item::items.nameToItems.equal_range(asLowerCaseString(name));
Expand Down Expand Up @@ -87,8 +72,7 @@ int ShopFunctions::luaShopSetIdFromName(lua_State* L) {

int ShopFunctions::luaShopSetNameItem(lua_State* L) {
// shop:setNameItem(name)
Shop* shop = getUserdata<Shop>(L, 1);
if (shop) {
if (const auto &shop = getUserdataShared<Shop>(L, 1)) {
shop->shopBlock.itemName = getString(L, 2);
pushBoolean(L, true);
} else {
Expand All @@ -99,8 +83,7 @@ int ShopFunctions::luaShopSetNameItem(lua_State* L) {

int ShopFunctions::luaShopSetCount(lua_State* L) {
// shop:setCount(count)
Shop* shop = getUserdata<Shop>(L, 1);
if (shop) {
if (const auto &shop = getUserdataShared<Shop>(L, 1)) {
shop->shopBlock.itemSubType = getNumber<uint32_t>(L, 2);
pushBoolean(L, true);
} else {
Expand All @@ -111,8 +94,7 @@ int ShopFunctions::luaShopSetCount(lua_State* L) {

int ShopFunctions::luaShopSetBuyPrice(lua_State* L) {
// shop:setBuyPrice(price)
Shop* shop = getUserdata<Shop>(L, 1);
if (shop) {
if (const auto &shop = getUserdataShared<Shop>(L, 1)) {
shop->shopBlock.itemBuyPrice = getNumber<uint32_t>(L, 2);
pushBoolean(L, true);
} else {
Expand All @@ -123,8 +105,7 @@ int ShopFunctions::luaShopSetBuyPrice(lua_State* L) {

int ShopFunctions::luaShopSetSellPrice(lua_State* L) {
// shop:setSellPrice(chance)
Shop* shop = getUserdata<Shop>(L, 1);
if (shop) {
if (const auto &shop = getUserdataShared<Shop>(L, 1)) {
shop->shopBlock.itemSellPrice = getNumber<uint32_t>(L, 2);
pushBoolean(L, true);
} else {
Expand All @@ -135,8 +116,7 @@ int ShopFunctions::luaShopSetSellPrice(lua_State* L) {

int ShopFunctions::luaShopSetStorageKey(lua_State* L) {
// shop:setStorageKey(storage)
Shop* shop = getUserdata<Shop>(L, 1);
if (shop) {
if (const auto &shop = getUserdataShared<Shop>(L, 1)) {
shop->shopBlock.itemStorageKey = getNumber<uint32_t>(L, 2);
pushBoolean(L, true);
} else {
Expand All @@ -147,8 +127,7 @@ int ShopFunctions::luaShopSetStorageKey(lua_State* L) {

int ShopFunctions::luaShopSetStorageValue(lua_State* L) {
// shop:setStorageValue(value)
Shop* shop = getUserdata<Shop>(L, 1);
if (shop) {
if (const auto &shop = getUserdataShared<Shop>(L, 1)) {
shop->shopBlock.itemStorageValue = getNumber<uint32_t>(L, 2);
pushBoolean(L, true);
} else {
Expand All @@ -159,9 +138,8 @@ int ShopFunctions::luaShopSetStorageValue(lua_State* L) {

int ShopFunctions::luaShopAddChildShop(lua_State* L) {
// shop:addChildShop(shop)
Shop* shop = getUserdata<Shop>(L, 1);
if (shop) {
shop->shopBlock.childShop.push_back(getUserdata<Shop>(L, 2)->shopBlock);
if (const auto &shop = getUserdataShared<Shop>(L, 1)) {
shop->shopBlock.childShop.push_back(getUserdataShared<Shop>(L, 2)->shopBlock);
} else {
lua_pushnil(L);
}
Expand Down
6 changes: 1 addition & 5 deletions src/lua/functions/creatures/npc/shop_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
class ShopFunctions final : LuaScriptInterface {
public:
static void init(lua_State* L) {
registerClass(L, "Shop", "", ShopFunctions::luaCreateShop);
registerMetaMethod(L, "Shop", "__gc", ShopFunctions::luaDeleteShop);
registerMethod(L, "Shop", "delete", ShopFunctions::luaDeleteShop);

registerSharedClass(L, "Shop", "", ShopFunctions::luaCreateShop);
registerMethod(L, "Shop", "setId", ShopFunctions::luaShopSetId);
registerMethod(L, "Shop", "setIdFromName", ShopFunctions::luaShopSetIdFromName);
registerMethod(L, "Shop", "setNameItem", ShopFunctions::luaShopSetNameItem);
Expand All @@ -31,7 +28,6 @@ class ShopFunctions final : LuaScriptInterface {

private:
static int luaCreateShop(lua_State* L);
static int luaDeleteShop(lua_State* L);
static int luaShopSetId(lua_State* L);
static int luaShopSetIdFromName(lua_State* L);
static int luaShopSetNameItem(lua_State* L);
Expand Down

0 comments on commit 363e902

Please sign in to comment.