summaryrefslogtreecommitdiff
path: root/technic/machines/register/generator.lua
blob: f8d7e6a660e240916d8f25f376acbb5380053477 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
local S = technic.getter

local fs_helpers = pipeworks.fs_helpers
local tube_entry = "^pipeworks_tube_connection_metallic.png"

local tube = {
	insert_object = function(pos, node, stack, direction)
		local meta = minetest.get_meta(pos)
		local inv = meta:get_inventory()
		return inv:add_item("src", stack)
	end,
	can_insert = function(pos, node, stack, direction)
		local meta = minetest.get_meta(pos)
		local inv = meta:get_inventory()
		if meta:get_int("splitstacks") == 1 then
			stack = stack:peek_item(1)
		end
		return inv:room_for_item("src", stack)
	end,
	connect_sides = {left=1, right=1, back=1, top=1, bottom=1},
}

function technic.register_generator(data)

	local tier = data.tier
	local ltier = string.lower(tier)

	local groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2,
		technic_machine=1, ["technic_"..ltier]=1}
	if data.tube then
		groups.tubedevice = 1
		groups.tubedevice_receiver = 1
	end
	local active_groups = {not_in_creative_inventory = 1}
	for k, v in pairs(groups) do active_groups[k] = v end

	local generator_formspec =
		"size[8,9;]"..
		"label[0,0;"..S("Fuel-Fired %s Generator"):format(tier).."]"..
		"list[current_name;src;3,1;1,1;]"..
		"image[4,1;1,1;default_furnace_fire_bg.png]"..
		"list[current_player;main;0,5;8,4;]"..
		"listring[]"
	
	local desc = S("Fuel-Fired %s Generator"):format(tier)
	
	local run = function(pos, node)
		local meta = minetest.get_meta(pos)
		local burn_time = meta:get_int("burn_time")
		local burn_totaltime = meta:get_int("burn_totaltime")
		-- If more to burn and the energy produced was used: produce some more
		if burn_time > 0 then
			meta:set_int(tier.."_EU_supply", data.supply)
			burn_time = burn_time - 1
			meta:set_int("burn_time", burn_time)
		end
		-- Burn another piece of fuel
		if burn_time == 0 then
			local inv = meta:get_inventory()
			if not inv:is_empty("src") then
				local fuellist = inv:get_list("src")
				local fuel
				local afterfuel
				fuel, afterfuel = minetest.get_craft_result(
						{method = "fuel", width = 1,
						items = fuellist})
				if not fuel or fuel.time == 0 then
					meta:set_string("infotext", S("%s Out Of Fuel"):format(desc))
					technic.swap_node(pos, "technic:"..ltier.."_generator")
					meta:set_int(tier.."_EU_supply", 0)
					return
				end
				meta:set_int("burn_time", fuel.time)
				meta:set_int("burn_totaltime", fuel.time)
				inv:set_stack("src", 1, afterfuel.items[1])
				technic.swap_node(pos, "technic:"..ltier.."_generator_active")
				meta:set_int(tier.."_EU_supply", data.supply)
			else
				technic.swap_node(pos, "technic:"..ltier.."_generator")
				meta:set_int(tier.."_EU_supply", 0)
			end
		end
		if burn_totaltime == 0 then burn_totaltime = 1 end
		local percent = math.floor((burn_time / burn_totaltime) * 100)
		meta:set_string("infotext", desc.." ("..percent.."%)")

		local form_buttons = ""
		if ltier ~= "lv" then
			form_buttons = fs_helpers.cycling_button(
				meta,
				pipeworks.button_base,
				"splitstacks",
				{
					pipeworks.button_off,
					pipeworks.button_on
				}
			)..pipeworks.button_label
		end
		meta:set_string("formspec",
			"size[8, 9]"..
			"label[0, 0;"..minetest.formspec_escape(desc).."]"..
			"list[current_name;src;3, 1;1, 1;]"..
			"image[4, 1;1, 1;default_furnace_fire_bg.png^[lowpart:"..
			(percent)..":default_furnace_fire_fg.png]"..
			"list[current_player;main;0, 5;8, 4;]"..
			"listring[]"..
			form_buttons
		)
	end

	local tentry = tube_entry
	if ltier == "lv" then tentry = "" end

	minetest.register_node("technic:"..ltier.."_generator", {
		description = desc,
		tiles = {
				"technic_"..ltier.."_generator_top.png"..tentry,
				"technic_machine_bottom.png"..tentry,
				"technic_"..ltier.."_generator_side.png"..tentry,
				"technic_"..ltier.."_generator_side.png"..tentry,
				"technic_"..ltier.."_generator_side.png"..tentry,
				"technic_"..ltier.."_generator_front.png"
		},
		paramtype2 = "facedir",
		groups = groups,
		connect_sides = {"bottom", "back", "left", "right"},
		legacy_facedir_simple = true,
		sounds = default.node_sound_wood_defaults(),
		tube = data.tube and tube or nil,
		on_construct = function(pos)
			local meta = minetest.get_meta(pos)
			local node = minetest.get_node(pos)
			meta:set_string("infotext", desc)
			meta:set_int(data.tier.."_EU_supply", 0)
			meta:set_int("burn_time", 0)
			meta:set_int("tube_time",  0)
			local form_buttons = ""
			if not string.find(node.name, ":lv_") then
				form_buttons = fs_helpers.cycling_button(
						meta,
						pipeworks.button_base,
						"splitstacks",
						{
							pipeworks.button_off,
							pipeworks.button_on
						}
					)..pipeworks.button_label
			end
			meta:set_string("formspec", generator_formspec..form_buttons)
			local inv = meta:get_inventory()
			inv:set_size("src", 1)
		end,
		can_dig = technic.machine_can_dig,
		allow_metadata_inventory_put = technic.machine_inventory_put,
		allow_metadata_inventory_take = technic.machine_inventory_take,
		allow_metadata_inventory_move = technic.machine_inventory_move,
		technic_run = run,
		after_place_node = data.tube and pipeworks.after_place,
		after_dig_node = technic.machine_after_dig_node,
		on_receive_fields = function(pos, formname, fields, sender)
			if not pipeworks.may_configure(pos, sender) then return end
			fs_helpers.on_receive_fields(pos, fields)
			local meta = minetest.get_meta(pos)
			local node = minetest.get_node(pos)
			local form = generator_formspec
			local form_buttons = ""
			if not string.find(node.name, ":lv_") then
				form_buttons = fs_helpers.cycling_button(
						meta,
						pipeworks.button_base,
						"splitstacks",
						{
							pipeworks.button_off,
							pipeworks.button_on
						}
					)..pipeworks.button_label
			end
			meta:set_string("formspec", generator_formspec..form_buttons)
		end,
	})

	minetest.register_node("technic:"..ltier.."_generator_active", {
		description = desc,
		tiles = {
			"technic_"..ltier.."_generator_top.png"..tube_entry,
			"technic_machine_bottom.png"..tube_entry,
			"technic_"..ltier.."_generator_side.png"..tube_entry,
			"technic_"..ltier.."_generator_side.png"..tube_entry,
			"technic_"..ltier.."_generator_side.png"..tube_entry,
			"technic_"..ltier.."_generator_front_active.png"
		},
		paramtype2 = "facedir",
		groups = active_groups,
		connect_sides = {"bottom"},
		legacy_facedir_simple = true,
		sounds = default.node_sound_wood_defaults(),
		tube = data.tube and tube or nil,
		drop = "technic:"..ltier.."_generator",
		can_dig = technic.machine_can_dig,
		after_dig_node = technic.machine_after_dig_node,
		allow_metadata_inventory_put = technic.machine_inventory_put,
		allow_metadata_inventory_take = technic.machine_inventory_take,
		allow_metadata_inventory_move = technic.machine_inventory_move,
		technic_run = run,
		technic_on_disable = function(pos, node)
			local timer = minetest.get_node_timer(pos)
        		timer:start(1)
        	end,
		on_timer = function(pos, node)
			local meta = minetest.get_meta(pos)
			local node = minetest.get_node(pos)

			-- Connected back?
			if meta:get_int(tier.."_EU_timeout") > 0 then return false end
			
			local burn_time = meta:get_int("burn_time") or 0

			if burn_time <= 0 then
				meta:set_int(tier.."_EU_supply", 0)
				meta:set_int("burn_time", 0)
				technic.swap_node(pos, "technic:"..ltier.."_generator")
				return false
			end

			local burn_totaltime = meta:get_int("burn_totaltime") or 0
			if burn_totaltime == 0 then burn_totaltime = 1 end
			burn_time = burn_time - 1
			meta:set_int("burn_time", burn_time)
			local percent = math.floor(burn_time / burn_totaltime * 100)

			local form_buttons = ""
			if not string.find(node.name, ":lv_") then
				form_buttons = fs_helpers.cycling_button(
					meta,
					pipeworks.button_base,
					"splitstacks",
					{
						pipeworks.button_off,
						pipeworks.button_on
					}
				)..pipeworks.button_label
			end
			meta:set_string("formspec",
				"size[8, 9]"..
				"label[0, 0;"..minetest.formspec_escape(desc).."]"..
				"list[current_name;src;3, 1;1, 1;]"..
				"image[4, 1;1, 1;default_furnace_fire_bg.png^[lowpart:"..
				(percent)..":default_furnace_fire_fg.png]"..
				"list[current_player;main;0, 5;8, 4;]"..
				"listring[]"..
				form_buttons
			)
			return true
		end,
		on_receive_fields = function(pos, formname, fields, sender)
			if not pipeworks.may_configure(pos, sender) then return end
			fs_helpers.on_receive_fields(pos, fields)
			local meta = minetest.get_meta(pos)
			local node = minetest.get_node(pos)
			local form_buttons = ""
			if not string.find(node.name, ":lv_") then
				form_buttons = fs_helpers.cycling_button(
						meta,
						pipeworks.button_base,
						"splitstacks",
						{
							pipeworks.button_off,
							pipeworks.button_on
						}
					)..pipeworks.button_label
			end

			local burn_totaltime = meta:get_int("burn_totaltime") or 0
			local burn_time = meta:get_int("burn_time")
			local percent = math.floor(burn_time / burn_totaltime * 100)

			meta:set_string("formspec",
				"size[8, 9]"..
				"label[0, 0;"..minetest.formspec_escape(desc).."]"..
				"list[current_name;src;3, 1;1, 1;]"..
				"image[4, 1;1, 1;default_furnace_fire_bg.png^[lowpart:"..
				(percent)..":default_furnace_fire_fg.png]"..
				"list[current_player;main;0, 5;8, 4;]"..
				"listring[]"..
				form_buttons
			)
		end,
	})

	technic.register_machine(tier, "technic:"..ltier.."_generator",        technic.producer)
	technic.register_machine(tier, "technic:"..ltier.."_generator_active", technic.producer)
end