CombatCalculateTool.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. if (att <= 0)
  170. {
  171. att = 1;
  172. }
  173. harmReturnInfo.att = att;
  174. target.combatHeroEntity.This<CombatHeroEntity>().HeroHurt(harmReturnInfo);
  175. return harmReturnInfo;
  176. }
  177. public HarmReturnInfo TrueHarm(CombatHeroEntity source, CombatHeroHitPoint target, long att,
  178. AttType attType, TriggerData triggerData,
  179. HarmType harmType = HarmType.Default)
  180. {
  181. HarmReturnInfo harmReturnInfo = CObjectPool.Instance.Fetch<HarmReturnInfo>();
  182. harmReturnInfo.source = source;
  183. harmReturnInfo.target = target;
  184. harmReturnInfo.att = att;
  185. harmReturnInfo.attType = attType;
  186. harmReturnInfo.harmType = harmType;
  187. harmReturnInfo.triggerData = triggerData;
  188. if (target.combatHeroEntity.isDie)
  189. {
  190. return harmReturnInfo;
  191. }
  192. target.combatHeroEntity.This<CombatHeroEntity>().HeroHurt(harmReturnInfo);
  193. return harmReturnInfo;
  194. }
  195. public HarmReturnInfo Recover(CombatHeroEntity source, CombatHeroHitPoint target, long att,
  196. AttType attType, HarmType harmType, TriggerData triggerData)
  197. {
  198. harmType |= HarmType.Recover;
  199. HarmReturnInfo harmReturnInfo = new HarmReturnInfo();
  200. harmReturnInfo.source = source;
  201. harmReturnInfo.target = target;
  202. harmReturnInfo.att = att;
  203. harmReturnInfo.attType = attType;
  204. harmReturnInfo.harmType = harmType;
  205. harmReturnInfo.triggerData = triggerData;
  206. if (target.combatHeroEntity.isDie)
  207. {
  208. return harmReturnInfo;
  209. }
  210. target.combatHeroEntity.This<CombatHeroEntity>().Recover(harmReturnInfo);
  211. return harmReturnInfo;
  212. }
  213. public ILifetCycleHitPoint[] GetMinHpHero(ILifetCycleHitPoint[] allLifetCycleHitPoints, int count)
  214. {
  215. if (allLifetCycleHitPoints == null)
  216. {
  217. return null;
  218. }
  219. BetterList<ILifetCycleHitPoint> findHero = new BetterList<ILifetCycleHitPoint>();
  220. findHero.AddRange(allLifetCycleHitPoints);
  221. int currCount = Math.Min(allLifetCycleHitPoints.Length, count);
  222. ILifetCycleHitPoint[] minHpHero = new ILifetCycleHitPoint[currCount];
  223. for (int k = 0; k < currCount; k++)
  224. {
  225. CombatHeroEntity lifetCycleHitPoint = null;
  226. int index = 0;
  227. if (findHero.Count <= 0)
  228. {
  229. return minHpHero;
  230. }
  231. lifetCycleHitPoint = findHero[0].IfLifeCycle.This<CombatHeroEntity>();
  232. for (int j = 0; j < findHero.Count; j++)
  233. {
  234. CombatHeroEntity lifetCycleHitPoint2 =
  235. findHero[j].IfLifeCycle.This<CombatHeroEntity>();
  236. if (lifetCycleHitPoint2.HpBl < lifetCycleHitPoint.HpBl)
  237. {
  238. lifetCycleHitPoint = lifetCycleHitPoint2;
  239. index = j;
  240. }
  241. }
  242. ILifetCycleHitPoint currFindHitPoint = lifetCycleHitPoint.GetMainHotPoin<ILifetCycleHitPoint>(true);
  243. minHpHero[k] = currFindHitPoint;
  244. findHero.RemoveAt(index);
  245. }
  246. return minHpHero;
  247. }
  248. private int GeWuXingTypeIndex(WuXingType e)
  249. {
  250. switch (e)
  251. {
  252. case WuXingType.Gold: return 0;
  253. case WuXingType.Wood: return 1;
  254. case WuXingType.Water: return 2;
  255. case WuXingType.Fire: return 3;
  256. case WuXingType.Earth: return 4;
  257. default: return 5; // 无效元素
  258. }
  259. }
  260. public int GetRestrained(WuXingType WuXingType, WuXingType targetWuXingType)
  261. {
  262. int c = 0;
  263. if (WuXingType.HasFlag(WuXingType.Gold))
  264. {
  265. if (targetWuXingType.HasFlag(WuXingType.Wood))
  266. {
  267. c++;
  268. }
  269. }
  270. if (WuXingType.HasFlag(WuXingType.Wood))
  271. {
  272. if (targetWuXingType.HasFlag(WuXingType.Earth))
  273. {
  274. c++;
  275. }
  276. }
  277. if (WuXingType.HasFlag(WuXingType.Water))
  278. {
  279. if (targetWuXingType.HasFlag(WuXingType.Fire))
  280. {
  281. c++;
  282. }
  283. }
  284. if (WuXingType.HasFlag(WuXingType.Fire))
  285. {
  286. if (targetWuXingType.HasFlag(WuXingType.Gold))
  287. {
  288. c++;
  289. }
  290. }
  291. if (WuXingType.HasFlag(WuXingType.Earth))
  292. {
  293. if (targetWuXingType.HasFlag(WuXingType.Wood))
  294. {
  295. c++;
  296. }
  297. }
  298. return c;
  299. }
  300. public long GetFaBaoDuiPingMaxValue(CombatMagicWeaponEntity a, CombatMagicWeaponEntity b)
  301. {
  302. CombatHeroEntity heroEntityA = a.RootMagicWeaponControl.combatHeroEntity;
  303. CombatHeroEntity heroEntityB = b.RootMagicWeaponControl.combatHeroEntity;
  304. int myRestrained = GetRestrained(a.WuXingType, b.WuXingType);
  305. int targetRestrained = GetRestrained(b.WuXingType, a.WuXingType);
  306. float lg_a = heroEntityA.CurrCombatHeroInfo.GetWuXingShuXing(a.WuXingType);
  307. float lg_b = heroEntityB.CurrCombatHeroInfo.GetWuXingShuXing(b.WuXingType);
  308. int c = myRestrained - targetRestrained;
  309. long myHp = (long)(a.HpBl);
  310. long targetHp = (long)(b.HpBl);
  311. float p2 = 100;
  312. if (c < 0) //a被压制
  313. {
  314. p2 = Mathf.Max(100, Mathf.Min(200, 30 + lg_b - lg_a));
  315. targetHp = (long)(targetHp * p2);
  316. }
  317. else if (c > 0)
  318. {
  319. p2 = Mathf.Max(100, Mathf.Min(200, 30 + lg_a - lg_b));
  320. myHp = (long)(myHp * p2);
  321. }
  322. a.HpBl = myHp;
  323. b.HpBl = targetHp;
  324. if (myHp > targetHp)
  325. {
  326. return targetHp;
  327. }
  328. else if (myHp < targetHp)
  329. {
  330. return myHp;
  331. }
  332. return targetHp;
  333. }
  334. public void FaBaoPengZhuang(CombatMagicWeaponEntity a, CombatMagicWeaponEntity b)
  335. {
  336. CombatHeroEntity heroEntityA = a.RootMagicWeaponControl.combatHeroEntity;
  337. CombatHeroEntity heroEntityB = b.RootMagicWeaponControl.combatHeroEntity;
  338. int myRestrained = GetRestrained(a.WuXingType, b.WuXingType);
  339. int targetRestrained = GetRestrained(b.WuXingType, a.WuXingType);
  340. float lg_a = heroEntityA.CurrCombatHeroInfo.GetWuXingShuXing(a.WuXingType);
  341. float lg_b = heroEntityB.CurrCombatHeroInfo.GetWuXingShuXing(b.WuXingType);
  342. int c = myRestrained - targetRestrained;
  343. long myHp = (long)(a.HpBl);
  344. long targetHp = (long)(b.HpBl);
  345. float p2 = 100;
  346. if (c < 0) //a被压制
  347. {
  348. p2 = Mathf.Max(100, Mathf.Min(200, 30 + lg_b - lg_a));
  349. targetHp = (long)(targetHp * p2);
  350. }
  351. else if (c > 0)
  352. {
  353. p2 = Mathf.Max(100, Mathf.Min(200, 30 + lg_a - lg_b));
  354. myHp = (long)(myHp * p2);
  355. }
  356. if (myHp > targetHp)
  357. {
  358. myHp -= targetHp;
  359. a.HpBl = myHp;
  360. }
  361. else if (myHp < targetHp)
  362. {
  363. targetHp -= myHp;
  364. b.HpBl = targetHp;
  365. }
  366. else if (myHp == targetHp)
  367. {
  368. a.HpBl = 0;
  369. b.HpBl = 0;
  370. }
  371. }
  372. public void GongFaPengZhuang(SkillFeaturesData a, SkillFeaturesData b, CombatHeroEntity heroEntityA,
  373. CombatHeroEntity heroEntityB)
  374. {
  375. a.InitPengZhuang();
  376. b.InitPengZhuang();
  377. GongFaPengZhuangFinishEventData gongFaPengZhuStart = GongFaPengZhuangFinishEventData.Create();
  378. gongFaPengZhuStart.a = a;
  379. gongFaPengZhuStart.b = b;
  380. CombatEventManager.Instance.Dispatch(CombatEventType.GongFaPengZhuangStart, gongFaPengZhuStart);
  381. int myRestrained = GetRestrained(a.WuXingType, b.WuXingType);
  382. int targetRestrained = GetRestrained(b.WuXingType, a.WuXingType);
  383. float lg_a = heroEntityA.CurrCombatHeroInfo.GetWuXingShuXing(a.WuXingType);
  384. float lg_b = heroEntityB.CurrCombatHeroInfo.GetWuXingShuXing(b.WuXingType);
  385. int c = myRestrained - targetRestrained;
  386. long myHp = (long)(a.hp);
  387. long targetHp = (long)(b.hp);
  388. a.pengZhuangHp = myHp;
  389. b.pengZhuangHp = targetHp;
  390. float p2 = 100;
  391. if (c < 0) //a被压制
  392. {
  393. p2 = Mathf.Max(100, Mathf.Min(200, 130 + lg_b - lg_a));
  394. targetHp = GetVlaueRatioForLong(targetHp, p2);
  395. }
  396. else if (c > 0)
  397. {
  398. p2 = Mathf.Max(100, Mathf.Min(200, 130 + lg_a - lg_b));
  399. myHp = GetVlaueRatioForLong(myHp, p2);
  400. }
  401. if (a.SkillFeaturesPengZhuangInfo.neutralizeQiangDu > 0)
  402. {
  403. targetHp -= GetVlaueRatioForLong(targetHp, a.SkillFeaturesPengZhuangInfo.neutralizeQiangDu);
  404. }
  405. if (b.SkillFeaturesPengZhuangInfo.neutralizeQiangDu > 0)
  406. {
  407. myHp -= GetVlaueRatioForLong(myHp, b.SkillFeaturesPengZhuangInfo.neutralizeQiangDu);
  408. }
  409. if (targetHp < 0)
  410. {
  411. targetHp = 0;
  412. }
  413. if (myHp < 0)
  414. {
  415. myHp = 0;
  416. }
  417. if (myHp > targetHp)
  418. {
  419. myHp -= targetHp;
  420. a.hp = myHp;
  421. b.hp = 0;
  422. }
  423. else if (myHp < targetHp)
  424. {
  425. targetHp -= myHp;
  426. b.hp = targetHp;
  427. a.hp = 0;
  428. }
  429. else if (myHp == targetHp)
  430. {
  431. a.hp = 0;
  432. b.hp = 0;
  433. }
  434. GongFaPengZhuangFinishEventData gongFaPengZhu = GongFaPengZhuangFinishEventData.Create();
  435. gongFaPengZhu.a = a;
  436. gongFaPengZhu.b = b;
  437. CombatEventManager.Instance.Dispatch(CombatEventType.GongFaPengZhuangFinish, gongFaPengZhu, false);
  438. CombatEventManager.Instance.Dispatch(CombatEventType.GongFaPengZhuangFinish2, gongFaPengZhu);
  439. a.DisposePengZhuang();
  440. b.DisposePengZhuang();
  441. }
  442. /// <summary>
  443. /// 是否相生
  444. /// </summary>
  445. /// <returns></returns>
  446. public bool IsSymbiosis(WuXingType a, WuXingType b)
  447. {
  448. int index = GeWuXingTypeIndex(a);
  449. if (index < 0)
  450. {
  451. return false;
  452. }
  453. return (b & Symbiosis[index]) != 0;
  454. }
  455. /// <summary>
  456. /// 是否相克制
  457. /// </summary>
  458. /// <returns></returns>
  459. public bool IsRestrain(WuXingType a, WuXingType b)
  460. {
  461. int index = GeWuXingTypeIndex(a);
  462. if (index < 0)
  463. {
  464. return false;
  465. }
  466. return (b & Restrain[index]) != 0;
  467. }
  468. public Color GetColor(WuXingType wuXingType)
  469. {
  470. switch ((int)wuXingType)
  471. {
  472. case 1:
  473. return new Color(1f, 0.98f, 0.09f);
  474. break;
  475. case 2:
  476. return new Color(0.19f, 0.51f, 1f);
  477. break;
  478. case 4:
  479. return new Color(0.17f, 1f, 0.35f);
  480. break;
  481. case 8:
  482. return new Color(1f, 0.19f, 0.04f);
  483. break;
  484. case 16:
  485. return new Color(1f, 0.65f, 0.17f);
  486. break;
  487. }
  488. return new Color(1, 1, 1, 0.3f);
  489. }
  490. public Color GetColor(int wuXingType)
  491. {
  492. switch ((int)wuXingType)
  493. {
  494. case 1:
  495. return new Color(1f, 0.98f, 0.09f);
  496. break;
  497. case 2:
  498. return new Color(0.19f, 0.51f, 1f);
  499. break;
  500. case 3:
  501. return new Color(0.17f, 1f, 0.35f);
  502. break;
  503. case 4:
  504. return new Color(1f, 0.19f, 0.04f);
  505. break;
  506. case 5:
  507. return new Color(1f, 0.65f, 0.17f);
  508. break;
  509. }
  510. return Color.white;
  511. }
  512. }
  513. }