Advertisement
natie3

move

Nov 7th, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function save()
  2.   f = fs.open("pos","w")
  3.   f.writeLine(x)
  4.   f.writeLine(y)
  5.   f.writeLine(z)
  6.   f.writeLine(facing)
  7.   f.close()
  8. end
  9.  
  10. function move(dir)
  11.   obstacle = false
  12.   if dir == "u" then
  13.     while not turtle.up() and not obstacle do
  14.       if turtle.detectUp() then
  15.         obstacle = true
  16.       else
  17.         os.sleep(0.5)
  18.       end
  19.     end
  20.     if not obstacle then
  21.       y = y + 1
  22.       save()
  23.     end
  24.     return not obstacle
  25.   elseif dir == "d" then
  26.     while not turtle.down() and not obstacle do
  27.       if turtle.detectDown() then
  28.         obstacle = true
  29.       else
  30.         os.sleep(0.5)
  31.       end
  32.     end
  33.     if not obstacle then
  34.       y = y - 1
  35.       save()
  36.     end
  37.     return not obstacle  
  38.   else
  39.     dir = tonumber(dir)
  40.     if facing - dir == -3 or facing - dir == 1 then
  41.       turtle.turnLeft()
  42.     elseif facing - dir == -2 or facing - dir == 2 then
  43.       back = true
  44.       if not turtle.back() then
  45.         turtle.turnLeft()
  46.         turtle.turnLeft()
  47.         back = false
  48.       end
  49.     elseif facing - dir == -1 or facing - dir == 3 then
  50.       turtle.turnRight()
  51.     end
  52.     if not back then
  53.       facing = dir
  54.       save()
  55.       while not turtle.forward() and not obstacle do
  56.         if turtle.detect() then
  57.           obstacle = true
  58.         else
  59.           os.sleep(0.5)
  60.         end
  61.       end
  62.     else
  63.       back = false
  64.     end
  65.     if not obstacle then
  66.       x, y, z = getCoords(x,y,z,tostring(dir))
  67.       save()
  68.     end
  69.     return not obstacle
  70.   end
  71. end  
  72.  
  73. function getCoords(x,y,z,dir)
  74.   if dir == "0" then
  75.     return x, y, (z+1)
  76.   elseif dir == "1" then
  77.     return (x-1), y, z
  78.   elseif dir == "2" then
  79.     return x, y, (z-1)
  80.   elseif dir == "3" then
  81.     return (x+1), y, z
  82.   elseif dir == "u" then
  83.     return x, (y+1), z
  84.   else
  85.     return x, (y-1), z
  86.   end
  87. end
  88.  
  89. -- initialize main
  90. modem = peripheral.wrap("right")
  91. f = fs.open("pos","r")
  92. x = tonumber(f.readLine())
  93. y = tonumber(f.readLine())
  94. z = tonumber(f.readLine())
  95. facing = tonumber(f.readLine())
  96. back = false
  97. pos = false
  98. eindx = 331
  99. eindy = 63
  100. eindz = 107
  101. id = 1234
  102. modem.open(7001)
  103.  
  104. while true do
  105.   event = { os.pullEvent("modem_message") }
  106.   data = textutils.unserialize(event[5])
  107.   if data.type == "moveAction" then
  108.     position = data.position
  109.     eindx = position.x
  110.     eindy = position.y
  111.     eindz = position.z
  112.     pos = false
  113.     while not pos do
  114.       request = textutils.serialize({ beginx = x, eindx = eindx, beginy = y, eindy = eindy, beginz = z, eindz = eindz, facing =  facing })
  115.       tabel = { type = "routeRequest", id = id, request = request }
  116.       table.insert(tabel,request)
  117.       modem.transmit(7000,7001,textutils.serialize(tabel))
  118.       event = { os.pullEvent("modem_message") }
  119.       print("received")
  120.       data = textutils.unserialize(event[5])
  121.       route = data.route
  122.       print(#route)
  123.       obstacle = false
  124.       i = 0
  125.       for k, v in pairs(route) do
  126.         write(v)
  127.       end
  128.       --io.read()
  129.       while i < #route and not obstacle do
  130.         i = i + 1
  131.         v = route[i]
  132.         if not move(v) then
  133.           tempx, tempy, tempz = getCoords(x,y,z,tostring(v))
  134.           info = { x = tempx, y = tempy, z = tempz }
  135.           tabel = { type = "addObstacle", id = id, info = info }
  136.           modem.transmit(7000,7001,textutils.serialize(tabel))
  137.           obstacle = true
  138.           print("obstacle")
  139.         end
  140.       end
  141.       if not obstacle then
  142.         pos = true
  143.       end
  144.     end
  145.   end
  146. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement