Advertisement
Guest User

move.lua

a guest
Dec 16th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.56 KB | None | 0 0
  1. function h2xy(h)
  2.     if h == 0 then
  3.         return 0,1
  4.     elseif h == 1 then
  5.         return 1,0
  6.     elseif h == 2 then
  7.         return 0,-1
  8.     elseif h == 3 then
  9.         return -1,0
  10.     end
  11.     return 0,0
  12.    
  13. end
  14.  
  15. function from_to_heading (ch,th)
  16.     if(ch==th) then
  17.         return 0
  18.     end
  19.    if(math.abs(ch-th)==2) then
  20.         turtle.turnLeft()
  21.         turtle.turnLeft()
  22.     elseif (ch-th == -1 or ch-th == 3) then
  23.         turtle.turnRight()
  24.     else
  25.         turtle.turnLeft()
  26.     end
  27.  
  28. end
  29.  
  30. function move_towards (cx,cy,cheading,tx,ty )
  31.     moved = false    
  32.     if cx != tx then
  33.         if (cx>tx) then
  34.             from_to_heading(cheading,3)
  35.             cheading = 3
  36.             moved = turtle.forward()
  37.         elseif (cx<tx) then
  38.             from_to_heading(cheading,1)
  39.             cheading = 1
  40.             moved = turtle.forward()
  41.         end
  42.     elseif (cy != ty) then
  43.         if(cy>ty) then
  44.             from_to_heading(cheading,2)
  45.             cheading = 2
  46.             moved = turtle.forward()
  47.         elseif (cy<ty) then
  48.             from_to_heading(cheading,0)
  49.             cheading = 0
  50.             moved = turtle.forward()
  51.         end
  52.     end
  53.     dx,dy = h2xy(cheading)
  54.     if(moved) then
  55.         cx = cx+dx
  56.         cy = cy+dy        
  57.     end
  58.     return moved,cheading,cx,cy
  59. end          
  60.  
  61.  
  62. function move_from_to(cx,cy,cheading,tx,ty)
  63.     while cx!= tx or cy != ty do
  64.         moved,nheading, nx,ny = move_towards(cx,cy,cheading,tx,ty)
  65.         cx = nx
  66.         cy = ny
  67.         cheading = nheading
  68.     end
  69. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement