• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[Lua] Autoloot system for OTHIRE

LaloHao

Member
Joined
Sep 14, 2008
Messages
73
Reaction score
14
Location
Mexico
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.


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:
-- Load Autoloot System
dofile(getDataDir() .. 'lib/autoloot.lua')

PHP:
STORAGE_AUTOLOOTMESSAGE = 31009
STORAGE_AUTOLOOT = {7575, 7576, 7577, 7578, 7579}
STORAGE_AUTOLOOTLIMIT = 5

function getContents(uid)
   local loot, i = {}, 0

   while i < getContainerSize(uid) do
      local v = getContainerItem(uid, i)
      table.insert(loot, v)
      i = i + 1
   end

   return loot
end

function getAutolootList(cid)
   local list, v, count = {}, 0, 0
   for _, k in ipairs(STORAGE_AUTOLOOT) do
      v = getPlayerStorageValue(cid, k)
      if v > 0 then
         count = count + 1
      end
      table.insert(list, v)
   end
   return list, count
end

function AutolootLootItem(cid, item, msg)
   local Item = item
   local count = Item.type
   if count < 1 then
      count = 1
   end
   doRemoveItem(item.uid)
   doPlayerAddItem(cid, Item.itemid, count)
   Item = getItemNameById(Item.itemid)
   if msg then
      doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, msg..count.." "..Item)
   end
end

function isAutolootShowEnabled(cid)
   return getPlayerStorageValue(cid,STORAGE_AUTOLOOTMESSAGE) and 1
end

function AutolootToggleShow(cid)
   if getPlayerStorageValue(cid, STORAGE_AUTOLOOTMESSAGE) == -1 then
      setPlayerStorageValue(cid, STORAGE_AUTOLOOTMESSAGE, 1)
      doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Showing autoloot.")
   else
      setPlayerStorageValue(cid, STORAGE_AUTOLOOTMESSAGE, -1)
      doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Hiding autoloot.")
   end
end

function AutolootPrint(cid, msg)
   doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, msg)
end

function AutolootPrintItems(cid, items, bullet)
   for _, v in ipairs(items) do
      if v > 0 then
         AutolootPrint(cid, bullet..getItemNameById(v))
      end
   end
end

function AutolootAddItem(cid, item, name)
   for _,v in ipairs(STORAGE_AUTOLOOT) do
      if getPlayerStorageValue(cid, v) == -1 then
         setPlayerStorageValue(cid, v, item)
         AutolootPrint(cid, ">>"..name.."<< added to autoloot. Slot:".._)
         break
      end
   end
end

function AutolootRemoveItem(cid, item, name)
   for _,v in ipairs(STORAGE_AUTOLOOT) do
      if getPlayerStorageValue(cid, v) == item then
         setPlayerStorageValue(cid, v, -1)
         AutolootPrint(cid, ">>"..name.."<< removed from autoloot.")
         break
      end
   end
end

function AutolootClean(cid)
   for _, k in ipairs(STORAGE_AUTOLOOT) do
      setPlayerStorageValue(cid, k, -1)
   end
end

function AutolootGetItemFromParam(rest)
   local item = tonumber(rest)
   local name = getItemIdByName(rest)
   local err = false
   if item then
      -- AutolootPrint(cid, "Data triggered by Item: ".. item)
   elseif name then
      item = name
      -- AutolootPrint(cid, "Data triggered by Name: ".. name)
   else
      err = "Not valid item."
   end

   if isItemContainer(item) then
      err = "This item can not be autolooted."
   end

   if not isItemMoveable(item) then
      err = "This item can not be autolooted."
   end
   return item, err
end

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

PHP:
local limit = 5

function onSay(cid, words, param)
   if param == "" then
      AutolootToggleShow(cid)
      return false
   end

   local help = "Help"
   local expl = string.explode(param, ':')
   local action, rest = expl[1], expl[2]
   -- doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Param: ".. param)
   -- doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "1: "..expl[1].." 2: ".. expl[2])

   local lootList, Items = getAutolootList(cid)

   local bulletItem = "----->"
   if (action:lower() == "list") then
      AutolootPrint(cid, "Autoloot list: ("..Items..") Items.")
      AutolootPrintItems(cid, lootList, bulletItem)
      return false
   elseif (action:lower() == "clean") then
      AutolootClean(cid)
      AutolootPrint(cid, "Autoloot list cleaned.")
      return false
   else
      if not rest then
         AutolootPrint(cid, help)
         return false
      end

      local item, err = AutolootGetItemFromParam(rest)
      if err then
         AutolootPrint(cid, err)
         return false
      end

      local name = getItemNameById(item)
      if (action:lower() == "add") then
         if (Items >= limit) then
            AutolootPrint(cid, "You already have " .. limit .. " autolooting items.")
            return false
         end

         if isInArray(lootList, item) then
            AutolootPrint(cid, "This item is already in your list.")
            return false
         end

         AutolootAddItem(cid, item, name)
      elseif (action:lower() == "remove") then
         if (Items < 1) then
            AutolootPrint(cid, "You dont have any item added.")
            return false
         end

         if not isInArray(lootList, item) then
            AutolootPrint(cid, ">>"..name.."<< is not in the list.")
            return false
         end

         AutolootRemoveItem(cid, item, name)
      end
   end
   return false
