Skip to content

Commit

Permalink
filters: it helps to actually commit the new file
Browse files Browse the repository at this point in the history
  • Loading branch information
ahicks92 committed Dec 7, 2024
1 parent c39388a commit a15f8e5
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions scripts/filters.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
--[[
Item and inventory filters.
Right now this file just contains a shim to get filters off stuff as a string so
that we can shove in calls for the 1.1-ish parts of the API to get things off
the ground, and some other utilities to hack things into working. This is worth
a module because it will later need to deal with quality, and also localisation
of filters.
]]
local TH = require("scripts.table-helpers")

local mod = {}

---@alias fa.Filters.Filterlike LuaEntity | LuaInventory

---@param filterable fa.Filters.Filterlike
---@param index number
---@return string?
function mod.get_filter_prototype(filterable, index)
print(serpent.line(filterable))
local filter = filterable.get_filter(index)
if filter then
return filter.name --[[@as string]]
end
return nil
end

-- cannot be used on inventories, because in inventories the filter indices are
-- inventory slot indices, not a list of filters.
---@param ent LuaEntity
---@return string[]
function mod.get_all_filters(ent)
local ret = {}

for i = 1, ent.filter_slot_count do
TH.insert_if_notnill(ret, mod.get_filter_prototype(ent, i))
end

return ret
end

---@param filterable fa.Filters.Filterlike
---@param index number
---@param filter ItemFilter?
function mod.set_filter(filterable, index, filter)
filterable.set_filter(index, filter)
if filterable.object_name ~= "LuaEntity" then return end

-- For entities we have to work out when to set use_filters.

-- Easy case: it is using a filter because this just set one.
if filter then
filterable.use_filters = true
return
end

-- Otherwise check all of them; if we find one turn it on; if we don't turn
-- it off.
for i = 1, filterable.filter_slot_count do
if filterable.get_filter(i) then
filterable.use_filters = true
return
end
end

filterable.use_filters = false
end

return mod

0 comments on commit a15f8e5

Please sign in to comment.