summaryrefslogtreecommitdiff
path: root/mesecons_battery/init.lua
blob: 2df22554067896d26a93dd3123e419fbcdfbddf0 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
for i = 1, 5 do
	minetest.register_node("mesecons_battery:battery_charging_"..i, {
		drawtype = "nodebox",
		tiles = {"jeija_battery_charging.png"},
		paramtype = "light",
		is_ground_content = true,
		walkable = true,
		node_box = {
			type = "fixed",
			fixed = {
				{-0.499, -0.499, -0.499, -0.4,   0.499,       0.499},
				{ 0.499, -0.499, -0.499,  0.4,   0.499,       0.499},
				{-0.499, -0.499, -0.499,  0.499, 0.499,      -0.4  },
				{-0.499, -0.499,  0.499,  0.499, 0.499,       0.4  },
				{-0.4  , -0.5  , -0.4  ,  0.4  , 1*(i/5)-0.5, 0.4}}
		},

		selection_box = {
			type = "fixed",
			fixed = {
				{-0.499, -0.499, -0.499, -0.4,   0.499,       0.499},
				{ 0.499, -0.499, -0.499,  0.4,   0.499,       0.499},
				{-0.499, -0.499, -0.499,  0.499, 0.499,      -0.4  },
				{-0.499, -0.499,  0.499,  0.499, 0.499,       0.4  },
				{-0.4  , -0.5  , -0.4  ,  0.4  , 1*(i/5)-0.5, 0.4}}
		},
		groups = {dig_immediate=2},
	    	description="Battery",
	})
	mesecon:add_receptor_node_off("mesecons_battery:battery_charging_"..i)
end

for i = 1, 5 do
	minetest.register_node("mesecons_battery:battery_discharging_"..i, {
		drawtype = "nodebox",
		tiles = {"jeija_battery_discharging.png"},
		paramtype = "light",
		is_ground_content = true,
		walkable = true,
		node_box = {
			type = "fixed",
			fixed = {
				{-0.499, -0.499, -0.499, -0.4,   0.499,       0.499},
				{ 0.499, -0.499, -0.499,  0.4,   0.499,       0.499},
				{-0.499, -0.499, -0.499,  0.499, 0.499,      -0.4  },
				{-0.499, -0.499,  0.499,  0.499, 0.499,       0.4  },
				{-0.4  , -0.5  , -0.4  ,  0.4  , 1*(i/5)-0.5, 0.4}}
		},

		selection_box = {
			type = "fixed",
			fixed = {
				{-0.499, -0.499, -0.499, -0.4,   0.499,       0.499},
				{ 0.499, -0.499, -0.499,  0.4,   0.499,       0.499},
				{-0.499, -0.499, -0.499,  0.499, 0.499,      -0.4  },
				{-0.499, -0.499,  0.499,  0.499, 0.499,       0.4  },
				{-0.4  , -0.5  , -0.4  ,  0.4  , 1*(i/5)-0.5, 0.4}}
		},
		groups = {dig_immediate=2},
	    	description="Battery",
	})
	mesecon:add_receptor_node("mesecons_battery:battery_discharging_"..i)
end

minetest.register_on_placenode(function (pos, newnode, placer)
	meta = minetest.env:get_meta(pos)
	meta:set_int("batterstate", 0)
	meta:set_int("charging", 0)
end)

minetest.register_on_punchnode(function(pos, node, puncher)
	if string.find(node.name, "mesecons_battery:battery_charging") then
		local meta = minetest.env:get_meta(pos);
		local batterystate = meta:get_int("batterystate")
		local charging = meta:get_int("charging")
		minetest.env:add_node(pos, {name=string.gsub(node.name, "charging", "discharging")})
		mesecon:receptor_on(pos)
		meta:set_int("batterystate", batterystate)
		meta:set_int("charging", charging)
	end
	if string.find(node.name, "mesecons_battery:battery_discharging") then
		local meta = minetest.env:get_meta(pos);
		local batterystate = meta:get_int("batterystate")
		local charging = meta:get_int("charging")
		minetest.env:add_node(pos, {name=string.gsub(node.name, "discharging", "charging")})
		mesecon:receptor_off(pos)
		meta:set_int("batterystate", batterystate)
		meta:set_int("charging", charging)
	end
end)

minetest.register_abm({
nodenames = {"mesecons_battery:battery_charging_1", "mesecons_battery:battery_charging_2", "mesecons_battery:battery_charging_3", "mesecons_battery:battery_charging_4", "mesecons_battery:battery_charging_5"},
	interval = 1,
	chance = 1,
	action = function(pos, node, active_object_count, active_object_count_wider)
		local meta = minetest.env:get_meta(pos);
		if meta:get_int("charging") == 1 then
			local batterystate = meta:get_int("batterystate")
			local charging = meta:get_int("charging")
			local name = node.name;
			if batterystate < 100 then --change battery charging state
				batterystate = batterystate + 1
			end

			if string.find(node.name, tostring(math.ceil(batterystate/20))) == nil then
				node.name = string.gsub(node.name, tostring(math.ceil(batterystate/20)-1), tostring(math.ceil(batterystate/20))) --change node for new nodebox model
			end
			minetest.env:add_node(pos, node)
			meta:set_int("batterystate", batterystate)
			meta:set_int("charging", charging)
		end
	end,
})

mesecon:register_on_signal_on(function(pos, node)
	if string.find(node.name, "mesecons_battery:battery") then
		minetest.env:get_meta(pos):set_int("charging", 1)
	end
end)

mesecon:register_on_signal_off(function(pos, node)
	if string.find(node.name, "mesecons_battery:battery") then
		minetest.env:get_meta(pos):set_int("charging", 0)
	end
end)

minetest.register_on_punchnode(function(pos, node, puncher)
	print(minetest.env:get_meta(pos):get_int("batterystate"))
end)