MultipleSkills.cs 20 KB

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