Advertisement
Guest User

search.lua

a guest
Feb 14th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.33 KB | None | 0 0
  1. --Version 1.0
  2.  
  3. args = {...}
  4. size = 0
  5. state = 1
  6. states = {"MINE", "REFUEL", "DEPOSIT","FINISHED"}
  7.  
  8.  
  9. os.loadAPI("oreMine/oreMine.lua")
  10. os.loadAPI("oreMine/move.lua")
  11.  
  12.  
  13. function createStart()
  14.     h = fs.open("startup","w")
  15.     h.writeLine("shell.run(\"oreMine/search.lua\")")
  16.     h.close()
  17. end
  18.  
  19. function deleteStart()
  20.     fs.delete("startup")
  21.     fs.delete("oreMine/size.lua")
  22.     fs.delete("oreMine/pos.lua")
  23.     fs.delete("oreMine/loc.lua")
  24. end
  25.  
  26. function saveState()
  27.     h = fs.open("oreMine/size.lua","w")
  28.     h.writeLine(size)
  29.     h.writeLine(state)
  30.     h.close()
  31. end
  32.  
  33. function readState()
  34.     h = fs.open("oreMine/size.lua","r")
  35.     size = tonumber(h.readLine())
  36.     state = tonumber(h.readLine())
  37.     h.close()
  38. end
  39.  
  40. function saveLoc()
  41.     h = fs.open("oreMine/loc.lua","w")
  42.     h.writeLine(move.getx())
  43.     h.writeLine(move.gety())
  44.     h.writeLine(move.getz())
  45.     h.close()
  46. end
  47.  
  48. function getLoc()
  49.     tx = 0
  50.     ty = 0
  51.     tz = 0
  52.     h = fs.open("oreMine/loc.lua","r")
  53.     tx = tonumber(h.readLine())
  54.     ty = tonumber(h.readLine())
  55.     tz = tonumber(h.readLine())
  56.     h.close()
  57.     return tx, ty, tz
  58. end
  59.  
  60. function saveOreLoc()
  61.     h = fs.open("oreMine/oreLoc.lua","w")
  62.     h.writeLine(move.getx())
  63.     h.writeLine(move.gety())
  64.     h.writeLine(move.getz())
  65.     h.close()
  66. end
  67.  
  68. function delOreLoc()
  69.     fs.delete("oreMine/oreLoc.lua")
  70. end
  71.  
  72. function getOreLoc()
  73.     tx = 0
  74.     ty = 0
  75.     tz = 0
  76.     h = fs.open("oreMine/oreLoc.lua","r")
  77.     tx = tonumber(h.readLine())
  78.     ty = tonumber(h.readLine())
  79.     tz = tonumber(h.readLine())
  80.     h.close()
  81.     return tx, ty, tz
  82. end
  83.  
  84. function mine()
  85.     state = 1
  86.     saveState()
  87.     if move.getx() > size then
  88.         state = 4
  89.         saveState()
  90.     end  
  91.     if move.getz() == -1*size and (move.getx()%2 == 0) then
  92.         move.move(move.getx()+1, move.gety() , move.getz())
  93.         saveOreLoc()
  94.         oreMine.find()
  95.         delOreLoc()
  96.         return
  97.     end
  98.     if move.getz() == 0 and (move.getx()%2 ~= 0) then
  99.         move.move(move.getx()+1, move.gety(), move.getz())
  100.         saveOreLoc()
  101.         oreMine.find()
  102.         delOreLoc()
  103.         return
  104.     end
  105.     if move.getx()%2 == 0 then
  106.         move.face(1)
  107.         move.forward()
  108.         saveOreLoc()
  109.         oreMine.find()
  110.         delOreLoc()
  111.     elseif move.getx()%2 ~= 0 then
  112.         move.face(3)
  113.         move.forward()
  114.         saveOreLoc()
  115.         oreMine.find()
  116.         delOreLoc()
  117.     end
  118. end
  119.  
  120. function fuel()
  121.     if turtle.getFuelLevel() < 300 then
  122.         state = 2
  123.         saveState()
  124.         for i = 1,16 do
  125.             turtle.select(i)
  126.             turtle.refuel(64)
  127.         end
  128.     end
  129.     if turtle.getFuelLevel() < 300 then
  130.         state = 2
  131.         saveState()
  132.         print("Out of Fuel")
  133.         if not fs.exists("oreMine/loc.lua") then
  134.             saveLoc()
  135.         end
  136.         move.move(0,0,0)
  137.         move.face(3)
  138.         for i = 1,16 do
  139.             turtle.select(i)
  140.             turtle.drop()
  141.         end
  142.         move.up()
  143.         while turtle.getFuelLevel() < 1000 do
  144.             turtle.select(1)
  145.             turtle.drop()
  146.             turtle.suck(64)
  147.             turtle.refuel(64)
  148.         end
  149.         move.move(getLoc())
  150.         fs.delete("oreMine/loc.lua")
  151.     end
  152. end
  153.  
  154. function deposit()
  155.     num = 0
  156.     for i = 1,16 do
  157.         if turtle.getItemCount(i) > 0 then
  158.             num = num + 1
  159.         end
  160.     end
  161.     if num < 15 then
  162.         return false
  163.     end
  164.        
  165.     state = 3
  166.     saveState()
  167.     print("Depositing Items")
  168.     if not fs.exists("oreMine/loc.lua") then
  169.         saveLoc()
  170.     end
  171.     move.move(0,0,0)
  172.     move.face(3)
  173.     for i = 1, 16 do
  174.         turtle.select(i)
  175.         turtle.refuel(32)
  176.         turtle.drop()
  177.     end
  178.     move.move(getLoc())
  179.     fs.delete("oreMine/loc.lua")
  180.     return true
  181. end
  182.  
  183. if tonumber(args[1]) then
  184.     size = tonumber(args[1])
  185.     saveState()
  186. elseif fs.exists("oreMine/size.lua") then
  187.     readState()
  188. else
  189.     print("Please enter a number")
  190.     return
  191. end
  192.  
  193. if fs.exists("oreMine/oreLoc.lua") then
  194.     move.move(getOreLoc())
  195. end
  196.  
  197. if 1*state == 2 then
  198.     fuel()
  199. elseif 1*state == 3 then
  200.     deposit()
  201. end
  202.  
  203. createStart()
  204. while state ~= 4 do
  205.     mine()
  206.     deposit()
  207.     fuel()
  208.     end
  209. move.move(0,0,0)
  210. move.face(1)
  211. deleteStart()
  212. print("Finished Mining!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement