summaryrefslogtreecommitdiff
path: root/firealarm_common/init.lua
blob: ae2a2a0a8140df789ba2e6d108112e6114f782c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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()