]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/client_commands.c
Add trees
[dragonblocks_alpha.git] / src / client / client_commands.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <dragontype/number.h>
4 #include "client/client.h"
5 #include "client/client_map.h"
6 #include "client/client_player.h"
7 #include "day.h"
8 #include "perlin.h"
9 #include "util.h"
10
11 static bool disconnect_handler(unused Client *client, bool good)
12 {
13         if (good)
14                 client_disconnect(false, NULL);
15         return true;
16 }
17
18 static bool auth_handler(Client *client, bool good)
19 {
20         u8 success;
21         if (! read_u8(client->fd, &success))
22                 return false;
23
24         if (! good)
25                 return true;
26
27         if (success) {
28                 printf("Authenticated successfully\n");
29                 client->state = CS_ACTIVE;
30         } else {
31                 printf("Authentication failed, please try again\n");
32                 client->state = CS_CREATED;
33         }
34
35         return true;
36 }
37
38 static bool block_handler(Client *client, bool good)
39 {
40         v3s32 pos;
41
42         if (! read_v3s32(client->fd, &pos))
43                 return false;
44
45         u64 size;
46
47         if (! read_u64(client->fd, &size))
48                 return false;
49
50         if (size > 1 << 16)
51                 return false;
52
53         u64 rawsize;
54
55         if (! read_u64(client->fd, &rawsize))
56                 return false;
57
58         char data[size];
59         if (! read_full(client->fd, data, size))
60                 return false;
61
62         MapBlock *block;
63
64         if (good)
65                 block = map_get_block(client_map.map, pos, true);
66         else
67                 block = map_allocate_block(pos);
68
69         bool ret = map_deserialize_block(block, data, size, rawsize);
70
71         if (good)
72                 client_map_block_received(block);
73         else
74                 map_free_block(block);
75
76         return ret;
77 }
78
79 static bool info_handler(Client *client, bool good)
80 {
81         u32 simulation_distance;
82         s32 server_seed;
83
84         if (! (read_u32(client->fd, &simulation_distance) && read_s32(client->fd, &server_seed)))
85                 return false;
86
87         if (good) {
88                 client_map_set_simulation_distance(simulation_distance);
89                 seed = server_seed;
90         }
91
92         return true;
93 }
94
95 static bool setpos_handler(Client *client, bool good)
96 {
97         v3f64 pos;
98
99         if (! read_v3f64(client->fd, &pos))
100                 return false;
101
102         if (good)
103                 client_player_set_position(pos);
104
105         return true;
106 }
107
108 static bool timeofday_handler(Client *client, bool good)
109 {
110         u64 time_of_day;
111
112         if (! read_u64(client->fd, &time_of_day))
113                 return false;
114
115         if (good)
116                 set_time_of_day(time_of_day);
117
118         return true;
119 }
120
121 CommandHandler command_handlers[CLIENT_COMMAND_COUNT] = {
122         {0},
123         {&disconnect_handler, "DISCONNECT", CS_CREATED | CS_AUTH | CS_ACTIVE},
124         {&auth_handler, "AUTH", CS_AUTH},
125         {&block_handler, "BLOCK", CS_ACTIVE},
126         {&info_handler, "INFO", CS_ACTIVE},
127         {&setpos_handler, "SETPOS", CS_ACTIVE},
128         {&timeofday_handler, "TIMEOFDAY", CS_ACTIVE},
129 };