Advertisement
Avevad

Roboexplore

Sep 27th, 2018
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.04 KB | None | 0 0
  1. local comp = require("component")
  2. local robot = require("robot")
  3. local tun = comp.tunnel
  4. local explored = {}
  5. local dirX = {0,1,0,-1}
  6. local dirY = {-1,0,1,0}
  7.  
  8. local function getOff(dir, addDir)
  9.   local resDir = (dir + addDir) % 4
  10.   return dirX[resDir + 1], dirY[resDir+1]
  11. end
  12.  
  13. local function isExplored(x, y)
  14.   if explored[x] == nil then
  15.     return false
  16.   end
  17.   return explored[x][y] == true
  18. end
  19.  
  20. local function setExplored(x, y)
  21.   if explored[x] == nil then
  22.     explored[x] = {}
  23.   end
  24.   explored[x][y] = true
  25. end
  26.  
  27. local function sendCmd(cmd, x, y)
  28.   tun.send(cmd, x, y)
  29. end
  30.  
  31. local function explore(x, y, dir)
  32.   setExplored(x,y)
  33.   sendCmd("in", x, y)
  34.   for cd = 0, 3 do
  35.     local xo, yo = getOff(dir, cd)
  36.     if robot.detect() then
  37.       sendCmd("block", x + xo, y + yo)
  38.     else
  39.       if not isExplored(x + xo, y + yo) then
  40.         robot.forward()
  41.         explore(x + xo, y + yo, (dir + cd)%4)
  42.       end
  43.     end
  44.     robot.turnRight()
  45.   end
  46.   sendCmd("out", x , y)
  47.   robot.back()
  48. end
  49.  
  50. explore(0,0,0)
  51. sendCmd("end", 0, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement