CardEffectCommons.cs 34 KB

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