ComputerMan123

magicTurtle API (not made by me)

Sep 27th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.84 KB | None | 0 0
  1. --magicTurtle API by Liraal
  2. --version 1.31 14-03-2012
  3.  
  4. local c=false
  5. local x, y, z --starting pos
  6. local face=2 --make sure that the turtle's pointing north
  7. local autoupdate=false --make this true if you want to autoupdate everytime
  8. local version=1.31
  9. local updateID=11 --ID of terminal sending updates/commands
  10. local armingID=11 --ID of terminal arming the mine
  11. local disarmingID=11 --ID of disarming terminal
  12.  
  13. function version()
  14. return version
  15. end
  16.  
  17. function setMineID(arming, disarming)
  18. armingID=arming
  19. disarmingID=disarming
  20. end
  21.  
  22. function setUpdateID(a)
  23. updateID=a
  24. end
  25.  
  26. function savePos()
  27. if not fs.isDir("/pos") then fs.makeDir("/pos") end
  28. local f1=io.open("/pos/posx", "w")
  29. local f2=io.open("/pos/posy", "w")
  30. local f3=io.open("/pos/posz", "w")
  31. local f4=io.open("/pos/face", "w")
  32. f1:write(x)
  33. f2:write(y)
  34. f3:write(z)
  35. f4:write(face)
  36. f1:close()
  37. f2:close()
  38. f3:close()
  39. f4:close()
  40. return true
  41. end
  42.  
  43. function getPosF()
  44. local f1=io.open("pos/posx", "r")
  45. local f2=io.open("pos/posy", "r")
  46. local f3=io.open("pos/posz", "r")
  47. local f4=io.open("pos/face", "r")
  48. x=f1:read()
  49. y=f2:read()
  50. z=f3:read()
  51. face=f4:read()
  52. f1:close()
  53. f2:close()
  54. f3:close()
  55. f4:close()
  56. end
  57.  
  58. function getPos()
  59. return x,y,z
  60. end
  61.  
  62. function setNorth()
  63. face=2
  64. end
  65.  
  66. function setPos(a, b, c, ...)
  67. x=a
  68. y=b
  69. z=c
  70. if ...~=nil then face=... end
  71. savePos()
  72. return true
  73. end
  74.  
  75. function faceAdd(a)
  76. if face+a>3 then face=face+a-4 else face=face+a end
  77. end
  78.  
  79. function faceSub(a)
  80. if face-a<0 then face=4-face-a else face=face-a end
  81. end
  82.  
  83. function faceSouth()
  84. while face~=0 do turnLeft() end
  85. end
  86.  
  87. function faceEast()
  88. while face~=1 do turnLeft() end
  89. end
  90.  
  91. function faceNorth()
  92. while face~=2 do turnLeft() end
  93. end
  94.  
  95. function faceWest()
  96. while face~=3 do turnLeft() end
  97. end
  98.  
  99. function faceSet(a)
  100. while face~=a do turnLeft() sleep(0.2) end
  101. end
  102.  
  103. function turnLeft()
  104. turtle.turnLeft()
  105. faceAdd(1)
  106. return true
  107. end
  108.  
  109. function turnRight()
  110. turtle.turnRight()
  111. faceSub(1)
  112. end
  113.  
  114. function rotate(angle)
  115. if angle>0 then
  116. for i=1, angle, 1 do
  117. turnLeft()
  118. end
  119. else
  120. angle=math.abs(angle)
  121. for i=1, angle, 1 do
  122. turnRight()
  123. end
  124. end
  125. end
  126.  
  127. function getPos()
  128. return x, y, z
  129. end
  130.  
  131. function getFacing()
  132. return face
  133. end
  134.  
  135. function up()
  136. if y~=127 then
  137. local state=turtle.up()
  138. if state then y=y+1 end
  139. return state
  140. else return false
  141. end
  142. end
  143.  
  144. function down()
  145. if y~=1 then
  146. local state=turtle.down()
  147. if state then y=y-1 end
  148. return state
  149. else return false
  150. end
  151. end
  152.  
  153. function forward(a)
  154. local state=true
  155. if face==0 then for i=1, a, 1 do state=turtle.forward() if state then z=z+1 end end end
  156. if face==1 then for i=1, a, 1 do state=turtle.forward() if state then x=x+1 end end end
  157. if face==2 then for i=1, a, 1 do state=turtle.forward() if state then z=z-1 end end end
  158. if face==3 then for i=1, a, 1 do state=turtle.forward() if state then x=x-1 end end end
  159. return state
  160. end
  161.  
  162. function back(a)
  163. local state=true
  164. if face==2 then for i=1, a, 1 do state=turtle.forward() if state then z=z-1 end end end
  165. if face==3 then for i=1, a, 1 do state=turtle.forward() if state then x=x-1 end end end
  166. if face==0 then for i=1, a, 1 do state=turtle.forward() if state then z=z+1 end end end
  167. if face==1 then for i=1, a, 1 do state=turtle.forward() if state then x=x+1 end end end
  168. return state
  169. end
  170.  
  171. function goToPos(a, b, c, mode)
  172. faceEast()
  173. a=a-x
  174. b=b-y
  175. c=c-z
  176. if a>=0 then
  177. for i=1, a, 1 do
  178. while mode and turtle.detect() do turtle.dig() sleep(0.2) end
  179. forward(1)
  180. end
  181. else
  182. a=math.abs(a)
  183. rotate(2)
  184. for i=1, a, 1 do
  185. while mode and turtle.detect() do turtle.dig() sleep(0.2) end
  186. forward(1)
  187. end
  188. end
  189. if b>=0 then for i=1, b, 1 do
  190. while mode and turtle.detectUp() do turtle.digUp() sleep(0.2) end
  191. up()
  192. end
  193. else
  194. b=math.abs(b)
  195. for i=1, b, 1 do
  196. while mode and turtle.detectDown() do turtle.digDown() sleep(0.2) end
  197. down()
  198. end
  199. end
  200. faceSouth()
  201. if c>=0 then
  202. for i=1, c, 1 do
  203. while mode and turtle.detect() do turtle.dig() sleep(0.2) end
  204. forward(1)
  205. end
  206. else
  207. c=math.abs(c)
  208. rotate(2)
  209. for i=1, c, 1 do
  210. while mode and turtle.detect() do turtle.dig() sleep(0.2) end
  211. forward(1)
  212. end
  213. end
  214. return true
  215. end
  216.  
  217. function tunnel(w, h, l)
  218. local xs, ys, zs=getPos()
  219. local faces=face
  220. for k=1, l, 1 do
  221. for j=1, h, 1 do
  222. turnLeft()
  223. for i=1, w, 1 do
  224. turtle.dig()
  225. forward(1)
  226. end
  227. for i=1, w, 1 do back(1) end
  228. turnRight()
  229. if j<h then turtle.digDown() down() end
  230. end
  231. for j=1, h, 1 do if j<h then up() end end
  232. turtle.dig() forward(1)
  233. end
  234. goToPos(xs, ys, zs, false)
  235. faceSet(faces)
  236. return true
  237. end
  238.  
  239. function staircase(w, h, l)
  240. local xs, ys, zs=getPos()
  241. local faces=face
  242. for k=1, l, 1 do
  243. for j=1, h, 1 do
  244. turnLeft()
  245. for i=1, w, 1 do
  246. turtle.dig()
  247. forward(1)
  248. end
  249. for i=1, w, 1 do back(1) end
  250. turnRight()
  251. if j<h then turtle.digDown() down() end
  252. end
  253. for j=1, h, 1 do up() end
  254. turtle.dig() forward(1)
  255. end
  256. goToPos(xs, ys, zs, false)
  257. faceSet(faces)
  258. return true
  259. end
  260.  
  261. function wread()
  262. local _, y=term.getCursorPos()
  263. local tmp=read()
  264. local x, _=term.getCursorPos()
  265. term.setCursorPos(x,y)
  266. return tmp
  267. end
  268.  
  269. function build(blueprint)
  270. local l=#blueprint
  271. local h=#blueprint[1]
  272. local w=#blueprint[1][1]
  273. local xs, ys, zs=getPos()
  274. local faces=face
  275. for k=1, l, 1 do
  276. for j=1, h, 1 do
  277. turnLeft()
  278. for i=1, w, 1 do
  279. turtle.dig()
  280. forward(1)
  281. end
  282. for i=1, w, 1 do
  283. local block=blueprint[k][j][i]
  284. print(block)
  285. if block~=0 then turtle.select(block) turtle.place() end
  286. back(1)
  287. end
  288. turnRight()
  289. if j<h then turtle.digDown() down() end
  290. end
  291. for j=1, h, 1 do if j<h then up() end end
  292. turtle.dig() forward(1)
  293. end
  294. goToPos(xs, ys, zs, false)
  295. faceSet(faces)
  296. return true
  297. end
  298.  
  299. function sketch()
  300. print("Blueprint maker")
  301. print("Use turtle's slot IDs for data")
  302. print("Enter dimensions")
  303. write(" Width: ") local w=wread()
  304. write(" Height: ") local h=wread()
  305. write(" Length: ") local l=wread()
  306. local t={}
  307. for i=1, l, 1 do
  308. print("Vertical Layer ",i)
  309. local tt={}
  310. for j=1, h, 1 do
  311. print("Horizontal Row ", j)
  312. local ttt={}
  313. for k=1, w, 1 do
  314. local tmp=tonumber(wread())
  315. table.insert(ttt, tmp)
  316. write(" ")
  317. end
  318. table.insert(tt, ttt)
  319. end
  320. table.insert(t, tt)
  321. end
  322. return t
  323. end
  324.  
  325. function getUpdate(side)
  326. local a,b,c=os.pullEvent()
  327. while a~="rednet_message" or b~=updateID do a,b,c=os.pullEvent() end
  328. local tmp=loadstring(c)
  329. tmp()
  330. return true
  331. end
  332.  
  333. function getUpdateP(side)
  334. if peripheral.getType(side)=="disk" and fs.exists("/"..disk.getMountPath(side).."/magicTurtleUpdate/update") then
  335. shell.run("/"..disk.getMountPath(side).."/magicTurtleUpdate/update")
  336. return true
  337. end
  338. return false
  339. end
  340.  
  341. function event()
  342. local a,b,c=os.pullEvent()
  343. if a=="rednet_message" and b==updateID and c=="update" then getUpdate(b) return true end
  344. if a=="peripheral" then getUpdateP(b) return true end
  345. return false
  346. end
  347.  
  348. function updateSeek()
  349. rednet.open("right")
  350. local result = false
  351. while not result do result = event() end
  352. return true
  353. end
  354.  
  355. function lua()
  356. while true do
  357. write(">")
  358. local com=wread() print("")
  359. if com=="exit" then return true end
  360. if com=="menu" then menu() end
  361. if c then break end
  362. local exec=loadstring(com)
  363. exec()
  364. end
  365. end
  366.  
  367. function autoUpdate()
  368. autoupdate=true
  369. while true do
  370. parallel.waitForAny(lua, updateSeek)
  371. end
  372. return true
  373. end
  374.  
  375. function isSpace()
  376. for i=1, 9, 1 do
  377. if turtle.getItemCount(i)==0 then return true end
  378. end
  379. return false
  380. end
  381.  
  382. function diamondMine(l,...)
  383. local xs,ys,zs=getPos()
  384. local a,b,c=getPos()
  385. while b~=12 do turtle.dgDown() down() turtle.dig() turtle.digDown() forward(1) a,b,c=getPos() end
  386. while isSpace() do
  387. if ...~=nil then digSlot(...) else turtle.dig() end
  388. forward(1) turtle.digDown()
  389. turnLeft() for i=1, l, 1 do
  390. if ...~=nil then digSlot(...) else turtle.dig() end
  391. forward(1) end
  392. back(l)
  393. rotate(2)
  394. for i=1, l, 1 do
  395. if ...~=nil then digSlot(...) else turtle.dig() end
  396. forward(1) end
  397. back(l)
  398. end
  399. goToPos(xs,ys,zs, true)
  400. return true
  401. end
  402.  
  403. function mine(mode, l,...)
  404. if mode=="diamond" then diamondMine(l,...) end
  405. if mode=="regular" then tunnel(l, l, l) end
  406. if mode=="quarry" then quarry(l, ...) end
  407. return true
  408. end
  409.  
  410. function sendCommand(id, side, exec)
  411. rednet.open(side)
  412. rednet.send(id, "update")
  413. local state=rednet.send(id, exec)
  414. return state
  415. end
  416.  
  417. function fcoord()
  418. if face==0 then z=z+1 end
  419. if face==1 then x=x+1 end
  420. if face==2 then z=z-1 end
  421. if face==3 then x=x-1 end
  422. end
  423.  
  424. function lookAround()
  425. if not turtle.forward() and rs.getInput("front") then return false end
  426. fcoord()
  427. if not rs.getInput("bottom") then
  428. back(1)
  429. turnRight() forward(1)
  430. if not rs.getInput("bottom") then
  431. back(2) rotate(2)
  432. if not rs.getInput("bottom") then
  433. back(1) turnRight() return false
  434. else return true
  435. end
  436. else return true
  437. end
  438. else return true
  439. end
  440. end
  441.  
  442. function road()
  443. while true do
  444. if not lookAround() then
  445. up()
  446. if not lookAround() then
  447. if not turtle.forward() and not rs.getInput("bottom") then turnLeft() write(1)
  448. if not turtle.forward() and not rs.getInput("bottom") then rotate(2) write(2)
  449. if not turtle.forward() and not rs.getInput("bottom") then break end end end
  450. write(13)
  451. fcoord()
  452. down() down()
  453. if not lookAround() then up() break end
  454. end
  455. end
  456. end
  457. end
  458.  
  459. function obs()
  460. while true do
  461. if turtle.detect() then
  462. blow=true
  463. write(1)
  464. back(1)
  465. turtle.select(5)
  466. turtle.place()
  467. for i=1, 5, 1 do
  468. write(2)
  469. if not turtle.detectDown() then turtle.select(1) turtle.placeDown() end
  470. back(1)
  471. end
  472. forward(5)
  473. for i=1, 5, 1 do
  474. back(1)
  475. turtle.select(6)
  476. turtle.place()
  477. end
  478. rs.setOutput("front", true) back(1) rs.setOutput("front", false)
  479. back(3)
  480. sleep(2.5)
  481. forward(9)
  482. blow=false
  483. end
  484. sleep(0.2)
  485. end
  486. end
  487.  
  488. function blowup()
  489. local tmp
  490. local blow=false
  491. while true do
  492. print(blow)
  493. if not blow then tmp=loadstring(read()) else tmp=loadstring("") end
  494. parallel.waitForAll(tmp, obs)
  495. end
  496. return true
  497. end
  498.  
  499. function wander()
  500. while true do
  501. if (math.random(1,10))>5 then turnLeft()
  502. else turnRight() end
  503. for i=1, math.random(10, 30), 1 do if not turtle.detect() then forward(1) end end
  504. sleep(1)
  505. end end
  506.  
  507. function wanderer(exec)
  508. local wand=true
  509. while wand do
  510. local tmp=loadstring(exec)
  511. parallel.waitForAny(wander, tmp)
  512. end
  513. end
  514.  
  515. function jack(sap)
  516. local xs,ys,zs=getPos()
  517. turtle.select(sap)
  518. turtle.place()
  519. up()
  520. while not turtle.detect() do sleep(0.2) end
  521. back(2) turnRight() back(2) up() up() up() up()
  522. tunnel(5,5,5)
  523. goToPos(xs,ys,zs, true)
  524. end
  525.  
  526. function treefarm(sap)
  527. while true do jack(sap) end
  528. end
  529.  
  530. function watchUp()
  531. while true do
  532. if turtle.detectUp() then rs.setOutput("bottom", true) end
  533. sleep(0.5)
  534. end
  535. end
  536.  
  537. function disarm()
  538. while true do
  539. local a,b,c=os.pullEventRaw()
  540. if a=="rednet_message" and b==disarmingID and c=="disarm" then arm=false end
  541. sleep(0.5)
  542. end
  543. end
  544.  
  545. function landmine(slot, net)
  546. local arm
  547. while not turtle.detectDown() do down() end
  548. turtle.digDown() down() turtle.digDown()
  549. turtle.select(slot)
  550. turtle.placeDown()
  551. while true do
  552. if net then
  553. local a,b,c=os.pullEventRaw()
  554. while a~="rednet_message" or b~=armingID or c~="arming" do a,b,c=os.pullEventRaw() end
  555. end
  556. arm=true
  557. while arm do
  558. if net then
  559. parallel.waitForAny(watchUp, disarm)
  560. else
  561. watchUp()
  562. end
  563. end
  564. end
  565. turtle.digdown() up()
  566. end
  567.  
  568. function patch(slot, l, w)
  569. for i=1, l, 1 do
  570. turnLeft()
  571. for j=1, w, 1 do
  572. forward(1)
  573. if (turtle.getItemCount(slot))==0 then return false end
  574. turtle.select(slot)
  575. while not turtle.detectDown() do turtle.placeDown() end
  576. end
  577. back(w) turnRight() forward(1)
  578. end
  579. back(l)
  580. end
  581.  
  582. function guardArea(exec, w, h)
  583. local tmp=loadstring(exec)
  584. while true do
  585. for i=1, h, 1 do
  586. turnLeft()
  587. for j=1, w, 1 do
  588. forward(1)
  589. if not turtle.detectDown() then tmp() end
  590. if turtle.detect() then tmp() end
  591. end
  592. back(w) turnRight() forward(1)
  593. end
  594. back(h)
  595. end
  596. end
  597.  
  598. function remoteArm(id)
  599. return rednet.send(id, "arming")
  600. end
  601.  
  602. function remoteDisarm(id)
  603. return rednet.send(id, "disarming")
  604. end
  605.  
  606. function remoteSleep(id, t)
  607. remoteDisarm(id)
  608. sleep(t)
  609. remoteArm(id)
  610. end
  611.  
  612. function cDigDown()
  613. local it={}
  614. for i=1, 9, 1 do
  615. table.insert(it, turtle.getItemCount(i))
  616. end
  617. turtle.digDown()
  618. for i=1, 9, 1 do
  619. if it[i]<turtle.getItemCount(i) then return i end
  620. end
  621. return false
  622. end
  623.  
  624. function cDig()
  625. local it={}
  626. for i=1, 9, 1 do
  627. table.insert(it, turtle.getItemCount(i))
  628. end
  629. turtle.dig()
  630. for i=1, 9, 1 do
  631. if it[i]<(turtle.getItemCount(i)) then return i end
  632. end
  633. return false
  634. end
  635.  
  636. function camoMine(slot)
  637. local slot2=cDigDown()
  638. down() turtle.digDown()
  639. turtle.select(slot) turtle.placeDown() up()
  640. turtle.select(slot2) turtle.placeDown()
  641. return true
  642. end
  643.  
  644. function mineGrid(slot)
  645. for i=1, 5, 1 do
  646. turnRight()
  647. for j=1, 5, 1 do
  648. camoMine(slot)
  649. forward(3)
  650. end
  651. back(15)
  652. turnLeft()
  653. forward(3)
  654. end
  655. back(9) turnRight() forward(6) turnLeft()
  656. end
  657.  
  658. function playerDetect()
  659. if not turtle.forward() and not turtle.dig() then turtle.back() return true else return false end
  660. end
  661.  
  662. function follow()
  663. while not playerDetect() do
  664. turnRight() sleep(0.1)
  665. end
  666. forward(1)
  667. end
  668.  
  669. function follower()
  670. while true do
  671. follow()
  672. sleep(0.1)
  673. end
  674. end
  675.  
  676. function detectLeft()
  677. turnLeft()
  678. local a=turtle.detect()
  679. turnRight()
  680. return a
  681. end
  682.  
  683. function detectRight()
  684. turnRight()
  685. local a=turtle.detect()
  686. turnLeft()
  687. return a
  688. end
  689.  
  690. function detectBack()
  691. rotate(2)
  692. local a=turtle.detect()
  693. rotate(2)
  694. return a
  695. end
  696.  
  697. function detect(side)
  698. if side=="top" then return turtle.detectUp() end
  699. if side=="bottom" then return turtle.detectDown() end
  700. if side=="left" then return detectLeft() end
  701. if side=="right" then return detectRight() end
  702. if side=="back" then return detectBack() end
  703. if side=="front" then return turtle.detect() end
  704. return false
  705. end
  706.  
  707. function detectDir(f)
  708. local tmp=getFacing()
  709. faceSet(f)
  710. local tmp2=turtle.detect()
  711. faceSet(tmp)
  712. return tmp2
  713. end
  714.  
  715. function path(face)
  716. while true do
  717. while detectRight() and not turtle.detect() do
  718. forward(1)
  719. end
  720. if detectRight() and turtle.detect() then turnLeft() end
  721. if not detectRight() then if not forward(1) then turnRight() end elseif not turtle.detectUp() then up() else down() end
  722. if not detectDir(face) then break end
  723. end
  724. end
  725.  
  726. function digSlot(slot)
  727. local s=cDig()
  728. if s and s~=slot then turtle.select(s) turtle.drop() turtle.select(slot) else return false end
  729. return true
  730. end
  731.  
  732. function quarry(w, l)
  733. local run=true
  734. while run and isSpace() do
  735. for i=1, l, 1 do
  736. while turtle.detect() do turtle.dig() end
  737. turnRight()
  738. for j=1, w, 1 do
  739. local s1=turtle.digDown() forward(1)
  740. end
  741. back(w)
  742. turnLeft()
  743. local s2=down()
  744. end
  745. if not s1 and not s2 then run=false end
  746. end
  747. return true
  748. end
  749.  
  750. function stf(a,b)
  751. local f=io.open(b,"w")
  752. for i=1, #a,1 do
  753. local ai=a[i]
  754. local tmp2=""
  755. for j=1, #ai, 1 do
  756. local tmp=""
  757. for k=1, #ai[j], 1 do tmp=tmp..ai[j][k] end
  758. tmp=tmp.." "
  759. tmp2=tmp2..tmp tmp=""
  760. end
  761. f:write(tmp2) f:write("\n")
  762. end
  763. f:close()
  764. return true
  765. end
  766.  
  767. function ctl(a)
  768. local t={}
  769. local tt={}
  770. local ttt={}
  771. for k=1, #a, 1 do
  772. tmp2=a[k]
  773. for i=1, #tmp2, 1 do
  774. local tmp=tmp2[i]
  775. for j=1, string.len(tmp), 1 do table.insert(ttt, string.sub(tmp, j,j)) end
  776. table.insert(tt,ttt) ttt={}
  777. end
  778. table.insert(t,tt) tt={}
  779. end
  780. return t
  781. end
  782.  
  783. function stt(a)
  784. local t={}
  785. local tmp=""
  786. for i=1, string.len(a), 1 do
  787. if string.sub(a,i,i)==" " then
  788. table.insert(t,tmp) tmp=""
  789. else
  790. tmp=tmp..string.sub(a,i,i)
  791. end
  792. end
  793. if tmp~="" then table.insert(t,tmp) end
  794. return t
  795. end
  796.  
  797. function tff(name)
  798. local tmp=io.open(name,"r")
  799. local n={}
  800. for line in tmp:lines() do
  801. table.insert(n,stt(line))
  802. end
  803. return ctl(n)
  804. end
  805.  
  806. function architect(w, h, l)
  807. local blueprint={}
  808. local xs, ys, zs=getPos()
  809. local faces=face
  810. local row={}
  811. local layer={}
  812. for k=1, l, 1 do
  813. for j=1, h, 1 do
  814. turnLeft()
  815. for i=1, w, 1 do
  816. local tmp=cDig()
  817. if not tmp then tmp=0 end
  818. table.insert(row, tmp)
  819. forward(1)
  820. end
  821. for i=1, w, 1 do back(1) end
  822. turnRight()
  823. table.insert(layer, row) row={}
  824. if j<h then
  825. local tmp=cDigDown()
  826. if not tmp then tmp=0 end
  827. table.insert(row, tmp)
  828. down()
  829. end
  830. end
  831. for j=1, h, 1 do if j<h then up() end end
  832. local tmp=cDig()
  833. if not tmp then tmp=0 end
  834. table.insert(row, tmp)
  835. forward(1)
  836. table.insert(blueprint, layer) layer={}
  837. end
  838. goToPos(xs, ys, zs, false)
  839. faceSet(faces)
  840. return blueprint
  841. end
Add Comment
Please, Sign In to add comment