MultipleSkills.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. // Blast Digivolution
  151. if (IsOnlyHandEffectStacked && IsOnlyOptionalEffectStacked && IsEachStackedEffectHasDistinctSourceCard)
  152. {
  153. if (player.isYou)
  154. {
  155. int skillIndex = 0;
  156. //if (!ContinuousController.instance.SkipSelectOrderOrCount)
  157. {
  158. if (oldIsSecurityGlassBlue)
  159. {
  160. player.securityObject.securityBreakGlass.gameObject.SetActive(false);
  161. }
  162. SelectHandEffect selectHandEffect = GManager.instance.GetComponent<SelectHandEffect>();
  163. selectHandEffect.SetUp(
  164. selectPlayer: player,
  165. canTargetCondition: (cardSource) => RootCardSources.Contains(cardSource),
  166. canTargetCondition_ByPreSelecetedList: null,
  167. canEndSelectCondition: null,
  168. maxCount: 1,
  169. canNoSelect: true,
  170. canEndNotMax: false,
  171. isShowOpponent: true,
  172. selectCardCoroutine: SelectCardCoroutine,
  173. afterSelectCardCoroutine: AfterSelectCardCoroutine,
  174. mode: SelectHandEffect.Mode.Custom,
  175. cardEffect: null);
  176. selectHandEffect.SetUpCustomMessage("Multiple effects are triggered.\nChoose which effect to process first.", "");
  177. selectHandEffect.SetNotShowCard();
  178. selectHandEffect.SetNotShowOpponentMessage();
  179. selectHandEffect.SetIsLocal();
  180. yield return StartCoroutine(selectHandEffect.Activate());
  181. IEnumerator SelectCardCoroutine(CardSource cardSource)
  182. {
  183. for (int i = 0; i < skillInfos_active.Count; i++)
  184. {
  185. SkillInfo skillInfo = skillInfos_active[i];
  186. if (skillInfo.CardEffect.EffectSourceCard == cardSource)
  187. {
  188. skillIndex = i;
  189. break;
  190. }
  191. }
  192. yield return null;
  193. }
  194. IEnumerator AfterSelectCardCoroutine(List<CardSource> cardSources)
  195. {
  196. if (cardSources.Count == 0)
  197. {
  198. skillIndex = -1;
  199. }
  200. yield return null;
  201. }
  202. }
  203. photonView.RPC("SetTargetSkill", RpcTarget.All, player.PlayerID, skillIndex);
  204. }
  205. else
  206. {
  207. #region AI
  208. if (GManager.instance.IsAI)
  209. {
  210. SetTargetSkill(player.PlayerID, 0);
  211. }
  212. #endregion
  213. }
  214. }
  215. else
  216. {
  217. if (player.isYou)
  218. {
  219. int skillIndex = -1;
  220. if (!ContinuousController.instance.autoEffectOrder)
  221. {
  222. yield return StartCoroutine(GManager.instance.selectCardPanel.OpenSelectCardPanel(
  223. Message: "Multiple effects are triggered.\nChoose which effect to process.",
  224. NotSelectButtonMessage: "Don't activate these effects.",
  225. EndSelectButtonMessage: "End Selection",
  226. _OnClickNotSelectButtonAction: null,
  227. _OnClickEndSelectButtonAction: null,
  228. RootCardSources: RootCardSources,
  229. _CanTargetCondition: (cardSource) => true,
  230. _CanTargetCondition_ByPreSelecetedList: null,
  231. _CanEndSelectCondition: null,
  232. _MaxCount: 1,
  233. _CanEndNotMax: false,
  234. _CanNoSelect: () => skillInfos_active.All(skillInfo => skillInfo.CardEffect.IsSkippable(skillInfo.Hashtable)),
  235. CanLookReverseCard: true,
  236. skillInfos: skillInfos_active,
  237. root: SelectCardEffect.Root.None));
  238. if (GManager.instance.selectCardPanel.SelectedIndex.Count > 0)
  239. {
  240. skillIndex = GManager.instance.selectCardPanel.SelectedIndex[0];
  241. }
  242. else
  243. {
  244. foreach (FieldPermanentCard fieldPermanentCard in player.FieldPermanentObjects)
  245. {
  246. fieldPermanentCard.OffPermanentIndexText();
  247. }
  248. }
  249. }
  250. else
  251. {
  252. skillIndex = AutomaticOrder.GetSkillIndexAutomaticOrder(skillInfos_active);
  253. }
  254. photonView.RPC("SetTargetSkill", RpcTarget.All, player.PlayerID, skillIndex);
  255. }
  256. else
  257. {
  258. if (!IsOnlyHandEffectStacked)
  259. {
  260. GManager.instance.commandText.OpenCommandText("The opponent is choosing which effect to process first.");
  261. }
  262. #region AI
  263. if (GManager.instance.IsAI)
  264. {
  265. SetTargetSkill(player.PlayerID, 0);
  266. }
  267. #endregion
  268. }
  269. }
  270. yield return new WaitUntil(() => player.HasPlayerSelection());
  271. ValueSelection valueSelection = player.DequeuePlayerSelection<ValueSelection>();
  272. _skillIndex = valueSelection != null ? valueSelection.ValueAsInt() : 0;
  273. GManager.instance.commandText.CloseCommandText();
  274. yield return new WaitWhile(() => GManager.instance.commandText.gameObject.activeSelf);
  275. yield return ContinuousController.instance.StartCoroutine(Activate(!(IsOnlyHandEffectStacked && IsOnlyOptionalEffectStacked && IsEachStackedEffectHasDistinctSourceCard)));
  276. }
  277. #endregion
  278. #region Executing the effect
  279. IEnumerator Activate(bool isCheckOptional)
  280. {
  281. if (_skillIndex < 0 || skillInfos_active.Count < _skillIndex)
  282. {
  283. StackedSkillInfos = new List<SkillInfo>();
  284. yield break;
  285. }
  286. if (oldIsSecurityGlassBlue)
  287. {
  288. player.securityObject.securityBreakGlass.gameObject.SetActive(false);
  289. }
  290. ICardEffect cardEffect = skillInfos_active[_skillIndex].CardEffect;
  291. Hashtable hashtable = skillInfos_active[_skillIndex].Hashtable;
  292. StackedSkillInfos.Remove(skillInfos_active[_skillIndex]);
  293. skillInfos_active[_skillIndex].CardEffect.SetOnProcessCallbuck(() =>
  294. {
  295. SkillInfos_used.Add(skillInfos_active[_skillIndex]);
  296. cardEffect.EffectSourceCard.cEntity_EffectController.RegisterUseEfffectThisTurn(cardEffect);
  297. });
  298. if (cardEffect is ActivateICardEffect)
  299. {
  300. if (cardEffect.CanActivate(hashtable))
  301. {
  302. // For interrupt processing
  303. if (IsCutinEffect(CheckNewTriggredSkill_mainStack))
  304. {
  305. // GManager.instance.autoProcessing.AddCutinEffect(cardEffect);
  306. yield return ContinuousController.instance.StartCoroutine(((ActivateICardEffect)cardEffect)
  307. .Activate_Optional_Effect_Execute(
  308. hashtable,
  309. isCheckOptional,
  310. useEffectCallback: GManager.instance.autoProcessing.AddCutinEffect));
  311. }
  312. else
  313. {
  314. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.ActivateEffectProcess(
  315. cardEffect,
  316. hashtable,
  317. isCheckOptional));
  318. }
  319. }
  320. }
  321. if (oldIsSecurityGlassBlue)
  322. {
  323. player.securityObject.securityBreakGlass.ShowBlueMatarial();
  324. }
  325. }
  326. #endregion
  327. //Rule processing
  328. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.RuleProcess());
  329. if (GManager.instance.turnStateMachine.endGame)
  330. {
  331. yield break;
  332. }
  333. #region If there are any newly triggered effects, resolve those first.
  334. if (!CheckNewTriggredSkill_mainStack)
  335. {
  336. yield return ContinuousController.instance.StartCoroutine(_autoProcessing.TriggeredSkillProcess(CheckNewTriggredSkill_mainStack, skipCondition));
  337. }
  338. else
  339. {
  340. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.TriggeredSkillProcess(CheckNewTriggredSkill_mainStack, null));
  341. }
  342. #endregion
  343. }
  344. else
  345. {
  346. break;
  347. }
  348. }
  349. }
  350. [PunRPC]
  351. public void SetTargetSkill(int playerID, int skillIndex)
  352. {
  353. Player selectionPlayer = GManager.instance.GetPlayerFromID(playerID);
  354. if (selectionPlayer == null)
  355. {
  356. return;
  357. }
  358. selectionPlayer.QueuePlayerSelection(new ValueSelection(skillIndex));
  359. }
  360. }