Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
** EPITECH PROJECT, 2024 |
3 |
|
|
** zappy |
4 |
|
|
** File description: |
5 |
|
|
** look.c |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
#include "client.h" |
9 |
|
|
#include "server.h" |
10 |
|
|
#include <stdlib.h> |
11 |
|
|
#include <string.h> |
12 |
|
|
#include <stdio.h> |
13 |
|
|
|
14 |
|
|
static const struct { |
15 |
|
|
object_t type; |
16 |
|
|
char *name; |
17 |
|
|
} object_handlers[] = { |
18 |
|
|
{FOOD, "food"}, |
19 |
|
|
{LINEMATE, "linemate"}, |
20 |
|
|
{DERAUMERE, "deraumere"}, |
21 |
|
|
{SIBUR, "sibur"}, |
22 |
|
|
{MENDIANE, "mendiane"}, |
23 |
|
|
{PHIRAS, "phiras"}, |
24 |
|
|
{THYSTAME, "thystame"}, |
25 |
|
|
{PLAYER, "player"}, |
26 |
|
|
{EMPTY, ""}, |
27 |
|
|
{0, NULL} |
28 |
|
|
}; |
29 |
|
|
|
30 |
|
|
static const int dx[] = {0, 1, 0, -1}; |
31 |
|
|
static const int dy[] = {-1, 0, 1, 0}; |
32 |
|
|
|
33 |
|
1 |
static tile_t *copy_map(tile_t *dest, tile_t *src, server_t *server) |
34 |
|
|
{ |
35 |
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 |
if (dest == NULL) |
36 |
|
|
return NULL; |
37 |
|
101 |
for (int i = 0; i < server-> |
38 |
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 1 times.
|
101 |
proprieties.width * server->proprieties.height; i++) { |
39 |
|
100 |
dest[i].num_objects = src[i].num_objects; |
40 |
|
100 |
dest[i].objects = malloc(sizeof(object_t) * src[i].num_objects); |
41 |
1/2
✓ Branch 0 taken 100 times.
✗ Branch 1 not taken.
|
100 |
if (dest[i].objects == NULL) |
42 |
|
|
return NULL; |
43 |
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 100 times.
|
103 |
for (size_t j = 0; j < src[i].num_objects; j++) |
44 |
|
3 |
dest[i].objects[j] = src[i].objects[j]; |
45 |
|
|
} |
46 |
|
|
return dest; |
47 |
|
|
} |
48 |
|
|
|
49 |
|
1 |
static void populate_map_with_players(tile_t *map, server_t *server) |
50 |
|
|
{ |
51 |
|
|
client_t *client; |
52 |
|
|
int index; |
53 |
|
|
|
54 |
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 |
for (client_list_t *tmp = TAILQ_FIRST(&server->clients); tmp != NULL; |
55 |
|
1 |
tmp = TAILQ_NEXT(tmp, entries)) { |
56 |
|
1 |
client = tmp->client; |
57 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 |
if (client->is_graphic) |
58 |
|
✗ |
continue; |
59 |
|
1 |
index = client->y * server->proprieties.width + client->x; |
60 |
|
1 |
map[index].num_objects++; |
61 |
|
1 |
map[index].objects = realloc(map[index].objects, |
62 |
|
|
map[index].num_objects * sizeof(object_t)); |
63 |
|
1 |
map[index].objects[map[index].num_objects - 1] = PLAYER; |
64 |
|
|
} |
65 |
|
1 |
} |
66 |
|
|
|
67 |
|
16 |
static void check_object_in_lookup_table(object_t object, char **tile_payload) |
68 |
|
|
{ |
69 |
|
|
int ret_val = 0; |
70 |
|
16 |
char *new_tile_payload; |
71 |
|
|
|
72 |
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 |
for (size_t i = 0; object_handlers[i].name != NULL; i++) { |
73 |
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 40 times.
|
56 |
if (object_handlers[i].type == object) { |
74 |
|
16 |
ret_val = asprintf(&new_tile_payload, "%s%s%s", |
75 |
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12 times.
|
16 |
*tile_payload, *tile_payload[0] ? " " : "", |
76 |
|
|
object_handlers[i].name); |
77 |
|
|
break; |
78 |
|
|
} |
79 |
|
|
} |
80 |
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
|
16 |
if (ret_val != -1) { |
81 |
|
16 |
free(*tile_payload); |
82 |
|
16 |
*tile_payload = new_tile_payload; |
83 |
|
|
} |
84 |
|
16 |
} |
85 |
|
|
|
86 |
|
4 |
static void object_to_string(tile_t tile, char **tile_payload) |
87 |
|
|
{ |
88 |
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
|
20 |
for (size_t i = 0; i < tile.num_objects; i++) { |
89 |
|
16 |
check_object_in_lookup_table(tile.objects[i], tile_payload); |
90 |
|
|
} |
91 |
|
4 |
} |
92 |
|
|
|
93 |
|
4 |
static void append_tile_to_payload(client_t *client, tile_t *map, int index) |
94 |
|
|
{ |
95 |
|
4 |
char *tile_payload = strdup(""); |
96 |
|
4 |
char *new_payload; |
97 |
|
|
|
98 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 |
if (tile_payload == NULL) |
99 |
|
✗ |
return; |
100 |
|
4 |
object_to_string(map[index], &tile_payload); |
101 |
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 |
if (asprintf(&new_payload, "%s%s%s", |
102 |
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
|
4 |
client->payload, client->payload[1] ? "," : "", tile_payload) != -1) { |
103 |
|
4 |
free(client->payload); |
104 |
|
4 |
client->payload = new_payload; |
105 |
|
|
} |
106 |
|
4 |
free(tile_payload); |
107 |
|
|
} |
108 |
|
|
|
109 |
|
1 |
static void handle_look(client_t *c, server_t *server, tile_t *map) |
110 |
|
|
{ |
111 |
|
1 |
int w = server->proprieties.width; |
112 |
|
1 |
int h = server->proprieties.height; |
113 |
|
1 |
int d = c->orientation - 1; |
114 |
|
|
int look_y; |
115 |
|
|
int look_x; |
116 |
|
|
|
117 |
|
1 |
free(c->payload); |
118 |
|
1 |
c->payload = strdup("["); |
119 |
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 |
if (c->payload == NULL) |
120 |
|
|
return; |
121 |
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 |
for (int level = 0; level <= (int)c->level; level++) { |
122 |
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
|
6 |
for (int offset = -level; offset <= level; offset++) { |
123 |
|
4 |
look_x = (c->x + dx[d] * level + dy[d] * offset + w) % w; |
124 |
|
4 |
look_y = (c->y + dy[d] * level - dx[d] * offset + h) % h; |
125 |
|
4 |
append_tile_to_payload(c, map, (look_y * w + look_x)); |
126 |
|
|
} |
127 |
|
|
} |
128 |
|
1 |
handle_response(&c->payload, "%s]\n", c->payload); |
129 |
|
|
} |
130 |
|
|
|
131 |
|
1 |
void look(client_t *c, server_t *server) |
132 |
|
|
{ |
133 |
|
1 |
tile_t *map = copy_map(calloc(server |
134 |
|
1 |
->proprieties.width * server->proprieties.height, |
135 |
|
|
sizeof(tile_t)), server->map, server); |
136 |
|
|
|
137 |
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 |
if (map == NULL) |
138 |
|
|
return; |
139 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 |
if (c->tclient[NB_REQUESTS_HANDLEABLE - 1].available_request == true) { |
140 |
|
✗ |
handle_response(&c->payload, "ko\n"); |
141 |
|
✗ |
client_time_handler(c, LOOK); |
142 |
|
✗ |
return; |
143 |
|
|
} |
144 |
|
1 |
populate_map_with_players(map, server); |
145 |
|
1 |
handle_look(c, server, map); |
146 |
|
1 |
for (int i = 0; i < server->proprieties.width * |
147 |
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 1 times.
|
101 |
server->proprieties.height; i++) |
148 |
|
100 |
free(map[i].objects); |
149 |
|
1 |
free(map); |
150 |
|
1 |
client_time_handler(c, LOOK); |
151 |
|
|
} |
152 |
|
|
|