BotRunner.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. using CommonAI.Zone;
  2. using CommonAI.Zone.Helper;
  3. using CommonAI.Zone.ZoneEditor;
  4. using CommonAI.ZoneClient.Agent;
  5. using CommonLang;
  6. using CommonLang.Concurrent;
  7. using CommonLang.Log;
  8. using CommonLang.Property;
  9. using CommonLang.Vector;
  10. using pomelo.area;
  11. using pomelo.chat;
  12. using pomelo.connector;
  13. using Pomelo.DotNetClient;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.IO;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading;
  20. using System.Windows.Forms;
  21. using XmdsBattleClient.Client;
  22. using XmdsBattleClientBot.Bot;
  23. using XmdsBattleClientBot.XmdsBot;
  24. namespace XmdsBotTest.Runner
  25. {
  26. public class BotRunner : IDisposable
  27. {
  28. protected readonly ListLogger events;
  29. protected readonly BotClient bot;
  30. protected readonly AddBotConfig add;
  31. protected readonly int index;
  32. protected XmdsNetClient client
  33. {
  34. get { return bot.Client; }
  35. }
  36. protected readonly AtomicReference<string> status = new AtomicReference<string>("");
  37. private HashMap<Type, RunnerModule> modules = new HashMap<Type, RunnerModule>();
  38. public BotClient Bot { get { return bot; } }
  39. public string RoleName { get { return bot.RoleName; } }
  40. public string SceneName { get { return bot.SceneName; } }
  41. public Pomelo.DotNetClient.NetWorkState NetState { get { return bot.NetState; } }
  42. public string Status { get { return status.Value; } }
  43. public BotRunner() {
  44. }
  45. public BotRunner(BotClient bot, int index, AddBotConfig add)
  46. {
  47. this.events = new ListLogger("event bot[" + bot.Account + "]");
  48. this.add = add;
  49. this.index = index;
  50. this.bot = bot;
  51. this.bot.event_OnStart += on_start;
  52. this.bot.event_OnStop += on_stop;
  53. this.bot.event_OnUpdate += on_update;
  54. this.bot.event_OnBattleUpdate += on_bs_update;
  55. this.bot.Client.GameSocket.IsSaveResponse = true;
  56. this.bot.Client.OnBeginEnterScene += on_gs_begin_enter_scene;
  57. this.bot.Client.OnBattlePlayerReady += on_bs_actor_ready;
  58. this.bot.Client.GameSocket.NetWorkStateChangedEvent += GameSocket_NetWorkStateChangedEvent;
  59. //this.bot.Client.LoginHandler.AddIpMapping(bot.Config.GetIpMapping());
  60. foreach (var mt in RunnerModule.GetSubTypes())
  61. {
  62. RunnerModule _module = ReflectionUtil.CreateInstance(mt, this) as RunnerModule;
  63. modules.Add(mt, _module);
  64. }
  65. }
  66. public void Start()
  67. {
  68. bot.Start();
  69. }
  70. public void Stop()
  71. {
  72. bot.Stop();
  73. }
  74. public void Dispose()
  75. {
  76. bot.Dispose();
  77. modules.Clear();
  78. }
  79. public override string ToString()
  80. {
  81. return bot.Account;
  82. }
  83. public void PopLogs(List<string> list)
  84. {
  85. events.PopLogs(list);
  86. }
  87. //--------------------------------------------------------------------------------------------------------
  88. private void test_entry(EntryRequest request, Action<PomeloException, EntryResponse> cb, object option)
  89. {
  90. // var _socket = client.GateSocket;
  91. // MemoryStream ms = new MemoryStream();
  92. // _socket.Serializer.Serialize(ms, request);
  93. // var req = new byte[ms.Length];
  94. // Array.Copy(ms.GetBuffer(), req, ms.Length);
  95. // var route = EventTypes.GetRequestKey(typeof(EntryRequest));
  96. // _socket.onRequestStart(route, option);
  97. // _socket.request_binary(route, req, (err, rsp) =>
  98. // {
  99. // MemoryStream rs = new MemoryStream(rsp);
  100. // var rsp_type = EventTypes.GetResponseType(route);
  101. // var response = _socket.Serializer.Deserialize(rs, null, rsp_type);
  102. // _socket.onRequestEnd(route, err, response, option);
  103. // cb(err, response as EntryResponse);
  104. // });
  105. }
  106. private void GameSocket_NetWorkStateChangedEvent(NetWorkState obj)
  107. {
  108. //if (obj == NetWorkState.DISCONNECTED)
  109. //{
  110. // new Thread(() =>
  111. // {
  112. // Thread.Sleep(5000);
  113. // events.Info("waiting 5 sec reconnect...");
  114. // //if (!bot.Client.GameSocket.IsConnected)
  115. // {
  116. // reconnect();
  117. // }
  118. // }).Start();
  119. //}
  120. }
  121. public void reconnect()
  122. {
  123. new Thread(() =>
  124. {
  125. bot.QueueTask(() =>
  126. {
  127. bot.Stop();
  128. });
  129. int wait = bot.Random.Next(100, 3000);
  130. events.Info("waiting " + wait + "ms...");
  131. Thread.Sleep(wait);
  132. bot.QueueTask(() =>
  133. {
  134. bot.connect(()=> {
  135. on_start(bot);
  136. });
  137. });
  138. }).Start();
  139. }
  140. public void connect()
  141. {
  142. if (bot.NetState != NetWorkState.CONNECTED) {
  143. status.Value = "重连中....";
  144. new Thread(() =>
  145. {
  146. bot.QueueTask(() =>
  147. {
  148. bot.Stop();
  149. });
  150. bot.QueueTask(() =>
  151. {
  152. bot.connect(() =>
  153. {
  154. status.Value = "连接成功...";
  155. on_start(bot);
  156. });
  157. });
  158. }).Start();
  159. } else
  160. {
  161. MessageBox.Show("该客户端已经连接,无需再连");
  162. }
  163. }
  164. /// <summary>
  165. /// 离开副本
  166. /// </summary>
  167. public void leaveScene()
  168. {
  169. bot.Client.GameSocket.fightLevelHandler.leaveDungeonRequest((err, res)=>{
  170. if(err != null)
  171. {
  172. MessageBox.Show("该场景不是副本,无法离开");
  173. }
  174. });
  175. }
  176. //--------------------------------------------------------------------------------------------------------
  177. protected virtual void on_start(BotClient obj)
  178. {
  179. events.Info("Bot started");
  180. obj.login(start_enter_server, (err)=>
  181. {
  182. obj.register(start_enter_server, on_error);
  183. });
  184. }
  185. protected virtual void on_stop(BotClient obj)
  186. {
  187. events.Info("Bot stoped");
  188. }
  189. protected virtual void on_update(BotClient obj)
  190. {
  191. foreach (var m in modules.Values) { m.OnUpdate(); }
  192. }
  193. protected virtual void on_bs_update(CommonAI.ZoneClient.ZoneActor obj)
  194. {
  195. }
  196. protected virtual void on_error(Exception err)
  197. {
  198. events.Error(err.Message, err);
  199. //将错误信息显示到当前状态
  200. status.Value = err.Message;
  201. }
  202. public virtual void log_response(string info, Exception err, dynamic r)
  203. {
  204. if (err != null)
  205. events.Error(info + " : " + err.Message);
  206. else if (r != null)
  207. events.Log(info + " : " + r.s2c_code + " : " + r.s2c_msg);
  208. }
  209. public CommonAI.Data.XmdsUnitPro LastSelectUnitPro { get; private set; }
  210. protected virtual void start_enter_server()
  211. {
  212. bot.gate_EnterServer(on_gate_enter_server, (err) =>
  213. {
  214. events.Error(err.Message);
  215. if (err is PomeloException)
  216. {
  217. var pe = err as PomeloException;
  218. if (pe.Code == 500)
  219. {
  220. reconnect();
  221. return;
  222. }
  223. }
  224. });
  225. }
  226. protected virtual void start_re_enter_server()
  227. {
  228. events.Log("gate re enter server");
  229. bot.gate_ReEnterServer((e)=>
  230. {
  231. if (e.s2c_players.Count > 0)
  232. {
  233. events.Log("gate bind player");
  234. bot.gate_BindPlayer(e.s2c_players[0].id, on_gate_bind_player, on_error);
  235. }
  236. },
  237. (err) =>
  238. {
  239. events.Error(err.Message);
  240. if (err is PomeloException)
  241. {
  242. var pe = err as PomeloException;
  243. if (pe.Code == 500)
  244. {
  245. reconnect();
  246. return;
  247. }
  248. }
  249. });
  250. }
  251. protected virtual void on_gate_enter_server(EntryResponse e)
  252. {
  253. events.Log("gate enter server done : player count = " + e.s2c_players.Count);
  254. bot.QueueTask((System.Action)(() =>
  255. {
  256. if (e.s2c_players.Count > 0)
  257. {
  258. events.Log("gate bind player");
  259. bot.gate_BindPlayer(e.s2c_players[0].id, this.on_gate_bind_player, this.on_error);
  260. }
  261. else
  262. {
  263. //this.LastSelectUnitPro = global::CommonAI.Data.XmdsUnitPro.None;
  264. this.LastSelectUnitPro = CUtils.RandomEnumValue<global::CommonAI.Data.XmdsUnitPro>(bot.Random);
  265. if (LastSelectUnitPro == global::CommonAI.Data.XmdsUnitPro.None)
  266. {
  267. LastSelectUnitPro = global::CommonAI.Data.XmdsUnitPro.Sword;
  268. }
  269. string name = CUtils.GetRandomInArray(add.RoleNameFormat, bot.Random);
  270. if (add.RandomRoleName || name == null)
  271. {
  272. events.Log("gate get random name");
  273. bot.gate_GetRandomName((int)LastSelectUnitPro, this.on_gate_get_random_name, this.on_error);
  274. }
  275. else
  276. {
  277. if (name.Contains("{0}"))
  278. name = string.Format(name, index.ToString(add.digit_format));
  279. events.Log("gate create role : " + name);
  280. bot.gate_CreateRole((int)LastSelectUnitPro, name, this.on_gate_create_role, (err) =>
  281. {
  282. on_error(err);
  283. events.Log("gate get random name");
  284. bot.gate_GetRandomName((int)LastSelectUnitPro, this.on_gate_get_random_name, this.on_error);
  285. });
  286. }
  287. }
  288. }));
  289. }
  290. protected virtual void on_gate_get_random_name(GetRandomNameResponse e)
  291. {
  292. events.Log("gate get random name done");
  293. bot.QueueTask(() =>
  294. {
  295. if (string.IsNullOrEmpty(e.s2c_name))
  296. {
  297. events.Log("gate get random name");
  298. bot.gate_GetRandomName((int)LastSelectUnitPro, on_gate_get_random_name, on_error);
  299. }
  300. else
  301. {
  302. events.Log("gate create role : " + e.s2c_name);
  303. bot.gate_CreateRole((int)LastSelectUnitPro, e.s2c_name, on_gate_create_role, (err) =>
  304. {
  305. on_error(err);
  306. events.Log("gate get random name");
  307. bot.gate_GetRandomName((int)LastSelectUnitPro, on_gate_get_random_name, on_error);
  308. });
  309. }
  310. });
  311. }
  312. protected virtual void on_gate_create_role(CreatePlayerResponse e)
  313. {
  314. events.Log("gate create role done : " + e.s2c_player.name);
  315. bot.QueueTask(() =>
  316. {
  317. events.Log("gate bind player : " + e.s2c_player.name);
  318. bot.gate_BindPlayer(e.s2c_player.id, on_gate_bind_player, on_error);
  319. });
  320. }
  321. protected virtual void on_gate_bind_player(BindPlayerResponse e)
  322. {
  323. events.Log("gate bind player done : " + e.s2c_player.name);
  324. foreach (var m in modules.Values) { m.OnGateBindPlayer(e); }
  325. }
  326. //--------------------------------------------------------------------------------------------------------
  327. protected virtual void on_gs_begin_enter_scene(XmdsBattleClient.Battle.XmdsBattleClient obj)
  328. {
  329. this.CurrentMoveAgent = null;
  330. this.CurrentRandomMoveTarget = null;
  331. events.Log("gs begin enter scene done");
  332. if (bot.Client.BattleClient != null)
  333. {
  334. bot.Client.BattleClient.Layer.ActorAdded += on_bs_actor_added;
  335. }
  336. bot.gs_EnterScene(on_gs_enter_scene, on_error);
  337. }
  338. protected virtual void on_gs_enter_scene(EnterSceneResponse response)
  339. {
  340. events.Log("bs enter scene done : " + response.s2c_instanceId);
  341. bot.LineIndex = response.s2c_areaIndex;
  342. bot.SendUnitGuard(true);
  343. foreach (var m in modules.Values) { m.OnGameEnterScene(response); }
  344. }
  345. private void on_bs_actor_ready(CommonAI.ZoneClient.ZoneLayer layer, CommonAI.ZoneClient.ZoneActor actor)
  346. {
  347. foreach (var m in modules.Values)
  348. {
  349. m.OnBattleActorReady(layer, actor);
  350. }
  351. }
  352. private void on_bs_actor_added(CommonAI.ZoneClient.ZoneLayer layer, CommonAI.ZoneClient.ZoneActor actor)
  353. {
  354. actor.OnDoEvent += on_bs_actor_do_event;
  355. layer.AddTimePeriodicMS(5000, (t) =>
  356. {
  357. check_move();
  358. });
  359. }
  360. private void on_bs_actor_do_event(CommonAI.ZoneClient.ZoneObject obj, CommonAI.Zone.ObjectEvent e)
  361. {
  362. if (e is UnitDeadEvent)
  363. {
  364. if (e.object_id == bot.CurrentZoneActor.ObjectID)
  365. {
  366. on_actor_dead(bot.CurrentZoneActor, e as UnitDeadEvent);
  367. }
  368. }
  369. }
  370. private void on_actor_dead(CommonAI.ZoneClient.ZoneActor actor, UnitDeadEvent e)
  371. {
  372. do_relive(2);
  373. foreach (var m in modules.Values) { m.OnBattleActorDead(actor, e); }
  374. }
  375. //--------------------------------------------------------------------------------------------------------
  376. public virtual void do_relive(int type)
  377. {
  378. bot.Client.GameSocket.playerHandler.reliveRequest(type, (e, r) =>
  379. {
  380. log_response("reliveRequest done : ", e, r);
  381. });
  382. }
  383. //--------------------------------------------------------------------------------------------------------
  384. #region GM
  385. private void sendCMD(string cmd, Action<PomeloException, SendChatResponse> cb)
  386. {
  387. //bot.Client.GameSocket.chatHandler.sendChatRequest(1, cmd, "", "", cb);
  388. }
  389. public virtual void do_gm_add_exp(int exp)
  390. {
  391. sendCMD("@gm add exp " + exp, (e, r) =>
  392. {
  393. log_response("do_gm_add_exp done : ", e, r);
  394. });
  395. }
  396. public virtual void do_gm_add_upexp(int exp)
  397. {
  398. sendCMD("@gm add upexp " + exp, (e, r) =>
  399. {
  400. log_response("do_gm_add_upexp done : ", e, r);
  401. });
  402. }
  403. public virtual void do_gm_add_diamond(int exp)
  404. {
  405. sendCMD("@gm add diamond " + exp, (e, r) =>
  406. {
  407. log_response("do_gm_add_diamond done : ", e, r);
  408. });
  409. }
  410. public virtual void do_gm_add_gold(int exp)
  411. {
  412. sendCMD("@gm add gold " + exp, (e, r) =>
  413. {
  414. log_response("do_gm_add_gold done : ", e, r);
  415. });
  416. }
  417. public virtual void do_gm_finish_task(int taskTemplateID)
  418. {
  419. sendCMD("@gm finishTask " + taskTemplateID, (e, r) =>
  420. {
  421. log_response("do_gm_finish_task done : ", e, r);
  422. });
  423. }
  424. public virtual void do_gm_unlock_bag(int num)
  425. {
  426. sendCMD("@gm unlock " + num, (e, r) =>
  427. {
  428. log_response("do_gm_unlock_bag done : ", e, r);
  429. });
  430. }
  431. /************************************************************************/
  432. /* 模拟充值成功回调 */
  433. /************************************************************************/
  434. public virtual void do_gm_pay_success(String orderId)
  435. {
  436. sendCMD("@gm paySuccess " + orderId, (e, r) =>
  437. {
  438. log_response("do_gm_pay_success done : ", e, r);
  439. });
  440. }
  441. public virtual void do_gm_open_func()
  442. {
  443. sendCMD("@gm openFunc", (e, r) =>
  444. {
  445. log_response("do_gm_open_func done : ", e, r);
  446. });
  447. }
  448. public virtual void do_gm_add_skin() {
  449. sendCMD("@gm allSkin",(e,r) => {
  450. log_response("do_gm_add_skin done : ", e, r);
  451. });
  452. }
  453. public virtual void do_gm_add_item(string code, int num)
  454. {
  455. sendCMD("@gm add " + code + " " + num, (e, r) =>
  456. {
  457. log_response("do_gm_add_item done : ", e, r);
  458. });
  459. }
  460. public virtual void do_gm_add_item()
  461. {
  462. //sendCMD("@gm add lclot1-test 1", (e, r) =>
  463. //{
  464. // log_response("do_gm_add_item done : ", e, r);
  465. //});
  466. //sendCMD("@gm add lweap1-test 1", (e, r) =>
  467. //{
  468. // log_response("do_gm_add_item done : ", e, r);
  469. //});
  470. }
  471. #endregion
  472. //--------------------------------------------------------------------------------------------------------
  473. #region Task
  474. public virtual BotClient.QuestData do_seek_task(int questID, BotClient.QuestData.SeekAction action)
  475. {
  476. var quest = bot.CurrentQuestManager.GetQuest(questID);
  477. if (quest != null)
  478. {
  479. events.Log("do_seek_task : " + quest);
  480. quest.Seek(action);
  481. }
  482. return quest;
  483. }
  484. public virtual BotClient.QuestData do_seek_expect_task(BotClient.QuestData.QuestStatus expect, BotClient.QuestData.SeekAction action, bool random = false)
  485. {
  486. BotClient.QuestData q = null;
  487. if ((q = bot.CurrentQuestManager.SeekExpectTask(BotClient.QuestData.QuestType.TRUNK, expect, action, random)) != null)
  488. {
  489. events.Log("do_seek_expect_task : " + q);
  490. return q;
  491. }
  492. if ((q = bot.CurrentQuestManager.SeekExpectTask(BotClient.QuestData.QuestType.INVALID, expect, action, random)) != null)
  493. {
  494. events.Log("do_seek_expect_task : " + q);
  495. return q;
  496. }
  497. return q;
  498. }
  499. public virtual BotClient.QuestData do_seek_random_task(BotClient.QuestData.SeekAction action, bool random = false)
  500. {
  501. BotClient.QuestData q = null;
  502. if ((q = bot.CurrentQuestManager.SeekExpectTask(BotClient.QuestData.QuestType.TRUNK, BotClient.QuestData.QuestStatus.CAN_FINISH, action, random)) != null)
  503. {
  504. events.Log("do_seek_random_task : " + q);
  505. return q;
  506. }
  507. if ((q = bot.CurrentQuestManager.SeekExpectTask(BotClient.QuestData.QuestType.TRUNK, BotClient.QuestData.QuestStatus.IN_PROGRESS, action, random)) != null)
  508. {
  509. events.Log("do_seek_random_task : " + q);
  510. return q;
  511. }
  512. if ((q = bot.CurrentQuestManager.SeekExpectTask(BotClient.QuestData.QuestType.TRUNK, BotClient.QuestData.QuestStatus.NEW, action, random)) != null)
  513. {
  514. events.Log("do_seek_random_task : " + q);
  515. return q;
  516. }
  517. if ((q = bot.CurrentQuestManager.SeekExpectTask(BotClient.QuestData.QuestType.INVALID, BotClient.QuestData.QuestStatus.CAN_FINISH, action, random)) != null)
  518. {
  519. events.Log("do_seek_random_task : " + q);
  520. return q;
  521. }
  522. if ((q = bot.CurrentQuestManager.SeekExpectTask(BotClient.QuestData.QuestType.INVALID, BotClient.QuestData.QuestStatus.IN_PROGRESS, action, random)) != null)
  523. {
  524. events.Log("do_seek_random_task : " + q);
  525. return q;
  526. }
  527. if ((q = bot.CurrentQuestManager.SeekExpectTask(BotClient.QuestData.QuestType.INVALID, BotClient.QuestData.QuestStatus.NEW, action, random)) != null)
  528. {
  529. events.Log("do_seek_random_task : " + q);
  530. return q;
  531. }
  532. return q;
  533. }
  534. public virtual void do_update_quest_status(BotClient.QuestData quest)
  535. {
  536. events.Log("do_update_quest_status : " + quest + " : " + quest.State);
  537. bot.Client.GameSocket.taskHandler.updateTaskStatusRequest(quest.TemplateID, (int)quest.Kind, (err, msg) =>
  538. {
  539. if (err != null)
  540. events.Error("updateTaskStatusRequest error : " + err.Message);
  541. else
  542. events.Log("updateTaskStatusRequest done : " + msg.s2c_code + " : " + msg.s2c_msg);
  543. });
  544. }
  545. public virtual void do_accept_quest(BotClient.QuestData quest, Action<Exception> error = null)
  546. {
  547. if (client.BattleActor != null)
  548. {
  549. events.Log("do_accept_quest : " + quest + " : " + quest.State);
  550. client.GameSocket.taskHandler.acceptTaskRequest(quest.TemplateID, (int)quest.Kind, client.BattleActor.ObjectID.ToString(), (err, msg) =>
  551. {
  552. if (err != null)
  553. {
  554. events.Error("acceptTaskRequest error : " + err.Message);
  555. if (error != null) error(err);
  556. }
  557. else
  558. {
  559. events.Log("acceptTaskRequest done : " + msg.s2c_code + " : " + msg.s2c_msg);
  560. }
  561. });
  562. }
  563. }
  564. public virtual void do_submit_task(BotClient.QuestData quest, Action<Exception> error = null)
  565. {
  566. if (client.BattleActor != null)
  567. {
  568. events.Log("do_submit_task : " + quest + " : " + quest.State);
  569. client.GameSocket.taskHandler.submitTaskRequest(quest.TemplateID, (int)quest.Kind, 0, client.BattleActor.ObjectID.ToString(), (err, msg) =>
  570. {
  571. if (err != null)
  572. {
  573. events.Error("submitTaskRequest error : " + err.Message);
  574. if (error != null) error(err);
  575. }
  576. else
  577. {
  578. events.Log("submitTaskRequest done : " + msg.s2c_code + " : " + msg.s2c_msg);
  579. }
  580. });
  581. }
  582. }
  583. #endregion
  584. //--------------------------------------------------------------------------------------------------------
  585. #region Move
  586. public ActorMoveAgent CurrentMoveAgent { get; private set; }
  587. public SceneObjectData CurrentRandomMoveTarget { get; private set; }
  588. private Vector2 mLastPos = new Vector2();
  589. private void check_move()
  590. {
  591. float min_step = client.DataRoot.Templates.CFG.OBJECT_MOVE_TO_MIN_STEP_SEC;
  592. bool not_move = MathVector.getDistance(mLastPos.X, mLastPos.Y, bot.CurrentZoneActor.X, bot.CurrentZoneActor.Y) < min_step;
  593. mLastPos.SetX(bot.CurrentZoneActor.X);
  594. mLastPos.SetY(bot.CurrentZoneActor.Y);
  595. if (not_move)
  596. {
  597. CurrentRandomMoveTarget = null;
  598. if (CurrentMoveAgent != null)
  599. CurrentMoveAgent.Stop();
  600. CurrentMoveAgent = null;
  601. }
  602. }
  603. private SceneObjectData do_bs_actor_random_move_regions<R>(List<R> list, bool attack, float min_distance) where R : SceneObjectData
  604. {
  605. var player = bot.CurrentZoneActor;
  606. var layer = bot.CurrentZoneLayer;
  607. if (player != null && layer.IsLoaded)
  608. {
  609. try
  610. {
  611. bot.SendUnitGuard(attack);
  612. if (CurrentRandomMoveTarget == null ||
  613. MathVector.getDistance(player.X, player.Y, CurrentRandomMoveTarget.X, CurrentRandomMoveTarget.Y) <= min_distance ||
  614. layer.FindPath(player.X, player.Y, CurrentRandomMoveTarget.X, CurrentRandomMoveTarget.Y) == null)
  615. {
  616. CUtils.RandomList<R>(bot.Random, list);
  617. foreach (var rg in list)
  618. {
  619. if (player.Parent.FindPath(player.X, player.Y, rg.X, rg.Y) != null)
  620. {
  621. events.Log("do_bs_actor_random_move_region : " + rg.Name);
  622. if (!attack)
  623. {
  624. do_start_move_agent(rg.X, rg.Y, min_distance);
  625. }
  626. else
  627. {
  628. player.SendUnitAttackMoveTo(rg.X, rg.Y, attack);
  629. }
  630. CurrentRandomMoveTarget = rg;
  631. return rg;
  632. }
  633. }
  634. }
  635. else if (bot.CurrentZoneActor.CurrentState == UnitActionStatus.Idle)
  636. {
  637. if (!attack)
  638. {
  639. do_start_move_agent(CurrentRandomMoveTarget.X, CurrentRandomMoveTarget.Y, min_distance);
  640. }
  641. else
  642. {
  643. player.SendUnitAttackMoveTo(CurrentRandomMoveTarget.X, CurrentRandomMoveTarget.Y, attack);
  644. }
  645. return CurrentRandomMoveTarget;
  646. }
  647. }
  648. catch (Exception err)
  649. {
  650. events.Error(err.Message, err);
  651. }
  652. }
  653. return null;
  654. }
  655. public virtual SceneObjectData do_bs_actor_random_move_region(bool attack = true, float min_distance = 0.1f)
  656. {
  657. return do_bs_actor_random_move_regions<RegionData>(bot.CurrentRegionManager.AllRegions(), attack, min_distance);
  658. }
  659. public virtual SceneObjectData do_bs_actor_random_move_transport(bool attack = true, float min_distance = 0.1f)
  660. {
  661. return do_bs_actor_random_move_regions<SceneObjectData>(bot.CurrentRegionManager.AllTransports(), attack, min_distance);
  662. }
  663. public virtual ActorMoveAgent do_start_move_agent(float x, float y, float distance, bool force_trans = false)
  664. {
  665. if (CurrentMoveAgent != null)
  666. {
  667. CurrentMoveAgent.Stop();
  668. }
  669. CurrentMoveAgent = new ActorMoveAgent(x, y, distance);
  670. bot.CurrentZoneActor.AddAgent(CurrentMoveAgent);
  671. bot.SendUnitGuard(false);
  672. if (CurrentMoveAgent.Result == CommonAI.RTS.Manhattan.AstarManhattan.FindPathResult.Destination)
  673. {
  674. bot.CurrentRegionManager.CheckTrans(1, force_trans);
  675. }
  676. else if (CurrentMoveAgent.IsEnd)
  677. {
  678. bot.CurrentZoneActor.SendUnitAttackMoveTo(x, y, false);
  679. }
  680. return CurrentMoveAgent;
  681. }
  682. #endregion
  683. //--------------------------------------------------------------------------------------------------------
  684. #region Pick
  685. public virtual CommonAI.ZoneClient.ZoneItem do_try_pick_nearest_item(out Vector2 move_to)
  686. {
  687. move_to = null;
  688. if (bot.CurrentZoneActor.CurrentState != UnitActionStatus.Pick)
  689. {
  690. var item = bot.CurrentZoneLayer.GetNearPickableItem(bot.CurrentZoneActor);
  691. if (item != null)
  692. {
  693. events.Log("try pick nearest item : " + item.Info.Name);
  694. if (MathVector.getDistance(bot.CurrentZoneActor.X, bot.CurrentZoneActor.Y, item.X, item.Y) < item.RadiusSize)
  695. {
  696. bot.CurrentZoneActor.SendUnitPickObject(item.ObjectID);
  697. return item;
  698. }
  699. else
  700. {
  701. move_to = new Vector2(item.X, item.Y);
  702. }
  703. }
  704. }
  705. return null;
  706. }
  707. public virtual CommonAI.ZoneClient.ZoneItem do_start_pick_any_item()
  708. {
  709. if (bot.CurrentZoneActor.CurrentState != UnitActionStatus.Pick)
  710. {
  711. var item = bot.CurrentZoneLayer.GetNearPickableItem(bot.CurrentZoneActor);
  712. if (item != null)
  713. {
  714. events.Log("do pick item : " + item.Info.Name);
  715. bot.CurrentZoneActor.SendUnitPickObject(item.ObjectID);
  716. return item;
  717. }
  718. }
  719. return null;
  720. }
  721. #endregion
  722. #region BagAndEquip
  723. #endregion
  724. //--------------------------------------------------------------------------------------------------------
  725. public abstract class RunnerModule
  726. {
  727. public string module_name = "模块名";
  728. private static HashMap<Type, bool> s_SubModules = new HashMap<Type, bool>();
  729. public static List<Type> GetSubTypes()
  730. {
  731. var ret = new List<Type>();
  732. foreach (var mt in typeof(RunnerModule).Assembly.GetTypes())
  733. {
  734. if (mt.IsSubclassOf(typeof(RunnerModule)) && !mt.IsAbstract)
  735. {
  736. ret.Add(mt);
  737. }
  738. }
  739. return ret;
  740. }
  741. /// <summary>
  742. /// 获取类中的属性值
  743. /// </summary>
  744. /// <param name="FieldName"></param>
  745. /// <param name="obj"></param>
  746. /// <returns></returns>
  747. public string GetModelValue(string FieldName, object obj)
  748. {
  749. try
  750. {
  751. Type Ts = obj.GetType();
  752. object o = Ts.GetProperty(FieldName).GetValue(obj, null);
  753. string Value = Convert.ToString(o);
  754. if (string.IsNullOrEmpty(Value)) return null;
  755. return Value;
  756. }
  757. catch
  758. {
  759. return null;
  760. }
  761. }
  762. public static void SetModuleEnable(Type type, bool value)
  763. {
  764. s_SubModules[type] = value;
  765. }
  766. public static bool GetModuleEnable(Type type)
  767. {
  768. bool enable = false;
  769. if (s_SubModules.TryGetValue(type, out enable))
  770. {
  771. return enable;
  772. }
  773. return false;
  774. }
  775. static RunnerModule()
  776. {
  777. foreach (var mt in GetSubTypes())
  778. {
  779. s_SubModules.Add(mt, true);
  780. }
  781. }
  782. private bool m_Enable;
  783. protected BotClient bot { get; private set; }
  784. protected BotRunner runner { get; private set; }
  785. protected XmdsNetClient client { get; private set; }
  786. protected Logger log { get; private set; }
  787. public bool Enable { get { return m_Enable; } }
  788. public void autoJoinTeam()
  789. {
  790. if(IsCanJoin)
  791. {
  792. autoJoinTeamRequest();
  793. }
  794. }
  795. public virtual void autoJoinTeamRequest()
  796. {
  797. }
  798. protected virtual int rand()
  799. {
  800. return 20;
  801. }
  802. public bool IsCanJoin
  803. {
  804. get { return Enable && bot.IsFree && (runner.CurrentMoveAgent == null) && bot.Random.Next(100) < rand(); }
  805. }
  806. public RunnerModule() {
  807. }
  808. public RunnerModule(BotRunner r)
  809. {
  810. this.runner = r;
  811. this.bot = r.Bot;
  812. this.client = bot.Client;
  813. this.log = r.events;
  814. this.m_Enable = GetModuleEnable(GetType());
  815. }
  816. public void SetStatus(string st)
  817. {
  818. runner.status.Value = st;
  819. }
  820. internal void OnUpdate()
  821. {
  822. bool enable = GetModuleEnable(GetType());
  823. if (m_Enable != enable)
  824. {
  825. m_Enable = enable;
  826. OnEnableChanged(enable);
  827. }
  828. }
  829. protected virtual void OnEnableChanged(bool enable) { }
  830. protected internal virtual void OnGateBindPlayer(BindPlayerResponse e) { }
  831. protected internal virtual void OnGameEnterScene(EnterSceneResponse response) { }
  832. protected internal virtual void OnBattleActorReady(CommonAI.ZoneClient.ZoneLayer layer, CommonAI.ZoneClient.ZoneActor actor) { }
  833. protected internal virtual void OnBattleActorDead(CommonAI.ZoneClient.ZoneActor actor, UnitDeadEvent e) { }
  834. }
  835. }
  836. }