Karain
Jack of all trades
Hello again wonderful people of otland, i have a small gift for those who would like to see something more interactive when talking to NPCs. i personally liked the idea of manually talking to NPCs, but with my OT experience, sometimes they aren't that simple or it breaks the immersion as you struggle to find the right keyword for them to respond after a good couple of minutes of guessing. so why not make premade options without making the NPC do all the talking.
introducing! NPC Dialogue Tree! bringing modern gaming concepts into tibia!
with this script, talking with NPCs will be much more friendlier and it's still just as customizable as regular NPCs. BUT MUCH EASIER!
How to install:
data/creaturescripts/creaturescripts.xml:
data/creaturescripts/scripts/login.lua:
add this before "return true"
data/creaturescripts/scripts/dialogue.lua:
data/npc/scripts/dialogue.lua:
data/npc/lib/npcsystem/npchandler.lua:
change all
to
And then create your own npc.xml with this script attached to it
Please let me know if you have any feedback, comments or problems, i hope you have fun.
Thank you!
introducing! NPC Dialogue Tree! bringing modern gaming concepts into tibia!
with this script, talking with NPCs will be much more friendlier and it's still just as customizable as regular NPCs. BUT MUCH EASIER!
data/creaturescripts/creaturescripts.xml:
Code:
<event type="modalWindow" name="Dialogue" script="dialogue.lua"/>
data/creaturescripts/scripts/login.lua:
add this before "return true"
Code:
player:registerEvent("Dialogue")
data/creaturescripts/scripts/dialogue.lua:
Code:
player_choices = {}
defaultButtons = {{id = 0x00, text = "Select", enter = true, escape = false}, {id = 0x01, text = "End", enter = false, escape = true}}
function Player:getChoiceText(choice_id)
if player_choices and player_choices[self:getId()] then
return player_choices[self:getId()][choice_id].text
else
return false
end
end
function Player:createDialogueWindowWithButtons(modalWindowId, headerText, bodyText, buttonTable, choiceTable, sendToPlayer, priority)
local var = ModalWindow(modalWindowId, headerText, bodyText)
for i = 1, #buttonTable do
var:addButton(buttonTable[i].id, buttonTable[i].text)
if buttonTable[i].enter then
var:setDefaultEnterButton(buttonTable[i].id)
end
if buttonTable[i].escape then
var:setDefaultEscapeButton(buttonTable[i].id)
end
end
player_choices[self:getId()] = choiceTable
for i = 0, #choiceTable do
if choiceTable[i] ~= nil and (choiceTable[i].storage == false or self:getStorageValue(choiceTable[i].storage[1]) == choiceTable[i].storage[2]) then
var:addChoice(i, choiceTable[i].text)
end
end
if not priority then
var:setPriority(false)
end
if sendToPlayer then
var:sendToPlayer(self)
end
end
function onModalWindow(player, modalWindowId, buttonId, choiceId) -- be careful here if you have other modalwindow scripts
if buttonId == 0x00 then
player:say(player:getChoiceText(choiceId),TALKTYPE_SAY)
elseif buttonId == 0x01 then
player:say("Good Bye.",TALKTYPE_SAY)
end
return true
end
data/npc/scripts/dialogue.lua:
Code:
local npc_dialogue = {
[1] = {
message="This is the message that shows up before the choices, make sure it's long enough if you are having long choices.",
choices= {
[1]={text="Choice 1", storage=false, dialogue=1, script="end"},
[2]={text="Choice 2",storage=false, dialogue=2, script="script1"},
[3]={text="Choice 3",storage={1234,1}, dialogue=3, script="trade"},
[4]={text="Choice 4",storage=false, dialogue=3, script="quest"},
[5]={text="Choice 5",storage=false, dialogue=false, script="quest2"}}},
[2] = {
message="Bla bla bla bla bla bla bla.",
choices= {
[1]={text="Choice 1",storage=false, dialogue=1, script="end"},
[2]={text="Choice 2",storage=false, dialogue=2, script="end"},
[3]={text="Choice 3",storage={1234,1}, dialogue=false, script="end"},
[4]={text="Choice 4",storage=false, dialogue=false, script="end"},
[5]={text="Choice 5",storage=false, dialogue=3, script="end"}}},
[3] = {
message="Brought to you by Matt Shadowwing.",
choices= {
[1]={text="Choice 1",storage={1245,2}, dialogue=false, script="end"},
[2]={text="Choice 2",storage=false, dialogue=false, script="end"},
[3]={text="Choice 3",storage={1234,1}, dialogue=false, script="end"},
[4]={text="Choice 4",storage=false, dialogue=1, script="end"},
[5]={text="Choice 5",storage=false, dialogue=2, script="end"}}}
}
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 function greetCallback(cid)
local player = Player(cid)
local npc = Npc(getNpcCid())
npcHandler:setMessage(MESSAGE_GREET, npc_dialogue[1].message)
player:createDialogueWindowWithButtons(1, npc:getName(), npc_dialogue[1].message, defaultButtons, npc_dialogue[1].choices, true, false)
talkstate[player:getId()] = 1
return true
end
local function creatureSayCallback(cid, type, msg)
local player = Player(cid)
local npc = Npc(getNpcCid())
if not npcHandler:isFocused(cid) then
return false
elseif talkstate[player:getId()] then
for _, v in pairs(npc_dialogue[talkstate[player:getId()]].choices) do
if msgcontains(msg, v.text) and (v.storage == false or player:getStorageValue(v.storage[1]) == v.storage[2]) then
if v.script == "end" then
talkstate[player:getId()] = v.dialogue
npcHandler:say(npc_dialogue[v.dialogue].message, cid)
player:createDialogueWindowWithButtons(1, npc:getName(), npc_dialogue[v.dialogue].message, defaultButtons, npc_dialogue[v.dialogue].choices, true, false)
elseif v.script == "trade" then
-- trading script here
talkstate[player:getId()] = v.dialogue
npcHandler:say(npc_dialogue[v.dialogue].message, cid)
player:createDialogueWindowWithButtons(1, npc:getName(), npc_dialogue[v.dialogue].message, defaultButtons, npc_dialogue[v.dialogue].choices, true, false)
elseif v.script == "quest" then
-- quest script here
talkstate[player:getId()] = v.dialogue
npcHandler:say(npc_dialogue[v.dialogue].message, cid)
player:createDialogueWindowWithButtons(1, npc:getName(), npc_dialogue[v.dialogue].message, defaultButtons, npc_dialogue[v.dialogue].choices, true, false)
end
end
end
end
return true
end
npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
data/npc/lib/npcsystem/npchandler.lua:
change all
Code:
TALKTYPE_PRIVATE_PN
Code:
TALKTYPE_SAY
And then create your own npc.xml with this script attached to it
Please let me know if you have any feedback, comments or problems, i hope you have fun.
Thank you!
Last edited: