Lua Blue Arrow when drop item rare

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

  1. WibbenZ

    WibbenZ Support Team Support Team Premium User

    Joined:
    Oct 16, 2008
    Messages:
    4,908
    Likes Received:
    780
    Best Answers:
    67
    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:
    208
    Likes Received:
    122
    Best Answers:
    14
    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 at 4:07 PM
    Codex NG likes this.
  3. Carlitos Flow

    Carlitos Flow Developer (lvl*1)

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

    WibbenZ Support Team Support Team Premium User

    Joined:
    Oct 16, 2008
    Messages:
    4,908
    Likes Received:
    780
    Best Answers:
    67
    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:
    138
    Likes Received:
    9
    Best Answers:
    3
    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 Member

    Joined:
    May 7, 2017
    Messages:
    83
    Likes Received:
    26
    Best Answers:
    4
    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:
    138
    Likes Received:
    9
    Best Answers:
    3
    Yes, another monster need dies on the same tile for the script be executed. Bump
     
  8. WibbenZ

    WibbenZ Support Team Support Team Premium User

    Joined:
    Oct 16, 2008
    Messages:
    4,908
    Likes Received:
    780
    Best Answers:
    67
  9. Carlitos Flow

    Carlitos Flow Developer (lvl*1)

    Joined:
    Mar 2, 2011
    Messages:
    138
    Likes Received:
    9
    Best Answers:
    3
    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. Xeraphus

    Xeraphus Premium User Premium User

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

    Carlitos Flow Developer (lvl*1)

    Joined:
    Mar 2, 2011
    Messages:
    138
    Likes Received:
    9
    Best Answers:
    3
    Solved! Thank you guys! specially Xeraphus ^^
     
    Last edited: May 21, 2017

Share This Page

Loading...