Christopher Head
2019-01-26 4f78a69ffc714886c9d6e812f78d543bb33fe674
commit | author | age
be2f30 1 local S = technic.getter
7a3cd4 2
4eaf48 3 local fs_helpers = pipeworks.fs_helpers
092ab7 4 local tube_entry = "^pipeworks_tube_connection_metallic.png"
4eaf48 5
7a3cd4 6 local tube = {
S 7     insert_object = function(pos, node, stack, direction)
8         local meta = minetest.get_meta(pos)
9         local inv = meta:get_inventory()
47b0b5 10         return inv:add_item("src", stack)
7a3cd4 11     end,
S 12     can_insert = function(pos, node, stack, direction)
13         local meta = minetest.get_meta(pos)
14         local inv = meta:get_inventory()
4eaf48 15         if meta:get_int("splitstacks") == 1 then
VE 16             stack = stack:peek_item(1)
17         end
18         return inv:room_for_item("src", stack)
7a3cd4 19     end,
S 20     connect_sides = {left=1, right=1, back=1, top=1, bottom=1},
21 }
be2f30 22
092ab7 23 function technic.register_generator(data)
VE 24
704925 25     local tier = data.tier
P 26     local ltier = string.lower(tier)
47b0b5 27
83c649 28     local groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2,
S 29         technic_machine=1, ["technic_"..ltier]=1}
7a3cd4 30     if data.tube then
S 31         groups.tubedevice = 1
32         groups.tubedevice_receiver = 1
33     end
83c649 34     local active_groups = {not_in_creative_inventory = 1}
S 35     for k, v in pairs(groups) do active_groups[k] = v end
47b0b5 36
704925 37     local generator_formspec =
fb9338 38         "size[8,9;]"..
7c4b70 39         "label[0,0;"..S("Fuel-Fired %s Generator"):format(tier).."]"..
704925 40         "list[current_name;src;3,1;1,1;]"..
P 41         "image[4,1;1,1;default_furnace_fire_bg.png]"..
d732c8 42         "list[current_player;main;0,5;8,4;]"..
E 43         "listring[]"
704925 44     
7c4b70 45     local desc = S("Fuel-Fired %s Generator"):format(tier)
563a4c 46     
N 47     local run = function(pos, node)
48         local meta = minetest.get_meta(pos)
49         local burn_time = meta:get_int("burn_time")
50         local burn_totaltime = meta:get_int("burn_totaltime")
51         -- If more to burn and the energy produced was used: produce some more
52         if burn_time > 0 then
53             meta:set_int(tier.."_EU_supply", data.supply)
54             burn_time = burn_time - 1
55             meta:set_int("burn_time", burn_time)
56         end
57         -- Burn another piece of fuel
58         if burn_time == 0 then
59             local inv = meta:get_inventory()
4f78a6 60             if not inv:is_empty("src") then
563a4c 61                 local fuellist = inv:get_list("src")
6ec12b 62                 local fuel
G 63                 local afterfuel
64                 fuel, afterfuel = minetest.get_craft_result(
563a4c 65                         {method = "fuel", width = 1,
N 66                         items = fuellist})
67                 if not fuel or fuel.time == 0 then
68                     meta:set_string("infotext", S("%s Out Of Fuel"):format(desc))
69                     technic.swap_node(pos, "technic:"..ltier.."_generator")
6d90eb 70                     meta:set_int(tier.."_EU_supply", 0)
563a4c 71                     return
N 72                 end
73                 meta:set_int("burn_time", fuel.time)
74                 meta:set_int("burn_totaltime", fuel.time)
6ec12b 75                 inv:set_stack("src", 1, afterfuel.items[1])
563a4c 76                 technic.swap_node(pos, "technic:"..ltier.."_generator_active")
N 77                 meta:set_int(tier.."_EU_supply", data.supply)
78             else
79                 technic.swap_node(pos, "technic:"..ltier.."_generator")
80                 meta:set_int(tier.."_EU_supply", 0)
81             end
82         end
83         if burn_totaltime == 0 then burn_totaltime = 1 end
84         local percent = math.floor((burn_time / burn_totaltime) * 100)
85         meta:set_string("infotext", desc.." ("..percent.."%)")
4eaf48 86
bdd45f 87         local form_buttons = ""
4eaf48 88         if ltier ~= "lv" then
bdd45f 89             form_buttons = fs_helpers.cycling_button(
4eaf48 90                 meta,
fab2c4 91                 pipeworks.button_base,
4eaf48 92                 "splitstacks",
VE 93                 {
fab2c4 94                     pipeworks.button_off,
VE 95                     pipeworks.button_on
4eaf48 96                 }
fab2c4 97             )..pipeworks.button_label
4eaf48 98         end
4f78a6 99         meta:set_string("formspec",
4eaf48 100             "size[8, 9]"..
VE 101             "label[0, 0;"..minetest.formspec_escape(desc).."]"..
102             "list[current_name;src;3, 1;1, 1;]"..
103             "image[4, 1;1, 1;default_furnace_fire_bg.png^[lowpart:"..
104             (percent)..":default_furnace_fire_fg.png]"..
105             "list[current_player;main;0, 5;8, 4;]"..
106             "listring[]"..
bdd45f 107             form_buttons
4eaf48 108         )
563a4c 109     end
092ab7 110
7c11ff 111     local tentry = tube_entry
VE 112     if ltier == "lv" then tentry = "" end
113
9aee83 114     minetest.register_node("technic:"..ltier.."_generator", {
704925 115         description = desc,
092ab7 116         tiles = {
7c11ff 117                 "technic_"..ltier.."_generator_top.png"..tentry,
VE 118                 "technic_machine_bottom.png"..tentry,
119                 "technic_"..ltier.."_generator_side.png"..tentry,
120                 "technic_"..ltier.."_generator_side.png"..tentry,
121                 "technic_"..ltier.."_generator_side.png"..tentry,
092ab7 122                 "technic_"..ltier.."_generator_front.png"
4f78a6 123         },
704925 124         paramtype2 = "facedir",
7a3cd4 125         groups = groups,
83c649 126         connect_sides = {"bottom", "back", "left", "right"},
704925 127         legacy_facedir_simple = true,
P 128         sounds = default.node_sound_wood_defaults(),
7a3cd4 129         tube = data.tube and tube or nil,
704925 130         on_construct = function(pos)
P 131             local meta = minetest.get_meta(pos)
bdd45f 132             local node = minetest.get_node(pos)
704925 133             meta:set_string("infotext", desc)
P 134             meta:set_int(data.tier.."_EU_supply", 0)
135             meta:set_int("burn_time", 0)
7a3cd4 136             meta:set_int("tube_time",  0)
bdd45f 137             local form_buttons = ""
VE 138             if not string.find(node.name, ":lv_") then
139                 form_buttons = fs_helpers.cycling_button(
4eaf48 140                         meta,
fab2c4 141                         pipeworks.button_base,
4eaf48 142                         "splitstacks",
VE 143                         {
fab2c4 144                             pipeworks.button_off,
VE 145                             pipeworks.button_on
4eaf48 146                         }
fab2c4 147                     )..pipeworks.button_label
4eaf48 148             end
bdd45f 149             meta:set_string("formspec", generator_formspec..form_buttons)
704925 150             local inv = meta:get_inventory()
P 151             inv:set_size("src", 1)
152         end,
0809dd 153         can_dig = technic.machine_can_dig,
S 154         allow_metadata_inventory_put = technic.machine_inventory_put,
155         allow_metadata_inventory_take = technic.machine_inventory_take,
156         allow_metadata_inventory_move = technic.machine_inventory_move,
563a4c 157         technic_run = run,
011397 158         after_place_node = data.tube and pipeworks.after_place,
4eaf48 159         after_dig_node = technic.machine_after_dig_node,
VE 160         on_receive_fields = function(pos, formname, fields, sender)
161             if not pipeworks.may_configure(pos, sender) then return end
162             fs_helpers.on_receive_fields(pos, fields)
163             local meta = minetest.get_meta(pos)
bdd45f 164             local node = minetest.get_node(pos)
4eaf48 165             local form = generator_formspec
bdd45f 166             local form_buttons = ""
VE 167             if not string.find(node.name, ":lv_") then
168                 form_buttons = fs_helpers.cycling_button(
4eaf48 169                         meta,
fab2c4 170                         pipeworks.button_base,
4eaf48 171                         "splitstacks",
VE 172                         {
fab2c4 173                             pipeworks.button_off,
VE 174                             pipeworks.button_on
4eaf48 175                         }
fab2c4 176                     )..pipeworks.button_label
4eaf48 177             end
bdd45f 178             meta:set_string("formspec", generator_formspec..form_buttons)
4eaf48 179         end,
704925 180     })
0809dd 181
704925 182     minetest.register_node("technic:"..ltier.."_generator_active", {
P 183         description = desc,
092ab7 184         tiles = {
VE 185             "technic_"..ltier.."_generator_top.png"..tube_entry,
186             "technic_machine_bottom.png"..tube_entry,
187             "technic_"..ltier.."_generator_side.png"..tube_entry,
188             "technic_"..ltier.."_generator_side.png"..tube_entry,
189             "technic_"..ltier.."_generator_side.png"..tube_entry,
190             "technic_"..ltier.."_generator_front_active.png"
191         },
704925 192         paramtype2 = "facedir",
7a3cd4 193         groups = active_groups,
83c649 194         connect_sides = {"bottom"},
704925 195         legacy_facedir_simple = true,
P 196         sounds = default.node_sound_wood_defaults(),
7a3cd4 197         tube = data.tube and tube or nil,
704925 198         drop = "technic:"..ltier.."_generator",
0809dd 199         can_dig = technic.machine_can_dig,
ceead2 200         after_dig_node = technic.machine_after_dig_node,
0809dd 201         allow_metadata_inventory_put = technic.machine_inventory_put,
S 202         allow_metadata_inventory_take = technic.machine_inventory_take,
203         allow_metadata_inventory_move = technic.machine_inventory_move,
563a4c 204         technic_run = run,
1c617f 205         technic_on_disable = function(pos, node)
N 206             local timer = minetest.get_node_timer(pos)
207                 timer:start(1)
208             end,
209         on_timer = function(pos, node)
210             local meta = minetest.get_meta(pos)
bdd45f 211             local node = minetest.get_node(pos)
VE 212
1c617f 213             -- Connected back?
3252da 214             if meta:get_int(tier.."_EU_timeout") > 0 then return false end
1c617f 215             
N 216             local burn_time = meta:get_int("burn_time") or 0
217
218             if burn_time <= 0 then
219                 meta:set_int(tier.."_EU_supply", 0)
220                 meta:set_int("burn_time", 0)
221                 technic.swap_node(pos, "technic:"..ltier.."_generator")
3252da 222                 return false
1c617f 223             end
N 224
225             local burn_totaltime = meta:get_int("burn_totaltime") or 0
226             if burn_totaltime == 0 then burn_totaltime = 1 end
227             burn_time = burn_time - 1
228             meta:set_int("burn_time", burn_time)
229             local percent = math.floor(burn_time / burn_totaltime * 100)
4eaf48 230
bdd45f 231             local form_buttons = ""
VE 232             if not string.find(node.name, ":lv_") then
233                 form_buttons = fs_helpers.cycling_button(
4eaf48 234                     meta,
fab2c4 235                     pipeworks.button_base,
4eaf48 236                     "splitstacks",
VE 237                     {
fab2c4 238                         pipeworks.button_off,
VE 239                         pipeworks.button_on
4eaf48 240                     }
fab2c4 241                 )..pipeworks.button_label
4eaf48 242             end
4f78a6 243             meta:set_string("formspec",
1c617f 244                 "size[8, 9]"..
N 245                 "label[0, 0;"..minetest.formspec_escape(desc).."]"..
246                 "list[current_name;src;3, 1;1, 1;]"..
247                 "image[4, 1;1, 1;default_furnace_fire_bg.png^[lowpart:"..
248                 (percent)..":default_furnace_fire_fg.png]"..
d732c8 249                 "list[current_player;main;0, 5;8, 4;]"..
4eaf48 250                 "listring[]"..
bdd45f 251                 form_buttons
4eaf48 252             )
3252da 253             return true
1c617f 254         end,
4eaf48 255         on_receive_fields = function(pos, formname, fields, sender)
VE 256             if not pipeworks.may_configure(pos, sender) then return end
257             fs_helpers.on_receive_fields(pos, fields)
258             local meta = minetest.get_meta(pos)
bdd45f 259             local node = minetest.get_node(pos)
VE 260             local form_buttons = ""
261             if not string.find(node.name, ":lv_") then
262                 form_buttons = fs_helpers.cycling_button(
4eaf48 263                         meta,
fab2c4 264                         pipeworks.button_base,
4eaf48 265                         "splitstacks",
VE 266                         {
fab2c4 267                             pipeworks.button_off,
VE 268                             pipeworks.button_on
4eaf48 269                         }
fab2c4 270                     )..pipeworks.button_label
4eaf48 271             end
VE 272
273             local burn_totaltime = meta:get_int("burn_totaltime") or 0
274             local burn_time = meta:get_int("burn_time")
275             local percent = math.floor(burn_time / burn_totaltime * 100)
276
4f78a6 277             meta:set_string("formspec",
4eaf48 278                 "size[8, 9]"..
VE 279                 "label[0, 0;"..minetest.formspec_escape(desc).."]"..
280                 "list[current_name;src;3, 1;1, 1;]"..
281                 "image[4, 1;1, 1;default_furnace_fire_bg.png^[lowpart:"..
282                 (percent)..":default_furnace_fire_fg.png]"..
283                 "list[current_player;main;0, 5;8, 4;]"..
284                 "listring[]"..
bdd45f 285                 form_buttons
4eaf48 286             )
VE 287         end,
704925 288     })
563a4c 289
a35db4 290     technic.register_machine(tier, "technic:"..ltier.."_generator",        technic.producer)
S 291     technic.register_machine(tier, "technic:"..ltier.."_generator_active", technic.producer)
704925 292 end
be2f30 293