WineCraftHD

State Machine

Dec 16th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.02 KB | None | 0 0
  1. local S_PREP = "PREP"
  2. local S_MINING = "MINING"
  3. local S_REFUEL = "REFUEL"
  4. local S_INVENTORY = "INVENTORY"
  5. local S_FINISH  = "FINISH"
  6. local S_ADVANCE = "ADVANCE"
  7.  
  8. -- Chests in slot 1, fuel in slot 2
  9. local IgnoreSlots = 3
  10.  
  11. local chestIndex = 1
  12. local CHEST = "minecraft:chest"
  13. local fuelIndex = 2
  14. local bucketIndex = 3
  15.  
  16. local DepthTarget = 100
  17. local fudgeFactor = 10
  18.  
  19. local mov = {}
  20. local rot = {}
  21. local dig = {}
  22.  
  23. local status = {}
  24. local statusFileName = "mstatus"
  25.  
  26. local function loadTable(name)
  27.         local file = fs.open(name,"r")
  28.         if file == nil then
  29.             return
  30.         end
  31.         local data = file.readAll()
  32.         file.close()
  33.         return textutils.unserialize(data)
  34. end
  35.  
  36. local function saveTable(table,name)
  37.         local file = fs.open(name,"w")
  38.         file.write(textutils.serialize(table))
  39.         file.close()
  40. end
  41.  
  42. local function saveStatus()
  43.     saveTable(status, statusFileName)        
  44. end
  45.  
  46. local function loadStatus()
  47.     status = loadTable(statusFileName)
  48. end
  49.  
  50. local function isChest()
  51.    local exists, data = turtle.inspectUp()
  52.    
  53.    if not exists then return false end
  54.    
  55.    return string.find(data.name, CHEST) ~= nil
  56. end
  57.  
  58. local DONT_NEED_FUEL = 0
  59. local GOT_SOME_FUEL = 1
  60. local OUT_OF_FUEL = -1
  61.  
  62. local function Refuel()
  63.         currentFuelRequired = (40 + (DepthTarget-status.progress)*3)/20
  64.         if turtle.getFuelLevel() < currentFuelRequired then
  65.                 turtle.select(fuelIndex)
  66.                 local r = turtle.refuel(currentFuelRequired)
  67.                 if r then
  68.                     return GOT_SOME_FUEL
  69.                 elseif not r then
  70.                     return OUT_OF_FUEL
  71.                 end
  72.         else
  73.                 return DONT_NEED_FUEL
  74.         end
  75. end
  76.  
  77. local function CheckRefuel()
  78.     if Refuel() == OUT_OF_FUEL then
  79.         print("***Out of fuel***")
  80.         status.status = S_STOP
  81.         return
  82.     end
  83.     local count = 0
  84.     while Refuel() == GOT_SOME_FUEL and count < 16 do
  85.         count = count + 1
  86.           print("refueling")            
  87.     end
  88. end
  89.  
  90. mov.forward = function(times)
  91.         local boolean v = false
  92.         for i=1,times do
  93.                 if turtle.detect() == false then
  94.                         while not turtle.forward() do
  95.                                 CheckRefuel()
  96.                                 sleep(1)
  97.                         end
  98.                         v = true
  99.                 else
  100.                         v = false
  101.                         return v
  102.                 end
  103.         end
  104.         return v
  105. end
  106.  
  107. mov.back = function(times)
  108.         local boolean v = false
  109.         for i=1,times do
  110.                 while not turtle.back() do
  111.                         CheckRefuel()
  112.                         sleep(1)
  113.                 end
  114.                 v = true
  115.         end
  116.         return v
  117. end
  118.  
  119. mov.up = function(times)
  120. --     print("mov.up")
  121.      -- print("-->")
  122.     local boolean v = false
  123.     for i=1,times do
  124.         if turtle.detectUp() == false then
  125.             local attempts = 0
  126.             while turtle.up() == false and attempts < 10 do
  127. --                 print("failed to move up")
  128.                 attempts = attempts + 1
  129.                 CheckRefuel()
  130.                 sleep(1)
  131.                 if attempts > 8 then
  132. --                     print("LOTS of fails move up")
  133.                     if status.status == S_FINISH then
  134.                         status.status = S_STOP
  135.                     else
  136.                         status.status = S_FINISH
  137.                     end
  138.                     return false
  139.                 end
  140.             end
  141.             v = true
  142.         else
  143.             v = false
  144.             -- print("<--")
  145.             return v
  146.         end
  147.     end
  148.     -- print("<--")
  149.     return v
  150. end
  151.  
  152. mov.down = function(times)
  153. --     print("mov.down")
  154.     -- print("-->")
  155.     local boolean v = false
  156.     for i=1,times do
  157.         if turtle.detectDown() == false then
  158.             local attempts = 0
  159.             while turtle.down() == false and attempts < 10 do
  160. --                 print("failed to move down")
  161.                 attempts = attempts + 1
  162.                 CheckRefuel()
  163.                 sleep(1)
  164.                 if attempts > 8 then
  165. --                     print("LOTS of fails move down")
  166.                     if status.status == S_FINISH then
  167.                         status.status = S_STOP
  168.                     else
  169.                         status.status = S_FINISH
  170.                     end
  171.                     return false
  172.                 end
  173.             end
  174.             v = true
  175.         else
  176.             v = false
  177.             -- print("<--")
  178.             return v
  179.         end
  180.     end
  181.     -- print("<--")
  182.     return v
  183. end
  184.  
  185. mov.place = function()
  186.     while not turtle.place() do
  187.           sleep(1)
  188.     end
  189.     return true
  190. end
  191.  
  192. rot.right = function()
  193.     while not turtle.turnRight() do
  194.             sleep(1)
  195.     end
  196.     return true
  197. end
  198.  
  199. rot.left = function()
  200.     while not turtle.turnLeft() do
  201.             sleep(1)
  202.     end
  203.     return true
  204. end
  205.  
  206. dig.forward = function()
  207.     print("dig.forward")
  208.     -- print("-->")
  209.         if turtle.detect() then
  210.                 while not turtle.dig() do
  211.                                 sleep(1)
  212.                 end
  213.     -- print("<--")
  214.                 return true
  215.         else
  216. --                 print("No Block to mine forward")
  217.     -- print("<--")
  218.                 return false
  219.         end
  220. end
  221.  
  222. dig.up = function()
  223.     print("dig.up")
  224.     -- print("-->")
  225.         if turtle.detectUp() then
  226.                 while not turtle.digUp() do
  227.                                 sleep(1)
  228.                 end
  229.     -- print("<--")
  230.                 return true
  231.         else
  232.                 print("No Block to mine up")
  233.     -- print("<--")
  234.                 return false
  235.         end
  236. end
  237.  
  238. dig.down = function()
  239.     print("In dig.down")
  240.     -- print("-->")
  241.         if turtle.detectDown() then
  242.                 while not turtle.digDown() do
  243.                                 sleep(1)
  244.                 end
  245.     -- print("<--")
  246.                 return true
  247.         else
  248.                 print("No Block to mine down")
  249.     -- print("<--")
  250.                 return false
  251.         end
  252. end
  253.  
  254. local function DigMoveForward()
  255.     print("In DigMoveForward")
  256.     -- print("-->")
  257.         if mov.forward(1) == false then
  258.                 dig.forward()
  259.                 sleep(0.5)
  260.                 DigMoveForward()
  261.         end
  262.     -- print("<--")
  263. end
  264.  
  265. local function DigMoveUp()
  266.     print("In DigMoveUp")
  267.     -- print("-->")
  268.     local count = 0
  269.         while mov.up(1) == false and count < 10 do
  270.             count = count + 1
  271.                 dig.up()
  272.                 sleep(0.5)
  273.         end
  274.         if count > 5 then
  275. --             print("had a hard time moving up")
  276.         end        
  277.     -- print("<--")
  278. end
  279.  
  280. local function DigMoveDown()
  281.     print("In DigMoveDown")
  282.     -- print("-->")
  283.     local count = 0
  284.         while mov.down(1) == false and count < 10 do
  285.                 dig.down()
  286.                 sleep(0.5)
  287.         end
  288.         if count > 5 then
  289. --             print("had a hard time moving up")
  290.         end
  291.         -- print("<--")
  292. end
  293.  
  294. local function ReturnBack()
  295.     print("In ReturnBack")
  296.     -- print("-->")
  297.     turtle.select(1)
  298.     local count = 0
  299.     while not isChest() and count < DepthTarget + fudgeFactor do
  300.         count = count + 1
  301.         print(count)
  302.         mov.up(1)
  303.     end
  304.     -- print("<--")
  305. end
  306.  
  307.  
  308.  
  309. local function DepositChest()
  310.     print("In DepositChest")
  311.     -- print("-->")
  312.     if not isChest then
  313.         print("ERROR: Should be below chest.")
  314.         status.status = S_STOP
  315.     -- print("<--")
  316.         return
  317.     end
  318.        
  319.     print("Depositing the Load...")
  320.     for i = IgnoreSlots + 1,16 do
  321.             turtle.select(i)
  322.             turtle.dropUp()
  323.     end
  324.     print("Out DepositChest")
  325. end
  326.  
  327. local function InventoryFull()
  328.     print("In InventoryFull")
  329.     -- print("-->")
  330.     SlotsFull = 0
  331.     for i=1,16 do
  332.             if turtle.getItemCount(i) > 0 then
  333.                     SlotsFull = SlotsFull + 1
  334.             end
  335.     end
  336.    
  337.     if SlotsFull == 16 then
  338.        -- print("<--")
  339.             return true;
  340.     else
  341.     -- print("<--")
  342.             return false;
  343.     end
  344. end
  345.  
  346. local function CheckInventory()
  347.     if InventoryFull() then
  348.         print("***Inventory Full***")
  349.         ReturnBack()
  350.         DepositChest()
  351.         for i=1,status.progress do
  352.             DigMoveDown()
  353.         end
  354.     end
  355. end
  356.  
  357. local function Harvest()
  358.     local exist,block = turtle.inspect()
  359.     if not exist then
  360.         return
  361.     end
  362.    
  363.     if string.find(block.name, "flowing_water") then
  364.         turtle.select(1)
  365.         turtle.place()
  366.     elseif string.find(block.name, "flowing_lava") then
  367.         if bucketIndex > 0 then
  368.             turtle.select(bucketIndex)
  369.             turtle.place()
  370.         end
  371.         turtle.select(1)
  372.         turtle.place()
  373.     end
  374.      
  375.     while turtle.suck() == true do
  376.         CheckInventory()
  377.     end
  378.    
  379.     dig.forward()
  380. end
  381.  
  382. local function Progress()
  383.     DigMoveDown()
  384.     status.progress = status.progress + 1
  385.     BlocksTillEnd = DepthTarget - status.progress    
  386. end
  387.  
  388. local function Finish()
  389.     print("In Finish")
  390.     -- print("-->")
  391.     ReturnBack()
  392.     DepositChest()
  393.  
  394.     status.status = S_ADVANCE  
  395.     -- print("<--")
  396. end
  397.  
  398. local function NextColumn()
  399. --     turtle.select(1)
  400. --     if not turtle.compareUp() then
  401. --         rot.right()
  402. --         rot.right()
  403. --         while not turtle.compareUp() do
  404. --             DigMoveForward()
  405. --         end
  406. --         rot.right()
  407. --         rot.right()
  408. --     end
  409.     for i=1,4 do
  410.         DigMoveForward()
  411.     end
  412.    
  413.     status.status = S_PREP
  414. end
  415.  
  416.  
  417. local function StartMining()
  418.     print("In StartMining")
  419.     -- print("-->")
  420.     sleep(1)
  421.     BlocksTillEnd = DepthTarget - status.progress
  422.      
  423.     while BlocksTillEnd > 0 do
  424.        
  425.         CheckRefuel()
  426.         if status.status == S_STOP then
  427.             return
  428.         end
  429.         CheckInventory()
  430.         Harvest()
  431.        
  432.         for i=1,4 do
  433.             Progress()
  434.         end            
  435.      
  436.         print("Depth: ")
  437.         print(status.progress)
  438.     end
  439.    
  440.     Harvest()
  441.    
  442.     status.status = S_FINISH
  443.    
  444.     -- print("<--")
  445. end
  446.  
  447. local function Prepare()
  448.     status.previous = ""
  449.     status.progress = 0
  450.    
  451.     local data = turtle.getItemDetail(chestIndex)
  452.     if data ~= nil and data.name ~= CHEST then
  453.         print("need more chests")
  454.         return
  455.     end
  456.    
  457.     local FuelCount = turtle.getItemCount(fuelIndex)
  458.  
  459.     if FuelCount == 0 then
  460.         print("please put fuel in the last slot of the Turtle.")
  461.         return
  462.     else
  463.         if FuelCount < 10 then
  464.             print("Please put at least 10 of the fuel you are using in the Turtle.")
  465.             return
  466.         end
  467.     end
  468.      
  469.     CheckRefuel()
  470.     if status.status == S_STOP then
  471.         return
  472.     end
  473.    
  474.     turtle.digUp()
  475.     turtle.select(1)
  476.     local result = turtle.placeUp()
  477.     if not result then
  478.         status.status = S_ADVANCE
  479.         return
  480.     end
  481.    
  482.     status.status = S_MINING
  483. end
  484.  
  485. function Main()
  486.     while status.status ~= S_STOP do
  487.         saveStatus()
  488.         sleep(1)
  489.         if status.status == S_PREP then
  490.             Prepare()
  491.            
  492.         elseif status.status == S_MINING then
  493.             StartMining()
  494.            
  495.         elseif status.status == S_FINISH then
  496.             Finish()
  497.            
  498.         elseif status.status == S_ADVANCE then
  499.             NextColumn()
  500.            
  501.         else
  502.             print("Unknown status: "..status.status)
  503.             status.status = S_STOP
  504.         end
  505.     end
  506.     ReturnBack()
  507. end
  508.  
  509. -- ON LAUNCH
  510. loadStatus()
  511. if status == nil then
  512.     status = { }
  513.    status.status = S_PREP
  514.    status.previous = ""
  515.    status.progress = 0
  516. end
  517.  
  518. if status.status ~= S_PREP and status.status ~= S_ADVANCE then
  519.     turtle.select(fuelIndex)
  520.     turtle.refuel(2)
  521.     ReturnBack()
  522. end
  523.  
  524. Main()
Advertisement
Add Comment
Please, Sign In to add comment