summaryrefslogtreecommitdiff
path: root/nodes/node_battery_holder.lua
blob: d29e02143fff22ec7b13d0b10aeed77e62a6a36f (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
-- internationalization boilerplate
local MP = minetest.get_modpath(minetest.get_current_modname())
local S, NS = dofile(MP.."/intllib.lua")


-- Battery storage. Controller node draws electrical power from here.
-- Note that batttery boxes are digtron group 7.

local battery_holder_formspec = 
	"size[8,9.3]" ..
	default.gui_bg ..
	default.gui_bg_img ..
	default.gui_slots ..
	"label[0,0;" .. S("Batteries") .. "]" ..
	"list[current_name;batteries;0,0.6;8,4;]" ..
	"list[current_player;main;0,5.15;8,1;]" ..
	"list[current_player;main;0,6.38;8,3;8]" ..
	"listring[current_name;batteries]" ..
	"listring[current_player;main]" ..
	default.get_hotbar_bg(0,5.15)


minetest.register_node("digtron:battery_holder", {
	description = S("Digtron Battery Holder"),
	_doc_items_longdesc = digtron.doc.battery_holder_longdesc,
	_doc_items_usagehelp = digtron.doc.battery_holder_usagehelp,
	_digtron_formspec = battery_holder_formspec,
	groups = {cracky = 3,  oddly_breakable_by_hand = 3, digtron = 7, tubedevice = 1, tubedevice_receiver = 1},
	drop = "digtron:battery_holder",
	sounds = digtron.metal_sounds,
	paramtype2= "facedir",
	drawtype = "nodebox",
	paramtype = "light",
	is_ground_content = false,
	tiles = {
		"digtron_plate.png^digtron_crossbrace.png^digtron_battery.png",
		"digtron_plate.png^digtron_crossbrace.png^digtron_battery.png",
		"digtron_plate.png^digtron_crossbrace.png^digtron_battery.png^digtron_storage.png",
		"digtron_plate.png^digtron_crossbrace.png^digtron_battery.png^digtron_storage.png",
		"digtron_plate.png^digtron_crossbrace.png^digtron_battery.png^digtron_storage.png",
		"digtron_plate.png^digtron_crossbrace.png^digtron_battery.png^digtron_storage.png",
		},

	on_construct = function(pos)
		local meta = minetest.get_meta(pos)
		meta:set_string("formspec", battery_holder_formspec)
		local inv = meta:get_inventory()
		inv:set_size("batteries", 8*4)
	end,
	
	-- Only allow RE batteries to be placed in the inventory
	allow_metadata_inventory_put = function(pos, listname, index, stack, player)
		if listname == "batteries" then
			local node_name = stack:get_name()
                                                 
			-- Only allow RE batteries from technic mode
			if node_name == "technic:battery" then                            
				local meta = stack:get_metadata()
				local md = minetest.deserialize(meta)
				-- And specifically if they hold any charge
				-- Disregard empty batteries, the player should know better
				if md.charge > 0 then
					return stack:get_count()    
				else
					return 0
				end
                                                 
			else
				return 0
			end
		end
		return 0
	end,
                                                  
                                                 
	can_dig = function(pos,player)
		local meta = minetest.get_meta(pos)
		local inv = meta:get_inventory()
		return inv:is_empty("batteries")
	end,
		
	-- Pipeworks compatibility
	-- Because who wouldn't send batteries through pipes if he could?
	-----------------------------------------------------------------

	tube = (function() if minetest.get_modpath("pipeworks") then return {
		insert_object = function(pos, node, stack, direction)
			if minetest.get_craft_result({method="batteries", width=1, items={stack}}).time ~= 0 then
				local meta = minetest.get_meta(pos)
				local inv = meta:get_inventory()
				return inv:add_item("batteries", stack)
			end
			return stack
		end,
		can_insert = function(pos, node, stack, direction)
			if minetest.get_craft_result({method="batteries", width=1, items={stack}}).time ~= 0 then
				local meta = minetest.get_meta(pos)
				local inv = meta:get_inventory()
				return inv:room_for_item("batteries", stack)
			end
			return false
		end,
		input_inventory = "batteries",
		connect_sides = {left = 1, right = 1, back = 1, front = 1, bottom = 1, top = 1}
	} end end)(),
	
	after_place_node = (function() if minetest.get_modpath("pipeworks") then return pipeworks.after_place end end)(),
	after_dig_node = (function() if minetest.get_modpath("pipeworks")then return pipeworks.after_dig end end)()
})




----------------------------- previous version below 

--[[


minetest.register_node("digtron:battery_holder", {
	description = S("Digtron Battery Holder"),
	_doc_items_longdesc = digtron.doc.batteryholder_longdesc,
	_doc_items_usagehelp = digtron.doc.batteryholder_usagehelp,
	groups = {cracky = 3,  oddly_breakable_by_hand=3, digtron = 7, tubedevice = 1, tubedevice_receiver = 1},
	drop = "digtron:battery_holder",
	sounds = digtron.metal_sounds,
	paramtype2= "facedir",
	drawtype = "nodebox",
	paramtype = "light",
	is_ground_content = false,
	tiles = {
		"digtron_plate.png^digtron_crossbrace.png^digtron_battery.png",
		"digtron_plate.png^digtron_crossbrace.png^digtron_battery.png",
		"digtron_plate.png^digtron_crossbrace.png^digtron_battery.png^digtron_storage.png",
		"digtron_plate.png^digtron_crossbrace.png^digtron_battery.png^digtron_storage.png",
		"digtron_plate.png^digtron_crossbrace.png^digtron_battery.png^digtron_storage.png",
		"digtron_plate.png^digtron_crossbrace.png^digtron_battery.png^digtron_storage.png",
		},

	on_construct = function(pos)
		local meta = minetest.get_meta(pos)
		meta:set_string("formspec", 
			"size[8,9.3]" ..
			default.gui_bg ..
			default.gui_bg_img ..
			default.gui_slots ..
			"label[0,0;" .. S("Batteries") .. "]" ..
			"list[current_name;batteries;0,0.6;8,4;]" ..
			"list[current_player;main;0,5.15;8,1;]" ..
			"list[current_player;main;0,6.38;8,3;8]" ..
			"listring[current_name;batteries]" ..
			"listring[current_player;main]" ..
			default.get_hotbar_bg(0,5.15)
		)
		local inv = meta:get_inventory()
		inv:set_size("batteries", 8*4)
	end,
	
	-- Only allow RE batteries to be placed in the inventory
	allow_metadata_inventory_put = function(pos, listname, index, stack, player)
		if listname == "batteries" then
			local node_name = stack:get_name()
                                                 
			-- Only allow RE batteries from technic mode
			if node_name == "technic:battery" then                            
				local meta = stack:get_metadata()
-- 				minetest.chat_send_all(minetest.serialize(meta))
				local md = minetest.deserialize(meta)
				minetest.chat_send_all("Battery has charge: "..md.charge)
				-- And specifically if they hold any charge
				if md.charge > 0 then
					return stack:get_count()    
				else
					return 0
				end
                                                 
			else
				return 0
			end
		end
		return 0
	end,
	
	can_dig = function(pos,player)
		local meta = minetest.get_meta(pos)
		local inv = meta:get_inventory()
		return inv:is_empty("batteries")
	end,
	
                                                  
	-- Pipeworks compatibility
	----------------------------------------------------------------

	tube = (function() if minetest.get_modpath("pipeworks") then return {
		insert_object = function(pos, node, stack, direction)
			local node_name = stack:get_name()
			if node_name == "technic:battery" then      
				local meta = minetest.get_meta(pos)
				local inv = meta:get_inventory()
				return inv:add_item("batteries", stack)
			end
			return stack
		end,
		can_insert = function(pos, node, stack, direction)
			local node_name = stack:get_name()
			if node_name == "technic:battery" then      
				local meta = minetest.get_meta(pos)
				local inv = meta:get_inventory()
				return inv:room_for_item("batteries", stack)
			end
			return false
		end,
		input_inventory = "batteries",
		connect_sides = {left = 1, right = 1, back = 1, front = 1, bottom = 1, top = 1}
	} end end)(),
	
	after_place_node = (function() if minetest.get_modpath("pipeworks") then return pipeworks.after_place end end)(),
	after_dig_node = (function() if minetest.get_modpath("pipeworks")then return pipeworks.after_dig end end)()
})
]]