techage/ta3_power/tiny_generator.lua

401 lines
12 KiB
Lua
Raw Permalink Normal View History

2020-01-26 01:22:06 +03:00
--[[
TechAge
=======
2022-06-06 21:39:55 +03:00
Copyright (C) 2019-2022 Joachim Stolberg
2020-01-26 01:22:06 +03:00
2020-10-19 20:09:17 +03:00
AGPL v3
2020-01-26 01:22:06 +03:00
See LICENSE.txt for more information
2022-01-03 23:40:31 +03:00
2020-01-26 01:22:06 +03:00
TA3 Tiny Power Generator
]]--
-- for lazy programmers
local M = minetest.get_meta
local S = techage.S
2020-01-31 21:55:10 +03:00
local Cable = techage.ElectricCable
2020-01-26 01:22:06 +03:00
local firebox = techage.firebox
local fuel = techage.fuel
local Pipe = techage.LiquidPipe
2021-06-09 23:09:59 +03:00
local power = networks.power
local liquid = networks.liquid
2023-02-19 18:47:06 +03:00
local control = networks.control
2020-01-26 01:22:06 +03:00
local CYCLE_TIME = 2
2021-06-27 19:29:46 +03:00
local STANDBY_TICKS = 1
local COUNTDOWN_TICKS = 2
local PWR_PERF = 12
2020-01-26 01:22:06 +03:00
local EFFICIENCY = 2.5
2020-01-31 21:55:10 +03:00
local function formspec(self, pos, nvm)
2020-02-02 00:00:58 +03:00
return "size[5,4]"..
2020-01-26 01:22:06 +03:00
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
2020-02-02 00:00:58 +03:00
"box[0,-0.1;4.8,0.5;#c6e8ff]"..
2021-06-09 23:09:59 +03:00
"label[0.2,-0.1;"..minetest.colorize( "#000000", S("Tiny Generator")).."]"..
2020-02-02 00:00:58 +03:00
fuel.fuel_container(0, 0.9, nvm)..
"image[1.4,1.6;1,1;techage_form_arrow_bg.png^[transformR270]"..
"image_button[1.4,3.2;1,1;".. self:get_state_button_image(nvm) ..";state_button;]"..
"tooltip[1.5,3;1,1;"..self:get_state_tooltip(nvm).."]"..
2021-06-27 19:29:46 +03:00
techage.formspec_power_bar(pos, 2.5, 0.8, S("Electricity"), nvm.provided, PWR_PERF)
2020-02-02 00:00:58 +03:00
end
local function play_sound(pos)
local mem = techage.get_mem(pos)
if not mem.handle or mem.handle == -1 then
mem.handle = minetest.sound_play("techage_generator", {
2022-01-03 23:40:31 +03:00
pos = pos,
2020-02-02 00:00:58 +03:00
gain = 1,
max_hear_distance = 10,
loop = true})
if mem.handle == -1 then
minetest.after(1, play_sound, pos)
end
end
end
local function stop_sound(pos)
local mem = techage.get_mem(pos)
if mem.handle then
minetest.sound_stop(mem.handle)
mem.handle = nil
end
2020-01-26 01:22:06 +03:00
end
2021-06-27 19:29:46 +03:00
local function has_fuel(pos, nvm)
return (nvm.burn_cycles or 0) > 0 or (nvm.liquid and nvm.liquid.amount and nvm.liquid.amount > 0)
end
2020-01-31 21:55:10 +03:00
local function can_start(pos, nvm, state)
2022-01-03 23:40:31 +03:00
if has_fuel(pos, nvm) then
return true
2020-01-26 01:22:06 +03:00
end
2020-02-02 00:00:58 +03:00
return S("no fuel")
2020-01-26 01:22:06 +03:00
end
2020-01-31 21:55:10 +03:00
local function start_node(pos, nvm, state)
2021-06-27 19:29:46 +03:00
local meta = M(pos)
nvm.provided = 0
local outdir = meta:get_int("outdir")
2020-02-02 00:00:58 +03:00
play_sound(pos)
2021-06-27 19:29:46 +03:00
power.start_storage_calc(pos, Cable, outdir)
techage.evaluate_charge_termination(nvm, meta)
2020-01-26 01:22:06 +03:00
end
2020-01-31 21:55:10 +03:00
local function stop_node(pos, nvm, state)
nvm.provided = 0
2020-02-02 00:00:58 +03:00
local outdir = M(pos):get_int("outdir")
stop_sound(pos)
2021-06-27 19:29:46 +03:00
power.start_storage_calc(pos, Cable, outdir)
2020-01-26 01:22:06 +03:00
end
local State = techage.NodeStates:new({
node_name_passive = "techage:tiny_generator",
node_name_active = "techage:tiny_generator_on",
cycle_time = CYCLE_TIME,
2021-06-27 19:29:46 +03:00
standby_ticks = STANDBY_TICKS,
2020-01-26 01:22:06 +03:00
formspec_func = formspec,
2020-01-31 21:55:10 +03:00
infotext_name = S("TA3 Tiny Power Generator"),
2020-01-26 01:22:06 +03:00
can_start = can_start,
start_node = start_node,
stop_node = stop_node,
})
2020-01-31 21:55:10 +03:00
local function burning(pos, nvm)
2021-06-27 19:29:46 +03:00
local ratio = math.max((nvm.provided or PWR_PERF) / PWR_PERF, 0.02)
2022-01-03 23:40:31 +03:00
2020-01-31 21:55:10 +03:00
nvm.liquid = nvm.liquid or {}
nvm.liquid.amount = nvm.liquid.amount or 0
nvm.burn_cycles = (nvm.burn_cycles or 0) - ratio
if nvm.burn_cycles <= 0 then
if nvm.liquid.amount > 0 then
nvm.liquid.amount = nvm.liquid.amount - 1
nvm.burn_cycles = fuel.burntime(nvm.liquid.name) * EFFICIENCY / CYCLE_TIME
nvm.burn_cycles_total = nvm.burn_cycles
2020-01-26 01:22:06 +03:00
else
2022-01-03 23:40:31 +03:00
nvm.liquid.name = nil
2020-01-26 01:22:06 +03:00
end
end
end
local function node_timer(pos, elapsed)
2020-01-31 21:55:10 +03:00
local nvm = techage.get_nvm(pos)
2021-06-27 19:29:46 +03:00
local running = techage.is_running(nvm)
local fuel = has_fuel(pos, nvm)
if running and not fuel then
State:standby(pos, nvm, S("no fuel"))
2022-01-12 22:50:18 +03:00
stop_node(pos, nvm, State)
2021-06-27 19:29:46 +03:00
elseif not running and fuel then
State:start(pos, nvm)
2022-01-12 22:50:18 +03:00
-- start_node() is called implicit
2021-06-27 19:29:46 +03:00
elseif running then
local meta = M(pos)
local outdir = meta:get_int("outdir")
local tp1 = tonumber(meta:get_string("termpoint1"))
local tp2 = tonumber(meta:get_string("termpoint2"))
nvm.provided = power.provide_power(pos, Cable, outdir, PWR_PERF, tp1, tp2)
local val = power.get_storage_load(pos, Cable, outdir, PWR_PERF)
if val > 0 then
nvm.load = val
end
burning(pos, nvm)
State:keep_running(pos, nvm, COUNTDOWN_TICKS)
2020-01-26 01:22:06 +03:00
end
2020-02-02 00:00:58 +03:00
if techage.is_activeformspec(pos) then
2021-06-27 19:29:46 +03:00
M(pos):set_string("formspec", formspec(State, pos, nvm))
2020-02-02 00:00:58 +03:00
end
2020-05-23 15:11:35 +03:00
return State:is_active(nvm)
2020-01-26 01:22:06 +03:00
end
local function on_receive_fields(pos, formname, fields, player)
if minetest.is_protected(pos, player:get_player_name()) then
return
end
2020-01-31 21:55:10 +03:00
local nvm = techage.get_nvm(pos)
State:state_button_event(pos, nvm, fields)
2020-01-26 01:22:06 +03:00
end
2020-02-02 00:00:58 +03:00
local function on_rightclick(pos, node, clicker)
2020-01-31 21:55:10 +03:00
local nvm = techage.get_nvm(pos)
2021-06-27 19:29:46 +03:00
techage.set_activeformspec(pos, clicker)
2020-01-31 21:55:10 +03:00
M(pos):set_string("formspec", formspec(State, pos, nvm))
2020-01-26 01:22:06 +03:00
end
2021-07-06 22:37:29 +03:00
local function get_generator_data(pos, outdir, tlib2)
2021-06-09 23:09:59 +03:00
local nvm = techage.get_nvm(pos)
2022-01-18 23:38:57 +03:00
if techage.is_running(nvm) then
2021-06-27 19:29:46 +03:00
return {level = (nvm.load or 0) / PWR_PERF, perf = PWR_PERF, capa = PWR_PERF * 2}
2020-02-02 00:00:58 +03:00
end
2021-06-09 23:09:59 +03:00
end
2020-01-26 01:22:06 +03:00
minetest.register_node("techage:tiny_generator", {
description = S("TA3 Tiny Power Generator"),
tiles = {
-- up, down, right, left, back, front
"techage_appl_electric_gen_top.png^techage_frame_ta3_top.png",
"techage_appl_electric_gen_top.png^techage_frame_ta3.png",
"techage_appl_electric_gen_side.png^techage_appl_hole_electric.png^techage_frame_ta3.png",
"techage_appl_electric_gen_side.png^techage_frame_ta3.png",
"techage_appl_electric_gen_front.png^[transformFX]^techage_frame_ta3.png",
"techage_appl_electric_gen_front.png^techage_frame_ta3.png",
},
paramtype2 = "facedir",
groups = {cracky=2, crumbly=2, choppy=2},
on_rotate = screwdriver.disallow,
is_ground_content = false,
after_place_node = function(pos, placer, itemstack)
2020-01-31 21:55:10 +03:00
local nvm = techage.get_nvm(pos)
2020-01-26 01:22:06 +03:00
local number = techage.add_node(pos, "techage:tiny_generator")
2020-01-31 21:55:10 +03:00
nvm.burn_cycles = 0
if itemstack then
local stack_meta = itemstack:get_meta()
if stack_meta then
local liquid_name = stack_meta:get_string("liquid_name")
local liquid_amount = stack_meta:get_int("liquid_amount")
if liquid_name ~= "" and fuel.burntime(liquid.name) and
liquid_amount >= 0 and liquid_amount <= fuel.CAPACITY then
nvm.liquid = nvm.liquid or {}
nvm.liquid.name = liquid_name
nvm.liquid.amount = liquid_amount
end
end
end
2020-01-31 21:55:10 +03:00
State:node_init(pos, nvm, number)
2020-02-02 00:00:58 +03:00
M(pos):set_string("formspec", formspec(State, pos, nvm))
M(pos):set_int("outdir", networks.side_to_outdir(pos, "R"))
2020-02-10 23:20:54 +03:00
Pipe:after_place_node(pos)
Cable:after_place_node(pos)
2020-01-26 01:22:06 +03:00
end,
2022-01-03 23:40:31 +03:00
2020-02-10 23:20:54 +03:00
after_dig_node = function(pos, oldnode)
Pipe:after_dig_node(pos)
Cable:after_dig_node(pos)
techage.del_mem(pos)
end,
preserve_metadata = function(pos, oldnode, oldmetadata, drops)
local nvm = techage.get_nvm(pos)
if nvm.liquid and nvm.liquid.name and nvm.liquid.amount > 0 then
-- generator tank is not empty
local meta = drops[1]:get_meta()
meta:set_string("liquid_name", nvm.liquid.name)
meta:set_int("liquid_amount", nvm.liquid.amount)
meta:set_string("description", S("TA3 Tiny Power Generator") .. " (fuel: " ..
2023-01-03 19:24:27 +03:00
tostring(nvm.liquid and nvm.liquid.amount or 0) .. "/" ..
tostring(fuel.CAPACITY) .. ")")
end
end,
2021-06-09 23:09:59 +03:00
get_generator_data = get_generator_data,
2022-01-03 23:40:31 +03:00
ta3_formspec = techage.generator_settings("ta3", PWR_PERF),
2020-01-26 01:22:06 +03:00
on_receive_fields = on_receive_fields,
on_rightclick = on_rightclick,
2020-02-02 00:00:58 +03:00
on_punch = fuel.on_punch,
2020-01-26 01:22:06 +03:00
on_timer = node_timer,
})
minetest.register_node("techage:tiny_generator_on", {
description = S("TA3 Tiny Power Generator"),
tiles = {
-- up, down, right, left, back, front
"techage_appl_electric_gen_top.png^techage_frame_ta3_top.png",
"techage_appl_electric_gen_top.png^techage_frame_ta3.png",
"techage_appl_electric_gen_side.png^techage_appl_hole_electric.png^techage_frame_ta3.png",
"techage_appl_electric_gen_side.png^techage_frame_ta3.png",
{
name = "techage_appl_electric_gen_front4.png^[transformFX]^techage_frame4_ta3.png",
2020-01-26 01:22:06 +03:00
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 0.8,
},
},
{
name = "techage_appl_electric_gen_front4.png^techage_frame4_ta3.png",
2020-01-26 01:22:06 +03:00
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 0.8,
},
},
},
2022-01-03 23:40:31 +03:00
2020-01-26 01:22:06 +03:00
paramtype = "light",
paramtype2 = "facedir",
groups = {not_in_creative_inventory=1},
diggable = false,
light_source = 4,
on_rotate = screwdriver.disallow,
is_ground_content = false,
2021-06-09 23:09:59 +03:00
get_generator_data = get_generator_data,
2022-01-03 23:40:31 +03:00
ta3_formspec = techage.generator_settings("ta3", PWR_PERF),
2020-01-26 01:22:06 +03:00
on_receive_fields = on_receive_fields,
on_rightclick = on_rightclick,
2020-02-02 00:00:58 +03:00
on_punch = fuel.on_punch,
2020-01-26 01:22:06 +03:00
on_timer = node_timer,
can_dig = fuel.can_dig,
})
2021-06-09 23:09:59 +03:00
local liquid_def = {
fuel_cat = fuel.BT_GASOLINE,
capa = fuel.CAPACITY,
peek = function(pos)
local nvm = techage.get_nvm(pos)
return liquid.srv_peek(nvm)
end,
put = function(pos, indir, name, amount)
if techage.fuel.valid_fuel(name, fuel.BT_GASOLINE) then
local nvm = techage.get_nvm(pos)
local leftover = liquid.srv_put(nvm, name, amount, fuel.CAPACITY)
if techage.is_activeformspec(pos) then
local nvm = techage.get_nvm(pos)
M(pos):set_string("formspec", formspec(State, pos, nvm))
end
return leftover
end
return amount
end,
take = function(pos, indir, name, amount)
local nvm = techage.get_nvm(pos)
local taken = liquid.srv_take(nvm, name, amount)
if techage.is_activeformspec(pos) then
local nvm = techage.get_nvm(pos)
M(pos):set_string("formspec", formspec(State, pos, nvm))
end
return taken
end,
untake = function(pos, indir, name, amount)
local nvm = techage.get_nvm(pos)
local leftover = liquid.srv_put(nvm, name, amount, fuel.CAPACITY)
if techage.is_activeformspec(pos) then
local nvm = techage.get_nvm(pos)
M(pos):set_string("formspec", formspec(State, pos, nvm))
end
return leftover
end,
}
power.register_nodes({"techage:tiny_generator", "techage:tiny_generator_on"}, Cable, "gen", {"R"})
liquid.register_nodes({"techage:tiny_generator", "techage:tiny_generator_on"}, Pipe, "tank", nil, liquid_def)
2020-01-26 01:22:06 +03:00
2023-02-19 18:47:06 +03:00
control.register_nodes({"techage:tiny_generator", "techage:tiny_generator_on"}, {
on_receive = function(pos, tlib2, topic, payload)
end,
on_request = function(pos, tlib2, topic)
if topic == "info" then
local nvm = techage.get_nvm(pos)
local meta = M(pos)
return {
type = S("TA3 Tiny Power Generator"),
number = meta:get_string("node_number") or "",
running = techage.is_running(nvm) or false,
available = PWR_PERF,
provided = nvm.provided or 0,
termpoint = meta:get_string("termpoint"),
}
end
return false
end,
}
)
2020-01-26 01:22:06 +03:00
techage.register_node({"techage:tiny_generator", "techage:tiny_generator_on"}, {
on_recv_message = function(pos, src, topic, payload)
2020-01-31 21:55:10 +03:00
local nvm = techage.get_nvm(pos)
if topic == "delivered" then
return nvm.provided or 0
elseif topic == "fuel" then
return techage.fuel.get_fuel_amount(nvm)
2020-01-26 01:22:06 +03:00
else
return State:on_receive_message(pos, topic, payload)
end
end,
2022-06-06 21:39:55 +03:00
on_beduino_receive_cmnd = function(pos, src, topic, payload)
return State:on_beduino_receive_cmnd(pos, topic, payload)
end,
on_beduino_request_data = function(pos, src, topic, payload)
local nvm = techage.get_nvm(pos)
if topic == 135 then
return 0, {nvm.provided or 0}
elseif topic == 132 then
return 0, {techage.fuel.get_fuel_amount(nvm)}
else
return State:on_beduino_request_data(pos, topic, payload)
end
end,
2020-02-02 00:00:58 +03:00
on_node_load = function(pos, node)
2020-01-26 01:22:06 +03:00
State:on_node_load(pos)
2020-02-02 00:00:58 +03:00
if node.name == "techage:tiny_generator_on" then
play_sound(pos)
2020-02-18 00:46:29 +03:00
end
local inv = M(pos):get_inventory()
if not inv:is_empty("fuel") then
local nvm = techage.get_nvm(pos)
nvm.liquid = nvm.liquid or {}
local count = inv:get_stack("fuel", 1):get_count()
nvm.liquid.amount = (nvm.liquid.amount or 0) + count
nvm.liquid.name = "techage:gasoline"
inv:set_stack("fuel", 1, nil)
2022-01-03 23:40:31 +03:00
end
2020-01-26 01:22:06 +03:00
end,
2022-01-03 23:40:31 +03:00
})
2020-01-26 01:22:06 +03:00
minetest.register_craft({
output = "techage:tiny_generator",
recipe = {
{'default:steel_ingot', 'techage:usmium_nuggets', 'default:steel_ingot'},
{'dye:red', 'basic_materials:gear_steel', 'techage:electric_cableS'},
{'default:steel_ingot', 'techage:vacuum_tube', 'default:steel_ingot'},
},
})