• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!

TFS 1.1 Source edit

Itutorial

Legendary OT User
Joined
Dec 23, 2014
Messages
2,461
Solutions
68
Reaction score
1,123
So, I am trying to figure out how to make it so npcs can send a trade window that requires items instead of gold to buy... I know I need to edit this function

Code:
int NpcScriptInterface::luaNpcOpenCustomShopWindow(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;
}

I am just not sure how.
 
I also might need to edit this guy

Code:
int NpcScriptInterface::luaDoSellItem(lua_State* L)
{
    //doSellItem(cid, itemid, amount, <optional> subtype, <optional> actionid, <optional: default: 1> canDropOnMap)
    Player* player = getPlayer(L, 1);
    if (!player) {
        reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
        pushBoolean(L, false);
        return 1;
    }

    uint32_t sellCount = 0;

    uint32_t itemId = getNumber<uint32_t>(L, 2);
    uint32_t amount = getNumber<uint32_t>(L, 3);
    uint32_t subType;

    int32_t n = getNumber<int32_t>(L, 4, -1);
    if (n != -1) {
        subType = n;
    } else {
        subType = 1;
    }

    uint32_t actionId = getNumber<uint32_t>(L, 5, 0);
    bool canDropOnMap = getBoolean(L, 6, true);

    const ItemType& it = Item::items[itemId];
    if (it.stackable) {
        while (amount > 0) {
            int32_t stackCount = std::min<int32_t>(100, amount);
            Item* item = Item::CreateItem(it.id, stackCount);
            if (item && actionId != 0) {
                item->setActionId(actionId);
            }

            if (g_game.internalPlayerAddItem(player, item, canDropOnMap) != RETURNVALUE_NOERROR) {
                delete item;
                lua_pushnumber(L, sellCount);
                return 1;
            }

            amount -= stackCount;
            sellCount += stackCount;
        }
    } else {
        for (uint32_t i = 0; i < amount; ++i) {
            Item* item = Item::CreateItem(it.id, subType);
            if (item && actionId != 0) {
                item->setActionId(actionId);
            }

            if (g_game.internalPlayerAddItem(player, item, canDropOnMap) != RETURNVALUE_NOERROR) {
                delete item;
                lua_pushnumber(L, sellCount);
                return 1;
            }

            ++sellCount;
        }
    }

    lua_pushnumber(L, sellCount);
    return 1;
}
 
Back
Top