View difference between Paste ID: FGdrJNQx and Pw54zbg4
SHOW: | | - or go back to the newest paste.
1
local count=0
2
local slot =1	 
3
local count=0
4
local breakMode=true
5
local args = {...}
6
local version="0.8.5" 
7
     --				1					2				3				4					5
8
local ftbl= {
9
				{turtle.placeDown,	mdt,	turtle.digDown,	turtle.detectDown,	turtle.compareDown	},
10
				{turtle.place,		mft,	turtle.dig	,	turtle.detect	,	turtle.compare		},
11
				{turtle.placeUp,	mut,		turtle.digUp,	turtle.detectUp,	turtle.compareUp	}
12
				
13
			}
14
15
local tx,ty,tz=0,0,0
16
local face=2;			
17
local dirinc = { {0,-1},{1,0},{0,1},{-1,0} }
18
local function tlt()
19
turtle.turnLeft()
20
face=(face+1)%4
21
end
22
local function trt()
23
turtle.turnRight()
24
face=(face+4-1)%4
25
end
26
local function mut()
27
if not turtle.up() then return false end
28
tz=tz+1
29
return true
30
end
31
local function mdt()
32
if not turtle.down() then return false end
33
tz=tz-1
34
return true
35
end
36
37
38
local function mft()
39
local idx = (face)%4+1
40
if not turtle.forward() then return false end
41
tx=tx+dirinc[idx][1];
42
ty=ty+dirinc[idx][2];
43
return true
44
end
45
local function mbt()
46
local idx = (face+2)%4+1
47
if not turtle.back() then return false end
48
tx=tx+dirinc[idx][1];
49
ty=ty+dirinc[idx][2];
50
return true
51
end
52
53
		
54
local function ensureSlot()
55
--write("e")
56
    if count<=0 then
57
		slot=slot+1;
58
		if(slot>16) then slot=1 end
59
		count=turtle.getItemCount(slot)
60
		if count==0 then print("not enough blocks"); error() end
61
		turtle.select(slot)
62
    end
63
end
64
65
			
66
local function digAndThrow(dir)
67
	if dir== nil  then dir=0 end
68
	local dir2=dir+2
69
	local full=false;
70
	local inv={}
71
	for i=1,16 do 
72
		inv[i]=turtle.getItemCount(i)
73
		if inv==0 then
74
		turtle.select(i)
75
		break
76
		end 
77
		if(i==16) then 
78
			--print("failed to place block - not critical")
79
	--		return false
80
			full = true
81
		end
82
		
83
	end
84
	-- if all slots are 
85
	if not ftbl[dir2][3]() then turtle.select(slot);return false end
86
	if not full then 
87
		turtle.drop()
88
		turtle.select(slot)
89
		return true
90
	else
91
		for i=1,16 do 
92
				if inv[i]~=turtle.getItemCount(i) then
93
				   turtle.select(i)
94
				   turtle.drop(turtle.getItemCount(i)-inv[i])
95
				   turtle.select(slot)
96
				   return true
97
				end
98
			end
99
		end
100
	return true
101
end
102
--[[
103
function fCirc(x,r)
104
return math.sqrt(r*r-x*x)
105
end
106
107
function followCirclePath()
108
local i=1
109
while true do
110
111
112
end
113
end
114
]]--
115
function backSpecial()
116
	while not mbt() do 
117
		if breakMode then 
118
		trt();trt();
119
			if not digAndThrow(0) then 
120
				print("obsticle found:waiting...");sleep(1) 
121
			else 
122
				--return false
123
			end
124
		trt();trt();
125
		else
126
			print("obsticle found:waiting...");sleep(1) 
127
		end
128
	end
129
	return true
130
end
131
132
function upSpecial()
133
while not mut() do 
134
	if not digAndThrow(1) then 
135
		print("can't move up")
136
		sleep(1)
137
	end 
138
end
139
end
140
141
142
local function tp(dir)
143
	if dir== nil  then dir=0 end
144
	local dir2=dir+2
