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
| ////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include "windows.h"
#include "math.h"
#define EMPTY 0
#define SEED 1
#define SPRING 2
#define STEM 3
#define ROOT 4
#define LEAF 5
#define GENOM_SIZE 16
#define TYPES_COUNT 5
#define XMAX 100
#define YMAX 70
const int pixelDiametr = ((GetSystemMetrics(SM_CXSCREEN) / XMAX + 3) * YMAX <= (GetSystemMetrics(SM_CYSCREEN))?(GetSystemMetrics(SM_CXSCREEN) / XMAX + 3):(GetSystemMetrics(SM_CYSCREEN) / YMAX + 3)); // авто подбор размера под монитор (формула)
const int xOffset = (GetSystemMetrics(SM_CXSCREEN) - (pixelDiametr - 3) * XMAX) / 2, yOffset = (GetSystemMetrics(SM_CYSCREEN) - (pixelDiametr - 3) * YMAX) / 2;
struct genom {
int left;
int up;
int down;
int right;
};
struct definePlant {
bool exists;
genom gen[GENOM_SIZE];
int energy;
sf::Color plantColor;
int age;
};
struct cellData {
int type;
int belongsTo;
int gen;
int seedAge;
};
definePlant plant[50];
cellData cell[XMAX][YMAX];
bool isSun = 1;
//sf::Color cell[XMAX][YMAX];
sf::RectangleShape pixel;
void drawAll(sf::RenderWindow &window) {
for (int x = 0; x < XMAX; x++) {
for (int y = 0; y < YMAX; y++) {
pixel.setFillColor((cell[x][y].type == EMPTY) ? sf::Color(0, 0, 0) : (cell[x][y].type == LEAF) ? plant[cell[x][y].belongsTo].plantColor + sf::Color(0, 40, 0) : (cell[x][y].type == ROOT) ? plant[cell[x][y].belongsTo].plantColor - sf::Color(20, 20, 20) : (cell[x][y].type == SEED) ? plant[cell[x][y].belongsTo].plantColor + sf::Color(20, 20, 20) : plant[cell[x][y].belongsTo].plantColor);
pixel.setPosition(x * (pixelDiametr - 3) + xOffset, y * (pixelDiametr - 3) + yOffset);
window.draw(pixel);
}
}
}
int createPlant(genom genGet[16]) {
for (int checkEmptyPlant = 0; checkEmptyPlant < 50; checkEmptyPlant++) {
if (!plant[checkEmptyPlant].exists) {
plant[checkEmptyPlant].exists = 1;
plant[checkEmptyPlant].energy = 100;
plant[checkEmptyPlant].age = 0;
plant[checkEmptyPlant].plantColor = sf::Color(rand() % 256 + 1, rand() % 256, rand() % 256);
for (int i = 0; i < GENOM_SIZE; i++) {
plant[checkEmptyPlant].gen[i] = { (rand() % 20) ? genGet[i].left : genGet[i].left + (rand() % 3 - 1), (rand() % 20) ? genGet[i].up : genGet[i].up + (rand() % 3 - 1), (rand() % 20) ? genGet[i].down : genGet[i].down + (rand() % 3 - 1), (rand() % 20) ? genGet[i].right : genGet[i].right + (rand() % 3 - 1) };
}
return checkEmptyPlant;
}
}
return -1;
}
////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
std::srand(static_cast<unsigned int>(std::time(NULL)));
// Define some constants
sf::Vector2f pixelSize(pixelDiametr, pixelDiametr);
// Create the window of the application
sf::RenderWindow window(sf::VideoMode(0, 0, 32), "SFML walk",
sf::Style::Titlebar | sf::Style::Close | sf::Style::Fullscreen);
window.setVerticalSyncEnabled(true);
pixel.setSize(pixelSize - sf::Vector2f(3, 3));
reset:
for (int i = 0; i < 50; i++) {
plant[i].exists = 0;
}
for (int x = 0; x < XMAX; x++) {
for (int y = 0; y < YMAX; y++) {
cell[x][y].type = EMPTY;
}
}
cell[XMAX / 2][YMAX / 2] = {SPRING, 0, 0};
plant[0].exists = 1;
plant[0].energy = 100;
plant[0].plantColor = sf::Color(rand() % 256, rand() % 256, rand() % 256);
plant[0].gen[0] = { 1, 2, 2, 1 }; // 16 seed 18 leaf
plant[0].gen[1] = { 1, 18, 18, 1 };
plant[0].gen[2] = { 16, 2, 2, 16 };
while (window.isOpen())
{
// Handle events
sf::Event event;
while (window.pollEvent(event))
{
// Window closed or escape key pressed: exit
if ((event.type == sf::Event::Closed) ||
((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape)))
{
window.close();
break;
}
}
//logic
int alive = 0;
bool ignore[XMAX][YMAX] = { 0 };
for (int xl = 0; xl < XMAX; xl++) {
for (int yl = 0; yl < YMAX; yl++) {
if (cell[xl][yl].type == EMPTY) { continue; }
if (!plant[cell[xl][yl].belongsTo].exists && !(cell[xl][yl].type == SEED && plant[cell[xl][yl].belongsTo].age > 99)) { cell[xl][yl] = {0, -1, 0}; }
if (ignore[xl][yl]) { continue; }
if (cell[xl][yl].type == STEM) {
plant[cell[xl][yl].belongsTo].energy -= 1;
}
if (cell[xl][yl].type == LEAF) {
if (isSun) { plant[cell[xl][yl].belongsTo].energy += 3; }
else { plant[cell[xl][yl].belongsTo].energy -= 1; }
}
if (cell[xl][yl].type == SEED) {
//if (cell[xl][yl].plantColor != plant[cell[xl][yl].belongsTo].plantColor) { cell[xl][yl] = { SPRING, createPlant(plant[cell[xl][yl].belongsTo].gen), 0 }; }
if (!plant[cell[xl][yl].belongsTo].exists) { cell[xl][yl] = { SPRING, createPlant(plant[cell[xl][yl].belongsTo].gen), 0 }; }
for (int xll = xl - 2; xll <= xl + 2; xll++) {
for (int yll = yl - 2; yll <= yl + 2; yll++) {
if (cell[xll][yll].type == SEED && xll != xl && yll != yl) { cell[xl][yl] = { 0, -1, 0 }; }
}
}
alive++;
}
if (cell[xl][yl].type == SPRING) {
//left
if (xl != 0 && cell[xl - 1][yl].type == EMPTY) {
if (plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].left < GENOM_SIZE) { //получение информации о геноме
cell[xl - 1][yl] = { SPRING, cell[xl][yl].belongsTo, plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].left };
}
else if (plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].left == 16) {
cell[xl - 1][yl] = { SEED, cell[xl][yl].belongsTo, plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].left };
}
else if (plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].left == 17) {
cell[xl - 1][yl] = { ROOT, cell[xl][yl].belongsTo };
}
else if (plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].left == 18) {
cell[xl - 1][yl] = { LEAF, cell[xl][yl].belongsTo };
}
}
//up
if (yl != 0 && cell[xl][yl - 1].type == EMPTY) {
if (plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].up < GENOM_SIZE) { //получение информации о геноме
cell[xl][yl - 1] = { SPRING, cell[xl][yl].belongsTo, plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].up };
}
else if (plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].up == 16) {
cell[xl][yl - 1] = { SEED, cell[xl][yl].belongsTo, plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].up };
}
else if (plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].up == 17) {
cell[xl][yl - 1] = { ROOT, cell[xl][yl].belongsTo };
}
else if (plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].up == 18) {
cell[xl][yl - 1] = { LEAF, cell[xl][yl].belongsTo };
}
}
//down
if (yl != YMAX - 1 && cell[xl][yl + 1].type == EMPTY) {
if (plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].down < GENOM_SIZE) { //получение информации о геноме
cell[xl][yl + 1] = { SPRING, cell[xl][yl].belongsTo, plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].down };
}
else if (plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].down == 16) {
cell[xl][yl + 1] = { SEED, cell[xl][yl].belongsTo, plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].down };
}
else if (plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].down == 17) {
cell[xl][yl + 1] = { ROOT, cell[xl][yl].belongsTo };
}
else if (plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].down == 18) {
cell[xl][yl + 1] = { LEAF, cell[xl][yl].belongsTo };
}
if (plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].down <= 18) {
ignore[xl][yl + 1] = 1;
}
}
//right
if (xl != XMAX - 1 && cell[xl + 1][yl].type == EMPTY) {
if (plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].right < GENOM_SIZE) { //получение информации о геноме
cell[xl + 1][yl] = { SPRING, cell[xl][yl].belongsTo, plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].right };
}
else if (plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].right == 16) {
cell[xl + 1][yl] = { SEED, cell[xl][yl].belongsTo, plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].right };
}
else if (plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].right == 17) {
cell[xl + 1][yl] = { ROOT, cell[xl][yl].belongsTo };
}
else if (plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].right == 18) {
cell[xl + 1][yl] = { LEAF, cell[xl][yl].belongsTo };
}
if (plant[cell[xl][yl].belongsTo].gen[cell[xl][yl].gen].right <= 18) {
ignore[xl + 1][yl] = 1;
}
}
cell[xl][yl].type = STEM;
continue;
}
//cell[xl][yl].plantColor = plant[cell[xl][yl].belongsTo].plantColor;
}
}
for (int pId = 0; pId < 50; pId++) {
if (!plant[pId].exists) { continue; }
alive++;
if (plant[pId].energy <= 0) { plant[pId].exists = 0; }
plant[pId].age++;
if (plant[pId].age >= 200) { plant[pId].exists = 0; }
}
if (!alive) { goto reset; }
// Clear the window
window.clear(sf::Color(0, 0, 0));
drawAll(window);
// Display things on screen
window.display();
Sleep(100);
}
return EXIT_SUCCESS;
} |