HZZoneLayer.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. using CommonAI.Zone.ZoneEditor;
  2. using CommonAI.ZoneClient;
  3. using CommonLang;
  4. using System;
  5. using CommonLang.Vector;
  6. using CommonAI.Zone;
  7. using System.Collections.Generic;
  8. using XmdsCommon.Plugin;
  9. using CommonAI.Zone.Helper;
  10. namespace XmdsCommon.ZoneClient
  11. {
  12. public class HZZoneLayer : ZoneLayer
  13. {
  14. private List<ZoneEditorArea> mSafeAreaList;
  15. public HZZoneLayer(EditorTemplates dataroot, ILayerClient client)
  16. : base(dataroot, client)
  17. {
  18. this.LayerInit += HZZoneLayer_LayerInit;
  19. }
  20. private void HZZoneLayer_LayerInit(ZoneLayer layer)
  21. {
  22. //扫出所有安全区域.
  23. ICollection<ZoneFlag> list = layer.Flags;
  24. if (list == null)
  25. {
  26. return;
  27. }
  28. mSafeAreaList = new List<ZoneEditorArea>();
  29. ZoneEditorArea zea = null;
  30. foreach (ZoneFlag e in list)
  31. {
  32. if (e is ZoneEditorArea)
  33. {
  34. zea = e as ZoneEditorArea;
  35. if (zea.CurrentMapNodeValue == XmdsMapBlock.BLOCK_VALUE_SAFE)
  36. {
  37. mSafeAreaList.Add(zea);
  38. }
  39. }
  40. }
  41. }
  42. public override bool IsCanPickItem(ZoneActor owner, ZoneItem item)
  43. {
  44. if (item == null)
  45. {
  46. return false;
  47. }
  48. if (IsTeamPickableItem(owner, item))
  49. {
  50. return true;
  51. }
  52. if (IsPickableItem(owner, item))
  53. {
  54. return true;
  55. }
  56. return false;
  57. }
  58. /// <summary>
  59. /// 获取当前可检取道具
  60. /// </summary>
  61. /// <param name="owner"></param>
  62. /// <returns></returns>
  63. /**
  64. public override ZoneItem GetNearPickableItem(ZoneActor owner)
  65. {
  66. ZoneItem ret = null;
  67. ForEachNearObjects<ZoneItem>(owner.X, owner.Y, (ZoneItem item, ref bool cancel) =>
  68. {
  69. if (IsTeamPickableItem(owner, item))
  70. {
  71. ret = item;
  72. cancel = true;
  73. }
  74. });
  75. if (ret != null)
  76. {
  77. return ret;
  78. }
  79. ForEachNearObjects<ZoneItem>(owner.X, owner.Y, (ZoneItem item, ref bool cancel) =>
  80. {
  81. if (IsPickableItem(owner, item))
  82. {
  83. ret = item;
  84. cancel = true;
  85. }
  86. });
  87. return ret;
  88. }
  89. **/
  90. public virtual bool IsTeamPickableItem(ZoneActor owner, ZoneItem item, bool no_touch = false)
  91. {
  92. if (no_touch || item.Info.Pickable && (item.Info.DropForAll || item.Force == owner.Force))
  93. {
  94. if (CMath.intersectRound(item.X, item.Y, item.Info.BodySize, owner.X, owner.Y, owner.Info.BodySize))
  95. {
  96. XmdsCommon.Message.DropItem di = item.SyncInfo.ExtData as XmdsCommon.Message.DropItem;
  97. if (di == null)
  98. {
  99. return true;
  100. }
  101. else
  102. {
  103. //Mode 1 自由拾取 只检查自己背包
  104. //Mode 2, 队伍背包,只检查队伍背包
  105. //Mode 3, Roll点,只判断所有权
  106. bool is_owner = false;
  107. if (di.PlayerUUID != null)
  108. {
  109. for (int i = 0; i < di.PlayerUUID.Count; i++)
  110. {
  111. if (owner.PlayerUUID == di.PlayerUUID[i])
  112. {
  113. is_owner = true;
  114. break;
  115. }
  116. }
  117. }
  118. if (is_owner)//有归属者.
  119. {
  120. if (di.Mode == 2)
  121. {
  122. return true;
  123. }
  124. else
  125. {
  126. return false;
  127. }
  128. }
  129. else
  130. {
  131. if (item.PassTimeMS >= di.ProtectTime)
  132. {
  133. return true;
  134. }
  135. else
  136. {
  137. return false;
  138. }
  139. }
  140. }
  141. }
  142. }
  143. return false;
  144. }
  145. public override bool IsPickableItem(ZoneActor owner, ZoneItem item, bool no_touch = false)
  146. {
  147. if (!item.Info.OnlyForShow && (item.Info.DropForAll || item.Force == owner.Force))
  148. {
  149. if (no_touch || CMath.intersectRound(item.X, item.Y, item.Info.BodySize, owner.X, owner.Y, owner.Info.BodySize))
  150. {
  151. XmdsCommon.Message.DropItem di = item.SyncInfo.ExtData as XmdsCommon.Message.DropItem;
  152. if (di == null)
  153. {
  154. return true;
  155. }
  156. else
  157. {
  158. bool is_owner = false;
  159. if (di.PlayerUUID != null)
  160. {
  161. for (int i = 0; i < di.PlayerUUID.Count; i++)
  162. {
  163. if (owner.PlayerUUID == di.PlayerUUID[i])
  164. {
  165. is_owner = true;
  166. break;
  167. }
  168. }
  169. }
  170. if (is_owner)
  171. {
  172. return true;
  173. }
  174. else
  175. {
  176. //if (item.PassTimeMS >= di.ProtectTime)
  177. if (item.PassTimeMS >= di.ProtectTime || item.SyncInfo.ItemExpireTimeMS == int.MaxValue) //edited by vajre 用于跨服拾取别人的装备
  178. {
  179. return true;
  180. }
  181. else
  182. {
  183. return false;
  184. }
  185. }
  186. }
  187. }
  188. }
  189. return false;
  190. }
  191. public bool IsInSafeArea(ZoneUnit src)
  192. {
  193. var sa = GetArea(src.X, src.Y);
  194. //var sa = mUnit.CurrentArea;
  195. if (sa != null && sa.CurrentMapNodeValue == XmdsMapBlock.BLOCK_VALUE_SAFE)
  196. {
  197. return true;
  198. }
  199. else
  200. {
  201. return false;
  202. }
  203. }
  204. public override bool IsAttackable(ZoneUnit src, ZoneUnit target, SkillTemplate.CastTarget expectTarget)
  205. {
  206. //绝对中立阵营,不能被选为攻击目标.
  207. if (target.Force == 0 || target.HP <= 0)
  208. {
  209. return false;
  210. }
  211. switch (expectTarget)
  212. {
  213. case SkillTemplate.CastTarget.Enemy:
  214. {
  215. bool isEnemy = (src.Virtual as XmdsClientVirtual.XmdsClientVirtual).IsEnemy(target);
  216. if (src.Info.UType == UnitInfo.UnitType.TYPE_PLAYER && target.Info.UType == UnitInfo.UnitType.TYPE_PLAYER)
  217. {
  218. if (IsInSafeArea(src) || IsInSafeArea(target))
  219. {
  220. return false;
  221. }
  222. else
  223. {
  224. return isEnemy;
  225. }
  226. }
  227. else
  228. {
  229. return isEnemy;
  230. }
  231. }
  232. case SkillTemplate.CastTarget.PetForMaster:
  233. if (src.Info.UType == UnitInfo.UnitType.TYPE_PET)
  234. {
  235. return (src != target) && (src.Force == target.Force);
  236. }
  237. return false;
  238. case SkillTemplate.CastTarget.Alias:
  239. case SkillTemplate.CastTarget.AlliesExcludeSelf:
  240. return (src != target) && (src.Virtual as XmdsClientVirtual.XmdsClientVirtual).IsAllies(target);
  241. case SkillTemplate.CastTarget.AlliesIncludeSelf:
  242. return (src.Virtual as XmdsClientVirtual.XmdsClientVirtual).IsAllies(target);
  243. case SkillTemplate.CastTarget.EveryOne:
  244. return true;
  245. case SkillTemplate.CastTarget.EveryOneExcludeSelf:
  246. return (src != target);
  247. case SkillTemplate.CastTarget.Self:
  248. return src == target;
  249. case SkillTemplate.CastTarget.EnemyAndSelf:
  250. {
  251. if (src == target)
  252. {
  253. return true;
  254. }
  255. else
  256. {
  257. bool isEnemy = (src.Virtual as XmdsClientVirtual.XmdsClientVirtual).IsEnemy(target);
  258. if (src.Info.UType == UnitInfo.UnitType.TYPE_PLAYER && target.Info.UType == UnitInfo.UnitType.TYPE_PLAYER)
  259. {
  260. if (IsInSafeArea(src) || IsInSafeArea(target))
  261. {
  262. return false;
  263. }
  264. else
  265. {
  266. return isEnemy;
  267. }
  268. }
  269. else
  270. {
  271. return isEnemy;
  272. }
  273. }
  274. };
  275. case SkillTemplate.CastTarget.NA:
  276. default:
  277. return false;
  278. }
  279. }
  280. /// <summary>
  281. /// 获得范围内符合目标条件的单位.
  282. /// </summary>
  283. /// <param name="owner"></param>
  284. /// <param name="range"></param>
  285. /// <param name="expect"></param>
  286. /// <returns></returns>
  287. public virtual ZoneUnit GetNearTarget<T>(ZoneActor owner, float range, SkillTemplate.CastTarget expect = SkillTemplate.CastTarget.Enemy) where T : XmdsClientVirtual.XmdsClientVirtual
  288. {
  289. ZoneUnit min = null;
  290. float min_len = range * range;
  291. ForEachNearObjects<ZoneUnit>(owner.X, owner.Y, range, (ZoneUnit u, ref bool cancel) =>
  292. {
  293. if (u.Virtual is T)
  294. {
  295. if (IsAttackable(owner, u, expect))
  296. {
  297. float len = MathVector.getDistanceSquare(u.X, u.Y, owner.X, owner.Y) - u.Info.BodyHitSize;
  298. if (min_len >= len)
  299. {
  300. min_len = len;
  301. min = u;
  302. }
  303. }
  304. }
  305. });
  306. return min;
  307. }
  308. /// <summary>
  309. /// 获得范围内血量最少的单位.
  310. /// </summary>
  311. /// <param name="owner"></param>
  312. /// <param name="range"></param>
  313. /// <param name="expect"></param>
  314. /// <returns></returns>
  315. public virtual ZoneUnit GetWeakestTarget<T>(ZoneActor owner, float range, SkillTemplate.CastTarget expect = SkillTemplate.CastTarget.Enemy) where T : XmdsClientVirtual.XmdsClientVirtual
  316. {
  317. using (var list = ListObjectPool<ZoneUnit>.AllocAutoRelease())
  318. {
  319. ForEachNearObjects<ZoneUnit>(owner.X, owner.Y, range, (ZoneUnit u, ref bool cancel) =>
  320. {
  321. if (u.Virtual is T)
  322. {
  323. if (CMath.intersectRound(owner.X, owner.Y, range, u.X, u.Y, u.Info.BodyHitSize))
  324. {
  325. if (IsAttackable(owner, u, expect))
  326. {
  327. list.Add(u);
  328. }
  329. }
  330. }
  331. });
  332. if (list.Count > 0)
  333. {
  334. list.Sort((a, b) =>
  335. {
  336. if (a.HP != b.HP)
  337. {
  338. return a.HP - b.HP;
  339. }
  340. float da = CMath.getDistanceSquare(a.X, a.Y, owner.X, owner.Y) - a.Info.BodyHitSize;
  341. float db = CMath.getDistanceSquare(b.X, b.Y, owner.X, owner.Y) - b.Info.BodyHitSize;
  342. return (int)(da - db);
  343. });
  344. return list[0];
  345. }
  346. else
  347. {
  348. return null;
  349. }
  350. }
  351. }
  352. public virtual ZoneEditorArea GetNearSafeAreaPos(float x, float y)
  353. {
  354. if (mSafeAreaList == null)
  355. {
  356. return null;
  357. }
  358. ZoneEditorArea ret = null;
  359. if (mSafeAreaList.Count > 0)
  360. {
  361. mSafeAreaList.Sort((a, b) =>
  362. {
  363. float da = CMath.getDistanceSquare(a.X, a.Y, x, y);
  364. float db = CMath.getDistanceSquare(b.X, b.Y, x, y);
  365. return (int)(da - db);
  366. });
  367. ret = mSafeAreaList[0];
  368. }
  369. return ret;
  370. }
  371. }
  372. }