From cb5d6eb61950b3a82b6d2386a34b76d984965616 Mon Sep 17 00:00:00 2001 From: Thomas--S Date: Tue, 21 Jul 2020 21:41:31 +0200 Subject: [PATCH] Add a simple Digtron battery. Further balancing or adjustments might be necessary. --- basic_machines/consumer.lua | 5 + digtron/battery.lua | 182 ++++++++++++++++++++++++++++++++++++ init.lua | 5 + locale/techage.de.tr | 2 + locale/template.txt | 2 + mod.conf | 2 +- 6 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 digtron/battery.lua diff --git a/basic_machines/consumer.lua b/basic_machines/consumer.lua index 53abb76..e7f1c95 100644 --- a/basic_machines/consumer.lua +++ b/basic_machines/consumer.lua @@ -176,6 +176,10 @@ function techage.register_consumer(base_name, inv_name, tiles, tNode, validState local node = minetest.get_node(pos) meta:set_int("push_dir", techage.side_to_indir("L", node.param2)) meta:set_int("pull_dir", techage.side_to_indir("R", node.param2)) + -- Delete existing node number. Needed for Digtron compatibility. + if (meta:contains("node_number")) then + meta:set_string("node_number", "") + end local number = "-" if stage > 2 then number = techage.add_node(pos, name_pas) @@ -240,6 +244,7 @@ function techage.register_consumer(base_name, inv_name, tiles, tNode, validState paramtype = tNode.paramtype, paramtype2 = "facedir", + drop = tNode.drop, groups = table.copy(tNode.groups), is_ground_content = false, sounds = tNode.sounds, diff --git a/digtron/battery.lua b/digtron/battery.lua new file mode 100644 index 0000000..ba09deb --- /dev/null +++ b/digtron/battery.lua @@ -0,0 +1,182 @@ +--[[ + + TechAge + ======= + + Copyright (C) 2019-2020 Joachim Stolberg + Copyright (C) 2020 Thomas S. + + GPL v3 + See LICENSE.txt for more information + + Electricity powered battery for Digtron + +]]-- + +-- for lazy programmers +local M = minetest.get_meta +local S = techage.S + +-- Consumer Related Data +local CRD = function(pos) return (minetest.registered_nodes[techage.get_node_lvm(pos).name] or {}).consumer end +local power = techage.power + +local STANDBY_TICKS = 3 +local COUNTDOWN_TICKS = 4 +local CYCLE_TIME = 4 +local INV_SIZE = 4 +local FUEL = "default:coal_lump" +local FUEL_STACK_MAX = ItemStack(FUEL):get_stack_max() +local TOTAL_MAX = INV_SIZE * FUEL_STACK_MAX + +local function count_coal(metadata) + local total = 0 + for _,stack in pairs(metadata.inventory.fuel) do + total = total + stack:get_count() + end + return total +end + +local function formspec(self, pos, nvm) + local meta = M(pos):to_table() + local total = 0 + if meta.inventory then + total = count_coal(meta) + end + return "size[5,4]".. + default.gui_bg.. + default.gui_bg_img.. + default.gui_slots.. + "box[0,-0.1;4.8,0.5;#c6e8ff]".. + "label[1,-0.1;"..minetest.colorize("#000000", S("Digtron Battery")).."]".. + power.formspec_label_bar(0, 0.8, S("Load"), TOTAL_MAX, total, S("Coal Equivalents")).. + "image_button[2.6,2;1,1;".. self:get_state_button_image(nvm) ..";state_button;]".. + "tooltip[2.6,2;1,1;"..self:get_state_tooltip(nvm).."]".. + "image[3.75,2;1,1;"..techage.get_power_image(pos, nvm).."]" +end + +local function allow_metadata_inventory_put(pos, listname, index, stack, player) + return 0 +end + +local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player) + return 0 +end + +local function allow_metadata_inventory_take(pos, listname, index, stack, player) + return 0 +end + +local function produce_coal(pos, crd, nvm, inv) + local stack = ItemStack(FUEL) + if inv:room_for_item("fuel", stack) then + inv:add_item("fuel", stack) + crd.State:keep_running(pos, nvm, COUNTDOWN_TICKS) + else + crd.State:idle(pos, nvm) + end +end + +local function keep_running(pos, elapsed) + local nvm = techage.get_nvm(pos) + local crd = CRD(pos) + local inv = M(pos):get_inventory() + produce_coal(pos, crd, nvm, inv) + + if techage.is_activeformspec(pos) then + M(pos):set_string("formspec", formspec(CRD(pos).State, pos, nvm)) + end +end + +local function on_receive_fields(pos, formname, fields, player) + if minetest.is_protected(pos, player:get_player_name()) then + return + end + local nvm = techage.get_nvm(pos) + CRD(pos).State:state_button_event(pos, nvm, fields) +end + + +local tiles = {} +-- '#' will be replaced by the stage number +-- '{power}' will be replaced by the power PNG + +tiles = { + -- up, down, right, left, back, front + "digtron_plate.png^digtron_core.png", + "digtron_plate.png^digtron_core.png", + "digtron_plate.png^digtron_battery.png", + "digtron_plate.png^digtron_battery.png", + "digtron_plate.png^digtron_battery.png", + "digtron_plate.png^digtron_battery.png", +} + +local tubing = { + on_recv_message = function(pos, src, topic, payload) + return CRD(pos).State:on_receive_message(pos, topic, payload) + end, +} + +local node_name_ta2, node_name_ta3, node_name_ta4 = +techage.register_consumer("digtron_battery", S("Digtron Battery"), { act = tiles, pas = tiles }, { + drawtype = "normal", + paramtype = "light", + cycle_time = CYCLE_TIME, + standby_ticks = STANDBY_TICKS, + formspec = formspec, + tubing = tubing, + after_place_node = function(pos, placer, itemstack) + local inv = M(pos):get_inventory() + inv:set_size('fuel', INV_SIZE) + if itemstack then + local stack_meta = itemstack:get_meta() + if stack_meta then + local coal_amount = techage.in_range(stack_meta:get_int("coal"), 0, TOTAL_MAX) + while coal_amount > 0 do + local amount = math.min(coal_amount, FUEL_STACK_MAX) + inv:add_item("fuel", ItemStack(FUEL.." "..amount)) + coal_amount = coal_amount - amount; + end + end + end + end, + after_dig_node = function(pos, oldnode, oldmetadata, digger) + local node = ItemStack(oldnode.name) + if oldmetadata.inventory then + local total = count_coal(oldmetadata) + local meta = node:get_meta() + meta:set_int("coal", total) + local text = S("Digtron Battery").." ("..math.floor(total/TOTAL_MAX * 100).." %)" + meta:set_string("description", text) + end + local inv = minetest.get_inventory({type="player", name=digger:get_player_name()}) + local left_over = inv:add_item("main", node) + if left_over:get_count() > 0 then + minetest.add_item(pos, node) + end + end, + on_rightclick = function(pos, node, clicker) + techage.set_activeformspec(pos, clicker) + local nvm = techage.get_nvm(pos) + M(pos):set_string("formspec", formspec(CRD(pos).State, pos, nvm)) + end, + drop = "", + node_timer = keep_running, + on_receive_fields = on_receive_fields, + allow_metadata_inventory_put = allow_metadata_inventory_put, + allow_metadata_inventory_move = allow_metadata_inventory_move, + allow_metadata_inventory_take = allow_metadata_inventory_take, + groups = {choppy=2, cracky=2, crumbly=2, digtron=5}, + sounds = default.node_sound_wood_defaults(), + power_consumption = {0,25,25,25}, + power_sides = techage.networks.AllSides, +}, {false, false, true, false}) + +minetest.register_craft({ + output = node_name_ta3, + recipe = { + {"group:wood", "default:copper_ingot", "group:wood"}, + {"techage:electric_cableS", "default:tin_ingot", "digtron:digtron_core"}, + {"group:wood", "default:copper_ingot", "group:wood"}, + }, +}) diff --git a/init.lua b/init.lua index d87d573..da3cc2f 100644 --- a/init.lua +++ b/init.lua @@ -206,6 +206,11 @@ dofile(MP.."/oil/reboiler.lua") dofile(MP.."/ta3_power/tiny_generator.lua") dofile(MP.."/ta3_power/akkubox.lua") +-- Digtron +if minetest.global_exists("digtron") then + dofile(MP.."/digtron/battery.lua") +end + -- Logic dofile(MP.."/logic/lib.lua") dofile(MP.."/logic/terminal.lua") diff --git a/locale/techage.de.tr b/locale/techage.de.tr index 2258d67..b791d03 100644 --- a/locale/techage.de.tr +++ b/locale/techage.de.tr @@ -35,6 +35,7 @@ Build derrick=Errichte Ölturm Catalyst=Katalysator Cement Block=Zement Block Cement Powder=Zement Pulver +Coal Equivalents=Kohleeinheiten Compressed Gravel=Komprimiertes Kies Configure up to 8 items @nto be pushed by the injector=Konfiguriere bis zu 8 Gegenstände \n die vom Injektor weitergegeben werden sollen Consum. 1=Konsum. 1 @@ -42,6 +43,7 @@ Consum. 2=Konsum. 2 Current power:=Strom aktuell: Depth=Tiefe Digging depth=Grabungstiefe +Digtron Battery=Digtron Akku Dirt with Ash=Erde mit Asche Display no: = Display Nr. Distributor=Verteiler diff --git a/locale/template.txt b/locale/template.txt index 44d7739..efb6c16 100644 --- a/locale/template.txt +++ b/locale/template.txt @@ -33,6 +33,7 @@ Build derrick= Catalyst= Cement Block= Cement Powder= +Coal Equivalents= Compressed Gravel= Configure up to 8 items @nto be pushed by the injector= Consum. 1= @@ -40,6 +41,7 @@ Consum. 2= Current power:= Depth= Digging depth= +Digtron Battery= Dirt with Ash= Display no: = Distributor= diff --git a/mod.conf b/mod.conf index 66b1420..517295f 100644 --- a/mod.conf +++ b/mod.conf @@ -1,4 +1,4 @@ name = techage depends = default,doors,flowers,tubelib2,basic_materials,bucket,stairs,screwdriver,minecart,lcdlib,safer_lua -optional_depends = unified_inventory,wielded_light,unifieddyes,moreores,ethereal,mesecon +optional_depends = unified_inventory,wielded_light,unifieddyes,moreores,ethereal,mesecon,digtron description = Techage, go through 4 tech ages in search of wealth and power!