local logged_items = {2160,2148, 7590, 2387} -- put here the item ids you want them to be logged
--[[ Functions ]]--
function doLog(pos,corpse_look,allowed_items,monster)
local main = getTileItemById(pos, corpse_look).uid
local found_items = searchContainer(main,allowed_items)
local file_name = "monsters_loot.txt"
if tableCount(found_items) > 0 then
createFile(file_name) -- creatinf file if not exist
local str, monster_found, left_items = updateFile(file_name, monster, found_items) -- updating current records and returning some checks
if not monster_found then
table.insert(str,"\n")
table.insert(str,createNewMonster(monster))
end
if tableCount(left_items) > 0 then
local f = getNewRecordPosition(str,monster)
local after = str[f+1] ~= nil and str[f+1] or ""
str[f+1] = createRecord(monster, left_items)..after
end
writeToFile(file_name,str)
end
end
function searchContainer(uid,items)
local found = {}
local containers = {}
for i = 0, getContainerSize(uid)-1 do
local item = getContainerItem(uid,i)
if not isContainer(item.uid) then
if isInArray(items,item.itemid) then
local count = found[item.itemid]
if count ~= nil then
found[item.itemid] = getCount(item) + count
else
found[item.itemid] = getCount(item)
end
end
else
table.insert(containers,item.uid)
end
end
for _, container in ipairs(containers) do
found = mergeTables(found,searchContainer(container,items))
end
return found
end
function updateRecord(line, count)
local n = 0
for numbers in line:match("%d+x"):gmatch("%d+") do
n = tonumber(numbers) +count
end
return string.gsub(line,"%d+x",n.."x")
end
function getNewRecordPosition(str,monster)
local n = 1
for c, line in pairs(str) do
if line:sub(1, 1) == '-' then
local name = line:match("%a+ *%a*")
if name ~= nil then
if name:lower() == monster:lower() then
return n
end
end
end
n = n+1
end
end
function createRecord(monster, left_items)
local str = ""
for itemid, count in pairs(left_items) do
str = str.."\n"..string.gsub(monster,"%a",string.upper,1)..": ".. left_items[itemid].."x ".. getItemNameById(itemid) .."["..itemid.."]"
end
return str
end
function createNewMonster(name)
local first = first or false
return "--[["..string.gsub(name,"%a",string.upper,1).. "]]--"
end
local start = "#######################################################################################################################\n#######################################################################################################################\n##################### Mosnter Loot Log #######################################\n################### By: Doggynub ############################\n########################################################################################################################\n"
function createFile(file)
local f = io.open(file,"r")
if f == nil then
local f = io.open(file,"w")
f:write(start)
f:close()
else
f:close()
end
end
function checkLineForItems(line,items)
local found, item_id = false, 0
for itemid, count in pairs(items) do
if line:find(itemid) ~= nil then
found = true
item_id = itemid
break
end
end
return found, item_id
end
function tableCount(table)
local n = 0
for _, count in pairs(table) do
n = n + 1
end
return n
end
function checkMonster(line, monster)
local name_found = ""
if string.sub(line, 1, 1) == '-' then
name_found = line:match("%a+ *%a*") ~= nil and line:match("%a+ *%a*") or ""
else
name_found = line:match("%a+ *%a*:") ~= nil and line:match("%a+ *%a*:"):match("%a+ *%a*") or ""
end
return name_found:lower() == monster:lower()
end
function updateFile(file, monster, items) -- if it finds the monster and item the item then it update else we need another loop
local left_items = items
local f = assert(io.open(file,"r"))
local str = {}
local monster_found = false
for line in f:lines() do
local line2 = line
if string.sub(line, 1, 1) ~= '#' then -- our comment check
if checkMonster(line,monster) then
monster_found = true
local found, item_id = checkLineForItems(line,left_items)
if found then
line2 = updateRecord(line, left_items[item_id])
left_items[item_id] = nil
end
end
table.insert(str,line2)
end
end
f:close()
return str, monster_found, left_items
end
function writeToFile(file,str)
local f = io.open(file,"w")
f:write(start)
for _,line in ipairs(str) do
f:write(line.."\n")
end
f:close()
end
function getCount(item)
return item.type == 0 and 1 or item.type
end
function mergeTables(table1,table2)
local new = table1
for itemid, count in pairs(table2) do
local c = table1[itemid]
if c ~= nil then
table1[itemid] = c + count
else
table1[itemid] = count
end
end
return new
end
--[[Scripts]]--
function onLogin(cid)
registerCreatureEvent(cid, "loot")
return true
end
function onKill(cid,target)
if isMonster(target) and isPlayer(cid) then
local pos = getThingPos(target)
local info = getMonsterInfo(getCreatureName(target))
local name = getCreatureName(target)
local corpse_id = info.lookCorpse
if corpse_id > 0 then
addEvent(doLog,100,pos,corpse_id,logged_items, name)
end
end
return true
end