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 SEED_SLOT = 1 -- 种子存放槽位
local WHEAT_SLOT = 2 -- 小麦存放槽位 local WHEAT_SLOT = 2 -- 小麦存放槽位
local FUEL_THRESHOLD = 200 -- 燃料不足阈值 local FUEL_THRESHOLD = 200 -- 燃料不足阈值
local CHEST_NAMES = {"chest", "shulker_box"} -- 箱子类型名称
-- 坐标跟踪变量 -- 坐标跟踪变量
local initialX, initialY, initialZ = 0, 0, 0 local initialX, initialY, initialZ = 0, 0, 0
local currentX, currentY, currentZ = 0, 0, 0 local currentX, currentY, currentZ = 0, 0, 0
local direction = 0 -- 0: 北, 1: 东, 2: 南, 3: 西 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 function checkFuel()
local currentFuel = turtle.getFuelLevel() local currentFuel = turtle.getFuelLevel()
@@ -54,10 +68,16 @@ end
-- 检测前方是否有成熟的麦子 -- 检测前方是否有成熟的麦子
local function detectWheat() local function detectWheat()
local success, data = turtle.inspect() local success, data = turtle.inspect()
if success and data.name and data.name == "minecraft:wheat" then if success then
-- 检查是否成熟 (age为7时成熟) -- 检查是否是箱子
if data.metadata and data.metadata.age == 7 then if isChest(data) then
return true 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
end end
return false return false
@@ -66,7 +86,13 @@ end
-- 收割麦子 -- 收割麦子
local function harvestWheat() local function harvestWheat()
print("Harvesting wheat...") print("Harvesting wheat...")
turtle.dig() -- 再次检查是否是箱子,防止误挖
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) os.sleep(0.5)

View File

@@ -31,12 +31,26 @@ end
local FUEL_THRESHOLD = 500 -- 燃料不足阈值 local FUEL_THRESHOLD = 500 -- 燃料不足阈值
local INVENTORY_FULL_THRESHOLD = 15 -- 背包满时剩余空格数 local INVENTORY_FULL_THRESHOLD = 15 -- 背包满时剩余空格数
local COAL_NAMES = {"minecraft:coal", "minecraft:charcoal"} -- 煤炭物品名称 local COAL_NAMES = {"minecraft:coal", "minecraft:charcoal"} -- 煤炭物品名称
local CHEST_NAMES = {"chest", "shulker_box"} -- 箱子类型名称
-- 坐标跟踪变量 -- 坐标跟踪变量
local initialX, initialY, initialZ = 0, 0, 0 local initialX, initialY, initialZ = 0, 0, 0
local currentX, currentY, currentZ = 0, 0, 0 local currentX, currentY, currentZ = 0, 0, 0
local direction = 0 -- 0: 北, 1: 东, 2: 南, 3: 西 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 function checkFuel()
local currentFuel = turtle.getFuelLevel() local currentFuel = turtle.getFuelLevel()
@@ -91,10 +105,16 @@ local function refuel()
end end
-- 挖掘脚下可能的煤矿 -- 挖掘脚下可能的煤矿
for i = 1, 3 do -- 尝试挖3格 for i = 1, 3 do -- 尝试挖3格
turtle.select(old_slot) -- 确保使用正确的工具 turtle.select(old_slot) -- 确保使用正确的工具
if turtle.detectDown() then if turtle.detectDown() then
turtle.digDown() -- 检查是否是箱子
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 for slot = 1, 16 do
if turtle.getItemCount(slot) > 0 then if turtle.getItemCount(slot) > 0 then
@@ -331,7 +351,7 @@ local function startMining()
end end
-- 尝试挖掘前方 -- 尝试挖掘前方
if not turtle.detect() then if not turtle.detect() then
print("Moving forward...") print("Moving forward...")
if turtle.forward() then if turtle.forward() then
-- 更新坐标 based on direction -- 更新坐标 based on direction
@@ -373,16 +393,30 @@ local function startMining()
end end
end end
else else
print("Mining...") -- 检查是否是箱子
turtle.dig() 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) -- os.sleep(0.5)
end end
-- 检查上方是否有方块可以挖掘 -- 检查上方是否有方块可以挖掘
if turtle.detectUp() then if turtle.detectUp() then
print("Mining above...") -- 检查是否是箱子
turtle.digUp() 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) -- os.sleep(0.5)
end end

View File

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