forked from ReikaKalseki/DragonIndustries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tech.lua
196 lines (182 loc) · 6.06 KB
/
tech.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
require "arrays"
require "mathhelper"
require "strings"
--[[
local CAMPAIGN_ONLY = {
"basic-mining",
"basic-electronics",
"basic-mapping",
"electric-inserter",
"basic-logistics",
"analyse-ship",
"basic-optics",
"basic-military",
"electric-mining",
"active-defense",
"repair-tech",
"passive-defense",
"improved-equipment",
"demo-science-pack",
"demo-logistics",
"demo-productivity-1",
"demo-shooting-speed-1",
}
--]]
function techHasDependencyRecursive(tech, dep, recurse, depth, path, root)
if not root then root = tech.name end
if not depth then depth = 0 end
if not path then path = {} end
if listHasValue(path, tech.name) then log("Found a tech recursion loop while checking " .. root .. " for " .. dep .. ", entering " .. tech.name .. ": " .. serpent.block(path)) return true end
table.insert(path, tech.name)
--if not recurse then log("Checking recursive deps of " .. tech.name .. " for " .. dep .. "; has [" .. serpent.block(tech.prerequisites)) end
--log("path is " .. serpent.block(path))
if not tech.prerequisites then return false end
for _,pre in pairs(tech.prerequisites) do
--log(depth .. " calls deep; Checking dep " .. pre .. " for " .. tech.name)
if pre == dep then
return true
end
if techHasDependencyRecursive(data.raw.technology[pre], dep, true, depth+1, path, root) then
return true
end
end
table.remove(path)
return false
end
function techUsesPack(tech, pack)
for _,ing in pairs(tech.unit.ingredients) do
if ing[1] == pack then
return true
end
end
return false
end
function addTechUnlock(tech, recipe)
if type(tech) == "string" then
tech = data.raw.technology[tech]
end
if not tech then error(serpent.block("No such technology found! " .. debug.traceback())) end
if not tech.effects then tech.effects = {} end
table.insert(tech.effects, {type = "unlock-recipe", recipe = recipe})
end
function removeTechUnlock(tech, recipe)
if type(tech) == "string" then
tech = data.raw.technology[tech]
end
if not tech then error(serpent.block("No such technology found! " .. debug.traceback())) end
if not tech.effects then return end
for i,eff in ipairs(tech.effects) do
if eff.type == "unlock-recipe" and eff.recipe == recipe then
table.remove(tech.effects, i)
break
end
end
end
--THIS IS NO LONGER A THING AS OF 0.18'S REVERSION TO THE CAMPAIGN
function isCampaignOnlyTech(tech)
--[[
if type(tech) == "string" then
tech = data.raw.technology[tech]
end
if not tech then error(serpent.block("No such technology found! " .. debug.traceback())) end
return not tech.enabled--listHasValue(CAMPAIGN_ONLY, tech.name)
--]]
return false
end
function getPrereqTechForPack(pack)
local tech = data.raw.technology[pack]
if pack == "automation-science-pack" and mods["EarlyExtensions"] then
--tech = data.raw.technology["basic-science"] --EE compat
return "basic-science"
end
return tech and tech.name or nil
end
function addPrereqToTech(tech, prereq)
if type(tech) == "string" then tech = data.raw.technology[tech] end
--if not tech then error("Tech does not exist!") end
if not tech then return end
if not tech.prerequisites then tech.prerequisites = {} end
table.insert(tech.prerequisites, prereq)
end
function splitTech(tech, prereqs, recipesToMove)
local base = data.raw.technology[tech]
local tech2 = table.deepcopy(base)
local a, b = string.find(tech, "-", 1, true)
local number = b and tonumber(string.sub(tech, b+1)) or nil
--error("Number " .. number .. " from " .. tech)
tech2.name = number and (tech .. "-" .. (number+1)) or (tech .. "-2")
log("Split " .. tech2.name .. " from " .. tech)
tech2.prerequisites = prereqs
table.insert(prereqs, tech)
tech2.effects = {}
for _,recipe in pairs(recipesToMove) do
table.insert(tech2.effects, {type = "unlock-recipe", recipe = recipe})
end
--for k,v in pairs(recipesToMove) do log(v) end
local keep = {}
for _,effect in pairs(base.effects) do
--log("Checking if list has " .. effect.recipe)
if effect.type == "unlock-recipe" and listHasValue(recipesToMove, effect.recipe) then
else
table.insert(keep, effect)
end
end
base.effects = keep
data:extend({tech2})
end
function removeSciencePackFromTech(techname, pack)
local tech = data.raw.technology[techname]
if not tech then return end
removeEntryFromListIf(tech.unit.ingredients, function(entry) return entry[1] == pack end)
local prereq = getPrereqTechForPack(pack)
if prereq and data.raw.technology[prereq] then
removeEntryFromList(tech.prerequisites, prereq)
end
log("Removed science pack " .. pack .. " from tech " .. tech.name)
end
function addSciencePackToTech(techname, pack)
local tech = data.raw.technology[techname]
if not tech then return end
if techUsesPack(tech, pack) then return end
local prereq = getPrereqTechForPack(pack)
if prereq and data.raw.technology[prereq] and not listHasValue(tech.prerequisites, prereq) then
table.insert(tech.prerequisites, prereq)
end
table.insert(tech.unit.ingredients, {pack, 1})
log("Added science pack " .. pack .. " to tech " .. tech.name)
end
function replaceTechPrereq(tech, old, new)
if type(tech) == "string" then tech = data.raw.technology[tech] end
if not tech then error(serpent.block("No such technology found! " .. debug.traceback())) end
local repl = {}
local flag = false
for _,prereq in pairs (tech.prerequisites) do
if prereq == old then
table.insert(repl, new)
flag = true
else
table.insert(repl, prereq)
end
end
tech.prerequisites = repl
log("Replaced prerequisite " .. old .. " with " .. new .. " in tech '" .. tech.name .. "'")
return flag
end
function replaceTechPack(tech, old, new, factor)
local f = factor and factor or 1
if type(tech) == "string" then tech = data.raw.technology[tech] end
if not tech then error(serpent.block("No such technology found! " .. debug.traceback())) end
local repl = {}
local flag = false
for _,pack in pairs (tech.unit.ingredients) do
if pack[1] == old then
table.insert(repl, {new, math.floor(pack[2]*f)})
flag = true
else
table.insert(repl, pack)
end
end
tech.unit.ingredients = repl
log("Replaced science pack " .. old .. " with " .. new .. " in tech '" .. tech.name .. "'")
return flag
end