MultipleSkills.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. NotSelectButtonMessage: "Don't activate these effects.",
  230. EndSelectButtonMessage: "End Selection",
  231. _OnClickNotSelectButtonAction: null,
  232. _OnClickEndSelectButtonAction: null,
  233. RootCardSources: RootCardSources,
  234. _CanTargetCondition: (cardSource) => true,
  235. _CanTargetCondition_ByPreSelecetedList: null,
  236. _CanEndSelectCondition: null,
  237. _MaxCount: 1,
  238. _CanEndNotMax: false,
  239. _CanNoSelect: () => skillInfos_active.All(skillInfo => skillInfo.CardEffect.IsOptional),
  240. CanLookReverseCard: true,
  241. skillInfos: skillInfos_active,
  242. root: SelectCardEffect.Root.None));
  243. if (GManager.instance.selectCardPanel.SelectedIndex.Count > 0)
  244. {
  245. skillIndex = GManager.instance.selectCardPanel.SelectedIndex[0];
  246. }
  247. else
  248. {
  249. foreach(SkillInfo skillInfo in skillInfos_active)
  250. {
  251. StackedSkillInfos.Remove(skillInfo);
  252. }
  253. continue;
  254. }
  255. }
  256. else
  257. {
  258. skillIndex = AutomaticOrder.GetSkillIndexAutomaticOrder(skillInfos_active);
  259. }
  260. photonView.RPC("SetTargetSkill", RpcTarget.All, skillIndex);
  261. }
  262. else
  263. {
  264. if (!IsOnlyHandEffectStacked)
  265. {
  266. GManager.instance.commandText.OpenCommandText("The opponent is choosing which effect to process first.");
  267. }
  268. #region AI
  269. if (GManager.instance.IsAI)
  270. {
  271. SetTargetSkill(0);
  272. }
  273. #endregion
  274. }
  275. }
  276. yield return new WaitWhile(() => !_endSelect);
  277. _endSelect = false;
  278. GManager.instance.commandText.CloseCommandText();
  279. yield return new WaitWhile(() => GManager.instance.commandText.gameObject.activeSelf);
  280. yield return ContinuousController.instance.StartCoroutine(Activate(!(IsOnlyHandEffectStacked && IsOnlyOptionalEffectStacked && IsEachStackedEffectHasDistinctSourceCard)));
  281. yield return GManager.instance.photonWaitController.StartWait("EndSelectMultipleSkill");
  282. }
  283. #endregion
  284. #region Executing the effect
  285. IEnumerator Activate(bool isCheckOptional)
  286. {
  287. if (_skillIndex < 0 || skillInfos_active.Count < _skillIndex)
  288. {
  289. StackedSkillInfos = new List<SkillInfo>();
  290. yield break;
  291. }
  292. if (oldIsSecurityGlassBlue)
  293. {
  294. player.securityObject.securityBreakGlass.gameObject.SetActive(false);
  295. }
  296. ICardEffect cardEffect = skillInfos_active[_skillIndex].CardEffect;
  297. Hashtable hashtable = skillInfos_active[_skillIndex].Hashtable;
  298. StackedSkillInfos.Remove(skillInfos_active[_skillIndex]);
  299. skillInfos_active[_skillIndex].CardEffect.SetOnProcessCallbuck(() =>
  300. {
  301. SkillInfos_used.Add(skillInfos_active[_skillIndex]);
  302. cardEffect.EffectSourceCard.cEntity_EffectController.RegisterUseEfffectThisTurn(cardEffect);
  303. });
  304. if (cardEffect is ActivateICardEffect)
  305. {
  306. if (cardEffect.CanActivate(hashtable))
  307. {
  308. // For interrupt processing
  309. if (IsCutinEffect(CheckNewTriggredSkill_mainStack))
  310. {
  311. // GManager.instance.autoProcessing.AddCutinEffect(cardEffect);
  312. yield return ContinuousController.instance.StartCoroutine(((ActivateICardEffect)cardEffect)
  313. .Activate_Optional_Effect_Execute(
  314. hashtable,
  315. isCheckOptional,
  316. useEffectCallback: GManager.instance.autoProcessing.AddCutinEffect));
  317. }
  318. else
  319. {
  320. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.ActivateEffectProcess(
  321. cardEffect,
  322. hashtable,
  323. isCheckOptional));
  324. }
  325. }
  326. }
  327. if (oldIsSecurityGlassBlue)
  328. {
  329. player.securityObject.securityBreakGlass.ShowBlueMatarial();
  330. }
  331. }
  332. #endregion
  333. //Rule processing
  334. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.RuleProcess());
  335. if (GManager.instance.turnStateMachine.endGame)
  336. {
  337. yield break;
  338. }
  339. #region If there are any newly triggered effects, resolve those first.
  340. if (!CheckNewTriggredSkill_mainStack)
  341. {
  342. yield return ContinuousController.instance.StartCoroutine(_autoProcessing.TriggeredSkillProcess(CheckNewTriggredSkill_mainStack, skipCondition));
  343. }
  344. else
  345. {
  346. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.TriggeredSkillProcess(CheckNewTriggredSkill_mainStack, null));
  347. }
  348. #endregion
  349. }
  350. else
  351. {
  352. break;
  353. }
  354. }
  355. }
  356. [PunRPC]
  357. public void SetTargetSkill(int skillIndex)
  358. {
  359. _skillIndex = skillIndex;
  360. _endSelect = true;
  361. }
  362. }