-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.pde
More file actions
214 lines (185 loc) · 5.56 KB
/
Snake.pde
File metadata and controls
214 lines (185 loc) · 5.56 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
private final float LToVTimesPhiFToLConstant = PI/2.0; //Sanity constant
private final float LToVConstant = 125.0; //Sanity constant
public class Snake extends Creature{
private final float l; //length
private final float m; //mutation constant
color tailColor = #000000;
float getL() {
return l;
};
float getM() {
return m;
};
float[] Mutate(float L, float V, float Phi, float F, float M) {
float[] mutatedValues = new float[5];
float VtoLRatio = V*L;
float PhiLtoFRatio = Phi*F/L;
mutatedValues[2] = constrain(Phi+random((-M*Phi), (M*Phi)), 0, 2*PI);
mutatedValues[3] = -mutatedValues[2]/(2*PI)+(F+Phi/(2*PI));
mutatedValues[0] = mutatedValues[2]*mutatedValues[3]/PhiLtoFRatio;
mutatedValues[1] = VtoLRatio/mutatedValues[0];
mutatedValues[4] = constrain(M+random((-M*M), (M*M)), 0, 1);
return mutatedValues;
};
Snake() {
super( random(PI/3, 2*PI), LToVTimesPhiFToLConstant );
l = LToVConstant / v;
m = random(0.0, 1.0);
FOVColor = headColor = color(0, int(random(128, 255)), 0);
tailColor = color(0, 0, int(random(128, 255)));
Coords.add(headPosition.clone());
brain = new Brain(true);
};
Snake(float L, float V, float Phi, float M) {
super( V, Phi );
l = L;
m = M;
FOVColor = headColor = color(0, int(random(128, 255)), 0);
tailColor = color(0, 0, int(random(128, 255)));
Coords.add(headPosition.clone());
brain = new Brain(true);
};
Snake(String data) {
String [] values = data.split(";");
int index = 0;
l = Float.parseFloat(values[index++]);
v = Float.parseFloat(values[index++]);
phi = Float.parseFloat(values[index++]);
m = Float.parseFloat(values[index++]);
FOVColor = headColor = color(0, int(random(128, 255)), 0);
tailColor = color(0, 0, int(random(128, 255)));
Coords.add(headPosition.clone());
brain = new Brain(true);
for(Matrix s : brain.S){
for(int i=0; i<s.getRowDimension(); i++)
for(int j=0; j<s.getColumnDimension(); j++){
s.set(i, j, Double.parseDouble(values[index++]));
}
}
};
Snake(Snake Parent) {
super();
// This creates very fast creatures, needs to be looked into
//float[] mutatedValues = Mutate(Parent.getL(), Parent.getV(), Parent.getPhi(), Parent.getF(), Parent.getM());
//l = mutatedValues[0];
//v = mutatedValues[1];
//phi = mutatedValues[2];
//f = mutatedValues[3];
//m = mutatedValues[4];
l = Parent.l;
v = Parent.v;
phi = Parent.phi;
f = Parent.f;
m = Parent.m;
FOVColor = headColor = color(0, int(random(128, 255)), 0);
tailColor = color(0, 0, int(random(128, 255)));
Coords.add(headPosition.clone());
brain = new Brain(Parent.brain);
brain.Mutate();
};
//Snake-specific drawing
ArrayList<Point> Coords = new ArrayList<Point>();
void setPosition(float X, float Y) {
super.setPosition(X, Y);
Coords.add(headPosition.clone());
};
void drawTail() {
float distance = 0;
noFill();
stroke(tailColor);
beginShape();
curveVertex(Coords.get(Coords.size()-1).x, Coords.get(Coords.size()-1).y);
for(int i=Coords.size()-2; i>-1; i--)
{
curveVertex(Coords.get(i).x, Coords.get(i).y);
distance += sqrt(sq(Coords.get(i).x - Coords.get(i+1).x)+sq(Coords.get(i).y - Coords.get(i+1).y));
if(distance >= l)
{ //<>// //<>//
for(; i>-1; i--)
{
Coords.remove(0);
}
}
}
endShape();
};
void update(boolean CanGoAhead, boolean DoDraw) {
super.update(CanGoAhead, DoDraw);
if(CanGoAhead) {
Coords.add(headPosition.clone());
}
if(DoDraw) {
this.drawTail();
float[] info = {l, v, phi, f, m};
String[] infoNames = {"L", "V", "Phi", "F", "M"};
HoverInfo(info, infoNames);
}
}
//Does that pass through?
boolean IsPassedThrough(Point start, Point end, boolean DoDraw) {
if(Coords.size() < 2)
return false;
else
{
Point P;
Segment snakePart = new Segment(), checkedPart = new Segment(start, end);
for(int i=0; i<Coords.size()-1; i++)
{
snakePart.Set(Coords.get(i), Coords.get(i+1));
P = snakePart.WhereIsIntersecting(checkedPart);
if(P != null) {
//Shows the collision
if(DoDraw) {
P.Draw(#FF0000, 15);
}
return true;
}
}
}
return false;
};
boolean IsPassedThrough(Segment checkedPart, boolean DoDraw) {
if(Coords.size() < 2)
return false;
else
{
Point P;
Segment snakePart = new Segment();
for(int i=0; i<Coords.size()-1; i++)
{
snakePart.Set(Coords.get(i), Coords.get(i+1));
P = snakePart.WhereIsIntersecting(checkedPart);
if(P != null) {
//Shows the collision
if(DoDraw) {
P.Draw(#FF0000, 15);
}
return true;
}
}
}
return false;
};
String Serialize(){
// float L, float V, float Phi, float M
StringBuilder me = new StringBuilder("");
// Snake characteristics
me.append(l);
me.append(';');
me.append(v);
me.append(';');
me.append(phi);
me.append(';');
me.append(m);
me.append(';');
// Brain characteristics
for(Matrix s : brain.S){
for(int i=0; i<s.getRowDimension(); i++)
for(int j=0; j<s.getColumnDimension(); j++){
me.append(s.get(i, j));
me.append(';');
}
}
return me.toString();
}
};