-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
1344 lines (1121 loc) · 48.5 KB
/
main.c
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include<assert.h>
#include<math.h>
#include<stdio.h>
#include<stdbool.h>
#include<string.h>
#include<time.h>
#include "SDL.h"
struct timespec time2() {
struct timespec now;
timespec_get(&now, TIME_UTC);
return now;
}
long time_diff(struct timespec t1, struct timespec t2) {
return ((t2.tv_nsec - t1.tv_nsec) / 1000000 + (t2.tv_sec - t1.tv_sec) * 1000);
}
int rand_gen(int min, int max) {
int difference = max - min;
int result = rand() / (RAND_MAX / (difference + 1));
result = result + min;
if (result > max) { result = max; }
return result;
}
struct Vec {
size_t size_element;
size_t capacity;
size_t len;
void* ptr;
};
struct Vec new_vec(size_t size) {
struct Vec vec;
void* ptr = malloc(size * 10);
vec.size_element = size;
vec.capacity = 10;
vec.len = 0;
vec.ptr = ptr;
return vec;
}
void clear_vec(struct Vec* vec) {
vec->len = 0;
}
void vec_free(struct Vec* vec) {
free(vec->ptr);
}
void vec_realloc(struct Vec* vec, size_t new_capacity, size_t size) {
void* new_ptr = malloc(new_capacity * size);
memcpy(new_ptr, vec->ptr, vec->len * vec->size_element);
free(vec->ptr);
vec->capacity = new_capacity;
vec->ptr = new_ptr;
}
void push_vec_ptr(struct Vec* vec, void* value) {
assert(vec->size_element == sizeof(size_t));
if (vec->capacity == vec->len) {
vec_realloc(vec, vec->capacity * 2, sizeof(size_t));
}
void** destination_ptr = vec->ptr + (vec->len * sizeof(size_t));
*destination_ptr = value;
vec->len = vec->len + 1;
}
void push_vec_vec(struct Vec* vec, struct Vec value) {
assert(vec->size_element == sizeof(struct Vec));
if (vec->capacity == vec->len) {
vec_realloc(vec, vec->capacity * 2, sizeof(struct Vec));
}
struct Vec* destination_ptr = vec->ptr + (vec->len * sizeof(struct Vec));
*destination_ptr = value;
vec->len = vec->len + 1;
}
void* get_vec_ptr(struct Vec* vec, size_t num) {
assert(vec->size_element == sizeof(size_t));
assert(vec->len > num);
void** destination_ptr = vec->ptr + (num * sizeof(size_t));
return *destination_ptr;
}
struct Vec* get_vec_vec(struct Vec* vec, size_t num) {
assert(vec->size_element == sizeof(struct Vec));
assert(vec->len > num);
struct Vec* destination_ptr = vec->ptr + (num * sizeof(struct Vec));
return destination_ptr;
}
void vec_swap_remove_ptr(struct Vec* vec, size_t num) {
assert(vec->size_element == sizeof(size_t));
assert(num < vec->len);
if (num != vec->len - 1) {
memcpy(vec->ptr + num * sizeof(size_t), vec->ptr + (vec->len - 1) * sizeof(size_t), sizeof(size_t));
}
vec->len = vec->len - 1;
}
void vec_swap_remove_vec(struct Vec* vec, size_t num) {
assert(vec->size_element == sizeof(struct Vec));
assert(num < vec->len);
if (num != vec->len - 1) {
memcpy(vec->ptr + num * sizeof(struct Vec), vec->ptr + (vec->len - 1) * sizeof(struct Vec), sizeof(struct Vec));
}
vec->len = vec->len - 1;
}
struct Point2d {
int x;
int y;
};
struct MouseEvents {
struct Point2d mouse_position;
struct Point2d mouse_position_matrix;
bool mouse_left_pressed;
bool mouse_right_pressed;
bool mouse_middle_pressed;
};
struct KeyboardEvents {
bool up_pressed;
bool down_pressed;
bool left_pressed;
bool right_pressed;
bool space_pressed;
bool w_pressed;
bool r_pressed;
};
struct Events {
struct MouseEvents mouse_events;
struct KeyboardEvents keyboard_events;
bool quit;
};
struct Rectangle {
int x;
int y;
};
struct Camera {
int x;
int y;
float z;
};
struct Constants {
int x;
int y;
bool max_camera;
size_t local_player;
int matrix_size;
struct Rectangle size_map;
float unit_speed;
int inertia;
int max_inertia;
float collision_speed;
int max_split;
};
struct Window {
SDL_Window* window;
SDL_Event input;
struct Events events;
};
struct Assets {
struct Vec textures;
struct Vec sounds;
};
struct DrawingBuffer {
struct Vec local_cells;
struct Vec drawable_units;
};
struct Player {
size_t index;
struct Vec cells;
struct Vec buildings;
struct Vec food;
};
struct Game {
struct Assets assets;
struct DrawingBuffer drawing_buffer;
struct Camera camera;
struct Vec players;
struct Vec units;
struct Vec matrix_unit;
struct Vec units_killed;
};
struct App {
struct Window window;
SDL_Renderer* renderer;
struct Constants constants;
struct Game game;
};
struct Texture {
SDL_Texture* image;
};
struct Texture* initialize_texture(struct App* app, uint8_t red, uint8_t green, uint8_t blue) {
uint8_t pixels[1000][1000][4];
for (int x = 0; x < 1000; x = x + 1) {
for (int y = 0; y < 1000; y = y + 1) {
pixels[x][y][0] = 0;
pixels[x][y][1] = 0;
pixels[x][y][2] = 0;
pixels[x][y][3] = 0;
float distance = sqrt(((float)x - 500) * ((float)x - 500) + ((float)y - 500) * ((float)y - 500));
if (distance < 500) {
pixels[x][y][0] = 255;
pixels[x][y][1] = 0;
pixels[x][y][2] = 0;
pixels[x][y][3] = 0;
if (distance < 480) {
pixels[x][y][0] = 255;
pixels[x][y][1] = blue;
pixels[x][y][2] = green;
pixels[x][y][3] = red;
}
}
}
}
SDL_Texture* sdl_texture;
SDL_Surface* sdl_surface = SDL_CreateRGBSurfaceWithFormatFrom(
&pixels,
1000,
1000,
1,
1000 * 4,
SDL_PIXELFORMAT_RGBA8888
);
sdl_texture = SDL_CreateTextureFromSurface(app->renderer, sdl_surface);
SDL_FreeSurface(sdl_surface);
struct Texture texture = { .image = sdl_texture };
struct Texture* texture_ptr = malloc(sizeof(struct Texture));
*texture_ptr = texture;
push_vec_ptr(&app->game.assets.textures, texture_ptr);
return texture_ptr;
}
void initialize_all_textures(struct App* app) {
initialize_texture(app, 200, 10, 10); //red
initialize_texture(app, 10, 200, 10); //green
initialize_texture(app, 10, 10, 200); //blue
initialize_texture(app, 80, 80, 200); //light blue
initialize_texture(app, 200, 100, 10); //orange
initialize_texture(app, 200, 10, 100); //purple
initialize_texture(app, 200, 200, 10); //yellow
}
void destroy_texture(struct Texture* texture) {
SDL_Texture* sdl_texture = (*texture).image;
SDL_DestroyTexture(sdl_texture);
}
struct Texture* return_texture(struct App* app, char* texture) {
if (strcmp(texture, "Red cell") == 0) { return get_vec_ptr(&app->game.assets.textures, 0); }
else if (strcmp(texture, "Green cell") == 0) { return get_vec_ptr(&app->game.assets.textures, 1); }
else if (strcmp(texture, "Blue cell") == 0) { return get_vec_ptr(&app->game.assets.textures, 2); }
else if (strcmp(texture, "Light Blue cell") == 0) { return get_vec_ptr(&app->game.assets.textures, 3); }
else if (strcmp(texture, "Orange cell") == 0) { return get_vec_ptr(&app->game.assets.textures, 4); }
else if (strcmp(texture, "Purple cell") == 0) { return get_vec_ptr(&app->game.assets.textures, 5); }
else if (strcmp(texture, "Yellow cell") == 0) { return get_vec_ptr(&app->game.assets.textures, 6); }
else if (strcmp(texture, "Random") == 0) { int rng = rand() / (RAND_MAX / 7); return get_vec_ptr(&app->game.assets.textures, rng); }
else { assert(false); }
}
struct Player* initialize_player(struct App* app) {
struct Player player;
player.index = app->game.players.len;
player.cells = new_vec(sizeof(size_t));
player.buildings = new_vec(sizeof(size_t));
player.food = new_vec(sizeof(size_t));
struct Player* player_ptr = malloc(sizeof(struct Player));
*player_ptr = player;
push_vec_ptr(&app->game.players, player_ptr);
return player_ptr;
}
enum UnitType {
Cell,
Building,
CellFood,
Food,
};
struct Unit {
size_t index;
struct Player* player;
enum UnitType unit_type;
int x;
int y;
int64_t mass;
bool moving;
int speed_x;
int speed_y;
int mergeable_time;
int collision_time;
struct Texture* texture;
struct Unit* killed; //Option
int64_t matrix_unit_index[3];
int64_t player_index;
};
struct DrawableUnit {
int x;
int y;
int speed_x;
int speed_y;
int64_t mass;
struct Texture* texture;
};
void push_vec_drawable(struct Vec* vec, struct DrawableUnit value) {
assert(vec->size_element == sizeof(struct DrawableUnit));
if (vec->capacity == vec->len) {
vec_realloc(vec, vec->capacity * 2, sizeof(struct DrawableUnit));
}
struct DrawableUnit* destination_ptr = vec->ptr + (vec->len * sizeof(struct DrawableUnit));
*destination_ptr = value;
vec->len = vec->len + 1;
}
struct DrawableUnit get_vec_drawable(struct Vec* vec, size_t num) {
assert(vec->size_element == sizeof(struct DrawableUnit));
struct DrawableUnit* destination_ptr = vec->ptr + (num * sizeof(struct DrawableUnit));
return *destination_ptr;
}
struct DrawableUnit* get_vec_drawable_ptr(struct Vec* vec, size_t num) {
assert(vec->size_element == sizeof(struct DrawableUnit));
struct DrawableUnit* destination_ptr = vec->ptr + (num * sizeof(struct DrawableUnit));
return destination_ptr;
}
void create_matrix(struct App* app) {
app->game.matrix_unit = new_vec(sizeof(struct Vec));
for (int x = 0; x < app->constants.size_map.x; x = x + 1) {
push_vec_vec(&app->game.matrix_unit, new_vec(sizeof(struct Vec)));
for (int y = 0; y < app->constants.size_map.y; y = y + 1) {
push_vec_vec(get_vec_vec(&app->game.matrix_unit, x), new_vec(sizeof(struct Unit*)));
}
}
}
void kill_unit_update_matrix(struct App* app, struct Unit* unit) {
//int64_t* matrix_index = &unit->matrix_unit_index;
int64_t matrix_index[3];
matrix_index[0] = unit->matrix_unit_index[0];
matrix_index[1] = unit->matrix_unit_index[1];
matrix_index[2] = unit->matrix_unit_index[2];
vec_swap_remove_ptr(get_vec_vec(get_vec_vec(&app->game.matrix_unit, matrix_index[0]), matrix_index[1]), matrix_index[2]);
if (matrix_index[2] < get_vec_vec(get_vec_vec(&app->game.matrix_unit, matrix_index[0]), matrix_index[1])->len) {
struct Unit* unit = get_vec_ptr(get_vec_vec(get_vec_vec(&app->game.matrix_unit, matrix_index[0]), matrix_index[1]), matrix_index[2]);
unit->matrix_unit_index[2] = matrix_index[2];
}
}
void add_unit_matrix(struct App* app, struct Unit* unit) {
int x = unit->x / app->constants.matrix_size;
int y = unit->y / app->constants.matrix_size;
unit->matrix_unit_index[0] = x;
unit->matrix_unit_index[1] = y;
unit->matrix_unit_index[2] = get_vec_vec(get_vec_vec(&app->game.matrix_unit, x), y)->len;
push_vec_ptr(get_vec_vec(get_vec_vec(&app->game.matrix_unit, x), y), unit);
}
void move_unit_update_matrix(struct App* app, struct Unit* unit) {
kill_unit_update_matrix(app, unit);
add_unit_matrix(app, unit);
}
struct Unit* initialize_unit(struct App* app, struct Player* player, enum UnitType unit_type, int x, int y, int64_t mass, int speed_x, int speed_y, char* texture_str) {
struct Texture* texture = return_texture(app, texture_str);
size_t player_index;
switch (unit_type) {
case Cell: player_index = player->cells.len; break;
case Building: player_index = player->buildings.len; break;
case Food: case CellFood: player_index = player->food.len; break;
};
struct Unit unit = {
.index = app->game.units.len,
.player= player,
.unit_type= unit_type,
.x= x,
.y= y,
.mass=mass,
.moving= true,
.speed_x= speed_x,
.speed_y= speed_y,
.mergeable_time= 0,
.collision_time= 0,
.texture= texture,
.killed= 0,
.matrix_unit_index= {0,0,0},
.player_index= player_index,
};
struct Unit* unit_ptr = malloc(sizeof(struct Unit));
*unit_ptr = unit;
add_unit_matrix(app, unit_ptr);
switch (unit_ptr->unit_type) {
case Cell: push_vec_ptr(&player->cells, unit_ptr); break;
case Building: push_vec_ptr(&player->buildings, unit_ptr); break;
case Food: case CellFood: push_vec_ptr(&player->food, unit_ptr); break;
}
push_vec_ptr(&app->game.units, unit_ptr);
return unit_ptr;
}
void initialize_cell(struct App* app, struct Player* player, int x, int y, int64_t mass, char* texture) {
initialize_unit(app, player, Cell, x, y, mass, 0, 0, texture);
}
void initialize_building(struct App* app, struct Player* player, int x, int y, int64_t mass, char* texture) {
initialize_unit(app, player, Building, x, y, mass, 0, 0, texture);
}
void initialize_food(struct App* app, struct Player* player, int x, int y, int64_t mass, char* texture) {
initialize_unit(app, player, Food, x, y, mass, 0, 0, texture);
}
void throw_cell(struct App* app, struct Player* player, int x, int y, int direction_x, int direction_y, int initial_speed_x, int initial_speed_y, int64_t mass, char* texture) {
float moving_x = (direction_x - x);
float moving_y = (direction_y - y);
float moving_total = sqrt(moving_x * moving_x + moving_y * moving_y);
int new_moving_x = 0;
int new_moving_y = 0;
float moving = moving_total;
if (fabs(moving_x) > 1.0) { new_moving_x = (0.5*((sqrt(mass / M_PI)) * moving_x / moving)); }
if (fabs(moving_y) > 1.0) { new_moving_y = (0.5*((sqrt(mass / M_PI)) * moving_y / moving)); }
int new_x = x + new_moving_x + initial_speed_x;
int new_y = y + new_moving_y + initial_speed_y;
if (new_x < 0) { new_x = 0; }
if (new_x > (app->constants.size_map.x - 1) * app->constants.matrix_size) { new_x = (app->constants.size_map.x - 1) * app->constants.matrix_size; }
if (new_y < 0) { new_y = 0; }
if (new_y > (app->constants.size_map.y - 1) * app->constants.matrix_size) { new_y = (app->constants.size_map.y - 1) * app->constants.matrix_size; }
int mergeable_time = sqrt(mass);
struct Unit* unit = initialize_unit(app, player, Cell, new_x, new_y, mass, new_moving_x + initial_speed_x, new_moving_y + initial_speed_y, texture);
unit->mergeable_time = mergeable_time;
unit->collision_time = 5;
}
void throw_food(struct App* app, struct Player* player, int x, int y, int direction_x, int direction_y, int64_t mass_thrower, int64_t mass, enum UnitType unit_type, float speed, char *texture) {
float moving_x = (direction_x - x);
float moving_y = (direction_y - y);
float moving_total = sqrt(moving_x * moving_x + moving_y * moving_y);
int new_moving_x = 0;
int new_moving_y = 0;
float moving = moving_total;
if (fabs(moving_x) > 1.0) { new_moving_x = speed*moving_x / moving; }
if (fabs(moving_y) > 1.0) { new_moving_y = speed*moving_y / moving; }
float distance = (new_moving_x) * (new_moving_x) + (new_moving_y) * (new_moving_y);
float delta = (mass_thrower / M_PI) / distance;
int x_begin = (new_moving_x * sqrt(delta));
int y_begin = (new_moving_y * sqrt(delta));
int new_x = x + x_begin + new_moving_x;
int new_y = y + y_begin + new_moving_y;
if (new_x < 0) { new_x = 0; }
if (new_x > (app->constants.size_map.x - 1) * app->constants.matrix_size) { new_x = (app->constants.size_map.x - 1) * app->constants.matrix_size; }
if (new_y < 0) { new_y = 0; }
if (new_y > (app->constants.size_map.y - 1) * app->constants.matrix_size) { new_y = (app->constants.size_map.y - 1) * app->constants.matrix_size; }
if (unit_type == Building) {
initialize_unit(app, player, Food, new_x, new_y, mass, new_moving_x, new_moving_y, texture);
} else if (unit_type == Cell) {
initialize_unit(app, player, CellFood, new_x, new_y, mass, new_moving_x, new_moving_y, texture);
}
}
struct Window new_window() {
int x = 1280;
int y = 720;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow(
"Cell sandbox",
SDL_WINDOWPOS_UNDEFINED_MASK,
SDL_WINDOWPOS_UNDEFINED_MASK,
x,
y,
SDL_WINDOW_RESIZABLE | SDL_WINDOW_MAXIMIZED |
SDL_WINDOW_SHOWN /*| SDL_WindowFlags_SDL_WINDOW_BORDERLESS as u32 */
);
SDL_Event input;
struct Events events;
events.keyboard_events.down_pressed = false;
events.keyboard_events.left_pressed = false;
events.keyboard_events.r_pressed = false;
events.keyboard_events.right_pressed = false;
events.keyboard_events.space_pressed = false;
events.keyboard_events.up_pressed = false;
events.keyboard_events.w_pressed = false;
events.quit = false;
struct Window w;
w.events = events;
w.input = input;
w.window = window;
return w;
}
struct App new_app() {
int x = 1280;
int y = 720;
struct Window window = new_window();
SDL_GetWindowSize(window.window, &x, &y);
SDL_Renderer* renderer = SDL_CreateRenderer(window.window, -1, 0);
struct App app;
app.window = window;
app.renderer = renderer;
struct Constants constants;
constants.x = x;
constants.y = y;
constants.max_camera = true;
constants.local_player = 0;
constants.matrix_size = 1000;
struct Rectangle size_map;
size_map.x = 100;
size_map.y = 100;
constants.size_map = size_map;
constants.unit_speed = 500.0;
constants.inertia = 10;
constants.max_inertia = 1500;
constants.collision_speed = 0.5;
constants.max_split = 512;
app.constants = constants;
struct Game game;
struct Assets assets;
assets.textures = new_vec(sizeof(size_t));
game.assets = assets;
struct DrawingBuffer drawing_buffer;
drawing_buffer.drawable_units = new_vec(sizeof(struct DrawableUnit));
drawing_buffer.local_cells = new_vec(sizeof(struct DrawableUnit));
game.drawing_buffer = drawing_buffer;
struct Camera camera;
camera.x = 10000;
camera.y = 10000;
camera.z = 0.5;
game.camera = camera;
game.players = new_vec(sizeof(size_t));
game.units = new_vec(sizeof(size_t));
game.matrix_unit = new_vec(sizeof(struct Vec));
game.units_killed = new_vec(sizeof(size_t));
app.game = game;
struct Player* computer = initialize_player(&app);
create_matrix(&app);
return app;
}
void end_universe(struct App* app) {
for (int i = 0; i < app->game.units.len; i = i + 1) {
struct Unit* unit = get_vec_ptr(&app->game.units, i);
free(unit);
}
for (int i = 0; i < app->game.players.len; i = i + 1) {
struct Player* player = get_vec_ptr(&app->game.players, i);
free(player);
}
for (int i = 0; i < app->game.assets.textures.len; i = i + 1) {
struct Texture* texture = get_vec_ptr(&app->game.assets.textures, i);
destroy_texture(texture);
free(texture);
}
}
void end_app(struct App* app) {
SDL_DestroyRenderer(app->renderer);
SDL_DestroyWindow(app->window.window);
SDL_Quit();
end_universe(app);
}
void synchronize_drawing_buffer(struct App* app) {
struct DrawingBuffer* buffer = &app->game.drawing_buffer;
clear_vec(&buffer->drawable_units);
for (int i = 0; i < app->game.units.len; i = i + 1) {
struct Unit* unit = get_vec_ptr(&app->game.units, i);
struct DrawableUnit draw_unit = {
.x = unit->x,
.y = unit->y,
.speed_x = unit->speed_x,
.speed_y = unit->speed_y,
.mass = unit->mass,
.texture = unit->texture,
};
push_vec_drawable(&buffer->drawable_units, draw_unit);
}
clear_vec(&buffer->local_cells);
struct Player* local_player = get_vec_ptr(&app->game.players, app->constants.local_player);
for (int i = 0; i < local_player->cells.len; i = i + 1) {
struct Unit* unit = get_vec_ptr(&local_player->cells, i);
struct DrawableUnit draw_unit = {
.x = unit->x,
.y = unit->y,
.speed_x = unit->speed_x,
.speed_y = unit->speed_y,
.mass = unit->mass,
.texture = unit->texture,
};
push_vec_drawable(&buffer->local_cells, draw_unit);
}
}
void poll_event(struct Window* window, SDL_Renderer* renderer, struct Constants* constants, struct Game* game) {
while (SDL_PollEvent(&window->input)> 0) {
SDL_Event input = window->input;
if (input.type == SDL_QUIT) { window->events.quit = true; } // Close app
if (input.type == SDL_WINDOWEVENT) {
if (input.window.event == SDL_WINDOWEVENT_RESIZED) {
int x, y;
SDL_GetWindowSize(window->window, &x, &y);
constants->x = x;
constants->y = y;
}
}
{ // All mouse events
struct MouseEvents* mouse_events = &window->events.mouse_events;
if (input.type == SDL_MOUSEBUTTONDOWN) { // Mouse button pressed
if (input.button.button == SDL_BUTTON_LEFT) { mouse_events->mouse_left_pressed = true; } // Mouse left button
if (input.button.button == SDL_BUTTON_RIGHT) { mouse_events->mouse_right_pressed = true; } // Mouse right button
if (input.button.button == SDL_BUTTON_MIDDLE) { mouse_events->mouse_middle_pressed = true; } // Mouse middle button
}
if (input.type == SDL_MOUSEBUTTONUP) { // Mouse button released
if (input.button.button == SDL_BUTTON_LEFT) { mouse_events->mouse_left_pressed = false; } // Mouse left button
if (input.button.button == SDL_BUTTON_RIGHT) { mouse_events->mouse_right_pressed = false; } // Mouse right button
}
if (input.type == SDL_MOUSEMOTION) { // Mouse moved
struct Point2d position = { .x = input.button.x , .y = input.button.y };
mouse_events->mouse_position = position;
}
if (input.type == SDL_MOUSEWHEEL) { // Mousewheel
int wheel_count = window->input.wheel.y;
if (wheel_count > 0) { constants->max_camera = false; }
struct Player* local_player = get_vec_ptr(&game->players, constants->local_player);
game->camera.z = game->camera.z * (1.0 + (wheel_count) * 0.1);
}
}
{ // All keyboard events
struct KeyboardEvents* keyboard_events = &window->events.keyboard_events;
if (input.type == SDL_KEYDOWN) { // Keyboard button pressed
if (input.key.keysym.sym == SDLK_z) { keyboard_events->up_pressed = true; } // Z
if (input.key.keysym.sym == SDLK_s) { keyboard_events->down_pressed = true; } // S
if (input.key.keysym.sym == SDLK_q) { keyboard_events->left_pressed = true; } // Q
if (input.key.keysym.sym == SDLK_d) { keyboard_events->right_pressed = true; } // D
if (input.key.keysym.sym == SDLK_w) { keyboard_events->w_pressed = true; } // W
if (input.key.keysym.sym == SDLK_r) { keyboard_events->r_pressed = true; } // R
if (input.key.keysym.sym == SDLK_SPACE) { keyboard_events->space_pressed = true; } // Space
if (input.key.keysym.sym == SDLK_ESCAPE ) { window->events.quit = true; } // Space
}
if (input.type == SDL_KEYUP ) { // Keyboard button released
if (input.key.keysym.sym == SDLK_z) { keyboard_events->up_pressed = false; } // Z
if (input.key.keysym.sym == SDLK_s) { keyboard_events->down_pressed = false; } // S
if (input.key.keysym.sym == SDLK_q) { keyboard_events->left_pressed = false; } // Q
if (input.key.keysym.sym == SDLK_d) { keyboard_events->right_pressed = false; } // D
if (input.key.keysym.sym == SDLK_w) { keyboard_events->w_pressed = false; } // W
if (input.key.keysym.sym == SDLK_r) { keyboard_events->r_pressed = false; } // R
}
}
}
}
void add_collision_time(struct Unit* unit, int x) {
unit->collision_time = unit->collision_time + x;
if (unit->collision_time < 0) { unit->collision_time = 0; };
}
void add_mergeable_time(struct Unit* unit, int x) {
unit->mergeable_time = unit->mergeable_time + x;
if (unit->mergeable_time < 0) { unit->mergeable_time = 0; };
}
void add_position(struct App* app, struct Unit* unit, int x, int y) {
unit->x = unit->x + x;
unit->y = unit->y + y;
if (unit->x < 0) { unit->x = 0; }
if (unit->x > (app->constants.size_map.x) * app->constants.matrix_size - 1) { unit->x = (app->constants.size_map.x) * app->constants.matrix_size - 1; }
if (unit->y < 0) { unit->y = 0; }
if (unit->y > (app->constants.size_map.y) * app->constants.matrix_size - 1) { unit->y = (app->constants.size_map.y) * app->constants.matrix_size - 1; }
move_unit_update_matrix(app, unit);
}
void add_mass(struct Unit* unit, int64_t mass) {
unit->mass = unit->mass + mass;
if (unit->mass < 100000) { unit->mass = 100000; }
if (unit->mass > 1000000000) { unit->mass = 1000000000; }
}
void add_speed(struct App* app, struct Unit* unit, int x, int y) {
unit->speed_x = unit->speed_x + x;
unit->speed_y = unit->speed_y + y;
if (unit->speed_x < -app->constants.max_inertia) { unit->speed_x = -app->constants.max_inertia; } if (unit->speed_x > app->constants.max_inertia) { unit->speed_x = app->constants.max_inertia; }
if (unit->speed_y < -app->constants.max_inertia) { unit->speed_y = -app->constants.max_inertia; } if (unit->speed_y > app->constants.max_inertia) { unit->speed_y = app->constants.max_inertia; }
}
void check_events(struct App* app) {
struct Player* local_player = get_vec_ptr(&app->game.players, app->constants.local_player);
if (app->window.events.mouse_events.mouse_left_pressed == true) {
}
if (app->window.events.mouse_events.mouse_right_pressed == true) {
}
if (app->window.events.keyboard_events.up_pressed) {
for (int i = 0; i < local_player->cells.len; i = i + 1) {
struct Unit* unit = get_vec_ptr(&local_player->cells, i);
//unit->mass += 1000000;
}
}
if (app->window.events.keyboard_events.down_pressed) { }
if (app->window.events.keyboard_events.left_pressed) { }
if (app->window.events.keyboard_events.right_pressed) { }
if (app->window.events.keyboard_events.space_pressed) {
app->window.events.keyboard_events.space_pressed = false;
int len = local_player->cells.len;
for (int i = 0; i < len; i = i + 1) {
if (local_player->cells.len < app->constants.max_split) {
struct Unit* unit = get_vec_ptr(&local_player->cells, i);
if (unit->mass > 100000 * 2) {
unit->mass /= 2;
int direction_x = app->window.events.mouse_events.mouse_position_matrix.x;
int direction_y = app->window.events.mouse_events.mouse_position_matrix.y;
throw_cell(app, local_player, unit->x, unit->y, direction_x, direction_y, unit->speed_x, unit->speed_y, unit->mass, "Green cell");
}
}
}
}
if (app->window.events.keyboard_events.w_pressed) {
for (int i = 0; i < local_player->cells.len; i = i + 1) {
struct Unit* unit = get_vec_ptr(&local_player->cells, i);
if (unit->mass > 100000) {
unit->mass -= 60000;
int direction_x = app->window.events.mouse_events.mouse_position_matrix.x;
int direction_y = app->window.events.mouse_events.mouse_position_matrix.y;
throw_food(app, local_player, unit->x, unit->y, direction_x, direction_y, unit->mass, 40000, Cell, 500.0, "Green cell");
}
}
}
if (app->window.events.keyboard_events.r_pressed) {
if (local_player->cells.len == 0) {
int x = rand() / (RAND_MAX / (app->constants.matrix_size * app->constants.size_map.x - 1000));
int y = rand() / (RAND_MAX / (app->constants.matrix_size * app->constants.size_map.y - 1000));
initialize_cell(app, local_player, x, y, 1000, "Green cell");
}
/*
for (int i = 0; i < local_player->cells.len; i = i + 1) {
struct Unit* unit = get_vec_ptr(&local_player->cells, i);
unit->mass = 100000;
}
*/
}
}
struct Point2d mouse_position_matrix(struct App* app, struct Point2d point) {
int center_x = (app->constants.x/2);
int center_y = (app->constants.y/2);
int x = point.x;
int y = point.y;
int x_matrix = (app->game.camera.x + (x - center_x) / app->game.camera.z);
int y_matrix = (app->game.camera.y + (y - center_y) / app->game.camera.z);
struct Point2d point_ingame = { .x = x_matrix, .y = y_matrix };
return point_ingame;
}
void moving(struct App* app, struct Unit* unit, int direction_x, int direction_y) {
app->window.events.mouse_events.mouse_position_matrix = mouse_position_matrix(app, app->window.events.mouse_events.mouse_position);
float moving_x = (direction_x - unit->x);
float moving_y = (direction_y - unit->y);
float moving_total = sqrt(moving_x * moving_x + moving_y * moving_y);
float distance_before_slow = 200.0;
float delta_speed = 1.0;
float difference = (moving_total - distance_before_slow) / distance_before_slow;
if (difference < 0.0) {
delta_speed -= fabs(difference);
}
float new_moving_x = 0.0;
float new_moving_y = 0.0;
float moving = moving_total / (app->constants.unit_speed * (1.0 / (log10(unit->mass))));
if (fabs(moving_x) > 1.0) { new_moving_x = delta_speed * moving_x / moving; }
if (fabs(moving_y) > 1.0) { new_moving_y = delta_speed * moving_y / moving; }
int new_x = unit->x + new_moving_x;
int new_y = unit->y + new_moving_y;
// inertia
int new_direction_x = new_x - unit->x;
int new_direction_y = new_y - unit->y;
new_direction_x = (new_direction_x + (app->constants.inertia - 1)*unit->speed_x)/app->constants.inertia;
new_direction_y = (new_direction_y + (app->constants.inertia - 1)*unit->speed_y)/app->constants.inertia;
int speed_x = new_direction_x - unit->speed_x;
int speed_y = new_direction_y - unit->speed_y;
add_position(app, unit, new_direction_x, new_direction_y);
add_speed(app, unit, speed_x, speed_y);
}
void move_local_cells(struct App* app) {
int direction_x = app->window.events.mouse_events.mouse_position_matrix.x;
int direction_y = app->window.events.mouse_events.mouse_position_matrix.y;
struct Player* local_player = get_vec_ptr(&app->game.players, app->constants.local_player);
for (int i = 0; i < local_player->cells.len; i = i + 1) {
struct Unit* unit = get_vec_ptr(&local_player->cells, i);
moving(app, unit, direction_x, direction_y);
}
}
bool add_to_killed_unit(struct App* app, struct Unit* unit, struct Unit* unit_killed) {
if (unit_killed->killed == 0) {
unit_killed->killed = unit;
push_vec_ptr(&app->game.units_killed, unit_killed);
return true;
}
return false;
}
void remove_killed_unit(struct App* app, struct Unit* unit_killed) {
struct Player* player = unit_killed->player;
size_t player_index = unit_killed->player_index;
struct Vec* vector_unit;
switch (unit_killed->unit_type) {
case Cell: vector_unit = &player->cells; break;
case Building: vector_unit = &player->buildings; break;
case Food: case CellFood: vector_unit = &player->food; break;
}
vec_swap_remove_ptr(vector_unit, player_index);
if (player_index < vector_unit->len) {
struct Unit* unit = get_vec_ptr(vector_unit, player_index);
unit->player_index = player_index;
}
size_t unit_index = unit_killed->index;
vec_swap_remove_ptr(&app->game.units, unit_index);
if (unit_index < app->game.units.len) {
struct Unit* unit = get_vec_ptr(&app->game.units, unit_index);
unit->index = unit_index;
}
kill_unit_update_matrix(app, unit_killed);
free(unit_killed);
}
void remove_killed_units(struct App* app) {
for (int i = 0; i < app->game.units_killed.len; i = i + 1) {
struct Unit* unit_killed = get_vec_ptr(&app->game.units_killed, i);
remove_killed_unit(app, unit_killed);
}
clear_vec(&app->game.units_killed);
}
float compute_distance_squared(int position1[2], int position2[2]) {
float result = (float)(position1[0] - position2[0]) * (float)(position1[0] - position2[0]) + (float)(position1[1] - position2[1]) * (float)(position1[1] - position2[1]);
return result;
}
bool check_unit_scope(struct Unit* unit, int position_unit[2], int64_t mass_unit, float radius_unit, struct Unit* unit_to_target) {
if (unit->unit_type == Cell) {
if (unit_to_target->unit_type == Cell) {
if (unit->player == unit_to_target->player) {
if (unit->mergeable_time == 0 && unit_to_target->mergeable_time == 0) {
if (unit->mass >= unit_to_target->mass) {
if (unit->killed != 0) { return false; }
int position_unit_target[2] = { unit_to_target->x, unit_to_target->y };
float radius_unit_target = sqrt(unit_to_target->mass / M_PI);
float scope = (radius_unit - 0.5 * radius_unit_target) * (radius_unit - 0.5 * radius_unit_target);
return (compute_distance_squared(position_unit, position_unit_target) <= scope);
}
}
else {
return false;
}
}
}
}
if (mass_unit > unit_to_target->mass * 1.33) {
int position_unit_target[2] = { unit_to_target->x, unit_to_target->y };
float radius_unit_target = sqrt(unit_to_target->mass / M_PI);
float scope = (radius_unit - 0.5 * radius_unit_target) * (radius_unit - 0.5 * radius_unit_target);
return compute_distance_squared(position_unit, position_unit_target) <= scope;
}
else {
return false;
}
}
struct Vec check_killable_units(struct App* app, struct Unit* unit) {
int x_field = unit->matrix_unit_index[0];
int y_field = unit->matrix_unit_index[1];
int position_unit[2] = { unit->x, unit->y };
int64_t mass_unit = unit->mass;
float radius_unit = sqrt(mass_unit / M_PI);
int scope_field = 1 + (radius_unit/app->constants.matrix_size);
struct Vec units_killed = new_vec(sizeof(struct Unit*));
int x_min;
int x_max;
int y_min;
int y_max;
x_min = x_field - scope_field; if (x_min < 0) { x_min = 0; }
x_max = x_field + scope_field; if (x_max >= app->constants.size_map.x) { x_max = app->constants.size_map.x - 1; }
y_min = y_field - scope_field; if (y_min < 0) { y_min = 0; }
y_max = y_field + scope_field; if (y_max >= app->constants.size_map.y) { y_max = app->constants.size_map.y - 1; }
for (int x = x_min; x <= x_max; x = x + 1) {
for (int y = y_min; y <= y_max; y = y + 1) {