From 18f791956bcdd5209c625987b246d9d665382187 Mon Sep 17 00:00:00 2001 From: Joachim Stolberg Date: Sun, 12 May 2019 20:27:54 +0200 Subject: [PATCH] battery added --- basic_machines/gravelrinser.lua | 1 - basic_machines/source.lua | 1 - basis/power.lua | 19 +- coal_power_station/battery.lua | 199 +++++++++++++++++++++ init.lua | 1 + test/battery.lua | 123 ------------- test/lamp.lua | 9 +- textures/techage_form_level_charge.png | Bin 0 -> 234 bytes textures/techage_form_level_charge.png.png | Bin 0 -> 234 bytes textures/techage_form_level_off.png | Bin 0 -> 221 bytes textures/techage_form_level_unload.png | Bin 0 -> 234 bytes 11 files changed, 221 insertions(+), 132 deletions(-) create mode 100644 coal_power_station/battery.lua delete mode 100644 test/battery.lua create mode 100644 textures/techage_form_level_charge.png create mode 100644 textures/techage_form_level_charge.png.png create mode 100644 textures/techage_form_level_off.png create mode 100644 textures/techage_form_level_unload.png diff --git a/basic_machines/gravelrinser.lua b/basic_machines/gravelrinser.lua index 193e4e9..b0b1549 100644 --- a/basic_machines/gravelrinser.lua +++ b/basic_machines/gravelrinser.lua @@ -89,7 +89,6 @@ end local function determine_water_dir(pos) local lvl = get_water_level(pos) - print(lvl) if lvl > get_water_level({x=pos.x+1, y=pos.y, z=pos.z}) then return 2 end diff --git a/basic_machines/source.lua b/basic_machines/source.lua index 0859213..5a9d74a 100644 --- a/basic_machines/source.lua +++ b/basic_machines/source.lua @@ -108,7 +108,6 @@ local function on_receive_fields(pos, formname, fields, player) state:state_button_event(pos, mem, fields) if fields.update then - local state = tStates[mem.state_num or 2] M(pos):set_string("formspec", formspec(state, pos, mem)) end end diff --git a/basis/power.lua b/basis/power.lua index 8e1fe4b..7cf4356 100644 --- a/basis/power.lua +++ b/basis/power.lua @@ -174,7 +174,7 @@ techage.generator = {} function techage.generator.after_place_node(pos) local mem = tubelib2.init_mem(pos) - mem.power_produce = 0 + mem.power_capacity = 0 TRD(pos).power_network:after_place_node(pos) return mem end @@ -218,10 +218,25 @@ function techage.generator.after_dig_node(pos, oldnode) end function techage.generator.formspec_level(mem, sum) - local percent = ((sum or 0) * 100) / (mem.power_capacity or 1) + local percent = math.min(((sum or 0) * 100) / (mem.power_capacity or 1), 100) return "techage_form_level_bg.png^[lowpart:"..percent..":techage_form_level_fg.png]" end +function techage.generator.formspec_battery_capa(max_capa, current_capa) + local percent = math.min(((current_capa or 0) * 100) / (max_capa or 1), 100) + return "techage_form_level_bg.png^[lowpart:"..percent..":techage_form_level_fg.png]" +end +function techage.generator.formspec_battery_load(mem) + if mem.power_capacity ~= 0 then + if mem.charging then + return "techage_form_level_charge.png]" + elseif mem.unloading then + return "techage_form_level_unload.png]" + end + end + return "techage_form_level_off.png]" +end + -- -- Distributor functions for nodes with 6 power sides (view from the outside) diff --git a/coal_power_station/battery.lua b/coal_power_station/battery.lua new file mode 100644 index 0000000..aae6af9 --- /dev/null +++ b/coal_power_station/battery.lua @@ -0,0 +1,199 @@ +--[[ + + TechAge + ======= + + Copyright (C) 2019 Joachim Stolberg + + LGPLv2.1+ + See LICENSE.txt for more information + + TA3 Battery Box + +]]-- + +-- 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 = 2 +local POWER_CONSUMPTION = 10 +local POWER_MAX_LOAD = 1000 + +local Power = techage.ElectricCable +local generator = techage.generator + +-- called from pipe network +local function valid_power_dir(pos, power_dir, in_dir) + return power_dir == in_dir +end + +local function formspec(self, pos, mem) + return "size[5,3]".. + default.gui_bg.. + default.gui_bg_img.. + default.gui_slots.. + "image[0,0.5;1,2;"..generator.formspec_battery_capa(POWER_MAX_LOAD, mem.capa).. + "label[0.2,2.5;Load]".. + "button[1.1,1;1.8,1;update;"..I("Update").."]".. + "image_button[3,1;1,1;".. self:get_state_button_image(mem) ..";state_button;]".. + "image[4,0.5;1,2;"..generator.formspec_battery_load(mem).. + "label[4.2,2.5;Flow]" +end + +local function turn_off(pos, mem) + generator.turn_power_on(pos, 0) + mem.charging = false + mem.unloading = false +end + +local function switch_to_unloading(pos, mem) + if not mem.unloading then + mem.unloading = true + mem.charging = false + generator.turn_power_on(pos, 2 * POWER_CONSUMPTION) + end +end + +local function switch_to_charging(pos, mem) + if mem.sum > POWER_CONSUMPTION and not mem.charging then + mem.charging = true + mem.unloading = false + generator.turn_power_on(pos, -POWER_CONSUMPTION) + end + mem.delayed_call = false +end + +local function start_node(pos, mem, state) + turn_off(pos, mem) +end + +local function stop_node(pos, mem, state) + turn_off(pos, mem) +end + +local State = techage.NodeStates:new({ + node_name_passive = "techage:ta3_battery", + 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) + if State:is_active(mem) then + mem.capa = mem.capa or 0 + if mem.charging then + if mem.capa < POWER_MAX_LOAD then + mem.capa = mem.capa + 1 + else + turn_off(pos, mem) + end + elseif mem.unloading then + if mem.capa > 0 then + mem.capa = mem.capa - 1 + else + turn_off(pos, mem) + end + end + end + --print("node_timer", S(pos), mem.sum, mem.power_capacity) + return State:is_active(mem) +end + +local function turn_power_on(pos, in_dir, sum) + local mem = tubelib2.get_mem(pos) + if State:is_active(mem) then + mem.capa = mem.capa or 0 + mem.sum = sum + --print("turn_power_on", sum, dump(mem)) + if mem.unloading then + if sum < 0 then + turn_off(pos, mem) + elseif sum > 2 * POWER_CONSUMPTION then + turn_off(pos, mem) + end + elseif mem.charging then + if sum < 0 then + turn_off(pos, mem) + end + else -- turned off + if sum > POWER_CONSUMPTION and not mem.delayed_call then + minetest.after(math.random(1.2, 5.0), + switch_to_charging, pos, mem) + mem.delayed_call = true + end + end + end +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 + local mem = tubelib2.get_mem(pos) + 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:ta3_battery", { + description = "TA3 Battery", + tiles = { + -- up, down, right, left, back, front + "techage_filling_ta3.png^techage_frame_ta3_top.png", + "techage_filling_ta3.png^techage_frame_ta3.png", + "techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_hole_electric.png", + "techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_source.png", + "techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_source.png", + "techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_source.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, "") + mem.charging = false + mem.unloading = false + 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:ta3_battery"}) diff --git a/init.lua b/init.lua index ceb8052..e5bf497 100644 --- a/init.lua +++ b/init.lua @@ -102,6 +102,7 @@ else dofile(MP.."/coal_power_station/generator.lua") dofile(MP.."/coal_power_station/turbine.lua") dofile(MP.."/coal_power_station/cooler.lua") + dofile(MP.."/coal_power_station/battery.lua") --dofile(MP.."/test/generator.lua") diff --git a/test/battery.lua b/test/battery.lua deleted file mode 100644 index 8feac0e..0000000 --- a/test/battery.lua +++ /dev/null @@ -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 = 12 - -local Axle = techage.Axle -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:battery", - 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:battery", { - description = "TA2 Battery", - tiles = { - -- up, down, right, left, back, front - "techage_filling_ta2.png^techage_frame_ta2_top.png", - "techage_filling_ta2.png^techage_frame_ta2.png", - "techage_filling_ta2.png^techage_axle_clutch.png^techage_frame_ta2.png", - "techage_filling_ta2.png^techage_frame_ta2.png^techage_appl_electric_power.png", - "techage_filling_ta2.png^techage_frame_ta2.png^techage_appl_electric_power.png", - "techage_filling_ta2.png^techage_frame_ta2.png^techage_appl_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 = Axle, - power_side = "R", - animated_power_network = true, - }, - - 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, -}) - -Axle:add_secondary_node_names({"techage:battery"}) \ No newline at end of file diff --git a/test/lamp.lua b/test/lamp.lua index 580d54c..81a04c2 100644 --- a/test/lamp.lua +++ b/test/lamp.lua @@ -7,7 +7,7 @@ local M = minetest.get_meta local MP = minetest.get_modpath("techage") local I,_ = dofile(MP.."/intllib.lua") -local POWER_CONSUMPTION = 2 +local POWER_CONSUMPTION = 1 local Power = techage.ElectricCable local consumer = techage.consumer @@ -23,13 +23,13 @@ end -- called from pipe network local function valid_power_dir(pos, power_dir, in_dir) - print("valid_power_dir", power_dir, in_dir) + --print("valid_power_dir", power_dir, in_dir) return true end local function lamp_turn_on_clbk(pos, in_dir, sum) local mem = tubelib2.get_mem(pos) - print("lamp_turn_on_clbk", sum, dump(mem)) + --print("lamp_turn_on_clbk", sum) if sum > 0 and mem.running then swap_node(pos, "techage:test_lamp_on") else @@ -39,7 +39,7 @@ end local function lamp_on_rightclick(pos, node, clicker) local mem = tubelib2.get_mem(pos) - print("lamp_on_rightclick", dump(mem)) + --print("lamp_on_rightclick", dump(mem)) if not mem.running then swap_node(pos, "techage:test_lamp_on") mem.running = true @@ -74,7 +74,6 @@ minetest.register_node("techage:test_lamp", { 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, diff --git a/textures/techage_form_level_charge.png b/textures/techage_form_level_charge.png new file mode 100644 index 0000000000000000000000000000000000000000..d95a49a50d881a2923ce7c546edeb4c6ea07a2ec GIT binary patch literal 234 zcmeAS@N?(olHy`uVBq!ia0vp^3P9|@!VDxk7i7!@QY`6?zK#qG8~eHcB(ehejKx9j zP7LeL$-D$|WHUn|N}Tg^b5rw5fLsO!=c3falGGH1^30M91$R&1fE2w{cAz+Cfk$L9 z0|U1(2s1Lwnj--e^-3$TU3K9x44YdqHSUt|Y(qLdXD-gK; ToPt3lP$z?@tDnm{r-UW|N@hPu literal 0 HcmV?d00001 diff --git a/textures/techage_form_level_charge.png.png b/textures/techage_form_level_charge.png.png new file mode 100644 index 0000000000000000000000000000000000000000..6c3526a5677dd14adc602ee648e0223f8268c6d6 GIT binary patch literal 234 zcmeAS@N?(olHy`uVBq!ia0vp^3P9|@!VDxk7i7!@QY`6?zK#qG8~eHcB(ehejKx9j zP7LeL$-D$|_yc@GTzxVNqoSe&{tF~_EdKxh|MbpvHXwB+L4Lsu4$p3+0XednArU3c z`MJ5Nc_lzD1A}u>YGO%hib8p2Nrr;Er*A-tUMf3KoU_0qvY3H^TNs2H8D`Cq01E1P zx;TbdoK8+Sz%-+G;eiGgMpXj|QHfVfdL9hB83MW$Bot;EY8iyEdYpTu!N71CBeIx*fm;}a85w5HkpK#+c)B=- zXq-<@XkcCz>Tsc9?nV<6ldKfOf6a_W$t-M4^HdmF8DbQtFweD|v;}0kr>mdKI;Vst E0GTN}+5i9m literal 0 HcmV?d00001 diff --git a/textures/techage_form_level_unload.png b/textures/techage_form_level_unload.png new file mode 100644 index 0000000000000000000000000000000000000000..a7463cf0fae77ebe583568183f0efe7526de8d60 GIT binary patch literal 234 zcmeAS@N?(olHy`uVBq!ia0vp^3P9|@!VDxk7i7!@QY`6?zK#qG8~eHcB(ehejKx9j zP7LeL$-D$|_yc@GTzxVNqoShzb8sYfEdKxh|K*wbe?aO=g8YIR9G=}s19D_DLn2C? z^K)}k^GbkR1_tM%)Wnk16ovB4k_-iRPv3wPy;OFfIA?)JWHAE+w=f7ZGR&GI0Tk5p zba4!^IGvnufN4hW!UGK~jE)8pq7tu|^gI}LGX!)CNhr)T)G`QR^*HxRgMmTBD{%ew SV`&LMoeZ9?elF{r5}E+_R6sxg literal 0 HcmV?d00001