Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
** EPITECH PROJECT, 2024 |
3 |
|
|
** zappy |
4 |
|
|
** File description: |
5 |
|
|
** eject.c |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
#include "client.h" |
9 |
|
|
#include "server.h" |
10 |
|
|
|
11 |
|
✗ |
static void purge_eggs_from_team(team_t *team, int x, int y) |
12 |
|
|
{ |
13 |
|
|
eggs_list_t *egg; |
14 |
|
|
|
15 |
|
✗ |
TAILQ_FOREACH(egg, &team->eggs, entries) { |
16 |
|
✗ |
if (egg->egg->x == x && egg->egg->y == y) { |
17 |
|
✗ |
TAILQ_REMOVE(&team->eggs, egg, entries); |
18 |
|
✗ |
free(egg->egg); |
19 |
|
✗ |
free(egg); |
20 |
|
✗ |
team->nb_eggs--; |
21 |
|
|
} |
22 |
|
|
} |
23 |
|
✗ |
} |
24 |
|
|
|
25 |
|
1 |
static void delete_eggs_on_tile(server_t *s, int x, int y) |
26 |
|
|
{ |
27 |
|
|
team_list_t *team; |
28 |
|
|
|
29 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 |
TAILQ_FOREACH(team, &s->teams, entries) |
30 |
|
✗ |
purge_eggs_from_team(team->team, x, y); |
31 |
|
1 |
} |
32 |
|
|
|
33 |
|
1 |
static int get_orientation_to_tile(int x, int y, int x2, int y2) |
34 |
|
|
{ |
35 |
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 |
if (x == x2 && y == y2 - 1) |
36 |
|
|
return NORTH; |
37 |
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 |
if (x == x2 && y == y2 + 1) |
38 |
|
|
return SOUTH; |
39 |
|
✗ |
if (x == x2 - 1 && y == y2) |
40 |
|
|
return WEST; |
41 |
|
✗ |
if (x == x2 + 1 && y == y2) |
42 |
|
✗ |
return EAST; |
43 |
|
|
return -1; |
44 |
|
|
} |
45 |
|
|
|
46 |
|
1 |
static int set_dx(unsigned char orientation) |
47 |
|
|
{ |
48 |
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 |
if (orientation == WEST) |
49 |
|
|
return -1; |
50 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 |
if (orientation == EAST) |
51 |
|
✗ |
return 1; |
52 |
|
|
return 0; |
53 |
|
|
} |
54 |
|
|
|
55 |
|
1 |
static int set_dy(unsigned char orientation) |
56 |
|
|
{ |
57 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 |
if (orientation == NORTH) |
58 |
|
|
return -1; |
59 |
|
✗ |
if (orientation == SOUTH) |
60 |
|
✗ |
return 1; |
61 |
|
|
return 0; |
62 |
|
|
} |
63 |
|
|
|
64 |
|
1 |
void send_data_to_graphiscs(server_t *s, client_list_t *n) |
65 |
|
|
{ |
66 |
|
1 |
message_to_graphicals(s, "pex %d\n", n->client->id); |
67 |
|
1 |
message_to_graphicals(s, "ppo %d %d %d %d\n", n->client->id, |
68 |
|
1 |
n->client->x, n->client->y, n->client->orientation); |
69 |
|
1 |
} |
70 |
|
|
|
71 |
|
1 |
void eject(client_t *c, server_t *s) |
72 |
|
|
{ |
73 |
|
1 |
int width = s->proprieties.width; |
74 |
|
1 |
int height = s->proprieties.height; |
75 |
|
1 |
signed char dx = set_dx(c->orientation); |
76 |
|
1 |
signed char dy = set_dy(c->orientation); |
77 |
|
|
|
78 |
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1 times.
|
13 |
for (client_list_t *n = TAILQ_FIRST(&s->clients); n != NULL; |
79 |
|
12 |
n = TAILQ_NEXT(n, entries)) { |
80 |
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
|
12 |
if (n->client->x == c->x && n->client->y == c->y |
81 |
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 |
&& n->client->id != c->id) { |
82 |
|
1 |
n->client->x = (n->client->x + dx + width) % width; |
83 |
|
1 |
n->client->y = (n->client->y + dy + height) % height; |
84 |
|
1 |
dprintf(n->client->fd, "eject: %d\n", get_orientation_to_tile( |
85 |
|
1 |
c->x, c->y, n->client->x, n->client->y)); |
86 |
|
1 |
send_data_to_graphiscs(s, n); |
87 |
|
|
} |
88 |
|
|
} |
89 |
|
1 |
delete_eggs_on_tile(s, c->x, c->y); |
90 |
|
1 |
handle_response(&c->payload, "ok\n"); |
91 |
|
1 |
client_time_handler(c, EJECT); |
92 |
|
1 |
} |
93 |
|
|
|