Yourdoom

circleFloor

May 17th, 2013
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.48 KB | None | 0 0
  1. local tArgs = {...}
  2. local dim = tonumber( tArgs[1] )
  3. local startDir = tArgs[2] == "l"
  4.  
  5. local coord = { 0, 0, 0 }
  6. local dir = 0
  7.  
  8. coord[1] = math.ceil( -dim / 2 )
  9. coord[3] = math.ceil( -dim / 2 )
  10. local startCoord = { coord[1], coord[2], coord[3] }
  11.  
  12. if not startDir then coord[3] = -coord[3] end
  13.  
  14. function curMoveAxis()
  15.    if dir % 2 == 0 then
  16.       return 1
  17.    else
  18.       return 3
  19.    end
  20. end
  21. function curMoveDir()
  22.    if math.floor( dir / 2 ) % 2 == 0 then
  23.       return 1
  24.    else
  25.       return -1
  26.    end
  27. end
  28.  
  29. function forward()
  30.    while not turtle.forward() do sleep( 1 ) end
  31.  
  32.    local axis = curMoveAxis()
  33.    coord[ axis ] = coord[ axis ] + curMoveDir()
  34. end
  35. function backward()
  36.    while not turtle.back() do sleep( 1 ) end
  37.  
  38.    local axis = curMoveAxis()
  39.    coord[ axis ] = coord[ axis ] - curMoveDir()
  40. end
  41. function turnLeft()
  42.    turtle.turnLeft()
  43.    dir = ( dir + 1 ) % 4
  44. end
  45. function turnRight()
  46.    turtle.turnRight()
  47.    dir = ( dir - 1 ) % 4
  48. end
  49. function turn( _dir )
  50.    if _dir then
  51.       turnLeft()
  52.    else
  53.       turnRight()
  54.    end
  55. end
  56.  
  57. function moveToSlot( to, fromMin, fromMax )
  58.    if not fromMax then fromMax = 16 end
  59.  
  60.    for i = fromMin, fromMax do
  61.       if turtle.getItemCount( to ) == 64 then break end
  62.  
  63.       if not i == to and turtle.getItemCount( i ) > 0 then
  64.          turtle.select( i )
  65.          turtle.transferTo( to, 64 )
  66.       end
  67.    end
  68.  
  69.    turtle.select( to )
  70. end
  71.  
  72. function inRange()
  73.    return coord[1] * coord[1] + coord[3] * coord[3] < ( dim / 2 ) * ( dim / 2 )
  74. end
  75.  
  76. for i = 1, dim do
  77.    local hasPlaced = false
  78.    print("Nextline")
  79.  
  80.    while inRange() or not hasPlaced do
  81.       if inRange() then
  82.          --print( "!!", coord[1], coord[3] )
  83.          moveToSlot( 1, 2 )
  84.          turtle.placeDown()
  85.          hasPlaced = true
  86.       end
  87.  
  88.       forward()
  89.    end
  90.  
  91.    turn( startDir )
  92.    forward()
  93.    turn( startDir )
  94.  
  95.    while inRange() do
  96.       backward()
  97.    end
  98.  
  99.    startDir = not startDir
  100. end
  101.  
  102. function sign( t )
  103.    if t < 0 then return -1 else return 1 end
  104. end
  105. function stepsToPos( newCoord )
  106.    local axis = curMoveAxis()
  107.  
  108.    return ( coord[ axis ] - newCoord[ axis ] ) * curMoveDir()
  109. end
  110. function moveTo( newCoord )
  111.    for i = 1, 2 do
  112.       local axis = curMoveAxis()
  113.  
  114.       while stepsToPos( newCoord ) > 0 do
  115.          backward()
  116.       end
  117.       while stepsToPos( newCoord ) < 0 do
  118.          forward()
  119.       end
  120.  
  121.       if i == 1 then turn(true) end
  122.    end
  123.    turn(false)
  124. end
  125.  
  126. moveTo( startCoord )
Advertisement
Add Comment
Please, Sign In to add comment