-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunit.lua
More file actions
510 lines (490 loc) · 13.8 KB
/
unit.lua
File metadata and controls
510 lines (490 loc) · 13.8 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
field = {{},{},{},{},{},{},{},{}}
unit = {} --is changed in DEPLOY
p1S = {{3,2},{4,2},{5,2},{6,2}}
p2S = {{3,7},{4,7},{5,7},{6,7}}
p3S = {{2,3},{2,4},{2,5},{2,6}}
p4S = {{7,3},{7,4},{7,5},{7,6}}
up = {{1,8},{2,8},{3,8},{4,8},{5,8},{6,8},{7,8},{8,8}}
down = {{1,1},{2,1},{3,1},{4,1},{5,1},{6,1},{7,1},{8,1}}
left = {{1,8},{1,7},{1,6},{1,5},{1,4},{1,3},{1,2},{1,1}}
right = {{8,8},{8,7},{8,6},{8,5},{8,4},{8,3},{8,2},{8,1}}
tests = {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}}
offit = { }
playerName = "p1S" --temporary, will be in twar.declare(who?)
require("middleclass")
Unit = class("Unit")
function Unit:init() --prepares the field
unit = {}
for x=1,8,1 do
for y=1,8,1 do
field[x][y] = 0
end
end
for a=1,8,1 do
table.insert(unit,{})
end
for a=1,8,1 do
for b=1,8,1 do
table.insert(unit[a],{})
end
end
end
function Unit:deploy(unitOrig,square) --unit (from build:make) and square, which asks for which of the four spawn points the unit will be spawned into
unit = {}
if instanceOf(Build,unitOrig) then unit = Build:new() end
for a=1,8,1 do
table.insert(unit,{})
end
for a=1,8,1 do
for b=1,8,1 do
table.insert(unit[a],{0})
end
end
for x=1,8,1 do
for y=1,8,1 do
for z=1,7,1 do
unit[x][y][z] = 0
---print(unit[x][y][z])
end
end
end
if instanceOf(Build,unitOrig) == false then return "nice try, suckah!" end
for x=1,5,1 do
for y=1,5,1 do
for z=1,5,1 do
if unit[x] ~= nil then
if unit[x][y] ~= nil then
if instanceOf(Chem,unitOrig[x][y][z]) then
--print(unitOrig[x][y][z]) uncomment for personal debug
unit[x+1][y+1][z+1] = unitOrig[x][y][z]
--print(unit[x+1][y+1][z+1])
else unit[x+1][y+1][z+1] = 0 end
end
end
end
end
end
for x=1,8,1 do
for y=1,8,1 do
for z=1,7,1 do
if unit[x][y][z] == nil then unit[x][y][z] = 0 end
end
end
end
--now place in spawn point <square>
player = _G[playerName]
local fX = player[square][1]
local fY = player[square][2]
field[fX][fY] = unit --in words: place unit in player 1's first spawn point by the x and y co-ordinates in the table p1S. in other words: ffffuuuuu...
for z=1,7,1 do --for every value of Z test if there are any blocks, if not, block above drops down (GRAVITY!)
for x=1,8,1 do --every value of X (I am so glad that I went for 5x5x5, not 10x10x10!)
for y=1,8,1 do --every value of Y
if instanceOf(Chem,field[fX][fY][x][y][z]) == false then --if there's no block in that place
if testFloat(x,y,z,fX,fY) == 0 then --if not sticky or floating then
local copy = field[fX][fY][x][y][z+1] --cut the block above
field[fX][fY][x][y][z+1] = 0 --down one level
field[fX][fY][x][y][z] = copy --and paste
end
end
end
end
end
end
--[[ What's next? I hear me ask. Stuff, I announce crazily.
Unit.move(x,y,unit) -- returns 1 if unit can be moved and is moved successfully !LOCAL!
# Unit.rotate(x,y,degrees) -- checks for ownership, sentience and if the unit CAN move (power > friction, ignored if centre block), and then rotates it by degrees (only multiples of 90 are valid). LOCAL FUNCTION FOR TESTING WITH.
Unit.Update() -- works out where the electricity is going in the block
Unit.on(x,y,blockX,blockY) -- checks for ownership, if the block (blockX,blockY) has a CPU val
Unit.off(x,y,blockX,blockY) -- opposite of Unit.on.
Unit.pulse(x,y,blockX,blockY) -- a single pulse
Unit.sense(x,y,blockX,blockY) -- if sentient, returns 0 if unpowered or 1 if powered.
]]--
function Unit:sense(fieldX,fieldY,x,y,z)
if field[fieldX][fieldY][x][y][z].cpu ~= nil then
if field[fieldX][fieldY][x][y][z].powered ~= nil then
return 1
else
return 0
end
end
end
function Unit:on(fieldX,fieldY,x,y,z)
if field[fieldX][fieldY][x][y][z].cpu ~= nil then
if field[fieldX][fieldY][x][y][z].cpu > 1 then
field[fieldX][fieldY][x][y][z].power = 4
return 1
end
end
return 0
end
function Unit:off(fieldX,fieldY,x,y,z)
if field[fieldX][fieldY][x][y][z].cpu ~= nil then
if field[fieldX][fieldY][x][y][z].cpu > 1 then
field[fieldX][fieldY][x][y][z].power = nil
return 1
end
end
return 0
end
function Unit:pulse(fieldX,fieldY,x,y,z)
if field[fieldX][fieldY][x][y][z].cpu ~= nil then
if field[fieldX][fieldY][x][y][z].cpu > 1 then
field[fieldX][fieldY][x][y][z].power = 4
table.insert(offit,{fieldX,fieldY,x,y,z})
return 1
end
end
return 0
end
function pulseCheck()
for i=1,#offit,1 do
field[offit[i][1]][offit[i][2]][offit[i][3]][offit[i][4]][offit[i][5]].power = nil
end
end
function Unit:update() --run after each TURN!
testElectricity()
for fieldX=1,8,1 do
for fieldY=1,8,1 do
if instanceOf(Build,field[fieldX][fieldY]) then
for x=1,8,1 do
for y=1,8,1 do
for z=1,7,1 do
if instanceOf(Chem,field[fieldX][fieldY][x][y][z]) then --produces error for some reason :P
if field[fieldX][fieldY][x][y][z].force ~= nil and field[fieldX][fieldY][x][y][z].isPowered == true then
local testDir = {x,y}
dir = findDir(testDir)
if dir ~= nil then move(fieldX,fieldY,dir) end --needs rotation!!!
dir = nil
end
end
end
end
end
end
end
end --not the end yet! do explosive testing
testExplosives()
end
function testExplosives()
for fieldX=1,8,1 do
for fieldY=1,8,1 do
if instanceOf(Build,field[fieldX][fieldY]) then
for x=1,8,1 do
for y=1,8,1 do
for z=1,8,1 do
if instanceOf(Chem,field[fieldX][fieldY][x][y][z]) then
if field[fieldX][fieldY][x][y][z].explosive ~= nil then
for a=1,6,1 do
local agree = false
local x2 = x + tests[a][1]
local y2 = x + tests[a][2]
local z2 = z + tests[a][3]
if instanceOf(Chem,field[fieldX][fieldY][x2][y2][z2]) then
if field[fieldX][fieldY][x2][y2][z2].powered ~= nil then
agree = true
end
end
end
if agree == true then
--EXPLOSION!!!!
explosion(fieldX,fieldY,x,y,z)
end
end
end
end
end
end
end
end
end
if true then --a totally useless line only there because I couldn't be bothered to untab all of those ends :P
for fieldX=1,5,1 do
for fieldY=1,5,1 do
if field[fieldX][fieldY] ~= 0 then
for x=1,8,1 do
for y=1,8,1 do
for z=1,6,1 do
if instanceOf(Chem,field[fieldX][fieldY][x][y][z]) then
if field[fieldX][fieldY][x][y][z].exploded ~= nil then
local stick = 0
local copy = field[fieldX][fieldY][x][y][z]
local x2 = x
local y2 = y
local z2 = z
local a = field[fieldX][fieldY][x][y][z].exploded
for a=1,6,1 do
local x2 = x + test[a][1]
local y2 = x + test[a][2]
local z2 = z + test[a][3]
if field[fieldX][fieldY][x2][y2][z2].sticky ~= 0 then
stick = stick + field[fieldX][fieldY][x2][y2][z2].sticky
end
end
if stick < field[fieldX][fieldY][x][y][z].explodedVal then
while true do
local x2 = x2 + a[1]
local y2 = y2 + a[2]
local z2 = z2 + a[3]
local power = field[fieldX][fieldY][x][y][z].explodedVal
if field[fieldX][fieldY][x2][y2][z2] == 0 and power > 0 then
field[fieldX][fieldY][x2][y2][z2] = copy
local copy = field[fieldX][fieldY][x2][y2][z2]
power = power - 1
else
explosion(fieldX,fieldY,x2,y2,z2)
break
end
end
end
end
end
end
end
end
end
end
end
end
clearExplosion()
pulseCheck()
end
function explosion(fieldX,fieldY,x,y,z)
for a=1,6,1 do
local x2 = x + test[a][1]
local y2 = y + test[a][2]
local z2 = z + test[a][3]
if x2 > 8 then
x2 = x2 - 8
fieldX = fieldX + 1
elseif x2 < 1 then
x2 = x2 + 8
fieldX = fieldX - 1
end
if y2 > 8 then
y2 = y2 - 8
fieldY = fieldY + 1
elseif y2 < 1 then
y2 = y2 + 8
fieldY = fieldY - 1
end
if z2 > 8 then z2 = 8 end
if z2 < 1 then z2 = 1 end
if instanceOf(Chem,field[fieldX][fieldY][x2][y2][z2]) then
field[fieldX][fieldY][x2][y2][z2].exploded = test[a]
field[fieldX][fieldY][x2][y2][z2].explodedVal = field[fieldX][fieldY][x][y][z].explosive
calc(fieldX,fieldY,x,y,z)
end
end
field[fieldX][fieldY][x][y][z] = 0
end
function clearExplosion()
for fieldX=1,8,1 do
for fieldY=1,8,1 do
if instanceOf(Build,field[fieldX][fieldY]) then
for x=1,8,1 do
for y=1,8,1 do
for z=1,8,1 do
if instanceOf(Chem,field[fieldX][fieldY][x][y][z]) then
field[fieldX][fieldY][x][y][z].exploded = nil
end
end
end
end
end
end
end
end
function testElectricity()
local powerSources = { }
for fieldX=1,8,1 do
for fieldY=1,8,1 do
if field[fieldX][fieldY] ~= 0 then
for x=1,8,1 do
for y=1,8,1 do
for z=1,7,1 do
if instanceOf(Chem,field[fieldX][fieldY][x][y][z]) then
field[fieldX][fieldY][x][y][z].powered = nil
end
end
end
end
end
end
end
while true do
for fieldX=1,8,1 do
for fieldY=1,8,1 do
if field[fieldX][fieldY] ~= 0 then
for x=1,7,1 do
for y=1,7,1 do
for z=1,6,1 do
if instanceOf(Chem,field[fieldX][fieldY][x][y][z]) then
if field[fieldX][fieldY][x][y][z].power ~= nil or field[fieldX][fieldY][x][y][z].powered ~= nil then
for a=1,6,1 do
local x2 = x + tests[a][1]
local y2 = x + tests[a][2]
local z2 = z + tests[a][3] --CURRENTLY IMPOSSIBLE TO SHARE POWER OVER FIELD SQUARES (e.g, from f[1][1][5][5][5] to f[2][1][1][5][5])
if x > 0 and x < 6 and y > 0 and y < 6 and z > 0 and z < 6 then
if instanceOf(Chem,field[fieldX][fieldY][x2][y2][z2]) then
if field[fieldX][fieldY][x2][y2][z2].conductivity ~= nil then
if field[fieldX][fieldY][x2][y2][z2].powered == nil then
field[fieldX][fieldY][x2][y2][z2].powered = 2
continue = true
end
end
end
end
end
end
end
end
end
end
end
end
end
if continue ~= true then break end -- why ~=? I don't know :P
end
end
function findDir(t) --no Z!
for i=1,8,1 do
if t == up[i] then
return up
elseif t == down[i] then
return down
elseif t == left[i] then
return left
elseif t == right[i] then
return right
end
end
return 0
end
function move(x,y,dir) --used to calculate if a block can move
local friction = 0
local force = 0
for xT=1,5,1 do
for yT=1,5,1 do
if testFloat(xT,xY,1,x,y) == 0 then
for zT=1,5,1 do
friction = field[x][y][xT][yT][zT].density + friction
end
end
end
end
local rbs = { } --stores all fuel blocks
local fBlock = 0
for f=1,8,1 do
for z=1,7,1 do
if field[x][y][dir[f][1]][dir[f][2]][z].force ~= nil then
fBlock = field[x][y][dir[f][1]][dir[f][2]][z].force --force of block on side
end
if fBlock ~= nil then
force = force + fBlock --calculate total force from side <dir>
table.insert(rbs,{dir[f][1],dir[f][2],z})
end
end
end
if force < friction then
return 0
else
local dif = force - friction
local b = 1 --counter
while true do --PAYMENT!
local i = rbs[b][1]
local j = rbs[b][2]
local k = rbs[b][3]
if field[x][y][i][j][k].force >= dif then
field[x][y][i][j][k].force = field[x][y][i][j][k].force - dif --pay with reduction of force
break --if four force is required, four is payed for
else
dif = dif - field[x][y][i][j][k].force
end
end
return 1
end
end
function testFloat(x,y,z,fX,fY)
local density = 0
for xT=1,5,1 do
for yT=1,5,1 do
if field[fX][fY] ~= 0 then
if instanceOf(Chem,field[fX][fY][xT][yT][z]) then
if field[fX][fY][xT][yT][z].density ~= nil then
density = density + field[fX][fY][xT][yT][z].density
end
end
end
end
end
local float = 0
for xT=1,x,1 do --check all blocks below stated
for yT=1,y,1 do
if field[fX][fY] ~= 0 then
if instanceOf(Chem,field[fX][fY][xT][yT][z]) then
if field[fX][fY][xT][yT][z].density ~= nil then
float = float + field[fX][fY][xT][yT][z].density
end
end
end
end
end
local sticky = 0
for zT=1,5,1 do
if x < 5 then
if field[fX][fY] ~= 0 then
if instanceOf(Chem,field[fX][fY][x+1][y][z]) then
if field[fX][fY][x+1][y][z].sticky ~= nil then sticky = 1 end
end
end
end
if x > 1 then
if field[fX][fY] ~= 0 then
if instanceOf(Chem,field[fX][fY][x-1][y][z]) then
if field[fX][fY][x-1][y][z].sticky ~= nil then sticky = 1 end
end
end
end
if y < 5 then
if field[fX][fY] ~= 0 then
if instanceOf(Chem,field[fX][fY][x][y+1][z]) then
if field[fX][fY][x][y+1][z].sticky ~= nil then sticky = 1 end
end
end
end
if y > 1 then
if field[fX][fY] ~= 0 then
if instanceOf(Chem,field[fX][fY][x][y-1][z]) then
if field[fX][fY][x][y-1][z].sticky ~= nil then sticky = 1 end
end
end
end
if field[fX][fY] ~= 0 then
if instanceOf(Chem,field[fX][fY][x][y][z]) then
if field[fX][fY][x][y][z].sticky ~= nil then
if x < 5 then
if instanceOf(Chem,field[fX][fY][x+1][y][z]) then sticky = 1 end
end
if x > 1 then
if instanceOf(Chem,field[fX][fY][x-1][y][z]) then sticky = 1 end
end
if y < 5 then
if instanceOf(Chem,field[fX][fY][x][y+1][z]) then sticky = 1 end
end
if y > 1 then
if instanceOf(Chem,field[fX][fY][x][y-1][z]) then sticky = 1 end
end
end
end
end
end
if sticky == 1 then
return 1
elseif density < float then
return 1
else return 0 end
end
function calc(fieldX,fieldY,x,y,z)
field[fieldX][fieldY][x][y][z].hp = field[fieldX][fieldY][x][y][z].hp - field[fieldX][fieldY][x][y][z].explodedVal
if field[fieldX][fieldY][x][y][z].hp < 1 then field[fieldX][fieldY][x][y][z] = 0 end
end