Advertisement
ivan52

reactor.robot

Jun 27th, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.89 KB | None | 0 0
  1. local r = require('robot')
  2. local computer = require('computer')
  3. local component = require('component')
  4. local fs = require('filesystem')
  5. local modem = component.modem
  6. local inv = component.inventory_controller
  7. local event = require('event')
  8.  
  9. RoodList = {}
  10. OthodList = {}
  11. FuelList = {}
  12.  
  13.  
  14.  
  15. modem.open(8)
  16. local friend
  17. local fs = io.open("friendAdres", "r")
  18. if not fs then
  19.     local SECRET_KEY_INPUT = "lkwejbhvlkwejbkjwerbfjwhvbjkehfvbejbwejb"
  20.  
  21.     while true do
  22.         local _, _, sender, _, _, message = event.pull("modem_message")
  23.         if message == SECRET_KEY_INPUT then
  24.             friend = sender
  25.             break
  26.         end
  27.     end
  28.     local SECRET_KEY_OUTPUT = "wqerfwevwegwergverbertgbvertesger"
  29.     os.sleep(2)
  30.     modem.send(friend, 8, SECRET_KEY_OUTPUT)
  31.     fs = io.open("friendAdres", "w")
  32.     fs:write(friend)
  33.     fs:close()
  34. else
  35.     friend = fs:read()
  36.     fs:close()
  37. end
  38.  
  39. --[[os.sleep(2)
  40. modem.send(friend, 8, "hello")
  41. --]]
  42.  
  43. -- ============================ NAVIGATION =============================--
  44. loc = {x=0, y=0, z=0, b=0}
  45.  
  46. function forward()
  47.     attempt = 0
  48.     while r.detect() do
  49.         r.swing()
  50.         attempt = attempt + 1
  51.         if attempt > 20 then
  52.             snos(3)
  53.             attempt = 0
  54.         end
  55.     end
  56.     while not r.forward() do r.swing() end
  57.     if loc.b == 0 then loc.y = loc.y+1
  58.     elseif loc.b == 1 then loc.x = loc.x+1
  59.     elseif loc.b == 2 then loc.y = loc.y-1
  60.     else loc.x = loc.x-1 end
  61.     return true
  62. end
  63. function back()
  64.     if not r.back() then
  65.         r.turnAround()
  66.         while not r.forward() do
  67.             r.swing()
  68.         end
  69.         r.turnAround()
  70.     end
  71.     if loc.b == 0 then loc.y = loc.y-1
  72.     elseif loc.b == 1 then loc.x = loc.x-1
  73.     elseif loc.b == 2 then loc.y = loc.y+1
  74.     else loc.x = loc.x+1 end
  75.     return true
  76. end
  77. function up()
  78.     attempt = 0
  79.     while r.detectUp() do
  80.         r.swingUp()
  81.         attempt = attempt +1
  82.         if attempt > 20 then
  83.             snos(1)
  84.             attempt = 0
  85.         end
  86.     end  
  87.     while not r.up() do r.swingUp() end
  88.     loc.z = loc.z+1
  89.     return true
  90. end
  91. function down()
  92.     attempt = 0
  93.     while r.detectDown() do
  94.         r.swingDown()
  95.         attempt = attempt + 1
  96.         if attempt > 20 then
  97.             snos(1)
  98.             attempt = 0
  99.         end  
  100.     end
  101.     while not r.down() do r.swingDown() end
  102.     loc.z = loc.z-1
  103.     return true
  104. end
  105. function turnRight()
  106.     loc.b = (loc.b+3)%4
  107.     r.turnRight()
  108. end
  109. function turnAround()
  110.     loc.b = (loc.b+2)%4
  111.     r.turnAround()
  112. end
  113. function turnLeft()
  114.     loc.b = (loc.b+1)%4
  115.     r.turnLeft()
  116. end
  117.  
  118. function povernut(side)
  119.     if side == 0 and loc.b == 3 then
  120.         turnLeft()
  121.     elseif side == 3 and loc.b == 0 then
  122.         turnRight()
  123.     else
  124.         while (loc.b < side) do
  125.             turnLeft()
  126.         end
  127.         while (loc.b > side) do
  128.             turnRight()
  129.         end
  130.     end
  131. end
  132. function goTo(x, y, z, b)
  133.     while z > loc.z do
  134.         up()
  135.     end
  136.     while z < loc.z do
  137.         down()
  138.     end
  139.     if x > loc.x then
  140.         povernut(1)
  141.     elseif x < loc.x then
  142.         povernut(3)
  143.     end
  144.     while x ~= loc.x do
  145.         forward()
  146.     end
  147.     if y > loc.y then
  148.         povernut(0)
  149.     elseif y < loc.y then
  150.         povernut(2)
  151.     end
  152.     while y ~= loc.y do
  153.         forward()
  154.     end
  155.     povernut(b)
  156. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement