ZoneIntervalRebirthMonster.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. OnInit += ZoneIntervalRebirthMonster_OnInit;
  36. OnUnitDead += ZoneIntervalRebirthMonster_OnUnitDead;
  37. }
  38. protected override void Disposing()
  39. {
  40. base.Disposing();
  41. OnInit -= ZoneIntervalRebirthMonster_OnInit;
  42. OnUnitDead -= ZoneIntervalRebirthMonster_OnUnitDead;
  43. }
  44. /// <summary>
  45. /// 从游戏服初始化场景数据
  46. /// </summary>
  47. /// <param name="timeOpenCloseList">从00点到当前的毫秒数{openTime, closeTime, openTime, closeTime ...}</param>
  48. /// <param name="monsterIdProbabilityList">monsterId,万分比概率{monsterId, probability, monsterId, probability ...}</param>
  49. /// <param name="createMonsterIntervalMS">创建Monster的时间间隔,单位毫秒</param>
  50. /// <param name="maxMonsterCount">创建monster的最大个数</param>
  51. public void InitSceneDataFromGameServer(int[] timeOpenCloseList, int[] monsterIdProbabilityList, int createMonsterIntervalMS, int maxMonsterNum)
  52. {
  53. Debug.Assert(!initedFromServer, "ZoneIntervalRebirthMonster.InitSceneDataFromGameServer already call, can not call it twice");
  54. initedFromServer = true;
  55. Log.Error("ZoneIntervalRebirthMonster.InitSceneDataFromGameServer");
  56. this.timeOpenCloseList = SortListBySecondValue(timeOpenCloseList);
  57. this.monsterIdProbabilityList = SortListBySecondValue(monsterIdProbabilityList);
  58. this.createMonsterIntervalMS = createMonsterIntervalMS;
  59. this.maxMonsterNum = maxMonsterNum;
  60. AdjustMonsterProbability(this.monsterIdProbabilityList);
  61. if (CheckInOpenTime(0))
  62. {
  63. TryCreateAllMonster(null);
  64. }else
  65. {
  66. timeTask = AddTimePeriodicMS(30000, WaitOpenTime);
  67. }
  68. }
  69. private void AdjustMonsterProbability(int[] list)
  70. {
  71. float n = 0;
  72. for (int i = 1; i < list.Length; i += 2)
  73. {
  74. n += list[i];
  75. }
  76. float scale = 10000f / n;
  77. int p = 0;
  78. for (int i = 1; i < list.Length; i += 2)
  79. {
  80. list[i] = (int)(list[i] * scale) + p;
  81. p = list[i];
  82. }
  83. list[list.Length - 1] = 10000;
  84. }
  85. private int[] SortListBySecondValue(int[] list)
  86. {
  87. int listLen = list.Length;
  88. int[] newList = new int[listLen];
  89. bool[] freeList = new bool[listLen / 2];
  90. for (int i = 0; i < listLen / 2; i++)
  91. freeList[i] = true;
  92. for (int n = 0; n < listLen; n += 2)
  93. {
  94. int tmpIdx = 0, probability = int.MaxValue;
  95. for (int i = 1; i < listLen; i += 2)
  96. {
  97. if (freeList[i/2] && list[i] < probability)
  98. {
  99. tmpIdx = i;
  100. probability = list[i];
  101. }
  102. }
  103. freeList[tmpIdx/2] = false;
  104. newList[n] = list[tmpIdx - 1];
  105. newList[n+1] = probability;
  106. }
  107. return newList;
  108. }
  109. private bool CheckInOpenTime(int offsetMillisecond)
  110. {
  111. DateTime datetime = DateTime.Now.AddMilliseconds(offsetMillisecond);
  112. int time = datetime.Hour * 3600000 + datetime.Minute * 60000 + datetime.Second * 1000 + datetime.Millisecond;
  113. for (int i = 0; i < timeOpenCloseList.Length; i += 2)
  114. {
  115. if (timeOpenCloseList[i] <= time && timeOpenCloseList[i + 1] > time)
  116. return true;
  117. }
  118. return false;
  119. }
  120. // 获得到下次开启的MS数
  121. private int GetDeltaTimeToOpen()
  122. {
  123. DateTime datetime = DateTime.Now;
  124. int time = datetime.Hour * 3600000 + datetime.Minute * 60000 + datetime.Second * 1000 + datetime.Millisecond;
  125. for (int i = 0; i < timeOpenCloseList.Length; i += 2)
  126. {
  127. if (timeOpenCloseList[i] > time)
  128. return timeOpenCloseList[i] - time;
  129. else if (timeOpenCloseList[i + 1] > time)
  130. return 0;
  131. }
  132. return 3600000 * 24 - time + timeOpenCloseList[0];
  133. }
  134. private void ZoneIntervalRebirthMonster_OnInit(InstanceZone zone)
  135. {
  136. Log.Error("ZoneIntervalRebirthMonster.ZoneIntervalRebirthMonster_OnInit");
  137. int i = 1;
  138. while(true)
  139. {
  140. InstanceFlag flag = getFlag(string.Format("bz{0:00}", i++));
  141. if (flag == null)
  142. break;
  143. freePointList.Add(flag);
  144. }
  145. // int[] timeList = { 14 * 3600000, 16 * 3600000, 16 * 3600000 + 10 * 60000, 16 * 3600000 + 30 * 60000, 17 * 3600000 , 18 * 3600000 };
  146. // int[] monsterIdProbablilityList = new int[] { 91110, 6000, 91120,3000, 91130,1000 };
  147. // InitSceneDataFromGameServer(timeList, monsterIdProbablilityList, 30000, 12);
  148. }
  149. private void ZoneIntervalRebirthMonster_OnUnitDead(InstanceZone zone, InstanceUnit obj, InstanceUnit attacker)
  150. {
  151. int idx = monsterList.FindIndex((unit) => { return obj == unit; });
  152. if (idx < 0)
  153. return;
  154. freePointList.Add(usingPointList[idx]);
  155. usingPointList.RemoveAt(idx);
  156. monsterList.RemoveAt(idx);
  157. if (!CheckInOpenTime(createMonsterIntervalMS))
  158. {
  159. if (timeTask != null)
  160. return;
  161. timeTask = AddTimePeriodicMS(30000, WaitOpenTime);
  162. return;
  163. }
  164. DelayCall(createMonsterIntervalMS, TryCreateMonster);
  165. }
  166. private void DelayCall(int delayMS, TickHandler handler)
  167. {
  168. long endTicks = DateTime.Now.Ticks + delayMS * TimeSpan.TicksPerMillisecond;
  169. AddTimePeriodicMS(1000, (TimeTaskMS task) =>
  170. {
  171. if (DateTime.Now.Ticks > endTicks)
  172. {
  173. task.Stop();
  174. handler(null);
  175. }
  176. });
  177. }
  178. private void WaitOpenTime(TimeTaskMS task)
  179. {
  180. if (CheckInOpenTime(0))
  181. {
  182. timeTask.Stop();
  183. timeTask = null;
  184. TryCreateAllMonster(null);
  185. }
  186. }
  187. private void TryCreateAllMonster(TimeTaskMS task)
  188. {
  189. if (monsterList.Count >= maxMonsterNum)
  190. {
  191. return;
  192. }
  193. int n = maxMonsterNum - monsterList.Count;
  194. Log.Error("ZoneIntervalRebirthMonster TryCreateAllMonster will create count=" + n);
  195. while (--n >= 0)
  196. TryCreateMonster(null);
  197. }
  198. private void TryCreateMonster(TimeTaskMS task)
  199. {
  200. if (monsterList.Count >= maxMonsterNum) return;
  201. int probalility = RandomN.Next(0, 10000);
  202. int monsterId = 0;
  203. for (int i = 1; i < monsterIdProbabilityList.Length; i += 2)
  204. {
  205. if (probalility <= monsterIdProbabilityList[i])
  206. {
  207. monsterId = monsterIdProbabilityList[i - 1];
  208. break;
  209. }
  210. }
  211. if (monsterId == 0) return;
  212. int flagIdx = RandomN.Next(0, freePointList.Count);
  213. InstanceFlag flag = freePointList[flagIdx];
  214. InstanceUnit unit = AddUnit(monsterId, null, 1, 0, flag.X, flag.Y, (float)RandomN.NextDouble() * 6.28f);
  215. if (unit != null)
  216. {
  217. monsterList.Add(unit);
  218. freePointList.RemoveAt(flagIdx);
  219. usingPointList.Add(flag);
  220. Log.Error("ZoneIntervalRebirthMonster createMonster count=" + monsterList.Count);
  221. }
  222. }
  223. }
  224. }