使用Opencomputers中的机器人实现IC2自动杂交 —— (1) 导航篇

寒假的时候玩了一个寒假的GTNH整合包,这个包可以说是非常好玩了。这个包的主线是格雷科技mod,中间穿插了很多其他的科技类mod,也有一些魔法mod。

这个包里面有一个mod叫工业2,是一个老牌科技类mod。但是我们今天要关注的不是它的科技的部分,而是它的一个独特的内容:杂交。

GTNH由于添加了一些和工业2作物联动的mod,加入了许多新的作物,通过这些作物,甚至可以实现的矿物自动生产和UU物质的廉价生产。可以说是非常OP了。

但是杂交也有它的缺点。头号缺点莫过于非常耗时间。杂交获得的作物种类,作物的各种属性,甚至打掉作物是否能够掉种子包都是一个未知数,俗称“赌草”。

而我们今天的主角是这个小家伙:

Robot — Opencomputers

它是来自Opencomputers mod的机器人,它就像我们小时候微机课学的小乌龟一样,可以往前走,往后退,左转右转。如果加上对应的插件,它还可以存储物品,和容器交互,和方块交互,获取方块的信息。可以说是非常万能了。

要控制这个小家伙我们使用的是Lua语言。这个语言是一种嵌入式语言,广泛用于各种游戏的控制台。它由于其语法简单,体积小,成为了广受欢迎的嵌入式语言。

如果我们要控制这个机器人的走动,分别使用下面四个语句就可以了:

robot.forward()
robot.back()
robot.turnLeft()
robot.turnRight()

其中robot.back()实际上不怎么会用到。

既然这个机器人能够走动,能够和方块交互,那我们可不可以通过这个机器人来自动化前面所提到的杂交过程呢? 答案是肯定到。倒不如说这正是我提到这个机器人的理由。

如果我们要控制机器人在一个区域走动,第一重要的肯定是知晓机器人的空间坐标,并且能够实现给定一个坐标,机器人能自动寻路到达那个地点的功能。

下面是我写的一个导航库:

local robot = require("robot")

local nowFacing = 1
local nowPos = {0, 0}
local savedFacing = 0
local savedPos = {0, 0}

local function getFacing()
    return nowFacing
end

local function getPos()
    return nowPos
end

local function turnTo(facing)
    local delta = (facing - nowFacing) % 4
    nowFacing = facing
    if delta <= 2 then
        for i = 1, delta do
            robot.turnRight()
        end
    else
        for i = 1, 4 - delta do
            robot.turnLeft()
        end
    end
end

local function turningDelta(facing)
    local delta = (facing - nowFacing) % 4
    if delta <= 2 then
        return delta
    else
        return 4-delta
    end
end

local function go(pos)
    if nowPos[1] == pos[1] and nowPos[2] == pos[2] then
        return
    end

    -- find path
    local posDelta = {pos[1]-nowPos[1], pos[2]-nowPos[2]}
    local path = {}

    if posDelta[1] > 0 then
        path[#path+1] = {2, posDelta[1]}
    elseif posDelta[1] < 0 then
        path[#path+1] = {4, -posDelta[1]}
    end

    if posDelta[2] > 0 then
        path[#path+1] = {1, posDelta[2]}
    elseif posDelta[2] < 0 then
        path[#path+1] = {3, -posDelta[2]}
    end

    -- optimal first turn
    if #path == 2 and turningDelta(path[2][1]) < turningDelta(path[1][1]) then
        path[1], path[2] = path[2], path[1]
    end

    for i=1, #path do
        turnTo(path[i][1])
        for _=1, path[i][2] do
            robot.forward()
        end
    end

    nowPos = pos
end

local function save()
    savedFacing = nowFacing
    savedPos = nowPos
end

local function resume()
    if savedFacing == 0 then
        return
    end
    go(savedPos)
    turnTo(savedFacing)
end

return {
    getFacing = getFacing,
    getPos = getPos,
    turnTo = turnTo,
    go = go,
    save = save,
    resume = resume
}

使用方法如下:

-- 加载库 (请确保如上代码已保存为gps.lua)
-- 加载库的位置为初始位置,面向的方向为y轴正方向。
gps = require("gps.lua")
-- 前往坐标{2, 3}
gps.go({2, 3})
-- 前往坐标{4, 5}
gps.go({4, 5})
-- 保存当前位置和朝向
gps.save()
-- 前往坐标{0, 0}
gps.go({0, 0})
-- 前往保存的坐标和朝向
gps.resume()

评论

  1. LiChen
    3年前
    2021-8-21 9:29:16

    还有吗 还有吗 :wink: :wink:

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