CardEffectCommons.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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. Debug.Log($"Digivolve into hand or trash: {ignoreDigivolutionRequirement}");
  351. if (ignoreDigivolutionRequirement)
  352. {
  353. fixedCost = ignoreDigivolutionRequirementFixedCost;
  354. }
  355. bool CanSelectCardCondition(CardSource cardSource)
  356. {
  357. if (cardSource.IsDigimon)
  358. {
  359. if (cardCondition == null || cardCondition(cardSource))
  360. {
  361. if (!cardSource.CanNotEvolve(targetPermanent))
  362. {
  363. if (cardSource.CanPlayCardTargetFrame(
  364. frame: targetPermanent.PermanentFrame,
  365. PayCost: payCost,
  366. cardEffect: activateClass,
  367. root: root,
  368. fixedCost: fixedCost,
  369. ignore: ignoreRequirements))
  370. {
  371. return true;
  372. }
  373. }
  374. }
  375. }
  376. return false;
  377. }
  378. #region reduce cost
  379. Func<EffectTiming, ICardEffect> getChangeCostEffect = null;
  380. if (reduceCostTuple != null)
  381. {
  382. bool PermanentCondition(Permanent permanent)
  383. {
  384. return permanent == targetPermanent;
  385. }
  386. bool CardCondition(CardSource cardSource)
  387. {
  388. if (cardSource != null)
  389. {
  390. if (cardSource.Owner == owner)
  391. {
  392. if (reduceCostTuple.Value.reduceCostCardCondition == null || reduceCostTuple.Value.reduceCostCardCondition(cardSource))
  393. {
  394. return true;
  395. }
  396. }
  397. }
  398. return false;
  399. }
  400. getChangeCostEffect = ChangeDigivolutionCostPlayerEffect(
  401. permanentCondition: PermanentCondition,
  402. cardCondition: CardCondition,
  403. rootCondition: null,
  404. changeValue: -reduceCostTuple.Value.reduceCost,
  405. activateClass: activateClass,
  406. setFixedCost: false);
  407. if (getChangeCostEffect != null)
  408. {
  409. AddEffectToPlayer(
  410. effectDuration: EffectDuration.UntilCalculateFixedCost,
  411. card: targetPermanent.TopCard,
  412. cardEffect: null,
  413. timing: EffectTiming.None,
  414. getCardEffect: getChangeCostEffect);
  415. }
  416. }
  417. #endregion
  418. #region set fixed cost
  419. Func<EffectTiming, ICardEffect> getFixedCostEffect = null;
  420. if (fixedCostTuple != null)
  421. {
  422. bool PermanentCondition(Permanent permanent)
  423. {
  424. return permanent == targetPermanent;
  425. }
  426. bool CardCondition(CardSource cardSource)
  427. {
  428. if (cardSource != null)
  429. {
  430. if (cardSource.Owner == owner)
  431. {
  432. if (fixedCostTuple.Value.fixedCostCardCondition == null || fixedCostTuple.Value.fixedCostCardCondition(cardSource))
  433. {
  434. return true;
  435. }
  436. }
  437. }
  438. return false;
  439. }
  440. getFixedCostEffect = ChangeDigivolutionCostPlayerEffect(
  441. permanentCondition: PermanentCondition,
  442. cardCondition: CardCondition,
  443. rootCondition: null,
  444. changeValue: fixedCost,
  445. activateClass: activateClass,
  446. setFixedCost: true);
  447. if (getChangeCostEffect != null)
  448. {
  449. AddEffectToPlayer(
  450. effectDuration: EffectDuration.UntilCalculateFixedCost,
  451. card: targetPermanent.TopCard,
  452. cardEffect: null,
  453. timing: EffectTiming.None,
  454. getCardEffect: getFixedCostEffect);
  455. }
  456. }
  457. #endregion
  458. #region ignore digivolution requirement
  459. Func<EffectTiming, ICardEffect> getIgnoreDigivolutionRequirementEffect = null;
  460. if (ignoreDigivolutionRequirement)
  461. {
  462. bool PermanentCondition(Permanent permanent)
  463. {
  464. return permanent == targetPermanent;
  465. }
  466. bool CardCondition(CardSource cardSource)
  467. {
  468. if (cardSource == null)
  469. {
  470. return false;
  471. }
  472. if (cardSource.Owner != owner)
  473. {
  474. return false;
  475. }
  476. if (cardCondition == null || cardCondition(cardSource))
  477. {
  478. return true;
  479. }
  480. return false;
  481. }
  482. getIgnoreDigivolutionRequirementEffect = GainIgnoreDigivolutionRequirementPlayerEffect(
  483. permanentCondition: PermanentCondition,
  484. cardCondition: CardCondition,
  485. ignoreDigivolutionRequirement: ignoreDigivolutionRequirement,
  486. digivolutionCost: fixedCost,
  487. activateClass: activateClass);
  488. if (getIgnoreDigivolutionRequirementEffect != null)
  489. {
  490. AddEffectToPlayer(
  491. effectDuration: EffectDuration.UntilCalculateFixedCost,
  492. card: targetPermanent.TopCard,
  493. cardEffect: null,
  494. timing: EffectTiming.None,
  495. getCardEffect: getIgnoreDigivolutionRequirementEffect);
  496. }
  497. }
  498. #endregion
  499. List<CardSource> selectedCards = new List<CardSource>();
  500. IEnumerator SelectCardCoroutine(CardSource cardSource)
  501. {
  502. selectedCards.Add(cardSource);
  503. yield return null;
  504. }
  505. if (isHand)
  506. {
  507. if (owner.HandCards.Count(CanSelectCardCondition) >= 1)
  508. {
  509. int maxCount = Mathf.Min(1, owner.HandCards.Count(CanSelectCardCondition));
  510. SelectHandEffect selectHandEffect = GManager.instance.GetComponent<SelectHandEffect>();
  511. selectHandEffect.SetUp(
  512. selectPlayer: owner,
  513. canTargetCondition: CanSelectCardCondition,
  514. canTargetCondition_ByPreSelecetedList: null,
  515. canEndSelectCondition: null,
  516. maxCount: maxCount,
  517. canNoSelect: true,
  518. canEndNotMax: false,
  519. isShowOpponent: true,
  520. selectCardCoroutine: SelectCardCoroutine,
  521. afterSelectCardCoroutine: null,
  522. mode: SelectHandEffect.Mode.Custom,
  523. cardEffect: activateClass);
  524. selectHandEffect.SetUpCustomMessage("Select 1 card to digivolve.", "The opponent is selecting 1 card to digivolve.");
  525. selectHandEffect.SetUpCustomMessage_ShowCard("Selected Card");
  526. yield return ContinuousController.instance.StartCoroutine(selectHandEffect.Activate());
  527. }
  528. }
  529. else
  530. {
  531. if (HasMatchConditionOwnersCardInTrash(targetPermanent.TopCard, CanSelectCardCondition) && !ignoreSelection)
  532. {
  533. int maxCount = 1;
  534. SelectCardEffect selectCardEffect = GManager.instance.GetComponent<SelectCardEffect>();
  535. selectCardEffect.SetUp(
  536. canTargetCondition: CanSelectCardCondition,
  537. canTargetCondition_ByPreSelecetedList: null,
  538. canEndSelectCondition: null,
  539. canNoSelect: () => true,
  540. selectCardCoroutine: SelectCardCoroutine,
  541. afterSelectCardCoroutine: null,
  542. message: "Select 1 card to digivolve.",
  543. maxCount: maxCount,
  544. canEndNotMax: false,
  545. isShowOpponent: true,
  546. mode: SelectCardEffect.Mode.Custom,
  547. root: SelectCardEffect.Root.Trash,
  548. customRootCardList: null,
  549. canLookReverseCard: true,
  550. selectPlayer: owner,
  551. cardEffect: activateClass);
  552. selectCardEffect.SetUpCustomMessage("Select 1 card to digivolve.", "The opponent is selecting 1 card to digivolve.");
  553. selectCardEffect.SetUpCustomMessage_ShowCard("Selected Card");
  554. yield return ContinuousController.instance.StartCoroutine(selectCardEffect.Activate());
  555. }
  556. else
  557. {
  558. selectedCards.Add(activateClass.EffectSourceCard);
  559. }
  560. }
  561. PlayCardClass playCardClass = new PlayCardClass(
  562. cardSources: selectedCards,
  563. hashtable: CardEffectHashtable(activateClass),
  564. payCost: payCost,
  565. targetPermanent: targetPermanent,
  566. isTapped: false,
  567. root: root,
  568. activateETB: true);
  569. //if (ignoreRequirements.Equals(IgnoreRequirement.All) || ignoreRequirements.Equals(IgnoreRequirement.Level))
  570. // playCardClass.SetIgnoreLevel();
  571. playCardClass.SetIgnoreRequirements(ignoreRequirements);
  572. //!ignoreRequirements
  573. if (fixedCost >= 0)
  574. playCardClass.SetFixedCost(fixedCost);
  575. yield return ContinuousController.instance.StartCoroutine(playCardClass.PlayCard());
  576. #region release effect
  577. if (getChangeCostEffect != null) owner.UntilCalculateFixedCostEffect.Remove(getChangeCostEffect);
  578. #endregion
  579. #region release effect
  580. if (getFixedCostEffect != null) owner.UntilCalculateFixedCostEffect.Remove(getFixedCostEffect);
  581. #endregion
  582. #region release effect
  583. if (getIgnoreDigivolutionRequirementEffect != null) owner.UntilCalculateFixedCostEffect.Remove(getIgnoreDigivolutionRequirementEffect);
  584. #endregion
  585. if (selectedCards.Count >= 1)
  586. {
  587. if (IsDigivolvedByTheEffect(targetPermanent, selectedCards[0], activateClass))
  588. {
  589. if (successProcess != null)
  590. {
  591. yield return ContinuousController.instance.StartCoroutine(successProcess);
  592. }
  593. }
  594. }
  595. }
  596. #endregion
  597. #region Target permanent Digivolves into Digimon card execution area
  598. public static IEnumerator DigivolveIntoExcecutingAreaCard(
  599. Permanent targetPermanent,
  600. Func<CardSource, bool> cardCondition,
  601. bool payCost,
  602. (int reduceCost, Func<CardSource, bool> reduceCostCardCondition)? reduceCostTuple,
  603. (int fixedCost, Func<CardSource, bool> fixedCostCardCondition)? fixedCostTuple,
  604. int ignoreDigivolutionRequirementFixedCost,
  605. ICardEffect activateClass,
  606. IEnumerator successProcess,
  607. bool ignoreLevel = false,
  608. bool ignoreSelection = false)
  609. {
  610. if (targetPermanent == null) yield break;
  611. if (targetPermanent.TopCard == null) yield break;
  612. if (activateClass == null) yield break;
  613. if (activateClass.EffectSourceCard == null) yield break;
  614. Player owner = targetPermanent.TopCard.Owner;
  615. SelectCardEffect.Root root = SelectCardEffect.Root.Execution;
  616. bool ignoreDigivolutionRequirement = ignoreDigivolutionRequirementFixedCost >= 0;
  617. int fixedCost = -1;
  618. if (fixedCostTuple != null)
  619. {
  620. fixedCost = fixedCostTuple.Value.fixedCost;
  621. }
  622. if (ignoreDigivolutionRequirement)
  623. {
  624. fixedCost = ignoreDigivolutionRequirementFixedCost;
  625. }
  626. bool CanSelectCardCondition(CardSource cardSource)
  627. {
  628. if (cardSource.IsDigimon)
  629. {
  630. if (cardCondition == null || cardCondition(cardSource))
  631. {
  632. if (!cardSource.CanNotEvolve(targetPermanent))
  633. {
  634. if (ignoreLevel
  635. || cardSource.CanPlayCardTargetFrame(
  636. frame: targetPermanent.PermanentFrame,
  637. PayCost: payCost,
  638. cardEffect: activateClass,
  639. root: root,
  640. fixedCost: fixedCost))
  641. {
  642. return true;
  643. }
  644. }
  645. }
  646. }
  647. return false;
  648. }
  649. #region reduce cost
  650. Func<EffectTiming, ICardEffect> getChangeCostEffect = null;
  651. if (reduceCostTuple != null)
  652. {
  653. bool PermanentCondition(Permanent permanent)
  654. {
  655. return permanent == targetPermanent;
  656. }
  657. bool CardCondition(CardSource cardSource)
  658. {
  659. if (cardSource != null)
  660. {
  661. if (cardSource.Owner == owner)
  662. {
  663. if (reduceCostTuple.Value.reduceCostCardCondition == null || reduceCostTuple.Value.reduceCostCardCondition(cardSource))
  664. {
  665. return true;
  666. }
  667. }
  668. }
  669. return false;
  670. }
  671. getChangeCostEffect = ChangeDigivolutionCostPlayerEffect(
  672. permanentCondition: PermanentCondition,
  673. cardCondition: CardCondition,
  674. rootCondition: null,
  675. changeValue: -reduceCostTuple.Value.reduceCost,
  676. activateClass: activateClass,
  677. setFixedCost: false);
  678. if (getChangeCostEffect != null)
  679. {
  680. AddEffectToPlayer(
  681. effectDuration: EffectDuration.UntilCalculateFixedCost,
  682. card: targetPermanent.TopCard,
  683. cardEffect: null,
  684. timing: EffectTiming.None,
  685. getCardEffect: getChangeCostEffect);
  686. }
  687. }
  688. #endregion
  689. #region set fixed cost
  690. Func<EffectTiming, ICardEffect> getFixedCostEffect = null;
  691. if (fixedCostTuple != null)
  692. {
  693. bool PermanentCondition(Permanent permanent)
  694. {
  695. return permanent == targetPermanent;
  696. }
  697. bool CardCondition(CardSource cardSource)
  698. {
  699. if (cardSource != null)
  700. {
  701. if (cardSource.Owner == owner)
  702. {
  703. if (fixedCostTuple.Value.fixedCostCardCondition == null || fixedCostTuple.Value.fixedCostCardCondition(cardSource))
  704. {
  705. return true;
  706. }
  707. }
  708. }
  709. return false;
  710. }
  711. getFixedCostEffect = ChangeDigivolutionCostPlayerEffect(
  712. permanentCondition: PermanentCondition,
  713. cardCondition: CardCondition,
  714. rootCondition: null,
  715. changeValue: fixedCost,
  716. activateClass: activateClass,
  717. setFixedCost: true);
  718. if (getChangeCostEffect != null)
  719. {
  720. AddEffectToPlayer(
  721. effectDuration: EffectDuration.UntilCalculateFixedCost,
  722. card: targetPermanent.TopCard,
  723. cardEffect: null,
  724. timing: EffectTiming.None,
  725. getCardEffect: getFixedCostEffect);
  726. }
  727. }
  728. #endregion
  729. #region ignore digivolution requirement
  730. Func<EffectTiming, ICardEffect> getIgnoreDigivolutionRequirementEffect = null;
  731. if (ignoreDigivolutionRequirement)
  732. {
  733. bool PermanentCondition(Permanent permanent)
  734. {
  735. return permanent == targetPermanent;
  736. }
  737. bool CardCondition(CardSource cardSource)
  738. {
  739. if (cardSource == null)
  740. {
  741. return false;
  742. }
  743. if (cardSource.Owner != owner)
  744. {
  745. return false;
  746. }
  747. if (cardCondition == null || cardCondition(cardSource))
  748. {
  749. return true;
  750. }
  751. return false;
  752. }
  753. getIgnoreDigivolutionRequirementEffect = GainIgnoreDigivolutionRequirementPlayerEffect(
  754. permanentCondition: PermanentCondition,
  755. cardCondition: CardCondition,
  756. ignoreDigivolutionRequirement: ignoreDigivolutionRequirement,
  757. digivolutionCost: fixedCost,
  758. activateClass: activateClass);
  759. if (getIgnoreDigivolutionRequirementEffect != null)
  760. {
  761. AddEffectToPlayer(
  762. effectDuration: EffectDuration.UntilCalculateFixedCost,
  763. card: targetPermanent.TopCard,
  764. cardEffect: null,
  765. timing: EffectTiming.None,
  766. getCardEffect: getIgnoreDigivolutionRequirementEffect);
  767. }
  768. }
  769. #endregion
  770. List<CardSource> selectedCards = new List<CardSource>();
  771. IEnumerator SelectCardCoroutine(CardSource cardSource)
  772. {
  773. selectedCards.Add(cardSource);
  774. yield return null;
  775. }
  776. if (owner.ExecutingCards.Count(CanSelectCardCondition) >= 1)
  777. {
  778. int maxCount = 1;
  779. SelectCardEffect selectCardEffect = GManager.instance.GetComponent<SelectCardEffect>();
  780. selectCardEffect.SetUp(
  781. canTargetCondition: CanSelectCardCondition,
  782. canTargetCondition_ByPreSelecetedList: null,
  783. canEndSelectCondition: null,
  784. canNoSelect: () => true,
  785. selectCardCoroutine: SelectCardCoroutine,
  786. afterSelectCardCoroutine: null,
  787. message: "Select 1 card to digivolve.",
  788. maxCount: maxCount,
  789. canEndNotMax: false,
  790. isShowOpponent: true,
  791. mode: SelectCardEffect.Mode.Custom,
  792. root: SelectCardEffect.Root.Execution,
  793. customRootCardList: null,
  794. canLookReverseCard: true,
  795. selectPlayer: owner,
  796. cardEffect: activateClass);
  797. selectCardEffect.SetUpCustomMessage("Select 1 card to digivolve.", "The opponent is selecting 1 card to digivolve.");
  798. selectCardEffect.SetUpCustomMessage_ShowCard("Selected Card");
  799. yield return ContinuousController.instance.StartCoroutine(selectCardEffect.Activate());
  800. }
  801. else
  802. {
  803. selectedCards.Add(activateClass.EffectSourceCard);
  804. }
  805. PlayCardClass playCardClass = new PlayCardClass(
  806. cardSources: selectedCards,
  807. hashtable: CardEffectHashtable(activateClass),
  808. payCost: payCost,
  809. targetPermanent: targetPermanent,
  810. isTapped: false,
  811. root: root,
  812. activateETB: true);
  813. if (ignoreLevel) playCardClass.SetIgnoreLevel();
  814. if (!ignoreDigivolutionRequirement && fixedCost >= 0) playCardClass.SetFixedCost(fixedCost);
  815. yield return ContinuousController.instance.StartCoroutine(playCardClass.PlayCard());
  816. #region release effect
  817. if (getChangeCostEffect != null) owner.UntilCalculateFixedCostEffect.Remove(getChangeCostEffect);
  818. #endregion
  819. #region release effect
  820. if (getFixedCostEffect != null) owner.UntilCalculateFixedCostEffect.Remove(getFixedCostEffect);
  821. #endregion
  822. #region release effect
  823. if (getIgnoreDigivolutionRequirementEffect != null) owner.UntilCalculateFixedCostEffect.Remove(getIgnoreDigivolutionRequirementEffect);
  824. #endregion
  825. if (selectedCards.Count >= 1)
  826. {
  827. if (IsDigivolvedByTheEffect(targetPermanent, selectedCards[0], activateClass))
  828. {
  829. if (successProcess != null)
  830. {
  831. yield return ContinuousController.instance.StartCoroutine(successProcess);
  832. }
  833. }
  834. }
  835. }
  836. #endregion
  837. #region Get the effect given by EffectTiming
  838. public static Func<EffectTiming, ICardEffect> GetCardEffectByEffectTiming(EffectTiming timing, ICardEffect cardEffect) => (_timing) => _timing == timing ? cardEffect : null;
  839. #endregion
  840. }