techage/steam_engine/flywheel.lua

284 lines
7.4 KiB
Lua
Raw Normal View History

2019-03-02 14:24:48 +03:00
--[[
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
2019-03-09 21:26:15 +03:00
-- Techage Related Data
local TRD = function(pos) return (minetest.registered_nodes[minetest.get_node(pos).name] or {}).techage end
2019-03-02 14:24:48 +03:00
-- Load support for intllib.
2019-03-17 17:33:13 +03:00
local MP = minetest.get_modpath("techage")
2019-03-02 14:24:48 +03:00
local I,_ = dofile(MP.."/intllib.lua")
2019-03-08 01:12:07 +03:00
local STANDBY_TICKS = 4
local COUNTDOWN_TICKS = 4
local CYCLE_TIME = 8
2019-05-02 00:01:14 +03:00
local POWER_CAPACITY = 20
2019-03-08 23:18:55 +03:00
local Axle = techage.Axle
local generator = techage.generator
2019-03-02 14:24:48 +03:00
2019-03-08 01:12:07 +03:00
local function formspec(self, pos, mem)
return "size[8,7]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
2019-03-08 23:18:55 +03:00
"image[6,0.5;1,2;"..generator.formspec_level(mem, mem.power_result)..
2019-03-08 01:12:07 +03:00
"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
2019-03-08 23:18:55 +03:00
local function start_cylinder(pos, on)
2019-03-08 01:12:07 +03:00
local pos2 = techage.get_pos(pos, 'L')
2019-03-09 21:26:15 +03:00
local trd = TRD(pos2)
if trd and trd.start_cylinder then
return trd.start_cylinder(pos2, on)
2019-03-08 01:12:07 +03:00
end
return false
end
2019-03-08 23:18:55 +03:00
local function can_start(pos, mem, state)
return start_cylinder(pos, true)
end
2019-04-28 22:34:21 +03:00
local function play_sound(pos)
local mem = tubelib2.get_mem(pos)
mem.handle = minetest.sound_play("techage_steamengine", {
pos = pos,
gain = 0.5,
max_hear_distance = 10})
minetest.after(2, play_sound, pos)
end
2019-03-08 01:12:07 +03:00
local function start_node(pos, mem, state)
2019-04-28 22:34:21 +03:00
print("start_node")
2019-03-08 23:18:55 +03:00
generator.turn_power_on(pos, POWER_CAPACITY)
2019-04-28 22:34:21 +03:00
play_sound(pos)
2019-03-08 01:12:07 +03:00
end
local function stop_node(pos, mem, state)
2019-03-08 23:18:55 +03:00
start_cylinder(pos, false)
generator.turn_power_on(pos, 0)
2019-04-28 22:34:21 +03:00
if mem.handle then
minetest.sound_stop(mem.handle)
mem.handle = nil
end
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 distibuting(pos, mem)
2019-03-08 23:18:55 +03:00
if mem.power_result > 0 then
2019-03-08 01:12:07 +03:00
State:keep_running(pos, mem, COUNTDOWN_TICKS)
2019-03-04 00:19:30 +03:00
else
2019-03-08 01:12:07 +03:00
State:fault(pos, mem)
2019-03-08 23:18:55 +03:00
start_cylinder(pos, false)
2019-03-09 00:52:27 +03:00
generator.turn_power_on(pos, 0)
2019-03-04 00:19:30 +03:00
end
2019-03-08 01:12:07 +03:00
end
local function node_timer(pos, elapsed)
local mem = tubelib2.get_mem(pos)
print("flywheel node_timer")
local pos2 = techage.get_pos(pos, 'L')
2019-03-08 23:18:55 +03:00
if minetest.get_node(pos2).name == "techage:cylinder_on" and tubelib2.get_mem(pos2).running then
2019-03-08 01:12:07 +03:00
distibuting(pos, mem)
else
State:fault(pos, mem)
2019-03-08 23:18:55 +03:00
start_cylinder(pos, false)
2019-03-09 00:52:27 +03:00
generator.turn_power_on(pos, 0)
end
2019-03-08 01:12:07 +03:00
return State:is_active(mem)
end
2019-03-02 22:51:16 +03:00
2019-03-09 15:42:51 +03:00
local function valid_power_dir(pos, power_dir, in_dir)
return power_dir == in_dir
2019-03-02 14:24:48 +03:00
end
2019-03-08 23:18:55 +03:00
local function turn_power_on(pos, in_dir, sum)
2019-03-08 01:12:07 +03:00
local mem = tubelib2.get_mem(pos)
2019-03-08 23:18:55 +03:00
-- store result for formspec
mem.power_result = sum
if State:is_active(mem) and sum <= 0 then
2019-03-08 01:12:07 +03:00
State:fault(pos, mem)
2019-03-08 23:18:55 +03:00
start_cylinder(pos, false)
-- No automatic turn on
mem.power_capacity = 0
2019-03-08 01:12:07 +03:00
end
2019-03-08 23:18:55 +03:00
M(pos):set_string("formspec", formspec(State, pos, mem))
2019-03-08 01:12:07 +03:00
end
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
2019-03-08 01:12:07 +03:00
local mem = tubelib2.get_mem(pos)
State:state_button_event(pos, mem, fields)
2019-03-02 14:24:48 +03:00
if fields.update then
2019-03-08 01:12:07 +03:00
M(pos):set_string("formspec", formspec(State, pos, mem))
2019-03-02 14:24:48 +03:00
end
end
2019-03-04 00:19:30 +03:00
local function on_rightclick(pos)
2019-03-02 14:24:48 +03:00
local mem = tubelib2.get_mem(pos)
2019-03-08 01:12:07 +03:00
M(pos):set_string("formspec", formspec(State, pos, mem))
2019-03-02 14:24:48 +03:00
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]",
},
techage = {
2019-03-08 01:12:07 +03:00
turn_on = turn_power_on,
2019-03-08 23:18:55 +03:00
read_power_consumption = generator.read_power_consumption,
power_network = Axle,
2019-04-28 17:04:45 +03:00
power_side = "R",
2019-03-08 23:18:55 +03:00
animated_power_network = true,
2019-03-02 14:24:48 +03:00
},
2019-03-08 01:12:07 +03:00
after_place_node = function(pos, placer)
2019-03-08 23:18:55 +03:00
local mem = generator.after_place_node(pos)
2019-03-08 01:12:07 +03:00
State:node_init(pos, mem, "")
2019-03-04 00:19:30 +03:00
on_rightclick(pos)
end,
2019-03-08 01:12:07 +03:00
after_dig_node = function(pos, oldnode, oldmetadata, digger)
State:after_dig_node(pos, oldnode, oldmetadata, digger)
2019-03-08 23:18:55 +03:00
generator.after_dig_node(pos, oldnode)
2019-03-08 01:12:07 +03:00
end,
2019-03-08 23:18:55 +03:00
after_tube_update = generator.after_tube_update,
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,
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 = 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",
{
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",
aspect_w = 32,
aspect_h = 32,
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",
aspect_w = 32,
aspect_h = 32,
2019-03-09 22:30:41 +03:00
length = 1.2,
2019-03-02 14:24:48 +03:00
},
},
},
techage = {
2019-03-08 01:12:07 +03:00
turn_on = turn_power_on,
2019-03-08 23:18:55 +03:00
read_power_consumption = generator.read_power_consumption,
power_network = Axle,
2019-04-28 17:04:45 +03:00
power_side = "R",
2019-03-08 23:18:55 +03:00
animated_power_network = true,
2019-03-02 14:24:48 +03:00
},
2019-03-08 01:12:07 +03:00
2019-03-08 23:18:55 +03:00
after_dig_node = function(pos, oldnode, oldmetadata, digger)
State:after_dig_node(pos, oldnode, oldmetadata, digger)
generator.after_dig_node(pos, oldnode)
end,
2019-03-02 14:24:48 +03:00
2019-03-08 23:18:55 +03:00
after_tube_update = generator.after_tube_update,
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,
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
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
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)
end
})
2019-05-02 00:01:14 +03:00
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")