Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Defaults
- local flags = {
- ["placing"] = false,
- ["placingDown"] = false,
- ["placingUp"] = false,
- ["placingLeft"] = false,
- ["placingRight"] = false,
- ["placingBack"] = false,
- ["digging"] = false,
- ["diggingUp"] = false,
- ["diggingDown"] = false,
- ["diggingLeft"] = false,
- ["diggingRight"] = false,
- ["attacking"] = true,
- }
- local fuelList = {
- "minecraft:coal",
- "minecraft:planks",
- "minecraft:log",
- "minecraft:stick",
- "minecraft:coal_block",
- }
- function contains(table, element)
- for _,v in ipairs(table) do
- if v == element then
- return true
- end
- end
- return false
- end
- function selectFromList(list)
- local selected = turtle.getItemDetail()
- if selected and contains(list, selected.name) then
- -- Already selected
- return true
- end
- for slot=16,1,-1 do
- local data = turtle.getItemDetail(slot)
- if data then
- if contains(list, data.name) then
- turtle.select(slot)
- return true
- end
- end
- end
- return false
- end
- function refuelIfLow(val, amt)
- val = val or 5
- amt = amt or 1
- if turtle.getFuelLevel() < val then
- if selectFromList(fuelList) then
- turtle.refuel(amt)
- end
- end
- end
- function select(slot)
- turtle.select(slot)
- end
- function togglePlace()
- flags["placing"] = not flags["placing"]
- end
- function togglePlaceDown()
- flags["placingDown"] = not flags["placingDown"]
- end
- function togglePlaceUp()
- flags["placingUp"] = not flags["placingUp"]
- end
- function togglePlaceLeft()
- flags["placingLeft"] = not flags["placingLeft"]
- end
- function togglePlaceRight()
- flags["placingRight"] = not flags["placingRight"]
- end
- function togglePlaceBack()
- flags["placingBack"] = not flags["placingBack"]
- end
- function toggleDig()
- flags["digging"] = not flags["digging"]
- end
- function toggleDigUp()
- flags["diggingUp"] = not flags["diggingUp"]
- end
- function toggleDigDown()
- flags["diggingDown"] = not flags["diggingDown"]
- end
- function toggleDigLeft()
- flags["diggingLeft"] = not flags["diggingLeft"]
- end
- function toggleDigRight()
- flags["diggingRight"] = not flags["diggingRight"]
- end
- function toggleAttack()
- flags["attacking"] = not flags["attacking"]
- end
- function turnRight(turns)
- turns = turns or 1
- for i=1,turns do
- turtle.turnRight()
- end
- end
- function turnLeft(turns)
- turns = turns or 1
- for i=1,turns do
- turtle.turnLeft()
- end
- end
- function move(func, blocks)
- blocks = blocks or 1
- local digFunc
- if func == "f" then
- func = turtle.forward
- digFunc = turtle.dig
- elseif func == "u" then
- func = turtle.up
- digFunc = turtle.digUp
- elseif func == "d" then
- func = turtle.down
- digFunc = turtle.digDown
- elseif func == "b" then
- func = turtle.back
- digFunc = turtle.dig
- end
- for block=1,blocks do
- if flags["placing"] and not turtle.detect() then
- turtle.place()
- end
- if flags["placingDown"] and not turtle.detectDown() then
- turtle.placeDown()
- end
- if flags["placingUp"] and not turtle.detectUp() then
- turtle.placeUp()
- end
- if flags["placingLeft"] then
- turnLeft()
- turtle.place()
- turnRight()
- end
- if flags["placingRight"] then
- turnRight()
- turtle.place()
- turnLeft()
- end
- if flags["placingBack"] then
- if math.random(2) == 1 then
- turnRight(2)
- else
- turnLeft(2)
- end
- turtle.place()
- if math.random(2) == 1 then
- turnRight(2)
- else
- turnLeft(2)
- end
- end
- local dug, digError
- if flags["digging"] then
- dug, digError = turtle.dig()
- if not dug and flags["attacking"] then
- repeat until not turtle.attack()
- end
- end
- if flags["diggingUp"] then
- turtle.digUp()
- end
- if flags["diggingDown"] then
- turtle.digDown()
- end
- if flags["diggingLeft"] then
- turnLeft()
- dug, digError = turtle.dig()
- if not dug and flags["attacking"] then
- repeat until not turtle.attack()
- end
- turnRight()
- end
- if flags["diggingRight"] then
- turnRight()
- dug, digError = turtle.dig()
- if not dug and flags["attacking"] then
- repeat until not turtle.attack()
- end
- turnLeft()
- end
- refuelIfLow()
- local moved = func()
- if not moved then
- repeat
- local dug,err
- if flags["digging"] then
- dug,err = digFunc()
- end
- if flags["attacking"] and not dug then
- if not turtle.attack() then
- print("I think I hit bedrock, skipping to next command...")
- return false
- end
- end
- until func()
- end
- end
- return true
- end
- function forward(blocks)
- return move("f", blocks)
- end
- function right(blocks)
- turnRight()
- local result = move("f", blocks)
- turnLeft()
- return result
- end
- function left(blocks)
- turnLeft()
- local result = move("f", blocks)
- turnRight()
- return result
- end
- function back(blocks)
- if flags["attacking"] or flags["digging"] then
- if math.random(2) == 1 then
- turnRight(2)
- else
- turnLeft(2)
- end
- end
- local result = move((flags["attacking"] or flags["digging"]) and "f" or "b", blocks)
- if flags["attacking"] or flags["digging"] then
- if math.random(2) == 1 then
- turnRight(2)
- else
- turnLeft(2)
- end
- end
- return result
- end
- function up(blocks)
- return move("u", blocks)
- end
- function down(blocks)
- return move("d", blocks)
- end
- function place()
- return turtle.place()
- end
- function placeDown()
- return turtle.placeDown()
- end
- function placeUp()
- return turtle.placeUp()
- end
- function placeBack()
- if math.random(2) == 1 then
- turnRight(2)
- else
- turnLeft(2)
- end
- local result = turtle.place()
- if math.random(2) == 1 then
- turnRight(2)
- else
- turnLeft(2)
- end
- return result
- end
- function placeLeft()
- turnLeft()
- local result = turtle.place()
- turnRight()
- return result
- end
- function placeRight()
- turnRight()
- local result = turtle.place()
- turnLeft()
- return result
- end
- function dig()
- return turtle.dig()
- end
- function digUp()
- return turtle.digUp()
- end
- function digDown()
- return turtle.digDown()
- end
- function digRight()
- turnRight()
- local result = turtle.dig()
- turnLeft()
- return result
- end
- function digLeft()
- turnLeft()
- local result = turtle.dig()
- turnRight()
- return result
- end
- function digBack()
- if math.random(2) == 1 then
- turnRight(2)
- else
- turnLeft(2)
- end
- local result = turtle.dig()
- if math.random(2) == 1 then
- turnRight(2)
- else
- turnLeft(2)
- end
- return result
- end
- local commands = {
- ["f"] = forward, ["fd"] = forward, ["forward"] = forward,
- ["r"] = right, ["right"] = right,
- ["l"] = left, ["left"] = left,
- ["b"] = back, ["back"] = back,
- ["u"] = up, ["up"] = up,
- ["d"] = down, ["down"] = down,
- ["tr"] = turnRight, ["rt"] = turnRight, ["turnright"] = turnRight,
- ["tl"] = turnLeft, ["lt"] = turnLeft, ["turnleft"] = turnLeft,
- ["s"] = select, ["sl"] = select, ["sel"] = select, ["select"] = select,
- ["p"] = place, ["place"] = place,
- ["tp"] = togglePlace, ["toggleplace"] = togglePlace,
- ["pd"] = placeDown, ["placedown"] = placeDown,
- ["tpd"] = togglePlaceDown, ["toggleplacedown"] = togglePlaceDown,
- ["pu"] = placeUp, ["placeup"] = placeUp,
- ["tpu"] = togglePlaceUp, ["toggleplaceup"] = togglePlaceUp,
- ["pl"] = placeLeft, ["placeleft"] = placeLeft,
- ["tpl"] = togglePlaceLeft, ["toggleplaceleft"] = togglePlaceLeft,
- ["pr"] = placeRight, ["placeright"] = placeRight,
- ["tpr"] = togglePlaceRight, ["toggleplaceright"] = togglePlaceRight,
- ["pb"] = placeBack, ["placeback"] = placeBack,
- ["tpb"] = togglePlaceBack, ["tpb"] = togglePlaceBack,
- ["dig"] = dig,
- ["td"] = toggleDig, ["tdig"] = toggleDig, ["toggledig"] = toggleDig,
- ["du"] = digUp, ["digup"] = digUp,
- ["tdu"] = toggleDigUp, ["tdigup"] = toggleDigUp, ["toggledigup"] = toggleDigUp,
- ["dd"] = digDown, ["digdown"] = digDown,
- ["tdd"] = toggleDigDown, ["tdigdown"] = toggleDigDown, ["toggledigdown"] = toggleDigDown,
- ["dr"] = digRight, ["digright"] = digRight,
- ["tdr"] = toggleDigRight, ["tdigright"] = toggleDigRight, ["toggledigright"] = toggleDigRight,
- ["dl"] = digLeft, ["digleft"] = digLeft,
- ["tdl"] = toggleDigLeft, ["tdigleft"] = toggleDigLeft, ["toggledigleft"] = toggleDigLeft,
- ["db"] = digBack, ["digback"] = digBack,
- ["ta"] = toggleAttack, ["toggleattack"] = toggleAttack,
- }
- function shift(t)
- return table.remove(t, 1)
- end
- local queue = {}
- function join(t, s)
- local result = ""
- for i,v in ipairs(t) do
- result = result .. v
- if i ~= #t then
- result = result .. s
- end
- end
- return result
- end
- function trim(s)
- return (s:gsub("^%s*(.-)%s*$", "%1"))
- end
- function capture(s)
- local p = "(%b()) (%d%d?)"
- local _,_,group,times = s:find(p)
- if not group then
- return false
- end
- local times = tonumber(times)
- local result = trim(
- s:gsub(p, (group:sub(2, -2) .. " "):rep(times))
- )
- return result
- end
- function parse(s)
- local result = s
- repeat
- result = capture(s) or result
- until not capture(result)
- return result
- end
- function validate(name)
- return name and (commands[name] or shell.resolveProgram(name))
- end
- local args = {...}
- local joinedArgs = join(args, " ")
- local unpackedArgs = {}
- for word in parse(joinedArgs):gmatch("%S+") do table.insert(unpackedArgs, word) end
- while #unpackedArgs ~= 0 do
- local name = shift(unpackedArgs)
- if not validate(name) then
- print(name .. " is not a valid program")
- return
- end
- local arg = tonumber(unpackedArgs[1])
- if arg ~= nil then
- shift(unpackedArgs)
- end
- table.insert(queue, { ["name"] = name, ["arg"] = arg })
- end
- for i,v in ipairs(queue) do
- local name, arg = v.name:lower(), v.arg
- local cmd = commands[name]
- if cmd then
- cmd(arg)
- elseif name then
- local resolved = shell.resolveProgram(name)
- if resolved then
- shell.run(resolved, arg)
- else
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement