2018-12-22 16:58:11 +01:00
|
|
|
--[[
|
|
|
|
|
|
|
|
Tube Library
|
|
|
|
============
|
|
|
|
|
2020-11-20 14:45:28 +01:00
|
|
|
Copyright (C) 2017-2020 Joachim Stolberg
|
2018-12-22 16:58:11 +01:00
|
|
|
|
2020-11-20 14:45:28 +01:00
|
|
|
AGPL v3
|
2018-12-22 16:58:11 +01:00
|
|
|
See LICENSE.txt for more information
|
|
|
|
|
|
|
|
node_states.lua:
|
|
|
|
|
|
|
|
A state model/class for tubelib nodes.
|
|
|
|
|
|
|
|
]]--
|
|
|
|
|
|
|
|
|
2019-05-20 23:40:20 +02:00
|
|
|
--[[
|
2018-12-22 16:58:11 +01:00
|
|
|
|
|
|
|
Node states:
|
|
|
|
|
|
|
|
+-----------------------------------+ +------------+
|
2019-05-20 23:40:20 +02:00
|
|
|
| | | |
|
|
|
|
| V V |
|
|
|
|
| +---------+ |
|
|
|
|
| | | |
|
|
|
|
| +---------| STOPPED | |
|
|
|
|
| | | | |
|
|
|
|
| button | +---------+ |
|
|
|
|
| | ^ |
|
|
|
|
repair | V | button |
|
|
|
|
| +---------+ | | button
|
|
|
|
| | |---------+ |
|
|
|
|
| | RUNNING | |
|
|
|
|
| +--------| |---------+ |
|
|
|
|
| | +---------+ | |
|
|
|
|
| | ^ | | |
|
|
|
|
| | | | | |
|
|
|
|
| V | V V |
|
|
|
|
| +---------+ +----------+ +---------+ |
|
|
|
|
| | | | | | | |
|
|
|
|
+---| DEFECT | | STANDBY/ | | FAULT |----------+
|
|
|
|
| | | BLOCKED | | |
|
|
|
|
+---------+ +----------+ +---------+
|
2018-12-22 16:58:11 +01:00
|
|
|
|
|
|
|
Node metadata:
|
|
|
|
"tubelib_number" - string with tubelib number, like "0123"
|
|
|
|
"tubelib_state" - node state, like "RUNNING"
|
|
|
|
"tubelib_item_meter" - node item/runtime counter
|
|
|
|
"tubelib_countdown" - countdown to stadby mode
|
|
|
|
"tubelib_aging" - aging counter
|
|
|
|
]]--
|
|
|
|
|
|
|
|
-- 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
|
|
|
|
|
|
|
|
|
2019-01-01 18:53:17 +01:00
|
|
|
local AGING_FACTOR = 4 -- defect random factor
|
2018-12-22 16:58:11 +01:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Local States
|
|
|
|
--
|
|
|
|
local STOPPED = tubelib.STOPPED
|
|
|
|
local RUNNING = tubelib.RUNNING
|
|
|
|
local STANDBY = tubelib.STANDBY
|
|
|
|
local FAULT = tubelib.FAULT
|
|
|
|
local BLOCKED = tubelib.BLOCKED
|
|
|
|
local DEFECT = tubelib.DEFECT
|
|
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
-- NodeStates Class Functions
|
|
|
|
--
|
|
|
|
tubelib.NodeStates = {}
|
|
|
|
local NodeStates = tubelib.NodeStates
|
|
|
|
|
2018-12-24 14:31:13 +01:00
|
|
|
local function start_condition_fullfilled(pos, meta)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2018-12-22 16:58:11 +01:00
|
|
|
function NodeStates:new(attr)
|
|
|
|
local o = {
|
|
|
|
-- mandatory
|
|
|
|
cycle_time = attr.cycle_time, -- for running state
|
2021-03-10 23:45:47 +01:00
|
|
|
first_cycle_time = attr.first_cycle_time, -- for first run, not required
|
2018-12-22 16:58:11 +01:00
|
|
|
standby_ticks = attr.standby_ticks, -- for standby state
|
|
|
|
has_item_meter = attr.has_item_meter, -- true/false
|
|
|
|
-- optional
|
|
|
|
node_name_passive = attr.node_name_passive,
|
2019-05-20 23:40:20 +02:00
|
|
|
node_name_active = attr.node_name_active,
|
2018-12-22 16:58:11 +01:00
|
|
|
node_name_defect = attr.node_name_defect,
|
|
|
|
infotext_name = attr.infotext_name,
|
2018-12-24 14:31:13 +01:00
|
|
|
start_condition_fullfilled = attr.start_condition_fullfilled or start_condition_fullfilled,
|
2018-12-28 17:16:14 +01:00
|
|
|
on_start = attr.on_start,
|
|
|
|
on_stop = attr.on_stop,
|
2019-01-05 18:04:56 +01:00
|
|
|
formspec_func = attr.formspec_func,
|
2018-12-22 16:58:11 +01:00
|
|
|
}
|
|
|
|
if attr.aging_factor then
|
2018-12-23 19:36:11 +01:00
|
|
|
o.aging_level1 = attr.aging_factor * tubelib.machine_aging_value
|
|
|
|
o.aging_level2 = attr.aging_factor * tubelib.machine_aging_value * AGING_FACTOR
|
2018-12-22 16:58:11 +01:00
|
|
|
end
|
|
|
|
setmetatable(o, self)
|
|
|
|
self.__index = self
|
|
|
|
return o
|
|
|
|
end
|
|
|
|
|
|
|
|
function NodeStates:node_init(pos, number)
|
|
|
|
local meta = M(pos)
|
|
|
|
meta:set_int("tubelib_state", STOPPED)
|
|
|
|
meta:set_string("tubelib_number", number)
|
|
|
|
if self.infotext_name then
|
|
|
|
meta:set_string("infotext", self.infotext_name.." "..number..": stopped")
|
|
|
|
end
|
|
|
|
if self.has_item_meter then
|
|
|
|
meta:set_int("tubelib_item_meter", 0)
|
|
|
|
end
|
|
|
|
if self.aging_level1 then
|
|
|
|
meta:set_int("tubelib_aging", 0)
|
|
|
|
end
|
|
|
|
if self.formspec_func then
|
2019-01-05 18:04:56 +01:00
|
|
|
meta:set_string("formspec", self.formspec_func(self, pos, meta))
|
2018-12-22 16:58:11 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function NodeStates:stop(pos, meta)
|
2018-12-28 17:16:14 +01:00
|
|
|
local state = meta:get_int("tubelib_state")
|
|
|
|
if state ~= DEFECT then
|
|
|
|
if self.on_stop then
|
|
|
|
self.on_stop(pos, meta, state)
|
|
|
|
end
|
2018-12-22 16:58:11 +01:00
|
|
|
meta:set_int("tubelib_state", STOPPED)
|
|
|
|
if self.node_name_passive then
|
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
node.name = self.node_name_passive
|
|
|
|
minetest.swap_node(pos, node)
|
|
|
|
end
|
|
|
|
if self.infotext_name then
|
|
|
|
local number = meta:get_string("tubelib_number")
|
|
|
|
meta:set_string("infotext", self.infotext_name.." "..number..": stopped")
|
|
|
|
end
|
|
|
|
if self.formspec_func then
|
2019-01-05 18:04:56 +01:00
|
|
|
meta:set_string("formspec", self.formspec_func(self, pos, meta))
|
2018-12-22 16:58:11 +01:00
|
|
|
end
|
|
|
|
minetest.get_node_timer(pos):stop()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function NodeStates:start(pos, meta, called_from_on_timer)
|
|
|
|
local state = meta:get_int("tubelib_state")
|
|
|
|
if state == STOPPED or state == STANDBY or state == BLOCKED then
|
2018-12-24 14:31:13 +01:00
|
|
|
if not self.start_condition_fullfilled(pos, meta) then
|
|
|
|
return false
|
|
|
|
end
|
2018-12-28 17:16:14 +01:00
|
|
|
if self.on_start then
|
|
|
|
self.on_start(pos, meta, state)
|
|
|
|
end
|
2018-12-22 16:58:11 +01:00
|
|
|
meta:set_int("tubelib_state", RUNNING)
|
2019-01-05 18:04:56 +01:00
|
|
|
meta:set_int("tubelib_countdown", 4)
|
2018-12-22 16:58:11 +01:00
|
|
|
if called_from_on_timer then
|
|
|
|
-- timer has to be stopped once to be able to be restarted
|
|
|
|
self.stop_timer = true
|
|
|
|
end
|
|
|
|
if self.node_name_active then
|
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
node.name = self.node_name_active
|
|
|
|
minetest.swap_node(pos, node)
|
|
|
|
end
|
|
|
|
if self.infotext_name then
|
|
|
|
local number = meta:get_string("tubelib_number")
|
|
|
|
meta:set_string("infotext", self.infotext_name.." "..number..": running")
|
|
|
|
end
|
|
|
|
if self.formspec_func then
|
2019-01-05 18:04:56 +01:00
|
|
|
meta:set_string("formspec", self.formspec_func(self, pos, meta))
|
2018-12-22 16:58:11 +01:00
|
|
|
end
|
2021-03-10 23:45:47 +01:00
|
|
|
local cycle_time = self.cycle_time
|
|
|
|
if self.first_cycle_time then
|
|
|
|
if meta:get_int("tubelib_first_run") == 1 then
|
|
|
|
meta:set_int("tubelib_first_run", 0)
|
|
|
|
cycle_time = self.cycle_time
|
|
|
|
else
|
|
|
|
meta:set_int("tubelib_first_run", 1)
|
|
|
|
cycle_time = self.first_cycle_time
|
|
|
|
end
|
|
|
|
end
|
|
|
|
minetest.get_node_timer(pos):start(cycle_time)
|
2018-12-22 16:58:11 +01:00
|
|
|
return true
|
|
|
|
end
|
2021-03-10 23:45:47 +01:00
|
|
|
if self.first_cycle_time and meta:get_int("tubelib_first_run") == 1 then
|
|
|
|
local cycle_time = self.cycle_time
|
|
|
|
local timer = minetest.get_node_timer(pos)
|
|
|
|
minetest.after(0, function ()
|
|
|
|
timer:set(cycle_time, timer:get_elapsed())
|
|
|
|
end)
|
|
|
|
meta:set_int("tubelib_first_run", 0)
|
|
|
|
end
|
2018-12-22 16:58:11 +01:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function NodeStates:standby(pos, meta)
|
|
|
|
if meta:get_int("tubelib_state") == RUNNING then
|
|
|
|
meta:set_int("tubelib_state", STANDBY)
|
|
|
|
-- timer has to be stopped once to be able to be restarted
|
|
|
|
self.stop_timer = true
|
|
|
|
if self.node_name_passive then
|
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
node.name = self.node_name_passive
|
|
|
|
minetest.swap_node(pos, node)
|
|
|
|
end
|
|
|
|
if self.infotext_name then
|
|
|
|
local number = meta:get_string("tubelib_number")
|
|
|
|
meta:set_string("infotext", self.infotext_name.." "..number..": standby")
|
|
|
|
end
|
|
|
|
if self.formspec_func then
|
2019-01-05 18:04:56 +01:00
|
|
|
meta:set_string("formspec", self.formspec_func(self, pos, meta))
|
2018-12-22 16:58:11 +01:00
|
|
|
end
|
|
|
|
minetest.get_node_timer(pos):start(self.cycle_time * self.standby_ticks)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
2019-05-20 23:40:20 +02:00
|
|
|
end
|
2018-12-22 16:58:11 +01:00
|
|
|
|
|
|
|
-- special case of standby for pushing nodes
|
|
|
|
function NodeStates:blocked(pos, meta)
|
|
|
|
if meta:get_int("tubelib_state") == RUNNING then
|
|
|
|
meta:set_int("tubelib_state", BLOCKED)
|
|
|
|
-- timer has to be stopped once to be able to be restarted
|
|
|
|
self.stop_timer = true
|
|
|
|
if self.node_name_passive then
|
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
node.name = self.node_name_passive
|
|
|
|
minetest.swap_node(pos, node)
|
|
|
|
end
|
|
|
|
if self.infotext_name then
|
|
|
|
local number = meta:get_string("tubelib_number")
|
|
|
|
meta:set_string("infotext", self.infotext_name.." "..number..": blocked")
|
|
|
|
end
|
|
|
|
if self.formspec_func then
|
2019-01-05 18:04:56 +01:00
|
|
|
meta:set_string("formspec", self.formspec_func(self, pos, meta))
|
2018-12-22 16:58:11 +01:00
|
|
|
end
|
|
|
|
minetest.get_node_timer(pos):start(self.cycle_time * self.standby_ticks)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
2019-05-20 23:40:20 +02:00
|
|
|
end
|
2018-12-22 16:58:11 +01:00
|
|
|
|
|
|
|
function NodeStates:fault(pos, meta)
|
|
|
|
if meta:get_int("tubelib_state") == RUNNING then
|
|
|
|
meta:set_int("tubelib_state", FAULT)
|
|
|
|
if self.node_name_passive then
|
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
node.name = self.node_name_passive
|
|
|
|
minetest.swap_node(pos, node)
|
|
|
|
end
|
|
|
|
if self.infotext_name then
|
|
|
|
local number = meta:get_string("tubelib_number")
|
|
|
|
meta:set_string("infotext", self.infotext_name.." "..number..": fault")
|
|
|
|
end
|
|
|
|
if self.formspec_func then
|
2019-01-05 18:04:56 +01:00
|
|
|
meta:set_string("formspec", self.formspec_func(self, pos, meta))
|
2018-12-22 16:58:11 +01:00
|
|
|
end
|
|
|
|
minetest.get_node_timer(pos):stop()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
2019-05-20 23:40:20 +02:00
|
|
|
end
|
2018-12-22 16:58:11 +01:00
|
|
|
|
|
|
|
function NodeStates:defect(pos, meta)
|
|
|
|
meta:set_int("tubelib_state", DEFECT)
|
|
|
|
if self.node_name_defect then
|
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
node.name = self.node_name_defect
|
|
|
|
minetest.swap_node(pos, node)
|
|
|
|
end
|
|
|
|
if self.infotext_name then
|
|
|
|
local number = meta:get_string("tubelib_number")
|
|
|
|
meta:set_string("infotext", self.infotext_name.." "..number..": defect")
|
|
|
|
end
|
|
|
|
if self.formspec_func then
|
2019-01-05 18:04:56 +01:00
|
|
|
meta:set_string("formspec", self.formspec_func(self, pos, meta))
|
2018-12-22 16:58:11 +01:00
|
|
|
end
|
|
|
|
minetest.get_node_timer(pos):stop()
|
|
|
|
return true
|
2019-05-20 23:40:20 +02:00
|
|
|
end
|
2018-12-22 16:58:11 +01:00
|
|
|
|
|
|
|
function NodeStates:get_state(meta)
|
|
|
|
return meta:get_int("tubelib_state")
|
|
|
|
end
|
|
|
|
|
|
|
|
function NodeStates:get_state_string(meta)
|
|
|
|
return tubelib.StateStrings[meta:get_int("tubelib_state")]
|
|
|
|
end
|
|
|
|
|
|
|
|
function NodeStates:is_active(meta)
|
|
|
|
local state = meta:get_int("tubelib_state")
|
|
|
|
if self.stop_timer == true then
|
|
|
|
self.stop_timer = false
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return state == RUNNING or state == STANDBY or state == BLOCKED
|
|
|
|
end
|
|
|
|
|
|
|
|
-- To be called if node is idle.
|
|
|
|
-- If countdown reaches zero, the node is set to STANDBY.
|
|
|
|
function NodeStates:idle(pos, meta)
|
|
|
|
local countdown = meta:get_int("tubelib_countdown") - 1
|
|
|
|
meta:set_int("tubelib_countdown", countdown)
|
|
|
|
if countdown < 0 then
|
|
|
|
self:standby(pos, meta)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- To be called after successful node action to raise the timer
|
|
|
|
-- and keep the node in state RUNNING
|
2018-12-25 22:06:05 +01:00
|
|
|
function NodeStates:keep_running(pos, meta, val, num_items)
|
2019-05-20 23:41:53 +02:00
|
|
|
if not num_items or num_items < 1 then num_items = 1 end
|
2018-12-22 16:58:11 +01:00
|
|
|
-- set to RUNNING if not already done
|
|
|
|
self:start(pos, meta, true)
|
|
|
|
meta:set_int("tubelib_countdown", val)
|
2018-12-25 22:06:05 +01:00
|
|
|
meta:set_int("tubelib_item_meter", meta:get_int("tubelib_item_meter") + (num_items or 1))
|
2020-08-12 20:15:26 +02:00
|
|
|
|
2018-12-22 16:58:11 +01:00
|
|
|
if self.aging_level1 then
|
2018-12-29 22:07:23 +01:00
|
|
|
local cnt = meta:get_int("tubelib_aging") + num_items
|
2018-12-22 16:58:11 +01:00
|
|
|
meta:set_int("tubelib_aging", cnt)
|
2019-05-17 00:05:59 +02:00
|
|
|
if (cnt > (self.aging_level1) and math.random(math.max(1, math.floor(self.aging_level2/num_items))) == 1)
|
2019-01-03 18:50:07 +01:00
|
|
|
or cnt >= 999999 then
|
2018-12-22 16:58:11 +01:00
|
|
|
self:defect(pos, meta)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Start/stop node based on button events.
|
|
|
|
-- if function returns false, no button was pressed
|
|
|
|
function NodeStates:state_button_event(pos, fields)
|
|
|
|
if fields.state_button ~= nil then
|
|
|
|
local state = self:get_state(M(pos))
|
|
|
|
if state == STOPPED or state == STANDBY or state == BLOCKED then
|
|
|
|
self:start(pos, M(pos))
|
|
|
|
elseif state == RUNNING or state == FAULT then
|
|
|
|
self:stop(pos, M(pos))
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function NodeStates:get_state_button_image(meta)
|
|
|
|
local state = meta:get_int("tubelib_state")
|
|
|
|
return tubelib.state_button(state)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- command interface
|
|
|
|
function NodeStates:on_receive_message(pos, topic, payload)
|
|
|
|
if topic == "on" then
|
|
|
|
self:start(pos, M(pos))
|
|
|
|
return true
|
|
|
|
elseif topic == "off" then
|
|
|
|
self:stop(pos, M(pos))
|
|
|
|
return true
|
|
|
|
elseif topic == "state" then
|
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
if node.name == "ignore" then -- unloaded node?
|
|
|
|
return "blocked"
|
|
|
|
end
|
|
|
|
return self:get_state_string(M(pos))
|
|
|
|
elseif self.has_item_meter and topic == "counter" then
|
|
|
|
return M(pos):get_int("tubelib_item_meter")
|
|
|
|
elseif self.has_item_meter and topic == "clear_counter" then
|
|
|
|
M(pos):set_int("tubelib_item_meter", 0)
|
|
|
|
return true
|
|
|
|
elseif self.aging_level1 and topic == "aging" then
|
|
|
|
return M(pos):get_int("tubelib_aging")
|
|
|
|
end
|
|
|
|
end
|
2019-05-20 23:40:20 +02:00
|
|
|
|
2018-12-22 16:58:11 +01:00
|
|
|
-- repair corrupt node data and/or migrate node to state2
|
2019-01-03 10:28:07 +01:00
|
|
|
function NodeStates:on_node_load(pos, not_start_timer)
|
2018-12-22 16:58:11 +01:00
|
|
|
local meta = minetest.get_meta(pos)
|
2019-05-20 23:40:20 +02:00
|
|
|
|
2018-12-22 16:58:11 +01:00
|
|
|
-- legacy node number/state/counter?
|
|
|
|
local number = meta:get_string("number")
|
2018-12-31 21:00:19 +01:00
|
|
|
if number ~= "" and number ~= nil then
|
2018-12-22 16:58:11 +01:00
|
|
|
meta:set_string("tubelib_number", number)
|
|
|
|
meta:set_int("tubelib_state", tubelib.state(meta:get_int("running")))
|
|
|
|
if self.has_item_meter then
|
|
|
|
meta:set_int("tubelib_item_meter", meta:get_int("counter"))
|
|
|
|
end
|
|
|
|
if self.aging_level1 then
|
|
|
|
meta:set_int("tubelib_aging", 0)
|
|
|
|
end
|
|
|
|
meta:set_string("number", nil)
|
|
|
|
meta:set_int("running", 0)
|
|
|
|
meta:set_int("counter", 0)
|
|
|
|
end
|
|
|
|
|
2019-04-23 21:46:08 +02:00
|
|
|
-- node corrupt?
|
|
|
|
if not tubelib.data_not_corrupted(pos) then
|
|
|
|
return
|
2018-12-22 16:58:11 +01:00
|
|
|
end
|
2019-05-20 23:40:20 +02:00
|
|
|
|
2018-12-22 16:58:11 +01:00
|
|
|
-- state corrupt?
|
|
|
|
local state = meta:get_int("tubelib_state")
|
|
|
|
if state == 0 then
|
|
|
|
if minetest.get_node_timer(pos):is_started() then
|
|
|
|
meta:set_int("tubelib_state", RUNNING)
|
|
|
|
else
|
|
|
|
meta:set_int("tubelib_state", STOPPED)
|
|
|
|
end
|
2019-01-03 10:28:07 +01:00
|
|
|
elseif state == RUNNING and not not_start_timer then
|
2018-12-22 16:58:11 +01:00
|
|
|
minetest.get_node_timer(pos):start(self.cycle_time)
|
|
|
|
elseif state == STANDBY then
|
|
|
|
minetest.get_node_timer(pos):start(self.cycle_time * self.standby_ticks)
|
|
|
|
elseif state == BLOCKED then
|
|
|
|
minetest.get_node_timer(pos):start(self.cycle_time * self.standby_ticks)
|
|
|
|
end
|
2019-05-20 23:40:20 +02:00
|
|
|
|
2018-12-28 22:16:55 +01:00
|
|
|
if self.formspec_func then
|
2019-01-05 18:04:56 +01:00
|
|
|
meta:set_string("formspec", self.formspec_func(self, pos, meta))
|
2018-12-28 22:16:55 +01:00
|
|
|
end
|
2018-12-22 16:58:11 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Repair of defect (feature!) nodes
|
|
|
|
function NodeStates:on_node_repair(pos)
|
|
|
|
local meta = M(pos)
|
|
|
|
if meta:get_int("tubelib_state") == DEFECT then
|
|
|
|
meta:set_int("tubelib_state", STOPPED)
|
|
|
|
if self.node_name_passive then
|
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
node.name = self.node_name_passive
|
|
|
|
minetest.swap_node(pos, node)
|
|
|
|
end
|
|
|
|
if self.aging_level1 then
|
|
|
|
meta:set_int("tubelib_aging", 0)
|
|
|
|
end
|
|
|
|
if self.infotext_name then
|
|
|
|
local number = meta:get_string("tubelib_number")
|
|
|
|
meta:set_string("infotext", self.infotext_name.." "..number..": stopped")
|
|
|
|
end
|
|
|
|
if self.formspec_func then
|
2019-01-05 18:04:56 +01:00
|
|
|
meta:set_string("formspec", self.formspec_func(self, pos, meta))
|
2018-12-22 16:58:11 +01:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
2019-05-20 23:40:20 +02:00
|
|
|
end
|
2018-12-22 16:58:11 +01:00
|
|
|
|
2020-08-12 20:15:26 +02:00
|
|
|
|
|
|
|
--[[
|
|
|
|
Callback after digging a node but before removing the node.
|
|
|
|
|
|
|
|
The tubelib node becomes defect after digging it:
|
|
|
|
- always if the aging counter "tubelib_aging" is greater than self.aging_level2
|
|
|
|
- with a certain probability if the aging counter "tubelib_aging" is greater than self.aging_level1
|
|
|
|
but smaller than self.aging_level2
|
|
|
|
|
|
|
|
Info: If a tubelib machine has been running quite some time but is dropped as a non-defect machine and then placed back again, the
|
|
|
|
tubelib machine will be reset to new (digging will reset the aging counter). So this code tries to prevent this exploit
|
|
|
|
|
|
|
|
]]--
|
|
|
|
function NodeStates:on_dig_node(pos, node, player)
|
|
|
|
local meta = M(pos)
|
|
|
|
local cnt = tonumber(meta:get_string("tubelib_aging"))
|
|
|
|
if (not cnt or cnt < 1) then
|
|
|
|
cnt = 1
|
|
|
|
end
|
|
|
|
|
|
|
|
local is_defect = (cnt > self.aging_level1) and ( math.random(math.max(1, math.floor(self.aging_level2 / cnt))) == 1 )
|
|
|
|
|
|
|
|
if is_defect then
|
|
|
|
self:defect(pos, meta) -- replace node with defect one
|
|
|
|
node = minetest.get_node(pos)
|
2018-12-22 16:58:11 +01:00
|
|
|
end
|
2020-08-12 20:15:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
minetest.node_dig(pos, node, player) -- default behaviour (this function is called automatically if on_dig() callback isn't set)
|
|
|
|
|
2018-12-22 16:58:11 +01:00
|
|
|
end
|
2020-08-12 20:15:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|