CardEffectCommons.cs 37 KB

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