From ca1adbedbab95e32a1328296df8036541404686d Mon Sep 17 00:00:00 2001 From: HybridDog Date: Fri, 18 Dec 2015 17:33:40 +0100 Subject: [PATCH] very unfinished: luacontroller tool --- README.md | 3 + moremesecons_luacontroller_tool/depends.txt | 2 + moremesecons_luacontroller_tool/init.lua | 157 ++++++++++++++++++ .../moremesecons_luacontroller_tool.png | Bin 0 -> 170 bytes 4 files changed, 162 insertions(+) create mode 100644 moremesecons_luacontroller_tool/depends.txt create mode 100644 moremesecons_luacontroller_tool/init.lua create mode 100644 moremesecons_luacontroller_tool/textures/moremesecons_luacontroller_tool.png diff --git a/README.md b/README.md index cb95968..de217ff 100644 --- a/README.md +++ b/README.md @@ -25,3 +25,6 @@ MoreMesecons is a mod for minetest wich adds some mesecons items. * `Temporary Gate` : If it receives a mesecons signal, whatever its duration, a mesecons signal is send with a fixed duration. You can change it by right-click (in seconds) (you can write for example 0.2 to send a pulse, or 20 to send long signals). * `Wireless` : Place 2 (or more) wireless somewhere. Change their channel by right-click. If you send a signal to a wireless, every wireless wich have the same channel will send the signal. * `Wireless Jammer` : If it receives a mesecons signal, it deactivates all wireless (receptors) in a radius of 15 nodes. + +TODO: +— add luacontroller template tool (see https://forum.minetest.net/viewtopic.php?p=201894#p201894) diff --git a/moremesecons_luacontroller_tool/depends.txt b/moremesecons_luacontroller_tool/depends.txt new file mode 100644 index 0000000..9408116 --- /dev/null +++ b/moremesecons_luacontroller_tool/depends.txt @@ -0,0 +1,2 @@ +mesecons +vector_extras diff --git a/moremesecons_luacontroller_tool/init.lua b/moremesecons_luacontroller_tool/init.lua new file mode 100644 index 0000000..2c61c72 --- /dev/null +++ b/moremesecons_luacontroller_tool/init.lua @@ -0,0 +1,157 @@ +local templates = { + singleplayer = {fir = "daw", mak = "delay()"}, +} + +-- when adding templates minetest.formspec_escape(string) should be used, even for the names +-- this way it doesn't work for multiplayer (missing tests at receiving) +-- formspec, luacontroller identification, saving etc. is unfinished + +-- used for the dropdown formspec element +local function fill_formspec_dropdown_list(t, selected) + local it,num = {},1 + for i in pairs(t) do + it[num] = i + num = num+1 + end + num = num-1 + table.sort(it) + local txt = "" + for i = 1,num do + txt = txt..i -- add available indices + if i ~= num then + txt = txt.."," + end + end + return txt + --spec = string.sub(spec, 1, -2) +end + +local pdata = {} + +local function get_selection_formspec(pname, selected_template) + -- current player name + pname = pname or pdata[pname].player_name + selected_template = selected_template or pdata[pname].template_name + local spec = "size[3,1]".. + + -- show available players, field player_name, current player name is the selected one + "dropdown[0,0;3,1;player_name;".. + fill_formspec_dropdown_list(templates, pname).. + ";"..pname.."]".. + + -- show templates of pname + "dropdown[0,1;3,1;template_name;".. + fill_formspec_dropdown_list(templates[pname], selected_template).. + ";"..selected_template.."]".. + + -- show selected template + "multiline["..templates[pname][selected_template].. + + buttonset.. + + buttonadd.. + + buttonsave + + return spec +end + +local function is_luacontroller(pos) + local node = minetest.get_node(pos) + if node.name ~= ":luacontroller" then + return false + end + +end + +minetest.register_tool("moremesecons_luacontroller_tool:luacontroller_template_tool", { + description = "luacontroller template tool", + inventory_image = "moremesecons_luacontroller_tool.png", + + on_place = function(itemstack, player, pt) + if not player + or not pt then + return + end + + local pos = pt.under + if not is_luacontroller(pos) then + return + end + + local pname = player:get_player_name() + pdata[pname] = { + pos = pos, + player_name = pname, + template_name = next(templates[pname]), + } + minetest.show_formspec(pname, "moremesecons:luacontroller_tool", spec) + + + end, +}) + +minetest.register_on_player_receive_fields(function(player, formname, fields) + if formname ~= "moremesecons:luacontroller_tool" + or fields.quit + or not player then + return + end + + local pname = player:get_player_name() + + if fields.player_name then + -- show available templates of that player + minetest.show_formspec(pname, "moremesecons:luacontroller_tool", + get_selection_formspec(fields.player_name) + ) + pdata[pname].player_name = fields.player_name + return + end + + if fields.template_name then + -- show selected template of that player + minetest.show_formspec(pname, "moremesecons:luacontroller_tool", + get_selection_formspec(nil, fields.template_name) + ) + pdata[pname].template_name = fields.template_name + return + end + + local pos = pdata[pname].pos + if not is_luacontroller(pos) then + -- this can happen + return + end + + local meta = minetest.get_meta(pos) + + if fields.set then + -- replace the code of the luacontroller with the template + meta:set_string("code", templates[pdata[pname].player_name][pdata[pname].template_name]) + minetest.chat_send_player(pname, "code set to template at "..vector.pos_to_string(pos)) + return + end + + if fields.add then + -- add the template to the end of the code of the luacontroller + meta:set_string("code", meta:get_string("code")..templates[pdata[pname].player_name][pdata[pname].template_name]) + minetest.chat_send_player(pname, "code added to luacontroller at "..vector.pos_to_string(pos)) + return + end + + if fields.save then + -- save the template, when you try to change others' templates, yours become changed + local savename = fields.save_name or pdata[pname].template_name + local code = fields.template_code or templates[pdata[pname].player_name][pdata[pname].template_name] + --[[ + if not code then + minetest.chat_send_player(pname, "you can't save if you didn't change the code") + return + end--]] + local template_name = pdata[pname].template_name + templates[pname][template_name] = code + minetest.chat_send_player(pname, "template "..pname.."/"..template_name.." saved") + return + end +end) diff --git a/moremesecons_luacontroller_tool/textures/moremesecons_luacontroller_tool.png b/moremesecons_luacontroller_tool/textures/moremesecons_luacontroller_tool.png new file mode 100644 index 0000000000000000000000000000000000000000..2791ed514ad2f566d7fb04a4068859ae63a700de GIT binary patch literal 170 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!63?wyl`GbKJPk>K|E0E^pH1UH)GVwat=OVW8HCdn~(A|hXZ?4!?@2zTu!r@ zc`HLcgO|OL`(V_(id(*Gmah>%m~qW3{eTJ28)<9v8_)M0+qmqX-o}ISRi7CiNvynK Q2(*O3)78&qol`;+02;(ObN~PV literal 0 HcmV?d00001