From 2b3cb531e75880aa8c0c36ad4386643dc3cee5cd Mon Sep 17 00:00:00 2001 From: Slinta Date: Mon, 6 Nov 2017 21:48:59 +0100 Subject: [PATCH] Initialize ai cleanup Signed-off-by: Slinta --- .../Assets/Scripts/AI/Enemy_AI.cs | 8 ++- .../{Core Classes => AI}/Initialize_AI.cs | 57 ++++++++----------- .../Initialize_AI.cs.meta | 0 .../Scripts/Core Classes/PlayManager.cs | 11 ++-- .../Scripts/Menu & Wrappers/LoadFromFile.cs | 21 ++----- 5 files changed, 41 insertions(+), 56 deletions(-) rename Dots-RTS-Development/Assets/Scripts/{Core Classes => AI}/Initialize_AI.cs (69%) rename Dots-RTS-Development/Assets/Scripts/{Core Classes => AI}/Initialize_AI.cs.meta (100%) diff --git a/Dots-RTS-Development/Assets/Scripts/AI/Enemy_AI.cs b/Dots-RTS-Development/Assets/Scripts/AI/Enemy_AI.cs index 7ae948d..bc63c8e 100644 --- a/Dots-RTS-Development/Assets/Scripts/AI/Enemy_AI.cs +++ b/Dots-RTS-Development/Assets/Scripts/AI/Enemy_AI.cs @@ -428,7 +428,13 @@ public static explicit operator Enemy_AI(Cell.enmTeam team) { return null; } else { - return Initialize_AI.AIs[(int)team - 2]; + foreach (Enemy_AI ai in Initialize_AI.AIs) { + if (ai.team == team) { + return ai; + } + } + Debug.LogError("AIs doesn't have an ai with team " + team); + return null; } } #endregion diff --git a/Dots-RTS-Development/Assets/Scripts/Core Classes/Initialize_AI.cs b/Dots-RTS-Development/Assets/Scripts/AI/Initialize_AI.cs similarity index 69% rename from Dots-RTS-Development/Assets/Scripts/Core Classes/Initialize_AI.cs rename to Dots-RTS-Development/Assets/Scripts/AI/Initialize_AI.cs index 6db3a93..8e6c18e 100644 --- a/Dots-RTS-Development/Assets/Scripts/Core Classes/Initialize_AI.cs +++ b/Dots-RTS-Development/Assets/Scripts/AI/Initialize_AI.cs @@ -4,51 +4,48 @@ public class Initialize_AI : MonoBehaviour { - public static List> clanList = new List>(); public GameObject playerData; - public bool[] initAIs = new bool[8] { false, false, false, false, false, false, false, false }; - public Cell.enmTeam[] aiTeams = new Cell.enmTeam[8] { Cell.enmTeam.NONE, Cell.enmTeam.NONE, Cell.enmTeam.NONE, Cell.enmTeam.NONE, Cell.enmTeam.NONE, Cell.enmTeam.NONE, Cell.enmTeam.NONE, Cell.enmTeam.NONE }; - public float[] decisionSpeeds = new float[8]; - public static Enemy_AI[] AIs = new Enemy_AI[8]; + public static List AIs = new List(); private Player playerScript; + private void OnEnable() { + AIs.Clear(); + } private void Start() { //Should be necessary only for older saves and Debug scene + if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == UnityEngine.SceneManagement.Scenes.DEBUG) { - StartAiInitialization(new Dictionary()); + StartAiInitialization(new Dictionary(), new Dictionary()); } } //Goes though all the cells and creates an AI for each team. - public void StartAiInitialization(Dictionary clanDict) { - clanList = BasicConversions.CDToActualClans(clanDict); - + public void StartAiInitialization(Dictionary clanDict, Dictionary difficultyDict) { GameObject g = Instantiate(playerData); g.name = "Player"; playerScript = g.GetComponent(); - foreach (Cell c in PlayManager.cells) { - if ((int)c.cellTeam >= 2) { - SetAis((int)c.cellTeam - 2, c.cellTeam); - } - } - Dictionary.KeyCollection keys = clanDict.Keys; - List tl = new List(); - foreach (Cell.enmTeam q in keys) { - tl.Add (q); + //Make an ai for every team contained in the clandict + Dictionary.KeyCollection teams = clanDict.Keys; + foreach (Cell.enmTeam team in teams) { + if ((int)team >= 2) { + float diff; + if (difficultyDict.TryGetValue(team, out diff) == false) { + SetAis(team, 2); + } + else { + SetAis(team, diff); + } + } } - List InterfaceList = new List(); foreach (Enemy_AI ai in AIs) { - - if (ai != null && tl.Contains (ai.team)) { - InterfaceList.Add(ai); - } + InterfaceList.Add(ai); } InterfaceList.Add(playerScript); @@ -105,16 +102,13 @@ public void StartAiInitialization(Dictionary clanDict) { */ } - public void SetAis(int index, Cell.enmTeam team) { + public void SetAis(Cell.enmTeam team, float decisionSpeed) { - if (initAIs[index] == false) { - initAIs[index] = true; - aiTeams[index] = team; - GameObject aiHolder = new GameObject("AI code " + index + " enemy " + (index + 1)); + GameObject aiHolder = new GameObject("AI code " + (int)team + " " + team); //Select AI preset according to the enemy team AI_Behaviour ai; - switch (index) { + switch ((int)team - 1) { case 0: { ai = aiHolder.AddComponent(); break; @@ -152,12 +146,11 @@ public void SetAis(int index, Cell.enmTeam team) { break; } } - ai.decisionSpeed = decisionSpeeds[index]; + ai.decisionSpeed = decisionSpeed; ai.team = team; ai.isActive = true; - AIs[index] = ai; + AIs.Add(ai); ai.playerScript = playerScript; - } } } diff --git a/Dots-RTS-Development/Assets/Scripts/Core Classes/Initialize_AI.cs.meta b/Dots-RTS-Development/Assets/Scripts/AI/Initialize_AI.cs.meta similarity index 100% rename from Dots-RTS-Development/Assets/Scripts/Core Classes/Initialize_AI.cs.meta rename to Dots-RTS-Development/Assets/Scripts/AI/Initialize_AI.cs.meta diff --git a/Dots-RTS-Development/Assets/Scripts/Core Classes/PlayManager.cs b/Dots-RTS-Development/Assets/Scripts/Core Classes/PlayManager.cs index 065d7ea..caaaca0 100644 --- a/Dots-RTS-Development/Assets/Scripts/Core Classes/PlayManager.cs +++ b/Dots-RTS-Development/Assets/Scripts/Core Classes/PlayManager.cs @@ -52,15 +52,14 @@ public IEnumerator GameState(float updateCheckFreq) { int activeAIs = 0; int alliedAIs = 0; - for (int i = 0; i < Initialize_AI.AIs.Length; i++) { - if (Initialize_AI.AIs[i] != null) { - if (Initialize_AI.AIs[i].isActive) { - activeAIs++; - } - if (Initialize_AI.AIs[i].IsAllyOf(playerScript)) { + foreach (Enemy_AI ai in Initialize_AI.AIs) { + if (ai.isActive) { + activeAIs++; + if (ai.IsAllyOf(playerScript)) { alliedAIs++; } } + } if (activeAIs == 0 || activeAIs == alliedAIs) { diff --git a/Dots-RTS-Development/Assets/Scripts/Menu & Wrappers/LoadFromFile.cs b/Dots-RTS-Development/Assets/Scripts/Menu & Wrappers/LoadFromFile.cs index 26e7a5a..864a495 100644 --- a/Dots-RTS-Development/Assets/Scripts/Menu & Wrappers/LoadFromFile.cs +++ b/Dots-RTS-Development/Assets/Scripts/Menu & Wrappers/LoadFromFile.cs @@ -35,12 +35,7 @@ void Start() { if (customSave.gameSize != 0) { Camera.main.orthographicSize = customSave.gameSize; } - GameObject.Find("Borders").GetComponent().ResizeBackground(customSave.savedAtAspect); - Dictionary.KeyCollection diffKeys = customSave.difficulty.Keys; - foreach (Cell.enmTeam key in diffKeys) { - //print("Possible error"); - customSave.difficulty.TryGetValue(key, out init.decisionSpeeds[(int)key - 2]); - } + GameObject.Find("Borders").GetComponent().ResizeBackground(customSave.savedAtAspect);; for (int j = 0; j < customSave.cells.Count; j++) { @@ -61,7 +56,7 @@ void Start() { //c.UpdateCellInfo(); } - init.StartAiInitialization(customSave.clans); + init.StartAiInitialization(customSave.clans, customSave.difficulty); } else if (PlayManager.levelState == PlayManager.PlaySceneState.CAMPAIGN) { @@ -70,10 +65,6 @@ void Start() { Camera.main.orthographicSize = campaignSave.game.gameSize; } GameObject.Find("Borders").GetComponent().ResizeBackground(campaignSave.game.savedAtAspect); - Dictionary.KeyCollection diffKeys = campaignSave.game.difficulty.Keys; - foreach (Cell.enmTeam key in diffKeys) { - campaignSave.game.difficulty.TryGetValue(key, out init.decisionSpeeds[(int)key - 2]); - } for (int j = 0; j < campaignSave.game.cells.Count; j++) { @@ -91,7 +82,7 @@ void Start() { c.UpdateCellInfo(); } - init.StartAiInitialization(campaignSave.game.clans); + init.StartAiInitialization(campaignSave.game.clans, campaignSave.game.difficulty); } else if (PlayManager.levelState == PlayManager.PlaySceneState.PREVIEW) { @@ -101,10 +92,6 @@ void Start() { Camera.main.orthographicSize = customSave.gameSize; } GameObject.Find("Borders").GetComponent().ResizeBackground(customSave.savedAtAspect); - Dictionary.KeyCollection diffKeys = customSave.difficulty.Keys; - foreach (Cell.enmTeam key in diffKeys) { - customSave.difficulty.TryGetValue(key, out init.decisionSpeeds[(int)key - 2]); - } for (int j = 0; j < customSave.cells.Count; j++) { @@ -122,7 +109,7 @@ void Start() { c.UpdateCellInfo(); } - init.StartAiInitialization(customSave.clans); + init.StartAiInitialization(customSave.clans, customSave.difficulty); } else { SceneManager.LoadScene(Scenes.PROFILES);