ZoneIntervalRebirthMonster.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using CommonAI.Zone;
  2. using CommonAI.Zone.ZoneEditor;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using XmdsCommonServer.Plugin.Scene;
  8. using CommonAI.Zone.Instance;
  9. using CommonAI.Zone.Helper;
  10. using CommonLang;
  11. using System.Diagnostics;
  12. using CommonAI.Data;
  13. namespace XmdsCommonZone.Zones
  14. {
  15. /// <summary>
  16. /// 定时刷宝箱类,最初用于连服宝箱,类似的功能需要继承该类,类似于Zone80001.cs
  17. /// </summary>
  18. public class ZoneIntervalRebirthMonster : XmdsServerScene
  19. {
  20. private bool initedFromServer = false;
  21. private TimeTaskMS timeTask = null;
  22. private int maxMonsterNum = 0;
  23. private int createMonsterIntervalMS = 0;
  24. // 按时间从小到大排序 { openTime, closeTime, openTime, closeTime ... }
  25. private int[] timeOpenCloseList = null;
  26. // 按概率从小到大排序 {怪物id, 万分比概率, 怪物id,万分比概率 ...}
  27. private int[] monsterIdProbabilityList = null;
  28. private List<InstanceUnit> monsterList = new List<InstanceUnit>();
  29. // monster的出生地坐标列表
  30. private List<InstanceFlag> usingPointList = new List<InstanceFlag>();
  31. private List<InstanceFlag> freePointList = new List<InstanceFlag>();
  32. public ZoneIntervalRebirthMonster(TemplateManager templates, InstanceZoneListener listener, SceneData data, GSCreateAreaData gsData, string bindGameSrvId)
  33. : base(templates, listener, data, gsData, bindGameSrvId)
  34. {
  35. AddEvents();
  36. }
  37. /// <summary>
  38. /// 从游戏服初始化场景数据
  39. /// </summary>
  40. /// <param name="timeOpenCloseList">从00点到当前的毫秒数{openTime, closeTime, openTime, closeTime ...}</param>
  41. /// <param name="monsterIdProbabilityList">monsterId,万分比概率{monsterId, probability, monsterId, probability ...}</param>
  42. /// <param name="createMonsterIntervalMS">创建Monster的时间间隔,单位毫秒</param>
  43. /// <param name="maxMonsterCount">创建monster的最大个数</param>
  44. public void InitSceneDataFromGameServer(int[] timeOpenCloseList, int[] monsterIdProbabilityList, int createMonsterIntervalMS, int maxMonsterNum)
  45. {
  46. Debug.Assert(!initedFromServer, "ZoneIntervalRebirthMonster.InitSceneDataFromGameServer already call, can not call it twice");
  47. initedFromServer = true;
  48. Log.Error("ZoneIntervalRebirthMonster.InitSceneDataFromGameServer");
  49. this.timeOpenCloseList = SortListBySecondValue(timeOpenCloseList);
  50. this.monsterIdProbabilityList = SortListBySecondValue(monsterIdProbabilityList);
  51. this.createMonsterIntervalMS = createMonsterIntervalMS;
  52. this.maxMonsterNum = maxMonsterNum;
  53. AdjustMonsterProbability(this.monsterIdProbabilityList);
  54. if (CheckInOpenTime(0))
  55. {
  56. TryCreateAllMonster(null);
  57. }else
  58. {
  59. timeTask = AddTimePeriodicMS(30000, WaitOpenTime);
  60. }
  61. }
  62. private void AdjustMonsterProbability(int[] list)
  63. {
  64. float n = 0;
  65. for (int i = 1; i < list.Length; i += 2)
  66. {
  67. n += list[i];
  68. }
  69. float scale = 10000f / n;
  70. int p = 0;
  71. for (int i = 1; i < list.Length; i += 2)
  72. {
  73. list[i] = (int)(list[i] * scale) + p;
  74. p = list[i];
  75. }
  76. list[list.Length - 1] = 10000;
  77. }
  78. private int[] SortListBySecondValue(int[] list)
  79. {
  80. int listLen = list.Length;
  81. int[] newList = new int[listLen];
  82. bool[] freeList = new bool[listLen / 2];
  83. for (int i = 0; i < listLen / 2; i++)
  84. freeList[i] = true;
  85. for (int n = 0; n < listLen; n += 2)
  86. {
  87. int tmpIdx = 0, probability = int.MaxValue;
  88. for (int i = 1; i < listLen; i += 2)
  89. {
  90. if (freeList[i/2] && list[i] < probability)
  91. {
  92. tmpIdx = i;
  93. probability = list[i];
  94. }
  95. }
  96. freeList[tmpIdx/2] = false;
  97. newList[n] = list[tmpIdx - 1];
  98. newList[n+1] = probability;
  99. }
  100. return newList;
  101. }
  102. private bool CheckInOpenTime(int offsetMillisecond)
  103. {
  104. DateTime datetime = DateTime.Now.AddMilliseconds(offsetMillisecond);
  105. int time = datetime.Hour * 3600000 + datetime.Minute * 60000 + datetime.Second * 1000 + datetime.Millisecond;
  106. for (int i = 0; i < timeOpenCloseList.Length; i += 2)
  107. {
  108. if (timeOpenCloseList[i] <= time && timeOpenCloseList[i + 1] > time)
  109. return true;
  110. }
  111. return false;
  112. }
  113. // 获得到下次开启的MS数
  114. private int GetDeltaTimeToOpen()
  115. {
  116. DateTime datetime = DateTime.Now;
  117. int time = datetime.Hour * 3600000 + datetime.Minute * 60000 + datetime.Second * 1000 + datetime.Millisecond;
  118. for (int i = 0; i < timeOpenCloseList.Length; i += 2)
  119. {
  120. if (timeOpenCloseList[i] > time)
  121. return timeOpenCloseList[i] - time;
  122. else if (timeOpenCloseList[i + 1] > time)
  123. return 0;
  124. }
  125. return 3600000 * 24 - time + timeOpenCloseList[0];
  126. }
  127. private void AddEvents()
  128. {
  129. OnInit += ZoneIntervalRebirthMonster_OnInit;
  130. OnUnitDead += ZoneIntervalRebirthMonster_OnUnitDead;
  131. }
  132. private void ZoneIntervalRebirthMonster_OnInit(InstanceZone zone)
  133. {
  134. Log.Error("ZoneIntervalRebirthMonster.ZoneIntervalRebirthMonster_OnInit");
  135. int i = 1;
  136. while(true)
  137. {
  138. InstanceFlag flag = getFlag(string.Format("bz{0:00}", i++));
  139. if (flag == null)
  140. break;
  141. freePointList.Add(flag);
  142. }
  143. // int[] timeList = { 14 * 3600000, 16 * 3600000, 16 * 3600000 + 10 * 60000, 16 * 3600000 + 30 * 60000, 17 * 3600000 , 18 * 3600000 };
  144. // int[] monsterIdProbablilityList = new int[] { 91110, 6000, 91120,3000, 91130,1000 };
  145. // InitSceneDataFromGameServer(timeList, monsterIdProbablilityList, 30000, 12);
  146. }
  147. private void ZoneIntervalRebirthMonster_OnUnitDead(InstanceZone zone, InstanceUnit obj, InstanceUnit attacker)
  148. {
  149. int idx = monsterList.FindIndex((unit) => { return obj == unit; });
  150. if (idx < 0)
  151. return;
  152. freePointList.Add(usingPointList[idx]);
  153. usingPointList.RemoveAt(idx);
  154. monsterList.RemoveAt(idx);
  155. if (!CheckInOpenTime(createMonsterIntervalMS))
  156. {
  157. if (timeTask != null)
  158. return;
  159. timeTask = AddTimePeriodicMS(30000, WaitOpenTime);
  160. return;
  161. }
  162. DelayCall(createMonsterIntervalMS, TryCreateMonster);
  163. }
  164. private void DelayCall(int delayMS, TickHandler handler)
  165. {
  166. long endTicks = DateTime.Now.Ticks + delayMS * TimeSpan.TicksPerMillisecond;
  167. AddTimePeriodicMS(1000, (TimeTaskMS task) =>
  168. {
  169. if (DateTime.Now.Ticks > endTicks)
  170. {
  171. task.Stop();
  172. handler(null);
  173. }
  174. });
  175. }
  176. private void WaitOpenTime(TimeTaskMS task)
  177. {
  178. if (CheckInOpenTime(0))
  179. {
  180. timeTask.Stop();
  181. timeTask = null;
  182. TryCreateAllMonster(null);
  183. }
  184. }
  185. private void TryCreateAllMonster(TimeTaskMS task)
  186. {
  187. if (monsterList.Count >= maxMonsterNum)
  188. {
  189. return;
  190. }
  191. int n = maxMonsterNum - monsterList.Count;
  192. Log.Error("ZoneIntervalRebirthMonster TryCreateAllMonster will create count=" + n);
  193. while (--n >= 0)
  194. TryCreateMonster(null);
  195. }
  196. private void TryCreateMonster(TimeTaskMS task)
  197. {
  198. if (monsterList.Count >= maxMonsterNum) return;
  199. int probalility = RandomN.Next(0, 10000);
  200. int monsterId = 0;
  201. for (int i = 1; i < monsterIdProbabilityList.Length; i += 2)
  202. {
  203. if (probalility <= monsterIdProbabilityList[i])
  204. {
  205. monsterId = monsterIdProbabilityList[i - 1];
  206. break;
  207. }
  208. }
  209. if (monsterId == 0) return;
  210. int flagIdx = RandomN.Next(0, freePointList.Count);
  211. InstanceFlag flag = freePointList[flagIdx];
  212. InstanceUnit unit = AddUnit(monsterId, null, 1, 0, flag.X, flag.Y, (float)RandomN.NextDouble() * 6.28f);
  213. if (unit != null)
  214. {
  215. monsterList.Add(unit);
  216. freePointList.RemoveAt(flagIdx);
  217. usingPointList.Add(flag);
  218. Log.Error("ZoneIntervalRebirthMonster createMonster count=" + monsterList.Count);
  219. }
  220. }
  221. }
  222. }