summaryrefslogtreecommitdiff
path: root/firealarm_common/init.lua
blob: 622b2f2c1d367ec2a54f03e38d7d15b4a9f340e2 (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
62
63
firealarm = {}

firealarm.devices = {
	panel = {},
	signaling = {},
	notification = {},
	annunciator = {},
}

function firealarm.loadNode(pos)
	minetest.forceload_block(pos,true)
end

function firealarm.loadDevLists()
	local path = minetest.get_worldpath().."/firealarm_devices"
	local file = io.open(path,"r")
	if not file then
		minetest.log("warning","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
		if not data.annunciator then data.annunciator = {} end
		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().."/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" and devType ~= "annunciator" 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" and devType ~= "annunciator" then
		error("Invalid device type specified")
	end
	local hash = minetest.hash_node_position(pos)
	firealarm.devices[devType][hash] = info
	firealarm.saveDevLists()
end

firealarm:loadDevLists()