techage/steam_engine/flywheel.lua
2019-05-21 00:05:53 +02:00

279 lines
7.1 KiB
Lua

--[[
TechAge
=======
Copyright (C) 2019 Joachim Stolberg
LGPLv2.1+
See LICENSE.txt for more information
TA2 Steam Engine Flywheel
]]--
-- for lazy programmers
local S = function(pos) if pos then return minetest.pos_to_string(pos) end end
local P = minetest.string_to_pos
local M = minetest.get_meta
-- Load support for intllib.
local MP = minetest.get_modpath("techage")
local I,_ = dofile(MP.."/intllib.lua")
local STANDBY_TICKS = 4
local COUNTDOWN_TICKS = 4
local CYCLE_TIME = 8
local POWER_CAPACITY = 25
local Axle = techage.Axle
local function formspec(self, pos, mem)
return "size[8,7]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"image[6,0.5;1,2;"..techage.power.formspec_power_bar(POWER_CAPACITY, mem.power_result).."]"..
"image_button[5,1;1,1;".. self:get_state_button_image(mem) ..";state_button;]"..
"button[2,1.5;2,1;update;"..I("Update").."]"..
"list[current_player;main;0,3;8,4;]"..
default.get_hotbar_bg(0, 3)
end
local function start_cylinder(pos, on, mem)
if not on then
if mem.handle then
minetest.sound_stop(mem.handle)
mem.handle = nil
end
end
local pos2 = techage.get_pos(pos, 'L')
local ndef = minetest.registered_nodes[minetest.get_node(pos2).name]
if ndef and ndef.start_cylinder then
return ndef.start_cylinder(pos2, on)
end
return false
end
local function can_start(pos, mem, state)
return start_cylinder(pos, true, mem)
end
local function play_sound(pos)
local mem = tubelib2.get_mem(pos)
if mem.techage_state == techage.RUNNING then
mem.handle = minetest.sound_play("techage_steamengine", {
pos = pos,
gain = 0.5,
max_hear_distance = 10})
minetest.after(2, play_sound, pos)
end
end
local function start_node(pos, mem, state)
techage.power.power_distribution(pos)
if mem.power_result > 0 then
techage.switch_axles(pos, true)
play_sound(pos)
end
end
local function stop_node(pos, mem, state)
start_cylinder(pos, false, mem)
techage.power.power_distribution(pos)
techage.switch_axles(pos, false)
end
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,
})
-- Pass1: Power balance calculation
local function on_power_pass1(pos, mem)
if State:is_active(mem) then
return -POWER_CAPACITY
end
return 0
end
-- Pass2: Power balance adjustment
local function on_power_pass2(pos, mem, sum)
return 0
end
-- Pass3: Power balance result
local function on_power_pass3(pos, mem, sum)
mem.power_result = sum
end
local function distibuting(pos, mem)
if mem.power_result > 0 then
State:keep_running(pos, mem, COUNTDOWN_TICKS)
else
State:fault(pos, mem)
start_cylinder(pos, false, mem)
techage.power.power_distribution(pos)
end
end
local function node_timer(pos, elapsed)
local mem = tubelib2.get_mem(pos)
local pos2 = techage.get_pos(pos, 'L')
if minetest.get_node(pos2).name == "techage:cylinder_on" and tubelib2.get_mem(pos2).running then
distibuting(pos, mem)
else
State:fault(pos, mem)
start_cylinder(pos, false, mem)
techage.power.power_distribution(pos)
end
return State:is_active(mem)
end
local function on_receive_fields(pos, formname, fields, player)
if minetest.is_protected(pos, player:get_player_name()) then
return
end
local mem = tubelib2.get_mem(pos)
State:state_button_event(pos, mem, fields)
if fields.update then
M(pos):set_string("formspec", formspec(State, pos, mem))
end
end
local function on_rightclick(pos)
local mem = tubelib2.get_mem(pos)
M(pos):set_string("formspec", formspec(State, pos, mem))
end
minetest.register_node("techage:flywheel", {
description = I("TA2 Flywheel"),
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]",
},
on_construct = tubelib2.init_mem,
after_place_node = function(pos, placer)
local mem = tubelib2.get_mem(pos)
State:node_init(pos, mem, "")
on_rightclick(pos)
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
State:after_dig_node(pos, oldnode, oldmetadata, digger)
end,
on_receive_fields = on_receive_fields,
on_rightclick = on_rightclick,
on_timer = node_timer,
drop = "",
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 = I("TA2 Flywheel"),
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",
{
image = "techage_filling8_ta2.png^techage_frame8_ta2.png^techage_flywheel8.png",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 1.2,
},
},
{
image = "techage_filling8_ta2.png^techage_frame8_ta2.png^techage_flywheel8.png^[transformFX]",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 1.2,
},
},
},
on_receive_fields = on_receive_fields,
on_rightclick = on_rightclick,
on_timer = node_timer,
drop = "",
paramtype2 = "facedir",
groups = {not_in_creative_inventory=1},
diggable = false,
on_rotate = screwdriver.disallow,
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
techage.power.register_node({"techage:flywheel", "techage:flywheel_on"}, {
on_power_pass1 = on_power_pass1,
on_power_pass2 = on_power_pass2,
on_power_pass3 = on_power_pass3,
conn_sides = {"R"},
power_network = Axle,
})
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"},
},
})
minetest.register_lbm({
label = "[techage] Steam engine sound",
name = "techage:steam_engine",
nodenames = {"techage:flywheel_on"},
run_at_every_load = true,
action = function(pos, node)
play_sound(pos)
minetest.get_node_timer(pos):start(CYCLE_TIME)
end
})
techage.register_help_page(I("TA2 Flywheel"),
I([[Part of the steam engine.
Has to be placed side by side
with the TA2 Cylinder.
Used to turn on/off the steam engine.
Connect the Flywheel with your TA2 machines
by means of Axles and Gearboxes
(see TA2 Steam Engine)]]), "techage:flywheel")