-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathlib.lua
169 lines (143 loc) · 3.72 KB
/
lib.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
local Public = {}
function Public.merge(old, new)
old = util.table.deepcopy(old)
for k, v in pairs(new) do
if v == "nil" then
old[k] = nil
else
old[k] = v
end
end
return old
end
Public.find = function(tbl, f, ...)
if type(f) == "function" then
for k, v in pairs(tbl) do
if f(v, k, ...) then
return v, k
end
end
else
for k, v in pairs(tbl) do
if v == f then
return v, k
end
end
end
return nil
end
function Public.restrict_surface_conditions(recipe_or_entity, conditions)
conditions = util.table.deepcopy(conditions)
if conditions.property then
conditions = { conditions }
end
local surface_conditions = recipe_or_entity.surface_conditions
and util.table.deepcopy(recipe_or_entity.surface_conditions)
or {}
for _, condition in pairs(conditions) do
for i = 1, #surface_conditions do
local existing = surface_conditions[i]
if existing.property == condition.property then
if condition.min ~= nil then
if existing.min ~= nil then
existing.min = math.max(existing.min, condition.min)
else
existing.min = condition.min
end
end
if condition.max ~= nil then
if existing.max ~= nil then
existing.max = math.min(existing.max, condition.max)
else
existing.max = condition.max
end
end
goto continue
end
end
-- No existing condition found, add new one
table.insert(surface_conditions, {
property = condition.property,
min = condition.min,
max = condition.max,
})
::continue::
end
recipe_or_entity.surface_conditions = surface_conditions
end
function Public.relax_surface_conditions(recipe_or_entity, conditions)
conditions = util.table.deepcopy(conditions)
if conditions.property then
conditions = { conditions }
end
local surface_conditions = recipe_or_entity.surface_conditions
and util.table.deepcopy(recipe_or_entity.surface_conditions)
or {}
for _, condition in pairs(conditions) do
for i = 1, #surface_conditions do
local existing_condition = surface_conditions[i]
if existing_condition.property == condition.property then
if condition.min ~= nil and existing_condition.min ~= nil then
existing_condition.min = math.min(existing_condition.min, condition.min)
end
if condition.max ~= nil and existing_condition.max ~= nil then
existing_condition.max = math.max(existing_condition.max, condition.max)
end
end
end
end
recipe_or_entity.surface_conditions = surface_conditions
end
function Public.cerys_research_unit(count)
return {
count = count,
ingredients = {
{ "automation-science-pack", 1 },
{ "logistic-science-pack", 1 },
{ "cerys-science-pack", 1 },
},
time = 60,
}
end
function Public.cerys_tech(args)
local science_count = args.science_count or 500
local ret = Public.merge({
type = "technology",
icon = "__space-age__/graphics/technology/research-productivity.png",
icon_size = 256,
}, args)
if not args.research_trigger and not args.unit then
ret.unit = Public.cerys_research_unit(science_count)
end
return ret
end
local version_pattern = "%d+"
local version_format = "%02d"
function Public.format_version(version, format)
if version then
format = format or version_format
local tbl = {}
for v in string.gmatch(version, version_pattern) do
tbl[#tbl + 1] = string.format(format, v)
end
if next(tbl) then
return table.concat(tbl, ".")
end
end
return nil
end
function Public.is_newer_version(old_version, current_version, format)
if current_version and not old_version then
return true
end
local v1 = Public.format_version(old_version, format)
local v2 = Public.format_version(current_version, format)
if v1 and v2 then
if v2 > v1 then
return true
end
return false
end
return nil
end
return Public