MultipleSkills.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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.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. #endregion
  101. #region Check if the effect can be activated
  102. if (!skillInfo.CardEffect.CanActivate(skillInfo.Hashtable))
  103. {
  104. Debug.Log($"{skillInfo.CardEffect.EffectName} Can't Activate");
  105. continue;
  106. }
  107. if (skipCondition != null)
  108. {
  109. if (skipCondition(_autoProcessing.skillInfos_used, skillInfo))
  110. {
  111. Debug.Log($"{skillInfo.CardEffect.EffectName} has been skipped");
  112. continue;
  113. }
  114. }
  115. if (skillInfo.CardEffect.ChainActivations > 0)
  116. {
  117. if (GManager.instance.autoProcessing.IsCutInEffectUsedMaxCount(skillInfo.CardEffect))
  118. {
  119. Debug.Log($"{skillInfo.CardEffect.EffectName} has exceeded its use");
  120. continue;
  121. }
  122. }
  123. if (IsCutinEffect(CheckNewTriggredSkill_mainStack))
  124. {
  125. Debug.Log($"{skillInfo.CardEffect.EffectName} is Cut In effect");
  126. if (GManager.instance.autoProcessing.IsCutInEffectHasUsed(skillInfo.CardEffect))
  127. {
  128. Debug.Log($"{skillInfo.CardEffect.EffectName} has been used");
  129. continue;
  130. }
  131. }
  132. skillInfos_active.Add(skillInfo);
  133. #endregion
  134. }
  135. skillInfos_active = skillInfos_active.Filter(skillInfo => skillInfo != null && skillInfo.CardEffect != null
  136. && skillInfo.CardEffect.CanActivate(skillInfo.Hashtable) && skillInfo.CardEffect.EffectSourceCard != null);
  137. if (skillInfos_active.Count > 0)
  138. {
  139. bool oldIsSecurityGlassBlue = player.securityObject.securityBreakGlass.IsBlueGlass && IsOnlyHandEffectStacked && IsOnlyOptionalEffectStacked && IsEachStackedEffectHasDistinctSourceCard;
  140. #region If the effect list is one, process normally.
  141. if (skillInfos_active.Count == 1)
  142. {
  143. _skillIndex = 0;
  144. yield return ContinuousController.instance.StartCoroutine(Activate(true));
  145. }
  146. #endregion
  147. #region If there are multiple effect lists, select which one to process first
  148. else
  149. {
  150. List<CardSource> RootCardSources = skillInfos_active.Map(skillInfo => skillInfo.CardEffect.EffectSourceCard);
  151. yield return GManager.instance.photonWaitController.StartWait("StartSelectMultipleSkill");
  152. // Blast Digivolution
  153. if (IsOnlyHandEffectStacked && IsOnlyOptionalEffectStacked && IsEachStackedEffectHasDistinctSourceCard)
  154. {
  155. if (player.isYou)
  156. {
  157. int skillIndex = 0;
  158. //if (!ContinuousController.instance.SkipSelectOrderOrCount)
  159. {
  160. if (oldIsSecurityGlassBlue)
  161. {
  162. player.securityObject.securityBreakGlass.gameObject.SetActive(false);
  163. }
  164. SelectHandEffect selectHandEffect = GManager.instance.GetComponent<SelectHandEffect>();
  165. selectHandEffect.SetUp(
  166. selectPlayer: player,
  167. canTargetCondition: (cardSource) => RootCardSources.Contains(cardSource),
  168. canTargetCondition_ByPreSelecetedList: null,
  169. canEndSelectCondition: null,
  170. maxCount: 1,
  171. canNoSelect: true,
  172. canEndNotMax: false,
  173. isShowOpponent: true,
  174. selectCardCoroutine: SelectCardCoroutine,
  175. afterSelectCardCoroutine: AfterSelectCardCoroutine,
  176. mode: SelectHandEffect.Mode.Custom,
  177. cardEffect: null);
  178. selectHandEffect.SetUpCustomMessage("Multiple effects are triggered.\nChoose which effect to process first.", "");
  179. selectHandEffect.SetNotShowCard();
  180. selectHandEffect.SetNotShowOpponentMessage();
  181. selectHandEffect.SetIsLocal();
  182. yield return StartCoroutine(selectHandEffect.Activate());
  183. IEnumerator SelectCardCoroutine(CardSource cardSource)
  184. {
  185. for (int i = 0; i < skillInfos_active.Count; i++)
  186. {
  187. SkillInfo skillInfo = skillInfos_active[i];
  188. if (skillInfo.CardEffect.EffectSourceCard == cardSource)
  189. {
  190. skillIndex = i;
  191. break;
  192. }
  193. }
  194. yield return null;
  195. }
  196. IEnumerator AfterSelectCardCoroutine(List<CardSource> cardSources)
  197. {
  198. if (cardSources.Count == 0)
  199. {
  200. skillIndex = -1;
  201. }
  202. yield return null;
  203. }
  204. }
  205. photonView.RPC("SetTargetSkill", RpcTarget.All, skillIndex);
  206. }
  207. else
  208. {
  209. #region AI
  210. if (GManager.instance.IsAI)
  211. {
  212. SetTargetSkill(0);
  213. }
  214. #endregion
  215. }
  216. }
  217. else
  218. {
  219. if (player.isYou)
  220. {
  221. int skillIndex = 0;
  222. if (!ContinuousController.instance.autoEffectOrder)
  223. {
  224. yield return StartCoroutine(GManager.instance.selectCardPanel.OpenSelectCardPanel(
  225. Message: "Multiple effects are triggered.\nChoose which effect to process.",
  226. RootCardSources: RootCardSources,
  227. _CanTargetCondition: (cardSource) => true,
  228. _CanTargetCondition_ByPreSelecetedList: null,
  229. _CanEndSelectCondition: null,
  230. _MaxCount: 1,
  231. _CanEndNotMax: false,
  232. _CanNoSelect: () => false,
  233. CanLookReverseCard: true,
  234. skillInfos: skillInfos_active,
  235. root: SelectCardEffect.Root.None));
  236. if (GManager.instance.selectCardPanel.SelectedIndex.Count > 0)
  237. {
  238. skillIndex = GManager.instance.selectCardPanel.SelectedIndex[0];
  239. }
  240. }
  241. else
  242. {
  243. skillIndex = AutomaticOrder.GetSkillIndexAutomaticOrder(skillInfos_active);
  244. }
  245. photonView.RPC("SetTargetSkill", RpcTarget.All, skillIndex);
  246. }
  247. else
  248. {
  249. if (!IsOnlyHandEffectStacked)
  250. {
  251. GManager.instance.commandText.OpenCommandText("The opponent is choosing which effect to process first.");
  252. }
  253. #region AI
  254. if (GManager.instance.IsAI)
  255. {
  256. SetTargetSkill(0);
  257. }
  258. #endregion
  259. }
  260. }
  261. yield return new WaitWhile(() => !_endSelect);
  262. _endSelect = false;
  263. GManager.instance.commandText.CloseCommandText();
  264. yield return new WaitWhile(() => GManager.instance.commandText.gameObject.activeSelf);
  265. yield return ContinuousController.instance.StartCoroutine(Activate(!(IsOnlyHandEffectStacked && IsOnlyOptionalEffectStacked && IsEachStackedEffectHasDistinctSourceCard)));
  266. yield return GManager.instance.photonWaitController.StartWait("EndSelectMultipleSkill");
  267. }
  268. #endregion
  269. #region Executing the effect
  270. IEnumerator Activate(bool isCheckOptional)
  271. {
  272. if (_skillIndex < 0 || skillInfos_active.Count < _skillIndex)
  273. {
  274. StackedSkillInfos = new List<SkillInfo>();
  275. yield break;
  276. }
  277. if (oldIsSecurityGlassBlue)
  278. {
  279. player.securityObject.securityBreakGlass.gameObject.SetActive(false);
  280. }
  281. ICardEffect cardEffect = skillInfos_active[_skillIndex].CardEffect;
  282. Hashtable hashtable = skillInfos_active[_skillIndex].Hashtable;
  283. StackedSkillInfos.Remove(skillInfos_active[_skillIndex]);
  284. skillInfos_active[_skillIndex].CardEffect.SetOnProcessCallbuck(() =>
  285. {
  286. SkillInfos_used.Add(skillInfos_active[_skillIndex]);
  287. cardEffect.EffectSourceCard.cEntity_EffectController.RegisterUseEfffectThisTurn(cardEffect);
  288. });
  289. if (cardEffect is ActivateICardEffect)
  290. {
  291. if (cardEffect.CanActivate(hashtable))
  292. {
  293. // For interrupt processing
  294. if (IsCutinEffect(CheckNewTriggredSkill_mainStack))
  295. {
  296. // GManager.instance.autoProcessing.AddCutinEffect(cardEffect);
  297. yield return ContinuousController.instance.StartCoroutine(((ActivateICardEffect)cardEffect)
  298. .Activate_Optional_Effect_Execute(
  299. hashtable,
  300. isCheckOptional,
  301. useEffectCallback: GManager.instance.autoProcessing.AddCutinEffect));
  302. }
  303. else
  304. {
  305. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.ActivateEffectProcess(
  306. cardEffect,
  307. hashtable,
  308. isCheckOptional));
  309. }
  310. }
  311. }
  312. if (oldIsSecurityGlassBlue)
  313. {
  314. player.securityObject.securityBreakGlass.ShowBlueMatarial();
  315. }
  316. }
  317. #endregion
  318. //Rule processing
  319. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.RuleProcess());
  320. if (GManager.instance.turnStateMachine.endGame)
  321. {
  322. yield break;
  323. }
  324. #region If there are any newly triggered effects, resolve those first.
  325. if (!CheckNewTriggredSkill_mainStack)
  326. {
  327. yield return ContinuousController.instance.StartCoroutine(_autoProcessing.TriggeredSkillProcess(CheckNewTriggredSkill_mainStack, skipCondition));
  328. }
  329. else
  330. {
  331. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.TriggeredSkillProcess(CheckNewTriggredSkill_mainStack, null));
  332. }
  333. #endregion
  334. }
  335. else
  336. {
  337. break;
  338. }
  339. }
  340. }
  341. [PunRPC]
  342. public void SetTargetSkill(int skillIndex)
  343. {
  344. _skillIndex = skillIndex;
  345. _endSelect = true;
  346. }
  347. }