Advertisement
JakobM7

CreatePlatform

Mar 29th, 2025 (edited)
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.39 KB | None | 0 0
  1. -- Script for Computercraft
  2. -- Script to make a platform for the player to build on
  3. -- Input Parameters:
  4. -- 1. Length of the platform (y-axis)
  5. -- 2. Width of the platform (x-axis)
  6. -- 3. Depth of the platform (z-axis)
  7.  
  8. -- There are internal variables tracking the Coordinates and the Orientation. To ensure the Coordinates dont get messed up, there will be custom functions for moving and rotating the turtle that will be used everywhere in the Proram.
  9. -- From the orignal Facing: forward = positive y, right: positive x, down: negative z
  10. -- --> The Platform will span over the internal coordinates x: 0 to Length-1, y: 0 to Width-1, z: -1 to -Depth
  11. -- The rotation will be represented by the numbers 0,1,2,3 --> 0: original rotation, 1: right, 2: 180d, 3: left
  12.  
  13. --[[
  14.  
  15.  
  16. Needed Functions:
  17. function "checkBlocks":
  18. First the Turtle will check if there are still Blocks in selected Slot to be placed.
  19. - If There are: return.
  20. - If there are not: change to the next slot and check again. Repeat this until either all except for the last slot were Checked or a slot with Items was found.
  21. - If A slot with Items was found: Keep the selection on it and return.
  22. - If no Slot with items was found: select Slot 1 and call the function "getNewBlocks". After That return.
  23.  
  24. function "getNewBlocks":
  25. variables in this scope will cache the coordinates the Turtle started this function from.
  26. -> in Beginning: x_hold = x, y_hold = y, z_hold = z, rot_hold = rot
  27. The turtle will go to the coordinates x=0,y=-1,z=0 with the rotation 2 using the function "GoToPosition"
  28. ->GoToPosition(0,-1,0,2)
  29. The Turtle will pick blocks from the chest to fill the inventory up (except for the last slot of the inventory (It should stay empty)).
  30. ->turtle.select(1), suck, turtle.select(2), suck, .... , turtle.select(15), suck, turtle,select(1), return
  31. After that the Turtle will go to the Coordinates it started from and rotate to the original position.
  32. -> GoToPosition(x_hold,y_hold,z_hold,rot_hold)
  33. the function will return so that the turtle can complete its job.
  34.  
  35. function "GoToPosition"(targetX, targetY, targetZ, targetRot):
  36. togoX = targetX-x
  37. togoY = targetY-y
  38. togoZ = targetZ-z
  39. while togoZ !=0:
  40. if togoZ < 0:
  41. customDown
  42. elseif togoZ > 0:
  43. customUp
  44. togoZ = targetZ-z
  45.  
  46. while togoX != 0:
  47. local desiredRot
  48. if togoX > 0: desiredRot = 1
  49. if togoX < 0: desiredRot = 3
  50. rotateTo(desiredRot)
  51. customForward
  52. togoX = targetX-x
  53.  
  54. while togoY != 0:
  55. local desiredRot
  56. if togoY > 0: desiredRot = 0
  57. if togoY < 0: desiredRot = 2
  58. rotateTo(desiredRot)
  59. customForward
  60. togoY = targetY-y
  61.  
  62. rotateTo(targetRot)
  63. return
  64.  
  65. function "rotateTo"(desiredRot):
  66. toRot = desiredRot - rotation
  67. if toRot == 3: customLeft
  68. while rotation != desiredRotation:
  69. customRight
  70.  
  71.  
  72. custom Moving/Rotation:
  73. 1. customForward:
  74. turtle.forwards()
  75. if rotation = 0: y+=1
  76. if rotation = 1: x+=1
  77. if rotation = 2: y-=1
  78. if rotation = 3: x-=1
  79. customBackwards:
  80. turtle.backwards()
  81. if rotation = 0: y-=1
  82. if rotation = 1: x-=1
  83. if rotation = 2: y+=1
  84. if rotation = 3: x+=1
  85. customUp:
  86. turtle.up()
  87. z+=1
  88. customDown:
  89. turtle.down()
  90. z-=1
  91. customRight:
  92. turtle.turnRight()
  93. if rotation >= 3: rotation = 0
  94. else: rotation +=1
  95. customLeft:
  96. turtle.turnLeft()
  97. if rotation <= 0: rotation = 3
  98. else: rotation -=1
  99.  
  100. Outline:
  101. 1. turtle will check if it has enough Fuel for the job (return error and exit if not)
  102. - For checking the amount of fuel needed assume the turtle will need to walk 4 * Depth * length * Width blocks.
  103. 2. Turtle takes one step forward and sets the internal coordinates and Roation to x=0,y=0,z=0,rotation=0
  104. 3. Turtle calls function "getNewBlocks" to fill up most of the inventory
  105. 4. Turtle will start its job
  106. (After every placed block: the Turtle will call the function "checkBlocks")
  107. While 0 <= x <= Width-1:
  108. While 0 <= y <= Length-1:
  109. While 0 >= z >= -Depth+1:
  110. selectLastSlot(=slot 16)
  111. turtle.DigDown
  112. customDown
  113. throw away item from selected (last) slot (if there is one now)
  114. While 0 >= z >= -Depth+1:
  115. checkBlocks
  116. customUp
  117. turtle.PlaceDown
  118.  
  119. if y >= Length-1 and rotation == 0:
  120. customRight
  121. customForward
  122. customRight
  123. elseif y <= 0 and rotation == 2
  124. customLeft
  125. customForward
  126. customLeft
  127. else:
  128. customForward
  129.  
  130. 5. Turtle will return to original Position (GoToPosition(0,-1,0,0))
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137. ]]
  138.  
  139.  
  140.  
  141.  
  142. -- Initialize needed Variables:
  143.  
  144. local x = 0
  145. local y = 0
  146. local z = 0
  147. local rotation = 0
  148. local length = 0
  149. local width = 0
  150. local depth = 0
  151. local fuelNeeded = 0
  152.  
  153. --getting the input parameters:
  154.  
  155. local args = {...}
  156. if #args ~= 3 then
  157. print("Usage: turtleMakePlatform <length> <width> <depth>")
  158. return
  159. end
  160.  
  161. length = tonumber(args[1])
  162. width = tonumber(args[2])
  163. depth = tonumber(args[3])
  164. if length <= 0 or width <= 0 or depth <= 0 then
  165. print("Length, Width and Depth must be positive numbers.")
  166. return
  167. end
  168.  
  169. print("Creating a platform with dimensions: \nLength = " .. length .. ", Width = " .. width .. ", Depth = " .. depth)
  170.  
  171.  
  172. --[[
  173.  
  174. custom Moving/Rotation:
  175. 1. customForward:
  176. turtle.forwards()
  177. if rotation = 0: y+=1
  178. if rotation = 1: x+=1
  179. if rotation = 2: y-=1
  180. if rotation = 3: x-=1
  181. customBackwards:
  182. turtle.backwards()
  183. if rotation = 0: y-=1
  184. if rotation = 1: x-=1
  185. if rotation = 2: y+=1
  186. if rotation = 3: x+=1
  187. customUp:
  188. turtle.up()
  189. z+=1
  190. customDown:
  191. turtle.down()
  192. z-=1
  193. customRight:
  194. turtle.turnRight()
  195. if rotation >= 3: rotation = 0
  196. else: rotation +=1
  197. customLeft:
  198. turtle.turnLeft()
  199. if rotation <= 0: rotation = 3
  200. else: rotation -=1
  201.  
  202. ]]
  203.  
  204. function customForward()
  205. -- print("Moving forward")
  206. --check if the turtle can move forward
  207. if not turtle.forward() then
  208. print("Cannot move forward")
  209. return false
  210. end
  211. if rotation == 0 then
  212. y = y + 1
  213. elseif rotation == 1 then
  214. x = x + 1
  215. elseif rotation == 2 then
  216. y = y - 1
  217. elseif rotation == 3 then
  218. x = x - 1
  219. end
  220. end
  221.  
  222. function customBackwards()
  223. --print("Moving backwards")
  224. --check if the turtle can move backwards
  225. if not turtle.back() then
  226. print("Cannot move backwards")
  227. return false
  228. end
  229. if rotation == 0 then
  230. y = y - 1
  231. elseif rotation == 1 then
  232. x = x - 1
  233. elseif rotation == 2 then
  234. y = y + 1
  235. elseif rotation == 3 then
  236. x = x + 1
  237. end
  238. end
  239.  
  240. function customUp()
  241. -- print("Moving up")
  242. --check if the turtle can move up
  243. if not turtle.up() then
  244. print("Cannot move up")
  245. return false
  246. end
  247. z = z + 1
  248. end
  249.  
  250. function customDown()
  251. -- print("Moving down")
  252. --check if the turtle can move down
  253. if not turtle.down() then
  254. print("Cannot move down")
  255. return false
  256. end
  257. z = z - 1
  258. end
  259.  
  260. function customRight()
  261. turtle.turnRight()
  262. rotation = (rotation + 1) % 4
  263. end
  264.  
  265. function customLeft()
  266. turtle.turnLeft()
  267. rotation = (rotation - 1) % 4
  268. end
  269.  
  270.  
  271.  
  272.  
  273. --[[
  274.  
  275. function "rotateTo"(desiredRot):
  276. toRot = desiredRot - rotation
  277. if toRot == 3: customLeft
  278. while rotation != desiredRotation:
  279. customRight
  280.  
  281. ]]
  282.  
  283. function rotateTo(desiredRot)
  284. local toRot = (desiredRot - rotation) % 4
  285. if toRot == 3 then
  286. customLeft()
  287. end
  288.  
  289. while rotation ~= desiredRot do
  290. customRight()
  291. end
  292. end
  293.  
  294. --[[
  295. function "GoToPosition"(targetX, targetY, targetZ, targetRot):
  296. togoX = targetX-x
  297. togoY = targetY-y
  298. togoZ = targetZ-z
  299. while togoZ !=0:
  300. if togoZ < 0:
  301. customDown
  302. elseif togoZ > 0:
  303. customUp
  304. togoZ = targetZ-z
  305.  
  306. while togoX != 0:
  307. local desiredRot
  308. if togoX > 0: desiredRot = 1
  309. if togoX < 0: desiredRot = 3
  310. rotateTo(desiredRot)
  311. customForward
  312. togoX = targetX-x
  313.  
  314. while togoY != 0:
  315. local desiredRot
  316. if togoY > 0: desiredRot = 0
  317. if togoY < 0: desiredRot = 2
  318. rotateTo(desiredRot)
  319. customForward
  320. togoY = targetY-y
  321.  
  322. rotateTo(targetRot)
  323. return
  324. ]]
  325.  
  326. function goToPosition(targetX, targetY, targetZ, targetRot)
  327. print("Going to position: " .. targetX .. ", " .. targetY .. ", " .. targetZ .. ", " .. targetRot)
  328. local togoX = targetX - x
  329. local togoY = targetY - y
  330. local togoZ = targetZ - z
  331.  
  332. while togoZ ~= 0 do
  333. if togoZ < 0 then
  334. customDown()
  335. elseif togoZ > 0 then
  336. customUp()
  337. end
  338. togoZ = targetZ - z
  339. end
  340.  
  341. while togoX ~= 0 do
  342. local desiredRot = (togoX > 0) and 1 or 3
  343. rotateTo(desiredRot)
  344. customForward()
  345. togoX = targetX - x
  346. end
  347.  
  348. while togoY ~= 0 do
  349. local desiredRot = (togoY > 0) and 0 or 2
  350. rotateTo(desiredRot)
  351. customForward()
  352. togoY = targetY - y
  353. end
  354.  
  355. rotateTo(targetRot)
  356. end
  357.  
  358. -- goToPosition but with the difference that it goes in the opposite order -> first y, then x, then z
  359. function goToPositionBack(targetX, targetY, targetZ, targetRot)
  360. print("Going back to position: " .. targetX .. ", " .. targetY .. ", " .. targetZ .. ", " .. targetRot)
  361. local togoX = targetX - x
  362. local togoY = targetY - y
  363. local togoZ = targetZ - z
  364.  
  365. print("togoY: " .. togoY)
  366. while togoY ~= 0 do
  367. local desiredRot = (togoY > 0) and 0 or 2
  368. print("desiredRot: " .. desiredRot)
  369. rotateTo(desiredRot)
  370. customForward()
  371. togoY = targetY - y
  372. end
  373. print("togoX: " .. togoX)
  374. while togoX ~= 0 do
  375. local desiredRot = (togoX > 0) and 1 or 3
  376. print("desiredRot: " .. desiredRot)
  377. rotateTo(desiredRot)
  378. customForward()
  379. togoX = targetX - x
  380. end
  381.  
  382. print("togoZ: " .. togoZ)
  383. while togoZ ~= 0 do
  384. if togoZ < 0 then
  385. customDown()
  386. elseif togoZ > 0 then
  387. customUp()
  388. end
  389. togoZ = targetZ - z
  390. end
  391.  
  392. rotateTo(targetRot)
  393. end
  394.  
  395.  
  396.  
  397.  
  398.  
  399. --[[
  400.  
  401. function "getNewBlocks":
  402. variables in this scope will cache the coordinates the Turtle started this function from.
  403. -> in Beginning: x_hold = x, y_hold = y, z_hold = z, rot_hold = rot
  404. The turtle will go to the coordinates x=0,y=-1,z=0 with the rotation 2 using the function "GoToPosition"
  405. ->GoToPosition(0,-1,0,2)
  406. The Turtle will pick blocks from the chest to fill the inventory up (except for the last slot of the inventory (It should stay empty)).
  407. ->turtle.select(1), suck, turtle.select(2), suck, .... , turtle.select(15), suck, turtle,select(1), return
  408. After that the Turtle will go to the Coordinates it started from and rotate to the original position.
  409. -> GoToPosition(x_hold,y_hold,z_hold,rot_hold)
  410. the function will return so that the turtle can complete its job.
  411.  
  412. ]]
  413.  
  414. function getNewBlocks()
  415. print("Getting new blocks...")
  416. local x_hold = x
  417. local y_hold = y
  418. local z_hold = z
  419. local rot_hold = rotation
  420.  
  421. goToPosition(0, -1, 0, 2)
  422.  
  423. for i = 1, 15 do
  424. turtle.select(i)
  425. turtle.suck()
  426. end
  427.  
  428. turtle.select(1)
  429. goToPositionBack(x_hold, y_hold, z_hold, rot_hold)
  430. end
  431.  
  432. --[[
  433. function "checkBlocks":
  434. First the Turtle will check if there are still Blocks in selected Slot to be placed.
  435. - If There are: return.
  436. - If there are not: change to the next slot and check again. Repeat this until either all except for the last slot were Checked or a slot with Items was found.
  437. - If A slot with Items was found: Keep the selection on it and return.
  438. - If no Slot with items was found: select Slot 1 and call the function "getNewBlocks". After That return.
  439.  
  440. ]]
  441.  
  442. function checkBlocks()
  443. -- If current slot has blocks, we're good
  444. if turtle.getItemCount() > 0 then
  445. return
  446. end
  447.  
  448. -- Look for a slot with blocks
  449. for i = 1, 15 do
  450. turtle.select(i)
  451. if turtle.getItemCount() > 0 then
  452. return
  453. end
  454. end
  455.  
  456. -- If we got here, no blocks were found
  457. turtle.select(1)
  458. getNewBlocks()
  459. end
  460.  
  461.  
  462.  
  463. --[[
  464. 1. turtle will check if it has enough Fuel for the job (return error and exit if not)
  465. - For checking the amount of fuel needed assume the turtle will need to walk 4 * Depth * length * Width blocks.
  466. ]]
  467.  
  468. function checkFuel()
  469. fuelNeeded = 4 * depth * length * width
  470. if turtle.getFuelLevel() < fuelNeeded then
  471. print("Not enough fuel. Please refuel.")
  472. return false
  473. end
  474. return true
  475. end
  476.  
  477. if not checkFuel() then
  478. return
  479. end
  480.  
  481. -- 2. Turtle takes one step forward and sets the internal coordinates and Roation to x=0,y=0,z=0,rotation=0
  482.  
  483. function initializeTurtle()
  484. customForward()
  485. x = 0
  486. y = 0
  487. z = 0
  488. rotation = 0
  489. end
  490. initializeTurtle()
  491.  
  492. -- 3. Turtle calls function "getNewBlocks" to fill up most of the inventory
  493.  
  494. getNewBlocks()
  495.  
  496. --[[4. Turtle will start its job
  497. (After every placed block: the Turtle will call the function "checkBlocks")
  498. While 0 <= x <= Width-1:
  499. While 0 <= y <= Length-1:
  500. While 0 >= z >= -Depth+1:
  501. selectLastSlot(=slot 16)
  502. turtle.DigDown
  503. customDown
  504. throw away item from selected (last) slot (if there is one now)
  505. While 0 >= z >= -Depth+1:
  506. checkBlocks
  507. customUp
  508. turtle.PlaceDown
  509.  
  510. if y >= Length-1 and rotation == 0:
  511. customRight
  512. customForward
  513. customRight
  514. elseif y <= 0 and rotation == 2
  515. customLeft
  516. customForward
  517. customLeft
  518. else:
  519. customForward
  520.  
  521. ]]
  522.  
  523. function makePlatform()
  524. print("Making platform...")
  525. while y >= 0 and y <= length-1 and x >= 0 and x <= width-1 do
  526. print("y= " .. y)
  527. -- Dig down to the required depth
  528. while z <= 0 and z >= -depth+1 do
  529. turtle.select(16) -- Select the last slot (slot 16)
  530. turtle.digDown() -- Dig down
  531. customDown() -- Move down (this updates z)
  532. turtle.drop() -- Drop the item in the last slot (if there is one now)
  533. end
  534.  
  535. -- Place blocks while coming back up
  536. while z < 0 and z >= -depth do
  537. turtle.select(16) -- Select the last slot (slot 16)
  538. turtle.digUp() -- Dig up (if there is a block)
  539. checkBlocks()
  540. customUp() -- Move up (this updates z)
  541. turtle.placeDown() -- Place block down
  542. end
  543.  
  544. if y >= length - 1 and rotation == 0 then
  545. customRight()
  546. customForward()
  547. customRight()
  548. elseif y <= 0 and rotation == 2 then
  549. customLeft()
  550. customForward()
  551. customLeft()
  552. else
  553. customForward()
  554. end
  555. end
  556.  
  557. end
  558.  
  559. makePlatform()
  560.  
  561.  
  562.  
  563.  
  564. -- 5. Turtle will return to original Position (GoToPosition(0,-1,0,0))
  565.  
  566.  
  567. goToPosition(0, -1, 0, 2)
  568.  
  569. -- put all Items into the chest (unless the chest is full)
  570. for i = 1, 15 do
  571. turtle.select(i)
  572. turtle.drop()
  573. end
  574. turtle.select(1)
  575. customRight()
  576. customRight()
  577.  
  578. -- notification that the job is done
  579. print("Platform created successfully!")
  580.  
  581.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement