Advertisement
Guest User

GPSa905

a guest
Oct 20th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.82 KB | None | 0 0
  1. if posx == nil then posx = 1 end
  2. if posy == nil then posy = 0 end
  3.  
  4. if fieldx == nil then fieldx = 9 end
  5. if fieldy == nil then fieldy = 9 end
  6.  
  7. if wait == nil then wait = 5 end
  8. if waitafter == nil then waitafter = true end
  9.  
  10. function coord()
  11. print("Position: "..posx.." / "..posy)
  12. end
  13.  
  14. function iposx()
  15. posx = posx + 1
  16. end
  17.  
  18. function iposy()
  19. posy = posy + 1
  20. end
  21.  
  22. function dposx()
  23. posx = posx - 1
  24. end
  25.  
  26. function dposy()
  27. posy = posy - 1
  28. end
  29.  
  30. function mine()
  31.     print("doing stuff here")
  32. end
  33.  
  34. function move(dx, dy)
  35. coord()
  36.     moveDirX(posx, dx)
  37.     moveDirY(posy, dy)
  38. coord()
  39. end
  40.  
  41. function moveDirX(ax, dx)
  42.     if ax == 0 then step(dx, 0) else
  43.         delta = ax-dx
  44.         step(delta, 0)
  45.     end
  46. end
  47.  
  48. function moveDirY(ay, dy)
  49.     if ay == 0 then step(dy, 1) else
  50.         delta = ay-dy
  51.         step(delta, 1)
  52.     end
  53. end
  54.  
  55. function betrag(zahl1)
  56.     if zahl1 < 0 then return zahl1 * -1
  57.     else return zahl1
  58.     end
  59. end
  60.  
  61. function step(count, axis)
  62.     if axis == 0 then
  63.         if count > 0 then
  64.             moveLeft(betrag(count))
  65.             return true
  66.         elseif count < 0 then
  67.             moveRight(betrag(count))
  68.             return true
  69.         end
  70.     elseif axis == 1 then
  71.         if count > 0 then
  72.             moveForward(betrag(count))
  73.             return true
  74.         elseif count < 0 then
  75.             moveBack(betrag(count))
  76.             return true
  77.         end
  78.     end
  79.    
  80. end
  81.  
  82. function moveRight(ci)
  83.     turtle.turnRight(1)
  84.         for i=1, ci do
  85.             print("Moving right. New: "..(posx+1).." / "..posy)
  86.             if not turtle.detect() then
  87.                 turtle.forward()
  88.                 iposx()
  89.             else print("I crashed right!")
  90.             end
  91.         end
  92.     turtle.turnLeft(1)
  93. end
  94.  
  95. function moveLeft(ci)
  96.     turtle.turnLeft(1)
  97.         for i=1, ci do
  98.             print("Moving left. New: "..(posx-1).." / "..posy)
  99.             if not turtle.detect() then
  100.                 turtle.forward()
  101.                 dposx()
  102.             else print("I crashed left!")
  103.             end
  104.         end
  105.     turtle.turnRight(1)
  106. end
  107.  
  108. function moveForward(ci)
  109.     for i=1, ci do
  110.         print("Moving forward. New: "..posx.." / "..(posy+1))
  111.         if not turtle.detect() then
  112.             turtle.forward()
  113.             iposy()
  114.         else print("I crashed forward!")
  115.         end
  116.     end
  117. end
  118.  
  119. function moveBack(ci)
  120.     turn180()
  121.     for i=1, ci do
  122.         print("Moving back. New: "..posx.." / "..(posy-1))
  123.         if not turtle.detect() then
  124.             turtle.forward()
  125.             dposy()
  126.         else print("I crashed backward!")
  127.         end
  128.     end
  129.     turn180()
  130. end
  131.  
  132. function turn180()
  133. turtle.turnRight()
  134. turtle.turnRight()
  135. end
  136.  
  137.  
  138. if not waitafter then sleep(wait) end
  139.  
  140. move(1,1)
  141. move(1,2)
  142. move(2,2)
  143. if waitafter then sleep(wait) end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement