CreatureEvent KillingInTheNameOF (task system) for OTHIRE 7.72

Discussion in 'GlobalEvents, Spells & CreatureEvents' started by Stellow, Mar 31, 2015.

  1. Stellow

    Stellow C++/C#/PHP/LUA

    Joined:
    Oct 23, 2008
    Messages:
    984
    Original script: https://otland.net/threads/grizzly-...me-of-quest-all-tasks-more-real-tibia.159150/
    Credits: Darkhous for the scripts

    I just addaped it to OTHIRE

    creaturescripts.xml
    Code:
      <event type="kill" name="KillTask" event="script" script="killtask.lua"/>
      <event type="login" name="TaskLogin" event="script" script="tasklogin.lua"/>
    killtask.lua
    Code:
    function onKill(cid, target, lastHit)
    local started = getPlayerStartedTasks(cid)
    
    if isPlayer(target) then return true end
    
    if started and #started > 0 then
    for _, id in ipairs(started) do
    if isInArray(tasks[id].creatures, getCreatureName(target):lower()) then
    if getPlayerStorageValue(cid, KILLSSTORAGE_BASE + id) < 0 then
    setPlayerStorageValue(cid, KILLSSTORAGE_BASE + id, 0)
    end
    if getPlayerStorageValue(cid, KILLSSTORAGE_BASE + id) < tasks[id].killsRequired then
    setPlayerStorageValue(cid, KILLSSTORAGE_BASE + id, getPlayerStorageValue(cid, KILLSSTORAGE_BASE + id) + 1)
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, getPlayerStorageValue(cid, KILLSSTORAGE_BASE + id) .. "/" .. tasks[id].killsRequired .. " " .. tasks[id].raceName .. " already killed.")
    end
    end
    end
    end
    return true
    end
    tasklogin.lua
    Code:
    function onLogin(cid)
    registerCreatureEvent(cid, "KillTask")
    return true
    end
    *obs: does not work directly on login.lua

    npc.xml
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    
    <npc name="Grizzly Adams" script="KillingInTheNameOf.lua" access="3" walkinterval="2000" lookdir="2">
      <mana now="800" max="800"/>
      <health now="200" max="200"/>
      <look type="144" head="97" body="97" legs="94" feet="97" addons="3"/>
    </npc>
    KillingInTheNameOf.lua
    Code:
    local keywordHandler = KeywordHandler:new()
    local npcHandler = NpcHandler:new(keywordHandler)
    NpcSystem.parseParameters(npcHandler)
    local talkState = {}
    
    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 choose = {}
    local cancel = {}
    local available = {}
    function creatureSayCallback(cid, type, msg)
    
    if npcHandler.focus ~= cid then
    return false
    end
    local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_PRIVATE and 0 or cid
    
    if isInArray({"tasks", "task", "mission"}, msg:lower()) then
    local can = getTasksByPlayer(cid)
    if #can > 0 then
    local text = ""
    local sep = ", "
    table.sort(can, (function(a, b) return (a < b) end))
    local t = 0
    for _, id in ipairs(can) do
    t = t + 1
    if t == #can - 1 then
    sep = " and "
    elseif t == #can then
    sep = "."
    end
    text = text .. "{" .. (tasks[id].name or tasks[id].raceName) .. "}" .. sep
    end
    selfSay("The current task" .. (#can > 1 and "s" or "") .. " that you can choose " .. (#can > 1 and "are" or "is") .. " " .. text)
    talkState[talkUser] = 0
    else
    selfSay("I don't have any task for you right now.")
    end
    elseif msg ~= "" and canStartTask(cid, msg) then
    if #getPlayerStartedTasks(cid) >= tasksByPlayer then
    selfSay("Sorry, but you already started " .. tasksByPlayer .. " tasks.")
    return true
    end
    local task = getTaskByName(msg)
    if task and getPlayerStorageValue(cid, QUESTSTORAGE_BASE + task) > 0 then
    return false
    end
    selfSay("In this task you must defeat " .. tasks[task].killsRequired .. " " .. tasks[task].raceName .. ". Are you sure that you want to start this task?")
    choose[cid] = task
    talkState[talkUser] = 1
    elseif msg:lower() == "yes" and talkState[talkUser] == 1 then
    setPlayerStorageValue(cid, QUESTSTORAGE_BASE + choose[cid], 1)
    selfSay("Excellent! You can check the status of your task saying report to me.")
    choose[cid] = nil
    talkState[talkUser] = 0
    elseif msg:lower() == "report" then
    local started = getPlayerStartedTasks(cid)
    local finishedAtLeastOne = false
    local finished = 0
    if started and #started > 0 then
    for _, id in ipairs(started) do
    if getPlayerStorageValue(cid, KILLSSTORAGE_BASE + id) >= tasks[id].killsRequired then
    for _, reward in ipairs(tasks[id].rewards) do
    print("Rewards loaded")
    local deny = false
    if reward.storage then
    if getPlayerStorageValue(cid, reward.storage[1]) >= reward.storage[2] then
    deny = true
    end
    end
    if isInArray({REWARD_MONEY, "money"}, reward.type:lower()) and not deny then
    doPlayerAddMoney(cid, reward.value[1])
    elseif isInArray({REWARD_EXP, "exp", "experience"}, reward.type:lower()) and not deny then
    doPlayerAddExp(cid, reward.value[1])
    doPlayerSendDefaultCancel(cid, "You gained " .. reward.value[1] .. " experience points.")
    elseif isInArray({REWARD_ACHIEVEMENT, "achievement", "ach"}, reward.type:lower()) and not deny then
    if doPlayerAddAchievement then
    doPlayerAddAchievement(cid, reward.value[1], true)
    end
    elseif isInArray({REWARD_STORAGE, "storage", "stor"}, reward.type:lower()) and not deny then
    setPlayerStorageValue(cid, reward.value[1], reward.value[2])
    elseif isInArray({REWARD_POINT, "points", "point"}, reward.type:lower()) and not deny then
    setPlayerStorageValue(cid, POINTSSTORAGE, getPlayerStorageValue(cid, POINTSSTORAGE) + reward.value[1])
    elseif isInArray({REWARD_ITEM, "item", "items", "object"}, reward.type:lower()) and not deny then
    doPlayerAddItem(cid, reward.value[1], reward.value[2])
    end
    
    if reward.storage then
    setPlayerStorageValue(cid, reward.storage[1], reward.storage[2])
    end
    end
    
    if tasks[id].norepeatable then
    setPlayerStorageValue(cid, QUESTSTORAGE_BASE + id, 2)
    else
    setPlayerStorageValue(cid, QUESTSTORAGE_BASE + id, 0)
    end
    setPlayerStorageValue(cid, KILLSSTORAGE_BASE + id, 0)
    if getPlayerStorageValue(cid, REPEATSTORAGE_BASE + id) < 1 then
    setPlayerStorageValue(cid, REPEATSTORAGE_BASE + id, 0)
    end
    setPlayerStorageValue(cid, REPEATSTORAGE_BASE + id, getPlayerStorageValue(cid, REPEATSTORAGE_BASE + id) + 1)
    finishedAtLeastOne = true
    finished = finished + 1
    end
    end
    
    if not finishedAtLeastOne then
    selfSay("You haven't finished any task yet.")
    else
    selfSay("Awesome! you finished " .. (finished > 1 and "various" or "a") .. " task" .. (finished > 1 and "s" or "") .. ". Talk to me again if you want to start a task.")
    end
    else
    selfSay("You haven't started any task yet.")
    end
    elseif msg:lower() == "started" then
    local started = getPlayerStartedTasks(cid)
    if started and #started > 0 then
    local text = ""
    local sep = ", "
    table.sort(started, (function(a, b) return (a < b) end))
    local t = 0
    for _, id in ipairs(started) do
    t = t + 1
    if t == #started - 1 then
    sep = " and "
    elseif t == #started then
    sep = "."
    end
    text = text .. "{" .. (tasks[id].name or tasks[id].raceName) .. "}" .. sep
    end
    
    selfSay("The current task" .. (#started > 1 and "s" or "") .. " that you started " .. (#started > 1 and "are" or "is") .. " " .. text)
    else
    selfSay("You haven't started any task yet.")
    end
    elseif msg:lower() == "cancel" then
    local started = getPlayerStartedTasks(cid)
    if started and #started > 0 then
    selfSay("Cancelling a task will make the count restart. Wich task you want to cancel?")
    talkState[talkUser] = 2
    else
    selfSay("You haven't started any task yet.")
    end
    elseif getTaskByName(msg) and talkState[talkUser] == 2 and isInArray(getPlayerStartedTasks(cid), getTaskByName(msg)) then
    local task = getTaskByName(msg)
    if getPlayerStorageValue(cid, KILLSSTORAGE_BASE + task) > 0 then
    selfSay("You currently killed " .. getPlayerStorageValue(cid, KILLSSTORAGE_BASE + task) .. "/" .. tasks[task].killsRequired .. " " .. tasks[task].raceName .. ". Cancelling this task will restart the count. Are you sure you want to cancel this task?")
    else
    selfSay("Are you sure you want to cancel this task?")
    end
    talkState[talkUser] = 3
    cancel[cid] = task
    elseif msg:lower() == "yes" and talkState[talkUser] == 3 then
    setPlayerStorageValue(cid, QUESTSTORAGE_BASE + cancel[cid], -1)
    setPlayerStorageValue(cid, KILLSSTORAGE_BASE + cancel[cid], -1)
    selfSay("You have cancelled the task " .. (tasks[cancel[cid]].name or tasks[cancel[cid]].raceName) .. ".")
    talkState[talkUser] = 0
    elseif isInArray({"points", "rank"}, msg:lower()) then
    selfSay("At this time, you have " .. getPlayerStorageValue(cid, POINTSSTORAGE) .. " Paw & Fur points. You " .. (getPlayerRank(cid) == 5 and "are an Elite Hunter" or getPlayerRank(cid) == 4 and "are a Trophy Hunter" or getPlayerRank(cid) == 3 and "are a Big Game Hunter" or getPlayerRank(cid) == 2 and "are a Ranger" or getPlayerRank(cid) == 1 and "are a Huntsman" or "haven't been ranked yet") .. ".")
    talkState[talkUser] = 0
    end
    end
    
    npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
    npcHandler:addModule(FocusModule:new())
     
    Volrath likes this.
  2. Stellow

    Stellow C++/C#/PHP/LUA

    Joined:
    Oct 23, 2008
    Messages:
    984
    I am trying to post the functions.lua (last partt) but it is to long to post, how can I do it, where is the attachement button.
     
  3. Tatuy1

    Tatuy1 Member

    Joined:
    Mar 24, 2014
    Messages:
    104
    Thank you very much! but, What is needed in functions.lua?
     
  4. Jompi

    Jompi A hoagie in disguise.

    Joined:
    Oct 22, 2007
    Messages:
    141
    I also would like to know that :eek:
     
  5. Stellow

    Stellow C++/C#/PHP/LUA

    Joined:
    Oct 23, 2008
    Messages:
    984
    Some variables are different. I am uploading it for you guys
     
  6. Stellow

    Stellow C++/C#/PHP/LUA

    Joined:
    Oct 23, 2008
    Messages:
    984
  7. Evil Puncker

    Evil Puncker I'd rather kiss a rattlesnake

    Joined:
    May 30, 2009
    Messages:
    5,372
    use pastebin.com xD
     
  8. Jompi

    Jompi A hoagie in disguise.

    Joined:
    Oct 22, 2007
    Messages:
    141
    Last edited: Apr 8, 2015
  9. Stellow

    Stellow C++/C#/PHP/LUA

    Joined:
    Oct 23, 2008
    Messages:
    984
    Good to know.
     
  10. Felipe93

    Felipe93 Ghost Member

    Joined:
    Mar 21, 2015
    Messages:
    609
    how adapt it to works in OTX 7.72?
     
  11. narko

    narko ;_; hello world ;_;

    Joined:
    Oct 19, 2008
    Messages:
    1,271
    OTX 7.72 is based on TFS 0.3.7 so the script system is the same, you can use a 0.4 script aswell It shall work.
     
    Felipe93 likes this.
  12. Felipe93

    Felipe93 Ghost Member

    Joined:
    Mar 21, 2015
    Messages:
    609
    gonna test thanks ffor yourr reply dude :)
     
  13. Darth Vader

    Darth Vader Never give up, never.. never

    Joined:
    Mar 9, 2016
    Messages:
    13
    Update changes * my opinion.

    Because in original have: if this target has a player, count & not have a summon protection.

    Example: if my other character calls "troll" and my another char kill him, count+1 in task.
    And if I summon a troll, and I kill him, count+1

    Code:
    function onKill(cid, target, lastHit)
    local started = getPlayerStartedTasks(cid)
    
    if isSummon(target) or isPlayer(target) then
        return false
    end
    
    if started and #started > 0 then
    for _, id in ipairs(started) do
    if isInArray(tasks[id].creatures, getCreatureName(target):lower()) then
    if getPlayerStorageValue(cid, KILLSSTORAGE_BASE + id) < 0 then
    setPlayerStorageValue(cid, KILLSSTORAGE_BASE + id, 0)
    end
    if getPlayerStorageValue(cid, KILLSSTORAGE_BASE + id) < tasks[id].killsRequired then
    setPlayerStorageValue(cid, KILLSSTORAGE_BASE + id, getPlayerStorageValue(cid, KILLSSTORAGE_BASE + id) + 1)
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, getPlayerStorageValue(cid, KILLSSTORAGE_BASE + id) .. "/" .. tasks[id].killsRequired .. " " .. tasks[id].raceName .. " already killed.")
    end
    end
    end
    end
    return true
    end
     
  14. JamesIsAwkward

    JamesIsAwkward New Member

    Joined:
    Aug 9, 2013
    Messages:
    12
  15. Betikoh

    Betikoh New Member

    Joined:
    Jul 23, 2008
    Messages:
    13
    reupload functions please
     
  16. bertil430

    bertil430 Member

    Joined:
    Nov 25, 2015
    Messages:
    85
    reupload functions plls
     
  17. bertil430

    bertil430 Member

    Joined:
    Nov 25, 2015
    Messages:
    85
  18. bertil430

    bertil430 Member

    Joined:
    Nov 25, 2015
    Messages:
    85
    reupload
     
  19. Stellow

    Stellow C++/C#/PHP/LUA

    Joined:
    Oct 23, 2008
    Messages:
    984
    Paste that on the bottom of functions.la

    Code:
    -- TASK
    
    
    RANK_NONE = 0
    RANK_HUNTSMAN = 1
    RANK_RANGER = 2
    RANK_BIGGAMEHUNTER = 3
    RANK_TROPHYHUNTER = 4
    RANK_ELITEHUNTER = 5
    
    REWARD_MONEY = 1
    REWARD_EXP = 2
    REWARD_ACHIEVEMENT = 3
    REWARD_STORAGE = 4
    REWARD_POINT = 5
    REWARD_ITEM = 6
    
    QUESTSTORAGE_BASE = 71500
    KILLSSTORAGE_BASE = 72500
    REPEATSTORAGE_BASE = 73500
    POINTSSTORAGE = 72500
    ONTASKSTORAGE = 74500
    tasks =
    {
    [1] = {killsRequired = 50, raceName = "Noob", level = {1, 666}, premium = false, creatures = {"rat", "cave rat", "spider", "poison spider", "bug"}, rewards = {
    {type = "exp", value = {8000}},
    {type = "money", value = {5000}},
    {type = "points", value = {1}},
    {type = "storage", value = {81500,1}}
    }},
    [2] = {killsRequired = 50, raceName = "Orcs", level = {1, 666}, premium = false, creatures = {"orc", "orc warrior", "orc spearman", "orc berserker", "orc leader"}, rewards = {
    {type = "exp", value = {10000}},
    {type = "money", value = {3500}},
    {type = "points", value = {1}}
    }},
    [3] = {bkillsRequired = 300, raceName = "Crocodiles", level = {1, 666}, premium = false, creatures = {"crocodile"}, rewards = {
    {type = "exp", value = {20000}},
    {type = "item", value = {2536,1}},
    {type = "points", value = {1}},
    {type = "storage", value = {81500,3}}
    }},
    [4] = {killsRequired = 300, raceName = "Tarantulas", level = {1, 666}, premium = false, creatures = {"tarantula"}, rewards = {
    {type = "exp", value = {30000}},
    {type = "points", value = {1}},
    {type = "storage", value = {81500,4}}
    }},
    [5] = {killsRequired = 150, raceName = "Carniphilas", level = {1, 666}, premium = false, creatures = {"carniphila"}, rewards = {
    {type = "exp", value = {30000}},
    {type = "item", value = {2477,1}},
    {type = "points", value = {1}},
    {type = "storage", value = {81500,5}}
    }}
    }
    
    tasksByPlayer = 3
    repeatTimes = 3
    
    function getPlayerRank(cid)
    return (getPlayerStorageValue(cid, POINTSSTORAGE) >= 100 and RANK_ELITEHUNTER or getPlayerStorageValue(cid, POINTSSTORAGE) >= 70 and RANK_TROPHYHUNTER or getPlayerStorageValue(cid, POINTSSTORAGE) >= 40 and RANK_BIGGAMEHUNTER or getPlayerStorageValue(cid, POINTSSTORAGE) >= 20 and RANK_RANGER or getPlayerStorageValue(cid, POINTSSTORAGE) >= 10 and RANK_HUNTSMAN or RANK_NONE)
    end
    
    function getTaskByName(name, table)
    local t = (table and table or tasks)
    for k, v in pairs(t) do
    if v.name then
    if v.name:lower() == name:lower() then
    return k
    end
    else
    if v.raceName:lower() == name:lower() then
    return k
    end
    end
    end
    return false
    end
    
    function getTasksByPlayer(cid)
    local canmake = {}
    local able = {}
    for k, v in pairs(tasks) do
    if getPlayerStorageValue(cid, QUESTSTORAGE_BASE + k) < 1 and getPlayerStorageValue(cid, REPEATSTORAGE_BASE + k) < repeatTimes then
    able[k] = true
    if getPlayerLevel(cid) < v.level[1] or getPlayerLevel(cid) > v.level[2] then
    able[k] = false
    end
    if v.storage and getPlayerStorageValue(cid, v.storage[1]) < v.storage[2] then
    able[k] = false
    end
    
    if v.rank then
    if getPlayerRank(cid) < v.rank then
    able[k] = false
    end
    end
    
    if v.premium then
    if not isPremium(cid) then
    able[k] = false
    end
    end
    
    if able[k] then
    table.insert(canmake, k)
    end
    end
    end
    return canmake
    end
    
    
    function canStartTask(cid, name, table)
    local v = ""
    local id = 0
    local t = (table and table or tasks)
    for k, i in pairs(t) do
    if i.name then
    if i.name:lower() == name:lower() then
    v = i
    id = k
    break
    end
    else
    if i.raceName:lower() == name:lower() then
    v = i
    id = k
    break
    end
    end
    end
    if v == "" then
    return false
    end
    if getPlayerStorageValue(cid, QUESTSTORAGE_BASE + id) > 0 then
    return false
    end
    if (getPlayerStorageValue(cid, REPEATSTORAGE_BASE + id) >= repeatTimes) or (v.norepeatable and getPlayerStorageValue(cid, REPEATSTORAGE_BASE + id) > 0) then
    return false
    end
    if getPlayerLevel(cid) >= v.level[1] and getPlayerLevel(cid) <= v.level[2] then
    if v.premium then
    if isPremium(cid) then
    if v.rank then
    if getPlayerRank(cid) >= v.rank then
    if v.storage then
    if getPlayerStorageValue(cid, v.storage[1]) >= v.storage[2] then
    return true
    end
    else
    return true
    end
    end
    else
    return true
    end
    end
    else
    return true
    end
    end
    return false
    end
    
    function getPlayerStartedTasks(cid)
    
    local tmp = {}
    for k, v in pairs(tasks) do
    if getPlayerStorageValue(cid, QUESTSTORAGE_BASE + k) > 0 and getPlayerStorageValue(cid, QUESTSTORAGE_BASE + k) < 2 then
    table.insert(tmp, k)
    end
    end
    return tmp
    end
    
    function isSummon(target)
    return (getCreatureMaster(target) ~= target)
    end
     
  20. Evilor

    Evilor New Member

    Joined:
    Oct 17, 2016
    Messages:
    25
    Hello, I use this datapack:
    https://github.com/peonso/tibialegacyserver
    I get this error when I write tasks:
    Code:
    Lua Script Error: [Npc interface]
    data/npc/scripts/KillingInTheNameOf.lua:onCreatureSay
    
    data/npc/scripts/KillingInTheNameOf.lua:22: attempt to call global 'getTasksByPlayer' (a nil value)
    stack traceback:
            data/npc/scripts/KillingInTheNameOf.lua:22: in function 'callback'
            data/npc/scripts/lib/npcsystem/npchandler.lua:301: in function 'onCreatureSay'
            data/npc/scripts/KillingInTheNameOf.lua:8: in function <data/npc/scripts/KillingInTheNameOf.lua:8>
    
    It works when I use normal othire data, can someone please help me?
    @Peonso meaby?
     

Share This Page