-
Notifications
You must be signed in to change notification settings - Fork 0
/
God.cs
125 lines (111 loc) · 2.99 KB
/
God.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class God : MonoBehaviour {
public UnityEngine.UI.Text generation_listing;
public int generation;
public int genome;
public int MAX_GENOMES; // MUST BE EVEN
bool detecting_round;
bool detecting_deadlock;
// Use this for initialization
void Start () {
generation = 0;
MAX_GENOMES = 8;
genome = 0;
detecting_round = false;
detecting_deadlock = false;
}
// Update is called once per frame
void Update () {
generation_listing.text = "Generation # " + (generation + 1) + "\nGenome # " + (genome + 1);
if (!detecting_round) {
StartCoroutine (DetectNewRound ());
}
if (!detecting_deadlock) {
StartCoroutine (DetectDeadlock ());
}
}
IEnumerator DetectNewRound()
{
detecting_round = true;
if (GameObject.FindGameObjectsWithTag ("Cart").Length == 0) {
yield return new WaitForSeconds (5f);
if (GameObject.FindGameObjectsWithTag ("Cart").Length == 0) {
NextGenome ();
}
} else {
yield return new WaitForSeconds (5f);
}
detecting_round = false;
}
IEnumerator DetectDeadlock()
{
bool deadlock = true;
detecting_deadlock = true;
GameObject[] carts = GameObject.FindGameObjectsWithTag ("Cart");
foreach (GameObject cart in carts) {
if (!cart.GetComponent<Cart> ().stopping) {
deadlock = false;
}
}
yield return new WaitForSeconds (10f);
GameObject[] carts_2 = GameObject.FindGameObjectsWithTag ("Cart");
foreach (GameObject cart in carts_2) {
if (!cart.GetComponent<Cart> ().stopping) {
deadlock = false;
}
}
if (deadlock == true) {
try
{
GameObject[] intersections = GameObject.FindGameObjectsWithTag ("Intersection");
foreach (GameObject intersection in intersections) {
foreach (GameObject cart in carts) {
if (intersection.GetComponent<ML> ().loc_x == cart.GetComponent<Cart> ().current_x &&
intersection.GetComponent<ML> ().loc_y == cart.GetComponent<Cart> ().current_y) {
intersection.GetComponent<ML> ().nets [genome].AddCost (500);
}
}
}
}
catch {}
NextGenome ();
}
detecting_deadlock = false;
}
void NextGenome()
{
Random.InitState ((int)69);
GameObject[] carts = GameObject.FindGameObjectsWithTag ("Cart");
foreach (GameObject cart in carts) {
Destroy (cart);
}
bool new_gene = false;
Debug.Log ("Next Genome!");
genome++;
if (genome == MAX_GENOMES) {
new_gene = true;
generation++;
genome = 0;
}
GameObject[] tunnels = GameObject.FindGameObjectsWithTag ("Tunnel");
foreach (GameObject tunnel in tunnels) {
Spawner spawner = tunnel.GetComponentInChildren<Spawner>();
if (spawner != null) {
spawner.NewRound();
}
}
GameObject[] intersections = GameObject.FindGameObjectsWithTag ("Intersection");
foreach (GameObject intersection in intersections) {
ML ml = intersection.GetComponent<ML> ();
if (ml != null) {
ml.cost = 0;
if (new_gene == true) {
ml.NewGeneration ();
}
}
}
new_gene = false;
}
}