From 454d3812ed54fccb7cab9b8011ae570b56cd1c3f Mon Sep 17 00:00:00 2001 From: Coder12a <38924418+Coder12a@users.noreply.github.com> Date: Fri, 9 Nov 2018 15:54:41 -0600 Subject: [PATCH] Update. New queue_task and chain_task. bug fix in node_funcs. And add text to readme. --- README.md | 90 +++++++++++++++++++++++++++++++++++++++++++++++++- api.txt | 43 +++++++++++++++++------- async.lua | 48 ++++++++++++++++++++++++--- node_funcs.lua | 32 +++++++++++++----- 4 files changed, 187 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 57616e5..2147b2b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,92 @@ # extended_api mod for minetest -This mod adds more functions and features to the minetest api. \ No newline at end of file +This mod adds more functions and features to the minetest api. + +extended_api +=========== + +extended_api is a minetest mod that extends the current minetest api. +It adds two new node events and contains async functions. + +Usage Async +=========== +1. create a async pool. +```lua +pool = minetest.Async.create_async_pool() +``` +2. set the priority of the async pool to high. +```lua +minetest.Async.priority(pool,50,500) +``` +3. iterate from 1 to 50 and log the value i. +```lua +minetest.Async.iterate(pool,1,50,function(i) + minetest.log(i) +end) +``` +4. run throught each element in a table. +```lua +local array = {"start","text2","text3","text4","text5","end"} +minetest.Async.foreach(pool,array, function(k,v) + minetest.log(v) +end) +``` +5. async do while loop. +```lua +local c = 50 +minetest.Async.do_while(pool,function() return c>0 end, function() + minetest.log(c) + c = c - 1 +end) +``` +6. register a async globalstep. this one spams the chat with the word spam. +```lua +minetest.Async.register_globalstep(pool,function(dtime) + minetest.chat_send_all("spam") +end) +``` +7. chain task runs a group of functions from a table. +```lua +minetest.Async.chain_task(pool,{ + function(args) + args.count = 1 + minetest.log(args.count) + return args + end, + function(args) + args.count = args.count + 1 + minetest.log(args.count) + return args + end} +) +``` +8. adds a single function to the task queue. This is a sort of waiting list. +```lua + minetest.Async.queue_task(pool,function() + minetest.log("Hello World!") + end) +``` +Usage Node +=========== +1. this covers both functions. I made this for a way to awake node timers without abms. +```lua +minetest.register_node("default:stone", { + description = "Stone", + tiles = {"default_stone.png"}, + groups = {cracky = 3, stone = 1}, + drop = 'default:cobble', + legacy_mineral = true, + sounds = default.node_sound_stone_defaults(), + on_construct_node_near_by = function(pos,other_pos,name) + if name == "tnt:tnt" then + minetest.chat_send_all("Do not place tnt near me thank you!") + end + end, + on_destruct_node_near_by = function(pos,other_pos,name) + if name == "default:dirt" then + minetest.chat_send_all("I hate dirt too!") + end + end, +}) +``` \ No newline at end of file diff --git a/api.txt b/api.txt index c9026b9..9594b8c 100644 --- a/api.txt +++ b/api.txt @@ -1,20 +1,39 @@ -- node_funcs --- Function happens when a node's constructer function is ran in a 3x3x3 near by. -on_construct_node_near_by(pos,other_pos) --- Function happens when a node's destructer function is ran 3x3x3 near by. -on_destruct_node_near_by(pos,other_pos) +-- Function happens when a node's constructer function is ran near by. +on_construct_node_near_by(pos,other_pos,name) +-- Function happens when a node's destructer function is ran near by. +on_destruct_node_near_by(pos,other_pos,name) -- async --- +-- Create's a async pool. minetest.Async.create_async_pool() --- return pool --- +-- Set the priority of the given async pool. +-- resting is the delay between resuming threads. +-- maxtime is how long a thread will work before yield. minetest.Async.priority(pool,resting,maxtime) --- +-- Async for loop. +-- from is the starting value for the loop. +-- to is the ending value to will break the loop. +-- func(i) is the function that is ran in the for loop. func can break the loop if it returns false. +-- callback() function is ran when the for loop ends. callback can be nil. minetest.Async.iterate(pool,from,to,func,callback) --- +-- Async foreach loop. +-- array is the list to loop throught. +-- func(k,v) is the function that is ran in the foreach loop. func can break the loop if it returns false. +-- callback() function is ran when the foreach loop ends. callback can be nil. minetest.Async.foreach(pool,array, func, callback) --- +-- Async do while loop. +-- condition_func() returns the condition. +-- func() is the function that is ran in the do while loop. func can break the loop if it returns false. +-- callback() function is ran when the do while loop ends. callback can be nil. minetest.Async.do_while(pool,condition_func, func, callback) --- +-- Async globalstep loop. this can never be shutdown like in the minetest version. happens every 0.05 seconds. +-- func(dtime) is the function that is ran in the globalstep. minetest.Async.register_globalstep(pool,func) --- -minetest.Async.queue_task(pool,tasks,callback) \ No newline at end of file +-- Async chain task. +-- tasks is a table of functions that ran in order. +-- callback() function is ran when the chain_task ends. callback can be nil. +minetest.Async.chain_task(pool,tasks,callback) +-- Async task queue. +-- func is a single function that gets added to the queue list. +-- callback() function is ran when the queue_task ends. callback can be nil. +minetest.Async.queue_task(pool,func,callback) \ No newline at end of file diff --git a/async.lua b/async.lua index 6a148e4..b6ca34d 100644 --- a/async.lua +++ b/async.lua @@ -1,7 +1,7 @@ minetest.Async = {} function minetest.Async.create_async_pool() - local pool = {threads = {},globalstep_threads = {},resting = 200,maxtime = 200,state = "suspended"} + local pool = {threads = {},globalstep_threads = {},task_queue = {},resting = 200,maxtime = 200,queue_threads = 8,state = "suspended"} return pool end @@ -69,7 +69,10 @@ function minetest.Async.iterate(pool,from,to,func,callback) local last_time = minetest.get_us_time() * 1000 local maxtime = pool.maxtime for i = from, to do - func(i) + local b = func(i) + if b and b == false then + break + end if minetest.get_us_time() * 1000 > last_time + maxtime then coroutine.yield() last_time = minetest.get_us_time() * 1000 @@ -87,7 +90,10 @@ function minetest.Async.foreach(pool,array, func, callback) local last_time = minetest.get_us_time() * 1000 local maxtime = pool.maxtime for k,v in ipairs(array) do - func(k,v) + local b = func(k,v) + if b and b == false then + break + end if minetest.get_us_time() * 1000 > last_time + maxtime then coroutine.yield() last_time = minetest.get_us_time() * 1000 @@ -141,7 +147,7 @@ function minetest.Async.register_globalstep(pool,func) minetest.Async.schedule_globalstep_worker(pool) end -function minetest.Async.queue_task(pool,tasks,callback) +function minetest.Async.chain_task(pool,tasks,callback) minetest.Async.create_worker(pool,function() local pass_arg = {} local last_time = minetest.get_us_time() * 1000 @@ -161,4 +167,38 @@ function minetest.Async.queue_task(pool,tasks,callback) end end) minetest.Async.schedule_worker(pool) +end + +function minetest.Async.queue_task(pool,func,callback) + table.insert(pool.task_queue,{func = func,callback = callback}) + if pool.queue_threads > 0 then + pool.queue_threads = pool.queue_threads - 1 + minetest.Async.create_worker(pool,function() + local pass_arg = {} + local last_time = minetest.get_us_time() * 1000 + local maxtime = pool.maxtime + while(true) do + local task_func = pool.task_queue[1] + if task_func and task_func.func then + table.remove(pool.task_queue,1) + pass_arg = {} + local p = task_func.func(pass_arg) + if p then + pass_arg = p + end + if task_func.callback then + task_func.callback(pass_arg) + end + if minetest.get_us_time() * 1000 > last_time + maxtime then + coroutine.yield() + last_time = minetest.get_us_time() * 1000 + end + else + pool.queue_threads = pool.queue_threads + 1 + break + end + end + end) + minetest.Async.schedule_worker(pool) + end end \ No newline at end of file diff --git a/node_funcs.lua b/node_funcs.lua index c64ddcd..0040b0f 100644 --- a/node_funcs.lua +++ b/node_funcs.lua @@ -1,4 +1,3 @@ --- on_construct_node_near_by(pos,constructed_pos) local function on_construct_override(pos) local lpos = pos local pos1 = {x=lpos.x-1,y=lpos.y-1,z=lpos.z-1} @@ -25,6 +24,10 @@ local function on_construct_override(pos) local n2z = pos2.z local data = vm:get_data() + + local m_vi = a:index(nx, ny, nz) + local myname = minetest.get_name_from_content_id(data[m_vi]) + for z = n1z, n2z do for y = n1y, n2y do for x = n1x, n2x do @@ -33,7 +36,7 @@ local function on_construct_override(pos) local name = minetest.get_name_from_content_id(data[vi]) local node = minetest.registered_nodes[name] if node.on_construct_node_near_by then - node.on_construct_node_near_by({x=x,y=y,z=z},lpos) + node.on_construct_node_near_by({x=x,y=y,z=z},lpos,myname) end end end @@ -67,6 +70,10 @@ local function on_construct_override2(pos) local n2z = pos2.z local data = vm:get_data() + + local m_vi = a:index(nx, ny, nz) + local myname = minetest.get_name_from_content_id(data[m_vi]) + for z = n1z, n2z do for y = n1y, n2y do for x = n1x, n2x do @@ -75,7 +82,7 @@ local function on_construct_override2(pos) local node = minetest.registered_nodes[name] if x ~= nx or y ~= ny or z ~= nz then if node.on_construct_node_near_by then - node.on_construct_node_near_by({x=x,y=y,z=z},lpos) + node.on_construct_node_near_by({x=x,y=y,z=z},lpos,myname) end else node.exa_old_on_construct(lpos) @@ -86,7 +93,6 @@ local function on_construct_override2(pos) end -- End --- on_destruct_node_near_by(pos,destructed_pos) local function on_destruct_override(pos) local lpos = pos local pos1 = {x=lpos.x-1,y=lpos.y-1,z=lpos.z-1} @@ -113,6 +119,10 @@ local function on_destruct_override(pos) local n2z = pos2.z local data = vm:get_data() + + local m_vi = a:index(nx, ny, nz) + local myname = minetest.get_name_from_content_id(data[m_vi]) + for z = n1z, n2z do for y = n1y, n2y do for x = n1x, n2x do @@ -121,7 +131,7 @@ local function on_destruct_override(pos) local name = minetest.get_name_from_content_id(data[vi]) local node = minetest.registered_nodes[name] if node.on_destruct_node_near_by then - node.on_destruct_node_near_by({x=x,y=y,z=z},lpos) + node.on_destruct_node_near_by({x=x,y=y,z=z},lpos,myname) end end end @@ -155,6 +165,10 @@ local function on_destruct_override2(pos) local n2z = pos2.z local data = vm:get_data() + + local m_vi = a:index(nx, ny, nz) + local myname = minetest.get_name_from_content_id(data[m_vi]) + for z = n1z, n2z do for y = n1y, n2y do for x = n1x, n2x do @@ -163,7 +177,7 @@ local function on_destruct_override2(pos) local node = minetest.registered_nodes[name] if x ~= nx or y ~= ny or z ~= nz then if node.on_destruct_node_near_by then - node.on_destruct_node_near_by({x=x,y=y,z=z},lpos) + node.on_destruct_node_near_by({x=x,y=y,z=z},lpos,myname) end else node.exa_old_on_destruct(lpos) @@ -179,7 +193,7 @@ function() for n, d in pairs(minetest.registered_nodes) do local cn = {} for k,v in pairs(minetest.registered_nodes[n]) do cn[k] = v end - -- on_construct_node_near_by(pos,other_pos) + -- on_construct_node_near_by(pos,other_pos,name) local on_con = cn.on_construct if on_con then cn.exa_old_on_construct = on_con @@ -188,7 +202,7 @@ function() on_con = on_construct_override end cn.on_construct = on_con - -- on_destruct_node_near_by(pos,other_pos) + -- on_destruct_node_near_by(pos,other_pos,name) local on_dis = cn.on_destruct if on_dis then cn.exa_old_on_destruct = on_dis @@ -196,7 +210,7 @@ function() else on_dis = on_destruct_override end - cn.on_destruct = on_con + cn.on_destruct = on_dis minetest.register_node(":"..n,cn) end end) \ No newline at end of file