MultipleSkills.cs 20 KB

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