Advertisement
jille_Jr

[CC] Turtle Do - by Slamakans

Nov 20th, 2017
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Defaults
  2. local flags = {
  3.     ["placing"] = false,
  4.     ["placingDown"] = false,
  5.     ["placingUp"] = false,
  6.     ["placingLeft"] = false,
  7.     ["placingRight"] = false,
  8.     ["placingBack"] = false,
  9.     ["digging"] = false,
  10.     ["diggingUp"] = false,
  11.     ["diggingDown"] = false,
  12.     ["diggingLeft"] = false,
  13.     ["diggingRight"] = false,
  14.     ["attacking"] = true,
  15. }
  16.  
  17. local fuelList = {
  18.     "minecraft:coal",
  19.     "minecraft:planks",
  20.     "minecraft:log",
  21.     "minecraft:stick",
  22.     "minecraft:coal_block",
  23. }
  24.  
  25. function contains(table, element)
  26.     for _,v in ipairs(table) do
  27.         if v == element then
  28.             return true
  29.         end
  30.     end
  31.     return false
  32. end
  33.  
  34. function selectFromList(list)
  35.     local selected = turtle.getItemDetail()
  36.     if selected and contains(list, selected.name) then
  37.         -- Already selected
  38.         return true
  39.     end
  40.  
  41.     for slot=16,1,-1 do
  42.         local data = turtle.getItemDetail(slot)
  43.  
  44.         if data then
  45.             if contains(list, data.name) then
  46.                 turtle.select(slot)
  47.                 return true
  48.             end
  49.         end
  50.     end
  51.     return false
  52. end
  53.  
  54. function refuelIfLow(val, amt)
  55.     val = val or 5
  56.     amt = amt or 1
  57.     if turtle.getFuelLevel() < val then
  58.         if selectFromList(fuelList) then
  59.             turtle.refuel(amt)
  60.         end
  61.     end
  62. end
  63.  
  64. function select(slot)
  65.     turtle.select(slot)
  66. end
  67.  
  68. function togglePlace()
  69.     flags["placing"] = not flags["placing"]
  70. end
  71.  
  72. function togglePlaceDown()
  73.     flags["placingDown"] = not flags["placingDown"]
  74. end
  75.  
  76. function togglePlaceUp()
  77.     flags["placingUp"] = not flags["placingUp"]
  78. end
  79.  
  80. function togglePlaceLeft()
  81.     flags["placingLeft"] = not flags["placingLeft"]
  82. end
  83.  
  84. function togglePlaceRight()
  85.     flags["placingRight"] = not flags["placingRight"]
  86. end
  87.  
  88. function togglePlaceBack()
  89.     flags["placingBack"] = not flags["placingBack"]
  90. end
  91.  
  92. function toggleDig()
  93.     flags["digging"] = not flags["digging"]
  94. end
  95.  
  96. function toggleDigUp()
  97.     flags["diggingUp"] = not flags["diggingUp"]
  98. end
  99.  
  100. function toggleDigDown()
  101.     flags["diggingDown"] = not flags["diggingDown"]
  102. end
  103.  
  104. function toggleDigLeft()
  105.     flags["diggingLeft"] = not flags["diggingLeft"]
  106. end
  107.  
  108. function toggleDigRight()
  109.     flags["diggingRight"] = not flags["diggingRight"]
  110. end
  111.  
  112. function toggleAttack()
  113.     flags["attacking"] = not flags["attacking"]
  114. end
  115.  
  116. function turnRight(turns)
  117.     turns = turns or 1
  118.     for i=1,turns do
  119.         turtle.turnRight()
  120.     end
  121. end
  122.  
  123. function turnLeft(turns)
  124.     turns = turns or 1
  125.     for i=1,turns do
  126.         turtle.turnLeft()
  127.     end
  128. end
  129.  
  130. function move(func, blocks)
  131.     blocks = blocks or 1
  132.     local digFunc
  133.     if func == "f" then
  134.         func = turtle.forward
  135.         digFunc = turtle.dig
  136.     elseif func == "u" then
  137.         func = turtle.up
  138.         digFunc = turtle.digUp
  139.     elseif func == "d" then
  140.         func = turtle.down
  141.         digFunc = turtle.digDown
  142.     elseif func == "b" then
  143.         func = turtle.back
  144.         digFunc = turtle.dig
  145.     end
  146.  
  147.     for block=1,blocks do
  148.         if flags["placing"] and not turtle.detect() then
  149.             turtle.place()
  150.         end
  151.  
  152.         if flags["placingDown"] and not turtle.detectDown() then
  153.             turtle.placeDown()
  154.         end
  155.  
  156.         if flags["placingUp"] and not turtle.detectUp() then
  157.             turtle.placeUp()
  158.         end
  159.  
  160.         if flags["placingLeft"] then
  161.             turnLeft()
  162.             turtle.place()
  163.             turnRight()
  164.         end
  165.  
  166.         if flags["placingRight"] then
  167.             turnRight()
  168.             turtle.place()
  169.             turnLeft()
  170.         end
  171.  
  172.         if flags["placingBack"] then
  173.             if math.random(2) == 1 then
  174.                 turnRight(2)
  175.             else
  176.                 turnLeft(2)
  177.             end
  178.             turtle.place()
  179.             if math.random(2) == 1 then
  180.                 turnRight(2)
  181.             else
  182.                 turnLeft(2)
  183.             end
  184.         end
  185.  
  186.         local dug, digError
  187.         if flags["digging"] then
  188.             dug, digError = turtle.dig()
  189.             if not dug and flags["attacking"] then
  190.                 repeat until not turtle.attack()
  191.             end
  192.         end
  193.  
  194.         if flags["diggingUp"] then
  195.             turtle.digUp()
  196.         end
  197.  
  198.         if flags["diggingDown"] then
  199.             turtle.digDown()
  200.         end
  201.  
  202.         if flags["diggingLeft"] then
  203.             turnLeft()
  204.             dug, digError = turtle.dig()
  205.             if not dug and flags["attacking"] then
  206.                 repeat until not turtle.attack()
  207.             end
  208.             turnRight()
  209.         end
  210.  
  211.         if flags["diggingRight"] then
  212.             turnRight()
  213.             dug, digError = turtle.dig()
  214.             if not dug and flags["attacking"] then
  215.                 repeat until not turtle.attack()
  216.             end
  217.             turnLeft()
  218.         end
  219.  
  220.         refuelIfLow()
  221.         local moved = func()
  222.         if not moved then
  223.             repeat
  224.                 local dug,err
  225.                 if flags["digging"] then
  226.                     dug,err = digFunc()
  227.                 end
  228.  
  229.                 if flags["attacking"] and not dug then
  230.                     if not turtle.attack() then
  231.                         print("I think I hit bedrock, skipping to next command...")
  232.                         return false
  233.                     end
  234.                 end
  235.             until func()
  236.         end
  237.     end
  238.  
  239.     return true
  240. end
  241.  
  242. function forward(blocks)
  243.     return move("f", blocks)
  244. end
  245.  
  246. function right(blocks)
  247.     turnRight()
  248.     local result = move("f", blocks)
  249.     turnLeft()
  250.     return result
  251. end
  252.  
  253. function left(blocks)
  254.     turnLeft()
  255.     local result = move("f", blocks)
  256.     turnRight()
  257.     return result
  258. end
  259.  
  260. function back(blocks)
  261.     if flags["attacking"] or flags["digging"] then
  262.         if math.random(2) == 1 then
  263.             turnRight(2)
  264.         else
  265.             turnLeft(2)
  266.         end
  267.     end
  268.     local result = move((flags["attacking"] or flags["digging"]) and "f" or "b", blocks)
  269.     if flags["attacking"] or flags["digging"] then
  270.         if math.random(2) == 1 then
  271.             turnRight(2)
  272.         else
  273.             turnLeft(2)
  274.         end
  275.     end
  276.     return result
  277. end
  278.  
  279. function up(blocks)
  280.     return move("u", blocks)
  281. end
  282.  
  283. function down(blocks)
  284.     return move("d", blocks)
  285. end
  286.  
  287. function place()
  288.     return turtle.place()
  289. end
  290.  
  291. function placeDown()
  292.     return turtle.placeDown()
  293. end
  294.  
  295. function placeUp()
  296.     return turtle.placeUp()
  297. end
  298.  
  299. function placeBack()
  300.     if math.random(2) == 1 then
  301.         turnRight(2)
  302.     else
  303.         turnLeft(2)
  304.     end
  305.     local result = turtle.place()
  306.     if math.random(2) == 1 then
  307.         turnRight(2)
  308.     else
  309.         turnLeft(2)
  310.     end
  311.     return result
  312. end
  313.  
  314. function placeLeft()
  315.     turnLeft()
  316.     local result = turtle.place()
  317.     turnRight()
  318.     return result
  319. end
  320.  
  321. function placeRight()
  322.     turnRight()
  323.     local result = turtle.place()
  324.     turnLeft()
  325.     return result
  326. end
  327.  
  328. function dig()
  329.     return turtle.dig()
  330. end
  331.  
  332. function digUp()
  333.     return turtle.digUp()
  334. end
  335.  
  336. function digDown()
  337.     return turtle.digDown()
  338. end
  339.  
  340. function digRight()
  341.     turnRight()
  342.     local result = turtle.dig()
  343.     turnLeft()
  344.     return result
  345. end
  346.  
  347. function digLeft()
  348.     turnLeft()
  349.     local result = turtle.dig()
  350.     turnRight()
  351.     return result
  352. end
  353.  
  354. function digBack()
  355.     if math.random(2) == 1 then
  356.         turnRight(2)
  357.     else
  358.         turnLeft(2)
  359.     end
  360.     local result = turtle.dig()
  361.     if math.random(2) == 1 then
  362.         turnRight(2)
  363.     else
  364.         turnLeft(2)
  365.     end
  366.     return result
  367. end
  368.  
  369. local commands = {
  370.     ["f"] = forward, ["fd"] = forward, ["forward"] = forward,
  371.     ["r"] = right, ["right"] = right,
  372.     ["l"] = left, ["left"] = left,
  373.     ["b"] = back, ["back"] = back,
  374.     ["u"] = up, ["up"] = up,
  375.     ["d"] = down, ["down"] = down,
  376.     ["tr"] = turnRight, ["rt"] = turnRight, ["turnright"] = turnRight,
  377.     ["tl"] = turnLeft, ["lt"] = turnLeft, ["turnleft"] = turnLeft,
  378.     ["s"] = select, ["sl"] = select, ["sel"] = select, ["select"] = select,
  379.     ["p"] = place, ["place"] = place,
  380.     ["tp"] = togglePlace, ["toggleplace"] = togglePlace,
  381.     ["pd"] = placeDown, ["placedown"] = placeDown,
  382.     ["tpd"] = togglePlaceDown, ["toggleplacedown"] = togglePlaceDown,
  383.     ["pu"] = placeUp, ["placeup"] = placeUp,
  384.     ["tpu"] = togglePlaceUp, ["toggleplaceup"] = togglePlaceUp,
  385.     ["pl"] = placeLeft, ["placeleft"] = placeLeft,
  386.     ["tpl"] = togglePlaceLeft, ["toggleplaceleft"] = togglePlaceLeft,
  387.     ["pr"] = placeRight, ["placeright"] = placeRight,
  388.     ["tpr"] = togglePlaceRight, ["toggleplaceright"] = togglePlaceRight,
  389.     ["pb"] = placeBack, ["placeback"] = placeBack,
  390.     ["tpb"] = togglePlaceBack, ["tpb"] = togglePlaceBack,
  391.     ["dig"] = dig,
  392.     ["td"] = toggleDig, ["tdig"] = toggleDig, ["toggledig"] = toggleDig,
  393.     ["du"] = digUp, ["digup"] = digUp,
  394.     ["tdu"] = toggleDigUp, ["tdigup"] = toggleDigUp, ["toggledigup"] = toggleDigUp,
  395.     ["dd"] = digDown, ["digdown"] = digDown,
  396.     ["tdd"] = toggleDigDown, ["tdigdown"] = toggleDigDown, ["toggledigdown"] = toggleDigDown,
  397.     ["dr"] = digRight, ["digright"] = digRight,
  398.     ["tdr"] = toggleDigRight, ["tdigright"] = toggleDigRight, ["toggledigright"] = toggleDigRight,
  399.     ["dl"] = digLeft, ["digleft"] = digLeft,
  400.     ["tdl"] = toggleDigLeft, ["tdigleft"] = toggleDigLeft, ["toggledigleft"] = toggleDigLeft,
  401.     ["db"] = digBack, ["digback"] = digBack,
  402.     ["ta"] = toggleAttack, ["toggleattack"] = toggleAttack,
  403. }
  404.  
  405. function shift(t)
  406.     return table.remove(t, 1)
  407. end
  408.  
  409. local queue = {}
  410.  
  411. function join(t, s)
  412.     local result = ""
  413.     for i,v in ipairs(t) do
  414.         result = result .. v
  415.         if i ~= #t then
  416.             result = result .. s
  417.         end
  418.     end
  419.    
  420.     return result
  421. end
  422.  
  423. function trim(s)
  424.   return (s:gsub("^%s*(.-)%s*$", "%1"))
  425. end
  426.  
  427. function capture(s)
  428.     local p = "(%b()) (%d%d?)"
  429.     local _,_,group,times = s:find(p)
  430.  
  431.     if not group then
  432.         return false
  433.     end
  434.  
  435.     local times = tonumber(times)
  436.     local result = trim(
  437.         s:gsub(p, (group:sub(2, -2) .. " "):rep(times))
  438.     )
  439.  
  440.     return result
  441. end
  442.  
  443. function parse(s)
  444.     local result = s
  445.     repeat
  446.         result = capture(s) or result
  447.     until not capture(result)
  448.  
  449.     return result
  450. end
  451.  
  452. function validate(name)
  453.     return name and (commands[name] or shell.resolveProgram(name))
  454. end
  455.  
  456. local args = {...}
  457. local joinedArgs = join(args, " ")
  458. local unpackedArgs = {}
  459. for word in parse(joinedArgs):gmatch("%S+") do table.insert(unpackedArgs, word) end
  460.  
  461. while #unpackedArgs ~= 0 do
  462.     local name = shift(unpackedArgs)
  463.     if not validate(name) then
  464.         print(name .. " is not a valid program")
  465.         return
  466.     end
  467.     local arg = tonumber(unpackedArgs[1])
  468.     if arg ~= nil then
  469.         shift(unpackedArgs)
  470.     end
  471.     table.insert(queue, { ["name"] = name, ["arg"] = arg })
  472. end
  473.  
  474. for i,v in ipairs(queue) do
  475.     local name, arg = v.name:lower(), v.arg
  476.     local cmd = commands[name]
  477.  
  478.     if cmd then
  479.         cmd(arg)
  480.     elseif name then
  481.         local resolved = shell.resolveProgram(name)
  482.         if resolved then
  483.             shell.run(resolved, arg)
  484.         else
  485.         end
  486.     end
  487. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement