-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbug.cpp
134 lines (110 loc) · 4.04 KB
/
bug.cpp
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
#include "bug.h"
#include <cmath>
#include <QDebug>
QDataStream& operator<<(QDataStream& stream, Bug &bug) {
stream << bug.age << bug.children << bug.generation;
stream << bug.energy;
stream << bug.dir << bug.color << bug.split << bug.action;
stream << bug.data;
stream << bug.get_dna().get_mutation();
stream << bug.payload;
stream << bug.processor;
return stream;
}
Bug::Bug(QDataStream &in): EnergyNode(), dna(), processor(), marked(false) {
float mutation;
in >> age >> children >> generation;
in >> energy;
in >> dir >> color >> split >> action;
in >> data;
in >> mutation;
in >> payload;
in >> processor;
dna.set_data(&data);
dna.set_mutation(mutation);
}
Bug::Bug(int data_size, float mut, int steps_per_update, int stack_size):
EnergyNode(), dna(data, mut), processor(steps_per_update, stack_size), dir(4),
split(0), action(0), payload(0), age(0), children(0), generation(0), marked(false)
{
data.resize(data_size);
for(int i = 0; i < data_size; i++) {
data[i] = qrand() % 256;
}
processor.set_data(data);
/*
int r, g, b, a;
r = 0xff - processor.get_out(1); // subtract so that default
g = 0xff - processor.get_out(2);
b = 0xff - processor.get_out(3);
//a = processor.get_out(4);
a = 0xff;
color.setRgb(r, g, b, a);
*/
dir = (processor.get_out(0) + 4) % 9; // 4 is stay still - default out values are 0
color = processor.get_out(1) % 254 + 1; // col 255 reserved for energy, col 0 for background
split = processor.get_out(2);
action = processor.get_out(3);
}
Bug::Bug(Bug *mummy, Bug *daddy, float mut, int max_size, int min_size, int steps_per_update, int stack_size):
EnergyNode(), dna(data, mut), processor(steps_per_update, stack_size), dir(4), split(0), action(0), payload(0), age(0), children(0), marked(false)
{
data.reserve(max_size);
mum = mummy;
dad = daddy;
dna.blend(mum->get_dna(), dad->get_dna(), max_size, min_size);
setX(mum->x());
setY(mum->y());
mum->add_child();
dad->add_child();
generation = qMax(mum->generation + 1, dad->generation + 1);
processor.set_data(data);
dir = (processor.get_out(0) + 4) % 9; // 4 is stay still - default out values are 0
color = processor.get_out(1) % 254 + 1; // col 255 reserved for energy, col 0 for background
split = processor.get_out(2);
action = processor.get_out(3);
}
Bug::Bug(Bug *parent, float mut, int max_size, int min_size, int steps_per_update, int stack_size):
EnergyNode(), dna(data, mut), processor(steps_per_update, stack_size), dir(4), split(0), action(0), payload(0), age(0), children(0), marked(false)
{
data.reserve(max_size);
dna.set_from(parent->get_dna(), max_size, min_size);
setX(parent->x());
setY(parent->y());
parent->add_child();
generation = parent->get_generation() + 1;
processor.set_data(data);
dir = (processor.get_out(0) + 4) % 9; // 4 is stay still - default out values are 0
color = processor.get_out(1) % 254 + 1; // col 255 reserved for energy, col 0 for background
split = processor.get_out(2);
action = processor.get_out(3);
}
Bug::~Bug() {
emit dying();
}
void Bug::set_vision(unsigned char vision[9], unsigned char altitude[9], unsigned char energy[9]) {
// read vision into start of memory
//qDebug() << "setting inputs";
//for(int i = 0; i < 9; i++) {
// processor.set_in(i, vision[i]);
//}
processor.set_in_multi(0, 9, vision);
processor.set_in_multi(9, 9, altitude);
processor.set_in_multi(18, 9, energy);
}
void Bug::update() {
processor.set_in(27, (unsigned char)((int)energy / 256));
processor.set_in(27 + 1, (unsigned char)((int)energy % 256));
// do one step of the processor
//qDebug() << "processor step";
processor.update();
//qDebug() << "getting outputs";
// read and apply direction
dir = (processor.get_out(0) + 4) % 9; // 4 is stay still - default out values are 0
// read and apply new colour
color = processor.get_out(1);
split = processor.get_out(2);
action = processor.get_out(3);
age++;
emit changed();
}