-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompTools.lua
More file actions
390 lines (331 loc) · 13.1 KB
/
Copy pathcompTools.lua
File metadata and controls
390 lines (331 loc) · 13.1 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
--initialization
-- import dependencies
local compTools = { _version = "1.0.0" }
--initialize GLBL table if needed
if GLBL == nil then
GLBL = {}
end
--function declarations
-- thread related
function compTools.anotherInstanceOfThisScriptIsRunning()
-- function initialization
local FUNC = {}
FUNC.threads = thread.listRunning()
for key,value in pairs(FUNC.threads) do
-- put arguments in scope safe table
FUNC.key,FUNC.value = key,value
if FUNC.threads[FUNC.key].getLabel() == thread.current().getLabel() and FUNC.threads[FUNC.key].getID() ~= thread.current().getID() then
return true
end
end
return false
end
function compTools.stopOtherInstancesOfThisScript()
-- function initialization
local FUNC = {}
FUNC.currentLabel = thread.current().getLabel()
FUNC.threads = thread.listRunning()
log("&7[&bNOTICE&7]&b The &fWHITE&b errors BELOW are intentionally software induced and not a sign of a problem.")
-- stop all identical script instances that arent this one
-- thread killing is asynchronous
for key,value in pairs(FUNC.threads) do
-- store args in scope-safe table
FUNC.key,FUNC.value = key,value
if FUNC.threads[FUNC.key].getLabel() == FUNC.currentLabel and FUNC.threads[FUNC.key].getID() ~= thread.current().getID() then
-- pause thread
FUNC.threads[FUNC.key].pause()
sleep(10)
-- stop thread
FUNC.threads[FUNC.key].stop()
end
end
-- give time for asynchronous thread killing to happen
-- turns this function into a (hopefully always) synchronous one
sleep(10)
log("&7[&bNOTICE&7]&b The &fWHITE&b errors ABOVE are intentionally software induced and not a sign of a problem.")
end
function compTools.stopAllInstancesOfThisScript()
compTools.stopOtherInstancesOfThisScript()
-- stop this script
thread.current().stop()
end
function compTools.givenScriptIsRunning(scriptLabel)
--function initialization
--initialize function table
local FUNC = {}
--store arguments in locally scoped table for scope safety
FUNC.scriptLabel = scriptLabel
FUNC.threads = thread.listRunning()
for key,value in pairs(FUNC.threads) do
-- put arguments in scope safe table
FUNC.key,FUNC.value = key,value
if FUNC.threads[FUNC.key].getLabel() == FUNC.scriptLabel then
return true
end
end
return false
end
function compTools.Bresenham3D(x1,y1,z1,x2,y2,z2)
--function initialization
--initialize function table
local FUNC = {}
--store arguments in locally scoped table for scope safety
FUNC.x1 = x1
FUNC.y1 = y1
FUNC.z1 = z1
FUNC.x2 = x2
FUNC.y2 = y2
FUNC.z2 = z2
FUNC.listOfPoints = {}
FUNC.listOfPoints[1] = {FUNC.x1,FUNC.y1,FUNC.z1}
FUNC.dx = math.abs(FUNC.x2 - FUNC.x1)
FUNC.dy = math.abs(FUNC.y2 - FUNC.y1)
FUNC.dz = math.abs(FUNC.z2 - FUNC.z1)
--declare local variables that can't be assigned yet
FUNC.xs = 0
FUNC.ys = 0
FUNC.zs = 0
if (FUNC.x2 > FUNC.x1) then
FUNC.xs = 1
else
FUNC.xs = -1
end
if (FUNC.y2 > FUNC.y1) then
FUNC.ys = 1
else
FUNC.ys = -1
end
if (FUNC.z2 > FUNC.z1) then
FUNC.zs = 1
else
FUNC.zs = -1
end
-- Driving axis is X-axis
if (FUNC.dx >= FUNC.dy and FUNC.dx >= FUNC.dz) then
FUNC.p1 = 2 * FUNC.dy - FUNC.dx
FUNC.p2 = 2 * FUNC.dz - FUNC.dx
while (FUNC.x1 ~= FUNC.x2) do
FUNC.x1 = FUNC.x1 + FUNC.xs
if (FUNC.p1 >= 0) then
FUNC.y1 = FUNC.y1 + FUNC.ys
FUNC.p1 = FUNC.p1 - (2 * FUNC.dx)
end
if (FUNC.p2 >= 0) then
FUNC.z1 = FUNC.z1 + FUNC.zs
FUNC.p2 = FUNC.p2 - (2 * FUNC.dx)
end
FUNC.p1 = FUNC.p1 + (2 * FUNC.dy)
FUNC.p2 = FUNC.p2 + (2 * FUNC.dz)
-- FUNC.listOfPoints.append((FUNC.x1, FUNC.y1, FUNC.z1))
FUNC.listOfPoints[#FUNC.listOfPoints + 1] = {FUNC.x1,FUNC.y1,FUNC.z1}
end
-- Driving axis is Y-axis
elseif (FUNC.dy >= FUNC.dx and FUNC.dy >= FUNC.dz) then
FUNC.p1 = 2 * FUNC.dx - FUNC.dy
FUNC.p2 = 2 * FUNC.dz - FUNC.dy
while (FUNC.y1 ~= FUNC.y2) do
FUNC.y1 = FUNC.y1 + FUNC.ys
if (FUNC.p1 >= 0) then
FUNC.x1 = FUNC.x1 + FUNC.xs
FUNC.p1 = FUNC.p1 - (2 * FUNC.dy)
end
if (FUNC.p2 >= 0) then
FUNC.z1 = FUNC.z1 + FUNC.zs
FUNC.p2 = FUNC.p2 - (2 * FUNC.dy)
end
FUNC.p1 = FUNC.p1 + (2 * FUNC.dx)
FUNC.p2 = FUNC.p2 + (2 * FUNC.dz)
-- FUNC.listOfPoints.append((FUNC.x1, FUNC.y1, FUNC.z1))
FUNC.listOfPoints[#FUNC.listOfPoints + 1] = {FUNC.x1,FUNC.y1,FUNC.z1}
end
-- Driving axis is Z-axis"
else
FUNC.p1 = 2 * FUNC.dy - FUNC.dz
FUNC.p2 = 2 * FUNC.dx - FUNC.dz
while (FUNC.z1 ~= FUNC.z2) do
FUNC.z1 = FUNC.z1 + FUNC.zs
if (FUNC.p1 >= 0) then
FUNC.y1 = FUNC.y1 + FUNC.ys
FUNC.p1 = FUNC.p1 - (2 * FUNC.dz)
end
if (FUNC.p2 >= 0) then
FUNC.x1 = FUNC.x1 + FUNC.xs
FUNC.p2 = FUNC.p2 - (2 * FUNC.dz)
end
FUNC.p1 = FUNC.p1 + (2 * FUNC.dy)
FUNC.p2 = FUNC.p2 + (2 * FUNC.dx)
-- FUNC.listOfPoints.append((FUNC.x1, FUNC.y1, FUNC.z1))
FUNC.listOfPoints[#FUNC.listOfPoints + 1] = {FUNC.x1,FUNC.y1,FUNC.z1}
end
end
return FUNC.listOfPoints
end
function compTools.numOfKeysInTable(table)
--function initialization
--initialize function table
local FUNC = {}
--store arguments in locally scoped table for scope safety
FUNC.table = table
FUNC.numKeys = 0
for key,value in pairs(FUNC.table) do
FUNC.numKeys = FUNC.numKeys + 1
end
return FUNC.numKeys
end
function compTools.tableIsEmpty(table)
return next(table) == nil
end
function compTools.spairs(t, order)
--initialize function table
local FUNC = {}
-- collect the keys
FUNC.keys = {}
for k in pairs(t) do
FUNC.keys[#FUNC.keys+1] = k
end
-- if order function given, sort by it by passing the table and keys a, b,
-- otherwise just sort the keys
if order then
table.sort(FUNC.keys, function(a,b) return order(t, a, b) end)
else
table.sort(FUNC.keys)
end
-- return the iterator function
FUNC.i = 0
return function()
FUNC.i = FUNC.i + 1
if FUNC.keys[FUNC.i] then
return FUNC.keys[FUNC.i], t[FUNC.keys[FUNC.i]]
end
end
end
function compTools.readAll(file)
--function initialization
--initialize function table
local FUNC = {}
--store arguments in locally scoped table for scope safety
FUNC.file = file
FUNC.f = assert(io.open("../"..FUNC.file, "rb"))
FUNC.content = FUNC.f:read("*all")
FUNC.f:close()
return FUNC.content
end
function compTools.sortTableByKeys(table)
--function initialization
--initialize function table
local FUNC = {}
--store arguments in locally scoped table for scope safety
FUNC.table = table
FUNC.sortedTable = {}
for k,v in compTools.spairs(FUNC.table) do
FUNC.sortedTable[#FUNC.sortedTable + 1] = k
end
return FUNC.sortedTable
end
--reverses the order of tables with numerical indexes
function compTools.reverse(arr)
--function initialization
--initialize function table
local FUNC = {}
--store arguments in locally scoped table for scope safety
FUNC.arr = arr
FUNC.i, FUNC.j = 1, #FUNC.arr
while FUNC.i < FUNC.j do
FUNC.arr[FUNC.i], FUNC.arr[FUNC.j] = FUNC.arr[FUNC.j], FUNC.arr[FUNC.i]
FUNC.i = FUNC.i + 1
FUNC.j = FUNC.j - 1
end
end
function compTools.distanceBetweenPoints(tX,tY,tZ, pX,pY,pZ)
--function initialization
--initialize function table
local FUNC = {}
--store arguments in locally scoped table for scope safety
FUNC.tX,FUNC.tY,FUNC.tZ = tX,tY,tZ
FUNC.pX,FUNC.pY,FUNC.pZ = pX,pY,pZ
return ((FUNC.tX - FUNC.pX)^2 + (FUNC.tY - FUNC.pY)^2 + (FUNC.tZ - FUNC.pZ)^2)^(1/2)
end
function compTools.playerDistanceFrom(tX,tY,tZ)
--function initialization
--initialize function table
local FUNC = {}
--store arguments in locally scoped table for scope safety
FUNC.tX = tX
FUNC.tY = tY
FUNC.tZ = tZ
FUNC.pX, FUNC.pY, FUNC.pZ = getPlayerPos()
-- target center of block
FUNC.tX = tX + 0.5
FUNC.tY = tY + 0.5
FUNC.tZ = tZ + 0.5
return compTools.distanceBetweenPoints(FUNC.tX,FUNC.tY,FUNC.tZ, FUNC.pX,FUNC.pY,FUNC.pZ)
end
function compTools.horizontalSquareDistanceBetween(x1,z1, x2,z2)
--function initialization
--initialize function table
local FUNC = {}
--store arguments in locally scoped table for scope safety
FUNC.x1 = x1
FUNC.z1 = z1
FUNC.x2 = x2
FUNC.z2 = z2
--calculate horizontal differences
FUNC.xDist = math.abs(FUNC.x1 - FUNC.x2)
FUNC.zDist = math.abs(FUNC.z1 - FUNC.z2)
--find greatest horizontal distance
if FUNC.xDist >= FUNC.zDist then
return FUNC.xDist
else
return FUNC.zDist
end
end
function compTools.verticalSquareDistanceBetween(y1, y2)
--function initialization
--initialize function table
local FUNC = {}
--store arguments in locally scoped table for scope safety
FUNC.y1 = y1
FUNC.y2 = y2
return math.abs(FUNC.y1 - FUNC.y2)
end
function compTools.playerhorizontalSquareDistanceBetween(x,z)
--function initialization
--initialize function table
local FUNC = {}
--store arguments in locally scoped table for scope safety
FUNC.x = x
FUNC.z = z
--declare local variables
--get player position
FUNC.pX, FUNC.pY, FUNC.pZ = getPlayerPos()
return compTools.horizontalSquareDistanceBetween(FUNC.x,FUNC.z, FUNC.pX,FUNC.pZ)
end
function compTools.playerVerticalSquareDistanceBetween(y)
--function initialization
--initialize function table
local FUNC = {}
--store arguments in locally scoped table for scope safety
FUNC.y = y
--declare local variables
--get player position
FUNC.pX, FUNC.pY, FUNC.pZ = getPlayerPos()
return compTools.verticalSquareDistanceBetween(FUNC.pY, FUNC.y)
end
function compTools.split(inputstr, sep)
--function initialization
--initialize function table
local FUNC = {}
--store arguments in locally scoped table for scope safety
FUNC.inputstr = inputstr
FUNC.sep = sep
FUNC.sep = FUNC.sep or '%s'
FUNC.t = {}
for field, s in string.gmatch(FUNC.inputstr, "([^" .. FUNC.sep .. "]*)(" .. FUNC.sep .. "?)") do
--put locl variables in safe scope
FUNC.field = field
FUNC.s = s
table.insert(FUNC.t, FUNC.field)
if FUNC.s == "" then return FUNC.t end
end
end
return compTools