2019-04-27 18:42:27 +03:00
|
|
|
--[[
|
|
|
|
|
|
|
|
TechAge
|
|
|
|
=======
|
|
|
|
|
|
|
|
Copyright (C) 2019 Joachim Stolberg
|
|
|
|
|
2019-08-22 21:49:47 +03:00
|
|
|
GPL v3
|
2019-04-27 18:42:27 +03:00
|
|
|
See LICENSE.txt for more information
|
|
|
|
|
2019-05-05 23:29:00 +03:00
|
|
|
TA3 Power Station Generator
|
2019-04-27 18:42:27 +03:00
|
|
|
|
|
|
|
]]--
|
|
|
|
|
|
|
|
-- for lazy programmers
|
|
|
|
local M = minetest.get_meta
|
2019-07-02 22:33:12 +03:00
|
|
|
local S = techage.S
|
2019-04-27 18:42:27 +03:00
|
|
|
|
|
|
|
local STANDBY_TICKS = 4
|
|
|
|
local COUNTDOWN_TICKS = 4
|
2019-06-16 22:06:16 +03:00
|
|
|
local CYCLE_TIME = 2
|
|
|
|
local PWR_CAPA = 80
|
2019-04-27 18:42:27 +03:00
|
|
|
|
2019-05-05 23:29:00 +03:00
|
|
|
local Cable = techage.ElectricCable
|
2019-08-17 00:44:11 +03:00
|
|
|
local power = techage.power
|
2019-04-27 18:42:27 +03:00
|
|
|
|
|
|
|
local function formspec(self, pos, mem)
|
|
|
|
return "size[8,7]"..
|
|
|
|
default.gui_bg..
|
|
|
|
default.gui_bg_img..
|
|
|
|
default.gui_slots..
|
2019-08-17 00:44:11 +03:00
|
|
|
"image[6,0.5;1,2;"..power.formspec_power_bar(PWR_CAPA, mem.provided).."]"..
|
2019-04-27 18:42:27 +03:00
|
|
|
"image_button[5,1;1,1;".. self:get_state_button_image(mem) ..";state_button;]"..
|
2019-07-02 22:33:12 +03:00
|
|
|
"button[2,1.5;2,1;update;"..S("Update").."]"..
|
2019-04-27 18:42:27 +03:00
|
|
|
"list[current_player;main;0,3;8,4;]"..
|
|
|
|
default.get_hotbar_bg(0, 3)
|
|
|
|
end
|
|
|
|
|
2019-06-16 22:06:16 +03:00
|
|
|
local function can_start(pos, mem, state)
|
2019-08-17 00:44:11 +03:00
|
|
|
return (mem.firebox_trigger or 0) > 0 -- by means of firebox
|
2019-05-05 23:29:00 +03:00
|
|
|
end
|
|
|
|
|
2019-04-27 18:42:27 +03:00
|
|
|
local function start_node(pos, mem, state)
|
2019-06-16 22:06:16 +03:00
|
|
|
mem.generating = true -- needed for power distribution
|
2019-08-17 00:44:11 +03:00
|
|
|
power.generator_start(pos, mem, PWR_CAPA)
|
2019-04-27 18:42:27 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
local function stop_node(pos, mem, state)
|
2019-06-16 22:06:16 +03:00
|
|
|
mem.generating = false
|
2019-08-17 00:44:11 +03:00
|
|
|
power.generator_stop(pos, mem)
|
2019-06-16 22:06:16 +03:00
|
|
|
mem.provided = 0
|
2019-04-27 18:42:27 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
local State = techage.NodeStates:new({
|
2019-05-05 23:29:00 +03:00
|
|
|
node_name_passive = "techage:generator",
|
|
|
|
node_name_active = "techage:generator_on",
|
2019-04-27 18:42:27 +03:00
|
|
|
cycle_time = CYCLE_TIME,
|
|
|
|
standby_ticks = STANDBY_TICKS,
|
|
|
|
formspec_func = formspec,
|
2019-07-02 22:33:12 +03:00
|
|
|
infotext_name = S("TA3 Generator"),
|
2019-06-16 22:06:16 +03:00
|
|
|
can_start = can_start,
|
2019-04-27 18:42:27 +03:00
|
|
|
start_node = start_node,
|
|
|
|
stop_node = stop_node,
|
|
|
|
})
|
|
|
|
|
|
|
|
local function node_timer(pos, elapsed)
|
|
|
|
local mem = tubelib2.get_mem(pos)
|
2019-08-01 23:31:43 +03:00
|
|
|
if mem.generating then
|
2019-08-17 00:44:11 +03:00
|
|
|
mem.firebox_trigger = (mem.firebox_trigger or 0) - 1
|
|
|
|
if mem.firebox_trigger <= 0 then
|
|
|
|
State:nopower(pos, mem)
|
2019-08-07 23:26:38 +03:00
|
|
|
mem.generating = false
|
2019-08-17 00:44:11 +03:00
|
|
|
power.generator_stop(pos, mem)
|
2019-08-01 23:31:43 +03:00
|
|
|
mem.provided = 0
|
2019-08-17 00:44:11 +03:00
|
|
|
else
|
|
|
|
mem.provided = power.generator_alive(pos, mem)
|
|
|
|
State:keep_running(pos, mem, COUNTDOWN_TICKS)
|
2019-08-01 23:31:43 +03:00
|
|
|
end
|
2019-04-27 18:42:27 +03:00
|
|
|
end
|
2019-08-17 00:44:11 +03:00
|
|
|
return State:is_active(mem)
|
2019-04-27 18:42:27 +03:00
|
|
|
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))
|
2019-06-16 22:06:16 +03:00
|
|
|
if mem.generating then
|
|
|
|
minetest.get_node_timer(pos):start(CYCLE_TIME)
|
|
|
|
end
|
2019-04-27 18:42:27 +03:00
|
|
|
end
|
|
|
|
|
2019-05-05 23:29:00 +03:00
|
|
|
minetest.register_node("techage:generator", {
|
2019-07-02 22:33:12 +03:00
|
|
|
description = S("TA3 Generator"),
|
2019-04-27 18:42:27 +03:00
|
|
|
tiles = {
|
|
|
|
-- up, down, right, left, back, front
|
2019-05-05 23:29:00 +03:00
|
|
|
"techage_filling_ta3.png^techage_frame_ta3.png",
|
|
|
|
"techage_filling_ta3.png^techage_frame_ta3.png",
|
|
|
|
"techage_filling_ta3.png^techage_appl_hole_electric.png^techage_frame_ta3.png",
|
|
|
|
"techage_filling_ta3.png^techage_appl_open.png^techage_frame_ta3.png",
|
|
|
|
"techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_generator.png",
|
|
|
|
"techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_generator.png^[transformFX]",
|
2019-04-27 18:42:27 +03:00
|
|
|
},
|
2019-05-21 17:37:05 +03:00
|
|
|
|
|
|
|
on_construct = tubelib2.init_mem,
|
2019-04-27 18:42:27 +03:00
|
|
|
|
|
|
|
after_place_node = function(pos, placer)
|
2019-08-24 15:12:33 +03:00
|
|
|
local mem = tubelib2.init_mem(pos)
|
2019-06-29 00:46:44 +03:00
|
|
|
local number = techage.add_node(pos, "techage:generator")
|
|
|
|
State:node_init(pos, mem, number)
|
2019-04-27 18:42:27 +03:00
|
|
|
on_rightclick(pos)
|
|
|
|
end,
|
|
|
|
|
|
|
|
on_receive_fields = on_receive_fields,
|
|
|
|
on_rightclick = on_rightclick,
|
|
|
|
on_timer = node_timer,
|
|
|
|
|
|
|
|
paramtype2 = "facedir",
|
|
|
|
groups = {cracky=2, crumbly=2, choppy=2},
|
|
|
|
on_rotate = screwdriver.disallow,
|
|
|
|
is_ground_content = false,
|
|
|
|
sounds = default.node_sound_wood_defaults(),
|
|
|
|
})
|
|
|
|
|
2019-05-05 23:29:00 +03:00
|
|
|
minetest.register_node("techage:generator_on", {
|
2019-07-02 22:33:12 +03:00
|
|
|
description = S("TA3 Generator"),
|
2019-04-27 18:42:27 +03:00
|
|
|
tiles = {
|
|
|
|
-- up, down, right, left, back, front
|
2019-05-06 22:09:13 +03:00
|
|
|
"techage_filling_ta3.png^techage_frame_ta3.png",
|
|
|
|
"techage_filling_ta3.png^techage_frame_ta3.png",
|
|
|
|
"techage_filling_ta3.png^techage_appl_hole_electric.png^techage_frame_ta3.png",
|
|
|
|
"techage_filling_ta3.png^techage_appl_open.png^techage_frame_ta3.png",
|
2019-04-27 18:42:27 +03:00
|
|
|
{
|
2019-05-06 22:09:13 +03:00
|
|
|
image = "techage_filling4_ta3.png^techage_appl_generator4.png^techage_frame4_ta3.png",
|
2019-04-27 18:42:27 +03:00
|
|
|
backface_culling = false,
|
|
|
|
animation = {
|
|
|
|
type = "vertical_frames",
|
|
|
|
aspect_w = 32,
|
|
|
|
aspect_h = 32,
|
2019-05-06 22:09:13 +03:00
|
|
|
length = 0.3,
|
2019-04-27 18:42:27 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-06-17 22:24:48 +03:00
|
|
|
image = "techage_filling4_ta3.png^techage_appl_generator4.png^[transformFX]^techage_frame4_ta3.png",
|
2019-04-27 18:42:27 +03:00
|
|
|
backface_culling = false,
|
|
|
|
animation = {
|
|
|
|
type = "vertical_frames",
|
|
|
|
aspect_w = 32,
|
|
|
|
aspect_h = 32,
|
2019-05-06 22:09:13 +03:00
|
|
|
length = 0.3,
|
2019-04-27 18:42:27 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
on_receive_fields = on_receive_fields,
|
|
|
|
on_rightclick = on_rightclick,
|
|
|
|
on_timer = node_timer,
|
|
|
|
|
2019-05-21 17:37:05 +03:00
|
|
|
drop = "",
|
2019-04-27 18:42:27 +03:00
|
|
|
paramtype2 = "facedir",
|
|
|
|
groups = {not_in_creative_inventory=1},
|
|
|
|
diggable = false,
|
|
|
|
on_rotate = screwdriver.disallow,
|
|
|
|
is_ground_content = false,
|
|
|
|
sounds = default.node_sound_wood_defaults(),
|
|
|
|
})
|
2019-05-05 23:29:00 +03:00
|
|
|
|
|
|
|
minetest.register_craft({
|
|
|
|
output = "techage:generator",
|
|
|
|
recipe = {
|
2019-05-11 17:52:59 +03:00
|
|
|
{"basic_materials:steel_bar", "dye:green", "default:wood"},
|
|
|
|
{"", "basic_materials:gear_steel", "techage:electric_cableS"},
|
2019-05-05 23:29:00 +03:00
|
|
|
{"default:wood", "techage:iron_ingot", "basic_materials:steel_bar"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2019-05-21 17:37:05 +03:00
|
|
|
techage.power.register_node({"techage:generator", "techage:generator_on"}, {
|
|
|
|
conn_sides = {"R"},
|
|
|
|
power_network = Cable,
|
|
|
|
})
|
2019-05-05 23:29:00 +03:00
|
|
|
|
2019-06-08 23:57:01 +03:00
|
|
|
-- for logical communication
|
|
|
|
techage.register_node({"techage:generator", "techage:generator_on"}, {
|
|
|
|
on_transfer = function(pos, in_dir, topic, payload)
|
|
|
|
local mem = tubelib2.get_mem(pos)
|
|
|
|
if topic == "power_level" then
|
|
|
|
local mem = tubelib2.get_mem(pos)
|
|
|
|
mem.power_level = payload
|
2019-06-16 22:06:16 +03:00
|
|
|
elseif topic == "trigger" then
|
2019-08-17 00:44:11 +03:00
|
|
|
mem.firebox_trigger = 3
|
2019-06-16 22:06:16 +03:00
|
|
|
mem.power_level = payload
|
|
|
|
if mem.generating then
|
2019-06-21 00:44:04 +03:00
|
|
|
return math.max((mem.provided or PWR_CAPA) / PWR_CAPA, 0.02)
|
2019-06-16 22:06:16 +03:00
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
2019-06-08 23:57:01 +03:00
|
|
|
end
|
2019-06-29 00:46:44 +03:00
|
|
|
end,
|
|
|
|
on_recv_message = function(pos, topic, payload)
|
|
|
|
local mem = tubelib2.get_mem(pos)
|
|
|
|
if topic == "load" then
|
|
|
|
return techage.power.percent(PWR_CAPA, mem.provided)
|
|
|
|
else
|
|
|
|
return State:on_receive_message(pos, topic, payload)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
on_node_load = function(pos)
|
|
|
|
State:on_node_load(pos)
|
|
|
|
end,
|
2019-06-08 23:57:01 +03:00
|
|
|
})
|
|
|
|
|
2019-07-02 22:33:12 +03:00
|
|
|
techage.register_entry_page("ta3ps", "generator",
|
|
|
|
S("TA3 Generator"),
|
2019-07-06 21:23:11 +03:00
|
|
|
S("Part of the Power Station. Has to be placed side by side with the TA3 Turbine. "..
|
2019-07-02 22:33:12 +03:00
|
|
|
"Connect the Generator with your TA3 machines by means of Electric Cables and Junction Boxes@n"..
|
2019-07-06 21:23:11 +03:00
|
|
|
"(see TA3 Power Station)"),
|
2019-07-02 22:33:12 +03:00
|
|
|
"techage:generator")
|