-
Notifications
You must be signed in to change notification settings - Fork 10
/
client.c
7125 lines (5848 loc) · 197 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
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* client.c
* XBolo
*
* Created by Robert Chrzanowski on 4/27/09.
* Copyright 2009 Robert Chrzanowski. All rights reserved.
*
*/
#include "client.h"
#include "server.h"
#include "terrain.h"
#include "tiles.h"
#include "images.h"
#include "bmap_client.h"
#include "io.h"
#include "errchk.h"
#include "timing.h"
#include "resolver.h"
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <math.h>
#include <fcntl.h>
#include <assert.h>
#include <pthread.h>
#include <netinet/tcp.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/**********************/
/* external variables */
/**********************/
int client_running = 0;
struct Client client;
/********************/
/* static variables */
/********************/
static struct {
/* client mutex */
pthread_mutex_t mutex;
} iclient;
static Pointi target;
int buildertask;
int collisionowner;
/*******************/
/* static routines */
/*******************/
static int cleanupclient();
/* client receive routines */
static int recvsrpause();
static int recvsrsendmesg();
static int recvsrdamage();
static int recvsrgrabtrees();
static int recvsrbuild();
static int recvsrgrow();
static int recvsrflood();
static int recvsrplacemine();
static int recvsrdropmine();
static int recvsrdropboat();
static int recvsrplayerjoin();
static int recvsrplayerrejoin();
static int recvsrplayerexit();
static int recvsrplayerdisc();
static int recvsrplayerkick();
static int recvsrplayerban();
static int recvsrrepairpill();
static int recvsrcoolpill();
static int recvsrcapturepill();
static int recvsrbuildpill();
static int recvsrdroppill();
static int recvsrreplenishbase();
static int recvsrcapturebase();
static int recvsrrefuel();
static int recvsrgrabboat();
static int recvsrmineack();
static int recvsrbuilderack();
static int recvsrsmallboom();
static int recvsrsuperboom();
static int recvsrhittank();
static int recvsrsetalliance();
static int recvsrtimelimit();
static int recvsrbasecontrol();
/* client send routines */
static int sendcldropboat(int x, int y);
static int sendcldroppills(float x, float y, uint16_t pills);
static int sendcldropmine(int x, int y);
static int sendcltouch(int x, int y);
static int sendclgrabtile(int x, int y);
static int sendclgrabtrees(int x, int y);
static int sendclbuildroad(int x, int y, int trees);
static int sendclbuildwall(int x, int y, int trees);
static int sendclbuildboat(int x, int y, int trees);
static int sendclbuildpill(int x, int y, int trees, int pill);
static int sendclrepairpill(int x, int y, int trees);
static int sendclplacemine(int x, int y);
static int sendcldamage(int x, int y, int boat);
static int sendclsmallboom(int x, int y);
static int sendclsuperboom(int x, int y);
static int sendclrefuel(int base, int armour, int shells, int mines);
static int sendclhittank(int player, float dir);
static int sendclupdate();
/* builder speed routines */
static float maxspeed(int x, int y);
static float maxturnspeed(int x, int y);
static float builderspeed(int x, int y, int player);
static float buildertargetspeed(int x, int y);
/* visibility routines */
static int decreasevis(Recti r);
static int increasevis(Recti r);
/* refreshes a changed square */
static int refresh(int x, int y);
/* terrain physics */
static int enter(Pointi new, Pointi old);
/* starts the player */
static int spawn();
/* game logic routines */
static int tankmovelogic(int player);
static int tanklocallogic(Pointi old);
static int builderlogic(int player);
static int pilllogic(Vec2f old);
static int shelllogic(int player);
static int explosionlogic(int player);
static int testhiddenmine(int x, int y);
/* convert world scale to tile */
static int w2t(float f);
/* converts radians to rudimentary direction */
static float rounddir(float dir);
/* */
static Vec2f collisiondetect(Vec2f p, float radius, int (*func)(Pointi square));
static int tankcollision(Pointi square);
static int buildercollision(Pointi square);
/* kill builder routines */
static int killsquarebuilder(Pointi p);
static int killpointbuilder(Vec2f p);
static int killbuilder();
/* terrain to tile mapping functions */
static int tilefor(int x, int y);
static int fogtilefor(int x, int y, int tile);
/* tank killing routines */
static int killtank();
static int drown();
static int smallboom();
static int superboom();
/* pill and base finding routines */
static int findpill(int x, int y);
static int findbase(int x, int y);
static int printmessage(int type, const char *body);
static int shellcollisiontest(struct Shell *shell, int player);
static int tanktest(int x, int y);
static int tankonaboattest(int x, int y);
static int testalliance(int p1, int p2);
static int circlesquare(Vec2f point, float radius, Pointi square);
static int getbuildertaskforcommand(int command, Pointi at);
/* client pthread routines */
static void *clientmainthread(void *);
static int dgramclient();
static int joinclient();
int initclient(void (*setplayerstatusfunc)(int player), void (*setpillstatusfunc)(int pill), void (*setbasestatusfunc)(int base), void (*settankstatusfunc)(), void (*playsound)(int sound), void (*printmessagefunc)(int type, const char *text), void (*joinprogress)(int statuscode, float scale), void (*loopupdate)(void)) {
int err;
TRY
client.hostname = NULL;
client.hiddenmines = 1;
client.player = -1;
if ((err = pthread_mutex_init(&iclient.mutex, NULL))) LOGFAIL(err)
client.setplayerstatus = setplayerstatusfunc;
client.setpillstatus = setpillstatusfunc;
client.setbasestatus = setbasestatusfunc;
client.settankstatus = settankstatusfunc;
client.playsound = playsound;
client.printmessage = printmessagefunc;
client.joinprogress = joinprogress;
client.loopupdate = loopupdate;
client.cntlsock = -1;
client.dgramsock = -1;
initlist(&client.changedtiles);
initlist(&client.explosions);
if (initbuf(&client.sendbuf)) LOGFAIL(errno)
if (initbuf(&client.recvbuf)) LOGFAIL(errno)
CLEANUP
ERRHANDLER(0, errno)
END
}
int startclient(const char *hostname, uint16_t port, const char playername[], const char password[]) {
int y, x, i;
int flags;
pthread_t thread;
int err;
assert(!client_running);
TRY
client.timelimitreached = 0;
client.basecontrolreached = 0;
client.spawned = 0;
client.kills = 0;
client.deaths = 0;
client.buildermines = 0;
client.buildertrees = 0;
client.builderpill = NOPILL;
/* initialize the players */
for (i = 0; i < MAXPLAYERS; i++) {
client.players[i].used = 0;
client.players[i].connected = 0;
bzero(client.players[i].name, MAXNAME);
bzero(client.players[i].host, MAXHOST);
client.players[i].seq = 0;
client.players[i].lastupdate = 0;
client.players[i].dead = 1;
client.players[i].boat = 1;
client.players[i].tank = make2f(0.0, 0.0);
client.players[i].dir = 0.0;
client.players[i].speed = 0.0;
client.players[i].turnspeed = 0.0;
client.players[i].kickdir = 0.0;
client.players[i].kickspeed = 0.0;
client.players[i].builderstatus = kBuilderReady;
client.players[i].builder = make2f(0.0, 0.0);
client.players[i].buildertarget = makepoint(0, 0);
client.players[i].builderwait = 0;
client.players[i].alliance = 0;
client.players[i].inputflags = 0;
initlist(&client.players[i].shells);
initlist(&client.players[i].explosions);
if (client.setplayerstatus) {
client.setplayerstatus(i);
}
}
/* initialize the player variables */
client.players[client.player].inputflags = 0;
/* copy name and password */
strncpy(client.name, playername, sizeof(client.name) - 1);
if ((client.passreq = password != NULL)) {
strncpy(client.pass, password, MAXPASS - 1);
}
else {
client.pass[0] = '\0';
}
/* make the screen blank */
for (y = 0; y < WIDTH; y++) {
for (x = 0; x < WIDTH; x++) {
client.images[y][x] = SEAA00IMAGE;
client.fog[y][x] = 0;
}
}
/* copy hostname */
if ((client.hostname = (char *)malloc(strlen(hostname) + 1)) == NULL) LOGFAIL(errno)
strcpy(client.hostname, hostname);
/* set port number */
client.srvaddr.sin_port = htons(port);
/* create the client.cntlsock */
if ((client.cntlsock = socket(AF_INET, SOCK_STREAM, 0)) == -1) LOGFAIL(errno)
if ((flags = fcntl(client.cntlsock, F_GETFL, 0)) == -1) LOGFAIL(errno)
if (fcntl(client.cntlsock, F_SETFL, flags | O_NONBLOCK)) LOGFAIL(errno);
/* create the client.dgramsock */
if ((client.dgramsock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) LOGFAIL(errno)
if ((flags = fcntl(client.dgramsock, F_GETFL, 0)) == -1) LOGFAIL(errno)
if (fcntl(client.dgramsock, F_SETFL, flags | O_NONBLOCK)) LOGFAIL(errno);
/* clear buffers */
if (readbuf(&client.sendbuf, NULL, client.sendbuf.nbytes) == -1) LOGFAIL(errno)
if (readbuf(&client.recvbuf, NULL, client.recvbuf.nbytes) == -1) LOGFAIL(errno)
/* initialize control pipes */
if (pipe(client.mainpipe)) LOGFAIL(errno)
if (pipe(client.threadpipe)) LOGFAIL(errno)
/* create the mainthread */
if ((err = pthread_create(&thread, NULL, clientmainthread, NULL))) LOGFAIL(err)
if ((err = pthread_detach(thread))) LOGFAIL(err)
client_running = 1;
CLEANUP
switch (ERROR) {
case 0:
RETURN(0)
default:
if (client.cntlsock != -1) {
while (close(client.cntlsock) && errno == EINTR);
client.cntlsock = -1;
}
if (client.dgramsock != -1) {
while (close(client.dgramsock) && errno == EINTR);
client.dgramsock = -1;
}
RETERR(-1)
}
END
}
int stopclient() {
char *buf[10];
ssize_t r;
assert(client_running);
TRY
/* close mainpipe */
if (closesock(client.mainpipe + 1)) LOGFAIL(errno)
/* wait for threadpipe to be closed */
for (;;) {
if ((r = read(client.threadpipe[0], buf, sizeof(buf))) == -1) {
if (errno != EINTR) LOGFAIL(errno)
}
else if (r == 0) {
break;
}
}
if (closesock(client.threadpipe)) LOGFAIL(errno)
client_running = 0;
CLEANUP
ERRHANDLER(0, -1)
END
}
int cleanupclient() {
int i;
TRY
free(client.hostname);
client.hostname = NULL;
client.player = -1;
if (closesock(&client.cntlsock)) LOGFAIL(errno)
if (closesock(&client.dgramsock)) LOGFAIL(errno)
clearlist(&client.changedtiles, free);
for (i = 0; i < MAXPLAYERS; i++) {
client.players[i].builderstatus = kBuilderReady;
}
CLEANUP
ERRHANDLER(0, -1)
END
}
int lockclient() {
int err;
TRY
if ((err = pthread_mutex_lock(&iclient.mutex))) LOGFAIL(err)
CLEANUP
ERRHANDLER(0, -1)
END
}
int unlockclient() {
int err;
TRY
if ((err = pthread_mutex_unlock(&iclient.mutex))) LOGFAIL(err)
CLEANUP
ERRHANDLER(0, -1)
END
}
int runclient() {
int i;
Vec2f old;
TRY
if (client.timelimitreached || client.basecontrolreached || client.pause) {
SUCCESS;
}
client.players[client.player].seq++;
old = client.players[client.player].tank;
/* update status of lagged players */
if (client.setplayerstatus) {
for (i = 0; i < MAXPLAYERS; i++) {
if (client.players[client.player].seq - client.players[i].lastupdate == 3*TICKSPERSEC) {
client.setplayerstatus(i);
}
else if (client.players[client.player].seq - client.players[i].lastupdate == TICKSPERSEC) {
client.setplayerstatus(i);
}
}
}
/* move tanks */
for (i = 0; i < MAXPLAYERS; i++) {
if (client.players[i].connected && client.players[i].seq != 0) {
Pointi old, new;
old = makepoint(client.players[i].tank.x, client.players[i].tank.y);
if (tankmovelogic(i)) LOGFAIL(errno)
new = makepoint(client.players[i].tank.x, client.players[i].tank.y);
if (!isequalpoint(new, old) && testalliance(client.player, i)) {
if (increasevis(makerect(new.x - 14, new.y - 14, 29, 29))) LOGFAIL(errno)
if (decreasevis(makerect(old.x - 14, old.y - 14, 29, 29))) LOGFAIL(errno)
}
}
}
/* local tank logic */
if (tanklocallogic(makepoint(old.x, old.y))) LOGFAIL(errno)
/* builder logic */
for (i = 0; i < MAXPLAYERS; i++) {
if (builderlogic(i)) LOGFAIL(errno)
}
/* pill logic */
if (pilllogic(old)) LOGFAIL(errno)
/* perform shell logic */
for (i = 0; i < MAXPLAYERS; i++) {
if (shelllogic(i)) LOGFAIL(errno)
}
/* perform explosion logic */
for (i = -1; i < MAXPLAYERS; i++) {
if (explosionlogic(i)) LOGFAIL(errno)
}
/* send updated location */
if (client.players[client.player].seq%5 == 0) {
if (sendclupdate()) LOGFAIL(errno)
}
/* loop update callback */
client.loopupdate();
CLEANUP
ERRHANDLER(0, -1)
END
}
int joinclient() {
int i;
struct JOIN_Preamble joinpreamble;
struct BOLO_Preamble bolopreamble;
uint8_t msg;
int ret;
int lookup = -1;
int gotlock = 0;
TRY
/* see if already a dot quad */
client.srvaddr.sin_addr.s_addr = inet_addr(client.hostname);
/* not a dot quad, resolve */
if (client.srvaddr.sin_addr.s_addr == INADDR_NONE) {
if (client.joinprogress) {
client.joinprogress(kJoinRESOLVING, 0.0);
}
/* lookup address */
if ((lookup = nslookup(client.hostname)) == -1) LOGFAIL(errno)
/* wait for exit or dns reply */
if ((ret = selectreadread(client.mainpipe[0], lookup)) == -1) {
LOGFAIL(errno)
}
else if (ret == 1) {
if (closesock(&lookup)) LOGFAIL(errno)
SUCCESS
}
/* get nslookup result */
if (nslookupresult(lookup, &client.srvaddr.sin_addr)) {
LOGFAIL(errno)
}
if (closesock(&lookup)) LOGFAIL(errno)
}
/* initialize name to addr and port */
client.srvaddr.sin_family = AF_INET;
bzero(client.srvaddr.sin_zero, 8);
/* connect to server */
if (client.joinprogress) {
client.joinprogress(kJoinCONNECTING, 0.0);
}
if ((connect(client.cntlsock, (struct sockaddr *)&client.srvaddr, INET_ADDRSTRLEN))) {
if (errno != EINPROGRESS) LOGFAIL(errno)
}
/* wait for connection to complete */
for (;;) {
int nfds;
fd_set readfds;
fd_set writefds;
/* select until we have success */
for (;;) {
nfds = 0;
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_SET(client.mainpipe[0], &readfds);
nfds = MAX(nfds, client.mainpipe[0]);
FD_SET(client.cntlsock, &writefds);
nfds = MAX(nfds, client.cntlsock);
if ((nfds = select(nfds + 1, &readfds, &writefds, NULL, NULL)) == -1) {
if (errno != EINTR) LOGFAIL(errno)
}
else {
break;
}
}
if (FD_ISSET(client.mainpipe[0], &readfds)) {
ret = 1;
SUCCESS
}
else if (FD_ISSET(client.cntlsock, &writefds)) {
if (connect(client.cntlsock, (struct sockaddr *)&client.srvaddr, INET_ADDRSTRLEN) && errno != EISCONN) LOGFAIL(errno)
break;
}
}
/* get local address of cntlsock */
struct sockaddr_in saddr;
socklen_t addrlen;
addrlen = INET_ADDRSTRLEN;
if (getsockname(client.cntlsock, (struct sockaddr *)&saddr, &addrlen)) LOGFAIL(errno)
/* bind dgramsock to local address */
while (bind(client.dgramsock, (struct sockaddr *)&saddr, addrlen)) {
if (errno != EAGAIN) {
LOGFAIL(errno)
}
usleep(10000);
}
/* set remote end for dgramsock */
if ((connect(client.dgramsock, (struct sockaddr *)&client.srvaddr, INET_ADDRSTRLEN))) LOGFAIL(errno)
bcopy(NET_GAME_IDENT, joinpreamble.ident, sizeof(NET_GAME_IDENT));
joinpreamble.version = NET_GAME_VERSION;
bzero(joinpreamble.name, MAXNAME);
strncpy(joinpreamble.name, client.name, sizeof(joinpreamble.name) - 1);
bzero(joinpreamble.pass, MAXPASS);
strncpy(joinpreamble.pass, client.pass, sizeof(joinpreamble.pass) - 1);
if (client.joinprogress) {
client.joinprogress(kJoinSENDJOIN, 0.0);
}
/* send join preamble */
if (writebuf(&client.sendbuf, &joinpreamble, sizeof(joinpreamble)) == -1) LOGFAIL(errno)
if ((ret = cntlsend(client.mainpipe[0], client.cntlsock, &client.sendbuf)) == -1) {
LOGFAIL(errno)
}
else if (ret == 1) {
SUCCESS
}
if (client.joinprogress) {
client.joinprogress(kJoinRECVPREAMBLE, 0.0);
}
/* recv msg */
if ((ret = cntlrecv(client.mainpipe[0], client.cntlsock, &client.recvbuf, sizeof(msg))) == -1) {
LOGFAIL(errno)
}
else if (ret == 1) {
SUCCESS
}
if (readbuf(&client.recvbuf, &msg, sizeof(msg)) == -1) LOGFAIL(errno)
if (msg == kBadVersionJOIN) FAIL(EBADVERSION)
if (msg == kDisallowJOIN) FAIL(EDISSALLOW)
if (msg == kBadPasswordJOIN) FAIL(EBADPASS)
if (msg == kServerFullJOIN) FAIL(ESERVERFULL)
if (msg == kServerTimeLimitReachedJOIN) FAIL(ETIMELIMIT)
if (msg == kBannedPlayerJOIN) FAIL(EBANNEDPLAYER)
if (msg != kSendingPreambleJOIN) LOGFAIL(ESERVERERROR)
/* recv preamble */
if ((ret = cntlrecv(client.mainpipe[0], client.cntlsock, &client.recvbuf, sizeof(bolopreamble))) == -1) {
LOGFAIL(errno)
}
else if (ret == 1) {
SUCCESS
}
if (readbuf(&client.recvbuf, &bolopreamble, sizeof(bolopreamble)) == -1) LOGFAIL(errno)
/* convert byte order */
bolopreamble.maplen = ntohl(bolopreamble.maplen);
/* recv map data */
ssize_t r;
if ((r = cntlrecv(client.mainpipe[0], client.cntlsock, &client.recvbuf, bolopreamble.maplen)) == -1) {
LOGFAIL(errno)
}
else if (r == 1) {
SUCCESS
}
if (client.joinprogress) {
client.joinprogress(kJoinRECVMAP, 100.0);
}
/*
while (client.recvbuf.nbytes < bolopreamble.maplen) {
ssize_t r;
if ((r = cntlrecv(client.mainpipe[0], client.cntlsock, &client.recvbuf, MIN(bolopreamble.maplen - client.recvbuf.nbytes, RECVMAPBLOCKSIZE))) == -1) {
LOGFAIL(errno)
}
else if (r == 1) {
SUCCESS
}
if (client.joinprogress) {
client.joinprogress(kJoinRECVMAP, (client.recvbuf.nbytes*100.0)/bolopreamble.maplen);
}
}
*/
/* modify client */
if (lockclient()) LOGFAIL(errno)
gotlock = 1;
client.player = bolopreamble.player;
client.hiddenmines = bolopreamble.hiddenmines;
if (bolopreamble.pause == 255) {
client.pause = -1;
}
else {
client.pause = bolopreamble.pause;
}
client.gametype = bolopreamble.gametype;
if (bolopreamble.gametype == kDominationGameType) {
/* the number of seconds to have control of all bases to win */
switch (bolopreamble.game.domination.type) {
case kOpenGame:
case kTournamentGame:
case kStrictGame:
client.game.domination.type = bolopreamble.game.domination.type;
break;
default:
assert(0);
}
client.game.domination.basecontrol = bolopreamble.game.domination.basecontrol;
}
else {
assert(0);
}
/* initialize the players */
for (i = 0; i < MAXPLAYERS; i++) {
client.players[i].used = bolopreamble.players[i].used;
client.players[i].connected = bolopreamble.players[i].connected;
client.players[i].seq = ntohl(bolopreamble.players[i].seq);
strncpy(client.players[i].name, (char *)bolopreamble.players[i].name, MAXNAME - 1);
strncpy(client.players[i].host, (char *)bolopreamble.players[i].host, MAXHOST - 1);
client.players[i].builderstatus = kBuilderReady;
client.players[i].alliance = ntohs(bolopreamble.players[i].alliance);
if (client.setplayerstatus) {
client.setplayerstatus(i);
}
}
/* load map data */
if (clientloadmap(client.recvbuf.ptr, bolopreamble.maplen)) LOGFAIL(errno)
if (readbuf(&client.recvbuf, NULL, bolopreamble.maplen) == -1) LOGFAIL(errno)
spawn();
if (increasevis(makerect(((int)client.players[bolopreamble.player].tank.x) - 14, ((int)client.players[bolopreamble.player].tank.y) - 14, 29, 29)) == -1) LOGFAIL(errno)
// if (increasevis(makerect(0, 0, WIDTH, WIDTH)) == -1) LOGFAIL(errno) /* for cheaters */
if (unlockclient()) LOGFAIL(errno)
gotlock = 0;
/* update pill status */
if (client.setpillstatus) {
for (i = 0; i < client.npills; i++) {
client.setpillstatus(i);
}
}
/* update base status */
if (client.setbasestatus) {
for (i = 0; i < client.nbases; i++) {
client.setbasestatus(i);
}
}
/* join complete */
if (client.joinprogress) {
client.joinprogress(kJoinSUCCESS, 100.0);
}
ret = 0;
CLEANUP
switch (ERROR) {
case 0:
RETURN(ret)
default:
if (lookup != -1) {
closesock(&lookup);
}
if (gotlock) {
unlockclient();
}
RETERR(-1)
}
END
}
int recvclient() {
TRY
/* receive loop */
for (;;) {
if (client.recvbuf.nbytes < sizeof(uint8_t)) FAIL(EAGAIN)
switch (*(uint8_t *)client.recvbuf.ptr) {
case SRPAUSE:
if (recvsrpause()) FAIL(errno)
break;
case SRSENDMESG:
if (recvsrsendmesg()) FAIL(errno)
break;
case SRDAMAGE:
if (recvsrdamage()) FAIL(errno)
break;
case SRGRABTREES:
if (recvsrgrabtrees()) FAIL(errno)
break;
case SRBUILD:
if (recvsrbuild()) FAIL(errno)
break;
case SRGROW:
if (recvsrgrow()) FAIL(errno)
break;
case SRFLOOD:
if (recvsrflood()) FAIL(errno)
break;
case SRPLACEMINE:
if (recvsrplacemine()) FAIL(errno)
break;
case SRDROPMINE:
if (recvsrdropmine()) FAIL(errno)
break;
case SRDROPBOAT:
if (recvsrdropboat()) FAIL(errno)
break;
case SRPLAYERJOIN:
if (recvsrplayerjoin()) FAIL(errno)
break;
case SRPLAYERREJOIN:
if (recvsrplayerrejoin()) FAIL(errno)
break;
case SRPLAYEREXIT:
if (recvsrplayerexit()) FAIL(errno)
if (!client.players[client.player].connected) SUCCESS
break;
case SRPLAYERDISC:
if (recvsrplayerdisc()) FAIL(errno)
if (!client.players[client.player].connected) SUCCESS
break;
case SRPLAYERKICK:
if (recvsrplayerkick()) FAIL(errno)
if (!client.players[client.player].connected) SUCCESS
break;
case SRPLAYERBAN:
if (recvsrplayerban()) FAIL(errno)
if (!client.players[client.player].connected) SUCCESS
break;
case SRREPAIRPILL:
if (recvsrrepairpill()) FAIL(errno)
break;
case SRCOOLPILL:
if (recvsrcoolpill()) FAIL(errno)
break;
case SRCAPTUREPILL:
if (recvsrcapturepill()) FAIL(errno)
break;
case SRBUILDPILL:
if (recvsrbuildpill()) FAIL(errno)
break;
case SRDROPPILL:
if (recvsrdroppill()) FAIL(errno)
break;
case SRREPLENISHBASE:
if (recvsrreplenishbase()) FAIL(errno)
break;
case SRCAPTUREBASE:
if (recvsrcapturebase()) FAIL(errno)
break;
case SRREFUEL:
if (recvsrrefuel()) FAIL(errno)
break;
case SRGRABBOAT:
if (recvsrgrabboat()) FAIL(errno)
break;
case SRMINEACK:
if (recvsrmineack()) FAIL(errno)
break;
case SRBUILDERACK:
if (recvsrbuilderack()) FAIL(errno)
break;
case SRSMALLBOOM:
if (recvsrsmallboom()) FAIL(errno)
break;
case SRSUPERBOOM:
if (recvsrsuperboom()) FAIL(errno)
break;
case SRHITTANK:
if (recvsrhittank()) FAIL(errno)
break;
case SRSETALLIANCE:
if (recvsrsetalliance()) FAIL(errno)
break;
case SRTIMELIMIT:
if (recvsrtimelimit()) FAIL(errno)
break;
case SRBASECONTROL:
if (recvsrbasecontrol()) FAIL(errno)
break;
default:
LOGFAIL(EINVAL)
break;
}
}
CLEANUP
ERRHANDLER(0, -1)
END
}
int selectclient(fd_set *readfds, fd_set *writefds, struct timeval *timeout) {
int nfds;
TRY
nfds = 0;
FD_ZERO(readfds);
FD_ZERO(writefds);
/* read on mainpipe */
FD_SET(client.mainpipe[0], readfds);
nfds = MAX(nfds, client.mainpipe[0]);
/* read on dgram sock */
FD_SET(client.dgramsock, readfds);
nfds = MAX(nfds, client.dgramsock);
/* read/write cntlsock */
FD_SET(client.cntlsock, readfds);
nfds = MAX(nfds, client.cntlsock);
if (client.sendbuf.nbytes > 0) {
FD_SET(client.cntlsock, writefds);
}
if ((nfds = select(nfds + 1, readfds, writefds, NULL, timeout)) == -1) {
if (errno != EINTR) {
LOGFAIL(errno)
}
nfds = 0;
}
CLEANUP
ERRHANDLER(nfds, -1)
END
}
void *clientmainthread(void *arg) {
int i;
uint64_t currenttime;
uint64_t nexttick;
int gotlock = 0;
TRY
if ((i = joinclient()) == -1) {
LOGFAIL(errno)
}
else if (i == 1) {
SUCCESS
}
/* set TCP_NODELAY on socket */
i = 1;