turn-off logic added to generator

This commit is contained in:
Joachim Stolberg 2019-03-05 21:20:44 +01:00
parent f7043f4e10
commit ca4eb4c2c7
6 changed files with 182 additions and 139 deletions

View File

@ -171,7 +171,9 @@ function NodeStates:stop(pos, mem)
if self.formspec_func then
M(pos):set_string("formspec", self.formspec_func(self, pos, mem))
end
if minetest.get_node_timer(pos):is_started() then
minetest.get_node_timer(pos):stop()
end
return true
end
return false
@ -205,7 +207,9 @@ function NodeStates:start(pos, mem, called_from_on_timer)
if self.formspec_func then
M(pos):set_string("formspec", self.formspec_func(self, pos, mem))
end
if not minetest.get_node_timer(pos):is_started() then
minetest.get_node_timer(pos):start(self.cycle_time)
end
return true
end
return false
@ -331,7 +335,9 @@ function NodeStates:keep_running(pos, mem, val, num_items)
-- set to RUNNING if not already done
self:start(pos, mem, true)
mem.techage_countdown = val
if self.has_item_meter then
mem.techage_item_meter = mem.techage_item_meter + (num_items or 1)
end
if self.aging_level1 then
local cnt = mem.techage_aging + num_items
mem.techage_aging = cnt
@ -471,12 +477,16 @@ function NodeStates:after_dig_node(pos, oldnode, oldmetadata, digger)
local mem = tubelib2.get_mem(pos)
local inv = minetest.get_inventory({type="player", name=digger:get_player_name()})
local cnt = mem.techage_aging or 0
if self.aging_level1 then
local is_defect = cnt > self.aging_level1 and math.random(self.aging_level2 / cnt) == 1
if self.node_name_defect and is_defect then
inv:add_item("main", ItemStack(self.node_name_defect))
else
inv:add_item("main", ItemStack(self.node_name_passive))
end
else
inv:add_item("main", ItemStack(self.node_name_passive))
end
end
-- Return "full", "loaded", or "empty" depending

View File

@ -16,8 +16,8 @@
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
local TP = function(pos) return minetest.registered_nodes[minetest.get_node(pos).name].techage end
local TN = function(node) return minetest.registered_nodes[node.name].techage end
local TP = function(pos) return (minetest.registered_nodes[minetest.get_node(pos).name] or {}).techage end
local TN = function(node) return (minetest.registered_nodes[node.name] or {}).techage end
-- Table to register the different power distribution network instances for global use
@ -63,6 +63,7 @@ end
-- Calculate the power consumption on the given network
local function power_consumption(pos, dir)
print("power_consumption")
if pos_already_reached(pos) then return 0 end
local mem = tubelib2.get_mem(pos)
local conn = mem.connections or {}

View File

@ -9,7 +9,7 @@ local I,_ = dofile(MP.."/intllib.lua")
local STANDBY_TICKS = 4
local COUNTDOWN_TICKS = 4
local CYCLE_TIME = 2
local CYCLE_TIME = 16
local function formspec(self, pos, mem)
return "size[8,7]"..
@ -43,7 +43,6 @@ local function stop_node(pos, mem, state)
M(pos):set_string("infotext", "Off")
end
local State = techage.NodeStates:new({
node_name_passive = "techage:power",
cycle_time = CYCLE_TIME,
@ -74,6 +73,18 @@ local function node_timer(pos, elapsed)
return State:is_active(mem)
end
local function valid_power_dir(pos, mem, in_dir)
return mem.power_dir == in_dir
end
local function turn_power_on(pos, in_dir, on)
local mem = tubelib2.get_mem(pos)
if State:is_active(mem) and not on then
State:fault(pos, mem)
end
end
local function on_receive_fields(pos, formname, fields, player)
if minetest.is_protected(pos, player:get_player_name()) then
return
@ -111,6 +122,8 @@ minetest.register_node("techage:power", {
power_consumption = techage.generator_power_consumption,
power_network = techage.ElectricCable,
power_consume = 0,
valid_power_dir = valid_power_dir,
turn_on = turn_power_on,
},
after_place_node = function(pos, placer)

View File

@ -19,14 +19,14 @@ dofile(MP.."/basis/trowel.lua") -- hidden networks
dofile(MP.."/basis/junction.lua") -- network junction box
-- Steam Engine
--dofile(MP.."/steam_engine/drive_axle.lua")
--dofile(MP.."/steam_engine/steam_pipe.lua")
--dofile(MP.."/steam_engine/firebox.lua")
--dofile(MP.."/steam_engine/boiler.lua")
dofile(MP.."/steam_engine/drive_axle.lua")
dofile(MP.."/steam_engine/steam_pipe.lua")
dofile(MP.."/steam_engine/firebox.lua")
dofile(MP.."/steam_engine/boiler.lua")
--dofile(MP.."/steam_engine/cylinder.lua")
--dofile(MP.."/steam_engine/flywheel.lua")
--dofile(MP.."/steam_engine/gearbox.lua")
--dofile(MP.."/steam_engine/consumer.lua")
dofile(MP.."/steam_engine/gearbox.lua")
dofile(MP.."/steam_engine/consumer.lua")
dofile(MP.."/electric/electric_cable.lua")
dofile(MP.."/electric/test.lua")

View File

@ -23,6 +23,8 @@ local I,_ = dofile(MP.."/intllib.lua")
local CYCLE_TIME = 4
local STANDBY_TICKS = 2
local COUNTDOWN_TICKS = 2
local HEAT_STEP = 10
local WATER_CONSUMPTION = 0.5
local MAX_WATER = 10
@ -34,9 +36,8 @@ local Water = {
["bucket:bucket_empty"] = true,
}
local function formspec(mem)
local function formspec(self, pos, mem)
local temp = mem.temperature or 20
local button = mem.running and I("Stop") or I("Start")
return "size[8,7]"..
default.gui_bg..
default.gui_bg_img..
@ -47,17 +48,116 @@ local function formspec(mem)
"list[context;input;1,1.6;1,1;]"..
"image[1,1.6;1,1;bucket_water.png]"..
"image[1,1.6;1,1;techage_form_mask.png]"..
"image[3,0.5;1,2;techage_form_temp_bg.png^[lowpart:"..
"image[2,0.5;1,2;techage_form_temp_bg.png^[lowpart:"..
temp..":techage_form_temp_fg.png]"..
"image[4,0.5;1,2;"..techage.generator_formspec_level(mem)..
"button[6,0.5;2,1;start;"..button.."]"..
"button[6,1.5;2,1;update;"..I("Update").."]"..
"image[7,0.5;1,2;"..techage.generator_formspec_level(mem)..
"image_button[6,1;1,1;".. self:get_state_button_image(mem) ..";state_button;]"..
"button[3,1.5;2,1;update;"..I("Update").."]"..
"list[current_player;main;0,3;8,4;]"..
"listring[current_name;water]"..
"listring[current_player;main]"..
default.get_hotbar_bg(0, 3)
end
local function can_start(pos, mem, state)
if mem.temperature >= 80 then
return techage.calc_power_consumption(pos, mem, POWER) > 0
end
return false
end
local function start_node(pos, mem, state)
techage.generator_on(pos, mem)
end
local function stop_node(pos, mem, state)
techage.generator_off(pos, mem)
end
local State = techage.NodeStates:new({
node_name_passive = "techage:boiler2",
cycle_time = CYCLE_TIME,
standby_ticks = STANDBY_TICKS,
has_item_meter = false,
formspec_func = formspec,
can_start = can_start,
start_node = start_node,
stop_node = stop_node,
})
local function get_water(pos)
local inv = M(pos):get_inventory()
local items = inv:get_stack("water", 1)
if items:get_count() > 0 then
local taken = items:take_item(1)
inv:set_stack("water", 1, items)
return true
end
return false
end
local function water_temperature(pos, mem)
mem.temperature = mem.temperature or 20
if mem.fire_trigger then
mem.temperature = math.min(mem.temperature + HEAT_STEP, 100)
else
mem.temperature = math.max(mem.temperature - HEAT_STEP, 20)
end
mem.fire_trigger = false
if mem.water_level == 0 then
if get_water(pos) then
mem.water_level = 100
else
mem.temperature = 20
end
end
return mem.temperature
end
local function steaming(pos, mem, temp)
print("steaming")
mem.water_level = math.max((mem.water_level or 0) - WATER_CONSUMPTION, 0)
if temp >= 80 then
local sum = techage.calc_power_consumption(pos, mem, 8)
if sum > 0 then
State:keep_running(pos, mem, COUNTDOWN_TICKS)
else
State:fault(pos, mem)
end
else
State:stop(pos, mem)
minetest.get_node_timer(pos):start(CYCLE_TIME)
end
end
local function node_timer(pos, elapsed)
local mem = tubelib2.get_mem(pos)
local temp = water_temperature(pos, mem)
print("node_timer", temp)
if State:is_active(mem) then
steaming(pos, mem, temp)
end
return mem.temperature > 20
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
local function can_dig(pos, player)
local inv = M(pos):get_inventory()
local mem = tubelib2.get_mem(pos)
@ -69,13 +169,15 @@ local function move_to_water(pos)
local water_stack = inv:get_stack("water", 1)
local input_stack = inv:get_stack("input", 1)
if input_stack:get_name() == "bucket:bucket_empty" and input_stack:get_count() == 1 then
if input_stack:get_name() == "bucket:bucket_empty" then
if input_stack:get_count() == 1 then
if water_stack:get_count() > 0 then
water_stack:set_count(water_stack:get_count() - 1)
input_stack = ItemStack("bucket:bucket_water")
inv:set_stack("water", 1, water_stack)
inv:set_stack("input", 1, input_stack)
end
end
elseif water_stack:get_count() < MAX_WATER then
if water_stack:get_count() == 0 then
water_stack = ItemStack("default:water_source")
@ -88,25 +190,12 @@ local function move_to_water(pos)
end
end
local function start_boiler(pos)
local mem = tubelib2.get_mem(pos)
mem.water_level = mem.water_level or 0
local inv = M(pos):get_inventory()
local water_stack = inv:get_stack("water", 1)
--print("trigger_boiler", mem.fire_trigger, mem.water_level, water_stack:get_count())
if mem.fire_trigger and (mem.water_level > 0 or water_stack:get_count() > 0) then
if not minetest.get_node_timer(pos):is_started() then
minetest.get_node_timer(pos):start(CYCLE_TIME)
end
end
end
local function allow_metadata_inventory_put(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
if listname == "input" and Water[stack:get_name()] then
start_boiler(pos)
return stack:get_count()
end
return 0
@ -122,77 +211,8 @@ local function allow_metadata_inventory_take(pos, listname, index, stack, player
return 0
end
local function on_receive_fields(pos, formname, fields, player)
if minetest.is_protected(pos, player:get_player_name()) then
return
end
if fields.update then
local mem = tubelib2.get_mem(pos)
M(pos):set_string("formspec", formspec(mem))
end
if fields.start then
local mem = tubelib2.get_mem(pos)
mem.running = not (mem.running or false)
if mem.running then
techage.generator_on(pos, POWER)
else
techage.generator_off(pos)
end
M(pos):set_string("formspec", formspec(mem))
end
end
local function on_rightclick(pos, node, clicker)
local mem = tubelib2.get_mem(pos)
M(pos):set_string("formspec", formspec(mem))
end
local function get_water(pos)
local inv = M(pos):get_inventory()
local items = inv:get_stack("water", 1)
if items:get_count() > 0 then
local taken = items:take_item(1)
inv:set_stack("water", 1, items)
return true
end
return false
end
local function node_timer(pos)
local mem = tubelib2.get_mem(pos)
mem.temperature = mem.temperature or 20
mem.water_level = math.max((mem.water_level or 0) - WATER_CONSUMPTION, 0)
print(mem.fire_trigger, mem.running, mem.temperature, mem.water_level)
if mem.fire_trigger then
mem.temperature = math.min(mem.temperature + HEAT_STEP, 100)
else
mem.temperature = math.max(mem.temperature - HEAT_STEP, 20)
end
if mem.water_level == 0 then
if get_water(pos) then
mem.water_level = 100
else
mem.temperature = 20
end
end
if mem.temperature > 80 and mem.running then
techage.generator_on(pos, POWER)
else
techage.generator_off(pos)
end
mem.fire_trigger = false
return mem.temperature > 20
end
minetest.register_node("techage:boiler1", {
description = I("TA2 Boiler"),
description = I("TA2 Boiler Base"),
tiles = {"techage_boiler.png"},
drawtype = "mesh",
mesh = "techage_boiler.obj",
@ -201,20 +221,7 @@ minetest.register_node("techage:boiler1", {
fixed = {-8/32, -16/32, -8/32, 8/32, 16/32, 8/32},
},
on_construct = function(pos)
local param2 = minetest.get_node(pos).param2
local pos2 = {x=pos.x, y=pos.y+1, z=pos.z}
local node = minetest.get_node(pos2)
if node.name == "air" then
minetest.set_node(pos2, {name="techage:boiler2", param2 = param2})
minetest.registered_nodes["techage:boiler2"].on_construct(pos2)
minetest.registered_nodes["techage:boiler2"].after_place_node(pos2)
on_rightclick(pos2)
end
end,
paramtype2 = "facedir",
--diggable = false,
groups = {cracky=1},
on_rotate = screwdriver.disallow,
is_ground_content = false,
@ -223,7 +230,7 @@ minetest.register_node("techage:boiler1", {
-- boiler2: Main part, needed as generator
minetest.register_node("techage:boiler2", {
description = ("TA2 Boiler"),
description = I("TA2 Boiler Top"),
tiles = {"techage_boiler2.png"},
drawtype = "mesh",
mesh = "techage_boiler.obj",
@ -240,42 +247,54 @@ minetest.register_node("techage:boiler2", {
on_rightclick = on_rightclick,
techage = {
power_network = techage.SteamPipe,
power_consumption = techage.generator_power_consumption,
power_network = techage.SteamPipe,
power_consume = 0,
trigger_boiler = function(pos)
local mem = tubelib2.get_mem(pos)
mem.fire_trigger = true
start_boiler(pos)
if not minetest.get_node_timer(pos):is_started() then
minetest.get_node_timer(pos):start(CYCLE_TIME)
end
end,
power_side = "U",
},
after_place_node = techage.generator_after_place_node,
after_tube_update = techage.generator_after_tube_update,
on_destruct = techage.generator_on_destruct,
on_construct = function(pos)
local inv = M(pos):get_inventory()
inv:set_size('water', 1)
inv:set_size('input', 1)
end,
after_dig_node = function(pos, oldnode)
techage.generator_after_dig_node(pos, oldnode)
after_place_node = function(pos, placer)
local mem = techage.generator_after_place_node(pos)
State:node_init(pos, mem, "")
local node = minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z})
if node.name == "techage:boiler1" then
minetest.remove_node({x=pos.x, y=pos.y-1, z=pos.z})
on_rightclick(pos)
end
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
State:after_dig_node(pos, oldnode, oldmetadata, digger)
techage.generator_after_dig_node(pos, oldnode)
end,
after_tube_update = techage.generator_after_tube_update,
on_destruct = techage.generator_on_destruct,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
State:after_dig_node(pos, oldnode, oldmetadata, digger)
techage.generator_after_dig_node(pos, oldnode)
end,
on_metadata_inventory_put = function(pos)
minetest.after(0.5, move_to_water, pos)
end,
paramtype2 = "facedir",
groups = {cracky=1, not_in_creative_inventory=1},
drop = "techage:boiler1",
groups = {cracky=1},
on_rotate = screwdriver.disallow,
is_ground_content = false,
sounds = default.node_sound_metal_defaults(),

View File

@ -16,8 +16,8 @@
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
local TP = function(pos) return minetest.registered_nodes[minetest.get_node(pos).name].techage end
local TN = function(node) return minetest.registered_nodes[node.name].techage end
local TP = function(pos) return (minetest.registered_nodes[minetest.get_node(pos).name] or {}).techage end
local TN = function(node) return (minetest.registered_nodes[node.name] or {}).techage end
-- Load support for intllib.
local MP = minetest.get_modpath("tubelib2")