z_mirror/RecipeBook

git clone https://git.raiguard.me/z_mirror/RecipeBook

Caleb HeuerDisable "show hidden" button by default04c2d70

trunk
3.2 KiB116 linesraw
1local grouped = require("scripts.database.grouped")
2local util = require("scripts.util")
3
4--- Each top-level prototype sorted into groups and subgroups for the search panel.
5--- @class SearchTree
6--- @field groups table<string, table<string, GenericPrototype[]>> Group name -> subgroup name -> members
7--- @field order table<SpritePath, integer>
8
9--- @alias GenericPrototype LuaEntityPrototype|LuaEquipmentPrototype|LuaFluidPrototype|LuaItemPrototype|LuaRecipePrototype|LuaTechnologyPrototype|LuaTilePrototype
10
11--- @param grouping GroupingMode
12--- @param materials_only boolean?
13--- @return SearchTree
14local function build_tree(grouping, materials_only)
15  --- @type SearchTree
16  local self = {
17    groups = {},
18    order = {},
19  }
20
21  --- @param prototype GenericPrototype
22  local function add(prototype)
23    if not util.should_include(prototype) then
24      return
25    end
26    if prototype.factoriopedia_alternative then
27      return
28    end
29    if prototype.parameter and prototype.object_name ~= "LuaEntityPrototype" then
30      return
31    end
32
33    local subgroup = self.groups[util.get_group(prototype).name][util.get_subgroup(prototype).name]
34    assert(subgroup, "Subgroup was nil.")
35    subgroup[#subgroup + 1] = prototype
36  end
37
38  for group_name, group_prototype in pairs(prototypes.item_group) do
39    local subgroups = {}
40    for _, subgroup_prototype in pairs(group_prototype.subgroups) do
41      subgroups[subgroup_prototype.name] = {}
42    end
43    self.groups[group_name] = subgroups
44  end
45
46  for _, item in pairs(prototypes.item) do
47    add(item)
48  end
49  for _, fluid in pairs(prototypes.fluid) do
50    add(fluid)
51  end
52  if not materials_only then
53    for _, entity in pairs(prototypes.entity) do
54      if grouping == "none" or not grouped.material[util.get_path(entity)] then
55        add(entity)
56      end
57    end
58    for _, recipe in pairs(prototypes.recipe) do
59      if grouping ~= "all" or not grouped.material[util.get_path(recipe)] then
60        add(recipe)
61      end
62    end
63    for _, tile in pairs(prototypes.tile) do
64      if grouping == "none" or not grouped.material[util.get_path(tile)] then
65        add(tile)
66      end
67    end
68    -- Space location
69    -- Asteroid chunk
70    -- Ammo
71    -- Space Connection
72    -- Virtual signal
73    -- Surface
74  end
75
76  local order = 0
77  for group_name, group in pairs(self.groups) do
78    for subgroup_name, subgroup in pairs(group) do
79      if not next(subgroup) then
80        group[subgroup_name] = nil
81        goto continue
82      end
83
84      -- TODO: This doesn't match up with Factoriopedia
85      table.sort(subgroup, function(a, b)
86        local a_order, b_order = a.order, b.order
87        if a_order == b_order then
88          return a.name < b.name
89        end
90        return a_order < b_order
91      end)
92
93      for i = 1, #subgroup do
94        order = order + 1
95        self.order[util.get_path(subgroup[i])] = order
96      end
97
98      ::continue::
99    end
100    if not next(group) then
101      self.groups[group_name] = nil
102    end
103  end
104
105  return self
106end
107
108--- @class SearchTreeMod
109local search_tree = {}
110
111search_tree.all = build_tree("all")
112search_tree.separate_recipes = build_tree("exclude-recipes")
113search_tree.materials = build_tree("none", true)
114search_tree.plain = build_tree("none")
115
116return search_tree