-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot-client.c
168 lines (138 loc) · 4.67 KB
/
bot-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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#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, keyboard_thread;
int client_socket;
void render() {
char map_data[MAP_DATA_SIZE(world)];
Player* player = malloc(sizeof(Player));
srand(time(NULL) * 100);
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);
Player* beast_in_sight = NULL;
// Find if any beast is in sight
for (int i = 0; i < MAX_BEASTS; i++) {
if (world.beasts[i] == NULL) {
continue;
}
beast_in_sight = world.beasts[i];
break;
}
Direction horizontal;
Direction vertical;
if (beast_in_sight != NULL) {
if (beast_in_sight->pos_y > player->pos_y) {
// Beast below player
vertical = UP;
} else if (beast_in_sight->pos_y < player->pos_y) {
// Beast above player
vertical = DOWN;
} else {
// Choose random vertical direction
vertical = (Direction)(rand() % 2 + 2);
}
if (beast_in_sight->pos_x > player->pos_x) {
// Beast to the right of the player
horizontal = LEFT;
} else if (beast_in_sight->pos_x < player->pos_x) {
// Beast to the left of the player
horizontal = RIGHT;
} else {
// Choose random horizontal direction
horizontal = (Direction)(rand() % 2);
}
} else {
// Move randomly
vertical = (Direction)(rand() % 2 + 2);
horizontal = (Direction)(rand() % 2);
}
// Randomly decide in which direction to run (vertically or horizontally)
int move_choice = rand() % 2;
if (move_choice == 0) {
sendCommand(client_socket, MOVE, vertical);
} else {
sendCommand(client_socket, MOVE, horizontal);
}
char buffer[1];
recv(client_socket, buffer, sizeof(buffer), 0);
// Print to screen
system("clear");
printWorld(world);
printOnePlayerDetails(player);
sleep(1);
}
pthread_exit(NULL);
}
void keyboardHandler() {
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();
if (key == 'q' || key == 'Q') {
sendCommand(client_socket, LEAVE, 0);
pthread_cancel(render_thread);
connected = false;
}
}
tcsetattr(STDIN_FILENO, TCSANOW, &old_term_settings);
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, CPU);
recv(client_socket, buffer, sizeof(buffer), 0);
if ((Response)buffer[0] == SERVER_FULL) {
ERROR("Server is full, cannot join")
}
// Get world info and build 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 (used only for quitting)
pthread_create(&keyboard_thread, NULL, (void*)keyboardHandler, NULL);
pthread_join(render_thread, NULL);
pthread_join(keyboard_thread, NULL);
// Clean up
close(client_socket);
printf("Disconnected\n");
return 0;
}