Jakey4543

Client

Dec 26th, 2020 (edited)
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. --// Client
  2.  
  3. local SERVER_PORT = 3230
  4. local CLIENT_PORT = 2020
  5.  
  6. local modem = peripheral.wrap("left")
  7. modem.open(CLIENT_PORT)
  8.  
  9. function MODEM_SEND(port,message)
  10. modem.transmit(port,CLIENT_PORT,message)
  11. end
  12.  
  13. function getItemIndex(itemName)
  14. for slot = 1, SLOT_COUNT, 1 do
  15. local item = turtle.getItemDetail(slot)
  16. if(item ~= nil) then
  17. if(item["name"] == itemName) then
  18. return slot
  19. end
  20. end
  21. end
  22. end
  23.  
  24. function split (inputstr, sep)
  25. if sep == nil then
  26. sep = "%s"
  27. end
  28. local t={}
  29. for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  30. table.insert(t, str)
  31. end
  32. return t
  33. end
  34.  
  35. function parseParams(data)
  36. parsed = {}
  37. params = split(data, " ")
  38.  
  39. parsed[1] = vector.new(params[1], params[2], params[3])
  40. parsed[2] = vector.new(params[4], params[5], params[6])
  41. parsed[3] = tonumber(params[7])
  42.  
  43. return (parsed)
  44. end
  45.  
  46. function locateVector()
  47. local v = vector.new(gps.locate())
  48. v.x = v.x - 1
  49. v.y = v.y
  50.  
  51. return v
  52. end
  53.  
  54. function recalculateVector(vec)
  55. return vector.new(vec.x - 1, vec.y, vec.z)
  56. end
  57.  
  58. function refuel()
  59. if turtle.getFuelLevel == 0 then
  60. turtle.select(1)
  61. turtle.placeUp()
  62. turtle.suckUp(1)
  63.  
  64. turtle.select(getItemIndex("minecraft:lava_bucket"))
  65.  
  66. turtle.refuel()
  67. turtle.dropUp()
  68. turtle.digUp()
  69. print("Refuel complete")
  70. end
  71. end
  72.  
  73. function getOrientation()
  74. loc1 = vector.new(gps.locate(2, false))
  75. if not turtle.forward() then
  76. for j=1,6 do
  77. if not turtle.forward() then
  78. turtle.dig()
  79. else
  80. break
  81. end
  82. end
  83. end
  84. loc2 = vector.new(gps.locate(2, false))
  85. heading = loc2 - loc1
  86. turtle.down()
  87. turtle.down()
  88. return ((heading.x + math.abs(heading.x) * 2) + (heading.z + math.abs(heading.z) * 3))
  89. end
  90.  
  91. function turnToFaceHeading(heading, destinationHeading)
  92. if(heading > destinationHeading) then
  93. for t = 1, math.abs(destinationHeading - heading), 1 do
  94. turtle.turnLeft()
  95. end
  96. elseif(heading < destinationHeading) then
  97. for t = 1, math.abs(destinationHeading - heading), 1 do
  98. turtle.turnRight()
  99. end
  100. end
  101. end
  102.  
  103. function setHeadingZ(zDiff, heading)
  104. local destinationHeading = heading
  105. if(zDiff < 0) then
  106. destinationHeading = 2
  107. elseif(zDiff > 0) then
  108. destinationHeading = 4
  109. end
  110. turnToFaceHeading(heading, destinationHeading)
  111.  
  112. return destinationHeading
  113. end
  114.  
  115. function setHeadingX(xDiff, heading)
  116. local destinationHeading = heading
  117. if(xDiff < 0) then
  118. destinationHeading = 1
  119. elseif(xDiff > 0) then
  120. destinationHeading = 3
  121. end
  122.  
  123. turnToFaceHeading(heading, destinationHeading)
  124. return destinationHeading
  125. end
  126.  
  127.  
  128. function mineLayer()
  129. turtle.dig()
  130. turtle.forward()
  131.  
  132. turtle.digUp()
  133. refuel()
  134. turtle.turnRight()
  135. turtle.dig()
  136. turtle.forward()
  137. turtle.digUp()
  138. turtle.back()
  139. turtle.turnLeft()
  140. end
  141.  
  142. function digAndMove(n)
  143. for x = 1, n, 1 do
  144. while(turtle.detect()) do
  145. turtle.dig()
  146. end
  147. turtle.forward()
  148. refuel()
  149. end
  150. end
  151.  
  152. function digAndMoveDown(n)
  153. for y = 1, n, 1 do
  154. print(y)
  155. while(turtle.detectDown()) do
  156. turtle.digDown()
  157. end
  158. turtle.down()
  159. refuel()
  160. end
  161. end
  162.  
  163. function moveTo(coords, heading)
  164. MODEM_SEND(SERVER_PORT,"BotMoving")
  165.  
  166. local currX, currY, currZ = gps.locate()
  167. local xDiff, yDiff, zDiff = coords.x - currX, coords.y - currY, coords.z - currZ
  168. print(string.format("Distances from start: %d %d %d", xDiff, yDiff, zDiff))
  169.  
  170. -- -x = 1
  171. -- -z = 2
  172. -- +x = 3
  173. -- +z = 4
  174.  
  175.  
  176. -- Move to X start
  177. heading = setHeadingX(xDiff, heading)
  178. digAndMove(math.abs(xDiff))
  179.  
  180. -- Move to Z start
  181. heading = setHeadingZ(zDiff, heading)
  182. digAndMove(math.abs(zDiff))
  183.  
  184. -- Move to Y start
  185. --if(yDiff < 0) then
  186. -- digAndMoveDown(math.abs(yDiff))
  187. --elseif(yDiff > 0) then
  188. -- digAndMoveUp(math.abs(yDiff))
  189. --end
  190.  
  191.  
  192. return heading
  193. end
  194.  
  195.  
  196. MODEM_SEND(SERVER_PORT,"BotOnline")
  197.  
  198. print("Bot Online")
  199.  
  200. event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
  201. print("Got message")
  202.  
  203. local parsedData = parseParams(msg)
  204. local startPos = parsedData[1]
  205. local toPos = parsedData[2]
  206. local tunnelLen = parsedData[3]
  207.  
  208. --local heading = calculateHeading()
  209.  
  210. print(string.format("To position: %d %d %d",toPos.x, toPos.y, toPos.z))
  211.  
  212. local finalHeading = moveTo(toPos, getOrientation())
  213.  
  214. local SOUTH_HEADING = 4
  215. --Turn to face South
  216. turnToFaceHeading(finalHeading, SOUTH_HEADING)
  217. finalHeading = SOUTH_HEADING
  218. --Now in Starting Position--
  219.  
  220. for i = 1,tunnelLen+0,1 do
  221. print("Mining layer")
  222. mineLayer()
  223. end
  224.  
  225. print("Mine Finished.")
Advertisement
Add Comment
Please, Sign In to add comment