Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local returning = false
- function tryRefuel()
- if getInvoItem("coal") then
- turtle.refuel(5)
- return true
- elseif getInvoItem("planks") then
- turtle.refuel(5)
- return true
- end
- return false
- end
- function checkFuel(flag)
- if not flag then
- local fuel = turtle.getFuelLevel()
- local distance = math.abs(x) + math.abs(y) + math.abs(z)
- if fuel-1 <= distance and not tryRefuel() then
- home()
- error("Alert: Fuel level critical. Returning to home point...")
- end
- end
- end
- function getFuel()
- faceMachine("craftChest")
- turtle.select(1)
- while turtle.suckUp() do
- if scanSlot(1, "coal") then
- turtle.transferTo(16)
- end
- turtle.drop()
- end
- while turtle.suck() do
- turtle.dropUp()
- end
- end
- -- 0 (-z)
- -- 3 (-x) 1 (+x)
- -- 2 (+z)
- function forward(num)
- num = num or 1
- for i = 1, num do
- if turtle.forward() then
- if f == 0 then
- z = z - 1
- update("z",z)
- elseif f == 1 then
- x = x + 1
- update("x",x)
- elseif f == 2 then
- z = z + 1
- update("z",z)
- elseif f == 3 then
- x = x - 1
- update("x",x)
- end
- checkFuel(returning)
- else
- return false
- end
- end
- return true
- end
- function back(num)
- num = num or 1
- for i = 1, num do
- if turtle.back() then
- if f == 0 then
- z = z + 1
- update("z",z)
- elseif f == 1 then
- x = x - 1
- update("x",x)
- elseif f == 2 then
- z = z - 1
- update("z",z)
- elseif f == 3 then
- x = x + 1
- update("x",x)
- end
- checkFuel(returning)
- else
- return false
- end
- end
- return true
- end
- function up(num)
- num = num or 1
- for i = 1, num do
- if turtle.up() then
- y = y + 1
- update("y",y)
- checkFuel(returning)
- else
- return false
- end
- end
- return true
- end
- function down(num)
- num = num or 1
- for i = 1, num do
- if turtle.down() then
- y = y - 1
- update("y",y)
- checkFuel(returning)
- else
- return false
- end
- end
- return true
- end
- function left(num)
- num = num or 1
- for i = 1, num do
- turtle.turnLeft()
- f = (f+3)%4
- update("f",f)
- end
- end
- function right(num)
- num = num or 1
- for i = 1, num do
- turtle.turnRight()
- f = (f+1)%4
- update("f",f)
- end
- end
- function home()
- returning = true
- if y == homePos[3] and x == homePos[1] and z == homePos[2] then
- returning = false
- return
- end
- while y ~= homePos[3]-2 do
- if y < homePos[3]-2 then
- turtle.digUp()
- up()
- elseif y > homePos[3]-2 then
- turtle.digDown()
- down()
- end
- end
- if z > homePos[2] then
- while f ~= 0 do
- left()
- end
- elseif z < homePos[2] then
- while f ~= 2 do
- left()
- end
- end
- while z ~= homePos[2] do
- forward()
- end
- if x > homePos[1] then
- while f ~= 3 do
- left()
- end
- elseif x < homePos[1] then
- while f ~= 1 do
- left()
- end
- end
- while x ~= homePos[1] do
- forward()
- end
- for i = 1, 2 do
- up()
- end
- returning = false
- end
- function travel(xPos, zPos, yPos)
- while y ~= yPos do
- if y < yPos then
- turtle.digUp()
- up()
- elseif y > yPos then
- turtle.digDown()
- down()
- end
- end
- if z > zPos then
- while f ~= 0 do
- left()
- end
- elseif z < zPos then
- while f ~= 2 do
- left()
- end
- end
- while z ~= zPos do
- turtle.dig()
- forward()
- end
- if x > xPos then
- while f ~= 3 do
- left()
- end
- elseif x < xPos then
- while f ~= 1 do
- left()
- end
- end
- while x ~= xPos do
- turtle.dig()
- forward()
- end
- end
- function update(var,value)
- if not fs.exists("saveData") then
- local h = fs.open("saveData", "w")
- h.close()
- end
- if type(value) == "number" then
- value = tostring(value)
- elseif type(value) == "table" then
- local v = "{"
- for i = 1, #value-1 do
- v = v .. tostring(value[i]) .. ","
- end
- v = v .. tostring(value[#value]) .. "}"
- value = v
- elseif type(value) == "string" then
- value = "\""..value.."\""
- end
- local str = fs.open("saveData","r").readAll()
- if str:find(var) then
- str = str:gsub(var.."%=.-%;",var.."="..value..";")
- else
- str = str .."\n"..var.."="..value..";"
- end
- local h = fs.open("saveData","w")
- h.write(str)
- h.close()
- end
- function initVars()
- if not fs.exists("saveData") then
- update("x", 0)
- update("z", 0)
- update("y", 0)
- update("f", 0)
- update("homePos", {0, 0, 0})
- update("currentTask", 0)
- update("version", 0)
- end
- shell.run("saveData")
- end
- function setPos(a, b, c, d)
- update("x", a)
- update("z", b)
- update("y", c)
- update("f", d)
- shell.run("saveData")
- end
- function setHome(homeX, homeZ, homeY)
- homePos = {homeX, homeZ, homeY}
- update("homePos", homePos)
- end
- function log(data)
- local h = fs.open("log", "w")
- if type(data) == "string" then
- h.writeLine(data)
- elseif type(data) == "table" then
- for i = 1, #data do
- h.writeLine(data[i])
- end
- end
- h.close()
- end
- function surface()
- while down() do
- end
- end
- function bottom()
- turtle.digDown()
- while down() do
- turtle.digDown()
- end
- end
- function scan(tStr)
- local s,d = turtle.inspect()
- if s then
- for i = 1, #tStr do
- if string.find(d.name, tStr[i]) then
- return true
- end
- end
- return false
- end
- return false
- end
- function scanUp(tStr)
- local s,d = turtle.inspectUp()
- if s then
- for i = 1, #tStr do
- if string.find(d.name, tStr[i]) then
- return true
- end
- end
- return false
- end
- return false
- end
- function scanDown(tStr)
- local s,d = turtle.inspectDown()
- if s then
- for i = 1, #tStr do
- if string.find(d.name, tStr[i]) then
- return true
- end
- end
- return false
- end
- return false
- end
- local vFlag = false
- function mineVein(tStr, lim, max)
- if not lim then
- lim = 128
- max = 128
- end
- if not max then
- max = lim
- end
- if lim == max then
- vFlag = false
- end
- lim = lim - 1
- if lim == 0 then
- vFlag = true
- return true
- end
- for i = 1, 4 do
- if scan(tStr) then
- turtle.dig()
- forward()
- mineVein(tStr, lim, max)
- if vFlag then return true end
- back()
- end
- left()
- end
- if scanUp(tStr) then
- turtle.digUp()
- up()
- mineVein(tStr, lim, max)
- if vFlag then return true end
- down()
- end
- if scanDown(tStr) then
- turtle.digDown()
- down()
- mineVein(tStr, lim, max)
- if vFlag then return true end
- up()
- end
- end
- function drillDown(num)
- for i = 1, num do
- turtle.digDown()
- if not down() then
- break
- end
- end
- end
- function checkFilled()
- for i = 1, 16 do
- if turtle.getItemSpace(i) == 64 then
- return true
- end
- end
- return false
- end
- function stripMine(length, strips, template)
- for i = 1, strips do
- for i = 1, length do
- turtle.dig()
- if not checkFilled()then
- home()
- clearInvo()
- clearTrash()
- end
- forward()
- vFlag = false
- mineVein(template)
- end
- left((i%2)*2+1)
- for i = 1, 3 do
- turtle.dig()
- if not checkFilled()then
- home()
- clearInvo()
- clearTrash()
- end
- forward()
- vFlag = false
- mineVein(template)
- end
- left((i%2)*2+1)
- end
- end
- function getInitialTree()
- turtle.dig()
- turtle.craft()
- turtle.refuel()
- forward()
- local n = 0
- while scanUp({"log"}) do
- turtle.digUp()
- up()
- end
- turtle.craft()
- turtle.refuel()
- for i = 2, n do
- down()
- end
- turtle.suckDown() ---grab fallen saplings
- down()
- end
- function coast(length, strips, template, num)
- for i = 1, strips do
- for i = 1, length do
- while not forward() do
- turtle.digUp()
- up()
- end
- surface()
- vFlag = false
- if mineVein(template, num) then
- break
- end
- end
- left((i%2)*2+1)
- for i = 1, 3 do
- while not forward() do
- turtle.digUp()
- up()
- end
- surface()
- --[[vFlag = false
- if mineVein(template, num) then
- break
- end]]--
- end
- left((i%2)*2+1)
- end
- end
- local rFlag = false
- function recursiveBreak(tStr)
- if not scanDown(tStr) then
- turtle.digDown()
- down()
- recursiveBreak(tStr)
- rFlag = true
- --up()
- end
- if rFlag then return end
- if not scan(tStr) then
- turtle.dig()
- forward()
- recursiveBreak(tStr)
- if rFlag then return end
- back()
- end
- left()
- if not scan(tStr) then
- turtle.dig()
- forward()
- recursiveBreak(tStr)
- if rFlag then return end
- back()
- end
- right(2)
- if not scan(tStr) then
- turtle.dig()
- forward()
- recursiveBreak(tStr)
- if rFlag then return end
- back()
- end
- left()
- end
- function setRelativeCoords()
- bottom()
- recursiveBreak({"bedrock"})
- local surface = y
- setPos(0, 0, 2, 0)
- setHome(0, 0, 20-surface)
- end
- ----------------------------------------CRAFTING----------------------------------------
- local craftSlots = {4, 8, 12, 16, 15, 14, 13}
- local craftTab = {
- ["plank"] = {{"log"}, {{1,0,0},{0,0,0},{0,0,0}}},
- ["stick"] = {{"plank"}, {{1,0,0},{1,0,0},{0,0,0}}},
- ["table"] = {{"plank"}, {{1,1,0},{1,1,0},{0,0,0}}},
- ["chest"] = {{"plank"}, {{1,1,1},{1,0,1},{1,1,1}}},
- ["furnace"] = {{"cobble"}, {{1,1,1},{1,0,1},{1,1,1}}},
- ["glass_pane"] = {{"glass"}, {{0,0,0},{1,1,1},{1,1,1}}},
- ["paper"] = {{"sugarcane"}, {{0,0,0},{0,0,0},{1,1,1}}},
- ["pickaxe"] = {{"stick", "diamond"}, {{2,2,2},{0,1,0},{0,1,0}}},
- ["computer"] = {{"stone", "redstone", "glass_pane"}, {{1,1,1},{1,2,1},{1,3,1}}},
- ["turtle"] = {{"iron_ingot", "CC%-Computer", "chest"}, {{1,1,1},{1,2,1},{1,3,1}}},
- ["mining_turtle"] = {{"CC%-Turtle", "diamond_pick", "crafting_table"}, {{0,0,0},{2,1,3},{0,0,0}}},
- ["disk_drive"] = {{"stone", "redstone"}, {{1,1,1},{1,2,1},{1,2,1}}},
- ["floppy_disk"] = {{"paper", "redstone"}, {{2,1,0},{0,0,0},{0,0,0}}},
- ["iron_ingot"] = {"coal", "iron_ore"},
- ["gold_ingot"] = {"coal", "gold_ore"},
- ["stone"] = {"coal", "cobble"},
- ["charcoal"] = {"coal", "log"},
- ["glass"] = {"coal", "sand"}
- }
- local machines = {
- ["craftChest"] = 0,
- ["furnace"] = 1
- }
- function getInvoItem(str)
- for i = 1, 16 do
- turtle.select(i)
- local d = turtle.getItemDetail()
- if d and string.find(d.name, str) then
- return true
- end
- end
- return false
- end
- function scanSlot(slot, tStr)
- turtle.select(slot)
- local d = turtle.getItemDetail()
- if type(tStr) == "table" then
- for i = 1, #tStr do
- if d and string.find(d.name, ":"..tStr[i]) then
- return true
- end
- end
- elseif type(tStr) == "string" then
- if d then print(tStr .. " " .. d.name) end
- if d and string.find(d.name, ":"..tStr) then
- return true
- end
- end
- return false
- end
- function clearInvo(tStr)
- if not tStr then
- for i = 1, 16 do
- turtle.select(i)
- turtle.dropUp()
- end
- else
- for n = 1, 7 do
- for i = 1, #tStr do
- if scanSlot(craftSlots[n], tStr[i]) then
- turtle.transferTo(craftSlots[i])
- end
- end
- turtle.dropUp()
- end
- for row = 1, 3 do
- for col = 1, 3 do
- for i = 1, #tStr do
- if scanSlot((row-1)*4+col, tStr[i]) then
- turtle.transferTo(craftSlots[i])
- end
- end
- turtle.dropUp()
- end
- end
- end
- end
- function buildFloor(slot)
- turtle.select(slot)
- back()
- left()
- back()
- for i = 1, 4 do
- for i = 1, 2 do
- forward()
- turtle.placeDown()
- end
- right()
- end
- forward()
- right()
- forward()
- end
- function prepCraft()
- surface()
- turtle.digDown()
- down()
- getInvoItem("dirt")
- turtle.placeUp()
- turtle.digDown()
- down()
- end
- function craftDirty(item, num)
- num = num or 64
- clearInvo(craftTab[item][1])
- for row = 1, 3 do
- for col = 1, 3 do
- if craftTab[item][2][row][col] > 0 then
- turtle.select(craftSlots[craftTab[item][2][row][col]])
- turtle.transferTo((row-1)*4+col, num)
- end
- end
- end
- for i = 1, #craftTab[item][1] do
- turtle.select(craftSlots[i])
- turtle.dropUp()
- end
- turtle.select(16)
- turtle.craft(num)
- up()
- while turtle.suckDown() do end
- down()
- end
- function getCraftingItems(tStr)
- faceMachine("craftChest")
- turtle.select(1)
- while turtle.suckUp() do
- for i = 1, #tStr do
- if scanSlot(1, tStr[i]) then
- turtle.transferTo(craftSlots[i])
- end
- end
- turtle.drop()
- end
- while turtle.suck() do
- turtle.dropUp()
- end
- end
- function craft(item, num)
- num = num or 64
- clearInvo()
- getCraftingItems(craftTab[item][1])
- for row = 1, 3 do
- for col = 1, 3 do
- if craftTab[item][2][row][col] > 0 then
- turtle.select(craftSlots[craftTab[item][2][row][col]])
- turtle.transferTo((row-1)*4+col, num)
- end
- end
- end
- for i = 1, #craftTab[item][1] do
- turtle.select(craftSlots[i])
- turtle.dropUp()
- end
- turtle.select(16)
- turtle.craft(num)
- end
- local essentials = {"log", "plank", "stick", "cobble", "stone", "redstone", "glass", "sand", "iron", "gold", "coal"}
- function clearTrash()
- clearInvo()
- local s = 1
- turtle.select(1)
- while turtle.suckUp() do
- local d = turtle.getItemDetail()
- if not scanSlot(s, essentials) then
- turtle.dropDown()
- else
- s = s + 1
- end
- end
- clearInvo()
- end
- function clearTrashDirty()
- for i = 1, 16 do
- turtle.select(i)
- if not scanSlot(i, essentials) then
- turtle.dropDown()
- end
- end
- end
- function smelt(item, num)
- clearInvo()
- getCraftingItems(craftTab[item])
- faceMachine("furnace")
- forward()
- up()
- forward()
- turtle.select(craftSlots[2])
- num = num or turtle.getItemCount()
- turtle.dropDown(num)
- back()
- down(2)
- forward()
- turtle.select(craftSlots[1])
- turtle.dropUp(math.ceil(num/8))
- sleep(num*10)
- turtle.select(16)
- turtle.suckUp()
- back()
- up()
- back()
- end
- function faceMachine(m)
- while f ~= machines[m] do
- left()
- end
- end
- ----------------------------------------FILE TRANSFER----------------------------------------
- function transferChildProgram()
- getInvoItem("CC%-Peripheral")
- up()
- turtle.place()
- getInvoItem("disk")
- turtle.drop()
- local h = fs.open("disk/startup", "w")
- local currentLabel = os.getComputerLabel()
- currentLabel = currentLabel:gsub("%-", "_")
- h.writeLine([[
- shell.run("pastebin get 7a4sZAXm run")
- local h = fs.open("startup", "w")
- h.writeLine("shell.run(\"run\")")
- h.close()
- os.setComputerLabel("]]..currentLabel.."-"..version..[[")
- ]])
- h.close()
- version = version + 1
- down()
- getInvoItem("CC%-Turtle")
- turtle.place()
- peripheral.wrap("front").turnOn()
- sleep(2)
- up()
- turtle.suck()
- turtle.dig()
- down()
- peripheral.wrap("front").reboot()
- back()
- end
- ----------------------------------------ACTUAL PROGRAM----------------------------------------
- function runTask(task)
- if task == 0 then --get initial wood
- getInitialTree()
- coast(5, 5, {"log"})
- currentTask = currentTask + 1
- update("currentTask", currentTask)
- elseif task == 1 then --craft chests for base
- prepCraft()
- craftDirty("plank")
- craftDirty("chest", 2)
- currentTask = currentTask + 1
- update("currentTask", currentTask)
- elseif task == 2 then --set relative coordinate system
- setRelativeCoords()
- currentTask = currentTask + 1
- update("currentTask", currentTask)
- elseif task == 3 then --set up base
- home()
- if getInvoItem("cobble") and turtle.getItemCount() > 8 then
- buildFloor(turtle.getSelectedSlot())
- end
- getInvoItem("chest")
- turtle.placeUp()
- faceMachine("craftChest")
- turtle.place()
- craft("furnace", 1)
- faceMachine("furnace")
- forward()
- turtle.place()
- turtle.digDown()
- back()
- clearTrash()
- currentTask = currentTask + 1
- update("currentTask", currentTask)
- elseif task == 4 then --mining (best range y = 6 - 13)
- getFuel()
- travel(0, 0, 10)
- stripMine(12, 4, {"iron", "gold", "redstone", "coal", "diamond", "sand"}) --30, 10
- home()
- clearTrash()
- currentTask = currentTask + 1
- update("currentTask", currentTask)
- elseif task == 5 then --find sand
- getFuel()
- down(2)
- forward()
- surface()
- coast(100, 1, {"sand"}, 64)
- home()
- clearTrash()
- currentTask = currentTask + 1
- update("currentTask", currentTask)
- elseif task == 6 then --craft turtle!!!
- smelt("iron_ingot", 7)
- smelt("stone", 7)
- smelt("glass", 6)
- craft("glass_pane", 1)
- craft("stick", 1)
- craft("table", 1)
- craft("chest", 1)
- craft("pickaxe", 1)
- craft("computer", 1)
- craft("turtle", 1)
- craft("mining_turtle", 1)
- currentTask = currentTask + 1
- update("currentTask", currentTask)
- elseif task == 7 then --find a new land for the new turtle!
- currentTask = currentTask + 1
- update("currentTask", currentTask)
- elseif task == 8 then --give the new turtle its software and start it up!
- transferChildProgram()
- error("Transfer Completed.")
- currentTask = 6
- update("currentTask", currentTask)
- end
- end
- initVars()
- home()
- while true do
- runTask(currentTask)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement