lamp3 and power switch box added

This commit is contained in:
Joachim Stolberg 2019-06-15 16:14:46 +02:00
parent da59f572fa
commit 3967788921
25 changed files with 840 additions and 579 deletions

View File

@ -21,6 +21,38 @@ local M = minetest.get_meta
local MP = minetest.get_modpath("techage") local MP = minetest.get_modpath("techage")
local I,_ = dofile(MP.."/intllib.lua") local I,_ = dofile(MP.."/intllib.lua")
-- Input data to generate the Param2ToDir table
local Input = {
8,9,10,11, -- 1
16,17,18,19, -- 2
4,5,6,7, -- 3
12,13,14,15, -- 4
0,1,2,3, -- 5
20,21,22,23, -- 6
}
-- translation from param2 to dir (out of the node upwards)
local Param2Dir = {}
for idx,val in ipairs(Input) do
Param2Dir[val] = math.floor((idx - 1) / 4) + 1
end
-- used by lamps and power switches
function techage.determine_node_bottom_as_dir(node)
return tubelib2.Turn180Deg[Param2Dir[node.param2] or 1]
end
function techage.determine_node_top_as_dir(node)
return Param2Dir[node.param2] or 1
end
-- rotation rules (screwdriver) for wallmounted "facedir" nodes
function techage.rotate_wallmounted(param2)
local offs = math.floor(param2 / 4) * 4
local rot = ((param2 % 4) + 1) % 4
return offs + rot
end
function techage.range(val, min, max) function techage.range(val, min, max)
val = tonumber(val) val = tonumber(val)
if val < min then return min end if val < min then return min end

View File

@ -187,7 +187,7 @@ minetest.register_craft({
output = "techage:ta3_booster", output = "techage:ta3_booster",
recipe = { recipe = {
{"basic_materials:steel_bar", "default:wood", "basic_materials:steel_bar"}, {"basic_materials:steel_bar", "default:wood", "basic_materials:steel_bar"},
{"techage:steam_pipeS", "basic_materials:gear_steel", "techage:steam_pipeS"}, {"", "basic_materials:gear_steel", ""},
{"basic_materials:steel_bar", "default:wood", "basic_materials:steel_bar"}, {"basic_materials:steel_bar", "default:wood", "basic_materials:steel_bar"},
}, },
}) })

View File

@ -47,6 +47,7 @@ else
-- Power networks -- Power networks
dofile(MP.."/power/power.lua") dofile(MP.."/power/power.lua")
dofile(MP.."/power/power2.lua")
dofile(MP.."/power/junction.lua") dofile(MP.."/power/junction.lua")
dofile(MP.."/power/drive_axle.lua") dofile(MP.."/power/drive_axle.lua")
dofile(MP.."/power/steam_pipe.lua") dofile(MP.."/power/steam_pipe.lua")
@ -119,6 +120,7 @@ else
dofile(MP.."/lamps/ceilinglamp.lua") dofile(MP.."/lamps/ceilinglamp.lua")
dofile(MP.."/lamps/industriallamp1.lua") dofile(MP.."/lamps/industriallamp1.lua")
dofile(MP.."/lamps/industriallamp2.lua") dofile(MP.."/lamps/industriallamp2.lua")
dofile(MP.."/lamps/industriallamp3.lua")
-- Oil -- Oil
dofile(MP.."/oil/help.lua") dofile(MP.."/oil/help.lua")
@ -132,17 +134,10 @@ else
dofile(MP.."/nodes/basalt.lua") dofile(MP.."/nodes/basalt.lua")
end end
-- Test
--dofile(MP.."/test/generator.lua") dofile(MP.."/recipe_checker.lua")
--dofile(MP.."/test/lamp.lua") dofile(MP.."/test/sink.lua")
-- dofile(MP.."/test/consumer.lua") dofile(MP.."/test/source.lua")
--dofile(MP.."/test/consumer2.lua") dofile(MP.."/test/akku.lua")
--dofile(MP.."/test/test.lua") dofile(MP.."/test/switch.lua")
--dofile(MP.."/fermenter/gasflare.lua")
--dofile(MP.."/nodes/test.lua")
--dofile(MP.."/mechanic/perf_test.lua")
end end

66
lamps/industriallamp3.lua Normal file
View File

@ -0,0 +1,66 @@
--[[
TechAge
=======
Copyright (C) 2019 Joachim Stolberg
LGPLv2.1+
See LICENSE.txt for more information
TA3/TA4 Industrial Lamp 3
]]--
-- Load support for intllib.
local MP = minetest.get_modpath("techage")
local I,_ = dofile(MP.."/intllib.lua")
local size = {x = 6/32, y = 4/32, z = 6/32}
techage.register_lamp("techage:industriallamp3", {
description = "TA Industrial Lamp 3",
inventory_image = 'techage_industriallamp_inv3.png',
tiles = {
-- up, down, right, left, back, front
'techage_industriallamp3.png',
'techage_industriallamp3.png',
'techage_industriallamp3.png^[transformR180',
'techage_industriallamp3.png^[transformR180',
'techage_industriallamp3.png',
'techage_industriallamp3.png',
},
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{-0.1875, -0.5, -0.1875, 0.1875, -0.375, 0.1875},
},
},
},{
tiles = {
-- up, down, right, left, back, front
'techage_industriallamp3_on.png',
'techage_industriallamp3_on.png',
'techage_industriallamp3_on.png^[transformR180',
'techage_industriallamp3_on.png^[transformR180',
'techage_industriallamp3_on.png',
'techage_industriallamp3_on.png',
},
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{-0.1875, -0.5, -0.1875, 0.1875, -0.375, 0.1875},
},
},
})
minetest.register_craft({
output = "techage:industriallamp3_off 2",
recipe = {
{"default:glass", "default:glass", ""},
{"techage:simplelamp_off", "dye:red", ""},
{"basic_materials:steel_bar", "basic_materials:steel_bar", ""},
},
})

View File

@ -7,27 +7,6 @@ local POWER_CONSUMPTION = 0.5
local Power = techage.ElectricCable local Power = techage.ElectricCable
-- Input data to generate the Param2ToDir table
local Input = {
8,9,10,11, -- 1
16,17,18,19, -- 2
4,5,6,7, -- 3
12,13,14,15, -- 4
0,1,2,3, -- 5
20,21,22,23, -- 6
}
local Param2Dir = {}
for idx,val in ipairs(Input) do
Param2Dir[val] = math.floor((idx - 1) / 4) + 1
end
local function rotate(param2)
local offs = math.floor(param2 / 4) * 4
local rot = ((param2 % 4) + 1) % 4
return offs + rot
end
local function swap_node(pos, postfix) local function swap_node(pos, postfix)
local node = techage.get_node_lvm(pos) local node = techage.get_node_lvm(pos)
local parts = string.split(node.name, "_") local parts = string.split(node.name, "_")
@ -74,7 +53,7 @@ local function on_rotate(pos, node, user, mode, new_param2)
if minetest.is_protected(pos, user:get_player_name()) then if minetest.is_protected(pos, user:get_player_name()) then
return false return false
end end
node.param2 = rotate(node.param2) node.param2 = techage.rotate_wallmounted(node.param2)
minetest.swap_node(pos, node) minetest.swap_node(pos, node)
return true return true
end end
@ -87,7 +66,7 @@ local function on_place(itemstack, placer, pointed_thing)
end end
local function determine_power_side(pos, node) local function determine_power_side(pos, node)
return {tubelib2.Turn180Deg[Param2Dir[node.param2] or 1]} return {techage.determine_node_bottom_as_dir(node)}
end end
function techage.register_lamp(basename, ndef_off, ndef_on) function techage.register_lamp(basename, ndef_off, ndef_on)

View File

@ -39,6 +39,7 @@ techage.register_junction("techage:electric_junction", 2/8, Boxes, techage.Elect
after_tube_update = function(node, pos, out_dir, peer_pos, peer_in_dir) after_tube_update = function(node, pos, out_dir, peer_pos, peer_in_dir)
local name = "techage:electric_junction"..techage.junction_type(pos, techage.ElectricCable) local name = "techage:electric_junction"..techage.junction_type(pos, techage.ElectricCable)
minetest.swap_node(pos, {name = name, param2 = 0}) minetest.swap_node(pos, {name = name, param2 = 0})
techage.power2.on_network_change(pos) ------------ TODO
end, end,
}) })

View File

@ -74,7 +74,6 @@ local function valid_indir(pos, in_dir)
end end
end end
if s ~= "" then if s ~= "" then
local tbl
for _,dir in ipairs(minetest.deserialize(s)) do for _,dir in ipairs(minetest.deserialize(s)) do
if dir == in_dir then if dir == in_dir then
return true return true
@ -101,15 +100,16 @@ local function get_clbk(pos, clbk_name)
end end
local function connection_walk(pos, clbk_name, sum) local function connection_walk(pos, clbk_name, sum)
local mem = tubelib2.get_mem(pos)
mem.interrupted_dirs = mem.interrupted_dirs or {}
local clbk = get_clbk(pos, clbk_name) local clbk = get_clbk(pos, clbk_name)
--print("connection_walk", S(pos), sum, clbk) --print("connection_walk", S(pos), sum, clbk)
if clbk then if clbk then
local mem = tubelib2.get_mem(pos)
sum = sum - (clbk(pos, mem, sum) or 0) sum = sum - (clbk(pos, mem, sum) or 0)
end end
local mem = tubelib2.get_mem(pos) for out_dir,item in pairs(mem.connections or {}) do
for _,item in pairs(mem.connections or {}) do if item.pos and not pos_already_reached(item.pos) and
if item.pos and not pos_already_reached(item.pos) then not mem.interrupted_dirs[out_dir] then
sum = connection_walk(item.pos, clbk_name, sum) sum = connection_walk(item.pos, clbk_name, sum)
end end
end end
@ -155,28 +155,31 @@ techage.power = {}
techage.power.power_distribution = power_distribution techage.power.power_distribution = power_distribution
-- User to turn on/off the power by means of a power switch -- Used to turn on/off the power by means of a power switch
function techage.power.power_cut(pos, dir, cable, cut) function techage.power.power_cut(pos, dir, cable, cut)
local npos = vector.add(pos, tubelib2.Dir6dToVector[dir or 0]) local npos = vector.add(pos, tubelib2.Dir6dToVector[dir or 0])
local meta = M(npos)
local node = minetest.get_node(npos)
if node.name ~= "techage:powerswitch_box" and
M(npos):get_string("techage_hidden_nodename") ~= "techage:powerswitch_box" then
return
end
local mem = tubelib2.get_mem(npos)
mem.interrupted_dirs = mem.interrupted_dirs or {}
if cut then if cut then
local param2 = meta:get_int("tl2_param2") mem.interrupted_dirs = {true, true, true, true, true, true}
if param2 ~= 0 then for dir,_ in pairs(mem.connections) do
meta:set_int("cable_cut", param2) mem.interrupted_dirs[dir] = false
meta:set_int("tl2_param2", 0) power_distribution(npos)
cable:after_dig_tube(npos, {param2 = param2}) mem.interrupted_dirs[dir] = true
end end
else else
local param2 = meta:get_int("cable_cut") mem.interrupted_dirs = {}
if param2 ~= 0 then power_distribution(npos)
meta:set_int("tl2_param2", param2)
meta:set_int("cable_cut", 0)
cable:tool_repair_tube(npos)
end end
end end
end
function techage.power.register_node(names, pwr_def) function techage.power.register_node(names, pwr_def)
for _,name in ipairs(names) do for _,name in ipairs(names) do

330
power/power2.lua Normal file
View File

@ -0,0 +1,330 @@
--[[
TechAge
=======
Copyright (C) 2019 Joachim Stolberg
LGPLv2.1+
See LICENSE.txt for more information
Power distribution and consumption calculation
for any kind of power distribution network
]]--
-- 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
-- Techage Related Data
local PWR = function(pos) return (minetest.registered_nodes[minetest.get_node(pos).name] or {}).power end
local PWRN = function(node) return (minetest.registered_nodes[node.name] or {}).power end
-- Used to determine the already passed nodes while power distribution
local Route = {}
local function pos_already_reached(pos)
local key = minetest.hash_node_position(pos)
if not Route[key] then
Route[key] = true
return false
end
return true
end
local SideToDir = {B=1, R=2, F=3, L=4, D=5, U=6}
local function side_to_dir(param2, side)
local dir = SideToDir[side]
if dir < 5 then
dir = (((dir - 1) + (param2 % 4)) % 4) + 1
end
return dir
end
--function techage.get_pos(pos, side)
-- local node = minetest.get_node(pos)
-- local dir = nil
-- if node.name ~= "air" and node.name ~= "ignore" then
-- dir = side_to_dir(node.param2, side)
-- end
-- return tubelib2.get_pos(pos, dir)
--end
local function set_conn_dirs(pos, sides)
local tbl = {}
local node = minetest.get_node(pos)
if type(sides) == "function" then
tbl = sides(pos, node)
else
for _,side in ipairs(sides) do
tbl[#tbl+1] = tubelib2.Turn180Deg[side_to_dir(node.param2, side)]
end
end
M(pos):set_string("power_dirs", minetest.serialize(tbl))
end
local function valid_indir(pos, in_dir)
local s = M(pos):get_string("power_dirs")
if s == "" then
local pwr = PWR(pos)
if pwr then
set_conn_dirs(pos, pwr.conn_sides)
end
end
if s ~= "" then
for _,dir in ipairs(minetest.deserialize(s)) do
if dir == in_dir then
return true
end
end
end
return false
end
local function valid_outdir(pos, out_dir)
return valid_indir(pos, tubelib2.Turn180Deg[out_dir])
end
-- Both nodes are from the same power network type?
local function matching_nodes(pos, peer_pos)
local tube_type1 = pos and PWR(pos) and PWR(pos).power_network.tube_type
local tube_type2 = peer_pos and PWR(peer_pos) and PWR(peer_pos).power_network.tube_type
return not tube_type1 or not tube_type2 or tube_type1 == tube_type2
end
local function connection_walk(pos, clbk)
local mem = tubelib2.get_mem(pos)
mem.interrupted_dirs = mem.interrupted_dirs or {}
if clbk then
clbk(pos, mem)
end
for out_dir,item in pairs(mem.connections or {}) do
if item.pos and not pos_already_reached(item.pos) and
not mem.interrupted_dirs[out_dir] then
connection_walk(item.pos, clbk)
end
end
end
-- determine one "generating" node as master (largest hash number)
local function determine_master(pos)
Route = {}
pos_already_reached(pos)
local hash = 0
local master = nil
connection_walk(pos, function(pos, mem)
if mem.generating then
local new = minetest.hash_node_position(pos)
if hash <= new then
hash = new
master = pos
end
end
end)
return master
end
-- store master position on all network nodes
local function store_master(pos, master_pos)
Route = {}
pos_already_reached(pos)
connection_walk(pos, function(pos, mem)
mem.master_pos = master_pos
mem.is_master = false
end)
end
-- called from master every 2 seconds
local function accounting(mem)
mem.debit2 = mem.debit1 or 0
mem.credit2 = mem.credit1 or 0
mem.secondary = mem.secondary or 0
mem.gap = mem.debit2 - mem.credit2
mem.credit2 = mem.credit2 + mem.secondary
--print("needed = "..(mem.debit2 or 0)..", available = "..(mem.credit2 or 0)..", gap = "..mem.gap..", secondary = "..mem.secondary)
mem.debit1 = 0
mem.credit1 = 0
mem.secondary = 0
end
-- called from any generator
local function on_power_switch(pos)
print("on_power_change"..S(pos))
local mem = tubelib2.get_mem(pos)
mem.master_pos = nil
mem.is_master = nil
local mpos = determine_master(pos)
store_master(pos, mpos)
if mpos then
print("master = "..S(mpos))
local mem = tubelib2.get_mem(mpos)
mem.is_master = true
return mem
end
end
-- called from tubelib2.after_tube_update
local function on_network_change(pos)
local mem = on_power_switch(pos)
if mem then
accounting(mem)
end
end
--
-- Generic API functions
--
techage.power2 = {}
techage.power2.power_switched = on_power_switch
techage.power2.on_network_change = on_network_change
function techage.power2.register_node(names, pwr_def)
for _,name in ipairs(names) do
local ndef = minetest.registered_nodes[name]
if ndef then
minetest.override_item(name, {
power = {
conn_sides = pwr_def.conn_sides or {"L", "R", "U", "D", "F", "B"},
power_network = pwr_def.power_network,
after_place_node = ndef.after_place_node,
after_dig_node = ndef.after_dig_node,
after_tube_update = ndef.after_tube_update,
},
-- after_place_node decorator
after_place_node = function(pos, placer, itemstack, pointed_thing)
local pwr = PWR(pos)
set_conn_dirs(pos, pwr.conn_sides)
pwr.power_network:after_place_node(pos)
if pwr.after_place_node then
return pwr.after_place_node(pos, placer, itemstack, pointed_thing)
end
end,
-- after_dig_node decorator
after_dig_node = function(pos, oldnode, oldmetadata, digger)
local pwr = PWRN(oldnode)
pwr.power_network:after_dig_node(pos)
minetest.after(0.1, tubelib2.del_mem, pos) -- At latest...
if pwr.after_dig_node then
return pwr.after_dig_node(pos, oldnode, oldmetadata, digger)
end
end,
-- tubelib2 callback, called after any connection change
after_tube_update = function(node, pos, out_dir, peer_pos, peer_in_dir)
local pwr = PWR(pos)
local mem = tubelib2.get_mem(pos)
mem.connections = mem.connections or {}
if not peer_pos or not valid_indir(peer_pos, peer_in_dir)
or not valid_outdir(pos, out_dir)
or not matching_nodes(pos, peer_pos) then
mem.connections[out_dir] = nil -- del connection
else
mem.connections[out_dir] = {pos = peer_pos, in_dir = peer_in_dir}
end
-- To be called delayed, so that all network connections have been established
minetest.after(0.2, on_network_change, pos)
if pwr.after_tube_update then
return pwr.after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir)
end
end,
})
pwr_def.power_network:add_secondary_node_names({name})
end
end
end
function techage.power2.consume_power(pos, needed)
local master_pos = tubelib2.get_mem(pos).master_pos
if master_pos then
local mem = tubelib2.get_mem(master_pos)
mem.debit1 = mem.debit1 or 0
mem.credit2 = mem.credit2 or 0
mem.debit1 = mem.debit1 + needed
if mem.credit2 >= needed then
mem.credit2 = mem.credit2 - needed
return needed
end
end
return 0
end
function techage.power2.provide_power(pos, available)
local mem = tubelib2.get_mem(pos)
if mem.is_master then
accounting(mem)
elseif mem.master_pos then
mem = tubelib2.get_mem(mem.master_pos)
else
return 0
end
mem.credit1 = mem.credit1 or 0
mem.debit2 = mem.debit2 or 0
mem.credit1 = mem.credit1 + available
if mem.debit2 > available then
mem.debit2 = mem.debit2 - available
return available
else
local rest = mem.debit2
mem.debit2 = 0
return rest
end
end
function techage.power2.secondary_power(pos, provide, consume)
local mem = tubelib2.get_mem(pos)
if mem.is_master then
accounting(mem)
elseif mem.master_pos then
mem = tubelib2.get_mem(mem.master_pos)
else
return 0
end
mem.gap = mem.gap or 0
mem.secondary = (mem.secondary or 0) + provide
if mem.gap > 0 then
local val = math.min(mem.gap, provide)
mem.gap = mem.gap - val
return val
elseif mem.gap < 0 then
return math.max(mem.gap, -consume)
end
return 0
-- mem.debit2 = mem.debit2 or 0
-- mem.credit1 = mem.credit1 + available
-- if mem.debit2 > available then
-- mem.debit2 = mem.debit2 - available
-- return available
-- else
-- local rest = mem.debit2
-- mem.debit2 = 0
-- return rest
-- end
end
function techage.power2.formspec_load_bar(charging, max_val)
local percent
charging = charging or 0
max_val = max_val or 1
if charging ~= 0 then
percent = 50 + math.ceil((charging * 50.0) / max_val)
end
if charging > 0 then
return "techage_form_level_off.png^[lowpart:"..percent..":techage_form_level_charge.png"
elseif charging < 0 then
return "techage_form_level_unload.png^[lowpart:"..percent..":techage_form_level_off.png"
else
return "techage_form_level_off.png"
end
end

View File

@ -74,6 +74,7 @@ minetest.register_node("techage:powerswitch", {
type = "fixed", type = "fixed",
fixed = { fixed = {
{ -1/4, -8/16, -1/4, 1/4, -7/16, 1/4}, { -1/4, -8/16, -1/4, 1/4, -7/16, 1/4},
{ -1/6, -12/16, -1/6, 1/6, -8/16, 1/6},
}, },
}, },
@ -103,6 +104,7 @@ minetest.register_node("techage:powerswitch_on", {
type = "fixed", type = "fixed",
fixed = { fixed = {
{ -1/4, -8/16, -1/4, 1/4, -7/16, 1/4}, { -1/4, -8/16, -1/4, 1/4, -7/16, 1/4},
{ -1/6, -12/16, -1/6, 1/6, -8/16, 1/6},
}, },
}, },
@ -120,6 +122,53 @@ minetest.register_node("techage:powerswitch_on", {
sounds = default.node_sound_wood_defaults(), sounds = default.node_sound_wood_defaults(),
}) })
local function on_place(itemstack, placer, pointed_thing)
if pointed_thing.type ~= "node" then
return itemstack
end
return minetest.rotate_and_place(itemstack, placer, pointed_thing)
end
local function on_rotate(pos, node, user, mode, new_param2)
if minetest.is_protected(pos, user:get_player_name()) then
return false
end
node.param2 = techage.rotate_wallmounted(node.param2)
minetest.swap_node(pos, node)
return true
end
minetest.register_node("techage:powerswitch_box", {
description = I("TA Power Switch Box"),
tiles = {
-- up, down, right, left, back, front
'techage_electric_switch.png',
'techage_electric_switch.png',
'techage_electric_junction.png',
'techage_electric_junction.png',
'techage_electric_switch.png',
'techage_electric_switch.png',
},
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{ -2/4, -1/4, -1/4, 2/4, 1/4, 1/4},
},
},
on_place = on_place,
on_rotate = on_rotate,
paramtype2 = "facedir",
groups = {choppy=2, cracky=2, crumbly=2, techage_trowel = 1},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
techage.power.register_node({"techage:powerswitch_box"}, {
power_network = techage.ElectricCable})
minetest.register_craft({ minetest.register_craft({
output = "techage:powerswitch 2", output = "techage:powerswitch 2",
recipe = { recipe = {

34
recipe_checker.lua Normal file
View File

@ -0,0 +1,34 @@
--
-- Script to check recipe overlaps
--
local Recipes = {}
local function recipe_key(items)
local tbl = {}
for idx = 1,9 do
tbl[#tbl + 1] = items[idx] or "#"
end
return table.concat(tbl, "-")
end
minetest.after(1, function()
for name,_ in pairs(minetest.registered_items) do
local mod = string.split(name, ":")[1]
if mod == "techage" or mod == "signs_bot" then
local recipes = minetest.get_all_craft_recipes(name)
if recipes then
for _,recipe in ipairs(recipes) do
if recipe and recipe.items then
--print(dump(recipe.items))
local key = recipe_key(recipe.items)
if Recipes[key] then
local text = Recipes[key].." and "..name.." have the same incredients"
minetest.log("error", text)
end
Recipes[key] = name
end
end
end
end
end
end)

104
test/akku.lua Normal file
View File

@ -0,0 +1,104 @@
-- 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
local CYCLE_TIME = 2
local PWR_PERF = 10
local PWR_CAPA = 300
local Cable = techage.ElectricCable
local secondary_power = techage.power2.secondary_power
local function in_range(val, min, max)
if val < min then return min end
if val > max then return max end
return val
end
local function formspec(pos, mem)
return "size[5,3]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"image[0,0.5;1,2;"..techage.power.formspec_power_bar(PWR_CAPA, mem.capa or 0).."]"..
"label[0.2,2.5;Load]"..
"button[1.1,1;1.8,1;update;Update]"..
"image[4,0.5;1,2;"..techage.power2.formspec_load_bar(-(mem.delivered or 0), PWR_PERF).."]"..
"label[4.2,2.5;Flow]"
end
local function node_timer(pos, elapsed)
--print("node_timer akku "..S(pos))
local mem = tubelib2.get_mem(pos)
mem.capa = mem.capa or 0
if mem.generating then
local delivered
if mem.capa >= PWR_CAPA then
delivered = secondary_power(pos, PWR_PERF, 0)
elseif mem.capa <= 0 then
delivered = secondary_power(pos, 0, PWR_PERF)
else
delivered = secondary_power(pos, PWR_PERF, PWR_PERF)
end
if delivered ~= mem.delivered then
mem.delivered = delivered
--techage.power2.power_switched(pos)
end
mem.capa = mem.capa - mem.delivered
mem.capa = in_range(mem.capa, 0, PWR_CAPA)
--print("delivered = "..mem.delivered)
return true
end
return false
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(pos, mem))
end
end
local function on_rightclick(pos)
local mem = tubelib2.get_mem(pos)
M(pos):set_string("formspec", formspec(pos, mem))
end
local function after_place_node(pos, placer)
local mem = tubelib2.get_mem(pos)
mem.generating = true
mem.capa = 0
minetest.get_node_timer(pos):start(CYCLE_TIME)
techage.power2.power_switched(pos)
on_rightclick(pos)
end
minetest.register_node("techage:akku", {
description = "Akku",
tiles = {
-- up, down, right, left, back, front
'techage_electric_button.png^techage_appl_source.png',
'techage_electric_button.png^techage_appl_source.png',
'techage_electric_button.png^techage_appl_source.png^techage_electric_plug.png',
'techage_electric_button.png^techage_appl_source.png',
'techage_electric_button.png^techage_appl_source.png',
'techage_electric_button.png^techage_appl_source.png',
},
paramtype2 = "facedir",
groups = {cracky=2, crumbly=2, choppy=2},
is_ground_content = false,
after_place_node = after_place_node,
on_receive_fields = on_receive_fields,
on_rightclick = on_rightclick,
on_timer = node_timer,
})
techage.power2.register_node({"techage:akku"}, {
power_network = Cable,
})

View File

@ -1,159 +0,0 @@
-- 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("techage")
local I,_ = dofile(MP.."/intllib.lua")
local POWER_CONSUMPTION = 2
local STANDBY_TICKS = 4
local CYCLE_TIME = 4
local Cable = techage.ElectricCable
local consumer = techage.consumer
local function formspec(self, pos, mem)
return "size[8,7]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"image_button[3,1.2;1,1;".. self:get_state_button_image(mem) ..";state_button;]"..
"list[current_player;main;0,3;8,4;]"..
default.get_hotbar_bg(0, 3)
end
local function valid_power_dir(pos, power_dir, in_dir)
return power_dir == in_dir
end
local function start_node(pos, mem, state)
consumer.turn_power_on(pos, POWER_CONSUMPTION)
end
local function stop_node(pos, mem, state)
consumer.turn_power_on(pos, 0)
end
local State = techage.NodeStates:new({
node_name_passive = "techage:ele_consumer",
node_name_active = "techage:ele_consumer_on",
cycle_time = CYCLE_TIME,
standby_ticks = STANDBY_TICKS,
formspec_func = formspec,
start_node = start_node,
stop_node = stop_node,
})
local function lamp_turn_on_clbk(pos, in_dir, sum)
local mem = tubelib2.get_mem(pos)
local state = State:get_state(mem)
if sum > 0 and state == techage.FAULT then
State:stop(pos, mem)
State:start(pos, mem)
elseif sum <= 0 and state == techage.RUNNING then
State:fault(pos, mem)
end
end
local function node_timer(pos, elapsed)
local mem = tubelib2.get_mem(pos)
return State:is_active(mem)
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
minetest.register_node("techage:ele_consumer", {
description = "Consumer",
tiles = {
-- up, down, right, left, back, front
'techage_electric_button.png',
'techage_electric_button.png',
'techage_electric_button.png',
'techage_electric_button.png^techage_electric_plug.png',
'techage_electric_button.png',
'techage_electric_button.png',
},
techage = {
turn_on = lamp_turn_on_clbk,
read_power_consumption = consumer.read_power_consumption,
power_network = Cable,
power_side = "L",
valid_power_dir = valid_power_dir,
},
after_place_node = function(pos, placer)
local mem = consumer.after_place_node(pos, placer)
State:node_init(pos, mem, "")
on_rightclick(pos)
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
State:after_dig_node(pos, oldnode, oldmetadata, digger)
consumer.after_dig_node(pos, oldnode)
end,
after_tube_update = consumer.after_tube_update,
on_timer = node_timer,
on_receive_fields = on_receive_fields,
on_rightclick = on_rightclick,
paramtype = "light",
light_source = 0,
sunlight_propagates = true,
paramtype2 = "facedir",
groups = {choppy=2, cracky=2, crumbly=2},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
minetest.register_node("techage:ele_consumer_on", {
description = "Consumer",
tiles = {
'techage_electric_button.png',
},
techage = {
turn_on = lamp_turn_on_clbk,
read_power_consumption = consumer.read_power_consumption,
power_network = Cable,
power_side = "L",
valid_power_dir = valid_power_dir,
},
after_dig_node = function(pos, oldnode, oldmetadata, digger)
State:after_dig_node(pos, oldnode, oldmetadata, digger)
consumer.after_dig_node(pos, oldnode)
end,
after_tube_update = consumer.after_tube_update,
on_timer = node_timer,
on_receive_fields = on_receive_fields,
on_rightclick = on_rightclick,
paramtype = "light",
light_source = LIGHT_MAX,
sunlight_propagates = true,
paramtype2 = "facedir",
drop = "techage:ele_consumer",
groups = {choppy=2, cracky=2, crumbly=2, not_in_creative_inventory=1},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})

View File

@ -1,140 +0,0 @@
--[[
TechAge
=======
Copyright (C) 2019 Joachim Stolberg
LGPLv2.1+
See LICENSE.txt for more information
TA2 Consumer
]]--
-- 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
local POWER_CONSUMPTION = 4
local Axle = techage.Axle
local consumer = techage.consumer
local function swap_node(pos, name)
local node = minetest.get_node(pos)
if node.name == name then
return
end
node.name = name
minetest.swap_node(pos, node)
end
-- To be able to check if power connection is on the
-- correct node side (power_dir == in_dir)
local function valid_power_dir(pos, power_dir, in_dir)
return power_dir == in_dir
end
local function turn_on_clbk(pos, in_dir, sum)
local mem = tubelib2.get_mem(pos)
if sum > 0 then
swap_node(pos, "techage:consumer_on")
else
swap_node(pos, "techage:consumer")
end
end
minetest.register_node("techage:consumer", {
description = "TechAge Consumer",
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_frame_ta2.png',
'techage_filling_ta2.png^techage_frame_ta2.png^techage_axle_clutch.png',
'techage_filling_ta2.png^techage_frame_ta2.png',
'techage_filling_ta2.png^techage_frame_ta2.png',
},
techage = {
turn_on = turn_on_clbk,
read_power_consumption = consumer.read_power_consumption,
power_network = Axle,
power_side = "L",
valid_power_dir = valid_power_dir,
animated_power_network = true,
},
after_place_node = function(pos, placer)
local mem = consumer.after_place_node(pos, placer)
mem.power_consumption = POWER_CONSUMPTION
end,
after_tube_update = consumer.after_tube_update,
after_dig_node = consumer.after_dig_node,
paramtype2 = "facedir",
groups = {choppy=2, cracky=2, crumbly=2},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
minetest.register_node("techage:consumer_on", {
description = "TechAge Consumer",
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_frame4_ta2.png^techage_appl_compressor4.png",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 0.6,
},
},
'techage_filling_ta2.png^techage_frame_ta2.png^techage_axle_clutch.png',
{
image = "techage_filling4_ta2.png^techage_frame4_ta2.png^techage_appl_compressor4.png",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 0.6,
},
},
{
image = "techage_filling4_ta2.png^techage_frame4_ta2.png^techage_appl_compressor4.png",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 0.6,
},
},
},
techage = {
turn_on = turn_on_clbk,
read_power_consumption = consumer.read_power_consumption,
power_network = Axle,
power_side = "L",
valid_power_dir = valid_power_dir,
animated_power_network = true,
},
after_tube_update = consumer.after_tube_update,
after_dig_node = consumer.after_dig_node,
paramtype2 = "facedir",
groups = {not_in_creative_inventory=1},
diggable = false,
drop = "techage:consumer",
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})

View File

@ -1,123 +0,0 @@
-- 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("techage")
local I,_ = dofile(MP.."/intllib.lua")
local STANDBY_TICKS = 4
local COUNTDOWN_TICKS = 4
local CYCLE_TIME = 16
local POWER_CAPACITY = 30
local Power = techage.ElectricCable
--local Power = techage.SteamPipe
local generator = techage.generator
local function formspec(self, pos, mem)
return "size[8,7]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"image[6,0.5;1,2;"..generator.formspec_level(mem, mem.power_result)..
"image_button[5,1;1,1;".. self:get_state_button_image(mem) ..";state_button;]"..
"button[2.5,1;1.8,1;update;"..I("Update").."]"..
"list[current_player;main;0,3;8,4;]"..
default.get_hotbar_bg(0, 3)
end
local function start_node(pos, mem, state)
generator.turn_power_on(pos, POWER_CAPACITY)
end
local function stop_node(pos, mem, state)
generator.turn_power_on(pos, 0)
end
local State = techage.NodeStates:new({
node_name_passive = "techage:test_generator",
cycle_time = CYCLE_TIME,
standby_ticks = STANDBY_TICKS,
formspec_func = formspec,
start_node = start_node,
stop_node = stop_node,
})
local function node_timer(pos, elapsed)
local mem = tubelib2.get_mem(pos)
return State:is_active(mem)
end
local function turn_power_on(pos, in_dir, sum)
local mem = tubelib2.get_mem(pos)
-- store result for formspec
mem.power_result = sum
if State:is_active(mem) and sum <= 0 then
State:fault(pos, mem)
-- No automatic turn on
mem.power_capacity = 0
end
M(pos):set_string("formspec", formspec(State, pos, mem))
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
minetest.register_node("techage:test_generator", {
description = "TechAge Test Generator",
tiles = {
-- up, down, right, left, back, front
'techage_electric_button.png^techage_electric_power.png',
'techage_electric_button.png^techage_electric_power.png',
'techage_electric_button.png^techage_electric_power.png^techage_electric_plug.png',
'techage_electric_button.png^techage_electric_power.png',
'techage_electric_button.png^techage_electric_power.png',
'techage_electric_button.png^techage_electric_power.png',
},
paramtype2 = "facedir",
groups = {cracky=2, crumbly=2, choppy=2},
on_rotate = screwdriver.disallow,
is_ground_content = false,
techage = {
turn_on = turn_power_on,
read_power_consumption = generator.read_power_consumption,
power_network = Power,
power_side = "R",
},
after_place_node = function(pos, placer)
local mem = generator.after_place_node(pos)
State:node_init(pos, mem, "")
on_rightclick(pos)
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
State:after_dig_node(pos, oldnode, oldmetadata, digger)
generator.after_dig_node(pos, oldnode)
end,
after_tube_update = generator.after_tube_update,
on_receive_fields = on_receive_fields,
on_rightclick = on_rightclick,
on_timer = node_timer,
})
Power:add_secondary_node_names({"techage:test_generator"})

View File

@ -1,91 +0,0 @@
--[[
TechAge
=======
Copyright (C) 2019 Joachim Stolberg
LGPLv2.1+
See LICENSE.txt for more information
distributor.lua:
]]--
-- 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
local N = minetest.get_node
local function formspec()
return "size[10.5,8.5]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"list[context;src;0,0;2,4;]"..
"list[current_player;main;1.25,4.5;8,4;]"..
"listring[context;src]"..
"listring[current_player;main]"
end
-- move items to the output slots
local function keep_running(pos, elapsed)
local meta = M(pos)
local inv = meta:get_inventory()
local name, num
for i = 1,10 do
--local list = inv:get_list("src")
for i = 1,8 do
--local stack = list[i]
local stack = inv:get_stack("src", i)
if stack:get_count() > 0 then
local taken = inv:remove_item("src", stack)
num = taken:get_count()
inv:add_item("src", taken)
break
end
end
end
return true
end
local function after_place_node(pos, placer)
local meta = M(pos)
local inv = meta:get_inventory()
inv:set_size('src', 8)
inv:add_item("src", ItemStack("wool:blue"))
inv:add_item("src", ItemStack("wool:red"))
inv:add_item("src", ItemStack("wool:green"))
meta:set_string("formspec", formspec())
minetest.get_node_timer(pos):start(0.1)
end
minetest.register_node("techage:perf_test", {
description = "perf_test",
tiles = {"techage_filling_ta2.png"},
after_place_node = after_place_node,
on_timer = keep_running,
paramtype = "light",
sunlight_propagates = true,
paramtype2 = "facedir",
groups = {choppy=2, cracky=2, crumbly=2},
is_ground_content = false,
})
minetest.register_lbm({
label = "[TechAge] Node update",
name = "techage:perf_test",
nodenames = {"techage:perf_test"},
run_at_every_load = true,
action = function(pos, node)
after_place_node(pos)
end
})

87
test/sink.lua Normal file
View File

@ -0,0 +1,87 @@
-- 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
local PWR_NEEDED = 2
local CYCLE_TIME = 2
local Cable = techage.ElectricCable
local consume_power = techage.power2.consume_power
local function swap_node(pos, name)
local node = minetest.get_node(pos)
if node.name == name then
return
end
node.name = name
minetest.swap_node(pos, node)
end
local function node_timer(pos, elapsed)
--print("node_timer sink "..S(pos))
local mem = tubelib2.get_mem(pos)
if mem.running then
local got = consume_power(pos, PWR_NEEDED)
if got < PWR_NEEDED then
swap_node(pos, "techage:sink")
else
swap_node(pos, "techage:sink_on")
end
return true
end
swap_node(pos, "techage:sink")
return false
end
local function on_rightclick(pos, node, clicker)
local mem = tubelib2.get_mem(pos)
if not mem.running then
mem.running = true
node_timer(pos, 2)
minetest.get_node_timer(pos):start(CYCLE_TIME)
else
swap_node(pos, "techage:sink")
minetest.get_node_timer(pos):stop()
mem.running = false
end
end
minetest.register_node("techage:sink", {
description = "Sink",
tiles = {'techage_electric_button.png'},
on_timer = node_timer,
on_rightclick = on_rightclick,
paramtype = "light",
light_source = 0,
sunlight_propagates = true,
paramtype2 = "facedir",
groups = {choppy=2, cracky=2, crumbly=2},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
minetest.register_node("techage:sink_on", {
description = "Sink",
tiles = {'techage_electric_button.png'},
on_timer = node_timer,
on_rightclick = on_rightclick,
paramtype = "light",
light_source = minetest.LIGHT_MAX,
sunlight_propagates = true,
paramtype2 = "facedir",
diggable = false,
drop = "",
groups = {not_in_creative_inventory=1},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
techage.power2.register_node({"techage:sink", "techage:sink_on"}, {
power_network = Cable,
})

56
test/source.lua Normal file
View File

@ -0,0 +1,56 @@
-- 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
local CYCLE_TIME = 2
local PWR_CAPA = 15
local Cable = techage.ElectricCable
local provide_power = techage.power2.provide_power
local function node_timer(pos, elapsed)
--print("node_timer source "..S(pos))
local mem = tubelib2.get_mem(pos)
if mem.generating then
local delivered = provide_power(pos, PWR_CAPA)
return true
end
return false
end
local function on_rightclick(pos, node, clicker)
local mem = tubelib2.get_mem(pos)
if not mem.generating then
mem.generating = true
node_timer(pos, 2)
minetest.get_node_timer(pos):start(CYCLE_TIME)
else
minetest.get_node_timer(pos):stop()
mem.generating = false
end
techage.power2.power_switched(pos)
end
minetest.register_node("techage:source", {
description = "Source",
tiles = {
-- up, down, right, left, back, front
'techage_electric_button.png^techage_appl_electronic_fab.png',
'techage_electric_button.png^techage_appl_electronic_fab.png',
'techage_electric_button.png^techage_appl_electronic_fab.png^techage_electric_plug.png',
'techage_electric_button.png^techage_appl_electronic_fab.png',
'techage_electric_button.png^techage_appl_electronic_fab.png',
'techage_electric_button.png^techage_appl_electronic_fab.png',
},
paramtype2 = "facedir",
groups = {cracky=2, crumbly=2, choppy=2},
is_ground_content = false,
on_rightclick = on_rightclick,
on_timer = node_timer,
})
techage.power2.register_node({"techage:source"}, {
power_network = Cable,
})

48
test/switch.lua Normal file
View File

@ -0,0 +1,48 @@
-- 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
local Cable = techage.ElectricCable
local on_network_change = techage.power2.on_network_change
local power_switched = techage.power2.power_switched
local function on_rightclick(pos, node, clicker)
local mem = tubelib2.get_mem(pos)
mem.interrupted_dirs = mem.interrupted_dirs or {}
mem.interrupted = not mem.interrupted
print("switch", mem.interrupted)
if mem.interrupted then
mem.interrupted_dirs = {true, true, true, true, true, true}
for dir,_ in pairs(mem.connections) do
mem.interrupted_dirs[dir] = false
--on_network_change(pos)
power_switched(pos)
mem.interrupted_dirs[dir] = true
end
else
mem.interrupted_dirs = {}
power_switched(pos)
end
end
minetest.register_node("techage:switch", {
description = "Switch",
tiles = {'techage_appl_switch_inv.png'},
on_timer = node_timer,
on_rightclick = on_rightclick,
paramtype = "light",
light_source = 0,
sunlight_propagates = true,
paramtype2 = "facedir",
groups = {choppy=2, cracky=2, crumbly=2},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
techage.power2.register_node({"techage:switch"}, {
power_network = Cable,
})

View File

@ -1,10 +0,0 @@
local function bubble(pos1, pos2, posc, amount)
local radius = math.floor(math.pow(amount, 0.333))
local xc = pos2.x - pos1.x
for x = posc.x - radius, posc.x + radius do
for y = posc.y - radius, posc.y + radius do
for z = posc.z - radius, posc.z + radius do
local idx = x - pos1.x +
(y - pos1.y) * 16 +
(z - pos1.z) * 16 * 16
print()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 687 B

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 713 B

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 548 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 262 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 283 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB