paramus

Cleaning robot [AP]

Jun 11th, 2026 (edited)
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.62 KB | Gaming | 0 0
  1. local detector = peripheral.find("environmentDetector")
  2. local location = {x=0,y=0,z=0}
  3. local facing = 0 --0=North, 1=East, 2=South, 3=West
  4.  
  5. --Parameters
  6. local radius = 15
  7. local base = {x=0,y=0,z=0}
  8.  
  9.  
  10. function CorrectRotation(expected)
  11.     if (facing~=expected) then
  12.         if (facing-1==expected) then
  13.             turtle.turnLeft()
  14.         else
  15.             for i=facing,expected,1 do
  16.                 turtle.turnRight()
  17.             end
  18.         end
  19.        
  20.     end
  21. end
  22.  
  23. function MoveDirection(direction, amount)
  24.     CorrectRotation(direction)
  25.     for i=1,amount,1 do
  26.         turtle.forward()
  27.         turtle.suck()
  28.     end
  29. end
  30.  
  31. function GoTo(x,y,z)
  32.     --stop right before arriving
  33.     while(true) do
  34.         if (x~=location.x) then
  35.             if (x>location.x) then
  36.                 MoveDirection(0, x-location.x)
  37.             else
  38.                 MoveDirection(2, location.x-x)
  39.             end
  40.         elseif (y~=location.y) then
  41.             if (y>location.y) then
  42.                 turtle.up()
  43.             else
  44.                 turtle.down()
  45.             end
  46.         elseif (z~=location.z) then
  47.             if (z>location.z) then
  48.                 MoveDirection(1, z-location.z)
  49.             else
  50.                 MoveDirection(3, location.z-z)
  51.             end
  52.         end
  53.     end
  54. end
  55.  
  56. while(true) do
  57.    
  58.     local entities = detector.scanEntities(radius)
  59.     if (entities ~= nil) then
  60.         for i,ent in ipairs(entities) do
  61.            
  62.             if (true) then
  63.                 GoTo(ent.x,ent.y,ent.z)
  64.                 GoTo(base.x, base.y, base.z)
  65.             end
  66.            
  67.         end
  68.     end
  69.    
  70.     sleep(2)
  71. end
Advertisement
Add Comment
Please, Sign In to add comment