Exemplo n.º 1
0
int NpcScriptInterface::luaNpcOpenShopWindow(lua_State* L)
{
    // npc:openShopWindow(cid, items, buyCallback, sellCallback)
    if (!isTable(L, 3)) {
        reportErrorFunc("item list is not a table.");
        pushBoolean(L, false);
        return 1;
    }

    Player* player = getPlayer(L, 2);
    if (!player) {
        reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
        pushBoolean(L, false);
        return 1;
    }

    Npc* npc = getUserdata<Npc>(L, 1);
    if (!npc) {
        reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
        pushBoolean(L, false);
        return 1;
    }

    int32_t sellCallback = -1;
    if (LuaScriptInterface::isFunction(L, 5)) {
        sellCallback = luaL_ref(L, LUA_REGISTRYINDEX);
    }

    int32_t buyCallback = -1;
    if (LuaScriptInterface::isFunction(L, 4)) {
        buyCallback = luaL_ref(L, LUA_REGISTRYINDEX);
    }

    std::list<ShopInfo> items;

    lua_pushnil(L);
    while (lua_next(L, 3) != 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->closeShopWindow(false);
    npc->addShopPlayer(player);

    player->setShopOwner(npc, buyCallback, sellCallback);
    player->openShopWindow(npc, items);

    pushBoolean(L, true);
    return 1;
}
Exemplo n.º 2
0
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;
}