Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Dots-RTS-Development/Assets/Scripts/AI/Enemy_AI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,48 @@

public class Initialize_AI : MonoBehaviour {

public static List<List<Cell.enmTeam>> clanList = new List<List<Cell.enmTeam>>();
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<Enemy_AI> AIs = new List<Enemy_AI>();

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<Cell.enmTeam, AIHolder>());
StartAiInitialization(new Dictionary<Cell.enmTeam, AIHolder>(), new Dictionary<Cell.enmTeam, float>());
}
}

//Goes though all the cells and creates an AI for each team.
public void StartAiInitialization(Dictionary<Cell.enmTeam, AIHolder> clanDict) {
clanList = BasicConversions.CDToActualClans(clanDict);

public void StartAiInitialization(Dictionary<Cell.enmTeam, AIHolder> clanDict, Dictionary<Cell.enmTeam, float> difficultyDict) {
GameObject g = Instantiate(playerData);
g.name = "Player";
playerScript = g.GetComponent<Player>();

foreach (Cell c in PlayManager.cells) {
if ((int)c.cellTeam >= 2) {
SetAis((int)c.cellTeam - 2, c.cellTeam);
}
}

Dictionary<Cell.enmTeam, AIHolder>.KeyCollection keys = clanDict.Keys;
List<Cell.enmTeam> tl = new List<Cell.enmTeam>();
foreach (Cell.enmTeam q in keys) {
tl.Add (q);
//Make an ai for every team contained in the clandict
Dictionary<Cell.enmTeam, AIHolder>.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<IAlly> InterfaceList = new List<IAlly>();
foreach (Enemy_AI ai in AIs) {

if (ai != null && tl.Contains (ai.team)) {
InterfaceList.Add(ai);
}
InterfaceList.Add(ai);
}
InterfaceList.Add(playerScript);

Expand Down Expand Up @@ -105,16 +102,13 @@ public void StartAiInitialization(Dictionary<Cell.enmTeam, AIHolder> 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<AI_0>();
break;
Expand Down Expand Up @@ -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;
}
}
}

11 changes: 5 additions & 6 deletions Dots-RTS-Development/Assets/Scripts/Core Classes/PlayManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ void Start() {
if (customSave.gameSize != 0) {
Camera.main.orthographicSize = customSave.gameSize;
}
GameObject.Find("Borders").GetComponent<PlayFieldSetup>().ResizeBackground(customSave.savedAtAspect);
Dictionary<Cell.enmTeam, float>.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<PlayFieldSetup>().ResizeBackground(customSave.savedAtAspect);;

for (int j = 0; j < customSave.cells.Count; j++) {

Expand All @@ -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) {
Expand All @@ -70,10 +65,6 @@ void Start() {
Camera.main.orthographicSize = campaignSave.game.gameSize;
}
GameObject.Find("Borders").GetComponent<PlayFieldSetup>().ResizeBackground(campaignSave.game.savedAtAspect);
Dictionary<Cell.enmTeam, float>.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++) {

Expand All @@ -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) {
Expand All @@ -101,10 +92,6 @@ void Start() {
Camera.main.orthographicSize = customSave.gameSize;
}
GameObject.Find("Borders").GetComponent<PlayFieldSetup>().ResizeBackground(customSave.savedAtAspect);
Dictionary<Cell.enmTeam, float>.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++) {

Expand All @@ -122,7 +109,7 @@ void Start() {

c.UpdateCellInfo();
}
init.StartAiInitialization(customSave.clans);
init.StartAiInitialization(customSave.clans, customSave.difficulty);
}
else {
SceneManager.LoadScene(Scenes.PROFILES);
Expand Down