Advertisement
Guest User

OpenComputers Quarry

a guest
Apr 24th, 2015
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.53 KB | None | 0 0
  1. -- quarry for Opencomputers !!!in CFG must limitFlightHeight=256
  2. -- version 1.0.0
  3.  
  4.  
  5. local component = require("component")
  6. local computer = require("computer")
  7. local robot = require("robot")
  8. local shell = require("shell")
  9. local sides = require("sides")
  10. local term = require("term")
  11. local fs = require("filesystem")
  12. local process = require("process")
  13. local io = require("io")
  14.  
  15.  
  16.  
  17.  
  18. if not component.isAvailable("robot") then
  19.   io.stderr:write("can only run on robots")
  20.   return
  21. end
  22.  
  23.  
  24.  
  25. local direction = {
  26.     ["forw"]  = 1,
  27.     ["right"] = 2,
  28.     ["back"]  = 3,
  29.     ["left"]  = 4,
  30. }
  31. local dir = direction.forw
  32. local pos = {
  33.     ["x"]   = 0,
  34.     ["y"]   = 0,
  35.     ["z"]   = 0,
  36. }
  37.  
  38. local name = "quarry"
  39. local done = false
  40.  
  41. globalState = 0
  42. aState = 0 --состояние автомата
  43. tmpState = 0
  44. local lenght,width,height = 0,0,0
  45. local xt,yt,zt,dt = 0,0,0,direction.forw
  46. mode = 1
  47.  
  48.  
  49.  
  50.  
  51. local function savestate()
  52.  
  53.     name = process.running(1)..".save"
  54.    
  55.     file = io.open(name,"w")
  56.     file:write(mode.."\n"..globalState.."\n"..pos.x.."\n"..pos.y.."\n"..pos.z.."\n"..dir.."\n"..tmpState.."\n"..aState.."\n"..xt.."\n"..yt.."\n"..zt.."\n")
  57.     file:write(dt.."\n"..lenght.."\n"..width.."\n"..height.."\n"..tostring(done).."\n")
  58.     file:close()
  59.      
  60. end
  61.  
  62. local function readState()
  63.  
  64.         name = process.running(1)..".save"
  65.  
  66.     if not fs.exists(name) then
  67.         return false
  68.     end
  69.    
  70.  
  71.     file = io.open(name,"r")
  72.  
  73.     mode=file:read()+0
  74.     globalState=file:read()+0
  75.     pos.x=file:read()+0
  76.     pos.y=file:read()+0
  77.     pos.z=file:read()+0
  78.     dir=file:read()+0
  79.     tmpState=file:read()+0
  80.     aState=file:read()+0
  81.     xt=file:read()+0
  82.     yt=file:read()+0
  83.     zt=file:read()+0
  84.     dt=file:read()+0
  85.     lenght=file:read()+0
  86.     width=file:read()+0
  87.     height=file:read()+0
  88.     done=file:read()
  89.  
  90.  
  91.     file:close()
  92.  
  93.    
  94.     if done == "true" then
  95.         done = true
  96.     else
  97.         done = false
  98.     end
  99.  
  100.     return true
  101. end
  102.  
  103. local function setStartup()
  104.     if fs.exists("startup") then
  105. --      if fs.exists("startup.old") then
  106.             fs.remove("startup")
  107. --      else
  108. --          shell.run("move","startup","startup.old")
  109. --      end
  110.     end
  111.     name = process.running(1)
  112.     file = io.open("startup","w")
  113.     file:write("shell.run(\""..name.."\")")
  114.     file:close()
  115. end
  116.  
  117. local function delStartup()
  118.     if fs.exists("startup") then
  119.         fs.remove("startup")
  120.     end
  121. --  if fs.exists("startup.old") then
  122. --      shell.run("move","startup.old","startup")
  123. --  end
  124.     name = process.running(1)..".save"
  125.     if fs.exists(name) then
  126.         fs.remove(name)
  127.     end
  128. end
  129.  
  130.  
  131. local function inventoryFull()
  132.     local bFull = true
  133. --  for n=1,16 do
  134.     for n=1,8 do
  135.         if robot.count(n) == 0 then
  136.             bFull = false
  137.         end
  138.     end
  139.     if bFull then
  140.         print( "No empty slots left." )
  141.         return true
  142.     end
  143.     return false
  144. end
  145.  
  146. local function unload()
  147.     print( "Unloading items..." )
  148.     local n = 1
  149.     if mode == 1 then
  150.         if not robot.detect() then
  151.             print("no detect chest")
  152.             while not robot.detect() do
  153.                 os.sleep(1)
  154.             end
  155.         end
  156.         while n <= 16 do
  157.             robot.select(n)
  158.             if robot.count(n) > 0 and not robot.drop() then
  159.                 print("Don't unload to chest")
  160.                 os.sleep(5)
  161.             else
  162.                 n = n + 1
  163.             end
  164.         end
  165.     else
  166.         if not robot.detectUp() then
  167.             print("no detect chest")
  168.             while not robot.detectUp() do
  169.                 os.sleep(1)
  170.             end
  171.         end
  172.         n = 2
  173.         while n <= 16 do
  174.             robot.select(n)
  175.             if robot.count(n) > 0 and not robot.dropUp() then
  176.                 print("Don't unload to chest")
  177.                 os.sleep(5)
  178.             else
  179.                 n = n + 1
  180.             end
  181.         end
  182.     end
  183.     robot.select(1)
  184. end
  185.  
  186. local goTo
  187.  
  188.  
  189.  
  190. local function returnToChest(drop)
  191.     if globalState == 1 or globalState == 2 then
  192.         xt = pos.x
  193.         yt = pos.y
  194.         zt = pos.z
  195.         dt = dir
  196.         globalState = 3
  197.         savestate()
  198.     end
  199.     if globalState == 3 then
  200.         print( "Returning to surface..." )
  201.         goTo( 0,0,0,direction.back)
  202.         if drop then
  203.             unload()
  204.         end
  205.         globalState = 4
  206.         savestate()
  207.     end
  208.     if globalState == 4 then
  209.         local fuelNeeded = (math.abs(xt) + math.abs(yt) + math.abs(zt) + 3)*2
  210.         --if not refuel( fuelNeeded ) then
  211.         --  print( "Waiting for fuel" )
  212.         --  while not refuel( fuelNeeded ) do
  213.         --      sleep(1)
  214.         --  end
  215.         --end
  216.         print( "Resuming mining..." )
  217.         goTo( xt,yt,zt,dt )
  218.         globalState = 2
  219.         xt = 0
  220.         yt = 0
  221.         zt = 0
  222.         dt = direction.forw
  223.             savestate()
  224.     end
  225. end
  226.  
  227. local function returnSupplies()
  228.     --if mode == 1 then
  229.         returnToChest(true)
  230.     --else
  231.     --  returnToEnderChest()
  232.     --end
  233. end
  234.  
  235. local function turnRight()
  236.     dir=dir+1  
  237.     if dir==5 then dir = direction.forw end
  238.     savestate()
  239.     robot.turnRight()
  240. end
  241.  
  242. local function turnLeft()
  243.     dir=dir-1  
  244.     if dir==0 then dir = direction.left end
  245.     savestate()
  246.     robot.turnLeft()
  247. end
  248.  
  249. local function checkBorder()
  250.     if (math.abs(pos.x) >=  width) or (math.abs(pos.z) >= lenght) then
  251.         print("going beyond a border")
  252.         delStartup()
  253.         error()
  254.     end
  255. end
  256.  
  257. local function down()
  258.     while true do
  259.         if robot.detectDown() then 
  260.             if robot.swingDown() then
  261. --              print("detect block")
  262.             else
  263.                 print("detect bedrock or unbreaking block")
  264.                 return false
  265.             end
  266.         elseif robot.swingDown() then
  267.             print("detect monster or player")
  268.         else   
  269.             local ty = pos.y
  270.             pos.y = pos.y - 1
  271.             savestate()
  272.             if robot.down() then
  273. --              print("it's ok!")
  274.                 checkBorder()
  275.                 return true
  276.             else
  277.                 print("detect unknowing object DOWN or limitFlightHeight=256")
  278.                 pos.y = ty             
  279.                 savestate()
  280.                 os.sleep(0.5)
  281.             end
  282.         end
  283.     end
  284. end
  285.  
  286. local function up()
  287.     while true do
  288.         if robot.detectUp() then    --обнаружен блок, сломать
  289.             if robot.swingUp()  then
  290. --              print("detect block")
  291.             else
  292.                 print("detect bedrock or unbreaking block")
  293.                 return false
  294.             end
  295.         elseif robot.swingUp() then -- Сверху нет блока, проверяем есть ли там существо
  296.             print("detect monster or player")
  297.         else    -- Передвигаемся вверх
  298.             local ty = pos.y
  299.             pos.y = pos.y + 1
  300.             savestate()
  301.             if robot.up() then
  302.                 --it's ok!
  303. --              print("it's ok!")
  304.                 checkBorder()
  305.                 return true
  306.             else
  307.                 -- backup
  308.                 print("detect unknowing object UP or limitFlightHeight=256")
  309.                 pos.y = ty
  310.                 savestate()
  311.                 os.sleep(0.5)
  312.             end
  313.         end
  314.     end
  315. end
  316.  
  317. local function forward(mined)
  318.     if mined then
  319.         if robot.detectUp() then
  320.             robot.swingUp()
  321.             if inventoryFull() then
  322.                 returnSupplies()
  323.             end
  324.         end
  325.         if robot.detectDown() then
  326.             robot.swingDown()
  327.             if inventoryFull() then
  328.                 returnSupplies()
  329.             end
  330.         end
  331.     end
  332.    
  333.     while true do
  334.         if robot.detect() then  --обнаружен блок, сломать
  335.             if robot.swing() then
  336. --              print("detect block")
  337.                  if mined and inventoryFull() then
  338.                      returnSupplies()
  339.                  end
  340.             else
  341.                 print("detect bedrock or unbreaking block")
  342.                 return false
  343.             end
  344.         elseif robot.swing() then -- Впереди нет блока, проверяем есть ли там существо
  345.             print("detect monster or player")
  346.              if mined and inventoryFull() then
  347.                  returnSupplies()
  348.              end
  349.         else    -- Передвигаемся вперед
  350.             if mined then -- Уберем гравий
  351.                 while robot.detectUp() do
  352.                     robot.swingUp()
  353.                     os.sleep(0.5)
  354.                 end
  355.             end
  356.             local tz,tx = pos.z,pos.x
  357.             if dir == direction.forw then
  358.                 pos.z=pos.z+1
  359.             elseif dir == direction.right then
  360.                 pos.x=pos.x+1
  361.             elseif dir == direction.back then
  362.                 pos.z=pos.z-1
  363.             elseif dir == direction.left then
  364.                 pos.x=pos.x-1
  365.             end
  366.                 savestate()
  367.  
  368.             if robot.forward() then
  369. --              print("it's ok!")
  370.                 checkBorder()
  371.                 return true
  372.             else
  373.                 -- backup          
  374.                 print("detect unknowing object FORWARD limitFlightHeight=256")
  375.                 pos.z = tz
  376.                 pos.x = tx
  377.                 savestate()
  378.                 os.sleep(0.5)
  379.             end
  380.         end
  381.     end
  382. end
  383.  
  384. local function detectBedrock() --объехать бедрок
  385.     turnLeft()
  386.     turnLeft()
  387.     while not up() do
  388.         if not forward() then
  389.             print("don't going to bedrock!")
  390.             delStartup()
  391.             error()
  392.             return false
  393.             -- не получилось объехать бедрок
  394.         end
  395.     end
  396.     return true
  397. end
  398.  
  399. function goTo( x, y, z, d)
  400.  
  401.  
  402.     while pos.y < y do  -- идем вверх
  403.         if not up() then
  404.             detectBedrock()
  405.         end
  406.     end
  407.     if pos.x > x then  
  408.         while dir ~= direction.left do
  409.             turnLeft()
  410.         end
  411.         while pos.x > x do
  412.             forward(false)
  413.         end
  414.     end
  415.     if pos.x < x then
  416.         while dir ~= direction.right do
  417.             turnRight()
  418.         end
  419.         while pos.x < x do
  420.             forward(false)
  421.         end
  422.     end
  423.     if pos.z > z then
  424.         while dir ~= direction.back do
  425.             turnRight()
  426.         end
  427.         while pos.z > z do
  428.             forward(false)
  429.         end
  430.     end
  431.     if pos.z < z then
  432.         while dir ~= direction.forw do
  433.             turnLeft()
  434.         end
  435.         while pos.z < z do
  436.             forward(false)
  437.         end
  438.     end
  439.     while pos.y > y do  -- идем вниз
  440.         down()
  441.     end
  442.     while d ~= dir do
  443.         turnLeft()
  444.     end
  445. end  
  446.  
  447. local function nextState()
  448.     if aState == 0 then
  449.         aState = 1
  450.     elseif aState == 1 then
  451.         if pos.z == lenght-1 and pos.x ~= width-1 then
  452.             aState = 2
  453.         elseif pos.z == lenght-1 and pos.x == width-1 then
  454.             aState = 9
  455.         end
  456.     elseif aState == 2 then
  457.         aState = 3
  458.     elseif aState == 3 then
  459.         if pos.z == 0 and pos.x ~= width-1 then
  460.             aState = 4
  461.         elseif pos.z == 0 and pos.x == width-1 then
  462.             aState = 10
  463.         end
  464.     elseif aState == 4 then
  465.         aState = 1
  466.     elseif aState == 5 then
  467.         if pos.z == lenght-1 and pos.x ~= 0 then
  468.             aState = 6
  469.         elseif pos.z == lenght-1 and pos.x == 0 then
  470.             aState = 11
  471.         end
  472.     elseif aState == 6 then
  473.         aState = 7
  474.     elseif aState == 7 then
  475.         if pos.z == 0 and pos.x ~= 0 then
  476.             aState = 8
  477.         elseif pos.z == 0 and pos.x == 0 then
  478.             aState = 12
  479.         end
  480.     elseif aState == 8 then
  481.         aState = 5
  482.     elseif aState == 9 then
  483.         aState = 7
  484.     elseif aState == 10 then
  485.         aState = 5
  486.     elseif aState == 11 then
  487.         aState = 3
  488.     elseif aState == 12 then
  489.         aState = 1
  490.     end
  491.     tmpState = 0
  492.     savestate()
  493.     return
  494. end
  495.  
  496.  
  497.  
  498. local function lower()
  499.     if robot.detectUp() then
  500.         robot.swingUp()
  501.     end
  502.     while tmpState ~= 3 do
  503.         -- ref()
  504.         tmpState = tmpState + 1
  505.         if not down() then
  506.             done = true
  507.             break
  508.         end
  509.     end
  510. end
  511.  
  512. local function main()
  513.     while not done do
  514.         if globalState == 2 then
  515.             if aState == 1 then
  516.                 -- ref()
  517.                 if not forward(true) then
  518.                     done = true
  519.                     break
  520.                 end
  521.             elseif aState == 2 then
  522.                 while tmpState ~= 3 do
  523.                     if tmpState == 0 then
  524.                         tmpState = tmpState + 1
  525.                         turnRight()
  526.                     elseif tmpState == 1 then
  527.                         -- ref()
  528.                         tmpState = tmpState + 1
  529.                         if not forward(true) then
  530.                             done = true
  531.                             break
  532.                         end
  533.                     elseif tmpState == 2 then
  534.                         tmpState = tmpState + 1
  535.                         turnRight()
  536.                     end
  537.                 end
  538.             elseif aState == 3 then
  539.                 -- ref()
  540.                 if not forward(true) then
  541.                     done = true
  542.                     break
  543.                 end
  544.             elseif aState == 4 then
  545.                 while tmpState ~= 3 do
  546.                     if tmpState == 0 then
  547.                         tmpState = tmpState + 1
  548.                         turnLeft()
  549.                     elseif tmpState == 1 then
  550.                         -- ref()
  551.                         tmpState = tmpState + 1
  552.                         if not forward(true) then
  553.                             done = true
  554.                             break
  555.                         end
  556.                     elseif tmpState == 2 then
  557.                         tmpState = tmpState + 1
  558.                         turnLeft()
  559.                     end
  560.                 end
  561.             elseif aState == 5 then
  562.                 -- ref()
  563.                 if not forward(true) then
  564.                     done = true
  565.                     break
  566.                 end
  567.             elseif aState == 6 then
  568.                 while tmpState ~= 3 do
  569.                     if tmpState == 0 then
  570.                         tmpState = tmpState + 1
  571.                         turnLeft()
  572.                     elseif tmpState == 1 then
  573.                         -- ref()
  574.                         tmpState = tmpState + 1
  575.                         if not forward(true) then
  576.                             done = true
  577.                             break
  578.                         end
  579.                     elseif tmpState == 2 then
  580.                         tmpState = tmpState + 1
  581.                         turnLeft()
  582.                     end
  583.                 end    
  584.             elseif aState == 7 then
  585.                 -- ref()
  586.                 if not forward(true) then
  587.                     done = true
  588.                     break
  589.                 end
  590.             elseif aState == 8 then
  591.                 while tmpState ~= 3 do
  592.                     if tmpState == 0 then
  593.                         tmpState = tmpState + 1
  594.                         turnRight()
  595.                     elseif tmpState == 1 then
  596.                         -- -- ref()
  597.                         tmpState = tmpState + 1
  598.                         if not forward(true) then
  599.                             done = true
  600.                             break
  601.                         end
  602.                     elseif tmpState == 2 then
  603.                         tmpState = tmpState + 1
  604.                         turnRight()
  605.                     end
  606.                 end    
  607.             elseif height+pos.y<3 then
  608.                 done = true
  609.                 break
  610.             elseif aState == 9 then
  611.                 while dir ~= direction.back do
  612.                     turnLeft()
  613.                 end
  614.                 lower()
  615.             elseif aState == 10 then
  616.                 while dir ~= direction.forw do
  617.                     turnLeft()
  618.                 end
  619.                 lower()    
  620.             elseif aState == 11 then
  621.                 while dir ~= direction.back do
  622.                     turnLeft()
  623.                 end
  624.                 lower()    
  625.             elseif aState == 12 then
  626.                 while dir ~= direction.forw do
  627.                     turnLeft()
  628.                 end
  629.                 lower()    
  630.             end
  631.             nextState()
  632.         elseif globalState == 3 or globalState == 4 or globalState == 5 or globalState == 6 then
  633.             returnSupplies()
  634.         end
  635.     end
  636.     savestate()
  637.     print( "Returning to surface..." )
  638.  
  639.     -- Return to where we started
  640.     goTo( 0,0,0,direction.back )
  641.     --if mode == 1 then
  642.         unload()
  643.     --else
  644.     --  returnToEnderChest()
  645.     --end
  646.     goTo( 0,0,0,direction.forw )
  647.     delStartup()
  648. end
  649.  
  650. local function recover()
  651.     if not readState() or globalState == 0 then
  652.         print("Programm is not running")
  653.         print( "Usage: quarry <lenght> <width> [<height>]" )
  654.         return
  655.     end
  656.     if globalState == 1 then
  657.         local done = false
  658.         while tmpState ~= 1 do
  659.             --ref()
  660.             if not down() then
  661.                 done = true
  662.                 break
  663.             end
  664.             tmpState=tmpState+1
  665.             saveState()
  666.         end
  667.         globalState = 2
  668.         saveState()
  669.     end
  670.     if globalState >= 2 then
  671. --      nextState()
  672.         main()
  673.     end
  674. end
  675.  
  676. local function start()
  677.     setStartup()
  678.     globalState = 1 -- Поехали копать
  679.     done = false
  680.  
  681.     while tmpState < 1 do
  682.         -- ref()
  683.         if not down() then
  684.             done = true
  685.             break
  686.         end
  687.         tmpState=tmpState+1
  688.         savestate()
  689.     end
  690.  
  691.     globalState = 2
  692.     savestate()
  693.     nextState()
  694.     main()
  695. end
  696.  
  697.  
  698.  
  699. local function GetFuel()
  700.             print("press any key to run quarry")
  701.             local input = io.read()
  702.             waiting = false
  703.             term.clear()
  704.             term.setCursor( 1, 1 )
  705.             robot.select(1)
  706.             --if mode == 2 and robot.count(1) < 1 then
  707.             --  print("Waiting for get chest to first slot")
  708.             --  while robot.count(1) < 1 do
  709.             --      os.sleep(1)
  710.             --  end
  711.             --end
  712.             -- print( "Fuel level: "..robot.getFuelLevel())
  713.             print( "Excavating...")
  714.             start()
  715.             return
  716.    
  717. end
  718.  
  719.  
  720. local tArgs = { ... }
  721. if #tArgs == 1 then
  722.     print( "Recovering...")
  723.     recover()
  724.     return
  725.  
  726. elseif #tArgs == 2 or #tArgs == 3 or #tArgs == 4 then
  727.     lenght = tonumber( tArgs[1] )
  728.     width  = tonumber( tArgs[2] )
  729.     height = 256
  730.     local arg = ""
  731.     if lenght < 1 or width < 1 or lenght > 255 or width > 255 then
  732.         print( "Quarry size must be positive" )
  733.         return
  734.     end
  735.     if #tArgs == 3 then
  736.         arg = tonumber( tArgs[3] )
  737.         if tArgs[3] == "-e" then
  738.             mode = 2
  739.         elseif tArgs[3] == "-f" then
  740.             FuelInfo()
  741.             return
  742.         elseif arg ~= nill then
  743.             height = arg
  744.             if height < 1 or height > 255 then
  745.                 print( "Quarry size must be positive" )
  746.                 return
  747.             end
  748.         else
  749.             print( "Usage: quarry <lenght> <width> [<height>] [<key>]; key: \"-e\", \"-f\"" )
  750.             return
  751.         end
  752.     elseif #tArgs == 4 then
  753.         arg = tonumber( tArgs[3] )
  754.         if arg ~= nill then
  755.             height = arg
  756.             if height < 1 or height > 255 then
  757.                 print( "Quarry size must be positive" )
  758.                 return
  759.             end
  760.         else
  761.             print( "Usage: quarry <lenght> <width> [<height>] [<key>]; key: \"-e\", \"-f\"" )
  762.             return
  763.         end
  764.         if tArgs[4] == "-e" then
  765.             mode = 2
  766.         elseif tArgs[4] == "-f" then
  767.             FuelInfo()
  768.             return
  769.         else
  770.             print( "Usage: quarry <lenght> <width> [<height>] [<key>]; key: \"-e\", \"-f\"" )
  771.             return
  772.         end
  773.     end
  774.     --starting
  775.     GetFuel()
  776.     return
  777. else
  778.     print( "Usage: quarry <lenght> <width> [<height>] [<key>]; key: \"-e\", \"-f\"" )
  779.     return
  780. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement