传奇复古三职业自动化脚本主程序整合与状态机核心代码实现

来源: 作者: 点击:
将战士、法师、道士的专属逻辑整合为单一主程序,核心在于引入“职业识别”与“状态机”机制。通过读取角色面板信息,脚本在初始化阶段动态加载对应的战斗循环与键位配置,实现一套代码兼容三职业。

一、 主程序架构与全局变量

主程序采用模块化设计,分为初始化、职业识别、主循环与异常处理四个核心模块。

-- 全局配置与状态机
local CURRENT_JOB = nil -- 1:战士 2:法师 3:道士
local IS_RUNNING = true
local SCRIPT_VERSION = "1.0.4_Final"

-- 职业专属键位表
local KEY_BINDS = {
[1] = {ATTACK="F1", SKILL2="F2", SKILL3="F3", SKILL4="F4", SKILL5="F5"}, -- 战士
[2] = {ATTACK="F1", SKILL2="F2", SKILL3="F3", SKILL4="F4", SKILL5="F5"}, -- 法师
[3] = {ATTACK="F1", SKILL2="F2", SKILL3="F3", SKILL4="F4", SKILL5="F5"} -- 道士
}


二、 初始化与职业自动识别

脚本启动时,通过识别血条颜色、职业图标或特定UI元素,自动判定当前角色职业,并加载对应配置。

function Initialize()
PrintLog("正在初始化脚本环境...")

-- 1. 基础环境检测
if not CheckGameWindow() then
PrintLog("未找到游戏窗口,请确认游戏已启动!")
IS_RUNNING = false
return
end

-- 2. 职业识别 (通过识别角色头像旁的职业图标色值)
local jobColor = GetPixelColor(50, 50) -- 假设职业图标坐标
if jobColor == "C0C0C0" then
CURRENT_JOB = 1 -- 战士
PrintLog("识别职业:战士")
elseif jobColor == "0000FF" then
CURRENT_JOB = 2 -- 法师
PrintLog("识别职业:法师")
elseif jobColor == "00FF00" then
CURRENT_JOB = 3 -- 道士
PrintLog("识别职业:道士")
else
PrintLog("无法识别职业,请手动配置!")
IS_RUNNING = false
end

-- 3. 加载专属键位
if CURRENT_JOB then
local binds = KEY_BINDS[CURRENT_JOB]
InitHotkeys(binds) -- 绑定快捷键
end
end


三、 统一战斗主循环

主循环根据CURRENT_JOB的值,将控制权分发给对应的职业战斗函数,并统一处理公共逻辑(如喝药、拾取、回城)。

function MainLoop()
while IS_RUNNING do
-- 1. 全局状态监控 (断网/掉线检测)
if CheckDisconnect() then
HandleReconnect()
goto continue
end

-- 2. 全局补给逻辑 (血蓝低于阈值自动喝药)
GlobalPotionCheck()

-- 3. 背包满检测与自动回城
if CheckBagFull() then
AutoReturnCity()
goto continue
end

-- 4. 职业分发逻辑
local target = FindTarget()
if target then
if CURRENT_JOB == 1 then
WarriorCombat(target)
elseif CURRENT_JOB == 2 then
MageCombat(target)
elseif CURRENT_JOB == 3 then
TaoistCombat(target)
end
else
-- 无怪时随机游走防卡位
RandomWalk()
end

-- 5. 自动拾取逻辑
AutoPickup()

::continue::
Delay(100) -- 主循环心跳延迟
end
end


四、 职业战斗子程序整合

将前文生成的战士、法师、道士逻辑封装为独立函数,供主循环调用。

-- 战士战斗逻辑
function WarriorCombat(target)
local binds = KEY_BINDS[CURRENT_JOB]
if IsSkillReady(binds.ATTACK) then -- 烈火
PressKey(binds.ATTACK)
Delay(300)
ClickTarget(target.x, target.y)
elseif IsInAttackRange(target, 2) then
PressKey(binds.SKILL4) -- 刺杀
ClickTarget(target.x, target.y)
else
PressKey(binds.SKILL2) -- 逐日
ClickTarget(target.x, target.y)
end
end

-- 法师战斗逻辑
function MageCombat(target)
local binds = KEY_BINDS[CURRENT_JOB]
if not HasBuff("MagicShield") then PressKey(binds.SKILL4) end -- 魔法盾

local mob_count = CountMobsInRange(300)
if mob_count > 3 then
PressKey(binds.SKILL3) -- 火墙
ClickTarget(target.x, target.y)
Delay(800)
PressKey(binds.ATTACK) -- 冰咆哮
ClickTarget(target.x, target.y)
else
PressKey(binds.SKILL2) -- 雷电
ClickTarget(target.x, target.y)
end
end

-- 道士战斗逻辑
function TaoistCombat(target)
local binds = KEY_BINDS[CURRENT_JOB]
if not IsPetAlive() then
PressKey(binds.SKILL4) -- 召唤神兽
Delay(2000)
PetAttack(target)
end

if not HasDebuff(target, "Poison") then
PressKey(binds.SKILL2) -- 施毒
ClickTarget(target.x, target.y)
Delay(600)
end

if IsInAttackRange(target, 5) then
PressKey(binds.ATTACK) -- 灵魂火符
ClickTarget(target.x, target.y)
end
end


五、 异常处理与防卡死机制

在脚本顶层加入全局异常捕获,防止因单次操作失败导致整个进程崩溃。

-- 脚本入口点
function OnStart()
-- 开启全局异常捕获
local status, err = pcall(function()
Initialize()
if IS_RUNNING then
MainLoop()
end
end)

-- 异常处理
if not status then
PrintLog("脚本发生致命错误: " .. tostring(err))
PrintLog("正在尝试自动重启...")
Delay(5000)
RestartScript()
end
end

-- 启动脚本
OnStart()


六、 部署与运行说明

环境依赖:确保脚本运行环境已安装对应的图像识别库(如OpenCV)与模拟按键驱动。
坐标校准:首次运行时,需使用辅助工具中的“取色器”与“坐标拾取器”,重新校准职业识别坐标、血条坐标及背包满图标坐标。
权限要求:在Windows系统下,需以“管理员身份”运行脚本宿主程序,否则可能因权限不足导致按键失效。
日志监控:脚本运行期间会生成log.txt,记录每次职业切换、异常断线与回城操作,便于后期排查卡死原因。