CardEffectCommons.cs 38 KB

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