feat(turtle): 添加箱子检测功能防止误挖

在农夫和矿工乌龟程序中添加箱子检测功能,防止误挖箱子。新增CHEST_NAMES列表和isChest函数来识别箱子类型,并在收割/挖掘前进行检查。当检测到箱子时会打印警告信息并跳过操作。

同时更新安装程序版本号至0.3.8 Beta 13。
This commit is contained in:
2025-09-05 20:26:27 +08:00
parent 89c3c49061
commit 3676f7a2ce
3 changed files with 76 additions and 16 deletions

View File

@@ -32,12 +32,26 @@ local WHEAT_THRESHOLD = 64 -- 小麦存储阈值
local SEED_SLOT = 1 -- 种子存放槽位
local WHEAT_SLOT = 2 -- 小麦存放槽位
local FUEL_THRESHOLD = 200 -- 燃料不足阈值
local CHEST_NAMES = {"chest", "shulker_box"} -- 箱子类型名称
-- 坐标跟踪变量
local initialX, initialY, initialZ = 0, 0, 0
local currentX, currentY, currentZ = 0, 0, 0
local direction = 0 -- 0: 北, 1: 东, 2: 南, 3: 西
-- 检查是否是箱子
local function isChest(block_data)
if not block_data or not block_data.name then
return false
end
for _, chest_name in ipairs(CHEST_NAMES) do
if string.find(block_data.name, chest_name) then
return true
end
end
return false
-- 检查燃料是否充足
local function checkFuel()
local currentFuel = turtle.getFuelLevel()
@@ -54,19 +68,31 @@ end
-- 检测前方是否有成熟的麦子
local function detectWheat()
local success, data = turtle.inspect()
if success and data.name and data.name == "minecraft:wheat" then
if success then
-- 先检查是否是箱子
if isChest(data) then
print(colors.red .. "Found chest! Mining is prohibited." .. colors.white)
return false
elseif data.name and data.name == "minecraft:wheat" then
-- 检查是否成熟 (age为7时成熟)
if data.metadata and data.metadata.age == 7 then
return true
end
end
end
return false
end
-- 收割麦子
local function harvestWheat()
print("Harvesting wheat...")
-- 再次检查是否是箱子,防止误挖
local success, data = turtle.inspect()
if success and not isChest(data) then
turtle.dig()
else
print(colors.red .. "Cannot harvest: Detected chest or invalid block." .. colors.white)
end
-- 等待掉落物
os.sleep(0.5)

View File

@@ -31,12 +31,26 @@ end
local FUEL_THRESHOLD = 500 -- 燃料不足阈值
local INVENTORY_FULL_THRESHOLD = 15 -- 背包满时剩余空格数
local COAL_NAMES = {"minecraft:coal", "minecraft:charcoal"} -- 煤炭物品名称
local CHEST_NAMES = {"chest", "shulker_box"} -- 箱子类型名称
-- 坐标跟踪变量
local initialX, initialY, initialZ = 0, 0, 0
local currentX, currentY, currentZ = 0, 0, 0
local direction = 0 -- 0: 北, 1: 东, 2: 南, 3: 西
-- 检查是否是箱子
local function isChest(block_data)
if not block_data or not block_data.name then
return false
end
for _, chest_name in ipairs(CHEST_NAMES) do
if string.find(block_data.name, chest_name) then
return true
end
end
return false
-- 检查燃料是否充足
local function checkFuel()
local currentFuel = turtle.getFuelLevel()
@@ -94,7 +108,13 @@ local function refuel()
for i = 1, 3 do -- 尝试挖3格
turtle.select(old_slot) -- 确保使用正确的工具
if turtle.detectDown() then
-- 检查是否是箱子
local success, data = turtle.inspectDown()
if success and isChest(data) then
print(colors.red .. "Found chest below! Mining is prohibited." .. colors.white)
else
turtle.digDown()
end
-- 检查是否挖到了煤炭
for slot = 1, 16 do
if turtle.getItemCount(slot) > 0 then
@@ -372,17 +392,31 @@ local function startMining()
end
end
end
else
-- 检查是否是箱子
local success, data = turtle.inspect()
if success and isChest(data) then
print(colors.red .. "Found chest! Mining is prohibited. Changing direction." .. colors.white)
turtle.turnRight()
direction = (direction + 1) % 4
else
print("Mining...")
turtle.dig()
end
-- 检查是否有掉落物需要捡起
-- os.sleep(0.5)
end
-- 检查上方是否有方块可以挖掘
if turtle.detectUp() then
-- 检查是否是箱子
local success, data = turtle.inspectUp()
if success and isChest(data) then
print(colors.red .. "Found chest above! Mining is prohibited." .. colors.white)
else
print("Mining above...")
turtle.digUp()
end
-- os.sleep(0.5)
end

View File

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