CardEffectCommons.cs 39 KB

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