Coverage report for server


src/
File: src/utils/map.c
Date: 2024-06-25 10:57:05
Lines:
33/46
71.7%
Functions:
4/5
80.0%
Branches:
14/20
70.0%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** zappy
4 ** File description:
5 ** map.c
6 */
7
8 #include "server.h"
9
10 static const struct {
11 object_t type;
12 char *name;
13 } object_handlers[] = {
14 {FOOD, "food"},
15 {LINEMATE, "linemate"},
16 {DERAUMERE, "deraumere"},
17 {SIBUR, "sibur"},
18 {MENDIANE, "mendiane"},
19 {PHIRAS, "phiras"},
20 {THYSTAME, "thystame"},
21 {PLAYER, "player"},
22 {EMPTY, ""},
23 {0, NULL}
24 };
25
26 31 void add_element_to_map(server_t *server, int x, int y, object_t object)
27 {
28 31 server->map[y * server->proprieties.width + x].objects =
29 31 realloc(server->map[y * server->proprieties.width + x].objects,
30 31 sizeof(object_t) * (server->map[y * server->proprieties.width + x]
31 31 .num_objects + 1));
32 31 server->map[y * server->proprieties.width + x].objects[server->map[y *
33 31 server->proprieties.width + x].num_objects] = object;
34 31 server->map[y * server->proprieties.width + x].num_objects++;
35 31 }
36
37 6 void remove_element_from_map(server_t *s, int x, int y, object_t o)
38 {
39 6 for (size_t i = 0; i < s->map[y * s->proprieties.width + x]
40
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 .num_objects; i++) {
41
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (s->map[y * s->proprieties.width + x].objects[i] != o)
42 continue;
43 6 s->map[y * s->proprieties.width + x].objects[i] =
44 s->map[y * s->proprieties.width + x]
45 6 .objects[s->map[y * s->proprieties.width + x]
46 .num_objects - 1];
47 6 s->map[y * s->proprieties.width + x].num_objects--;
48
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if (s->map[y * s->proprieties.width + x].num_objects > 0) {
49 4 s->map[y * s->proprieties.width + x].objects =
50 4 realloc(s->map[y * s->proprieties.width + x].objects,
51 sizeof(object_t) * s->map[y * s->proprieties.width + x]
52 .num_objects);
53 } else {
54 2 free(s->map[y * s->proprieties.width + x].objects);
55 2 s->map[y * s->proprieties.width + x].objects = NULL;
56 }
57 break;
58 }
59 6 }
60
61 info_map_t get_map_density(server_t *server)
62 {
63 info_map_t info_map = {0};
64 int map_size = server->proprieties.width * server->proprieties.height;
65 tile_t *cell = NULL;
66
67 for (int idx = 0; idx < map_size; idx++) {
68 cell = &server->map[idx];
69 for (size_t k = 0; k < cell->num_objects; k++) {
70 info_map.food += (cell->objects[k] == FOOD);
71 info_map.linemate += (cell->objects[k] == LINEMATE);
72 info_map.deraumere += (cell->objects[k] == DERAUMERE);
73 info_map.sibur += (cell->objects[k] == SIBUR);
74 info_map.mendiane += (cell->objects[k] == MENDIANE);
75 info_map.phiras += (cell->objects[k] == PHIRAS);
76 info_map.thystame += (cell->objects[k] == THYSTAME);
77 }
78 }
79 return info_map;
80 }
81
82 100 void print_tile(tile_t *tile)
83 {
84 dprintf(1, "[");
85
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 100 times.
103 for (size_t k = 0; k < tile->num_objects; k++) {
86 3 dprintf(1, "%s%s", object_handlers[tile->objects[k]].name,
87
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 k + 1 < tile->num_objects ? ", " : "");
88 }
89 dprintf(1, "]");
90 100 }
91
92 1 void print_map(server_t *server)
93 {
94
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1 times.
11 for (int i = 0; i < server->proprieties.height; i++) {
95
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 10 times.
110 for (int j = 0; j < server->proprieties.width; j++) {
96 dprintf(1, "%d, %d, ", j, i);
97 100 print_tile(&server->map[i * server->proprieties.width + j]);
98
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 90 times.
100 dprintf(1, "%s", j + 1 < server->proprieties.width ? ", " : "");
99 }
100 dprintf(1, "\n");
101 }
102 1 }
103