CardEffectCommons.cs 36 KB

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