local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
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
local talkState = {}
local chosenOffer = {}
function creatureSayCallback(cid, type, msg)
if(not npcHandler:isFocused(cid)) then
return false
end
local offers = {
["obsidian knife"] = {
askConfirmationMsg = "You bringing me draconian steel and obsidian lance in exchange for obsidian knife?",
requirements = {
{5889,1}, {2425,1}
},
reward = {
{5887,1}
}
},
["piece of royal steel"] = {
askConfirmationMsg = "I can forge a piece of royal steel if you bring me a crown armor. Want to trade?",
requirements = {
{2487,1}
},
reward = {
{5887,1}
}
},
["piece of hell steel"] = {
askConfirmationMsg = "I can forge a piece of hell steel if you bring me a devil helmet. Want to trade?",
requirements = {
{2462,1}
},
reward = {
{5888,1}
}
},
["piece of draconian steel"] = {
askConfirmationMsg = "I can forge a piece of draconian steel if you bring me a dragon shield. Want to trade?",
requirements = {
{2516,1}
},
reward = {
{5889,1}
}
},
["huge chunk of crude iron"] = {
askConfirmationMsg = "I can forge a huge chunk of crude iron if you bring me a giant sword. Want to trade?",
requirements = {
{2393,1}
},
reward = {
{5892,1}
}
},
["infernal bolt"] = {
askConfirmationMsg = "I can forge 3 infernal bolts if you bring me a soul orb. Want to trade?",
requirements = {
{5944,1}
},
reward = {
{6529,3}
}
}
}
if msgcontains(msg, 'offer') then
-- Create the offer list message of the NPC based on entries in `offers` table.
local response = ""
local count = 0
for itemname, _ in pairs(offers) do
count = count + 1
response = response .. "{" .. itemname .. "}, "
end
if count > 0 then
response = "Here you can forge the following items: " .. string.sub(response, 1, -3) .. "."
talkState[cid] = "offering_to_craft"
else
response = "I have nothing to offer."
end
selfSay(response, cid)
return true
end
local lowerMsg = string.lower(msg) -- We will store a lowercase version of the player's message here and use that for comparison. So if they write back "Crown Armor" with capital letters, it will still work.
if talkState[cid] == "offering_to_craft" then
if offers[lowerMsg] then
npcHandler:say(offers[lowerMsg].askConfirmationMsg, cid)
talkState[cid] = "asking_to_confirm_to_craft"
chosenOffer[cid] = lowerMsg
else
npcHandler:say("I don't {offer} that.", cid)
talkState[cid] = nil
end
return true
end
if talkState[cid] == "asking_to_confirm_to_craft" then
if lowerMsg == "yes" then
local offer = offers[chosenOffer[cid]]
local hasRequiredMaterials = true
local missingMaterials = ""
for _,requirement in pairs(offer.requirements) do
if getPlayerItemCount(cid, requirement[1]) < requirement[2] then
if hasRequiredMaterials then
hasRequiredMaterials = false
end
missingMaterials = missingMaterials .. math.abs(getPlayerItemCount(cid, requirement[1]) - requirement[2]) .. "x " .. getItemNameById(requirement[1]) .. ", "
end
end
if hasRequiredMaterials then
talkState[cid] = string.lower(msg)
npcHandler:say("Cling clang!", cid)
for _,requirement in pairs(offer.requirements) do
doPlayerRemoveItem(cid, requirement[1], requirement[2])
end
for _,reward in pairs(offer.reward) do
doPlayerAddItem(cid, reward[1], reward[2])
end
talkState[cid] = nil
else
missingMaterials = "You no have " .. string.sub(missingMaterials, 1, -3) .. "."
npcHandler:say(missingMaterials, cid)
end
else
npcHandler:say("Pity. Some other time.", cid)
talkState[cid] = nil
end
chosenOffer[cid] = nil
return true
end
return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())