开发指南

挂机脚本开发

Demo.py

"""
脚本说明
"""

from ZBot import Script
from ZBot import Image
from ZBot.utils import printf

class BotScript(Script):
    """Demo Script"""
    delay_time_min = 3.6    # 脚本循环最短时间
    delay_time_max = 4.1    # 脚本循环最长时间
    is_return_app = False   # 运行结束后是否激活指定App
    retry_count = 2         # 执行失败后尝试的次数
    horizontal_offset = 0   # 左右偏移量 80为最大值
    vertical_offset = 18    # 上下偏移量 16为最大值
    inout_offset = 18       # 大小偏移量 16为最大值
    type_begin = 0          # 计数器,开始位
    type_end = 6            # 计数器,结束为

    # state数组是状态队列,按照顺序逐一执行
    states = [
        {'name': "job1", 'on_enter': "job1"},
        {'name': "job2", 'on_enter': "job2"},
        'failed', 'done', 'initial'
    ]

    # 脚本参数字典
    parameter = {
        "Object Type": {
            "type": "select",
            "data": {
                "cut": (15.4, 17.3, 1),
                "smelt": (51.4, 55.8, 1),
                "string": (7.7, 9.3, 1),
                "spell": (30.7, 33.3, 1),
            }
        },
    }

    def timer_thread(self):
        """
        定时线程, 脚本运行时定时执行thread_pool中的方法。
        一般用来战斗中检测数值变化, 采取响应动作
        """
        printf("[DeMo] init_thread")
        self.thread_pool = [
            (self.func1, 0.5),      # 每0.2秒执行一次 self.func1
        ]

    def func1(self):
        status = self.game.get_status
        hp = status["hp"]
        if hp <= 1000:
            #do something
            pass

    def run_once(self):
        """
        该函数在Script生命周期中只运行一次,负责处理参数
        """
        try:
            self.action = self.script_args[1]
        except:
            self.action = "cut"
        finally:
            if self.action in self.parameter["Object Type"]["data"]:
                self.delay_time_min, self.delay_time_max, _ = self.parameter["Object Type"]["data"][self.action]
            else:
                printf(f"[Demo] 参数错误 {self.action}")
                self.quit_status = f"参数错误"
                self.to_failed()

    # job1失败后由该修饰器负责重试
    @Script.retry("wait", wait_time=1)
    def job1(self):
        """
        job1是第一个动作函数
        """
        # 鼠标随机移动
        self.game.device.mouse_random()

        # 目标1
        Image1 = Image(
                        "game/bank_chest/fort_forinthry",   # 目标物体截图目录
                        x_offset=(-10, 10, 3),              # x轴随机偏移从-10到10, 小数点后保留3位
                        y_offset=(-10, 10, 3),              # y轴随机偏移从-10到10, 小数点后保留3位
                        class_id=3,                         # yolo模型的物体id
        )

        # 目标2
        Image2 = Image("game/menu/use_bank")

        # 右键点Image1,左键点Image2
        result = self.game.device.right_left_click(
                        Image1,
                        Image2,
                        threshold=0.85,                     # 精度阈值
                        engine="yolov8"                     # 使用yolov8模型, 不指定参数则使用opencv
        )
        return result

    def job2(self):
        """
        job2是第二个动作函数
        """
        # 目标1
        Image1 = Image("game/bank_chest/fort_forinthry")
        # 目标2
        Image2 = Image("game/menu/use_bank")
        # 左键点Image1,左键点Image2
        return self.game.device.left_left_click(Image1, Image2)
部署
  • 脚本

    将脚本放入scripts\rs3目录下即可. 如果是osrs脚本则放入对应目录

  • 图像

    脚本中使用的图像放到modules\images\rs3\macos\1x

    • rs3 是游戏名

    • macos 是操作系统, 如果是全平台脚本则需要放入多个copy

    • 1x 是dpi 1 模式,通常要准备dpi 1和dpi2模式的图像

运行

python bot.pyc -b demo

事件脚本开发

事件脚本是基于系统消息触发的各种事件处理。 比如: Grace of the elves、Brooch of the Gods、满包提示、异常状态等。

Blessing.py

from ZBot import Script
from ZBot import Image
from ZBot.utils import printf, sleep, create_random


class RandomEvent(Script):
    is_return_app = True    # 运行结束后是否激活指定App
    retry_count = 6         # 执行失败后尝试的次数
    states = [
        {'name': "wait_for_sleep", 'on_enter': "wait_for_sleep"},
        {'name': "clean_blessing", 'on_enter': "clean_blessing"},
        'failed', 'done', 'initial'
    ]

    def wait_for_sleep(self):
        """
        等待几秒, 模仿玩家行为
        """
        printf("[Event] Finding Blessing")
        sleep(create_random(1.1, 3.5, 2))

    @Script.retry("wait", wait_time=0.9)
    def clean_blessing(self):
        # 右键点击Blessing, 左键点击Capture divine blessing菜单
        result = self.game.device.right_left_click(
            Image("blessing.png", class_id=1),
            Image("capture_divine_blessing.png"),
            human=False,
            engine="yolov8"
        )
        if result:
            printf("[Event] Fuck Blessing")
        else:
            printf("[Event] Not found Blessing")
        # 随机移动鼠标
        self.game.device.mouse_random(human=False)
        return result
部署
  • 脚本

    将脚本放入scripts\rs3\event目录下即可. 如果是osrs脚本则放入对应目录

  • 图像

    与挂机脚本相同

  • 设置触发条件

    修改ZBot\game\event_rs3.conf文件. 增加一行

    A blessing from the gods appears:Blessing

    当系统消息包含 A blessing from the gods appears 时,执行Blessing脚本

运行

事件脚本由系统消息触发运行, 无需人为干预