feat(turtle): 添加海龟控制程序及帮助文档

添加海龟控制程序turtle.lua,实现移动、挖掘、放置和物品栏管理功能
添加对应的帮助文档turtle.hlp,说明程序使用方法和命令列表
This commit is contained in:
2025-09-03 21:26:12 +08:00
parent 788ed1f685
commit 320876740b
12 changed files with 884 additions and 1 deletions

View File

@@ -0,0 +1,91 @@
=== Turtle Control ===
A program to control turtle movement, mining, and inventory management in CC Tweaked.
== Basic Usage ==
>>color yellow
turtle <command> [arguments]
>>color white
== Available Commands ==
- **Movement Commands:**
- **forward**, **f**: Move forward
- **back**, **b**: Move back
- **up**, **u**: Move up
- **down**, **d**: Move down
- **left**, **l**: Turn left
- **right**, **r**: Turn right
- **Mining Commands:**
- **dig** [up|down]: Dig forward (or up/down)
- **digall**: Dig in all directions (forward, up, down)
- **Placement Commands:**
- **place** [up|down]: Place block forward (or up/down)
- **Inventory Commands:**
- **inventory**, **inv**: Show inventory
- **select** <slot>: Select slot (1-16)
- **Utility Commands:**
- **help**, **h**: Show this help message
- **version**, **v**: Show program version
== Usage Examples ==
1. Move forward:
>>color yellow
turtle forward
>>color white
2. Move up:
>>color yellow
turtle up
>>color white
3. Turn right:
>>color yellow
turtle right
>>color white
4. Dig upward:
>>color yellow
turtle dig up
>>color white
5. Place a block downward:
>>color yellow
turtle place down
>>color white
6. Show inventory:
>>color yellow
turtle inventory
>>color white
7. Select slot 5:
>>color yellow
turtle select 5
>>color white
8. Dig in all directions:
>>color yellow
turtle digall
>>color white
== Notes ==
- This program must be run on a turtle
- Movement and mining commands require fuel
- Mining commands require appropriate tools
- Placement commands require blocks in the selected slot
- Inventory slots are numbered from 1 to 16
== Troubleshooting ==
- If movement fails, check fuel level and ensure path is clear
- If mining fails, check if you have the right tool for the block
- If placing fails, ensure you have blocks in the selected slot
- If inventory is not displaying correctly, try selecting a slot first

View 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