From ef4875d4565d3cd37315463dc7ee2d3f8355db74 Mon Sep 17 00:00:00 2001 From: Leonmmcoset Date: Fri, 5 Sep 2025 20:59:32 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E7=A8=8B=E5=BA=8F):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=B5=B7=E9=BE=9F=E9=87=87=E7=9F=BF=E5=92=8C=E5=86=9C=E5=9C=BA?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E7=9A=84=E5=A4=9A=E4=B8=AA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 更新安装程序版本号至0.3.8 Beta 15 - 修改采矿程序:调整背包满阈值,添加中断处理和返回起始位置功能 - 修改农场程序:添加背包满检测,改进物品存放逻辑,添加中断处理 - 统一两个程序的错误提示信息和返回起始位置功能 --- .../lua/rom/programs/turtle_farmer.lua | 50 ++++++++++++++++--- .../lua/rom/programs/turtle_miner.lua | 16 +++++- installer.lua | 2 +- 3 files changed, 58 insertions(+), 10 deletions(-) diff --git a/data/computercraft/lua/rom/programs/turtle_farmer.lua b/data/computercraft/lua/rom/programs/turtle_farmer.lua index 543c27f..302fa20 100644 --- a/data/computercraft/lua/rom/programs/turtle_farmer.lua +++ b/data/computercraft/lua/rom/programs/turtle_farmer.lua @@ -32,6 +32,7 @@ local WHEAT_THRESHOLD = 64 -- 小麦存储阈值 local SEED_SLOT = 1 -- 种子存放槽位 local WHEAT_SLOT = 2 -- 小麦存放槽位 local FUEL_THRESHOLD = 200 -- 燃料不足阈值 +local INVENTORY_FULL_THRESHOLD = 0 -- 背包满时剩余空格数 (0表示16个格子全满) local CHEST_NAMES = {"chest", "shulker_box"} -- 箱子类型名称 -- 坐标跟踪变量 @@ -50,6 +51,17 @@ local function isChest(block_data) end end return false +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 -- 检查燃料是否充足 @@ -205,13 +217,25 @@ local function returnToStart() 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) + -- 检查是否需要存放小麦或背包已满 + local need_deposit = false - -- 检查当前槽位的小麦数量 + -- 检查小麦数量 turtle.select(WHEAT_SLOT) - if turtle.getItemCount(WHEAT_SLOT) < WHEAT_THRESHOLD then + if turtle.getItemCount(WHEAT_SLOT) >= WHEAT_THRESHOLD then + need_deposit = true + end + + -- 检查背包是否已满 + if isInventoryFull() then + print(colors.yellow .. "Inventory is full. Returning to start position first..." .. colors.white) + need_deposit = true + end + + -- 如果不需要存放,直接返回 + if not need_deposit then return true end @@ -270,7 +294,7 @@ local function findChestAndDeposit() return true end - print(colors.red .. "No chest found nearby." .. colors.white) + print(colors.red .. "No chest found nearby. Already at starting position." .. colors.white) return false end @@ -283,6 +307,8 @@ local function startFarming() currentX, currentY, currentZ = 0, 0, 0 direction = 0 -- 初始方向朝北 + -- 捕获Ctrl+T中断,确保在停止时返回初始位置 + local success, error = pcall(function() while true do -- 检查燃料 if not checkFuel() then @@ -290,9 +316,9 @@ local function startFarming() break end - -- 检查是否需要存放小麦 + -- 检查是否需要存放小麦或背包已满 if not findChestAndDeposit() then - print(colors.red .. "Cannot deposit wheat. Stopping farming." .. colors.white) + print(colors.red .. "Cannot deposit items. Stopping farming." .. colors.white) break end @@ -326,6 +352,16 @@ local function startFarming() os.sleep(0.1) end + end + ) + + -- 确保在停止时返回初始位置 + if not success then + print(colors.red .. "Program interrupted: " .. tostring(error) .. "" .. colors.white) + end + + print(colors.yellow .. "Returning to starting position before stopping..." .. colors.white) + returnToStart() print(colors.yellow .. "Auto farming stopped." .. colors.white) end diff --git a/data/computercraft/lua/rom/programs/turtle_miner.lua b/data/computercraft/lua/rom/programs/turtle_miner.lua index 76b687a..faf5001 100644 --- a/data/computercraft/lua/rom/programs/turtle_miner.lua +++ b/data/computercraft/lua/rom/programs/turtle_miner.lua @@ -29,7 +29,7 @@ end -- 配置常量 local FUEL_THRESHOLD = 500 -- 燃料不足阈值 -local INVENTORY_FULL_THRESHOLD = 15 -- 背包满时剩余空格数 +local INVENTORY_FULL_THRESHOLD = 0 -- 背包满时剩余空格数 (0表示16个格子全满) local COAL_NAMES = {"minecraft:coal", "minecraft:charcoal"} -- 煤炭物品名称 local CHEST_NAMES = {"chest", "shulker_box"} -- 箱子类型名称 @@ -321,7 +321,7 @@ local function findChestAndDeposit() return true end - print(colors.red .. "No chest found nearby." .. colors.white) + print(colors.red .. "No chest found nearby. Already at starting position." .. colors.white) return false end @@ -334,6 +334,8 @@ local function startMining() currentX, currentY, currentZ = 0, 0, 0 direction = 0 -- 初始方向朝北 + -- 捕获Ctrl+T中断,确保在停止时返回初始位置 + local success, error = pcall(function() while true do -- 检查燃料 if not checkFuel() then @@ -425,6 +427,16 @@ local function startMining() -- os.sleep(0.1) end + end + ) + + -- 确保在停止时返回初始位置 + if not success then + print(colors.red .. "Program interrupted: " .. tostring(error) .. "" .. colors.white) + end + + print(colors.yellow .. "Returning to starting position before stopping..." .. colors.white) + returnToStart() print(colors.yellow .. "Auto mining stopped." .. colors.white) end diff --git a/installer.lua b/installer.lua index 8ef9b67..8f6a48b 100644 --- a/installer.lua +++ b/installer.lua @@ -1,5 +1,5 @@ -- LeonOS installer -local INSTALLER_VERSION = "0.3.8 Beta 14" +local INSTALLER_VERSION = "0.3.8 Beta 15" local DEFAULT_ROM_DIR = "/leonos" print("Start loading LeonOS installer ("..INSTALLER_VERSION..")...")