MudkipTheEpic

Beacon API

Jun 15th, 2013
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. local modem
  2. for k,v in pairs(peripheral.getNames()) do
  3. if peripheral.getType(v)=="modem" and peripheral.call(v,"isWireless") then
  4. modem=peripheral.wrap(v)
  5. end
  6. end
  7. assert(modem,"No wireless modem found")
  8.  
  9. local up=function()
  10. local tries=0
  11. while (not e or tries>10) do
  12. local e=turtle.up()
  13. if not e then turtle.digUp() turtle.attackUp() tries=tries+1 end
  14. end
  15. if tries==10 then error("Something in way... Cannot continue.",3) end
  16. return true
  17. end
  18. local fd=function()
  19. local tries=0
  20. while (not e or tries>10) do
  21. local e=turtle.forward()
  22. if not e then turtle.dig() turtle.attack() tries=tries+1 end
  23. end
  24. if tries==10 then error("Something in way... Cannot continue.",3) end
  25. return true
  26. end
  27. local down=function()
  28. local tries=0
  29. while (not e or tries>10) do
  30. local e=turtle.down()
  31. if not e then turtle.digDown() turtle.attackDown() tries=tries+1 end
  32. end
  33. if tries==10 then error("Something in way... Cannot continue.",3) end
  34. return true
  35. end
  36.  
  37. function goTo(channel, msg)
  38. if not turtle then error("Can't go with a computer. Shtewpid",2) end
  39. local isGPS
  40. if channel==true and msg==nil then
  41. channel=65534
  42. isGPS=true
  43. end
  44. modem.closeAll()
  45. modem.open(channel)
  46. local distanceT={}
  47. local function checkIfBeacon(ch,msg) --Checks if there is a beacon with that channel and message
  48. local e,s,c,r,m,d
  49. if not modem.isOpen(ch) then modem.open(ch) end
  50. modem.transmit(ch,ch,"PING")
  51. local id=os.startTimer(2)
  52. repeat
  53. e,s,c,r,m,d=os.pullEventRaw()
  54. until (e=="modem_message" and c==ch and (not msg or msg==m)) or (e=="timer" and s==id) --If it matches or times out then end the loop.
  55. if e=="timer" then return false end
  56. return true
  57. end
  58. if not checkIfBeacon(channel,msg) then
  59. return false, "Invalid beacon."
  60. end
  61. turtle.up() --start moving out of the way
  62. turtle.turnRight()--^
  63. turtle.turnRight()--^
  64. turtle.forward() --^
  65. local function getData() --Get data
  66. modem.transmit(channel,channel,"PING")
  67. local eventData
  68. local tID=os.startTimer(5)
  69. repeat
  70. eventData={os.pullEventRaw()}
  71. if eventData[1]=="modem_message" and (eventData[5]==msg or msg==nil) then return eventData end
  72. --print("test")
  73. until eventData[1]=="terminate" or (eventData[1]=="timer" and eventData[2]==tID)
  74. return (eventData[1]=="terminate" and error("Terminated",0)) or error("Lost signal.",2)--
  75. end
  76. local function getCloser(func)
  77. while true do
  78. func()
  79. local nDat=getData()
  80. if nDat[6]>distanceT[#distanceT] or nDat[6]==1 then break --If it got farther or its next to it then exit the loop.
  81. else
  82. table.insert(distanceT,nDat[6]) --Insert to the distance table.
  83. end
  84. end
  85. end
  86. local eventData=getData() --Get the data
  87. if isGPS then msg=eventData[5] end --if its a gps then the message is that message
  88. table.insert(distanceT,eventData[6])
  89. turtle.up()
  90. local nData=getData()
  91. table.insert(distanceT,nData[6])
  92. if distanceT[#distanceT-1]>distanceT[#distanceT] then
  93. --This means its higher
  94. getCloser(up)
  95. turtle.digDown()
  96. turtle.down()
  97. else
  98. --Lower
  99. getCloser(down)
  100. turtle.digUp()
  101. turtle.up()
  102. end
  103. local eventData=getData()
  104. table.insert(distanceT,eventData[6])
  105. turtle.forward()
  106. local nData=getData()
  107. table.insert(distanceT,nData[6])
  108. if distanceT[#distanceT-1]>distanceT[#distanceT] then
  109. --Keep going forward
  110. getCloser(fd)
  111. turtle.back()
  112. else
  113. --Moonwalk
  114. turtle.turnRight()
  115. turtle.turnRight()
  116. getCloser(fd)
  117. turtle.back()
  118. --turtle.turnRight()
  119. --turtle.turnRight()
  120. end
  121. --Turn right and do (almost) the same thing
  122. turtle.turnRight()
  123.  
  124. local eventData=getData()
  125. table.insert(distanceT,eventData[6])
  126. turtle.forward()
  127. local nData=getData()
  128. table.insert(distanceT,nData[6])
  129. if distanceT[#distanceT-1]>distanceT[#distanceT] then
  130. --Keep going forward
  131. getCloser(function() repeat turtle.dig() turtle.attack() until (turtle.forward()) end)
  132. else
  133. --Moonwalk
  134. turtle.turnRight()
  135. turtle.turnRight()
  136. getCloser(function() repeat turtle.dig() turtle.attack() until (turtle.forward()) end)
  137. end
  138. return peripheral.getType("front")=="computer" or peripheral.getType("front")=="turtle" --Return true if its an actual host.
  139. end
  140.  
  141.  
  142. function host(channel, msg) --Host a beacon
  143. modem.closeAll()
  144. modem.open(channel)
  145. repeat
  146. local eventData={os.pullEventRaw("modem_message")}
  147. if eventData[1]=="modem_message" and eventData[3]==channel and eventData[5]=="PING" then --If its pinged send the message
  148. modem.transmit(channel,channel,msg)
  149. end
  150. until eventData[1]=="terminate"
  151. return error("Terminated",0)
  152. end
Advertisement
Add Comment
Please, Sign In to add comment