techage/steam_engine/flywheel.lua

250 lines
6.7 KiB
Lua
Raw Normal View History

2019-03-02 14:24:48 +03:00
--[[
TechAge
=======
2021-06-05 12:42:30 +03:00
Copyright (C) 2019-2021 Joachim Stolberg
2019-03-02 14:24:48 +03:00
2020-10-19 20:09:17 +03:00
AGPL v3
2019-03-02 14:24:48 +03:00
See LICENSE.txt for more information
TA2 Steam Engine Flywheel
]]--
-- for lazy programmers
local M = minetest.get_meta
local S = techage.S
2019-03-02 14:24:48 +03:00
2019-03-08 01:12:07 +03:00
local STANDBY_TICKS = 4
local COUNTDOWN_TICKS = 4
2019-06-16 22:06:16 +03:00
local CYCLE_TIME = 2
2021-06-05 12:42:30 +03:00
local PWR_PERF = 25
2019-03-08 23:18:55 +03:00
local Axle = techage.Axle
2021-06-05 12:42:30 +03:00
local power = networks.power
2019-03-02 14:24:48 +03:00
2020-01-26 01:15:44 +03:00
-- Axles texture animation
local function switch_axles(pos, on)
local outdir = M(pos):get_int("outdir")
Axle:switch_tube_line(pos, outdir, on and "on" or "off")
end
local function formspec(self, pos, nvm)
2021-06-07 23:00:52 +03:00
return "size[4,4]" ..
"box[0,-0.1;3.8,0.5;#c6e8ff]" ..
"label[1,-0.1;" .. minetest.colorize( "#000000", S("Flywheel")) .. "]" ..
default.gui_bg ..
default.gui_bg_img ..
default.gui_slots ..
techage.formspec_power_bar(pos, 0, 0.8, S("power"), nvm.provided, PWR_PERF) ..
"image_button[2.8,2;1,1;" .. self:get_state_button_image(nvm) .. ";state_button;]" ..
"tooltip[2.8,2;1,1; " .. self:get_state_tooltip(nvm) .. "]"
2019-03-08 01:12:07 +03:00
end
2020-01-26 01:15:44 +03:00
local function transfer_cylinder(pos, topic, payload)
return techage.transfer(pos, "L", topic, payload, nil,
{"techage:cylinder", "techage:cylinder_on"})
2019-04-28 22:34:21 +03:00
end
2020-01-26 01:15:44 +03:00
local function can_start(pos, nvm, state)
return (nvm.firebox_trigger or 0) > 0 -- by means of firebox
2019-03-08 01:12:07 +03:00
end
2020-01-26 01:15:44 +03:00
local function start_node(pos, nvm, state)
switch_axles(pos, true)
local outdir = M(pos):get_int("outdir")
2021-06-05 12:42:30 +03:00
power.start_storage_calc(pos, Axle, outdir)
2020-01-26 01:15:44 +03:00
transfer_cylinder(pos, "start")
nvm.running = true
end
local function stop_node(pos, nvm, state)
switch_axles(pos, false)
local outdir = M(pos):get_int("outdir")
2021-06-05 12:42:30 +03:00
power.start_storage_calc(pos, Axle, outdir)
2020-01-26 01:15:44 +03:00
nvm.provided = 0
transfer_cylinder(pos, "stop")
nvm.running = false
2019-03-02 14:24:48 +03:00
end
2019-03-08 01:12:07 +03:00
local State = techage.NodeStates:new({
node_name_passive = "techage:flywheel",
node_name_active = "techage:flywheel_on",
cycle_time = CYCLE_TIME,
standby_ticks = STANDBY_TICKS,
formspec_func = formspec,
can_start = can_start,
start_node = start_node,
stop_node = stop_node,
})
local function node_timer(pos, elapsed)
2020-01-26 01:15:44 +03:00
local nvm = techage.get_nvm(pos)
nvm.firebox_trigger = (nvm.firebox_trigger or 0) - 1
if nvm.firebox_trigger <= 0 then
State:nopower(pos, nvm)
stop_node(pos, nvm, State)
transfer_cylinder(pos, "stop")
else
local outdir = M(pos):get_int("outdir")
2021-06-05 12:42:30 +03:00
nvm.provided = power.provide_power(pos, Axle, outdir, PWR_PERF)
2020-01-26 01:15:44 +03:00
State:keep_running(pos, nvm, COUNTDOWN_TICKS)
2021-06-05 12:42:30 +03:00
local data = power.get_storage_data(pos, Axle, outdir)
if data then
nvm.load = data.level * PWR_PERF
end
end
2020-01-26 01:15:44 +03:00
if techage.is_activeformspec(pos) then
M(pos):set_string("formspec", formspec(State, pos, nvm))
end
return State:is_active(nvm)
2019-03-08 01:12:07 +03:00
end
2019-03-02 22:51:16 +03:00
2019-03-02 14:24:48 +03:00
local function on_receive_fields(pos, formname, fields, player)
if minetest.is_protected(pos, player:get_player_name()) then
return
end
2020-01-26 01:15:44 +03:00
local nvm,_ = techage.get_nvm(pos, true)
State:state_button_event(pos, nvm, fields)
2019-03-02 14:24:48 +03:00
end
2020-01-26 01:15:44 +03:00
local function on_rightclick(pos, node, clicker)
local nvm = techage.get_nvm(pos)
techage.set_activeformspec(pos, clicker)
M(pos):set_string("formspec", formspec(State, pos, nvm))
end
local function after_place_node(pos)
local nvm = techage.get_nvm(pos)
State:node_init(pos, nvm, "")
M(pos):set_int("outdir", networks.side_to_outdir(pos, "R"))
M(pos):set_string("formspec", formspec(State, pos, nvm))
Axle:after_place_node(pos)
2019-03-02 14:24:48 +03:00
end
2020-01-26 01:15:44 +03:00
local function after_dig_node(pos, oldnode)
Axle:after_dig_node(pos)
techage.del_mem(pos)
end
2021-06-05 12:42:30 +03:00
local function get_generator_data(pos, tlib2)
local nvm = techage.get_nvm(pos)
if nvm.running then
2021-06-07 23:00:52 +03:00
return {level = (nvm.load or 0) / PWR_PERF, perf = PWR_PERF, capa = PWR_PERF * 4}
2021-06-05 12:42:30 +03:00
else
2021-06-07 23:00:52 +03:00
return {level = 0, perf = PWR_PERF, capa = PWR_PERF * 4}
2021-06-05 12:42:30 +03:00
end
2020-01-26 01:15:44 +03:00
end
2019-03-02 14:24:48 +03:00
minetest.register_node("techage:flywheel", {
description = S("TA2 Flywheel"),
2019-03-02 14:24:48 +03:00
tiles = {
-- up, down, right, left, back, front
"techage_filling_ta2.png^techage_frame_ta2.png",
"techage_filling_ta2.png^techage_frame_ta2.png",
"techage_filling_ta2.png^techage_axle_clutch.png^techage_frame_ta2.png",
"techage_filling_ta2.png^techage_appl_open.png^techage_frame_ta2.png",
"techage_filling_ta2.png^techage_frame_ta2.png^techage_flywheel.png",
"techage_filling_ta2.png^techage_frame_ta2.png^techage_flywheel.png^[transformFX]",
},
2019-05-21 01:05:53 +03:00
2019-03-02 14:24:48 +03:00
on_receive_fields = on_receive_fields,
on_rightclick = on_rightclick,
2019-03-08 23:18:55 +03:00
on_timer = node_timer,
2020-01-26 01:15:44 +03:00
after_place_node = after_place_node,
after_dig_node = after_dig_node,
2021-06-05 12:42:30 +03:00
get_generator_data = get_generator_data,
2019-03-02 14:24:48 +03:00
paramtype2 = "facedir",
groups = {cracky=2, crumbly=2, choppy=2},
on_rotate = screwdriver.disallow,
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
minetest.register_node("techage:flywheel_on", {
description = S("TA2 Flywheel"),
2019-03-02 14:24:48 +03:00
tiles = {
-- up, down, right, left, back, front
"techage_filling_ta2.png^techage_frame_ta2.png",
"techage_filling_ta2.png^techage_frame_ta2.png",
{
image = "techage_filling4_ta2.png^techage_axle_clutch4.png^techage_frame4_ta2.png",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 0.6,
},
},
"techage_filling_ta2.png^techage_appl_open.png^techage_frame_ta2.png",
{
2019-03-09 22:30:41 +03:00
image = "techage_filling8_ta2.png^techage_frame8_ta2.png^techage_flywheel8.png",
2019-03-02 14:24:48 +03:00
backface_culling = false,
animation = {
type = "vertical_frames",
2020-01-26 01:15:44 +03:00
aspect_w = 64,
aspect_h = 64,
2019-03-09 22:30:41 +03:00
length = 1.2,
2019-03-02 14:24:48 +03:00
},
},
{
2019-03-09 22:30:41 +03:00
image = "techage_filling8_ta2.png^techage_frame8_ta2.png^techage_flywheel8.png^[transformFX]",
2019-03-02 14:24:48 +03:00
backface_culling = false,
animation = {
type = "vertical_frames",
2020-01-26 01:15:44 +03:00
aspect_w = 64,
aspect_h = 64,
2019-03-09 22:30:41 +03:00
length = 1.2,
2019-03-02 14:24:48 +03:00
},
},
},
2019-03-08 01:12:07 +03:00
2019-03-02 14:24:48 +03:00
on_receive_fields = on_receive_fields,
on_rightclick = on_rightclick,
2019-03-08 23:18:55 +03:00
on_timer = node_timer,
2020-01-26 01:15:44 +03:00
after_place_node = after_place_node,
after_dig_node = after_dig_node,
2021-06-05 12:42:30 +03:00
get_generator_data = get_generator_data,
2019-03-02 14:24:48 +03:00
2019-05-05 23:29:00 +03:00
drop = "",
2019-03-02 14:24:48 +03:00
paramtype2 = "facedir",
2019-03-08 01:12:07 +03:00
groups = {not_in_creative_inventory=1},
diggable = false,
2019-03-02 14:24:48 +03:00
on_rotate = screwdriver.disallow,
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
2019-04-28 17:04:45 +03:00
2021-06-05 12:42:30 +03:00
power.register_nodes({"techage:flywheel", "techage:flywheel_on"}, Axle, "gen", {"R"})
2019-05-21 01:05:53 +03:00
2019-06-16 22:06:16 +03:00
techage.register_node({"techage:flywheel", "techage:flywheel_on"}, {
on_transfer = function(pos, in_dir, topic, payload)
2020-01-26 01:15:44 +03:00
local nvm = techage.get_nvm(pos)
2019-06-16 22:06:16 +03:00
if topic == "trigger" then
2020-01-26 01:15:44 +03:00
nvm.firebox_trigger = 3
if nvm.running then
2021-06-05 12:42:30 +03:00
return math.max((nvm.provided or PWR_PERF) / PWR_PERF, 0.1)
2019-06-16 22:06:16 +03:00
else
return 0
end
end
2020-01-26 01:15:44 +03:00
end,
on_node_load = function(pos, node)
State:on_node_load(pos)
end,
2019-06-16 22:06:16 +03:00
})
2019-04-28 17:04:45 +03:00
minetest.register_craft({
output = "techage:flywheel",
recipe = {
{"basic_materials:steel_bar", "dye:red", "default:wood"},
{"", "basic_materials:gear_steel", "techage:axle"},
{"default:wood", "techage:iron_ingot", "basic_materials:steel_bar"},
},
})
2019-04-28 22:34:21 +03:00
2020-01-26 01:15:44 +03:00