GameContextDeterminarion.cs 21 KB

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