elepower/elepower_machines/machines/bases/generator.lua

147 lines
4.0 KiB
Lua
Raw Permalink Normal View History

-- see elepower_papi >> external_nodes_items.lua for explanation
-- shorten table ref
local epr = ele.external.ref
2018-06-18 07:09:43 +00:00
2018-08-11 12:47:25 +00:00
local function get_formspec_default(power, percent, state)
return "size[8,8.5]"..
epr.gui_bg..
epr.gui_bg_img..
epr.gui_slots..
ele.formspec.power_meter(power)..
2018-08-11 12:47:25 +00:00
ele.formspec.state_switcher(7, 0, state)..
"list[context;src;3,1.5;1,1;]"..
"image[4,1.5;1,1;elepower_gui_icon_elepower_bg.png^[lowpart:"..
percent..":elepower_gui_icon_elepower.png]"..
"list[current_player;main;0,4.25;8,1;]"..
"list[current_player;main;0,5.5;8,3;8]"..
"listring[current_player;main]"..
"listring[context;src]"..
"listring[current_player;main]"..
epr.get_hotbar_bg(0, 4.25)
end
2018-06-18 07:09:43 +00:00
function elepm.register_fuel_generator(nodename, nodedef)
if not nodedef.groups then
nodedef.groups = {}
end
nodedef.groups["ele_machine"] = 1
nodedef.groups["ele_provider"] = 1
2018-06-19 14:21:32 +00:00
nodedef.groups["tubedevice"] = 1
nodedef.groups["tubedevice_receiver"] = 1
2018-06-18 07:09:43 +00:00
2018-07-28 14:29:56 +00:00
-- Allow for custom formspec
local get_formspec = get_formspec_default
if nodedef.get_formspec then
get_formspec = nodedef.get_formspec
nodedef.get_formspec = nil
end
2019-01-28 11:43:07 +00:00
if not nodedef.ele_upgrades then
nodedef.ele_upgrades = {
capacitor = {"capacity"},
}
end
2018-06-18 07:09:43 +00:00
nodedef.on_timer = function (pos, elapsed)
local refresh = false
local meta = minetest.get_meta(pos)
2018-06-18 07:09:43 +00:00
local burn_time = meta:get_int("burn_time")
local burn_totaltime = meta:get_int("burn_totaltime")
local capacity = ele.helpers.get_node_property(meta, pos, "capacity")
local generation = ele.helpers.get_node_property(meta, pos, "usage")
local storage = ele.helpers.get_node_property(meta, pos, "storage")
2018-08-11 12:47:25 +00:00
local state = meta:get_int("state")
local is_enabled = ele.helpers.state_enabled(meta, pos, state)
local status = "Idle"
local pow_buffer = {capacity = capacity, storage = storage, usage = 0}
while true do
if not is_enabled then
status = "Off"
break
2018-06-18 07:09:43 +00:00
end
2018-08-11 12:47:25 +00:00
-- If more to burn and the energy produced was used: produce some more
if burn_time > 0 then
if storage + generation > capacity then
break
2018-08-11 12:47:25 +00:00
end
2018-08-12 12:42:32 +00:00
pow_buffer.storage = pow_buffer.storage + generation
2018-08-11 15:36:15 +00:00
pow_buffer.usage = generation
2018-06-18 07:09:43 +00:00
2018-08-11 12:47:25 +00:00
burn_time = burn_time - 1
meta:set_int("burn_time", burn_time)
refresh = true
2018-08-11 12:47:25 +00:00
end
2018-06-18 07:09:43 +00:00
2018-08-11 12:47:25 +00:00
status = "Active"
2018-06-18 07:09:43 +00:00
-- 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, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
2018-06-18 07:09:43 +00:00
if not fuel or fuel.time == 0 then
ele.helpers.swap_node(pos, nodename)
break
end
2018-06-18 07:09:43 +00:00
meta:set_int("burn_time", fuel.time)
meta:set_int("burn_totaltime", fuel.time)
inv:set_stack("src", 1, afterfuel.items[1])
pow_buffer.usage = generation
2018-06-18 07:09:43 +00:00
if nodedef.ele_active_node then
local active_node = nodename.."_active"
if nodedef.ele_active_node ~= true then
active_node = nodedef.ele_active_node
end
ele.helpers.swap_node(pos, active_node)
2018-06-18 07:09:43 +00:00
end
refresh = true
else
2018-08-11 12:47:25 +00:00
status = "Idle"
ele.helpers.swap_node(pos, nodename)
refresh = false
2018-06-18 07:09:43 +00:00
end
end
if burn_totaltime == 0 then burn_totaltime = 1 end
break
2018-06-18 07:09:43 +00:00
end
local percent = math.floor((burn_time / burn_totaltime) * 100)
2018-08-11 12:47:25 +00:00
meta:set_string("formspec", get_formspec(pow_buffer, percent, state))
meta:set_string("infotext", ("%s %s"):format(nodedef.description, status) ..
2018-08-12 12:42:32 +00:00
"\n" .. ele.capacity_text(capacity, pow_buffer.storage))
meta:set_int("storage", pow_buffer.storage)
2018-06-18 07:09:43 +00:00
return refresh
2018-06-18 07:09:43 +00:00
end
nodedef.on_construct = function (pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
inv:set_size("src", 1)
local capacity = ele.helpers.get_node_property(meta, pos, "capacity")
local storage = ele.helpers.get_node_property(meta, pos, "storage")
meta:set_string("formspec", get_formspec({capacity = capacity, storage = storage, usage = 0}, 0))
2018-06-18 07:09:43 +00:00
end
ele.register_machine(nodename, nodedef)
end