Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor base machine production loop #397

Merged
merged 2 commits into from
Feb 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 23 additions & 26 deletions technic/machines/register/machine_base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ end

local connect_default = {"bottom", "back", "left", "right"}

local function round(v)
return math.floor(v + 0.5)
end

function technic.register_base_machine(nodename, data)
local colon, modname, name, def = technic.register_compat_v1_to_v2(nodename, data)
local texture_prefix = modname.."_"..name
Expand Down Expand Up @@ -106,9 +102,15 @@ function technic.register_base_machine(nodename, data)
tube.insert_object = def.insert_object
end

local update_node = function(pos, meta, newnode, infotext, demand, src_time)
technic.swap_node(pos, newnode)
meta:set_string("infotext", infotext)
meta:set_int(tier.."_EU_demand", demand)
meta:set_int("src_time", src_time)
end

local run = function(pos, node)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local eu_input = meta:get_int(tier.."_EU_input")
local machine_demand = def.demand

Expand All @@ -127,38 +129,33 @@ function technic.register_base_machine(nodename, data)
technic.handle_machine_pipeworks(pos, tube_upgrade)
end

local powered = eu_input >= machine_demand[EU_upgrade+1]
local inv = meta:get_inventory()
local demand = machine_demand[EU_upgrade+1]
local powered = eu_input >= demand
local src_time = meta:get_int("src_time")
if powered then
meta:set_int("src_time", meta:get_int("src_time") + round(def.speed*10))
src_time = src_time + math.floor(def.speed * 10 + 0.5)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(just fyi, not really an issue here) i tried "inlining" some of the builtin functions and got a few percent more performance out of it if they are called often (for example here: https://github.com/BuckarooBanzay/mapsync/blob/master/encoding.lua#L1)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, thought that for a moment too when I removed round(n) function but most of time this loop is executed something like twice or so unless someone add some extreme def.speed machine. Of course this times number of active machine in world but even then would still be very low.

I guess that could even be done later like for everything at once and make it kinda like a style rule rather than based on sometimes questionable benefit.

I did actually think a bit about optimizations too: this probably could be rewritten to grab all ingredients at once, calculate iterations beforehand, process recipes directly and modify inventories only once. (through I'm not gonna go there just yet 😅)

end
while true do
local recipe = inv:get_list("src") and technic.get_recipe(typename, inv:get_list("src"))
if not recipe then
technic.swap_node(pos, nodename)
meta:set_string("infotext", infotext_idle)
meta:set_int(tier.."_EU_demand", 0)
meta:set_int("src_time", 0)
update_node(pos, meta, nodename, infotext_idle, 0, 0)
return
end
meta:set_int(tier.."_EU_demand", machine_demand[EU_upgrade+1])
technic.swap_node(pos, nodename.."_active")
meta:set_string("infotext", infotext_active .. "\n" ..
S("Demand: @1", technic.EU_string(machine_demand[EU_upgrade+1])))
if meta:get_int("src_time") < round(recipe.time*10) then
if not powered then
technic.swap_node(pos, nodename)
meta:set_string("infotext", infotext_unpowered)
local recipe_time = math.floor(recipe.time * 10 + 0.5)
if src_time < recipe_time then
if powered then
local infotext = infotext_active .. "\n" .. S("Demand: @1", technic.EU_string(demand))
update_node(pos, meta, nodename.."_active", infotext, demand, src_time)
else
update_node(pos, meta, nodename, infotext_unpowered, demand, src_time)
end
return
end
if not technic.process_recipe(recipe, inv) then
technic.swap_node(pos, nodename)
meta:set_string("infotext", infotext_idle)
meta:set_int(tier.."_EU_demand", 0)
meta:set_int("src_time", round(recipe.time*10))
elseif not technic.process_recipe(recipe, inv) then
update_node(pos, meta, nodename, infotext_idle, 0, recipe_time)
return
end
meta:set_int("src_time", meta:get_int("src_time") - round(recipe.time*10))
src_time = src_time - recipe_time
end
end

Expand Down
Loading