CardEffectCommons.cs 36 KB

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