Tie item ejection rate to machine production rate.

Items will be ejected from machines as quickly as they are processed only while the machine is active and fully upgraded with control logic units.
This commit is contained in:
Bluebird
2015-02-21 21:03:05 -06:00
parent a793747d92
commit 5bc306d4ba
2 changed files with 57 additions and 19 deletions

View File

@ -51,7 +51,7 @@ local function on_machine_downgrade(meta, stack, list)
local inv = meta:get_inventory() local inv = meta:get_inventory()
local upg1, upg2 = inv:get_stack("upgrade1", 1), inv:get_stack("upgrade2", 1) local upg1, upg2 = inv:get_stack("upgrade1", 1), inv:get_stack("upgrade2", 1)
-- only set 0 if theres not a nother chest in the other list too -- only set 0 if theres not another chest in the other list too
if (not upg1 or not upg2 or upg1:get_name() ~= upg2:get_name()) then if (not upg1 or not upg2 or upg1:get_name() ~= upg2:get_name()) then
meta:set_int("public", 0) meta:set_int("public", 0)
end end
@ -60,7 +60,7 @@ local function on_machine_downgrade(meta, stack, list)
end end
function technic.send_items(pos, x_velocity, z_velocity, output_name) function technic.send_items(pos, x_velocity, z_velocity, output_name, item_count)
-- Send items on their way in the pipe system. -- Send items on their way in the pipe system.
if output_name == nil then if output_name == nil then
output_name = "dst" output_name = "dst"
@ -74,9 +74,14 @@ function technic.send_items(pos, x_velocity, z_velocity, output_name)
if stack then if stack then
local item0 = stack:to_table() local item0 = stack:to_table()
if item0 then if item0 then
item0["count"] = "1" -- Calculate how many items to move. Don't move more items than exist.
-- Basically, 'item_count' is the maximum number of items to move.
local num_items = item_count or 1
if stack:get_count() < num_items then num_items = stack:get_count() end
item0["count"] = tostring(num_items)
technic.tube_inject_item(pos, pos, vector.new(x_velocity, 0, z_velocity), item0) technic.tube_inject_item(pos, pos, vector.new(x_velocity, 0, z_velocity), item0)
stack:take_item(1) stack:take_item(num_items)
inv:set_stack(output_name, i, stack) inv:set_stack(output_name, i, stack)
return return
end end
@ -84,7 +89,6 @@ function technic.send_items(pos, x_velocity, z_velocity, output_name)
end end
end end
function technic.smelt_item(meta, result, speed) function technic.smelt_item(meta, result, speed)
local inv = meta:get_inventory() local inv = meta:get_inventory()
meta:set_int("cook_time", meta:get_int("cook_time") + 1) meta:set_int("cook_time", meta:get_int("cook_time") + 1)
@ -105,7 +109,7 @@ function technic.smelt_item(meta, result, speed)
end end
end end
function technic.handle_machine_pipeworks(pos, tube_upgrade, send_function) function technic.handle_machine_pipeworks(pos, tube_upgrade, send_function, items_processed_this_cycle)
if send_function == nil then if send_function == nil then
send_function = technic.send_items send_function = technic.send_items
end end
@ -117,7 +121,6 @@ function technic.handle_machine_pipeworks(pos, tube_upgrade, send_function)
local x_velocity = 0 local x_velocity = 0
local z_velocity = 0 local z_velocity = 0
-- Output is on the left side of the furnace
if node.param2 == 3 then pos1.z = pos1.z - 1 z_velocity = -1 end if node.param2 == 3 then pos1.z = pos1.z - 1 z_velocity = -1 end
if node.param2 == 2 then pos1.x = pos1.x - 1 x_velocity = -1 end if node.param2 == 2 then pos1.x = pos1.x - 1 x_velocity = -1 end
if node.param2 == 1 then pos1.z = pos1.z + 1 z_velocity = 1 end if node.param2 == 1 then pos1.z = pos1.z + 1 z_velocity = 1 end
@ -128,13 +131,43 @@ function technic.handle_machine_pipeworks(pos, tube_upgrade, send_function)
if minetest.get_item_group(node1.name, "tubedevice") > 0 then if minetest.get_item_group(node1.name, "tubedevice") > 0 then
output_tube_connected = true output_tube_connected = true
end end
local tube_time = meta:get_int("tube_time") + tube_upgrade
if tube_time >= 2 then items_processed_this_cycle = items_processed_this_cycle or 1
local eject_count = meta:get_int("eject_count") + items_processed_this_cycle
meta:set_int("eject_count", eject_count)
local tube_upgrade_max = 2
local norm_tube_upgrade = tube_upgrade / tube_upgrade_max
local stack_size = 11
local tube_time = meta:get_int("tube_time") + 1
if tube_time >= 10 then
tube_time = 0 tube_time = 0
if output_tube_connected then if output_tube_connected then
send_function(pos, x_velocity, z_velocity) -- Calculate how many items to eject during this run. Eject none if no
-- tube upgrades exist, and eject as many as were produced if the max
-- number of tube upgrades exist.
-- Always try to eject at least 5 to 10 items per execution. This is the
-- fallback behavior for when the machine isn't producing anything (and
-- thus 'eject_count' is 0).
if eject_count < 10 then eject_count = 10 end
eject_count = eject_count * norm_tube_upgrade
if eject_count > 0 then
-- Eject items. Use multiple stacks if needed, to avoid ejecting a
-- monolithic stack.
while eject_count >= stack_size do
send_function(pos, x_velocity, z_velocity, nil, stack_size)
eject_count = eject_count - stack_size
end
send_function(pos, x_velocity, z_velocity, nil, eject_count)
end end
end end
meta:set_int("eject_count", 0)
end
meta:set_int("tube_time", tube_time) meta:set_int("tube_time", tube_time)
end end

