-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreature.pde
More file actions
235 lines (209 loc) · 5.72 KB
/
Creature.pde
File metadata and controls
235 lines (209 loc) · 5.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
abstract class Creature implements Hoverable {
float v; //velocity
float phi; //field of view angle
float f; //recognition constant
color headColor = #000000, FOVColor = #000000;
Point headPosition = new Point(int(random(safetyMargin, size[0]-safetyMargin)), int(random(safetyMargin, size[1]-safetyMargin)));
float delta = PI/90, courseAngle = 0;
float fitness = 0;
int miceCaught;
Ray[] Rays = new Ray[rayNumber];
Brain brain = new Brain();
Creature() {
v = 0.0;
phi = 0.0;
f = 0.0;
for (int i=0; i<rayNumber; i++) {
Rays[i] = new Ray();
}
fitness = 0;
lastOutput = 0;
miceCaught= 0;
};
Creature(float Phi, float LToVTimesPhiFToLConstant) {
phi = Phi;
f = -phi/(2*PI)+1.05;
v = LToVTimesPhiFToLConstant/(phi*f);
for (int i=0; i<rayNumber; i++) {
Rays[i] = new Ray();
}
fitness = 0;
lastOutput = 0;
miceCaught= 0;
}
float getV() {
return v;
};
float getPhi() {
return phi;
};
float getF() {
return f;
};
void SetFitness(float F) {
fitness = F;
};
void MultiplyFitness(float F) {
fitness *= F;
};
void IncreaseFitness(float By) {
fitness += By;
};
void setPosition() {
headPosition = new Point(int(random(safetyMargin, size[0])), int(random(safetyMargin, size[1])));
};
void setPosition(Point head) {
headPosition = head.clone();
};
void setPosition(float X, float Y) {
headPosition.x = X;
headPosition.y = Y;
};
Point calculateNewPosition() {
return new Point(
min(max(0, headPosition.x+v*cos(courseAngle)), size[0]),
min(max(0, headPosition.y+v*sin(courseAngle)), size[1])
);
};
void setAngle(float Delta) {
delta = Delta;
};
void DrawFOV() {
fill(FOVColor, 128);
stroke(#FFFF00);
beginShape(TRIANGLE_FAN);
vertex(headPosition.x,headPosition.y);
Point P = new Point();
for(int i=0; i<rayNumber; i++) {
P.Set(headPosition.x+FOVBaseSize*(f+0.5)*cos(courseAngle-phi/2+i*phi/rayNumber),
headPosition.y+FOVBaseSize*(f+0.5)*sin(courseAngle-phi/2+i*phi/rayNumber));
vertex(P.x, P.y);
Rays[i].Set(headPosition, P);
//Rays[i].Draw(#FFFF00);
}
endShape();
};
void DrawHead() {
headPosition.Draw(headColor, 5);
};
boolean hasReactedToInput = false;
float lastOutput;
int skip = 5;
void update(boolean CanGoAhead, boolean DoDraw) {
if(CanGoAhead) {
courseAngle = (courseAngle+delta)%(2*PI);
Point newHead = headPosition.clone();
newHead.x = headPosition.x+v*cos(courseAngle);
newHead.y = headPosition.y+v*sin(courseAngle);
if(newHead.x < 0) {
headPosition.x = 0;
courseAngle = 0;
}
else if (newHead.x > size[0]) {
headPosition.x = size[0];
courseAngle = PI;
}
else {
headPosition.x = newHead.x;
}
if(newHead.y < 0) {
headPosition.y = 0;
courseAngle = PI/2;
}
else if (newHead.y > size[1]) {
headPosition.y = size[1];
courseAngle = PI+PI/2;
}
else {
headPosition.y = newHead.y;
}
}
if(DoDraw) {
DrawFOV();
DrawHead();
}
if(lastOutput != 0 && skip < 0) {
float curOutput = brain.DecideAngle();
if(abs(lastOutput - curOutput) >1e-6){
hasReactedToInput = true;
}
lastOutput = curOutput;
}
else{
lastOutput = brain.DecideAngle();
skip--;
}
this.setAngle(lastOutput);
};
void GetSightings(float[][] Sightings) {
brain.GetSightings(Sightings);
};
public int IsSeenByRay(Creature C) {
int IS = -1;
for(int i=0; i< rayNumber; i++) {
if(Rays[i].CheckIfInFrontAndInInterval(C.headPosition, sightInterval))
IS = i;
}
return IS;
};
public int IsSeenByRay(Point P) {
int IS = -1;
for(int i=0; i< rayNumber; i++) {
if(Rays[i].CheckIfInFrontAndInInterval(P, sightInterval))
IS = i;
}
return IS;
};
//Hoverable
void HoverInfo(float[] information, String[] informationNames) {
float rectX=0, rectY=0, rectW=0, rectH=0;
textSize(10);
rectMode(CORNER);
textAlign(BASELINE);
if(abs(mouseX-headPosition.x)<25 && abs(mouseY-headPosition.y)<25)
{
rectW = 50;
for(int i=0; i<information.length; i++)
if(rectW < informationNames[i].length()+10+80)
rectW = informationNames[i].length()+10+80;
rectH = information.length*12;
if(headPosition.x>= size[0]/2) {
if(headPosition.y>=size[1]/2) {
rectX = headPosition.x - 10 - rectW;
rectY = headPosition.y - 10 - rectH;
}
else {
rectX = headPosition.x - 10 - rectW;
rectY = headPosition.y + 10;
}
}
else {
if(headPosition.y>=size[1]/2) {
rectX = headPosition.x + 10;
rectY = headPosition.y - 10 - rectH;
}
else {
rectX = headPosition.x + 10;
rectY = headPosition.y + 10;
}
}
stroke(#FF00FF);
fill(255, 192);
rect(rectX, rectY, rectW, rectH, 2);
for(int i=0; i<information.length; i++)
{
informationNames[i] += ": ";
float difference = rectW-90-informationNames[i].length()+2;
for(int j=0; j<difference; j++)
{
informationNames[i] += " ";
}
}
for(int i=0; i<information.length; i++)
{
fill(0, 0, 255);
text(informationNames[i]+information[i], rectX, rectY+((float)i/information.length)*rectH+10);
}
}
};
};