mirror of
https://github.com/CCLeonOS/LeonOS.git
synced 2026-03-03 15:17:01 +00:00
feat(turtle): 添加自动采矿海龟程序及帮助文档
添加新的海龟自动采矿程序,包含以下功能: - 自动挖掘前方和上方的方块 - 智能燃料管理系统,低燃料时自动补充 - 背包满时自动寻找附近箱子存放物品 - 包含详细的帮助文档和使用说明 同时更新安装程序版本号至0.3.8 Beta 8
This commit is contained in:
72
data/computercraft/lua/rom/help/turtle_miner.hlp
Normal file
72
data/computercraft/lua/rom/help/turtle_miner.hlp
Normal file
@@ -0,0 +1,72 @@
|
||||
=== Turtle Auto Miner ===
|
||||
>>color white
|
||||
A program that automatically mines blocks, manages fuel, and deposits items in chests.
|
||||
|
||||
== Usage ==
|
||||
>>color yellow
|
||||
turtle_miner
|
||||
>>color white
|
||||
|
||||
== Description ==
|
||||
The Turtle Auto Miner is an advanced mining program that allows a turtle to:
|
||||
- Automatically mine blocks in front of it
|
||||
- Manage fuel levels and refuel when necessary
|
||||
- Search for coal ore if no fuel is available in inventory
|
||||
- Find nearby chests and deposit items when inventory is full
|
||||
|
||||
== Features ==
|
||||
>>color green
|
||||
- Automatic mining of blocks in front and above
|
||||
- Intelligent fuel management system
|
||||
- Automatic coal searching when fuel is low
|
||||
- Chest detection and item depositing
|
||||
- Obstacle avoidance
|
||||
- Status updates during operation
|
||||
>>color white
|
||||
|
||||
== How It Works ==
|
||||
1. The turtle will start mining blocks in front of it
|
||||
2. It continuously checks fuel levels
|
||||
3. When fuel is low, it will:
|
||||
- First check inventory for fuel items
|
||||
- If none found, search for coal ore by digging downward
|
||||
4. When inventory is nearly full, it will:
|
||||
- Search for nearby chests in all directions
|
||||
- Deposit all non-tool and non-fuel items into the chest
|
||||
5. The program runs until manually stopped with Ctrl+T or encounters critical issues
|
||||
|
||||
== Fuel Management ==
|
||||
- The turtle will refuel when fuel level drops below 500
|
||||
- It recognizes all fuel items (including coal, charcoal, and other burnable items)
|
||||
- If no fuel is found in inventory, it will dig downward to search for coal ore
|
||||
|
||||
== Inventory Management ==
|
||||
- The turtle considers its inventory full when there are 15 or fewer empty slots
|
||||
- When depositing items, it keeps tools and fuel in inventory
|
||||
- It searches for chests in all four directions, above, and below
|
||||
|
||||
== Important Notes ==
|
||||
>>color red
|
||||
- Make sure the turtle has appropriate mining tools
|
||||
- Keep some fuel in the turtle's inventory before starting
|
||||
- Place a chest nearby to collect mined items
|
||||
- Monitor the turtle's progress periodically
|
||||
- Press Ctrl+T to stop the program at any time
|
||||
>>color white
|
||||
|
||||
== Troubleshooting ==
|
||||
- If the turtle gets stuck, press Ctrl+T to stop and manually move it
|
||||
- If it can't find a chest, place one nearby and restart
|
||||
- If it runs out of fuel, manually add fuel to its inventory
|
||||
- If mining efficiency is low, ensure the turtle has the right tools
|
||||
|
||||
== Examples ==
|
||||
Start the auto miner:
|
||||
>>color yellow
|
||||
turtle_miner
|
||||
>>color white
|
||||
|
||||
View this help message:
|
||||
>>color yellow
|
||||
turtle_miner help
|
||||
>>color white
|
||||
309
data/computercraft/lua/rom/programs/turtle_miner.lua
Normal file
309
data/computercraft/lua/rom/programs/turtle_miner.lua
Normal file
@@ -0,0 +1,309 @@
|
||||
-- Turtle Auto Miner Program
|
||||
-- Part of LeonOS for CC Tweaked
|
||||
|
||||
local term = require("term")
|
||||
local colors = require("colors")
|
||||
local textutils = require("textutils")
|
||||
|
||||
-- 保存当前颜色设置
|
||||
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
|
||||
Reference in New Issue
Block a user