View File

@ -78,22 +78,22 @@ function technic.register_base_machine(data)
if data.upgrade then if data.upgrade then
EU_upgrade, tube_upgrade = technic.handle_machine_upgrades(meta) EU_upgrade, tube_upgrade = technic.handle_machine_upgrades(meta)
end end
if data.tube then
technic.handle_machine_pipeworks(pos, tube_upgrade)
end
local powered = eu_input >= machine_demand[EU_upgrade+1] local powered = eu_input >= machine_demand[EU_upgrade+1]
if powered then if powered then
meta:set_int("src_time", meta:get_int("src_time") + round(data.speed*10)) meta:set_int("src_time", meta:get_int("src_time") + round(data.speed*10))
end end
while true do
local items_processed_this_cycle = 0
while true do -- Begin recipe processing.
local result = technic.get_recipe(typename, inv:get_list("src")) local result = technic.get_recipe(typename, inv:get_list("src"))
if not result then if not result then
technic.swap_node(pos, machine_node) technic.swap_node(pos, machine_node)
meta:set_string("infotext", S("%s Idle"):format(machine_desc_tier)) meta:set_string("infotext", S("%s Idle"):format(machine_desc_tier))
meta:set_int(tier.."_EU_demand", 0) meta:set_int(tier.."_EU_demand", 0)
meta:set_int("src_time", 0) meta:set_int("src_time", 0)
return break
end end
meta:set_int(tier.."_EU_demand", machine_demand[EU_upgrade+1]) meta:set_int(tier.."_EU_demand", machine_demand[EU_upgrade+1])
technic.swap_node(pos, machine_node.."_active") technic.swap_node(pos, machine_node.."_active")
@ -103,7 +103,7 @@ function technic.register_base_machine(data)
technic.swap_node(pos, machine_node) technic.swap_node(pos, machine_node)
meta:set_string("infotext", S("%s Unpowered"):format(machine_desc_tier)) meta:set_string("infotext", S("%s Unpowered"):format(machine_desc_tier))
end end
return break
end end
local output = result.output local output = result.output
if type(output) ~= "table" then output = { output } end if type(output) ~= "table" then output = { output } end
@ -120,14 +120,19 @@ function technic.register_base_machine(data)
break break
end end
inv:add_item("dst_tmp", o) inv:add_item("dst_tmp", o)
items_processed_this_cycle = items_processed_this_cycle + o:get_count()
end end
if not room_for_output then if not room_for_output then
meta:set_int("src_time", round(result.time*10)) meta:set_int("src_time", round(result.time*10))
return break
end end
meta:set_int("src_time", meta:get_int("src_time") - round(result.time*10)) meta:set_int("src_time", meta:get_int("src_time") - round(result.time*10))
inv:set_list("src", result.new_input) inv:set_list("src", result.new_input)
inv:set_list("dst", inv:get_list("dst_tmp")) inv:set_list("dst", inv:get_list("dst_tmp"))
end -- End of recipe processing.
if data.tube then
technic.handle_machine_pipeworks(pos, tube_upgrade, nil, items_processed_this_cycle)
end end
end end