Advertisement
LeslieCraft

Turtle goto v0.8

Jan 26th, 2013
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function cls()
  2.     term.clear()
  3.     term.setCursorPos(1,1)
  4. end
  5.  
  6. function fileInstall()
  7.     fs.makeDir("save")
  8.     local posX = 0
  9.     local posY = 0
  10.     local posZ = 0
  11.     local posA = 0
  12.     local gotox = 0
  13.     local gotoy = 0
  14.     local gotoz = 0
  15.     local gotoa = 0
  16.     local file = fs.open("save/goto.cfg", "w")
  17.     file.writeLine("Turtle goto CFG file By Matteo")
  18.     file.writeLine("------------------------------")
  19.     file.writeLine("")
  20.     file.writeLine("[POSITION]")
  21.     file.writeLine("posX    =   " .. posX)
  22.     file.writeLine("posY    =   " .. posY)
  23.     file.writeLine("posZ    =   " .. posZ)
  24.     file.writeLine("face    =   " .. posA)
  25.     file.writeLine("")
  26.     file.writeLine("[GOTO POSITION]")
  27.     file.writeLine("gotox   =   " .. gotox)
  28.     file.writeLine("gotoy   =   " .. gotoy)
  29.     file.writeLine("gotoz   =   " .. gotoz)
  30.     file.writeLine("gotoa   =   " .. gotoa)
  31.     file.close()
  32. end
  33.  
  34. function saveData()
  35.     local file = fs.open("save/goto.cfg", "w")
  36.     file.writeLine("Turtle goto CFG file By Matteo")
  37.     file.writeLine("------------------------------")
  38.     file.writeLine("")
  39.     file.writeLine("[POSITION]")
  40.     file.writeLine("posX    =   " .. posX)
  41.     file.writeLine("posY    =   " .. posY)
  42.     file.writeLine("posZ    =   " .. posZ)
  43.     file.writeLine("face    =   " .. posA)
  44.     file.writeLine("")
  45.     file.writeLine("[GOTO POSITION]")
  46.     file.writeLine("gotox   =   " .. gotox)
  47.     file.writeLine("gotoy   =   " .. gotoy)
  48.     file.writeLine("gotoz   =   " .. gotoz)
  49.     file.writeLine("gotoa   =   " .. gotoa)
  50.     file.close()
  51.     cls()
  52.     printS2()
  53. end
  54.  
  55. function loadData()
  56.     if fs.exists("save/goto.cfg") then
  57.         local file = fs.open("save/goto.cfg", "r")
  58.         file.readLine()
  59.         file.readLine()
  60.         file.readLine()
  61.         file.readLine()
  62.         lx = file.readLine()
  63.         s1 = lx:find("=")
  64.         posX = tonumber(lx:sub(s1+2))
  65.         ly = file.readLine()
  66.         s1 = ly:find("=")
  67.         posY = tonumber(ly:sub(s1+2))
  68.         lz = file.readLine()
  69.         s1 = lz:find("=")
  70.         posZ = tonumber(lz:sub(s1+2))
  71.         f = file.readLine()
  72.         s1 = f:find("=")
  73.         posA = tonumber(f:sub(s1+2))
  74.        
  75.         file.readLine()
  76.         file.readLine()
  77.         lxgo = file.readLine()
  78.         s1 = lxgo:find("=")
  79.         gotox = tonumber(lxgo:sub(s1+2))
  80.         lygo = file.readLine()
  81.         s1 = lygo:find("=")
  82.         gotoy = tonumber(lygo:sub(s1+2))
  83.         lzgo = file.readLine()
  84.         s1 = lzgo:find("=")
  85.         gotoz = tonumber(lzgo:sub(s1+2))
  86.         fgo  = file.readLine()
  87.         s1 = fgo:find("=")
  88.         gotoa = tonumber(fgo:sub(s1+2))
  89.         file.close()
  90.     else
  91.         posX = "???"
  92.         posY = "???"
  93.         posZ = "???"
  94.         posA = "???"
  95.         cls()
  96.         print("Nincsenek meg a log file-ok.")
  97.         print("Eloszor az install reszt inditsd el.")
  98.         sleep(3)
  99.     end
  100. end
  101.  
  102. -----[[Move functions]]-----
  103.  
  104. function Tforward()
  105.     if turtle.forward() then
  106.         if posA == 0 then
  107.             loadData()
  108.             posZ = posZ + 1
  109.             saveData()
  110.         elseif posA == 1 then
  111.             loadData()
  112.             posX = posX - 1
  113.             saveData()
  114.         elseif posA == 2 then
  115.             loadData()
  116.             posZ = posZ - 1
  117.             saveData()
  118.         elseif posA == 3 then
  119.             loadData()
  120.             posX = posX + 1
  121.             saveData()
  122.         end
  123.     else
  124.         turtle.dig()
  125.         turtle.attack()
  126.         sleep(.4)
  127.         return Tforward()
  128.     end
  129. end
  130.  
  131. function Tback()
  132.     if turtle.back() then
  133.         if posA == 0 then
  134.             loadData()
  135.             posZ = posZ - 1
  136.             saveData()
  137.         elseif posA == 1 then
  138.             loadData()
  139.             posX = posX + 1
  140.             saveData()
  141.         elseif posA == 2 then
  142.             loadData()
  143.             posZ = posZ + 1
  144.             saveData()
  145.         elseif posA == 3 then
  146.             loadData()
  147.             posX = posX - 1
  148.             saveData()
  149.         end
  150.     else
  151.         turnL(2)
  152.         turtle.dig()
  153.         turtle.attack()
  154.         turnL(2)
  155.         return Tback()
  156.     end
  157. end
  158.  
  159. function Tup()
  160.     if turtle.up() then
  161.         loadData()
  162.         posY = posY + 1
  163.         saveData()
  164.     else
  165.         turtle.digUp()
  166.         turtle.attackUp()
  167.         return Tup()
  168.     end
  169. end
  170.  
  171. function Tdown()
  172.     if turtle.down() then
  173.         loadData()
  174.         posY = posY - 1
  175.         saveData()
  176.     else
  177.         turtle.digDown()
  178.         turtle.attackDown()
  179.         return Tdown()
  180.     end
  181. end
  182.  
  183. function TturnL()
  184.     if turtle.turnLeft() then
  185.         loadData()
  186.         posA = math.fmod(posA+3, 4)
  187.         saveData()
  188.     end
  189. end
  190.  
  191. function TturnR()
  192.     if turtle.turnRight() then
  193.         loadData()
  194.         posA = math.fmod(posA+1, 4)
  195.         saveData()
  196.     end
  197. end
  198.  
  199. -----[[Steps]]-----
  200.  
  201. function forward(forward)
  202.     for i = 1, forward do
  203.         Tforward()
  204.     end
  205. end
  206.  
  207. function back(back)
  208.     for i = 1, back do
  209.         Tback()
  210.     end
  211. end
  212.  
  213. function up(up)
  214.     for i = 1, up do
  215.         Tup()
  216.     end
  217. end
  218.  
  219. function down(down)
  220.     for i = 1, down do
  221.         Tdown()
  222.     end
  223. end
  224.  
  225. function turnL(TL)
  226.     for i = 1, TL do
  227.         TturnL()
  228.     end
  229. end
  230.  
  231. function turnR(TR)
  232.     for i = 1, TR do
  233.         TturnR()
  234.     end
  235. end
  236.  
  237. function printS()
  238.     if fs.exists("save/goto.cfg") == false then
  239.         print("Nincsenek meg a log file-ok.")
  240.         print("Eloszor az install reszt inditsd el.")
  241.         sleep(3)
  242.         return main()
  243.     else
  244.         loadData()
  245.         print("x: " .. posX)
  246.         print("y: " .. posY)
  247.         print("z: " .. posZ)
  248.         print("a: " .. posA)
  249.     end
  250. end
  251.  
  252. function goto(gotox, gotoy, gotoz, gotoa)
  253.     if posX < gotox then        -- X
  254.         if posA == 0 then
  255.             turnL(1)
  256.             forward(gotox-posX)
  257.         elseif  posA == 1 then
  258.             back(gotox-posX)
  259.         elseif posA == 2 then
  260.             turnR(1)
  261.             forward(gotox-posX)
  262.         elseif posA == 3 then
  263.             forward(gotox-posX)
  264.         end
  265.     elseif posX > gotox then
  266.         if posA == 0 then
  267.             turnL(1)
  268.             back(posX-gotox)
  269.         elseif posA == 1 then
  270.             forward(posX-gotox)
  271.         elseif posA == 2 then
  272.             turnR(1)
  273.             back(posX-gotox)
  274.         elseif posA == 3 then
  275.             back(posX-gotox)
  276.         end
  277.     end
  278.     if posY < gotoy then        -- Y
  279.         up(gotoy-posY)
  280.     else
  281.         down(posY-gotoy)
  282.     end
  283.     if posZ < gotoz then        -- Z
  284.         if posA == 0 then
  285.             forward(gotoz-posZ)
  286.         elseif posA == 1 then
  287.             turnL(1)
  288.             forward(gotoz-posZ)
  289.         elseif posA == 2 then
  290.             back(gotoz-posZ)
  291.         elseif posA == 3 then
  292.             turnR(1)
  293.             forward(gotoz-posZ)
  294.         end
  295.     elseif posZ > gotoz then
  296.         if posA == 0 then
  297.             back(posZ-gotoz)
  298.         elseif posA == 1 then
  299.             turnL(1)
  300.             back(posZ-gotoz)
  301.         elseif posA == 2 then
  302.             forward(posZ-gotoz)
  303.         elseif posA == 3 then
  304.             turnR(1)
  305.             back(posZ-gotoz)
  306.         end
  307.     end
  308.     local alpha = gotoa-posA+4
  309.     if gotoa == 999 then
  310.         alpha = 0
  311.     end
  312.     if math.fmod(alpha, 4) == 1 then --végső szögbe forgás
  313.         turnR(1)
  314.     elseif math.fmod(alpha, 4) == 2 then
  315.         turnR(2)
  316.     elseif math.fmod(alpha, 4) == 3 then
  317.         turnL(1)
  318.     end
  319. end
  320.  
  321. function printS2()
  322.     loadData()
  323.     term.clear()
  324.     term.setCursorPos(4,2)
  325.     print("x: ")
  326.     term.setCursorPos(4,3)
  327.     print("y: ")
  328.     term.setCursorPos(4,4)
  329.     print("z: ")
  330.     term.setCursorPos(4,5)
  331.     print("a: ")
  332.     term.setCursorPos(4,6)
  333.     print("Start")
  334.     term.setCursorPos(4,7)
  335.     print("Back")
  336.     term.setCursorPos(9,1)
  337.     print("In")
  338.     term.setCursorPos(15,1)
  339.     print("Go")    
  340.     term.setCursorPos(4,1)
  341.     print("Pos")
  342.     term.setCursorPos(21,1)
  343.     print("Set")
  344.     term.setCursorPos(9,2)
  345.     print( posX )
  346.     term.setCursorPos(9,3)
  347.     print( posY )
  348.     term.setCursorPos(9,4)
  349.     print( posZ )
  350.     term.setCursorPos(9,5)
  351.     print( posA )
  352.     term.setCursorPos(15,2)
  353.     print( gotox )
  354.     term.setCursorPos(15,3)
  355.     print( gotoy )
  356.     term.setCursorPos(15,4)
  357.     print( gotoz )
  358.     term.setCursorPos(15,5)
  359.     print( gotoa )
  360. end
  361.  
  362. -----[[ Menu ]]-----
  363.  
  364. -----[[ Local variables ]]-----
  365.  
  366. local termWidth, termHeight = term.getSize()
  367. local selectedItem = 1
  368. local inMainMenu = true
  369. local inGotoMenu = false
  370. local inWhereMenu = false
  371.  
  372. -----[[ Menu Methods ]]-----        --Menu
  373.  
  374. function Choice1()
  375.     cls()
  376.     inGotoMenu = true
  377.     selectedItem = 1
  378.     while inGotoMenu do
  379.         loadData()
  380.         term.clear()
  381.         term.setCursorPos(9,1)
  382.         print("In")
  383.         term.setCursorPos(15,1)
  384.         print("Go")    
  385.         term.setCursorPos(4,1)
  386.         print("Pos")
  387.         term.setCursorPos(21,1)
  388.         print("Set")
  389.         term.setCursorPos(9,2)
  390.         print( posX )
  391.         term.setCursorPos(9,3)
  392.         print( posY )
  393.         term.setCursorPos(9,4)
  394.         print( posZ )
  395.         term.setCursorPos(9,5)
  396.         print( posA )
  397.         term.setCursorPos(15,2)
  398.         print( gotox )
  399.         term.setCursorPos(15,3)
  400.         print( gotoy )
  401.         term.setCursorPos(15,4)
  402.         print( gotoz )
  403.         term.setCursorPos(15,5)
  404.         print( gotoa )
  405.         term.setCursorPos(1,2)
  406.         printMenu(gotoMenu)
  407.         event, key = os.pullEvent("key")
  408.         onKeyPressed(key, gotoMenu)
  409.     end
  410. end
  411.  
  412. function Choice2()
  413.     inWhereMenu = true
  414.     selectedItem = 1
  415.     while inWhereMenu do
  416.         loadData()
  417.         term.clear()
  418.         term.setCursorPos(9,1)
  419.         print("In")
  420.         term.setCursorPos(15,1)
  421.         print("Go")    
  422.         term.setCursorPos(4,1)
  423.         print("Pos")
  424.         term.setCursorPos(21,1)
  425.         print("Set")
  426.         term.setCursorPos(9,2)
  427.         print( posX )
  428.         term.setCursorPos(9,3)
  429.         print( posY )
  430.         term.setCursorPos(9,4)
  431.         print( posZ )
  432.         term.setCursorPos(9,5)
  433.         print( posA )
  434.         term.setCursorPos(15,2)
  435.         print( gotox )
  436.         term.setCursorPos(15,3)
  437.         print( gotoy )
  438.         term.setCursorPos(15,4)
  439.         print( gotoz )
  440.         term.setCursorPos(15,5)
  441.         print( gotoa )
  442.         term.setCursorPos(1,2)
  443.         printMenu(whereMenu)
  444.         event, key = os.pullEvent("key")
  445.         onKeyPressed(key, whereMenu)
  446.     end
  447. end
  448.  
  449. function Choice3()
  450.     cls()
  451.     fileInstall()
  452.     print("Log file-ok telepitve")
  453.     sleep(2)
  454. end
  455.  
  456. function Choice4()
  457.     cls()
  458.     printS()
  459.     print("Tovább [ENTER]")
  460.     id, key = os.pullEvent("key")
  461.     if key ~= keys.enter then
  462.         return Choice4()
  463.     end
  464. end
  465.  
  466. function Choice5()
  467.     cls()
  468.     print("Log file-ok torolve")
  469.     if fs.exists("save/goto.cfg") then
  470.         local file = fs.open("save/goto.cfg", "r")
  471.         file.close()
  472.         fs.delete("save")
  473.         print("Log Mappa es file-ok torolve")
  474.         sleep(3)
  475.     end
  476. end
  477.  
  478. function Choice6()
  479.     cls()
  480.     for i = 1, 16 do
  481.         turtle.select(i)
  482.         turtle.refuel()
  483.         cls()
  484.         print("Slot ellenőrizve: " .. i )
  485.         print("Uzemanyag szint: " .. turtle.getFuelLevel())
  486.     end
  487.     turtle.select(1)
  488. end
  489.  
  490. function Exit()
  491.     inMainMenu = false
  492. end
  493.  
  494. function Choicex()
  495.     term.setCursorPos(21,2)
  496.     gotox = tonumber(read())
  497.     if gotox == nil then
  498.         cls()
  499.         print("Csak szamot lehet megadni!")
  500.         sleep(3)
  501.     else
  502.         if gotox > (posX+100) then
  503.             gotox = posX
  504.         end
  505.         gotox = gotox + 0
  506.         selectedItem = 2
  507.         saveData()
  508.     end
  509. end
  510.  
  511. function Choicey()
  512.     term.setCursorPos(21,3)
  513.     gotoy = tonumber(read())
  514.     if gotoy == nil then
  515.         cls()
  516.         print("Csak szamot lehet megadni!")
  517.         sleep(3)
  518.     else
  519.         if gotoy > (posY+100) then
  520.             gotoy = posY
  521.         end
  522.         gotoy = gotoy + 0
  523.         selectedItem = 3
  524.         saveData()
  525.     end
  526. end
  527.  
  528. function Choicez()
  529.     term.setCursorPos(21,4)
  530.     gotoz = tonumber(read())
  531.     if gotoz == nil then
  532.         cls()
  533.         print("Csak szamot lehet megadni!")
  534.         sleep(3)
  535.     else
  536.         if gotoz > (posZ+100) then
  537.             gotoz = posZ
  538.         end
  539.         gotoz = gotoz + 0
  540.         selectedItem = 4
  541.         saveData()
  542.     end
  543. end
  544.  
  545. function Choicea()
  546.     term.setCursorPos(21,5)
  547.     gotoa = tonumber(read())
  548.     if gotoa == nil then
  549.         cls()
  550.         print("Csak szamot lehet megadni!")
  551.         sleep(3)
  552.     else
  553.         gotoa = math.fmod(gotoa, 4)
  554.         selectedItem = 5
  555.         saveData()
  556.     end
  557. end
  558.  
  559. function Wherex()
  560.     term.setCursorPos(21,2)
  561.     posX = tonumber(read())
  562.     if posX == nil then
  563.         cls()
  564.         print("Csak szamot lehet megadni!")
  565.         sleep(3)
  566.     else
  567.         posX = posX + 0
  568.         selectedItem = 2
  569.         saveData()
  570.     end
  571. end
  572.  
  573. function Wherey()
  574.     term.setCursorPos(21,3)
  575.     posY = tonumber(read())
  576.     if posY == nil then
  577.         cls()
  578.         print("Csak szamot lehet megadni!")
  579.         sleep(3)
  580.     else
  581.         posY = posY + 0
  582.         selectedItem = 3
  583.         saveData()
  584.     end
  585. end
  586.  
  587. function Wherez()
  588.     term.setCursorPos(21,4)
  589.     posZ = tonumber(read())
  590.     if posZ == nil then
  591.         cls()
  592.         print("Csak szamot lehet megadni!")
  593.         sleep(3)
  594.     else
  595.         posZ = posZ + 0
  596.         selectedItem = 4
  597.         saveData()
  598.     end
  599. end
  600.  
  601. function Wherea()
  602.     term.setCursorPos(21,5)
  603.     posA = tonumber(read())
  604.     if posA == nil then
  605.         cls()
  606.         print("Csak szamot lehet megadni!")
  607.         sleep(3)
  608.     else
  609.         posA = math.fmod(posA, 4)
  610.         selectedItem = 5
  611.         saveData()
  612.     end
  613. end
  614.  
  615. function Back()
  616.     selectedItem = 1
  617.     inGotoMenu = false
  618.     inWhereMenu = false
  619. end
  620.  
  621. function Start()
  622.     goto(gotox, gotoy, gotoz, gotoa)
  623. end
  624.  
  625. -----[[ Menu Definitions ]]-----
  626.  
  627. mainMenu = {
  628.     [1] = { text = "Goto", handler = Choice1 },
  629.     [2] = { text = "Hol vagyok", handler = Choice4 },
  630.     [3] = { text = "Kalibráció", handler = Choice2 },
  631.     [4] = { text = "Install", handler = Choice3 },
  632.     [5] = { text = "Uninstall", handler = Choice5 },
  633.     [6] = { text = "Tankolás", handler = Choice6 },
  634.     [7] = { text = "Exit", handler = Exit }
  635. }
  636.  
  637. gotoMenu = {
  638.     [1] = { text = "x: ", handler = Choicex },
  639.     [2] = { text = "y: ", handler = Choicey },
  640.     [3] = { text = "z: ", handler = Choicez },
  641.     [4] = { text = "a: ", handler = Choicea },
  642.     [5] = { text = "Start", handler = Start},
  643.     [6] = { text = "Back", handler = Back }
  644. }
  645.  
  646. whereMenu = {
  647.     [1] = { text = "x: ", handler = Wherex },
  648.     [2] = { text = "y: ", handler = Wherey },
  649.     [3] = { text = "z: ", handler = Wherez },
  650.     [4] = { text = "a: ", handler = Wherea },
  651.     [5] = { text = "Back", handler = Back }
  652. }
  653.  
  654. -----[[ Printing Methods ]]-----
  655.  
  656. function printMenu(menu)
  657.     for i=1,#menu do
  658.         if i == selectedItem then
  659.             print(">> "..menu[i].text)
  660.         else
  661.             print("   "..menu[i].text)
  662.         end
  663.     end
  664. end
  665.  
  666. -----[[ Handler Methods ]]-----
  667.  
  668. function onKeyPressed( key, menu )
  669.     if key == keys.enter then
  670.         onItemSelected(menu)
  671.     elseif key == keys.up then
  672.         if selectedItem > 1 then
  673.             selectedItem = selectedItem - 1
  674.         end
  675.     elseif key == keys.down then
  676.         if selectedItem < #menu then
  677.             selectedItem = selectedItem +1
  678.         end
  679.     end
  680. end
  681.  
  682. function onItemSelected(menu)
  683.     menu[selectedItem].handler()
  684. end
  685.  
  686. -----[[ Main Method ]]-----
  687.  
  688. function main()
  689.     while inMainMenu do
  690.         cls()
  691.         printMenu(mainMenu)
  692.         event, key = os.pullEvent("key")
  693.         onKeyPressed(key, mainMenu)
  694.     end
  695. end
  696.  
  697. -----[[Main program]]-----
  698.  
  699. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement