View difference between Paste ID: 8ec8JLaN and vF48S0mZ
SHOW: | | - or go back to the newest paste.
1
--roadBuilder
2
3
currentFacing = 0
4
currentlySelectedSlot = 1
5
fuelLevelToRefuelAt = 5
6
initBlocksCount = 0
7
minSlot = 1
8
maxSlot = 16
9
10
11
currentY = 0
12
13
args = { ... }
14
15
minimumDown = 2
16
17
BLOCK_PILLAR = 1
18
BLOCK_GLASS = 3
19
BLOCK_WALL = 1
20
BLOCK_WALLSTAIRS = 2
21
BLOCK_LAMP = 4
22
23-
BLOCK_FENCE = 3
23+
24-
BLOCK_TORCH = 4
24+
  if #args > 0 then
25
    local length = tonumber(args[1])
26
    initBlocks()    
27-
  if #args > 1 then
27+
	for i=1,length,1 do
28
	  makePart()   
29-
    local roadType = args[2]  
29+
	end
30
  else
31-
    for i=1,length,1 do
31+
    print("Usage: road <length>")
32-
	  currentY = 0
32+
33-
      print("Building part: "..i.." of "..length);
33+
34-
      if roadType=="tun" then
34+
35-
        tun_makePart()   
35+
function makePart()
36-
      elseif roadType=="optun" then
36+
37-
        optun_makePart()
37+
    case(false)
38-
      elseif roadType=="village" then
38+
39-
        village_makePart()
39+
40-
      elseif roadType=="susp" then
40+
41-
        susp_makePart()
41+
  pillar()
42-
      else
42+
43-
        print("Avail types: tun,optun,susp,village")       
43+
44
  turnLeft()
45
  go("forward",true)
46
  turnLeft()
47
  turnLeft()
48-
    print("Usage: road <length> [type=tun|optun|susp|village]")
48+
  case(false)
49
  case(true)
50
  case(false)
51
  for i=1,3,1 do
52-
function susp_makePart()
52+
    case(false)
53-
  currentY = 0
53+
54
end
55
56
function case(lamp)
57-
  for i=1,6 do
57+
58
    replaceBlock(BLOCK_LAMP,"down")
59-
	go("forward",true)
59+
60
    replaceBlock(BLOCK_WALL,"down")
61
  end	
62
  turnRight()
63
  go("forward",true)	
64
  replaceBlock(BLOCK_WALL,"down")
65-
  for i=1,6 do
65+
66-
    replaceBlock(BLOCK_FENCE,"down")
66+
67-
	go("forward",true)
67+
68
  replaceBlock(BLOCK_WALL,"down")
69-
  replaceBlock(BLOCK_FENCE,"down")
69+
70
  go("down",true)
71
  go("down",true)
72
  turnRight()
73-
  for i=1,6 do
73+
74
  go("down",true)
75-
	go("forward",true)
75+
76
  replaceBlock(BLOCK_WALLSTAIRS,"up")
77
  turnRight()
78
  turnRight()
79
  go("forward",true)	  
80
  turnRight()
81
  turnRight()
82-
  for i=1,6 do    
82+
83-
    if i==2 or i==6 then
83+
84-
	  replaceBlock(BLOCK_TORCH,"down")
84+
85
  go("forward",true)
86-
	if i==4 then
86+
87-
	  replaceBlock(BLOCK_FENCE,"down")
87+
88-
	end  
88+
89-
	go("forward",true)
89+
90
  go("up",true)	
91
  replaceBlock(BLOCK_WALLSTAIRS,"down")
92
  go("forward",true)	
93
  replaceBlock(BLOCK_WALL,"down")
94-
  goX(3,"forward",true)
94+
95-
  replaceBlock(BLOCK_FENCE,"down")
95+
96
  go("forward",true)	
97-
  replaceBlock(BLOCK_FENCE,"down")
97+
98
  go("forward",true)	
99
  go("forward",true)	
100
  turnRight()
101
  turnRight()
102
  go("down",true)	
103
  replaceBlock(BLOCK_WALLSTAIRS,"forward")
104
  go("down",true)	
105
  replaceBlock(BLOCK_WALL,"forward")
106
  go("down",true)	
107
  replaceBlock(BLOCK_GLASS,"forward")
108
  go("down",true)	
109-
  goX(3,"down",true)
109+
110
  go("down",true)	
111
  go("forward",true)	
112
  replaceBlock(BLOCK_WALL,"forward")
113
  turnRight()
114
  turnRight()
115
  go("forward",true)	
116
  turnRight()
117
  turnRight()
118-
  while currentY < (0 + 2) do
118+
119
  go("forward",true)	
120-
    replaceBlock(BLOCK_PILLAR,"down")
120+
121-
    currentY = currentY + 1
121+
122
  turnLeft()
123
  go("forward",true)	
124
  go("up",true)	
125
  turnRight()    
126
  go("forward",true)	
127
  go("up",true)	
128
  turnRight()  
129
  go("forward",true)	
130
  go("forward",true)	
131
  go("forward",true)	  
132
  turnLeft()
133
end
134
135
function pillar()
136-
  goX(4,"forward",true)
136+
137
    currentY  = currentY - 1
138
    go("down",true)
139-
  goX(2,"forward",true)
139+
140
  repeat
141-
  --next
141+
142
    go("down",true)
143
  until not isEmpty("down")     
144-
function village_makePart()
144+
145
    go("up",true)	
146
	replaceBlock(BLOCK_PILLAR,"down")
147
	turnLeft()
148-
  village_pillar()
148+
149
	replaceBlock(BLOCK_PILLAR,"down")
150
	turnLeft()
151
	turnLeft()
152
	go("forward",true)	
153
	go("forward",true)	
154
	replaceBlock(BLOCK_PILLAR,"down")
155-
  for j=1,2 do
155+
156-
    for i=1,5,1 do
156+
157-
      replaceBlock(BLOCK_FENCE,"down")
157+
158-
      if i==3 then
158+
159-
        go("up",true)	
159+
160-
        replaceBlock(BLOCK_TORCH,"down")
160+
161-
        go("forward",true)
161+
162-
        go("down",true)		
162+
163-
      else
163+
164-
        go("forward",true)	
164+
165-
      end  
165+
166-
    end 
166+
167-
    turnLeft()
167+
168
  replaceBlock(BLOCK_PILLAR,"down")
169-
    if j==1 then     
169+
170-
	  go("forward",true)	
170+
171-
	  turnLeft()
171+
172-
	  go("forward",true)	
172+
173-
    else      
173+
174
  turnLeft()
175
  replaceBlock(BLOCK_PILLAR,"down")
176
  go("forward",true)
177
  replaceBlock(BLOCK_PILLAR,"down")
178
  go("forward",true)
179-
function optun_makePart()
179+
180
  turnLeft()
181-
    optun_case(false)
181+
182
  go("forward",true)
183
  turnRight()
184
  go("forward",true)
185-
  tun_pillar()
185+
186
187
188
189
function replaceBlock(id, direction)
190
  if not isEmpty(direction) then
191
    dig(direction)
192-
  optun_case(false)
192+
193-
  optun_case(true)
193+
194-
  optun_case(false)
194+
195
196-
    optun_case(false)
196+
197
  local slotNum = GetBlock(id)
198
  local result = false
199
  while slotNum == 0 do
200-
function tun_makePart()
200+
201
    awaitFix("I have no blocks as in selected slot")
202-
    tun_case(false)
202+
203
  end
204
  ensureFuel()	
205
  selectSlot(slotNum)
206-
  tun_pillar()
206+
207
	 result = turtle.placeUp()
208
  elseif direction=="down" then
209
	 result = turtle.placeDown()	  
210
  else 
211
	 result = turtle.place()
212
  end	 
213-
  tun_case(false)
213+
214-
  tun_case(true)
214+
215-
  tun_case(false)
215+
216
function GetBlock(id)
217-
    tun_case(false)
217+
218
    return id
219
  else 
220
    selectSlot(id)
221-
function optun_case(lamp)
221+
222
	  if turtle.getItemCount(i) > 0 then
223
        if turtle.compareTo(i) then
224
		  return i
225
		end
226
	  end
227
    end
228
  end	
229
  return 0
230
end
231
232
233
function ___initBlocks()
234
  if initBlocksCount==0 then
235
    selectSlot(1)
236
    while turtle.getItemCount(currentlySelectedSlot)~=0 do
237
      initBlocksCount = initBlocksCount + 1
238
      selectSlot(currentlySelectedSlot + 1)
239
    end
240
  end
241
  if initBlocksCount==0 then
242
    return false
243
  end
244
  minSlot = initBlocksCount + 1
245
  maxSlot = 15
246
end
247
248
249
function initBlocks()
250
  initBlocksCount = 4
251
  minSlot = initBlocksCount 
252
  maxSlot = 15
253
end
254
255
function isEmpty(side)
256
    local result = false
257
    if side=="up" then
258
	  result = not turtle.detectUp()
259
    elseif side=="down" then
260
	  result = not turtle.detectDown()
261
    else 
