|
本帖最后由 loveqiao361 于 2021-5-20 13:56 编辑
【难点】主线程(即入口脚本D2BotLead.dbj)中的login(me.profile)语句在执行时,主线程无法被暂停/停止(pause()/stop())。
【解决方案】由于一直到大厅(Lobby)时才能暂停主线程,则到大厅之后,暂停主线程,创建一个新的副线程,用以退出到选择人物界面改变国度,最后使用副线程来恢复主线程。
【实行】
1.编写副线程脚本(tools/ChangeRealm.js)
/**
* @filename ChangeRealm.js
* @author Black phoen1x
* @desc Thread for changing realm
*/
function main() {
include("json2.js");
include("OOG.js");
include("common/misc.js");
D2Bot.init(); //初始化D2Bot#
var needChangeRealm = true, //是否需要改变国度 一开始需要所以设置为true
targetRealm = 2; //目标国度 第2个国度
/*
函数 恢复主线程
*/
this.dbjResume = function () {
var i, script,
scripts = ["D2BotLead.dbj", "D2BotFollow.dbj", "D2BotMule.dbj"];
for (i = 0; i < scripts.length; i += 1) {
script = getScript(scripts[i]);
if (script) {
if (!script.running) {
if (i === 0) {
print("Resuming.");
}
script.resume();
}
}
}
};
/*
函数 退出到选择人物界面改变国度
*/
this.changeRealm = function (realm) {
var control, controlGet = false;
D2Bot.updateStatus("Changing Realm"); //在D2Bot#显示状态Changing Realm
ControlAction.click(6, 693, 490, 80, 20); //点击quit
while (!getLocation() === 12) { //等待进入选择人物界面
delay(50);
}
while (!controlGet) { ////等待直到获取Change Realm按钮
control = getControl(6, 609, 113, 182, 30);
if (control) {
controlGet = true;
}
delay(50);
}
control.click(); //点击Change Realm按钮
while (!getLocation() === 43) { //等待进入选择国度界面
delay(50);
}
ControlAction.click(4, 461, 230, 320, 70, 621, 245 + 30 * (realm - 1)); //点击国度标签
ControlAction.click(6, 495, 438, 96, 32); //点击OK按钮
needChangeRealm = false; //已更改完毕,不需要改变国度
}
while (true) {
if (getLocation() === 1) { //当进入lobby后
if (needChangeRealm) { //如果需要改变国度
scriptBroadcast("pause"); //暂停主线程
this.changeRealm(targetRealm); //退出到选择人物界面改变国度
this.dbjResume(); //恢复主线程
return true; //结束线程 减少功耗
}
}
delay(500);
}
}
2.修改主线程脚本(D2BotLead.dbj)
/*
函数 暂停主线程
*/
function dbjPause() {
var i, script,
scripts = ["D2BotLead.dbj", "D2BotFollow.dbj", "D2BotMule.dbj"];
for (i = 0; i < scripts.length; i += 1) {
script = getScript(scripts);
if (script) {
if (script.running) {
if (i === 0) {
print("Pausing.");
}
script.pause();
}
}
}
}
function ScriptMsgEvent(msg) {
switch (msg) {
case "mule":
AutoMule.check = true;
break;
case "muleTorch":
AutoMule.torchAnniCheck = 1;
break;
case "muleAnni":
AutoMule.torchAnniCheck = 2;
break;
case "torch":
TorchSystem.check = true;
break;
case "crafting":
CraftingSystem.check = true;
break;
case "getMuleMode":
if (AutoMule.torchAnniCheck === 2) {
scriptBroadcast("2");
} else if (AutoMule.torchAnniCheck === 1) {
scriptBroadcast("1");
} else if (AutoMule.check) {
scriptBroadcast("0");
}
break;
case "pingquit":
pingQuit = true;
break;
case "pause": //事件广播脚本消息"pause"发生时
dbjPause(); //暂停主线程
break;
}
}
/*
函数 主线程入口
*/
function main() {
debugLog(me.profile);
addEventListener('copydata', ReceiveCopyData);
addEventListener('scriptmsg', ScriptMsgEvent);
while (!handle) {
delay(100);
}
DataFile.updateStats("handle", handle);
delay(500);
D2Bot.init();
load("tools/heartbeat.js");
load("tools/ChangeRealm.js"); //载入副线程
|
|