Coverage report for server


src/
File: src/commands/ai/incantation.c
Date: 2024-06-25 10:57:05
Lines:
82/86
95.3%
Functions:
9/9
100.0%
Branches:
41/58
70.7%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** zappy
4 ** File description:
5 ** incantation.c
6 */
7
8 #include "server.h"
9
10 static const int required_resources[7][7] = {
11 {1, 1, 0, 0, 0, 0, 0},
12 {2, 1, 1, 1, 0, 0, 0},
13 {2, 2, 0, 1, 0, 2, 0},
14 {4, 1, 1, 2, 0, 1, 0},
15 {4, 1, 2, 1, 3, 0, 0},
16 {6, 1, 2, 3, 0, 1, 0},
17 {6, 2, 2, 2, 2, 2, 1}
18 };
19
20 8 static info_map_t get_tile(server_t *server, size_t x, size_t y)
21 {
22 8 tile_t tile = server->map[x + y * server->proprieties.width];
23 info_map_t info = {0};
24
25
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 8 times.
42 for (size_t i = 0; i < tile.num_objects; i++) {
26 34 info.food += tile.objects[i] == FOOD;
27 34 info.linemate += tile.objects[i] == LINEMATE;
28 34 info.deraumere += tile.objects[i] == DERAUMERE;
29 34 info.sibur += tile.objects[i] == SIBUR;
30 34 info.mendiane += tile.objects[i] == MENDIANE;
31 34 info.phiras += tile.objects[i] == PHIRAS;
32 34 info.thystame += tile.objects[i] == THYSTAME;
33 }
34 8 return info;
35 }
36
37 8 static bool check_requirements_met(
38 const info_map_t resource_count,
39 int players_on_tile,
40 size_t required_level
41 )
42 {
43 8 const int *req = required_resources[required_level - 1];
44
45 return (
46 15 players_on_tile >= req[0] &&
47
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 resource_count.linemate >= req[1] &&
48
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 resource_count.deraumere >= req[2] &&
49
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 resource_count.sibur >= req[3] &&
50
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 resource_count.mendiane >= req[4] &&
51
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
14 resource_count.phiras >= req[5] &&
52
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 resource_count.thystame >= req[6]
53 );
54 }
55
56 24 static void remove_resource_from_tile(
57 tile_t *tile,
58 size_t resource_type,
59 size_t *resource_count
60 )
61 {
62
2/2
✓ Branch 0 taken 134 times.
✓ Branch 1 taken 9 times.
143 for (size_t j = 0; j < tile->num_objects; j++) {
63
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 119 times.
134 if (tile->objects[j] == resource_type) {
64 15 tile->objects[j] = tile->objects[tile->num_objects - 1];
65 15 resource_count[resource_type]--;
66 15 break;
67 }
68 }
69 24 }
70
71 3 static void remove_resources(
72 tile_t *tile,
73 info_map_t resources,
74 size_t required_level
75 )
76 {
77 3 size_t resource_count[7] = {
78 3 resources.food,
79 3 resources.linemate,
80 3 resources.deraumere,
81 3 resources.sibur,
82 3 resources.mendiane,
83 3 resources.phiras,
84 3 resources.thystame
85 };
86
87
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 3 times.
24 for (size_t i = 0; i < 7; i++) {
88 for (int j = 0;
89
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 21 times.
45 j < required_resources[required_level - 1][i]; j++) {
90 24 remove_resource_from_tile(tile, i, resource_count);
91 }
92 }
93 3 }
94
95 8 static size_t get_nb_players_on_tile(client_t *client, server_t *server)
96 {
97 size_t players_on_tile = 0;
98 client_list_t *client_entry = NULL;
99
100
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 if (client == NULL || TAILQ_EMPTY(&server->clients))
101 return 0;
102
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 8 times.
29 TAILQ_FOREACH(client_entry, &server->clients, entries) {
103
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (client_entry->client
104
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 && client_entry->client->is_graphic == false
105 && client_entry->client->x == client->x
106
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 && client_entry->client->y == client->y
107
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 && client_entry->client->level == client->level)
108 21 players_on_tile++;
109 }
110 return players_on_tile;
111 }
112
113 12 static void run_logic_on_group(
114 client_t *client,
115 server_t *server,
116 size_t required_level,
117 void (*func)(client_t *client, server_t *server)
118 )
119 {
120 client_list_t *client_entry = NULL;
121
122
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 12 times.
48 TAILQ_FOREACH(client_entry, &server->clients, entries) {
123
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 if (client_entry->client
124
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 && client_entry->client->is_graphic == false
125 && client_entry->client->x == client->x
126
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 && client_entry->client->y == client->y
127
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 && client_entry->client->level == required_level)
128 36 func(client_entry->client, server);
129 }
130 12 }
131
132 8 static bool are_requierment_met_encapsulation(
133 client_t *client,
134 info_map_t resource_count,
135 size_t players_on_tile,
136 size_t required_level
137 )
138 {
139
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if (!check_requirements_met(
140 resource_count,
141 players_on_tile,
142 required_level)
143 ) {
144 2 dprintf(client->fd, "ko\n");
145 2 client->is_incanting = false;
146 2 return false;
147 }
148 return true;
149 }
150
151 5 void incantation(client_t *client, server_t *server)
152 {
153 5 info_map_t resource_count = get_tile(server, client->x, client->y);
154 5 size_t players_on_tile = get_nb_players_on_tile(client, server);
155
156
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 3 times.
5 if (!are_requierment_met_encapsulation(client, resource_count,
157 players_on_tile, client->level))
158 2 return;
159 3 run_logic_on_group(client, server, client->level, callback_freeze);
160 3 run_logic_on_group(client, server, client->level,
161 callback_start_incantation_set_payload);
162 3 send_start_incantation_to_graphicals(client, server);
163 3 client_time_handler(client, INCANTATION);
164 }
165
166 3 void incantation_callback_end_of_command(client_t *c, server_t *s)
167 {
168 3 info_map_t resource_count = get_tile(s, c->x, c->y);
169 3 size_t players_on_tile = get_nb_players_on_tile(c, s);
170 3 tile_t *tile = &s->map[c->x + c->y * s->proprieties.width];
171
172
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (c->is_incanting == false)
173 return;
174
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (!are_requierment_met_encapsulation(c, resource_count,
175 players_on_tile, c->level)) {
176 run_logic_on_group(c, s, c->level, callback_unfreeze);
177 message_to_graphicals(s, "pie %hhd %hhd %d\n", c->x, c->y, 0);
178 return;
179 }
180 3 remove_resources(tile, resource_count, c->level);
181 3 run_logic_on_group(c, s, c->level, callback_level_up);
182 3 run_logic_on_group(c, s, c->level,
183 callback_end_incantation_set_payload);
184 3 message_to_graphicals(s, "pie %hhd %hhd %d\n", c->x, c->y, 1);
185
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (c->level == LAST_LEVEL)
186 1 message_to_graphicals(s, "seg %s\n", c->team_name);
187 }
188