-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.c
3376 lines (2954 loc) · 93.1 KB
/
main.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
/*
* main.c
* Copyright (C) 1998 Brainchild Design - http://brainchilddesign.com/
*
* Copyright (C) 2001 Chuck Mason <cemason@users.sourceforge.net>
*
* Copyright (C) 2002 Florian Schulze <crow@icculus.org>
*
* This file is part of Jump'n'Bump.
*
* Jump'n'Bump is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Jump'n'Bump is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "globals.h"
#include "version.h"
#include <fcntl.h>
#ifdef _MSC_VER
#include <io.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
#include <SDL.h>
#include <SDL_main.h>
#ifndef NO_SDL_NET
#include <SDL_net.h>
#endif /* NO_SDL_NET */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
gob_t rabbit_gobs = { 0 };
gob_t font_gobs = { 0 };
gob_t object_gobs = { 0 };
gob_t number_gobs = { 0 };
main_info_t main_info;
player_t player[JNB_MAX_PLAYERS];
player_anim_t player_anims[7];
object_t objects[NUM_OBJECTS];
joy_t joy;
mouse_t mouse;
char* datfile_name = NULL;
char *background_pic;
char *mask_pic;
int flip = 0;
int ai[JNB_MAX_PLAYERS];
unsigned int ban_map[17][22] = {
{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0},
{1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1},
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
{1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
{2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 0, 0, 0, 0, 0, 1, 3, 3, 3, 1, 1, 1},
{2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
struct {
int num_frames;
int restart_frame;
struct {
int image;
int ticks;
} frame[80];
} object_anims[8] = {
{
6, 0, {
{
0, 3}, {
1, 3}, {
2, 3}, {
3, 3}, {
4, 3}, {
5, 3}, {
0, 0}, {
0, 0}, {
0, 0}, {
0, 0}
}
}, {
9, 0, {
{
6, 2}, {
7, 2}, {
8, 2}, {
9, 2}, {
10, 2}, {
11, 2}, {
12, 2}, {
13, 2}, {
14, 2}, {
0, 0}
}
}, {
5, 0, {
{
15, 3}, {
16, 3}, {
16, 3}, {
17, 3}, {
18, 3}, {
19, 3}, {
0, 0}, {
0, 0}, {
0, 0}, {
0, 0}
}
}, {
10, 0, {
{
20, 2}, {
21, 2}, {
22, 2}, {
23, 2}, {
24, 2}, {
25, 2}, {
24, 2}, {
23, 2}, {
22, 2}, {
21, 2}
}
}, {
10, 0, {
{
26, 2}, {
27, 2}, {
28, 2}, {
29, 2}, {
30, 2}, {
31, 2}, {
30, 2}, {
29, 2}, {
28, 2}, {
27, 2}
}
}, {
10, 0, {
{
32, 2}, {
33, 2}, {
34, 2}, {
35, 2}, {
36, 2}, {
37, 2}, {
36, 2}, {
35, 2}, {
34, 2}, {
33, 2}
}
}, {
10, 0, {
{
38, 2}, {
39, 2}, {
40, 2}, {
41, 2}, {
42, 2}, {
43, 2}, {
42, 2}, {
41, 2}, {
40, 2}, {
39, 2}
}
}, {
4, 0, {
{
76, 4}, {
77, 4}, {
78, 4}, {
79, 4}, {
0, 0}, {
0, 0}, {
0, 0}, {
0, 0}, {
0, 0}, {
0, 0}
}
}
};
int flies_enabled = 1;
struct {
int x, y;
int old_x, old_y;
int old_draw_x, old_draw_y;
int back[2];
int back_defined[2];
} flies[NUM_FLIES];
struct {
struct {
short num_pobs;
struct {
int x, y;
int image;
gob_t *pob_data;
} pobs[NUM_LEFTOVERS];
} page[2];
} leftovers;
int pogostick, bunnies_in_space, jetpack, lord_of_the_flies, blood_is_thicker_than_water;
#ifndef _MSC_VER
int filelength(int handle)
{
struct stat buf;
if (fstat(handle, &buf) == -1) {
perror("filelength");
exit(EXIT_FAILURE);
}
return buf.st_size;
}
#endif
/* networking shite. */
int client_player_num = -1;
int is_server = 1;
int is_net = 0;
#ifndef NO_SDL_NET
TCPsocket sock = NULL;
SDLNet_SocketSet socketset = NULL;
typedef struct
{
TCPsocket sock;
IPaddress addr;
SDLNet_SocketSet socketset;
} NetInfo;
NetInfo net_info[JNB_MAX_PLAYERS];
#endif
typedef struct
{
Uint32 cmd;
int32_t arg;
int32_t arg2;
int32_t arg3;
int32_t arg4;
} NetPacket;
#define NETPKTBUFSIZE (4 + 4 + 4 + 4 + 4)
#define NETCMD_NACK (0xF00DF00D + 0)
#define NETCMD_ACK (0xF00DF00D + 1)
#define NETCMD_HELLO (0xF00DF00D + 2)
#define NETCMD_GREENLIGHT (0xF00DF00D + 3)
#define NETCMD_MOVE (0xF00DF00D + 4)
#define NETCMD_BYE (0xF00DF00D + 5)
#define NETCMD_POSITION (0xF00DF00D + 6)
#define NETCMD_ALIVE (0xF00DF00D + 7)
#define NETCMD_KILL (0xF00DF00D + 8)
#ifndef NO_SDL_NET
void bufToPacket(const char *buf, NetPacket *pkt)
{
SDLNet_Write32(*((Uint32*) (buf + 0)), &pkt->cmd);
SDLNet_Write32(*((Uint32*) (buf + 4)), &pkt->arg);
SDLNet_Write32(*((Uint32*) (buf + 8)), &pkt->arg2);
SDLNet_Write32(*((Uint32*) (buf + 12)), &pkt->arg3);
SDLNet_Write32(*((Uint32*) (buf + 16)), &pkt->arg4);
/*
pkt->cmd = ntohl(*((unsigned long *) (buf + 0)));
pkt->arg = (long) ntohl(*((unsigned long *) (buf + 4)));
pkt->arg2 = (long) ntohl(*((unsigned long *) (buf + 8)));
pkt->arg3 = (long) ntohl(*((unsigned long *) (buf + 12)));
pkt->arg4 = (long) ntohl(*((unsigned long *) (buf + 16)));
*/
}
void packetToBuf(const NetPacket *pkt, char *buf)
{
*((Uint32*) (buf + 0)) = SDLNet_Read32(&pkt->cmd);
*((Uint32*) (buf + 4)) = SDLNet_Read32(&pkt->arg);
*((Uint32*) (buf + 8)) = SDLNet_Read32(&pkt->arg2);
*((Uint32*) (buf + 12)) = SDLNet_Read32(&pkt->arg3);
*((Uint32*) (buf + 16)) = SDLNet_Read32(&pkt->arg4);
/*
*((unsigned long *) (buf + 0)) = htonl(pkt->cmd);
*((unsigned long *) (buf + 4)) = htonl((unsigned long) pkt->arg);
*((unsigned long *) (buf + 8)) = htonl((unsigned long) pkt->arg2);
*((unsigned long *) (buf + 12)) = htonl((unsigned long) pkt->arg3);
*((unsigned long *) (buf + 16)) = htonl((unsigned long) pkt->arg4);
*/
}
void sendPacketToSock(TCPsocket s, NetPacket *pkt)
{
int bytes_left = NETPKTBUFSIZE;
int bw;
char buf[NETPKTBUFSIZE];
char *ptr = buf;
packetToBuf(pkt, buf);
while (bytes_left > 0) {
bw = SDLNet_TCP_Send(s, ptr, bytes_left);
if (bw < 0) {
fprintf(stderr, "SERVER: SDLNet_TCP_Send(): %s\n", SDLNet_GetError());
SDLNet_TCP_Close(s);
exit(42);
} else if (bw == 0) {
SDL_Delay(1);
} else {
bytes_left -= bw;
ptr += bw;
}
}
}
void sendPacket(int playerid, NetPacket *pkt)
{
if ( (playerid < JNB_MAX_PLAYERS) && (playerid >= 0)) {
if ((player[playerid].enabled) && (playerid != client_player_num)) {
sendPacketToSock(net_info[playerid].sock, pkt);
}
}
}
void sendPacketToAll(NetPacket *pkt)
{
int i;
for (i = 0; i < JNB_MAX_PLAYERS; i++) {
sendPacket(i, pkt);
}
}
/** read a packet from the given TCPsocket
Returns -1 if some error occured, 0 if there was no data available and 1 if a
packet was successfully read.
Note: the socket has to be in the supplied socketset.
TODO: this function will bomb if a packet arrives in pieces, there is no
inherent guarantee that the next call will be made on the same socket. */
int grabPacket(TCPsocket s, SDLNet_SocketSet ss, NetPacket *pkt)
{
static char buf[NETPKTBUFSIZE];
static int buf_count = 0;
int rc;
if (SDLNet_CheckSockets(ss, 0) <= 0)
return 0;
if(!SDLNet_SocketReady(s))
return 0;
rc = SDLNet_TCP_Recv(s, &buf[buf_count], NETPKTBUFSIZE - buf_count);
if (rc <= 0) {
/* closed connection? */
return -1;
} else if (rc != NETPKTBUFSIZE) {
/* we got a partial packet. Store what we got in the static buffer and
return so that the next call can read the rest. Hopefully. */
buf_count = rc;
return 0;
} else {
buf_count = 0;
bufToPacket(buf, pkt);
return 1;
}
}
int serverRecvPacket(NetPacket *pkt)
{
int rc;
int i;
assert(is_server);
for (i = 0; i < JNB_MAX_PLAYERS; i++) {
TCPsocket s = net_info[i].sock;
if ((i == client_player_num) || (!player[i].enabled))
continue;
rc = grabPacket(s, net_info[i].socketset, pkt);
if (rc < 0) {
NetPacket pkt;
player[i].enabled = 0;
SDLNet_TCP_Close(s);
pkt.cmd = NETCMD_BYE;
pkt.arg = i;
pkt.arg2 = 0;
pkt.arg3 = 0;
pkt.arg4 = 0;
sendPacketToAll(&pkt);
} else if (rc > 0) {
return(i); /* it's all good. */
}
}
return(-1); /* no packets available currently. */
}
void wait_for_greenlight(void)
{
NetPacket pkt;
int i;
printf("CLIENT: Waiting for greenlight...\n");
do {
int rc;
while ((rc = grabPacket(sock, socketset, &pkt)) == 0) {
SDL_Delay(100); /* nap and then try again. */
}
if (rc < 0) {
printf("CLIENT: Lost connection.\n");
SDLNet_TCP_Close(sock);
exit(42);
}
} while (pkt.cmd != NETCMD_GREENLIGHT);
printf("CLIENT: Got greenlight.\n");
for (i = 0; i < JNB_MAX_PLAYERS; i++) {
if (pkt.arg & (1 << i)) {
printf("CLIENT: There is a player #%d.\n", i);
player[i].enabled = 1;
}
}
}
static int buggered_off = 0;
void tellServerGoodbye(void)
{
NetPacket pkt;
if (!buggered_off) {
buggered_off = 1;
pkt.cmd = NETCMD_BYE;
pkt.arg = client_player_num;
pkt.arg2 = 0;
pkt.arg3 = 0;
pkt.arg4 = 0;
sendPacketToSock(sock, &pkt);
}
}
#endif /* NO_SDL_NET */
void processMovePacket(NetPacket *pkt)
{
int playerid = pkt->arg;
int movetype = ((pkt->arg2 >> 16) & 0xFF);
int newval = ((pkt->arg2 >> 0) & 0xFF);
if (movetype == MOVEMENT_LEFT) {
player[playerid].action_left = newval;
} else if (movetype == MOVEMENT_RIGHT) {
player[playerid].action_right = newval;
} else if (movetype == MOVEMENT_UP) {
player[playerid].action_up = newval;
} else {
printf("bogus MOVE packet!\n");
}
player[playerid].x = pkt->arg3;
player[playerid].y = pkt->arg4;
}
void tellServerPlayerMoved(int playerid, int movement_type, int newval)
{
NetPacket pkt;
pkt.cmd = NETCMD_MOVE;
pkt.arg = playerid;
pkt.arg2 = ( ((movement_type & 0xFF) << 16) | ((newval & 0xFF) << 0) );
pkt.arg3 = player[playerid].x;
pkt.arg4 = player[playerid].y;
if (is_server) {
processMovePacket(&pkt);
#ifndef NO_SDL_NET
if (is_net)
sendPacketToAll(&pkt);
} else {
sendPacketToSock(sock, &pkt);
#endif
}
}
#ifndef NO_SDL_NET
void tellServerNewPosition(void)
{
NetPacket pkt;
pkt.cmd = NETCMD_POSITION;
pkt.arg = client_player_num;
pkt.arg2 = player[client_player_num].x;
pkt.arg3 = player[client_player_num].y;
if (is_server) {
sendPacketToAll(&pkt);
} else {
sendPacketToSock(sock, &pkt);
}
}
#endif /* NO_SDL_NET */
void processKillPacket(NetPacket *pkt)
{
int c1 = pkt->arg;
int c2 = pkt->arg2;
int x = pkt->arg3;
int y = pkt->arg4;
int c4 = 0;
int s1 = 0;
player[c1].y_add = -player[c1].y_add;
if (player[c1].y_add > -262144L)
player[c1].y_add = -262144L;
player[c1].jump_abort = 1;
player[c2].dead_flag = 1;
if (player[c2].anim != 6) {
player[c2].anim = 6;
player[c2].frame = 0;
player[c2].frame_tick = 0;
player[c2].image = player_anims[player[c2].anim].frame[player[c2].frame].image + player[c2].direction * 9;
if (main_info.no_gore == 0) {
for (c4 = 0; c4 < 6; c4++)
add_object(OBJ_FUR, (x >> 16) + 6 + rnd(5), (y >> 16) + 6 + rnd(5), (rnd(65535) - 32768) * 3, (rnd(65535) - 32768) * 3, 0, 44 + c2 * 8);
for (c4 = 0; c4 < 6; c4++)
add_object(OBJ_FLESH, (x >> 16) + 6 + rnd(5), (y >> 16) + 6 + rnd(5), (rnd(65535) - 32768) * 3, (rnd(65535) - 32768) * 3, 0, 76);
for (c4 = 0; c4 < 6; c4++)
add_object(OBJ_FLESH, (x >> 16) + 6 + rnd(5), (y >> 16) + 6 + rnd(5), (rnd(65535) - 32768) * 3, (rnd(65535) - 32768) * 3, 0, 77);
for (c4 = 0; c4 < 8; c4++)
add_object(OBJ_FLESH, (x >> 16) + 6 + rnd(5), (y >> 16) + 6 + rnd(5), (rnd(65535) - 32768) * 3, (rnd(65535) - 32768) * 3, 0, 78);
for (c4 = 0; c4 < 10; c4++)
add_object(OBJ_FLESH, (x >> 16) + 6 + rnd(5), (y >> 16) + 6 + rnd(5), (rnd(65535) - 32768) * 3, (rnd(65535) - 32768) * 3, 0, 79);
}
dj_play_sfx(SFX_DEATH, (unsigned short)(SFX_DEATH_FREQ + rnd(2000) - 1000), 64, -1);
player[c1].bumps++;
player[c1].bumped[c2]++;
s1 = player[c1].bumps % 100;
add_leftovers(0, 360, 34 + c1 * 64, s1 / 10, &number_gobs);
add_leftovers(1, 360, 34 + c1 * 64, s1 / 10, &number_gobs);
add_leftovers(0, 376, 34 + c1 * 64, s1 - (s1 / 10) * 10, &number_gobs);
add_leftovers(1, 376, 34 + c1 * 64, s1 - (s1 / 10) * 10, &number_gobs);
}
}
#ifndef NO_SDL_NET
void processPositionPacket(NetPacket *pkt)
{
int playerid = pkt->arg;
player[playerid].x = pkt->arg2;
player[playerid].y = pkt->arg3;
}
void processAlivePacket(NetPacket *pkt)
{
int playerid = pkt->arg;
player[playerid].dead_flag = 0;
player[playerid].x = pkt->arg2;
player[playerid].y = pkt->arg3;
}
void serverTellEveryoneGoodbye(void)
{
int i;
if (!buggered_off) {
buggered_off = 1;
for (i = 0; i < JNB_MAX_PLAYERS; i++) {
if (player[i].enabled) {
NetPacket pkt;
pkt.cmd = NETCMD_BYE;
pkt.arg = i;
pkt.arg2 = 0;
pkt.arg3 = 0;
pkt.arg4 = 0;
sendPacketToAll(&pkt);
}
}
}
}
int server_said_bye = 0;
int update_players_from_server(void)
{
NetPacket pkt;
int rc;
assert(!is_server);
while ((rc = grabPacket(sock, socketset, &pkt)) != 0) {
if (rc < 0) {
printf("CLIENT: Lost connection.\n");
pkt.cmd = NETCMD_BYE;
pkt.arg = client_player_num;
}
if (pkt.cmd == NETCMD_BYE) {
if (pkt.arg == client_player_num) {
SDLNet_FreeSocketSet(socketset);
SDLNet_TCP_Close(sock);
sock = NULL;
server_said_bye = 1;
return(0);
} else {
player[pkt.arg].enabled = 0;
}
} else if (pkt.cmd == NETCMD_MOVE) {
processMovePacket(&pkt);
} else if (pkt.cmd == NETCMD_ALIVE) {
processAlivePacket(&pkt);
} else if (pkt.cmd == NETCMD_POSITION) {
processPositionPacket(&pkt);
} else if (pkt.cmd == NETCMD_KILL) {
processKillPacket(&pkt);
} else {
printf("CLIENT: Got an unknown packet: 0x%uX.\n", (unsigned int) pkt.cmd);
}
}
return(1);
}
void serverSendAlive(int playerid)
{
NetPacket pkt;
assert(is_server);
pkt.cmd = NETCMD_ALIVE;
pkt.arg = playerid;
pkt.arg2 = player[playerid].x;
pkt.arg3 = player[playerid].y;
sendPacketToAll(&pkt);
}
#endif /* NO_SDL_NET */
void serverSendKillPacket(int killer, int victim)
{
NetPacket pkt;
assert(is_server);
pkt.cmd = NETCMD_KILL;
pkt.arg = killer;
pkt.arg2 = victim;
pkt.arg3 = player[victim].x;
pkt.arg4 = player[victim].y;
processKillPacket(&pkt);
#ifndef NO_SDL_NET
if (is_net)
sendPacketToAll(&pkt);
#endif
}
#ifndef NO_SDL_NET
void update_players_from_clients(void)
{
int i;
NetPacket pkt;
int playerid;
assert(is_server);
while ((playerid = serverRecvPacket(&pkt)) >= 0) {
if (pkt.cmd == NETCMD_BYE) {
pkt.arg = playerid; /* just in case. */
sendPacketToAll(&pkt);
player[playerid].enabled = 0;
SDLNet_FreeSocketSet(net_info[playerid].socketset);
SDLNet_TCP_Close(net_info[playerid].sock);
} else if (pkt.cmd == NETCMD_POSITION) {
pkt.arg = playerid; /* just in case. */
processPositionPacket(&pkt);
for (i = 0; i < JNB_MAX_PLAYERS; i++) {
if (i != playerid) {
sendPacket(i, &pkt);
}
}
} else if (pkt.cmd == NETCMD_MOVE) {
pkt.arg = playerid; /* just in case. */
/*
pkt.arg3 = player[playerid].x;
pkt.arg4 = player[playerid].y;
*/
processMovePacket(&pkt);
sendPacketToAll(&pkt);
} else {
printf("SERVER: Got unknown packet (0x%uX).\n", (unsigned int) pkt.cmd);
}
}
}
void init_server(const char *netarg)
{
NetPacket pkt;
IPaddress addr;
int i;
int wait_for_clients = ((netarg == NULL) ? 0 : atoi(netarg));
const char *ipstr;
/** assign player number zero as default for the server */
if(-1 == client_player_num)
client_player_num = 0;
if ((wait_for_clients >= JNB_MAX_PLAYERS) || (wait_for_clients < 0)) {
printf("SERVER: Waiting for bogus client count (%d).\n", wait_for_clients);
exit(42);
}
if (SDLNet_Init() < 0) {
exit(42);
}
atexit(SDLNet_Quit);
SDLNet_ResolveHost(&addr, NULL, JNB_INETPORT);
ipstr = SDLNet_ResolveIP(&addr);
SDLNet_ResolveHost(&addr, ipstr, JNB_INETPORT);
printf("SERVER: we are %s (%i.%i.%i.%i:%i).\n", ipstr, (addr.host >> 0) & 0xff, (addr.host >> 8) & 0xff, (addr.host >> 16) & 0xff, (addr.host >> 24) & 0xff, addr.port);
net_info[client_player_num].addr = addr;
addr.host = INADDR_ANY;
sock = SDLNet_TCP_Open(&addr);
if (sock == NULL) {
fprintf(stderr, "SERVER: SDLNet_TCP_Open(): %s\n", SDLNet_GetError());
exit(42);
}
player[client_player_num].enabled = 1;
printf("SERVER: waiting for (%d) clients...\n", wait_for_clients);
socketset = SDLNet_AllocSocketSet(JNB_MAX_PLAYERS + 1);
SDLNet_TCP_AddSocket(socketset, sock);
while (wait_for_clients > 0)
{
char buf[NETPKTBUFSIZE];
IPaddress *from;
int negatory = 1;
int br;
TCPsocket s;
/* Wait for events */
SDLNet_CheckSockets(socketset, ~0);
if ( SDLNet_SocketReady(sock) ) {
s = SDLNet_TCP_Accept(sock);
if (s == NULL)
{
fprintf(stderr, "SERVER: SDLNet_TCP_Accept(): %s", SDLNet_GetError());
SDLNet_TCP_Close(sock);
exit(42);
}
} else
continue;
br = SDLNet_TCP_Recv(s, buf, NETPKTBUFSIZE);
if (br < 0) {
fprintf(stderr, "SERVER: SDLNet_TCP_Recv(): %s\n", SDLNet_GetError());
SDLNet_TCP_Close(s);
SDLNet_TCP_Close(sock);
exit(42);
}
from = SDLNet_TCP_GetPeerAddress(s);
ipstr = SDLNet_ResolveIP(from);
printf("SERVER: Got data from %s (%i.%i.%i.%i:%i).\n", ipstr, (from->host >> 0) & 0xff, (from->host >> 8) & 0xff, (from->host >> 16) & 0xff, (from->host >> 24) & 0xff, from->port);
if (br != NETPKTBUFSIZE) {
printf("SERVER: Bogus packet.\n");
continue;
}
bufToPacket(buf, &pkt);
if (pkt.cmd != NETCMD_HELLO) {
printf("SERVER: Bogus packet.\n");
continue;
}
printf("SERVER: Client claims to be player #%d.\n", (int) pkt.arg);
if (-1 == pkt.arg) {
int i;
for(i=0; i!=JNB_MAX_PLAYERS; ++i) {
if(!player[i].enabled) {
printf("SERVER: assigning %d as player number\n", i);
pkt.arg = i;
break;
}
}
}
if ((pkt.arg>=JNB_MAX_PLAYERS)||(pkt.arg<0)) {
printf("SERVER: (that's an invalid player number.)\n");
} else if (player[pkt.arg].enabled) {
printf("SERVER: (that player number is already taken.)\n");
} else {
negatory = 0;
}
if (negatory) {
printf("SERVER: Forbidding connection.\n");
pkt.cmd = NETCMD_NACK;
sendPacketToSock(s, &pkt);
SDLNet_TCP_Close(s);
} else {
player[pkt.arg].enabled = 1;
net_info[pkt.arg].sock = s;
net_info[pkt.arg].addr = *from;
net_info[pkt.arg].socketset = SDLNet_AllocSocketSet(1);
SDLNet_TCP_AddSocket(net_info[pkt.arg].socketset, net_info[pkt.arg].sock);
wait_for_clients--;
printf("SERVER: Granting connection. (%d) to go.\n", wait_for_clients);
pkt.cmd = NETCMD_ACK;
sendPacket(pkt.arg, &pkt);
}
}
SDLNet_TCP_Close(sock); /* done with the listen socket. */
SDLNet_FreeSocketSet(socketset);
sock = NULL;
socketset = NULL;
printf("SERVER: Got all our connections. Greenlighting clients...\n");
pkt.cmd = NETCMD_GREENLIGHT;
pkt.arg = 0;
for (i = 0; i < JNB_MAX_PLAYERS; i++) {
if (player[i].enabled) {
pkt.arg |= (1 << i);
}
}
sendPacketToAll(&pkt);
}
void connect_to_server(char *netarg)
{
NetPacket pkt;
char buf[NETPKTBUFSIZE];
const char *ipstr;
IPaddress hent;
IPaddress addr;
int br;
if (netarg == NULL) {
printf("CLIENT: Need to specify host to connect to.\n");
exit(42);
}
if (SDLNet_Init() < 0) {
exit(42);
}
atexit(SDLNet_Quit);
SDLNet_ResolveHost(&addr, NULL, JNB_INETPORT);
ipstr = SDLNet_ResolveIP(&addr);
SDLNet_ResolveHost(&addr, ipstr, JNB_INETPORT);
printf("CLIENT: we are %s (%i.%i.%i.%i:%i).\n", ipstr, (addr.host >> 0) & 0xff, (addr.host >> 8) & 0xff, (addr.host >> 16) & 0xff, (addr.host >> 24) & 0xff, addr.port);
if (SDLNet_ResolveHost(&hent, netarg, JNB_INETPORT) < 0) {
fprintf(stderr, "CLIENT: couldn't find host: %s\n", SDLNet_GetError());
exit(42);
}
sock = SDLNet_TCP_Open(&hent);
if (sock == NULL) {
fprintf(stderr, "CLIENT: SDLNet_TCP_Open(): %s\n", SDLNet_GetError());
exit(42);
}
socketset = SDLNet_AllocSocketSet(1);
SDLNet_TCP_AddSocket(socketset, sock);
printf("CLIENT: connected to %s...\n", SDLNet_ResolveIP(&hent));
printf("CLIENT: Sending HELLO packet...\n");
pkt.cmd = NETCMD_HELLO;
pkt.arg = client_player_num;
sendPacketToSock(sock, &pkt);
printf("CLIENT: Waiting for ACK from server...\n");
br = SDLNet_TCP_Recv(sock, buf, NETPKTBUFSIZE);
if (br < 0) {
fprintf(stderr, "CLIENT: recv(): %s\n", SDLNet_GetError());
SDLNet_FreeSocketSet(socketset);
SDLNet_TCP_Close(sock);
exit(42);
}
if (br != NETPKTBUFSIZE) {
printf("CLIENT: Bogus packet size (%d of %d). FIXME.\n", br, NETPKTBUFSIZE);
SDLNet_FreeSocketSet(socketset);
SDLNet_TCP_Close(sock);
exit(42);
}
bufToPacket(buf, &pkt);
if (pkt.cmd == NETCMD_NACK) {
printf("CLIENT: Server forbid us from playing.\n");
SDLNet_FreeSocketSet(socketset);
SDLNet_TCP_Close(sock);
exit(42);
}
if (pkt.cmd != NETCMD_ACK) {
printf("CLIENT: Unexpected packet (cmd=0x%uX).\n", (unsigned int) pkt.cmd);
SDLNet_FreeSocketSet(socketset);
SDLNet_TCP_Close(sock);
exit(42);
}
client_player_num = pkt.arg;
player[client_player_num].enabled = 1;
net_info[client_player_num].addr = addr;
printf("CLIENT: Server accepted our connection.\n");
wait_for_greenlight();
}
#endif /* NO_SDL_NET */
static void flip_pixels(char *pixels)
{
int x,y;
char temp;
assert(pixels);
for (y = 0; y < JNB_HEIGHT; y++) {
for (x = 0; x < (352/2); x++) {
temp = pixels[y*JNB_WIDTH+x];
pixels[y*JNB_WIDTH+x] = pixels[y*JNB_WIDTH+(352-x)-1];
pixels[y*JNB_WIDTH+(352-x)-1] = temp;
}
}
}
int main(int argc, char *argv[])
{