TDY2014

Immersive Engineering Ore Sampler

Dec 23rd, 2016
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.74 KB | None | 0 0
  1. --[[
  2. About this program ...
  3. - This program is tested using FTB Infinity Evolved 2.6.0 Modpack.
  4. - This program uses turtle to find mineral deposit using Immersive Engineering Core Sample Drill.
  5. - As stated above, the turtle must have these following items in its inventory:
  6. 1. Coal or Charcoal (for fueling)
  7. 2. Any pickaxe-harvestable block (the drill need a block below to function)
  8. 3. Tesseract (or any RF power source for the drill)
  9. 4. Immersive Engineering Core Sample Drill (well, the drill itself)
  10. Note: These items must be sorted in this order strictly!
  11. --]]
  12.  
  13. -- Global variables
  14. startPos = {
  15.     x = 0,
  16.     z = 0,
  17.     f = 0
  18. }
  19. facing = {"SOUTH", "WEST", "NORTH", "EAST"}
  20. iteration = 0
  21. currentIteration = 0
  22.  
  23. function closeError(message,outputFile)
  24.     if outputFile ~= nil then
  25.         outputFile.writeLine(message)
  26.         outputFile.writeLine("An error occur at iteration " .. (currentIteration+1) .. "/" .. iteration .. ".")
  27.         outputFile.close()
  28.         error(message)
  29.     else
  30.         error("[Failed] Output file doesn't exist.")
  31.     end
  32. end
  33.  
  34. function refuel()
  35.     if turtle.getItemCount(1) <= 0 then
  36.         closeError("[Failed] Turtle run out of fuel.",outputFile)
  37.     end
  38.     if turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() < 1 then
  39.         turtle.refuel()
  40.     end
  41. end
  42.  
  43. -- For safety reasons, these are the overridden moving functions
  44. function safeForward(outputFile)
  45.     if not turtle.forward() then
  46.         closeError("[Failed] There's an obstruct in the turtle path.",outputFile)
  47.     end
  48. end
  49.  
  50. function safeUp(outputFile)
  51.     if not turtle.up() then
  52.         closeError("[Failed] There's an obstruct in the turtle path.",outputFile)
  53.     end
  54. end
  55.  
  56. function safeDown(outputFile)
  57.     if not turtle.down() then
  58.         closeError("[Failed] There's an obstruct in the turtle path.",outputFile)
  59.     end
  60. end
  61.  
  62. function safePlace(outputFile)
  63.     if not turtle.place() then
  64.         closeError("[Failed] There's an obstruct in the turtle path.",outputFile)
  65.     end
  66. end
  67.  
  68. function beginSampling(outputFile)
  69.     -- Check requirement items in inventory
  70.     if turtle.getItemCount(2) < 1 then
  71.         closeError("[Failed] Missing platform block.",outputFile)
  72.         return false
  73.     end
  74.     if turtle.getItemCount(3) < 1 then
  75.         closeError("[Failed] Missing Tesseract.",outputFile)
  76.         return false
  77.     end
  78.     if turtle.getItemCount(4) < 1 then
  79.         closeError("[Failed] Missing Core Sample Drill 1.",outputFile)
  80.         return false
  81.     end
  82.    
  83.     -- Place platform block
  84.     turtle.select(2)
  85.     safePlace(outputFile)
  86.     turtle.turnRight()
  87.     safeForward(outputFile)
  88.     turtle.turnLeft()
  89.  
  90.     -- Place Tesseract
  91.     turtle.select(3)
  92.     safeUp(outputFile)
  93.     safePlace(outputFile)
  94.  
  95.     -- Place Core Sample Drill
  96.     turtle.select(4)
  97.     turtle.turnLeft()
  98.     safeForward(outputFile)
  99.     turtle.turnRight()
  100.     safePlace(outputFile)
  101.  
  102.     print("Established sampling site successfully")
  103.     return true
  104. end
  105.  
  106. function doSampling(outputFile)
  107.     -- Get the drill that "suppose" to be in front of the turtle right now
  108.     local drill = peripheral.wrap("front")
  109.     if drill == nil then
  110.         closeError("[Failed] Missing Core Sample Drill 2.",outputFile)
  111.     end
  112.  
  113.     while not drill.isSamplingFinished() do
  114.         if drill.getEnergyStored() <= 0 then
  115.             closeError("[Failed] The drill ran out of power.",outputFile)
  116.         end
  117.     end
  118.  
  119.     local result = drill.getVeinLocalizedName()
  120.     if result == nil then
  121.         outputFile.write("None\n")
  122.     else
  123.         outputFile.write(result .. "\n")
  124.     end
  125.  
  126.     print("Done sampling successfully")
  127.     return true
  128. end
  129.  
  130. function endSampling(outputFile)
  131.     -- Remove the platform blocks
  132.     turtle.select(2)
  133.     safeDown(outputFile)
  134.     turtle.dig()
  135.     turtle.turnRight()
  136.     safeForward(outputFile)
  137.     turtle.turnLeft()
  138.  
  139.     -- Remove Tesseract
  140.     turtle.select(3)
  141.     safeUp(outputFile)
  142.     turtle.dig()
  143.  
  144.     -- Remove Core Sample Drill
  145.     turtle.select(4)
  146.     turtle.turnLeft()
  147.     safeForward(outputFile)
  148.     turtle.turnRight()
  149.     turtle.dig()
  150.  
  151.     print("Removed sampling site successfully")
  152.     return true
  153. end
  154.  
  155. function main()
  156.     -- Input current turtle coordinate and facing (This may be replaced by gps API)
  157.     local stringInput
  158.     print("Please input current x coordinate : ")
  159.     stringInput = io.read()
  160.     startPos["x"] = tonumber(stringInput)
  161.     print("Please input current z coordinate : ")
  162.     stringInput = io.read()
  163.     startPos["z"] = tonumber(stringInput)
  164.     print("Please input current f (facing) : ")
  165.     stringInput = io.read()
  166.     startPos["f"] = tonumber(stringInput)
  167.     while startPos["f"] < 0 or startPos["f"] > 3 do
  168.         print("Invalid f (facing) input, please input 0 - 3. (Check your facing by pressing F3)")
  169.         stringInput = io.read()
  170.         startPos["f"] = tonumber(stringInput)
  171.     end
  172.  
  173.     -- Input number of iterations (number of chunks being sampled)
  174.     print("Please input number of iterations : ")
  175.     stringInput = io.read()
  176.     iteration = tonumber(stringInput)
  177.    
  178.     print("x,z,f,i = " .. startPos["x"] .. "," .. startPos["z"] .. "," .. startPos["f"] .. "," .. iteration)
  179.  
  180.     -- Initialize output file
  181.     local timeText = http.get("http://www.timeapi.org/utc/in+7+hours?format=%25d%25m%25Y_%25H%25M%25S").readAll()
  182.     local outputFile = fs.open("OreSampling_" .. timeText, "w")
  183.     outputFile.writeLine("Ore Sampling Report started at (" .. startPos["x"] .. ", " .. startPos["z"] .. ") facing to " .. facing[startPos["f"]])
  184.     outputFile.writeLine("Number of iterations : " .. iteration)
  185.    
  186.     local deltaZ = 0
  187.     local deltaX = 0
  188.     while currentIteration < iteration do
  189.         refuel(outputFile)
  190.         print("f : " .. startPos["f"])
  191.         deltaZ = ((startPos["f"]+1)%2)*(startPos["f"]-1)*currentIteration*16
  192.         deltaX = (startPos["f"]%2)*(startPos["f"]-2)*currentIteration*16
  193.         outputFile.write("[1]" .. (startPos["x"]+deltaX) .. (startPos["z"]+deltaZ) .. " : ")
  194.         beginSampling(outputFile)
  195.         doSampling(outputFile)
  196.         endSampling(outputFile)
  197.         for i=1,16 do
  198.             safeForward(outputFile)
  199.         end
  200.         currentIteration = currentIteration + 1
  201.     end
  202.    
  203.     print("All samplings done")
  204. end
  205.  
  206. main()
Advertisement
Add Comment
Please, Sign In to add comment