fix some more crashes due to outdated nodes not having the right metadata

This commit is contained in:
obl3pplifp 2019-05-20 22:36:14 +01:00
parent 84e574156e
commit 7a7784fc5e

@ -15,33 +15,33 @@
]]-- ]]--
--[[ --[[
Node states: Node states:
+-----------------------------------+ +------------+ +-----------------------------------+ +------------+
| | | | | | | |
| V V | | V V |
| +---------+ | | +---------+ |
| | | | | | | |
| +---------| STOPPED | | | +---------| STOPPED | |
| | | | | | | | | |
| button | +---------+ | | button | +---------+ |
| | ^ | | | ^ |
repair | V | button | repair | V | button |
| +---------+ | | button | +---------+ | | button
| | |---------+ | | | |---------+ |
| | RUNNING | | | | RUNNING | |
| +--------| |---------+ | | +--------| |---------+ |
| | +---------+ | | | | +---------+ | |
| | ^ | | | | | ^ | | |
| | | | | | | | | | | |
| V | V V | | V | V V |
| +---------+ +----------+ +---------+ | | +---------+ +----------+ +---------+ |
| | | | | | | | | | | | | | | |
+---| DEFECT | | STANDBY/ | | FAULT |----------+ +---| DEFECT | | STANDBY/ | | FAULT |----------+
| | | BLOCKED | | | | | | BLOCKED | | |
+---------+ +----------+ +---------+ +---------+ +----------+ +---------+
Node metadata: Node metadata:
"tubelib_number" - string with tubelib number, like "0123" "tubelib_number" - string with tubelib number, like "0123"
@ -88,7 +88,7 @@ function NodeStates:new(attr)
has_item_meter = attr.has_item_meter, -- true/false has_item_meter = attr.has_item_meter, -- true/false
-- optional -- optional
node_name_passive = attr.node_name_passive, node_name_passive = attr.node_name_passive,
node_name_active = attr.node_name_active, node_name_active = attr.node_name_active,
node_name_defect = attr.node_name_defect, node_name_defect = attr.node_name_defect,
infotext_name = attr.infotext_name, infotext_name = attr.infotext_name,
start_condition_fullfilled = attr.start_condition_fullfilled or start_condition_fullfilled, start_condition_fullfilled = attr.start_condition_fullfilled or start_condition_fullfilled,
@ -202,7 +202,7 @@ function NodeStates:standby(pos, meta)
return true return true
end end
return false return false
end end
-- special case of standby for pushing nodes -- special case of standby for pushing nodes
function NodeStates:blocked(pos, meta) function NodeStates:blocked(pos, meta)
@ -226,7 +226,7 @@ function NodeStates:blocked(pos, meta)
return true return true
end end
return false return false
end end
function NodeStates:fault(pos, meta) function NodeStates:fault(pos, meta)
if meta:get_int("tubelib_state") == RUNNING then if meta:get_int("tubelib_state") == RUNNING then
@ -247,7 +247,7 @@ function NodeStates:fault(pos, meta)
return true return true
end end
return false return false
end end
function NodeStates:defect(pos, meta) function NodeStates:defect(pos, meta)
meta:set_int("tubelib_state", DEFECT) meta:set_int("tubelib_state", DEFECT)
@ -265,7 +265,7 @@ function NodeStates:defect(pos, meta)
end end
minetest.get_node_timer(pos):stop() minetest.get_node_timer(pos):stop()
return true return true
end end
function NodeStates:get_state(meta) function NodeStates:get_state(meta)
return meta:get_int("tubelib_state") return meta:get_int("tubelib_state")
@ -297,7 +297,7 @@ end
-- To be called after successful node action to raise the timer -- To be called after successful node action to raise the timer
-- and keep the node in state RUNNING -- and keep the node in state RUNNING
function NodeStates:keep_running(pos, meta, val, num_items) function NodeStates:keep_running(pos, meta, val, num_items)
num_items = num_items or 1 if not num_items or num_items < 1 then num_items = 1 end
-- set to RUNNING if not already done -- set to RUNNING if not already done
self:start(pos, meta, true) self:start(pos, meta, true)
meta:set_int("tubelib_countdown", val) meta:set_int("tubelib_countdown", val)
@ -355,11 +355,11 @@ function NodeStates:on_receive_message(pos, topic, payload)
return M(pos):get_int("tubelib_aging") return M(pos):get_int("tubelib_aging")
end end
end end
-- repair corrupt node data and/or migrate node to state2 -- repair corrupt node data and/or migrate node to state2
function NodeStates:on_node_load(pos, not_start_timer) function NodeStates:on_node_load(pos, not_start_timer)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
-- legacy node number/state/counter? -- legacy node number/state/counter?
local number = meta:get_string("number") local number = meta:get_string("number")
if number ~= "" and number ~= nil then if number ~= "" and number ~= nil then
@ -380,7 +380,7 @@ function NodeStates:on_node_load(pos, not_start_timer)
if not tubelib.data_not_corrupted(pos) then if not tubelib.data_not_corrupted(pos) then
return return
end end
-- state corrupt? -- state corrupt?
local state = meta:get_int("tubelib_state") local state = meta:get_int("tubelib_state")
if state == 0 then if state == 0 then
@ -396,7 +396,7 @@ function NodeStates:on_node_load(pos, not_start_timer)
elseif state == BLOCKED then elseif state == BLOCKED then
minetest.get_node_timer(pos):start(self.cycle_time * self.standby_ticks) minetest.get_node_timer(pos):start(self.cycle_time * self.standby_ticks)
end end
if self.formspec_func then if self.formspec_func then
meta:set_string("formspec", self.formspec_func(self, pos, meta)) meta:set_string("formspec", self.formspec_func(self, pos, meta))
end end
@ -425,12 +425,13 @@ function NodeStates:on_node_repair(pos)
return true return true
end end
return false return false
end end
-- Return working or defect machine, depending on machine lifetime -- Return working or defect machine, depending on machine lifetime
function NodeStates:after_dig_node(pos, oldnode, oldmetadata, digger) function NodeStates:after_dig_node(pos, oldnode, oldmetadata, digger)
local inv = minetest.get_inventory({type="player", name=digger:get_player_name()}) local inv = minetest.get_inventory({type="player", name=digger:get_player_name()})
local cnt = oldmetadata.fields.tubelib_aging and tonumber(oldmetadata.fields.tubelib_aging) or 0 local cnt = oldmetadata.fields.tubelib_aging and tonumber(oldmetadata.fields.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 local is_defect = cnt > self.aging_level1 and math.random(math.max(1, math.floor(self.aging_level2 / cnt))) == 1
if self.node_name_defect and is_defect then if self.node_name_defect and is_defect then
inv:add_item("main", ItemStack(self.node_name_defect)) inv:add_item("main", ItemStack(self.node_name_defect))