262
	  result = not turtle.detect()
263
    end	
264
    return result
265
end
266
267
--select slot
268
function selectSlot(x)
269
  if x<1 then
270
    x = 1
271
  elseif x > 16 then
272
    x = 16
273
  end
274
  turtle.select(x)
275
  currentlySelectedSlot = x
276
end
277
278
279
--Check fuel, refueling from inventory special slot or not
280
function ensureFuel() 
281
  -- Determine whether a refuel is required
282-
function tun_case(lamp)
282+
283
  if (fuelLevel ~= "unlimited") then
284
    if (fuelLevel < fuelLevelToRefuelAt) then
285
      -- Need to refuel
286
      selectSlot(16)
287
      local fuelItems = turtle.getItemCount(16) 
288
      -- Do we need to impact the emergency fuel to continue? (always  
289
      -- keep one fuel item in slot 16)
290
      if (fuelItems == 0) then
291
        awaitFix("I want more fuel!")
292
      elseif (fuelItems == 1) then
293
        --load fuel from another slot
294
	local fueled = false
295
	local fromSlot = initBlocksCount + 1
296
        for i=fromSlot,15,1 do
297
          selectSlot(i)
298
	  if (turtle.compareTo(16)) then
299
	    print("Refueling from not fuel slot: "+i)
300
	    turtle.refuel(1)
301
	    turtle.transferTo(16)			
302
	    fueled = true
303
          end
304
        end	
305
        if not fueled then
306
          print("completely out of fuel!")
307
        end
308
      else --fuelItems > 1
309
        print("Time to refueling!")
310
        turtle.refuel(1)
311
      end
312
    end
313
  end
314
end 
315
316
317
--turns
318
function turnFacing(facing) 
319
  while facing ~= currentFacing do
320
    turnRight()
321
  end
322
end
323
324
--turns
325
function turnLeft()
326
  ensureFuel()
327
  turtle.turnLeft()
328
  currentFacing = currentFacing - 1
329
  if currentFacing < 0 then
330
    currentFacing = 3
331
  end
332
end
333
334
--turns
335
function turnRight()
336
  ensureFuel()
337
  turtle.turnRight()
338
  currentFacing = currentFacing + 1
339
  if currentFacing >= 4 then
340
    currentFacing = 0
341
  end
342
end
343
344
-- BOOL dig direction with retries 
345
function dig(direction)
346
  local result = false
347
  local tries = 0
348
  selectSlot(1)
349
  while (not result) and (tries < 10) do
350
    ensureFuel()	
351
    if direction=="up" then
352
	    result = turtle.digUp()
353
    elseif direction=="down" then
354
	    result = turtle.digDown()	  
355
    else 
356
	    result = turtle.dig()
357
    end	  
358
    tries = tries + 1
359
  end
360
  if not result then
361
    print ("can't dig in "..direction)
362-
function tun_pillar()
362+
363
  return result
364
end
365
366
--go direction with breaking blocks and refueling
367
function go(direction, allowDig)
368
    if direction=="right" then
369
      turnRight()
370
      direction="forward"
371
    end
372
    if direction=="left" then
373
      turnLeft()
374
      direction="forward"
375
    end
376
    if direction=="forward" then       
377
       while turtle.detect() do
378
         if allowDig then
379
	       ensureFuel()
380
           turtle.dig()
381
         else 
382
           awaitFix("can't forward")
383
	     end
384
       end
385
	   
386
       while not turtle.forward() do
387
	     ensureFuel()
388
         turtle.dig()	   
389
	   end
390
    end
391
    if direction=="up" then
392
       while turtle.detectUp() do
393
         if allowDig then
394
		   ensureFuel()
395
           turtle.digUp()
396
         else 
397
           awaitFix("can't up")
398
	     end
399
       end	   
400
       while not turtle.up() do
401
	       ensureFuel()
402
	       turtle.digUp()	   
403
	   end
404
    end
405
    if direction=="down" then
406
       while turtle.detectDown() do
407
         if allowDig then
408
	       ensureFuel()
409
           turtle.digDown()
410
         else 
411
           awaitFix("can't down")
412
	     end
413
       end	   
414
        while not turtle.down() do
415-
function village_pillar()
415+
416
	       turtle.digDown()	   
417
	   end
418
    end
419
end
420
421
--wait for user action
422
function awaitFix(message)
423
    print("I have problem: " .. message)
424
    print("Please fix it and press Enter to continue")
425
    io.read()
426-
    replaceBlock(BLOCK_PILLAR,"down")
426+
427-
    currentY = currentY + 1
427+
428
429
430
main() --execute ALL