Subsidedgibbon0

RC_Drone

Jan 27th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. print("ID?")
  2. ID = tonumber(read())
  3. x = 45
  4. y = 45
  5. xOff = 20
  6. yOff = 36
  7. dir = 0
  8. frontx = 0
  9. fronty = 1
  10.  
  11. function turn(s)
  12. if s == "Right" then
  13. turtle.turnRight()
  14. dir = dir + 1
  15. if dir > 3 then dir = 0 end
  16. end
  17. if s == "Left" then
  18. turtle.turnLeft()
  19. dir = dir - 1
  20. if dir < 0 then dir = 3 end
  21. end
  22. Updatedif()
  23. end
  24.  
  25. function Updatedif()
  26. if dir == 0 then frontx = 0 fronty = -1 end
  27. if dir == 1 then frontx = 1 fronty = 0 end
  28. if dir == 2 then frontx = 0 fronty = 1 end
  29. if dir == 3 then frontx = -1 fronty = 0 end
  30. end
  31.  
  32. function Scan()
  33. scannedx = tostring(x + frontx)
  34. if scannedx:len() < 2 then scannedx = table.concat({"0",scannedx},"") end
  35. scannedy = tostring(y + fronty)
  36. if scannedy:len() < 2 then scannedy = table.concat({"0",scannedy},"") end
  37.  
  38. scan = turtle.detect()
  39. if scan then rednet.send(ID, table.concat({"0",scannedx,scannedy,"0"},"")) end
  40. if not scan then rednet.send(ID, table.concat({"0",scannedx,scannedy,"2"},""))end
  41. end
  42.  
  43. function FullScan()
  44. for i = 1,4 do
  45. Scan()
  46. turn("Right")
  47. end
  48. end
  49.  
  50. function SetDir(s)
  51. while dir ~= s do
  52. turn("Right")
  53. end
  54. end
  55.  
  56. function Dig()
  57. turtle.dig()
  58. os.sleep(0.40)
  59. Scan()
  60. end
  61.  
  62. function Place()
  63. turtle.place()
  64. Scan()
  65. end
  66.  
  67. function Forward()
  68. if not turtle.detect() then
  69. if dir == 0 and y > 1 then
  70. turtle.forward()
  71. y = y - 1
  72. if y > 8 and y + 1 > 8 and y < 82 and y + 1 < 82 then
  73. yOff = yOff - 1
  74. end
  75. end
  76. if dir == 1 and x < 90 then
  77. turtle.forward()
  78. x = x + 1
  79. if x < 66 and x - 1 < 66 and x > 24 and x - 1 > 24 then
  80. xOff = xOff + 1
  81. end
  82. end
  83. if dir == 2 and y < 90 then
  84. turtle.forward()
  85. y = y + 1
  86. if y < 82 and y - 1 < 82 and y > 8 and y - 1 > 8 then
  87. yOff = yOff + 1
  88. end
  89. end
  90. if dir == 3 and x > 1 then
  91. turtle.forward()
  92. x = x - 1
  93. if x > 24 and x + 1 > 24 and x < 66 and x + 1 < 66 then
  94. xOff = xOff - 1
  95. end
  96. end
  97. end
  98. local stringmat = {}
  99. stringmat[1] = tostring(xOff)
  100. stringmat[2] = tostring(yOff)
  101. stringmat[3] = tostring(x)
  102. stringmat[4] = tostring(y)
  103. for i = 1,4 do
  104. local stringeval = stringmat[i]
  105. if stringeval:len() == 1 then
  106. stringmat[i] = table.concat({"0",stringeval},"")
  107. end
  108. end
  109. rednet.send(ID, table.concat({"2",stringmat[1],stringmat[2],stringmat[3],stringmat[4]},""))
  110. Scan()
  111. end
  112.  
  113. function Parse(s)
  114. if s:sub(1,1) == "0" then SetDir(tonumber(s:sub(2,2))) end
  115. if s:sub(1,1) == "2" then Dig() end
  116. if s:sub(1,1) == "3" then FullScan() end
  117. if s:sub(1,1) == "4" then Place() end
  118. if s:sub(1,1) == "1" then Forward() end
  119. end
  120.  
  121. rednet.open("right")
  122. repeat
  123. local event,p1,p2
  124. event,p1,p2 = os.pullEvent()
  125. if event == "rednet_message" then
  126. Parse(p2)
  127. rednet.send(ID, "1")
  128. end
  129. until event == "key" and p1 == 25
  130. rednet.close("left")
Add Comment
Please, Sign In to add comment