feat(turtle): 新增自动农场程序并改进矿工程序

新增自动农场程序turtle_farmer.lua,实现自动收割小麦、种植种子和存储功能
改进turtle_miner.lua,添加坐标跟踪和返回起点功能
更新安装程序版本号至0.3.8 Beta 12
This commit is contained in:
2025-09-05 20:18:35 +08:00
parent 082fb99ae0
commit 89c3c49061
4 changed files with 511 additions and 9 deletions

View File

@@ -0,0 +1,73 @@
=== Turtle Auto Farmer ===
>>color white
A program that automatically harvests mature wheat, plants seeds, and deposits wheat in chests.
== Usage ==
>>color yellow
turtle_farmer
>>color white
== Description ==
The Turtle Auto Farmer is an agricultural program that allows a turtle to:
- Automatically detect and harvest mature wheat
- Plant wheat seeds in suitable soil
- Deposit harvested wheat in nearby chests when a full stack is collected
- Monitor fuel levels during operation
== Features ==
>>color green
- Automatic detection of mature wheat
- Seed planting in soil or farmland
- Wheat storage management (deposits at 64 units)
- Fuel level monitoring
- Chest detection in all directions
- Status updates during operation
>>color white
== How It Works ==
1. The turtle will move forward and search for mature wheat
2. When mature wheat is detected, it will be harvested
3. The turtle will collect wheat and seeds
4. Seeds are automatically stored in a designated slot for planting
5. When a full stack of wheat (64) is collected, the turtle will:
- Search for nearby chests in all directions
- Deposit the wheat into the chest
6. The turtle will plant seeds on suitable ground as it moves
7. The program runs until manually stopped with Ctrl+T or encounters critical issues
== Fuel Management ==
- The turtle will check fuel levels continuously
- Operation will stop if fuel drops below 200 units
- Ensure the turtle has sufficient fuel before starting
== Inventory Management ==
- Seeds are automatically stored in slot 1
- Wheat is automatically stored in slot 2
- When wheat in slot 2 reaches 64 units, the turtle will search for a chest
- The turtle will deposit wheat in any detected chest
== Important Notes ==
>>color red
- Start the turtle on farmland or near a wheat field
- Ensure the turtle has at least one wheat seed in slot 1 initially
- Place a chest nearby to collect harvested wheat
- 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 seeds, manually add seeds to slot 1
- If it runs out of fuel, manually add fuel to its inventory
== Examples ==
Start the auto farmer:
>>color yellow
turtle_farmer
>>color white
View this help message:
>>color yellow
turtle_farmer help
>>color white

View File

@@ -0,0 +1,324 @@
-- Turtle Auto Farmer 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.green)
term.at(1, 1).clearLine()
term.at(1, 1).write("=== Turtle Auto Farmer ===")
-- 恢复颜色设置
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 WHEAT_THRESHOLD = 64 -- 小麦存储阈值
local SEED_SLOT = 1 -- 种子存放槽位
local WHEAT_SLOT = 2 -- 小麦存放槽位
local FUEL_THRESHOLD = 200 -- 燃料不足阈值
-- 坐标跟踪变量
local initialX, initialY, initialZ = 0, 0, 0
local currentX, currentY, currentZ = 0, 0, 0
local direction = 0 -- 0: 北, 1: 东, 2: 南, 3: 西
-- 检查燃料是否充足
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 detectWheat()
local success, data = turtle.inspect()
if success and data.name and data.name == "minecraft:wheat" then
-- 检查是否成熟 (age为7时成熟)
if data.metadata and data.metadata.age == 7 then
return true
end
end
return false
end
-- 收割麦子
local function harvestWheat()
print("Harvesting wheat...")
turtle.dig()
-- 等待掉落物
os.sleep(0.5)
-- 收集掉落的小麦和种子
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 itemDetail.name == "minecraft:wheat" then
-- 移动小麦到指定槽位
if slot ~= WHEAT_SLOT and turtle.getItemCount(WHEAT_SLOT) == 0 then
turtle.select(slot)
turtle.transferTo(WHEAT_SLOT, turtle.getItemCount(slot))
end
elseif itemDetail and itemDetail.name == "minecraft:wheat_seeds" then
-- 移动种子到指定槽位
if slot ~= SEED_SLOT then
turtle.select(slot)
turtle.transferTo(SEED_SLOT, turtle.getItemCount(slot))
end
end
end
end
turtle.select(initial_slot)
end
-- 种植种子
local function plantSeed()
-- 检查是否有种子
if turtle.getItemCount(SEED_SLOT) == 0 then
print(colors.red .. "No seeds available!" .. colors.white)
return false
end
-- 选择种子槽位
turtle.select(SEED_SLOT)
-- 检查脚下是否有泥土或耕地
local success, data = turtle.inspectDown()
if success and (data.name == "minecraft:dirt" or data.name == "minecraft:grass_block" or data.name == "minecraft:farmland") then
print("Planting seed...")
turtle.placeDown()
return true
end
return false
end
-- 返回初始位置
local function returnToStart()
print(colors.yellow .. "Returning to starting position..." .. colors.white)
-- 先调整方向朝北
while direction ~= 0 do
turtle.turnRight()
direction = (direction + 1) % 4
end
-- 移动回初始X坐标
if currentX > 0 then
for _ = 1, currentX do
turtle.back()
end
elseif currentX < 0 then
for _ = 1, -currentX do
turtle.forward()
end
end
-- 调整方向朝西
while direction ~= 3 do
turtle.turnRight()
direction = (direction + 1) % 4
end
-- 移动回初始Z坐标
if currentZ > 0 then
for _ = 1, currentZ do
turtle.back()
end
elseif currentZ < 0 then
for _ = 1, -currentZ do
turtle.forward()
end
end
-- 恢复朝北方向
while direction ~= 0 do
turtle.turnRight()
direction = (direction + 1) % 4
end
-- 移动回初始Y坐标
if currentY > 0 then
for _ = 1, currentY do
turtle.down()
end
elseif currentY < 0 then
for _ = 1, -currentY do
turtle.up()
end
end
-- 重置当前坐标
currentX, currentY, currentZ = 0, 0, 0
print(colors.green .. "Returned to starting position." .. colors.white)
end
-- 寻找附近的箱子并存放小麦
local function findChestAndDeposit()
print(colors.yellow .. "Wheat count reached limit. Returning to start position first..." .. colors.white)
-- 检查当前槽位的小麦数量
turtle.select(WHEAT_SLOT)
if turtle.getItemCount(WHEAT_SLOT) < WHEAT_THRESHOLD then
return true
end
-- 先返回初始位置
returnToStart()
print(colors.yellow .. "Looking for chest near starting position..." .. 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 wheat..." .. colors.white)
-- 存放小麦
turtle.select(WHEAT_SLOT)
turtle.drop()
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 wheat..." .. colors.white)
turtle.select(WHEAT_SLOT)
turtle.dropUp()
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 wheat..." .. colors.white)
turtle.select(WHEAT_SLOT)
turtle.dropDown()
print(colors.green .. "Deposit complete!" .. colors.white)
return true
end
print(colors.red .. "No chest found nearby." .. colors.white)
return false
end
-- 自动种植和收割主循环
local function startFarming()
print(colors.green .. "Starting auto farming..." .. colors.white)
print("Press Ctrl+T to stop.")
-- 重置坐标
currentX, currentY, currentZ = 0, 0, 0
direction = 0 -- 初始方向朝北
while true do
-- 检查燃料
if not checkFuel() then
print(colors.red .. "Fuel low! Stopping farming." .. colors.white)
break
end
-- 检查是否需要存放小麦
if not findChestAndDeposit() then
print(colors.red .. "Cannot deposit wheat. Stopping farming." .. colors.white)
break
end
-- 检测并收割成熟的麦子
if detectWheat() then
harvestWheat()
else
-- 没有成熟的麦子,向前移动
if turtle.forward() then
-- 更新坐标 based on direction
if direction == 0 then -- 北
currentZ = currentZ - 1
elseif direction == 1 then -- 东
currentX = currentX + 1
elseif direction == 2 then -- 南
currentZ = currentZ + 1
elseif direction == 3 then -- 西
currentX = currentX - 1
end
else
print(colors.red .. "Cannot move forward. Changing direction." .. colors.white)
turtle.turnRight()
direction = (direction + 1) % 4
end
end
-- 种植种子
plantSeed()
-- 短暂延迟避免CPU占用过高
os.sleep(0.1)
end
print(colors.yellow .. "Auto farming stopped." .. colors.white)
end
-- 显示帮助信息
local function showHelp()
print("Turtle Auto Farmer")
print("Usage: turtle_farmer")
print("")
print("Features:")
print(" - Automatically detects and harvests mature wheat")
print(" - Plants wheat seeds automatically")
print(" - Deposits wheat in nearby chests 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
startFarming()
end

View File

@@ -32,6 +32,11 @@ local FUEL_THRESHOLD = 500 -- 燃料不足阈值
local INVENTORY_FULL_THRESHOLD = 15 -- 背包满时剩余空格数
local COAL_NAMES = {"minecraft:coal", "minecraft:charcoal"} -- 煤炭物品名称
-- 坐标跟踪变量
local initialX, initialY, initialZ = 0, 0, 0
local currentX, currentY, currentZ = 0, 0, 0
local direction = 0 -- 0: 北, 1: 东, 2: 南, 3: 西
-- 检查燃料是否充足
local function checkFuel()
local currentFuel = turtle.getFuelLevel()
@@ -109,14 +114,18 @@ local function refuel()
end
end
if not turtle.down() then
if turtle.down() then
currentY = currentY - 1
else
break
end
end
-- 回到原来的位置
for i = 1, 3 do
if not turtle.up() then
if turtle.up() then
currentY = currentY + 1
else
break
end
end
@@ -138,9 +147,74 @@ local function isInventoryFull()
return empty_slots <= INVENTORY_FULL_THRESHOLD
end
-- 返回初始位置
local function returnToStart()
print(colors.yellow .. "Returning to starting position..." .. colors.white)
-- 先调整方向朝北
while direction ~= 0 do
turtle.turnRight()
direction = (direction + 1) % 4
end
-- 移动回初始X坐标
if currentX > 0 then
for _ = 1, currentX do
turtle.back()
end
elseif currentX < 0 then
for _ = 1, -currentX do
turtle.forward()
end
end
-- 调整方向朝西
while direction ~= 3 do
turtle.turnRight()
direction = (direction + 1) % 4
end
-- 移动回初始Z坐标
if currentZ > 0 then
for _ = 1, currentZ do
turtle.back()
end
elseif currentZ < 0 then
for _ = 1, -currentZ do
turtle.forward()
end
end
-- 恢复朝北方向
while direction ~= 0 do
turtle.turnRight()
direction = (direction + 1) % 4
end
-- 移动回初始Y坐标
if currentY > 0 then
for _ = 1, currentY do
turtle.down()
end
elseif currentY < 0 then
for _ = 1, -currentY do
turtle.up()
end
end
-- 重置当前坐标
currentX, currentY, currentZ = 0, 0, 0
print(colors.green .. "Returned to starting position." .. colors.white)
end
-- 寻找附近的箱子并存放物品
local function findChestAndDeposit()
print(colors.yellow .. "Inventory is full. Looking for chest..." .. colors.white)
print(colors.yellow .. "Inventory is full. Returning to start position first..." .. colors.white)
-- 先返回初始位置
returnToStart()
print(colors.yellow .. "Looking for chest near starting position..." .. colors.white)
-- 尝试在周围寻找箱子
for direction = 1, 4 do -- 四个方向
@@ -235,6 +309,10 @@ local function startMining()
print(colors.green .. "Starting auto mining..." .. colors.white)
print("Press Ctrl+T to stop.")
-- 重置坐标
currentX, currentY, currentZ = 0, 0, 0
direction = 0 -- 初始方向朝北
while true do
-- 检查燃料
if not checkFuel() then
@@ -255,15 +333,42 @@ local function startMining()
-- 尝试挖掘前方
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 turtle.forward() then
-- 更新坐标 based on direction
if direction == 0 then -- 北
currentZ = currentZ - 1
elseif direction == 1 then -- 东
currentX = currentX + 1
elseif direction == 2 then -- 南
currentZ = currentZ + 1
elseif direction == 3 then -- 西
currentX = currentX - 1
end
else
print(colors.red .. "Cannot move forward. Changing direction." .. colors.white)
turtle.turnRight()
direction = (direction + 1) % 4
if not turtle.forward() then
turtle.turnLeft()
turtle.turnLeft()
if not turtle.forward() then
direction = (direction + 2) % 4
if turtle.forward() then
-- 更新坐标 based on direction
if direction == 0 then -- 北
currentZ = currentZ - 1
elseif direction == 1 then -- 东
currentX = currentX + 1
elseif direction == 2 then -- 南
currentZ = currentZ + 1
elseif direction == 3 then -- 西
currentX = currentX - 1
end
else
turtle.turnRight()
turtle.up()
direction = (direction + 1) % 4
if turtle.up() then
currentY = currentY + 1
end
end
end
end

View File

@@ -1,5 +1,5 @@
-- LeonOS installer
local INSTALLER_VERSION = "0.3.8 Beta 11"
local INSTALLER_VERSION = "0.3.8 Beta 12"
local DEFAULT_ROM_DIR = "/leonos"
print("Start loading LeonOS installer ("..INSTALLER_VERSION..")...")