JakobM7

Minecraft computercraft replace ocean

Mar 25th, 2025 (edited)
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.36 KB | Gaming | 0 0
  1. -- Minecraft Turtle script to make a ocean area usable for a player by placing blocks under itself and therefore creating a landmass.
  2.  
  3. --[[
  4. The Script takes the following arguments:
  5. 1. The length of the area
  6. 2. The width of the area
  7. 3. The depth how much the turtle should dig down
  8.  
  9. The turtle will go down (Without digging because there should be water) the specified depth and then place blocks under itself to create a landmass.
  10. Therefore it will go back and forth and turn left and right to create a new layer of blocks.
  11. If the turtle is done with the first layer it will go up by a block and place blocks there to create a new layer.
  12. This will be repeated until the turtle is at the same level as it started.
  13. Behind the start position of the turtle will be a chest with the blocks that the turtle needs to place.
  14. If there are blocks in the way the turtle will dig through them to get to its desired position.
  15. The turtle will also check if it has enough fuel and refuel if necessary.
  16. The turtle will also check if there are enough blocks in the chest and if not it will pause the program.
  17. ]]
  18.  
  19. -- Function to check and refuel if necessary
  20. function checkFuel(requiredFuel)
  21. local currentFuel = turtle.getFuelLevel()
  22.  
  23. -- If fuel is unlimited, return true
  24. if currentFuel == "unlimited" then
  25. return true
  26. end
  27.  
  28. -- Check if we have enough fuel
  29. if currentFuel < requiredFuel then
  30. print("Low fuel. Attempting to refuel...")
  31.  
  32. -- Try to refuel from inventory
  33. for i = 1, 16 do
  34. turtle.select(i)
  35. if turtle.refuel(0) then
  36. local amountToRefuel = math.min(64, math.ceil((requiredFuel - currentFuel) / 80))
  37. if turtle.refuel(amountToRefuel) then
  38. print("Refueled successfully.")
  39. -- Check the new fuel level again
  40. if turtle.getFuelLevel() >= requiredFuel then
  41. return true
  42. end
  43. end
  44. end
  45. end
  46.  
  47. print("WARNING: Not enough fuel. Need at least " .. requiredFuel .. " fuel points.")
  48. return false
  49. end
  50.  
  51. return true
  52. end
  53.  
  54. -- Function to ensure a block is selected
  55. function ensureBlockSelected()
  56. if selectBlockSlot() then
  57. return true
  58. end
  59.  
  60. print("Out of blocks! Attempting to get more...")
  61. if not getBlocks() then
  62. print("Please add more blocks to the chest and press Enter to continue.")
  63. read()
  64. if not getBlocks() then
  65. print("Still no blocks available. Exiting.")
  66. return false
  67. end
  68. end
  69.  
  70. return selectBlockSlot()
  71. end
  72.  
  73. -- Function to safely place a block downward
  74. function safelyPlaceBlockDown()
  75. if not ensureBlockSelected() then
  76. return false
  77. end
  78.  
  79. -- If there's already a block down, consider it success
  80. if turtle.detectDown() then
  81. return true
  82. end
  83.  
  84. -- Try to place block
  85. local success = turtle.placeDown()
  86.  
  87. -- Try one more time if failed
  88. if not success then
  89. if not ensureBlockSelected() then
  90. return false
  91. end
  92. success = turtle.placeDown()
  93. end
  94.  
  95. return success
  96. end
  97.  
  98. -- Function to get blocks from the chest behind
  99. function getBlocks()
  100. -- Save current selected slot
  101. local currentSlot = turtle.getSelectedSlot()
  102.  
  103. -- Turn around to face the chest
  104. turtle.turnRight()
  105. turtle.turnRight()
  106.  
  107. -- Check if chest exists
  108. if not turtle.detect() then
  109. print("No chest found behind the turtle!")
  110. -- Return to original orientation
  111. turtle.turnRight()
  112. turtle.turnRight()
  113. turtle.select(currentSlot)
  114. return false
  115. end
  116.  
  117. -- Count current blocks
  118. local totalBlocks = 0
  119. for i = 1, 16 do
  120. totalBlocks = totalBlocks + turtle.getItemCount(i)
  121. end
  122.  
  123. -- Try to get more blocks if we have empty slots
  124. local slotsAvailable = false
  125. for i = 1, 16 do
  126. if turtle.getItemCount(i) == 0 then
  127. slotsAvailable = true
  128. break
  129. end
  130. end
  131.  
  132. if slotsAvailable then
  133. -- Try to get blocks until inventory is full or chest is empty
  134. local attemptCount = 0
  135. while slotsAvailable and attemptCount < 16 do
  136. turtle.select(1)
  137. if not turtle.suck() then
  138. break
  139. end
  140. print("Collected blocks from chest.")
  141.  
  142. -- Check if we still have empty slots
  143. slotsAvailable = false
  144. for i = 1, 16 do
  145. if turtle.getItemCount(i) == 0 then
  146. slotsAvailable = true
  147. break
  148. end
  149. end
  150. attemptCount = attemptCount + 1
  151. end
  152. end
  153.  
  154. -- Turn back around
  155. turtle.turnRight()
  156. turtle.turnRight()
  157.  
  158. -- Count blocks again
  159. local newTotalBlocks = 0
  160. for i = 1, 16 do
  161. newTotalBlocks = newTotalBlocks + turtle.getItemCount(i)
  162. end
  163.  
  164. -- Restore selected slot
  165. turtle.select(currentSlot)
  166.  
  167. return newTotalBlocks > 0
  168. end
  169.  
  170. -- Function to select a slot with blocks
  171. function selectBlockSlot()
  172. for i = 1, 16 do
  173. if turtle.getItemCount(i) > 0 then
  174. turtle.select(i)
  175. return true
  176. end
  177. end
  178. return false
  179. end
  180.  
  181. -- Function to move to the starting position
  182. function moveToStartPosition(depth)
  183. -- Subtract 1 from depth to fix the "one block too deep" issue
  184. for i = 1, depth - 1 do
  185. -- Dig if there's a block in the way
  186. if turtle.detectDown() then
  187. turtle.digDown()
  188. end
  189. if not turtle.down() then
  190. -- Try digging again in case something fell in the way
  191. if turtle.detectDown() then
  192. turtle.digDown()
  193. if not turtle.down() then
  194. print("Couldn't move down at depth " .. i .. " even after digging.")
  195. return false
  196. end
  197. else
  198. print("Couldn't move down at depth " .. i)
  199. return false
  200. end
  201. end
  202. end
  203. return true
  204. end
  205.  
  206. -- Function to try moving forward, digging if necessary
  207. function moveForwardSafely()
  208. if turtle.detect() then
  209. turtle.dig()
  210. os.sleep(0.5) -- Wait for falling blocks/sand
  211. end
  212.  
  213. if not turtle.forward() then
  214. -- Try digging again
  215. if turtle.detect() then
  216. turtle.dig()
  217. os.sleep(0.5)
  218. return turtle.forward()
  219. else
  220. print("Couldn't move forward - path might be blocked")
  221. return false
  222. end
  223. end
  224.  
  225. return true
  226. end
  227.  
  228. -- Function to place a single layer of blocks
  229. function placeLayerOfBlocks(length, width)
  230. local turnRight = true
  231. local posX, posZ = 0, 0 -- Track position relative to start
  232.  
  233. for w = 1, width do
  234. -- Place block under current position
  235. if not safelyPlaceBlockDown() then
  236. print("Failed to place block at position X=" .. posX .. ", Z=" .. posZ)
  237. return false
  238. end
  239.  
  240. -- Move forward placing blocks for the entire row (except for the last row's last position)
  241. if w < width or posX < length - 1 then
  242. for l = 1, length-1 do
  243. -- Move forward, digging if necessary
  244. if not moveForwardSafely() then
  245. print("Failed to move forward in row " .. w .. " at position X=" .. posX .. ", Z=" .. posZ)
  246. return false
  247. end
  248.  
  249. -- Update position
  250. if turnRight then
  251. posX = posX + 1
  252. else
  253. posX = posX - 1
  254. end
  255.  
  256. -- Place block below
  257. if not safelyPlaceBlockDown() then
  258. print("Failed to place block at position X=" .. posX .. ", Z=" .. posZ)
  259. return false
  260. end
  261. end
  262.  
  263. -- Don't turn or move forward if this is the last row
  264. if w < width then
  265. -- Turn for the next row
  266. if turnRight then
  267. turtle.turnRight()
  268.  
  269. if not moveForwardSafely() then
  270. print("Failed to move forward when turning for next row")
  271. return false
  272. end
  273.  
  274. posZ = posZ + 1
  275.  
  276. turtle.turnRight()
  277. else
  278. turtle.turnLeft()
  279.  
  280. if not moveForwardSafely() then
  281. print("Failed to move forward when turning for next row")
  282. return false
  283. end
  284.  
  285. posZ = posZ + 1
  286.  
  287. turtle.turnLeft()
  288. end
  289.  
  290. turnRight = not turnRight
  291. end
  292. end
  293. end
  294.  
  295. -- Return to starting position
  296. print("Returning to starting position. Current position: X=" .. posX .. ", Z=" .. posZ)
  297.  
  298. -- First, orient correctly based on final row direction
  299. if (width % 2) == 0 then
  300. -- If width is even, we ended facing the opposite direction
  301. turtle.turnRight()
  302. turtle.turnRight()
  303. end
  304.  
  305. -- Move back to X=0 (start of row)
  306. for i = 1, math.abs(posX) do
  307. if not moveForwardSafely() then
  308. print("Couldn't return to starting position (X)")
  309. return false
  310. end
  311. end
  312.  
  313. -- Turn to face the Z direction (back toward origin)
  314. if posX > 0 then
  315. turtle.turnRight()
  316. else
  317. turtle.turnLeft()
  318. end
  319.  
  320. -- Move back to Z=0 (first row)
  321. for i = 1, posZ do
  322. if not moveForwardSafely() then
  323. print("Couldn't return to starting position (Z)")
  324. return false
  325. end
  326. end
  327.  
  328. -- Turn to face the original direction (positive X)
  329. turtle.turnRight()
  330.  
  331. print("Successfully returned to starting position")
  332. return true
  333. end
  334.  
  335. -- Main function to build all layers
  336. function buildLayers(length, width, depth)
  337. -- Calculate required fuel (more accurate approximation)
  338. local movesPerLayer = (length * width) + (width * 2) + length + depth
  339. local totalMoves = movesPerLayer * depth + (depth * 2) + 2
  340. local requiredFuel = totalMoves + 10 -- Adding a safety margin
  341.  
  342. print("Estimated fuel required: " .. requiredFuel)
  343.  
  344. -- Check if we have enough fuel
  345. if not checkFuel(requiredFuel) then
  346. return false
  347. end
  348.  
  349. -- Get initial blocks
  350. if not getBlocks() then
  351. print("No blocks in chest! Please add blocks and press Enter to continue.")
  352. read()
  353. if not getBlocks() then
  354. print("Still no blocks available. Exiting.")
  355. return false
  356. end
  357. end
  358.  
  359. -- Move forward one block before starting
  360. print("Moving forward one block to start building")
  361. if not moveForwardSafely() then
  362. print("Couldn't move forward to start building")
  363. return false
  364. end
  365.  
  366. -- Move to starting position (one block less to avoid being too deep)
  367. print("Going down to depth " .. depth)
  368. if not moveToStartPosition(depth) then
  369. return false
  370. end
  371.  
  372. -- Reset orientation for consistent coordinate tracking
  373. local facing = 0 -- 0 = positive X, 1 = positive Z, 2 = negative X, 3 = negative Z
  374.  
  375. -- Place layers from bottom to top
  376. for layer = 1, depth do
  377. print("Building layer " .. layer .. " of " .. depth)
  378.  
  379. -- Place block under the starting position is now handled in placeLayerOfBlocks
  380. if not placeLayerOfBlocks(length, width) then
  381. print("Failed to complete layer " .. layer)
  382. return false
  383. end
  384.  
  385. -- Move up one level if not the final layer
  386. if layer < depth then
  387. print("Moving up one layer")
  388. if not turtle.up() then
  389. print("Couldn't move up to the next layer")
  390. return false
  391. end
  392. end
  393.  
  394. -- Get more blocks if needed for the next layer
  395. if layer < depth and not ensureBlockSelected() then
  396. print("Unable to get more blocks for the next layer")
  397. return false
  398. end
  399. end
  400.  
  401. -- Return to the surface - we need to go up exactly the right number of blocks
  402. print("Returning to the surface")
  403. -- We went down (depth-1) blocks and then up (depth-1) blocks during layering
  404. -- So we should be at the initial depth already, no need for extra ascent
  405. -- But let's check in case we need to go up
  406. local current_depth = 0
  407. while true do
  408. if turtle.detectUp() then
  409. -- There's a block above, try to dig it
  410. turtle.digUp()
  411. if not turtle.up() then
  412. print("Warning: Couldn't return to the surface, path blocked")
  413. break
  414. end
  415. current_depth = current_depth + 1
  416. elseif not turtle.up() then
  417. -- We've hit the surface (likely water)
  418. break
  419. else
  420. current_depth = current_depth + 1
  421. end
  422.  
  423. -- Safety check to prevent infinite loop
  424. if current_depth >= 10 then
  425. print("Warning: Ascent limit reached, stopping")
  426. break
  427. end
  428. end
  429.  
  430. -- Return to original position (behind the chest)
  431. print("Moving back to original position")
  432. if not turtle.back() then
  433. print("Warning: Couldn't move back to original position")
  434. -- Try digging if necessary
  435. turtle.turnRight()
  436. turtle.turnRight()
  437. if turtle.detect() then
  438. turtle.dig()
  439. if not turtle.forward() then
  440. print("Failed to return to original position")
  441. return true -- Still consider the build successful
  442. end
  443. else
  444. print("Failed to return to original position")
  445. return true -- Still consider the build successful
  446. end
  447. -- Turn back to original orientation
  448. turtle.turnRight()
  449. turtle.turnRight()
  450. end
  451.  
  452. print("Successfully built all layers!")
  453. return true
  454. end
  455.  
  456. -- Parse command line arguments
  457. local args = {...}
  458. if #args < 3 then
  459. print("Usage: TurtleMakeUsableRoom <length> <width> <depth>")
  460. return
  461. end
  462.  
  463. local length = tonumber(args[1])
  464. local width = tonumber(args[2])
  465. local depth = tonumber(args[3])
  466.  
  467. if not length or not width or not depth then
  468. print("All arguments must be numbers!")
  469. return
  470. end
  471.  
  472. if length < 1 or width < 1 or depth < 1 then
  473. print("All dimensions must be positive numbers!")
  474. return
  475. end
  476.  
  477. print("Building an area of " .. length .. "x" .. width .. " blocks to a depth of " .. depth)
  478. local success = buildLayers(length, width, depth)
  479.  
  480. if success then
  481. print("Build completed successfully!")
  482. else
  483. print("Build failed. Please check the errors above.")
  484. end
  485.  
  486.  
  487.  
Add Comment
Please, Sign In to add comment