From df75dc7b7ec03a3b30052efbea988c3ab2b8f430 Mon Sep 17 00:00:00 2001 From: cheapie Date: Fri, 24 Aug 2018 16:19:53 -0500 Subject: Initial commit --- firealarm_common/init.lua | 61 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 firealarm_common/init.lua (limited to 'firealarm_common/init.lua') diff --git a/firealarm_common/init.lua b/firealarm_common/init.lua new file mode 100644 index 0000000..ae2a2a0 --- /dev/null +++ b/firealarm_common/init.lua @@ -0,0 +1,61 @@ +firealarm = {} + +firealarm.devices = { + panel = {}, + signaling = {}, + notification = {}, +} + +function firealarm.loadNode(pos) + minetest.forceload_block(pos,true) +end + +function firealarm.loadDevLists() + local path = minetest.get_worldpath()..DIR_DELIM.."firealarm_devices" + local file = io.open(path,"r") + if not file then + minetest.log("error","Unable to open fire alarm devices table for reading. ".. + "This is normal on the first start.") + firealarm.saveDevLists() + return + end + local serdata = file:read("*a") + file:close() + local data = minetest.deserialize(serdata) + if type(data) == "table" then + firealarm.devices = data + else + error("Fire alarm devices table is corrupted or contains invalid data") + end +end + +function firealarm.saveDevLists() + local path = minetest.get_worldpath()..DIR_DELIM.."firealarm_devices" + local file = io.open(path,"w") + if not file then + minetest.log("error","Unable to open fire alarm devices table for writing") + return + end + local serdata = minetest.serialize(firealarm.devices) + file:write(serdata) + file:close() +end + +function firealarm.getDevInfo(devType,pos) + if devType ~= "panel" and devType ~= "signaling" and devType ~= "notification" then + error("Invalid device type specified") + end + local hash = minetest.hash_node_position(pos) + return firealarm.devices[devType][hash] +end + +function firealarm.setDevInfo(devType,pos,info) + if devType ~= "panel" and devType ~= "signaling" and devType ~= "notification" then + error("Invalid device type specified") + end + local hash = minetest.hash_node_position(pos) + firealarm.devices[devType][hash] = info + firealarm.saveDevLists() +end + +firealarm:loadDevLists() -- cgit v1.2.3