Jump to content
  • 0

Sell item to npc then npc will give something in return item/zeny/points


Louis T Steinhil

Question


  • Group:  Members
  • Topic Count:  37
  • Topics Per Day:  0.01
  • Content Count:  177
  • Reputation:   33
  • Joined:  06/22/13
  • Last Seen:  

Hello this is my script. I know something is missing but it has no errors. (latest rAthena trunk) Thanks in advance!

Here's the code:

firstcity,229,87,4	script	Salvage NPC	856,{
    mes "[Salvage NPC]";
    mes "Hello! I can salvage your old weapons and armor.";
    mes "In return, I'll give you a special item based on the weapon's level.";
    next;
    mes "[Salvage NPC]";
    mes "Would you like to proceed?";
    switch(select("Yes:No")) {
        case 1:
            callfunc("salvage_item");
            break;
        case 2:
            mes "[Salvage NPC]";
            mes "Alright, come back if you change your mind.";
            close;
    }
    close;
    
    // Handle item sale
    OnSellItem:
        // Rewards based on weapon level
        setarray .@reward_common, 501, 502; // Example common loot item IDs
        setarray .@reward_rare, 503, 504;   // Example rare loot item IDs
        setarray .@reward_unique, 505, 506; // Example unique loot item IDs
        setarray .@reward_legendary, 507, 508; // Example legendary loot item IDs
        setarray .@reward_ancient, 509, 510; // Example ancient loot item IDs

        set .@item_id, @sold_nameid;
        set .@weapon_level, getiteminfo(.@item_id, ITEMINFO_WEAPONLEVEL);

        switch (.@weapon_level) {
            case 1:
                // Reward common item/points
                getitem .@reward_common[rand(getarraysize(.@reward_common))], 1;
                break;
            case 2:
                // Reward rare item/points
                getitem .@reward_rare[rand(getarraysize(.@reward_rare))], 1;
                break;
            case 3:
                // Reward unique item/points
                getitem .@reward_unique[rand(getarraysize(.@reward_unique))], 1;
                break;
            case 4:
                // Reward legendary item/points
                getitem .@reward_legendary[rand(getarraysize(.@reward_legendary))], 1;
                break;
            case 5:
                // Reward ancient item/points
                getitem .@reward_ancient[rand(getarraysize(.@reward_ancient))], 1;
                break;
            default:
                dispbottom "This item cannot be salvaged.";
        }

        dispbottom "Thank you! Here is your reward.";
        end;
}

function	script	salvage_item	{
    mes "[Salvage NPC]";
    mes "Please show me the items you want to salvage.";
    next;

    // Fetch player's inventory
    getinventorylist;

    // Collect all weapons and armor in the player's inventory
    set .@item_count, 0;
    for (.@i = 0; .@i < @inventorylist_count; .@i++) {
        set .@type, getiteminfo(@inventorylist_id[.@i], 2);
        if ((.@type == 4 || .@type == 5) && @inventorylist_equip[.@i] == 0) { // Type 4 = Weapon, Type 5 = Armor, and not equipped
            set .@salvageable_items[.@item_count], @inventorylist_id[.@i];
            set .@salvageable_items_qty[.@item_count], @inventorylist_amount[.@i];
            set .@item_count, .@item_count + 1;
        }
    }

    if (.@item_count == 0) {
        mes "[Salvage NPC]";
        mes "You don't have any salvageable items.";
        close;
    }

    mes "[Salvage NPC]";
    mes "Here is a summary of the items you can salvage:";
    for (.@i = 0; .@i < .@item_count; .@i++) {
        mes "^0000FF" + getitemname(.@salvageable_items[.@i]) + "^000000 x" + .@salvageable_items_qty[.@i];
    }
    next;

    if (select("Proceed with Salvage:Cancel") == 2) {
        mes "[Salvage NPC]";
        mes "Alright, come back if you change your mind.";
        close;
    }

    // Call the shop
    callshop "salvage_shop", 2;
    npcshopattach "salvage_shop";
    close;
}

// Shop definition
-	shop	salvage_shop	-1,501:-1

here's the debug:

[Debug]: npc_scriptcont: Salvage NPC (sd->npc_id=111487803) is not 'Unknown NPC' (id=0).

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

  • Group:  Members
  • Topic Count:  63
  • Topics Per Day:  0.02
  • Content Count:  1016
  • Reputation:   191
  • Joined:  11/27/14
  • Last Seen:  

The script you provided is close to being functional, but there are some issues that need to be addressed to ensure it works correctly. Below is the corrected script with explanations for the changes:

Here is the corrected script

firstcity,229,87,4	script	Salvage NPC	856,{
    mes "[Salvage NPC]";
    mes "Hello! I can salvage your old weapons and armor.";
    mes "In return, I'll give you a special item based on the weapon's level.";
    next;
    mes "[Salvage NPC]";
    mes "Would you like to proceed?";
    switch(select("Yes:No")) {
        case 1:
            callfunc("salvage_item");
            break;
        case 2:
            mes "[Salvage NPC]";
            mes "Alright, come back if you change your mind.";
            close;
    }
    close;
}

function	script	salvage_item	{
    mes "[Salvage NPC]";
    mes "Please show me the items you want to salvage.";
    next;

    // Fetch player's inventory
    getinventorylist;

    // Collect all weapons and armor in the player's inventory
    set .@item_count, 0;
    for (.@i = 0; .@i < @inventorylist_count; .@i++) {
        set .@type, getiteminfo(@inventorylist_id[.@i], ITEMINFO_TYPE);
        if ((.@type == IT_WEAPON || .@type == IT_ARMOR) && @inventorylist_equip[.@i] == 0) { // Type 4 = Weapon, Type 5 = Armor, and not equipped
            set .@salvageable_items[.@item_count], @inventorylist_id[.@i];
            set .@salvageable_items_qty[.@item_count], @inventorylist_amount[.@i];
            set .@item_count, .@item_count + 1;
        }
    }

    if (.@item_count == 0) {
        mes "[Salvage NPC]";
        mes "You don't have any salvageable items.";
        close;
    }

    mes "[Salvage NPC]";
    mes "Here is a summary of the items you can salvage:";
    for (.@i = 0; .@i < .@item_count; .@i++) {
        mes "^0000FF" + getitemname(.@salvageable_items[.@i]) + "^000000 x" + .@salvageable_items_qty[.@i];
    }
    next;

    if (select("Proceed with Salvage:Cancel") == 2) {
        mes "[Salvage NPC]";
        mes "Alright, come back if you change your mind.";
        close;
    }

    // Call the shop
    callshop "salvage_shop", 1;
    npcshopattach "salvage_shop";
    close;
}

// Shop definition with placeholder items (you need to fill this with the actual items you want to allow for salvage)
-	shop	salvage_shop	-1,501:10000,502:10000,503:10000,504:10000,505:10000,506:10000,507:10000,508:10000,509:10000,510:10000

OnSellItem:
    // Rewards based on weapon level
    setarray .@reward_common, 501, 502; // Example common loot item IDs
    setarray .@reward_rare, 503, 504;   // Example rare loot item IDs
    setarray .@reward_unique, 505, 506; // Example unique loot item IDs
    setarray .@reward_legendary, 507, 508; // Example legendary loot item IDs
    setarray .@reward_ancient, 509, 510; // Example ancient loot item IDs

    set .@item_id, @sold_nameid;
    set .@weapon_level, getiteminfo(.@item_id, ITEMINFO_WEAPONLV);

    switch (.@weapon_level) {
        case 1:
            // Reward common item/points
            getitem .@reward_common[rand(getarraysize(.@reward_common))], 1;
            break;
        case 2:
            // Reward rare item/points
            getitem .@reward_rare[rand(getarraysize(.@reward_rare))], 1;
            break;
        case 3:
            // Reward unique item/points
            getitem .@reward_unique[rand(getarraysize(.@reward_unique))], 1;
            break;
        case 4:
            // Reward legendary item/points
            getitem .@reward_legendary[rand(getarraysize(.@reward_legendary))], 1;
            break;
        case 5:
            // Reward ancient item/points
            getitem .@reward_ancient[rand(getarraysize(.@reward_ancient))], 1;
            break;
        default:
            dispbottom "This item cannot be salvaged.";
    }

    dispbottom "Thank you! Here is your reward.";
    end;
  • The OnSellItem label should be properly defined and hooked into the shop functionality.
  • The shop definition needs to include the items that are eligible for sale.
  • The getiteminfo function needs to fetch the correct item information.
  • Some logic and flow improvements.

     

    Key Changes and Fixes:

  • Item Info Fetching:

    • Changed getiteminfo(.@item_id, ITEMINFO_WEAPONLEVEL) to getiteminfo(.@item_id, ITEMINFO_WEAPONLV) to correctly fetch the weapon level.
  • Shop Definition:

    • The salvage_shop now includes example items with prices. Adjust these item IDs and prices according to your server's item database.
  • Shop Invocation:

    • Adjusted callshop "salvage_shop", 1; to make sure the shop is called correctly.
  • Reward Logic:

    • getiteminfo uses ITEMINFO_WEAPONLV to fetch the weapon level correctly.
  • Item Type Constants:

    • Added constants IT_WEAPON and IT_ARMOR for better readability.
  • Make sure the items you want to allow for salvage are included in the salvage_shop definition. Adjust the item IDs and the rewards as per your server's requirements. This should make the script functional and enable the salvage NPC to work correctly.

  • Upvote 1
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  37
  • Topics Per Day:  0.01
  • Content Count:  177
  • Reputation:   33
  • Joined:  06/22/13
  • Last Seen:  

On 6/1/2024 at 1:41 PM, Poring King said:

The script you provided is close to being functional, but there are some issues that need to be addressed to ensure it works correctly. Below is the corrected script with explanations for the changes:

Here is the corrected script

firstcity,229,87,4	script	Salvage NPC	856,{
    mes "[Salvage NPC]";
    mes "Hello! I can salvage your old weapons and armor.";
    mes "In return, I'll give you a special item based on the weapon's level.";
    next;
    mes "[Salvage NPC]";
    mes "Would you like to proceed?";
    switch(select("Yes:No")) {
        case 1:
            callfunc("salvage_item");
            break;
        case 2:
            mes "[Salvage NPC]";
            mes "Alright, come back if you change your mind.";
            close;
    }
    close;
}

function	script	salvage_item	{
    mes "[Salvage NPC]";
    mes "Please show me the items you want to salvage.";
    next;

    // Fetch player's inventory
    getinventorylist;

    // Collect all weapons and armor in the player's inventory
    set .@item_count, 0;
    for (.@i = 0; .@i < @inventorylist_count; .@i++) {
        set .@type, getiteminfo(@inventorylist_id[.@i], ITEMINFO_TYPE);
        if ((.@type == IT_WEAPON || .@type == IT_ARMOR) && @inventorylist_equip[.@i] == 0) { // Type 4 = Weapon, Type 5 = Armor, and not equipped
            set .@salvageable_items[.@item_count], @inventorylist_id[.@i];
            set .@salvageable_items_qty[.@item_count], @inventorylist_amount[.@i];
            set .@item_count, .@item_count + 1;
        }
    }

    if (.@item_count == 0) {
        mes "[Salvage NPC]";
        mes "You don't have any salvageable items.";
        close;
    }

    mes "[Salvage NPC]";
    mes "Here is a summary of the items you can salvage:";
    for (.@i = 0; .@i < .@item_count; .@i++) {
        mes "^0000FF" + getitemname(.@salvageable_items[.@i]) + "^000000 x" + .@salvageable_items_qty[.@i];
    }
    next;

    if (select("Proceed with Salvage:Cancel") == 2) {
        mes "[Salvage NPC]";
        mes "Alright, come back if you change your mind.";
        close;
    }

    // Call the shop
    callshop "salvage_shop", 1;
    npcshopattach "salvage_shop";
    close;
}

// Shop definition with placeholder items (you need to fill this with the actual items you want to allow for salvage)
-	shop	salvage_shop	-1,501:10000,502:10000,503:10000,504:10000,505:10000,506:10000,507:10000,508:10000,509:10000,510:10000

OnSellItem:
    // Rewards based on weapon level
    setarray .@reward_common, 501, 502; // Example common loot item IDs
    setarray .@reward_rare, 503, 504;   // Example rare loot item IDs
    setarray .@reward_unique, 505, 506; // Example unique loot item IDs
    setarray .@reward_legendary, 507, 508; // Example legendary loot item IDs
    setarray .@reward_ancient, 509, 510; // Example ancient loot item IDs

    set .@item_id, @sold_nameid;
    set .@weapon_level, getiteminfo(.@item_id, ITEMINFO_WEAPONLV);

    switch (.@weapon_level) {
        case 1:
            // Reward common item/points
            getitem .@reward_common[rand(getarraysize(.@reward_common))], 1;
            break;
        case 2:
            // Reward rare item/points
            getitem .@reward_rare[rand(getarraysize(.@reward_rare))], 1;
            break;
        case 3:
            // Reward unique item/points
            getitem .@reward_unique[rand(getarraysize(.@reward_unique))], 1;
            break;
        case 4:
            // Reward legendary item/points
            getitem .@reward_legendary[rand(getarraysize(.@reward_legendary))], 1;
            break;
        case 5:
            // Reward ancient item/points
            getitem .@reward_ancient[rand(getarraysize(.@reward_ancient))], 1;
            break;
        default:
            dispbottom "This item cannot be salvaged.";
    }

    dispbottom "Thank you! Here is your reward.";
    end;
  • The OnSellItem label should be properly defined and hooked into the shop functionality.
  • The shop definition needs to include the items that are eligible for sale.
  • The getiteminfo function needs to fetch the correct item information.
  • Some logic and flow improvements.

     

    Key Changes and Fixes:

  • Item Info Fetching:

    • Changed getiteminfo(.@item_id, ITEMINFO_WEAPONLEVEL) to getiteminfo(.@item_id, ITEMINFO_WEAPONLV) to correctly fetch the weapon level.
  • Shop Definition:

    • The salvage_shop now includes example items with prices. Adjust these item IDs and prices according to your server's item database.
  • Shop Invocation:

    • Adjusted callshop "salvage_shop", 1; to make sure the shop is called correctly.
  • Reward Logic:

    • getiteminfo uses ITEMINFO_WEAPONLV to fetch the weapon level correctly.
  • Item Type Constants:

    • Added constants IT_WEAPON and IT_ARMOR for better readability.
  • Make sure the items you want to allow for salvage are included in the salvage_shop definition. Adjust the item IDs and the rewards as per your server's requirements. This should make the script functional and enable the salvage NPC to work correctly.

Thank you sir!

Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  63
  • Topics Per Day:  0.02
  • Content Count:  1016
  • Reputation:   191
  • Joined:  11/27/14
  • Last Seen:  

Up the solve mark so other will know that there is a solution to this trend

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...