]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/client.c
88bcded37fa0c1c923b0f5308fb83c14c047601f
[dragonblocks_alpha.git] / src / client / client.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include "client/client.h"
6 #include "client/client_auth.h"
7 #include "client/client_map.h"
8 #include "client/client_player.h"
9 #include "client/game.h"
10 #include "client/input.h"
11 #include "day.h"
12 #include "signal_handlers.h"
13 #include "perlin.h"
14 #include "types.h"
15 #include "util.h"
16
17 DragonnetPeer *client;
18
19 static bool finished = false;
20
21 static bool on_recv(unused DragonnetPeer *peer, DragonnetTypeId type, unused void *pkt)
22 {
23         while (client_auth.state == AUTH_INIT)
24                 ;
25
26         return (client_auth.state == AUTH_WAIT) == (type == DRAGONNET_TYPE_ToClientAuth);
27 }
28
29 static void on_disconnect(unused DragonnetPeer *peer)
30 {
31         interrupted = true;
32         while (! finished)
33                 ;
34 }
35
36 static void on_ToClientAuth(unused DragonnetPeer *peer, ToClientAuth *pkt)
37 {
38         if (pkt->success) {
39                 client_auth.state = AUTH_SUCCESS;
40                 printf("Authenticated successfully\n");
41         } else {
42                 client_auth.state = AUTH_INIT;
43                 printf("Authentication failed, please try again\n");
44         }
45 }
46
47 static void on_ToClientBlock(unused DragonnetPeer *peer, ToClientBlock *pkt)
48 {
49         MapBlock *block = map_get_block(client_map.map, pkt->pos, true);
50
51         map_deserialize_block(block, pkt->data);
52         client_map_block_received(block);
53 }
54
55 static void on_ToClientInfo(unused DragonnetPeer *peer, ToClientInfo *pkt)
56 {
57         client_map_set_simulation_distance(pkt->simulation_distance);
58         seed = pkt->seed;
59 }
60
61 static void on_ToClientPos(unused DragonnetPeer *peer, ToClientPos *pkt)
62 {
63         client_player_set_position(pkt->pos);
64 }
65
66 static void on_ToClientTimeOfDay(unused DragonnetPeer *peer, ToClientTimeOfDay *pkt)
67 {
68         set_time_of_day(pkt->time_of_day);
69 }
70
71 int main(int argc, char **argv)
72 {
73         if (argc < 2) {
74                 fprintf(stderr, "Missing address\n");
75                 return EXIT_FAILURE;
76         }
77
78         if (! (client = dragonnet_connect(argv[1]))) {
79                 fprintf(stderr, "Failed to connect to server\n");
80                 return EXIT_FAILURE;
81         }
82
83         client->on_disconnect = &on_disconnect;
84         client->on_recv = &on_recv;
85         client->on_recv_type[DRAGONNET_TYPE_ToClientAuth     ] = (void *) &on_ToClientAuth;
86         client->on_recv_type[DRAGONNET_TYPE_ToClientBlock    ] = (void *) &on_ToClientBlock;
87         client->on_recv_type[DRAGONNET_TYPE_ToClientInfo     ] = (void *) &on_ToClientInfo;
88         client->on_recv_type[DRAGONNET_TYPE_ToClientPos      ] = (void *) &on_ToClientPos;
89         client->on_recv_type[DRAGONNET_TYPE_ToClientTimeOfDay] = (void *) &on_ToClientTimeOfDay;
90
91         signal_handlers_init();
92         client_map_init();
93         client_player_init();
94         dragonnet_peer_run(client);
95
96         if (! client_auth_init())
97                 return EXIT_FAILURE;
98
99         if (! game())
100                 return EXIT_FAILURE;
101
102         dragonnet_peer_shutdown(client);
103         client_auth_deinit();
104         client_player_deinit();
105         client_map_deinit();
106
107         pthread_t recv_thread = client->recv_thread;
108         finished = true;
109         pthread_join(recv_thread, NULL);
110
111         return EXIT_SUCCESS;
112 }