CardEffectCommons.cs 36 KB

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