CardEffectCommons.cs 41 KB

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