145
	
146
	if ftbl[dir2][1]() then
147
		count=count-1
148
		ensureSlot()
149
	else
150
		if  digAndThrow(dir) then 
151
			if ftbl[dir2][1]() then
152
				count=count-1
153
				ensureSlot()
154
			end
155
		else
156
			print("failed to place block - not critical")
157
		end
158
	end
159
end
160
161
local function findDims(inclusive)
162
local x,y=1,1
163
local xr,yr=0,0
164
if inclusive== nil then inclusive=true end
165
while mft() do
166
x=x+1
167
end
168
if inclusive then 
169
digAndThrow(0) 
170
mft()
171
x=x+1
172
end
173
xr=x
174
tlt()
175
while mft() do
176
y=y+1
177
end
178
if inclusive then 
179
digAndThrow(0) 
180
mft()
181
y=y+1
182
end
183
yr=y
184
while y>1 do mbt();y=y-1 end
185
trt()
186
while x>1 do mbt();x=x-1 end
187
188
return xr,yr
189
end
190
191
192
193
194
-------------------------------------------------------------
195
local function buildWallPolygon(ar,len,r,z,connected,startDiagonal)
196
local i=1
197
local fin=false;
198
local diagonal=false;
199
local turnleft=true
200
local firstz = true
201
local pb=0;
202
if connected== nil then connected=true end
203
if startDiagonal== nil then startDiagonal=false end
204
diagonal=startDiagonal
205
local lx=0
206
	zr=z %3 
207
	z=math.floor(z/3)
208
	if (zr~=0) then z=z+1 end
209
	local buildUp=true;
210
	local buildForward=true;
211
--print(ar[1].." "..len.." "..r.." "..z)
212
for k=1,z do
213
--write("z")
214
firstz=true
215
	if(k<z) then buildUp=true;buildForward=true;
216
	elseif zr==1 then   buildUp=false;buildForward=false;
217
	elseif zr==2 then   buildUp=false;buildForward=true; 
218
	else 				buildUp=true ;buildForward=true;
219
	end
220
	upSpecial()
221
	fin=false
222
	for l=1,r  do
223
		for i=1,len,2 do
224
		pb=0
225
--	write (i..">"..ar[i])
226
			lx=ar[i]
227
			--[[if(lx<0) then lx=-lx; turnleft=false 
228
			else 
229
				turnleft = true
230
			end
231
			--]]
232
			for x=1,lx do
233
		--	write("x")
234
			if not connected or not firstz then 
235
				tp(-1) 
236
				pb=pb+1
237
					if buildUp then
238
						tp(1)
239
					end
240
			end
241
				--write("b")
242
				backSpecial()
243
				if (not connected or not firstz) and buildForward then
244
			--	write("-")
245
					tp(0)
246
				end
247
				firstz=false
248
				if diagonal then
249
					--if pb>0 then
250
						if turnleft then trt() else tlt() end
251
						backSpecial()
252
						if not turnleft then trt() else tlt() end
253
						--tlt()
254
					--end
255
				end
256
			
257
		--		write(">"..x)
258
			end
259
			lx=ar[i+1]
260
			if lx<0 then turnleft= false; lx=-lx
261
			else
262
			turnleft=true
263
			end
264
			for i=1,lx do 
265
				if not diagonal then 
266
					if not turnleft then trt() else tlt() end
267
				end
268
				diagonal= not diagonal
269
			end
270
		--	print("out of x loop")
271
		if i==len and l==r then fin= true end
272
		--[[
273
			if not diagonal then 
274
			--tlt() 
275
			if not turnleft then trt() else tlt() end
276
			
277
			end
278
			diagonal=not diagonal;
279
		--]]
280
		end
281
282
	end
283
			
284
		tp(-1)
285
		
286
		if buildForward and j==lim then
287
				upSpecial()
288
				tp(-1)
289
		end
290
		if buildUp then
291
			if j~=lim then
292
				tp(1)
293
			else
294
				upSpecial()
295
				tp(-1)
