-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworld.cpp
More file actions
341 lines (319 loc) · 9.9 KB
/
world.cpp
File metadata and controls
341 lines (319 loc) · 9.9 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "world.h"
#include <QApplication>
World::World(QObject *parent):
QObject(parent), best_animal(NULL), best_fitness(0), save_best_on_destroy(false), current_ID(1)
{
}
World::~World()
{
if (save_best_on_destroy){
Animal* ani;
foreach (ani,anis){
if (ani->getFitness() > best_fitness){ // store best animal
if (best_animal != NULL) delete best_animal;
best_fitness = ani->getFitness();
best_animal = ani->cloneAnimal();
best_animal->setID(ani->getID());
}
}
if (best_animal != NULL){
#ifdef DEBUG
qDebug(QString("Best animal (%1) stored with fitness (%2)").arg(best_animal->getID()).arg(best_fitness).toAscii().data());
#endif
saveBestAnimal();
}
}
if (best_animal != NULL)
delete best_animal;
}
/*EyeData World::getEye(ObjectCoord oc, Direction dir)
{
EyeData e;
e.distance = map.getDistance(oc,dir);
e.type = map.getType(oc.addDist(e.distance,dir));
return e;
}*/
void World::addAnimal(Animal *ani, ObjectCoord coord_start)
{
if (ani != NULL){
ani->setParent(this);
ani->setMap(&map);
ani->setID(current_ID++);
connect(ani,SIGNAL(move(Direction)),SLOT(onMove(Direction)));
connect(ani,SIGNAL(eat(Direction)),SLOT(onEat(Direction)));
connect(ani,SIGNAL(suicide()),SLOT(onSuicide()));
connect(ani,SIGNAL(wait()),SLOT(onWait()));
connect(ani,SIGNAL(split(Direction)),SLOT(onSplit(Direction)));
connect(ani,SIGNAL(splitMutate(Direction)),SLOT(onSplit_Mutate(Direction)));
connect(this,SIGNAL(tick()),ani,SLOT(onTick()));
if ((coord_start.x != -1) && (coord_start.y != -1)){
ani->coord.x = coord_start.x;
ani->coord.y = coord_start.y;
}else{
do {
ani->coord.x = qrand() % MAP_X_SIZE;
ani->coord.y = qrand() % MAP_Y_SIZE;
} while (map.getType(ani->coord) == otAnimal);
}
map.createObj(ani->coord, otAnimal);
anis.append(ani);
}
}
void World::addAnimal(QList<quint8> cmds,
QList<quint8> mems,
int cmd_start_ptr,
int mem_start_ptr, ObjectCoord coord_start)
{
Animal* ani = new Animal(&map,cmds,mems,cmd_start_ptr,mem_start_ptr,this);
addAnimal(ani,coord_start);
}
QImage World::getImage()
{
return map.getImage();
}
void World::makeStep()
{
emit tick();
}
void World::saveBestAnimal(QString filename)
{
if (best_animal != NULL){
if (filename.isEmpty()){
filename = QString("best_id%1_fit%2.ani").arg(best_animal->getID()).arg(best_fitness);
}
best_animal->saveAnimal(filename);
}
}
void World::onMove(Direction direction)
{
if (QString("Animal").compare(sender()->metaObject()->className()) == 0){
Animal* ani = (Animal*) sender();
if (ani == NULL) return;
ani->food--;
if (ani->food < 0){
onSuicide();
return;
}
ObjectCoord oc = ani->coord;
oc.addDist(1,direction);
if (map.coordIsValid(oc)) oc = map.correctCoord(oc);
switch (map.getType(oc)){
case otNone:
case otFood: //food will be destroyed
map.moveObj(ani->coord, direction);
ani->coord.addDist(1,direction);
break;
case otStone:
case otAnimal:
//You cannot go on Stone or Animal!
break;
}
}
}
void World::onEat(Direction direction)
{
if (QString("Animal").compare(sender()->metaObject()->className()) == 0){
Animal* ani = (Animal*) sender();
if (ani == NULL) return;
ani->food--;
//suicide later, give chance to eat
ObjectCoord oc = ani->coord;
oc.addDist(1,direction);
if (map.coordIsValid(oc)) oc = map.correctCoord(oc);
switch (map.getType(oc)){
case otFood: //food will be destroyed
ani->fitnessUp();
ani->food += 100;
map.deleteObj(oc);
break;
case otAnimal:
ani->fitnessUp(10);
ani->food += 1000;
//delete animal
{
Animal* ani2=findAnimalByCoord(oc);
if (ani2 != NULL){
#ifdef DEBUG
qDebug(QString("(%1) kill (%2)").arg(ani->getID()).arg(ani2->getID()).toAscii().data());
#endif
killAnimal(ani2);
}
}
map.deleteObj(oc);
break;
case otNone:
case otStone:
//You cannot eat Stone!
//map.moveObj(ani->coord, direction);
//ani->coord.addDist(1,direction);
break;
}
if (ani->food < 0){
onSuicide();
return;
}
}
}
void World::onWait()
{
if (QString("Animal").compare(sender()->metaObject()->className()) == 0){
Animal* ani = (Animal*) sender();
if (ani != NULL)
ani->food--;
}
}
void World::onSuicide()
{
if (QString("Animal").compare(sender()->metaObject()->className()) == 0){
Animal* ani = (Animal*) sender();
if (ani != NULL){
//TODO: add stats
#ifdef DEBUG
qDebug(QString("(%1) suicide with fitness(%2), food(%3)").arg(ani->getID()).arg(ani->getFitness()).arg(ani->food).toAscii().data());
#endif
killAnimal(ani);
}
}
}
void World::onSplit(Direction direction)
{
if (QString("Animal").compare(sender()->metaObject()->className()) == 0){
Animal* ani = (Animal*) sender();
if (ani == NULL) return;
ani->food--;
if (ani->food < 0){
onSuicide();
return;
}
ObjectCoord oc = ani->coord;
oc.addDist(1,direction);
if (map.coordIsValid(oc)) oc = map.correctCoord(oc);
switch (map.getType(oc)){
case otNone:
case otFood: //food will be destroyed
if (ani->food > 1000){
map.deleteObj(oc);
ani->fitnessUp(50);
Animal *new_ani = ani->cloneAnimal();
addAnimal(new_ani,oc);
#ifdef DEBUG
qDebug(QString("(%1) fitness(%3), food(%2) splitted to (%4)").arg(ani->getID()).arg(ani->food).arg(ani->getFitness()).arg(new_ani->getID()).toAscii().data());
#endif
ani->food -= 1000;
}
break;
case otAnimal:
case otStone:
//You cannot split on Stone and Animal!
break;
}
}
}
void World::onSplit_Mutate(Direction direction)
{
if (QString("Animal").compare(sender()->metaObject()->className()) == 0){
Animal* ani = (Animal*) sender();
if (ani == NULL) return;
ani->food--;
if (ani->food < 0){
onSuicide();
return;
}
ObjectCoord oc = ani->coord;
oc.addDist(1,direction);
if (map.coordIsValid(oc)) oc = map.correctCoord(oc);
switch (map.getType(oc)){
case otNone:
case otFood: //food will be destroyed
if (ani->food > 1000){
map.deleteObj(oc);
ani->fitnessUp(50);
//Animal *new_ani = ani->cloneAnimal();
QList<quint8> cmds = ani->getCommands();
QList<quint8> mems = ani->getMemory();
for (int i=0; i < (qrand() % 10); i++){
switch(qrand() % 4){
case 0:
cmds[qrand() % cmds.size()] = qrand() % (256);
break;
case 1:
cmds.append(qrand() % (256));
break;
case 2:
cmds.removeAt(qrand() % cmds.size());
break;
case 3:
cmds.insert(qrand() % cmds.size(), qrand() % (256));
break;
}
}
Animal *new_ani = new Animal(cmds,mems);
addAnimal(new_ani,oc);
#ifdef DEBUG
qDebug(QString("(%1) fitness(%3), food(%2) splitted to (%4)").arg(ani->getID()).arg(ani->food).arg(ani->getFitness()).arg(new_ani->getID()).toAscii().data());
#endif
ani->food -= 1000;
}
break;
case otAnimal:
case otStone:
//You cannot split on Stone and Animal!
break;
}
}
}
Animal *World::findAnimalByCoord(ObjectCoord oc)
{
Animal* ani;
foreach (ani, anis){
if (ani->coord == oc) return ani;
}
return NULL;
}
void World::killAnimal(Animal *ani)
{
if (ani != NULL){
if (ani->getFitness() > best_fitness){ // store best animal
if (best_animal != NULL) delete best_animal;
best_fitness = ani->getFitness();
best_animal = ani->cloneAnimal();
best_animal->setID(ani->getID());
}
#ifdef DEBUG
qDebug(QString("(%1) fitness(%2), food(%3) is dead").arg(ani->getID()).arg(ani->getFitness()).arg(ani->food).toAscii().data());
#endif
map.deleteObj(ani->coord); // clean map
anis.removeAll(ani); // remove ani from lives animals
ani->disconnect(this); // disconnect ani from world
disconnect(this,SIGNAL(tick()),ani,SLOT(onTick()));
delete ani; // kill ani
}
}
Animal *World::findBestLiveAnimal()
{
Animal *ani, *best;
best = NULL;
quint32 best_fitness=0;
foreach (ani,anis){
if (ani->getFitness() > best_fitness){ // store best animal
best_fitness = ani->getFitness();
best = ani;
}
}
return best;
}
void World::feedAnimal()
{
map.addFood(qrand()%1000+100);
}
void World::killWeakAnimals()
{
#ifdef DEBUG
qDebug("kill weak animals!");
#endif
Animal* ani;
foreach (ani, anis){
if (ani->getFitness() == 0)
killAnimal(ani);
}
}