Advertisement
SufficingPit

Stairs CC

Mar 9th, 2024 (edited)
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.83 KB | None | 0 0
  1. local runtimeArgs = { ... };
  2.  
  3. local isReturningHome = false;
  4. local isTerminating = false;
  5.  
  6. xPos = 0;
  7. yPos = 0;
  8. zPos = 0;
  9. curDir = 0;
  10.  
  11. directions = {
  12. [0] = "forward",
  13. [1] = "right",
  14. [2] = "back",
  15. [3] = "left"
  16. }
  17.  
  18. turnDirs = {
  19. right = turtle.turnRight,
  20. left = turtle.turnLeft
  21. }
  22.  
  23. --[[function digDownSpecial()
  24. turtle.digDown();
  25. end]]
  26.  
  27. digDirs = {
  28. up = turtle.digUp,
  29. down = turtle.digDown,
  30. forward = turtle.dig,
  31. back = turtle.dig,
  32. left = turtle.dig,
  33. right = turtle.dig
  34. }
  35.  
  36. moveDirs = {
  37. forward = turtle.forward,
  38. back = turtle.back,
  39. up = turtle.up,
  40. down = turtle.down,
  41. }
  42.  
  43. function printCurDir()
  44. --print( "[+] Current Direction: " .. directions[curDir] .. " [-]");
  45. end
  46. function printCurPos()
  47. --print( "[+] Current Pos:");
  48. --print( " XPos- " .. xPos );
  49. --print( " YPos- " .. yPos );
  50. --print( "[-]" );
  51. end
  52. function printCurrentFuel()
  53. --print( "[+] Current Fuel Level: " .. turtle.getFuelLevel() .. " [-]");
  54. end
  55.  
  56. function selectItem( itemID )
  57. --print( "[+] Selecting Item..." );
  58.  
  59. for i = 1, 16, 1 do
  60.  
  61. curItem = turtle.getItemDetail( i ) or { name = "Empty" };
  62. --print( "Checking Slot: " .. i );
  63. --print( " - " .. curItem.name );
  64. if( curItem.name == itemID ) then
  65.  
  66. turtle.select( i );
  67. --print( "[-] Selected Item." );
  68. return true;
  69.  
  70. end
  71.  
  72. end
  73.  
  74. --print( "[-!] Failed Select Item." );
  75. return false;
  76. end
  77.  
  78. function terminateProgram()
  79. if( isTerminating ) then return end
  80. isTerminating = true;
  81. print( "!!! Terminating Program !!!" );
  82. print( "!!! Returning Home !!!" );
  83. returnHome();
  84. print( "!!! Terminated Program !!!" );
  85. sleep(1);
  86. os.queueEvent( "terminate" );
  87. sleep(1);
  88. os.queueEvent( "terminate" );
  89. end
  90.  
  91. local fuelPerCoal = 0;
  92. function refuel()
  93. --printCurrentFuel();
  94. --print( "[+] Refueling..." );
  95.  
  96. local returnHomeFuelAmount = math.abs( xPos ) + math.abs( yPos ) + math.abs( zPos ) + 5;
  97.  
  98. local numCoal = 0;
  99. if( selectItem( "minecraft:coal" ) ) then
  100. numCoal = turtle.getItemCount();
  101. end
  102.  
  103. local curFuel = turtle.getFuelLevel();
  104. local shouldRefuel = curFuel <= returnHomeFuelAmount;
  105.  
  106. if( fuelPerCoal == 0 and turtle.refuel(1) ) then
  107. fuelPerCoal = turtle.getFuelLevel() - curFuel;
  108. curFuel = turtle.getFuelLevel();
  109. end
  110.  
  111. if( numCoal * fuelPerCoal <= returnHomeFuelAmount ) then
  112. terminateProgram();
  113. end
  114.  
  115. if( shouldRefuel ) then
  116.  
  117. if( turtle.refuel(1) ) then
  118. --print( "[-] Refueled" );
  119. else
  120. --print( "[-!] Failed Refuel" );
  121. while( not turtle.refuel(1) and ( not isReturningHome or curFuel <= 0 ) ) do
  122. sleep( 0.5 );
  123. print( "!!! OUT OF FUEL !!!" );
  124. selectItem( "minecraft:coal" );
  125. end
  126. --terminateProgram();
  127. end
  128.  
  129. else
  130. --print( "[-] Refuel Not Needed" );
  131. end
  132. --printCurrentFuel()
  133. end
  134.  
  135. function directionValFix( dir )
  136. return math.abs( dir % 4 );
  137. end
  138.  
  139. function turn( dir )
  140. --printCurDir()
  141. --print( "[+] Turning: " .. dir .. "..." );
  142. dirVal = 1;
  143. if( dir == "left" ) then
  144. dirVal = -dirVal;
  145. end
  146.  
  147. curDir = directionValFix( curDir + dirVal );
  148. turnDirs[dir]();
  149.  
  150. --print( "[-] Turned: " .. dir .. "." );
  151. --printCurDir()
  152. end
  153.  
  154. local stopAddingToHistory = false;
  155. local moveHistory = {};
  156.  
  157. function move( dir )
  158. refuel();
  159. dropOffMaterials();
  160. --printCurPos();
  161. --print( "[+] Moveing: " .. dir .. "..." );
  162.  
  163. function actualMove( direct )
  164. digDirs[dir]();
  165. if( dir ~= "up" and dir ~= "down" ) then
  166. if( moveDirs[direct]() ) then
  167. --Forward
  168. if( curDir == 0 ) then
  169. zPos = zPos + 1;
  170. end
  171. -- Right
  172. if( curDir == 1 ) then
  173. xPos = xPos + 1;
  174. end
  175. -- Back
  176. if( curDir == 2 ) then
  177. zPos = zPos - 1;
  178. end
  179. --Left
  180. if( curDir == 3 ) then
  181. xPos = xPos - 1;
  182. end
  183. --print( "[-] Moved: " .. dir .. "." );
  184. else
  185. --print( "[-!] Failed Moved: " .. dir .. "." );
  186. end
  187. else
  188. if( moveDirs[direct]() ) then
  189. if( dir == "up" ) then
  190. yPos = yPos + 1;
  191. end
  192.  
  193. if ( dir == "down" ) then
  194. yPos = yPos - 1;
  195. end
  196.  
  197. --print( "[-] Moved: " .. dir .. "." );
  198. else
  199. --print( "[-!] Failed Moved: " .. dir .. "." );
  200. end
  201. end
  202. end
  203.  
  204. if( dir ~= "up" and dir ~= "down" ) then
  205. if( dir == "back" ) then
  206. turn( "right" );
  207. turn( "right" );
  208. end
  209.  
  210. if( dir == "right" or dir == "left" ) then
  211. turn( dir );
  212. end
  213.  
  214. actualMove( "forward" );
  215. else
  216. actualMove( dir );
  217. end
  218.  
  219. if( not stopAddingToHistory ) then
  220. table.insert( moveHistory, { x = xPos, y = yPos, z = zPos } );
  221. end
  222.  
  223. --printCurPos()
  224. end
  225.  
  226. placeDirs = {
  227. forward = turtle.place,
  228. up = turtle.placeUp,
  229. down = turtle.placeDown
  230. }
  231.  
  232. function build( dir, blockID )
  233. selectItem( blockID );
  234. return placeDirs[dir]();
  235. end
  236.  
  237. function faceDirection( dir )
  238. if( directions[directionValFix( curDir-1 )] == dir ) then
  239. turn( "left" );
  240. return;
  241. end
  242.  
  243. for i = 0, 3, 1 do
  244. if( directions[curDir] == dir ) then
  245. return;
  246. end
  247.  
  248. turn( "right" );
  249. end
  250. end
  251.  
  252. function moveDirection( dir )
  253. if( dir ~= "up" and dir ~= "down" ) then
  254. faceDirection( dir );
  255. move( "forward" );
  256. else
  257. move( dir );
  258. end
  259. end
  260.  
  261. function moveTo( x, y, z )
  262. function axisMove( axis, curVal, toVal )
  263. if( curVal ~= toVal ) then
  264.  
  265. changeVal = 1;
  266. if( curVal > toVal ) then
  267. changeVal = -1;
  268. end
  269.  
  270. for curAxisVal = curVal, toVal-changeVal, changeVal do
  271.  
  272. if( axis == "x" ) then
  273.  
  274. if( changeVal > 0 ) then
  275. moveDirection( "right" );
  276. else
  277. moveDirection( "left" );
  278. end
  279.  
  280. end
  281.  
  282. if( axis == "y" ) then
  283.  
  284. if( changeVal > 0 ) then
  285. moveDirection( "up" );
  286. else
  287. moveDirection( "down" );
  288. end
  289.  
  290. end
  291.  
  292. if( axis == "z" ) then
  293.  
  294. if( changeVal > 0 ) then
  295. moveDirection( "forward" );
  296. else
  297. moveDirection( "back" );
  298. end
  299.  
  300. end
  301.  
  302. end
  303.  
  304. end
  305.  
  306. end
  307.  
  308. axisMove( "y", yPos, y );
  309. axisMove( "x", xPos, x );
  310. axisMove( "z", zPos, z );
  311. end
  312.  
  313. function reverseMove()
  314. if( #moveHistory == 0 ) then return end
  315.  
  316. local lastMove = moveHistory[#moveHistory];
  317. moveHistory[#moveHistory] = nil;
  318.  
  319. moveTo( lastMove.x, lastMove.y, lastMove.z );
  320. end
  321.  
  322. function reverseAllMoves()
  323. stopAddingToHistory = true;
  324. while #moveHistory > 0 do
  325. reverseMove();
  326. end
  327. end
  328.  
  329. function toBool( val )
  330. return val == "true" or val == true;
  331. end
  332.  
  333. local buildingBlocks = {
  334. ["minecraft:cobblestone"] = true,
  335. ["minecraft:cobbled_deepslate"] = true,
  336. ["minecraft:netherrack"] = true
  337. }
  338.  
  339. local importantItems = {
  340. ["minecraft:coal"] = true
  341. }
  342.  
  343. for k, v in pairs(buildingBlocks) do table.insert(importantItems, v) end
  344.  
  345. function placeBuildingBlock( dir )
  346. for key, value in pairs(buildingBlocks) do
  347. if( build( dir, key ) ) then
  348. return true;
  349. end
  350. end
  351.  
  352. return false;
  353. end
  354.  
  355. function returnHome()
  356. if( isReturningHome ) then return end
  357. isReturningHome = true;
  358. reverseAllMoves();
  359. moveTo( 0, 0, 0 );
  360. faceDirection( "forward" );
  361. isReturningHome = false;
  362. end
  363.  
  364. local currentlyDroppingOffMaterials = false;
  365. function dropOffMaterials()
  366. if( currentlyDroppingOffMaterials ) then return false end
  367.  
  368. local isInventoryFilled = true;
  369. for i = 1, 16, 1 do
  370. local curItem = turtle.getItemDetail( i ) or { name = "Empty" };
  371. if( curItem.name == "Empty" ) then
  372. isInventoryFilled = false;
  373. break;
  374. end
  375. end
  376.  
  377. if( isInventoryFilled ) then
  378. currentlyDroppingOffMaterials = true;
  379.  
  380. local startData = { dir = curDir, x = xPos, y = yPos, z = zPos };
  381. returnHome();
  382.  
  383. local skippedBuildMat = false;
  384. local skippedCoal = false;
  385.  
  386. for i = 1, 16, 1 do
  387.  
  388. local curItem = turtle.getItemDetail( i ) or { name = "Empty" };
  389. local curID = curItem.name;
  390.  
  391. local isImportant = importantItems[curID];
  392. local shouldDrop = isImportant;
  393. if( shouldDrop ) then
  394. turtle.select( i );
  395. turtle.dropDown();
  396. end
  397. end
  398.  
  399. currentlyDroppingOffMaterials = false;
  400. moveTo( xPos, yPos, startData.z );
  401. moveTo( startData.x, yPos, zPos );
  402. moveTo( xPos, startData.y, zPos );
  403. faceDirection( directions[startData.dir] );
  404. end
  405. end
  406.  
  407. -- Start of Program
  408. local deep = tonumber( runtimeArgs[1] );
  409. local type = runtimeArgs[2];
  410.  
  411. -- Builds stairs down in a spiral pattern
  412. local function BuildSpiralStairs(deep)
  413. turn( "right" );
  414. for i = 1, deep, 1 do
  415. build( "down", "minecraft:cobblestone" );
  416.  
  417. move( "forward" );
  418. turtle.digUp();
  419.  
  420. turn( "right" );
  421. build( "forward", "minecraft:cobblestone" );
  422.  
  423. turn("left")
  424. turn("left")
  425. build( "forward", "minecraft:cobblestone" );
  426.  
  427. move( "down" );
  428. build( "down", "minecraft:cobblestone" );
  429. build( "forward", "minecraft:cobblestone" );
  430.  
  431. turn( "right" );
  432. turn( "right" );
  433. build( "forward", "minecraft:cobblestone" );
  434. turn("left")
  435.  
  436.  
  437. move( "forward" );
  438. turtle.digUp();
  439. build( "forward", "minecraft:cobblestone" );
  440.  
  441. turn( "right" );
  442. build( "forward", "minecraft:cobblestone" );
  443.  
  444. move( "down" );
  445. build( "forward", "minecraft:cobblestone" );
  446. build( "down", "minecraft:cobblestone" );
  447.  
  448. turn("left")
  449. build( "forward", "minecraft:cobblestone" );
  450.  
  451. turn( "left" );
  452. end
  453. end
  454.  
  455. local function BuildStrightStairs(deep)
  456. for i = 1, deep, 1 do
  457. turtle.digUp();
  458. build( "down", "minecraft:cobblestone" );
  459.  
  460. if(i == 1) then
  461. turn( "left" );
  462. end
  463. build( "forward", "minecraft:cobblestone" );
  464.  
  465. turn( "left" );
  466. turn( "left" );
  467.  
  468. stopAddingToHistory = true;
  469. move( "forward" );
  470. turtle.digUp();
  471. build( "down", "minecraft:cobblestone" );
  472. build( "forward", "minecraft:cobblestone" );
  473.  
  474. turn( "left" );
  475. move( "forward" );
  476. turtle.digUp();
  477.  
  478. turn( "left" );
  479. move( "forward" );
  480. turtle.digUp();
  481.  
  482. stopAddingToHistory = false;
  483. move( "down" );
  484. end
  485. end
  486.  
  487. local StrctureTypes = {
  488. ["spiral"] = BuildSpiralStairs,
  489. ["straight"] = BuildStrightStairs
  490. }
  491.  
  492. StrctureTypes[type](deep);
  493. -- End of Program
  494. dropOffMaterials();
  495. returnHome();
  496.  
  497. --[[
  498. - Turns once to either direction
  499. turn( "left" );
  500. turn( "right" );
  501.  
  502. - Fully turns til it reaches that direction based on the original orientation of the turtle when the program started
  503. faceDirection( "forward" );
  504. faceDirection( "back" );
  505. faceDirection( "right" );
  506. faceDirection( "left" );
  507.  
  508. - Moves turtle based on its current orientation
  509. move( "forward" );
  510. move( "back" );
  511. move( "right" );
  512. move( "left" );
  513. move( "up" );
  514. move( "down" );
  515.  
  516. - Moves turtle to inputed cords based on/to the original pos and direction the turtle was when program started
  517. (Will move on y axis first, then x, and last z.)
  518. moveTo( x, y, z );
  519.  
  520. - Moves turtle in direction based on the original orientation of the turtle when the program started
  521. moveDirection( "forward" );
  522. moveDirection( "back" );
  523. moveDirection( "right" );
  524. moveDirection( "left" );
  525. moveDirection( "up" );
  526. moveDirection( "down" );
  527.  
  528. - Searhes in inventory and selects inputed item, if the item doesn't exist it will terminate the program so things don't break.
  529. selectItem( itemID );
  530.  
  531. - places the block in whatever direction based on the turtle's current orientation
  532. build( "forward", blockID );
  533. build( "up", blockID );
  534. build( "down", blockID );
  535.  
  536. pastebin run Ymze4E42 5 straight
  537. pastebin run Ymze4E42 5 spiral
  538.  
  539. - To do
  540.  
  541.  
  542. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement