z_mirror/RecipeBook

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

Caleb HeuerAdd alternate mode for grouping to only split out recipesa65a064

trunk
1.4 KiB64 linesraw
1local grouped = require("scripts.database.grouped")
2local util = require("scripts.util")
3
4--- @class History: {[integer]: GenericPrototype, index: integer}
5local history = {}
6local mt = { __index = history }
7script.register_metatable("history", mt)
8
9function history.new()
10  return setmetatable({ index = 0 }, mt)
11end
12
13--- @param prototype GenericPrototype
14--- @param grouping_mode GroupingMode
15--- @return boolean
16function history:push(prototype, grouping_mode)
17  if grouping_mode ~= "none" then
18    if
19      (prototype.object_name == "LuaRecipePrototype" and grouping_mode == "all")
20      or (prototype.object_name ~= "LuaRecipePrototype" and grouping_mode ~= "none")
21    then
22      local base = grouped.material[util.get_path(prototype)]
23      if base then
24        prototype = base
25      end
26    end
27  end
28  if self:current() == prototype then
29    return false
30  end
31  for i = self.index + 1, #self do
32    self[i] = nil
33  end
34  for i = self.index, 1, -1 do
35    if self[i] == prototype then
36      table.remove(self, i)
37    end
38  end
39  self[#self + 1] = prototype
40  self.index = #self
41  return true
42end
43
44function history:prev()
45  self.index = math.max(1, self.index - 1)
46end
47
48function history:next()
49  self.index = math.min(#self, self.index + 1)
50end
51
52function history:current()
53  return self[self.index]
54end
55
56function history:at_front()
57  return self.index <= 1
58end
59
60function history:at_back()
61  return self.index == #self
62end
63
64return history