-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtechfuncs.lua
More file actions
170 lines (160 loc) · 4.38 KB
/
Copy pathtechfuncs.lua
File metadata and controls
170 lines (160 loc) · 4.38 KB
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
local techfuncs = {}
function techfuncs.addRecipeUnlock(tech, rec)
if not (data.raw.technology[tech] and data.raw.recipe[rec]) then
error("Trying to add recipe: " .. rec .. "(" .. serpent.line(data.raw.recipe[rec], {maxlength = 10}) .. ") to tech: " .. tech .. " (" .. serpent.line(data.raw.technology[tech], {maxlength = 10}) .. ")but one doesn't exist! ")
return
end
if data.raw.technology[tech].effects then
local found = false
for k, v in pairs(data.raw.technology[tech].effects) do
if v.recipe == rec then
found = true
break
end
end
if not found then
table.insert(data.raw.technology[tech].effects, {type = "unlock-recipe", recipe = rec})
end
else
data.raw.technology[tech].effects = {{type = "unlock-recipe", recipe = rec}}
end
end
function techfuncs.removeRecipeUnlock(tech, rec)
if not data.raw.technology[tech] then
return
end
if data.raw.technology[tech].effects then
local fi = -1
for k, v in pairs(data.raw.technology[tech].effects) do
if v.recipe == rec then
fi = k
break
end
end
if fi > 0 then
table.remove(data.raw.technology[tech].effects, fi)
end
end
end
function techfuncs.addPrereq(tech, prereq)
if not (data.raw.technology[tech] and data.raw.technology[prereq]) then
error("Trying to add tech: " .. prereq .. "(" .. serpent.line(data.raw.technology[prereq], {maxlength = 10}) .. ") to tech: " .. tech .. " (" .. serpent.line(data.raw.technology[tech], {maxlength = 10}) .. ")but one doesn't exist! ")
return
end
local pr = data.raw.technology[tech].prerequisites
if pr then
found = false
for k, v in pairs(pr) do
if v == prereq then
found = true
break
end
end
if not found then
table.insert(pr, prereq)
end
else
data.raw.technology[tech].prerequisites = {prereq}
end
end
function techfuncs.removePrereq(tech, prereq)
--don't check for prereq tech's existence - the reason we're removing the requirement might be we've deleted it already!
if not data.raw.technology[tech] then
return
end
local pr = data.raw.technology[tech].prerequisites
local fi = -1
if pr then
for k, v in pairs(pr) do
if v == prereq then
fi = k
break
end
end
if fi > 0 then
local techs = data.raw.technology
for k, v in pairs(techs) do
local preqs = data.raw.technology[k].prerequisites
if preqs then
local add = false
local exists = false
for _, preq in pairs(preqs) do
if preq == tech then
add = true
end
if preq == prereq then
exists = true
end
end
if add and not exists then
table.insert(preqs, prereq)
end
end
::continue::
end
table.remove(pr, fi)
end
end
end
function techfuncs.addSciencePackToTech(tech, item)
if tech then
if tech.unit then
if tech.unit.ingredients then
local found = false
for k, v in pairs(tech.unit.ingredients) do
if v[1] == item or v["name"] == item then
found = true
end
end
if not found then
table.insert(tech.unit.ingredients, {item, 1})
end
end
else
tech.unit.ingredients = {{item, 1}}
end
end
end
function techfuncs.addSciencePack(tech, item)
if not (data.raw.technology[tech] and data.raw.tool[item]) then
error("Trying to add science: " .. item .. "(" .. serpent.line(data.raw.tool[item], {maxlength = 10}) .. ") to tech: " .. tech .. " (" .. serpent.line(data.raw.technology[tech], {maxlength = 10}) .. ")but one doesn't exist! ")
return
end
local t = data.raw.technology[tech]
techfuncs.addSciencePackToTech(t, item)
end
function techfuncs.removeSciencePackFromDifficulty(tech, item)
if tech then
if tech.unit then
if tech.unit.ingredients then
local fi = -1
for k, v in pairs(tech.unit.ingredients) do
if v[1] == item or v["name"] == item then
fi = k
break
end
end
if fi > 0 then
table.remove(tech.unit.ingredients, fi)
end
end
end
end
end
function techfuncs.removeSciencePack(tech, item)
if not (data.raw.technology[tech] and data.raw.tool[item]) then
return
end
local t = data.raw.technology[tech]
techfuncs.removeSciencePackFromDifficulty(t, item)
end
function techfuncs.compilePrereqs(...)
local pr = {}
for k, v in pairs(...) do
if k and v then
table.insert(pr, v)
end
end
return pr
end
return techfuncs