296
			end
297
		end
298
--[[			if not octagon then 
299
		tlt()
300
		end--]]
301
		--backSpecial()
302
--		if buildForward and k~=z then
303
	--		tp(0)
304
	--	end
305
306
307
end
308
end
309
--
310
local function buildPlate(x,y)
311
	upSpecial()
312
	for j=1,y do
313
		for i=1,x-1 do
314
	        tp(-1)
315
		   backSpecial()
316
    	end
317
			tp(-1)
318
			if(j<y) then 
319
			if(j%2== 1) then tlt() else trt() end
320
	        backSpecial()
321
	        if(j%2== 1) then tlt() else trt() end
322
			end
323
            --turtle.back()
324
	end
325
	
326
		if x%2==1 then 
327
		trt();trt()
328
		else
329
		tlt()
330
		end
331
332
	end
333
-----------------------------------------------------------------------
334
print ("Rectangular "..version.. " room builder by Niseg")
335
	local sel=0
336
	local x1,y1,z1
337
	local argmode=false
338
339
340
341
	local fuel=turtle.getFuelLevel();
342
if(fuel<10) then 
343
	print(fuel.." fuel is probobly not enough ");
344
	print("put in more fuel and refuel all")
345
	print("then you can start the program")
346
	error()
347
end
348
--[[
349
	local foundfuel=false;
350
	for i=1,16 do 
351
		if not foundfuel and turtle.getItemCount(i)>0 then 
352
			trutle.select(i) 
353
			if turtle.refuel(turtle.getItemCount(i))  then foundfuel=true  end
354
		if foundfuel and i<16 then 
355
		--condense
356
		turtle.select(i+1)
357
		turtle.transferTo(i)
358
		end
359
	end
360
]]
361
362
	if #args==4 then 
363
		argmode=true
364
		local tempnum
365
		for i=1,4 do 
366
			tempnum=tonumber(args[i])
367
			if(tempnum==nil) then 
368
			print("invalid argument #"..i.."="..args[i])
369
			print("expecting a number")
370
			
371
			error()
372
			end
373
			args[i]=tempnum;
374
		end
375
		x1,y1,z1,sel=args[1],args[2],args[3],args[4]
376
	else
377
		print ("cmd line syntax(skip UI): ")
378
		print("build <length> <width> <height> <0-5>")
379
		--print("0 = walls only 1= adds floor 2= adds roof 3= adds both" )
380
		print("")
381
		--print("no arguments running UI")
382
		if #args~=0 then error() end
383
		end
384
385
	for i=1,16 do 
386
		if turtle.getItemCount(i)>0 then 
387
			turtle.select(i);
388
			slot=i
389
			count=turtle.getItemCount(slot)
390
			break 
391
			end 
392
	end