end

PHP:
function onUse(cid, item, fromPosition, itemEx, toPosition)
   local corpse = item.uid
   if not isContainer(corpse) then
      return false
   end
   local lootMessage = "Autolooted: "

   local lootList, Items = getAutolootList(cid)

   if Items == 0 then
      return false
   end

   local loot = getContents(corpse)
   local party = getPartyMembers(cid)

   if party then
      if lastHit then
         for _, pid in ipairs(getPartyMembers(cid)) do
            if isAutolootShowEnabled(pid) then
               -- Print(loot)
            end
         end
      end
   else
      local item, count
      for v in ipairs(loot) do
         item = loot[v].itemid
         if isInArray(lootList, item) then
            if isAutolootShowEnabled(cid) then
               AutolootLootItem(cid, loot[v], lootMessage)
            else
               AutolootLootItem(cid, loot[v])
            end
         end
      end
   end
   return false
end

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:
    <!-- Autoloot corpses -->
    <action itemid="3052" script="autoloot.lua" />
    <action itemid="2979" script="autoloot.lua" />
    <action itemid="2916" script="autoloot.lua" />
    <action itemid="4280" script="autoloot.lua" />
    <action itemid="2857" script="autoloot.lua" />
    <action itemid="3016" script="autoloot.lua" />
    <action itemid="3016" script="autoloot.lua" />
    <action itemid="3128" script="autoloot.lua" />
    <action itemid="3016" script="autoloot.lua" />
    <action itemid="3016" script="autoloot.lua" />
    <action itemid="3128" script="autoloot.lua" />
    <action itemid="2876" script="autoloot.lua" />
    <action itemid="3034" script="autoloot.lua" />
    <action itemid="3119" script="autoloot.lua" />
    <action itemid="3016" script="autoloot.lua" />
    <action itemid="2952" script="autoloot.lua" />
    <action itemid="2881" script="autoloot.lua" />
    <action itemid="3034" script="autoloot.lua" />
    <action itemid="2876" script="autoloot.lua" />
    <action itemid="3016" script="autoloot.lua" />
    <action itemid="2940" script="autoloot.lua" />
    <action itemid="2376" script="autoloot.lua" />
    <action itemid="2967" script="autoloot.lua" />
    <action itemid="2820" script="autoloot.lua" />
    <action itemid="1740" script="autoloot.lua" />
    <action itemid="2931" script="autoloot.lua" />
    <action itemid="3043" script="autoloot.lua" />
    <action itemid="2979" script="autoloot.lua" />
    <action itemid="3052" script="autoloot.lua" />
    <action itemid="2979" script="autoloot.lua" />
    <action itemid="2916" script="autoloot.lua" />
    <action itemid="4280" script="autoloot.lua" />
    <action itemid="2857" script="autoloot.lua" />
    <action itemid="3016" script="autoloot.lua" />
    <action itemid="3016" script="autoloot.lua" />
    <action itemid="3128" script="autoloot.lua" />
    <action itemid="3016" script="autoloot.lua" />
    <action itemid="3016" script="autoloot.lua" />
    <action itemid="3128" script="autoloot.lua" />
    <action itemid="2876" script="autoloot.lua" />
    <action itemid="3034" script="autoloot.lua" />
    <action itemid="3119" script="autoloot.lua" />
    <action itemid="3016" script="autoloot.lua" />
    <action itemid="2952" script="autoloot.lua" />
    <action itemid="2881" script="autoloot.lua" />
    <action itemid="3034" script="autoloot.lua" />
    <action itemid="2876" script="autoloot.lua" />
    <action itemid="3016" script="autoloot.lua" />
    <action itemid="4307" script="autoloot.lua" />
    <action itemid="3049" script="autoloot.lua" />
    <action itemid="4259" script="autoloot.lua" />
    <action itemid="2817" script="autoloot.lua" />
    <action itemid="2960" script="autoloot.lua" />
    <action itemid="1496" script="autoloot.lua" />
    <action itemid="3128" script="autoloot.lua" />
    <action itemid="2876" script="autoloot.lua" />
    <action itemid="4262" script="autoloot.lua" />
    <action itemid="2843" script="autoloot.lua" />
    <action itemid="4993" script="autoloot.lua" />
    <action itemid="4326" script="autoloot.lua" />
    <action itemid="4298" script="autoloot.lua" />
    <action itemid="2935" script="autoloot.lua" />
    <action itemid="3128" script="autoloot.lua" />
    <action itemid="2987" script="autoloot.lua" />
    <action itemid="4277" script="autoloot.lua" />
    <action itemid="2920" script="autoloot.lua" />
    <action itemid="4301" script="autoloot.lua" />
    <action itemid="2940" script="autoloot.lua" />
    <action itemid="2376" script="autoloot.lua" />
    <action itemid="2967" script="autoloot.lua" />
    <action itemid="2820" script="autoloot.lua" />
    <action itemid="1740" script="autoloot.lua" />
    <action itemid="3128" script="autoloot.lua" />
    <action itemid="2905" script="autoloot.lua" />
    <action itemid="4320" script="autoloot.lua" />
    <action itemid="3128" script="autoloot.lua" />
    <action itemid="3104" script="autoloot.lua" />
    <action itemid="4994" script="autoloot.lua" />
    <action itemid="3040" script="autoloot.lua" />
    <action itemid="3065" script="autoloot.lua" />
    <action itemid="3007" script="autoloot.lua" />
    <action itemid="3065" script="autoloot.lua" />
    <action itemid="1492" script="autoloot.lua" />
    <action itemid="4286" script="autoloot.lua" />
    <action itemid="3069" script="autoloot.lua" />
    <action itemid="3055" script="autoloot.lua" />
    <action itemid="2809" script="autoloot.lua" />
    <action itemid="2983" script="autoloot.lua" />
    <action itemid="3128" script="autoloot.lua" />
    <action itemid="3019" script="autoloot.lua" />
    <action itemid="3004" script="autoloot.lua" />
    <action itemid="2969" script="autoloot.lua" />
    <action itemid="4304" script="autoloot.lua" />
    <action itemid="2924" script="autoloot.lua" />
    <action itemid="4268" script="autoloot.lua" />
    <action itemid="2989" script="autoloot.lua" />
    <action itemid="2949" script="autoloot.lua" />
    <action itemid="4274" script="autoloot.lua" />
    <action itemid="3128" script="autoloot.lua" />
    <action itemid="4265" script="autoloot.lua" />
    <action itemid="2813" script="autoloot.lua" />
    <action itemid="4292" script="autoloot.lua" />
    <action itemid="2938" script="autoloot.lua" />
    <action itemid="2928" script="autoloot.lua" />
    <action itemid="2916" script="autoloot.lua" />
    <action itemid="2995" script="autoloot.lua" />
    <action itemid="5014" script="autoloot.lua" />
    <action itemid="4994" script="autoloot.lua" />
    <action itemid="4271" script="autoloot.lua" />
    <action itemid="2913" script="autoloot.lua" />
    <action itemid="3010" script="autoloot.lua" />
    <action itemid="2886" script="autoloot.lua" />
    <action itemid="3031" script="autoloot.lua" />
    <action itemid="2940" script="autoloot.lua" />
    <action itemid="3128" script="autoloot.lua" />
    <action itemid="2902" script="autoloot.lua" />
    <action itemid="2897" script="autoloot.lua" />
    <action itemid="2914" script="autoloot.lua" />
    <action itemid="2972" script="autoloot.lua" />
    <action itemid="3128" script="autoloot.lua" />
    <action itemid="4992" script="autoloot.lua" />
    <action itemid="3052" script="autoloot.lua" />
    <action itemid="2820" script="autoloot.lua" />
    <action itemid="3068" script="autoloot.lua" />
    <action itemid="3119" script="autoloot.lua" />
    <action itemid="2849" script="autoloot.lua" />
    <action itemid="2998" script="autoloot.lua" />
    <action itemid="4280" script="autoloot.lua" />
    <action itemid="2848" script="autoloot.lua" />
    <action itemid="4253" script="autoloot.lua" />
    <action itemid="2860" script="autoloot.lua" />
    <action itemid="2826" script="autoloot.lua" />
    <action itemid="1496" script="autoloot.lua" />
    <action itemid="2881" script="autoloot.lua" />
    <action itemid="3128" script="autoloot.lua" />
    <action itemid="2813" script="autoloot.lua" />
    <action itemid="2871" script="autoloot.lua" />
    <action itemid="2985" script="autoloot.lua" />
    <action itemid="2967" script="autoloot.lua" />
    <action itemid="2862" script="autoloot.lua" />
    <action itemid="2945" script="autoloot.lua" />
    <action itemid="3001" script="autoloot.lua" />
    <action itemid="3037" script="autoloot.lua" />
    <action itemid="2952" script="autoloot.lua" />
    <action itemid="2839" script="autoloot.lua" />
    <action itemid="3065" script="autoloot.lua" />
    <action itemid="3022" script="autoloot.lua" />
    <action itemid="3028" script="autoloot.lua" />
    <action itemid="2864" script="autoloot.lua" />
    <action itemid="2824" script="autoloot.lua" />
    <action itemid="2830" script="autoloot.lua" />
    <action itemid="2899" script="autoloot.lua" />
    <action itemid="2893" script="autoloot.lua" />
    <action itemid="4323" script="autoloot.lua" />
    <action itemid="3128" script="autoloot.lua" />
    <action itemid="3046" script="autoloot.lua" />
    <action itemid="3025" script="autoloot.lua" />
    <action itemid="3013" script="autoloot.lua" />
    <action itemid="2835" script="autoloot.lua" />
    <action itemid="4289" script="autoloot.lua" />
    <action itemid="2889" script="autoloot.lua" />
    <action itemid="2806" script="autoloot.lua" />
    <action itemid="2981" script="autoloot.lua" />
    <action itemid="3113" script="autoloot.lua" />
    <action itemid="4283" script="autoloot.lua" />
    <action itemid="3065" script="autoloot.lua" />
    <action itemid="3128" script="autoloot.lua" />
    <action itemid="2956" script="autoloot.lua" />
    <action itemid="4256" script="autoloot.lua" />
    <action itemid="2866" script="autoloot.lua" />
    <action itemid="4317" script="autoloot.lua" />
    <action itemid="4314" script="autoloot.lua" />
    <action itemid="2857" script="autoloot.lua" />
    <action itemid="4310" script="autoloot.lua" />
    <action itemid="2908" script="autoloot.lua" />
    <action itemid="4295" script="autoloot.lua" />
    <action itemid="3128" script="autoloot.lua" />
 
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.
 
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.
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
 
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

