-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.h
More file actions
451 lines (375 loc) · 14.5 KB
/
Copy pathengine.h
File metadata and controls
451 lines (375 loc) · 14.5 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#pragma once
#include <SFML/Graphics.hpp>
#include <vector>
const int WIDTH = 300;
const int HEIGHT = 225;
const int CELL_SIZE = 3;
enum Element{
AIR = 0,
SAND = 1,
WATER = 2,
STONE = 3,
DIRT = 4,
FIRE = 5,
ACID_L = 6,
ACID_R = 7,
SMOKE = 8,
};
class SandBoxEngine{
private:
std::vector<unsigned short> grid;
std::vector<unsigned short> colors;
sf::VertexArray pixels;
int getIndex(int x, int y){
return y*WIDTH+x;
}
bool isVaild(int x,int y) const{
return (x>=0 && x <WIDTH && y >= 0 && y < HEIGHT);
}
inline void moveElement(int from, int to, int type, int oldColor, int newColor){
grid[from] = AIR;
grid[to] = type;
colors[to] = oldColor;
colors[from] = newColor;
}
bool AcidAtTheBottom(int currentIndex, int x, int y, int color){
if(!isVaild(x,y+1)) return false;
int downIdx = getIndex(x,y+1);
if(isVaild(x,y) && (grid[downIdx] == ACID_L || grid[downIdx] == ACID_R)){
grid[currentIndex] = AIR;
grid[downIdx] = AIR;
colors[currentIndex] = color;
colors[downIdx] = 0;
return true;
}
return false;
}
bool WaterAtTheBottom(int currentIndex, int x, int y){
if(!isVaild(x, y+1)) return false;
int downIdx = getIndex(x, y+1);
if(grid[downIdx] == WATER){
std::swap(grid[currentIndex], grid[downIdx]);
std::swap(colors[currentIndex], colors[downIdx]);
return true;
}
return false;
}
void updateSand(int x, int y, int currentIndex, int color){
int downIdx = getIndex(x,y+1);
if(AcidAtTheBottom(currentIndex, x, y, color)) return;
if(WaterAtTheBottom(currentIndex, x, y)) return;
if(isVaild(x,y+1) && grid[getIndex(x,y+1)] == AIR){
moveElement(currentIndex, downIdx, SAND, colors[currentIndex], color);
return;
}
int sideDir = (std::rand() % 2 == 0) ? -1 : 1;
int diagL = getIndex(x - sideDir, y+1);
int diagR = getIndex(x + sideDir, y+1);
if(isVaild(x - sideDir, y+1) && grid[diagL] == AIR) {
moveElement(currentIndex, downIdx, SAND, colors[currentIndex], color);
return;
}
if(isVaild(x + sideDir, y+1) && grid[diagR] == AIR){
moveElement(currentIndex, downIdx, SAND, colors[currentIndex], color);
return;
}
//Появление земли
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
for(int i =0; i < 4; ++i){
int tx = x+dx[i];
int ty = y+dy[i];
if(isVaild(tx, ty)){
int targeIdx = getIndex(tx, ty);
if(grid[targeIdx] == WATER){
grid[currentIndex] = AIR;
grid[targeIdx] = DIRT;
return;
}
}
}
}
void updateWater(int x, int y, int currentIndex, int color){
if(AcidAtTheBottom(currentIndex, x, y, color)) return;
int downIdx = getIndex(x, y+1);
if(isVaild(x, y+1) && grid[downIdx] == AIR){
moveElement(currentIndex, downIdx, WATER, colors[currentIndex], color);
return;
}
int sideDir = (std::rand() % 2 == 0) ? -1 : 1;
int diagL = x+sideDir;
int diagR = x - sideDir;
if(isVaild(diagL, y+1) && grid[getIndex(diagL, y+1)] == AIR){
moveElement(currentIndex, getIndex(diagL, y+1), WATER, colors[currentIndex], color);
return;
}
if(isVaild(diagR, y+1) && grid[getIndex(diagR, y+1)] == AIR){
moveElement(currentIndex, getIndex(diagR, y+1), WATER, colors[currentIndex], color);
return;
}
int maxSteps = 15;
int currentX = x;
for(int i = 0; i < maxSteps; ++i){
int nextX = currentX + sideDir;
if(isVaild(nextX, y) && grid[getIndex(nextX, y)] == AIR){
currentX = nextX;
} else {
break;
}
}
if(currentX != x){
moveElement(currentIndex, getIndex(currentX, y), WATER, colors[currentIndex], color);
return;
}
}
void updateDirt(int x, int y, int currentIndex, int color){
if(AcidAtTheBottom(currentIndex, x, y, color)) return;
if(WaterAtTheBottom(currentIndex, x, y)) return;
int downIdx = getIndex(x, y+1);
if(isVaild(x, y+1) && grid[downIdx] == AIR){
moveElement(currentIndex, downIdx, DIRT, colors[currentIndex], color);
return;
}
int sideDir = (std::rand() % 2 == 0) ? -1 : 1;
int diagL = x+sideDir;
int diagR = x-sideDir;
if(isVaild(diagL, y+1) && grid[getIndex(diagL, y+1)] == AIR){
moveElement(currentIndex, getIndex(diagL, y+1), DIRT, colors[currentIndex], color);
return;
}
if(isVaild(diagR, y+1) && grid[getIndex(diagR, y+1)] == AIR){
moveElement(currentIndex, getIndex(diagR, y+1), DIRT, colors[currentIndex], color);
return;
}
}
void updateAcid(int x, int y, int currentIndex, int color) {
int currentType = grid[currentIndex];
int downIndex = getIndex(x, y + 1);
if (isVaild(x, y + 1)) {
if (grid[downIndex] == AIR) {
grid[currentIndex] = AIR;
grid[downIndex] = currentType;
colors[downIndex] = colors[currentIndex];
colors[currentIndex] = color;
return;
} else if (grid[downIndex] != ACID_L && grid[downIndex] != ACID_R) {
grid[currentIndex] = AIR;
grid[downIndex] = AIR;
return;
}
}
int dir = (std::rand() % 2 == 0) ? -1 : 1;
int sideX = x + dir;
if (isVaild(sideX, y)) {
int sideIndex = getIndex(sideX, y);
if (grid[sideIndex] == AIR) {
grid[currentIndex] = AIR;
grid[sideIndex] = (dir == -1) ? ACID_L : ACID_R;
colors[sideIndex] = colors[currentIndex];
colors[currentIndex] = color;
return;
} else if (grid[sideIndex] != ACID_L && grid[sideIndex] != ACID_R) {
grid[currentIndex] = AIR;
grid[sideIndex] = AIR;
return;
}
}
int altX = x - dir;
if (isVaild(altX, y)) {
int altIndex = getIndex(altX, y);
if (grid[altIndex] == AIR) {
grid[currentIndex] = AIR;
grid[altIndex] = (dir == 1) ? ACID_L : ACID_R;
colors[altIndex] = colors[currentIndex];
colors[currentIndex] = color;
return;
} else if (grid[altIndex] != ACID_L && grid[altIndex] != ACID_R) {
grid[currentIndex] = AIR;
grid[altIndex] = AIR;
return;
}
}
}
void updateFire(int x, int y, int currentIndex, int color){
if(std::rand()%100 < 5){
grid[currentIndex] = (std::rand() % 2 == 0) ? SMOKE : AIR;
return;
}
int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int dy[] = {-1, -1, -1, 0, 0, 1, 1, 1};
//Тушение огня
bool isExtinguished = false;
for (int i = 0; i < 8; ++i) {
int targetX = x + dx[i];
int targetY = y + dy[i];
if (isVaild(targetX, targetY)) {
int targetIndex = getIndex(targetX, targetY);
if (grid[targetIndex] == WATER) {
moveElement(currentIndex, targetIndex, SMOKE, colors[targetIndex], color);
isExtinguished = true;
break;
}
}
}
//Распростронение огня
if(std::rand() % 5 == 0){
int dir = std::rand() % 8;
for (int i = 0; i < 8; ++i) {
int currentDir = (dir + i) % 8;
int targetX = x + dx[currentDir];
int targetY = y + dy[currentDir];
if (isVaild(targetX, targetY)) {
int targetIndex = getIndex(targetX, targetY);
if (grid[targetIndex] == DIRT) {
grid[targetIndex] = FIRE;
break;
}
}
}
}
}
void updateSmoke(int x, int y, int currentIndex, int color){
if (std::rand() % 100 < 1) {
grid[currentIndex] = AIR;
colors[currentIndex] = 0;
return;
}
if (y == 0) {
if (std::rand() % 100 < 15) {
grid[currentIndex] = AIR;
colors[currentIndex] = 0;
}
return;
}
if (std::rand() % 100 < 30) {
int dirX = (std::rand() % 3) - 1;
int tx = x + dirX;
int ty = y - 1;
if (isVaild(tx, ty) && grid[getIndex(tx, ty)] == AIR) {
int targetIndex = getIndex(tx, ty);
moveElement(currentIndex, targetIndex, SMOKE, colors[currentIndex], color);
return;
}
else {
int sideX = x + ((std::rand() % 2 == 0) ? -1 : 1);
if (isVaild(sideX, y) && grid[getIndex(sideX, y)] == AIR) {
int targetIndex = getIndex(sideX, y);
moveElement(currentIndex, targetIndex, SMOKE, colors[currentIndex], color);
return;
}
}
}
}
public:
SandBoxEngine(){
grid.assign(WIDTH*HEIGHT, AIR);
colors.assign(WIDTH*HEIGHT, AIR);
pixels.setPrimitiveType(sf::PrimitiveType::Quads);
pixels.resize(WIDTH*HEIGHT*4);
for(int y = 0; y < HEIGHT; ++y){
for(int x = 0; x < WIDTH; ++x){
int index = getIndex(x,y);
int vIndex = 4*index;
float left = x*CELL_SIZE;
float top = y *CELL_SIZE;
float rigth = left + CELL_SIZE;
float bottom = top + CELL_SIZE;
pixels[vIndex + 0].position = sf::Vector2f(left,top);
pixels[vIndex + 1].position = sf::Vector2f(rigth, top);
pixels[vIndex + 2].position = sf::Vector2f(rigth, bottom);
pixels[vIndex + 3].position = sf::Vector2f(left, bottom);
colors[index] = std::rand() % 2;
}
}
}
void addBlock(int mouseX, int mouseY, int brushSize, int BlockId) {
for (int dy = -brushSize; dy <= brushSize; ++dy) {
for (int dx = -brushSize; dx <= brushSize; ++dx) {
int nx = mouseX + dx;
int ny = mouseY + dy;
if (!isVaild(nx, ny)) continue;
int index = getIndex(nx, ny);
if (BlockId == FIRE) {
if (grid[index] == DIRT) {
grid[index] = FIRE;
}
}
else if (BlockId == AIR || grid[index] == AIR) {
if (BlockId == ACID_L) {
grid[index] = (std::rand() % 2 == 0) ? ACID_L : ACID_R;
} else {
grid[index] = BlockId;
colors[index] = std::rand() % 2;
}
}
}
}
}
void FillGrid(int type){
std::fill(grid.begin(), grid.end(), type);
}
// Логика обновления блоков
void update(){
for(int y = HEIGHT - 1; y >= 0; --y){
bool leftToRight = (std::rand() % 2 == 0);
for(int step = 0; step < WIDTH; ++step){
int x = leftToRight ? step : (WIDTH - 1 - step);
int currentIndex = getIndex(x,y);
int color = std::rand() % 2;
switch (grid[currentIndex])
{
case SAND: updateSand(x,y, currentIndex, color); break;
case WATER: updateWater(x,y, currentIndex, color); break;
case DIRT: updateDirt(x, y, currentIndex, color); break;
case ACID_L:
case ACID_R: updateAcid(x, y, currentIndex, color); break;
case FIRE: updateFire(x, y, currentIndex, color); break;
case SMOKE: updateSmoke(x, y, currentIndex, color); break;
}
}
}
}
void draw(sf::RenderWindow& win){
for(int y = 0; y <HEIGHT; ++y){
for(int x = 0; x < WIDTH; ++x){
int index = getIndex(x,y);
sf::Color cellColor;
switch (grid[getIndex(x,y)])
{
case AIR:
cellColor = sf::Color(20,20,20);
break;
case SAND:
cellColor = (colors[index] == 0) ? sf::Color(235,190,85) : sf::Color(185,135,45);
break;
case WATER:
cellColor = (colors[index] == 0) ? sf::Color(40,160,175) : sf::Color(127,255,212);
break;
case DIRT:
cellColor = (colors[index] == 0) ? sf::Color(120,75,45) : sf::Color(85,50,30);
break;
case STONE:
cellColor = (colors[index] == 0) ? sf::Color(70,75,80) : sf::Color(110,115,120);
break;
case ACID_L:
cellColor = (std::rand() % 2 == 0) ? sf::Color(143,254,9) : sf::Color(45,140,5);
break;
case ACID_R:
cellColor = (std::rand() % 2 == 0) ? sf::Color(143,254,9) : sf::Color(45,140,5);
break;
case FIRE:
cellColor = (std::rand() % 2 == 0) ? sf::Color(255, 69, 0) : sf::Color(255, 140, 0);
break;
case SMOKE:
cellColor = (colors[index] == 0) ? sf::Color(80, 80, 80) : sf::Color(120, 120, 120);
break;
}
pixels[(index * 4) + 0].color = cellColor;
pixels[(index * 4) + 1].color = cellColor;
pixels[(index * 4) + 2].color = cellColor;
pixels[(index * 4) + 3].color = cellColor;
}
}
win.draw(pixels);
}
};