CardEffectCommons.cs 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEditor;
  7. using UnityEditor.Experimental.GraphView;
  8. public partial class CardEffectCommons
  9. {
  10. #region Digivolution Requirements
  11. public enum IgnoreRequirement
  12. {
  13. None,
  14. All,
  15. Level,
  16. Color
  17. }
  18. #endregion
  19. #region Play cards as new permanents
  20. public static IEnumerator PlayPermanentCards(List<CardSource> cardSources, ICardEffect activateClass, bool payCost, bool isTapped, SelectCardEffect.Root root, bool activateETB, bool isBreedingArea = false, int fixedCost = -1)
  21. {
  22. if (cardSources == null) yield break;
  23. cardSources = cardSources
  24. .Filter(cardSource => cardSource != null)
  25. .Filter(cardSource => CanPlayAsNewPermanent(
  26. cardSource: cardSource,
  27. payCost: payCost,
  28. cardEffect: activateClass,
  29. isBreedingArea: isBreedingArea,
  30. fixedCost: fixedCost));
  31. if (cardSources.Count == 0) yield break;
  32. PlayCardClass playCardClass = new PlayCardClass(
  33. cardSources: cardSources,
  34. hashtable: CardEffectHashtable(activateClass),
  35. payCost: payCost,
  36. targetPermanent: null,
  37. isTapped: isTapped,
  38. root: root,
  39. activateETB: activateETB);
  40. if (isBreedingArea)
  41. playCardClass.SetIsBreedingArea();
  42. playCardClass.SetFixedCost(fixedCost);
  43. yield return ContinuousController.instance.StartCoroutine(playCardClass.PlayCard());
  44. }
  45. #endregion
  46. #region Play option cards
  47. public static IEnumerator PlayOptionCards(List<CardSource> cardSources, ICardEffect activateClass, bool payCost, SelectCardEffect.Root root,
  48. bool setAddSecurityEndOption = false)
  49. {
  50. if (cardSources == null) yield break;
  51. cardSources = cardSources
  52. .Filter(cardSource => cardSource != null)
  53. .Filter(cardSource => !cardSource.CanNotPlayThisOption);
  54. if (cardSources.Count == 0) yield break;
  55. PlayCardClass playCard = new PlayCardClass(
  56. cardSources: cardSources,
  57. hashtable: CardEffectHashtable(activateClass),
  58. payCost: payCost,
  59. targetPermanent: null,
  60. isTapped: false,
  61. root: root,
  62. activateETB: true);
  63. if (activateClass != null)
  64. {
  65. playCard.SetShowEffect();
  66. }
  67. if (setAddSecurityEndOption)
  68. {
  69. playCard.SetAddSecurityEndOption();
  70. }
  71. yield return ContinuousController.instance.StartCoroutine(playCard.PlayCard());
  72. }
  73. #endregion
  74. #region Play Delay Option cards as new permanents
  75. public static IEnumerator PlaceDelayOptionCards(CardSource card, ICardEffect cardEffect, SelectCardEffect.Root root = SelectCardEffect.Root.Execution)
  76. {
  77. if (CanPlayAsNewPermanent(cardSource: card, payCost: false, cardEffect: cardEffect, isPlayOption: true))
  78. {
  79. PlayPermanentClass playPermanent = new PlayPermanentClass(
  80. cardSources: new List<CardSource>() { card },
  81. hashtable: CardEffectHashtable(cardEffect),
  82. targetPermanent: null,
  83. isTapped: false,
  84. root: root,
  85. ActivateETB: true);
  86. playPermanent.SetISPlayOption();
  87. yield return ContinuousController.instance.StartCoroutine(playPermanent.PlayPermanent());
  88. if (IsExistOnBattleArea(card))
  89. {
  90. card.PermanentOfThisCard().IsPlayedOptionPermanent = true;
  91. }
  92. }
  93. }
  94. #endregion
  95. #region Play 1 Token
  96. public static IEnumerator PlayToken(CEntity_Base tokenData, ICardEffect activateClass, bool isOwnerPermanent, bool isTapped, int quantity = 1)
  97. {
  98. if (activateClass == null) yield break;
  99. if (activateClass.EffectSourceCard == null) yield break;
  100. if (tokenData == null) yield break;
  101. CardSource card = activateClass.EffectSourceCard;
  102. Player player = isOwnerPermanent ? card.Owner : card.Owner.Enemy;
  103. if (card.Owner.fieldCardFrames.Count((frame) => frame.IsEmptyFrame() && frame.IsBattleAreaFrame()) >= quantity)
  104. {
  105. List<CardSource> playCards = new List<CardSource>();
  106. for (int i = 0; i < quantity; i++)
  107. {
  108. CardSource tokenCard = CardObjectController.CreateCardSource(
  109. player.PlayerID,
  110. tokenData,
  111. true);
  112. playCards.Add(tokenCard);
  113. }
  114. if (CanPlayAsNewPermanent(cardSource: playCards[0], payCost: false, cardEffect: activateClass))
  115. {
  116. yield return ContinuousController.instance.StartCoroutine(new PlayCardClass(
  117. cardSources: playCards,
  118. hashtable: CardEffectHashtable(activateClass),
  119. payCost: false,
  120. targetPermanent: null,
  121. isTapped: isTapped,
  122. root: SelectCardEffect.Root.None,
  123. activateETB: true).PlayCard());
  124. }
  125. }
  126. }
  127. #endregion
  128. #region Play 1 [Diaboromon] Token
  129. public static IEnumerator PlayDiaboromonToken(ICardEffect activateClass,int quantity = 1)
  130. {
  131. yield return ContinuousController.instance.StartCoroutine(PlayToken(
  132. tokenData: ContinuousController.instance.DiaboromonToken,
  133. activateClass: activateClass,
  134. isOwnerPermanent: true,
  135. isTapped: false,
  136. quantity: quantity
  137. ));
  138. }
  139. #endregion
  140. #region Play 1 [Amon of Crimson Flame] Token
  141. public static IEnumerator PlayAmonToken(ICardEffect activateClass)
  142. {
  143. yield return ContinuousController.instance.StartCoroutine(PlayToken(
  144. tokenData: ContinuousController.instance.AmonToken,
  145. activateClass: activateClass,
  146. isOwnerPermanent: true,
  147. isTapped: false
  148. ));
  149. }
  150. #endregion
  151. #region Play 1 [Umon of Blue Thunder] Token
  152. public static IEnumerator PlayUmonToken(ICardEffect activateClass)
  153. {
  154. yield return ContinuousController.instance.StartCoroutine(PlayToken(
  155. tokenData: ContinuousController.instance.UmonToken,
  156. activateClass: activateClass,
  157. isOwnerPermanent: true,
  158. isTapped: false
  159. ));
  160. }
  161. #endregion
  162. #region Play 1 [Fujitsumon] Token
  163. public static IEnumerator PlayFujitsumonToken(ICardEffect activateClass, bool isOwnerPermanent)
  164. {
  165. yield return ContinuousController.instance.StartCoroutine(PlayToken(
  166. tokenData: ContinuousController.instance.FujitsumonToken,
  167. activateClass: activateClass,
  168. isOwnerPermanent: isOwnerPermanent,
  169. isTapped: true
  170. ));
  171. }
  172. #endregion
  173. #region Play 1 [Gyuukimon] Token
  174. public static IEnumerator PlayGyuukimonToken(ICardEffect activateClass)
  175. {
  176. yield return ContinuousController.instance.StartCoroutine(PlayToken(
  177. tokenData: ContinuousController.instance.GyuukimonToken,
  178. activateClass: activateClass,
  179. isOwnerPermanent: true,
  180. isTapped: false
  181. ));
  182. }
  183. #endregion
  184. #region Play 1 [KoHagurumon] Token
  185. public static IEnumerator PlayKoHagurumonToken(ICardEffect activateClass)
  186. {
  187. yield return ContinuousController.instance.StartCoroutine(PlayToken(
  188. tokenData: ContinuousController.instance.KoHagurumonToken,
  189. activateClass: activateClass,
  190. isOwnerPermanent: true,
  191. isTapped: false
  192. ));
  193. }
  194. #endregion
  195. #region Play 1 [Familiar] Token
  196. public static IEnumerator PlayFamiliarToken(ICardEffect activateClass, int quantity = 1)
  197. {
  198. yield return ContinuousController.instance.StartCoroutine(PlayToken(
  199. tokenData: ContinuousController.instance.FamiliarToken,
  200. activateClass: activateClass,
  201. isOwnerPermanent: true,
  202. isTapped: false,
  203. quantity: quantity
  204. ));
  205. }
  206. #endregion
  207. #region Play 1 [Volée & Zerdrücken] Token
  208. public static IEnumerator PlayVoleeZerdrucken(ICardEffect activateClass)
  209. {
  210. yield return ContinuousController.instance.StartCoroutine(PlayToken(
  211. tokenData: ContinuousController.instance.VoleeZerdruckenToken,
  212. activateClass: activateClass,
  213. isOwnerPermanent: true,
  214. isTapped: false
  215. ));
  216. }
  217. #endregion
  218. #region Security effect of "add this card to hand"
  219. public static IEnumerator AddThisCardToHand(CardSource card1, ICardEffect activateClass)
  220. {
  221. yield return new WaitForSeconds(0.5f);
  222. yield return ContinuousController.instance.StartCoroutine(card1.Owner.brainStormObject.CloseBrainstrorm(card1));
  223. yield return ContinuousController.instance.StartCoroutine(CardObjectController.AddHandCards(new List<CardSource>() { card1 }, false, activateClass));
  224. }
  225. #endregion
  226. #region Delete target permanents, and the effect determines whether the permanent has been deleted or not
  227. public static IEnumerator DeletePeremanentAndProcessAccordingToResult(List<Permanent> targetPermanents, ICardEffect activateClass, Func<List<Permanent>, IEnumerator> successProcess, Func<IEnumerator> failureProcess)
  228. {
  229. DestroyPermanentsClass destroyPermanentsClass = new DestroyPermanentsClass(targetPermanents, CardEffectHashtable(activateClass));
  230. yield return ContinuousController.instance.StartCoroutine(destroyPermanentsClass.Destroy());
  231. if (targetPermanents.Some((permanent) => destroyPermanentsClass.IsDestroyed(permanent)))
  232. {
  233. if (successProcess != null)
  234. {
  235. yield return ContinuousController.instance.StartCoroutine(successProcess(destroyPermanentsClass.DestroyedPermanents));
  236. }
  237. }
  238. else
  239. {
  240. if (failureProcess != null)
  241. {
  242. yield return ContinuousController.instance.StartCoroutine(failureProcess());
  243. }
  244. }
  245. }
  246. #endregion
  247. #region Bounce target 1 permanent, and the effect determines whether the permanent has been bounced or not
  248. public static IEnumerator BouncePeremanentAndProcessAccordingToResult(List<Permanent> targetPermanents, ICardEffect activateClass, IEnumerator successProcess, IEnumerator failureProcess)
  249. {
  250. HandBounceClaass bouncePermanentClass = new HandBounceClaass(targetPermanents, CardEffectHashtable(activateClass));
  251. yield return ContinuousController.instance.StartCoroutine(bouncePermanentClass.Bounce());
  252. if (targetPermanents.Some((permanent) => bouncePermanentClass.IsBounced(permanent)))
  253. {
  254. if (successProcess != null)
  255. {
  256. yield return ContinuousController.instance.StartCoroutine(successProcess);
  257. }
  258. }
  259. else
  260. {
  261. if (failureProcess != null)
  262. {
  263. yield return ContinuousController.instance.StartCoroutine(failureProcess);
  264. }
  265. }
  266. }
  267. #endregion
  268. #region Deck Bounce target 1 permanent, and the effect determines whether the permanent has been bounced or not
  269. public static IEnumerator DeckBouncePeremanentAndProcessAccordingToResult(List<Permanent> targetPermanents, ICardEffect activateClass, IEnumerator successProcess, IEnumerator failureProcess)
  270. {
  271. DeckBottomBounceClass deckBounceClass = new DeckBottomBounceClass(targetPermanents, CardEffectHashtable(activateClass));
  272. yield return ContinuousController.instance.StartCoroutine(deckBounceClass.DeckBounce());
  273. if (targetPermanents.Some((permanent) => deckBounceClass.IsDeckBounced(permanent)))
  274. {
  275. if (successProcess != null)
  276. {
  277. yield return ContinuousController.instance.StartCoroutine(successProcess);
  278. }
  279. }
  280. else
  281. {
  282. if (failureProcess != null)
  283. {
  284. yield return ContinuousController.instance.StartCoroutine(failureProcess);
  285. }
  286. }
  287. }
  288. #endregion
  289. #region Trash digivolution cards from top or bottom
  290. public static IEnumerator TrashDigivolutionCardsFromTopOrBottom(Permanent targetPermanent, int trashCount, bool isFromTop, ICardEffect activateClass)
  291. {
  292. if (activateClass == null) yield break;
  293. if (targetPermanent == null) yield break;
  294. if (targetPermanent.TopCard == null) yield break;
  295. if (targetPermanent.DigivolutionCards.Count((cardSource) => !cardSource.CanNotTrashFromDigivolutionCards(activateClass)) == 0) yield break;
  296. if (targetPermanent.TopCard.CanNotBeAffected(activateClass)) yield break;
  297. if (trashCount <= 0) yield break;
  298. List<CardSource> trashTargetCards = new List<CardSource>();
  299. for (int i = 0; i < trashCount; i++)
  300. {
  301. if (targetPermanent.DigivolutionCards.Count >= i + 1)
  302. {
  303. int index = isFromTop ? i : targetPermanent.DigivolutionCards.Count - 1 - i;
  304. CardSource trashTargetCard = targetPermanent.DigivolutionCards[index];
  305. trashTargetCards.Add(trashTargetCard);
  306. }
  307. }
  308. yield return ContinuousController.instance.StartCoroutine(new ITrashDigivolutionCards(targetPermanent, trashTargetCards, activateClass).TrashDigivolutionCards());
  309. }
  310. #endregion
  311. #region this Option card's Main effect
  312. public static ActivateClass OptionMainEffect(CardSource card) => card.EffectList(EffectTiming.OptionSkill).Find(cardEffect => cardEffect != null && cardEffect is ActivateClass && cardEffect.EffectDiscription.Contains("[Main]")) as ActivateClass;
  313. #endregion
  314. #region this Option card's Security effect
  315. public static ActivateClass OptionSecurityEffect(CardSource card) => card.EffectList(EffectTiming.SecuritySkill).Find(cardEffect => cardEffect != null && cardEffect is ActivateClass && cardEffect.EffectDiscription.Contains("[Security]")) as ActivateClass;
  316. #endregion
  317. #region Add Option's Security effect that "Activate this card's Main effect" to cardEffects
  318. public static void AddActivateMainOptionSecurityEffect(CardSource card, ref List<ICardEffect> cardEffects, string effectName, string effectDiscription = "", Func<ICardEffect, IEnumerator> afterMainEffect = null)
  319. {
  320. if (OptionMainEffect(card) == null) return;
  321. cardEffects.Add(CardEffectFactory.ActivateMainOptionSecurityEffect(card, effectName, effectDiscription, afterMainEffect));
  322. }
  323. #endregion
  324. #region Target permanent Digivolves into Digimon card from hand or trash
  325. public static IEnumerator DigivolveIntoHandOrTrashCard(
  326. Permanent targetPermanent,
  327. Func<CardSource, bool> cardCondition,
  328. bool payCost,
  329. (int reduceCost, Func<CardSource, bool> reduceCostCardCondition)? reduceCostTuple,
  330. (int fixedCost, Func<CardSource, bool> fixedCostCardCondition)? fixedCostTuple,
  331. int ignoreDigivolutionRequirementFixedCost,
  332. bool isHand,
  333. ICardEffect activateClass,
  334. IEnumerator successProcess,
  335. //bool ignoreLevel = false,
  336. bool ignoreSelection = false,
  337. IgnoreRequirement ignoreRequirements = IgnoreRequirement.None,
  338. IEnumerator failedProcess = null)
  339. {
  340. if (targetPermanent == null) yield break;
  341. if (targetPermanent.TopCard == null) yield break;
  342. if (activateClass == null) yield break;
  343. if (activateClass.EffectSourceCard == null) yield break;
  344. Player owner = targetPermanent.TopCard.Owner;
  345. SelectCardEffect.Root root = isHand ? SelectCardEffect.Root.Hand : SelectCardEffect.Root.Trash;
  346. bool ignoreDigivolutionRequirement = !ignoreRequirements.Equals(IgnoreRequirement.None) || ignoreDigivolutionRequirementFixedCost >= 0;// ignoreDigivolutionRequirementFixedCost >= 0 || ignoreRequirements;
  347. int fixedCost = -1;
  348. bool successful = false;
  349. if (fixedCostTuple != null)
  350. {
  351. fixedCost = fixedCostTuple.Value.fixedCost;
  352. }
  353. Debug.Log($"Digivolve into hand or trash: {ignoreDigivolutionRequirement}");
  354. if (ignoreDigivolutionRequirement)
  355. {
  356. fixedCost = ignoreDigivolutionRequirementFixedCost;
  357. }
  358. bool CanSelectCardCondition(CardSource cardSource)
  359. {
  360. if (cardSource.IsDigimon)
  361. {
  362. if (cardCondition == null || cardCondition(cardSource))
  363. {
  364. if (!cardSource.CanNotEvolve(targetPermanent))
  365. {
  366. if (cardSource.CanPlayCardTargetFrame(
  367. frame: targetPermanent.PermanentFrame,
  368. PayCost: payCost,
  369. cardEffect: activateClass,
  370. root: root,
  371. fixedCost: fixedCost,
  372. ignore: ignoreRequirements))
  373. {
  374. return true;
  375. }
  376. }
  377. }
  378. }
  379. return false;
  380. }
  381. #region reduce cost
  382. Func<EffectTiming, ICardEffect> getChangeCostEffect = null;
  383. if (reduceCostTuple != null)
  384. {
  385. bool PermanentCondition(Permanent permanent)
  386. {
  387. return permanent == targetPermanent;
  388. }
  389. bool CardCondition(CardSource cardSource)
  390. {
  391. if (cardSource != null)
  392. {
  393. if (cardSource.Owner == owner)
  394. {
  395. if (reduceCostTuple.Value.reduceCostCardCondition == null || reduceCostTuple.Value.reduceCostCardCondition(cardSource))
  396. {
  397. return true;
  398. }
  399. }
  400. }
  401. return false;
  402. }
  403. getChangeCostEffect = ChangeDigivolutionCostPlayerEffect(
  404. permanentCondition: PermanentCondition,
  405. cardCondition: CardCondition,
  406. rootCondition: null,
  407. changeValue: -reduceCostTuple.Value.reduceCost,
  408. activateClass: activateClass,
  409. setFixedCost: false);
  410. if (getChangeCostEffect != null)
  411. {
  412. AddEffectToPlayer(
  413. effectDuration: EffectDuration.UntilCalculateFixedCost,
  414. card: targetPermanent.TopCard,
  415. cardEffect: null,
  416. timing: EffectTiming.None,
  417. getCardEffect: getChangeCostEffect);
  418. }
  419. }
  420. #endregion
  421. #region set fixed cost
  422. Func<EffectTiming, ICardEffect> getFixedCostEffect = null;
  423. if (fixedCostTuple != null)
  424. {
  425. bool PermanentCondition(Permanent permanent)
  426. {
  427. return permanent == targetPermanent;
  428. }
  429. bool CardCondition(CardSource cardSource)
  430. {
  431. if (cardSource != null)
  432. {
  433. if (cardSource.Owner == owner)
  434. {
  435. if (fixedCostTuple.Value.fixedCostCardCondition == null || fixedCostTuple.Value.fixedCostCardCondition(cardSource))
  436. {
  437. return true;
  438. }
  439. }
  440. }
  441. return false;
  442. }
  443. getFixedCostEffect = ChangeDigivolutionCostPlayerEffect(
  444. permanentCondition: PermanentCondition,
  445. cardCondition: CardCondition,
  446. rootCondition: null,
  447. changeValue: fixedCost,
  448. activateClass: activateClass,
  449. setFixedCost: true);
  450. if (getChangeCostEffect != null)
  451. {
  452. AddEffectToPlayer(
  453. effectDuration: EffectDuration.UntilCalculateFixedCost,
  454. card: targetPermanent.TopCard,
  455. cardEffect: null,
  456. timing: EffectTiming.None,
  457. getCardEffect: getFixedCostEffect);
  458. }
  459. }
  460. #endregion
  461. #region ignore digivolution requirement
  462. Func<EffectTiming, ICardEffect> getIgnoreDigivolutionRequirementEffect = null;
  463. if (ignoreDigivolutionRequirement)
  464. {
  465. bool PermanentCondition(Permanent permanent)
  466. {
  467. return permanent == targetPermanent;
  468. }
  469. bool CardCondition(CardSource cardSource)
  470. {
  471. if (cardSource == null)
  472. {
  473. return false;
  474. }
  475. if (cardSource.Owner != owner)
  476. {
  477. return false;
  478. }
  479. if (cardCondition == null || cardCondition(cardSource))
  480. {
  481. return true;
  482. }
  483. return false;
  484. }
  485. getIgnoreDigivolutionRequirementEffect = GainIgnoreDigivolutionRequirementPlayerEffect(
  486. permanentCondition: PermanentCondition,
  487. cardCondition: CardCondition,
  488. ignoreDigivolutionRequirement: ignoreDigivolutionRequirement,
  489. digivolutionCost: fixedCost,
  490. activateClass: activateClass);
  491. if (getIgnoreDigivolutionRequirementEffect != null)
  492. {
  493. AddEffectToPlayer(
  494. effectDuration: EffectDuration.UntilCalculateFixedCost,
  495. card: targetPermanent.TopCard,
  496. cardEffect: null,
  497. timing: EffectTiming.None,
  498. getCardEffect: getIgnoreDigivolutionRequirementEffect);
  499. }
  500. }
  501. #endregion
  502. List<CardSource> selectedCards = new List<CardSource>();
  503. IEnumerator SelectCardCoroutine(CardSource cardSource)
  504. {
  505. selectedCards.Add(cardSource);
  506. yield return null;
  507. }
  508. if (isHand)
  509. {
  510. if (owner.HandCards.Count(CanSelectCardCondition) >= 1)
  511. {
  512. int maxCount = Mathf.Min(1, owner.HandCards.Count(CanSelectCardCondition));
  513. SelectHandEffect selectHandEffect = GManager.instance.GetComponent<SelectHandEffect>();
  514. selectHandEffect.SetUp(
  515. selectPlayer: owner,
  516. canTargetCondition: CanSelectCardCondition,
  517. canTargetCondition_ByPreSelecetedList: null,
  518. canEndSelectCondition: null,
  519. maxCount: maxCount,
  520. canNoSelect: true,
  521. canEndNotMax: false,
  522. isShowOpponent: true,
  523. selectCardCoroutine: SelectCardCoroutine,
  524. afterSelectCardCoroutine: null,
  525. mode: SelectHandEffect.Mode.Custom,
  526. cardEffect: activateClass);
  527. selectHandEffect.SetUpCustomMessage("Select 1 card to digivolve.", "The opponent is selecting 1 card to digivolve.");
  528. selectHandEffect.SetUpCustomMessage_ShowCard("Selected Card");
  529. yield return ContinuousController.instance.StartCoroutine(selectHandEffect.Activate());
  530. }
  531. }
  532. else
  533. {
  534. if (HasMatchConditionOwnersCardInTrash(targetPermanent.TopCard, CanSelectCardCondition) && !ignoreSelection)
  535. {
  536. int maxCount = 1;
  537. SelectCardEffect selectCardEffect = GManager.instance.GetComponent<SelectCardEffect>();
  538. selectCardEffect.SetUp(
  539. canTargetCondition: CanSelectCardCondition,
  540. canTargetCondition_ByPreSelecetedList: null,
  541. canEndSelectCondition: null,
  542. canNoSelect: () => true,
  543. selectCardCoroutine: SelectCardCoroutine,
  544. afterSelectCardCoroutine: null,
  545. message: "Select 1 card to digivolve.",
  546. maxCount: maxCount,
  547. canEndNotMax: false,
  548. isShowOpponent: true,
  549. mode: SelectCardEffect.Mode.Custom,
  550. root: SelectCardEffect.Root.Trash,
  551. customRootCardList: null,
  552. canLookReverseCard: true,
  553. selectPlayer: owner,
  554. cardEffect: activateClass);
  555. selectCardEffect.SetUpCustomMessage("Select 1 card to digivolve.", "The opponent is selecting 1 card to digivolve.");
  556. selectCardEffect.SetUpCustomMessage_ShowCard("Selected Card");
  557. yield return ContinuousController.instance.StartCoroutine(selectCardEffect.Activate());
  558. }
  559. else
  560. {
  561. if(ignoreSelection)
  562. selectedCards.Add(activateClass.EffectSourceCard);
  563. }
  564. }
  565. PlayCardClass playCardClass = new PlayCardClass(
  566. cardSources: selectedCards,
  567. hashtable: CardEffectHashtable(activateClass),
  568. payCost: payCost,
  569. targetPermanent: targetPermanent,
  570. isTapped: false,
  571. root: root,
  572. activateETB: true);
  573. //if (ignoreRequirements.Equals(IgnoreRequirement.All) || ignoreRequirements.Equals(IgnoreRequirement.Level))
  574. // playCardClass.SetIgnoreLevel();
  575. playCardClass.SetIgnoreRequirements(ignoreRequirements);
  576. //!ignoreRequirements
  577. if (fixedCost >= 0)
  578. playCardClass.SetFixedCost(fixedCost);
  579. yield return ContinuousController.instance.StartCoroutine(playCardClass.PlayCard());
  580. #region release effect
  581. if (getChangeCostEffect != null) owner.UntilCalculateFixedCostEffect.Remove(getChangeCostEffect);
  582. #endregion
  583. #region release effect
  584. if (getFixedCostEffect != null) owner.UntilCalculateFixedCostEffect.Remove(getFixedCostEffect);
  585. #endregion
  586. #region release effect
  587. if (getIgnoreDigivolutionRequirementEffect != null) owner.UntilCalculateFixedCostEffect.Remove(getIgnoreDigivolutionRequirementEffect);
  588. #endregion
  589. if (selectedCards.Count >= 1)
  590. {
  591. if (IsDigivolvedByTheEffect(targetPermanent, selectedCards[0], activateClass))
  592. {
  593. successful = true;
  594. }
  595. }
  596. if (successful)
  597. {
  598. if (successProcess != null)
  599. yield return ContinuousController.instance.StartCoroutine(successProcess);
  600. }
  601. else
  602. {
  603. if (failedProcess != null)
  604. yield return ContinuousController.instance.StartCoroutine(failedProcess);
  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. }