Skip to content

Commit

Permalink
Hack inserter filters into working enough for 2.0 base
Browse files Browse the repository at this point in the history
But don't do as good a job as we could, because all of this needs to be redone so we can handle quality
  • Loading branch information
ahicks92 committed Dec 7, 2024
1 parent abe5691 commit addbbc9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 30 deletions.
23 changes: 13 additions & 10 deletions control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ local fa_kk = require("scripts.kruise-kontrol-wrapper")
local fa_quickbar = require("scripts.quickbar")
local BeltAnalyzer = require("scripts.ui.belt-analyzer")
local FaCommands = require("scripts.fa-commands")
local Filters = require("scripts.filters")
local Consts = require("scripts.consts")
local Research = require("scripts.research")
local Rulers = require("scripts.rulers")
Expand Down Expand Up @@ -298,7 +299,7 @@ function read_inventory_slot(pindex, start_phrase_in, inv_in)
--Label it as an empty slot
result = result .. "Empty Slot"
--Check if the empty slot has a filter set
local filter_name = p.get_main_inventory().get_filter(index)
local filter_name = Filters.get_filter_prototype(p.get_main_inventory(), index)
if filter_name ~= nil then
result = result .. " filtered for " .. filter_name --laterdo localise this name
end
Expand All @@ -311,7 +312,7 @@ function read_inventory_slot(pindex, start_phrase_in, inv_in)
printout(fa_blueprints.get_blueprint_book_info(stack, false), pindex)
elseif stack.valid_for_read then
--Check if the slot is filtered
local filter_name = p.get_main_inventory().get_filter(index)
local filter_name = Filters.get_filter_prototype(p.get_main_inventory(), index)
if filter_name ~= nil then result = result .. " filtered " end
--Check if the stack has damage
if stack.health < 1 then result = result .. " damaged " end
Expand Down Expand Up @@ -4383,7 +4384,7 @@ script.on_event("click-menu", function(event)
players[pindex].item_cache[players[pindex].item_selector.index].name
)
sectors_i.inventory[players[pindex].building.index] =
players[pindex].building.ent.get_filter(players[pindex].building.index)
Filters.get_filter_prototype(players[pindex].building.ent, players[pindex].building.index)
printout("Filter set.", pindex)
players[pindex].building.item_selection = false
players[pindex].item_selection = false
Expand Down Expand Up @@ -7182,7 +7183,7 @@ function set_selected_inventory_slot_filter(pindex)
index = index or 1
--Act according to the situation defined by the filter slot, slot item, and hand item.
local menu = players[pindex].menu
local filter = inv.get_filter(index)
local filter = Filters.get_filter_prototype(inv, index)
local slot_item = inv[index]
local hand_item = p.cursor_stack

Expand Down Expand Up @@ -7228,19 +7229,21 @@ end
--Returns the currently selected entity inventory based on the current mod menu and mod sector.
function get_selected_inventory_and_slot(pindex)
local p = game.get_player(pindex)
local c = p.character
if not c then return nil end
local inv = nil
local index = nil
local menu = players[pindex].menu
if menu == "inventory" then
inv = p.get_main_inventory()
inv = c.get_main_inventory()
index = players[pindex].inventory.index
elseif menu == "player_trash" then
inv = p.get_inventory(defines.inventory.character_trash)
inv = c.get_inventory(defines.inventory.character_trash)
index = players[pindex].inventory.index
elseif menu == "building" or menu == "vehicle" then
local sector_name = players[pindex].building.sector_name
if sector_name == "player inventory from building" then
inv = p.get_main_inventory()
inv = c.get_main_inventory()
index = players[pindex].inventory.index
else
inv = players[pindex].building.sectors[players[pindex].building.sector].inventory
Expand Down Expand Up @@ -7660,7 +7663,7 @@ function set_inserter_filter_by_hand(pindex, ent)
if stack == nil or stack.valid_for_read == false then
--Delete last filter
for i = ent.filter_slot_count, 1, -1 do
local filt = ent.get_filter(i)
local filt = Filters.get_filter_prototype(ent, i)
if filt ~= nil then
ent.set_filter(i, nil)
return "Last filter cleared"
Expand All @@ -7670,10 +7673,10 @@ function set_inserter_filter_by_hand(pindex, ent)
else
--Add item in hand as next filter
for i = 1, ent.filter_slot_count, 1 do
local filt = ent.get_filter(i)
local filt = Filters.get_filter_prototype(ent, i)
if filt == nil then
ent.set_filter(i, stack.name)
if ent.get_filter(i) == stack.name then
if Filters.get_filter_prototype(ent, i) == stack.name then
return "Added filter"
else
return "Filter setting failed"
Expand Down
3 changes: 2 additions & 1 deletion scripts/building-vehicle-sectors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local localising = require("scripts.localising")
local fa_belts = require("scripts.transport-belts")
local fa_blueprints = require("scripts.blueprints")
local BeltAnalyzer = require("scripts.ui.belt-analyzer")
local Filters = require("scripts.filters")

