FAÇA BACKUP DA PASTA MONSTERS E CREATURESCRIPTS ANTES DE UTILIZAR!
Funciona para todas as versões TFS 0.3.6+ ate TFS 1.2 (Provavelmente funciona para outras tambem mas não me dei o trabalho de testar)
Introdução:
Bem, como o titulo mesmo diz, o script vai criar e modificar todos os monstros para terem scripts default (Caso o monstro não tenha script) e tambem pode adicionar creaturescripts default, exemplo o onDeath.
Code:
monsterevent.lua
local dir = "./data/monster/"
local csdir = "./data/creaturescripts/"
local monstersXML = "monsters.xml"
local csXML = "creaturescripts.xml"
local csTag = '\n <event type="%t" name="%n" script="%s"/>' -- Tag format on creaturescripts.xml (%t = type, %n = name, %s = script)
local defaultText =
[[function onCreatureAppear(self, creature) return false end
function onCreatureDisappear(self, creature) return false end
function onCreatureMove(self, creature, oldPosition, newPosition) return false end
function onCreatureSay(self, creature, type, message) return false end
function onThink(self, interval) return false end]]
local events = {
["monsterdeath"] = {type = "death", file="monsterdeath.lua", defaultText=[[
function onDeath(cid, corpse, lasthitkiller, mostdamagekiller, lasthitunjustified, mostdamageunjustified)
return true
end]]},
}
------- Não edite a partir dessa linha se nao souber o que esta fazendo.
function parseXML(file)
local ret = {["monster"] = {}, ["event"] = {}}
local data = ""
for line in file:lines() do
data = data .. line .. "\n"
line = line:match("<(.-)/>")
if line then
local element = line:match("^%s*(.-)%s")
if element == "monster" or element == "event" then
local t = {}
for i, x in line:gmatch('%s+(.-)="(.-)"') do
t[i] = x
end
table.insert(ret[element], t)
end
end
end
return ret, data:sub(1, #data-1)
end
function init()
----- Parsing monsters.xml -----
local monstersfile = io.open(dir .. monstersXML, "r")
print("Parsing XML " .. dir .. monstersXML .. "...")
local xmltable = parseXML(monstersfile)
monstersfile:close()
--------------------------------
-- Creating scripts directory --
local testfile = io.open(dir .. "scripts/test", "w")
if not testfile then
os.execute("mkdir data\\monster\\scripts")
print("Directory " .. dir .. "scripts has been created.")
else
testfile:close()
end
os.remove(dir .. "scripts/test")
---------------------------------
------ Creating default.lua -------
local defaultlua = io.open(dir .. "scripts/default.lua", "w")
defaultlua:write(defaultText)
defaultlua:close()
print("File " .. dir .. "scripts/default.lua has been created.")
---------------------------------
---- Modify all monsters XML! ----
local modified = 0
for i, contents in ipairs(xmltable["monster"]) do
local monsterxml = io.open(dir .. contents.file, "r")
if monsterxml then
local xmldata = monsterxml:read("*a")
local _mod = false
local s, f, content = xmldata:find("<monster%s(.-)>")
local r, p, cs = content:find('script="(.-)"')
monsterxml:close()
newc = content
if not cs then
xmldata = xmldata:gsub(content, newc .. ' script="default.lua"')
_mod = true
elseif cs ~= "default.lua" then
print('Warning: The file "' .. contents.file .. '" already has a script.')
end
local s, f, content = xmldata:find("<monster%s(.-)>")
local x, q, scriptcontent = xmldata:find("<script>(.-)</script>")
local readyevents = {}
local addevents = ""
if scriptcontent then
for line in scriptcontent:gmatch("(.-)\n") do
local event = line:match('<event%s-name="(.-)"')
if event then
readyevents[event] = true
end
end
end
for event, _c in pairs(events) do
if not readyevents[event] then
addevents = addevents .. '<event name="' .. event .. '"/>\n'
end
end
if addevents ~= "" then
_mod = true
if scriptcontent and scriptcontent:find("event") then
xmldata = xmldata:gsub(scriptcontent, scriptcontent .. addevents)
elseif x and q then
xmldata = xmldata:sub(1, x-1) .. xmldata:sub(q+1)
xmldata = xmldata:sub(1, f) .. "\n<script>\n" .. addevents .. "\n</script>" .. xmldata:sub(f+1)
else
xmldata = xmldata:sub(1, f) .. "\n<script>\n" .. addevents .. "\n</script>" .. xmldata:sub(f+1)
end
end
if xmldata then
if _mod then
modified = modified+1
end
local monsterxmlwrite = io.open(dir .. contents.file, "w")
monsterxmlwrite:write(xmldata)
monsterxmlwrite:close()
else
print("Error in: " .. contents.name)
end
else
print("Warning: Error opening file " .. contents.file)
end
end
print("Total monsters XML modified: " .. modified)
----------------------------------
--- Parsing creaturescripts.xml --
local csfile = io.open(csdir .. csXML, "r")
print("Parsing XML " .. csdir .. csXML .. "...")
local csxmltable, xmldata = parseXML(csfile)
csfile:close()
----------------------------------
--- Modify creaturescripts.xml ---
local _mod = false
local readytags = {}
for i, contents in ipairs(csxmltable["event"]) do
if events[contents.name] then
readytags[contents.name] = true
end
end
for event, contents in pairs(events) do
if not readytags[event] then
_mod = true
local currenttag = csTag
currenttag = currenttag:gsub("%%t", contents.type)
currenttag = currenttag:gsub("%%n", event)
currenttag = currenttag:gsub("%%s", contents.file)
local r, w = xmldata:find("<creaturescripts>")
xmldata = xmldata:sub(1, w) .. currenttag .. xmldata:sub(w+1)
end
end
if _mod then
local csfilewrite = io.open(csdir .. csXML, "w")
csfilewrite:write(xmldata)
csfilewrite:close()
print("Modified " .. csdir .. csXML)
end
----------------------------------
----- Creating event scripts -----
print("Creating lua creaturescripts...")
for event, contents in pairs(events) do
local luafile = io.open(csdir .. "scripts/" .. contents.file, "w")
luafile:write(contents.defaultText)
luafile:close()
end
----------------------------------
print("All the modifications were done.")
return true
end
local check = io.open("meventsinstall.lua", "r")
if not check then
local ret = init()
if ret then
local file = io.open("meventsinstall.lua", "w")
file:write("true")
file:close()
end
else
check:close()
end
Explicando o que o code realmente faz:
O code vai ser executado somente uma vez e vai editar todos os monstros do seu OTServer presentes no monsters.xml adicionando um script default, que seria o defaultText presente no inicio do code (So funciona em TFS 1.0+) e tambem ira adicionar eventos ao XML do monstro sem remover os que ja existem (se existirem).
Depois disso ele ira editar o creaturescripts.xml para criar a tag do evento e tambem criar o script que esta na tabela events.
Como instalar:
TFS 0.3.6:
Só botar ele na pasta lib do seu OT que ele ja vai executar quando voce abrir o executavel.
TFS 1.x:
Bote o code na pasta "lib\core" e edite o core.lua nessa pasta adicionando essa linha:
dofile('data/lib/core/monstersevent.lua')
Se voce fizer tudo certo deve aparecer isso quando voce abrir o server:
Você pode rodar o script novamente deletando o arquivo "meventsinstall.lua" que sera criado na pasta do server, ele soh ira adicionar algo se voce mudar alguma coisa no code.
Porque usar esse code:
Não usar funçoes como onAttack, onSpawn entre outras para registrar eventos em monstros.
Usar as funçoes onCreatureAppear, onCreatureDisappear, onCreatureMove, onCreatureSay e onThink em todos os monstros do server (TFS 1.0+)