• 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!

Write events in many monsters at once

Strack

Member
Joined
May 15, 2009
Messages
199
Reaction score
14
At first, sorry for my english.

Hello Otlanders, here i bring an script i made recently...

Description:
The idea of the script is to write events in monsters .xml files as fast as possible.
How many times you wanted to add an event to many monsters but they were so many that you end bored of adding the event 1 to 1?
With this script you can add events to all the monsters in any folder inside /data/monsters.

Here is the script and then a brief explanation:

actions.xml add:
Code:
<action itemid="5957" script="testear.lua"/>

/scripts/testear.lua:
Code:
--Credits Strack

function existfile(file)
local f = io.open(file, "rb")
   if f then f:close() end
return f ~= nil
end
   
function getLines(file)
   if not existfile(file) then return {} end
   lines = {}
   for line in io.lines(file) do 
      lines[#lines + 1] = line
   end
return lines
end

function writefile(f,text)
   if not existfile(f) then return false end
   local file = io.open(f,'w+')
   file:write(text)
   io.close(file)
end
   
function onUse(cid, item, frompos, item2, topos) 

local folders = {'Amazons','Annelids','Apes','Arachnids'}
local additionaltext = '<event name="corpsename"/>'

for _,fld in pairs(folders) do -- por cada folder en el array
   local path = "dir \""..getDataDir().."monster\\"..fld.."\\\""
   local f = io.popen(path)
   if f then
      for i in (f:read("*a")):gmatch("(.-)\n(.-)") do 
         local found = i:match('(%a.-).xml')
         if found then
            local filt = string.gsub(path, '"', '')
            filt = string.gsub(filt, 'dir ', '')
            filt = string.gsub(filt, '\\\\', '\\')

            local filedir = filt..found..".xml" 
         
            -- editing found file
            local text = ""
            local wrote = false
            local lines = getLines(filedir)
            for k,v in pairs(lines) do
               local a = string.find(v, "<script>")
               if a ~= nil then 
                  v = string.gsub(v, "<script>", "<script> \n "..additionaltext.."\n")
                  wrote=true
               end

               local a = string.find(v, "</monster>")
               if a ~= nil and not wrote then
                  text = text.." <script>\n"..additionaltext.."\n</script>\n"
                  wrote=true
               end
               text = text..""..v.."\n"
            -- end of editing      
            end--end of for k,v
            writefile(filedir,text)
         end--end of IF found
      end--end of for i...
   else
      print("failed to read")
   end--end of if F
end
return true
end


You just need to edit these lines:
Code:
local folders = {'Amazons','Annelids','Apes','Arachnids'}
local additionaltext = '<event name="corpsename"/>'

additionaltext = event tag + event name
folders = here are the name of every folder you want this script search in, in order to find any monter' xml file and add the event text.
If you also want it to search in data/monsters/, you must add an empty parameter to local folders.
example:
Code:
local folders = {'Amazons','Annelids','Apes','Arachnids',''}

In this case it will seach inside:
Code:
/data/monsters/
/data/monsters/Amazons/
/data/monsters/Annelids/
/data/monsters/Apes/
/data/monsters/Arachnids

and inside these folders, the script will find for any .xml file and will add: <script><event name="corpsename"/></script> in the monster xml file.

If you had another event inside the xml file, it will add the event, keeping all events you had.

I know this is far from being perfect, but I made it for me and decided to post it for those who find it usefull.

Comment if you have any doubt or have any idea in order to improve this script.

Regards, Strack.
 
Last edited:
Back
Top