393
local arshape={}
394
--[[
395
local con=true
396
local rpt
397
local sd=false
398
if args[1]=="1" then con=false 
399
elseif args[1]=="2" then sd=true 
400
elseif args[1]=="3" then con=false;sd=true;
401
end
402
403
z1=tonumber(args[2])
404
rpt=tonumber(args[3])
405
for i=4,#args  do
406
arshape[i-3]=tonumber(args[i])
407
end
408
buildWallPolygon(arshape,#arshape,rpt,z1,con,sd )
409
--]]
410
--ttr();ttr();
411
	
412
--error()
413
if not argmode then
414
	--print("make sure the turtle is refueled")
415
	print("Load the blocks in contiguous slots")
416
	print("Place turtle at bottom right corner")
417
--	print("(builds in place)")
418
	print("Hold CTRL-T to terminate")
419
	local blockcount=0
420
	local constraint=0
421
	local floorcount=0
422
	local wallcount=0
423
424
425
	for i=1,16 do blockcount=blockcount + turtle.getItemCount(i) end
426
	print ("Block count= "..blockcount)
427
--	print("")
428
	repeat 
429
	print("0/-1 length or width will start auto detection")
430
	print("0 will build over the block -1 will build by it")
431
	write("Length(1st)/flat side? ")
432
    x1=tonumber(read())
433
	until x1~= nil;
434
    repeat 
435
	write("Width(2nd)/diagonal side? ")
436
    y1=tonumber(read())
437
	until y1~= nil;
438
end
439
if y1==0 or x1==0 then
440
print("finding room dimensions including markers")
441
if not argmode then
442
print("press enter to continue")
443
read()
444
end
445
x1,y1=findDims(true)
446
print("building "..x1.." long and "..y1.." wide" )
447
elseif y1==-1 or x1==-1 then
448
print("finding room dimensions excluding markers")
449
if not argmode then
450
print("press enter to continue")
451
read()
452
end
453
x1,y1=findDims(false)
454
print("building "..x1.." long and "..y1.." wide" )
455
end
456
	blockcount=0
457
	for i=1,16 do blockcount=blockcount + turtle.getItemCount(i) end
458
	wallcount=x1*y1 -(x1-2)*(y1-2)
459
	floorcount=x1*y1
460
	--local ox,oy
461
	--oy=math.floor(x1/3)
462
	--ox=x1-(oy*2)
463
	
464
--	local wc_oct=ox*4 +math.max(0,(oy-2))*4
465
	local wc_oct=x1*4 +math.max(0,(y1-2))*4
466
if not argmode then
467
	print ("Block count= "..blockcount)
468
	print("Cost per wall= "..wallcount.." floor/roof= "..floorcount)
469
	print("max heights:")
470
	write("Walls= " ..math.floor(blockcount/wallcount))
471
	write(" with floor/roof= "..math.max(0,math.floor((blockcount-floorcount)/wallcount)))
472
	print(" both= "..math.max(0,math.floor((blockcount-floorcount)/wallcount)))
473
	write("single wall= "..math.floor(blockcount/x1).." ")
474
	print("octagon ="..math.floor(blockcount/wc_oct))
475
    repeat 
476
	write("Height? ")
477
	z1=tonumber(read())
478
	until z1~= nil;
479
 end
480
	
481
	local fuel_cost;
482
if not argmode then
483
	repeat 	
484
	print("0  walls only")
485
	print("1  floor and walls")
486
	print("2  walls and roof ")
487
	print("3  floor walls and roof" )
488
	print("4  a single wall  ".."5  octagon " )
489
	print("6  hexagon  7 Parallelogram 8 digonal wall")
490
	print("9  cross 10 cross diagonal corner")
491
	print("11  cross start long side")
492
	sel=tonumber(read())
493
	until(sel~= nil and sel>=0 and sel<=11) 
494
end	
495
496
	if sel==0 then 
497
		constraint=z1*wallcount;
498
		fuel_cost=math.ceil(constraint/3)+z1+10;
499
	elseif sel==1 or sel==2 then 
500
		constraint=(z1-1)*wallcount +floorcount 
501
		fuel_cost=math.ceil((constraint-floorcount)/3)+z1+10+floorcount;
502
	elseif sel==3 then
503
		constraint=(z1-2)*wallcount +2*floorcount
504
		fuel_cost=math.ceil((constraint-floorcount)/3)+z1+10+2*floorcount;
505
	elseif sel== 4 then
506
		constraint=z1*x1;
507
		fuel_cost=math.ceil(constraint/3)+z1+10;
508
	--elseif sel==5 then 
509
	--y1=x1
510
	else
511
	   constraint=0;
512
	   fuel_cost=0;
513
	--[[
514
	local y2=math.floor(x1/3)
515
	local x2=x1-(y2*2)
516
	constraint=z1*(x2*4+y2*4);
517
	fuel_cost=math.ceil((constraint+y2*4)/3)+z1+10;]]--
518
	end
519
	print("min fuel cost = "..fuel_cost);
520
	if fuel<fuel_cost then print(fuel.." fuel is not enough!");
521
	error()
522
	end
523
	
524
	if(blockcount<constraint)  then 
525
		print("you will have to resupply the turtle while it's working")
526
		print (" needed blocks = "..(constraint-blockcount).." ( "..math.ceil((constraint-blockcount)/64).."~ stacks)" )
527
		if not argmode then	
528
				print(" press enter to start or hold CTRL-T to quit")
529
				read()
530
		end
531
	end
532
533
	
534
	--turtle.select(1)
535
	--- starts building
536
	trt();trt();
537
	if sel>=0 and sel<=3 then 
538
539
		if sel==1 or sel ==3 then 
540
			buildPlate(x1,y1)
541
			z1=z1-1
542
		end
543
	
544
		arshape[1]=x1-1
545
		arshape[2]=2
546
		arshape[3]=y1-1
547
		arshape[4]=2
548
		if z1>1 and (sel==2 or sel==3) then z1=z1-1 end
549
		if z1>0 then buildWallPolygon(arshape,4,2,z1,true,false) end
550
		if sel==2 or sel ==3 then 
551
			buildPlate(x1,y1)
552
		end
553
		
554
	elseif sel==4 then  -- wall
555
	arshape[1]=x1-1
556
	arshape[2]=4
557
	arshape[3]=0
558
	arshape[4]=0
559
	arshape[5]=0
560
	arshape[6]=0
561
	buildWallPolygon(arshape,2,1,z1,false)
562
	elseif sel==5 then -- octagon
563
	arshape[1]=x1-1
564
	arshape[2]=1
565
	arshape[3]=y1-1
566
	arshape[4]=1
567
	buildWallPolygon(arshape,4,4,z1)
568
	elseif sel==6 then  -- hexagon
569
	arshape[1]=x1-1
570
	arshape[2]=1
571
	arshape[3]=y1-1
572
	arshape[4]=2
573
	arshape[5]=y1-1
574
	arshape[6]=1
575
	buildWallPolygon(arshape,6,2,z1)
576
	
577
	elseif sel==7 then -- Parallelogram  
578
	arshape[1]=x1-1
579
	arshape[2]=2
580
	arshape[3]=y1-1
581
	arshape[4]=2
582
	buildWallPolygon(arshape,4,2,z1,true,true)
583
	
584
	elseif sel==8 then  -- diagonal wall
585
	arshape[1]=x1-1
586
	arshape[2]=4
587
	arshape[3]=0
588
	arshape[4]=0	
589
	arshape[5]=0
590
	arshape[6]=0
591
	buildWallPolygon(arshape,6,1,z1,false,true)
592
	elseif sel==9 then  -- cross
593
	arshape[1]=x1-1
594
	arshape[2]=-2
595
	arshape[3]=y1-1
596
	arshape[4]=2
597
	arshape[5]=y1-1
598
	arshape[6]=-2
599
	buildWallPolygon(arshape,6,4,z1,true,false)
600
	elseif sel==10 then  -- cross with diagonal corner
601
	arshape[1]=x1-2
602
	arshape[2]=-1
603
	arshape[3]=1
604
	arshape[4]=-1
605
	arshape[5]=y1-2
606
	arshape[6]=1
607
	arshape[7]=1
608
	arshape[8]=1
609
	arshape[9]=y1-2
610
	arshape[10]=-1
611
	arshape[11]=1
612
	arshape[12]=-1
613
	
614
	buildWallPolygon(arshape,12,4,z1,true,false)
615
	elseif sel==11 then  -- cross start long side
616
	arshape[1]=y1-1
617
	arshape[2]=2
618
	arshape[3]=x1-1
619
	arshape[4]=2
620
	arshape[5]=y1-1
621
	arshape[6]=-2
622
	buildWallPolygon(arshape,6,4,z1,true,false)
623
	else
624
	
625
	print("invalid shape")
626
	error()
627
	end
628
	trt();trt()
629
	print("done. cost was "..(fuel-turtle.getFuelLevel()).." fuel");
630
	print("Fuel left : "..turtle.getFuelLevel())
631
	print("position relative to start = "..tx..","..ty..","..tz)