[Lua] Autoloot system for OTHIRE

Discussion in 'Mods & Lua Functions' started by LaloHao, Oct 31, 2016.

  1. LaloHao

    LaloHao Member

    Joined:
    Sep 14, 2008
    Messages:
    73
    Likes Received:
    12
    Best Answers:
    0
    I just finished this, so its not fully tested, but everything seems to be working so far.

    When you open the corpse it grabs what's in your autoloot list.
    [​IMG]

    Grabbed the code from here https://otland.net/threads/autoloot.91385/ and converted stuff to tables

    Example of command usage
    !autoloot list
    !autoloot clean
    !autoloot add:gold coin
    !autoloot remove:gold coin

    !autoloot with no argument toggles the display of messages when autolooting stuff.

    PHP:
    1.  
    2. -- Load Autoloot System
    3. dofile(getDataDir() .. 'lib/autoloot.lua')
    4.  

    PHP:
    1.  
    2. STORAGE_AUTOLOOTMESSAGE = 31009
    3. STORAGE_AUTOLOOT = {7575, 7576, 7577, 7578, 7579}
    4. STORAGE_AUTOLOOTLIMIT = 5
    5.  
    6. function getContents(uid)
    7.    local loot, i = {}, 0
    8.  
    9.    while i < getContainerSize(uid) do
    10.       local v = getContainerItem(uid, i)
    11.       table.insert(loot, v)
    12.       i = i + 1
    13.    end
    14.  
    15.    return loot
    16.  
    17. function getAutolootList(cid)
    18.    local list, v, count = {}, 0, 0
    19.    for _, k in ipairs(STORAGE_AUTOLOOT) do
    20.       v = getPlayerStorageValue(cid, k)
    21.       if v > 0 then
    22.          count = count + 1
    23.       end
    24.       table.insert(list, v)
    25.    end
    26.    return list, count
    27.  
    28. function AutolootLootItem(cid, item, msg)
    29.    local Item = item
    30.    local count = Item.type
    31.    if count < 1 then
    32.       count = 1
    33.    end
    34.    doRemoveItem(item.uid)
    35.    doPlayerAddItem(cid, Item.itemid, count)
    36.    Item = getItemNameById(Item.itemid)
    37.    if msg then
    38.       doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, msg..count.." "..Item)
    39.    end
    40.  
    41. function isAutolootShowEnabled(cid)
    42.    return getPlayerStorageValue(cid,STORAGE_AUTOLOOTMESSAGE) and 1
    43.  
    44. function AutolootToggleShow(cid)
    45.    if getPlayerStorageValue(cid, STORAGE_AUTOLOOTMESSAGE) == -1 then
    46.       setPlayerStorageValue(cid, STORAGE_AUTOLOOTMESSAGE, 1)
    47.       doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Showing autoloot.")
    48.    else
    49.       setPlayerStorageValue(cid, STORAGE_AUTOLOOTMESSAGE, -1)
    50.       doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Hiding autoloot.")
    51.    end
    52.  
    53. function AutolootPrint(cid, msg)
    54.    doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, msg)
    55.  
    56. function AutolootPrintItems(cid, items, bullet)
    57.    for _, v in ipairs(items) do
    58.       if v > 0 then
    59.          AutolootPrint(cid, bullet..getItemNameById(v))
    60.       end
    61.    end
    62.  
    63. function AutolootAddItem(cid, item, name)
    64.    for _,v in ipairs(STORAGE_AUTOLOOT) do
    65.       if getPlayerStorageValue(cid, v) == -1 then
    66.          setPlayerStorageValue(cid, v, item)
    67.          AutolootPrint(cid, ">>"..name.."<< added to autoloot. Slot:".._)
    68.          break
    69.       end
    70.    end
    71.  
    72. function AutolootRemoveItem(cid, item, name)
    73.    for _,v in ipairs(STORAGE_AUTOLOOT) do
    74.       if getPlayerStorageValue(cid, v) == item then
    75.          setPlayerStorageValue(cid, v, -1)
    76.          AutolootPrint(cid, ">>"..name.."<< removed from autoloot.")
    77.          break
    78.       end
    79.    end
    80.  
    81. function AutolootClean(cid)
    82.    for _, k in ipairs(STORAGE_AUTOLOOT) do
    83.       setPlayerStorageValue(cid, k, -1)
    84.    end
    85.  
    86. function AutolootGetItemFromParam(rest)
    87.    local item = tonumber(rest)
    88.    local name = getItemIdByName(rest)
    89.    local err = false
    90.    if item then
    91.       -- AutolootPrint(cid, "Data triggered by Item: ".. item)
    92.    elseif name then
    93.       item = name
    94.       -- AutolootPrint(cid, "Data triggered by Name: ".. name)
    95.    else
    96.       err = "Not valid item."
    97.    end
    98.  
    99.    if isItemContainer(item) then
    100.       err = "This item can not be autolooted."
    101.    end
    102.  
    103.    if not isItemMoveable(item) then
    104.       err = "This item can not be autolooted."
    105.    end
    106.    return item, err
    107.  

    PHP:
    1.  
    2. <talkaction words="!autoloot" filter="first word" access="0" script="autoloot.lua"/>
    3.  

    PHP:
    1.  
    2. local limit = 5
    3.  
    4. function onSay(cid, words, param)
    5.    if param == "" then
    6.       AutolootToggleShow(cid)
    7.       return false
    8.    end
    9.  
    10.    local help = "Help"
    11.    local expl = string.explode(param, ':')
    12.    local action, rest = expl[1], expl[2]
    13.    -- doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Param: ".. param)
    14.    -- doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "1: "..expl[1].." 2: ".. expl[2])
    15.  
    16.    local lootList, Items = getAutolootList(cid)
    17.  
    18.    local bulletItem = "----->"
    19.    if (action:lower() == "list") then
    20.       AutolootPrint(cid, "Autoloot list: ("..Items..") Items.")
    21.       AutolootPrintItems(cid, lootList, bulletItem)
    22.       return false
    23.    elseif (action:lower() == "clean") then
    24.       AutolootClean(cid)
    25.       AutolootPrint(cid, "Autoloot list cleaned.")
    26.       return false
    27.    else
    28.       if not rest then
    29.          AutolootPrint(cid, help)
    30.          return false
    31.       end
    32.  
    33.       local item, err = AutolootGetItemFromParam(rest)
    34.       if err then
    35.          AutolootPrint(cid, err)
    36.          return false
    37.       end
    38.  
    39.       local name = getItemNameById(item)
    40.       if (action:lower() == "add") then
    41.          if (Items >= limit) then
    42.             AutolootPrint(cid, "You already have " .. limit .. " autolooting items.")
    43.             return false
    44.          end
    45.  
    46.          if isInArray(lootList, item) then
    47.             AutolootPrint(cid, "This item is already in your list.")
    48.             return false
    49.          end
    50.  
    51.          AutolootAddItem(cid, item, name)
    52.       elseif (action:lower() == "remove") then
    53.          if (Items < 1) then
    54.             AutolootPrint(cid, "You dont have any item added.")
    55.             return false
    56.          end
    57.  
    58.          if not isInArray(lootList, item) then
    59.             AutolootPrint(cid, ">>"..name.."<< is not in the list.")
    60.             return false
    61.          end
    62.  
    63.          AutolootRemoveItem(cid, item, name)
    64.       end
    65.    end
    66.    return false

    PHP:
    1.  
    2. function onUse(cid, item, fromPosition, itemEx, toPosition)
    3.    local corpse = item.uid
    4.    if not isContainer(corpse) then
    5.       return false
    6.    end
    7.    local lootMessage = "Autolooted: "
    8.  
    9.    local lootList, Items = getAutolootList(cid)
    10.  
    11.    if Items == 0 then
    12.       return false
    13.    end
    14.  
    15.    local loot = getContents(corpse)
    16.    local party = getPartyMembers(cid)
    17.  
    18.    if party then
    19.       if lastHit then
    20.          for _, pid in ipairs(getPartyMembers(cid)) do
    21.             if isAutolootShowEnabled(pid) then
    22.                -- Print(loot)
    23.             end
    24.          end
    25.       end
    26.    else
    27.       local item, count
    28.       for v in ipairs(loot) do
    29.          item = loot[v].itemid
    30.          if isInArray(lootList, item) then
    31.             if isAutolootShowEnabled(cid) then
    32.                AutolootLootItem(cid, loot[v], lootMessage)
    33.             else
    34.                AutolootLootItem(cid, loot[v])
    35.             end
    36.          end
    37.       end
    38.    end
    39.    return false

    I extracted the corpses id's with a script from data/monster/*.xml so it only works on fresh corpses, you are free to add bags/backpacks/chests or the rest of the not so fresh corpses.
    PHP:
    1.     <!-- Autoloot corpses -->
    2.     <action itemid="3052" script="autoloot.lua" />
    3.     <action itemid="2979" script="autoloot.lua" />
    4.     <action itemid="2916" script="autoloot.lua" />
    5.     <action itemid="4280" script="autoloot.lua" />
    6.     <action itemid="2857" script="autoloot.lua" />
    7.     <action itemid="3016" script="autoloot.lua" />
    8.     <action itemid="3016" script="autoloot.lua" />
    9.     <action itemid="3128" script="autoloot.lua" />
    10.     <action itemid="3016" script="autoloot.lua" />
    11.     <action itemid="3016" script="autoloot.lua" />
    12.     <action itemid="3128" script="autoloot.lua" />
    13.     <action itemid="2876" script="autoloot.lua" />
    14.     <action itemid="3034" script="autoloot.lua" />
    15.     <action itemid="3119" script="autoloot.lua" />
    16.     <action itemid="3016" script="autoloot.lua" />
    17.     <action itemid="2952" script="autoloot.lua" />
    18.     <action itemid="2881" script="autoloot.lua" />
    19.     <action itemid="3034" script="autoloot.lua" />
    20.     <action itemid="2876" script="autoloot.lua" />
    21.     <action itemid="3016" script="autoloot.lua" />
    22.     <action itemid="2940" script="autoloot.lua" />
    23.     <action itemid="2376" script="autoloot.lua" />
    24.     <action itemid="2967" script="autoloot.lua" />
    25.     <action itemid="2820" script="autoloot.lua" />
    26.     <action itemid="1740" script="autoloot.lua" />
    27.     <action itemid="2931" script="autoloot.lua" />
    28.     <action itemid="3043" script="autoloot.lua" />
    29.     <action itemid="2979" script="autoloot.lua" />
    30.     <action itemid="3052" script="autoloot.lua" />
    31.     <action itemid="2979" script="autoloot.lua" />
    32.     <action itemid="2916" script="autoloot.lua" />
    33.     <action itemid="4280" script="autoloot.lua" />
    34.     <action itemid="2857" script="autoloot.lua" />
    35.     <action itemid="3016" script="autoloot.lua" />
    36.     <action itemid="3016" script="autoloot.lua" />
    37.     <action itemid="3128" script="autoloot.lua" />
    38.     <action itemid="3016" script="autoloot.lua" />
    39.     <action itemid="3016" script="autoloot.lua" />
    40.     <action itemid="3128" script="autoloot.lua" />
    41.     <action itemid="2876" script="autoloot.lua" />
    42.     <action itemid="3034" script="autoloot.lua" />
    43.     <action itemid="3119" script="autoloot.lua" />
    44.     <action itemid="3016" script="autoloot.lua" />
    45.     <action itemid="2952" script="autoloot.lua" />
    46.     <action itemid="2881" script="autoloot.lua" />
    47.     <action itemid="3034" script="autoloot.lua" />
    48.     <action itemid="2876" script="autoloot.lua" />
    49.     <action itemid="3016" script="autoloot.lua" />
    50.     <action itemid="4307" script="autoloot.lua" />
    51.     <action itemid="3049" script="autoloot.lua" />
    52.     <action itemid="4259" script="autoloot.lua" />
    53.     <action itemid="2817" script="autoloot.lua" />
    54.     <action itemid="2960" script="autoloot.lua" />
    55.     <action itemid="1496" script="autoloot.lua" />
    56.     <action itemid="3128" script="autoloot.lua" />
    57.     <action itemid="2876" script="autoloot.lua" />
    58.     <action itemid="4262" script="autoloot.lua" />
    59.     <action itemid="2843" script="autoloot.lua" />
    60.     <action itemid="4993" script="autoloot.lua" />
    61.     <action itemid="4326" script="autoloot.lua" />
    62.     <action itemid="4298" script="autoloot.lua" />
    63.     <action itemid="2935" script="autoloot.lua" />
    64.     <action itemid="3128" script="autoloot.lua" />
    65.     <action itemid="2987" script="autoloot.lua" />
    66.     <action itemid="4277" script="autoloot.lua" />
    67.     <action itemid="2920" script="autoloot.lua" />
    68.     <action itemid="4301" script="autoloot.lua" />
    69.     <action itemid="2940" script="autoloot.lua" />
    70.     <action itemid="2376" script="autoloot.lua" />
    71.     <action itemid="2967" script="autoloot.lua" />
    72.     <action itemid="2820" script="autoloot.lua" />
    73.     <action itemid="1740" script="autoloot.lua" />
    74.     <action itemid="3128" script="autoloot.lua" />
    75.     <action itemid="2905" script="autoloot.lua" />
    76.     <action itemid="4320" script="autoloot.lua" />
    77.     <action itemid="3128" script="autoloot.lua" />
    78.     <action itemid="3104" script="autoloot.lua" />
    79.     <action itemid="4994" script="autoloot.lua" />
    80.     <action itemid="3040" script="autoloot.lua" />
    81.     <action itemid="3065" script="autoloot.lua" />
    82.     <action itemid="3007" script="autoloot.lua" />
    83.     <action itemid="3065" script="autoloot.lua" />
    84.     <action itemid="1492" script="autoloot.lua" />
    85.     <action itemid="4286" script="autoloot.lua" />
    86.     <action itemid="3069" script="autoloot.lua" />
    87.     <action itemid="3055" script="autoloot.lua" />
    88.     <action itemid="2809" script="autoloot.lua" />
    89.     <action itemid="2983" script="autoloot.lua" />
    90.     <action itemid="3128" script="autoloot.lua" />
    91.     <action itemid="3019" script="autoloot.lua" />
    92.     <action itemid="3004" script="autoloot.lua" />
    93.     <action itemid="2969" script="autoloot.lua" />
    94.     <action itemid="4304" script="autoloot.lua" />
    95.     <action itemid="2924" script="autoloot.lua" />
    96.     <action itemid="4268" script="autoloot.lua" />
    97.     <action itemid="2989" script="autoloot.lua" />
    98.     <action itemid="2949" script="autoloot.lua" />
    99.     <action itemid="4274" script="autoloot.lua" />
    100.     <action itemid="3128" script="autoloot.lua" />
    101.     <action itemid="4265" script="autoloot.lua" />
    102.     <action itemid="2813" script="autoloot.lua" />
    103.     <action itemid="4292" script="autoloot.lua" />
    104.     <action itemid="2938" script="autoloot.lua" />
    105.     <action itemid="2928" script="autoloot.lua" />
    106.     <action itemid="2916" script="autoloot.lua" />
    107.     <action itemid="2995" script="autoloot.lua" />
    108.     <action itemid="5014" script="autoloot.lua" />
    109.     <action itemid="4994" script="autoloot.lua" />
    110.     <action itemid="4271" script="autoloot.lua" />
    111.     <action itemid="2913" script="autoloot.lua" />
    112.     <action itemid="3010" script="autoloot.lua" />
    113.     <action itemid="2886" script="autoloot.lua" />
    114.     <action itemid="3031" script="autoloot.lua" />
    115.     <action itemid="2940" script="autoloot.lua" />
    116.     <action itemid="3128" script="autoloot.lua" />
    117.     <action itemid="2902" script="autoloot.lua" />
    118.     <action itemid="2897" script="autoloot.lua" />
    119.     <action itemid="2914" script="autoloot.lua" />
    120.     <action itemid="2972" script="autoloot.lua" />
    121.     <action itemid="3128" script="autoloot.lua" />
    122.     <action itemid="4992" script="autoloot.lua" />
    123.     <action itemid="3052" script="autoloot.lua" />
    124.     <action itemid="2820" script="autoloot.lua" />
    125.     <action itemid="3068" script="autoloot.lua" />
    126.     <action itemid="3119" script="autoloot.lua" />
    127.     <action itemid="2849" script="autoloot.lua" />
    128.     <action itemid="2998" script="autoloot.lua" />
    129.     <action itemid="4280" script="autoloot.lua" />
    130.     <action itemid="2848" script="autoloot.lua" />
    131.     <action itemid="4253" script="autoloot.lua" />
    132.     <action itemid="2860" script="autoloot.lua" />
    133.     <action itemid="2826" script="autoloot.lua" />
    134.     <action itemid="1496" script="autoloot.lua" />
    135.     <action itemid="2881" script="autoloot.lua" />
    136.     <action itemid="3128" script="autoloot.lua" />
    137.     <action itemid="2813" script="autoloot.lua" />
    138.     <action itemid="2871" script="autoloot.lua" />
    139.     <action itemid="2985" script="autoloot.lua" />
    140.     <action itemid="2967" script="autoloot.lua" />
    141.     <action itemid="2862" script="autoloot.lua" />
    142.     <action itemid="2945" script="autoloot.lua" />
    143.     <action itemid="3001" script="autoloot.lua" />
    144.     <action itemid="3037" script="autoloot.lua" />
    145.     <action itemid="2952" script="autoloot.lua" />
    146.     <action itemid="2839" script="autoloot.lua" />
    147.     <action itemid="3065" script="autoloot.lua" />
    148.     <action itemid="3022" script="autoloot.lua" />
    149.     <action itemid="3028" script="autoloot.lua" />
    150.     <action itemid="2864" script="autoloot.lua" />
    151.     <action itemid="2824" script="autoloot.lua" />
    152.     <action itemid="2830" script="autoloot.lua" />
    153.     <action itemid="2899" script="autoloot.lua" />
    154.     <action itemid="2893" script="autoloot.lua" />
    155.     <action itemid="4323" script="autoloot.lua" />
    156.     <action itemid="3128" script="autoloot.lua" />
    157.     <action itemid="3046" script="autoloot.lua" />
    158.     <action itemid="3025" script="autoloot.lua" />
    159.     <action itemid="3013" script="autoloot.lua" />
    160.     <action itemid="2835" script="autoloot.lua" />
    161.     <action itemid="4289" script="autoloot.lua" />
    162.     <action itemid="2889" script="autoloot.lua" />
    163.     <action itemid="2806" script="autoloot.lua" />
    164.     <action itemid="2981" script="autoloot.lua" />
    165.     <action itemid="3113" script="autoloot.lua" />
    166.     <action itemid="4283" script="autoloot.lua" />
    167.     <action itemid="3065" script="autoloot.lua" />
    168.     <action itemid="3128" script="autoloot.lua" />
    169.     <action itemid="2956" script="autoloot.lua" />
    170.     <action itemid="4256" script="autoloot.lua" />
    171.     <action itemid="2866" script="autoloot.lua" />
    172.     <action itemid="4317" script="autoloot.lua" />
    173.     <action itemid="4314" script="autoloot.lua" />
    174.     <action itemid="2857" script="autoloot.lua" />
    175.     <action itemid="4310" script="autoloot.lua" />
    176.     <action itemid="2908" script="autoloot.lua" />
    177.     <action itemid="4295" script="autoloot.lua" />
    178.     <action itemid="3128" script="autoloot.lua" />
     
    strutZ, login12 and Peonso like this.
  2. SnorkY

    SnorkY Well-Known Member

    Joined:
    Nov 28, 2007
    Messages:
    1,218
    Likes Received:
    206
    Best Answers:
    0
    Very nice scripts! I don't see any use for this, though. Looting thing's is a part of the game and in some instances it requires some brain and fast hands.
     
    LaloHao likes this.
  3. LaloHao

    LaloHao Member

    Joined:
    Sep 14, 2008
    Messages:
    73
    Likes Received:
    12
    Best Answers:
    0
    Technically anything is part of the game and everything custom is extra (maps different from RL tibia or OTCLIENT for example), but that would be a phisophical matter and i dont know if we can discuss it here

    There are many games that have this feature, and i always wanted to make it when i first hosted servers back in 7.5 and didnt have any knowledge of programming, you can limit it to 1 autoloot item, or sell autoloot slots for cash, idk, still i find it very cool and i wanted to share.

    Cheers
     
    SnorkY likes this.
  4. SnorkY

    SnorkY Well-Known Member

    Joined:
    Nov 28, 2007
    Messages:
    1,218
    Likes Received:
    206
    Best Answers:
    0
    Yeah, it is a cool function.
     
  5. login12

    login12 void newbie scripter()

    Joined:
    Feb 26, 2011
    Messages:
    139
    Likes Received:
    7
    Best Answers:
    0
    Its so very nice!
    So easy configurable, good job ;)
     
  6. LaloHao

    LaloHao Member

    Joined:
    Sep 14, 2008
    Messages:
    73
    Likes Received:
    12
    Best Answers:
    0
    It doesn't find items in bags, but with this function it does
    PHP:
    1.  
    2. function getContents2(uid)
    3.    local loot, i = {}, 0
    4.  
    5.    while i < getContainerSize(uid) do
    6.       local v = getContainerItem(uid, i)
    7.       if isContainer(v.uid) then
    8.          local subcontainer = getContents(v.uid)
    9.          for __, vv in ipairs(subcontainer) do
    10.             table.insert(loot, vv)
    11.          end
    12.       else
    13.          table.insert(loot, v)
    14.       end
    15.       i = i + 1
    16.    end
    17.  
    18.    return loot
    19.  

    Use
    PHP:
    1.    local loot = getContents2(corpse)
    Instead of
    PHP:
    1.    local loot = getContents(corpse)
    in actions/scripts/autoloot.lua
     

Share This Page

Loading...