ModuleMasterTask.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using CommonAI.Zone;
  2. using CommonAI.Zone.Helper;
  3. using CommonAI.Zone.ZoneEditor;
  4. using CommonAI.ZoneClient;
  5. using CommonAI.ZoneClient.Agent;
  6. using CommonLang;
  7. using CommonLang.Concurrent;
  8. using CommonLang.Log;
  9. using CommonLang.Property;
  10. using CommonLang.Vector;
  11. using pomelo.area;
  12. using pomelo.connector;
  13. using Pomelo.DotNetClient;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading;
  19. using XmdsBattleClient.Client;
  20. using XmdsBattleClientBot.Bot;
  21. namespace XmdsBotTest.Runner
  22. {
  23. public class ModuleMasterTask : BotRunner.RunnerModule
  24. {
  25. private BotClient.QuestData.SeekInfo CurrentQuestSeeking;
  26. private int InProgressQuestTicking = 0;
  27. public ModuleMasterTask(BotRunner r) : base(r)
  28. {
  29. bot.Client.GameSocket.listen<TaskUpdatePush>(on_task_update_push);
  30. }
  31. protected override void OnEnableChanged(bool enable)
  32. {
  33. if (!enable)
  34. do_clear_current_seeking();
  35. }
  36. protected internal override void OnGateBindPlayer(BindPlayerResponse e)
  37. {
  38. runner.do_gm_add_exp(999999999);
  39. }
  40. protected internal override void OnBattleActorDead(ZoneActor actor, UnitDeadEvent e)
  41. {
  42. do_clear_current_seeking();
  43. }
  44. protected internal override void OnGameEnterScene(EnterSceneResponse response)
  45. {
  46. do_clear_current_seeking();
  47. }
  48. protected internal override void OnBattleActorReady(CommonAI.ZoneClient.ZoneLayer layer, CommonAI.ZoneClient.ZoneActor actor)
  49. {
  50. layer.AddTimeDelayMS(bot.Random.Next(Config.CheckIntervalMS, Config.CheckIntervalMS * 2), (t) =>
  51. {
  52. if (Enable) runner.do_seek_random_task(on_seek_action);
  53. });
  54. layer.AddTimePeriodicMS(Config.CheckIntervalMS, (t) =>
  55. {
  56. if (bot.CurrentZoneActor.CurrentState == UnitActionStatus.Dead)
  57. {
  58. runner.do_relive(1);
  59. }
  60. if (Enable)
  61. acceptDailyTaskRequest();
  62. else
  63. do_clear_current_seeking();
  64. });
  65. }
  66. private void acceptDailyTaskRequest() {
  67. client.GameSocket.taskHandler.acceptDailyTaskRequest("17",
  68. (err, rsp) =>
  69. { });
  70. }
  71. private void on_task_update_push(TaskUpdatePush e)
  72. {
  73. if (Enable)
  74. {
  75. log.Log("on_task_update_push");
  76. if (CurrentQuestSeeking != null)
  77. {
  78. // if (CurrentQuestSeeking.quest.State != BotClient.QuestData.QuestStatus.DONE)
  79. // {
  80. // log.Log("continue do_seek_task : " + CurrentQuestSeeking.quest);
  81. // runner.do_seek_task(CurrentQuestSeeking.quest.TemplateID, on_seek_action);
  82. // }
  83. }
  84. }
  85. }
  86. private void do_clear_current_seeking()
  87. {
  88. if (runner.CurrentMoveAgent != null)
  89. {
  90. runner.CurrentMoveAgent.Stop();
  91. }
  92. InProgressQuestTicking = 0;
  93. CurrentQuestSeeking = null;
  94. }
  95. private void on_seek_action(BotClient.QuestData quest, BotClient.QuestData.SeekInfo seek)
  96. {
  97. SetStatus(quest.Name + "(" + quest.State + ")");
  98. log.Log("on_seek_action : " + quest);
  99. if (quest != null && quest.TemplateID == 1015)
  100. {
  101. runner.do_gm_finish_task(quest.TemplateID);
  102. bot.CurrentRegionManager.CheckTrans(1, true);
  103. }
  104. if (CurrentQuestSeeking == null || CurrentQuestSeeking.quest.TemplateID != quest.TemplateID)
  105. {
  106. InProgressQuestTicking = 0;
  107. }
  108. CurrentQuestSeeking = seek;
  109. if (seek.MoveTo != null)
  110. {
  111. var agent = runner.do_start_move_agent(CurrentQuestSeeking.MoveTo.X, CurrentQuestSeeking.MoveTo.Y, Config.MinSeekRange);
  112. if (agent.IsEnd || agent.Result == CommonAI.RTS.Manhattan.AstarManhattan.FindPathResult.Destination)
  113. {
  114. bot.CurrentZoneActor.SendUnitGuard(true);
  115. if (CurrentQuestSeeking.areaId != bot.CurrentZoneLayer.Data.TemplateID)
  116. {
  117. bot.CurrentRegionManager.CheckTrans(1, false);
  118. }
  119. }
  120. else
  121. {
  122. bot.CurrentZoneActor.SendUnitGuard(false);
  123. }
  124. }
  125. else if (seek.NoWay)
  126. {
  127. runner.do_bs_actor_random_move_transport(true, Config.MinSeekRange);
  128. }
  129. else
  130. {
  131. switch (seek.quest.State)
  132. {
  133. case BotClient.QuestData.QuestStatus.IN_PROGRESS:
  134. runner.do_gm_finish_task(quest.TemplateID);
  135. break;
  136. case BotClient.QuestData.QuestStatus.CAN_FINISH:
  137. runner.do_submit_task(quest, (err) => { do_clear_current_seeking(); });
  138. break;
  139. case BotClient.QuestData.QuestStatus.NEW:
  140. runner.do_accept_quest(quest, (err) => { do_clear_current_seeking(); });
  141. break;
  142. }
  143. bot.CurrentZoneLayer.AddTimeDelayMS(bot.Random.Next(Config.CheckIntervalMS, Config.CheckIntervalMS * 2), (t) =>
  144. {
  145. runner.do_seek_random_task(on_seek_action, true);
  146. });
  147. }
  148. }
  149. private void on_seek_quest_move_done(BotClient.QuestData.SeekInfo seek_info)
  150. {
  151. if (runner.CurrentMoveAgent != null)
  152. {
  153. runner.CurrentMoveAgent.Stop();
  154. }
  155. log.Log("on_seek_quest_move_done : " + seek_info.quest);
  156. if (seek_info.areaId == bot.CurrentZoneLayer.SceneID)
  157. {
  158. switch (seek_info.quest.State)
  159. {
  160. case BotClient.QuestData.QuestStatus.NEW:
  161. bot.CurrentZoneActor.SendUnitGuard(false);
  162. runner.do_accept_quest(seek_info.quest, (err) => { do_clear_current_seeking(); });
  163. break;
  164. case BotClient.QuestData.QuestStatus.IN_PROGRESS:
  165. log.Error(seek_info.quest.SubType + " : " + seek_info.quest + " : " + seek_info.quest.State + " T=" + InProgressQuestTicking);
  166. switch (seek_info.quest.SubType)
  167. {
  168. case BotClient.QuestData.EventType.InterActiveItem:
  169. bot.CurrentZoneActor.SendUnitGuard(false);
  170. Vector2 move_to;
  171. var item = runner.do_try_pick_nearest_item(out move_to);
  172. if (item == null && move_to != null)
  173. {
  174. CurrentQuestSeeking.MoveTo = move_to;
  175. }
  176. break;
  177. case BotClient.QuestData.EventType.KillCollect:
  178. bot.CurrentZoneActor.SendUnitGuard(true);
  179. runner.do_start_pick_any_item();
  180. break;
  181. case BotClient.QuestData.EventType.KillMonster:
  182. bot.CurrentZoneActor.SendUnitGuard(true);
  183. break;
  184. case BotClient.QuestData.EventType.InterActiveNpc:
  185. runner.do_update_quest_status(seek_info.quest);
  186. break;
  187. }
  188. break;
  189. case BotClient.QuestData.QuestStatus.CAN_FINISH:
  190. runner.do_submit_task(seek_info.quest, (err) =>
  191. {
  192. do_clear_current_seeking();
  193. runner.do_gm_finish_task(seek_info.quest.TemplateID);
  194. runner.do_seek_random_task(on_seek_action, true);
  195. });
  196. break;
  197. case BotClient.QuestData.QuestStatus.DONE:
  198. log.Log("DONE : " + seek_info.quest + " : " + seek_info.quest.State);
  199. CurrentQuestSeeking = null;
  200. bot.CurrentZoneActor.SendUnitGuard(false);
  201. runner.do_seek_random_task(on_seek_action);
  202. break;
  203. }
  204. }
  205. else
  206. {
  207. if (bot.CurrentRegionManager.CheckTrans(1, false) == false)
  208. {
  209. bot.CurrentZoneActor.SendUnitGuard(true);
  210. }
  211. log.Log("continue do_seek_task : " + seek_info.quest);
  212. runner.do_seek_task(seek_info.quest.TemplateID, on_seek_action);
  213. }
  214. if (seek_info.quest.State == BotClient.QuestData.QuestStatus.IN_PROGRESS)
  215. {
  216. InProgressQuestTicking++;
  217. if (InProgressQuestTicking >= Config.QuestTickingMaxTime)
  218. {
  219. InProgressQuestTicking = 0;
  220. runner.do_gm_finish_task(CurrentQuestSeeking.quest.TemplateID);
  221. bot.CurrentZoneLayer.AddTimeDelayMS(bot.Random.Next(Config.CheckIntervalMS, Config.CheckIntervalMS * 2), (t) =>
  222. {
  223. runner.do_seek_random_task(on_seek_action, true);
  224. });
  225. }
  226. }
  227. }
  228. [Desc("任务配置")]
  229. [Expandable]
  230. public class Config
  231. {
  232. [Desc("师门任务检测间隔")]
  233. public static int CheckIntervalMS = 5000;
  234. [Desc("任务进度最大尝试次数")]
  235. public static int QuestTickingMaxTime = 20;
  236. [Desc("寻路到达目的地距离")]
  237. public static float MinSeekRange = 0.01f;
  238. public override string ToString()
  239. {
  240. return "任务配置";
  241. }
  242. }
  243. }
  244. }