CardEffectCommons.cs 35 KB

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