CombatCalculateTool.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. using System;
  2. using System.Collections.Generic;
  3. using CombatLibrary.CombatLibrary.CombatCore.CustomizeTimeLogic.FxLogic;
  4. using Common.Utility.CombatEvent;
  5. using Excel2Json;
  6. using Fort23.Core;
  7. using Fort23.UTool;
  8. using GameLogic.Combat.Buff;
  9. using GameLogic.Combat.Hero;
  10. using GameLogic.Combat.Skill;
  11. using GameLogic.Player;
  12. using UnityEngine;
  13. using Utility;
  14. using UTool.CustomizeTimeLogic.FxLogic.TimeLineEventinterface;
  15. using Random = System.Random;
  16. namespace GameLogic.Combat.CombatTool
  17. {
  18. public class CombatCalculateTool : Singleton<CombatCalculateTool>
  19. {
  20. public Random Random = new Random();
  21. static readonly WuXingType[] Symbiosis = new WuXingType[6]
  22. {
  23. WuXingType.Water, // 金生水
  24. WuXingType.Fire, // 木生火
  25. WuXingType.Wood, // 水生木
  26. WuXingType.Earth, // 火生土
  27. WuXingType.Gold, // 土生金
  28. WuXingType.Null
  29. };
  30. // 相克关系表(用位表示)
  31. static readonly WuXingType[] Restrain = new WuXingType[6]
  32. {
  33. WuXingType.Wood, // 金克木
  34. WuXingType.Earth, // 木克土
  35. WuXingType.Fire, // 水克火
  36. WuXingType.Gold, // 火克金
  37. WuXingType.Water, // 土克水
  38. WuXingType.Null
  39. };
  40. public CombatCalculateTool()
  41. {
  42. Random = new Random(System.DateTime.Now.Millisecond);
  43. }
  44. public int GetOdd()
  45. {
  46. return Random.Next(0, 100);
  47. }
  48. public int GetOdd(int min, int max)
  49. {
  50. return Random.Next(min, max);
  51. }
  52. public long GetVlaueRatioForLong(long value, float ration)
  53. {
  54. long v = (value * (long)(ration * 100)) / 10000;
  55. return v;
  56. }
  57. public float GetVlaueRatioForFloat(float value, float ration)
  58. {
  59. float v = (value * ration) / 100f;
  60. return v;
  61. }
  62. public int GetVlaueRatioForInt(int value, float ration)
  63. {
  64. int v = (value * (int)(ration * 100)) / 10000;
  65. return v;
  66. }
  67. public HarmReturnInfo Harm(CombatHeroEntity source, CombatHeroEntity target, long att, AttType attType,
  68. TriggerData triggerData, WuXingType WuXingType, ISkillFeatures skillFeatures,
  69. HarmType harmType = HarmType.Null)
  70. {
  71. return Harm(source, target.GetMainHotPoin<CombatHeroHitPoint>(), att,
  72. attType, triggerData, WuXingType, skillFeatures, harmType);
  73. }
  74. /// <summary>
  75. /// 造成伤害
  76. /// </summary>n
  77. /// <param name="source">攻击方</param>
  78. /// <param name="target">被攻击方</param>
  79. /// <param name="att">伤害值</param>
  80. public HarmReturnInfo Harm(CombatHeroEntity source, CombatHeroHitPoint target, long att,
  81. AttType attType, TriggerData triggerData, WuXingType WuXingType, ISkillFeatures skillFeatures,
  82. HarmType harmType = HarmType.Default, HarmReturnInfo harmReturnInfo = null)
  83. {
  84. if (harmReturnInfo == null)
  85. {
  86. harmReturnInfo = CObjectPool.Instance.Fetch<HarmReturnInfo>();
  87. }
  88. harmReturnInfo.yuanShiAtt = att;
  89. harmReturnInfo.source = source;
  90. harmReturnInfo.target = target;
  91. harmReturnInfo.att = att;
  92. harmReturnInfo.attType = attType;
  93. harmReturnInfo.WuXingType = WuXingType;
  94. harmReturnInfo.harmType = harmType;
  95. harmReturnInfo.triggerData = triggerData;
  96. if (target.combatHeroEntity.isDie)
  97. {
  98. return harmReturnInfo;
  99. }
  100. if (CombatController.currActiveCombat.IsGameOver)
  101. {
  102. return harmReturnInfo;
  103. }
  104. float qiangDu = 100;
  105. SkillFeaturesData skillFeaturesData = skillFeatures as SkillFeaturesData;
  106. if (skillFeaturesData != null && skillFeaturesData.maxHp > 0)
  107. {
  108. qiangDu = (skillFeaturesData.hp * 100) / skillFeaturesData.maxHp;
  109. }
  110. harmReturnInfo.QiangDu = qiangDu;
  111. att = GetVlaueRatioForLong(att, qiangDu);
  112. float wuxing = source.CurrCombatHeroInfo.GetWuXingShuXing(WuXingType);
  113. // int index = GeWuXingTypeIndex(WuXingType);
  114. // WuXingType kzWuXing = Restrain[index];
  115. // float direnWuXing = target.combatHeroEntity.CurrCombatHeroInfo.GetWuXingShuXing(kzWuXing);
  116. att = GetVlaueRatioForLong(att, wuxing);
  117. long targetDef = target.combatHeroEntity.CurrCombatHeroInfo.defense.Value;
  118. targetDef -= GetVlaueRatioForLong(targetDef, harmReturnInfo.ArmorPiercing);
  119. float def =
  120. (targetDef * 100.0f / source.CurrCombatHeroInfo.k);
  121. int p1_id = (int)def;
  122. p1_id = Math.Min(100, Math.Max(1, p1_id));
  123. MitigationParaConfig mitigationParaConfig = ConfigComponent.Instance.Get<MitigationParaConfig>(p1_id);
  124. float p1 = (def * mitigationParaConfig.mitigationPara) / 100f;
  125. att = GetVlaueRatioForLong(att, 100 - p1);
  126. harmReturnInfo.att = att;
  127. StartInjuredEventData startInjuredEventData = StartInjuredEventData.Create();
  128. startInjuredEventData.HarmReturnInfo = harmReturnInfo;
  129. CombatEventManager.Instance.Dispatch(CombatEventType.StartInjured, startInjuredEventData);
  130. att = harmReturnInfo.att;
  131. if (harmReturnInfo.isInvalid)
  132. {
  133. return harmReturnInfo;
  134. }
  135. switch (WuXingType)
  136. {
  137. case WuXingType.Gold:
  138. att += GetVlaueRatioForLong(att,
  139. source.CurrCombatHeroInfo.Metal_HarmAdd);
  140. break;
  141. case WuXingType.Water:
  142. att += GetVlaueRatioForLong(att,
  143. source.CurrCombatHeroInfo.Water_HarmAdd);
  144. break;
  145. case WuXingType.Fire:
  146. att += GetVlaueRatioForLong(att,
  147. source.CurrCombatHeroInfo.Fire_HarmAdd);
  148. break;
  149. case WuXingType.Earth:
  150. att += GetVlaueRatioForLong(att,
  151. source.CurrCombatHeroInfo.Earth_HarmAdd);
  152. break;
  153. case WuXingType.Wood:
  154. att += GetVlaueRatioForLong(att,
  155. source.CurrCombatHeroInfo.Wood_HarmAdd);
  156. break;
  157. }
  158. if (!harmReturnInfo.attType.HasFlag(AttType.Buff))
  159. {
  160. att += GetVlaueRatioForLong(att,
  161. source.CurrCombatHeroInfo.finallyHarmAdd -
  162. target.combatHeroEntity.CurrCombatHeroInfo.finallyHarmReduce);
  163. }
  164. else
  165. {
  166. att += GetVlaueRatioForLong(att,
  167. source.CurrCombatHeroInfo.abnormalHarmAdd);
  168. }
  169. harmReturnInfo.att = att;
  170. target.combatHeroEntity.This<CombatHeroEntity>().HeroHurt(harmReturnInfo);
  171. return harmReturnInfo;
  172. }
  173. public HarmReturnInfo TrueHarm(CombatHeroEntity source, CombatHeroHitPoint target, long att,
  174. AttType attType, TriggerData triggerData,
  175. HarmType harmType = HarmType.Default)
  176. {
  177. HarmReturnInfo harmReturnInfo = CObjectPool.Instance.Fetch<HarmReturnInfo>();
  178. harmReturnInfo.source = source;
  179. harmReturnInfo.target = target;
  180. harmReturnInfo.att = att;
  181. harmReturnInfo.attType = attType;
  182. harmReturnInfo.harmType = harmType;
  183. harmReturnInfo.triggerData = triggerData;
  184. if (target.combatHeroEntity.isDie)
  185. {
  186. return harmReturnInfo;
  187. }
  188. target.combatHeroEntity.This<CombatHeroEntity>().HeroHurt(harmReturnInfo);
  189. return harmReturnInfo;
  190. }
  191. public HarmReturnInfo Recover(CombatHeroEntity source, CombatHeroHitPoint target, long att,
  192. AttType attType, HarmType harmType, TriggerData triggerData)
  193. {
  194. harmType |= HarmType.Recover;
  195. HarmReturnInfo harmReturnInfo = new HarmReturnInfo();
  196. harmReturnInfo.source = source;
  197. harmReturnInfo.target = target;
  198. harmReturnInfo.att = att;
  199. harmReturnInfo.attType = attType;
  200. harmReturnInfo.harmType = harmType;
  201. harmReturnInfo.triggerData = triggerData;
  202. if (target.combatHeroEntity.isDie)
  203. {
  204. return harmReturnInfo;
  205. }
  206. target.combatHeroEntity.This<CombatHeroEntity>().Recover(harmReturnInfo);
  207. return harmReturnInfo;
  208. }
  209. public ILifetCycleHitPoint[] GetMinHpHero(ILifetCycleHitPoint[] allLifetCycleHitPoints, int count)
  210. {
  211. if (allLifetCycleHitPoints == null)
  212. {
  213. return null;
  214. }
  215. BetterList<ILifetCycleHitPoint> findHero = new BetterList<ILifetCycleHitPoint>();
  216. findHero.AddRange(allLifetCycleHitPoints);
  217. int currCount = Math.Min(allLifetCycleHitPoints.Length, count);
  218. ILifetCycleHitPoint[] minHpHero = new ILifetCycleHitPoint[currCount];
  219. for (int k = 0; k < currCount; k++)
  220. {
  221. CombatHeroEntity lifetCycleHitPoint = null;
  222. int index = 0;
  223. if (findHero.Count <= 0)
  224. {
  225. return minHpHero;
  226. }
  227. lifetCycleHitPoint = findHero[0].IfLifeCycle.This<CombatHeroEntity>();
  228. for (int j = 0; j < findHero.Count; j++)
  229. {
  230. CombatHeroEntity lifetCycleHitPoint2 =
  231. findHero[j].IfLifeCycle.This<CombatHeroEntity>();
  232. if (lifetCycleHitPoint2.HpBl < lifetCycleHitPoint.HpBl)
  233. {
  234. lifetCycleHitPoint = lifetCycleHitPoint2;
  235. index = j;
  236. }
  237. }
  238. ILifetCycleHitPoint currFindHitPoint = lifetCycleHitPoint.GetMainHotPoin<ILifetCycleHitPoint>(true);
  239. minHpHero[k] = currFindHitPoint;
  240. findHero.RemoveAt(index);
  241. }
  242. return minHpHero;
  243. }
  244. private int GeWuXingTypeIndex(WuXingType e)
  245. {
  246. switch (e)
  247. {
  248. case WuXingType.Gold: return 0;
  249. case WuXingType.Wood: return 1;
  250. case WuXingType.Water: return 2;
  251. case WuXingType.Fire: return 3;
  252. case WuXingType.Earth: return 4;
  253. default: return 5; // 无效元素
  254. }
  255. }
  256. public int GetRestrained(WuXingType WuXingType, WuXingType targetWuXingType)
  257. {
  258. int c = 0;
  259. if (WuXingType.HasFlag(WuXingType.Gold))
  260. {
  261. if (targetWuXingType.HasFlag(WuXingType.Wood))
  262. {
  263. c++;
  264. }
  265. }
  266. if (WuXingType.HasFlag(WuXingType.Wood))
  267. {
  268. if (targetWuXingType.HasFlag(WuXingType.Earth))
  269. {
  270. c++;
  271. }
  272. }
  273. if (WuXingType.HasFlag(WuXingType.Water))
  274. {
  275. if (targetWuXingType.HasFlag(WuXingType.Fire))
  276. {
  277. c++;
  278. }
  279. }
  280. if (WuXingType.HasFlag(WuXingType.Fire))
  281. {
  282. if (targetWuXingType.HasFlag(WuXingType.Gold))
  283. {
  284. c++;
  285. }
  286. }
  287. if (WuXingType.HasFlag(WuXingType.Earth))
  288. {
  289. if (targetWuXingType.HasFlag(WuXingType.Wood))
  290. {
  291. c++;
  292. }
  293. }
  294. return c;
  295. }
  296. public long GetFaBaoDuiPingMaxValue(CombatMagicWeaponEntity a, CombatMagicWeaponEntity b)
  297. {
  298. CombatHeroEntity heroEntityA = a.RootMagicWeaponControl.combatHeroEntity;
  299. CombatHeroEntity heroEntityB = b.RootMagicWeaponControl.combatHeroEntity;
  300. int myRestrained = GetRestrained(a.WuXingType, b.WuXingType);
  301. int targetRestrained = GetRestrained(b.WuXingType, a.WuXingType);
  302. float lg_a = heroEntityA.CurrCombatHeroInfo.GetWuXingShuXing(a.WuXingType);
  303. float lg_b = heroEntityB.CurrCombatHeroInfo.GetWuXingShuXing(b.WuXingType);
  304. int c = myRestrained - targetRestrained;
  305. long myHp = (long)(a.HpBl);
  306. long targetHp = (long)(b.HpBl);
  307. float p2 = 100;
  308. if (c < 0) //a被压制
  309. {
  310. p2 = Mathf.Max(100, Mathf.Min(200, 30 + lg_b - lg_a));
  311. targetHp = (long)(targetHp * p2);
  312. }
  313. else if (c > 0)
  314. {
  315. p2 = Mathf.Max(100, Mathf.Min(200, 30 + lg_a - lg_b));
  316. myHp = (long)(myHp * p2);
  317. }
  318. a.HpBl= myHp;
  319. b.HpBl= targetHp;
  320. if (myHp > targetHp)
  321. {
  322. return targetHp;
  323. }
  324. else if (myHp < targetHp)
  325. {
  326. return myHp;
  327. }
  328. return targetHp;
  329. }
  330. public void FaBaoPengZhuang(CombatMagicWeaponEntity a, CombatMagicWeaponEntity b)
  331. {
  332. CombatHeroEntity heroEntityA = a.RootMagicWeaponControl.combatHeroEntity;
  333. CombatHeroEntity heroEntityB = b.RootMagicWeaponControl.combatHeroEntity;
  334. int myRestrained = GetRestrained(a.WuXingType, b.WuXingType);
  335. int targetRestrained = GetRestrained(b.WuXingType, a.WuXingType);
  336. float lg_a = heroEntityA.CurrCombatHeroInfo.GetWuXingShuXing(a.WuXingType);
  337. float lg_b = heroEntityB.CurrCombatHeroInfo.GetWuXingShuXing(b.WuXingType);
  338. int c = myRestrained - targetRestrained;
  339. long myHp = (long)(a.HpBl);
  340. long targetHp = (long)(b.HpBl);
  341. float p2 = 100;
  342. if (c < 0) //a被压制
  343. {
  344. p2 = Mathf.Max(100, Mathf.Min(200, 30 + lg_b - lg_a));
  345. targetHp = (long)(targetHp * p2);
  346. }
  347. else if (c > 0)
  348. {
  349. p2 = Mathf.Max(100, Mathf.Min(200, 30 + lg_a - lg_b));
  350. myHp = (long)(myHp * p2);
  351. }
  352. if (myHp > targetHp)
  353. {
  354. myHp -= targetHp;
  355. a.HpBl = myHp;
  356. }
  357. else if (myHp < targetHp)
  358. {
  359. targetHp -= myHp;
  360. b.HpBl = targetHp;
  361. }
  362. else if (myHp == targetHp)
  363. {
  364. a.HpBl = 0;
  365. b.HpBl = 0;
  366. }
  367. }
  368. public void GongFaPengZhuang(SkillFeaturesData a, SkillFeaturesData b, CombatHeroEntity heroEntityA,
  369. CombatHeroEntity heroEntityB)
  370. {
  371. a.InitPengZhuang();
  372. b.InitPengZhuang();
  373. GongFaPengZhuangFinishEventData gongFaPengZhuStart = GongFaPengZhuangFinishEventData.Create();
  374. gongFaPengZhuStart.a = a;
  375. gongFaPengZhuStart.b = b;
  376. CombatEventManager.Instance.Dispatch(CombatEventType.GongFaPengZhuangStart, gongFaPengZhuStart);
  377. int myRestrained = GetRestrained(a.WuXingType, b.WuXingType);
  378. int targetRestrained = GetRestrained(b.WuXingType, a.WuXingType);
  379. float lg_a = heroEntityA.CurrCombatHeroInfo.GetWuXingShuXing(a.WuXingType);
  380. float lg_b = heroEntityB.CurrCombatHeroInfo.GetWuXingShuXing(b.WuXingType);
  381. int c = myRestrained - targetRestrained;
  382. long myHp = (long)(a.hp);
  383. long targetHp = (long)(b.hp);
  384. a.pengZhuangHp = myHp;
  385. b.pengZhuangHp = targetHp;
  386. float p2 = 100;
  387. if (c < 0) //a被压制
  388. {
  389. p2 = Mathf.Max(100, Mathf.Min(200, 130 + lg_b - lg_a));
  390. targetHp = GetVlaueRatioForLong(targetHp, p2);
  391. }
  392. else if (c > 0)
  393. {
  394. p2 = Mathf.Max(100, Mathf.Min(200, 130 + lg_a - lg_b));
  395. myHp = GetVlaueRatioForLong(myHp, p2);
  396. }
  397. if (a.SkillFeaturesPengZhuangInfo.neutralizeQiangDu > 0)
  398. {
  399. targetHp -= GetVlaueRatioForLong(targetHp, a.SkillFeaturesPengZhuangInfo.neutralizeQiangDu);
  400. }
  401. if (b.SkillFeaturesPengZhuangInfo.neutralizeQiangDu > 0)
  402. {
  403. myHp -= GetVlaueRatioForLong(myHp, b.SkillFeaturesPengZhuangInfo.neutralizeQiangDu);
  404. }
  405. if (targetHp < 0)
  406. {
  407. targetHp = 0;
  408. }
  409. if (myHp < 0)
  410. {
  411. myHp = 0;
  412. }
  413. if (myHp > targetHp)
  414. {
  415. myHp -= targetHp;
  416. a.hp = myHp;
  417. b.hp = 0;
  418. }
  419. else if (myHp < targetHp)
  420. {
  421. targetHp -= myHp;
  422. b.hp = targetHp;
  423. a.hp = 0;
  424. }
  425. else if (myHp == targetHp)
  426. {
  427. a.hp = 0;
  428. b.hp = 0;
  429. }
  430. GongFaPengZhuangFinishEventData gongFaPengZhu = GongFaPengZhuangFinishEventData.Create();
  431. gongFaPengZhu.a = a;
  432. gongFaPengZhu.b = b;
  433. CombatEventManager.Instance.Dispatch(CombatEventType.GongFaPengZhuangFinish, gongFaPengZhu, false);
  434. CombatEventManager.Instance.Dispatch(CombatEventType.GongFaPengZhuangFinish2, gongFaPengZhu);
  435. a.DisposePengZhuang();
  436. b.DisposePengZhuang();
  437. }
  438. /// <summary>
  439. /// 是否相生
  440. /// </summary>
  441. /// <returns></returns>
  442. public bool IsSymbiosis(WuXingType a, WuXingType b)
  443. {
  444. int index = GeWuXingTypeIndex(a);
  445. if (index < 0)
  446. {
  447. return false;
  448. }
  449. return (b & Symbiosis[index]) != 0;
  450. }
  451. /// <summary>
  452. /// 是否相克制
  453. /// </summary>
  454. /// <returns></returns>
  455. public bool IsRestrain(WuXingType a, WuXingType b)
  456. {
  457. int index = GeWuXingTypeIndex(a);
  458. if (index < 0)
  459. {
  460. return false;
  461. }
  462. return (b & Restrain[index]) != 0;
  463. }
  464. public Color GetColor(WuXingType wuXingType)
  465. {
  466. switch ((int)wuXingType)
  467. {
  468. case 1:
  469. return new Color(1f, 0.98f, 0.09f);
  470. break;
  471. case 2:
  472. return new Color(0.19f, 0.51f, 1f);
  473. break;
  474. case 4:
  475. return new Color(0.17f, 1f, 0.35f);
  476. break;
  477. case 8:
  478. return new Color(1f, 0.19f, 0.04f);
  479. break;
  480. case 16:
  481. return new Color(1f, 0.65f, 0.17f);
  482. break;
  483. }
  484. return Color.white;
  485. }
  486. public Color GetColor(int wuXingType)
  487. {
  488. switch ((int)wuXingType)
  489. {
  490. case 1:
  491. return new Color(1f, 0.98f, 0.09f);
  492. break;
  493. case 2:
  494. return new Color(0.19f, 0.51f, 1f);
  495. break;
  496. case 3:
  497. return new Color(0.17f, 1f, 0.35f);
  498. break;
  499. case 4:
  500. return new Color(1f, 0.19f, 0.04f);
  501. break;
  502. case 5:
  503. return new Color(1f, 0.65f, 0.17f);
  504. break;
  505. }
  506. return Color.white;
  507. }
  508. }
  509. }