• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Remove!

Printer

if Printer then print("LUA") end
Senator
Premium User
Joined
Dec 27, 2009
Messages
5,780
Solutions
31
Reaction score
2,299
Location
Sweden?
Solved
 
Last edited:
As far as I know you can do this via global storages.
I guess they don't reset after a server crash or restart, they are stored in the database.
If you use an sql query to update the DB each time an item drops you can easily get the statistics ouf of the DB.

To make it even esier, the GlobalStorage can be the item id and the value the current count.
setGlobalStorageValue(ITEM_ID, COUNT)
simple as that
 
It should be like:
LUA:
if getContainerItem(demoncorpse.uid, magicswordID) == true then
setGlobalStorageValue(magicswordID, (getGlobalStorageValue(magicswordID)+1))
end

Try an onUse() like
LUA:
local demonid = xxxx --corpseID
local ms = xxxx --magic sword ID
function onUse(cid, item, fromPosition, itemEx, toPosition)
if itemEx.itemid == demonid then
   if getContainerItem(itemEx.uid, ms) == true then
      setGlobalStorageValue(ms, getGlobalStorageValue(ms)+1)
   end
end
return true
end
Could be that you cannot open corpses now, so delete "return true" if that is the case.

I'm sure you'll ask how to add that in actions.xml, so here you go
XML:
<action itemid="DEMON_CORPSE_ID" event="script" value="yourScript.lua"/>
Again, could happen that you cannot open corpses now!
 
Last edited:
Error when i open the server

Code:
[25/02/2012 08:37:37] [Error - LuaScriptInterface::loadFile] data/actions/scripts/lc.lua:6: unexpected symbol near ')'
[25/02/2012 08:37:37] [Warning - Event::loadScript] Cannot load script (data/actions/scripts/lc.lua)
[25/02/2012 08:37:37] data/actions/scripts/lc.lua:6: unexpected symbol near ')'
 
As you said i cant open the dead bodys and i wonder where can i see how many times the monster droop the loot item.
 
Well , first he needs that each monster have his own dropped count the thing you do there is wrong from the side of idea and scripting too (getContainerItem second argument is slot not item id).

Second it isn't bad to use storages but just it would take many storages if he wants multiple items dropped by multiple monsters.
 
You can try something like that
make new loot.lua in creatureevent.xml-->scripts
LUA:
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

now in creatureevent.xml paste
XML:
<event type="kill" name="loot" event="script" value="loot.lua"/>
	<event type="login" name="lootlogin" event="script" value="loot.lua"/>

Well this will mainly create a log and will update the log every kill the log is placed in server directory name "monster_loot"

example

Code:
#######################################################################################################################
#######################################################################################################################
##################### 						Mosnter Loot Log			#######################################
###################							By: Doggynub       								############################
########################################################################################################################


--[[Rat]]--

Rat: 13x gold coin[2148]


--[[Demon]]--

Demon: 2x great mana potion[7590]
Demon: 528x gold coin[2148]


--[[Orshabaal]]--

Orshabaal: 171x gold coin[2148]


--[[Warlock]]--

Warlock: 59x gold coin[2148]


--[[Dragon]]--

Dragon: 33x gold coin[2148]
 
Back
Top