GameContextDeterminarion.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. public partial class CardEffectCommons
  5. {
  6. public static Dictionary<ICardEffect, Permanent> CardPermanenceMap = new Dictionary<ICardEffect, Permanent>();
  7. public static Dictionary<ICardEffect, SelectCardEffect.Root> CardLocationMap = new Dictionary<ICardEffect, SelectCardEffect.Root>();
  8. public static Dictionary<ICardEffect, CardSource> OnDeletionCardMap = new Dictionary<ICardEffect, CardSource>();
  9. #region Invalidate Cards that are not in their correct location
  10. private static Permanent FailurePermanent = new(new List<CardSource>());
  11. private static SelectCardEffect.Root FailureLocation = SelectCardEffect.Root.None;
  12. public static void EnforceLocationCheck()
  13. {
  14. foreach(ICardEffect cardEffect in CardPermanenceMap.Keys.ToList().Clone())
  15. {
  16. if (cardEffect.EffectSourceCard.PermanentOfThisCard() != CardPermanenceMap[cardEffect])
  17. CardPermanenceMap[cardEffect] = FailurePermanent;//Mark as a Permanent that nothing else should ever be to ensure it will fail -Activate checks
  18. }
  19. foreach(ICardEffect cardEffect in CardLocationMap.Keys.ToList().Clone())
  20. {
  21. if (!IsCorrectLocation(cardEffect))
  22. CardLocationMap[cardEffect] = FailureLocation;
  23. }
  24. foreach(ICardEffect cardEffect in OnDeletionCardMap.Keys.ToList().Clone())
  25. {
  26. if (!IsTopCardStillInTrash(cardEffect))
  27. {
  28. OnDeletionCardMap[cardEffect] = null;
  29. }
  30. }
  31. }
  32. public static void ClearEffectLocations()
  33. {
  34. CardPermanenceMap = new Dictionary<ICardEffect, Permanent>();
  35. CardLocationMap = new Dictionary<ICardEffect, SelectCardEffect.Root>();
  36. OnDeletionCardMap = new Dictionary<ICardEffect, CardSource>();
  37. }
  38. #endregion
  39. #region Card Location Helper methods
  40. public static bool IsCorrectLocation(ICardEffect cardEffect)
  41. {
  42. SelectCardEffect.Root root = FailureLocation;
  43. if (cardEffect != null && cardEffect.EffectSourceCard != null && CardLocationMap.TryGetValue(cardEffect, out root))
  44. {
  45. CardSource cardSource = cardEffect.EffectSourceCard;
  46. switch(root)
  47. {
  48. case SelectCardEffect.Root.Trash:
  49. return IsExistOnTrash(cardSource);
  50. case SelectCardEffect.Root.Security:
  51. return IsExistInSecurity(cardSource) && cardSource.IsFaceUp;
  52. case SelectCardEffect.Root.Hand:
  53. return IsExistOnHand(cardSource);
  54. case SelectCardEffect.Root.None:
  55. return false;
  56. }
  57. }
  58. return false;
  59. }
  60. #endregion
  61. #region OnDeletion Capture and Check
  62. public static void RegisterOnDeletion(Permanent permanent, ICardEffect cardEffect)
  63. {
  64. if(permanent == null || permanent.TopCard == null)
  65. return;
  66. OnDeletionCardMap[cardEffect] = permanent.TopCard;
  67. }
  68. public static bool IsTopCardStillInTrash(ICardEffect cardEffect)
  69. {
  70. return cardEffect != null && OnDeletionCardMap.TryGetValue(cardEffect, out CardSource card) && card != null && IsExistOnTrash(card);
  71. }
  72. #endregion
  73. #region Trigger conditions - Capture current Permanent for card
  74. public static bool IsExistOnFieldTrigger(CardSource card, ICardEffect cardEffect)
  75. {
  76. bool exists = IsExistOnField(card);
  77. if (exists)
  78. CardPermanenceMap[cardEffect] = card.PermanentOfThisCard();
  79. return exists;
  80. }
  81. public static bool IsExistOnBreedingAreaTrigger(CardSource card, ICardEffect cardEffect)
  82. {
  83. bool exists = IsExistOnBreedingArea(card);
  84. if (exists)
  85. CardPermanenceMap[cardEffect] = card.PermanentOfThisCard();
  86. return exists;
  87. }
  88. public static bool IsExistOnBattleAreaTrigger(CardSource card, ICardEffect cardEffect)
  89. {
  90. bool exists = IsExistOnBattleArea(card);
  91. if (exists)
  92. CardPermanenceMap[cardEffect] = card.PermanentOfThisCard();
  93. return exists;
  94. }
  95. public static bool IsExistOnBattleAreaDigimonTrigger(CardSource card, ICardEffect cardEffect)
  96. {
  97. bool exists = IsExistOnBattleAreaDigimon(card);
  98. if (exists)
  99. CardPermanenceMap[cardEffect] = card.PermanentOfThisCard();
  100. return exists;
  101. }
  102. public static bool IsExistDigivolutionCardsTrigger(CardSource card, ICardEffect cardEffect)
  103. {
  104. bool exists = IsExistDigivolutionCards(card);
  105. if (exists)
  106. CardPermanenceMap[cardEffect] = card.PermanentOfThisCard();
  107. return exists;
  108. }
  109. public static bool IsExistLinkedTrigger(CardSource card, ICardEffect cardEffect)
  110. {
  111. bool exists = IsExistLinked(card);
  112. if (exists)
  113. CardPermanenceMap[cardEffect] = card.PermanentOfThisCard();
  114. return exists;
  115. }
  116. public static bool IsExistOnTrashTrigger(CardSource card, ICardEffect cardEffect)
  117. {
  118. bool exists = IsExistOnTrash(card);
  119. if (exists)
  120. CardLocationMap[cardEffect] = SelectCardEffect.Root.Trash;
  121. return exists;
  122. }
  123. public static bool IsExistInSecurityTrigger(CardSource card, ICardEffect cardEffect)
  124. {
  125. bool exists = IsExistInSecurity(card);
  126. if (exists)
  127. CardLocationMap[cardEffect] = SelectCardEffect.Root.Security;
  128. return exists;
  129. }
  130. public static bool IsExistOnHandTrigger(CardSource card, ICardEffect cardEffect)
  131. {
  132. bool exists = IsExistOnHand(card);
  133. if (exists)
  134. CardLocationMap[cardEffect] = SelectCardEffect.Root.Hand;
  135. return exists;
  136. }
  137. #endregion
  138. #region Activate conditions - Check card is still part of the same Permanent
  139. public static bool IsExistOnFieldActivate(CardSource card, ICardEffect cardEffect)
  140. {
  141. Permanent permanent = null;
  142. return IsExistOnField(card) && CardPermanenceMap.TryGetValue(cardEffect, out permanent) && permanent == card.PermanentOfThisCard();
  143. }
  144. public static bool IsExistOnBreedingAreaActivate(CardSource card, ICardEffect cardEffect)
  145. {
  146. Permanent permanent = null;
  147. return IsExistOnBreedingArea(card) && CardPermanenceMap.TryGetValue(cardEffect, out permanent) && permanent == card.PermanentOfThisCard();
  148. }
  149. public static bool IsExistOnBattleAreaActivate(CardSource card, ICardEffect cardEffect)
  150. {
  151. Permanent permanent = null;
  152. return IsExistOnBattleArea(card) && CardPermanenceMap.TryGetValue(cardEffect, out permanent) && permanent == card.PermanentOfThisCard();
  153. }
  154. public static bool IsExistOnBattleAreaDigimonActivate(CardSource card, ICardEffect cardEffect)
  155. {
  156. Permanent permanent = null;
  157. return IsExistOnBattleAreaDigimon(card) && CardPermanenceMap.TryGetValue(cardEffect, out permanent) && permanent == card.PermanentOfThisCard();
  158. }
  159. public static bool IsExistDigivolutionCardsActivate(CardSource card, ICardEffect cardEffect)
  160. {
  161. Permanent permanent = null;
  162. return IsExistDigivolutionCards(card) && CardPermanenceMap.TryGetValue(cardEffect, out permanent) && permanent == card.PermanentOfThisCard();
  163. }
  164. public static bool IsExistLinkedActivate(CardSource card, ICardEffect cardEffect)
  165. {
  166. Permanent permanent = null;
  167. return IsExistLinked(card) && CardPermanenceMap.TryGetValue(cardEffect, out permanent) && permanent == card.PermanentOfThisCard();
  168. }
  169. public static bool IsExistOnTrashActivate(CardSource card, ICardEffect cardEffect)
  170. {
  171. return IsExistOnTrash(card) && IsCorrectLocation(cardEffect);
  172. }
  173. public static bool IsExistInSecurityActivate(CardSource card, ICardEffect cardEffect)
  174. {
  175. return IsExistInSecurity(card) && IsCorrectLocation(cardEffect);
  176. }
  177. public static bool IsExistOnHandActivate(CardSource card, ICardEffect cardEffect)
  178. {
  179. return IsExistOnHand(card) && IsCorrectLocation(cardEffect);
  180. }
  181. #endregion
  182. #region Whether the card is in the field
  183. public static bool IsExistOnField(CardSource card)
  184. {
  185. if (card != null)
  186. {
  187. if (card.PermanentOfThisCard() != null)
  188. {
  189. return true;
  190. }
  191. }
  192. return false;
  193. }
  194. #endregion
  195. #region Whether the card is in the Breeding Area
  196. public static bool IsExistOnBreedingArea(CardSource card)
  197. {
  198. if (IsExistOnField(card))
  199. {
  200. if (card.Owner.GetBreedingAreaPermanents().Contains(card.PermanentOfThisCard()))
  201. {
  202. return true;
  203. }
  204. }
  205. return false;
  206. }
  207. #endregion
  208. #region Whether the card is Digimon and in the Breeding Area
  209. public static bool IsExistOnBreedingAreaDigimon(CardSource card)
  210. {
  211. if (IsExistOnField(card))
  212. {
  213. if (card.Owner.GetBreedingAreaPermanents().Contains(card.PermanentOfThisCard()))
  214. {
  215. if (card.PermanentOfThisCard().IsDigimon)
  216. {
  217. return true;
  218. }
  219. }
  220. }
  221. return false;
  222. }
  223. #endregion
  224. #region Whether the card is in the Battle Area
  225. public static bool IsExistOnBattleArea(CardSource card)
  226. {
  227. if (IsExistOnField(card))
  228. {
  229. if (card.Owner.GetBattleAreaPermanents().Contains(card.PermanentOfThisCard()))
  230. {
  231. return true;
  232. }
  233. }
  234. return false;
  235. }
  236. #endregion
  237. #region Whether the card is Digimon and in the Battle Area
  238. public static bool IsExistOnBattleAreaDigimon(CardSource card)
  239. {
  240. if (IsExistOnBattleArea(card))
  241. {
  242. if (card.Owner.GetBattleAreaDigimons().Contains(card.PermanentOfThisCard()))
  243. {
  244. return true;
  245. }
  246. }
  247. return false;
  248. }
  249. #endregion
  250. #region Whether the card is in hand
  251. public static bool IsExistOnHand(CardSource card)
  252. {
  253. if (card.Owner.HandCards.Contains(card))
  254. {
  255. return true;
  256. }
  257. return false;
  258. }
  259. #endregion
  260. #region Whether the card is a Digivolution Card
  261. public static bool IsExistDigivolutionCards(CardSource card)
  262. {
  263. if (IsExistOnField(card))
  264. return card.PermanentOfThisCard().DigivolutionCards.Contains(card);
  265. return false;
  266. }
  267. #endregion
  268. #region Whether the card is Linked
  269. public static bool IsExistLinked(CardSource card)
  270. {
  271. if (IsExistOnField(card))
  272. return card.PermanentOfThisCard().LinkedCards.Contains(card);
  273. return false;
  274. }
  275. #endregion
  276. #region Whether the card is in trash
  277. public static bool IsExistOnTrash(CardSource card)
  278. {
  279. if (card.Owner.TrashCards.Contains(card))
  280. {
  281. return true;
  282. }
  283. return false;
  284. }
  285. #endregion
  286. #region Whether the card is in trash
  287. public static bool IsExistInAnyTrash(CardSource card)
  288. {
  289. if (card.Owner.TrashCards.Contains(card))
  290. {
  291. return true;
  292. }
  293. if (card.Owner.Enemy.TrashCards.Contains(card))
  294. {
  295. return true;
  296. }
  297. return false;
  298. }
  299. #endregion
  300. #region Whether the card is in Executing Area
  301. public static bool IsExistOnExecutingArea(CardSource card)
  302. {
  303. if (card.Owner.ExecutingCards.Contains(card))
  304. {
  305. return true;
  306. }
  307. return false;
  308. }
  309. #endregion
  310. #region Whether the card is in Security Area
  311. public static bool IsExistInSecurity(CardSource card, bool isFlipped = false)
  312. {
  313. if (card.Owner.SecurityCards.Contains(card))
  314. return card.IsFlipped == isFlipped;
  315. return false;
  316. }
  317. #endregion
  318. #region Whether the card Can be played as a new permanent
  319. public static bool CanPlayAsNewPermanent(
  320. CardSource cardSource,
  321. bool payCost,
  322. ICardEffect cardEffect,
  323. SelectCardEffect.Root root = SelectCardEffect.Root.Hand,
  324. bool isBreedingArea = false,
  325. bool isPlayOption = false,
  326. int fixedCost = -1) =>
  327. cardSource != null &&
  328. (isPlayOption || !cardSource.IsOption) &&
  329. !GManager.instance.GetComponent<SelectDigiXrosClass>().selectedDigicrossCards.Contains(cardSource) &&
  330. !GManager.instance.GetComponent<SelectAssemblyClass>().selectedAssemblyCards.Contains(cardSource)
  331. && cardSource.Owner.fieldCardFrames.Some((frame) =>
  332. frame.IsEmptyFrame()
  333. && cardSource.CanPlayCardTargetFrame(frame, payCost, cardEffect, root, isBreedingArea: isBreedingArea, fixedCost: fixedCost));
  334. #endregion
  335. #region Whether the permanent is in the Field
  336. public static bool IsPermanentExistsOnField(Permanent permanent)
  337. {
  338. if (permanent != null)
  339. {
  340. if (permanent.TopCard != null)
  341. {
  342. if (permanent.TopCard.Owner.GetBreedingAreaPermanents().Contains(permanent))
  343. {
  344. return true;
  345. }
  346. if (permanent.TopCard.Owner.GetBattleAreaPermanents().Contains(permanent))
  347. {
  348. return true;
  349. }
  350. }
  351. }
  352. return false;
  353. }
  354. #endregion
  355. #region Whether the permanent is in the Battle Area
  356. public static bool IsPermanentExistsOnBattleArea(Permanent permanent)
  357. {
  358. if (permanent != null)
  359. {
  360. if (permanent.TopCard != null)
  361. {
  362. if (permanent.TopCard.Owner.GetBattleAreaPermanents().Contains(permanent))
  363. {
  364. return true;
  365. }
  366. }
  367. }
  368. return false;
  369. }
  370. #endregion
  371. #region Whether the permanent is in the Breeding Area
  372. public static bool IsPermanentExistsOnBreedingArea(Permanent permanent)
  373. {
  374. if (permanent != null)
  375. {
  376. if (permanent.TopCard != null)
  377. {
  378. if (permanent.TopCard.Owner.GetBreedingAreaPermanents().Contains(permanent))
  379. {
  380. return true;
  381. }
  382. }
  383. }
  384. return false;
  385. }
  386. #endregion
  387. #region Whether the permanent is card's owner's
  388. public static bool IsOwnerPermanent(Permanent permanent, CardSource card)
  389. {
  390. if (permanent != null)
  391. {
  392. if (permanent.TopCard != null)
  393. {
  394. if (card != null)
  395. {
  396. if (permanent.TopCard.Owner == card.Owner)
  397. {
  398. return true;
  399. }
  400. }
  401. }
  402. }
  403. return false;
  404. }
  405. #endregion
  406. #region Whether the permanent is card's owner's opponent's
  407. public static bool IsOpponentPermanent(Permanent permanent, CardSource card)
  408. {
  409. if (permanent != null)
  410. {
  411. if (permanent.TopCard != null)
  412. {
  413. if (permanent.TopCard.Owner == card.Owner.Enemy)
  414. {
  415. return true;
  416. }
  417. }
  418. }
  419. return false;
  420. }
  421. #endregion
  422. #region Whether the permanent is in the card's owner's Battle Area
  423. public static bool IsPermanentExistsOnOwnerBattleArea(Permanent permanent, CardSource card)
  424. {
  425. if (IsPermanentExistsOnBattleArea(permanent))
  426. {
  427. if (IsOwnerPermanent(permanent, card))
  428. {
  429. return true;
  430. }
  431. }
  432. return false;
  433. }
  434. #endregion
  435. #region Whether the permanent is in the card's owner's opponent's Battle Area
  436. public static bool IsPermanentExistsOnOpponentBattleArea(Permanent permanent, CardSource card)
  437. {
  438. if (IsPermanentExistsOnBattleArea(permanent))
  439. {
  440. if (IsOpponentPermanent(permanent, card))
  441. {
  442. return true;
  443. }
  444. }
  445. return false;
  446. }
  447. #endregion
  448. #region Whether the permanent is in the card's owner's Breeding Area
  449. public static bool IsPermanentExistsOnOwnerBreedingArea(Permanent permanent, CardSource card)
  450. {
  451. if (IsPermanentExistsOnBreedingArea(permanent))
  452. {
  453. if (IsOwnerPermanent(permanent, card))
  454. {
  455. return true;
  456. }
  457. }
  458. return false;
  459. }
  460. #endregion
  461. #region Whether the permanent is in the card's owner's opponent's Breeding Area
  462. public static bool IsPermanentExistsOnOpponentBreedingArea(Permanent permanent, CardSource card)
  463. {
  464. if (IsPermanentExistsOnBreedingArea(permanent))
  465. {
  466. if (IsOpponentPermanent(permanent, card))
  467. {
  468. return true;
  469. }
  470. }
  471. return false;
  472. }
  473. #endregion
  474. #region Whether the permanent is Digimon and in the Battle Area
  475. public static bool IsPermanentExistsOnBattleAreaDigimon(Permanent permanent)
  476. {
  477. if (IsPermanentExistsOnBattleArea(permanent))
  478. {
  479. if (permanent.IsDigimon)
  480. {
  481. return true;
  482. }
  483. }
  484. return false;
  485. }
  486. #endregion
  487. #region Whether the permanent is Digimon and in the card's owner's Battle Area
  488. public static bool IsPermanentExistsOnOwnerBattleAreaDigimon(Permanent permanent, CardSource card)
  489. {
  490. if (IsPermanentExistsOnOwnerBattleArea(permanent, card))
  491. {
  492. if (permanent.IsDigimon)
  493. {
  494. return true;
  495. }
  496. }
  497. return false;
  498. }
  499. #endregion
  500. #region Whether the permanent is Digimon and in the card's owner's opponent's Battle Area
  501. public static bool IsPermanentExistsOnOpponentBattleAreaDigimon(Permanent permanent, CardSource card)
  502. {
  503. if (IsPermanentExistsOnOpponentBattleArea(permanent, card))
  504. {
  505. if (permanent.IsDigimon)
  506. {
  507. return true;
  508. }
  509. }
  510. return false;
  511. }
  512. #endregion
  513. #region Whether the permanent is Tamer and in the Battle Area
  514. public static bool IsPermanentExistsOnBattleAreaTamer(Permanent permanent)
  515. {
  516. if (IsPermanentExistsOnBattleArea(permanent))
  517. {
  518. if (permanent.IsTamer)
  519. {
  520. return true;
  521. }
  522. }
  523. return false;
  524. }
  525. #endregion
  526. #region Whether the permanent is Tamer and in the card's owner's Battle Area
  527. public static bool IsPermanentExistsOnOwnerBattleAreaTamer(Permanent permanent, CardSource card)
  528. {
  529. if (IsPermanentExistsOnOwnerBattleArea(permanent, card))
  530. {
  531. if (permanent.IsTamer)
  532. {
  533. return true;
  534. }
  535. }
  536. return false;
  537. }
  538. #endregion
  539. #region Whether the permanent is Tamer and in the card's owner's opponent's Battle Area
  540. public static bool IsPermanentExistsOnOpponentBattleAreaTamer(Permanent permanent, CardSource card)
  541. {
  542. if (IsPermanentExistsOnOpponentBattleArea(permanent, card))
  543. {
  544. if (permanent.IsTamer)
  545. {
  546. return true;
  547. }
  548. }
  549. return false;
  550. }
  551. #endregion
  552. #region How many permanents there are in the Battle Area that satisfy the condition
  553. public static int MatchConditionPermanentCount(Func<Permanent, bool> CanSelectPermanentCondition, bool isContainBreedingArea = false)
  554. {
  555. if (isContainBreedingArea)
  556. {
  557. return GManager.instance.turnStateMachine.gameContext.Players
  558. .Map(player => player.GetFieldPermanents())
  559. .Flat()
  560. .Count(CanSelectPermanentCondition);
  561. }
  562. else
  563. {
  564. return GManager.instance.turnStateMachine.gameContext.Players
  565. .Map(player => player.GetBattleAreaPermanents())
  566. .Flat()
  567. .Count(CanSelectPermanentCondition);
  568. }
  569. }
  570. #endregion
  571. #region How many permanents there are in the owner's Battle Area that satisfy the condition
  572. public static int MatchConditionOwnersPermanentCount(CardSource card, Func<Permanent, bool> CanSelectPermanentCondition)
  573. {
  574. return MatchConditionPermanentCount(permanent => CanSelectPermanentCondition(permanent) && IsOwnerPermanent(permanent, card));
  575. }
  576. #endregion
  577. #region How many permanents there are in the owner's opponent's Battle Area that satisfy the condition
  578. public static int MatchConditionOpponentsPermanentCount(CardSource card, Func<Permanent, bool> CanSelectPermanentCondition)
  579. {
  580. return MatchConditionPermanentCount(permanent => CanSelectPermanentCondition(permanent) && IsOpponentPermanent(permanent, card));
  581. }
  582. #endregion
  583. #region Whether there is at least 1 permanent in the Battle Area that satisfies the condition
  584. public static bool HasMatchConditionPermanent(Func<Permanent, bool> CanSelectPermanentCondition, bool isContainBreedingArea = false)
  585. {
  586. if (isContainBreedingArea)
  587. {
  588. return GManager.instance.turnStateMachine.gameContext.Players
  589. .Map(player => player.GetFieldPermanents())
  590. .Flat()
  591. .Some(CanSelectPermanentCondition);
  592. }
  593. else
  594. {
  595. return GManager.instance.turnStateMachine.gameContext.Players
  596. .Map(player => player.GetBattleAreaPermanents())
  597. .Flat()
  598. .Some(CanSelectPermanentCondition);
  599. }
  600. }
  601. #endregion
  602. #region Whether there is at least 1 card in the owner's hand that satisfies the condition
  603. public static bool HasMatchConditionOwnersHand(CardSource card, Func<CardSource, bool> CanSelectCardCondition)
  604. {
  605. return card.Owner.HandCards.Some(CanSelectCardCondition);
  606. }
  607. #endregion
  608. #region How many cards there are in the owner's hand that satisfy the condition
  609. public static int MatchConditionOwnersCardCountInHand(CardSource card, Func<CardSource, bool> CanSelectCardCondition)
  610. {
  611. return card.Owner.HandCards.Count(CanSelectCardCondition);
  612. }
  613. #endregion
  614. #region Whether there is at least 1 permanent in the owner's Battle Area that satisfies the condition
  615. public static bool HasMatchConditionOwnersPermanent(CardSource card, Func<Permanent, bool> CanSelectPermanentCondition)
  616. {
  617. return GManager.instance.turnStateMachine.gameContext.Players
  618. .Map(player => player.GetBattleAreaPermanents())
  619. .Flat()
  620. .Some(permanent => IsOwnerPermanent(permanent, card) && CanSelectPermanentCondition(permanent));
  621. }
  622. #endregion
  623. #region Whether there is at least 1 permanent in the owner's Breeding Area that satisfies the condition
  624. public static bool HasMatchConditionOwnersBreedingPermanent(CardSource card, Func<Permanent, bool> CanSelectPermanentCondition)
  625. {
  626. return GManager.instance.turnStateMachine.gameContext.Players
  627. .Map(player => player.GetBreedingAreaPermanents())
  628. .Flat()
  629. .Some(permanent => IsOwnerPermanent(permanent, card) && CanSelectPermanentCondition(permanent));
  630. }
  631. #endregion
  632. #region Whether there is at least 1 source in this cards sources that satisfies the condition
  633. public static bool HasMatchConditionPermanentDigivolutionCards(CardSource card, Func<CardSource, bool> CanSelectPermanentCondition)
  634. {
  635. return card.PermanentOfThisCard().DigivolutionCards.Some(cardSource => CanSelectPermanentCondition(cardSource));
  636. }
  637. #endregion
  638. #region Whether there is at least 1 permanent in the owner's opponent's Battle Area that satisfies the condition
  639. public static bool HasMatchConditionOpponentsPermanent(CardSource card, Func<Permanent, bool> CanSelectPermanentCondition)
  640. {
  641. return GManager.instance.turnStateMachine.gameContext.Players
  642. .Map(player => player.GetBattleAreaPermanents())
  643. .Flat()
  644. .Some(permanent => IsOpponentPermanent(permanent, card) && CanSelectPermanentCondition(permanent));
  645. }
  646. #endregion
  647. #region Whether there is at least 1 permanent in the owner's Security Area that satisfies the condition
  648. public static bool HasMatchConditionOwnersSecurity(CardSource card, Func<CardSource, bool> CanSelectPermanentCondition, bool flipped = true)
  649. {
  650. return GManager.instance.turnStateMachine.gameContext.Players
  651. .Map(player => player.SecurityCards)
  652. .Flat()
  653. .Some(cardSource => cardSource.IsFlipped == flipped && CanSelectPermanentCondition(cardSource) && cardSource.Owner == card.Owner);
  654. }
  655. #endregion
  656. #region How many cards there are in the owner's trash that satisfy the condition
  657. public static int MatchConditionOwnersCardCountInTrash(CardSource card, Func<CardSource, bool> CanSelectCardCondition)
  658. {
  659. return card.Owner.TrashCards.Count(CanSelectCardCondition);
  660. }
  661. #endregion
  662. #region How many cards there are in the owner's opponent's trash that satisfy the condition
  663. public static int MatchConditionOpponentsCardCountInTrash(CardSource card, Func<CardSource, bool> CanSelectCardCondition)
  664. {
  665. return card.Owner.Enemy.TrashCards.Count(CanSelectCardCondition);
  666. }
  667. #endregion
  668. #region Whether there is at least 1 card in the owner's trash that satisfies the condition
  669. public static bool HasMatchConditionOwnersCardInTrash(CardSource card, Func<CardSource, bool> CanSelectCardCondition)
  670. {
  671. return card.Owner.TrashCards.Some(CanSelectCardCondition);
  672. }
  673. #endregion
  674. #region Whether there is at least 1 card in the owner's opponent's trash that satisfies the condition
  675. public static bool HasMatchConditionOpponentsCardInTrash(CardSource card, Func<CardSource, bool> CanSelectCardCondition)
  676. {
  677. return card.Owner.Enemy.TrashCards.Some(CanSelectCardCondition);
  678. }
  679. #endregion
  680. #region Whether the list has no element
  681. public static bool HasNoElement<T>(List<T> list) => list.Count <= 0;
  682. #endregion
  683. #region Wheter it is the player's turn
  684. public static bool IsOwnerTurn(CardSource card) => GManager.instance.turnStateMachine.gameContext.TurnPlayer == card.Owner;
  685. public static bool IsOpponentTurn(CardSource card) => !IsOwnerTurn(card);
  686. #endregion
  687. #region Whether the effect is your effect
  688. public static bool IsOwnerEffect(ICardEffect cardEffect, CardSource card)
  689. {
  690. if (cardEffect != null)
  691. {
  692. if (cardEffect.EffectSourceCard != null)
  693. {
  694. if (cardEffect.EffectSourceCard.Owner == card.Owner)
  695. {
  696. return true;
  697. }
  698. }
  699. }
  700. return false;
  701. }
  702. #endregion
  703. #region Whether the effect is opponent's effect
  704. public static bool IsOpponentEffect(ICardEffect cardEffect, CardSource card)
  705. {
  706. if (cardEffect != null)
  707. {
  708. if (cardEffect.EffectSourceCard != null)
  709. {
  710. if (cardEffect.EffectSourceCard.Owner == card.Owner.Enemy)
  711. {
  712. return true;
  713. }
  714. }
  715. }
  716. return false;
  717. }
  718. #endregion
  719. #region Get unique colour count from permanents in owners battle area
  720. public static int GetUniqueColourCountOnOwnerBattleArea(CardSource card, Func<Permanent, bool> canGetCardColour)
  721. {
  722. var uniqueColors = card.Owner.GetBattleAreaPermanents()
  723. .Filter(x => canGetCardColour(x))
  724. .Select(x => x.TopCard)
  725. .SelectMany(x => x.CardColors)
  726. .Distinct()
  727. .ToHashSet();
  728. return uniqueColors.Count;
  729. }
  730. #endregion
  731. #region Get unique colour count from permanents in opponents battle area
  732. public static int GetUniqueColourCountOnOpponentsBattleArea(CardSource card, Func<Permanent, bool> canGetCardColour)
  733. {
  734. var uniqueColors = card.Owner.Enemy.GetBattleAreaPermanents()
  735. .Filter(x => canGetCardColour(x))
  736. .Select(x => x.TopCard)
  737. .SelectMany(x => x.CardColors)
  738. .Distinct()
  739. .ToHashSet();
  740. return uniqueColors.Count;
  741. }
  742. #endregion
  743. #region Does Owner has 1 or less tamers on the field
  744. public static bool OwnerHas1OrLessTamers(CardSource card)
  745. {
  746. return card.Owner.GetBattleAreaPermanents()
  747. .Count(permanent => permanent.IsTamer && permanent.TopCard.Owner == card.Owner) <= 1;
  748. }
  749. #endregion
  750. #region Universial Root Can No Select Condition
  751. public static bool UniversalRootCanNoSelectCondition(CardSource cardSource)
  752. {
  753. if (cardSource.PayingCost(SelectCardEffect.Root.Hand, null, checkAvailability: false) > cardSource.Owner.MaxMemoryCost) return false;
  754. if (cardSource.PayingCost(SelectCardEffect.Root.Trash, null, checkAvailability: false) > cardSource.Owner.MaxMemoryCost) return false;
  755. if (cardSource.PayingCost(SelectCardEffect.Root.DigivolutionCards, null, checkAvailability: false) > cardSource.Owner.MaxMemoryCost) return false;
  756. if (cardSource.PayingCost(SelectCardEffect.Root.Library, null, checkAvailability: false) > cardSource.Owner.MaxMemoryCost) return false;
  757. if (cardSource.PayingCost(SelectCardEffect.Root.DigivolutionCards, null, checkAvailability: false) > cardSource.Owner.MaxMemoryCost) return false;
  758. if (cardSource.PayingCost(SelectCardEffect.Root.LinkedCards, null, checkAvailability: false) > cardSource.Owner.MaxMemoryCost) return false;
  759. if (cardSource.PayingCost(SelectCardEffect.Root.Security, null, checkAvailability: false) > cardSource.Owner.MaxMemoryCost) return false;
  760. return true;
  761. }
  762. #endregion
  763. }