science-extra-trigger-techs/data-updates.lua

changeset 19
adbc5c74f279
equal deleted inserted replaced
18:6088a99984dc 19:adbc5c74f279
1 ---@type table<string, int>
2 local min_science_requirements = {}
3
4 -- Extract tech tier from the name
5 -- e.g. "military-3" => 3
6 ---@param name string
7 local function tech_level(name)
8 local a, b = string.find(name, "-%d+$")
9 if a == nil
10 then
11 return nil
12 else
13 return tonumber(string.sub(name, a+1, b))
14 end
15 end
16
17 for _, technology in pairs(data.raw["technology"])
18 do
19 if technology.unit
20 then
21 local prerequisites = {}
22 for _, prereq in pairs(technology.prerequisites or {})
23 do
24 prerequisites[prereq] = 1
25 end
26 local unit_count = 50
27 if technology.unit.count
28 then
29 unit_count = technology.unit.count
30 elseif technology.unit.count_formula
31 then
32 local level = tech_level(technology.name) or 1
33 unit_count = math.max(1, helpers.evaluate_expression(technology.unit.count_formula, {l = level, L = level}))
34 end
35 for _, ing in pairs(technology.unit.ingredients or {})
36 do
37 if prerequisites[ing[1]]
38 then
39 local count = math.min(500, ing[2] * unit_count / 5)
40 if not min_science_requirements[ing[1]]
41 then
42 min_science_requirements[ing[1]] = count
43 else
44 min_science_requirements[ing[1]] = math.min(min_science_requirements[ing[1]], count)
45 end
46 end
47 end
48 end
49 end
50
51 for _, technology in pairs(data.raw["technology"])
52 do
53 local science_pack = nil
54 for _, effect in pairs(technology.effects or {})
55 do
56 if effect.type == "unlock-recipe" and min_science_requirements[effect.recipe]
57 then
58 science_pack = effect.recipe
59 break
60 end
61 end
62 if science_pack ~= nil
63 then
64 ---@type data.TechnologyPrototype
65 local trigger_tech = {
66 type = "technology",
67 name = technology.name.."-2",
68 research_trigger = {
69 type = "craft-item",
70 item = science_pack,
71 count = math.min(500, min_science_requirements[science_pack]),
72 },
73 icon = technology.icon,
74 icons = technology.icons,
75 icon_size = technology.icon_size,
76 prerequisites = {technology.name},
77 upgrade = true,
78 }
79 technology["_science_extra_trigger_technology"] = technology.name.."-2"
80 data:extend{trigger_tech}
81 end
82 end

mercurial