GameContextDeterminarion.cs 21 KB

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