From 5ca090fc736486849090d3846ddf5f06142a9892 Mon Sep 17 00:00:00 2001 From: LeMagnesium Date: Sat, 28 Nov 2015 21:17:30 +0100 Subject: [PATCH] [rofl] Implement ability to apply files --- mods/rofl/init.lua | 178 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 151 insertions(+), 27 deletions(-) diff --git a/mods/rofl/init.lua b/mods/rofl/init.lua index 717613d4..6aec7ad5 100755 --- a/mods/rofl/init.lua +++ b/mods/rofl/init.lua @@ -17,10 +17,10 @@ rofl.files = {} rofl.conf = {} rofl.conf.Tick = 20 -- Tick interval -rofl.conf.Rpertick = 2000 -- Max reading operations per tick -rofl.conf.Wpertick = 5000 -- Max writing operations per tick -rofl.conf.Apertick = 1000 -- Max application operations per tick -rofl.conf.Lpertick = 7500 -- Max loading operations per tick +rofl.conf.Rpertick = 20000 -- Max reading operations per tick +rofl.conf.Wpertick = 50000 -- Max writing operations per tick +rofl.conf.Apertick = 10000 -- Max application operations per tick +rofl.conf.Lpertick = 75000 -- Max loading operations per tick rofl.buffers = {} rofl.buffers.RCache = {} -- Reading @@ -29,7 +29,10 @@ rofl.buffers.LCache = {} -- Loading -- not in use rofl.buffers.ACache = {} -- Application - +------------------------------------------------- +-- ROFL Capture (not copter) +-- Queue nodes to be written in a buffer file +------------------------------------------------- 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 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 y = posD.y, posU.y do for z = posD.z, posU.z do - local node = minetest.get_node_or_nil({x = x, y = y, z = z}) - if not node or node.name == "ignore" then - return false + local node = minetest.get_node({x = x, y = y, z = z}) + if node.name == "ignore" then + -- return false, "Area unloaded : ~" .. minetest.pos_to_string({x = x, y = y, z = z}) else table.insert(rofl.buffers.WCache, {f, x-posD.x .. ";" .. y-posD.y .. ";" .. z-posD.z .. "|" .. minetest.serialize(node) .. "\n"}) n = n + 1 @@ -52,7 +55,7 @@ function rofl.capture(f, pos1, pos2) end minetest.register_chatcommand("roflCapture", { - params = " ", + params = " ", description = "Capture nodes contained in area pos1 to pos2", privs = {server=true}, func = function(name, param) @@ -68,34 +71,119 @@ minetest.register_chatcommand("roflCapture", { p1 = minetest.string_to_pos(p1) p2 = minetest.string_to_pos(p2) - local k = rofl.capture(f, p1, p2) + local k, u = rofl.capture(f, p1, p2) 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 " .. core.pos_to_string(p1, 1) .. " to " .. core.pos_to_string(p2, 1) .. " in file " .. f .. " : " .. k .. " nodes" else - return false, "Failed to capture the area" + return false, "Failed to capture the area : " .. u end end, }) -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 = {} +---------------------- +-- Node application +---------------------- + +function rofl.apply(pos, buf) + local f = io.open(bufferfiles .. buf .. ".buf") + if not f then + return false, "No such buffer : " .. buf 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 + local l = 0 + for line in f:lines() do + local t, node = unpack(line:split("|")) + t = t:split(";") - for fname, f in pairs(rofl.files) do - f:close() - minetest.log("action", "{ROFL} Closing filebuf " .. fname) - end -end) + 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() + return l +end + +minetest.register_chatcommand("roflPaste", { + params = " []", + 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 = "", + 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 = "", + 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() local t = os.time() @@ -125,14 +213,30 @@ function tick() 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 if table.getn(rofl.buffers.ACache) > 0 then - minetest.log("action", "{ROFL} ACache has " .. table.getn(rofl.buffers.WCache) .. " elements") - minetest.log("action", "{ROFL} Doing up to " .. rofl.conf.Wpertick .. " writing operations") + minetest.log("action", "{ROFL} ACache has " .. table.getn(rofl.buffers.ACache) .. " elements") + minetest.log("action", "{ROFL} Doing up to " .. rofl.conf.Apertick .. " application operations") for x = 1, rofl.conf.Apertick do 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) end @@ -149,3 +253,23 @@ function tick() end 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)