Yeah, it is a cool function.
 
It doesn't find items in bags, but with this function it does
PHP:
function getContents2(uid)
   local loot, i = {}, 0

   while i < getContainerSize(uid) do
      local v = getContainerItem(uid, i)
      if isContainer(v.uid) then
         local subcontainer = getContents(v.uid)
         for __, vv in ipairs(subcontainer) do
            table.insert(loot, vv)
         end
      else
         table.insert(loot, v)
      end
      i = i + 1
   end

   return loot
end

Use
PHP:
   local loot = getContents2(corpse)

Instead of
PHP:
   local loot = getContents(corpse)

in actions/scripts/autoloot.lua
 
It doesn't find items in bags, but with this function it does
PHP:
function getContents2(uid)
   local loot, i = {}, 0

   while i < getContainerSize(uid) do
      local v = getContainerItem(uid, i)
      if isContainer(v.uid) then
         local subcontainer = getContents(v.uid)
         for __, vv in ipairs(subcontainer) do
            table.insert(loot, vv)
         end
      else
         table.insert(loot, v)
      end
      i = i + 1
   end

   return loot
end

Use
PHP:
   local loot = getContents2(corpse)

Instead of
PHP:
   local loot = getContents(corpse)

in actions/scripts/autoloot.lua


Nice! Thank you too.
 
Thanks a lot for the code, it's working fine for me. I only noticed that a lot of corpses were missing in actions.xml.

I wrote a short python script to find all corpses that are containers in my items.xml and generate according entries for the actions.xml (entries need to be copied there). Here's the script if anyone else is interested:

Python:
import xml.etree.ElementTree as ET

tree = ET.parse('data/items/items.xml')
root = tree.getroot()

for child in root:
    if child.attrib['name'].find('dead') != -1:
        children = child.getchildren()
        is_container = False
        for attrib in children:
            if attrib.attrib['key'] == 'containerSize':
                is_container = True
        if is_container and 'id' in child.attrib:
            print('<action itemid="{}" script="autoloot.lua" />'.format(child.attrib['id']))
 
Is a good system, would be nice if you add autostack when you loot stackables items, and loot items to a configured backpack, for example.

Lua:
!autoloot add, Fire Sword, Red Backpack

Anyway good contribution.
 
Hello,

Anyone can convert it that items will be stackable like gold coins/platinum coins ? I can paid for it.

Thanks in advance !
 
Back
Top