techage/power/gearbox.lua

147 lines
3.4 KiB
Lua
Raw Permalink Normal View History

2020-01-26 01:22:06 +03:00
--[[
TechAge
=======
2021-06-05 12:42:30 +03:00
Copyright (C) 2019-2021 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
TA2 Gearbox
]]--
-- for lazy programmers
local P2S = minetest.pos_to_string
local M = minetest.get_meta
local S = techage.S
local PWR_NEEDED = 1
2021-06-05 12:42:30 +03:00
local CYCLE_TIME = 2
2020-01-26 01:22:06 +03:00
local Axle = techage.Axle
2021-06-05 12:42:30 +03:00
local power = networks.power
2020-01-26 01:22:06 +03:00
-- Axles texture animation
local function switch_axles(pos, on)
2021-06-05 12:42:30 +03:00
for _,outdir in ipairs(networks.get_outdirs(pos, Axle)) do
2020-01-26 01:22:06 +03:00
Axle:switch_tube_line(pos, outdir, on and "on" or "off")
end
end
local function swap_node(pos, name)
local node = techage.get_node_lvm(pos)
if node.name == name then
return
end
node.name = name
minetest.swap_node(pos, node)
end
2021-06-05 12:42:30 +03:00
local function node_timer_on(pos, elapsed)
local consumed = power.consume_power(pos, Axle, 0, PWR_NEEDED)
if consumed == 0 then
swap_node(pos, "techage:gearbox")
switch_axles(pos, false)
end
return true
2020-01-26 01:22:06 +03:00
end
2021-06-05 12:42:30 +03:00
local function node_timer_off(pos, elapsed)
if power.power_available(pos, Axle, 0) then
swap_node(pos, "techage:gearbox_on")
switch_axles(pos, true)
end
2020-01-26 01:22:06 +03:00
return true
end
-- to be able to restart the node after server crashes
local function techage_on_repair(pos)
minetest.get_node_timer(pos):start(CYCLE_TIME)
end
local function after_place_node(pos)
Axle:after_place_node(pos)
minetest.get_node_timer(pos):start(CYCLE_TIME)
end
local function after_dig_node(pos, oldnode)
Axle:after_dig_node(pos)
end
2022-01-03 23:40:31 +03:00
local function tubelib2_on_update2_on(pos, outdir, tlib2, node)
2021-06-05 12:42:30 +03:00
power.update_network(pos, 0, tlib2, node)
switch_axles(pos, true)
2020-01-26 01:22:06 +03:00
end
2022-01-03 23:40:31 +03:00
local function tubelib2_on_update2_off(pos, outdir, tlib2, node)
2021-06-05 12:42:30 +03:00
power.update_network(pos, 0, tlib2, node)
switch_axles(pos, false)
end
2020-01-26 01:22:06 +03:00
minetest.register_node("techage:gearbox", {
description = S("TA2 Gearbox"),
tiles = {"techage_filling_ta2.png^techage_axle_gearbox.png^techage_frame_ta2.png"},
2022-01-03 23:40:31 +03:00
2021-06-05 12:42:30 +03:00
on_timer = node_timer_off,
2020-01-26 01:22:06 +03:00
techage_on_repair = techage_on_repair,
after_place_node = after_place_node,
after_dig_node = after_dig_node,
2021-06-05 12:42:30 +03:00
tubelib2_on_update2 = tubelib2_on_update2_off,
2020-01-26 01:22:06 +03:00
paramtype = "light",
2022-01-03 23:40:31 +03:00
light_source = 0,
2020-01-26 01:22:06 +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:gearbox_on", {
tiles = {
-- up, down, right, left, back, front
{
name = "techage_filling4_ta2.png^techage_axle_gearbox4.png^techage_frame4_ta2.png",
2020-01-26 01:22:06 +03:00
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 0.6,
},
},
},
2022-01-03 23:40:31 +03:00
2021-06-05 12:42:30 +03:00
on_timer = node_timer_on,
2020-01-26 01:22:06 +03:00
techage_on_repair = techage_on_repair,
after_place_node = after_place_node,
after_dig_node = after_dig_node,
2021-06-05 12:42:30 +03:00
tubelib2_on_update2 = tubelib2_on_update2_on,
2020-01-26 01:22:06 +03:00
paramtype2 = "facedir",
groups = {not_in_creative_inventory=1},
diggable = false,
drop = "",
on_rotate = screwdriver.disallow,
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
2021-06-05 12:42:30 +03:00
power.register_nodes({"techage:gearbox", "techage:gearbox_on"}, Axle, "junc")
2022-01-03 23:40:31 +03:00
2021-06-09 23:09:59 +03:00
techage.register_node({"techage:gearbox", "techage:gearbox_on"}, {
on_node_load = function(pos, node)
minetest.get_node_timer(pos):start(CYCLE_TIME)
end,
})
2020-01-26 01:22:06 +03:00
minetest.register_craft({
output = "techage:gearbox 2",
recipe = {
{"default:junglewood", "techage:axle", "default:wood"},
{"techage:axle", "techage:iron_ingot", "techage:axle"},
{"default:wood", "techage:axle", "default:junglewood"},
},
})