Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = {...}
- if not fs.exists("refill") then
- shell.run("pastebin get biP6fFEi refill")
- end
- shell.run("refill")
- function recordStep(n)
- local h = fs.open("nObjective",'w')
- h.writeLine("nObjective = "..tostring(n)..";")
- h.close()
- end
- function recordPos(heightPos,widthPos,lengthPos,face)
- local h = fs.open("position","w")
- h.writeLine("heightPos = "..tostring(heightPos)..";")
- h.writeLine("widthPos = "..tostring(widthPos)..";")
- h.writeLine("lengthPos = "..tostring(lengthPos)..";")
- h.writeLine("face = ".."\""..tostring(face).."\""..";")
- h.close()
- end
- function updateVar(file,variable,newValue)
- local exists
- local withinTable = false
- if not fs.exists(file) then
- error("file does not exist")
- end
- if variable:find("%.") then
- variable = variable:gsub("(.*%.)","")
- withinTable = true
- print(variable)
- end
- --if variable
- variable = tostring(variable)
- newValue = textutils.serialize(newValue)
- --print(variable)
- local string = fs.open(file,"r").readAll()
- local pattern,replace
- if not withinTable then
- pattern = variable.." =(.-;)"
- replace = variable.." = "..newValue..";"
- else
- pattern = variable.." =(.-,)"
- replace = variable.." = "..newValue..","
- print("pattern: ",pattern)
- print("replace: ",replace)
- end
- --print(string:find(pattern))
- exists = string:find(pattern)
- if exists then
- local h = fs.open(file,"w")
- string = string:gsub(pattern,replace)
- h.write(string)
- h.close()
- --else
- --local h = fs.open(file,"a")
- --h.writeLine(replace)
- --h.close()
- end
- end
- function reinitialize()
- heightPos = 0
- widthPos = 0
- lengthPos = 0
- face = "south"
- recordPos(heightPos,widthPos,lengthPos,face)
- end
- if tonumber(tArgs[1]) then
- heightPos = tonumber(tArgs[1])
- widthPos = tonumber(tArgs[2])
- lengthPos = tonumber(tArgs[3])
- face = tArgs[4]
- recordPos(heightPos,widthPos,lengthPos,face)
- end
- if tArgs[1] == "reset" then
- reinitialize()
- end
- if fs.exists("position") then
- shell.run("position")
- else
- reinitialize()
- end
- --Navigation function--
- local function matchHeight(heightGoal)
- if heightGoal > heightPos then
- while heightGoal > heightPos do
- up()
- end
- elseif heightGoal < heightPos then
- while heightGoal < heightPos do
- down()
- end
- end
- end
- local function matchWidth(widthGoal)
- if widthGoal < widthPos then
- turn("west")
- while widthGoal < widthPos do
- forward()
- end
- elseif widthGoal > widthPos then
- turn("east")
- while widthGoal > widthPos do
- forward()
- end
- end
- end
- local function matchLength(lengthGoal)
- if lengthGoal > lengthPos then
- turn("south")
- while lengthGoal > lengthPos do
- forward()
- end
- elseif lengthGoal < lengthPos then
- turn("north")
- while lengthGoal < lengthPos do
- forward()
- end
- end
- end
- function goto2(heightGoal,widthGoal,lengthGoal)
- --shell.run("position")
- if turtle.getFuelLevel() < 200 then
- assert(refill)
- refill()
- end
- assert(face)
- matchHeight(heightGoal)
- if face == "south" or face == "north" then
- if face == "south" and lengthGoal > lengthPos then
- matchLength(lengthGoal)
- matchWidth(widthGoal)
- elseif face == "north" and lengthGoal < lengthPos then
- matchLength(lengthGoal)
- matchWidth(widthGoal)
- else
- matchWidth(widthGoal)
- matchLength(lengthGoal)
- end
- else
- if face == "west" and widthGoal < widthPos then
- matchWidth(widthGoal)
- matchLength(lengthGoal)
- elseif face == "east" and widthGoal > widthPos then
- matchWidth(widthGoal)
- matchLength(lengthGoal)
- else
- matchLength(lengthGoal)
- matchWidth(widthGoal)
- end
- end
- end
- function goto(heightGoal,widthGoal,lengthGoal)
- --shell.run("position")
- assert(face)
- matchHeight(heightGoal)
- if face == "south" then
- if lengthGoal > lengthPos then
- while lengthGoal > lengthPos do
- forward()
- end
- elseif lengthGoal < lengthPos then
- while lengthGoal < lengthPos do
- backward()
- end
- end
- matchWidth(widthGoal)
- elseif face == "north" then
- if lengthGoal < lengthPos then
- while lengthGoal < lengthPos do
- forward()
- end
- elseif lengthGoal > lengthPos then
- while lengthGoal > lengthPos do
- backward()
- end
- end
- matchWidth(widthGoal)
- elseif face == "west" then
- if widthGoal < widthPos then
- while widthGoal < widthPos do
- forward()
- end
- elseif widthGoal > widthPos then
- while widthGoal > widthPos do
- backward()
- end
- end
- matchLength(lengthGoal)
- elseif face == "east" then
- if widthGoal > widthPos then
- while widthGoal > widthPos do
- forward()
- end
- elseif widthGoal < widthPos then
- while widthGoal < widthPos do
- backward()
- end
- end
- matchLength(lengthGoal)
- end
- end
- function update(dir)
- if dir == "forward" then
- if face == "north" then
- lengthPos = lengthPos - 1
- elseif face == "south" then
- lengthPos = lengthPos + 1
- elseif face == "east" then
- widthPos = widthPos + 1
- elseif face == "west" then
- widthPos = widthPos - 1
- end
- elseif dir == "backward" then
- if face == "north" then
- lengthPos = lengthPos + 1
- elseif face == "south" then
- lengthPos = lengthPos - 1
- elseif face == "east" then
- widthPos = widthPos - 1
- elseif face == "west" then
- widthPos = widthPos + 1
- end
- elseif dir == "up" then
- heightPos = heightPos + 1
- elseif dir == "down" then
- heightPos = heightPos - 1
- elseif dir == "right" then
- if face == "north" then
- face = "east"
- elseif face == "east" then
- face = "south"
- elseif face == "south" then
- face = "west"
- elseif face == "west" then
- face = "north"
- end
- elseif dir == "left" then
- if face == "north" then
- face = "west"
- elseif face == "west" then
- face = "south"
- elseif face == "south" then
- face = "east"
- elseif face == "east" then
- face = "north"
- end
- end
- recordPos(heightPos,widthPos,lengthPos,face)
- end
- --Movement functions--
- function courtesyOLD()
- --if not turtle then
- if turtle.up() then
- sleep(3)
- while not turtle.down() do
- sleep(1)
- end
- return
- elseif turtle.down() then
- sleep(3)
- while not turtle.up() do
- sleep(1)
- end
- return
- elseif turtle.forward() then
- sleep(3)
- while not turtle.back() do
- sleep(1)
- end
- return
- elseif turtle.back() then
- sleep(3)
- while not turtle.forward() do
- sleep(1)
- end
- return
- end
- end
- function courtesy()
- local choice = math.random(1,6)
- if choice == 1 then
- if turtle.up() then
- update("up")
- sleep(3)
- while not turtle.down() do
- sleep(1)
- end
- update("down")
- end
- return
- elseif choice == 2 then
- if turtle.down() then
- update("down")
- sleep(3)
- while not turtle.up() do
- sleep(1)
- end
- update("up")
- end
- return
- elseif choice == 3 then
- if turtle.forward() then
- update("forward")
- sleep(3)
- while not turtle.back() do
- sleep(1)
- end
- update("backward")
- end
- return
- elseif choice == 4 then
- if turtle.back() then
- update("backward")
- sleep(3)
- while not turtle.forward() do
- sleep(1)
- end
- update("forward")
- end
- return
- elseif choice == 5 then
- right()
- if turtle.forward() then
- update("forward")
- sleep(3)
- while not turtle.back() do
- sleep(1)
- end
- update("backward")
- end
- left()
- return
- elseif choice == 6 then
- left()
- if turtle.forward() then
- update("forward")
- sleep(3)
- while not turtle.back() do
- sleep(1)
- end
- update("backward")
- end
- right()
- return
- end
- end
- function forward()
- update("forward")
- while not turtle.forward() do
- if turtle.getFuelLevel() == 0 then
- error("out of fuel")
- end
- local a,check = turtle.inspect()
- if a then
- if not check.name:lower():find("turtle") then
- turtle.dig()
- else
- courtesy()
- end
- end
- end
- end
- function backward()
- update("backward")
- while not turtle.back() do
- if turtle.getFuelLevel() == 0 then
- error("out of fuel")
- end
- right()
- right()
- local a,check = turtle.inspect()
- if a then
- if not check.name:lower():find("turtle") then
- turtle.dig()
- else
- courtesy()
- end
- end
- right()
- right()
- end
- end
- function up()
- update("up")
- while not turtle.up() do
- if turtle.getFuelLevel() == 0 then
- error("out of fuel")
- end
- local a,check = turtle.inspectUp()
- if a then
- if not check.name:lower():find("turtle") then
- turtle.digUp()
- else
- courtesy()
- end
- end
- end
- end
- function down()
- update("down")
- while not turtle.down() do
- if turtle.getFuelLevel() == 0 then
- error("out of fuel")
- end
- local a,check = turtle.inspectDown()
- if a then
- if not check.name:lower():find("turtle") then
- turtle.digDown()
- else
- courtesy()
- end
- end
- end
- end
- function right()
- update("right")
- turtle.turnRight()
- end
- function left()
- update("left")
- turtle.turnLeft()
- end
- function turn(dir)
- if face == "north" then
- if dir == "east" then
- while face ~= dir do
- right()
- end
- else
- while face ~= dir do
- left()
- end
- end
- elseif face == "east" then
- if dir == "south" then
- while face ~= dir do
- right()
- end
- else
- while face ~= dir do
- left()
- end
- end
- elseif face == "south" then
- if dir == "west" then
- while face ~= dir do
- right()
- end
- else
- while face ~= dir do
- left()
- end
- end
- elseif face == "west" then
- if dir == "north" then
- while face ~= dir do
- right()
- end
- else
- while face ~= dir do
- left()
- end
- end
- end
- end
- function place()
- while not turtle.placeDown() do
- turtle.digDown()
- end
- return true
- end
- function saveInventory()
- end
- function updateInventory()
- --updates inventory table with whats in the turtle inventory
- local inventory = {}
- for i = 1,16 do
- inventory[i] = turtle.getItemDetail(i)
- end
- end
- function find(id,data,slots)
- --input numid or strname, numdata, and tabslots
- --output slotNumber
- --slots[id][data] = {name,damage,orientation}
- local exceptions = {"stairs","slabs","door","ladder","torch","lever","button","rail","chest","furnace"}
- local name, damage, orientation
- if type(id) == "number" then
- name = slots[id][data][1]
- damage = slots[id][data][2]
- orientation = data
- --convert to string ie minecraft:stone
- elseif type(id) == "string" then
- name = id
- damage = data
- orientation = data
- end
- --assert(name)
- if not name then
- --skips if not assigned in slots
- return "skip"
- end
- for i=1,16 do
- local item = turtle.getItemDetail(i)
- if item and item.name == name then
- if item.damage == damage then
- return i
- end
- end
- end
- if term.isColor() then
- term.setTextColor(colors.red)
- end
- print("not enough "..name..", "..damage)
- term.setTextColor(colors.white)
- return nil
- end
- function findWrench()
- for i =1,16 do
- local deets = turtle.getItemDetail(i)
- if deets and (deets.name:lower():find("wrench") or deets.name:lower():find("hammer")) then
- return i
- end
- end
- error("can't find the wrench/hammer")
- end
- function includeOffset(block,data,relativeDirection)
- local function direction2offset(relativeDirection)
- if relativeDirection == "south" then
- return 0
- elseif relativeDirection == "west" then
- return 1
- elseif relativeDirection == "north" then
- return 2
- elseif relativeDirection == "east" then
- return 3
- end
- end
- local function implementData(data,relativeDirection)
- turn_offset = direction2offset(relativeDirection)
- local chestDirs = {[0]=2,5,3,4}
- for i,v in pairs(chestDirs) do
- if data == chestDirs[i] then
- data = chestDirs[(i+turn_offset)%4]
- return data
- end
- end
- end
- local function implementDataStairs(data,relativeDirection)
- turn_offset = direction2offset(relativeDirection)
- local chestDirs = {[0]=3,0,2,1}
- local reversed = false
- if data > 3 then
- data = data - 4
- reversed = true
- end
- for i,v in pairs(chestDirs) do
- if data == chestDirs[i] then
- data = chestDirs[(i+turn_offset)%4]
- if reversed then
- data = data + 4
- end
- return data
- end
- end
- end
- local function implementDataStairsOld(data,relativeDirection)
- turn_offset = direction2offset(relativeDirection)
- for i = 0,3 do
- if data == i then
- data = (data + turn_offset)%4
- return data
- end
- end
- for i = 4,7 do
- if data == i then
- data = ((data-4) + turn_offset)%4 + 4
- return data
- end
- end
- end
- if block.name:lower():find("stairs") then
- data = implementDataStairs(data,relativeDirection)
- elseif block.name:lower():find("chest") then
- data = implementData(data,relativeDirection)
- end
- return data
- end
- function findAndPlace(id,data,slots,wrench)
- --wrench turning included
- --check for wrench 14
- --check if slabs or stairs
- local nSlot = find(id,data,slots)
- if nSlot == "skip" then
- return
- end
- if not nSlot then
- assert(nObjective)
- assert(instructions)
- assert(slots)
- checklist = checklistMaker(nObjective,instructions,slots)
- checklist, rubbishList = subtractInventory(checklist)
- check_chests(checklist,rubbishList)
- nSlot = find(id,data,slots)
- if not nSlot then
- error("please refill materials in refill chest")
- end
- local x,y,z = unpack(instructions[nObjective],1,3)
- if reference.multiturtle then
- goto(reference.finalx+1,y,z)
- end
- goto(x+1,y,z)
- end
- turtle.select(nSlot)
- local block = turtle.getItemDetail()
- if block then
- --lookup stairs or slabs
- if (block.name:lower():find("stairs") or block.name:lower():find("slab") or block.name:lower():find("chest") or block.name:lower():find("furnace")) and wrench and id~=324 and id~=96 then
- --place until metadata matches
- place()
- data = includeOffset(block,data,reference.relativeDirection)
- local tries = 0
- while true do
- local stair,orient = turtle.inspectDown()
- if stair then
- if orient.metadata == data then
- break
- end
- end
- turtle.select(findWrench())
- turtle.placeDown()
- tries = tries + 1
- if tries > 9 then
- break
- end
- end
- else
- place()
- end
- else
- return false
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement