Skip to content

Commit

Permalink
Artisan Benches: Fuel Device: add inv sizes, :init(), :is_empty()
Browse files Browse the repository at this point in the history
… into `Device` & reuse it. Relates to #1787, #1789
  • Loading branch information
alek13 committed Nov 23, 2024
1 parent 16e4d15 commit e4fc67a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 41 deletions.
33 changes: 28 additions & 5 deletions mods/lord/Core/fuel_device/src/fuel_device/Device.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,7 +44,7 @@ end
--- Constructor
--- @public
--- @param pos table<number,number,number>
--- @return Device
--- @return fuel_device.Device
function Device:new(pos)
local class = self
self = {}
Expand Down Expand Up @@ -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',
Expand All @@ -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 `"<Device.NAME>: "`
function Device:activate(hint)
local meta = self:get_meta()
local percent = math_floor(meta:get_float('fuel_time') / meta:get_float('fuel_totaltime') * 100)
Expand All @@ -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 `"<Device.NAME>: "`
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'))
Expand Down
11 changes: 7 additions & 4 deletions mods/lord/Core/fuel_device/src/fuel_device/node.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit e4fc67a

Please sign in to comment.