Files
LeonOS/data/computercraft/lua/rom/programs/turtle_miner.lua

310 lines
8.8 KiB
Lua
Raw Normal View History

-- Turtle Auto Miner Program
-- Part of LeonOS for CC Tweaked
local term = require("term")
local colors = require("colors")
local textutils = require("textutils")
local os = require("os")
-- 保存当前颜色设置
local old_fg = term.getTextColor()
local old_bg = term.getBackgroundColor()
-- 设置名称栏颜色并显示
term.setTextColor(colors.white)
term.setBackgroundColor(colors.cyan)
term.at(1, 1).clearLine()
term.at(1, 1).write("=== Turtle Auto Miner ===")
-- 恢复颜色设置
term.setTextColor(old_fg)
term.setBackgroundColor(old_bg)
term.at(1, 2)
-- 检查是否有turtle
local turtle = require("turtle")
if not turtle then
error("No turtle detected. Please run this program on a turtle.", 0)
end
-- 配置常量
local FUEL_THRESHOLD = 500 -- 燃料不足阈值
local INVENTORY_FULL_THRESHOLD = 15 -- 背包满时剩余空格数
local COAL_NAMES = {"minecraft:coal", "minecraft:charcoal"} -- 煤炭物品名称
-- 检查燃料是否充足
local function checkFuel()
local currentFuel = turtle.getFuelLevel()
local fuelLimit = turtle.getFuelLimit()
-- 无限燃料模式或燃料充足
if fuelLimit == "unlimited" or currentFuel >= FUEL_THRESHOLD then
return true
end
return false
end
-- 寻找并使用燃料
local function refuel()
print(colors.yellow .. "Fuel low! Searching for fuel..." .. colors.white)
local initial_slot = turtle.getSelectedSlot()
local found_fuel = false
-- 检查背包中是否有燃料
for slot = 1, 16 do
if turtle.getItemCount(slot) > 0 then
turtle.select(slot)
local itemDetail = turtle.getItemDetail()
if itemDetail and itemDetail.fuel then
found_fuel = true
print("Found fuel in slot " .. slot .. ": " .. itemDetail.name)
print("Refueling...")
turtle.refuel(turtle.getItemCount(slot))
print(colors.green .. "Fuel level: " .. turtle.getFuelLevel() .. colors.white)
break
end
end
end
-- 恢复原来选中的槽位
turtle.select(initial_slot)
-- 如果没有找到燃料,尝试向下挖寻找煤矿
if not found_fuel then
print(colors.red .. "No fuel in inventory. Searching for coal ore..." .. colors.white)
local old_slot = turtle.getSelectedSlot()
-- 先检查脚下是否有方块
if not turtle.detectDown() then
print("Nothing below. Moving down...")
if not turtle.down() then
print(colors.red .. "Cannot move down." .. colors.white)
turtle.select(old_slot)
return false
end
end
-- 挖掘脚下可能的煤矿
for i = 1, 3 do -- 尝试挖3格
turtle.select(old_slot) -- 确保使用正确的工具
if turtle.detectDown() then
turtle.digDown()
-- 检查是否挖到了煤炭
for slot = 1, 16 do
if turtle.getItemCount(slot) > 0 then
turtle.select(slot)
local itemDetail = turtle.getItemDetail()
if itemDetail then
for _, coal_name in ipairs(COAL_NAMES) do
if string.find(itemDetail.name, coal_name) then
print(colors.green .. "Found coal!" .. colors.white)
turtle.refuel(turtle.getItemCount(slot))
turtle.select(old_slot)
return true
end
end
end
end
end
end
if not turtle.down() then
break
end
end
-- 回到原来的位置
for i = 1, 3 do
if not turtle.up() then
break
end
end
turtle.select(old_slot)
end
return found_fuel or checkFuel()
end
-- 检查背包是否已满
local function isInventoryFull()
local empty_slots = 0
for i = 1, 16 do
if turtle.getItemCount(i) == 0 then
empty_slots = empty_slots + 1
end
end
return empty_slots <= INVENTORY_FULL_THRESHOLD
end
-- 寻找附近的箱子并存放物品
local function findChestAndDeposit()
print(colors.yellow .. "Inventory is full. Looking for chest..." .. colors.white)
-- 尝试在周围寻找箱子
for direction = 1, 4 do -- 四个方向
local found_chest = false
-- 检查前方是否有箱子
local success, data = turtle.inspect()
if success then
if data.name and (string.find(data.name, "chest") or string.find(data.name, "shulker_box")) then
found_chest = true
end
end
if found_chest then
print(colors.green .. "Found chest! Depositing items..." .. colors.white)
-- 存放所有物品(除了工具和燃料)
local initial_slot = turtle.getSelectedSlot()
for slot = 1, 16 do
if turtle.getItemCount(slot) > 0 then
turtle.select(slot)
local itemDetail = turtle.getItemDetail()
-- 跳过工具和燃料
if itemDetail then
local is_tool = itemDetail.damage ~= nil
local is_fuel = itemDetail.fuel or false
if not is_tool and not is_fuel then
turtle.drop()
end
end
end
end
turtle.select(initial_slot)
print(colors.green .. "Deposit complete!" .. colors.white)
return true
end
-- 转向下一个方向
turtle.turnRight()
end
-- 如果没找到箱子,尝试向上和向下
print("Checking up and down...")
-- 检查上方
local success, data = turtle.inspectUp()
if success and data.name and (string.find(data.name, "chest") or string.find(data.name, "shulker_box")) then
print(colors.green .. "Found chest above! Depositing items..." .. colors.white)
local initial_slot = turtle.getSelectedSlot()
for slot = 1, 16 do
if turtle.getItemCount(slot) > 0 then
turtle.select(slot)
local itemDetail = turtle.getItemDetail()
if itemDetail and not itemDetail.damage and not itemDetail.fuel then
turtle.dropUp()
end
end
end
turtle.select(initial_slot)
print(colors.green .. "Deposit complete!" .. colors.white)
return true
end
-- 检查下方
success, data = turtle.inspectDown()
if success and data.name and (string.find(data.name, "chest") or string.find(data.name, "shulker_box")) then
print(colors.green .. "Found chest below! Depositing items..." .. colors.white)
local initial_slot = turtle.getSelectedSlot()
for slot = 1, 16 do
if turtle.getItemCount(slot) > 0 then
turtle.select(slot)
local itemDetail = turtle.getItemDetail()
if itemDetail and not itemDetail.damage and not itemDetail.fuel then
turtle.dropDown()
end
end
end
turtle.select(initial_slot)
print(colors.green .. "Deposit complete!" .. colors.white)
return true
end
print(colors.red .. "No chest found nearby." .. colors.white)
return false
end
-- 自动挖矿主循环
local function startMining()
print(colors.green .. "Starting auto mining..." .. colors.white)
print("Press Ctrl+T to stop.")
while true do
-- 检查燃料
if not checkFuel() then
if not refuel() then
print(colors.red .. "Critical fuel shortage! Stopping mining." .. colors.white)
break
end
end
-- 检查背包是否已满
if isInventoryFull() then
if not findChestAndDeposit() then
print(colors.red .. "Cannot deposit items. Stopping mining." .. colors.white)
break
end
end
-- 尝试挖掘前方
if not turtle.detect() then
print("Moving forward...")
if not turtle.forward() then
print(colors.red .. "Cannot move forward. Changing direction." .. colors.white)
turtle.turnRight()
if not turtle.forward() then
turtle.turnLeft()
turtle.turnLeft()
if not turtle.forward() then
turtle.turnRight()
turtle.up()
end
end
end
else
print("Mining...")
turtle.dig()
-- 检查是否有掉落物需要捡起
os.sleep(0.5)
end
-- 检查上方是否有方块可以挖掘
if turtle.detectUp() then
print("Mining above...")
turtle.digUp()
os.sleep(0.5)
end
-- 短暂延迟避免CPU占用过高
os.sleep(0.1)
end
print(colors.yellow .. "Auto mining stopped." .. colors.white)
end
-- 显示帮助信息
local function showHelp()
print("Turtle Auto Miner")
print("Usage: turtle_miner")
print("")
print("Features:")
print(" - Automatically mines blocks in front of the turtle")
print(" - Refuels automatically when fuel is low")
print(" - Searches for coal ore if no fuel is available in inventory")
print(" - Finds nearby chests and deposits items when inventory is full")
print(" - Press Ctrl+T to stop the program")
end
-- 主程序
local args = {...}
if #args > 0 and (args[1] == "help" or args[1] == "-h" or args[1] == "--help") then
showHelp()
else
startMining()
end