• 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!

Buy items with demonic essences, with tables, NPC 0.3.7

  • Thread starter Thread starter Xikini
  • Start date Start date
X

Xikini

Guest
The Forgotten Server, version 0.3.7_SVN (Crying Damson)

I want an npc which will trade with demonic essence instead of gold coins.

Ideally the npc would have these functions.

trade -> which item would you like to trade? or would like to to know the items you can trade?
can trade -> loops and checks for all items in table, and sends "I trade demonic essences for thing1, thing2, and thing3.
thing1 -> check for item name in table
-> I can sell you that item for ..check item price in table.. . Would you like to do that?
yes ->check price from table -> check if player has enough essence -> give item from table.

The harder part..
If a person wants thing1, but thing1 comes in different colours, ask if want a specific one, or a random one for less price.

thing2 -> I can sell you thing2 for ..check price.. or I can give you a random thing2 for ..otherPrice.. . Would you like thing2 or random?
thing2 -> checkprice -> check player essence -> give item.
random -> checkprice -> check player essence -> give item+ add actionID from table.

Some id's to work with.
essence = 6500
brown backpack = 1988
dwarven ring = 2213
life ring = 2168
knife = 2403
present box = 1990 (random item box, I will script an action to give random item onUse.)

Guessing the table would need these in it..
Code:
[1] = {item = "knife",          type = weapon,   itemID = 2403, price = 100, randomPrice = 0, actionID = 0},
[2] = {item = "brown backpack", type = backpack, itemID = 1988, price = 100, randomPrice = 20, actionID = 45055},
[3] = {item = "dwarven ring",   type = ring,     itemID = 2213, price = 200, randomPrice = 40, actionID = 45056},
[4] = {item = "life ring",      type = ring,     itemID = 2168, price = 200, randomPrice = 40, actionID = 45056}

I'm not really looking for a script..
I'm more looking for how to access the table, and keep the position within the table saved while talking with the npc.
I'm just terrible with loops and tables. :P
 
Above the function add
Code:
local xmsg = {}
Under talkState[talkUser] = 1 add
Code:
xmsg[cid] = msg
Then under the msgcontains yes line
Code:
local x = config[xmsg[cid]:lower()]

Oh my. It now works till that part.
Why are tables so hard :oops:

error
Code:
[14:3:01.698] [Error - NpcScript Interface]
[14:3:01.699] data/npc/scripts/CarlsQuests/essencedude.lua:onCreatureSay
[14:3:01.699] Description:
[14:3:01.699] (luaDoPlayerAddItem) Item not found
npc
Code:
elseif msgcontains(msg, "knife") then
     selfSay("Would you like to buy "..x.name.." for "..x.price.." demonic essences?", cid)
     talkState[talkUser] = 1
     xmsg[cid] = msg
   elseif msgcontains(msg, "yes") and talkState[talkUser] == 1 then
     local x = config[xmsg[cid]:lower()]
     if player_money >= x.price then
       doPlayerRemoveItem(cid,6500,x.price)
       doPlayerAddItem(cid,x.itemdID,1)
       selfSay("Thank you kind sir. Here is your "..x.name..".", cid)
       talkState[talkUser] = 0
     else
       selfSay("You do not have enough demonic essence for this item.", cid)
       talkState[talkUser] = 0
     end



   elseif msgcontains(msg, "no") and talkState[talkUser] >= 1 then
     selfSay("No then.", cid)
     talkState[talkUser] = 0

   end
--edit
Had to change the remove money, to remove item :p
Edit I'm an idiot. I wrote itemdID instead of itemID
 
doPlayerAddItem(cid,x.itemdID,1)
Change to itemID :p
I'm assuming at this point I'd have to
Code:
elseif msgcontains(msg, "knife") then
     selfSay("Would you like to buy "..x.name.." for "..x.price.." demonic essences?", cid)
     talkState[talkUser] = 1
     xmsg[cid] = msg
   elseif msgcontains(msg, "brown backpack") then
     selfSay("Would you like to buy "..x.name.." for "..x.price.." demonic essences?", cid)
     talkState[talkUser] = 1
     xmsg[cid] = msg
   elseif msgcontains(msg, "life ring") then
     selfSay("Would you like to buy "..x.name.." for "..x.price.." demonic essences?", cid)
     talkState[talkUser] = 1
     xmsg[cid] = msg
Is there anyway to change it for..
Code:
elseif msgcontains(msg, ""..x.."") then
So if anything from the config is accessed it would all work the same?

Current npc, for kicks.
Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}
local xmsg = {}

function onCreatureAppear(cid)  npcHandler:onCreatureAppear(cid)  end
function onCreatureDisappear(cid)  npcHandler:onCreatureDisappear(cid)  end
function onCreatureSay(cid, type, msg)  npcHandler:onCreatureSay(cid, type, msg)  end
function onThink()  npcHandler:onThink()  end

   function greet(cid)
     talkState[cid] = 0
     return true
   end
   
   function getNpcName()
     return getCreatureName(getNpcId())
   end
   
function creatureSayCallback(cid, type, msg)
  if(not npcHandler:isFocused(cid)) then
  return false
  end
   
local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid

   local player_money  = getPlayerItemCount(cid,6500)
   
   local config = {
     ["brown backpack"] =    {itemID = "1988", price = 999, name = "brown backpack"},
     ["knife"] =        {itemID = "2403", price = 100, name = "knife"},
     ["dwarven ring"] =      {itemID = "2213", price = 200, name = "dwarven ring"},
     ["life ring"] =      {itemID = "2168", price = 200, name = "life ring"}
   }
   
   local x = config[msg:lower()]
   if msgcontains(msg, "help") then -- HELP START (NEVER EDIT)
  local text = ""
  for i, x in pairs(config) do
  if text ~= "" then
  text = text.. ", "
  end
  text = text..config[i].name
  end
  selfSay("I sell items for demonic essence. The items I have for sale are.. " ..text.. "", cid)
   
   elseif msgcontains(msg, "knife") then
     selfSay("Would you like to buy "..x.name.." for "..x.price.." demonic essences?", cid)
     talkState[talkUser] = 1
     xmsg[cid] = msg
   elseif msgcontains(msg, "brown backpack") then
     selfSay("Would you like to buy "..x.name.." for "..x.price.." demonic essences?", cid)
     talkState[talkUser] = 1
     xmsg[cid] = msg
   elseif msgcontains(msg, "life ring") then
     selfSay("Would you like to buy "..x.name.." for "..x.price.." demonic essences?", cid)
     talkState[talkUser] = 1
     xmsg[cid] = msg
   elseif msgcontains(msg, "yes") and talkState[talkUser] == 1 then
     local x = config[xmsg[cid]:lower()]
     if player_money >= x.price then
       doPlayerRemoveItem(cid,6500,x.price)
       doPlayerAddItem(cid,x.itemID,1)
       selfSay("Thank you kind sir. Here is your "..x.name..".", cid)
       talkState[talkUser] = 0
     else
       selfSay("You do not have enough demonic essence for this item.", cid)
       talkState[talkUser] = 0
     end
   
   
   
   elseif msgcontains(msg, "no") and talkState[talkUser] >= 1 then
     selfSay("No then.", cid)
     talkState[talkUser] = 0

   end

   return true   
end

npcHandler:setCallback(CALLBACK_GREET, greet)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
@Limos do you have any idea why when I say help it shows the list in item order instead of table order?
current = brown backpack, life ring, dwarven ring, knife.
want = brown backpack, knife, dwarven ring, life ring.
 
whats the difference?
btw u can read this: http://lua-users.org/wiki/TableLibraryTutorial
I've read the link close to 22 times. this part seems to make the most sense for sorting my table.
Code:
> t = { 3,2,5,1,4 }
> table.sort(t, function(a,b) return a<b end)
> = table.concat(t, ", ")
1, 2, 3, 4, 5
However after 12 hours of working on this, I'm still stumped on how to implement it.
The order when looping with pairs is undefined. Just because you added one item after adding another doesn't mean that's the order they'll be in with pairs.
So does this mean it's impossible to sort it from top to bottom?
I just don't understand how it even checks the itemID number.
To make sure it wasn't just checking the first integer it came across I put n=1,n=2 in front of all the itmeID's, but made no difference.
Then I tried using ipairs, and instead of name using numbers, but couldn't get it to work all I'd get was error's and never got the npc to load.

So altogether I'm simply more confused then before.

It currently sorts by item number
1988, 2168, 2213, 2403
instead of the desired
1988, 2403, 2213, 2168 (table order)

Code:
local config = {
     ["brown backpack"] =    {itemID = 1988, price = 999, name = "brown backpack"},
     ["knife"] =             {itemID = 2403, price = 100, name = "knife"},
     ["dwarven ring"] =      {itemID = 2213, price = 200, name = "dwarven ring"},
     ["life ring"] =         {itemID = 2168, price = 200, name = "life ring"}
   }
 
   local x = config[msg:lower()]
   if msgcontains(msg, "help") then -- HELP START (NEVER EDIT)
  local text = ""
  for i, x in pairs(config) do
  if text ~= "" then
  text = text.. ", "
  end
  text = text..config[i].name
  end
  selfSay("I sell items for demonic essence. The items I have for sale are.. " ..text.. "", cid)

If it not possible just say it's not possible. But I don't understand how it's even reading the itemID numbers :p
 
Back
Top