Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
bengardner committed Jan 28, 2024
1 parent 57156f7 commit 12bb7d0
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/GlobalState.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,8 @@ function M.get_fuel_table()
for _, prot in pairs(game.item_prototypes) do
local fc = prot.fuel_category
if fc ~= nil then
table.insert(fuels, { name=prot.name, val=prot.stack_size * prot.fuel_value, cat=fc })
--table.insert(fuels, { name=prot.name, val=prot.stack_size * prot.fuel_value, cat=fc })
table.insert(fuels, { name=prot.name, val=prot.fuel_value, cat=fc })
end
end
table.sort(fuels, function (a, b) return a.val > b.val end)
Expand Down
2 changes: 1 addition & 1 deletion src/NetworkTankAutoConfig.lua
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ function M.auto_config(entity)
local config = {
type = "take",
fluid = name,
buffer = constants.MAX_TANK_SIZE,
buffer = constants.DEFAULT_TANK_REQUEST,
limit = 0,
no_limit = true,
temperature = fluid_proto.default_temperature,
Expand Down
2 changes: 1 addition & 1 deletion src/NetworkTankGui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ function M.set_default_buffer_and_limit(self)
limit = Constants.MAX_TANK_SIZE
end
M.set_temperature(self, game.fluid_prototypes[fluid].default_temperature)
M.set_buffer(self, Constants.MAX_TANK_SIZE)
M.set_buffer(self, Constants.DEFAULT_TANK_REQUEST)
M.set_limit(self, limit)
end
end
Expand Down
56 changes: 54 additions & 2 deletions src/NetworkViewUi_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ local function update_player_selected(player)
if info.service_tick_delta ~= nil then
desc_flow.add {
type="label",
caption = string.format("Service period: %.0f seconds", info.service_tick_delta / 60),
caption = string.format("Service period: %.2f seconds", info.service_tick_delta / 60),
ignored_by_interaction = true,
}
end
Expand All @@ -575,23 +575,75 @@ local function update_player_selected(player)
type = "sprite-button",
sprite = "item/" .. info.ore_name,
}
end
end
if info.requests ~= nil then
--[[
local take_hflow = desc_flow.add {
type="flow",
ignored_by_interaction = true,
}
]]
local take_hflow = desc_flow.add {
type="table",
style="compact_slot_table",
column_count = 7,
ignored_by_interaction = true,
}
for _, r in ipairs(info.requests) do
if r.type == "take" then
take_hflow.add {
type = "sprite-button",
style="transparent_slot",
sprite = "item/" .. r.item,
number = r.buffer or 0,
}
end
end
local inv = ent.get_output_inventory()
local is_locked = (inv.get_bar() < #inv)
desc_flow.add {
type="label",
caption = string.format("Locked: %s", is_locked),
ignored_by_interaction = true,
}
end
--string.format("[%s] %s", ent.unit_number, ent.localised_name)
--print(string.format("name=%s type=%s", ent.name, ent.type))
if ent.type == "assembling-machine" then
print(string.format("name=%s type=%s is_crafting=%s s=%s p=%s",
ent.name, ent.type,
ent.is_crafting(),
ent.crafting_speed,
ent.crafting_progress * 100))
local r = ent.get_recipe()
if r ~= nil then
local rtime = r.energy / ent.crafting_speed -- time to finish one recipe
local info = GlobalState.entity_info_get(ent.unit_number)
local svc_ticks = 60 * 60 -- assume 60 seconds
if info ~= nil and info.service_tick_delta ~= nil then
svc_ticks = info.service_tick_delta
end
local rp = game.recipe_prototypes[r.name]
if rp ~= nil then
print(string.format(" overload_multiplier=%s", rp.overload_multiplier))
end
--[[
calculate the amount of ingredients that should be in the input.
Assume we service once every 60 seconds.
If it takes 5 units for one recipe and the recipe takes 10 seconds, then we can finish
60/10 = 6 per minute. That means we need 6*5=30 items in the input. Anything more is a waste.
Can be basd on the service time. Minimum is the amount reuired by the recipe.
]]
local mult = svc_ticks / (rtime * 60)
local rmult = math.ceil(mult)
print(string.format(" energy=%s time=%s svc=%s mult=%s %s", r.energy, rtime, svc_ticks, mult, rmult))
local needed = {} -- key=item, val=amount
for _, ing in ipairs(r.ingredients) do
needed[ing.name] = math.floor(math.max(ing.amount, ing.amount * rmult))
end
print(serpent.line(needed))
end
end
end

local function on_selected_entity_changed(event)
Expand Down
36 changes: 26 additions & 10 deletions src/ServiceEntity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,36 @@ function M.refuel_entity(entity)
end
end

----
local function service_recipe_inv(entity, inv, recipe, factor)
if recipe ~= nil and inv ~= nil then

local rtime = recipe.energy / entity.crafting_speed -- time to finish one recipe

local svc_ticks = 60 * 60 -- assume 60 seconds if no info
local info = GlobalState.entity_info_get(entity.unit_number)
if info ~= nil and info.service_tick_delta ~= nil then
svc_ticks = info.service_tick_delta
end
-- calculate the recipe multiplier
local mult = math.ceil(svc_ticks / (rtime * 60))

local needed = {} -- key=item, val=amount
for _, ing in ipairs(recipe.ingredients) do
needed[ing.name] = math.floor(math.max(ing.amount, ing.amount * mult))
end

local contents = inv.get_contents()
for _, ing in pairs(recipe.ingredients) do
local prot = game.item_prototypes[ing.name]
--print(string.format("recipe: %s [%s] need=%s have=%s", entity.name, entity.unit_number, serpent.line(needed), serpent.line(contents)))

for name, amount in pairs(needed) do
-- only handle items here
local prot = game.item_prototypes[name]
if prot ~= nil then
local n_have = contents[ing.name] or 0
local n_need = math.max(ing.amount, math.max(ing.amount * factor, prot.stack_size))
if n_have < n_need then
transfer_item_to_inv(entity, inv, ing.name, n_need - n_have)
n_have = inv.get_item_count(ing.name)
if n_have < ing.amount then
GlobalState.missing_item_set(ing.name, entity.unit_number, ing.amount - n_have)
end
local n_have = contents[name] or 0
local n_want = math.min(inv.get_insertable_count(name), amount)
if n_have < n_want then
transfer_item_to_inv(entity, inv, name, n_want - n_have)
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions src/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ M.NUM_INVENTORY_SLOTS = 48
M.TANK_AREA = 1000
M.TANK_HEIGHT = 1
M.MAX_TANK_SIZE = M.TANK_AREA * M.TANK_HEIGHT * 100
M.DEFAULT_TANK_REQUEST = 1000

M.ALERT_TRANSFER_TICKS = 10 * 60
M.MAX_MISSING_TICKS = 30 * 60
Expand All @@ -23,4 +24,9 @@ M.NETWORK_TANK_NAMES = {
["network-tank-provider"] = { gui=false, type="input", base_level=-0.5 }, -- from pipes to network
}

M.NETWORK_CHEST_NAMES = {
["network-chest"] = true,
["network-chest-provider"] = false,
}

return M
7 changes: 5 additions & 2 deletions src/prototypes/network-chests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,11 @@ end

function M.main()
-- FIXME: these should be the logistic chests with inv size of 19
add_chest("network-chest", true)
add_chest("network-chest-provider", false)
for k, v in pairs(constants.NETWORK_CHEST_NAMES) do
add_chest(k, v)
end
--add_chest("network-chest", true)
--add_chest("network-chest-provider", false)
--add_chest("network-chest-requester") -- currently useless
end

Expand Down

0 comments on commit 12bb7d0

Please sign in to comment.