Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 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("Enter turtle's x coordinate: ")
  15. startingPosition.x = io.read("*l")
  16. term.write("Enter turtle's y coordinate: ")
  17. startingPosition.y = io.read("*l")
  18. term.write("Enter turtle's z coordinate: ")
  19. startingPosition.z = io.read("*l")
  20. local currentPosition = startingPosition
  21.  
  22. local function tableString(tbl)
  23. local str = "{"
  24. for k,v in pairs(tbl) do
  25. str = str .. k .. "=" .. v
  26. end
  27. str = str .. "}"
  28. return str
  29. end
  30.  
  31. local function consumeFuel()
  32. for i = 1, 16, 1 do
  33. local fuelDifference = turtle.getFuelLimit() - turtle.getFuelLevel()
  34. if(fuelDifference < (80 * 64)) then
  35. turtle.refuel()
  36. end
  37. end
  38. term.write("[CaveEX]: New fuel amount is " .. turtle.getFuelLevel() .. " actions/moves.")
  39. end
  40.  
  41. local function compactPosition()
  42. return "x: " .. currentPosition.x .. ", y: " .. currentPosition.y .. ", z: " .. currentPosition.z
  43. end
  44.  
  45. local function logMessage(msg)
  46. logMessages[#logMessages + 1] = tostring(msg)
  47. if(debugOn) then print(msg) end
  48. end
  49.  
  50. local function updatePosition()
  51. if(dirWorld == "north") then
  52. startingPosition.z = startingPosition.z + 1
  53. elseif(dirWorld == "south") then
  54. startingPosition.z = startingPosition.z - 1
  55. elseif(dirWorld == "west") then
  56. startingPosition.x = startingPosition.x - 1
  57. elseif(dirWorld == "east") then
  58. startingPosition.x = startingPosition.x + 1
  59. end
  60. end
  61.  
  62. local function forward(n)
  63. for i = 1, n, 1 do
  64. turtle.forward()
  65. updatePosition()
  66. end
  67. end
  68.  
  69. local function noDuplicate(newEntry)
  70. for i, v in pairs(blocksExplored) do
  71. if(v == newEntry) then
  72. return false
  73. end
  74. end
  75.  
  76. for i, v in pairs(blockQueue) do
  77. if(v == newEntry) then
  78. return false
  79. end
  80. end
  81.  
  82. return true
  83. end
  84.  
  85. local function dirToOffset()
  86. if(dirWorld == "north") then
  87. return 0, 1
  88. elseif(dirWorld == "south") then
  89. return 0, -1
  90. elseif(dirWorld == "west") then
  91. return -1, 0
  92. elseif(dirWorld == "east") then
  93. return 1, 0
  94. end
  95. end
  96.  
  97. local function leftTurn()
  98. if(dirWorld == "north") then
  99. dirWorld = "west"
  100. elseif(dirWorld == "west") then
  101. dirWorld = "south"
  102. elseif(dirWorld == "south") then
  103. dirWorld = "east"
  104. elseif(dirWorld == "east") then
  105. dirWorld = "north"
  106. end
  107. end
  108.  
  109. local function addIfAirFront()
  110. consumeFuel()
  111. if(not turtle.detect()) then
  112. local x0, z0 = dirToOffset()
  113. local newEntry = { x = currentPosition.x + x0, y = currentPosition.y, z = currentPosition.z + z0 }
  114. if(noDuplicate(newEntry)) then
  115. blockQueue[#blockQueue + 1] = newEntry
  116. logMessage("[CaveEX]: blockQueue += " .. tableString(blockQueue[#blockQueue]))
  117. end
  118. else
  119. local x0, z0 = dirToOffset()
  120. local success, data = turtle.inspect()
  121. local newEntry = { x = currentPosition.x + x0, y = currentPosition.y, z = currentPosition.z + z0, name = data.name, meta = data.metadata }
  122. if(noDuplicate(newEntry)) then
  123. blocksExplored[#blocksExplored + 1] = newEntry
  124. logMessage("[CaveEX]: blocksExplored += " .. tableString(blocksExplored[#blocksExplored]))
  125. end
  126. end
  127. turtle.turnLeft()
  128. leftTurn()
  129. end
  130.  
  131. local function addIfAir()
  132. if(not turtle.detectDown()) then
  133. local newEntry = { x = currentPosition.x, y = currentPosition.y - 1, z = currentPosition.z }
  134. if(noDuplicate(newEntry)) then
  135. blockQueue[#blockQueue + 1] = newEntry
  136. logMessage("[CaveEX]: blockQueue += " .. tableString(blockQueue[#blockQueue]))
  137. end
  138. else
  139. local success, data = turtle.inspectDown()
  140. local newEntry = { x = currentPosition.x, y = currentPosition.y - 1, z = currentPosition.z, name = data.name, meta = data.metadata }
  141. if(noDuplicate(newEntry)) then
  142. blocksExplored[#blocksExplored + 1] = newEntry
  143. logMessage("[CaveEX]: blocksExplored += " .. tableString(blocksExplored[#blocksExplored]))
  144. end
  145. end
  146.  
  147. if(not turtle.detectUp()) then
  148. local newEntry = { x = currentPosition.x, y = currentPosition.y + 1, z = currentPosition.z }
  149. if(noDuplicate(newEntry)) then
  150. blockQueue[#blockQueue + 1] = newEntry
  151. logMessage("[CaveEX]: blockQueue += " .. tableString(blockQueue[#blockQueue]))
  152. end
  153. else
  154. local success, data = turtle.inspectUp()
  155. local newEntry = { x = currentPosition.x, y = currentPosition.y + 1, z = currentPosition.z, name = data.name, meta = data.metadata }
  156. if(noDuplicate(newEntry)) then
  157. blocksExplored[#blocksExplored + 1] = newEntry
  158. logMessage("[CaveEX]: blocksExplored += " .. tableString(blocksExplored[#blocksExplored]))
  159. end
  160. end
  161.  
  162. addIfAirFront()
  163. addIfAirFront()
  164. addIfAirFront()
  165. addIfAirFront()
  166. end
  167.  
  168. term.write("CaveEX starting from " .. compactPosition())
  169.  
  170. consumeFuel()
  171.  
  172. -- Populate blockQueue with the blocks around us.
  173.  
  174. addIfAir()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement