-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.c
132 lines (108 loc) · 3.38 KB
/
client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <termios.h>
#include <unistd.h>
#include "config.h"
#include "game/entity.h"
#include "game/world.h"
#include "util/api.h"
#include "util/socket.h"
#define ERROR(msg) \
{ \
perror(msg); \
exit(EXIT_FAILURE); \
}
World world;
bool connected = true;
pthread_t render_thread;
int client_socket;
void render() {
char map_data[MAP_DATA_SIZE(world)];
Player *player = malloc(sizeof(Player));
while (connected) {
sendCommand(client_socket, MAP, 0);
memset(&map_data, 0, sizeof(map_data));
recv(client_socket, map_data, sizeof(map_data), 0);
loadMapData(map_data, &world, player);
system("clear");
printWorld(world);
printOnePlayerDetails(player);
sleep(1);
}
pthread_exit(NULL);
}
int main() {
client_socket = getClientSocket(HOST, PORT);
if (client_socket == -1) {
ERROR("Error opening socket")
} else if (client_socket == -2) {
ERROR("Could not find host")
} else if (client_socket == -3) {
ERROR("Error connecting to socket")
}
char buffer[1024] = { 0 };
// Join the game
sendCommand(client_socket, JOIN, HUMAN);
recv(client_socket, buffer, sizeof(buffer), 0);
if ((Response)buffer[0] == SERVER_FULL) {
ERROR("Server is full, cannot join")
}
// Get world info and build an empty world
char world_size[2];
sendCommand(client_socket, WORLD_SIZE, 0);
recv(client_socket, world_size, sizeof(world_size), 0);
// World size is sent as 1 lower, so we need to adjust for it here
world = emptyWorld((int)world_size[0] + 1, (int)world_size[1] + 1);
populateWorldWithAir(&world);
// Render loop
pthread_create(&render_thread, NULL, (void *)render, NULL);
// Keyboard handler
struct termios old_term_settings, new_term_settings;
tcgetattr(STDIN_FILENO, &old_term_settings);
new_term_settings = old_term_settings;
// Disable terminal canonical mode
// This sends stdin immediately without buffering
new_term_settings.c_lflag &= (~ICANON & ~ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &new_term_settings);
while (connected) {
unsigned char key = getchar();
switch (key) {
case 'Q':
case 'q':
sendCommand(client_socket, LEAVE, 0);
connected = false;
break;
case 'd':
sendCommand(client_socket, MOVE, RIGHT);
break;
case 'a':
sendCommand(client_socket, MOVE, LEFT);
break;
case 's':
sendCommand(client_socket, MOVE, DOWN);
break;
case 'w':
sendCommand(client_socket, MOVE, UP);
break;
default:
continue;
}
// Wait for a response
memset(buffer, 0, sizeof(buffer));
recv(client_socket, buffer, sizeof(buffer), 0);
Response response = (Response)buffer[0];
if (response == REMOVED) {
connected = false;
}
usleep(10000);
}
// Clean up
tcsetattr(STDIN_FILENO, TCSANOW, &old_term_settings);
close(client_socket);
printf("Disconnected\n");
return 0;
}