From c1759eac9c4fa2bb335c5b01b91a99121c5174a8 Mon Sep 17 00:00:00 2001 From: cheapie Date: Thu, 4 Apr 2024 18:06:20 -0500 Subject: Convert PIs and lanterns to new communication system Existing PIs/lanterns will need to be removed and replaced --- controller.lua | 31 ++++++++--------- pilantern.lua | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 117 insertions(+), 19 deletions(-) diff --git a/controller.lua b/controller.lua index 3fde24f..cc4f68d 100644 --- a/controller.lua +++ b/controller.lua @@ -453,10 +453,9 @@ function celevator.controller.finish(pos,mem,changedinterrupts) local oldpitext = oldmem.pifloor or "--" local newpitext = mem.pifloor or "--" if oldpitext ~= newpitext then - local pis = minetest.deserialize(meta:get_string("pis")) or {} - for hash in pairs(pis) do - local pipos = minetest.get_position_from_hash(hash) - celevator.pi.settext(pipos,newpitext) + local pis = carinfo.pis + for _,pi in pairs(pis) do + celevator.pi.settext(pi.pos,newpitext) end end local oldpiuparrow = oldmem.piuparrow @@ -464,26 +463,24 @@ function celevator.controller.finish(pos,mem,changedinterrupts) local oldpidownarrow = oldmem.pidownarrow local newpidownarrow = mem.pidownarrow if oldpiuparrow ~= newpiuparrow then - local pis = minetest.deserialize(meta:get_string("pis")) or {} - for hash in pairs(pis) do - local pipos = minetest.get_position_from_hash(hash) - celevator.pi.setarrow(pipos,"up",newpiuparrow) + local pis = carinfo.pis + for _,pi in pairs(pis) do + celevator.pi.setarrow(pi.pos,"up",newpiuparrow) end end if oldpidownarrow ~= newpidownarrow then - local pis = minetest.deserialize(meta:get_string("pis")) or {} - for hash in pairs(pis) do - local pipos = minetest.get_position_from_hash(hash) - celevator.pi.setarrow(pipos,"down",newpidownarrow) + local pis = carinfo.pis + for _,pi in pairs(pis) do + celevator.pi.setarrow(pi.pos,"down",newpidownarrow) end end local oldlanterns = oldmem.lanterns or {} local newlanterns = mem.lanterns or {} - local lanterns = minetest.deserialize(meta:get_string("lanterns")) or {} - for hash,landing in pairs(lanterns) do - if oldlanterns[landing] ~= newlanterns[landing] then - celevator.lantern.setlight(minetest.get_position_from_hash(hash),"up",newlanterns[landing] == "up") - celevator.lantern.setlight(minetest.get_position_from_hash(hash),"down",newlanterns[landing] == "down") + local lanterns = carinfo.lanterns + for _,lantern in pairs(lanterns) do + if oldlanterns[lantern.landing] ~= newlanterns[lantern.landing] then + celevator.lantern.setlight(lantern.pos,"up",newlanterns[lantern.landing] == "up") + celevator.lantern.setlight(lantern.pos,"down",newlanterns[lantern.landing] == "down") end end meta:set_string("mem",minetest.serialize(mem)) diff --git a/pilantern.lua b/pilantern.lua index 55f2867..8986209 100644 --- a/pilantern.lua +++ b/pilantern.lua @@ -136,7 +136,36 @@ minetest.register_node("celevator:pi",{ {-0.25,-0.453,0.475,0.25,-0.125,0.5}, }, }, - on_destruct = celevator.pi.removeentity, + after_place_node = function(pos) + local meta = minetest.get_meta(pos) + meta:set_string("formspec","formspec_version[7]size[8,5]field[0.5,0.5;7,1;carid;Car ID;]button[3,3.5;2,1;save;Save]") + end, + on_receive_fields = function(pos,_,fields) + if tonumber(fields.carid) then + local carid = tonumber(fields.carid) + local carinfo = minetest.deserialize(celevator.storage:get_string(string.format("car%d",carid))) + if not carinfo then return end + table.insert(carinfo.pis,{pos=pos}) + celevator.storage:set_string(string.format("car%d",carid),minetest.serialize(carinfo)) + local meta = minetest.get_meta(pos) + meta:set_int("carid",carid) + meta:set_string("formspec","") + end + end, + on_destruct = function(pos) + celevator.pi.removeentity(pos) + local meta = minetest.get_meta(pos) + local carid = meta:get_int("carid") + if carid == 0 then return end + local carinfo = minetest.deserialize(celevator.storage:get_string(string.format("car%d",carid))) + if not carinfo then return end + for i,pi in pairs(carinfo.pis) do + if vector.equals(pos,pi.pos) then + table.remove(carinfo.pis,i) + celevator.storage:set_string(string.format("car%d",carid),minetest.serialize(carinfo)) + end + end + end, on_construct = function(pos) local meta = minetest.get_meta(pos) meta:set_string("text","--") @@ -279,7 +308,6 @@ for _,state in ipairs(validstates) do }, paramtype = "light", paramtype2 = "facedir", - on_destruct = celevator.pi.removeentity, drawtype = "nodebox", node_box = { type = "fixed", @@ -287,6 +315,49 @@ for _,state in ipairs(validstates) do {-0.25,-0.5,0.475,0.25,0.125,0.5}, }, }, + after_place_node = function(pos) + local meta = minetest.get_meta(pos) + meta:set_string("formspec","formspec_version[7]size[8,5]field[0.5,0.5;7,1;carid;Car ID;]field[0.5,2;7,1;landing;Landing Number;]button[3,3.5;2,1;save;Save]") + end, + on_receive_fields = function(pos,_,fields) + if tonumber(fields.carid) and tonumber(fields.landing) then + local carid = tonumber(fields.carid) + local landing = tonumber(fields.landing) + local carinfo = minetest.deserialize(celevator.storage:get_string(string.format("car%d",carid))) + if not carinfo then return end + table.insert(carinfo.pis,{pos=pos}) + table.insert(carinfo.lanterns,{pos=pos,landing=landing}) + celevator.storage:set_string(string.format("car%d",carid),minetest.serialize(carinfo)) + local meta = minetest.get_meta(pos) + meta:set_int("carid",carid) + meta:set_string("formspec","") + end + end, + on_destruct = function(pos) + celevator.pi.removeentity(pos) + local meta = minetest.get_meta(pos) + local carid = meta:get_int("carid") + if carid == 0 then return end + local carinfo = minetest.deserialize(celevator.storage:get_string(string.format("car%d",carid))) + if not carinfo then return end + for i,pi in pairs(carinfo.pis) do + if vector.equals(pos,pi.pos) then + table.remove(carinfo.pis,i) + celevator.storage:set_string(string.format("car%d",carid),minetest.serialize(carinfo)) + end + end + for i,lantern in pairs(carinfo.lanterns) do + if vector.equals(pos,lantern.pos) then + table.remove(carinfo.lanterns,i) + celevator.storage:set_string(string.format("car%d",carid),minetest.serialize(carinfo)) + end + end + end, + on_construct = function(pos) + local meta = minetest.get_meta(pos) + meta:set_string("text","--") + celevator.pi.updatedisplay(pos) + end, }) nname = "celevator:lantern_"..state[1] dropname = nname @@ -314,6 +385,36 @@ for _,state in ipairs(validstates) do boringside, makelanterntex(state[1],state[2],state[3]) }, + after_place_node = function(pos) + local meta = minetest.get_meta(pos) + meta:set_string("formspec","formspec_version[7]size[8,5]field[0.5,0.5;7,1;carid;Car ID;]field[0.5,2;7,1;landing;Landing Number;]button[3,3.5;2,1;save;Save]") + end, + on_receive_fields = function(pos,_,fields) + if tonumber(fields.carid) and tonumber(fields.landing) then + local carid = tonumber(fields.carid) + local landing = tonumber(fields.landing) + local carinfo = minetest.deserialize(celevator.storage:get_string(string.format("car%d",carid))) + if not carinfo then return end + table.insert(carinfo.lanterns,{pos=pos,landing=landing}) + celevator.storage:set_string(string.format("car%d",carid),minetest.serialize(carinfo)) + local meta = minetest.get_meta(pos) + meta:set_int("carid",carid) + meta:set_string("formspec","") + end + end, + on_destruct = function(pos) + local meta = minetest.get_meta(pos) + local carid = meta:get_int("carid") + if carid == 0 then return end + local carinfo = minetest.deserialize(celevator.storage:get_string(string.format("car%d",carid))) + if not carinfo then return end + for i,lantern in pairs(carinfo.lanterns) do + if vector.equals(pos,lantern.pos) then + table.remove(carinfo.lanterns,i) + celevator.storage:set_string(string.format("car%d",carid),minetest.serialize(carinfo)) + end + end + end, paramtype = "light", paramtype2 = "facedir", drawtype = "nodebox", -- cgit v1.2.3