Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local func = {}
- minFuel=1000
- func.ores = {
- "minecraft:iron_ore",
- "minecraft:coal_ore",
- "minecraft:diamond_ore",
- "minecraft:gold_ore",
- "minecraft:lapis_ore",
- "minecraft:redstone_ore",
- "minecraft:quartz_ore"
- }
- world={}
- function func.inventoryOver()
- local totalSpaceOver=0
- for i=1,16 do
- totalSpaceOver=totalSpaceOver+turtle.getItemSpace(i)
- end
- return totalSpaceOver
- end
- function func.refreshLoc()
- turtle.select(2)
- turtle.drop()
- turtle.select(1)
- shell.run("unequip left")
- shell.run("equip 1 left")
- xC,yC,zC=gps.locate()
- shell.run("unequip left")
- shell.run("equip 2 left")
- end
- xC,yC,zC=gps.locate()
- func.refreshLoc()
- rotC=0
- --rot 0 = x+
- --rot 1 = z+
- --rot 2 = x-
- --rot 3 = z-
- func.moves=0
- function func.dropTrashStone()
- end
- function func.tableContainsString(tbl, str)
- for key, value in pairs(tbl) do
- if value == str then
- return true
- end
- end
- --print(textutils.serialise(tbl))
- --func.saveTableToFile(tbl,"world")
- return false
- end
- function func.containsString(table1, table2)
- for key1, str1 in pairs(table1) do
- for key2, str2 in pairs(table2) do
- if tostring(str1) == tostring(str2) then
- return true
- end
- end
- end
- return false
- end
- function func.getFirstKey(tbl)
- -- Iterate over the table using next() and return the first key encountered
- for key, _ in pairs(tbl) do
- return key
- end
- -- If the table is empty, return nil
- return nil
- end
- function func.getKeyByValue(tbl, searchValue)
- for key, value in pairs(tbl) do
- if value == searchValue then
- return key
- end
- end
- return nil -- Return nil if the value is not found in the table
- end
- function func.getFirstKeyWithValueInTable(tbl, valuesTable)
- for key, value in pairs(tbl) do
- for _, v in ipairs(valuesTable) do
- if value == v then
- return key
- end
- end
- end
- return nil
- end
- function func.stringToTable(str)
- local tbl = {}
- local i = 1
- for value in str:gmatch("([^,]+)") do
- if i == 1 then
- tbl.x = tonumber(value)
- elseif i == 2 then
- tbl.y = tonumber(value)
- elseif i == 3 then
- tbl.z = tonumber(value)
- else
- break -- Only process first three values
- end
- i = i + 1
- end
- tbl.rot=0
- return tbl
- end
- function func.getBlockName()
- local frontBlock, frontData = turtle.inspect()
- if frontBlock==true then
- return frontData.name
- end
- return "minecraft:air"
- end
- function func.getBlockNameDown()
- local frontBlock, frontData = turtle.inspectDown()
- if frontBlock==true then
- return frontData.name
- end
- return "minecraft:air"
- end
- function func.getBlockNameUp()
- local frontBlock, frontData = turtle.inspectUp()
- if frontBlock==true then
- return frontData.name
- end
- return "minecraft:air"
- end
- function func.scanWorld()
- oldRot=rotC
- while 0~=rotC do
- func.rotate(1)
- end
- world[(xC+1)..","..yC..","..zC]=tostring(func.getBlockName())
- world[xC..","..(yC+1)..","..zC]=tostring(func.getBlockNameUp())
- world[xC..","..(yC-1)..","..zC]=tostring(func.getBlockNameDown())
- func.rotate(1)
- world[xC..","..yC..","..(zC+1)]=tostring(func.getBlockName())
- func.rotate(1)
- world[(xC-1)..","..yC..","..zC]=tostring(func.getBlockName())
- func.rotate(1)
- world[xC..","..yC..","..(zC-1)]=tostring(func.getBlockName())
- while oldRot~=rotC do
- func.rotate(1)
- end
- end
- function func.mineVeinOre()
- oldLoc=func.getLoc()
- --print("Starting veinmine "..targetName)
- func.scanWorld()
- --print(textutils.serialise(world))
- while func.containsString(world,func.ores)==true do
- local newVeinCoord=func.stringToTable(func.getFirstKeyWithValueInTable(world,func.ores))
- print("Trying to veinmine "..world[func.getFirstKeyWithValueInTable(world,func.ores)])
- func.goto(newVeinCoord)
- func.scanWorld()
- end
- --print("Stopped veinmining "..targetName)
- func.goto(oldLoc)
- end
- function func.mineVein(targetName)
- oldLoc=func.getLoc()
- func.scanWorld()
- while func.tableContainsString(world,targetName)==true do
- local newVeinCoord=func.stringToTable(func.getKeyByValue(world,targetName))
- func.goto(newVeinCoord)
- func.scanWorld()
- end
- print("Trying to veinmine "..targetName)
- func.goto(oldLoc)
- end
- function func.goto(goal)
- print(textutils.serialise(func.getLoc()))
- --Get the correct X
- if goal.x>xC then
- while rotC ~=0 do
- func.rotate(1)
- end
- while goal.x~=xC do
- func.forceMove(0)
- end
- end
- if goal.x<xC then
- while rotC ~=2 do
- func.rotate(1)
- end
- while goal.x~=xC do
- func.forceMove(0)
- end
- end
- --Get the correct Z
- if goal.z>zC then
- while rotC ~=1 do
- func.rotate(1)
- end
- while goal.z~=zC do
- func.forceMove(0)
- end
- end
- if goal.z<zC then
- while rotC ~=3 do
- func.rotate(1)
- end
- while goal.z~=zC do
- func.forceMove(0)
- end
- end
- --Get the correct Y
- if goal.y>yC then
- while goal.y~=yC do
- func.forceMove(2)
- end
- end
- if goal.y<yC then
- while goal.y~=yC do
- func.forceMove(3)
- end
- end
- --Get the correct Rot
- while goal.rot~=rotC do
- func.rotate(1)
- end
- end
- function func.getLoc()
- local a={}
- a.x=xC
- a.y=yC
- a.z=zC
- a.rot=rotC
- return a
- end
- function func.testPrint()
- print("test")
- end
- function func.autoFuel()
- if turtle.getFuelLevel()<minFuel then
- func.refuel()
- end
- end
- function func.refuel()
- for i=1,16 do
- turtle.select(i)
- turtle.refuel()
- end
- print("Amount of fuel over: "..turtle.getFuelLevel().."/"..turtle.getFuelLimit())
- end
- --dir 0=forward
- --dir 1=back
- --dir 2=up
- --dir 3=down
- function func.move(direction)
- if math.fmod(func.moves,10) == 0 then
- func.refreshLoc()
- end
- --print(textutils.serialise(func.getLoc()))
- func.moves=func.moves+1
- if turtle.getFuelLevel()>0 then
- if direction==0 then
- if turtle.forward()==true then
- if rotC ==0 then
- xC=xC+1
- end
- if rotC ==1 then
- zC=zC+1
- end
- if rotC ==2 then
- xC=xC-1
- end
- if rotC ==3 then
- zC=zC-1
- end
- end
- elseif direction==1 then
- if turtle.back()==true then
- if rotC ==0 then
- xC=xC-1
- end
- if rotC ==1 then
- zC=zC-1
- end
- if rotC ==2 then
- xC=xC+1
- end
- if rotC ==3 then
- zC=zC+1
- end
- end
- elseif direction==2 then
- if turtle.up()==true then
- yC=yC+1
- end
- elseif direction==3 then
- if turtle.down()==true then
- yC=yC-1
- end
- end
- end
- --print("x: "..xC)
- -- print("y: "..yC)
- --print("z: "..zC)
- --print("Rotation"..rotC)
- end
- --dir 0=left
- --dir 1=right
- function func.rotate(direction)
- if direction==0 then
- turtle.turnLeft()
- roC=rotC-1
- elseif direction==1 then
- turtle.turnRight()
- rotC=rotC+1
- end
- if rotC<0 then
- rotC=3
- end
- if rotC>3 then
- rotC=0
- end
- --print("Rotation"..rotC)
- end
- --Can't force back!
- function func.forceMove(direction)
- if direction==0 then
- turtle.dig()
- func.move(direction)
- world[xC..","..yC..","..zC] = nil
- elseif direction==2 then
- turtle.digUp()
- func.move(direction)
- world[xC..","..yC..","..zC] = nil
- elseif direction==3 then
- turtle.digDown()
- func.move(direction)
- world[xC..","..yC..","..zC] = nil
- end
- end
- function func.dropAll()
- for i=1,16 do
- turtle.select(i)
- turtle.drop()
- end
- end
- function func.getSlotName(slot)
- return (turtle.getItemDetail(slot).name)
- end
- function func.saveTableToFile(tbl, filename)
- local file = io.open(filename, "w")
- if file then
- file:write(func.tableToString(tbl))
- file:close()
- print("Table saved to " .. filename)
- else
- print("Error: Unable to open file " .. filename .. " for writing.")
- end
- end
- -- Helper function to convert table to string
- function func.tableToString(tbl)
- local str = "{\n"
- for k, v in pairs(tbl) do
- if type(k) == "number" then
- str = str .. "[" .. k .. "]"
- else
- str = str .. '["' .. k .. '"]'
- end
- str = str .. " = "
- if type(v) == "table" then
- str = str .. tableToString(v)
- elseif type(v) == "string" then
- str = str .. '"' .. v .. '"'
- else
- str = str .. tostring(v)
- end
- str = str .. ",\n"
- end
- str = str .. "}"
- return str
- end
- return func
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement