mirror of
https://github.com/CCLeonOS/LeonOS.git
synced 2026-03-03 15:17:01 +00:00
feat(turtle): 添加海龟控制程序及帮助文档
添加海龟控制程序turtle.lua,实现移动、挖掘、放置和物品栏管理功能 添加对应的帮助文档turtle.hlp,说明程序使用方法和命令列表
This commit is contained in:
238
data/computercraft/lua/rom/programs/turtle.lua
Normal file
238
data/computercraft/lua/rom/programs/turtle.lua
Normal file
@@ -0,0 +1,238 @@
|
||||
-- turtle.lua - Turtle control program for CC Tweaked
|
||||
|
||||
local term = require("term")
|
||||
local colors = require("colors")
|
||||
local expect = require("cc.expect").expect
|
||||
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 Control ===")
|
||||
|
||||
-- 恢复颜色设置
|
||||
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 function show_help()
|
||||
print("Turtle Control Program")
|
||||
print("Usage:")
|
||||
print(" turtle <command> [arguments]")
|
||||
print("")
|
||||
print("Movement Commands:")
|
||||
print(" forward, f - Move forward")
|
||||
print(" back, b - Move back")
|
||||
print(" up, u - Move up")
|
||||
print(" down, d - Move down")
|
||||
print(" left, l - Turn left")
|
||||
print(" right, r - Turn right")
|
||||
print("")
|
||||
print("Mining Commands:")
|
||||
print(" dig, d [up|down] - Dig forward (or up/down)")
|
||||
print(" digall - Dig in all directions")
|
||||
print("")
|
||||
print("Placement Commands:")
|
||||
print(" place [up|down] - Place block forward (or up/down)")
|
||||
print("")
|
||||
print("Inventory Commands:")
|
||||
print(" inventory, inv - Show inventory")
|
||||
print(" select <slot> - Select slot (1-16)")
|
||||
print("")
|
||||
print("Utility Commands:")
|
||||
print(" help, h - Show this help message")
|
||||
print(" version, v - Show program version")
|
||||
end
|
||||
|
||||
-- 显示版本信息
|
||||
local function show_version()
|
||||
print("Turtle Control Program v1.0")
|
||||
print("Part of LeonOS for CC Tweaked")
|
||||
end
|
||||
|
||||
-- 移动命令
|
||||
local function move_forward()
|
||||
print("Moving forward...")
|
||||
if turtle.forward() then
|
||||
print(colors.green .. "Success!" .. colors.white)
|
||||
else
|
||||
print(colors.red .. "Failed! Blocked or no fuel." .. colors.white)
|
||||
end
|
||||
end
|
||||
|
||||
local function move_back()
|
||||
print("Moving back...")
|
||||
if turtle.back() then
|
||||
print(colors.green .. "Success!" .. colors.white)
|
||||
else
|
||||
print(colors.red .. "Failed! Blocked or no fuel." .. colors.white)
|
||||
end
|
||||
end
|
||||
|
||||
local function move_up()
|
||||
print("Moving up...")
|
||||
if turtle.up() then
|
||||
print(colors.green .. "Success!" .. colors.white)
|
||||
else
|
||||
print(colors.red .. "Failed! Blocked or no fuel." .. colors.white)
|
||||
end
|
||||
end
|
||||
|
||||
local function move_down()
|
||||
print("Moving down...")
|
||||
if turtle.down() then
|
||||
print(colors.green .. "Success!" .. colors.white)
|
||||
else
|
||||
print(colors.red .. "Failed! Blocked or no fuel." .. colors.white)
|
||||
end
|
||||
end
|
||||
|
||||
local function turn_left()
|
||||
print("Turning left...")
|
||||
turtle.turnLeft()
|
||||
print(colors.green .. "Success!" .. colors.white)
|
||||
end
|
||||
|
||||
local function turn_right()
|
||||
print("Turning right...")
|
||||
turtle.turnRight()
|
||||
print(colors.green .. "Success!" .. colors.white)
|
||||
end
|
||||
|
||||
-- 挖掘命令
|
||||
local function dig(direction)
|
||||
direction = direction or "forward"
|
||||
print("Digging " .. direction .. "...")
|
||||
local success
|
||||
if direction == "up" then
|
||||
success = turtle.digUp()
|
||||
elseif direction == "down" then
|
||||
success = turtle.digDown()
|
||||
else
|
||||
success = turtle.dig()
|
||||
end
|
||||
if success then
|
||||
print(colors.green .. "Success!" .. colors.white)
|
||||
else
|
||||
print(colors.red .. "Failed! Nothing to dig or no tool." .. colors.white)
|
||||
end
|
||||
end
|
||||
|
||||
local function dig_all()
|
||||
print("Digging in all directions...")
|
||||
dig()
|
||||
dig("up")
|
||||
dig("down")
|
||||
end
|
||||
|
||||
-- 放置命令
|
||||
local function place(direction)
|
||||
direction = direction or "forward"
|
||||
print("Placing block " .. direction .. "...")
|
||||
local success
|
||||
if direction == "up" then
|
||||
success = turtle.placeUp()
|
||||
elseif direction == "down" then
|
||||
success = turtle.placeDown()
|
||||
else
|
||||
success = turtle.place()
|
||||
end
|
||||
if success then
|
||||
print(colors.green .. "Success!" .. colors.white)
|
||||
else
|
||||
print(colors.red .. "Failed! No block to place or invalid position." .. colors.white)
|
||||
end
|
||||
end
|
||||
|
||||
-- 物品栏命令
|
||||
local function show_inventory()
|
||||
print("Turtle Inventory:")
|
||||
print("=================")
|
||||
for i = 1, 16 do
|
||||
local itemCount = turtle.getItemCount(i)
|
||||
if itemCount > 0 then
|
||||
term.setTextColor(colors.yellow)
|
||||
print("Slot " .. i .. ":" .. colors.white .. " " .. itemCount .. " items")
|
||||
else
|
||||
print("Slot " .. i .. ": Empty")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function select_slot(slot)
|
||||
expect(1, slot, "number")
|
||||
if slot < 1 or slot > 16 then
|
||||
error("Slot must be between 1 and 16", 0)
|
||||
end
|
||||
print("Selecting slot " .. slot .. "...")
|
||||
if turtle.select(slot) then
|
||||
print(colors.green .. "Success!" .. colors.white)
|
||||
else
|
||||
print(colors.red .. "Failed! Invalid slot." .. colors.white)
|
||||
end
|
||||
end
|
||||
|
||||
-- 主程序
|
||||
local args = {...}
|
||||
if #args == 0 then
|
||||
show_help()
|
||||
else
|
||||
local command = args[1]:lower()
|
||||
if command == "help" or command == "h" then
|
||||
show_help()
|
||||
elseif command == "version" or command == "v" then
|
||||
show_version()
|
||||
elseif command == "forward" or command == "f" then
|
||||
move_forward()
|
||||
elseif command == "back" or command == "b" then
|
||||
move_back()
|
||||
elseif command == "up" or command == "u" then
|
||||
move_up()
|
||||
elseif command == "down" or command == "d" then
|
||||
if #args > 1 and (args[2] == "up" or args[2] == "down") then
|
||||
dig(args[2])
|
||||
else
|
||||
move_down()
|
||||
end
|
||||
elseif command == "left" or command == "l" then
|
||||
turn_left()
|
||||
elseif command == "right" or command == "r" then
|
||||
turn_right()
|
||||
elseif command == "dig" then
|
||||
if #args > 1 and (args[2] == "up" or args[2] == "down") then
|
||||
dig(args[2])
|
||||
else
|
||||
dig()
|
||||
end
|
||||
elseif command == "digall" then
|
||||
dig_all()
|
||||
elseif command == "place" then
|
||||
if #args > 1 and (args[2] == "up" or args[2] == "down") then
|
||||
place(args[2])
|
||||
else
|
||||
place()
|
||||
end
|
||||
elseif command == "inventory" or command == "inv" then
|
||||
show_inventory()
|
||||
elseif command == "select" then
|
||||
if #args > 1 then
|
||||
select_slot(tonumber(args[2]))
|
||||
else
|
||||
error("Missing slot number", 0)
|
||||
end
|
||||
else
|
||||
error("Unknown command: " .. command, 0)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user