From 4e6e431efdd46d8157ddeae809bf375d39823293 Mon Sep 17 00:00:00 2001 From: rnd Date: Tue, 24 Jul 2018 16:04:22 +0200 Subject: [PATCH] new demo scripts --- init.lua | 4 +- scripts/gui/craft_guide.lua | 108 ++++++++++++++++++++++ scripts/gui/gui_deposit_withdraw_demo.lua | 82 ++++++++++++++++ scripts/server mods/chatlog.lua | 50 ++++++++++ 4 files changed, 242 insertions(+), 2 deletions(-) create mode 100644 scripts/gui/craft_guide.lua create mode 100644 scripts/gui/gui_deposit_withdraw_demo.lua create mode 100644 scripts/server mods/chatlog.lua diff --git a/init.lua b/init.lua index 4449c40..2e69cef 100644 --- a/init.lua +++ b/init.lua @@ -472,7 +472,7 @@ function getSandboxEnv (name) maxn = table.maxn, remove = table.remove, sort = table.sort, - }, + } env.code.run = function(script) if basic_robot.data[name].authlevel < 3 then @@ -1936,7 +1936,7 @@ end minetest.register_craftitem("basic_robot:control", { description = "Robot remote control", inventory_image = "control.png", - groups = {book = 1, not_in_creative_inventory = 1}, + groups = {book = 1}, --not_in_creative_inventory = 1 stack_max = 1, on_secondary_use = function(itemstack, user, pointed_thing) diff --git a/scripts/gui/craft_guide.lua b/scripts/gui/craft_guide.lua new file mode 100644 index 0000000..fc088e1 --- /dev/null +++ b/scripts/gui/craft_guide.lua @@ -0,0 +1,108 @@ +-- ROBOT craft guide by rnd, 2017 +if not list then + + tname = "rnd"; + list = {}; + tmplist = _G.minetest.registered_items; + for k,v in pairs(tmplist) do + local texture = v.inventory_image or ""; + if texture=="" and v.tiles then texture = v.tiles[1] or "" end + if (not v.groups.not_in_craft_guide or v.groups.not_in_craft_guide == 0) and type(texture)=="string" and texture~="" then + list[#list+1] = {_G.minetest.formspec_escape(k),_G.minetest.formspec_escape(v.description),_G.minetest.formspec_escape(texture)}; -- v.inventory_image, k, v.description + end + end + + + idx = 1; n = 35; row = 6; size = 1.25; + filter = "" item = "" recipeid = 1 + filterlist = {}; for i = 1,#list do filterlist[i] = i end + + get_texture = function(ritem) + local v = _G.minetest.registered_items[ritem]; if not v then return "" end + local texture = v.inventory_image or ""; + if texture=="" and v.tiles then texture = v.tiles[1] or "" end + if type(texture)~="string" then return "" end + return texture + end + + get_form = function() + local form = "size[7.5,8.5]"; + local x,y,i; local idxt = idx+n; if idxt > #filterlist then idxt = #filterlist end + for i = idx, idxt do + local id = filterlist[i]; + if list[id] and list[id][3] then + x = ((i-idx) % row) + y = (i-idx-x)/row; + form = form .. "image_button[".. x*size ..",".. y*size+0.75 .. ";"..size.."," .. size .. ";" .. list[id][3] ..";".."item"..";".. list[id][1] .."]" + end + end + form = form .. "textarea[0.25,0;2,0.75;filter;filter;"..filter .. "]" .. "button[2.,0;1,0.5;search;search]".. + "button[5.5,0;1,0.5;prev;PREV]" .. "button[6.5,0;1,0.5;next;NEXT]" .. "label[4,0;".. idx .. "-"..idxt .. "/" .. #filterlist.."]"; + return form + end + + get_recipe = function() + local form = "size[7.5,8.5]"; + local recipes = _G.minetest.get_all_craft_recipes(item); if not recipes then return end; + local recipe = recipes[recipeid]; if not recipe then return end + local items = recipe.items + local x,y,i; + for i = 0, 8 do + local ritem = items[i+1] or ""; local sritem = ""; + local j = string.find(ritem,":"); if j then sritem = string.sub(ritem,j+1) end; --ritem = _G.minetest.formspec_escape(ritem); + x = (i % 3) + y = (i-x)/3; + form = form .. "image_button[".. x*size ..",".. y*size+0.75 .. ";"..size.."," .. size .. ";" .. get_texture(ritem) ..";".."item"..";".. sritem .."]" + end + form = form .. "textarea[0.25,0;2,0.75;recipeid;recipeid ".. #recipes .. ";"..recipeid .. "]" .. "button[2.,0;1,0.5;go;go]".. + "label[3,0;" .. item .. "]" .. "button[6.5,0;1,0.5;back;BACK]" ; + return form + end + + s=0 +end + +if s==0 then + local p = find_player(4); s = 1 + if p then + self.show_form(p[1],get_form()) + else + self.remove() + end +end + + +sender,fields = self.read_form() +if sender then + + if fields.search then + filter = fields.filter or "" + filterlist = {}; + for i = 1,#list do + if string.find(list[i][1],filter) then filterlist[#filterlist+1] = i end + end + idx=1;self.show_form(sender,get_form()) + + elseif fields.prev then + idx = idx - n; if idx<1 then idx =#filterlist-n end + self.show_form(sender,get_form()) + elseif fields.next then + idx = idx+n; if idx > #filterlist then idx = 1 end + self.show_form(sender,get_form()) + elseif fields.back then + self.show_form(sender,get_form()) + elseif fields.recipeid then + recipeid = tonumber(fields.recipeid) or 1; + self.show_form(sender,get_recipe()) + elseif fields.item then + item = fields.item; + local recipes = _G.minetest.get_all_craft_recipes(item); + local count = 0; if recipes then count = #recipes end + if count>0 then + recipeid = 1 + self.show_form(sender,get_recipe() or "") + end + elseif fields.quit then + self.remove() + end +end \ No newline at end of file diff --git a/scripts/gui/gui_deposit_withdraw_demo.lua b/scripts/gui/gui_deposit_withdraw_demo.lua new file mode 100644 index 0000000..3c87489 --- /dev/null +++ b/scripts/gui/gui_deposit_withdraw_demo.lua @@ -0,0 +1,82 @@ + +if not init then init = true + + deposit = function(pname) + _, text = book.read(1); + local data = deserialize(text) or {}; + + local player = _G.minetest.get_player_by_name(pname) + local inv = player:get_inventory(); + local pstack = inv:get_stack("main", 1); + local iname = pstack:to_string() + + + item, count = _G.string.match(iname,"(%S+)%s?(%d*)"); + item = item or "";if item == "" then return end + + count = tonumber(count) or 1; + + --say("item " .. item .. ", count " .. count) + + data[pname] = data[pname] or {}; + local pdata = data[pname]; + pdata[item] = (pdata[item] or 0) + count + + inv:set_stack("main", 1, _G.ItemStack("")) + + book.write(1,"",serialize(data)) + local form = "size [5,5] label[0,0; You deposited " .. item .. "( " .. count .. " pieces)]" + self.show_form(pname, form) + --say(pname .. " deposited " .. item .. "( " .. count .. " pieces) ") + end + + check = function(pname) + _, text = book.read(1); + local data = deserialize(text) or {}; + data[pname] = data[pname] or {}; + + --say(serialize(data[pname])) + local text = serialize(data[pname]) + local form = "size[5,5] textarea[0,0;6,6;STORAGE;STORAGE;".. + "YOU HAVE STORED FOLLOWING ITEMS:\n\n".. minetest.formspec_escape(text) .. "\n\nUse WITHDRAW to get items back]" + self.show_form(pname, form) + --say(pname .. " deposited " .. item .. "( " .. count .. " pieces) ") + end + + withdraw = function(pname) + _, text = book.read(1); + local data = deserialize(text) or {}; + data[pname] = data[pname] or {}; + + local player = _G.minetest.get_player_by_name(pname) + local inv = player:get_inventory(); + local pdata = data[pname] + for k,v in pairs(pdata) do + inv:add_item("main", _G.ItemStack(k .. " " .. v)) + end + data[pname] = nil; + book.write(1,"",serialize(data)) + end + + + local players = find_player(4) + if not players then self.remove() end + + pname = players[1] + + local form = "size [5,5] button[0,0;2,1;DEPOSIT;DEPOSIT] button[0,1;2,1;CHECK;CHECK] button[0,2;2,1;WITHDRAW;WITHDRAW]" + self.show_form(pname, form) +end + +sender,fields = self.read_form() +if sender then + if fields.DEPOSIT then + deposit(sender) + elseif fields.CHECK then + check(sender) + elseif fields.WITHDRAW then + withdraw(sender) + end + + --say(sender .. " clicked " .. serialize(fields)) +end diff --git a/scripts/server mods/chatlog.lua b/scripts/server mods/chatlog.lua new file mode 100644 index 0000000..2589db6 --- /dev/null +++ b/scripts/server mods/chatlog.lua @@ -0,0 +1,50 @@ +--rnd 2017 +if not logdata then + self.label("chatlog bot"); + _G.minetest.forceload_block(self.pos(),true) + n = 500; -- store so many messsages before repeating + maxresults = 100 -- display at most 'this' result + + logdata = {}; -- circular array to hold messages + idx = 1; + insert_log = function(logdata,text) -- store new message + idx = idx +1; + if idx > n then idx = 1 end + logdata[idx] = text; + end + + retrieve_log = function(logdata,count,filter) -- return last k messages, with filter only selected messages + + local k = 0; + local i=idx; local j=0; local ret = {} + + for j = 1,n do + if not logdata[i] then break end + if filter and not string.find(logdata[i], filter) then + else + ret[#ret+1] = logdata[i] + k=k+1 + if k>=count then break end -- enough results + end + i=i-1; if i < 1 then i = n end + end + return table.concat(ret,"\n") + end + + self.listen(1) +end + +speaker, msg = self.listen_msg() +if msg then + if string.sub(msg,1,4) == "?log" then + local j = string.find(msg," ",7); -- find first argument + local k;local text; + if j then k = tonumber(string.sub(msg,6,j-1)) else k = tonumber(string.sub(msg,6)) end -- if there was first argument find second + k = k or maxresults; + if j then text = retrieve_log(logdata,k,string.sub(msg,j+1)) else text = retrieve_log(logdata,k) end + local form = "size[8,8]".. "textarea[0.,0;11.,9.5;text;chatlog;".. text .. "]" + self.show_form(speaker, form) + else + insert_log(logdata, os.date("%X") .. " " .. speaker .. "> " .. msg) + end +end \ No newline at end of file