Advertisement
Martellini90

Tunnel con diramazioni

Jan 17th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.07 KB | None | 0 0
  1. local tunnelLength, branchLength, fuelNeeded, numBranch, enderchest, net, home, enderSlot, bucketSlot, receiverId, percentage, phase, phaseColor, fuelFound, homeFuel, startFuel;
  2. local arg={ ... };
  3.  
  4. enderchest=false;
  5. net=false;
  6. home=false;
  7. enderSlot=14;
  8. torchSlot=15
  9. bucketSlot=16;
  10. receiverId=-1;
  11. percentage=0;
  12. fuelFound=0;
  13. homeFuel=0;
  14. startFuel=turtle.getFuelLevel();
  15. phase="";
  16. phaseColor=colors.white;
  17.  
  18. local function setup() --Setup for the program
  19. local event,char
  20.  
  21. print("How long the tunnel should be?");
  22. tunnelLength=tonumber(io.read());
  23. print("How long a branch should be?");
  24. branchLength=tonumber(io.read());
  25.  
  26. if (enderchest)
  27. then
  28. if (turtle.getItemCount(enderSlot)==0)
  29. then
  30. print("\nPut an EnderChest (from EnderStorage) in slot "..tostring(enderSlot).."!\nPress any key to continue.");
  31. os.pullEvent("key");
  32. end
  33. else
  34. print("\nPut a large chest to the left of the turtle!\nPress any key to continue.");
  35. os.pullEvent("key");
  36. end
  37.  
  38. if (turtle.getItemCount(bucketSlot)==0)
  39. then
  40. print("\nPut an empty bucket in slot "..tostring(bucketSlot).."!\nPress any key to continue.");
  41. os.pullEvent("key");
  42. end
  43.  
  44. if (turtle.getItemCount(torchSlot)==0)
  45. then
  46. print("\nPut some torches in slot "..tostring(torchSlot).."!\nPress any key to continue.");
  47. os.pullEvent("key");
  48. end
  49.  
  50. sleep(0.2);
  51.  
  52. print("\nMain tunnel length: "..tunnelLength.."\nIs this correct? Y/N");
  53. event,char=os.pullEvent("char");
  54.  
  55. if(char=="n")
  56. then
  57. print("How long the tunnel should be?");
  58. tunnelLength=tonumber(io.read());
  59. end
  60.  
  61. sleep(0.2);
  62.  
  63. print("\nTunnel branches length: "..branchLength.."\nIs this correct? Y/N");
  64. event, char=os.pullEvent("char");
  65.  
  66. if(char=="n")
  67. then
  68. print("How long a branch should be?");
  69. branchLength=tonumber(io.read());
  70. end
  71. end
  72.  
  73. local function help() --Print the help guide
  74. print("To use this program the turtle must have a pickaxe on the left and a wireless modem to the right.")
  75. print("\nArguments:");
  76. print("\nhelp:\nDisplay this help guide.");
  77. print("\nstart:\nStart the program.");
  78. os.pullEvent("key");
  79. print("\nAdditional arguments:");
  80. print("\nenderchest:\nWhen the inventory is full, the Turtle places an Ender Chest to empty its inventory, otherwise it returns to home to empty its inventory. (Place a chest to the left of the turtle before running the program)");
  81. os.pullEvent("key");
  82. print("\nrednet id:\nSends data over the rednet. Replace \"id\" with the id of the receiver computer. Get the receiver command: pastebin get R89UWx10 BMReceiver");
  83. os.pullEvent("key");
  84. print("\nhome:\nMakes the turtle go at the start position after a server restart.");
  85. os.pullEvent("key");
  86. print("\n\nExample:\nBranchMine start enderchest rednet 10");
  87. print("\nThe Turtle will place down the EnderChest (from EnderStorage) to empty its inventory, and the data will be sent to the pc with id 10.")
  88. end
  89.  
  90. local function sendData() --Send the data to the receiver
  91. if (not net)
  92. then
  93. return
  94. end
  95.  
  96. percentage=(((startFuel-turtle.getFuelLevel())-homeFuel)*100)/fuelNeeded;
  97. msg=""..percentage..","..phase..","..phaseColor..","..turtle.getFuelLevel()..","..fuelFound;
  98. rednet.send(receiverId, msg);
  99. end
  100.  
  101. local function checkFull() --Check if inventory is full
  102.  
  103. if (not enderchest)
  104. then
  105. numb=14;
  106. else
  107. numb=13;
  108. end
  109.  
  110. for i=1,numb
  111. do
  112. if (turtle.getItemCount(i)==0)
  113. then
  114. return false;
  115. end
  116. end
  117.  
  118. return true;
  119. end
  120.  
  121. local function updateFile() --Update the file
  122. local file, str, _net, _enderchest;
  123.  
  124. if (not home)
  125. then
  126. return;
  127. end
  128.  
  129. _net=0;
  130. _enderchest=0;
  131.  
  132. if (net)
  133. then
  134. _net=1;
  135. end
  136.  
  137. if (enderchest)
  138. then
  139. _enderchest=1;
  140. end
  141.  
  142. file=fs.open("BMStatus", "w");
  143.  
  144. str=advMovAPI.getX()..","..advMovAPI.getY()..","..advMovAPI.getDir()..",".._enderchest..",".._net..","..receiverId..","..fuelFound..","..percentage;
  145.  
  146. file.write(str);
  147.  
  148. file.close();
  149. end
  150.  
  151. local function turnL()
  152. advMovAPI.turnL();
  153. updateFile();
  154. end
  155.  
  156. local function turnR()
  157. advMovAPI.turnR();
  158. updateFile();
  159. end
  160.  
  161.  
  162. local function returnBack(x, y, dir) --Go to coords
  163.  
  164. while(advMovAPI.getY()~=y)
  165. do
  166. advMovAPI.forward(true);
  167. homeFuel=homeFuel+1;
  168. end
  169.  
  170. if (advMovAPI.getX()<x)
  171. then
  172. turnR();
  173. elseif(advMovAPI.getX()>x)
  174. then
  175. turnL();
  176. end
  177.  
  178. while(advMovAPI.getX()~=x)
  179. do
  180. advMovAPI.forward(true);
  181. homeFuel=homeFuel+1;
  182. end
  183.  
  184. while(advMovAPI.getDir()>dir)
  185. do
  186. turnR();
  187. end
  188.  
  189. while(advMovAPI.getDir()<dir)
  190. do
  191. turnL();
  192. end
  193. end
  194.  
  195. local function dropAll(ender) --Drop all item in the inventory
  196. local slot, numb, prevSlot, prevPhase;
  197.  
  198. prevSlot=turtle.getSelectedSlot();
  199.  
  200. if (not ender)
  201. then
  202. numb=14;
  203. else
  204. numb=13;
  205. end
  206.  
  207. for slot=1,numb
  208. do
  209. turtle.select(slot);
  210. phaseColor=colors.yellow;
  211. phase="Droppping Item";
  212. sendData();
  213.  
  214. if (ender)
  215. then
  216. prevPhase=phase;
  217. while (not turtle.drop())
  218. do
  219. if (turtle.getItemCount(slot)==0)
  220. then
  221. break;
  222. end
  223.  
  224. sleep(2);
  225. phaseColor=colors.red;
  226. phase="EnderChest full!";
  227. sendData();
  228. phase=prevPhase;
  229. end
  230. else
  231. prevPhase=phase;
  232. while (not turtle.drop())
  233. do
  234. if (turtle.getItemCount(slot)==0)
  235. then
  236. break;
  237. end
  238.  
  239. sleep(2);
  240. phaseColor=colors.red;
  241. phase="Home chest full!";
  242. sendData();
  243. phase=prevPhase;
  244. end
  245. end
  246. end
  247.  
  248. phaseColor=colors.white;
  249. turtle.select(prevSlot);
  250. end
  251.  
  252. local function returnHome() --return home to do different tasks
  253. local x, y, dir;
  254.  
  255. x=advMovAPI.getX();
  256. y=advMovAPI.getY();
  257. dir=advMovAPI.getDir();
  258.  
  259. if (advMovAPI.getX()>0)
  260. then
  261. if (advMovAPI.getDir()==0)
  262. then
  263. turnL();
  264. turnL();
  265. elseif (advMovAPI.getDir()==1)
  266. then
  267. turnL();
  268. elseif (advMovAPI.getDir()==3)
  269. then
  270. turnR();
  271. end
  272. elseif (advMovAPI.getX()<0)
  273. then
  274. if (advMovAPI.getDir()==2)
  275. then
  276. turnL();
  277. turnL();
  278. elseif (advMovAPI.getDir()==1)
  279. then
  280. turnR();
  281. elseif (advMovAPI.getDir()==3)
  282. then
  283. turnL();
  284. end
  285. end
  286.  
  287. while(advMovAPI.getX()~=0)
  288. do
  289. advMovAPI.forward(true);
  290. homeFuel=homeFuel+1;
  291. end
  292.  
  293. if (advMovAPI.getY()>0)
  294. then
  295. if (advMovAPI.getDir()==1)
  296. then
  297. turnL();
  298. turnL();
  299. elseif (advMovAPI.getDir()==2)
  300. then
  301. turnL();
  302. elseif (advMovAPI.getDir()==0)
  303. then
  304. turnR();
  305. end
  306. elseif (advMovAPI.getY()<0)--This is useless...
  307. then
  308. if (advMovAPI.getDir()==3)
  309. then
  310. turnL();
  311. turnL();
  312. elseif (advMovAPI.getDir()==2)
  313. then
  314. turnR();
  315. elseif (advMovAPI.getDir()==0)
  316. then
  317. turnL();
  318. end
  319. end
  320.  
  321. while(advMovAPI.getY()~=0)
  322. do
  323. advMovAPI.forward(true);
  324. homeFuel=homeFuel+1;
  325. end
  326.  
  327. if (advMovAPI.getDir()==0)
  328. then
  329. turnL();
  330. elseif (advMovAPI.getDir()==2)
  331. then
  332. turnR();
  333. elseif (advMovAPI.getDir()==3)
  334. then
  335. turnR();
  336. turnR();
  337. end
  338.  
  339. turnL();
  340. dropAll(false);
  341. turnR();
  342. returnBack(x, y, dir);
  343. end
  344.  
  345. local function pickUp() --Pick up all item from an inventory *unused
  346. local slot
  347.  
  348. slot=1;
  349. print("Taking seeds...");
  350.  
  351. for slot=1,16
  352. do
  353. turtle.select(slot);
  354. turtle.suck(64);
  355. end
  356. end
  357.  
  358. local function select() --Select the first not empty slot *unused
  359. local slot
  360.  
  361. for slot=1,16
  362. do
  363. if (turtle.getItemCount(slot)>0)
  364. then
  365. turtle.select(slot);
  366. return;
  367. end
  368. end
  369. end
  370.  
  371. local function placeTorch()
  372. local prevSlot;
  373.  
  374. prevSlot=turtle.getSelectedSlot();
  375. turtle.select(torchSlot);
  376. turtle.placeUp();
  377. turtle.select(prevSlot);
  378. end
  379.  
  380. local function checkForLiquid() --Check for lava in front of it
  381. local prevSlot,full,fuel;
  382.  
  383. prevSlot=turtle.getSelectedSlot();
  384. turtle.select(bucketSlot);
  385.  
  386. if (tonumber(turtle.getFuelLimit())==nil)
  387. then
  388. return;
  389. end
  390.  
  391. if (turtle.place())
  392. then
  393. fuel=turtle.getFuelLevel();
  394.  
  395. if (turtle.refuel())
  396. then
  397. if (not full)
  398. then
  399. startFuel=startFuel+(turtle.getFuelLevel()-fuel);
  400. fuelFound=fuelFound+(turtle.getFuelLevel()-fuel);
  401. end
  402. else
  403. turtle.place();
  404. end
  405. end
  406.  
  407. turtle.select(prevSlot);
  408. end
  409.  
  410. local function refresh() --Do different tasks
  411. local prevSlot;
  412.  
  413. updateFile();
  414. sendData();
  415. checkForLiquid();
  416.  
  417. if (checkFull())
  418. then
  419. if (enderchest)
  420. then
  421. prevSlot=turtle.getSelectedSlot();
  422. turtle.select(enderSlot);
  423.  
  424. while(not turtle.place())
  425. do
  426. turtle.dig();
  427. turtle.attack();
  428. end
  429.  
  430. dropAll(true);
  431. turtle.dig();
  432. turtle.select(prevSlot);
  433. else
  434. returnHome();
  435. end
  436. end
  437. end
  438.  
  439. local function digTunnel() --Dig the main tunnel
  440. phase="Main tunnel";
  441.  
  442. for i=0,tunnelLength
  443. do
  444. if (i<tunnelLength)
  445. then
  446. turtle.dig();
  447. end
  448.  
  449. turtle.digUp();
  450. turtle.Up(1);
  451. turtle.digUp();
  452.  
  453. if (i<tunnelLength)
  454. then
  455. advMovAPI.forward(true);
  456. refresh();
  457. end
  458.  
  459. end
  460.  
  461. turnR();
  462. turtle.dig();
  463. advMovAPI.forward(true);
  464. turnR();
  465. refresh();
  466.  
  467. for i=0,tunnelLength
  468. do
  469. if (i<tunnelLength)
  470. then
  471. turtle.dig();
  472. end
  473.  
  474. turtle.digUp();
  475.  
  476. if (i<tunnelLength)
  477. then
  478. advMovAPI.forward(true);
  479. refresh();
  480. end
  481.  
  482. end
  483.  
  484. turnR();
  485. advMovAPI.forward(true);
  486. turnR();
  487. refresh();
  488. end
  489.  
  490. local function digBranch() --Dig a branch
  491. torchTimer=10;
  492.  
  493. for i=0,branchLength
  494. do
  495. if (i<branchLength)
  496. then
  497. turtle.dig();
  498. end
  499.  
  500. turtle.digUp();
  501.  
  502. if (i<branchLength)
  503. then
  504. advMovAPI.forward(true);
  505. refresh();
  506. end
  507. end
  508.  
  509. turnL();
  510. turnL();
  511.  
  512. while (advMovAPI.getX()~=0)
  513. do
  514. if (torchTimer>=11)
  515. then
  516. placeTorch();
  517.  
  518. torchTimer=0;
  519. end
  520.  
  521. torchTimer=torchTimer+1;
  522.  
  523. if (advMovAPI.getX()==2 or advMovAPI.getX()==-1)
  524. then
  525. placeTorch();
  526. end
  527.  
  528. advMovAPI.forward(true);
  529. refresh();
  530. end
  531. end
  532.  
  533. local function digBranches() --Steps to dig branches consecutively
  534. for i=0,numBranch-1
  535. do
  536. phase="Branch number "..tostring(i+1);
  537. turnL();
  538. digBranch();
  539. advMovAPI.forward(true)
  540. digBranch();
  541. turnR();
  542. refresh();
  543.  
  544. if (i<numBranch-1)
  545. then
  546. advMovAPI.forward(true);
  547. advMovAPI.forward(true);
  548. advMovAPI.forward(true);
  549.  
  550. end
  551. refresh();
  552. end
  553.  
  554. while (advMovAPI.getY()>0)
  555. do
  556. advMovAPI.back();
  557. refresh();
  558. end
  559. end
  560.  
  561. ------------------------------------------------------
  562.  
  563. term.clear();
  564. term.setCursorPos(1, 1);
  565.  
  566. if (#arg>0)
  567. then
  568. if (arg[1]=="help")
  569. then
  570. help();
  571. return;
  572. elseif(arg[1]=="start")
  573. then
  574. for i=2,#arg
  575. do
  576. if (arg[i]=="enderchest")
  577. then
  578. enderchest=true;
  579. elseif (arg[i]=="rednet")
  580. then
  581. net=true;
  582. if (arg[i+1]~=nil)
  583. then
  584. receiverId=tonumber(arg[i+1]);
  585. i=i+2;
  586. else
  587. printError("You have to specify the id of the computer receiver after the argument \"rednet\".");
  588. return;
  589. end
  590.  
  591. rednet.open("right");
  592. elseif (arg[i]=="home")
  593. then
  594. home=true;
  595.  
  596. if (not fs.exists("startup"))
  597. then
  598. shell.run("pastebin", "get", "QSG5bWKr", "startup");
  599. end
  600. end
  601. end
  602. else
  603. error("Wrong arguments");
  604. end
  605. else
  606. printError("Wrong number of arguments")
  607. print("Use \"BranchMine help\" to display an help guide.")
  608. return;
  609. end
  610.  
  611. if (not fs.exists("SolidAPI/advMovAPI"))
  612. then
  613. printError("Could not find the API advMovAPI.");
  614. print("Trying to download it from pastebin.\nPress any key to continue.");
  615.  
  616. os.pullEvent("key");
  617. term.clear();
  618. term.setCursorPos(1, 1);
  619.  
  620. shell.run("pastebin", "get", "4XhYtBvC", "SolidAPI/advMovAPI");
  621.  
  622. print("Press any key to continue.");
  623. os.pullEvent("char");
  624. term.clear();
  625. term.setCursorPos(1, 1);
  626.  
  627. if (not fs.exists("SolidAPI/advMovAPI"))
  628. then
  629. error("Could not find advMovAPI. An error has occurred while downloading the API.");
  630. end
  631.  
  632. if (os.loadAPI("SolidAPI/advMovAPI"))
  633. then
  634. advMovAPI.setCoords(0, 0, 0);
  635. advMovAPI.setDir(1);
  636. else
  637. fs.delete("SolidAPI/advMovAPI");
  638. error("Could not load API \"advMovAPI\". Deleting the API...");
  639. end
  640. else
  641. if (os.loadAPI("SolidAPI/advMovAPI"))
  642. then
  643. advMovAPI.setCoords(0, 0, 0);
  644. advMovAPI.setDir(1);
  645. else
  646. fs.delete("SolidAPI/advMovAPI");
  647. error("Could not load API \"advMovAPI\". Deleting the API...");
  648. end
  649. end
  650.  
  651. term.clear();
  652. term.setCursorPos(1, 1);
  653.  
  654. setup();
  655.  
  656. term.clear();
  657. term.setCursorPos(1, 1);
  658.  
  659. numBranch=math.ceil(tunnelLength/3);
  660. fuelNeeded=(tunnelLength*2+2)+(2*numBranch)+((numBranch-1)*3)+((numBranch-1)*3)+(numBranch*branchLength*4)+2;--ft+2+fb
  661.  
  662. if (turtle.getFuelLevel()<fuelNeeded)
  663. then
  664. print("The fuel level is too low to run the program.\n");
  665. print("Needed: "..fuelNeeded);
  666. print("Current: "..turtle.getFuelLevel());
  667. print("\nIt is suggested to put more fuel than necessary to prevent an occasionally return to the starting position.");
  668. return;
  669. end
  670.  
  671. digTunnel();
  672. sendData();
  673. advMovAPI.forward(true);
  674. digBranches();
  675. sendData();
  676.  
  677. if (not enderchest)
  678. then
  679. turnL();
  680. dropAll(false);
  681. turnR();
  682. else
  683. turtle.select(enderSlot);
  684.  
  685. while(not turtle.place())
  686. do
  687. turtle.dig();
  688. turtle.attack();
  689. end
  690.  
  691. dropAll(true);
  692. turtle.dig();
  693. end
  694.  
  695. phase="Completed";
  696. phaseColor=colors.lime;
  697. sendData();
  698. fs.delete("BMStatus");
  699.  
  700. --TODO: more data send
  701.  
  702. --Original script by SolidSnake96AS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement