CardEffectCommons.cs 35 KB

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