CardEffectCommons.cs 37 KB

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