Lua Blue Arrow when drop item rare

Discussion in 'Support' started by Carlitos Flow, May 13, 2017.

  1. WibbenZ

    WibbenZ Support Team Support Team

    Joined:
    Oct 16, 2008
    Messages:
    5,825
    Likes Received:
    1,003
    Best Answers:
    193
    Give this a try;
    Code (Lua):
    1. function countdown(pos, timer, effect)
    2.     if timer < 0 then
    3.         return true
    4.     end
    5.     doSendMagicEffect(pos, effect)
    6.     addEvent(countdown, 1000, pos, timer - 1, effect)
    7. end
    8.  
    9. local rare_items = {5897}
    10. local effect = CONST_ME_TUTORIALARROW
    11. local timer = 60
    12.  
    13. function onKill(cid, target, corpse, lastHit)
    14.     local idc, pos = getMonsterInfo(getCreatureName(target)).lookCorpse, getCreaturePosition(target)
    15.     local corpse = getTileItemById(pos, idc).uid
    16.  
    17.     if isMonster(target) then
    18.         if isContainer(corpse) then
    19.             for i = getContainerSize(corpse)-1, 0, -1 do
    20.                 local containerItem = getContainerItem(corpse, i)
    21.                 if isContainer(containerItem) then
    22.                     for i = getContainerSize(containerItem)-1, 0, -1 do
    23.                         if isInArray(rare_items, getContainerItem(corpse, i).itemid) then
    24.                             doSendMagicEffect(pos, effect)
    25.                             addEvent(countdown, 1000, pos, timer, effect)
    26.                             return true
    27.                         end
    28.                     end
    29.                 elseif isInArray(rare_items, containerItem.itemid) then
    30.                     doSendMagicEffect(pos, effect)
    31.                     addEvent(countdown, 1000, pos, timer, effect)
    32.                     return true
    33.                 end
    34.             end
    35.         end
    36.     end
    37.     return true
    38. end
    39.  
     
    Codex NG likes this.
  2. Aled

    Aled Active Member (probably)

    Joined:
    Mar 30, 2017
    Messages:
    214
    Likes Received:
    126
    Best Answers:
    16
    if anyone is interested I wrote this for TFS 1.x but you can set what loot rate to notify players
    Code (Lua):
    1. local effect = CONST_ME_TUTORIALARROW -- magic effect
    2. local chance = 5 -- magic effect for item that drops less than 5% of the time
    3.  
    4. function onLogin(player)
    5.     player:registerEvent('rarelootkill')
    6.     return true
    7. end
    8.  
    9. function onKill(player, target)
    10.     if target:isMonster() then target:registerEvent('rarelootdeath') end
    11.     return true
    12. end
    13.  
    14. function getContainerItemIds(container)
    15.     t = {}
    16.     if not container then return t end
    17.     if not container:getSize() then return t end
    18.     for i = container:getSize()-1, 1, -1 do
    19.         table.insert(t,container:getItem(i):getId())
    20.     end
    21.     return t
    22. end
    23.  
    24. function hasItemById(container,itemid)
    25.     if not itemid or not container then return false end
    26.     if isInArray(getContainerItemIds(container),itemid) then return true end
    27.     return false
    28. end
    29.  
    30. function onDeath(creature, corpse, killer, mostDamageKiller, unjustified, mostDamageUnjustified)
    31.     if not creature then return true end
    32.     local loot = creature:getType():getLoot()
    33.     if not loot then return true end
    34.     local totalchance = 0
    35.     for i, k in pairs(loot) do
    36.         totalchance = totalchance + k.chance
    37.     end
    38.     addEvent(function()
    39.         for i, k in pairs(loot) do
    40.             if not corpse or not killer then
    41.                 return
    42.             end
    43.             if hasItemById(corpse,k.itemId) and k.chance/totalchance < chance/100  then
    44.                 corpse:getPosition():sendMagicEffect(effect)
    45.                 killer:sendTextMessage(MESSAGE_STATUS_DEFAULT, "You looted a "..ItemType(k.itemId):getName().."!")
    46.             end
    47.         end
    48.     end,1)
    49.     return true
    50. end
    Code (XML):
    1.     <event type="login" name="rarelootlogin" script="loot.lua" />
    2.     <event type="kill" name="rarelootkill" script="loot.lua" />
    3.     <event type="death" name="rarelootdeath" script="loot.lua" />
    not extensively tested, be careful
     
    Last edited: May 25, 2017
    Codex NG likes this.
  3. Carlitos Flow

    Carlitos Flow Developer (lvl*1)

    Joined:
    Mar 2, 2011
    Messages:
    156
    Likes Received:
    10
    Best Answers:
    4
    The same problem of the corpse, need other inside of who drops the item
     
  4. WibbenZ

    WibbenZ Support Team Support Team

    Joined:
    Oct 16, 2008
    Messages:
    5,825
    Likes Received:
    1,003
    Best Answers:
    193
    But what? Do you have bags inside of bags?
    This will check the corpse itself, if it has a container in the corpse it will check that aswell.
     
  5. Carlitos Flow

    Carlitos Flow Developer (lvl*1)

    Joined:
    Mar 2, 2011
    Messages:
    156
    Likes Received:
    10
    Best Answers:
    4
    If i kill a wolf than drops a wolf paw, don't show any effect, but i kill other wolf on the same position of the wolf than drops a Wolf paw, it will show the effect, its a bug. :/
     
  6. Ziker

    Ziker Active Member

    Joined:
    May 7, 2017
    Messages:
    183
    Likes Received:
    49
    Best Answers:
    6
    I think he meant that there's no corpse when onKill is executed. Therefore if another monster dies on the same tile, the old corpse is taken into account, but the latest corpse is not there yet in onKill handler. Any chance there's a bug there?? Likee, the corpse is there only if player is killed, but for monsters onKill is executed before loot is generated and thus no corpse exists at the time of onKill event?
     
    Carlitos Flow likes this.
  7. Carlitos Flow

    Carlitos Flow Developer (lvl*1)

    Joined:
    Mar 2, 2011
    Messages:
    156
    Likes Received:
    10
    Best Answers:
    4
    Yes, another monster need dies on the same tile for the script be executed. Bump
     
  8. WibbenZ

    WibbenZ Support Team Support Team

    Joined:
    Oct 16, 2008
    Messages:
    5,825
    Likes Received:
    1,003
    Best Answers:
    193
  9. Carlitos Flow

    Carlitos Flow Developer (lvl*1)

    Joined:
    Mar 2, 2011
    Messages:
    156
    Likes Received:
    10
    Best Answers:
    4
    Guys a little video of the bug. The first respawn drops somes wolf paws, i need kill other respawn on the same tile from who drops (x1 respawn) to got the effect. :/


    Script:
    Code (Lua):
    1.  
    2. function countdown(pos, timer, effect)
    3.     if timer < 0 then
    4.         return true
    5.     end
    6.     doSendMagicEffect(pos, effect)
    7.     addEvent(countdown, 6500, pos, timer - 1, effect)
    8. end
    9.  
    10. local rare_items = {5897}
    11. local effect = CONST_ME_TUTORIALARROW
    12. local timer = 5
    13.  
    14. function onKill(cid, target, corpse, lastHit)
    15.     local idc, pos = getMonsterInfo(getCreatureName(target)).lookCorpse, getCreaturePosition(target)
    16.     local corpse = getTileItemById(pos, idc).uid
    17.  
    18.     if isMonster(target) then
    19.         if isContainer(corpse) then
    20.             for i = getContainerSize(corpse)-1, 0, -1 do
    21.                 local containerItem = getContainerItem(corpse, i)
    22.                 if isContainer(containerItem) then
    23.                     for b = getContainerSize(containerItem)-1, 0, -1 do
    24.                         if isInArray(rare_items, getContainerItem(corpse, b).itemid) then
    25.                             doSendMagicEffect(pos, effect)
    26.                             addEvent(countdown, 6500, pos, timer, effect)
    27.                             return true
    28.                         end
    29.                     end
    30.                 elseif isInArray(rare_items, containerItem.itemid) then
    31.                     doSendMagicEffect(pos, effect)
    32.                     addEvent(countdown, 6500, pos, timer, effect)
    33.                     return true
    34.                 end
    35.             end
    36.         end
    37.     end
    38.     return true
    39. end
    40.  
     
  10. Static_

    Static_ Well-Known Member

    Joined:
    Feb 14, 2015
    Messages:
    3,140
    Likes Received:
    1,357
    Best Answers:
    188
    Best Answer
    Code (Lua):
    1.  
    2. local rare_items = {5897}
    3. local effect = CONST_ME_TUTORIALARROW
    4. local timer = 5
    5.  
    6. local function countdown(pos, timer)
    7.     if timer < 0 then
    8.         return true
    9.     end
    10.     doSendMagicEffect(pos, effect)
    11.     addEvent(countdown, 6500, pos, timer - 1)
    12. end
    13.  
    14. local function scanContainer(uid, pos)
    15.     for i = getContainerSize(uid) - 1, 0, -1 do
    16.         local item = getContainerItem(uid, i)
    17.         if isContainer(item.uid) then
    18.             scanContainer(item.uid, pos)
    19.         else
    20.             if isInArray(rare_items, item.itemid) then
    21.                 doSendMagicEffect(pos, effect)
    22.                 addEvent(countdown, 6500, pos, timer)
    23.             end
    24.         end
    25.     end
    26. end
    27.  
    28. local function scanCorpse(pos, corpseId)
    29.     local corpse = getTileItemById(pos, corpseId)
    30.     if not corpse then
    31.         return
    32.     end
    33.     if not isContainer(corpse.uid) then
    34.         return
    35.     end
    36.     scanContainer(corpse.uid, pos)
    37. end
    38.  
    39. function onKill(cid, target, corpse, lastHit)
    40.     if isPlayer(target) then
    41.         return true
    42.     end
    43.     local corpseId = getMonsterInfo(getCreatureName(target)).lookCorpse
    44.     local pos = getCreaturePosition(target)
    45.     addEvent(scanCorpse, 1, pos, corpseId)
    46.     return true
    47. end
    48.  
     
    Last edited: Jun 24, 2017
    Aled, WibbenZ and Carlitos Flow like this.
  11. Carlitos Flow

    Carlitos Flow Developer (lvl*1)

    Joined:
    Mar 2, 2011
    Messages:
    156
    Likes Received:
    10
    Best Answers:
    4
    Solved! Thank you guys! specially Xeraphus ^^
     
    Last edited: May 21, 2017
  12. Carlitos Flow

    Carlitos Flow Developer (lvl*1)

    Joined:
    Mar 2, 2011
    Messages:
    156
    Likes Received:
    10
    Best Answers:
    4
    I found a bug, when you kill another player on the console:
    /bluearrow.lua:eek:nKill
    Description:
    /bluearrow.lua:43: attempt to index a boolean value stack traceback:
    Monster not found.
     
  13. Itutorial

    Itutorial Well-Known Member

    Joined:
    Dec 23, 2014
    Messages:
    1,001
    Likes Received:
    163
    Best Answers:
    12
    Code (Lua):
    1.  
    2. local rare_items = {5897}
    3. local effect = CONST_ME_TUTORIALARROW
    4. local timer = 5
    5.  
    6. local function countdown(pos, timer)
    7.     if timer < 0 then
    8.         return true
    9.     end
    10.     doSendMagicEffect(pos, effect)
    11.     addEvent(countdown, 6500, pos, timer - 1)
    12. end
    13.  
    14. local function scanContainer(uid, pos)
    15.     for i = getContainerSize(uid) - 1, 0, -1 do
    16.         local item = getContainerItem(uid, i)
    17.         if isContainer(item.uid) then
    18.             scanContainer(item.uid, pos)
    19.         else
    20.             if isInArray(rare_items, item.itemid) then
    21.                 doSendMagicEffect(pos, effect)
    22.                 addEvent(countdown, 6500, pos, timer)
    23.             end
    24.         end
    25.     end
    26. end
    27.  
    28. local function scanCorpse(pos, corpseId)
    29.     local corpse = getTileItemById(pos, corpseId)
    30.     if not corpse then
    31.         return
    32.     end
    33.     if not isContainer(corpse.uid) then
    34.         return
    35.     end
    36.     scanContainer(corpse.uid, pos)
    37. end
    38.  
    39. function onKill(cid, target, corpse, lastHit)
    40.     if not isMonster(target) then
    41.         return
    42.     end
    43.     local corpseId = getMonsterInfo(getCreatureName(target)).lookCorpse
    44.     local pos = getCreaturePosition(target)
    45.     addEvent(scanCorpse, 1, pos, corpseId)
    46.     return true
    47. end
    48.  
     
  14. Carlitos Flow

    Carlitos Flow Developer (lvl*1)

    Joined:
    Mar 2, 2011
    Messages:
    156
    Likes Received:
    10
    Best Answers:
    4
    Same problem, the player doesn't dead only 0 of life.
    @Static_ Maybe you can fix it bro, you make the script, thank you in advance guys.
     
  15. Static_

    Static_ Well-Known Member

    Joined:
    Feb 14, 2015
    Messages:
    3,140
    Likes Received:
    1,357
    Best Answers:
    188
    1. dont tag me
    2. ill be nice anyways and help
    does this only happen on specific monsters? if so then you need to have the monster name from the actual monster xml the same as the one in monsters.xml
    for example name="Demon" in demon.xml has to be name="Demon" in monsters.xml
     
  16. Carlitos Flow

    Carlitos Flow Developer (lvl*1)

    Joined:
    Mar 2, 2011
    Messages:
    156
    Likes Received:
    10
    Best Answers:
    4
    Sorry. Well bro, its happens when one player attack other player. With monster it works.

    @Edit: I fix it, thanks anyway. On the function onKill
    Code (Lua):
    1.  
    2. function onKill(cid, target, corpse, lastHit)
    3.    if isMonster(target) then
    4.     local corpseId = getMonsterInfo(getCreatureName(target)).lookCorpse
    5.     local pos = getCreaturePosition(target)
    6.     addEvent(scanCorpse, 1, pos, corpseId)
    7.     else
    8.     return 0
    9. end
    10.     return true
    11.  end
     
    Last edited: Jun 24, 2017
  17. Static_

    Static_ Well-Known Member

    Joined:
    Feb 14, 2015
    Messages:
    3,140
    Likes Received:
    1,357
    Best Answers:
    188
    edited the code in the best answer post
     
    WibbenZ and Carlitos Flow like this.
  18. Andréew

    Andréew Well-Known Member

    Joined:
    Apr 14, 2015
    Messages:
    385
    Likes Received:
    429
    Best Answers:
    0
    Can you do a small tutorial on how to install this on your server? :)
     
  19. Static_

    Static_ Well-Known Member

    Joined:
    Feb 14, 2015
    Messages:
    3,140
    Likes Received:
    1,357
    Best Answers:
    188
    register it like any other onkill script you have in your server in creaturescripts
     
    Andréew likes this.

Share This Page

Loading...