-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSensor.java
More file actions
178 lines (156 loc) · 8.72 KB
/
Sensor.java
File metadata and controls
178 lines (156 loc) · 8.72 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
public class Sensor extends Neuron{
public SensorMethod sensorMethod;
public static int numberOfSensorMethods = 8; // Update this when creating new Sensor methods
public int methodID;
private static int searchDepth = 10;
public Sensor(int methodID) {
super("Sensor");
this.methodID = methodID%(numberOfSensorMethods);
switch(this.methodID){
case 0: this.sensorMethod = Sensor::nearestFoodDistance; break;
case 1: this.sensorMethod = Sensor::detectFoodXDirection; break;
case 2: this.sensorMethod = Sensor::detectFoodYDirection; break;
case 3: this.sensorMethod = Sensor::nearestCreatureDistance; break;
case 4: this.sensorMethod = Sensor::detectCreatureXDirection; break;
case 5: this.sensorMethod = Sensor::detectCreatureYDirection; break;
case 6: this.sensorMethod = Sensor::Oscillator; break;
case 7: this.sensorMethod = Sensor::random; break;
}
}
public interface SensorMethod {
double invoke(Creature creature) throws Exception;
}
////////////////////////////////////////////////////////
// SENSOR METHODS // SENSOR METHODS // SENSOR METHODS //
////////////////////////////////////////////////////////
public static double nearestFoodDistance(Creature creature) throws Exception{
int[] coorOfFoundObject = findNearestObject(creature.getPosX(), creature.getPosY(), Main.loaded.foodLocations);
double distance = distance(coorOfFoundObject, creature);
// returns a value between 1 and 0
return 1-(distance/Main.loaded.worldSize);
}
private static double detectFoodXDirection (Creature creature) throws Exception{
int[] coorOfFoundObject = findNearestObject(creature.getPosX(), creature.getPosY(), Main.loaded.foodLocations);
return directionX(coorOfFoundObject[0], creature);
}
private static double detectFoodYDirection (Creature creature) throws Exception{
int[] coorOfFoundObject = findNearestObject(creature.getPosX(), creature.getPosY(), Main.loaded.foodLocations);
return directionY(coorOfFoundObject[1], creature);
}
private static double nearestCreatureDistance(Creature creature) throws Exception{
int[] coorOfFoundObject = findNearestObject(creature.getPosX(), creature.getPosY(), Main.loaded.creatureLocations);
double distance = distance(coorOfFoundObject, creature);
// returns a value between 1 and 0
return 1-(distance/Main.loaded.worldSize);
}
private static double detectCreatureXDirection (Creature creature) throws Exception{
int[] coorOfFoundObject = findNearestObject(creature.getPosX(), creature.getPosY(), Main.loaded.creatureLocations);
return directionX(coorOfFoundObject[0], creature);
}
private static double detectCreatureYDirection (Creature creature) throws Exception{
int[] indexOfFoundObject = findNearestObject(creature.getPosX(), creature.getPosY(), Main.loaded.creatureLocations);
return directionY(indexOfFoundObject[1], creature);
}
private static double Oscillator(Creature creature) {
if(Main.loaded.currentGenerationTick%creature.getGenome().getOscillatorPeriod()==0){
return 1;
}
else{
return -1;
}
}
private static double random(Creature creature){
return Main.loaded.random.nextDouble(-1,1);
}
////////////////////////////////////////////////////////
// SENSOR METHOD ASSISTORS // SENSOR METHOD ASSISTORS //
////////////////////////////////////////////////////////
public static int[] findNearestObject(int centerX, int centerY, int[][] objectLocations) throws Exception{
// Search logic
if(objectLocations[centerX][centerY] > 0){ // Check Center
return new int[]{centerX,centerY};
}
for(int i=0; i<searchDepth; i++){
if(objectLocations[centerX][(centerY+i)%Main.loaded.worldSize] > 0){ // Check Above
return new int[]{centerX,(centerY+i)%Main.loaded.worldSize};
}
if(objectLocations[((centerX+i)%Main.loaded.worldSize)%Main.loaded.worldSize][centerY] > 0){ // Check Right
return new int[]{(centerX+i)%Main.loaded.worldSize,centerY};
}
if(objectLocations[((centerX-i)+Main.loaded.worldSize)%Main.loaded.worldSize][centerY] > 0){ // Check Left
return new int[]{((centerX-i)+Main.loaded.worldSize)%Main.loaded.worldSize,centerY};
}
if(objectLocations[centerX][((centerY-i)+Main.loaded.worldSize)%Main.loaded.worldSize] > 0){ // Check Below
return new int[]{centerX,((centerY-i)+Main.loaded.worldSize)%Main.loaded.worldSize};
}
for(int j=1; j<i+1;j++){
if(objectLocations[(centerX+j)%Main.loaded.worldSize][(centerY+i)%Main.loaded.worldSize] > 0){ // Check Above Right
return new int[]{(centerX+j)%Main.loaded.worldSize,(centerY+i)%Main.loaded.worldSize};
}
if(objectLocations[((centerX-j)+Main.loaded.worldSize)%Main.loaded.worldSize][(centerY+i)%Main.loaded.worldSize] > 0){ // Check Above Left
return new int[]{((centerX-j)+Main.loaded.worldSize)%Main.loaded.worldSize,(centerY+i)%Main.loaded.worldSize};
}
if(objectLocations[(centerX+j)%Main.loaded.worldSize][((centerY-i)+Main.loaded.worldSize)%Main.loaded.worldSize] > 0){ // Check Below Right
return new int[]{(centerX+j)%Main.loaded.worldSize,((centerY-i)+Main.loaded.worldSize)%Main.loaded.worldSize};
}
if(objectLocations[((centerX-j)+Main.loaded.worldSize)%Main.loaded.worldSize][((centerY-i)+Main.loaded.worldSize)%Main.loaded.worldSize] > 0){ // Check Below Left
return new int[]{((centerX-j)+Main.loaded.worldSize)%Main.loaded.worldSize,((centerY-i)+Main.loaded.worldSize)%Main.loaded.worldSize};
}
if(j < i){
if(objectLocations[(centerX+i)%Main.loaded.worldSize][(centerY+j)%Main.loaded.worldSize] > 0){ // Check Right Above
return new int[]{(centerX+i)%Main.loaded.worldSize,(centerY+j)%Main.loaded.worldSize};
}
if(objectLocations[(centerX+i)%Main.loaded.worldSize][((centerY-j)+Main.loaded.worldSize)%Main.loaded.worldSize] > 0){ // Check Right Below
return new int[]{(centerX+i)%Main.loaded.worldSize,((centerY-j)+Main.loaded.worldSize)%Main.loaded.worldSize};
}
if(objectLocations[((centerX-i)+Main.loaded.worldSize)%Main.loaded.worldSize][(centerY+j)%Main.loaded.worldSize] > 0){ // Check Left Above
return new int[]{((centerX-i)+Main.loaded.worldSize)%Main.loaded.worldSize,(centerY+j)%Main.loaded.worldSize};
}
if(objectLocations[((centerX-i)+Main.loaded.worldSize)%Main.loaded.worldSize][((centerY-j)+Main.loaded.worldSize)%Main.loaded.worldSize] > 0){ // Check Left Below
return new int[]{((centerX-i)+Main.loaded.worldSize)%Main.loaded.worldSize,((centerY-j)+Main.loaded.worldSize)%Main.loaded.worldSize};
}
}
}
}
Exception noObjectFound = new Exception("No Object was Found");
throw noObjectFound;
}
public static double directionX(int posX, Creature creature) throws NullPointerException{
// left
if (posX < creature.getPosX()) {
return -1.0;
}
// right
else if (posX > creature.getPosX()) {
return 1.0;
}
// same
else {
return 0.0;
}
}
public static double directionY(int posY, Creature creature) throws NullPointerException {
// down
if (posY < creature.getPosY()) {
return -1.0;
}
// up
else if (posY > creature.getPosY()) {
return 1.0;
}
// same
else {
return 0.0;
}
}
// distance needs to be fixed for modulus
public static double distance(int[] coor, Creature creature) throws NullPointerException {
int x = creature.getPosX();
int y = creature.getPosY();
// find shortest distance along each axis
int xDifference = Math.min(Math.abs(coor[0] - x), Math.abs(coor[0] - (x - Main.loaded.worldSize)));
int yDifference = Math.min(Math.abs(coor[1] - y), Math.abs(coor[1] - (y - Main.loaded.worldSize)));
// using Pyth theorem
return Math.sqrt(Math.pow(xDifference, 2) + Math.pow(yDifference, 2));
}
}