1
0
mirror of https://github.com/sys4-fr/server-nalc.git synced 2024-09-28 15:30:33 +02:00

[rofl] Implement ability to apply files

This commit is contained in:
LeMagnesium 2015-11-28 21:17:30 +01:00
parent 5e5bc34748
commit 5ca090fc73

View File

@ -17,10 +17,10 @@ rofl.files = {}
rofl.conf = {} rofl.conf = {}
rofl.conf.Tick = 20 -- Tick interval rofl.conf.Tick = 20 -- Tick interval
rofl.conf.Rpertick = 2000 -- Max reading operations per tick rofl.conf.Rpertick = 20000 -- Max reading operations per tick
rofl.conf.Wpertick = 5000 -- Max writing operations per tick rofl.conf.Wpertick = 50000 -- Max writing operations per tick
rofl.conf.Apertick = 1000 -- Max application operations per tick rofl.conf.Apertick = 10000 -- Max application operations per tick
rofl.conf.Lpertick = 7500 -- Max loading operations per tick rofl.conf.Lpertick = 75000 -- Max loading operations per tick
rofl.buffers = {} rofl.buffers = {}
rofl.buffers.RCache = {} -- Reading rofl.buffers.RCache = {} -- Reading
@ -29,7 +29,10 @@ rofl.buffers.LCache = {} -- Loading -- not in use
rofl.buffers.ACache = {} -- Application rofl.buffers.ACache = {} -- Application
-------------------------------------------------
-- ROFL Capture (not copter)
-- Queue nodes to be written in a buffer file
-------------------------------------------------
function rofl.capture(f, pos1, pos2) function rofl.capture(f, pos1, pos2)
local posU = {x = math.max(pos1.x, pos2.x), y = math.max(pos1.y, pos2.y), z = math.max(pos1.z, pos2.z)} local posU = {x = math.max(pos1.x, pos2.x), y = math.max(pos1.y, pos2.y), z = math.max(pos1.z, pos2.z)}
local posD = {x = math.min(pos1.x, pos2.x), y = math.min(pos1.y, pos2.y), z = math.min(pos1.z, pos2.z)} local posD = {x = math.min(pos1.x, pos2.x), y = math.min(pos1.y, pos2.y), z = math.min(pos1.z, pos2.z)}
@ -38,9 +41,9 @@ function rofl.capture(f, pos1, pos2)
for x = posD.x, posU.x do for x = posD.x, posU.x do
for y = posD.y, posU.y do for y = posD.y, posU.y do
for z = posD.z, posU.z do for z = posD.z, posU.z do
local node = minetest.get_node_or_nil({x = x, y = y, z = z}) local node = minetest.get_node({x = x, y = y, z = z})
if not node or node.name == "ignore" then if node.name == "ignore" then
return false -- return false, "Area unloaded : ~" .. minetest.pos_to_string({x = x, y = y, z = z})
else else
table.insert(rofl.buffers.WCache, {f, x-posD.x .. ";" .. y-posD.y .. ";" .. z-posD.z .. "|" .. minetest.serialize(node) .. "\n"}) table.insert(rofl.buffers.WCache, {f, x-posD.x .. ";" .. y-posD.y .. ";" .. z-posD.z .. "|" .. minetest.serialize(node) .. "\n"})
n = n + 1 n = n + 1
@ -52,7 +55,7 @@ function rofl.capture(f, pos1, pos2)
end end
minetest.register_chatcommand("roflCapture", { minetest.register_chatcommand("roflCapture", {
params = "<pos1> <pos2> <name>", params = "<name> <pos1> <pos2>",
description = "Capture nodes contained in area pos1 to pos2", description = "Capture nodes contained in area pos1 to pos2",
privs = {server=true}, privs = {server=true},
func = function(name, param) func = function(name, param)
@ -68,34 +71,119 @@ minetest.register_chatcommand("roflCapture", {
p1 = minetest.string_to_pos(p1) p1 = minetest.string_to_pos(p1)
p2 = minetest.string_to_pos(p2) p2 = minetest.string_to_pos(p2)
local k = rofl.capture(f, p1, p2) local k, u = rofl.capture(f, p1, p2)
if k then if k then
io.close(io.open(bufferfiles .. f .. ".buf", "w")) -- Remove any previous file that would have the same name
return true, "Successfully captured area from " .. return true, "Successfully captured area from " ..
core.pos_to_string(p1, 1) .. " to " .. core.pos_to_string(p2, 1) .. core.pos_to_string(p1, 1) .. " to " .. core.pos_to_string(p2, 1) ..
" in file " .. f .. " : " .. k .. " nodes" " in file " .. f .. " : " .. k .. " nodes"
else else
return false, "Failed to capture the area" return false, "Failed to capture the area : " .. u
end end
end, end,
}) })
minetest.register_on_shutdown(function() ----------------------
if table.getn(rofl.buffers.WCache) > 0 then -- Node application
minetest.log("warning", "{ROFL} WCache is not empty! Canceling all pending writing operations") ----------------------
rofl.buffers.WCache = {}
function rofl.apply(pos, buf)
local f = io.open(bufferfiles .. buf .. ".buf")
if not f then
return false, "No such buffer : " .. buf
end end
if table.getn(rofl.buffers.ACache) > 0 then local l = 0
minetest.log("warning", "{ROFL} ACache is not empty! Canceling all pending application operations") for line in f:lines() do
rofl.buffers.ACache = {} local t, node = unpack(line:split("|"))
end t = t:split(";")
for fname, f in pairs(rofl.files) do if table.getn(t) == 3 then
local pos2 = {x = t[1] + math.floor(pos.x), y = t[2] + math.floor(pos.y), z = t[3] + math.floor(pos.z)}
node = minetest.deserialize(node)
table.insert(rofl.buffers.ACache, {pos2, node})
l = l + 1
end
end
f:close() f:close()
minetest.log("action", "{ROFL} Closing filebuf " .. fname) return l
end end
end)
minetest.register_chatcommand("roflPaste", {
params = "<name> [<pos>]",
description = "Apply nodes present in buffer",
privs = {server=true},
func = function(name, param)
local bufname, pos = unpack(param:split(" "))
if not bufname or bufname == "" then
return false, "Invalid buffer name"
elseif not pos or pos == "" then
local p = minetest.get_player_by_name(name)
if not p then
return false, "Unable to get your position"
else
pos = minetest.pos_to_string(p:getpos())
end
end
if not minetest.string_to_pos(pos) then
return false, "Invalid pos"
end
local k, u = rofl.apply(minetest.string_to_pos(pos), bufname)
if not k then
return false, "Unable to apply nodes : " .. u
else
minetest.log("action", "{ROFL} " .. k .. " nodes queued")
return true, k .. " nodes are queued"
end
end
})
-----------------
-- Empty Caches
-----------------
minetest.register_chatcommand("roflFlush", {
params = "<W|A>",
description = "Empty Writing or Application buffers",
privs = {server=true},
func = function(name, param)
if param == "" then
return false, "Enter a letter, either W(riting) or A(pplication)"
elseif param == "W" then
rofl.buffers.WCache = {}
return true, "WCache flushed"
elseif param == "A" then
rofl.buffers.ACache = {}
return true, "ACache flushed"
else
return false, "Unknown cache name : " .. param
end
end
})
-------------------------
-- Delete buffer files
-------------------------
minetest.register_chatcommand("roflReset", {
params = "<name>",
description = "Empty a buffer file (or create the file if it doesn't exist)",
privs = {server=true},
func = function(name, param)
param = param:split(" ")[1]
if param == "" then
return false, "Enter a buffer name please"
else
io.close(io.open(bufferfiles .. param .. ".buf", "w"))
return true, "Done"
end
end
})
-------------------------------
-- Regularly work on queues
-------------------------------
function tick() function tick()
local t = os.time() local t = os.time()
@ -125,14 +213,30 @@ function tick()
end end
end end
--[[if table.getn(rofl.buffers.LCache) > 0 then
minetest.log("action", "{ROFL} LCache has " .. table.getn(rofl.buffers.LCache) .. " elements")
minetest.log("action", "{ROFL} Doing up to " .. rofl.conf.Lpertick .. " loading operations")
for x = 1, rofl.conf.Lpertick do
if not rofl.buffers.LCache[1] then break end
table.remove(rofl.buffers.LCache, 1)
end
if table.getn(rofl.buffers.LCache) == 0 then
minetest.log("action", "{ROFL} Loading operations finished")
end
end]]
-- Applying -- Applying
if table.getn(rofl.buffers.ACache) > 0 then if table.getn(rofl.buffers.ACache) > 0 then
minetest.log("action", "{ROFL} ACache has " .. table.getn(rofl.buffers.WCache) .. " elements") minetest.log("action", "{ROFL} ACache has " .. table.getn(rofl.buffers.ACache) .. " elements")
minetest.log("action", "{ROFL} Doing up to " .. rofl.conf.Wpertick .. " writing operations") minetest.log("action", "{ROFL} Doing up to " .. rofl.conf.Apertick .. " application operations")
for x = 1, rofl.conf.Apertick do for x = 1, rofl.conf.Apertick do
if not rofl.buffers.ACache[1] then break end if not rofl.buffers.ACache[1] then break end
buffer:write(rofl.buffers.ACache[1])
print(minetest.pos_to_string(rofl.buffers.ACache[1][1]) .. " : " .. rofl.buffers.ACache[1][2].name)
minetest.set_node(rofl.buffers.ACache[1][1], rofl.buffers.ACache[1][2])
table.remove(rofl.buffers.ACache, 1) table.remove(rofl.buffers.ACache, 1)
end end
@ -149,3 +253,23 @@ function tick()
end end
minetest.after(1, tick) minetest.after(1, tick)
--------------------------------
-- Clean our mess on shutdown
--------------------------------
minetest.register_on_shutdown(function()
if table.getn(rofl.buffers.WCache) > 0 then
minetest.log("warning", "{ROFL} WCache is not empty! Canceling all pending writing operations")
rofl.buffers.WCache = {}
end
if table.getn(rofl.buffers.ACache) > 0 then
minetest.log("warning", "{ROFL} ACache is not empty! Canceling all pending application operations")
rofl.buffers.ACache = {}
end
for fname, f in pairs(rofl.files) do
f:close()
minetest.log("action", "{ROFL} Closing filebuf " .. fname)
end
end)