local mod = {}

Expand Down Expand Up @@ -166,7 +167,7 @@ function mod.open_operable_building(ent, pindex)
})
--Add inserter filter info
for i = 1, ent.filter_slot_count do
local filter = ent.get_filter(i)
local filter = Filters.get_filter_prototype(ent, i)
if filter == nil then filter = "No filter selected." end
table.insert(players[pindex].building.sectors[#players[pindex].building.sectors].inventory, filter)
end
Expand Down
36 changes: 17 additions & 19 deletions scripts/fa-info.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ won't change our mind later or maybe even go as far as adding settings for this
stuff.
]]
local dirs = defines.direction
local Filters = require("scripts.filters")
local util = require("util")

local F = require("scripts.field-ref")
Expand Down Expand Up @@ -971,6 +972,21 @@ local function ent_info_belt_contents(ctx)
if not found_items then ctx.message:fragment({ "fa.ent-info-transport-belt-empty" }) end
end

---@param ctx fa.Info.EntInfoContext
local function ent_info_filters(ctx)
local filts = Filters.get_all_filters(ctx.ent)
if next(filts) then
local first = true

for _, name in pairs(filts) do
ctx.message:list_item()
if first then ctx.message:fragment("Filters for") end
first = false
ctx.message:fragment(Localising.get_localised_name_with_fallback(prototypes.item[name]))
end
end
end

--Outputs basic entity info, usually called when the cursor selects an entity.
---@param ent LuaEntity
---@return LocalisedString
Expand Down Expand Up @@ -1063,28 +1079,10 @@ function mod.ent_info(pindex, ent, is_scanner)
ctx.message:fragment(BotLogistics.roboport_contents_info(ent))
end
run_handler(ent_info_spidertron)
run_handler(ent_info_filters)

--Inserters: Explain held items, pickup and drop positions
if ent.type == "inserter" then
--Declare filters
if ent.filter_slot_count > 0 then
ctx.message:fragment("Filters for")
local active_filter_count = 0
for i = 1, ent.filter_slot_count, 1 do
local filt = ent.get_filter(i)
if filt ~= nil then
active_filter_count = active_filter_count + 1
if active_filter_count > 1 then filter_result = filter_result .. " and " end
local local_name = Localising.get(prototypes.item[filt.name], pindex)
if local_name == nil then local_name = tostring(filt.name) or " unknown item " end
filter_result = filter_result .. local_name
end
end
if active_filter_count > 0 then
ctx.message:fragment(filter_result)
run_handler(",")
end
end
--Read held item
if ent.held_stack ~= nil and ent.held_stack.valid_for_read and ent.held_stack.valid then
ctx.message:fragment(", holding")
Expand Down
4 changes: 4 additions & 0 deletions scripts/table-helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,8 @@ function mod.nqc_to_sorted_descending(tab)
return ret
end

function mod.insert_if_notnill(tab, what)
if what then table.insert(tab, what) end
end

return mod

0 comments on commit addbbc9

Please sign in to comment.