-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.lua
296 lines (284 loc) · 13 KB
/
api.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
remote.add_interface("TSM-API",
{
list_priorities = function(surface)
if surface ~= nil then
if storage.newpriority[surface] ~= nil then
return storage.newpriority[surface]
else
return {}
end
else
if storage.newpriority ~= nil then
return storage.newpriority
else
return {}
end
end
end,
define_new_priority = function(icon1, icon2, stationlist, surface)
local resource = {}
local id = {}
resource.elem_type = "signal"
resource.name = icon1
id.elem_type = "signal"
id.name = icon2
-- if game.item_prototypes[icon1] ~= nil then
-- resource.type = "item"
-- elseif game.fluid_prototypes[icon1] ~= nil then
-- resource.type = "fluid"
-- elseif game.virtual_signal_prototypes[icon1] ~= nil then
-- resource.type = "virtual"
-- else
resource.type = check_icon(icon1)
if resource.type == "error" then
game.print("Remote priority add failed, icon " .. icon1 .. " invalid")
return
end
-- if game.item_prototypes[icon2] ~= nil then
-- id.type = "item"
-- elseif game.fluid_prototypes[icon2] ~= nil then
-- id.type = "fluid"
-- elseif game.virtual_signal_prototypes[icon2] ~= nil then
-- id.type = "virtual"
-- else
id.type = check_icon(icon2)
if id.type == "error" then
game.print("Remote priority add failed, icon " .. icon2 .. " invalid")
return
end
if surface == nil then
surface = "nauvis"
end
local wc = { rb_or = true, rb_and = false, inc_ef = true, empty = true, full = false, inactivity = true, inact_int = 5, wait_timer = false, wait_int = 30, count = false, count_amt = 1000, count_ddn = 1 }
storage.newpriority = storage.newpriority or {}
storage.newpriority[surface] = storage.newpriority[surface] or {}
storage.newpriority[surface][icon1] = storage.newpriority[surface][icon1] or {}
storage.newpriority[surface][icon1][icon2] = storage.newpriority[surface][icon1][icon2] or {}
storage.newpriority[surface][icon1][icon2].resource = resource
storage.newpriority[surface][icon1][icon2].id = id
storage.newpriority[surface][icon1][icon2].station = storage.newpriority[surface][icon1][icon2].station or {}
local i = 1
if stationlist ~= nil then
for _, station in pairs(stationlist) do
storage.newpriority[surface][icon1][icon2].station[i] = { "", station }
i = i + 1
end
end
storage.newpriority[surface][icon1][icon2].wc = wc
updaterequestedpublisher(icon1, icon2, surface)
end,
update_wc = function(icon1, icon2, wc, surface)
if check_icon(icon1) == "error" then
game.print("Remote update wait condition failed, icon " .. tostring(icon1) .. " invalid")
return
end
if check_icon(icon2) == "error" then
game.print("Remote update wait condition failed, icon " .. tostring(icon2) .. " invalid")
return
end
if surface == nil then
surface = "nauvis"
end
if check_priority(icon1, icon2, surface) == true then
if wc.rb_or ~= nil then
if wc.rb_or == true or wc.rb_or == false then
storage.newpriority[surface][icon1][icon2].wc.rb_or = wc.rb_or
storage.newpriority[surface][icon1][icon2].wc.rb_and = not (wc.rb_or)
else
game.print("rb_or requires boolean value, not " .. tostring(wc.rb_or))
return
end
end
if wc.rb_and ~= nil then
if wc.rb_and == true or wc.rb_and == false then
storage.newpriority[surface][icon1][icon2].wc.rb_or = not (wc.rb_and)
storage.newpriority[surface][icon1][icon2].wc.rb_and = wc.rb_and
else
game.print("rb_and requires boolean value, not " .. tostring(wc.rb_and))
return
end
end
if wc.inc_ef ~= nil then
if wc.inc_ef == true or wc.inc_ef == false then
storage.newpriority[surface][icon1][icon2].wc.inc_ef = wc.inc_ef
else
game.print("inc_ef requires boolean value, not " .. tostring(wc.inc_ef))
return
end
end
if wc.empty ~= nil then
if wc.empty == true or wc.empty == false then
storage.newpriority[surface][icon1][icon2].wc.empty = wc.empty
storage.newpriority[surface][icon1][icon2].wc.full = not (wc.empty)
else
game.print("empty requires boolean value, not " .. tostring(wc.empty))
return
end
end
if wc.full ~= nil then
if wc.full == true or wc.full == false then
storage.newpriority[surface][icon1][icon2].wc.empty = not (wc.full)
storage.newpriority[surface][icon1][icon2].wc.full = wc.full
else
game.print("full requires boolean value, not " .. tostring(wc.full))
return
end
end
if wc.inactivity ~= nil then
if wc.inactivity == true or wc.inactivity == false then
storage.newpriority[surface][icon1][icon2].wc.inactivity = wc.inactivity
else
game.print("inactivity requires boolean value, not " .. tostring(wc.inactivity))
return
end
end
if wc.inact_int ~= nil then
if tonumber(wc.inact_int) then
if wc.inact_int >= 0 then
storage.newpriority[surface][icon1][icon2].wc.inact_int = wc.inact_int
else
game.print("inact_int must be greater than 0, not " .. tostring(wc.inact_int))
return
end
else
game.print("inact_int requires numeric value, not " .. tostring(wc.inact_int))
return
end
end
if wc.wait_timer ~= nil then
if wc.wait_timer == true or wc.wait_timer == false then
storage.newpriority[surface][icon1][icon2].wc.wait_timer = wc.wait_timer
else
game.print("wait_timer requires boolean value, not " .. tostring(wc.wait_timer))
return
end
end
if wc.wait_int ~= nil then
if tonumber(wc.wait_int) then
if wc.wait_int >= 0 then
storage.newpriority[surface][icon1][icon2].wc.wait_int = wc.wait_int
else
game.print("wait_int must be greater than 0, not " .. tostring(wc.wait_int))
return
end
else
game.print("wait_int requires numeric value, not " .. tostring(wc.wait_int))
return
end
end
if wc.count ~= nil then
if wc.count == true or wc.count == false then
storage.newpriority[surface][icon1][icon2].wc.count = wc.count
else
game.print("count requires boolean value, not " .. tostring(wc.count))
return
end
end
if wc.count_amt ~= nil then
if tonumber(wc.count_amt) then
storage.newpriority[surface][icon1][icon2].wc.count_amt = wc.count_amt
else
game.print("count_amt requires numeric value, not " .. tostring(wc.count_amt))
return
end
end
if wc.count_ddn ~= nil then
if tonumber(wc.count_ddn) then
storage.newpriority[surface][icon1][icon2].wc.count_ddn = wc.count_ddn
else
game.print("count_ddn requires numeric value, not " .. tostring(wc.count_ddn))
return
end
end
else
game.print("Priority " .. surface .. ":" .. icon1 .. ":" .. icon2 .. " not found")
end
end,
append_station = function(icon1, icon2, stationlist, surface)
if check_icon(icon1) == "error" then
game.print("Remote append station failed, icon " .. icon1 .. " invalid")
return
end
if check_icon(icon2) == "error" then
game.print("Remote append station failed, icon " .. icon2 .. " invalid")
return
end
if surface == nil then
surface = "nauvis"
end
if check_priority(icon1, icon2, surface) then
if storage.newpriority[surface][icon1][icon2] ~= {} then
storage.newpriority[surface][icon1][icon2].station = storage.newpriority[surface][icon1][icon2]
.station or {}
local i = table_size(storage.newpriority[surface][icon1][icon2].station) + 1
if stationlist ~= nil then
for _, station in pairs(stationlist) do
storage.newpriority[surface][icon1][icon2].station[i] = { "", station }
i = i + 1
end
end
updaterequestedpublisher(icon1, icon2, surface)
else
game.print("Priority " .. icon1 .. ":" .. icon2 .. " not found")
end
else
game.print("Priority " .. surface .. ":" .. icon1 .. ":" .. icon2 .. " not found")
end
end,
prepend_station = function(icon1, icon2, stationlist, surface)
if check_icon(icon1) == "error" then
game.print("Remote prepend station failed, icon " .. icon1 .. " invalid")
return
end
if check_icon(icon2) == "error" then
game.print("Remote prepend station failed, icon " .. icon2 .. " invalid")
return
end
if surface == nil then
surface = "nauvis"
end
if check_priority(icon1, icon2, surface) then
storage.newpriority[surface][icon1][icon2].station = storage.newpriority[surface][icon1][icon2].station or
{}
local current_stations = table.deepcopy(storage.newpriority[surface][icon1][icon2].station)
local i = 1
if stationlist ~= nil then
for _, station in pairs(stationlist) do
storage.newpriority[surface][icon1][icon2].station[i] = { "", station }
i = i + 1
end
end
if current_stations ~= nil then
for _, station in pairs(current_stations) do
storage.newpriority[surface][icon1][icon2].station[i] = station
i = i + 1
end
end
updaterequestedpublisher(icon1, icon2, surface)
else
game.print("Priority " .. icon1 .. ":" .. icon2 .. " not found")
end
end
})
function check_icon(icon)
local type = "error"
if prototypes.item[icon] ~= nil then
type = "item"
elseif prototypes.fluid[icon] ~= nil then
type = "fluid"
elseif prototypes.virtual_signal[icon] ~= nil then
type = "virtual"
end
return type
end
function check_priority(icon1, icon2, surface)
local check = false
if storage.newpriority[surface] ~= nil then
if storage.newpriority[surface][icon1] ~= nil then
if storage.newpriority[surface][icon1][icon2] ~= nil then
check = true
end
end
end
return check
end