GameContextDeterminarion.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System;
  4. using System.Linq;
  5. using UnityEngine;
  6. public partial class CardEffectCommons
  7. {
  8. #region Whether the card is in the field
  9. public static bool IsExistOnField(CardSource card)
  10. {
  11. if (card != null)
  12. {
  13. if (card.PermanentOfThisCard() != null)
  14. {
  15. return true;
  16. }
  17. }
  18. return false;
  19. }
  20. #endregion
  21. #region Whether the card is in the Breeding Area
  22. public static bool IsExistOnBreedingArea(CardSource card)
  23. {
  24. if (IsExistOnField(card))
  25. {
  26. if (card.Owner.GetBreedingAreaPermanents().Contains(card.PermanentOfThisCard()))
  27. {
  28. return true;
  29. }
  30. }
  31. return false;
  32. }
  33. #endregion
  34. #region Whether the card is Digimon and in the Breeding Area
  35. public static bool IsExistOnBreedingAreaDigimon(CardSource card)
  36. {
  37. if (IsExistOnField(card))
  38. {
  39. if (card.Owner.GetBreedingAreaPermanents().Contains(card.PermanentOfThisCard()))
  40. {
  41. if (card.PermanentOfThisCard().IsDigimon)
  42. {
  43. return true;
  44. }
  45. }
  46. }
  47. return false;
  48. }
  49. #endregion
  50. #region Whether the card is in the Battle Area
  51. public static bool IsExistOnBattleArea(CardSource card)
  52. {
  53. if (IsExistOnField(card))
  54. {
  55. if (card.Owner.GetBattleAreaPermanents().Contains(card.PermanentOfThisCard()))
  56. {
  57. return true;
  58. }
  59. }
  60. return false;
  61. }
  62. #endregion
  63. #region Whether the card is Digimon and in the Battle Area
  64. public static bool IsExistOnBattleAreaDigimon(CardSource card)
  65. {
  66. if (IsExistOnBattleArea(card))
  67. {
  68. if (card.Owner.GetBattleAreaDigimons().Contains(card.PermanentOfThisCard()))
  69. {
  70. return true;
  71. }
  72. }
  73. return false;
  74. }
  75. #endregion
  76. #region Whether the card is in hand
  77. public static bool IsExistOnHand(CardSource card)
  78. {
  79. if (card.Owner.HandCards.Contains(card))
  80. {
  81. return true;
  82. }
  83. return false;
  84. }
  85. #endregion
  86. #region Whether the card is Linked
  87. public static bool IsExistLinked(CardSource card)
  88. {
  89. if (IsExistOnField(card))
  90. return card.PermanentOfThisCard().LinkedCards.Contains(card);
  91. return false;
  92. }
  93. #endregion
  94. #region Whether the card is in trash
  95. public static bool IsExistOnTrash(CardSource card)
  96. {
  97. if (card.Owner.TrashCards.Contains(card))
  98. {
  99. return true;
  100. }
  101. return false;
  102. }
  103. #endregion
  104. #region Whether the card is in Executing Area
  105. public static bool IsExistOnExecutingArea(CardSource card)
  106. {
  107. if (card.Owner.ExecutingCards.Contains(card))
  108. {
  109. return true;
  110. }
  111. return false;
  112. }
  113. #endregion
  114. #region Whether the card is in Security Area
  115. public static bool IsExistInSecurity(CardSource card, bool isFlipped = false)
  116. {
  117. if (card.Owner.SecurityCards.Contains(card))
  118. return card.IsFlipped == isFlipped;
  119. return false;
  120. }
  121. #endregion
  122. #region Whether the card Can be played as a new permanent
  123. public static bool CanPlayAsNewPermanent(
  124. CardSource cardSource,
  125. bool payCost,
  126. ICardEffect cardEffect,
  127. SelectCardEffect.Root root = SelectCardEffect.Root.Hand,
  128. bool isBreedingArea = false,
  129. bool isPlayOption = false,
  130. int fixedCost = -1) =>
  131. cardSource != null &&
  132. (isPlayOption || !cardSource.IsOption) &&
  133. !GManager.instance.GetComponent<SelectDigiXrosClass>().selectedDigicrossCards.Contains(cardSource) &&
  134. !GManager.instance.GetComponent<SelectAssemblyClass>().selectedAssemblyCards.Contains(cardSource)
  135. && cardSource.Owner.fieldCardFrames.Some((frame) =>
  136. frame.IsEmptyFrame()
  137. && cardSource.CanPlayCardTargetFrame(frame, payCost, cardEffect, root, isBreedingArea: isBreedingArea, fixedCost: fixedCost));
  138. #endregion
  139. #region Whether the permanent is in the Field
  140. public static bool IsPermanentExistsOnField(Permanent permanent)
  141. {
  142. if (permanent != null)
  143. {
  144. if (permanent.TopCard != null)
  145. {
  146. if (permanent.TopCard.Owner.GetBreedingAreaPermanents().Contains(permanent))
  147. {
  148. return true;
  149. }
  150. if (permanent.TopCard.Owner.GetBattleAreaPermanents().Contains(permanent))
  151. {
  152. return true;
  153. }
  154. }
  155. }
  156. return false;
  157. }
  158. #endregion
  159. #region Whether the permanent is in the Battle Area
  160. public static bool IsPermanentExistsOnBattleArea(Permanent permanent)
  161. {
  162. if (permanent != null)
  163. {
  164. if (permanent.TopCard != null)
  165. {
  166. if (permanent.TopCard.Owner.GetBattleAreaPermanents().Contains(permanent))
  167. {
  168. return true;
  169. }
  170. }
  171. }
  172. return false;
  173. }
  174. #endregion
  175. #region Whether the permanent is in the Breeding Area
  176. public static bool IsPermanentExistsOnBreedingArea(Permanent permanent)
  177. {
  178. if (permanent != null)
  179. {
  180. if (permanent.TopCard != null)
  181. {
  182. if (permanent.TopCard.Owner.GetBreedingAreaPermanents().Contains(permanent))
  183. {
  184. return true;
  185. }
  186. }
  187. }
  188. return false;
  189. }
  190. #endregion
  191. #region Whether the permanent is card's owner's
  192. public static bool IsOwnerPermanent(Permanent permanent, CardSource card)
  193. {
  194. if (permanent != null)
  195. {
  196. if (permanent.TopCard != null)
  197. {
  198. if (card != null)
  199. {
  200. if (permanent.TopCard.Owner == card.Owner)
  201. {
  202. return true;
  203. }
  204. }
  205. }
  206. }
  207. return false;
  208. }
  209. #endregion
  210. #region Whether the permanent is card's owner's opponent's
  211. public static bool IsOpponentPermanent(Permanent permanent, CardSource card)
  212. {
  213. if (permanent != null)
  214. {
  215. if (permanent.TopCard != null)
  216. {
  217. if (permanent.TopCard.Owner == card.Owner.Enemy)
  218. {
  219. return true;
  220. }
  221. }
  222. }
  223. return false;
  224. }
  225. #endregion
  226. #region Whether the permanent is in the card's owner's Battle Area
  227. public static bool IsPermanentExistsOnOwnerBattleArea(Permanent permanent, CardSource card)
  228. {
  229. if (IsPermanentExistsOnBattleArea(permanent))
  230. {
  231. if (IsOwnerPermanent(permanent, card))
  232. {
  233. return true;
  234. }
  235. }
  236. return false;
  237. }
  238. #endregion
  239. #region Whether the permanent is in the card's owner's opponent's Battle Area
  240. public static bool IsPermanentExistsOnOpponentBattleArea(Permanent permanent, CardSource card)
  241. {
  242. if (IsPermanentExistsOnBattleArea(permanent))
  243. {
  244. if (IsOpponentPermanent(permanent, card))
  245. {
  246. return true;
  247. }
  248. }
  249. return false;
  250. }
  251. #endregion
  252. #region Whether the permanent is in the card's owner's Breeding Area
  253. public static bool IsPermanentExistsOnOwnerBreedingArea(Permanent permanent, CardSource card)
  254. {
  255. if (IsPermanentExistsOnBreedingArea(permanent))
  256. {
  257. if (IsOwnerPermanent(permanent, card))
  258. {
  259. return true;
  260. }
  261. }
  262. return false;
  263. }
  264. #endregion
  265. #region Whether the permanent is in the card's owner's opponent's Breeding Area
  266. public static bool IsPermanentExistsOnOpponentBreedingArea(Permanent permanent, CardSource card)
  267. {
  268. if (IsPermanentExistsOnBreedingArea(permanent))
  269. {
  270. if (IsOpponentPermanent(permanent, card))
  271. {
  272. return true;
  273. }
  274. }
  275. return false;
  276. }
  277. #endregion
  278. #region Whether the permanent is Digimon and in the Battle Area
  279. public static bool IsPermanentExistsOnBattleAreaDigimon(Permanent permanent)
  280. {
  281. if (IsPermanentExistsOnBattleArea(permanent))
  282. {
  283. if (permanent.IsDigimon)
  284. {
  285. return true;
  286. }
  287. }
  288. return false;
  289. }
  290. #endregion
  291. #region Whether the permanent is Digimon and in the card's owner's Battle Area
  292. public static bool IsPermanentExistsOnOwnerBattleAreaDigimon(Permanent permanent, CardSource card)
  293. {
  294. if (IsPermanentExistsOnOwnerBattleArea(permanent, card))
  295. {
  296. if (permanent.IsDigimon)
  297. {
  298. return true;
  299. }
  300. }
  301. return false;
  302. }
  303. #endregion
  304. #region Whether the permanent is Digimon and in the card's owner's opponent's Battle Area
  305. public static bool IsPermanentExistsOnOpponentBattleAreaDigimon(Permanent permanent, CardSource card)
  306. {
  307. if (IsPermanentExistsOnOpponentBattleArea(permanent, card))
  308. {
  309. if (permanent.IsDigimon)
  310. {
  311. return true;
  312. }
  313. }
  314. return false;
  315. }
  316. #endregion
  317. #region Whether the permanent is Tamer and in the Battle Area
  318. public static bool IsPermanentExistsOnBattleAreaTamer(Permanent permanent)
  319. {
  320. if (IsPermanentExistsOnBattleArea(permanent))
  321. {
  322. if (permanent.IsTamer)
  323. {
  324. return true;
  325. }
  326. }
  327. return false;
  328. }
  329. #endregion
  330. #region Whether the permanent is Tamer and in the card's owner's Battle Area
  331. public static bool IsPermanentExistsOnOwnerBattleAreaTamer(Permanent permanent, CardSource card)
  332. {
  333. if (IsPermanentExistsOnOwnerBattleArea(permanent, card))
  334. {
  335. if (permanent.IsTamer)
  336. {
  337. return true;
  338. }
  339. }
  340. return false;
  341. }
  342. #endregion
  343. #region Whether the permanent is Tamer and in the card's owner's opponent's Battle Area
  344. public static bool IsPermanentExistsOnOpponentBattleAreaTamer(Permanent permanent, CardSource card)
  345. {
  346. if (IsPermanentExistsOnOpponentBattleArea(permanent, card))
  347. {
  348. if (permanent.IsTamer)
  349. {
  350. return true;
  351. }
  352. }
  353. return false;
  354. }
  355. #endregion
  356. #region How many permanents there are in the Battle Area that satisfy the condition
  357. public static int MatchConditionPermanentCount(Func<Permanent, bool> CanSelectPermanentCondition, bool isContainBreedingArea = false)
  358. {
  359. if (isContainBreedingArea)
  360. {
  361. return GManager.instance.turnStateMachine.gameContext.Players
  362. .Map(player => player.GetFieldPermanents())
  363. .Flat()
  364. .Count(CanSelectPermanentCondition);
  365. }
  366. else
  367. {
  368. return GManager.instance.turnStateMachine.gameContext.Players
  369. .Map(player => player.GetBattleAreaPermanents())
  370. .Flat()
  371. .Count(CanSelectPermanentCondition);
  372. }
  373. }
  374. #endregion
  375. #region How many permanents there are in the owner's Battle Area that satisfy the condition
  376. public static int MatchConditionOwnersPermanentCount(CardSource card, Func<Permanent, bool> CanSelectPermanentCondition)
  377. {
  378. return MatchConditionPermanentCount(permanent => CanSelectPermanentCondition(permanent) && IsOwnerPermanent(permanent, card));
  379. }
  380. #endregion
  381. #region How many permanents there are in the owner's opponent's Battle Area that satisfy the condition
  382. public static int MatchConditionOpponentsPermanentCount(CardSource card, Func<Permanent, bool> CanSelectPermanentCondition)
  383. {
  384. return MatchConditionPermanentCount(permanent => CanSelectPermanentCondition(permanent) && IsOpponentPermanent(permanent, card));
  385. }
  386. #endregion
  387. #region Whether there is at least 1 permanent in the Battle Area that satisfies the condition
  388. public static bool HasMatchConditionPermanent(Func<Permanent, bool> CanSelectPermanentCondition, bool isContainBreedingArea = false)
  389. {
  390. if (isContainBreedingArea)
  391. {
  392. return GManager.instance.turnStateMachine.gameContext.Players
  393. .Map(player => player.GetFieldPermanents())
  394. .Flat()
  395. .Some(CanSelectPermanentCondition);
  396. }
  397. else
  398. {
  399. return GManager.instance.turnStateMachine.gameContext.Players
  400. .Map(player => player.GetBattleAreaPermanents())
  401. .Flat()
  402. .Some(CanSelectPermanentCondition);
  403. }
  404. }
  405. #endregion
  406. #region Whether there is at least 1 card in the owner's hand that satisfies the condition
  407. public static bool HasMatchConditionOwnersHand(CardSource card, Func<CardSource, bool> CanSelectCardCondition)
  408. {
  409. return card.Owner.HandCards.Some(CanSelectCardCondition);
  410. }
  411. #endregion
  412. #region How many cards there are in the owner's hand that satisfy the condition
  413. public static int MatchConditionOwnersCardCountInHand(CardSource card, Func<CardSource, bool> CanSelectCardCondition)
  414. {
  415. return card.Owner.HandCards.Count(CanSelectCardCondition);
  416. }
  417. #endregion
  418. #region Whether there is at least 1 permanent in the owner's Battle Area that satisfies the condition
  419. public static bool HasMatchConditionOwnersPermanent(CardSource card, Func<Permanent, bool> CanSelectPermanentCondition)
  420. {
  421. return GManager.instance.turnStateMachine.gameContext.Players
  422. .Map(player => player.GetBattleAreaPermanents())
  423. .Flat()
  424. .Some(permanent => IsOwnerPermanent(permanent, card) && CanSelectPermanentCondition(permanent));
  425. }
  426. #endregion
  427. #region Whether there is at least 1 permanent in the owner's Breeding Area that satisfies the condition
  428. public static bool HasMatchConditionOwnersBreedingPermanent(CardSource card, Func<Permanent, bool> CanSelectPermanentCondition)
  429. {
  430. return GManager.instance.turnStateMachine.gameContext.Players
  431. .Map(player => player.GetBreedingAreaPermanents())
  432. .Flat()
  433. .Some(permanent => IsOwnerPermanent(permanent, card) && CanSelectPermanentCondition(permanent));
  434. }
  435. #endregion
  436. #region Whether there is at least 1 source in this cards sources that satisfies the condition
  437. public static bool HasMatchConditionPermanentDigivolutionCards(CardSource card, Func<CardSource, bool> CanSelectPermanentCondition)
  438. {
  439. return card.PermanentOfThisCard().DigivolutionCards.Some(cardSource => CanSelectPermanentCondition(cardSource));
  440. }
  441. #endregion
  442. #region Whether there is at least 1 permanent in the owner's opponent's Battle Area that satisfies the condition
  443. public static bool HasMatchConditionOpponentsPermanent(CardSource card, Func<Permanent, bool> CanSelectPermanentCondition)
  444. {
  445. return GManager.instance.turnStateMachine.gameContext.Players
  446. .Map(player => player.GetBattleAreaPermanents())
  447. .Flat()
  448. .Some(permanent => IsOpponentPermanent(permanent, card) && CanSelectPermanentCondition(permanent));
  449. }
  450. #endregion
  451. #region Whether there is at least 1 permanent in the owner's Security Area that satisfies the condition
  452. public static bool HasMatchConditionOwnersSecurity(CardSource card, Func<CardSource, bool> CanSelectPermanentCondition, bool flipped = true)
  453. {
  454. return GManager.instance.turnStateMachine.gameContext.Players
  455. .Map(player => player.SecurityCards)
  456. .Flat()
  457. .Some(cardSource => cardSource.IsFlipped == flipped && CanSelectPermanentCondition(cardSource) && cardSource.Owner == card.Owner);
  458. }
  459. #endregion
  460. #region How many cards there are in the owner's trash that satisfy the condition
  461. public static int MatchConditionOwnersCardCountInTrash(CardSource card, Func<CardSource, bool> CanSelectCardCondition)
  462. {
  463. return card.Owner.TrashCards.Count(CanSelectCardCondition);
  464. }
  465. #endregion
  466. #region How many cards there are in the owner's opponent's trash that satisfy the condition
  467. public static int MatchConditionOpponentsCardCountInTrash(CardSource card, Func<CardSource, bool> CanSelectCardCondition)
  468. {
  469. return card.Owner.Enemy.TrashCards.Count(CanSelectCardCondition);
  470. }
  471. #endregion
  472. #region Whether there is at least 1 card in the owner's trash that satisfies the condition
  473. public static bool HasMatchConditionOwnersCardInTrash(CardSource card, Func<CardSource, bool> CanSelectCardCondition)
  474. {
  475. return card.Owner.TrashCards.Some(CanSelectCardCondition);
  476. }
  477. #endregion
  478. #region Whether there is at least 1 card in the owner's opponent's trash that satisfies the condition
  479. public static bool HasMatchConditionOpponentsCardInTrash(CardSource card, Func<CardSource, bool> CanSelectCardCondition)
  480. {
  481. return card.Owner.Enemy.TrashCards.Some(CanSelectCardCondition);
  482. }
  483. #endregion
  484. #region Whether the list has no element
  485. public static bool HasNoElement<T>(List<T> list) => list.Count <= 0;
  486. #endregion
  487. #region Wheter it is the player's turn
  488. public static bool IsOwnerTurn(CardSource card) => GManager.instance.turnStateMachine.gameContext.TurnPlayer == card.Owner;
  489. public static bool IsOpponentTurn(CardSource card) => !IsOwnerTurn(card);
  490. #endregion
  491. #region Whether the effect is your effect
  492. public static bool IsOwnerEffect(ICardEffect cardEffect, CardSource card)
  493. {
  494. if (cardEffect != null)
  495. {
  496. if (cardEffect.EffectSourceCard != null)
  497. {
  498. if (cardEffect.EffectSourceCard.Owner == card.Owner)
  499. {
  500. return true;
  501. }
  502. }
  503. }
  504. return false;
  505. }
  506. #endregion
  507. #region Whether the effect is opponent's effect
  508. public static bool IsOpponentEffect(ICardEffect cardEffect, CardSource card)
  509. {
  510. if (cardEffect != null)
  511. {
  512. if (cardEffect.EffectSourceCard != null)
  513. {
  514. if (cardEffect.EffectSourceCard.Owner == card.Owner.Enemy)
  515. {
  516. return true;
  517. }
  518. }
  519. }
  520. return false;
  521. }
  522. #endregion
  523. #region Get unique colour count from permanents in owners battle area
  524. public static int GetUniqueColourCountOnOwnerBattleArea(CardSource card, Func<Permanent, bool> canGetCardColour)
  525. {
  526. var uniqueColors = card.Owner.GetBattleAreaPermanents()
  527. .Filter(x => canGetCardColour(x))
  528. .Select(x => x.TopCard)
  529. .SelectMany(x => x.CardColors)
  530. .Distinct()
  531. .ToHashSet();
  532. return uniqueColors.Count;
  533. }
  534. #endregion
  535. #region Get unique colour count from permanents in opponents battle area
  536. public static int GetUniqueColourCountOnOpponentsBattleArea(CardSource card, Func<Permanent, bool> canGetCardColour)
  537. {
  538. var uniqueColors = card.Owner.Enemy.GetBattleAreaPermanents()
  539. .Filter(x => canGetCardColour(x))
  540. .Select(x => x.TopCard)
  541. .SelectMany(x => x.CardColors)
  542. .Distinct()
  543. .ToHashSet();
  544. return uniqueColors.Count;
  545. }
  546. #endregion
  547. #region Does Owner has 1 or less tamers on the field
  548. public static bool OwnerHas1OrLessTamers(CardSource card)
  549. {
  550. return card.Owner.GetBattleAreaPermanents()
  551. .Count(permanent => permanent.IsTamer && permanent.TopCard.Owner == card.Owner) <= 1;
  552. }
  553. #endregion
  554. }