Computercraft/src/main/programs/turtle-miner-fibo.lua
2024-06-02 02:42:31 +02:00

269 lines
6.3 KiB
Lua

basic = require("lib.basic")
oreList = {
[0] = "minecraft:gold_ore",
[1] = "minecraft:deepslate_gold_ore",
[2] = "minecraft:iron_ore",
[3] = "minecraft:deepslate_iron_ore",
[4] = "minecraft:coal_ore",
[5] = "minecraft:deepslate_coal_ore",
[6] = "minecraft:lapisOre",
[7] = "minecraft:deepslate_lapisOre",
[8] = "minecraft:diamond_ore",
[9] = "minecraft:deepslate_diamond_ore",
[10] = "minecraft:redstone_ore",
[11] = "minecraft:deepslate_redstone_ore",
[12] = "minecraft:emerald_ore",
[13] = "minecraft:deepslate_emerald_ore",
[14] = "minecraft:quartz_ore",
[15] = "minecraft:deepslate_quartz_ore",
[16] = "minecraft:copper_ore",
[17] = "minecraft:deepslate_copper_ore",
[18] = "minecraft:ancient_debris",
}
throwAwayList = {
[0] = "minecraft:cobblestone",
[1] = "minecraft:dirt",
[2] = "minecraft:gravel",
[3] = "minecraft:andesite",
[4] = "minecraft:cobbled_deepslate",
[5] = "minecraft:diorite",
[6] = "minecraft:tuff",
[7] = "minecraft:netherrack",
}
local function setState()
basic.writeToFile("main/files/state-turtle-miner-fibo.txt", textutils.serialize(state))
end
local function getState()
if not fs.exists "main/files/state-turtle-miner-fibo.txt" then
turtleState = {}
turtleState["script"] = "main/programs/turtle-miner-fibo.lua"
turtleState["info"] = ""
turtleState["fuelLevel"] = turtle.getFuelLevel()
turtleState["dataCurrentFibo"] = 2
turtleState["dataGoalRows"] = 0
turtleState["dataGoalPathProgress"] = 0
turtleState["dataCurrentRow"] = 0
turtleState["dataCurrentPathProgress"] = 0
basic.writeToFile("main/files/state-turtle-miner-fibo.txt", textutils.serialize(turtleState))
else
turtleState = basic.readFileAll("main/files/state-turtle-miner-fibo.txt")
turtleState = textutils.unserialize(turtleState)
end
return turtleState
end
local function clearInventory()
for i=1, 16 do
turtle.select(i)
itemDetail = turtle.getItemDetail()
for j=0, #throwAwayList do
itemDetail = turtle.getItemDetail()
if itemDetail ~= nil then
if itemDetail.name == throwAwayList[j] then
turtle.dropDown()
end
end
end
end
turtle.select(1)
end
local function storeItems()
--refuel all
shell.run("refuel all")
--check inv for chest and place it
chestAvailable = false
for i=1, 16 do
turtle.select(i)
itemDetail = turtle.getItemDetail()
if itemDetail ~= nil then
if itemDetail.name == "minecraft:chest" then
turtle.up()
turtle.digUp()
turtle.up()
turtle.digUp()
turtle.placeUp()
--TODO save in list, where chest is placed
chestAvailable = true
break
elseif i == 16 then
--TODO save in state that no chests are available
end
end
end
--drop items in chest
if chestAvailable then
for i=1, 16 do
turtle.select(i)
itemDetail = turtle.getItemDetail()
if itemDetail ~= nil then
if itemDetail.name ~= "minecraft:torch" and itemDetail.name ~= "minecraft:chest" then
turtle.dropUp()
turtle.down()
turtle.down()
end
end
end
end
end
local function checkInventory()
for i=1, 16 do
turtle.select(i)
itemCount = turtle.getItemCount()
if itemCount == 0 then
turtle.select(1)
return
end
end
storeItems()
turtle.select(1)
end
local function isOre(blockData)
for i=0, #oreList do
if blockData["name"] == oreList[i] then
return true
end
end
return false
end
local function recursiveOreMining()
for i=0, 3 do
turtle.turnLeft()
flockFound, blockData = turtle.inspect()
if isOre(blockData) then
turtle.dig()
turtle.forward()
recursiveOreMining()
turtle.back()
end
end
flockFound, blockData = turtle.inspectUp()
if isOre(blockData) then
turtle.digUp()
turtle.up()
recursiveOreMining()
turtle.down()
end
flockFound, blockData = turtle.inspectDown()
if isOre(blockData) then
turtle.digDown()
turtle.down()
recursiveOreMining()
turtle.up()
end
end
local function mineRow()
--mine a 3 Block high Tunnel for <distance> Blocks
for i=state["dataCurrentPathProgress"], state["dataGoalPathProgress"]-2 do
print("row: " .. state["dataCurrentRow"]+1 .. "/" .. state["dataGoalRows"] .. ", pathProgress: " .. state["dataCurrentPathProgress"]+1 .. "/" .. state["dataGoalPathProgress"])
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.digDown()
--check current mining step for ore
turtle.up()
recursiveOreMining()
turtle.down()
recursiveOreMining()
turtle.down()
recursiveOreMining()
turtle.up()
checkInventory()
clearInventory()
--save path progress
state["dataCurrentPathProgress"] = i+1
state["fuelLevel"] = turtle.getFuelLevel()
setState(state)
end
print("row: " .. state["dataCurrentRow"]+1 .. "/" .. state["dataGoalRows"] .. ", pathProgress: " .. state["dataCurrentPathProgress"]+1 .. "/" .. state["dataGoalPathProgress"])
end
local function mineChunk()
state = getState()
state["dataGoalRows"] = state["dataCurrentFibo"]
state["dataGoalPathProgress"] = 3*(state["dataCurrentFibo"]-1)+1
setState(state)
for i=state["dataCurrentRow"], state["dataGoalRows"]-1 do
mineRow(state["dataGoalPathProgress"])
--turn back and return to start of row if not last row
if i<state["dataGoalRows"]-1 then
turtle.turnLeft()
turtle.turnLeft()
--while not turtle.detect() do
-- turtle.forward()
--end
for j=1, state["dataGoalPathProgress"] do
turtle.forward()
end
end
--go to next row
turtle.turnLeft()
for j=0, 2 do
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.digDown()
end
turtle.turnLeft()
state["dataCurrentRow"] = i+1
state["dataCurrentPathProgress"] = 0
end
--reset row progress
state["dataCurrentRow"] = 0
end
local function mineFibo()
state = getState()
while true do
mineChunk()
--go to next chunk
turtle.turnRight()
for j=1, 2 do
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.digDown()
end
--get next fibo number
state["dataCurrentFibo"] = math.floor((state["dataCurrentFibo"] * (1+math.sqrt(5))/2) + 0.5)
setState(state)
end
end
mineFibo()