MultipleSkills.cs 19 KB

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