Skip to content

Commit

Permalink
limit certain nodes to individual max
Browse files Browse the repository at this point in the history
Some nodes have cpu intensive side-effects when placed.
To avoid lag, this commit adds mechanism to limit per node.
  • Loading branch information
SwissalpS committed Feb 21, 2020
1 parent 48aec8c commit 608c8c4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
4 changes: 4 additions & 0 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ local path = minetest.get_modpath("replacer")

replacer = {}

-- limit by node, use replacer.register_limit(sName, iMax)
replacer.limit_list = {}

-- don't allow these at all
replacer.blacklist = {}

-- playing with tnt and creative building are usually contradictory
Expand Down
29 changes: 28 additions & 1 deletion replacer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,33 @@ replacer.mode_colours[r.modes[2]] = "#54FFAC"
replacer.mode_colours[r.modes[3]] = "#9F6200"
replacer.mode_colours[r.modes[4]] = "#FF5457"

local is_int = function(value)
return type(value) == 'number' and math.floor(value) == value
end

function replacer.register_limit(node_name, node_max)
-- ignore nil and negative numbers
if (nil == node_max) or (0 > node_max) then
return
end
-- ignore non-integers
if not is_int(node_max) then
return
end
-- add to blacklist if limit is zero
if 0 == node_max then
replacer.blacklist[node_name] = true
minetest.log("info", rb.blacklist_insert:format(node_name))
return
end
-- log info if already limited
if nil ~= r.limit_list[node_name] then
minetest.log("info", rb.limit_override:format(node_name, r.limit_list[node_name]))
end
r.limit_list[node_name] = node_max
minetest.log("info", rb.limit_insert:format(node_name, node_max))
end

function replacer.get_data(stack)
local metaRef = stack:get_meta()
local data = metaRef:get_string("replacer"):split(" ") or {}
Expand Down Expand Up @@ -257,7 +284,7 @@ function replacer.replace(itemstack, user, pt, right_clicked)
return
end

local max_nodes = replacer.max_nodes
local max_nodes = r.limit_list[nnd.name] or r.max_nodes
local charge
if replacer.has_technic_mod and (not (creative_enabled or has_give)) then
charge = r.get_charge(itemstack)
Expand Down
3 changes: 3 additions & 0 deletions replacer_blabla.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ replacer.blabla.not_in_inventory = 'Item not in your inventory: "%s".'
replacer.blabla.set_to = 'Node replacement tool set to: "%s".'
replacer.blabla.description_basic = "Node replacement tool"
replacer.blabla.description_technic = "Node replacement tool (technic)"
replacer.blabla.limit_override = 'Setting already set node-limit for "%s" was %d.'
replacer.blabla.limit_insert = 'Setting node-limit for "%s" to %d.'
replacer.blabla.blacklist_insert = 'Blacklisted "%s".'

0 comments on commit 608c8c4

Please sign in to comment.