Coverage report for server


src/
File: src/meteors.c
Date: 2024-06-25 10:57:05
Lines:
0/51
0.0%
Functions:
0/4
0.0%
Branches:
0/34
0.0%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** zappy
4 ** File description:
5 ** meteors.c
6 */
7
8 #include "flags.h"
9 #include "server.h"
10
11 ✗ static info_map_t get_tile(server_t *server, size_t x, size_t y)
12 {
13 ✗ tile_t tile = server->map[x + y * server->proprieties.width];
14 info_map_t info = {0};
15
16 ✗ for (size_t i = 0; i < tile.num_objects; i++) {
17 ✗ info.food += tile.objects[i] == FOOD;
18 ✗ info.linemate += tile.objects[i] == LINEMATE;
19 ✗ info.deraumere += tile.objects[i] == DERAUMERE;
20 ✗ info.sibur += tile.objects[i] == SIBUR;
21 ✗ info.mendiane += tile.objects[i] == MENDIANE;
22 ✗ info.phiras += tile.objects[i] == PHIRAS;
23 ✗ info.thystame += tile.objects[i] == THYSTAME;
24 }
25 ✗ return info;
26 }
27
28 ✗ static void add_object(server_t *server, int max, int value, int obj_enum)
29 {
30 ✗ int diff = max - value;
31 ✗ int x = server->proprieties.width;
32 ✗ int y = server->proprieties.height;
33 int x_rand = 0;
34 int y_rand = 0;
35 info_map_t in = {0};
36
37 ✗ for (int i = diff; i >= 0; i--) {
38 ✗ x_rand = rand_p(x);
39 ✗ y_rand = rand_p(y);
40 ✗ add_element_to_map(server, x_rand, y_rand, obj_enum);
41 ✗ in = get_tile(server, x_rand, y_rand);
42 ✗ if (in.food >= max || in.linemate >= max || in.deraumere >= max ||
43 ✗ in.sibur >= max || in.mendiane >= max || in.phiras >= max ||
44 in.thystame >= max)
45 break;
46 ✗ message_to_graphicals(server, "bct %d %d %d %d %d %d %d %d %d\n",
47 x_rand, y_rand, in.food, in.linemate, in.deraumere,
48 in.sibur, in.mendiane, in.phiras, in.thystame);
49 }
50 ✗ }
51
52 ✗ static void fill_objects(server_t *server, info_map_t *max, info_map_t *map)
53 {
54 ✗ add_object(server, max->food, map->food, FOOD);
55 ✗ add_object(server, max->linemate, map->linemate, LINEMATE);
56 ✗ add_object(server, max->deraumere, map->deraumere, DERAUMERE);
57 ✗ add_object(server, max->sibur, map->sibur, SIBUR);
58 ✗ add_object(server, max->mendiane, map->mendiane, MENDIANE);
59 ✗ add_object(server, max->phiras, map->phiras, PHIRAS);
60 ✗ add_object(server, max->thystame, map->thystame, THYSTAME);
61 ✗ }
62
63 ✗ void handle_meteors(server_t *server)
64 {
65 ✗ struct timespec current = server->current_time;
66 ✗ struct timespec meteor_time = server->meteor_last_time;
67 ✗ double interval = METEORS_LIMIT / (double)server->proprieties.frequency;
68 ✗ time_t sec_sus = (current.tv_sec - meteor_time.tv_sec);
69 ✗ time_t nsec_sus = (current.tv_nsec - meteor_time.tv_nsec);
70 ✗ double elapsed = sec_sus + nsec_sus / NANOSECONDS_IN_SECOND;
71 ✗ info_map_t map;
72 ✗ info_map_t max_map = server->proprieties.max_map;
73
74 ✗ if (elapsed >= interval) {
75 ✗ map = get_map_density(server);
76 ✗ clock_gettime(CLOCK_REALTIME, &server->meteor_last_time);
77 ✗ if (map.thystame != max_map.thystame || map.phiras != max_map.phiras ||
78 ✗ map.mendiane != max_map.mendiane || map.sibur != max_map.sibur ||
79 ✗ map.linemate != max_map.linemate || map.food != max_map.food ||
80 ✗ map.deraumere != max_map.deraumere)
81 ✗ fill_objects(server, &max_map, &map);
82 }
83 ✗ }
84