BotPlayer.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using CommonAI.Zone;
  2. using CommonAI.Zone.Helper;
  3. using CommonAI.Zone.ZoneEditor;
  4. using CommonAIServer.Connector.Client;
  5. using CommonLang;
  6. using CommonLang.Log;
  7. using CommonNetwork.Sockets;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace CommonAIServer.Connector.Bot
  14. {
  15. public class BotPlayer : IDisposable
  16. {
  17. private static Random random = new Random();
  18. private readonly Logger log;
  19. private readonly string mName;
  20. private readonly NetTestClient mClient;
  21. private readonly int mFixedIntervalMS;
  22. private System.Threading.Timer mTimer;
  23. private long mLastUpdateTime;
  24. public string Name { get { return mName; } }
  25. public bool IsRunning { get { return mClient.Client.Session.IsConnected; } }
  26. public BotPlayer(string name, string roomID, UnitInfo unit, int force, EditorTemplates dataroot, string connectString)
  27. {
  28. this.log = LoggerFactory.GetLogger("Bot[" + name + "]");
  29. CommonAI.ZoneServer.CreateUnitInfoR2B ret = new CommonAI.ZoneServer.CreateUnitInfoR2B();
  30. ret.UnitTemplateID = unit.TemplateID;
  31. ret.Force = force;
  32. this.mFixedIntervalMS = (1000 / dataroot.Templates.CFG.SYSTEM_FPS);
  33. this.mName = name;
  34. this.mClient = new NetTestClient(name,
  35. roomID, typeof(NetSession).FullName,
  36. connectString,
  37. mFixedIntervalMS,
  38. 20, ret, false, null, dataroot);
  39. this.mClient.Client.Layer.ActorAdded += Layer_ActorAdded;
  40. this.mClient.Client.Disconnectd += Client_Disconnectd;
  41. }
  42. private void Client_Disconnectd(CommonAIClient.Client.BattleClient bc)
  43. {
  44. this.Dispose();
  45. }
  46. private void Layer_ActorAdded(CommonAI.ZoneClient.ZoneLayer layer, CommonAI.ZoneClient.ZoneActor actor)
  47. {
  48. actor.SendUnitGuard(true);
  49. startMoveToTarget();
  50. }
  51. public void Start()
  52. {
  53. this.mClient.Start();
  54. this.mTimer = new System.Threading.Timer(update, this, mFixedIntervalMS, mFixedIntervalMS);
  55. }
  56. private void update(object state)
  57. {
  58. lock (this)
  59. {
  60. long curTime = CommonLang.CUtils.CurrentTimeMS;
  61. if (mLastUpdateTime == 0)
  62. {
  63. mLastUpdateTime = curTime;
  64. }
  65. int intervalMS = (int)(curTime - mLastUpdateTime);
  66. this.mLastUpdateTime = curTime;
  67. try
  68. {
  69. updateInTarget(intervalMS);
  70. this.mClient.Update(intervalMS);
  71. }
  72. catch (Exception err)
  73. {
  74. log.Error(err.Message, err);
  75. }
  76. }
  77. }
  78. public void SendLeaveRoom()
  79. {
  80. mClient.Client.SendLeaveRoom();
  81. }
  82. public void Dispose()
  83. {
  84. try
  85. {
  86. lock (this)
  87. {
  88. mTimer.Dispose();
  89. }
  90. }
  91. catch (Exception err)
  92. {
  93. log.Error(err.Message, err);
  94. }
  95. try
  96. {
  97. mClient.Stop();
  98. mClient.Dispose();
  99. }
  100. catch (Exception err)
  101. {
  102. log.Error(err.Message, err);
  103. }
  104. }
  105. private TimeExpire<RegionData> target_expire;
  106. private void startMoveToTarget()
  107. {
  108. var list = new List<RegionData>(mClient.Client.Layer.Data.Regions);
  109. CUtils.RandomList(random, list);
  110. foreach (var rd in list)
  111. {
  112. foreach (var ab in rd.GetAbilities())
  113. {
  114. if (ab is SpawnUnitAbilityData)
  115. {
  116. var suab = ab as SpawnUnitAbilityData;
  117. if (suab.Force != mClient.Client.Actor.Force)
  118. {
  119. target_expire = new TimeExpire<RegionData>(rd, random.Next(10000, 100000));
  120. mClient.Client.Actor.SendUnitAttackMoveTo(rd.X, rd.Y, true);
  121. return;
  122. }
  123. }
  124. }
  125. }
  126. }
  127. private void updateInTarget(int intervalMS)
  128. {
  129. if (target_expire != null && target_expire.Update(intervalMS))
  130. {
  131. var rg = target_expire.Tag;
  132. var actor = mClient.Client.Actor;
  133. startMoveToTarget();
  134. }
  135. }
  136. }
  137. }