forked from DpEpsilon/DpJuice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overrun.c
365 lines (305 loc) · 7.92 KB
/
overrun.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "overrun.h"
#define FALSE 0
#define TRUE 1
#define BUFFER_SIZE 6000
#define COMMAND_SIZE 1024
#define TMP_SIZE 64
static int terrain[MAX_SIZE+2][MAX_SIZE+2];
static int minerals[MAX_PLAYERS+2];
static struct student
{
int pid, id, x, y, level;
} students[MAX_SIZE*MAX_SIZE+2];
static int total_students;
static int student_count[MAX_PLAYERS+2];
static int playernum, boardsize, playerid;
static char in_buffer[BUFFER_SIZE+10];
static char out_buffer[BUFFER_SIZE+10];
static char tmp[TMP_SIZE];
static int command_upto = 0;
static char command_buffer[COMMAND_SIZE];
static int name_set = FALSE;
static int has_built = FALSE;
static ssize_t packet_size;
static char ip_address[20];
static int vis_output = FALSE;
static int soc;
static int cmd_line_args(int args, char * argv[]);
static int connect_self();
static int process_packet(char *buffer);
static void clear_terrain();
int main(int argc, char * argv[])
{
strcpy(ip_address, "127.0.0.1");
if (cmd_line_args(argc, argv) == 1) return 1;
for (;;) // Reconnect loop
{
name_set = FALSE;
if (connect_self() == 1) return 1;
for (;;) // Packet processing loop
{
int prc_ret;
memset(in_buffer, 0, sizeof(char)*BUFFER_SIZE);
packet_size = recv(soc, in_buffer, BUFFER_SIZE, 0);
if (packet_size == -1)
{
fprintf(stderr, "error: problem in receiving packet\n");
fprintf(stderr, "Reconnecting...\n");
break;
}
if (packet_size == 0)
{
fprintf(stderr, "error: connection closed by host\n");
fprintf(stderr, "Reconnecting...\n");
break;
}
int buf_upto = 0;
while (in_buffer[buf_upto] != '\0') {
command_buffer[command_upto++] = in_buffer[buf_upto];
if (in_buffer[buf_upto] == '\n') {
command_buffer[command_upto] = '\0';
prc_ret = process_packet(command_buffer);
command_upto = 0;
}
buf_upto++;
}
if (prc_ret == 1)
{
fprintf(stderr, "Exiting...\n");
return 1;
}
if (prc_ret == 2)
{
fprintf(stderr, "Reconnecting...\n");
break;
}
}
}
return 0;
}
// Returns 2 if there is a need to reconnect,
// 1 if an fatal error occurs and 0 otherwise.
static int process_packet(char *buffer)
{
fprintf(stderr, "%s", buffer);
if (memcmp(buffer, "NAME PLEASE", 11) == 0) // NAME PLEASE
{
clientRegister(); // setName should be called within this
if (!name_set)
{
fprintf(stderr, "fatal: bot failed to set name\n");
return 1;
}
strcat(out_buffer, " -1 -1 -1\n");
if (send(soc, out_buffer, strlen(out_buffer), 0) == -1)
{
fprintf(stderr, "error: could not send data\n");
return 2;
}
} else if (memcmp(buffer, "NEWGAME", 7) == 0) {
// %*s because I'm cool like that
sscanf(buffer, "%*s %d %d %d %s",
&playernum, &boardsize, &playerid, tmp);
clientInit(playernum, boardsize, playerid);
strcpy(out_buffer, "READY ");
strncat(out_buffer, tmp, BUFFER_SIZE/4);
strcat(out_buffer, "\n");
if (send(soc, out_buffer, strlen(out_buffer), 0) == -1)
{
fprintf(stderr, "error: could not send data\n");
return 2;
}
} else if (memcmp(buffer, "GAMEOVER", 8) == 0) {
fprintf(stderr, buffer);
} else if (memcmp(buffer, "CELL", 4) == 0) {
int x, y, type;
sscanf(buffer, "%*s %d %d %d", &x, &y, &type);
terrain[x][y] = type;
} else if (memcmp(buffer, "MINERALS", 8) == 0) {
int pid, count;
sscanf(buffer, "%*s %d %d", &pid, &count);
minerals[pid] = count;
} else if (memcmp(buffer, "LOCATION", 8) == 0) {
int pid, id, x, y, level;
sscanf(buffer, "%*s %d %d %d %d %d", &pid, &id, &x, &y, &level);
students[total_students].pid = pid;
students[total_students].id = id;
students[total_students].x = x;
students[total_students].y = y;
students[total_students].level = level;
student_count[pid]++;
total_students++;
} else if (memcmp(buffer, "YOURMOVE", 8) == 0) {
int x, y, i;
// Terrain info
for (x = 0; x < boardsize; x++)
{
for (y = 0; y < boardsize; y++)
{
clientTerrainInfo(x, y, terrain[x][y]);
}
}
// Juice info
for (i = 1; i <= playernum; i++) // Yes, pid starts from 1
{
clientJuiceInfo(i, minerals[i]);
}
for (i = total_students-1; i >= 0; i--)
{
clientStudentLocation(students[i].pid, students[i].id,
students[i].x, students[i].y,
students[i].level);
}
has_built = FALSE;
// This is the dangerous bit:
clientDoTurn();
if (!has_built) {
build(0);
}
// TODO: Send back build information (since we only build once)
// But we should be fine with sending it within build() for now
// Reset student count
memset(student_count, 0, sizeof(int)*MAX_PLAYERS+2);
total_students = 0;
} else {
fprintf(stderr, "WARNING: unknown header:\n");
}
return 0;
}
void setName(const char* name)
{
if (!name_set)
{
strcpy(out_buffer, "NAME ");
// Might as well set a rough limit,
// since the server will have a further limit
strncat(out_buffer, name, BUFFER_SIZE/4);
name_set = TRUE;
}
else
{
fprintf(stderr, "WARNING: client called setName() more than once\n");
}
}
void move(int uid, int move)
{
strcpy(out_buffer, "MOVE ");
snprintf(tmp, BUFFER_SIZE/4, "%d ", uid);
strncat(out_buffer, tmp, BUFFER_SIZE/4);
snprintf(tmp, BUFFER_SIZE/4, "%d\n", move);
strncat(out_buffer, tmp, BUFFER_SIZE/4);
if (send(soc, out_buffer, strlen(out_buffer), 0) == -1)
{
fprintf(stderr, "error: could not send data\n");
}
}
void build(int cost)
{
if (has_built) {
fprintf(stderr, "WARNING: client called build() more than once "
"in this turn\n");
}
has_built = TRUE;
strcpy(out_buffer, "BUILD ");
snprintf(tmp, BUFFER_SIZE/4, "%d\n", cost);
strncat(out_buffer, tmp, BUFFER_SIZE/4);
if (send(soc, out_buffer, strlen(out_buffer), 0) == -1)
{
fprintf(stderr, "error: could not send data\n");
}
}
// Who the hell put these functions in the header
int getCost(int level)
{
return level;
}
int getLevel(int cost)
{
return cost;
}
static int connect_self()
{
struct sockaddr_in sockAddr;
int Res; // ip in int form
soc = socket(AF_INET, SOCK_STREAM, 0);
if (soc == -1)
{
fprintf(stderr, "fatal: socket fail\n");
return 1;
}
memset(&sockAddr, 0, sizeof(sockAddr));
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(12317);
fprintf(stderr, "%s\n", ip_address);
Res = inet_pton(AF_INET, ip_address, &sockAddr.sin_addr);
if (0 > Res)
{
fprintf(stderr, "fatal: not a valid ip address family\n");
close(soc);
return 1;
}
else if (0 == Res)
{
fprintf(stderr, "fatal: not a valid ip address\n");
close(soc);
return 1;
}
for (;;)
{
if (-1 != connect(soc, (struct sockaddr *)&sockAddr, sizeof(sockAddr)))
{
break;
}
fprintf(stderr, "error: connect failed\n");
// UNIX SLEEP FUNCTION: GRRRR
sleep(2);
}
return 0;
}
static void clear_terrain() {
int i, j;
for (i = 0; i < MAX_SIZE; i++) {
for (j = 0; j < MAX_SIZE; j++) {
terrain[i][j] = 0;
}
}
}
static int cmd_line_args(int argc, char * argv[])
{
int i;
for (i = 1; i < argc; i++)
{
fprintf(stderr, "%s\n", argv[i]);
if (argv[i][0] == '-')
{
if (argv[i][1] == '-')
{
if (strncmp("--vis-output", argv[i], 20) == 0)
{
vis_output = TRUE;
}
}
else
{
if (strncmp("-vo", argv[i], 20) == 0)
{
vis_output = TRUE;
}
}
}
else if (ip_address[0] == '\0')
{
fprintf(stderr, "%s\n", argv[i]);
strncpy(ip_address, argv[i], 19);
}
}
return 0;
}