Advertisement
Guest User

LayFloor

a guest
Feb 26th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.29 KB | None | 0 0
  1. print("Floor Layer 0.1")
  2. print("Lays floor between all walls")
  3. print("It will keep going forward if")
  4. print("no walls are detected")
  5. print("")
  6. print("Fuel level:"..turtle.getFuelLevel())
  7. local DEBUG=false
  8. print("Currnet Position from home:")
  9. print("X="..x.." Y="..y.." Dir="..dir)
  10. print("Rest Home position? y/n")
  11. if (read()=="y") then
  12.   x=0
  13.   y=0
  14. end
  15. back=false
  16. finished=false
  17. --track direction facing
  18. --F=0 R=1 B=2 L=3
  19. dir=0
  20. local cx=0
  21. local cy=0
  22. local cdir=0
  23.  
  24. turtle.select(1)
  25. slot=1
  26.  
  27. function PlaceBlock()
  28.   if not turtle.detectDown() then    
  29.     if turtle.getItemCount(slot) > 0 then
  30.       turtle.placeDown()
  31.     else
  32.       stock=false  
  33.       for i=1,16 do
  34.         if turtle.getItemCount(i)>0 then
  35.           turtle.select(i)
  36.           slot=i
  37.           stock=true
  38.           turtle.placeDown()
  39.           break
  40.         end
  41.       end
  42.       if stock == false then
  43.         print("Calling returnHome()")
  44.         local cx=x
  45.         local cy=y
  46.         local cdir=dir
  47.         GoTo(0,0,0)
  48.         Restock()
  49.         if CheckStock() then
  50.           print("No Materials left")
  51.           exit()
  52.         end
  53.         GoTo(cx,cy,cdir)
  54.         Turtle.placeDown()
  55.       end
  56.     end
  57.   end
  58. end
  59.  
  60. function TurnLeft()
  61.   print("TurnLeft() Called")
  62.   DebugPosition()
  63.   turtle.turnLeft()
  64.   dir=(dir-1)%4
  65. end
  66.  
  67. function TurnRight()
  68.   DebugPosition()
  69.   turtle.turnRight()
  70.   dir=(dir+1)%4
  71. end
  72.  
  73. function MoveForward()
  74.  DebugPosition()
  75.  if turtle.forward() then
  76.    if dir==0 then
  77.      x=x+1
  78.    elseif dir==1 then
  79.      y=y+1
  80.    elseif dir==2 then
  81.      x=x-1
  82.    elseif dir==3 then
  83.      y=y-1
  84.    else
  85.      print("ERROR: Undefined direction")
  86.    end
  87.  end
  88. end
  89.  
  90. function Turn(MyDir)
  91.   while MyDir~=dir do
  92.     if ((dir-MyDir)%4)==1 then
  93.       TurnLeft()
  94.     else
  95.       TurnRight()
  96.     end
  97.   end
  98. end
  99.  
  100. function GoTo(gx,gy,gdir)
  101.   --print("Moving X")
  102.   if (gx-x) > 0 then
  103.     Turn(0)
  104.   elseif (gx-x) < 0 then
  105.     Turn(2)
  106.   end
  107.   while x~=gx do
  108.     MoveForward()
  109.   end
  110.  
  111.   --print("Moving Y")
  112.   if (gy-y) > 0 then
  113.     Turn(1)
  114.   elseif (gy-y) < 0 then
  115.     Turn(3)
  116.   end
  117.   while y~=gy do
  118.     MoveForward()
  119.   end
  120.   Turn(gdir)
  121. end    
  122.  
  123. function NextBlock()
  124.   if not turtle.detect() then
  125.    MoveForward()
  126.   else
  127.    if dir==0 then
  128.      TurnRight()
  129.      if turtle.detect() then
  130.        print("That was the last row")
  131.        finished=true
  132.      else
  133.        MoveForward()
  134.        TurnRight()
  135.      end
  136.    else
  137.      TurnLeft()
  138.      if turtle.detect() then
  139.        print("That was the last row")
  140.        finished=true
  141.      else
  142.        MoveForward()
  143.        TurnLeft()
  144.      end
  145.    end
  146.  end
  147. end
  148.  
  149. function Restock()
  150.   local result=true
  151.   while result do
  152.     result=turtle.suckUp()
  153.   end
  154.   print(result)
  155. end
  156.  
  157. function CheckStock()
  158.   for i=1, 16 do
  159.     if turtle.getItemCount()>0 then
  160.       stock=true
  161.       return true
  162.     else
  163.       stock=false
  164.       return false
  165.     end
  166.   end    
  167. end
  168. function DebugPosition()
  169.   if DEBUG==true then
  170.     print("x="..x.." y="..y.." dir="..dir)
  171.   end
  172. end
  173.  
  174. Restock()
  175.  
  176. while ((finished==false) and (turtle.getFuelLevel() > (x+y+5))) do
  177.   --print("Debug "..x.." "..y.." "..dir)
  178.   --DebugPosition()
  179.   --print("Call PlaceBlock()")
  180.   PlaceBlock()
  181.   --print("call NextBlock()")
  182.   NextBlock()
  183. end
  184.  
  185. GoTo(0,0,0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement