CardEffectCommons.cs 42 KB

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