int NpcScriptInterface::luaOpenShopWindow(lua_State* L)
{
//openShopWindow(cid, items, onBuy callback, onSell callback)
int32_t sellCallback;
if (lua_isfunction(L, -1) == 0) {
sellCallback = -1;
lua_pop(L, 1); // skip it - use default value
} else {
sellCallback = popCallback(L);
}
int32_t buyCallback;
if (lua_isfunction(L, -1) == 0) {
buyCallback = -1;
lua_pop(L, 1); // skip it - use default value
} else {
buyCallback = popCallback(L);
}
if (lua_istable(L, -1) == 0) {
reportError(__FUNCTION__, "item list is not a table.");
pushBoolean(L, false);
return 1;
}
std::list<ShopInfo> items;
lua_pushnil(L);
while (lua_next(L, -2) != 0) {
const auto tableIndex = lua_gettop(L);
ShopInfo item;
item.itemId = getField<uint32_t>(L, tableIndex, "id");
item.subType = getField<int32_t>(L, tableIndex, "subType");
if (item.subType == 0) {
item.subType = getField<int32_t>(L, tableIndex, "subtype");
lua_pop(L, 1);
}
item.buyPrice = getField<uint32_t>(L, tableIndex, "buy");
item.sellPrice = getField<uint32_t>(L, tableIndex, "sell");
item.realName = getFieldString(L, tableIndex, "name");
items.push_back(item);
lua_pop(L, 6);
}
lua_pop(L, 1);
Player* player = getPlayer(L, -1);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
pushBoolean(L, false);
return 1;
}
//Close any eventual other shop window currently open.
player->closeShopWindow(false);
Npc* npc = getScriptEnv()->getNpc();
if (!npc) {
reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
pushBoolean(L, false);
return 1;
}
npc->addShopPlayer(player);
player->setShopOwner(npc, buyCallback, sellCallback);
player->openShopWindow(npc, items);
pushBoolean(L, true);
return 1;
}