-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.lua
487 lines (393 loc) · 13.3 KB
/
build.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
local tArgs = { ... };
-- The different orientations (DOWN is +Y and UP is -Y)
local NORTH = 0; -- -Z
local EAST = 1; -- +X
local SOUTH = 2; -- +Z
local WEST = 3; -- -X
-- The different directions
local UP = 4;
local DOWN = 5;
local FORWARD = 6;
local RIGHT = 7;
local BACK = 8;
local LEFT = 9;
-- The starting position and orientation
local orientation = NORTH;
-- Available Command Line Arguments
local argOpts = { offset=4, force=0, cube=4, help=0};
local offsets = {0, 0, 0, 0};
-- --------------------------------- --
-- MOVEMENT FUNCTIONS --
-- --------------------------------- --
--
-- Used to move the turtle in the specified direction. If something is blocking it, the turtle
-- will continue to attack and attempt to move until it completes.
--
-- @param dir - The direction or orientation to move the turtle
--
function move(dir, force)
if (force ~= nil and force == true) then
breakBlock(dir);
end
if (dir == FORWARD) then
while (not(turtle.forward())) do turtle.attack(); end;
elseif (dir == UP) then
while (not(turtle.up())) do turtle.attackUp(); end;
elseif (dir == DOWN) then
while (not(turtle.down())) do turtle.attackDown(); end;
else
dir = dir2Orient(dir);
turn(dir);
move(FORWARD,force);
end
end
--
-- Used to turn the turtle the specified direction or to the orientation provided
--
-- @param dir - The direction or orientation to turn the turtle
--
function turn(dir)
if (dir == RIGHT) then
turtle.turnRight();
orientation = (orientation + 1) % 4;
elseif (dir == LEFT) then
turtle.turnLeft();
orientation = (orientation + 3) % 4; -- mathematically, a left turn is the same as 3 right turns
elseif (dir == NORTH or dir == EAST or dir == SOUTH or dir == WEST) then
local numLeftTurns = ((4 + orientation) - dir) % 4;
if (numLeftTurns == 3) then -- it's shorter to do just one right turn for this case
turtle.turnRight();
else
for i=1,numLeftTurns do turtle.turnLeft(); end -- perform left turns to new orientation
end
orientation = dir;
end
end
--
-- Moves the turtle forward, right, then up the specified offset and then turns it to the orientation
--
function moveToOffset(force)
local oriForward = orientation;
local oriRight = (orientation + 1)%4;
-- move forward by the set amount
if (offsets[1] < 0) then
offsets[1] = offsets[1] * -1;
oriForward = (oriForward + 2) % 4;
end
for i=1,offsets[1] do
move(oriForward,force);
end
-- move to the right by the set amount
if (offsets[2] < 0) then
offsets[2] = offsets[2] * -1;
oriRight = (oriRight + 2) % 4;
end
for i=1,offsets[2] do
move(oriRight,force);
end
ori = UP;
if (offsets[3] < 0) then
offsets[3] = offsets[3] * -1;
ori = DOWN;
end
for i=1,offsets[3] do
move(ori,force);
end
turn(offsets[4]);
end
-- --------------------------------- --
-- UTILITY FUNCTIONS --
-- --------------------------------- --
--
-- Used to parse out the command line arguments using the structure provided and put them in a table.
--
-- @param args - The command line arguments that you want to parse
-- @param opts - The available options for command line arguments that will be used to determine
-- the structure of the return data.
-- @return Will return a boolean value specifying if it succeeded or not. If it is false then the
-- second return item will be a string stating why it failed. If the first return value
-- is true then the second item will be a table containing all the parsed items
--
function readInArguments(args, opts)
local i = 1;
local curr = nil;
-- initialize the return data
local rArgs = {};
rArgs.others = {};
while (i <= #args) do
if (curr == nil) then -- we aren't currently reading in a flag's sub-items
curr = args[i]; -- get the next item
if (tonumber(args[i])==nil and string.sub(curr,1,1) == "-") then -- it's a new flag
curr = string.sub(curr,2); -- removes the starting '-' character
if (opts[curr] == nil) then -- it's an unknown flag type
return false, ("Unknown flag " .. curr);
else -- initialize the flag's table
rArgs[curr] = {};
end
else -- it wasn't a flag, so it's just a generic left over argument
rArgs.others[#(rArgs.others) + 1] = curr;
curr = nil;
end
else -- the next item should be a sub-item under the current flag
if (tonumber(args[i])==nil and string.sub(args[i],1,1) == "-" ) then -- make sure it's not another flag already
return false, ("The " .. curr .. " flag requires " .. opts[curr] .. " additional arguments.");
else
rArgs[curr][#(rArgs[curr]) + 1] = args[i];
end
end
if (curr ~= nil and #(rArgs[curr]) >= opts[curr] ) then -- if we have read in all the sub-items for this flag
curr = nil;
end
i = i + 1;
end
if (curr ~= nil) then -- an issue occured here and we weren't able to read in all the arguments required
return false, ("The " .. curr .. " flag requires " .. opts[curr] .. " additional arguments.");
else
return true, rArgs;
end
end
--
-- Used to place a block in a given direction. Will move the
-- selected inventory slot if the currently selected on is empty.
--
-- @param The direction in which you want to place a block.
-- @param force - Boolean stating if blocks should be broken if they're in the way
--
function placeBlock(dir, force)
if (force ~= nil and force == true) then
breakBlock(dir);
end
while (turtle.getItemCount(turtle.getSelectedSlot()) == 0) do
turtle.select((turtle.getSelectedSlot() % 16) + 1);
end
if (dir == nil or dir == FORWARD) then
turtle.place();
elseif (dir == UP) then
turtle.placeUp();
elseif (dir == DOWN) then
turtle.placeDown();
else
end
end
--
-- Used to destroy a block in a given direction/orientation.
--
-- @param dir - The direction or orientation of the block to break
--
function breakBlock(dir)
if (dir == FORWARD) then
turtle.dig();
elseif (dir == UP) then
turtle.digUp();
elseif (dir == DOWN) then
turtle.digDown();
else
dir = dir2Orient(dir);
turn(dir);
turtle.dig();
end
end
--
-- Converts a relative direction to an orientation value. If the value is not a direction then it
-- just returns the direction value that was passed in.
--
-- @param dir - The direction to convert into an orientation
-- @return Returns an orientation value
--
function dir2Orient(dir)
if (dir == FORWARD) then
return orientation;
elseif (dir == BACK) then
return ((orientation + 2) % 4);
elseif (dir == LEFT) then
return ((orientation + 3) % 4);
elseif (dir == RIGHT) then
return ((orientation + 1) % 4);
else
return dir;
end
end
--
-- Used to get a count of all the items in the turtle's inventory
--
-- @return Returns the total number of items in turtle's inventory
--
function countAllBlocks()
local counter = 0;
for i=1,16 do
counter = counter + turtle.getItemCount(i);
end
return counter;
end
-- --------------------------------- --
-- BUILDING FUNCTIONS --
-- --------------------------------- --
--
-- Used to perform some prechecks and build a cube structure
--
-- @param args - The table of arguments that was parsed for the cube flag
-- @param force - Boolean stating if blocks should be broken if they're in the way
--
function buildCube(args,force)
-- check all the command line arguments for the cube structure
if #args ~= 4 then
print("You must pass in true/false to specify if the cube is filled as well as a length Forward, Right, and Up to build.");
print("Usage: build cube <filled> <forward> <right> <up>");
return;
end
-- get all the lengths for the cube
local lengthForward = tonumber(args[2]);
local lengthRight = tonumber(args[3]);
local lengthUp = tonumber(args[4]);
local blocksRequired = 0;
local buildFunction = nil;
-- if one of the dimensions is only 1 block long then default to filled
if (lengthForward == 1 or lengthRight == 1 or lengthUp ==1) then
args[1] = "true";
end
-- check if we're doing a filled cube or hollow cube
if args[1] == "true" then -- filled cube
blocksRequired = lengthForward * lengthRight * lengthUp;
buildFunction = buildFilledCube;
--buildFilledCube(lengthForward, lengthRight, lengthUp);
else -- hollow cube
blocksRequired = (lengthForward * lengthRight * lengthUp) - ((lengthForward-2) * (lengthRight-2) * (lengthUp-2));
buildFunction = buildHollowCube;
--buildHollowCube(lengthForward, lengthRight, lengthUp);
end
-- make sure we have enough blocks and fuel
if (blocksRequired > countAllBlocks()) then
print("Not enough blocks in the turtle inventory to build the desired structure.");
print(blocksRequired .. " blocks required.");
return;
elseif ( (blocksRequired + offsets[1] + offsets[2] + offsets[3] + 1) > turtle.getFuelLevel()) then
print("Not enough fuel for the required operation.");
print((blocksRequired + offsets[1] + offsets[2] + offsets[3] + 1) .. " fuel required.");
return;
end
moveToOffset(force);
buildFunction(lengthForward, lengthRight, lengthUp, force);
end
--
-- Used to build a filled cube
--
-- @param x - The number of blocks North of the turtle to build
-- @param y - The number of blocks East of the turtle to build
-- @param z - The number of blocks Up from the turtle to build
-- @param force - Boolean stating if blocks should be broken if they're in the way
--
function buildFilledCube(x, y, z, force)
local layerDirection = EAST;
for k=1,z do
move(UP,force);
for j=1,y do
for i=1,x do
placeBlock(DOWN,force);
if (i ~= x) then move(FORWARD,force); end
end
local nextOrientation = (orientation + 2) % 4; -- turn 180 degrees
if (j ~= y) then
turn(layerDirection);
move(FORWARD,force);
end
turn(nextOrientation);
end
layerDirection = (layerDirection + 2) % 4; -- we'll be building the next layer moving the other way
end
end
--
-- Used to build a hollow cube
--
-- @param x - The number of blocks North of the turtle to build
-- @param y - The number of blocks East of the turtle to build
-- @param z - The number of blocks Up from the turtle to build
-- @param force - Boolean stating if blocks should be broken if they're in the way
--
function buildHollowCube(x, y, z, force)
local layerDirection = EAST;
-- do the base layer
move(UP,force);
for j=1,y do
for i=1,x do
placeBlock(DOWN,force);
if (i ~= x) then move(FORWARD,force); end
end
local nextOrientation = (orientation + 2) % 4; -- turn 180 degrees
if (j ~= y) then
turn(layerDirection);
move(FORWARD,force);
end
turn(nextOrientation);
end -- the base layer loop
layerDirection = (layerDirection + 2) % 4; -- we'll be building the top layer moving the other way
-- do the walls
for k=1,(z-2) do
move(UP,force);
-- each layer is two L shapes of the two side lengths
local turnDirection = RIGHT;
if ( (y%2) == 0) then turnDirection = LEFT; end
for h=1,2 do
-- do a wall of the first length
for i=1,(x-1) do
placeBlock(DOWN,force);
move(FORWARD,force);
end
turn(turnDirection);
-- do a wall of the second length
for j=1,(y-1) do
placeBlock(DOWN,force);
move(FORWARD,force);
end
turn(turnDirection);
end
end -- the walls loop
-- do the upper layer
move(UP,force);
for j=1,y do
for i=1,x do
placeBlock(DOWN,force);
if (i ~= x) then move(FORWARD,force); end
end
local nextOrientation = (orientation + 2) % 4; -- turn 180 degrees
if (j ~= y) then
turn(layerDirection);
move(FORWARD,force);
end
turn(nextOrientation);
end -- the upper layer loop
end
-- --------------------------------- --
-- STARTING THE PROGRAM... --
-- --------------------------------- --
term.clear();
term.setCursorPos(1,1);
turtle.select(1);
if #tArgs == 0 then
print("Usage: build <structure> [options]");
return;
end
ok, arguments = readInArguments(tArgs,argOpts);
if (not ok) then -- something went wrong when trying to parse the arguments
print(arguments);
return;
end
local forceFlag = false;
if (arguments.force ~= nil) then
forceFlag = true;
ok, message = turtle.digUp();
if (not ok and message=="No tool to dig with") then
print("Cannot use the -force flag because there is no tool equiped to dig with.");
return;
end
end
if (arguments.offset ~= nil) then
for i=1,4 do
offsets[i] = tonumber(arguments.offset[i]);
end
end
if (arguments.help ~= nil) then
print("Available structures: cube");
return;
elseif (arguments.cube ~= nil) then
buildCube(arguments.cube,forceFlag);
end