-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathshape - old circle code.lua
1795 lines (1711 loc) · 50.9 KB
/
shape - old circle code.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
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Variable Setup
-- Command Line input Table
local argTable = {...}
-- Flag Variables: These are conditions for different features (all flags are named foo_bar, all other variables are named fooBar)
local cmd_line = false
local cmd_line_resume = false
local cmd_line_cost_only = false
local chain_next_shape = false -- This tells goHome() where to end, if true it goes to (0, 0, positionZ) if false it goes to (-1, -1, 0)
local special_chain = false -- For certain shapes that finish where the next chained shape should start, goHome() will only turn to face 0 if true
local cost_only = false
local sim_mode = false
local resupply = false
local enderchest_refilling = false
local can_use_gps = false
local return_to_home = false -- whether the turtle shall return to start after build
-- Record Keeping Variables: These are for recoding the blocks and fuel used
local blocks = 0
local fuel = 0
-- Position Tracking Variables: These are for keeping track of the turtle's position
local positionX = 0
local positionY = 0
local positionZ = 0
local facing = 0
local gpsPositionX = 0
local gpsPositionY = 0
local gpsPositionZ = 0
local gpsFacing = 0
-- General Variables: Other variables that don't fit in the other categories
local choice = ""
-- Progress Table: These variables are the tables that the turtle's progress is tracked in
local tempProgTable = {}
local progTable = {} --This is the LOCAL table! used for local stuff only, and is ONLY EVER WRITTEN when sim_mode is FALSE
local progFileName = "ShapesProgressFile"
-- Utility functions
function writeOut(...) -- ... lets writeOut() pass any arguments to print(). so writeOut(1,2,3) is the same as print(1,2,3). previously writeOut(1,2,3) would have been the same as print(1)
for i, v in ipairs(arg) do
print(v)
end
end
function getInput(inputType, message, option1, option2)
local input = ""
if inputType == "string" then
writeOut(message.. "(" ..option1 .. " or "..option2..")" )
while true do
input = io.read()
input = string.lower(input)
if input ~= option1 and input ~= option2 then
writeOut("You didn't enter a valid option. Please try again.")
else
return input
end
end
end
if inputType == "int" then
writeOut(message)
while true do
input = io.read()
if tonumber(input) ~= nil then
return tonumber(input)
else
writeOut("Need a number. Please try again")
end
end
end
end
function wrapModules() -- checks for and wraps turtle modules
local test = 0
if peripheral.getType("left" )== "resupply" then
resupplymodule=peripheral.wrap("left")
resupply = true
elseif peripheral.getType("right") == "resupply" then
resupplymodule=peripheral.wrap("right")
resupply = true
end
if peripheral.getType("left") == "modem" then
modem=peripheral.wrap("left")
test, _, _ = gps.locate(1)
if test ~= nil then
can_use_gps = true
end
elseif peripheral.getType("right") == "modem" then
modem=peripheral.wrap("right")
test, _, _ = gps.locate(1)
if test ~= nil then
can_use_gps = true
end
end
if resupply then
return "resupply"
end
end
function linkToRSStation() -- Links to resupply station
if resupplymodule.link() then
return true
else
writeOut("Please put Resupply Station to the left of the turtle and press Enter to continue")
io.read()
linkToRSStation()
end
end
function compareResources()
if (turtle.compareTo(1) == false) then
turtle.drop()
end
end
function firstFullSlot()
for i = 1, 16 do
if (turtle.getItemCount(i) > 1) then
return i
end
end
end
function turtleEmpty()
for i = 1, 16 do
if (turtle.getItemCount(i) > 1) then
return false
end
end
return true
end
function checkResources()
if resupply then
if turtle.getItemCount(activeSlot) <= 1 then
while not(resupplymodule.resupply(1)) do
os.sleep(0.5)
end
end
elseif enderchest_refilling then
compareResources()
while (turtle.getItemCount(activeSlot) <= 1) do
if (activeSlot == 15) and (turtle.getItemCount(activeSlot)<=1) then
turtle.select(16)
turtle.digUp()
for i = 1, 15 do
turtle.select(i)
turtle.drop()
end
turtle.select(16)
turtle.placeUp()
turtle.select(1)
for i = 1, 15 do
turtle.suckUp()
end
turtle.select(16)
turtle.digUp()
activeSlot = 1
turtle.select(activeSlot)
else
activeSlot = activeSlot + 1
-- writeOut("Turtle slot empty, trying slot "..activeSlot)
turtle.select(activeSlot)
end
compareResources()
os.sleep(0.2)
end
else
compareResources()
while (turtle.getItemCount(activeSlot) <= 1) do
if turtleEmpty() then
writeOut("Turtle is empty, please put building block in slots and press enter to continue")
io.read()
activeSlot = 1
turtle.select(activeSlot)
else
activeSlot = firstFullSlot()
turtle.select(activeSlot)
end
compareResources()
end
end
end
function checkFuel()
if (not(tonumber(turtle.getFuelLevel()) == nil)) then
while turtle.getFuelLevel() < 50 do
writeOut("Turtle almost out of fuel, pausing. Please drop fuel in inventory. And press enter.")
io.read()
turtle.refuel()
end
end
end
function placeBlock()
blocks = blocks + 1
simulationCheck()
if cost_only then
return
end
if turtle.detectDown() and not turtle.compareDown() then
turtle.digDown()
end
checkResources()
turtle.placeDown()
progressUpdate()
end
function round(toBeRounded, decimalPlace) -- Needed for hexagon and octagon
local multiplier = 10^(decimalPlace or 0)
return math.floor(toBeRounded * multiplier + 0.5) / multiplier
end
-- Navigation functions
-- Allow the turtle to move while tracking its position
-- This allows us to just give a destination point and have it go there
function turnRightTrack()
simulationCheck()
facing = facing + 1
if facing >= 4 then
facing = 0
end
progressUpdate()
if cost_only then
return
end
turtle.turnRight()
end
function turnLeftTrack()
simulationCheck()
facing = facing - 1
if facing < 0 then
facing = 3
end
progressUpdate()
if cost_only then
return
end
turtle.turnLeft()
end
function turnAroundTrack()
turnLeftTrack()
turnLeftTrack()
end
function turnToFace(direction)
if (direction < 0) then
return false
end
direction = direction % 4
while facing ~= direction do
turnRightTrack()
end
return true
end
function safeForward()
simulationCheck()
if facing == 0 then
positionY = positionY + 1
elseif facing == 1 then
positionX = positionX + 1
elseif facing == 2 then
positionY = positionY - 1
elseif facing == 3 then
positionX = positionX - 1
end
fuel = fuel + 1
progressUpdate()
if cost_only then
return
end
checkFuel()
local success = false
local tries = 0
while not success do
success = turtle.forward()
if not success then
while (not success) and tries < 6 do
tries = tries + 1
turtle.dig()
success = turtle.forward()
sleep(0.3)
end
if not success then
writeOut("Blocked attempting to move forward.")
writeOut("Please clear and press enter to continue.")
io.read()
end
end
end
end
function safeBack()
simulationCheck()
if facing == 0 then
positionY = positionY - 1
elseif facing == 1 then
positionX = positionX - 1
elseif facing == 2 then
positionY = positionY + 1
elseif facing == 3 then
positionX = positionX + 1
end
fuel = fuel + 1
progressUpdate()
if cost_only then
return
end
checkFuel()
local success = false
local tries = 0
while not success do
success = turtle.back()
if not success then
turnAroundTrack()
while turtle.detect() and tries < 6 do
tries = tries + 1
if turtle.dig() then
break
end
sleep(0.3)
end
turnAroundTrack()
success = turtle.back()
if not success then
writeOut("Blocked attempting to move back.")
writeOut("Please clear and press enter to continue.")
io.read()
end
end
end
end
function safeUp()
simulationCheck()
positionZ = positionZ + 1
fuel = fuel + 1
progressUpdate()
if cost_only then
return
end
checkFuel()
local success = false
while not success do
success = turtle.up()
if not success then
while turtle.detectUp() do
if not turtle.digUp() then
writeOut("Blocked attempting to move up.")
writeOut("Please clear and press enter to continue.")
io.read()
end
end
end
end
end
function safeDown()
simulationCheck()
positionZ = positionZ - 1
fuel = fuel + 1
progressUpdate()
if cost_only then
return
end
checkFuel()
local success = false
while not success do
success = turtle.down()
if not success then
while turtle.detectDown() do
if not turtle.digDown() then
writeOut("Blocked attempting to move down.")
writeOut("Please clear and press enter to continue.")
io.read()
end
end
end
end
end
function moveY(targetY)
if targetY == positionY then
return
end
if (facing ~= 0 and facing ~= 2) then -- Check axis
turnRightTrack()
end
while targetY > positionY do
if facing == 0 then
safeForward()
else
safeBack()
end
end
while targetY < positionY do
if facing == 2 then
safeForward()
else
safeBack()
end
end
end
function moveX(targetX)
if targetX == positionX then
return
end
if (facing ~= 1 and facing ~= 3) then -- Check axis
turnRightTrack()
end
while targetX > positionX do
if facing == 1 then
safeForward()
else
safeBack()
end
end
while targetX < positionX do
if facing == 3 then
safeForward()
else
safeBack()
end
end
end
function moveZ(targetZ)
if targetZ == positionZ then
return
end
while targetZ < positionZ do
safeDown()
end
while targetZ > positionZ do
safeUp()
end
end
-- I *HIGHLY* suggest formatting all shape subroutines to use the format that dome() uses; specifically, navigateTo(x,y,[z]) then placeBlock(). This should ensure proper "data recording" and also makes readability better
function navigateTo(targetX, targetY, targetZ, move_z_first)
targetZ = targetZ or positionZ -- If targetZ isn't used in the function call, it defaults to its current z position, this should make it compatible with all previous implementations of navigateTo()
move_z_first = move_z_first or false -- Defaults to moving z last, if true is passed as 4th argument, it moves vertically first
if move_z_first then
moveZ(targetZ)
end
if facing == 0 or facing == 2 then -- Y axis
moveY(targetY)
moveX(targetX)
else
moveX(targetX)
moveY(targetY)
end
if not move_z_first then
moveZ(targetZ)
end
end
function goHome()
if chain_next_shape then
if not special_chain then
navigateTo(0, 0) -- So another program can chain multiple shapes together to create bigger structures
end
else
navigateTo(-1, -1, 0) -- So the user can collect the turtle when it is done, not 0,0,0 because some shapes use the 0,0 column
end
turnToFace(0)
end
-- Shape Building functions
function drawLine(endX, endY, startX, startY)
startX = startX or positionX
startY = startY or positionY
deltaX = math.abs(endX - startX)
deltaY = math.abs(endY - startY)
errorVar = 0
if deltaX >= deltaY then
deltaErr = math.abs(deltaY/deltaX)
if startX < endX then
if startY < endY then
counterY = startY
for counterX = startX, endX do
navigateTo(counterX, counterY)
placeBlock()
errorVar = errorVar + deltaErr
if errorVar >= 0.5 then
counterY = counterY + 1
errorVar = errorVar - 1
end
end
else
counterY = startY
for counterX = startX, endX do
navigateTo(counterX, counterY)
placeBlock()
errorVar = errorVar + deltaErr
if errorVar >= 0.5 then
counterY = counterY - 1
errorVar = errorVar - 1
end
end
end
else
if startY < endY then
counterY = startY
for counterX = startX, endX, -1 do
navigateTo(counterX, counterY)
placeBlock()
errorVar = errorVar + deltaErr
if errorVar >= 0.5 then
counterY = counterY + 1
errorVar = errorVar - 1
end
end
else
counterY = startY
for counterX = startX, endX, -1 do
navigateTo(counterX, counterY)
placeBlock()
errorVar = errorVar + deltaErr
if errorVar >= 0.5 then
counterY = counterY - 1
errorVar = errorVar - 1
end
end
end
end
else
deltaErr = math.abs(deltaX/deltaY)
if startY < endY then
if startX < endX then
counterX = startX
for counterY = startY, endY do
navigateTo(counterX, counterY)
placeBlock()
errorVar = errorVar + deltaErr
if errorVar >= 0.5 then
counterX = counterX + 1
errorVar = errorVar - 1
end
end
else
counterX = startX
for counterY = startY, endY do
navigateTo(counterX, counterY)
placeBlock()
errorVar = errorVar + deltaErr
if errorVar >= 0.5 then
counterX = counterX - 1
errorVar = errorVar - 1
end
end
end
else
if startX < endX then
counterX = startX
for counterY = startY, endY, -1 do
navigateTo(counterX, counterY)
placeBlock()
errorVar = errorVar + deltaErr
if errorVar >= 0.5 then
counterX = counterX + 1
errorVar = errorVar - 1
end
end
else
counterX = startX
for counterY = startY, endY, -1 do
navigateTo(counterX, counterY)
placeBlock()
errorVar = errorVar + deltaErr
if errorVar >= 0.5 then
counterX = counterX - 1
errorVar = errorVar - 1
end
end
end
end
end
end
function rectangle(width, depth, startX, startY)
startX = startX or positionX
startY = startY or positionY
endX = startX + width - 1
endY = startY + depth - 1
drawLine(startX, endY, startX, startY)
drawLine(endX, endY, startX, endY)
drawLine(endX, startY, endX, endY)
drawLine(startX, startY, endX, startY)
end
function square(length, startX, startY)
startX = startX or positionX
startY = startY or positionY
rectangle(length, length, startX, startY)
end
function wall(depth, height)
for i = 1, depth do
for j = 1, height do
placeBlock()
if j < height then
navigateTo(positionX, positionY, positionZ + 1)
end
end
if (i ~= depth) then
navigateTo(positionX, positionY + 1, 0)
end
end
end
function platform(width, depth, startX, startY)
startX = startX or positionX
startY = startY or positionY
endX = startX + width - 1
endY = startY + depth - 1
forward = true
for counterY = startY, endY do
if forward then
for counterX = startX, endX do
navigateTo(counterX, counterY)
placeBlock()
end
else
for counterX = endX, startX, -1 do
navigateTo(counterX, counterY)
placeBlock()
end
end
forward = not forward
end
end
function cuboid(width, depth, height, hollow)
for i = 0, height - 1 do
navigateTo(0, 0, i)
if (hollow == "n") then
platform(width, depth, 0, 0)
else
rectangle(width, depth, 0, 0)
end
end
end
function pyramid(length, hollow)
-- local height = math.ceil(length / 2) - 1
i = 0
while (length > 0) do
navigateTo(i, i, i)
if (hollow == "y") then
rectangle(length, length, i, i)
else
platform(length, length, i, i)
end
i = i + 1
length = length - 2
end
end
function stair(width, height, startX, startY) -- Last two might be able to be used to make a basic home-like shape later?
startX = startX or positionX
startY = startY or positionY
endX = startX + width - 1
endY = startY + height - 1
forward = true
for counterY = startY, endY do
if forward then
for counterX = startX, endX do
navigateTo(counterX, counterY)
placeBlock()
end
else
for counterX = endX, startX, -1 do
navigateTo(counterX, counterY)
placeBlock()
end
end
if counterY ~= endY then
navigateTo(positionX, positionY, positionZ + 1)
forward = not forward
end
end
end
function circle(radius)
width = radius * 2 + 1
sqrt3 = 3 ^ 0.5
boundaryRadius = radius + 1.0
boundary2 = boundaryRadius ^ 2
z = radius
cz2 = (radius - z) ^ 2
limitOffsetY = (boundary2 - cz2) ^ 0.5
maxOffestY = math.ceil(limitOffsetY)
-- We do first the +x side, then the -x side to make movement efficient
for side = 0, 1 do
-- On the right we go from small y to large y, on the left reversed
-- This makes us travel clockwise (from below) around each layer
if (side == 0) then
yStart = radius - maxOffestY
yEnd = radius + maxOffestY
yStep = 1
else
yStart = radius + maxOffestY
yEnd = radius - maxOffestY
yStep = -1
end
for y = yStart, yEnd, yStep do
cy2 = (radius - y) ^ 2
remainder2 = (boundary2 - cz2 - cy2)
if remainder2 >= 0 then
-- This is the maximum difference in x from the centre we can be without definitely being outside the radius
maxOffsetX = math.ceil((boundary2 - cz2 - cy2) ^ 0.5)
-- Only do either the +x or -x side
if (side == 0) then
-- +x side
xStart = radius
xEnd = radius + maxOffsetX
else
-- -x side
xStart = radius - maxOffsetX
xEnd = radius - 1
end
-- Reverse direction we traverse xs when in -y side
if y > radius then
temp = xStart
xStart = xEnd
xEnd = temp
xStep = -1
else
xStep = 1
end
for x = xStart, xEnd, xStep do
cx2 = (radius - x) ^ 2
distanceToCentre = (cx2 + cy2 + cz2) ^ 0.5
-- Only blocks within the radius but still within 1 3d-diagonal block of the edge are eligible
if distanceToCentre < boundaryRadius and distanceToCentre + sqrt3 >= boundaryRadius then
offsets = {{0, 1, 0}, {0, -1, 0}, {1, 0, 0}, {-1, 0, 0}, {0, 0, 1}, {0, 0, -1}}
for i=1,6 do
offset = offsets[i]
dx = offset[1]
dy = offset[2]
dz = offset[3]
if ((radius - (x + dx)) ^ 2 + (radius - (y + dy)) ^ 2 + (radius - (z + dz)) ^ 2) ^ 0.5 >= boundaryRadius then
-- This is a point to use
navigateTo(x, y)
placeBlock()
break
end
end
end
end
end
end
end
end
function dome(typus, radius)
-- Main dome and sphere building routine
width = radius * 2 + 1
sqrt3 = 3 ^ 0.5
boundaryRadius = radius + 1.0
boundary2 = boundaryRadius ^ 2
if typus == "dome" then
zstart = radius
elseif typus == "sphere" then
zstart = 0
elseif typus == "bowl" then
zstart = 0
end
if typus == "bowl" then
zend = radius
else
zend = width - 1
end
-- This loop is for each vertical layer through the sphere or dome.
for z = zstart,zend do
if not cost_only and z ~= zstart then
navigateTo(positionX, positionY, positionZ + 1)
end
--writeOut("Layer " .. z)
cz2 = (radius - z) ^ 2
limitOffsetY = (boundary2 - cz2) ^ 0.5
maxOffestY = math.ceil(limitOffsetY)
-- We do first the +x side, then the -x side to make movement efficient
for side = 0,1 do
-- On the right we go from small y to large y, on the left reversed
-- This makes us travel clockwise (from below) around each layer
if (side == 0) then
yStart = radius - maxOffestY
yEnd = radius + maxOffestY
yStep = 1
else
yStart = radius + maxOffestY
yEnd = radius - maxOffestY
yStep = -1
end
for y = yStart,yEnd,yStep do
cy2 = (radius - y) ^ 2
remainder2 = (boundary2 - cz2 - cy2)
if remainder2 >= 0 then
-- This is the maximum difference in x from the centre we can be without definitely being outside the radius
maxOffsetX = math.ceil((boundary2 - cz2 - cy2) ^ 0.5)
-- Only do either the +x or -x side
if (side == 0) then
-- +x side
xStart = radius
xEnd = radius + maxOffsetX
else
-- -x side
xStart = radius - maxOffsetX
xEnd = radius - 1
end
-- Reverse direction we traverse xs when in -y side
if y > radius then
temp = xStart
xStart = xEnd
xEnd = temp
xStep = -1
else
xStep = 1
end
for x = xStart,xEnd,xStep do
cx2 = (radius - x) ^ 2
distanceToCentre = (cx2 + cy2 + cz2) ^ 0.5
-- Only blocks within the radius but still within 1 3d-diagonal block of the edge are eligible
if distanceToCentre < boundaryRadius and distanceToCentre + sqrt3 >= boundaryRadius then
offsets = {{0, 1, 0}, {0, -1, 0}, {1, 0, 0}, {-1, 0, 0}, {0, 0, 1}, {0, 0, -1}}
for i=1,6 do
offset = offsets[i]
dx = offset[1]
dy = offset[2]
dz = offset[3]
if ((radius - (x + dx)) ^ 2 + (radius - (y + dy)) ^ 2 + (radius - (z + dz)) ^ 2) ^ 0.5 >= boundaryRadius then
-- This is a point to use
navigateTo(x, y)
placeBlock()
break
end
end
end
end
end
end
end
end
end
function cylinder(radius, height)
for i = 1, height do
circle(radius)
navigateTo(positionX, positionY, positionZ + 1)
end
end
polygonCornerList = {} -- Public list of corner coords for n-gons, will be used for hexagons, octagons, and future polygons.
-- It should be constructed as a nested list eg. {{x0,y0},{x1,y1},{x2,y2}...}
function constructPolygon() -- Uses polygonCornerList to draw sides between each point
if #polygonCornerList == 0 then
return false
end
for i = 1, #polygonCornerList do
startX = polygonCornerList[i][1]
startY = polygonCornerList[i][2]
if i == #polygonCornerList then
j = 1
else
j = i + 1
end
stopX = polygonCornerList[j][1]
stopY = polygonCornerList[j][2]
drawLine(stopX, stopY, startX, startY)
end
return true
end
function arbitraryPolygon(numberOfSides, Radius) -- Future function, this will eventually replace octagon and hexagon functions
end
function hexagon(sideLength) -- Fills out polygonCornerList with the points for a hexagon
sideLength = sideLength - 1
local changeX = sideLength / 2
local changeY = round(math.sqrt(3) * changeX, 0)
changeX = round(changeX, 0)
polygonCornerList[1] = {changeX, 0}
polygonCornerList[2] = {(changeX + sideLength), 0}
polygonCornerList[3] = {((2 * changeX) + sideLength), changeY}
polygonCornerList[4] = {(changeX + sideLength), (2 * changeY)}
polygonCornerList[5] = {changeX, (2 * changeY)}
polygonCornerList[6] = {0, changeY}
if not constructPolygon() then
error("This error should never happen.")
end
end
function octagon(sideLength) -- Fills out polygonCornerList with the points for an octagon
sideLength = sideLength - 1
local change = round((sideLength - 1) / math.sqrt(2), 0)
polygonCornerList[1] = {change, 0}
polygonCornerList[2] = {(change + sideLength), 0}
polygonCornerList[3] = {((2 * change) + sideLength), change}
polygonCornerList[4] = {((2 * change) + sideLength), (change + sideLength)}
polygonCornerList[5] = {(change + sideLength), ((2 * change) + sideLength)}
polygonCornerList[6] = {change, ((2 * change) + sideLength)}
polygonCornerList[7] = {0, (change + sideLength)}
polygonCornerList[8] = {0, change}
if not constructPolygon() then
error("This error should never happen.")
end
end
function sixprism(length, height)
for i = 1, height do
hexagon(length)
if i ~= height then
navigateTo(positionX, positionY, positionZ + 1)
end
end
end
function eightprism(length, height)
for i = 1, height do
octagon(length)
if i ~= height then
navigateTo(positionX, positionY, positionZ + 1)
end
end
end
-- Previous Progress Resuming, Simulation functions, Command Line, and File Backend
-- Will check for a "progress" file.
function CheckForPrevious()
if fs.exists(progFileName) then
return true
else
return false
end
end
-- Creates a progress file, containing a serialized table consisting of the shape type, shape input params, and the last known x, y, and z coords of the turtle (beginning of build project)
function ProgressFileCreate()
if not CheckForPrevious() then
fs.makeDir(progFileName)
return true
else
return false
end
end
-- Deletes the progress file (at the end of the project, or at beginning if user chooses to delete old progress)
function ProgressFileDelete()
if fs.exists(progFileName) then
fs.delete(progFileName)
return true
else
return false
end
end
-- To read the shape params from the file. Shape type, and input params (e.g. "dome" and radius)
function ReadShapeParams()
-- TODO. Unneeded for now, can just use the table elements directly
end
function WriteShapeParams(...) -- The ... lets it take any number of arguments and stores it to the table arg{} | This is still unused anywhere
local paramTable = arg
local paramName = "param"
local paramName2 = paramName
for i, v in ipairs(paramTable) do -- Iterates through the args passed to the function, ex. paramTable[1] i = 1 so paramName2 should be "param1", tested and works!
paramName2 = paramName .. i
tempProgTable[paramName2] = v
progTable[paramName2] = v
end
end
-- function to write the progress to the file (x, y, z)
function writeProgress()
local progFile
local progString = ""
if not (sim_mode or cost_only) then
progString = textutils.serialize(progTable) -- Put in here to save processing time when in cost_only
progFile = fs.open(progFileName, "w")
progFile.write(progString)
progFile.close()
end
end