MatthewGB

RC Turtle Main

Apr 23rd, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. os.pullEvent = os.pullEventRaw
  2. if peripheral.isPresent("left") then
  3. m= peripheral.wrap("left")
  4. m.turnOn()
  5. end
  6. if peripheral.isPresent("top") then
  7. m= peripheral.wrap("top")
  8. m.turnOn()
  9. end
  10. if peripheral.isPresent("back") then
  11. m= peripheral.wrap("back")
  12. m.turnOn()
  13. end
  14. if peripheral.isPresent("front") then
  15. m= peripheral.wrap("front")
  16. m.turnOn()
  17. end
  18. if peripheral.isPresent("bottom") then
  19. m= peripheral.wrap("bottom")
  20. m.turnOn()
  21. end
  22. function dig(side)
  23. if side == "front" then
  24. if turtle.detect() == true then
  25. rs.setOutput("front", false)
  26. while rs.getInput(side) do
  27. sleep(0.2)
  28. end
  29. end
  30. elseif side == "down" then
  31. if turtle.detectDown() == true then
  32. rs.setOutput("bottom", false)
  33. while rs.getInput("bottom") do
  34. sleep(0.2)
  35. end
  36. rs.setOutput("bottom", true)
  37. end
  38. elseif side == "up" then
  39. if turtle.detectUp() == true then
  40. rs.setOutput("top", false)
  41. while rs.getInput("top") do
  42. sleep(0.2)
  43. end
  44. end
  45. end
  46. print("Block in the way, digging!")
  47. if side == "front" then
  48. turtle.dig()
  49. elseif side == "down" then
  50. turtle.digDown()
  51. elseif side == "up" then
  52. turtle.digUp()
  53. end
  54. end
  55. function docmd(aaction, adir, ax)
  56. if aaction == "mv" then
  57. print("Action: mv")
  58. i = 0
  59. while i<ax do
  60. print("Action done"..tostring(i).."/"..tostring(ax).." times")
  61. print("Direction "..tostring(adir))
  62. if adir == "f" then
  63. while rs.getInput("front") == true do
  64. a = 0 -- Do NOTHING
  65. end
  66. dig("front")
  67. elseif adir == "u" then
  68. while rs.getInput("top") == true do
  69. a = 0 -- Do NOTHING
  70. end
  71. dig("up")
  72. elseif adir == "d" then
  73. while rs.getInput("bottom") == true do
  74. a = 0 -- Do NOTHING
  75. end
  76. dig("down")
  77. end
  78. if adir == "f" then
  79. turtle.forward()
  80. elseif adir == "b" then
  81. turtle.back()
  82. elseif adir == "u" then
  83. turtle.up()
  84. elseif adir == "d" then
  85. turtle.down()
  86. else
  87. print("Unknown direction!")
  88. end
  89. i = i + 1
  90. rs.setOutput("front", true)
  91. rs.setOutput("back", true)
  92. rs.setOutput("right", true)
  93. rs.setOutput("left", true)
  94. rs.setOutput("bottom", true)
  95. rs.setOutput("top", true)
  96. end
  97. elseif aaction == "select" then
  98. turtle.select(x)
  99. end
  100. end
  101. dir = "f"
  102. x = 0
  103. action = ""
  104. max = 17
  105. -- 1 = forward
  106. -- 2 = back
  107. -- 3 = up
  108. -- 4 = down
  109. rednet.open("right")
  110. shell.run("label set RCTurtleSlave")
  111. while 1<2 do
  112. dir = 0
  113. x = 0
  114. action = ""
  115. print("Waiting for cmd")
  116. dist = "N/A"
  117. id, msg = rednet.receive()
  118. print("ID "..id..", "..dist.." blocks away said "..msg..".")
  119. msg2 = msg
  120. if msg:sub(1,7) == "forward" then
  121. action = "mv"
  122. dir = "f"
  123. x = tonumber(msg2:sub(8,11))
  124. elseif msg:sub(1,2) == "up" then
  125. action = "mv"
  126. dir = "u"
  127. x = tonumber(msg2:sub(3,5))
  128. elseif msg:sub(1,4) == "down" then
  129. action = "mv"
  130. dir = "d"
  131. x = tonumber(msg2:sub(5,8))
  132. elseif msg:sub(1,4) == "back" then
  133. action = "mv"
  134. dir = "b"
  135. x = tonumber(msg2:sub(5,8))
  136. elseif msg:sub(1,6) == "select" then
  137. action = "select"
  138. dir = "dasjndkjfn"
  139. x = tonumber(msg2:sub(7,8))
  140. elseif msg == "ud" then
  141. shell.run("pastebin get HVArpJ7c ud")
  142. shell.run("ud")
  143. elseif msg == "dig" then
  144. turtle.dig()
  145. elseif msg == "drop" then
  146. turtle.drop()
  147. elseif msg == "dropdown" then
  148. turtle.dropDown()
  149. elseif msg == "dropup" then
  150. turtle.dropUp()
  151. elseif msg == "empty" then
  152. i = 1
  153. while i<max do
  154. turtle.select(i)
  155. turtle.drop(i)
  156. i = i + 1
  157. end
  158. elseif msg == "emptydown" then
  159. i = 1
  160. while i<max do
  161. turtle.select(i)
  162. turtle.dropDown(i)
  163. i = i + 1
  164. end
  165. elseif msg == "suck" then
  166. turtle.suck()
  167. elseif msg == "tr" then
  168. turtle.turnRight()
  169. elseif msg == "tl" then
  170. turtle.turnLeft()
  171. elseif msg == "ping" then
  172. print("Command: ping")
  173. rednet.broadcast("TPong")
  174. print("Sent a TPong")
  175. else
  176. print("Unknown cmd")
  177. end
  178. docmd(action, dir, x)
  179. end
Advertisement
Add Comment
Please, Sign In to add comment