Advertisement
pedrosgali

API/move1.6

Dec 8th, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.95 KB | None | 0 0
  1. rednet.open("right")
  2. comName = os.getComputerLabel()
  3.  
  4. --GLOBAL VARIABLES--
  5.  
  6. digging = false
  7. fuelSlot = 1
  8. fuelMin = 80
  9. north = 0
  10. east = 1
  11. south = 2
  12. west = 3
  13. safezone = 80
  14. state = "idle"
  15. curSlot = 1
  16.  
  17. --REDNET FUNCTIONS--
  18.  
  19. function report(msg)
  20.     rednet.open("right")
  21.     clear()
  22.     print(msg)
  23.     state = msg
  24.     dumpLoc()
  25. end
  26.  
  27. function sendFile()
  28.     data = fs.open("API/mon", "r")
  29.     hostServer = tonumber(data.readLine())
  30.     data.close()
  31.     reportData = fs.open("loc", "r")
  32.     data = reportData.readAll()
  33.     reportData.close()
  34.     rednet.send(hostServer, data)
  35. end
  36.  
  37. --FILING FUNCTIONS--
  38.  
  39. function getLoc()
  40.     if fs.exists("loc") then
  41.         data = fs.open("loc", "r")
  42.             x = tonumber(data.readLine())
  43.             y = tonumber(data.readLine())
  44.             z = tonumber(data.readLine())
  45.             h = tonumber(data.readLine())
  46.             data.close()
  47.     else
  48.            x = homeX
  49.             y = homeY
  50.             z = homeZ
  51.             h = homeH
  52.         dumpLoc()
  53.     end
  54. end
  55.  
  56. function dumpLoc()
  57.     if fs.exists("loc") then
  58.     fs.delete("loc")
  59.     end
  60.     data = fs.open("loc", "w")
  61.     data.writeLine(tostring(x))
  62.     data.writeLine(tostring(y))
  63.     data.writeLine(tostring(z))
  64.     data.writeLine(tostring(h))
  65.         data.writeLine(tostring(comName))
  66.         data.writeLine(tostring(state))
  67.     data.close()
  68.     sleep(0.2)
  69.     sendFile()
  70. end
  71.  
  72. --MOVEMENT FUNCTIONS--
  73.  
  74. function refuel(reqFuel)
  75.     turtle.select(fuelSlot)
  76.     if reqFuel == nil then
  77.         reqFuel = fuelMin
  78.     end
  79.     if turtle.getItemCount(fuelSlot) < 16 then
  80.         if digging then
  81.             unload()
  82.         else
  83.             getFuel()
  84.         end
  85.     end
  86.     fuel = turtle.getFuelLevel()
  87.     while fuel < reqFuel do
  88.         if turtle.getItemCount(fuelSlot) <= 16 then
  89.             getFuel()
  90.         else
  91.             turtle.refuel(1)
  92.             fuel = turtle.getFuelLevel()
  93.         end
  94.     end
  95. end
  96.  
  97. function fd(dig)
  98.     getLoc()
  99.     refuel()
  100.     while true do
  101.         if dig == "dig" or dig == "d" or dig == "Dig" or dig == "D" then
  102.             turtle.dig()
  103.         elseif dig == nil then
  104.         end
  105.         if turtle.forward() then
  106.                 if h == 0 then
  107.                    z = z - 1
  108.                     elseif h == 1 then
  109.                         x = x + 1
  110.                 elseif h == 2 then
  111.                    z = z + 1
  112.                     elseif h == 3 then
  113.                         x = x - 1
  114.                     end
  115.             report("Moving.")
  116.             break
  117.             else
  118.                report("Blocked")
  119.             end
  120.     end
  121.     dumpLoc()
  122. end
  123.  
  124. function up(dig)
  125.     getLoc()
  126.     refuel()
  127.     while true do
  128.         if dig == "dig" or dig == "d" or dig == "Dig" or dig == "D" then
  129.             turtle.digUp()
  130.         elseif dig == nil then
  131.         end
  132.         if turtle.up() then
  133.             y = y + 1
  134.             report("Moving.")
  135.             break
  136.             else
  137.                 report("Blocked")
  138.             end
  139.     end
  140.     dumpLoc()
  141. end
  142.  
  143. function dn(dig)
  144.     getLoc()
  145.     refuel()
  146.     while true do
  147.         if dig == "dig" or dig == "d" or dig == "Dig" or dig == "D" then
  148.             turtle.digDown()
  149.         elseif dig == nil then
  150.         end
  151.         if turtle.down() then
  152.             y = y - 1
  153.             report("Moving.")
  154.             break
  155.             else
  156.             report("Blocked")
  157.             end
  158.     end
  159.     dumpLoc()
  160. end
  161.  
  162. function rt()
  163.     getLoc()
  164.     report("Turning.")
  165.     turtle.turnRight()
  166.     --report("Updating data file.")
  167.     h = (h + 1) % 4
  168.     dumpLoc()
  169. end
  170.  
  171. function lt()
  172.     getLoc()
  173.     turtle.turnLeft()
  174.     h = (h - 1) % 4
  175.     while h < 0 do
  176.         h = h + 4
  177.     end
  178.     dumpLoc()
  179. end
  180.  
  181. function spin()
  182.     rt()
  183.     rt()
  184. end
  185.  
  186. --ADVANCED MOVEMENT FUNCTIONS--
  187.  
  188. function gofd(dist)
  189.     if dist == nil then
  190.         if dig == "dig" or dig == "d" then
  191.             fd("d")
  192.         else
  193.             fd()
  194.         end
  195.     else
  196.            while dist > 0 do
  197.             if dig == "dig" or dig == "d" then
  198.                 fd("d")
  199.             else
  200.                     fd()
  201.             end
  202.             dist = dist - 1
  203.             end
  204.     end
  205. end
  206.  
  207. function face(b)
  208.     getLoc()
  209.     while true do
  210.         if b == h then
  211.             break
  212.         else
  213.             getLoc()
  214.             rt()
  215.             dumpLoc()
  216.             --report("Turning. "..tostring(h))
  217.         end
  218.         end
  219. end
  220.  
  221. function findX(pos, dig)
  222.     getLoc()
  223.     if x > pos then
  224.            face(west)
  225.             while x > pos do
  226.             if dig == nil then
  227.                     fd()
  228.                 getLoc()
  229.             else
  230.                 fd("dig")
  231.                 getLoc()
  232.                 end
  233.         end
  234.     elseif x < pos then
  235.            face(east)
  236.         while x < pos do
  237.             if dig == nil then
  238.                       fd()
  239.                 getLoc()
  240.             else
  241.                 fd("dig")
  242.                 getLoc()
  243.             end
  244.             end
  245.     else
  246.     end
  247. end
  248.  
  249. function findZ(pos, dig)
  250.     getLoc()
  251.     if z > pos then
  252.            face(north)
  253.             while z > pos do
  254.             if dig == nil then
  255.                       fd()
  256.                 getLoc()
  257.             else
  258.                 fd("dig")
  259.                 getLoc()
  260.             end
  261.             end
  262.     elseif z < pos then
  263.            face(south)
  264.         while z < pos do
  265.             if dig == nil then
  266.                       fd()
  267.                 getLoc()
  268.             else
  269.                 fd("dig")
  270.                 getLoc()
  271.             end
  272.             end
  273.     else
  274.     end
  275. end
  276.  
  277. function findY(height, dig)
  278.     getLoc()
  279.     if y > height then
  280.         while y > height do
  281.             if dig == nil then
  282.                    dn()
  283.                 getLoc()
  284.             else
  285.                 dn("dig")
  286.                 getLoc()
  287.             end
  288.            end
  289.     elseif y < height then
  290.         while y < height do
  291.             if dig == nil then
  292.                       up()
  293.                 getLoc()
  294.             else
  295.                 up("dig")
  296.                 getLoc()
  297.             end
  298.             end
  299.     else
  300.     end
  301. end
  302.  
  303. function moveTo(newx, newy, newz, heading, tunnel)
  304.     if tunnel == nil then
  305.         findY(safezone)
  306.         findX(newx)
  307.         findZ(newz)
  308.         findY(newy)
  309.     else
  310.         findY(safezone, "dig")
  311.         findX(newx)
  312.         findZ(newz)
  313.         findY(newy, "dig")
  314.     end
  315.     if heading == nil then
  316.         face(north)
  317.     else
  318.         face(heading)
  319.     end
  320.     report("idle")
  321. end
  322.  
  323. function goHome(dig)
  324.     data = fs.open("Locs/home", "r")
  325.     homeX = tonumber(data.readLine())
  326.     homeY = tonumber(data.readLine())
  327.     homeZ = tonumber(data.readLine())
  328.     homeH = tonumber(data.readLine())
  329.     data.close()
  330.     if dig == nil then
  331.         findY(safezone)
  332.     else
  333.         findY(safezone, "dig")
  334.     end
  335.     findZ(homeZ)
  336.     findX(homeX)
  337.     findY(homeY)
  338.     face(homeH)
  339.     report("Home.")
  340. end
  341.  
  342. function setTempHome()
  343.     data = fs.open("loc", "r")
  344.     tempHomeX = tonumber(data.readLine())
  345.     tempHomeY = tonumber(data.readLine())
  346.     tempHomeZ = tonumber(data.readLine())
  347.     tempHomeH = tonumber(data.readLine())
  348.     data.close()
  349. end
  350.  
  351. function returnTempHome()
  352.     findX(tempHomeX, "d")
  353.     findZ(tempHomeZ, "d")
  354.     findY(tempHomeY, "d")
  355.     face(tempHomeH)
  356. end
  357.  
  358. function setTemp()
  359.     data = fs.open("loc", "r")
  360.     tempX = tonumber(data.readLine())
  361.     tempY = tonumber(data.readLine())
  362.     tempZ = tonumber(data.readLine())
  363.     tempH = tonumber(data.readLine())
  364.     data.close()
  365. end
  366.  
  367. function returnTemp()
  368.     findX(tempX, "d")
  369.     findZ(tempZ, "d")
  370.     findY(tempY, "d")
  371.     face(tempH)
  372. end
  373.  
  374. function launch()
  375.     face(north)
  376.     fd()
  377.     findY(safezone, "d")
  378. end
  379.  
  380. function disable()
  381.     spin()
  382.     fd()
  383.     rt()
  384.     fd()
  385.     while true do
  386.         if turtle.detect() then
  387.             lt()
  388.             fd()
  389.             rt()
  390.         else
  391.             fd()
  392.             spin()
  393.             break
  394.         end
  395.     end
  396. end
  397.  
  398. function enable()
  399.     fd()
  400.     fd()
  401.     lt()
  402.     findZ(homeZ)
  403.     findX(homeX)
  404. end
  405.  
  406. --MINING FUNCTIONS--
  407.  
  408. function checkTurns()
  409.     if turns == "left" then
  410.         turns = 0
  411.     elseif turns == "right" then
  412.         turns = 1
  413.     end
  414.     if turns == 0 then
  415.         move.lt()
  416.         move.fd("d")
  417.         move.lt()
  418.         turns = 1
  419.     else
  420.         move.rt()
  421.         move.fd("d")
  422.         move.rt()
  423.         turns = 0
  424.     end
  425. end
  426.  
  427. function digLine(length, tunnel)
  428.     for i = 1, length do
  429.         if tunnel == nil then
  430.             checkFull()
  431.             fd("d")
  432.         else
  433.             checkFull()
  434.             turtle.digUp()
  435.             turtle.digDown()
  436.             fd("d")
  437.             turtle.digUp()
  438.             turtle.digDown()
  439.         end
  440.     end
  441. end
  442.  
  443. function digGrid(length, width, side, tunnel)
  444.     turns = side
  445.     if tunnel == nil then
  446.         for i = 1, width do
  447.             digLine(length - 1)
  448.             checkTurns()
  449.         end
  450.     else
  451.         for i = 1, width do
  452.             digLine(length - 1, "d")
  453.             if i ~= width then
  454.                 checkTurns()
  455.             end
  456.         end
  457.     end
  458. end
  459.  
  460. function digRoom(length, width, height, depth, side)
  461.     digging = true
  462.     findY(depth, "d")
  463.     setTempHome()
  464.     qRows = math.floor(height / 3)
  465.     rRows = height - (qRows * 3)
  466.     for i = 1, qRows do
  467.         turns = side
  468.         digGrid(length, width, side, "d")
  469.         if i == qRows then
  470.             tempHomeY = tempHomeY + 2
  471.         else
  472.             tempHomeY = tempHomeY + 3
  473.         end
  474.         returnTempHome()
  475.     end
  476.     for i = 1, rRows do
  477.         digGrid(length, width, side)
  478.         tempHomeY = tempHomeY + 1
  479.         returnTempHome()
  480.     end
  481.     unload()
  482.     digging = false
  483. end
  484.  
  485. --BUILDING FUNCTIONS--
  486.  
  487. function nextStack()
  488.     for i = 2, 16 do
  489.         turtle.select(i)
  490.         if turtle.getItemCount(i) > 0 then
  491.             break
  492.         end
  493.     end
  494. end
  495.  
  496. function placeLine(length)
  497.     length = length - 1
  498.     for i = 1, length do
  499.         nextStack()
  500.         turtle.placeDown()
  501.         if i ~= length then
  502.             fd()
  503.         end
  504.     end
  505. end
  506.  
  507. function placeGrid(length, width, side)
  508.     if side == nil then
  509.         side = "right"
  510.     end
  511.     for i = 1, width do
  512.         placeLine(width)
  513.         if i ~= width then
  514.             checkTurns()
  515.         end
  516.     end
  517. end
  518.  
  519. function build(length, width, height, hollow, side)
  520.     if side == nil then
  521.         side = "right"
  522.     end
  523.     setTempHome()
  524.     if hollow then
  525.         placeGrid(length, width, side)
  526.         tempHomeY = tempHomeY + 1
  527.         returnTempHome()
  528.         for i = 1, height - 2 do
  529.             for j = 1, 4 do
  530.                 placeLine(length)
  531.                 if side == "right" then
  532.                     rt()
  533.                 else
  534.                     lt()
  535.                 end
  536.             end
  537.             tempHomeY = tempHomeY + 1
  538.             returnTempHome()
  539.         end
  540.         placeGrid(length, width, side)
  541.         goHome()
  542.     else
  543.         for i = 1, height do
  544.             placeGrid(length, width)
  545.             tempHomeY = tempHomeY + 1
  546.             returnTempHome()
  547.         end
  548.     end
  549. end
  550.  
  551. --USEFUL FUNCTIONS--
  552.  
  553. function clear()
  554.     term.clear()
  555.     term.setCursorPos(1, 1)
  556. end
  557.  
  558. function select(slot)
  559.     turtle.select(slot)
  560.     curSlot = slot
  561. end
  562.  
  563. function place(slot, dir)
  564.     select(Slot)
  565.     if dir == "up" or dir =="u" then
  566.            turtle.placeUp()
  567.     elseif dir == "down" or dir == "d" then
  568.            turtle.placeDown()
  569.     elseif dir == nil then
  570.            turtle.place()
  571.     end
  572. end
  573.  
  574. function rest(count)
  575.     while count > 0 do
  576.         report("Sl: "..tostring(count).." sec.")
  577.         sleep(1)
  578.         count = count - 1
  579.     end
  580. end
  581.  
  582. function fill(slot)
  583.     select(slot)
  584.     while turtle.getItemCount(slot) < 64 do
  585.            turtle.suck()
  586.     end
  587.     for i = 1, 16 do
  588.         select(i)
  589.            if i == slot then
  590.             else
  591.                if turtle.compareTo(slot) then
  592.                       turtle.drop()
  593.                     end
  594.             end
  595.     end
  596. end
  597.  
  598. function emptyFrom(slot)
  599.     for i = slot, 16 do
  600.         select(i)
  601.             turtle.drop()
  602.     end
  603. end
  604.  
  605. function unload()
  606.         report("Full.")
  607.         sleep(5)
  608.         setTemp()
  609.             returnTempHome()
  610.         goHome()
  611.         rt()
  612.         emptyFrom(2)
  613.         spin()
  614.         fill(fuelSlot)
  615.         rt()
  616.         findY(safezone)
  617.         returnTempHome()
  618.             returnTemp()
  619.         select(1)
  620. end
  621.  
  622. function getFuel()
  623.         report("Refuel.")
  624.         sleep(5)
  625.         getLoc()
  626.         setTemp()
  627.         goHome("d")
  628.         lt()
  629.         fill(fuelSlot)
  630.         rt()
  631.         returnTemp()
  632.         select(1)
  633. end
  634.  
  635. function checkStack(tStack, tNum, altStack)
  636.     select(tStack)
  637.     if turtle.getItemCount(tStack) <= tNum then
  638.         select(altStack)
  639.     end
  640. end
  641.  
  642. function checkFull()
  643.     if turtle.getItemCount(16) >= 1 then
  644.         unload()
  645.         select(1)
  646.     end
  647. end
  648.  
  649. function checkAll()
  650.     count = 0
  651.     for i = 1, 16 do
  652.         select(i)
  653.         if turtle.getItemCount(i) > 0 then
  654.             break
  655.         end
  656.         count = count + 1
  657.     end
  658.     if count == 16 then
  659.         unload()
  660.     end
  661. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement