Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.47 KB | None | 0 0
  1. local startingPosition = { x = 0, y = 0, z = 0}
  2.  
  3. local blocksExplored = {}
  4. local blockQueue = {}
  5. local logMessages = {}
  6.  
  7. local dirWorld = "north"
  8.  
  9. term.write("Enable debug output mode?: ")
  10. local debugOn = true
  11. if(io.read("*l") == "false") then
  12. debugOn = false
  13. end
  14. term.write("Enable block logging mode?: ")
  15. local blockLog = true
  16. if(io.read("*l") == "false") then
  17. blockLog = false
  18. end
  19. term.write("Enable distance logging mode?: ")
  20. local distanceLog = true
  21. if(io.read("*l") == "false") then
  22. distanceLog = false
  23. end
  24. term.write("Enter turtle's x coordinate: ")
  25. startingPosition.x = io.read("*l")
  26. term.write("Enter turtle's y coordinate: ")
  27. startingPosition.y = io.read("*l")
  28. term.write("Enter turtle's z coordinate: ")
  29. startingPosition.z = io.read("*l")
  30. local currentPosition = startingPosition
  31.  
  32. local function tableString(tbl)
  33. local str = "{"
  34. for k,v in pairs(tbl) do
  35. str = str .. k .. "=" .. v
  36. end
  37. str = str .. "}"
  38. return str
  39. end
  40.  
  41. local function consumeFuel()
  42. for i = 1, 16, 1 do
  43. local fuelDifference = turtle.getFuelLimit() - turtle.getFuelLevel()
  44. if(fuelDifference < (80 * 64)) then
  45. turtle.refuel()
  46. end
  47. end
  48. term.write("[CaveEX]: New fuel amount is " .. turtle.getFuelLevel() .. " actions/moves.")
  49. end
  50.  
  51. local function compactPosition()
  52. return "x: " .. currentPosition.x .. ", y: " .. currentPosition.y .. ", z: " .. currentPosition.z
  53. end
  54.  
  55. local function logMessage(msg)
  56. logMessages[#logMessages + 1] = tostring(msg)
  57. if(debugOn) then print(msg) end
  58. end
  59.  
  60. local function updatePosition()
  61. if(dirWorld == "north") then
  62. startingPosition.z = startingPosition.z + 1
  63. elseif(dirWorld == "south") then
  64. startingPosition.z = startingPosition.z - 1
  65. elseif(dirWorld == "west") then
  66. startingPosition.x = startingPosition.x - 1
  67. elseif(dirWorld == "east") then
  68. startingPosition.x = startingPosition.x + 1
  69. end
  70. end
  71.  
  72. local function forward(n)
  73. for i = 1, n, 1 do
  74. turtle.forward()
  75. updatePosition()
  76. end
  77. end
  78.  
  79. local function noDuplicate(newEntry)
  80. for i, v in pairs(blocksExplored) do
  81. if(v == newEntry) then
  82. logMessage("[CaveEX]: Found duplicate in blocksExplored " .. tableString(newEntry))
  83. return false
  84. end
  85. end
  86.  
  87. for i, v in pairs(blockQueue) do
  88. if(v == newEntry) then
  89. logMessage("[CaveEX]: Found duplicate in blockQueue " .. tableString(newEntry))
  90. return false
  91. end
  92. end
  93.  
  94. return true
  95. end
  96.  
  97. local function dirToOffset()
  98. if(dirWorld == "north") then
  99. return 0, 1
  100. elseif(dirWorld == "south") then
  101. return 0, -1
  102. elseif(dirWorld == "west") then
  103. return -1, 0
  104. elseif(dirWorld == "east") then
  105. return 1, 0
  106. end
  107. end
  108.  
  109. local function leftTurn()
  110. if(dirWorld == "north") then
  111. dirWorld = "west"
  112. elseif(dirWorld == "west") then
  113. dirWorld = "south"
  114. elseif(dirWorld == "south") then
  115. dirWorld = "east"
  116. elseif(dirWorld == "east") then
  117. dirWorld = "north"
  118. end
  119. end
  120.  
  121. local function addIfAirFront()
  122. consumeFuel()
  123. if(not turtle.detect()) then
  124. local x0, z0 = dirToOffset()
  125. local newEntry = { x = currentPosition.x + x0, y = currentPosition.y, z = currentPosition.z + z0 }
  126. if(noDuplicate(newEntry)) then
  127. blockQueue[#blockQueue + 1] = newEntry
  128. if(blockLog) then logMessage("[CaveEX]: blockQueue += " .. tableString(blockQueue[#blockQueue])) end
  129. end
  130. else
  131. local x0, z0 = dirToOffset()
  132. local success, data = turtle.inspect()
  133. local newEntry = { x = currentPosition.x + x0, y = currentPosition.y, z = currentPosition.z + z0, name = data.name, meta = data.metadata }
  134. if(noDuplicate(newEntry)) then
  135. blocksExplored[#blocksExplored + 1] = newEntry
  136. if(blockLog) then logMessage("[CaveEX]: blocksExplored += " .. tableString(blocksExplored[#blocksExplored])) end
  137. end
  138. end
  139. turtle.turnLeft()
  140. leftTurn()
  141. end
  142.  
  143. local function addIfAir()
  144. if(not turtle.detectDown()) then
  145. local newEntry = { x = currentPosition.x, y = currentPosition.y - 1, z = currentPosition.z }
  146. if(noDuplicate(newEntry)) then
  147. blockQueue[#blockQueue + 1] = newEntry
  148. if(blockLog) then logMessage("[CaveEX]: blockQueue += " .. tableString(blockQueue[#blockQueue])) end
  149. end
  150. else
  151. local success, data = turtle.inspectDown()
  152. local newEntry = { x = currentPosition.x, y = currentPosition.y - 1, z = currentPosition.z, name = data.name, meta = data.metadata }
  153. if(noDuplicate(newEntry)) then
  154. blocksExplored[#blocksExplored + 1] = newEntry
  155. if(blockLog) then logMessage("[CaveEX]: blocksExplored += " .. tableString(blocksExplored[#blocksExplored])) end
  156. end
  157. end
  158.  
  159. if(not turtle.detectUp()) then
  160. local newEntry = { x = currentPosition.x, y = currentPosition.y + 1, z = currentPosition.z }
  161. if(noDuplicate(newEntry)) then
  162. blockQueue[#blockQueue + 1] = newEntry
  163. if(blockLog) then logMessage("[CaveEX]: blockQueue += " .. tableString(blockQueue[#blockQueue])) end
  164. end
  165. else
  166. local success, data = turtle.inspectUp()
  167. local newEntry = { x = currentPosition.x, y = currentPosition.y + 1, z = currentPosition.z, name = data.name, meta = data.metadata }
  168. if(noDuplicate(newEntry)) then
  169. blocksExplored[#blocksExplored + 1] = newEntry
  170. if(blockLog) then logMessage("[CaveEX]: blocksExplored += " .. tableString(blocksExplored[#blocksExplored])) end
  171. end
  172. end
  173.  
  174. addIfAirFront()
  175. addIfAirFront()
  176. addIfAirFront()
  177. addIfAirFront()
  178. end
  179.  
  180. term.write("CaveEX starting from " .. compactPosition())
  181.  
  182. local timeStart = os.clock()
  183.  
  184. consumeFuel()
  185.  
  186. -- Populate blockQueue with the blocks around us.
  187.  
  188. addIfAir()
  189.  
  190. local function lowestTable(tbl)
  191. local lowestValue = 999999
  192. local lowest = nil
  193. for key, value in pairs(tbl) do
  194. if(value < lowestValue) then
  195. lowestValue = value
  196. lowest = key
  197. end
  198. end
  199. return lowest
  200. end
  201.  
  202. local function indexTable(tbl, ele)
  203. for key, value in pairs(tbl) do
  204. if(value.x == ele.x and value.y == ele.y and value.z == ele.z) then
  205. return key
  206. end
  207. end
  208. end
  209.  
  210. local function getNeighbours(current)
  211. local returnable = {}
  212. for key, value in pairs(blocksExplored) do
  213. if(value.x == current.x and value.y == current.y and (value.z == current.z + 1 or value.z == current.z - 1)) then
  214. returnable[#returnable + 1] = value
  215. elseif((value.x == current.x + 1 or value.x == current.x - 1) and value.y == current.y and value.z == current.z) then
  216. returnable[#returnable + 1] = value
  217. elseif(value.x == current.x and (value.y == current.y + 1 or value.y == current.y - 1) and value.z == current.z) then
  218. returnable[#returnable + 1] = value
  219. end
  220. end
  221. end
  222.  
  223. local function h(value, goal)
  224. local xD = goal.x - value.x
  225. local yD = (goal.y - value.y) * 10
  226. local zD = goal.z - value.z
  227. return math.sqrt(xD^2 + yD^2 + zD^2)
  228. end
  229.  
  230. local function aSTAR(start, goal)
  231. local openSet = blocksExplored -- number indexed
  232. local cameFrom = {} -- value indexed
  233.  
  234. local gScore = {} -- value indexed
  235. gScore[start] = 0
  236.  
  237. local fScore = {} -- value indexed
  238. fScore[start] = h(start, goal)
  239.  
  240. while(#openSet > 0) do
  241. local current = lowestTable(fScore) -- check
  242. if(current == goal) then
  243. term.write("I FRIKKIN GOT IT BOY!")
  244. return true
  245. end
  246.  
  247. term.write("b4 path " .. #openSet)
  248. openSet[indexTable(openSet, current)] = nil -- check
  249. term.write("a4 path " .. #openSet)
  250.  
  251. for key, value in pairs(getNeighbours(current)) do
  252. local tentative_gScore = gScore[current] + 1
  253. if(tentative_gScore < gScore[value]) then
  254. cameFrom[value] = current
  255. gScore[value] = tentative_gScore
  256. fScore[value] = gScore[value] + h(value, goal)
  257. if(openSet[indexTable(openSet, value)] == nil) then
  258. openSet[#openSet + 1] = value
  259. end
  260. end
  261. end
  262. end
  263.  
  264. term.write("didn't find it, sad!")
  265. end
  266.  
  267. local function pathfind()
  268. -- Pathfind our way to the nearest block.
  269. local nearestBlock = { x = 0, y = 0, z = 0 }
  270. local nearestBlockValue = 999999
  271. for _,v in pairs(blockQueue) do
  272. local xD = currentPosition.x - v.x
  273. local yD = (currentPosition.y - v.y) * 10 -- height difference mega important
  274. local zD = currentPosition.z - v.z
  275. local distance = math.sqrt(xD^2 + yD^2 + zD^2)
  276. if(distanceLog) then logMessage("[CaveEX]: New distance " .. distance) end
  277. if(distance < nearestBlockValue) then
  278. nearestBlockValue = distance
  279. nearestBlock = { x = v.x, y = v.y, z = v.z }
  280. end
  281. end
  282. if(distanceLog) then logMessage("[CaveEX]: Closest block " .. tableString(nearestBlock)) end
  283. local path = aSTAR(currentPosition, nearestBlock)
  284. end
  285.  
  286. --[[while(#blockQueue > 0) do
  287. pathfind()
  288. addIfAir()
  289. end]]
  290.  
  291. pathfind()
  292.  
  293. local timeEnd = os.clock() - timeStart
  294.  
  295. term.write("CaveEX finished in " .. timeEnd .. " seconds.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement