From e4fc67a8ebc1e1a84558b5debd54853577df03d3 Mon Sep 17 00:00:00 2001 From: alek13 Date: Sun, 24 Nov 2024 01:15:31 +0300 Subject: [PATCH] Artisan Benches: Fuel Device: add inv sizes, `:init()`, `:is_empty()` into `Device` & reuse it. Relates to #1787, #1789 --- .../fuel_device/src/fuel_device/Device.lua | 33 +++++++++-- .../Core/fuel_device/src/fuel_device/node.lua | 11 ++-- .../fuel_device/node/definition/common.lua | 57 ++++++++----------- 3 files changed, 60 insertions(+), 41 deletions(-) diff --git a/mods/lord/Core/fuel_device/src/fuel_device/Device.lua b/mods/lord/Core/fuel_device/src/fuel_device/Device.lua index efd1d73ed..fd6d70cc1 100644 --- a/mods/lord/Core/fuel_device/src/fuel_device/Device.lua +++ b/mods/lord/Core/fuel_device/src/fuel_device/Device.lua @@ -22,6 +22,11 @@ local Device = { inactive = nil, active = nil, }, + --- @static + --- @type {src:number,dst:number} + size_of = { fuel = 1, src = 1, dst = 1, }, + + -- dynamic: --- @type Position position = nil, --- @type NodeMetaRef @@ -39,7 +44,7 @@ end --- Constructor --- @public --- @param pos table ---- @return Device +--- @return fuel_device.Device function Device:new(pos) local class = self self = {} @@ -72,7 +77,7 @@ local function get_initiated_meta(pos) end --- @param meta NodeMetaRef -local function reset_meta_vars(meta) +local function reset_meta_times(meta) for _, name in pairs({ 'fuel_totaltime', 'fuel_time', @@ -96,9 +101,27 @@ function Device:get_meta() return self.meta end +--- @public +function Device:init() + self:get_meta():set_string('formspec', self.form.get_spec('inactive')) + self:get_meta():set_string('infotext', self.NAME) + local inventory = self:get_meta():get_inventory() + inventory:set_size('fuel', self.size_of.fuel or 1) + inventory:set_size('src', self.size_of.src or 1) + inventory:set_size('dst', self.size_of.dst or 1) +end + +--- @public +--- @return boolean +function Device:is_empty() + local inventory = self:get_meta():get_inventory() + + return inventory:is_empty('fuel') and inventory:is_empty('src') and inventory:is_empty('dst') +end + --- Sets Node into active device with new hint. --- @public ---- @param hint string A template for hinting in English. Use '%s' for machine name placeholder. +--- @param hint string text shown in `infotext` after `": "` function Device:activate(hint) local meta = self:get_meta() local percent = math_floor(meta:get_float('fuel_time') / meta:get_float('fuel_totaltime') * 100) @@ -111,10 +134,10 @@ end --- Sets Node into inactive device with new hint. --- @public ---- @param hint string A template for hinting in English. Use '%s' for machine name placeholder. +--- @param hint string text shown in `infotext` after `": "` function Device:deactivate(hint) minetest.get_node_timer(self.position):stop() - reset_meta_vars(self:get_meta()) + reset_meta_times(self:get_meta()) minetest.swap_node_if_not_same(self.position, self.node_name.inactive) self:get_meta():set_string('infotext', self.NAME .. ': ' .. hint) self:get_meta():set_string('formspec', self.form.get_spec('inactive')) diff --git a/mods/lord/Core/fuel_device/src/fuel_device/node.lua b/mods/lord/Core/fuel_device/src/fuel_device/node.lua index 01f055889..ea47b444a 100644 --- a/mods/lord/Core/fuel_device/src/fuel_device/node.lua +++ b/mods/lord/Core/fuel_device/src/fuel_device/node.lua @@ -19,11 +19,14 @@ local definition = { --- --- @generic GenericDevice: fuel_device.Device --- @return GenericDevice -local function create_generic_device(device_name, form, node_name) +local function create_generic_device(device_name, form, node_name, size_of) + size_of = size_of or table.overwrite({ src = 1, dst = 1 }, size_of) + return Device:extended({ NAME = device_name, form = form, - node_name = node_name + node_name = node_name, + size_of = size_of, }) end @@ -59,10 +62,10 @@ local function register_nodes(device_name, craft_method, nodes_definitions, form inactive = nodes_definitions.inactive.node_name:replace('^:', ''), active = nodes_definitions.active.node_name:replace('^:', ''), } - DeviceClass = DeviceClass or create_generic_device(device_name, form, node_name) + DeviceClass = DeviceClass or create_generic_device(device_name, form, node_name, size_of) ProcessorClass = ProcessorClass or create_generic_processor(DeviceClass, craft_method) - local common = definition.common.get(device_name, node_name.inactive, form, size_of) + local common = definition.common.get(DeviceClass) local inventory_callbacks = definition.inventory_callbacks.get(device_name, ProcessorClass) --- @type NodeDefinition diff --git a/mods/lord/Core/fuel_device/src/fuel_device/node/definition/common.lua b/mods/lord/Core/fuel_device/src/fuel_device/node/definition/common.lua index 1d1d42d7c..adad4c36e 100644 --- a/mods/lord/Core/fuel_device/src/fuel_device/node/definition/common.lua +++ b/mods/lord/Core/fuel_device/src/fuel_device/node/definition/common.lua @@ -1,48 +1,41 @@ ---- @generic GenericForm: fuel_device.node.Form ---- @param device_name string ---- @param form GenericForm ---- @param size_of {fuel:number,src:number,dst:number} -local function get_on_construct_function(device_name, form, size_of) +--- @generic GenericDevice: fuel_device.Device +--- @param Device fuel_device.Device +--- @return fun(pos:Position) +local function get_on_construct_function(Device) return function(pos) - local meta = minetest.get_meta(pos) - meta:set_string('formspec', form.get_spec('inactive')) - meta:set_string('infotext', device_name) - local inv = meta:get_inventory() - inv:set_size('fuel', size_of.fuel or 1) - inv:set_size('src', size_of.src or 1) - inv:set_size('dst', size_of.dst or 1) + Device:new(pos):init() + end +end + +--- @generic GenericDevice: fuel_device.Device +--- @param Device fuel_device.Device +--- @return fun(pos:Position,player:Player) +local function get_can_dig_function(Device) + --- @param pos Position + --- @param player Player + return function(pos, player) + if Device:new(pos):is_empty() then + return true + end + return false end end local common_definition = { paramtype2 = 'facedir', is_ground_content = false, - can_dig = function(pos,player) - local meta = minetest.get_meta(pos); - local inv = meta:get_inventory() - if not inv:is_empty('fuel') then - return false - elseif not inv:is_empty('dst') then - return false - elseif not inv:is_empty('src') then - return false - end - return true - end, } return { - --- @generic GenericForm: fuel_device.node.Form - --- @param device_name string - --- @param inactive_node_name string - --- @param form GenericForm - --- @param size_of {fuel:number,src:number,dst:number}|nil - get = function(device_name, inactive_node_name, form, size_of) + --- @generic GenericDevice: fuel_device.Device + --- @param Device GenericDevice + get = function(Device) return table.merge(common_definition, { - drop = inactive_node_name, - on_construct = get_on_construct_function(device_name, form, size_of or {}) + drop = Device.node_name.inactive, + on_construct = get_on_construct_function(Device), + can_dig = get_can_dig_function(Device), }) end }