bug fixes and improvements

This commit is contained in:
Joachim Stolberg 2018-12-28 17:16:14 +01:00
parent 58718c2f0f
commit 021f5c450c
6 changed files with 103 additions and 56 deletions

@ -3,7 +3,7 @@
Gravel Sieve Mod Gravel Sieve Mod
================ ================
v1.07 by JoSt v1.09 by JoSt
Derived from the work of celeron55, Perttu Ahola (furnace) Derived from the work of celeron55, Perttu Ahola (furnace)
Pipeworks support added by FiftySix Pipeworks support added by FiftySix
@ -35,6 +35,7 @@
2018-01-01 V1.06 * Hopper support added 2018-01-01 V1.06 * Hopper support added
2018-01-02 V1.07 * changed to registered ores 2018-01-02 V1.07 * changed to registered ores
2018-02-09 V1.08 * Pipeworks support added, bugfix for issue #7 2018-02-09 V1.08 * Pipeworks support added, bugfix for issue #7
2018-12-28 V1.09 * Ore probability calculation changed (thanks to obl3pplifp)
]]-- ]]--
gravelsieve = { gravelsieve = {
@ -70,28 +71,28 @@ local function add_ores()
and drop ~= item.ore and drop ~= item.ore
and drop ~= "" and drop ~= ""
and item.ore_type == "scatter" and item.ore_type == "scatter"
and item.wherein == "default:stone"
and item.clust_scarcity ~= nil and item.clust_scarcity > 0 and item.clust_scarcity ~= nil and item.clust_scarcity > 0
and item.clust_size ~= nil and item.clust_size > 0 then and item.clust_num_ores ~= nil and item.clust_num_ores > 0
local probability = item.clust_scarcity / item.clust_size / and item.y_max ~= nil and item.y_min ~= nil then
PROBABILITY_FACTOR * gravelsieve.ore_rarity local probability = (gravelsieve.ore_rarity / PROBABILITY_FACTOR) * item.clust_scarcity /
probability = math.floor(probability) (item.clust_num_ores * ((item.y_max - item.y_min) / 65535))
if probability > 20 then if gravelsieve.ore_probability[drop] == nil then
if gravelsieve.ore_probability[drop] == nil then gravelsieve.ore_probability[drop] = probability
gravelsieve.ore_probability[drop] = probability else
else -- harmonic sum
gravelsieve.ore_probability[drop] = gravelsieve.ore_probability[drop] = 1.0 / ((1.0 / gravelsieve.ore_probability[drop]) +
math.min(gravelsieve.ore_probability[drop], probability) (1.0 / probability))
end
end end
end end
end end
end end
local overall_probability = 0.0 local overall_probability = 0.0
for name,probability in pairs(gravelsieve.ore_probability) do for name,probability in pairs(gravelsieve.ore_probability) do
print(string.format("[gravelsieve] %-32s %u", name, probability)) minetest.log("info", string.format("[gravelsieve] %-32s %u", name, probability))
overall_probability = overall_probability + 1.0/probability overall_probability = overall_probability + 1.0/probability
end end
print(string.format("[gravelsieve] Overall probability %g", overall_probability)) minetest.log("info", string.format("[gravelsieve] Overall probability %g", overall_probability))
end end
minetest.after(1, add_ores) minetest.after(1, add_ores)

@ -26,6 +26,7 @@ local storage = minetest.get_mod_storage()
local NextNumber = minetest.deserialize(storage:get_string("NextNumber")) or 1 local NextNumber = minetest.deserialize(storage:get_string("NextNumber")) or 1
local Version = minetest.deserialize(storage:get_string("Version")) or 1 local Version = minetest.deserialize(storage:get_string("Version")) or 1
local Number2Pos = minetest.deserialize(storage:get_string("Number2Pos")) or {} local Number2Pos = minetest.deserialize(storage:get_string("Number2Pos")) or {}
local FreeNumbers = {}
local function update_mod_storage() local function update_mod_storage()
minetest.log("action", "[Tubelib] Store data...") minetest.log("action", "[Tubelib] Store data...")
@ -70,11 +71,18 @@ end
-- Determine position related node number for addressing purposes -- Determine position related node number for addressing purposes
local function get_number(pos) local function get_number(pos)
local key = get_key_str(pos) local key = get_key_str(pos)
local num
if not Key2Number[key] then if not Key2Number[key] then
Key2Number[key] = NextNumber num = table.remove(FreeNumbers)
NextNumber = NextNumber + 1 if not num then
Key2Number[key] = NextNumber
NextNumber = NextNumber + 1
num = string.format("%.04u", Key2Number[key])
end
else
num = string.format("%.04u", Key2Number[key])
end end
return string.format("%.04u", Key2Number[key]) return num
end end
local function generate_Key2Number() local function generate_Key2Number()
@ -525,6 +533,14 @@ local function data_maintenance()
end end
end end
end end
-- collect unused node numbers
for idx = 1,NextNumber-1 do
--FreeNumbers
local num = string.format("%.04u", idx)
if not Number2Pos[num] then
FreeNumbers[#FreeNumbers+1] = num
end
end
print("[Tubelib] Data maintenance finished") print("[Tubelib] Data maintenance finished")
end end

@ -52,9 +52,10 @@ local function calc_area(pos)
end end
local function add_pos(pos, player) local function add_pos(pos, player)
local pos1 = calc_area(pos)
local lPos = minetest.deserialize(player:get_attribute("tubelib_forceload_blocks")) or {} local lPos = minetest.deserialize(player:get_attribute("tubelib_forceload_blocks")) or {}
if not in_list(lPos, pos) and #lPos < tubelib.max_num_forceload_blocks then if not in_list(lPos, pos1) and #lPos < tubelib.max_num_forceload_blocks then
lPos[#lPos+1] = pos lPos[#lPos+1] = pos1
player:set_attribute("tubelib_forceload_blocks", minetest.serialize(lPos)) player:set_attribute("tubelib_forceload_blocks", minetest.serialize(lPos))
return true return true
end end
@ -62,9 +63,9 @@ local function add_pos(pos, player)
end end
local function del_pos(pos, player) local function del_pos(pos, player)
local pos1 = calc_area(pos)
local lPos = minetest.deserialize(player:get_attribute("tubelib_forceload_blocks")) or {} local lPos = minetest.deserialize(player:get_attribute("tubelib_forceload_blocks")) or {}
lPos = remove_list_elem(lPos, pos) lPos = remove_list_elem(lPos, pos1)
lPos = remove_list_elem(lPos, pos)
player:set_attribute("tubelib_forceload_blocks", minetest.serialize(lPos)) player:set_attribute("tubelib_forceload_blocks", minetest.serialize(lPos))
end end
@ -79,6 +80,27 @@ local function get_data(pos, player)
return pos1, pos2, num, max return pos1, pos2, num, max
end end
local function formspec(player)
local lPos = get_pos_list(player)
local tRes = {}
tRes[1] = "size[7,9]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"label[0,0;List of your Forceload Blocks:]"
for idx,pos in ipairs(lPos) do
local pos1, pos2 = calc_area(pos)
local ypos = 0.2 + idx * 0.4
tRes[#tRes+1] = "label[0,"..ypos..";"..idx.."]"
tRes[#tRes+1] = "label[0.8,"..ypos..";"..S(pos1).."]"
tRes[#tRes+1] = "label[3.2,"..ypos..";to]"
tRes[#tRes+1] = "label[4,"..ypos..";"..S(pos2).."]"
end
return table.concat(tRes)
end
minetest.register_node("tubelib:forceload", { minetest.register_node("tubelib:forceload", {
description = "Tubelib Forceload Block", description = "Tubelib Forceload Block",
tiles = { tiles = {
@ -107,7 +129,7 @@ minetest.register_node("tubelib:forceload", {
tubelib.mark_region(placer:get_player_name(), pos1, pos2) tubelib.mark_region(placer:get_player_name(), pos1, pos2)
M(pos):set_string("owner", placer:get_player_name()) M(pos):set_string("owner", placer:get_player_name())
else else
chat(placer, "Max. number of Forceload Blocks reached!") chat(placer, "Area already loaded or max. number of Forceload Blocks reached!")
minetest.remove_node(pos) minetest.remove_node(pos)
return itemstack return itemstack
end end
@ -119,6 +141,14 @@ minetest.register_node("tubelib:forceload", {
tubelib.unmark_region(oldmetadata.fields.owner) tubelib.unmark_region(oldmetadata.fields.owner)
end, end,
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
if M(pos):get_string("owner") == clicker:get_player_name() or
minetest.check_player_privs(clicker:get_player_name(), "server") then
local s = formspec(clicker)
minetest.show_formspec(clicker:get_player_name(), "tubelib:forceload", s)
end
end,
on_punch = function(pos, node, puncher, pointed_thing) on_punch = function(pos, node, puncher, pointed_thing)
local pos1, pos2 = calc_area(pos) local pos1, pos2 = calc_area(pos)
tubelib.switch_region(puncher:get_player_name(), pos1, pos2) tubelib.switch_region(puncher:get_player_name(), pos1, pos2)

@ -92,6 +92,8 @@ function NodeStates:new(attr)
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,
on_start = attr.on_start,
on_stop = attr.on_stop,
} }
if attr.aging_factor then if attr.aging_factor then
o.aging_level1 = attr.aging_factor * tubelib.machine_aging_value o.aging_level1 = attr.aging_factor * tubelib.machine_aging_value
@ -125,7 +127,11 @@ function NodeStates:node_init(pos, number)
end end
function NodeStates:stop(pos, meta) function NodeStates:stop(pos, meta)
if meta:get_int("tubelib_state") ~= DEFECT then local state = meta:get_int("tubelib_state")
if state ~= DEFECT then
if self.on_stop then
self.on_stop(pos, meta, state)
end
meta:set_int("tubelib_state", STOPPED) meta:set_int("tubelib_state", STOPPED)
if self.node_name_passive then if self.node_name_passive then
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
@ -151,6 +157,9 @@ function NodeStates:start(pos, meta, called_from_on_timer)
if not self.start_condition_fullfilled(pos, meta) then if not self.start_condition_fullfilled(pos, meta) then
return false return false
end end
if self.on_start then
self.on_start(pos, meta, state)
end
meta:set_int("tubelib_state", RUNNING) meta:set_int("tubelib_state", RUNNING)
if called_from_on_timer then if called_from_on_timer then
-- timer has to be stopped once to be able to be restarted -- timer has to be stopped once to be able to be restarted

@ -32,6 +32,13 @@ local STANDBY_TICKS = 4 -- used for blocked state
local COUNTDOWN_TICKS = 2 local COUNTDOWN_TICKS = 2
local OFFSET = 5 -- for uneven terrains local OFFSET = 5 -- for uneven terrains
-- start on top of the base block
local function working_start_pos(pos)
local working_pos = table.copy(pos)
working_pos.y = working_pos.y + MAX_HEIGHT
return working_pos
end
local State = tubelib.NodeStates:new({ local State = tubelib.NodeStates:new({
node_name_passive = "tubelib_addons1:harvester_base", node_name_passive = "tubelib_addons1:harvester_base",
node_name_defect = "tubelib_addons1:harvester_defect", node_name_defect = "tubelib_addons1:harvester_defect",
@ -40,6 +47,12 @@ local State = tubelib.NodeStates:new({
standby_ticks = STANDBY_TICKS, standby_ticks = STANDBY_TICKS,
has_item_meter = true, has_item_meter = true,
aging_factor = 15, aging_factor = 15,
on_start = function(pos, meta, oldstate)
local this = minetest.deserialize(meta:get_string("this"))
this.idx = 0
this.working_pos = working_start_pos(pos)
meta:set_string("this", minetest.serialize(this))
end,
}) })
local Radius2Idx = {[4]=1 ,[6]=2, [8]=3, [10]=4, [12]=5, [14]=6, [16]=7} local Radius2Idx = {[4]=1 ,[6]=2, [8]=3, [10]=4, [12]=5, [14]=6, [16]=7}
@ -118,19 +131,11 @@ local function allow_metadata_inventory_take(pos, listname, index, stack, player
return stack:get_count() return stack:get_count()
end end
-- start on top of the base block
local function working_start_pos(pos)
local working_pos = table.copy(pos)
working_pos.y = working_pos.y + MAX_HEIGHT
return working_pos
end
local function get_next_pos(old_pos, idx) local function get_next_pos(old_pos, idx)
local facedir = WorkingSteps[idx] local facedir = WorkingSteps[idx]
return vector.add(old_pos, core.facedir_to_dir(facedir)) return vector.add(old_pos, core.facedir_to_dir(facedir))
end end
-- Remove saplings lying arround -- Remove saplings lying arround
local function remove_all_sapling_items(pos) local function remove_all_sapling_items(pos)
for _, object in pairs(minetest.get_objects_inside_radius(pos, 4)) do for _, object in pairs(minetest.get_objects_inside_radius(pos, 4)) do
@ -241,11 +246,11 @@ end
local function keep_running(pos, elapsed) local function keep_running(pos, elapsed)
local meta = M(pos) local meta = M(pos)
local this = minetest.deserialize(meta:get_string("this")) local this = minetest.deserialize(meta:get_string("this"))
this.num_items = 0
if not_blocked(pos, this, meta) then if not_blocked(pos, this, meta) then
if check_fuel(pos, this, meta) then if check_fuel(pos, this, meta) then
if calc_new_pos(pos, this, meta) then if calc_new_pos(pos, this, meta) then
this.num_items = 0
if harvest_field(this, meta) then if harvest_field(this, meta) then
meta:set_string("this", minetest.serialize(this)) meta:set_string("this", minetest.serialize(this))
meta:set_string("infotext", meta:set_string("infotext",
@ -287,16 +292,7 @@ local function on_receive_fields(pos, formname, fields, player)
this.endless = fields.endless == "true" and 1 or 0 this.endless = fields.endless == "true" and 1 or 0
end end
if fields.state_button ~= nil then State:state_button_event(pos, fields)
local state = State:get_state(meta)
if state == tubelib.STOPPED or state == tubelib.STANDBY or state == tubelib.BLOCKED then
this.idx = 0
this.working_pos = working_start_pos(pos)
State:start(pos, meta)
elseif state == tubelib.RUNNING or state == tubelib.FAULT then
State:stop(pos, meta)
end
end
meta:set_string("this", minetest.serialize(this)) meta:set_string("this", minetest.serialize(this))
end end

@ -43,6 +43,12 @@ local State = tubelib.NodeStates:new({
standby_ticks = STANDBY_TICKS, standby_ticks = STANDBY_TICKS,
has_item_meter = true, has_item_meter = true,
aging_factor = 12, aging_factor = 12,
on_stop = function(pos, meta, oldstate)
if oldstate == tubelib.RUNNING then
meta:set_int("idx", 1) -- restart from the beginning
meta:set_string("quarry_pos", nil)
end
end,
}) })
local function formspec(pos, meta) local function formspec(pos, meta)
@ -110,8 +116,7 @@ local function allow_metadata_inventory_put(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then if minetest.is_protected(pos, player:get_player_name()) then
return 0 return 0
end end
local meta = minetest.get_meta(pos) local inv = M(pos):get_inventory()
local inv = meta:get_inventory()
if listname == "main" then if listname == "main" then
return stack:get_count() return stack:get_count()
elseif listname == "fuel" and stack:get_name() == "tubelib_addons1:biofuel" then elseif listname == "fuel" and stack:get_name() == "tubelib_addons1:biofuel" then
@ -195,7 +200,7 @@ local function quarry_next_node(pos, meta)
local node = get_node_lvm(quarry_pos) local node = get_node_lvm(quarry_pos)
if node then if node then
local number = meta:get_string("number") local number = meta:get_string("tubelib_number")
local order = tubelib_addons1.GroundNodes[node.name] local order = tubelib_addons1.GroundNodes[node.name]
if order ~= nil then if order ~= nil then
local inv = meta:get_inventory() local inv = meta:get_inventory()
@ -253,16 +258,7 @@ local function on_receive_fields(pos, formname, fields, player)
end end
meta:set_int("endless", endless) meta:set_int("endless", endless)
if fields.state_button ~= nil then State:state_button_event(pos, fields)
local state = State:get_state(meta)
if state == tubelib.STOPPED or state == tubelib.STANDBY or state == tubelib.BLOCKED then
meta:set_int("idx", 1) -- restart from the beginning
meta:set_string("quarry_pos", nil)
State:start(pos, meta)
elseif state == tubelib.RUNNING or state == tubelib.FAULT then
State:stop(pos, meta)
end
end
end end
minetest.register_node("tubelib_addons1:quarry", { minetest.register_node("tubelib_addons1:quarry", {
@ -285,7 +281,6 @@ minetest.register_node("tubelib_addons1:quarry", {
local number = tubelib.add_node(pos, "tubelib_addons1:quarry") local number = tubelib.add_node(pos, "tubelib_addons1:quarry")
local facedir = minetest.dir_to_facedir(placer:get_look_dir(), false) local facedir = minetest.dir_to_facedir(placer:get_look_dir(), false)
meta:set_int("facedir", facedir) meta:set_int("facedir", facedir)
meta:set_string("number", number)
meta:set_string("owner", placer:get_player_name()) meta:set_string("owner", placer:get_player_name())
meta:set_int("endless", 0) meta:set_int("endless", 0)
meta:set_int("curr_level", -1) meta:set_int("curr_level", -1)