alesandreo

t.lua

May 28th, 2020 (edited)
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 26.90 KB | None | 0 0
  1. --
  2. -- Created by IntelliJ IDEA.
  3. -- User: amcconaughey
  4. -- Date: 3/30/20
  5. -- Time: 9:01 AM
  6. -- To change this template use File | Settings | File Templates.
  7. --
  8. --module "t"
  9. --turtle = require "turtle"
  10.  
  11. log_level = "info"
  12. log_category = {}
  13. log_category["log"] = log_level
  14. --log_category["breadcrumbs"] = "debug"
  15. --log_category["compare_positions"] = "trace"
  16.  
  17. turtle_pos = {}
  18. saved_positions = {}
  19. crumbs = {}
  20. redo_crumbs = {}
  21. redoing = false
  22. stack = 0
  23. MAX_STACK_DEPTH = 10
  24.  
  25. dig_blacklist = {}
  26. dig_whitelist = {}
  27. filler_whitelist = {}
  28. flag_whitelist = {}
  29. dig_whitelist_tags = {}
  30. dig_blacklist_tags = {}
  31.  
  32. CHEST_SLOT = 13
  33. BUCKET_SLOT = 14
  34. FILLER_SLOT = 15
  35. TORCH_SLOT = 16
  36. CRUMBS = true
  37.  
  38. ore_flagged = false
  39.  
  40. dig_whitelist_tags["forge:ores"] = true
  41. dig_whitelist["minecraft:obsidian"] = true
  42. dig_whitelist["powah:dry_ice"] = true
  43. dig_whitelist["forbidden_arcanus:runestone"] = true
  44. dig_whitelist["druidcraft:fiery_glass_ore"] = true
  45.  
  46. filler_whitelist["minecraft:cobblestone"] = true
  47. filler_whitelist["minecraft:dirt"] = true
  48. filler_whitelist["minecraft:granite"] = true
  49. filler_whitelist["minecraft:diorite"] = true
  50. filler_whitelist["minecraft:andesite"] = true
  51. filler_whitelist["embellishcraft:basalt_cobblestone"] = true
  52. filler_whitelist["rftoolsbase:dimensionalshard_overworld"] = true
  53.  
  54.  
  55. function init()
  56.     turtle_pos["x"] = 0
  57.     turtle_pos["y"] = 0
  58.     turtle_pos["z"] = 0
  59.     turtle_pos["face"] = 10000
  60. end
  61.  
  62. function shallowcopytable(orig)
  63.     local orig_type = type(orig)
  64.     local copy
  65.     if orig_type == 'table' then
  66.         copy = {}
  67.         for orig_key, orig_value in pairs(orig) do
  68.             copy[orig_key] = orig_value
  69.         end
  70.     else -- number, string, boolean, etc
  71.         copy = orig
  72.     end
  73.     return copy
  74. end
  75.  
  76. function getLogLevel(level)
  77.     level = string.lower(level)
  78.     if level == "fatal" then
  79.         return 0
  80.     elseif level == "error" then
  81.         return 1
  82.     elseif level == "warn" then
  83.         return 2
  84.     elseif level == "info" then
  85.         return 3
  86.     elseif level == "debug" then
  87.         return 4
  88.     elseif level == "trace" then
  89.         return 5
  90.     end
  91. end
  92.  
  93. function getCategoryLevel(category)
  94.     category = category or "log"
  95.     return log_category[category] or log_category["log"]
  96. end
  97.  
  98. function log(message, level, category)
  99.     level = level or "info"
  100.     category = category or "log"
  101.     if getLogLevel(level) <= getLogLevel(getCategoryLevel(category)) then
  102.         print(string.upper(level)..": "..message)
  103.         return true
  104.     end
  105.     return true
  106. end
  107.  
  108. function deleteTable(tab)
  109.     for k, v in pairs(tab) do
  110.         tab[k] = nil
  111.     end
  112.     tab = nil
  113. end
  114.  
  115. function savePosition(name, position)
  116.     position = position or getTurtlePosition()
  117.     if saved_positions[name] then error("Position "..name.." is already saved.") end
  118.     saved_positions[name] = shallowcopytable(position)
  119. end
  120.  
  121. function deletePosition(name)
  122.     if not getPosition(name) then error("Position "..name.." doesn't exist.") end
  123.     deleteTable(getPosition(name))
  124.     saved_positions[name] = nil
  125.     return (not saved_positions[name])
  126. end
  127.  
  128. function getPosition(name)
  129.     local pos = saved_positions[name]
  130.     if not pos then error("No position named: "..name) end
  131.     return pos
  132. end
  133.  
  134. function comparePositions(_position_one, _position_two)
  135.     if not _position_one then error("_position_one is nil.") end
  136.     if not _position_two then error("_position_two is nil.") end
  137.     log("x: ".._position_one["x"].."<>".._position_two["x"], "trace", "compare_positions")
  138.     log("y: ".._position_one["y"].."<>".._position_two["y"], "trace", "compare_positions")
  139.     log("z: ".._position_one["z"].."<>".._position_two["z"], "trace", "compare_positions")
  140.     log("face: "..getFace(_position_one["face"]).."<>"..getFace(_position_two["face"]), "trace", "compare_positions")
  141.     if _position_one["x"] ~= _position_two["x"] then
  142.         log("x: ".._position_one["x"].."<>".._position_two["x"], "trace", "compare_positions")
  143.         return false
  144.     end
  145.     if _position_one["y"] ~= _position_two["y"] then
  146.         log("y: ".._position_one["y"].."<>".._position_two["y"], "trace", "compare_positions")
  147.         return false
  148.     end
  149.     if _position_one["z"] ~= _position_two["z"] then
  150.         log("z: ".._position_one["z"].."<>".._position_two["z"], "trace", "compare_positions")
  151.         return false
  152.     end
  153.     if getFace(_position_one["face"]) ~= getFace(_position_two["face"]) then
  154.         log("face: "..getFace(_position_one["face"]).."<>"..getFace(_position_two["face"]), "trace", "compare_positions")
  155.         return false
  156.     end
  157.     return true
  158. end
  159.  
  160. -- 0 = North
  161. -- 1 = East
  162. -- 2 = South
  163. -- 3 = West
  164. function getFace(_position)
  165.     _position = _position or turtle_pos
  166.     return turtle_pos["face"] % 4
  167. end
  168.  
  169. function getFaceAsString(facing)
  170.     if facing == 0 then
  171.         return "North"
  172.     elseif facing == 1 then
  173.         return "East"
  174.     elseif facing == 2 then
  175.         return "South"
  176.     elseif facing == 3 then
  177.         return "West"
  178.     else
  179.         return ""
  180.     end
  181. end
  182.  
  183. function getFaceFromString(facing)
  184.     if facing == "North" then
  185.         return 0
  186.     elseif facing == "East" then
  187.         return 1
  188.     elseif facing == "South" then
  189.         return 2
  190.     elseif facing == "West" then
  191.         return 3
  192.     end
  193. end
  194.  
  195. function adjustPostion(adjustment)
  196.     local facing = getFace()
  197.     if facing == 0 then
  198.         turtle_pos["x"] = turtle_pos["x"] + adjustment
  199.     elseif facing == 1 then
  200.         turtle_pos["z"] = turtle_pos["z"] + adjustment
  201.     elseif facing == 2 then
  202.         adjustment = adjustment * -1
  203.         turtle_pos["x"] = turtle_pos["x"] + adjustment
  204.     elseif facing == 3 then
  205.         adjustment = adjustment * -1
  206.         turtle_pos["z"] = turtle_pos["z"] + adjustment
  207.     end
  208. end
  209.  
  210. function getTurtlePosition()
  211.     local v_turtle_pos = shallowcopytable(turtle_pos)
  212.     return v_turtle_pos
  213. end
  214.  
  215. function getFrontPosition()
  216.     local adjustment = 1
  217.     local facing = getFace()
  218.     local v_turtle_pos = shallowcopytable(turtle_pos)
  219.     if facing == 0 then
  220.         v_turtle_pos["x"] = v_turtle_pos["x"] + adjustment
  221.     elseif facing == 1 then
  222.         v_turtle_pos["z"] = v_turtle_pos["z"] + adjustment
  223.     elseif facing == 2 then
  224.         adjustment = adjustment * -1
  225.         v_turtle_pos["x"] = v_turtle_pos["x"] + adjustment
  226.     elseif facing == 3 then
  227.         adjustment = adjustment * -1
  228.         v_turtle_pos["z"] = v_turtle_pos["z"] + adjustment
  229.     end
  230.     return v_turtle_pos
  231. end
  232.  
  233. function turnToFace(target_facing, record)
  234.     if record == nil then record = true end
  235.     if type(target_facing) == "string" then
  236.         target_facing = getFaceFromString(target_facing)
  237.     end
  238.     while (getFace() ~= target_facing) do
  239.         turnLeft(record)
  240.     end
  241.     return true
  242. end
  243.  
  244. function turnLeft(record)
  245.     if record == nil then record = true end
  246.     if record then record = CRUMBS end
  247.     turtle.turnLeft()
  248.     if record then recordAction("turnLeft") end
  249.     turtle_pos["face"] = turtle_pos["face"] - 1
  250.     return true
  251. end
  252.  
  253. function turnRight(record)
  254.     if record == nil then record = true end
  255.     if record then record = CRUMBS end
  256.     turtle.turnRight()
  257.     if record then recordAction("turnRight") end
  258.     turtle_pos["face"] = turtle_pos["face"] + 1
  259.     return true
  260. end
  261.  
  262. function up(record)
  263.     if record == nil then record = true end
  264.     if record then record = CRUMBS end
  265.     local count = 0
  266.     while turtle.detectUp() and count <= 10 do
  267.         turtle.digUp()
  268.         os.sleep(0.1)
  269.         count = count + 1
  270.     end
  271.     if turtle.up() then
  272.         if record then recordAction("up") end
  273.         turtle_pos["y"] = turtle_pos["y"] + 1
  274.         return true
  275.     else
  276.         return false
  277.     end
  278. end
  279.  
  280. function down(record)
  281.     if record == nil then record = true end
  282.     if record then record = CRUMBS end
  283.     local count = 0
  284.     while turtle.detectDown() and count <= 10 do
  285.         turtle.digDown()
  286.         os.sleep(0.1)
  287.         count = count + 1
  288.     end
  289.     if turtle.down() then
  290.         if record then recordAction("down") end
  291.         turtle_pos["y"] = turtle_pos["y"] - 1
  292.         return true
  293.     else
  294.         return false
  295.     end
  296. end
  297.  
  298. function forward(record)
  299.     if record == nil then record = true end
  300.     if record then record = CRUMBS end
  301.     local count = 0
  302.     while turtle.detect() and count <= 10 do
  303.         turtle.dig()
  304.         count = count + 1
  305.     end
  306.     if turtle.forward() then
  307.         if record then recordAction("forward") end
  308.         adjustPostion(1)
  309.         return true
  310.     else
  311.         return false
  312.     end
  313. end
  314.  
  315. function back(record)
  316.     if record == nil then record = true end
  317.     if record then record = CRUMBS end
  318.     local ret_val
  319.     if turtle.back() then
  320.         if record then recordAction("back") end
  321.         adjustPostion(-1)
  322.         return true
  323.     else
  324.         turnAround(false)
  325.         ret_val = forward(false)
  326.         turnAround(false)
  327.         if record then recordAction("back") end
  328.         return ret_val
  329.     end
  330. end
  331.  
  332. function strafeLeft(record)
  333.     local return_value = true
  334.     turnLeft(record)
  335.     return_value = forward(record)
  336.     turnRight(record)
  337.     return return_value
  338. end
  339.  
  340. function strafeRight(record)
  341.     local return_value = true
  342.     turnRight(record)
  343.     return_value = forward(record)
  344.     turnLeft(record)
  345.     return return_value
  346. end
  347.  
  348. function turnAround(record)
  349.     turnRight(record)
  350.     return turnRight(record)
  351. end
  352.  
  353. function clearRedoTable()
  354.     for i, v in ipairs(redo_crumbs) do
  355.         table.remove(redo_crumbs, i)
  356.     end
  357. end
  358.  
  359. function recordAction(action)
  360.     if not redoing then
  361.         clearRedoTable()
  362.     end
  363.     table.insert(crumbs, action)
  364. end
  365.  
  366. function rewind(numberOfMoves, record)
  367.     local return_value = false
  368.     if record == nil then record = true end
  369.     if CRUMBS then
  370.         local numberOfMoves = numberOfMoves or 1
  371.         local count = 0
  372.         local action = false
  373.         repeat
  374.             count = count + 1
  375.             action = table.remove(crumbs)
  376.             if action then
  377.                 log("Rewinding: "..action, "debug", 'breadcrumbs')
  378.                 return_value = undoAction(action, record)
  379.             else
  380.                 log("Rewinding: nil", "debug", 'breadcrumbs')
  381.                 return_value = false
  382.             end
  383.         until ((count >= numberOfMoves) or (not return_value))
  384.     end
  385.     return return_value
  386. end
  387.  
  388. function rewindToPosition(position, record)
  389.     local return_value
  390.     if record == nil then record = true end
  391.     repeat
  392.         return_value = rewind(1, record)
  393.     until ((not return_value) or (comparePositions(turtle_pos, position)))
  394. end
  395.  
  396. function redo(numberOfMoves)
  397.     local return_value = false
  398.     if CRUMBS then
  399.         local numberOfMoves = numberOfMoves or 1
  400.         local count = 0
  401.         local action
  402.         redoing = true
  403.         repeat
  404.             count = count + 1
  405.             action = table.remove(redo_crumbs)
  406.             if action then
  407.                 log("Redoing: "..action, "debug", 'breadcrumbs')
  408.                 return_value = doAction(action)
  409.             else
  410.                 log("Redoing: nil", "debug", 'breadcrumbs')
  411.                 return_value = false
  412.             end
  413.         until ( (count >= numberOfMoves) or (not return_value) )
  414.         redoing = false
  415.     end
  416.     return return_value
  417. end
  418.  
  419. function resumeToPosition(position, record)
  420.     if record == nil then record = true end
  421.     local return_value
  422.     repeat
  423.         return_value = redo()
  424.     until ((comparePositions(turtle_pos, position)) or (not return_value))
  425. end
  426.  
  427. function getOppositeAction(action)
  428.     if not action then error("No action passed to do action.") end
  429.     local lower_action = string.lower(action)
  430.     if lower_action == "turnleft" then
  431.         return "turnRight"
  432.     elseif lower_action == "turnright" then
  433.         return "turnLeft"
  434.     elseif lower_action == "up" then
  435.         return "down"
  436.     elseif lower_action == "down" then
  437.         return "up"
  438.     elseif lower_action == "forward" then
  439.         return "back"
  440.     elseif lower_action == "back" then
  441.         return "forward"
  442.     else
  443.         error("Invalid action passed: \"",action,"\"")
  444.         return false
  445.     end
  446. end
  447.  
  448. function doAction(action, record)
  449.     if not action then error("No action passed to do action.") end
  450.     local lower_action = string.lower(action)
  451.     if lower_action == "turnleft" then
  452.         return turnLeft(record)
  453.     elseif lower_action == "turnright" then
  454.         return turnRight(record)
  455.     elseif lower_action == "up" then
  456.         return up(record)
  457.     elseif lower_action == "down" then
  458.         return down(record)
  459.     elseif lower_action == "forward" then
  460.         return forward(record)
  461.     elseif lower_action == "back" then
  462.         return back(record)
  463.     else
  464.         error("Invalid action passed: \"",action,"\"")
  465.         return false
  466.     end
  467. end
  468.  
  469. function undoAction(action, record)
  470.     if not action then error("Passed nil as action to undo") return false end
  471.     if record == nil then record = true end
  472.     local result
  473.     result = doAction(getOppositeAction(action), false)
  474.     if record then
  475.         table.insert(redo_crumbs, action)
  476.     end
  477.     return result
  478. end
  479.  
  480. function getPositionString(_position)
  481.     _position = _position or turtle_pos
  482.     return "[",_position["x"],",",_position["y"],",",_position["z"],",",getFaceAsString(getFace(_position["face"])),"]"
  483. end
  484.  
  485. function printPos(_position)
  486.     print(getPositionString(_position))
  487. end
  488.  
  489. function checkFillWhiteList(block_name)
  490.     if filler_whitelist[block_name] then
  491.         return true
  492.     else
  493.         return false
  494.     end
  495. end
  496.  
  497. function checkOreFlag()
  498.     return ore_flagged
  499. end
  500.  
  501. function clearOreFlag()
  502.     ore_flagged = false
  503. end
  504.  
  505. function checkWhiteList(block_data)
  506.     block_name = block_data["name"]
  507.     for tag, bl in pairs(block_data.tags) do
  508.         if bl and dig_whitelist_tags[tag] then
  509.             return true
  510.         end
  511.     end
  512.     if dig_whitelist[block_name] then
  513.         return true
  514.     else
  515.         return false
  516.     end
  517. end
  518.  
  519. function checkBlackList(block_data)
  520.     block_name = block_data["name"]
  521.     for tag, bl in pairs(block_data.tags) do
  522.         if bl and dig_blacklist_tags[tag] then
  523.             return true
  524.         end
  525.     end
  526.     if flag_whitelist[block_name] then
  527.         ore_flagged = true
  528.         return true
  529.     end
  530.     if dig_blacklist[block_name] then
  531.         return true
  532.     else
  533.         return false
  534.     end
  535. end
  536.  
  537. function isBlockValuable(block_data)
  538.     local valuable = false
  539.     if checkWhiteList(block_data) then
  540.         valuable = true
  541.     end
  542.     if checkBlackList(block_data) then
  543.         valuable = false
  544.     end
  545.     return valuable
  546. end
  547.  
  548. function restockFillMaterial()
  549.     local slot_data
  550.     local orig_select = turtle.getSelectedSlot()
  551.     local filler_space = turtle.getItemSpace(FILLER_SLOT)
  552.     if turtle.getItemCount(FILLER_SLOT) == 0 then
  553.         for slot=1,12 do
  554.              slot_data = turtle.getItemDetail(slot)
  555.             if slot_data and checkFillWhiteList(slot_data.name) then
  556.                 turtle.select(slot)
  557.                 turtle.transferTo(FILLER_SLOT)
  558.             end
  559.         end
  560.     end
  561.     if filler_space > 0 then
  562.         for slot=1,12 do
  563.             turtle.select(slot)
  564.             if turtle.compareTo(FILLER_SLOT) then
  565.                 turtle.transferTo(FILLER_SLOT, filler_space)
  566.                 filler_space = turtle.getItemSpace(FILLER_SLOT)
  567.                 if filler_space == 0 then
  568.                     break
  569.                 end
  570.             end
  571.             os.sleep(1)
  572.         end
  573.     end
  574.     turtle.select(orig_select)
  575. end
  576.  
  577. function fill()
  578.     local orig_select = turtle.getSelectedSlot()
  579.     turtle.select(FILLER_SLOT)
  580.     turtle.place()
  581.     if turtle.getItemCount() == 0 then
  582.         restockFillMaterial()
  583.     end
  584.     turtle.select(orig_select)
  585. end
  586.  
  587. function fillUp()
  588.     local orig_select = turtle.getSelectedSlot()
  589.     turtle.select(FILLER_SLOT)
  590.     turtle.placeUp()
  591.     if turtle.getItemCount() == 0 then
  592.         restockFillMaterial()
  593.     end
  594.     turtle.select(orig_select)
  595. end
  596.  
  597. function fillDown()
  598.     local orig_select = turtle.getSelectedSlot()
  599.     turtle.select(FILLER_SLOT)
  600.     turtle.placeDown()
  601.     if turtle.getItemCount() == 1 then
  602.         restockFillMaterial()
  603.     end
  604.     turtle.select(orig_select)
  605. end
  606.  
  607. function processLavaUp()
  608.     local present, block_data = turtle.inspectUp()
  609.     local block_name = block_data.name
  610.     if present then
  611.         if string.find(block_name, 'lava') then
  612.             turtle.select(BUCKET_SLOT)
  613.             turtle.placeUp()
  614.             turtle.refuel()
  615.             return false
  616.         end
  617.     end
  618. end
  619.  
  620. function processLava()
  621.     local present, block_data = turtle.inspect()
  622.     local block_name = block_data.name
  623.     if present then
  624.         if string.find(block_name, 'lava') then
  625.             turtle.select(BUCKET_SLOT)
  626.             turtle.place()
  627.             turtle.refuel()
  628.             return false
  629.         end
  630.     end
  631. end
  632.  
  633. function processLavaDown()
  634.     local present, block_data = turtle.inspectDown()
  635.     local block_name = block_data.name
  636.     if present then
  637.         if string.find(block_name, 'lava') then
  638.             turtle.select(BUCKET_SLOT)
  639.             turtle.placeDown()
  640.             turtle.refuel()
  641.             return false
  642.         end
  643.     end
  644. end
  645.  
  646. function mine(count, height, placement)
  647.     turtle.select(1)
  648.     count = count or 1
  649.     if placement == nil then placement = true end
  650.     height = height or 3
  651.     local counter=0
  652.     repeat
  653.         counter = counter + 1
  654.         processLava()
  655.         forward()
  656.         local present, block_data = turtle.inspect()
  657.         if present then
  658.             if string.find(block_data.name, 'water') then
  659.                 fill()
  660.             end
  661.         end
  662.         local present, block_data = turtle.inspectUp()
  663.         if present then
  664.             if string.find(block_data.name, 'water') then
  665.                 up(false)
  666.                 fill()
  667.                 fillUp()
  668.                 down(false)
  669.                 fillUp()
  670.             end
  671.         end
  672.         local present, block_data = turtle.inspectDown()
  673.         if present then
  674.             if string.find(block_data.name, 'water') then
  675.                 down(false)
  676.                 fill()
  677.                 up(false)
  678.                 fillDown()
  679.             end
  680.         end
  681.         log("Turtle position: "..getPositionString(), "debug", "mine")
  682.         processLavaUp()
  683.         processLavaDown()
  684.         if turtle.detectUp() then
  685.             turtle.digUp()
  686.         end
  687.         if turtle.detectDown() then
  688.             turtle.digDown()
  689.         end
  690.         mineWalls(height, placement)
  691.         os.sleep(0.2)
  692.     until (counter >= count)
  693. end
  694.  
  695. function checkBlockUp()
  696.     local present, block_data = turtle.inspectUp()
  697.     if not present then
  698.         return false
  699.     end
  700.     return isBlockValuable(block_data)
  701. end
  702.  
  703. function checkBlock()
  704.     local present, block_data = turtle.inspect()
  705.     if not present then
  706.         return false
  707.     end
  708.     return isBlockValuable(block_data)
  709. end
  710.  
  711. function checkBlockDown()
  712.     local present, block_data = turtle.inspectDown()
  713.     if not present then
  714.         return false
  715.     end
  716.     return isBlockValuable(block_data)
  717. end
  718.  
  719. function mineOre()
  720.     if stack >= MAX_STACK_DEPTH then
  721.         log("Maximum recursion reached.", "debug", "mine")
  722.         return true
  723.     end
  724.     os.sleep(stack/10.0)
  725.     stack = stack + 1
  726.     forward()
  727.     processLava()
  728.     if checkBlock() then
  729.         mineOre()
  730.     end
  731.     turnLeft(false)
  732.     processLava()
  733.     if checkBlock() then
  734.         mineOre()
  735.     end
  736.     turnLeft(false)
  737.     processLava()
  738.     if checkBlock() then
  739.         mineOre()
  740.     end
  741.     turnLeft(false)
  742.     processLava()
  743.     if checkBlock() then
  744.         mineOre()
  745.     end
  746.     turnLeft(false)
  747.     processLavaUp()
  748.     if checkBlockUp() then
  749.         mineOreUp()
  750.     end
  751.     if checkBlockDown() then
  752.         mineOreDown()
  753.     end
  754.     rewind(1, false)
  755.     fill()
  756.     stack = stack - 1
  757. end
  758.  
  759. function mineOreUp()
  760.     if stack >= MAX_STACK_DEPTH then
  761.         log("Maximum recursion reached.", "debug", "mine")
  762.         return true
  763.     end
  764.     os.sleep(stack/10.0)
  765.     stack = stack + 1
  766.     up()
  767.     processLava()
  768.     if checkBlock() then
  769.         mineOre()
  770.     end
  771.     turnLeft(false)
  772.     processLava()
  773.     if checkBlock() then
  774.         mineOre()
  775.     end
  776.     turnLeft(false)
  777.     processLava()
  778.     if checkBlock() then
  779.         mineOre()
  780.     end
  781.     turnLeft(false)
  782.     processLava()
  783.     if checkBlock() then
  784.         mineOre()
  785.     end
  786.     turnLeft(false)
  787.     processLavaUp()
  788.     if checkBlockUp() then
  789.         mineOreUp()
  790.     end
  791.     processLavaDown()
  792.     if checkBlockDown() then
  793.         mineOreDown()
  794.     end
  795.     rewind(1, false)
  796.     fillUp()
  797.     stack = stack - 1
  798. end
  799.  
  800. function mineOreDown()
  801.     if stack >= MAX_STACK_DEPTH then
  802.         log("Maximum recursion reached.", "debug", 'mine')
  803.         return true
  804.     end
  805.     os.sleep(stack/10.0)
  806.     stack = stack + 1
  807.     down()
  808.     processLava()
  809.     if checkBlock() then
  810.         mineOre()
  811.     end
  812.     turnLeft(false)
  813.     processLava()
  814.     if checkBlock() then
  815.         mineOre()
  816.     end
  817.     turnLeft(false)
  818.     processLava()
  819.     if checkBlock() then
  820.         mineOre()
  821.     end
  822.     turnLeft(false)
  823.     processLava()
  824.     if checkBlock() then
  825.         mineOre()
  826.     end
  827.     processLava()
  828.     turnLeft(false)
  829.     processLavaUp()
  830.     if checkBlockUp() then
  831.         mineOreUp()
  832.     end
  833.     processLavaDown()
  834.     if checkBlockDown() then
  835.         mineOreDown()
  836.     end
  837.     rewind(1, false)
  838.     fillDown()
  839.     stack = stack - 1
  840. end
  841.  
  842. function processBlockUp(placement)
  843.     if placement == nil then placement = true end
  844.     processLavaUp()
  845.     if checkBlockUp() then
  846.         mineOreUp()
  847.     end
  848.     if placement then
  849.         if not turtle.detectUp() then
  850.             fillUp()
  851.         end
  852.     end
  853. end
  854.  
  855. function processBlock(placement)
  856.     if placement == nil then placement = true end
  857.     processLava()
  858.     if checkBlock() then
  859.         mineOre()
  860.     end
  861.     if placement then
  862.         if not turtle.detect() then
  863.             fill()
  864.         end
  865.     end
  866. end
  867.  
  868. function processBlockDown(placement)
  869.     if placement == nil then placement = true end
  870.     processLavaDown()
  871.     if checkBlockDown() then
  872.         mineOreDown()
  873.     end
  874.     if placement then
  875.         if not turtle.detectDown() then
  876.             fillDown()
  877.         end
  878.     end
  879. end
  880.  
  881. function processLevel()
  882.     local facing = getFace()
  883.     repeat
  884.         processLava()
  885.         processBlock()
  886.         turnLeft(false)
  887.     until (facing == getFace())
  888.     return true
  889. end
  890.  
  891. function mineWalls(height, placement)
  892.     if placement == nil then placement = true end
  893.     height = height or 3
  894.     local y_start = turtle_pos["y"]
  895.     local tar_y_pos = turtle_pos["y"] + height - 1
  896.     down(false)
  897.     processBlockDown(placement)
  898.     turnRight(false)
  899.     processBlock(placement)
  900.     turnAround(false)
  901.     processBlock(placement)
  902.     up(false)
  903.     processBlock(placement)
  904.     turnAround(false)
  905.     processBlock(placement)
  906.     up(false)
  907.     processBlock(placement)
  908.     turnAround(false)
  909.     processBlock(placement)
  910.     processBlockUp(placement)
  911.     turnRight(false)
  912.     down(false)
  913. end
  914.  
  915. function emptyInventory()
  916.     local og_slot = turtle.getSelectedSlot()
  917.     turtle.select(CHEST_SLOT)
  918.     down(false)
  919.     turtle.digDown()
  920.     turtle.placeDown()
  921.     for slot=1,12 do
  922.         turtle.select(slot)
  923.         turtle.dropDown()
  924.     end
  925.     turtle.select(og_slot)
  926.     up(false)
  927. end
  928.  
  929. function clearInventoryAndRefuel()
  930.     savePosition("resume")
  931.     repeat
  932.         rewind()
  933.     until (comparePositions(turtle_pos, getPosition("start")))
  934.     log(getPositionString(), "debug", "inventory")
  935.     for slot=1,12 do
  936.             turtle.select(slot)
  937.             turtle.dropDown()
  938.     end
  939.     turnRight(false)
  940.     turtle.select(2)
  941.     turtle.suck()
  942.     turtle.refuel()
  943.     turnLeft(false)
  944.     turnLeft(false)
  945.     turtle.select(TORCH_SLOT)
  946.     turtle.suck()
  947.     turtle.select(FILLER_SLOT)
  948.     turtle.suck()
  949.     turnRight(false)
  950.     repeat
  951.         redo()
  952.     until (comparePositions(turtle_pos, getPosition("resume")))
  953.     deletePosition("resume")
  954.     log(getPositionString(), "debug", "inventory")
  955. end
  956.  
  957. function placeTorchUp()
  958.     local orig_sel = turtle.getSelectedSlot()
  959.     turtle.select(TORCH_SLOT)
  960.     turtle.placeUp()
  961.     turtle.select(orig_sel)
  962. end
  963.  
  964. function placeTorch()
  965.     local orig_sel = turtle.getSelectedSlot()
  966.     turtle.select(TORCH_SLOT)
  967.     turtle.place()
  968.     turtle.select(orig_sel)
  969. end
  970.  
  971. function placeTorchDown()
  972.     local orig_sel = turtle.getSelectedSlot()
  973.     turtle.select(TORCH_SLOT)
  974.     turtle.placeDown()
  975.     turtle.select(orig_sel)
  976. end
  977.  
  978. function processInventory()
  979.     local og_slot
  980.     local inventory = {}
  981.     local full = true
  982.     local torch_data = turtle.getItemDetail(TORCH_SLOT)
  983.     local filler_data = turtle.getItemDetail(FILLER_SLOT)
  984.     if torch_data and turtle.getItemSpace(TORCH_SLOT) > 0 then
  985.         inventory[ torch_data.name] = TORCH_SLOT
  986.     end
  987.     if filler_data and turtle.getItemCount(FILLER_SLOT) > 0 then
  988.         inventory[filler_data.name] = FILLER_SLOT
  989.     end
  990.     for slot=1,12 do
  991.         local slot_info = turtle.getItemDetail(slot)
  992.         if slot_info then
  993.             if turtle.getItemSpace(slot) > 0 then
  994.                 if inventory[slot_info.name] then
  995.                     turtle.select(slot)
  996.                     turtle.transferTo(inventory[slot_info.name], turtle.getItemSpace(inventory[slot_info.name]))
  997.                     if turtle.getItemCount() > 0 then
  998.                         inventory[slot_info.name] = slot
  999.                     elseif turtle.getItemSpace(inventory[slot_info.name]) == 0 then
  1000.                         inventory[slot_info.name] = nil
  1001.                     end
  1002.                     if turtle.getItemCount() == 0 then
  1003.                         full = false
  1004.                     end
  1005.                 else
  1006.                     inventory[slot_info.name] = slot
  1007.                 end
  1008.             end
  1009.         else
  1010.             full = false
  1011.         end
  1012.     end
  1013.     return full
  1014. end
  1015.  
  1016. ------------------------------
  1017. -- Lava Swim Functions
  1018. -- These use a bucket to pickup lava move in then place it behind
  1019. ------------------------------
  1020. function lavaSwim()
  1021.     local orig_sel = turtle.getSelectedSlot()
  1022.     turtle.select(BUCKET_SLOT)
  1023.     turtle.place()
  1024.     forward()
  1025.     turnAround(false)
  1026.     turtle.place()
  1027.     turnAround(false)
  1028.     turtle.select(orig_sel)
  1029. end
  1030.  
  1031. function lavaSwimUp()
  1032.     local orig_sel = turtle.getselectedslot()
  1033.     turtle.select(BUCKET_SLOT)
  1034.     turtle.placeUp()
  1035.     up()
  1036.     turtle.placeDown()
  1037.     turtle.select(orig_sel)
  1038. end
  1039.  
  1040. function lavaSwimDown()
  1041.     local orig_sel = turtle.getselectedslot()
  1042.     turtle.select(BUCKET_SLOT)
  1043.     turtle.placeDown()
  1044.     down()
  1045.     turtle.placeUp()
  1046.     turtle.select(orig_sel)
  1047. end
  1048.  
  1049. init()
  1050.  
Add Comment
Please, Sign In to add comment