译者注:因为后续有引用组件部分的内容,在组件部分翻译完后再回头查看这篇
此 API 打包了机器人组件的功能,允许与机器人实现更直观的互动。
使用如下方式在你的 Lua 代码中调用这个 API:
local robot_api = require("robot") robot_api.forward()
不推荐直接访问机器人组件API,虽然可以通过组件接口这么做:
local component = require("component") local robot_component_api = component.robot -- 如果你使用 OpenOS 可以这么写,否则要写 component.proxy(component.list("robot")()) robot_component_api.move(sides.front)
尽管机器人组件提供了更通用的功能,比如说 move([side: number])
或者 drop([side: number])
,但这个API能提供更直观、更安全的功能,比如说 turnRight
, dropDown
, forward
。使用哪一种由你自己决定,甚至可以混用二者。
请注意机器人本身就是一台开放式电脑,和其它电脑一样,只是多了一个机器人组件,所以一般的API仍然可用。
查看 方块:机器人 获得更多有关机器人本身的信息。
译者注:本段的原文引用了组件-物品栏部分的内容,当该部分翻译完成后,这里会自动被翻译。
Slot indexes do not match between the robot's internal and external view of its inventory
When a robot interacts with its own inventory it is using its internal view. You access the inventory of another robot via its external view.
The reason for this distinction is to separate the two inventory spaces: the main inventory and the tool belt inventory. Internally, the robot selects slots only in its inventory space, and not its toolbelt. It can equip
or unequip items from its toolbelt. However, externally, an observer interacts with a single inventory that includes both the toolbelt as well as the main inventory.
The Robot's GUI displays the slots of a robot aligned from top-left to bottom-right. So for example the slot at index 1 is displayed in the top-left corner, the slot at index 2 is the next to the right and the first slot on the 2nd row is index 5.
These are the slot indexes a robot uses on it self (for its own main inventory, an interview view).
How many slots a robot has available depends on it's setup. If a slot is specified in a function that exceeds the amount of slots a robot has available an error is thrown.
The robot can select
, place
, drop
, compare
(etc) items from its main inventory using these slot indexes as they represent the internal perspective of its inventory.
Slot indexes of a robot's inventory to an outside observer will be offset by 4. To an outside observer, slots 1 through 4 refer to slots in the robot toolbelt. The first slot of the main inventory is slot 1 from inside the robot (internal view), but is thus slot 5 to an outside observer. Yes, this can be confusing at first. This design choice was made so that, to an outside observer, [slot 1] always indicated the first toolbelt slot regardless of the robot inventory size, and [slot 5] was always the first main inventory slot.
The robot inventory, again to an external observer, is sided inventory. The robot will not share all its inventory to all sides. Slot indexes do not change to these different sides. Slot 1, therefore, always refers to the first toolbelt slot, even when inaccessible to a side.
机器人能够灵活移动,但你应该了解一点细节。
机器人有飞行高度限制。机器人移动的基本规则是这样的:
flightHigh
格以上的位置是不可用的(限制飞行能力,默认是 8)
违反这些规则会导致机器人返回 impossible move
错误。
你可以安装悬浮升级提升(tire 1)乃至规避(tire 2)这个限制。或者你也可以在模组配置文件中禁用这个限制,只要把 limitFlightHeight
设置到 256 或者更高就行。
(译者注:为了保证程序员友好,原文中的专有名词不做翻译,仅在这段末尾附上对照表)
robot.name(): string
robot.detect(): boolean, string
true
(提示:即使面前的方块是 passable
,机器人也会返回 true
),否则返回 false
。第二个返回的参数粗略地描述前面的是什么,这个参数将会是 entity
, solid
, replaceable
, liquid
, passable
或者是 air
。robot.detectUp(): boolean, string
robot.detect()
一样,只是这个探测机器人头顶的东西。robot.detectDown(): boolean, string
robot.detect()
一样,只是这个探测机器人脚踩的东西。robot.select([slot: number]): number
robot.inventorySize(): number
robot.count([slot: number]): number
robot.space([slot: number]):number
robot.transferTo(slot: number[, count: number]): boolean
true
,如果交换两个格子的物品成功了,否则返回 false
。robot.compareTo(slot: number):boolean
robot.detect()
来确认机器人面前是否真的有方块。robot.compareUp(): boolean
robot.compare()
一样,只是这个比较机器人头顶的方块。robot.compareDown(): boolean
robot.compare()
一样,只是这个比较机器人脚踩的方块。robot.drop([count: number]): boolean
dropDown
来做这个。这个方法, drop
,会将东西丢在面前。true
,如果有至少一个物品被丢出去,反之返回 false
。robot.use
函数来和这些方块互动。robot.dropUp(): boolean
robot.drop()
一样,只是这个把物品丢在机器人头顶。robot.dropDown(): boolean
robot.drop()
一样,只是这个把物品丢在机器人脚底。robot.suck([count: number]): boolean
true
,如果捡起了至少一个物品,否则返回 false
。robot.drop
的反向,而且也会以相同的方式和物品栏互动。但是它只会拿走物品栏中第一个可用的物品。想要更精确的物品栏管理,你需要为机器人安装 物品栏控制器升级。false
之前尝试捡起其它掉落物。
如果当前选择的物品栏格子包含和机器人捡起的掉落物不同的物品,机器人会尝试将物品放在这个格子之后第一个可用的格子,要么是空的,要么是包含相同物品并且在放入机器人捡起的物品后小于等于最大堆叠上限的。如果必要,它会将物品分割到不同的格子中。如果在选定的格子之后没有可用的格子,这个函数就会失败,即使在选定的格子之前有能装下这些物品的格子。robot.suckUp([count: number]): boolean
robot.suck()
一样,只是这个尝试捡起头顶的物品。robot.suckDown([count: number]): boolean
robot.suck()
一样,只是这个尝试捡起脚踩的物品。robot.place([side: number[, sneaky: boolean]]): boolean[, string]
true
,机器人会模拟潜行时放置方块(如同一个按住 Shift 放置方块的玩家),这一般不是必要的,只是为了保持和其它模组的兼容性。true
,否则返回 false
。如果放置失败,第二个返回值会描述为什么放置失败。robot.placeUp([side: number[, sneaky: boolean]]): boolean[, string]
robot.place()
一样,只是这个尝试在头顶放置方块。robot.placeDown([side: number[, sneaky: boolean]]): boolean[, string]
robot.place()
一样,只是这个尝试在脚底放置方块。robot.durability(): number, number, number or nil, string
nil
以及一条描述为何不能返回耐久值的消息。错误信息要么是 no tool equipped
,要么是 too connot be damaged
。robot.swing([side: number[, sneaky: boolean]]): boolean[, string]
robot.swingUp([side: number[, sneaky: boolean]]): boolean[, string]
robot.swing()
一样,只是这个会将头顶的方块或实体当作目标。robot.swingDown([side: number[, sneaky: boolean]]): boolean[, string]
robot.swing()
一样,只是这个会将脚底的方块或实体当作目标。robot.use([side: number[, sneaky: boolean[, duration: number]]]): boolean[, string]
true
,机器人会模拟潜行点击右键(如同玩家按住 Shift 时点击右键)。某些物品(例如桶)在这时会有不同的行为。
duration - 这个物品被使用多长时间。这在使用例如弓之类的蓄力物品时很有用。block_activated
- 一个方块被激活了(例如拉杆、按钮或门)。item_interacted
- 装备的工具与世界互动了,例如用剪刀剪羊毛。item_placed
- 某些东西被放置在世界上了。这不仅仅由可放置的方块造成,还会由可以创造方块或实体的物品造成(例如打火石或刷怪蛋)。item_used
- 装备的物品被激活了,例如喷溅型药水。air
- 装备的物品要求有一个目标但是却没有。请注意,如果你的机器人有一个天使方块升级,这个值永远不会被返回,但是一些动作仍然不会有效果。
robot.useUp([side: number[, sneaky: boolean[, duration: number]]]): boolean[, string]
robot.use()
一样,只是这个会瞄准机器人头顶的位置。robot.useDown([side: number[, sneaky: boolean[, duration: number]]]): boolean[, string]
robot.use()
一样,只是这个会瞄准机器人脚底的位置。robot.forward(): boolean[, string]
true
,如果机器人成功移动了,否则返回 nil
。如果移动失败,第二个返回值会描述为什么失败,它会是“不可能的移动”、“没有足够能量”或者是 robot.detect
返回的障碍描述之一。robot.back(): boolean[, string]
robot.forward()
一样,只是这个会让机器人尝试向后移动。robot.up(): boolean[, string]
robot.forward()
一样,只是这个会让机器人尝试向上移动。robot.down(): boolean[, string]
robot.forward()
一样,只是这个会让机器人尝试向下移动。robot.turnLeft()
robot.turnRight()
robot.turnLeft()
一样,只是这个会让机器人右转 90°。robot.turnAround
robot.turnRight()
是一样的。 robot.level(): number
robot.tankCount(): number
robot.selectTank(tank)
robot.tankLevel([tank:number]): number
robot.compareFluidTo(tank: number): boolean
robot.transferFluidTo(tank: number[, count: number]): boolean
robot.compareFluid(): boolean
robot.compareFluidUp(): boolean
robot.compareFluid()
一样,只是这个会比较机器人头顶的液体储罐。robot.compareFluidDown(): boolean
robot.compareFluid()
一样,只是这个会比较机器人脚底的液体储罐。robot.drain([count: number]): boolean
robot.drainUp([count: number]): boolean
robot.drain()
一样,只是这个会抽取机器人头顶的液体。robot.drainDown([count: number]): boolean
robot.drain()
一样,只是这个会抽取机器人脚底的液体。robot.fill([count: number]): boolean
robot.fillUp([count: number]): boolean
robot.fill()
一样,只是这个会向机器人头顶注入液体。robot.fillDown([count: number]): boolean
robot.fill()
一样,只是这个会向机器人脚底注入液体。名词 | 翻译 | 解释 |
blloean | 布尔值 | 只有真true 和假false 两个值 |
number | 数字 | 在 Lua 中为双精度浮点数 |
string | 字符串 | OC 的中文编码有问题,请避免使用 |