MultipleSkills.cs 18 KB

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