]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/client_commands.c
Add oceans and toggle keys for flight & collision
[dragonblocks_alpha.git] / src / client / client_commands.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include "client/client.h"
4 #include "client/client_map.h"
5 #include "types.h"
6 #include "util.h"
7
8 static bool disconnect_handler(unused Client *client, bool good)
9 {
10         if (good)
11                 client_disconnect(false, NULL);
12         return true;
13 }
14
15 static bool auth_handler(Client *client, bool good)
16 {
17         u8 success;
18         if (! read_u8(client->fd, &success))
19                 return false;
20
21         if (! good)
22                 return true;
23
24         if (success) {
25                 printf("Authenticated successfully\n");
26                 client->state = CS_ACTIVE;
27         } else {
28                 printf("Authentication failed, please try again\n");
29                 client->state = CS_CREATED;
30         }
31
32         return true;
33 }
34
35 static bool block_handler(Client *client, bool good)
36 {
37         v3s32 pos;
38
39         if (! read_v3s32(client->fd, &pos))
40                 return false;
41
42         size_t size;
43
44         if (! read_u64(client->fd, &size))
45                 return false;
46
47         if (size > sizeof(MapBlockData))        // guard to prevent malicious or malfunctioning packets from allocating huge unnecessary amounts of memory
48                 return false;
49
50         char data[size];
51         if (! read_full(client->fd, data, size))
52                 return false;
53
54         MapBlock *block;
55
56         if (good)
57                 block = map_get_block(client_map.map, pos, true);
58         else
59                 block = map_allocate_block(pos);
60
61         bool ret = map_deserialize_block(block, data, size);
62
63         if (good)
64                 client_map_block_received(block);
65         else
66                 map_free_block(block);
67
68         return ret;
69 }
70
71 static bool simulation_distance_handler(Client *client, bool good)
72 {
73         u32 simulation_distance;
74
75         if (! read_u32(client->fd, &simulation_distance))
76                 return false;
77
78         if (good)
79                 client_map_set_simulation_distance(simulation_distance);
80
81         return true;
82 }
83
84 CommandHandler command_handlers[CLIENT_COMMAND_COUNT] = {
85         {0},
86         {&disconnect_handler, "DISCONNECT", CS_CREATED | CS_AUTH | CS_ACTIVE},
87         {&auth_handler, "AUTH", CS_AUTH},
88         {&block_handler, "BLOCK", CS_ACTIVE},
89         {&simulation_distance_handler, "SIMULATION_DISTANCE", CS_ACTIVE},
90 };