Advertisement
AdslHouba

GPS et dessinez avec des turtles

Jan 12th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.35 KB | None | 0 0
  1. -- Episode : //www.youtube.com/watch?v=xI1Ov35cILU
  2. -- Explication : //www.youtube.com/watch?v=x5N5_0L0wI0
  3.  
  4. if os.getComputerLabel()==nil then
  5.     os.setComputerLabel("Noel "..os.getComputerID())
  6.     print("set up nom")
  7. end
  8. if fs.exists("config") then
  9.     print("charge config")
  10.     file=fs.open("config","r")
  11.     config= textutils.unserialize(file.readAll())
  12.     file.close()
  13.     print(textutils.serialize(config))
  14. else
  15.     print("creation config")
  16.     local x, y, z = gps.locate(5)
  17.     config={
  18.         x=math.floor(x),
  19.         y=math.floor(y),
  20.         z=math.floor(z)
  21.     }
  22.     file=fs.open("config","w")
  23.     file.write(textutils.serialize(config))
  24.     file.close()
  25. end
  26.  
  27. local compass=peripheral.find("compass")
  28.  
  29.  
  30.  
  31. print("Attente carburant")
  32. local event, p1 = os.pullEvent("turtle_inventory")
  33. turtle.refuel(1)
  34. print("ok")
  35.  
  36. local modem=peripheral.find("modem")
  37. modem.open(4242)
  38. event, side, frequency, replyFrequency, message, distance = os.pullEvent("modem_message")
  39.  
  40. while true do
  41.     local x, y, z = gps.locate(5)
  42.     x=math.floor(x)
  43.     z=math.floor(z)
  44.     y=math.floor(y)
  45.     direction=compass.getFacing()
  46.     if config.x==x and config.y==y and config.z==z then return end
  47.     if config.y~=y then
  48.         if config.y>y then
  49.             turtle.up()
  50.         else
  51.             turtle.down()
  52.         end
  53.     elseif config.x~=x then
  54.         print(direction)
  55.         if direction=='north' then -- z --         
  56.             if config.x>x then
  57.                 turtle.turnRight()
  58.             else
  59.                 turtle.turnLeft()
  60.             end
  61.         elseif direction=='east' then -- x ++
  62.             if config.x>x then
  63.                 turtle.forward()
  64.             else
  65.                 turtle.turnRight()
  66.                 turtle.turnRight()
  67.             end
  68.         elseif direction=='south' then -- z ++
  69.             if config.x>x then
  70.                 turtle.turnLeft()
  71.             else
  72.                 turtle.turnRight()
  73.             end
  74.         else -- x --
  75.             if config.x>x then
  76.                 turtle.turnRight()
  77.                 turtle.turnRight()
  78.             else
  79.                 turtle.forward()
  80.             end
  81.         end    
  82.     else
  83.         print(direction)
  84.         if direction=='north' then -- z --
  85.             if config.z<z then
  86.                 turtle.forward()
  87.             else
  88.                 turtle.turnRight()
  89.                 turtle.turnRight()
  90.             end
  91.         elseif direction=='east' then -- x ++
  92.             if config.z>z then
  93.                 turtle.turnRight()
  94.             else
  95.                 turtle.turnLeft()
  96.             end
  97.         elseif direction=='south' then -- z ++
  98.             if config.z<z then
  99.                 turtle.turnRight()
  100.                 turtle.turnRight()
  101.             else
  102.                 turtle.forward()
  103.             end
  104.         else -- x --
  105.             if config.z>z then
  106.                 turtle.turnLeft()
  107.             else
  108.                 turtle.turnRight()
  109.             end
  110.         end
  111.     end
  112. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement