techage/steam_engine/boiler.lua

284 lines
7.6 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 Boiler
]]--
-- 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("tubelib2")
local I,_ = dofile(MP.."/intllib.lua")
local CYCLE_TIME = 4
local HEAT_STEP = 10
2019-03-04 00:19:30 +03:00
local WATER_CONSUMPTION = 0.5
2019-03-02 14:24:48 +03:00
local MAX_WATER = 10
local POWER = 10
local Water = {
["bucket:bucket_river_water"] = true,
["bucket:bucket_water"] = true,
["bucket:bucket_empty"] = true,
}
local function formspec(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..
default.gui_slots..
"image_button[0,0.2;1,1;techage_form_inventory.png;storage;;true;false;]"..
"list[context;water;1,0.2;1,1;]"..
"image_button[0,1.6;1,1;techage_form_input.png;input;;true;false;]"..
"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:"..
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").."]"..
"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_dig(pos, player)
local inv = M(pos):get_inventory()
local mem = tubelib2.get_mem(pos)
return inv:is_empty("water") and inv:is_empty("input") and not mem.running
end
local function move_to_water(pos)
local inv = M(pos):get_inventory()
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 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
elseif water_stack:get_count() < MAX_WATER then
if water_stack:get_count() == 0 then
water_stack = ItemStack("default:water_source")
else
water_stack:set_count(water_stack:get_count() + 1)
end
input_stack = ItemStack("bucket:bucket_empty")
inv:set_stack("water", 1, water_stack)
inv:set_stack("input", 1, input_stack)
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)
2019-03-02 22:51:16 +03:00
--print("trigger_boiler", mem.fire_trigger, mem.water_level, water_stack:get_count())
2019-03-02 14:24:48 +03:00
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
end
local function allow_metadata_inventory_take(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
if listname == "input" then
return stack:get_count()
end
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
2019-03-02 22:51:16 +03:00
minetest.register_node("techage:boiler1", {
2019-03-02 14:24:48 +03:00
description = I("TA2 Boiler"),
tiles = {"techage_boiler.png"},
drawtype = "mesh",
mesh = "techage_boiler.obj",
selection_box = {
type = "fixed",
2019-03-02 22:51:16 +03:00
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,
sounds = default.node_sound_metal_defaults(),
})
-- boiler2: Main part, needed as generator
minetest.register_node("techage:boiler2", {
description = ("TA2 Boiler"),
tiles = {"techage_boiler2.png"},
drawtype = "mesh",
mesh = "techage_boiler.obj",
selection_box = {
type = "fixed",
fixed = {-10/32, -48/32, -10/32, 10/32, 16/32, 10/32},
2019-03-02 14:24:48 +03:00
},
can_dig = can_dig,
on_timer = node_timer,
allow_metadata_inventory_put = allow_metadata_inventory_put,
allow_metadata_inventory_take = allow_metadata_inventory_take,
on_receive_fields = on_receive_fields,
on_rightclick = on_rightclick,
techage = {
2019-03-02 22:51:16 +03:00
power_network = techage.SteamPipe,
power_consumption = techage.generator_power_consumption,
power_consume = 0,
2019-03-02 14:24:48 +03:00
trigger_boiler = function(pos)
local mem = tubelib2.get_mem(pos)
mem.fire_trigger = true
start_boiler(pos)
end,
2019-03-02 22:51:16 +03:00
power_side = "U",
2019-03-02 14:24:48 +03:00
},
2019-03-02 22:51:16 +03:00
after_place_node = techage.generator_after_place_node,
after_tube_update = techage.generator_after_tube_update,
on_destruct = techage.generator_on_destruct,
2019-03-02 14:24:48 +03:00
on_construct = function(pos)
local inv = M(pos):get_inventory()
inv:set_size('water', 1)
inv:set_size('input', 1)
end,
2019-03-02 22:51:16 +03:00
after_dig_node = function(pos, oldnode)
techage.generator_after_dig_node(pos, oldnode)
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})
end
2019-03-02 14:24:48 +03:00
end,
on_metadata_inventory_put = function(pos)
minetest.after(0.5, move_to_water, pos)
end,
paramtype2 = "facedir",
2019-03-02 22:51:16 +03:00
groups = {cracky=1, not_in_creative_inventory=1},
drop = "techage:boiler1",
2019-03-02 14:24:48 +03:00
on_rotate = screwdriver.disallow,
is_ground_content = false,
sounds = default.node_sound_metal_defaults(),
})