概述#

基于对 nBattle.cPlayer 字段的深度监控,我们成功识别了控制角色输入的关键字段,并实现了完整的 P2 控制系统。

核心发现#

关键字段#

通过 DeepP1Explorer.lua 的监控,我们发现了以下关键字段:

输入字段#

  • pl_input_new (UInt16): 新输入值,每帧更新
  • pl_input_old (UInt16): 上一帧的输入
  • pl_input_now (UInt16): 当前正在处理的输入
  • pl_input_old1 (UInt16): 上上帧的输入

按钮字段#

  • pl_sw_new (UInt16): 新按钮状态
  • pl_sw_old (UInt16): 旧按钮状态
  • pl_sw_now (UInt16): 当前按钮状态

动作状态字段#

  • act_st (nBattle.ACT_ST.ID): 当前动作状态
  • act_st_old: 之前的动作状态
  • act_dir (SByte): 动作方向 (-1, 0, 1)

输入值映射#

从监控数据中提取的输入值:

INPUT = {
    NONE = 0,
    UP = 1,
    DOWN = 2,
    RIGHT = 4,
    LEFT = 8,
    LP = 32,      -- 轻拳
    MP = 16,      -- 中拳
    HP = 64,      -- 重拳
    LK = 128,     -- 轻脚
    MK = 256,     -- 中脚
    HK = 512,     -- 重脚
}

按钮值映射#

BUTTON = {
    NONE = 0,
    RIGHT = 8,
    LEFT = 4,
    LP = 32,
    MP = 16,
}

文件说明#

1. P2InputController.lua#

基础控制器,提供核心功能:

  • init(): 初始化控制器,获取 P2 对象
  • setInput(value): 设置输入值
  • setButton(value): 设置按钮值
  • pressButton(direction, button, duration): 按下按钮
  • releaseAll(): 释放所有输入
  • readInputState(): 读取当前输入状态
  • monitorInput(duration): 监控输入变化

2. AdvancedP2Controller.lua#

高级控制器,继承基础控制器并添加:

  • executeMotion(motion, button): 执行特殊指令

    • QCF (236): 波动拳指令
    • DP (623): 升龙拳指令
    • QCB (214): 竜巻旋風脚指令
    • HCF (41236): 半圈指令
    • HCB (63214): 反半圈指令
    • SPD (360): 360度指令
  • executeCombo(combo_name): 执行预设连招

    • basic_chain: 基础连招
    • hadoken_combo: 波动拳连招
    • shoryuken_combo: 升龙拳连招
    • target_combo: 目标连招
  • defend(duration): 防御模式

  • jumpAttack(direction, button): 跳跃攻击

  • throw(): 投技

  • aiPattern(pattern, duration): AI 行为模式

    • aggressive: 激进模式
    • defensive: 防御模式
    • balanced: 平衡模式

3. test_p2_controller.lua#

基础测试脚本,包含 5 种测试模式:

  1. 基础输入测试
  2. 连续攻击测试
  3. 移动测试
  4. 输入监控
  5. 自定义序列

4. test_advanced_p2.lua#

综合测试脚本,包含 7 种测试模式:

  1. 基础攻击测试
  2. 特殊指令测试
  3. 连招测试
  4. 移动和跳跃测试
  5. 防御测试
  6. AI 行为模式测试
  7. 完整演示

使用方法#

基础使用#

local P2Controller = require("P2InputController")

-- 初始化
P2Controller:init()

-- 按下轻拳
P2Controller:pressButton(nil, "LP", 3)

-- 等待几帧
for i = 1, 10 do coroutine.yield() end

-- 释放输入
P2Controller:releaseAll()

高级使用#

local AdvancedP2 = require("AdvancedP2Controller")

-- 初始化
AdvancedP2:init()

-- 执行波动拳
AdvancedP2:executeMotion("QCF", "LP")

-- 执行连招
AdvancedP2:executeCombo("basic_chain")

-- 启动 AI 模式
AdvancedP2:aiPattern("aggressive", 600)

自定义动作序列#

local sequence = {
    {
        name = "轻拳",
        button = "LP",
        duration = 3,
        wait = 5,
        release = true
    },
    {
        name = "等待",
        wait = 20
    },
    {
        name = "波动拳",
        motion = "QCF",
        button = "LP",
        wait = 30
    }
}

P2Controller:executeSequence(sequence)

运行测试#

  1. 启动 SF6 并进入训练模式
  2. 在 REFramework 控制台中运行:
    dofile("D:\\projects\\sf6_260122\\lua_external\\test_advanced_p2.lua")
    
  3. 修改脚本中的 test_mode 变量选择不同测试

输入值组合#

可以通过位运算组合多个输入:

-- 前下 (波动拳第二步)
local input = P2Controller.INPUT.DOWN + P2Controller.INPUT.RIGHT

-- 跳跃 + 轻拳
local input = P2Controller.INPUT.UP + P2Controller.INPUT.LP

注意事项#

  1. 帧数控制: 每个动作需要持续足够的帧数才能被识别
  2. 输入时机: 连招需要精确的时机,建议使用 coroutine.yield() 控制
  3. 释放输入: 每次输入后记得调用 releaseAll()
  4. 监控模式: 使用 monitorInput() 可以观察输入变化,帮助调试

调试技巧#

查看当前状态#

local state = P2Controller:readInputState()
print("input_new:", state.input_new)
print("act_st:", state.act_st)

监控输入变化#

P2Controller:monitorInput(300)  -- 监控 300 帧

扩展开发#

添加新的特殊指令#

在 AdvancedP2Controller.lua 的 MOTION 表中添加:

MOTION.MY_MOTION = {
    {direction = P2Controller.INPUT.DOWN, frames = 2},
    {direction = P2Controller.INPUT.RIGHT, frames = 2},
    -- ... 更多步骤
}

添加新的连招#

在 executeCombo() 函数的 combos 表中添加:

my_combo = {
    {name = "轻拳", button = "LP", wait = 8},
    {name = "中拳", button = "MP", wait = 15},
    {name = "波动拳", motion = "QCF", button = "LP", wait = 30},
}

技术细节#

字段变化示例#

从监控日志中可以看到:

[CHANGE] pl_input_new: 0 -> 8 (diff: +8)      # 按下左键
[CHANGE] pl_sw_new: 0 -> 4 (diff: +4)         # 对应的按钮状态
[CHANGE] act_st: 0 -> 8 (diff: +8)            # 动作状态改变

输入处理流程#

  1. 设置 pl_input_new 和 pl_input_now
  2. 设置对应的 pl_sw_new 和 pl_sw_now
  3. 游戏引擎读取这些值
  4. 更新 act_st (动作状态)
  5. 执行相应的动作

已知问题#

  1. 某些复杂指令可能需要调整帧数
  2. 不同角色的连招时机可能不同
  3. 在线模式可能不工作(仅限训练模式)

未来改进#

  • 添加更多角色特定的连招
  • 实现更智能的 AI 行为
  • 添加反应式防御(根据 P1 动作自动防御)
  • 支持录制和回放输入序列
  • 添加随机变化避免重复

相关文件#

  • DeepP1Explorer.lua: 字段监控工具(用于研究)
  • result.md: 监控结果(包含所有字段变化)
  • docs/研究让1p和2p进行动作/: 研究文档目录

贡献#

如果发现新的有用字段或改进方法,请更新此文档。