forked from lastleon/EvolutionSimulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvoSim2.pde
More file actions
228 lines (198 loc) · 5.98 KB
/
EvoSim2.pde
File metadata and controls
228 lines (198 loc) · 5.98 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
import grafica.*;
//// Verschiedene Buttonarten
// ! Reihenfolge darf nicht verändert werden ! //
enum ButtonType {
FITNESS, AVGAGE, POPULATION, FLOOD, GENERATION, SWITCHSLIDER
};
//// Outputs zum speichern der Daten
PrintWriter outputOldestAge;
PrintWriter outputAverageAge;
PrintWriter outputAverageFitness;
PrintWriter outputDeathsAndBirths;
PrintWriter outputPopulationSize;
// Momentane Dateinummer: z.B. 532 -> durchschnittsFitnessLw532.txt
// wird unten aus saveDataIndex.dat ausgelesen
int fileNumber;
// bestimmt, ob die Daten gespeichert werden
boolean save = false;
//// Weltvariablen
// speichert die momentane Skalierung, offset, etc.
// fenstergroesse muss seperat geändert werden, sollte immer gleich sein & einen schönen Wert haben, z.B. 100, 500,...
int worldSize;
float scale = 0.1;
float xOffset = 0.0;
float yOffset = 0.0;
float xOffsetTotal = 0.0;
float yOffsetTotal = 0.0;
float xPressed, yPressed;
//// Interface
PImage bod;
PImage head;
// neues Fenster
PApplet iface;
// Plot und zugehörige Daten
GPlot plot;
int plotX = 0;
int plotY = 0;
int plotWidth = 250;
int plotHeight = 250;
float[] margin = new float[] {30, 30, 10, 10};
ButtonType selectedButton = ButtonType.FITNESS;
//// restliche globale Variablen
// ID System
int currentID = 0;
// Kreaturen können mit GODMODE per Hand hinzugefügt werden
boolean godmode = false;
// wenn Maus gedrückt, dann locked wahr
boolean locked = false;
// friert die Simulation ein
boolean freeze = false;
//// Welt
public World map;
final public static float stdOceanLevel = 44;
float oceanLevel = 70;
boolean sinks = true;
void settings() {
size(700, 700);
bod = loadImage("Body.png");
head = loadImage("Head.png");
//fullScreen();
}
void setup() {
// Einstellungen
frameRate(100);
noStroke();
loop();
// Welt erstellt
map = new World(125, 100); // Darf nicht 10 sein, sonst hängt sich die Simulation auf (??????)
// Interface (neuesFenster) erstellt
iface = new Interface();
// saveDataIndex erhöht, Dateien & Writer erstellt
if (save) {
if (!fileExists(sketchPath("saveDataIndex.dat"))) {
fileNumber = 1;
byte[] b = {1};
saveBytes("saveDataIndex.dat", b);
} else {
fileNumber = bytesToInt(loadBytes("saveDataIndex.dat")) + 1;
saveBytes("saveDataIndex.dat", intToBytes(fileNumber));
}
outputOldestAge = createWriter("./data/ältestesLw/ältestesLw"+fileNumber+".txt");
outputAverageAge = createWriter("./data/durchschnittsLw/durchschnittsLw"+fileNumber+".txt");
outputAverageFitness = createWriter("./data/durchschnittsFitnessLw/durchschnittsFitnessLw"+fileNumber+".txt");
outputDeathsAndBirths = createWriter("./data/todeUndGeburtenLw/todeUndGeburtenLw"+fileNumber+".txt");
outputPopulationSize = createWriter("./data/population/population"+fileNumber+".txt");
}
// Welt & Kreaturen werden angezeigt
map.showWorld();
map.showCreature();
}
// Mainloop
void draw() {
map.update();
if (sinks) {
for (int j = 0; j < map.world.length; j++) {
for (Field f : map.world[j]) {
f.influenceByWater();
}
}
if (oceanLevel > stdOceanLevel) {
oceanLevel-= 0.1;
}
}
}
// Eventhandler
void mouseWheel(MouseEvent event) {
float e = event.getCount();
// Grenzwerte der Scale werden überprüft
scale -= (e / 10)*scale;
if (scale < 0.07) {
scale = 0.07;
}
if (scale > 10) {
scale = 10;
}
// zoom auf Mauszeiger
if (!(scale <= 0.07 || scale >= 10)) {
float rMouseX = (mouseX-(xOffsetTotal))/scale;
float rMouseY = (mouseY-(yOffsetTotal))/scale;
xOffsetTotal += rMouseX * (e / 10)*scale;
yOffsetTotal += rMouseY * (e / 10)*scale;
}
if (xOffsetTotal > 0) xOffsetTotal = 0;
if (yOffsetTotal > 0) yOffsetTotal = 0;
if ( xOffsetTotal + map.worldBounds*scale < width) xOffsetTotal = width-map.worldBounds*scale;
if ( yOffsetTotal + map.worldBounds*scale < height) yOffsetTotal = height-map.worldBounds*scale;
}
void mouseDragged() {
// offset pro Frame wird berechnet und zu Gesamtoffset addiert, wenn Maus gedrückt ist
if (locked) {
xOffset = (mouseX - xPressed);
yOffset = (mouseY - yPressed);
xOffsetTotal += xOffset;
yOffsetTotal += yOffset;
xPressed = mouseX;
yPressed = mouseY;
cursor(MOVE);
if (xOffsetTotal > 0) xOffsetTotal = 0;
if (yOffsetTotal > 0) yOffsetTotal = 0;
if ( xOffsetTotal + map.worldBounds*scale < width) xOffsetTotal = width-map.worldBounds*scale;
if ( yOffsetTotal + map.worldBounds*scale < height) yOffsetTotal = height-map.worldBounds*scale;
}
}
void keyPressed() {
// Leertaste : zoom & offset zurückgesetzt
if (key ==' ') {
scale = 1;
xOffsetTotal = 0;
yOffsetTotal = 0;
}
// GODMODE
if (key == 'g') {
godmode = !godmode;
}
// GODMODE kommandos
if (key == 'n' && godmode == true) {
map.population.addCreature(new Creature(mouseX, mouseY, map, currentID));
currentID++;
}
if (key == 'd' && godmode == true) {
Creature c = map.getCreature(new PVector(mouseX, mouseY));
if (c != null) {
map.population.removeCreature(c);
}
}
}
void mousePressed() {
// temporäre Variablen für Offsetberechnung
locked = true;
xPressed = mouseX;
yPressed = mouseY;
}
void mouseReleased() {
// offset soll nicht mehr berechnet werden
locked = false;
cursor(ARROW);
}
//// Helfermethoden
byte[] intToBytes(int x) {
int bLength = floor(x/255);
byte[] returnValue = new byte[bLength+1];
for (int y = 0; y < bLength; y++) {
returnValue[y] = byte(255);
}
returnValue[bLength] = byte(x%255);
return returnValue;
}
int bytesToInt(byte[] b) {
int returnValue = 0;
for (byte by : b) {
returnValue += int(by);
}
return returnValue;
}
//// I/O Methoden
boolean fileExists(String path) {
File file=new File(sketchPath(path));
return file.exists();
}