-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
599 lines (533 loc) · 14.4 KB
/
client.c
File metadata and controls
599 lines (533 loc) · 14.4 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#else
#include <GL/glut.h>
#include <AL/al.h>
#include <AL/alc.h>
#endif
#include <tpl.h>
#include <enet/enet.h>
#include "vector2.h"
#include "sound_list.h"
#include "arena.h"
#include "chat.h"
#include "user.h"
#include "client.h"
#define LOBBY 0
#define PREGAME 1
#define GAME 2
#define POSTGAME 3
#define NOT_CONNECTED 0
#define NORMAL 1
#define MESSAGE 2
#define NAME 3
#define COMMAND 4
typedef struct tronkeys{
char lr;
char ud;
char t;
char s;
} tronkeys;
typedef struct clienttype{
ENetPeer * enet_server;
ENetHost * enet_client;
vector2 w;
int game_state;
int game_mode;
arena c_game;
chat c_chat;
user c_users;
char winner[80];
char mbuf[500];
tronkeys g_keys;
int mbuf_num;
double ctimer;
double timer;
ALuint key_buf;
s_list key_click;
} clienttype;
static int get_input(client clnt, unsigned char key);
static void normal_keys(client clnt, unsigned key);
static void renderBitmapString(float x, float y, void *font, char *string);
static void not_connected_keys(client clnt, unsigned key);
static void message_keys(client clnt, unsigned key);
static void name_keys(client clnt, unsigned key);
static void client_get_game_init(client clnt, ENetPacket * packet);
static void client_get_game_free(client clnt);
static void send_keys(client clnt, int channel);
client client_init(ENetHost * enet_client){
client clnt;
clnt = (client)malloc(sizeof(clienttype));
if(clnt == NULL){return NULL;}
clnt->game_state = LOBBY;
clnt->game_mode = NOT_CONNECTED;
clnt->c_game = NULL;
clnt->c_chat = chat_init();
clnt->c_users = user_init();
clnt->w.x = 800;
clnt->w.y = 600;
clnt->timer = 0;
clnt->ctimer = 0;
clnt->enet_client = enet_client;
clnt->mbuf[1] = '\0';
clnt->mbuf_num = 0;
clnt->g_keys.lr = 0;
clnt->g_keys.ud = 0;
clnt->g_keys.t = 0;
clnt->g_keys.s = 0;
alGenBuffers(1, &clnt->key_buf);
snd_load_file("./sound_data/click2.ogg", clnt->key_buf);
clnt->key_click = s_init(clnt->key_buf);
return clnt;
}
void client_connect(client clnt){
clnt->game_mode = NORMAL;
chat_add_message(clnt->c_chat, "Client", "Connection succesful. Have fun.");
}
void client_disconnect(client clnt){
clnt->game_mode = NOT_CONNECTED;
user_free(clnt->c_users);
clnt->c_users = user_init();
if(clnt->c_game != NULL){
arena_free_sound(clnt->c_game);
arena_free(clnt->c_game);
clnt->c_game = NULL;
}
chat_add_message(clnt->c_chat, "Client", "You have been disconnected from the server.");
}
void client_update(client clnt, double dt){
s_update(clnt->key_click);
clnt->ctimer += dt;
clnt->timer -= dt;
if(clnt->game_state == PREGAME && clnt->timer < 0){
clnt->game_state = GAME;
}
if(clnt->game_state == GAME){
int winner_id;
if(arena_player_status(clnt->c_game) < 2 ){
clnt->game_state = POSTGAME;
if((winner_id = arena_winner(clnt->c_game)) >= 0){
char * name = user_nameby_aid(clnt->c_users, winner_id);
sprintf(clnt->winner, "%s wins!", name);
}
else {
strcpy(clnt->winner, "Tie");
}
}
}
clnt->mbuf[clnt->mbuf_num] = clnt->ctimer < 0.5 ? '|' : ' ';
if(clnt->ctimer > 1)
{clnt->ctimer = 0;}
if(clnt->c_game != NULL && clnt->game_state != PREGAME){
arena_update_client(clnt->c_game, dt);
}
if(clnt->game_state != LOBBY){
send_keys(clnt, 3);
}
}
void client_render(client clnt){
char *buf;
glColor3f(1, 1, 1);
switch(clnt->game_mode){
case NOT_CONNECTED:
glPushMatrix();
glLoadIdentity();
renderBitmapString(10, clnt->w.y - 15, GLUT_BITMAP_HELVETICA_10, "Please enter the server you would like to connect to:\0");
glPopMatrix();
glPushMatrix();
glLoadIdentity();
renderBitmapString(10, clnt->w.y- 27, GLUT_BITMAP_HELVETICA_10, clnt->mbuf);
glPopMatrix();
break;
case MESSAGE:
glPopMatrix();
glPushMatrix();
glLoadIdentity();
renderBitmapString(10, clnt->w.y - 15, GLUT_BITMAP_HELVETICA_10, "Message: ");
renderBitmapString(10, clnt->w.y - 27, GLUT_BITMAP_HELVETICA_10, clnt->mbuf);
glPopMatrix();
break;
case NAME:
glPushMatrix();
glLoadIdentity();
renderBitmapString(10, clnt->w.y - 15, GLUT_BITMAP_HELVETICA_10, "What would you like your name to be?");
renderBitmapString(10, clnt->w.y - 27, GLUT_BITMAP_HELVETICA_10, clnt->mbuf);
glPopMatrix();
break;
}
switch(clnt->game_state){
case LOBBY:
if(clnt->game_mode != NOT_CONNECTED){
user_render(clnt->c_users, clnt->w.x, clnt->w.y);
}
glColor3f(1, 1, 1);
chat_render(clnt->c_chat, 0);
break;
case GAME:
arena_render(clnt->c_game);
glColor3f(1, 1, 1);
chat_render(clnt->c_chat, 1);
break;
case POSTGAME:
arena_render(clnt->c_game);
glColor3f(1, 1, 1);
chat_render(clnt->c_chat, 1);
double ratio = glutGet(GLUT_WINDOW_WIDTH)/(double)glutGet(GLUT_WINDOW_HEIGHT);
double wx = ratio * clnt->w.y;
glPushMatrix();
glLoadIdentity();
renderBitmapString(wx/2, clnt->w.y/2, GLUT_BITMAP_HELVETICA_18, clnt->winner);
glPopMatrix();
break;
case PREGAME:
arena_render(clnt->c_game);
glColor3f(1, 1, 1);
chat_render(clnt->c_chat, 1);
if(clnt->timer > 0 ){
//draw timer here.
double ratio = glutGet(GLUT_WINDOW_WIDTH)/(double)glutGet(GLUT_WINDOW_HEIGHT);
double wx = ratio * clnt->w.y;
char buf[5];
sprintf(buf, "%.0f", clnt->timer + 0.5);
glPushMatrix();
glLoadIdentity();
renderBitmapString(wx/2, clnt->w.y/2, GLUT_BITMAP_HELVETICA_18, buf);
glPopMatrix();
}
break;
}
}
void client_keys(client clnt, unsigned char key){
switch(clnt->game_mode){
case NOT_CONNECTED:
not_connected_keys(clnt, key);
break;
case MESSAGE:
message_keys(clnt, key);
break;
case NAME:
name_keys(clnt, key);
break;
case NORMAL:
normal_keys(clnt, key);
break;
}
}
void client_rkeys(client clnt, unsigned char key){
switch(key){
case 'a':
clnt->g_keys.s = 0;
break;
}
}
void client_skeys(client clnt, int key){
switch(key) {
case GLUT_KEY_LEFT :
clnt->g_keys.lr = -1;
break;
case GLUT_KEY_RIGHT :
clnt->g_keys.lr = 1;
break;
case GLUT_KEY_UP :
clnt->g_keys.ud = 1;
break;
case GLUT_KEY_DOWN :
clnt->g_keys.ud = -1;
break;
}
}
void client_rskeys(client clnt, int key){
switch (key) {
case GLUT_KEY_LEFT :
if(clnt->g_keys.lr<0){clnt->g_keys.lr=0;}
break;
case GLUT_KEY_RIGHT :
if(clnt->g_keys.lr>0){clnt->g_keys.lr=0;}
break;
case GLUT_KEY_UP :
if(clnt->g_keys.ud>0){clnt->g_keys.ud=0;}
break;
case GLUT_KEY_DOWN :
if(clnt->g_keys.ud<0){clnt->g_keys.ud=0;}
break;
}
}
void client_process_packets(client clnt, ENetEvent *event){
vector2 pos; pos.x = 0; pos.y = 0;
int id;
switch(event->channelID){
case 0:
user_get_chat_message(clnt->c_users, clnt->c_chat, event->packet);
s_add_snd(clnt->key_click, pos);
break;
case 1:
user_get_list(clnt->c_users, event->packet);
break;
case 2:
client_get_game_init(clnt, event->packet);
break;
case 3:
s_add_snd(clnt->key_click, pos);
user_get_disconnect(clnt->c_users, event->packet, clnt->c_chat);
break;
case 4:
if(clnt->c_game != NULL)
{arena_get_update(clnt->c_game, event->packet);}
break;
case 5:
client_get_game_free(clnt);
clnt->g_keys.t = 0;
break;
case 6:
id = user_get_arena_id(event->packet);
arena_set_my_id(clnt->c_game, id);
arena_init_sound(clnt->c_game);
break;
}
}
void client_free(client clnt){
ENetEvent event;
if(clnt->game_mode != NOT_CONNECTED){
enet_peer_disconnect (clnt->enet_server, 0);
/* Allow up to 3 seconds for the disconnect to succeed
and drop any packets received packets.*/
int connection = 1;
while (enet_host_service (clnt->enet_client, & event, 3000) > 0){
switch (event.type){
case ENET_EVENT_TYPE_RECEIVE:
enet_packet_destroy (event.packet);
break;
case ENET_EVENT_TYPE_DISCONNECT:
if(clnt->c_game != NULL) {arena_free(clnt->c_game);}
clnt->c_game = NULL;
connection = 0;
}
}
/* We've arrived here, so the disconnect attempt didn't */
/* succeed yet. Force the connection down. */
if(connection)
{enet_peer_reset (clnt->enet_server);}
}
chat_free(clnt->c_chat);
user_free(clnt->c_users);
if(clnt->c_game != NULL) {arena_free(clnt->c_game);}
free(clnt);
}
int client_connection(client clnt){
if(clnt->game_mode == NOT_CONNECTED)
{return 0;}
else
{return 1;}
}
static int get_input(client clnt, unsigned char key) {
int i;
if(key==10 || key==13){
return 1;
}
else if(key>=1 && key < 127 && clnt->mbuf_num < 500){
clnt->mbuf[clnt->mbuf_num]=key;
clnt->mbuf_num++;
clnt->mbuf[clnt->mbuf_num+1] = '\0';
return 0;
}
else if (key == 127 && clnt->mbuf_num > 0) {
clnt->mbuf_num--;
clnt->mbuf[clnt->mbuf_num+1] = '\0';
return 0;
}
return 0;
}
static void normal_keys(client clnt, unsigned key){
ENetEvent event;
ENetPacket * packet;
switch(key){
case ' ':
if(clnt->game_state == LOBBY){
clnt->mbuf[clnt->mbuf_num] = '\0';
packet = enet_packet_create (clnt->mbuf, strlen (clnt->mbuf) + 1, ENET_PACKET_FLAG_RELIABLE);
enet_peer_send (clnt->enet_server, 2, packet);
}
break;
case 'a':
if(clnt->game_state != LOBBY){
clnt->g_keys.s = 1;
}
break;
case 's':
if(clnt->game_state != LOBBY){
clnt->g_keys.t = clnt->g_keys.t ? 0 : 1;
}
break;
case 'T':
case 't':
clnt->game_mode = MESSAGE;
break;
case 'N':
case 'n':
clnt->game_mode = NAME;
break;
case 27:
if(clnt->game_mode != NOT_CONNECTED){
clnt->game_mode = NOT_CONNECTED;
user_free(clnt->c_users);
clnt->c_users = user_init();
enet_peer_disconnect (clnt->enet_server, 0);
/* Allow up to 3 seconds for the disconnect to succeed
and drop any packets received packets.
*/
while (enet_host_service (clnt->enet_client, & event, 3000) > 0){
switch (event.type){
case ENET_EVENT_TYPE_RECEIVE:
enet_packet_destroy (event.packet);
break;
case ENET_EVENT_TYPE_DISCONNECT:
printf("You left the server.\n");
chat_add_message(clnt->c_chat, "Client", "You left the server. Push <esc> again to exit the game");
clnt->game_state = LOBBY;
if(clnt->c_game != NULL) {
arena_free_sound(clnt->c_game);
arena_free(clnt->c_game);}
clnt->c_game = NULL;
return;
}
}
/* We've arrived here, so the disconnect attempt didn't */
/* succeed yet. Force the connection down. */
enet_peer_reset (clnt->enet_server);
}
break;
}
}
vector2 clnt_gm_size(client clnt){
return clnt->w;
}
static void message_keys(client clnt, unsigned key){
ENetPacket * packet;
ENetAddress address;
ENetEvent event;
char disp [500];
if(get_input(clnt, key)){
clnt->mbuf[clnt->mbuf_num] = '\0';
packet = enet_packet_create (clnt->mbuf, strlen (clnt->mbuf) + 1, ENET_PACKET_FLAG_RELIABLE);
enet_peer_send (clnt->enet_server, 0, packet);
clnt->mbuf_num = 0;
clnt->mbuf[1] = '\0';
clnt->game_mode = NORMAL;
}
if(key == 27)
{clnt->game_mode = NORMAL;}
}
static void name_keys(client clnt, unsigned key){
ENetPacket * packet;
ENetAddress address;
ENetEvent event;
char disp [500];
if(get_input(clnt, key)){
clnt->mbuf[clnt->mbuf_num] = '\0';
packet = enet_packet_create (clnt->mbuf, strlen (clnt->mbuf) + 1, ENET_PACKET_FLAG_RELIABLE);
enet_peer_send (clnt->enet_server, 1, packet);
clnt->mbuf_num = 0;
clnt->mbuf[1] = '\0';
clnt->game_mode = NORMAL;
}
if(key == 27)
{clnt->game_mode = NORMAL;}
}
static void not_connected_keys(client clnt, unsigned key){
ENetPacket * packet;
ENetAddress address;
ENetEvent event;
char disp [500];
//Connect to the server using the string in mbuf as the address upon hitting return.
if(get_input(clnt, key)){
clnt->mbuf[clnt->mbuf_num] = '\0';
enet_address_set_host (& address, clnt->mbuf);
address.port = 5001;
/* Initiate the connection. The third argument is the number of channels, the 4th is user supplied data.*/
clnt->enet_server = enet_host_connect (clnt->enet_client, & address, 255, 0);
if (clnt->enet_server == NULL){
fprintf (stderr,"No available peers for initiating an ENet connection.\n");
exit (EXIT_FAILURE);
}
/* Wait up to 5 seconds for the connection attempt to succeed. */
if (enet_host_service (clnt->enet_client, & event, 5000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT){
sprintf(disp, "Connection to %s succeeded.\n", clnt->mbuf);
printf("Connection to %s succeeded.\n", clnt->mbuf);
chat_add_message(clnt->c_chat, "Client", disp);
clnt->game_mode = NORMAL;
clnt->game_state = LOBBY;
clnt->mbuf_num = 0;
clnt->mbuf[1] = '\0';
}
else{
enet_peer_reset (clnt->enet_server);
sprintf(disp, "Connection to %s failed.", clnt->mbuf);
chat_add_message(clnt->c_chat, "Client", disp);
printf("Connection to %s failed.\n", clnt->mbuf);
clnt->mbuf_num = 0;
clnt->mbuf[1] = '\0';
}
}
if (key == 27){
exit(0);
}
}
static void renderBitmapString(
float x,
float y,
void *font,
char *string) {
char *c;
glRasterPos2f(x, y);
for (c=string; *c != '\0'; c++) {
glutBitmapCharacter(font, *c);
}
}
static void client_get_game_init(client clnt, ENetPacket * packet){
double timer;
int ply_num, x_bd, y_bd;
tpl_node * tn;
tn = tpl_map("fiii", &timer, &ply_num, &x_bd, &y_bd);
tpl_load(tn, TPL_MEM, packet->data, packet->dataLength);
tpl_unpack(tn, 0);
tpl_free(tn);
clnt->game_state = PREGAME;
clnt->timer = timer;
clnt->c_game = arena_init(ply_num, 0, x_bd, y_bd);
clnt->w.x = x_bd*1.2f;
clnt->w.y = y_bd*1.2f;
double ratio = glutGet(GLUT_WINDOW_WIDTH)/(double)glutGet(GLUT_WINDOW_HEIGHT);
double wx = ratio * clnt->w.y;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, wx, 0, clnt->w.y);
glMatrixMode(GL_MODELVIEW);
}
static void client_get_game_free(client clnt){
if(clnt->c_game != NULL){
arena_free_sound(clnt->c_game);
arena_free(clnt->c_game);
clnt->c_game = NULL;
}
clnt->game_state = LOBBY;
}
static void send_keys(client clnt, int channel){
tpl_node *tn;
void *addr;
size_t len;
tronkeys tmp = clnt->g_keys;
tn = tpl_map("cccc", &tmp.lr, &tmp.ud, &tmp.t, &tmp.s);
tpl_pack(tn, 0);
tpl_dump(tn, TPL_MEM, &addr, &len);
tpl_free(tn);
//send the package to the server
ENetPacket * packet;
packet = enet_packet_create (addr, len, ENET_PACKET_FLAG_RELIABLE);
enet_peer_send (clnt->enet_server, channel, packet);
free(addr);
}