Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
** EPITECH PROJECT, 2024 |
3 |
|
|
** zappy |
4 |
|
|
** File description: |
5 |
|
|
** broadcast.c |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
#include "server.h" |
9 |
|
|
#include <math.h> |
10 |
|
|
|
11 |
|
50 |
static double adjust_angle_receiver_rotation(client_t *receiver, int angle) |
12 |
|
|
{ |
13 |
1/5
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 50 times.
|
50 |
switch (receiver->orientation) { |
14 |
|
✗ |
case NORTH: |
15 |
|
✗ |
angle = fmod(angle + 360, 360); |
16 |
|
✗ |
break; |
17 |
|
✗ |
case EAST: |
18 |
|
✗ |
angle = fmod(angle + 90, 360); |
19 |
|
✗ |
break; |
20 |
|
✗ |
case SOUTH: |
21 |
|
✗ |
angle = fmod(angle + 180, 360); |
22 |
|
✗ |
break; |
23 |
|
✗ |
case WEST: |
24 |
|
✗ |
angle = fmod(angle + 270, 360); |
25 |
|
✗ |
break; |
26 |
|
|
} |
27 |
|
50 |
return angle; |
28 |
|
|
} |
29 |
|
|
|
30 |
|
50 |
static int get_sound_direction( |
31 |
|
|
client_t *sender, |
32 |
|
|
client_t *receiver, |
33 |
|
|
int map_width, |
34 |
|
|
int map_height |
35 |
|
|
) |
36 |
|
|
{ |
37 |
|
50 |
int dx = sender->x - receiver->x; |
38 |
|
50 |
int dy = sender->y - receiver->y; |
39 |
|
|
double angle; |
40 |
|
50 |
const int direction_lookup[8] = {1, 2, 3, 4, 5, 6, 7, 8}; |
41 |
|
|
|
42 |
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 40 times.
|
50 |
if (abs(dx) > map_width / 2) |
43 |
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 |
dx = (dx > 0) ? dx - map_width : dx + map_width; |
44 |
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 40 times.
|
50 |
if (abs(dy) > map_height / 2) |
45 |
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 |
dy = (dy > 0) ? dy - map_height : dy + map_height; |
46 |
|
50 |
angle = atan2(dy, dx) * 180 / M_PI; |
47 |
|
50 |
angle = adjust_angle_receiver_rotation(receiver, angle); |
48 |
1/2
✓ Branch 0 taken 140 times.
✗ Branch 1 not taken.
|
140 |
for (int i = 0; i < 8; i++) { |
49 |
3/4
✓ Branch 0 taken 140 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 70 times.
✓ Branch 3 taken 70 times.
|
140 |
if ((angle >= 337.5 + i * 45 || angle < 22.5 + i * 45) |
50 |
3/4
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
|
70 |
&& (i != 0 || angle >= 337.5)) |
51 |
|
50 |
return direction_lookup[i]; |
52 |
|
|
} |
53 |
|
|
return 0; |
54 |
|
|
} |
55 |
|
|
|
56 |
|
5 |
static void send_broadcast_to_graphicals(client_t *client, server_t *server) |
57 |
|
|
{ |
58 |
|
|
client_list_t *client_list_entry; |
59 |
|
|
client_t *receiver; |
60 |
|
|
|
61 |
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 |
if (client->commands[1] == NULL) |
62 |
|
|
return; |
63 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 |
if (server->proprieties.is_iteration == true && |
64 |
|
✗ |
strcmp(client->commands[1], "STOP") == 0) |
65 |
|
✗ |
exit(OK_STATUS); |
66 |
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 5 times.
|
60 |
TAILQ_FOREACH(client_list_entry, &server->clients, entries) { |
67 |
|
55 |
receiver = client_list_entry->client; |
68 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
|
55 |
if (receiver->is_graphic) |
69 |
|
✗ |
dprintf(receiver->fd, "pbc %d %s\n", |
70 |
|
✗ |
client->id, client->commands[1]); |
71 |
|
|
} |
72 |
|
|
} |
73 |
|
|
|
74 |
|
5 |
void broadcast(client_t *client, server_t *server) |
75 |
|
|
{ |
76 |
|
|
int d; |
77 |
|
|
client_t *receiver; |
78 |
|
|
|
79 |
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 5 times.
|
60 |
for (client_list_t *tmp = TAILQ_FIRST(&server->clients); tmp != NULL; |
80 |
|
55 |
tmp = TAILQ_NEXT(tmp, entries)) { |
81 |
|
55 |
receiver = tmp->client; |
82 |
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 50 times.
|
55 |
if (receiver == client) |
83 |
|
5 |
continue; |
84 |
|
50 |
d = get_sound_direction( |
85 |
|
|
client, |
86 |
|
|
receiver, |
87 |
|
|
server->proprieties.width, |
88 |
|
|
server->proprieties.height |
89 |
|
|
); |
90 |
|
50 |
dprintf(receiver->fd, "message %d, %s\n", d, client->commands[1]); |
91 |
|
|
} |
92 |
|
5 |
handle_response(&client->payload, "ok\n"); |
93 |
|
5 |
send_broadcast_to_graphicals(client, server); |
94 |
|
5 |
client_time_handler(client, BROADCAST); |
95 |
|
5 |
} |
96 |
|
|
|