MrCervelo

Build Platform

Feb 3rd, 2013
559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.21 KB | None | 0 0
  1. function move(direction, count)
  2.   if ( direction == "forward" ) then
  3.     for i=1,count do
  4.       checkFuel()
  5.       while ( not turtle.forward() ) do
  6.         turtle.dig()
  7.         checkInv()
  8.       end
  9.     end
  10.   elseif ( direction == "up" ) then
  11.     for i=1,count do
  12.       checkFuel()
  13.       while ( not turtle.up() ) do
  14.         turtle.digUp()
  15.         checkInv()
  16.       end
  17.     end
  18.   elseif ( direction == "down" ) then
  19.     for i=1,count do
  20.       checkFuel()
  21.       while ( not turtle.down() ) do
  22.         turtle.digDown()
  23.         checkInv()
  24.       end
  25.     end
  26.   else
  27.     io.write("Unknown direction specified: " .. direction .. "\n")
  28.     os.exit()
  29.   end
  30. end
  31.  
  32. function turn(direction)
  33.   if ( direction == "left" ) then
  34.     turtle.turnLeft()
  35.   elseif ( direction == "right" ) then
  36.     turtle.turnRight()
  37.   else
  38.     io.write("Unknown turn Direction: " .. direction .. "\n")
  39.     os.exit()
  40.   end
  41. end
  42.  
  43. function checkInv()
  44.   full=true
  45.   for i=freeSlot,16 do
  46.     full=(turtle.getItemCount(i) > 0 and full)
  47.   end
  48.   return full
  49. end
  50.  
  51. function checkFuel()
  52.   if ( turtle.getFuelLevel() <= 1 ) then
  53.     io.write("The turtle is very low on Fuel. Please place fuel in slot " .. freeSlot .. "\n")
  54.     turtle.select(freeSlot)
  55.     while ( turtle.getFuelLevel() <= 1 ) do
  56.       turtle.refuel()
  57.       sleep(1)
  58.     end
  59.   end
  60. end
  61.  
  62. function getSlot()
  63.   if ( turtle.getItemCount(currentSlot) > 0 ) then
  64.     return
  65.   else
  66.     for i=1,#subs do
  67.       if ( turtle.getItemCount(subs[i]) > 0 ) then
  68.         currentSlot=i
  69.         return
  70.       end
  71.     end
  72.     io.write("There are no blocks available for building. Please place blocks in one of the following slots\n")
  73.     for i=1,#subs do
  74.       io.write(subs[i] .. "\n")
  75.     end
  76.     while ( true ) do
  77.       for i=1,#subs do
  78.         if ( turtle.getItemCount(subs[i]) > 0 ) then
  79.           currentSlot=i
  80.           return
  81.         end
  82.       end
  83.       sleep(1)
  84.     end
  85.   end
  86. end
  87.  
  88. function placeBlock()
  89.   getSlot()
  90.   turtle.select(currentSlot)
  91.   if ( not turtle.compareDown() ) then
  92.     turtle.digDown()
  93.     turtle.placeDown()
  94.   end
  95.   turtle.select(freeSlot)
  96. end
  97.  
  98. function checkBlocks(requiredNumber)
  99.   j=2
  100.   subs={}
  101.   subs[1]=1
  102.   turtle.select(1)
  103.   if ( turtle.getItemCount(1) > 0 ) then
  104.     blockCount=turtle.getItemCount(1)
  105.     for i=2,16 do
  106.       if ( turtle.compareTo(i) ) then
  107.         subs[j]=i
  108.         j=j+1
  109.         blockCount = blockCount + turtle.getItemCount(i)
  110.       else
  111.         if ( freeSlot == 0 ) then
  112.           freeSlot=i
  113.         end
  114.       end
  115.     end
  116.   else
  117.     io.write("There are no blocks in slot 1. Please correct and run again\n")
  118.     os.exit()
  119.   end
  120.   if ( blocksReq > blockCount ) then
  121.     io.write("There are insufficient blocks in subsitute slots, a refill will be required\n")
  122.   end
  123. end
  124.  
  125.  
  126. args={ ... }
  127. length=tonumber(args[1])
  128. width=tonumber(args[2])
  129. freeSlot=0
  130. currentSlot=1
  131. blocksReq=length*width
  132. checkBlocks()
  133. turnDirection="right"
  134. for i=1,width do
  135.   for j=1,length do
  136.     placeBlock()
  137.     if ( j ~= length ) then
  138.       move("forward",1)
  139.     end
  140.   end
  141.   turn(turnDirection)
  142.   move("forward", 1)
  143.   turn(turnDirection)
  144.   if ( i % 2 == 1 ) then
  145.     turnDirection="left"
  146.   else
  147.     turnDirection="right"
  148.   end
  149. end
Advertisement
Add Comment
Please, Sign In to add comment