CardEffectCommons.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEditor;
  7. public partial class CardEffectCommons
  8. {
  9. #region Play cards as new permanents
  10. public static IEnumerator PlayPermanentCards(List<CardSource> cardSources, ICardEffect activateClass, bool payCost, bool isTapped, SelectCardEffect.Root root, bool activateETB, bool isBreedingArea = false, int fixedCost = -1)
  11. {
  12. if (cardSources == null) yield break;
  13. cardSources = cardSources
  14. .Filter(cardSource => cardSource != null)
  15. .Filter(cardSource => CanPlayAsNewPermanent(
  16. cardSource: cardSource,
  17. payCost: payCost,
  18. cardEffect: activateClass,
  19. isBreedingArea: isBreedingArea));
  20. if (cardSources.Count == 0) yield break;
  21. PlayCardClass playCardClass = new PlayCardClass(
  22. cardSources: cardSources,
  23. hashtable: CardEffectHashtable(activateClass),
  24. payCost: payCost,
  25. targetPermanent: null,
  26. isTapped: isTapped,
  27. root: root,
  28. activateETB: activateETB);
  29. if (isBreedingArea)
  30. {
  31. playCardClass.SetIsBreedingArea();
  32. }
  33. playCardClass.SetFixedCost(fixedCost);
  34. yield return ContinuousController.instance.StartCoroutine(playCardClass.PlayCard());
  35. }
  36. #endregion
  37. #region Play option cards
  38. public static IEnumerator PlayOptionCards(List<CardSource> cardSources, ICardEffect activateClass, bool payCost, SelectCardEffect.Root root,
  39. bool setAddSecurityEndOption = false)
  40. {
  41. if (cardSources == null) yield break;
  42. cardSources = cardSources
  43. .Filter(cardSource => cardSource != null)
  44. .Filter(cardSource => !cardSource.CanNotPlayThisOption);
  45. if (cardSources.Count == 0) yield break;
  46. PlayCardClass playCard = new PlayCardClass(
  47. cardSources: cardSources,
  48. hashtable: CardEffectHashtable(activateClass),
  49. payCost: payCost,
  50. targetPermanent: null,
  51. isTapped: false,
  52. root: root,
  53. activateETB: true);
  54. if (activateClass != null)
  55. {
  56. playCard.SetShowEffect();
  57. }
  58. if (setAddSecurityEndOption)
  59. {
  60. playCard.SetAddSecurityEndOption();
  61. }
  62. yield return ContinuousController.instance.StartCoroutine(playCard.PlayCard());
  63. }
  64. #endregion
  65. #region Play Delay Option cards as new permanents
  66. public static IEnumerator PlaceDelayOptionCards(CardSource card, ICardEffect cardEffect, SelectCardEffect.Root root = SelectCardEffect.Root.Execution)
  67. {
  68. if (CanPlayAsNewPermanent(cardSource: card, payCost: false, cardEffect: cardEffect, isPlayOption: true))
  69. {
  70. PlayPermanentClass playPermanent = new PlayPermanentClass(
  71. cardSources: new List<CardSource>() { card },
  72. hashtable: CardEffectHashtable(cardEffect),
  73. targetPermanent: null,
  74. isTapped: false,
  75. root: root,
  76. ActivateETB: true);
  77. playPermanent.SetISPlayOption();
  78. yield return ContinuousController.instance.StartCoroutine(playPermanent.PlayPermanent());
  79. if (IsExistOnBattleArea(card))
  80. {
  81. card.PermanentOfThisCard().IsPlayedOptionPermanent = true;
  82. }
  83. }
  84. }
  85. #endregion
  86. #region Play 1 Token
  87. public static IEnumerator PlayToken(CEntity_Base tokenData, ICardEffect activateClass, bool isOwnerPermanent, bool isTapped)
  88. {
  89. if (activateClass == null) yield break;
  90. if (activateClass.EffectSourceCard == null) yield break;
  91. if (tokenData == null) yield break;
  92. CardSource card = activateClass.EffectSourceCard;
  93. Player player = isOwnerPermanent ? card.Owner : card.Owner.Enemy;
  94. if (card.Owner.fieldCardFrames.Count((frame) => frame.IsEmptyFrame() && frame.IsBattleAreaFrame()) >= 1)
  95. {
  96. CardSource tokenCard = CardObjectController.CreateCardSource(
  97. player.PlayerID,
  98. tokenData,
  99. true);
  100. if (CanPlayAsNewPermanent(cardSource: tokenCard, payCost: false, cardEffect: activateClass))
  101. {
  102. yield return ContinuousController.instance.StartCoroutine(new PlayCardClass(
  103. cardSources: new List<CardSource>() { tokenCard },
  104. hashtable: CardEffectHashtable(activateClass),
  105. payCost: false,
  106. targetPermanent: null,
  107. isTapped: isTapped,
  108. root: SelectCardEffect.Root.None,
  109. activateETB: true).PlayCard());
  110. }
  111. }
  112. }
  113. #endregion
  114. #region Play 1 [Diaboromon] Token
  115. public static IEnumerator PlayDiaboromonToken(ICardEffect activateClass)
  116. {
  117. yield return ContinuousController.instance.StartCoroutine(PlayToken(
  118. tokenData: ContinuousController.instance.DiaboromonToken,
  119. activateClass: activateClass,
  120. isOwnerPermanent: true,
  121. isTapped: false
  122. ));
  123. }
  124. #endregion
  125. #region Play 1 [Amon of Crimson Flame] Token
  126. public static IEnumerator PlayAmonToken(ICardEffect activateClass)
  127. {
  128. yield return ContinuousController.instance.StartCoroutine(PlayToken(
  129. tokenData: ContinuousController.instance.AmonToken,
  130. activateClass: activateClass,
  131. isOwnerPermanent: true,
  132. isTapped: false
  133. ));
  134. }
  135. #endregion
  136. #region Play 1 [Umon of Blue Thunder] Token
  137. public static IEnumerator PlayUmonToken(ICardEffect activateClass)
  138. {
  139. yield return ContinuousController.instance.StartCoroutine(PlayToken(
  140. tokenData: ContinuousController.instance.UmonToken,
  141. activateClass: activateClass,
  142. isOwnerPermanent: true,
  143. isTapped: false
  144. ));
  145. }
  146. #endregion
  147. #region Play 1 [Fujitsumon] Token
  148. public static IEnumerator PlayFujitsumonToken(ICardEffect activateClass, bool isOwnerPermanent)
  149. {
  150. yield return ContinuousController.instance.StartCoroutine(PlayToken(
  151. tokenData: ContinuousController.instance.FujitsumonToken,
  152. activateClass: activateClass,
  153. isOwnerPermanent: isOwnerPermanent,
  154. isTapped: true
  155. ));
  156. }
  157. #endregion
  158. #region Play 1 [Gyuukimon] Token
  159. public static IEnumerator PlayGyuukimonToken(ICardEffect activateClass)
  160. {
  161. yield return ContinuousController.instance.StartCoroutine(PlayToken(
  162. tokenData: ContinuousController.instance.GyuukimonToken,
  163. activateClass: activateClass,
  164. isOwnerPermanent: true,
  165. isTapped: false
  166. ));
  167. }
  168. #endregion
  169. #region Play 1 [KoHagurumon] Token
  170. public static IEnumerator PlayKoHagurumonToken(ICardEffect activateClass)
  171. {
  172. yield return ContinuousController.instance.StartCoroutine(PlayToken(
  173. tokenData: ContinuousController.instance.KoHagurumonToken,
  174. activateClass: activateClass,
  175. isOwnerPermanent: true,
  176. isTapped: false
  177. ));
  178. }
  179. #endregion
  180. #region Security effect of "add this card to hand"
  181. public static IEnumerator AddThisCardToHand(CardSource card1, ICardEffect activateClass)
  182. {
  183. yield return new WaitForSeconds(0.5f);
  184. yield return ContinuousController.instance.StartCoroutine(card1.Owner.brainStormObject.CloseBrainstrorm(card1));
  185. yield return ContinuousController.instance.StartCoroutine(CardObjectController.AddHandCards(new List<CardSource>() { card1 }, false, activateClass));
  186. }
  187. #endregion
  188. #region Delete target permanents, and the effect determines whether the permanent has been deleted or not
  189. public static IEnumerator DeletePeremanentAndProcessAccordingToResult(List<Permanent> targetPermanents, ICardEffect activateClass, Func<List<Permanent>, IEnumerator> successProcess, Func<IEnumerator> failureProcess)
  190. {
  191. DestroyPermanentsClass destroyPermanentsClass = new DestroyPermanentsClass(targetPermanents, CardEffectHashtable(activateClass));
  192. yield return ContinuousController.instance.StartCoroutine(destroyPermanentsClass.Destroy());
  193. if (targetPermanents.Some((permanent) => destroyPermanentsClass.IsDestroyed(permanent)))
  194. {
  195. if (successProcess != null)
  196. {
  197. yield return ContinuousController.instance.StartCoroutine(successProcess(destroyPermanentsClass.DestroyedPermanents));
  198. }
  199. }
  200. else
  201. {
  202. if (failureProcess != null)
  203. {
  204. yield return ContinuousController.instance.StartCoroutine(failureProcess());
  205. }
  206. }
  207. }
  208. #endregion
  209. #region Bounce target 1 permanent, and the effect determines whether the permanent has been bounced or not
  210. public static IEnumerator BouncePeremanentAndProcessAccordingToResult(List<Permanent> targetPermanents, ICardEffect activateClass, IEnumerator successProcess, IEnumerator failureProcess)
  211. {
  212. HandBounceClaass bouncePermanentClass = new HandBounceClaass(targetPermanents, CardEffectHashtable(activateClass));
  213. yield return ContinuousController.instance.StartCoroutine(bouncePermanentClass.Bounce());
  214. if (targetPermanents.Some((permanent) => bouncePermanentClass.IsBounced(permanent)))
  215. {
  216. if (successProcess != null)
  217. {
  218. yield return ContinuousController.instance.StartCoroutine(successProcess);
  219. }
  220. }
  221. else
  222. {
  223. if (failureProcess != null)
  224. {
  225. yield return ContinuousController.instance.StartCoroutine(failureProcess);
  226. }
  227. }
  228. }
  229. #endregion
  230. #region Deck Bounce target 1 permanent, and the effect determines whether the permanent has been bounced or not
  231. public static IEnumerator DeckBouncePeremanentAndProcessAccordingToResult(List<Permanent> targetPermanents, ICardEffect activateClass, IEnumerator successProcess, IEnumerator failureProcess)
  232. {
  233. DeckBottomBounceClass deckBounceClass = new DeckBottomBounceClass(targetPermanents, CardEffectHashtable(activateClass));
  234. yield return ContinuousController.instance.StartCoroutine(deckBounceClass.DeckBounce());
  235. if (targetPermanents.Some((permanent) => deckBounceClass.IsDeckBounced(permanent)))
  236. {
  237. if (successProcess != null)
  238. {
  239. yield return ContinuousController.instance.StartCoroutine(successProcess);
  240. }
  241. }
  242. else
  243. {
  244. if (failureProcess != null)
  245. {
  246. yield return ContinuousController.instance.StartCoroutine(failureProcess);
  247. }
  248. }
  249. }
  250. #endregion
  251. #region Trash digivolution cards from top or bottom
  252. public static IEnumerator TrashDigivolutionCardsFromTopOrBottom(Permanent targetPermanent, int trashCount, bool isFromTop, ICardEffect activateClass)
  253. {
  254. if (activateClass == null) yield break;
  255. if (targetPermanent == null) yield break;
  256. if (targetPermanent.TopCard == null) yield break;
  257. if (targetPermanent.DigivolutionCards.Count((cardSource) => !cardSource.CanNotTrashFromDigivolutionCards(activateClass)) == 0) yield break;
  258. if (targetPermanent.TopCard.CanNotBeAffected(activateClass)) yield break;
  259. if (trashCount <= 0) yield break;
  260. List<CardSource> trashTargetCards = new List<CardSource>();
  261. for (int i = 0; i < trashCount; i++)
  262. {
  263. if (targetPermanent.DigivolutionCards.Count >= i + 1)
  264. {
  265. int index = isFromTop ? i : targetPermanent.DigivolutionCards.Count - 1 - i;
  266. CardSource trashTargetCard = targetPermanent.DigivolutionCards[index];
  267. trashTargetCards.Add(trashTargetCard);
  268. }
  269. }
  270. yield return ContinuousController.instance.StartCoroutine(new ITrashDigivolutionCards(targetPermanent, trashTargetCards, activateClass).TrashDigivolutionCards());
  271. }
  272. #endregion
  273. #region this Option card's Main effect
  274. public static ActivateClass OptionMainEffect(CardSource card) => card.EffectList(EffectTiming.OptionSkill).Find(cardEffect => cardEffect != null && cardEffect is ActivateClass && cardEffect.EffectDiscription.Contains("[Main]")) as ActivateClass;
  275. #endregion
  276. #region this Option card's Security effect
  277. public static ActivateClass OptionSecurityEffect(CardSource card) => card.EffectList(EffectTiming.SecuritySkill).Find(cardEffect => cardEffect != null && cardEffect is ActivateClass && cardEffect.EffectDiscription.Contains("[Security]")) as ActivateClass;
  278. #endregion
  279. #region Add Option's Security effect that "Activate this card's Main effect" to cardEffects
  280. public static void AddActivateMainOptionSecurityEffect(CardSource card, ref List<ICardEffect> cardEffects, string effectName, string effectDiscription = "", Func<ICardEffect, IEnumerator> afterMainEffect = null)
  281. {
  282. if (OptionMainEffect(card) == null) return;
  283. cardEffects.Add(CardEffectFactory.ActivateMainOptionSecurityEffect(card, effectName, effectDiscription, afterMainEffect));
  284. }
  285. #endregion
  286. #region Target permanent Digivolves into Digimon card from hand or trash
  287. public static IEnumerator DigivolveIntoHandOrTrashCard(
  288. Permanent targetPermanent,
  289. Func<CardSource, bool> cardCondition,
  290. bool payCost,
  291. (int reduceCost, Func<CardSource, bool> reduceCostCardCondition)? reduceCostTuple,
  292. (int fixedCost, Func<CardSource, bool> fixedCostCardCondition)? fixedCostTuple,
  293. int ignoreDigivolutionRequirementFixedCost,
  294. bool isHand,
  295. ICardEffect activateClass,
  296. IEnumerator successProcess,
  297. bool ignoreLevel = false,
  298. bool ignoreSelection = false)
  299. {
  300. if (targetPermanent == null) yield break;
  301. if (targetPermanent.TopCard == null) yield break;
  302. if (activateClass == null) yield break;
  303. if (activateClass.EffectSourceCard == null) yield break;
  304. Player owner = targetPermanent.TopCard.Owner;
  305. SelectCardEffect.Root root = isHand ? SelectCardEffect.Root.Hand : SelectCardEffect.Root.Trash;
  306. bool ignoreDigivolutionRequirement = ignoreDigivolutionRequirementFixedCost >= 0;
  307. int fixedCost = -1;
  308. if (fixedCostTuple != null)
  309. {
  310. fixedCost = fixedCostTuple.Value.fixedCost;
  311. }
  312. if (ignoreDigivolutionRequirement)
  313. {
  314. fixedCost = ignoreDigivolutionRequirementFixedCost;
  315. }
  316. bool CanSelectCardCondition(CardSource cardSource)
  317. {
  318. if (cardSource.IsDigimon)
  319. {
  320. if (cardCondition == null || cardCondition(cardSource))
  321. {
  322. if (!cardSource.CanNotEvolve(targetPermanent))
  323. {
  324. if (ignoreLevel
  325. || cardSource.CanPlayCardTargetFrame(
  326. frame: targetPermanent.PermanentFrame,
  327. PayCost: payCost,
  328. cardEffect: activateClass,
  329. root: root,
  330. fixedCost: fixedCost))
  331. {
  332. return true;
  333. }
  334. }
  335. }
  336. }
  337. return false;
  338. }
  339. #region reduce cost
  340. Func<EffectTiming, ICardEffect> getChangeCostEffect = null;
  341. if (reduceCostTuple != null)
  342. {
  343. bool PermanentCondition(Permanent permanent)
  344. {
  345. return permanent == targetPermanent;
  346. }
  347. bool CardCondition(CardSource cardSource)
  348. {
  349. if (cardSource != null)
  350. {
  351. if (cardSource.Owner == owner)
  352. {
  353. if (reduceCostTuple.Value.reduceCostCardCondition == null || reduceCostTuple.Value.reduceCostCardCondition(cardSource))
  354. {
  355. return true;
  356. }
  357. }
  358. }
  359. return false;
  360. }
  361. getChangeCostEffect = ChangeDigivolutionCostPlayerEffect(
  362. permanentCondition: PermanentCondition,
  363. cardCondition: CardCondition,
  364. rootCondition: null,
  365. changeValue: -reduceCostTuple.Value.reduceCost,
  366. activateClass: activateClass,
  367. setFixedCost: false);
  368. if (getChangeCostEffect != null)
  369. {
  370. AddEffectToPlayer(
  371. effectDuration: EffectDuration.UntilCalculateFixedCost,
  372. card: targetPermanent.TopCard,
  373. cardEffect: null,
  374. timing: EffectTiming.None,
  375. getCardEffect: getChangeCostEffect);
  376. }
  377. }
  378. #endregion
  379. #region set fixed cost
  380. Func<EffectTiming, ICardEffect> getFixedCostEffect = null;
  381. if (fixedCostTuple != null)
  382. {
  383. bool PermanentCondition(Permanent permanent)
  384. {
  385. return permanent == targetPermanent;
  386. }
  387. bool CardCondition(CardSource cardSource)
  388. {
  389. if (cardSource != null)
  390. {
  391. if (cardSource.Owner == owner)
  392. {
  393. if (fixedCostTuple.Value.fixedCostCardCondition == null || fixedCostTuple.Value.fixedCostCardCondition(cardSource))
  394. {
  395. return true;
  396. }
  397. }
  398. }
  399. return false;
  400. }
  401. getFixedCostEffect = ChangeDigivolutionCostPlayerEffect(
  402. permanentCondition: PermanentCondition,
  403. cardCondition: CardCondition,
  404. rootCondition: null,
  405. changeValue: fixedCost,
  406. activateClass: activateClass,
  407. setFixedCost: true);
  408. if (getChangeCostEffect != null)
  409. {
  410. AddEffectToPlayer(
  411. effectDuration: EffectDuration.UntilCalculateFixedCost,
  412. card: targetPermanent.TopCard,
  413. cardEffect: null,
  414. timing: EffectTiming.None,
  415. getCardEffect: getFixedCostEffect);
  416. }
  417. }
  418. #endregion
  419. #region ignore digivolution requirement
  420. Func<EffectTiming, ICardEffect> getIgnoreDigivolutionRequirementEffect = null;
  421. if (ignoreDigivolutionRequirement)
  422. {
  423. bool PermanentCondition(Permanent permanent)
  424. {
  425. return permanent == targetPermanent;
  426. }
  427. bool CardCondition(CardSource cardSource)
  428. {
  429. if (cardSource == null)
  430. {
  431. return false;
  432. }
  433. if (cardSource.Owner != owner)
  434. {
  435. return false;
  436. }
  437. if (cardCondition == null || cardCondition(cardSource))
  438. {
  439. return true;
  440. }
  441. return false;
  442. }
  443. getIgnoreDigivolutionRequirementEffect = GainIgnoreDigivolutionRequirementPlayerEffect(
  444. permanentCondition: PermanentCondition,
  445. cardCondition: CardCondition,
  446. ignoreDigivolutionRequirement: ignoreDigivolutionRequirement,
  447. digivolutionCost: fixedCost,
  448. activateClass: activateClass);
  449. if (getIgnoreDigivolutionRequirementEffect != null)
  450. {
  451. AddEffectToPlayer(
  452. effectDuration: EffectDuration.UntilCalculateFixedCost,
  453. card: targetPermanent.TopCard,
  454. cardEffect: null,
  455. timing: EffectTiming.None,
  456. getCardEffect: getIgnoreDigivolutionRequirementEffect);
  457. }
  458. }
  459. #endregion
  460. List<CardSource> selectedCards = new List<CardSource>();
  461. IEnumerator SelectCardCoroutine(CardSource cardSource)
  462. {
  463. selectedCards.Add(cardSource);
  464. yield return null;
  465. }
  466. if (isHand)
  467. {
  468. if (owner.HandCards.Count(CanSelectCardCondition) >= 1)
  469. {
  470. int maxCount = 1;
  471. SelectHandEffect selectHandEffect = GManager.instance.GetComponent<SelectHandEffect>();
  472. selectHandEffect.SetUp(
  473. selectPlayer: owner,
  474. canTargetCondition: CanSelectCardCondition,
  475. canTargetCondition_ByPreSelecetedList: null,
  476. canEndSelectCondition: null,
  477. maxCount: maxCount,
  478. canNoSelect: true,
  479. canEndNotMax: false,
  480. isShowOpponent: true,
  481. selectCardCoroutine: SelectCardCoroutine,
  482. afterSelectCardCoroutine: null,
  483. mode: SelectHandEffect.Mode.Custom,
  484. cardEffect: activateClass);
  485. selectHandEffect.SetUpCustomMessage("Select 1 card to digivolve.", "The opponent is selecting 1 card to digivolve.");
  486. selectHandEffect.SetUpCustomMessage_ShowCard("Selected Card");
  487. yield return ContinuousController.instance.StartCoroutine(selectHandEffect.Activate());
  488. }
  489. }
  490. else
  491. {
  492. if (HasMatchConditionOwnersCardInTrash(targetPermanent.TopCard, CanSelectCardCondition) && !ignoreSelection)
  493. {
  494. int maxCount = 1;
  495. SelectCardEffect selectCardEffect = GManager.instance.GetComponent<SelectCardEffect>();
  496. selectCardEffect.SetUp(
  497. canTargetCondition: CanSelectCardCondition,
  498. canTargetCondition_ByPreSelecetedList: null,
  499. canEndSelectCondition: null,
  500. canNoSelect: () => true,
  501. selectCardCoroutine: SelectCardCoroutine,
  502. afterSelectCardCoroutine: null,
  503. message: "Select 1 card to digivolve.",
  504. maxCount: maxCount,
  505. canEndNotMax: false,
  506. isShowOpponent: true,
  507. mode: SelectCardEffect.Mode.Custom,
  508. root: SelectCardEffect.Root.Trash,
  509. customRootCardList: null,
  510. canLookReverseCard: true,
  511. selectPlayer: owner,
  512. cardEffect: activateClass);
  513. selectCardEffect.SetUpCustomMessage("Select 1 card to digivolve.", "The opponent is selecting 1 card to digivolve.");
  514. selectCardEffect.SetUpCustomMessage_ShowCard("Selected Card");
  515. yield return ContinuousController.instance.StartCoroutine(selectCardEffect.Activate());
  516. }
  517. else
  518. {
  519. selectedCards.Add(activateClass.EffectSourceCard);
  520. }
  521. }
  522. PlayCardClass playCardClass = new PlayCardClass(
  523. cardSources: selectedCards,
  524. hashtable: CardEffectHashtable(activateClass),
  525. payCost: payCost,
  526. targetPermanent: targetPermanent,
  527. isTapped: false,
  528. root: root,
  529. activateETB: true);
  530. if (ignoreLevel) playCardClass.SetIgnoreLevel();
  531. if (!ignoreDigivolutionRequirement && fixedCost >= 0) playCardClass.SetFixedCost(fixedCost);
  532. yield return ContinuousController.instance.StartCoroutine(playCardClass.PlayCard());
  533. #region release effect
  534. if (getChangeCostEffect != null) owner.UntilCalculateFixedCostEffect.Remove(getChangeCostEffect);
  535. #endregion
  536. #region release effect
  537. if (getFixedCostEffect != null) owner.UntilCalculateFixedCostEffect.Remove(getFixedCostEffect);
  538. #endregion
  539. #region release effect
  540. if (getIgnoreDigivolutionRequirementEffect != null) owner.UntilCalculateFixedCostEffect.Remove(getIgnoreDigivolutionRequirementEffect);
  541. #endregion
  542. if (selectedCards.Count >= 1)
  543. {
  544. if (IsDigivolvedByTheEffect(targetPermanent, selectedCards[0], activateClass))
  545. {
  546. if (successProcess != null)
  547. {
  548. yield return ContinuousController.instance.StartCoroutine(successProcess);
  549. }
  550. }
  551. }
  552. }
  553. #endregion
  554. #region Target permanent Digivolves into Digimon card execution area
  555. public static IEnumerator DigivolveIntoExcecutingAreaCard(
  556. Permanent targetPermanent,
  557. Func<CardSource, bool> cardCondition,
  558. bool payCost,
  559. (int reduceCost, Func<CardSource, bool> reduceCostCardCondition)? reduceCostTuple,
  560. (int fixedCost, Func<CardSource, bool> fixedCostCardCondition)? fixedCostTuple,
  561. int ignoreDigivolutionRequirementFixedCost,
  562. ICardEffect activateClass,
  563. IEnumerator successProcess,
  564. bool ignoreLevel = false,
  565. bool ignoreSelection = false)
  566. {
  567. if (targetPermanent == null) yield break;
  568. if (targetPermanent.TopCard == null) yield break;
  569. if (activateClass == null) yield break;
  570. if (activateClass.EffectSourceCard == null) yield break;
  571. Player owner = targetPermanent.TopCard.Owner;
  572. SelectCardEffect.Root root = SelectCardEffect.Root.Execution;
  573. bool ignoreDigivolutionRequirement = ignoreDigivolutionRequirementFixedCost >= 0;
  574. int fixedCost = -1;
  575. if (fixedCostTuple != null)
  576. {
  577. fixedCost = fixedCostTuple.Value.fixedCost;
  578. }
  579. if (ignoreDigivolutionRequirement)
  580. {
  581. fixedCost = ignoreDigivolutionRequirementFixedCost;
  582. }
  583. bool CanSelectCardCondition(CardSource cardSource)
  584. {
  585. if (cardSource.IsDigimon)
  586. {
  587. if (cardCondition == null || cardCondition(cardSource))
  588. {
  589. if (!cardSource.CanNotEvolve(targetPermanent))
  590. {
  591. if (ignoreLevel
  592. || cardSource.CanPlayCardTargetFrame(
  593. frame: targetPermanent.PermanentFrame,
  594. PayCost: payCost,
  595. cardEffect: activateClass,
  596. root: root,
  597. fixedCost: fixedCost))
  598. {
  599. return true;
  600. }
  601. }
  602. }
  603. }
  604. return false;
  605. }
  606. #region reduce cost
  607. Func<EffectTiming, ICardEffect> getChangeCostEffect = null;
  608. if (reduceCostTuple != null)
  609. {
  610. bool PermanentCondition(Permanent permanent)
  611. {
  612. return permanent == targetPermanent;
  613. }
  614. bool CardCondition(CardSource cardSource)
  615. {
  616. if (cardSource != null)
  617. {
  618. if (cardSource.Owner == owner)
  619. {
  620. if (reduceCostTuple.Value.reduceCostCardCondition == null || reduceCostTuple.Value.reduceCostCardCondition(cardSource))
  621. {
  622. return true;
  623. }
  624. }
  625. }
  626. return false;
  627. }
  628. getChangeCostEffect = ChangeDigivolutionCostPlayerEffect(
  629. permanentCondition: PermanentCondition,
  630. cardCondition: CardCondition,
  631. rootCondition: null,
  632. changeValue: -reduceCostTuple.Value.reduceCost,
  633. activateClass: activateClass,
  634. setFixedCost: false);
  635. if (getChangeCostEffect != null)
  636. {
  637. AddEffectToPlayer(
  638. effectDuration: EffectDuration.UntilCalculateFixedCost,
  639. card: targetPermanent.TopCard,
  640. cardEffect: null,
  641. timing: EffectTiming.None,
  642. getCardEffect: getChangeCostEffect);
  643. }
  644. }
  645. #endregion
  646. #region set fixed cost
  647. Func<EffectTiming, ICardEffect> getFixedCostEffect = null;
  648. if (fixedCostTuple != null)
  649. {
  650. bool PermanentCondition(Permanent permanent)
  651. {
  652. return permanent == targetPermanent;
  653. }
  654. bool CardCondition(CardSource cardSource)
  655. {
  656. if (cardSource != null)
  657. {
  658. if (cardSource.Owner == owner)
  659. {
  660. if (fixedCostTuple.Value.fixedCostCardCondition == null || fixedCostTuple.Value.fixedCostCardCondition(cardSource))
  661. {
  662. return true;
  663. }
  664. }
  665. }
  666. return false;
  667. }
  668. getFixedCostEffect = ChangeDigivolutionCostPlayerEffect(
  669. permanentCondition: PermanentCondition,
  670. cardCondition: CardCondition,
  671. rootCondition: null,
  672. changeValue: fixedCost,
  673. activateClass: activateClass,
  674. setFixedCost: true);
  675. if (getChangeCostEffect != null)
  676. {
  677. AddEffectToPlayer(
  678. effectDuration: EffectDuration.UntilCalculateFixedCost,
  679. card: targetPermanent.TopCard,
  680. cardEffect: null,
  681. timing: EffectTiming.None,
  682. getCardEffect: getFixedCostEffect);
  683. }
  684. }
  685. #endregion
  686. #region ignore digivolution requirement
  687. Func<EffectTiming, ICardEffect> getIgnoreDigivolutionRequirementEffect = null;
  688. if (ignoreDigivolutionRequirement)
  689. {
  690. bool PermanentCondition(Permanent permanent)
  691. {
  692. return permanent == targetPermanent;
  693. }
  694. bool CardCondition(CardSource cardSource)
  695. {
  696. if (cardSource == null)
  697. {
  698. return false;
  699. }
  700. if (cardSource.Owner != owner)
  701. {
  702. return false;
  703. }
  704. if (cardCondition == null || cardCondition(cardSource))
  705. {
  706. return true;
  707. }
  708. return false;
  709. }
  710. getIgnoreDigivolutionRequirementEffect = GainIgnoreDigivolutionRequirementPlayerEffect(
  711. permanentCondition: PermanentCondition,
  712. cardCondition: CardCondition,
  713. ignoreDigivolutionRequirement: ignoreDigivolutionRequirement,
  714. digivolutionCost: fixedCost,
  715. activateClass: activateClass);
  716. if (getIgnoreDigivolutionRequirementEffect != null)
  717. {
  718. AddEffectToPlayer(
  719. effectDuration: EffectDuration.UntilCalculateFixedCost,
  720. card: targetPermanent.TopCard,
  721. cardEffect: null,
  722. timing: EffectTiming.None,
  723. getCardEffect: getIgnoreDigivolutionRequirementEffect);
  724. }
  725. }
  726. #endregion
  727. List<CardSource> selectedCards = new List<CardSource>();
  728. IEnumerator SelectCardCoroutine(CardSource cardSource)
  729. {
  730. selectedCards.Add(cardSource);
  731. yield return null;
  732. }
  733. if (owner.ExecutingCards.Count(CanSelectCardCondition) >= 1)
  734. {
  735. int maxCount = 1;
  736. SelectCardEffect selectCardEffect = GManager.instance.GetComponent<SelectCardEffect>();
  737. selectCardEffect.SetUp(
  738. canTargetCondition: CanSelectCardCondition,
  739. canTargetCondition_ByPreSelecetedList: null,
  740. canEndSelectCondition: null,
  741. canNoSelect: () => true,
  742. selectCardCoroutine: SelectCardCoroutine,
  743. afterSelectCardCoroutine: null,
  744. message: "Select 1 card to digivolve.",
  745. maxCount: maxCount,
  746. canEndNotMax: false,
  747. isShowOpponent: true,
  748. mode: SelectCardEffect.Mode.Custom,
  749. root: SelectCardEffect.Root.Execution,
  750. customRootCardList: null,
  751. canLookReverseCard: true,
  752. selectPlayer: owner,
  753. cardEffect: activateClass);
  754. selectCardEffect.SetUpCustomMessage("Select 1 card to digivolve.", "The opponent is selecting 1 card to digivolve.");
  755. selectCardEffect.SetUpCustomMessage_ShowCard("Selected Card");
  756. yield return ContinuousController.instance.StartCoroutine(selectCardEffect.Activate());
  757. }
  758. else
  759. {
  760. selectedCards.Add(activateClass.EffectSourceCard);
  761. }
  762. PlayCardClass playCardClass = new PlayCardClass(
  763. cardSources: selectedCards,
  764. hashtable: CardEffectHashtable(activateClass),
  765. payCost: payCost,
  766. targetPermanent: targetPermanent,
  767. isTapped: false,
  768. root: root,
  769. activateETB: true);
  770. if (ignoreLevel) playCardClass.SetIgnoreLevel();
  771. if (!ignoreDigivolutionRequirement && fixedCost >= 0) playCardClass.SetFixedCost(fixedCost);
  772. yield return ContinuousController.instance.StartCoroutine(playCardClass.PlayCard());
  773. #region release effect
  774. if (getChangeCostEffect != null) owner.UntilCalculateFixedCostEffect.Remove(getChangeCostEffect);
  775. #endregion
  776. #region release effect
  777. if (getFixedCostEffect != null) owner.UntilCalculateFixedCostEffect.Remove(getFixedCostEffect);
  778. #endregion
  779. #region release effect
  780. if (getIgnoreDigivolutionRequirementEffect != null) owner.UntilCalculateFixedCostEffect.Remove(getIgnoreDigivolutionRequirementEffect);
  781. #endregion
  782. if (selectedCards.Count >= 1)
  783. {
  784. if (IsDigivolvedByTheEffect(targetPermanent, selectedCards[0], activateClass))
  785. {
  786. if (successProcess != null)
  787. {
  788. yield return ContinuousController.instance.StartCoroutine(successProcess);
  789. }
  790. }
  791. }
  792. }
  793. #endregion
  794. #region Get the effect given by EffectTiming
  795. public static Func<EffectTiming, ICardEffect> GetCardEffectByEffectTiming(EffectTiming timing, ICardEffect cardEffect) => (_timing) => _timing == timing ? cardEffect : null;
  796. #endregion
  797. }