kLeeNex24

3d

Aug 8th, 2013
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.01 KB | None | 0 0
  1. pos = {
  2. x=0,
  3. y=0,
  4. z=0,
  5. facing = 0  --clockwise
  6. }
  7. Ydir = true
  8.  
  9. function Y()
  10.  Ydir = not Ydir
  11. end
  12. wasPlacedUp = 0
  13. wasPlacedDown = 0
  14.  
  15. function sendAlarm(text)
  16.  term.clear()
  17.  term.setCursorPos(1,1)
  18.  print(text)
  19.  print("try to resolve the issue and press Enter to continue")
  20.  read()
  21.  term.clear()
  22.  term.setCursorPos(1,1)
  23. end
  24.  
  25. function sortInv()
  26.  for i=1,16 do
  27.   itemCount = turtle.getItemCount(i)
  28.   if turtle.getItemSpace(i) ~= 0 then
  29.     for j=16,i+1,-1 do
  30.       turtle.select(i)
  31.       if turtle.getItemCount(j)~=0 and (turtle.getItemCount(i) == 0 or turtle.compareTo(j)) then
  32.         turtle.select(j)
  33.         turtle.transferTo(i)
  34.         if turtle.getItemSpace(i) == 0 then break end
  35.       end
  36.      end
  37.     end
  38.    if turtle.getItemCount(i) == 0 then break end
  39.   end
  40. end
  41.  
  42. function cantPlace(n)--side, 0=up, 1=down, 2=mid
  43.  side = ""
  44.  isBlocked=false
  45.  if n==0 then isBlocked = turtle.detectUp()
  46.    side = "up"
  47.  elseif n==1 then isBlocked = turtle.detectDown()
  48.    side = "down"
  49.  else isBlocked = turtle.detect()
  50.    side = "front" end
  51.  
  52.  if isBlocked == true then
  53.   sendAlarm("I cant place the block in " .. side .. " direction!")
  54.  else
  55.   sortInv()
  56.   turtle.select(1)
  57.   if turtle.getItemCount(1) == 0 then toChest() end
  58.  end
  59. end
  60.  
  61. function forward(n)
  62.  for i=1,n do
  63.   turtle.forward()
  64.   if pos.facing == 0 then pos.x = pos.x + 1
  65.   elseif pos.facing == 1 then pos.y = pos.y + 1
  66.   elseif pos.facing == 2 then pos.x = pos.x - 1
  67.   else pos.y = pos.y - 1 end
  68.  end
  69. end
  70.  
  71. function up(n)
  72.  print("up("..n..")")
  73.  for i=1,n do
  74.   print("up()")
  75.   turtle.up()
  76.   pos.z = pos.z + 1
  77.  end
  78. end
  79.  
  80. function down(n)
  81.  for i=1,n do
  82.   turtle.down()
  83.   pos.z = pos.z - 1
  84.  end
  85. end
  86.  
  87. function turnLeft()
  88.  turtle.turnLeft()
  89.  pos.facing = pos.facing - 1
  90.  if pos.facing < 0 then pos.facing = 3 end
  91. end
  92.  
  93. function turnRight()
  94.  turtle.turnRight()
  95.  pos.facing = pos.facing + 1
  96.  if pos.facing > 3 then pos.facing = 0 end
  97. end
  98.  
  99. function placeUp()
  100.  if turtle.placeUp() == false then cantPlace(0) turtle.placeUp() end
  101. end
  102.  
  103. function placeDown()
  104.  if turtle.placeDown() == false then cantPlace(1) turtle.placeDown() end
  105. end
  106.  
  107. function place()
  108.  if turtle.place() == false then cantPlace(2) turtle.place() end
  109. end
  110.  
  111. function placeMid()
  112.  forward(1)
  113.  turnLeft()
  114.  turnLeft()
  115.  place()
  116. end
  117.  
  118. function setFacing(n)
  119.  k = n - pos.facing
  120.  if k ~= 0 then
  121.   if k < 0 then k = k + 4 end
  122.   if k < 3 then
  123.    while n ~= pos.facing do turnRight() end
  124.   else
  125.    while n ~= pos.facing do turnLeft() end
  126.   end
  127.  end
  128. end
  129.  
  130. function setFacingY()
  131.  if Ydir == true then setFacing(1) else setFacing(3) end
  132. end
  133.  
  134. function setFacingNotY()
  135.  if Ydir == false then setFacing(1) else setFacing(3) end
  136. end
  137.  
  138. function toChest()
  139.  oldPos = pos
  140.    if wasPlacedUp == 1 and wasPlacedMid == 0 then setFacingY() forward(1) end
  141.    up(2)  
  142.  
  143.    setFacing(3)
  144.    forward(pos.y)
  145.    setFacing(2)
  146.    forward(pos.x + 1)
  147.    down(pos.z - 1)
  148.    
  149.    useChest()
  150.    
  151.    up(oldPos.z+1)
  152.    setFacing(1)
  153.    if wasPlacedUp == 1 and wasPlacedMid == 0 then
  154.      if Ydir == true then forward(oldPos.y+1)
  155.      else forward(oldPos.y-1) end
  156.     else forward(oldPos.y) end
  157.    setFacing(4)
  158.    forward(oldPos.x+1)
  159.    down(2)
  160.    if wasPlacedUp == 1 and wasPlacedMid == 0 then setFacingNotY() forward(1) end
  161.    setFacing(oldPos.facing)
  162.  
  163. end
  164.  
  165. useChest()
  166. end
  167.  
  168. function g(x,y,z,_placeUp,_placeDown,_placeMid)
  169. --print("need to go["..x..","..y..","..z.."], im in ["..pos.x..","..pos.y..","..pos.z.."]")
  170.  if pos.z ~= z then
  171.    if wasPlacedUp == 1 and wasPlacedMid == 0 then forward(1) end
  172.    up(z-pos.z)
  173.   end
  174.  
  175.  if pos.x ~= x then
  176.   if pos.x < x then setFacing(0) else setFacing(2) end
  177.   forward(math.abs(x - pos.x))
  178.  end
  179.  
  180.  if pos.y ~= y then
  181.   if pos.y < y then setFacing(1) else setFacing(3) end
  182.   forward(math.abs(y - pos.y))
  183.  end
  184.  
  185.  setFacingY()
  186.  
  187.  if _placeUp == 1 then placeUp() end
  188.  if _placeDown == 1 then placeDown() end
  189.  if _placeMid == 1 then placeMid() end
  190.  
  191.   wasPlacedUp = _placeUp
  192.   wasPlacedMid = _placeMid
  193. end
  194.  
  195. --CODE
  196. placeUp()
Advertisement
Add Comment
Please, Sign In to add comment