MultipleSkills.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Photon;
  5. using Photon.Pun;
  6. using System;
  7. using System.Linq;
  8. public class MultipleSkills : MonoBehaviourPunCallbacks
  9. {
  10. public bool IsUsing { get; private set; } = false;
  11. AutoProcessing _autoProcessing = null;
  12. public List<SkillInfo> SkillInfos_used { get; private set; } = new List<SkillInfo>();
  13. public List<SkillInfo> StackedSkillInfos = new List<SkillInfo>();
  14. public bool IsOnlyHandEffectStacked => StackedSkillInfos.Every(skillInfo =>
  15. skillInfo.CardEffect != null && skillInfo.CardEffect.EffectSourceCard != null && CardEffectCommons.IsExistOnHand(skillInfo.CardEffect.EffectSourceCard) && skillInfo.CardEffect.EffectDiscription.Contains("[Hand]"));
  16. bool IsOnlyOptionalEffectStacked => StackedSkillInfos.Every(skillInfo => skillInfo.CardEffect != null && skillInfo.CardEffect.IsOptionalCondition(null));
  17. bool IsEachStackedEffectHasDistinctSourceCard => StackedSkillInfos.Filter(skillInfo1 => skillInfo1.CardEffect != null && skillInfo1.CardEffect.EffectSourceCard != null)
  18. .Every(skillInfo => StackedSkillInfos.Filter(skillInfo1 => skillInfo1.CardEffect != null && skillInfo1.CardEffect.EffectSourceCard != null)
  19. .Count(otherSkillInfo => otherSkillInfo != skillInfo && skillInfo.CardEffect.EffectSourceCard == otherSkillInfo.CardEffect.EffectSourceCard) == 0);
  20. public IEnumerator ActivateMultipleSkills(List<SkillInfo> skillInfos, AutoProcessing autoProcessing, bool CheckNewTriggredSkill_mainStack, Func<List<SkillInfo>, SkillInfo, bool> skipCondition)
  21. {
  22. _skillIndex = 0;
  23. IsUsing = true;
  24. SkillInfos_used = new List<SkillInfo>();
  25. StackedSkillInfos = new List<SkillInfo>();
  26. _autoProcessing = autoProcessing;
  27. List<SkillInfo> TurnPlayerSkillInfos = new List<SkillInfo>();
  28. List<SkillInfo> NonTurnPlayerSkillInfos = new List<SkillInfo>();
  29. foreach (SkillInfo skillInfo in skillInfos)
  30. {
  31. if (skillInfo != null)
  32. {
  33. ICardEffect cardEffect = skillInfo.CardEffect;
  34. if (cardEffect.EffectSourceCard != null)
  35. {
  36. if (cardEffect.EffectSourceCard.Owner == GManager.instance.turnStateMachine.gameContext.TurnPlayer)
  37. {
  38. TurnPlayerSkillInfos.Add(skillInfo);
  39. }
  40. else if (cardEffect.EffectSourceCard.Owner == GManager.instance.turnStateMachine.gameContext.NonTurnPlayer)
  41. {
  42. NonTurnPlayerSkillInfos.Add(skillInfo);
  43. }
  44. }
  45. }
  46. }
  47. GManager.instance.turnStateMachine.isSync = true;
  48. yield return ContinuousController.instance.StartCoroutine(ActivateMultipleSkills_OnePlayer(TurnPlayerSkillInfos, GManager.instance.turnStateMachine.gameContext.TurnPlayer, CheckNewTriggredSkill_mainStack, skipCondition));
  49. yield return ContinuousController.instance.StartCoroutine(ActivateMultipleSkills_OnePlayer(NonTurnPlayerSkillInfos, GManager.instance.turnStateMachine.gameContext.NonTurnPlayer, CheckNewTriggredSkill_mainStack, skipCondition));
  50. GManager.instance.turnStateMachine.isSync = false;
  51. SkillInfos_used = new List<SkillInfo>();
  52. IsUsing = false;
  53. }
  54. bool _endSelect = false;
  55. int _skillIndex;
  56. bool IsCutinEffect(bool CheckNewTriggredSkill_mainStack) => !CheckNewTriggredSkill_mainStack && _autoProcessing != GManager.instance.autoProcessing;
  57. IEnumerator ActivateMultipleSkills_OnePlayer(List<SkillInfo> skillInfos, Player player, bool CheckNewTriggredSkill_mainStack, Func<List<SkillInfo>, SkillInfo, bool> skipCondition)
  58. {
  59. StackedSkillInfos = new List<SkillInfo>();
  60. foreach (SkillInfo skillInfo in skillInfos)
  61. {
  62. StackedSkillInfos.Add(skillInfo);
  63. }
  64. while (true)
  65. {
  66. List<SkillInfo> skillInfos_active = new List<SkillInfo>();
  67. foreach (SkillInfo skillInfo in StackedSkillInfos)
  68. {
  69. #region set the flag whether it is Digimon's effect or Tamer's effect
  70. if (skillInfo.CardEffect != null)
  71. {
  72. if (skillInfo.CardEffect.EffectSourceCard != null)
  73. {
  74. CardSource card = skillInfo.CardEffect.EffectSourceCard;
  75. if (card != null)
  76. {
  77. if (!skillInfo.CardEffect.IsDigimonEffect)
  78. {
  79. if (skillInfo.CardEffect.IsInheritedEffect || skillInfo.CardEffect.IsLinkedEffect)
  80. {
  81. skillInfo.CardEffect.SetIsDigimonEffect(true);
  82. }
  83. else
  84. {
  85. if (card.PermanentOfThisCard() != null)
  86. {
  87. skillInfo.CardEffect.SetIsDigimonEffect(card.PermanentOfThisCard().IsDigimon);
  88. skillInfo.CardEffect.SetIsTamerEffect(card.PermanentOfThisCard().IsTamer);
  89. }
  90. else
  91. {
  92. skillInfo.CardEffect.SetIsTamerEffect(card.IsTamer);
  93. }
  94. if (card == GManager.instance.attackProcess.SecurityDigimon)
  95. {
  96. skillInfo.CardEffect.SetIsDigimonEffect(true);
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }
  103. #endregion
  104. #region Check if the effect can be activated
  105. if (!skillInfo.CardEffect.CanActivate(skillInfo.Hashtable))
  106. {
  107. Debug.Log($"{skillInfo.CardEffect.EffectName} Can't Activate");
  108. continue;
  109. }
  110. if (skipCondition != null)
  111. {
  112. if (skipCondition(_autoProcessing.skillInfos_used, skillInfo))
  113. {
  114. Debug.Log($"{skillInfo.CardEffect.EffectName} has been skipped");
  115. continue;
  116. }
  117. }
  118. if (skillInfo.CardEffect.ChainActivations > 0)
  119. {
  120. if (GManager.instance.autoProcessing.IsCutInEffectUsedMaxCount(skillInfo.CardEffect))
  121. {
  122. Debug.Log($"{skillInfo.CardEffect.EffectName} has exceeded its use");
  123. continue;
  124. }
  125. }
  126. if (IsCutinEffect(CheckNewTriggredSkill_mainStack))
  127. {
  128. Debug.Log($"{skillInfo.CardEffect.EffectName} is Cut In effect");
  129. if (GManager.instance.autoProcessing.IsCutInEffectHasUsed(skillInfo.CardEffect))
  130. {
  131. Debug.Log($"{skillInfo.CardEffect.EffectName} has been used");
  132. continue;
  133. }
  134. }
  135. skillInfos_active.Add(skillInfo);
  136. #endregion
  137. }
  138. skillInfos_active = skillInfos_active.Filter(skillInfo => skillInfo != null && skillInfo.CardEffect != null
  139. && skillInfo.CardEffect.CanActivate(skillInfo.Hashtable) && skillInfo.CardEffect.EffectSourceCard != null);
  140. if (skillInfos_active.Count > 0)
  141. {
  142. bool oldIsSecurityGlassBlue = player.securityObject.securityBreakGlass.IsBlueGlass && IsOnlyHandEffectStacked && IsOnlyOptionalEffectStacked && IsEachStackedEffectHasDistinctSourceCard;
  143. #region If the effect list is one, process normally.
  144. if (skillInfos_active.Count == 1)
  145. {
  146. _skillIndex = 0;
  147. yield return ContinuousController.instance.StartCoroutine(Activate(true));
  148. }
  149. #endregion
  150. #region If there are multiple effect lists, select which one to process first
  151. else
  152. {
  153. List<CardSource> RootCardSources = skillInfos_active.Map(skillInfo => skillInfo.CardEffect.EffectSourceCard);
  154. yield return GManager.instance.photonWaitController.StartWait("StartSelectMultipleSkill");
  155. // Blast Digivolution
  156. if (IsOnlyHandEffectStacked && IsOnlyOptionalEffectStacked && IsEachStackedEffectHasDistinctSourceCard)
  157. {
  158. if (player.isYou)
  159. {
  160. int skillIndex = 0;
  161. //if (!ContinuousController.instance.SkipSelectOrderOrCount)
  162. {
  163. if (oldIsSecurityGlassBlue)
  164. {
  165. player.securityObject.securityBreakGlass.gameObject.SetActive(false);
  166. }
  167. SelectHandEffect selectHandEffect = GManager.instance.GetComponent<SelectHandEffect>();
  168. selectHandEffect.SetUp(
  169. selectPlayer: player,
  170. canTargetCondition: (cardSource) => RootCardSources.Contains(cardSource),
  171. canTargetCondition_ByPreSelecetedList: null,
  172. canEndSelectCondition: null,
  173. maxCount: 1,
  174. canNoSelect: true,
  175. canEndNotMax: false,
  176. isShowOpponent: true,
  177. selectCardCoroutine: SelectCardCoroutine,
  178. afterSelectCardCoroutine: AfterSelectCardCoroutine,
  179. mode: SelectHandEffect.Mode.Custom,
  180. cardEffect: null);
  181. selectHandEffect.SetUpCustomMessage("Multiple effects are triggered.\nChoose which effect to process first.", "");
  182. selectHandEffect.SetNotShowCard();
  183. selectHandEffect.SetNotShowOpponentMessage();
  184. selectHandEffect.SetIsLocal();
  185. yield return StartCoroutine(selectHandEffect.Activate());
  186. IEnumerator SelectCardCoroutine(CardSource cardSource)
  187. {
  188. for (int i = 0; i < skillInfos_active.Count; i++)
  189. {
  190. SkillInfo skillInfo = skillInfos_active[i];
  191. if (skillInfo.CardEffect.EffectSourceCard == cardSource)
  192. {
  193. skillIndex = i;
  194. break;
  195. }
  196. }
  197. yield return null;
  198. }
  199. IEnumerator AfterSelectCardCoroutine(List<CardSource> cardSources)
  200. {
  201. if (cardSources.Count == 0)
  202. {
  203. skillIndex = -1;
  204. }
  205. yield return null;
  206. }
  207. }
  208. photonView.RPC("SetTargetSkill", RpcTarget.All, skillIndex);
  209. }
  210. else
  211. {
  212. #region AI
  213. if (GManager.instance.IsAI)
  214. {
  215. SetTargetSkill(0);
  216. }
  217. #endregion
  218. }
  219. }
  220. else
  221. {
  222. if (player.isYou)
  223. {
  224. int skillIndex = -1;
  225. if (!ContinuousController.instance.autoEffectOrder)
  226. {
  227. yield return StartCoroutine(GManager.instance.selectCardPanel.OpenSelectCardPanel(
  228. Message: "Multiple effects are triggered.\nChoose which effect to process.",
  229. NotSelectButtonMessage: "Don't activate these effects.",
  230. EndSelectButtonMessage: "End Selection",
  231. _OnClickNotSelectButtonAction: null,
  232. _OnClickEndSelectButtonAction: null,
  233. RootCardSources: RootCardSources,
  234. _CanTargetCondition: (cardSource) => true,
  235. _CanTargetCondition_ByPreSelecetedList: null,
  236. _CanEndSelectCondition: null,
  237. _MaxCount: 1,
  238. _CanEndNotMax: false,
  239. _CanNoSelect: () => skillInfos_active.All(skillInfo => skillInfo.CardEffect.IsOptionalCondition(skillInfo.Hashtable)),
  240. CanLookReverseCard: true,
  241. skillInfos: skillInfos_active,
  242. root: SelectCardEffect.Root.None));
  243. if (GManager.instance.selectCardPanel.SelectedIndex.Count > 0)
  244. {
  245. skillIndex = GManager.instance.selectCardPanel.SelectedIndex[0];
  246. }
  247. else
  248. {
  249. foreach (FieldPermanentCard fieldPermanentCard in player.FieldPermanentObjects)
  250. {
  251. fieldPermanentCard.OffPermanentIndexText();
  252. }
  253. }
  254. }
  255. else
  256. {
  257. skillIndex = AutomaticOrder.GetSkillIndexAutomaticOrder(skillInfos_active);
  258. }
  259. photonView.RPC("SetTargetSkill", RpcTarget.All, skillIndex);
  260. }
  261. else
  262. {
  263. if (!IsOnlyHandEffectStacked)
  264. {
  265. GManager.instance.commandText.OpenCommandText("The opponent is choosing which effect to process first.");
  266. }
  267. #region AI
  268. if (GManager.instance.IsAI)
  269. {
  270. SetTargetSkill(0);
  271. }
  272. #endregion
  273. }
  274. }
  275. yield return new WaitWhile(() => !_endSelect);
  276. _endSelect = false;
  277. GManager.instance.commandText.CloseCommandText();
  278. yield return new WaitWhile(() => GManager.instance.commandText.gameObject.activeSelf);
  279. yield return ContinuousController.instance.StartCoroutine(Activate(!(IsOnlyHandEffectStacked && IsOnlyOptionalEffectStacked && IsEachStackedEffectHasDistinctSourceCard)));
  280. yield return GManager.instance.photonWaitController.StartWait("EndSelectMultipleSkill");
  281. }
  282. #endregion
  283. #region Executing the effect
  284. IEnumerator Activate(bool isCheckOptional)
  285. {
  286. if (_skillIndex < 0 || skillInfos_active.Count < _skillIndex)
  287. {
  288. StackedSkillInfos = new List<SkillInfo>();
  289. yield break;
  290. }
  291. if (oldIsSecurityGlassBlue)
  292. {
  293. player.securityObject.securityBreakGlass.gameObject.SetActive(false);
  294. }
  295. ICardEffect cardEffect = skillInfos_active[_skillIndex].CardEffect;
  296. Hashtable hashtable = skillInfos_active[_skillIndex].Hashtable;
  297. StackedSkillInfos.Remove(skillInfos_active[_skillIndex]);
  298. skillInfos_active[_skillIndex].CardEffect.SetOnProcessCallbuck(() =>
  299. {
  300. SkillInfos_used.Add(skillInfos_active[_skillIndex]);
  301. cardEffect.EffectSourceCard.cEntity_EffectController.RegisterUseEfffectThisTurn(cardEffect);
  302. });
  303. if (cardEffect is ActivateICardEffect)
  304. {
  305. if (cardEffect.CanActivate(hashtable))
  306. {
  307. // For interrupt processing
  308. if (IsCutinEffect(CheckNewTriggredSkill_mainStack))
  309. {
  310. // GManager.instance.autoProcessing.AddCutinEffect(cardEffect);
  311. yield return ContinuousController.instance.StartCoroutine(((ActivateICardEffect)cardEffect)
  312. .Activate_Optional_Effect_Execute(
  313. hashtable,
  314. isCheckOptional,
  315. useEffectCallback: GManager.instance.autoProcessing.AddCutinEffect));
  316. }
  317. else
  318. {
  319. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.ActivateEffectProcess(
  320. cardEffect,
  321. hashtable,
  322. isCheckOptional));
  323. }
  324. }
  325. }
  326. if (oldIsSecurityGlassBlue)
  327. {
  328. player.securityObject.securityBreakGlass.ShowBlueMatarial();
  329. }
  330. }
  331. #endregion
  332. //Rule processing
  333. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.RuleProcess());
  334. if (GManager.instance.turnStateMachine.endGame)
  335. {
  336. yield break;
  337. }
  338. #region If there are any newly triggered effects, resolve those first.
  339. if (!CheckNewTriggredSkill_mainStack)
  340. {
  341. yield return ContinuousController.instance.StartCoroutine(_autoProcessing.TriggeredSkillProcess(CheckNewTriggredSkill_mainStack, skipCondition));
  342. }
  343. else
  344. {
  345. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.TriggeredSkillProcess(CheckNewTriggredSkill_mainStack, null));
  346. }
  347. #endregion
  348. }
  349. else
  350. {
  351. break;
  352. }
  353. }
  354. }
  355. [PunRPC]
  356. public void SetTargetSkill(int skillIndex)
  357. {
  358. _skillIndex = skillIndex;
  359. _endSelect = true;
  360. }
  361. }