BotPlayer.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 XmdsServerTestBots.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 = (byte)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. // if (CMath.includeRoundPoint(actor.X, actor.Y, actor.Info.GuardRange ,rg.X, rg.Y))
  135. // {
  136. //
  137. // }
  138. }
  139. }
  140. /*
  141. public class MoveAgent
  142. {
  143. public delegate void UpdateAction(float x, float y, object ud);
  144. public delegate void EndAction(bool complete, float x, float y, object ud);
  145. UpdateAction updateAction;
  146. EndAction endAction;
  147. object userdata = null;
  148. float speed = 0;
  149. //private List<Vector2> wayPoints;
  150. bool isActor = false;
  151. List<Vector2> path;
  152. bool active = false;
  153. private float mEndDistance;
  154. public bool IsActive
  155. {
  156. get
  157. {
  158. return active;
  159. }
  160. }
  161. public MoveAgent(bool isActor, UpdateAction updateAction, EndAction endAction)
  162. {
  163. this.isActor = isActor;
  164. this.updateAction = updateAction;
  165. this.endAction = endAction;
  166. path = new List<Vector2>();
  167. }
  168. public List<Vector2> StartSeek(float fromX, float fromY, float toX, float toY, float speed, float endDis, object ud)
  169. {
  170. if (active)
  171. {
  172. fromX = path[0].x;
  173. fromY = path[0].y;
  174. }
  175. AstarManhattan.MWayPoint wayPoints;
  176. AstarManhattan.FindPathResult ret = layer.FindPathResult(fromX, fromY, toX, toY, out wayPoints);
  177. if (ret == AstarManhattan.FindPathResult.Destination)
  178. {
  179. path.Clear();
  180. path.Add(new Vector2(toX, toY));
  181. }
  182. else if (ret == AstarManhattan.FindPathResult.Cross)
  183. {
  184. //已经取过一次,不需要重复取.Editor by Alex.Yu
  185. //wayPoints = layer.FindPath(fromX, fromY, toX, toY);
  186. path.Clear();
  187. path.Add(new Vector2(fromX, fromY));
  188. do
  189. {
  190. path.Add(new Vector2(wayPoints.PosX, wayPoints.PosY));
  191. wayPoints = wayPoints.Next;
  192. }
  193. while (wayPoints != null);
  194. //path.Add(new Vector2(toX, toY));
  195. }
  196. else
  197. {
  198. return null;
  199. }
  200. this.speed = speed;
  201. this.mEndDistance = endDis;
  202. this.userdata = ud;
  203. active = true;
  204. return path;
  205. }
  206. public void update(int deltatime)
  207. {
  208. if (!active) return;
  209. Vector2 pos = path[0];
  210. float length = speed * deltatime / 1000f;
  211. if (length > 0 && path.Count > 1)
  212. {
  213. float l = Vector2.Distance(path[1], path[0]);
  214. if (l <= length)
  215. {
  216. path.RemoveAt(0);
  217. }
  218. else
  219. {
  220. path[0] = Vector2.MoveTowards(path[0], path[1], length);//path[0] + (path[1] - path[0]).normalized * length;
  221. }
  222. }
  223. //pos = path[0] - pos;
  224. //pos = path[0];
  225. updateAction(path[0].x, path[0].y, userdata);
  226. if (path.Count < 2)
  227. {
  228. StopSeek(true, path[0].x, path[0].y, userdata);
  229. }
  230. else if (mEndDistance > 0)
  231. {
  232. if (path.Count == 2)
  233. {
  234. if (Vector2.Distance(path[0], path[1]) <= mEndDistance)
  235. {
  236. StopSeek(true, path[0].x, path[0].y, userdata);
  237. }
  238. }
  239. }
  240. }
  241. private void StopSeek(bool complete, float x, float y, object ud)
  242. {
  243. if (active)
  244. {
  245. path.Clear();
  246. //updateAction = null;
  247. active = false;
  248. if (endAction != null)
  249. endAction(complete, 0, 0, ud);
  250. userdata = null;
  251. }
  252. }
  253. /// <summary>
  254. /// 外部打断寻路.
  255. /// </summary>
  256. /// <param name="x"></param>
  257. /// <param name="y"></param>
  258. /// <param name="ud"></param>
  259. public void StopSeek(float x = 0, float y = 0, object ud = null)
  260. {
  261. this.StopSeek(false, x, y, ud);
  262. }
  263. }*/
  264. }
  265. }