techage/.test/akku.lua

90 lines
2.5 KiB
Lua
Raw Normal View History

2019-06-15 17:14:46 +03:00
-- 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
2019-08-15 18:06:58 +03:00
local power = techage.power
2019-06-15 17:14:46 +03:00
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]"..
2019-08-08 15:43:45 +03:00
"image[4,0.5;1,2;"..techage.power.formspec_load_bar(-(mem.delivered or 0), PWR_PERF).."]"..
2019-06-15 17:14:46 +03:00
"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)
2019-08-15 18:06:58 +03:00
if mem.running then
mem.delivered = power.secondary_alive(pos, mem, mem.capa, PWR_CAPA)
--print("provided", mem.delivered)
2019-06-15 17:14:46 +03:00
mem.capa = mem.capa - mem.delivered
mem.capa = in_range(mem.capa, 0, PWR_CAPA)
end
2019-08-15 18:06:58 +03:00
return mem.running
2019-06-15 17:14:46 +03:00
end
2019-08-15 18:06:58 +03:00
2019-06-15 17:14:46 +03:00
local function on_receive_fields(pos, formname, fields, player)
if minetest.is_protected(pos, player:get_player_name()) then
return
end
2019-08-15 18:06:58 +03:00
local mem = tubelib2.get_mem(pos)
techage.power.network_changed(pos, mem)
2019-06-15 17:14:46 +03:00
if fields.update then
M(pos):set_string("formspec", formspec(pos, mem))
end
end
local function after_place_node(pos, placer)
-- secondary 'after_place_node', called by power. Don't use tubelib2.init_mem(pos)!!!
local mem = tubelib2.get_mem(pos)
2019-08-15 18:06:58 +03:00
mem.running = true
2019-06-15 17:14:46 +03:00
mem.capa = 0
minetest.get_node_timer(pos):start(CYCLE_TIME)
2019-08-15 18:06:58 +03:00
power.secondary_start(pos, mem, PWR_PERF, PWR_PERF)
M(pos):set_string("formspec", formspec(pos, mem))
2019-06-15 17:14:46 +03:00
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_timer = node_timer,
})
2019-08-08 15:43:45 +03:00
techage.power.register_node({"techage:akku"}, {
2019-06-15 17:14:46 +03:00
power_network = Cable,
})