-
Notifications
You must be signed in to change notification settings - Fork 17
/
bot.c
1166 lines (984 loc) · 26.6 KB
/
bot.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
#ifndef WIN32
#include "config.h"
#endif
#include <stdio.h>
#include <signal.h>
#include <math.h>
#ifdef HAVE_FLOAT_H
#include <float.h>
#endif
#ifdef HAVE_SIGINFO_H
#include <siginfo.h>
#endif
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#include <ctype.h>
#if (!defined(WIN32))
#ifdef TIME_WITH_SYS_TIME
#include <sys/time.h>
#include <time.h>
#else
#ifdef TM_IN_SYS_TIME
#include <sys/time.h>
#else
#include <time.h>
#endif
#endif
#else
#include <time.h>
#endif
#include <errno.h>
#include <stdlib.h>
#ifndef WIN32
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#ifndef __USE_GNU
#define __USE_GNU
#endif
#else
#include <winsock.h>
#endif
#include <string.h>
#include "sprite.h"
#include "data.h"
#include "cfg.h"
#include "net.h"
#include "hash.h"
#include "time.h"
#include "math.h"
#include "getopt.h"
#include "error.h"
#ifdef WIN32
int consoleApp=1;
#endif
#define N_NAMES 18
#define CAN_SEE_X 60
#define CAN_SEE_Y 10
/* some empty functions and variables */
void c_shutdown(void){}
void init_blit(void){}
void shutdown_blit(void){}
int SCREEN_X=1,SCREEN_Y=1;
unsigned char *screen,*screen_a;
#ifdef TRI_D
unsigned char *screen2,*screen2_a;
#endif
static int in_signal_handler=0;
static int level_sprites_start;
static int level_number=-1;
/* my health, armor, frags, deaths, ammo, ... and ID */
static unsigned char health,armor;
static unsigned int frags,deaths;
static unsigned short ammo[ARMS];
static unsigned char current_weapon;
static unsigned char weapons;
static int my_id;
/* for use with the -n option */
static char *static_name=NULL;
/* connection with server */
static int connected=0;
int console_ok=1;
/* networking */
int fd; /* socket */
static struct sockaddr_in server; /* server address */
/* objects */
static struct object_list objects;
struct object_list *last_obj;
static struct it* hero;
static unsigned long_long game_start_offset; /* time difference between game start on this machine and on server */
static struct /* keyboard status */
{
unsigned char right,left,jump,creep,speed,fire,weapon,down_ladder;
}keyboard_status;
static char *names[N_NAMES]={
"Terminator",
"Jack The Ripper",
"Rambo",
"Exhumator",
"Assassin",
"Arnold",
"Necromancer",
"Predator",
"Rocky",
"Harvester",
"Lamer",
"Killme",
"Looser",
"Krueger",
"I'll kill you",
"Zombieman",
"Hellraiser",
"Eraser"
};
static int direction=0; /* 0=stop, 1=left, 2=right */
static int const1,const2,const3,const4;
static unsigned short port=DEFAULT_PORT;
static char *host;
static int priority;
/* 0=nothing
* 1=kill player
* 2=find rifle
* 3=find shotgun
* 4=find UZI
* 5=find grenades
* 6=find gun ammo
* 7=find shotgun ammo
* 8=find rifle ammo
* 9=find uzi ammo
* 10=find medikit
*/
/*-----------------------------------------------------------------------*/
#define can_see(a,b) (a<add_int(hero->x,CAN_SEE_X)&&a>sub_int(hero->x,CAN_SEE_X)&&b>sub_int(hero->y,CAN_SEE_Y)&&b<add_int(hero->y,CAN_SEE_Y))
static int odds(int p)
{
return (random()%1000)<p;
}
/* free all before exit */
static void clear_memory(void)
{
struct object_list *o;
for (o=&objects;o->next;)
delete_obj(o->next->member.id);
free_area();
shutdown_sprites();
free_sprites(0);
}
/* shut down the client */
static void shut_down(int a)
{
if (a)
{
clear_memory();
free_packet_buffer();
check_memory_leaks();
EXIT(0);
}
}
/* find address of server and fill the server address structure */
static char * find_server(char *name,unsigned short port)
{
struct hostent *h;
h=gethostbyname(name);
if (!h)return "Error: Can't resolve server address.\n";
server.sin_family=AF_INET;
server.sin_port=htons(port);
server.sin_addr=*((struct in_addr*)(h->h_addr_list[0]));
return 0;
}
/* initialize socket */
static char * init_socket(void)
{
fd=socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
if(fd<0)return "Can't get socket.\n";
return 0;
}
#define MAX_COUNT 32
/* send quit request to server */
static void send_quit(void)
{
char p;
fd_set rfds;
struct timeval tv;
int a=sizeof(server);
int count=0;
tv.tv_sec=2;
tv.tv_usec=0;
FD_ZERO(&rfds);
FD_SET(fd,&rfds);
send_again:
p=P_QUIT_REQUEST;
count ++;
send_packet(&p,1,(struct sockaddr *)(&server),my_id,0);
if (!select(fd+1,&rfds,0,0,&tv)&&count<=MAX_COUNT)goto send_again;
recv_packet(&p,1,(struct sockaddr*)(&server),&a,1,my_id,0);
if (p!=P_PLAYER_DELETED&&count<=MAX_COUNT)goto send_again;
}
#undef MAX_COUNT
/* initiate connection with server */
static char * contact_server(int color, char *name)
{
static char packet[256];
int l=strlen(name)+1;
int a,r;
int min,maj;
fd_set fds;
struct timeval tv;
tv.tv_sec=4;
tv.tv_usec=0;
FD_ZERO(&fds);
FD_SET(fd,&fds);
packet[0]=P_NEW_PLAYER;
packet[1]=0;
packet[2]=VERSION_MAJOR;
packet[3]=VERSION_MINOR;
packet[4]=color;
if (l>MAX_NAME_LEN) {l=MAX_NAME_LEN; packet[5+MAX_NAME_LEN]='\0';}
memcpy(packet+5,name,l);
printf("Using name \"%s\".\n", packet+5);
send_packet(packet,l+5,(struct sockaddr*)(&server),my_id,0);
if (!select(fd+1,&fds,NULL,NULL,&tv))return "No reply within 4 seconds.\n";
if ((r=recv_packet(packet,256,0,0,1,0,0))<0)
{
if (errno==EINTR)return "Server hung up.\n";
else return "Connection error.\n";
}
switch(*packet)
{
case P_PLAYER_REFUSED:
switch(packet[1])
{
case E_INCOMPATIBLE_VERSION:
return "Incompatible client version. Connection refused.\n";
case E_NAME_IN_USE:
return "This name is already in use. Connection refused.\n";
default:
return "Connection refused.\n";
}
case P_PLAYER_ACCEPTED:
my_id=get_int(packet+35);
if (r<41){send_quit();return "Incompatible server version. Givin' up.\n";}
maj=packet[39];
min=packet[40];
if (maj!=VERSION_MAJOR||min<MIN_SERVER_VERSION_MINOR)
{send_quit();return "Incompatible server version. Givin' up.\n";}
game_start_offset=get_time();
game_start_offset-=get_long_long(packet+27);
health=100;
armor=0;
for(a=0;a<ARMS;a++)
ammo[a]=0;
ammo[0]=weapon[0].basic_ammo;
current_weapon=0;
weapons = WEAPON_MASK_GUN |
WEAPON_MASK_GRENADE |
WEAPON_MASK_CHAINSAW |
WEAPON_MASK_BLOODRAIN; /* gun, grenades, chainsaw
and bloodrain */
hero=new_obj(
get_int(packet+1), /* ID */
T_PLAYER, /* type */
0, /* time to live */
get_int16(packet+5), /* sprite */
0, /* position */
get_int(packet+23), /* status */
get_int(packet+7), /* X */
get_int(packet+11), /* Y */
get_int(packet+15), /* XSPEED */
get_int(packet+19), /* YSPEED */
0
);
break;
default:
return "Connection error.\n";
}
return 0;
}
/* I want to be born again */
static void send_reenter_game(void)
{
char packet;
packet=P_REENTER_GAME;
send_packet(&packet,1,(struct sockaddr*)(&server),my_id,0);
}
/* send chat message */
static void send_message(char *msg)
{
static char packet[MAX_MESSAGE_LENGTH + 2];
int len;
len = strlen(msg) + 1;
packet[0] = P_MESSAGE;
packet[1] = 0x00;
memcpy(packet + 2, msg, len);
send_packet(packet, len + 2, (struct sockaddr *)(&server), my_id, 0);
}
/* send end of game to server */
static void send_keyboard(void)
{
char packet[3];
packet[0]=P_KEYBOARD;
packet[1]= keyboard_status.right|
(keyboard_status.left<<1)|
(keyboard_status.jump<<2)|
(keyboard_status.creep<<3)|
(keyboard_status.speed<<4)|
(keyboard_status.fire<<5)|
(keyboard_status.down_ladder<<6);
packet[2]= keyboard_status.weapon;
send_packet(packet,3,(struct sockaddr*)(&server),my_id,0);
}
static void reset_keyboard(void)
{
keyboard_status.left=0;
keyboard_status.right=0;
keyboard_status.speed=1;
keyboard_status.jump=0;
keyboard_status.creep=0;
keyboard_status.fire=0;
keyboard_status.weapon=0;
keyboard_status.down_ladder=0;
}
static void test_object(struct it *obj)
{
if (obj==hero)return;
if (!can_see(obj->x,obj->y))return;
switch(obj->type)
{
case T_PLAYER:
if (obj->status&S_INVISIBLE)
break;
if (priority>1)
break;
priority=1;
keyboard_status.fire=1;
if (obj->x>hero->x)direction=2;
else direction=1;
if (my_abs(obj->x-hero->x)<int2double(30))direction=0;
break;
case T_BULLET:
case T_SHRAPNEL:
case T_BFGCELL:
case T_CHAIN:
if ((my_sgn(hero->x-obj->x)==my_sgn(obj->xspeed)||my_abs(hero->x-obj->x)<int2double(20))&&obj->y>=hero->y&&obj->y<hero->y+int2double(PLAYER_HEIGHT))
keyboard_status.creep=1;
break;
case T_RIFLE:
if (priority>2)break;
if ((weapons&(1<<WEAPON_RIFLE))&&ammo[WEAPON_RIFLE])break;
priority=2;
if (obj->x>hero->x)direction=2;
else direction=1;
break;
case T_SHOTGUN:
if (priority>3)break;
if ((weapons&(1<<WEAPON_SHOTGUN))&&ammo[WEAPON_SHOTGUN])break;
priority=3;
if (obj->x>hero->x)direction=2;
else direction=1;
break;
case T_UZI:
if (priority>4)break;
if ((weapons&(1<<WEAPON_UZI))&&ammo[WEAPON_UZI])break;
priority=4;
if (obj->x>hero->x)direction=2;
else direction=1;
break;
case T_AMMO_GRENADE:
if (priority>5)break;
if (!(weapons&(1<<WEAPON_GRENADE))||ammo[WEAPON_GRENADE])break;
priority=5;
if (obj->x>hero->x)direction=2;
else direction=1;
break;
case T_AMMO_GUN:
if (priority>6)break;
if (!(weapons&(1<<WEAPON_GUN))||ammo[WEAPON_GUN])break;
priority=6;
if (obj->x>hero->x)direction=2;
else direction=1;
break;
case T_AMMO_SHOTGUN:
if (priority>7)break;
if (!(weapons&(1<<WEAPON_SHOTGUN))||ammo[WEAPON_SHOTGUN])break;
priority=7;
if (obj->x>hero->x)direction=2;
else direction=1;
break;
case T_AMMO_RIFLE:
if (priority>8)break;
if (!(weapons&(1<<WEAPON_RIFLE))||ammo[WEAPON_RIFLE])break;
priority=8;
if (obj->x>hero->x)direction=2;
else direction=1;
break;
case T_AMMO_UZI:
if (priority>9)break;
if (!(weapons&(1<<WEAPON_UZI))||ammo[WEAPON_UZI])break;
priority=9;
if (obj->x>hero->x)direction=2;
else direction=1;
break;
case T_MEDIKIT:
if (priority>10)break;
if (health>70)break;
priority=10;
if (obj->x>hero->x)direction=2;
else direction=1;
break;
}
}
/* destroys all objects except hero (before level change) */
static void clean_memory(void)
{
struct object_list *o;
/* delete all objects except hero */
for (o=&objects;o->next;)
if ((hero->id)!=(o->next->member.id))delete_obj(o->next->member.id);
else o=o->next;
}
static void change_level(void)
{
char *LEVEL;
char txt[256];
clean_memory();
free_sprites(level_sprites_start);
reinit_area();
LEVEL=load_level(level_number);
snprintf(txt,256,"Loading level \"%s\".\n",LEVEL);
ERROR(txt);
snprintf(txt,256,"%s%s%s",DATA_PATH,LEVEL,LEVEL_SPRITES_SUFFIX);
load_sprites(txt);
snprintf(txt,256,"%s%s%s",DATA_PATH,LEVEL,STATIC_DATA_SUFFIX);
load_data(txt);
mem_free(LEVEL);
}
/* recompute object positions */
static void update_game(void)
{
struct object_list *p;
int w,h;
unsigned char stop_x,stop_y,sy;
unsigned long_long t;
int x,y,x1,y1,DELTA_TIME;
for(p=&objects;p->next;p=p->next)
{
if (p->next->member.type == T_NOTHING)
continue;
if (p->next->member.status & S_DEAD)
continue; /* dead player */
/* decrement time to live */
if (p->next->member.ttl>0)
{
p->next->member.ttl--;
if (!p->next->member.ttl)
{
if ((p->next->member.type)==T_PLAYER)
p->next->member.status &= ~S_SHOOTING;
else
{
if (p->next->member.type!=T_GRENADE){ /* client's waiting for P_EXPLODE_GRENADE and doesn't delete the grenade yet */
p=p->prev;
delete_obj(p->next->next->member.id);
continue;}
}
}
}
test_object(&(p->next->member));
/* maintain only objects that you are allowed to maintain */
if (!(obj_attr[p->next->member.type].maintainer&1))continue;
/* if not falling slow down x motion */
if (!(p->next->member.status & S_FALLING))
p->next->member.xspeed=mul(p->next->member.xspeed,obj_attr[p->next->member.type].slow_down_x);
/* fall */
if (obj_attr[p->next->member.type].fall)
{
p->next->member.status |= S_FALLING;
p->next->member.yspeed+=FALL_ACCEL;
/* but not too fast */
if (p->next->member.yspeed>MAX_Y_SPEED)p->next->member.yspeed=MAX_Y_SPEED;
}
get_dimensions(p->next->member.type,p->next->member.status,sprites[p->next->member.sprite].positions,&w,&h);
x=p->next->member.x;
y=p->next->member.y;
t=get_time();
DELTA_TIME=float2double(((double)(long_long)(t-p->next->member.last_updated))/MICROSECONDS);
update_position(
&(p->next->member),
p->next->member.x+mul(p->next->member.xspeed,DELTA_TIME),
p->next->member.y+mul(p->next->member.yspeed,DELTA_TIME),
w,h,&stop_x,&stop_y);
p->next->member.last_updated=t;
/* walk up the stairs */
if (stop_x&&p->next->member.type==T_PLAYER&&!(p->next->member.status & S_CREEP))
{
x1=p->next->member.x;
y1=p->next->member.y;
p->next->member.x=x;
p->next->member.y=y-int2double(1);
update_position(
&(p->next->member),
p->next->member.x+mul(p->next->member.xspeed,DELTA_TIME),
p->next->member.y+mul(p->next->member.yspeed,DELTA_TIME),
w,h,0,&sy);
if ((p->next->member.xspeed>0&&p->next->member.x<=x1)||(p->next->member.xspeed<0&&p->next->member.x>=x1)) /* restore original values */
{
p->next->member.x=x1;
p->next->member.y=y1;
}
else
{
stop_y=sy;
stop_x=0;
}
}
if (stop_x)p->next->member.xspeed=-mul(p->next->member.xspeed,obj_attr[p->next->member.type].bounce_x);
if (my_abs(p->next->member.xspeed)<MIN_X_SPEED)
{
p->next->member.xspeed=0;
p->next->member.status &= ~S_WALKING;
}
if (stop_y)
{
p->next->member.yspeed=mul(p->next->member.yspeed,obj_attr[p->next->member.type].bounce_y);
p->next->member.yspeed=-p->next->member.yspeed;
if (my_abs(p->next->member.yspeed)<MIN_Y_SPEED)
{
p->next->member.yspeed=0;
if (stop_y==1)p->next->member.status &= ~S_FALLING;
}
}
if ((p->next->member.type == T_SHRAPNEL || p->next->member.type == T_BULLET ||
p->next->member.type == T_BFGCELL || p->next->member.type == T_CHAIN) &&
(stop_x || stop_y)) { /* bullet and shrapnel die crashing into wall */
p=p->prev; /* deleting object makes a great mess in for cycle, so we must cheat the cycle */
delete_obj(p->next->next->member.id);
continue;
}
}
}
/* returns number of read bytes */
static int process_packet(char *packet,int l)
{
int a,n=l;
switch(*packet)
{
case P_CHUNK:
for (a=1;a<l&&a<MAX_PACKET_LENGTH;a+=n)
n=process_packet(packet+a,l-a);
break;
case P_NEW_OBJ:
if (l < (n=30))break; /* invalid packet */
new_obj(
get_int(packet+1), /* ID */
packet[27], /* type */
get_int16(packet+28), /* time to live */
get_int16(packet+5), /* sprite */
0, /* anim position */
get_int(packet+23), /* status */
get_int(packet+7), /* x */
get_int(packet+11), /* y */
get_int(packet+15), /* xspeed */
get_int(packet+19), /* yspeed */
0 /* data */
);
break;
case P_PLAYER_DELETED:
n=1;
shut_down(1);
break;
case P_DELETE_OBJECT:
if (l<5)break; /* invalid packet */
delete_obj(get_int(packet+1));
n=5;
break;
case P_UPDATE_OBJECT:
if (l < (n=28))break; /* invalid packet */
{
struct object_list *p;
p=find_in_table(get_int(packet+1));
if (!p)break;
if(packet[5]-(p->member.update_counter)>127)break; /* throw out old updates */
p->member.update_counter=packet[5];
p->member.x=get_int(packet+6);
p->member.y=get_int(packet+10);
p->member.xspeed=get_int(packet+14);
p->member.yspeed=get_int(packet+18);
p->member.status=get_int(packet+22);
p->member.data=0;
p->member.ttl=get_int16(packet+26);
/* kdyz tasi, tak se nahodi ttl */
if (p->member.type==T_PLAYER&&(p->member.status & S_HOLDING)&&(p->member.status & S_SHOOTING))
p->member.ttl=weapon[current_weapon].cadence+HOLD_GUN_AFTER_SHOOT;
}
break;
case P_UPDATE_OBJECT_POS:
if (l<22)break; /* invalid packet */
{
struct object_list *p;
n=22;
p=find_in_table(get_int(packet+1));
if (!p)break;
if(packet[5]-(p->member.update_counter)>127)break; /* throw out old updates */
p->member.update_counter=packet[5];
p->member.x=get_int(packet+6);
p->member.y=get_int(packet+10);
p->member.xspeed=get_int(packet+14);
p->member.yspeed=get_int(packet+18);
}
break;
case P_UPDATE_OBJECT_SPEED:
if (l<14)break; /* invalid packet */
{
struct object_list *p;
n=14;
p=find_in_table(get_int(packet+1));
if (!p)break;
if(packet[5]-(p->member.update_counter)>127)break; /* throw out old updates */
p->member.update_counter=packet[5];
p->member.xspeed=get_int(packet+6);
p->member.yspeed=get_int(packet+10);
}
break;
case P_UPDATE_OBJECT_COORDS:
if (l<14)break; /* invalid packet */
{
struct object_list *p;
n=14;
p=find_in_table(get_int(packet+1));
if (!p)break;
if(packet[5]-(p->member.update_counter)>127)break; /* throw out old updates */
p->member.update_counter=packet[5];
p->member.x=get_int(packet+6);
p->member.y=get_int(packet+10);
}
break;
case P_UPDATE_OBJECT_SPEED_STATUS:
if (l < (n=18))break; /* invalid packet */
{
struct object_list *p;
p=find_in_table(get_int(packet+1));
if (!p)break;
if(packet[5]-(p->member.update_counter)>127)break; /* throw out old updates */
p->member.update_counter=packet[5];
p->member.xspeed=get_int(packet+6);
p->member.yspeed=get_int(packet+10);
p->member.status=get_int(packet+14);
/* kdyz tasi, tak se nahodi ttl */
if (p->member.type==T_PLAYER&&(p->member.status & S_HOLDING)&&(p->member.status & S_SHOOTING))
p->member.ttl=weapon[current_weapon].cadence+HOLD_GUN_AFTER_SHOOT;
}
break;
case P_UPDATE_OBJECT_COORDS_STATUS:
if (l < (n=18))break; /* invalid packet */
{
struct object_list *p;
p=find_in_table(get_int(packet+1));
if (!p)break;
if(packet[5]-(p->member.update_counter)>127)break; /* throw out old updates */
p->member.update_counter=packet[5];
p->member.x=get_int(packet+6);
p->member.y=get_int(packet+10);
p->member.status=get_int(packet+14);
/* kdyz tasi, tak se nahodi ttl */
if (p->member.type==T_PLAYER&&(p->member.status & S_HOLDING)&&(p->member.status & S_SHOOTING))
p->member.ttl=weapon[current_weapon].cadence+HOLD_GUN_AFTER_SHOOT;
}
break;
case P_UPDATE_OBJECT_SPEED_STATUS_TTL:
if (l < (n=20))break; /* invalid packet */
{
struct object_list *p;
p=find_in_table(get_int(packet+1));
if (!p)break;
if(packet[5]-(p->member.update_counter)>127)break; /* throw out old updates */
p->member.update_counter=packet[5];
p->member.xspeed=get_int(packet+6);
p->member.yspeed=get_int(packet+10);
p->member.status=get_int(packet+14);
p->member.ttl=get_int16(packet+18);
/* kdyz tasi, tak se nahodi ttl */
if (p->member.type==T_PLAYER&&(p->member.status & S_HOLDING)&&(p->member.status & S_SHOOTING))
p->member.ttl=weapon[current_weapon].cadence+HOLD_GUN_AFTER_SHOOT;
}
break;
case P_UPDATE_OBJECT_COORDS_STATUS_TTL:
if (l < (n=20))break; /* invalid packet */
{
struct object_list *p;
p=find_in_table(get_int(packet+1));
if (!p)break;
if(packet[5]-(p->member.update_counter)>127)break; /* throw out old updates */
p->member.update_counter=packet[5];
p->member.x=get_int(packet+6);
p->member.y=get_int(packet+10);
p->member.status=get_int(packet+14);
p->member.ttl=get_int16(packet+18);
/* kdyz tasi, tak se nahodi ttl */
if (p->member.type==T_PLAYER&&(p->member.status & S_HOLDING)&&(p->member.status & S_SHOOTING))
p->member.ttl=weapon[current_weapon].cadence+HOLD_GUN_AFTER_SHOOT;
}
break;
case P_UPDATE_STATUS:
if (l < (n=9))break; /* invalid packet */
{
struct object_list *p;
p=find_in_table(get_int(packet+1));
if (!p)break; /* ignore objects we don't have */
p->member.status=get_int(packet+5);
/* kdyz tasi, tak se nahodi ttl */
if (p->member.type==T_PLAYER&&(p->member.status & S_HOLDING)&&(p->member.status & S_SHOOTING))
p->member.ttl=weapon[current_weapon].cadence+HOLD_GUN_AFTER_SHOOT;
}
break;
case P_HIT:
if (l<8)break; /* invalid packet */
{
struct object_list *p;
n=8;
p=find_in_table(get_int(packet+1));
if (!p)break; /* ignore objects we don't have */
p->member.status |= S_HIT;
p->member.data=(void*)(long)((packet[5]<<16)+(packet[7]<<8)+(packet[6]));
/* kdyz tasi, tak se nahodi ttl */
if (p->member.type==T_PLAYER&&(p->member.status & S_HOLDING)&&(p->member.status & S_SHOOTING))
p->member.ttl=weapon[current_weapon].cadence+HOLD_GUN_AFTER_SHOOT;
}
break;
case P_UPDATE_PLAYER:
if (l<15+2*ARMS)break; /* invalid packet */
health=packet[1];
armor=packet[2];
for (a=0;a<ARMS;a++)
ammo[a]=get_int16(packet+3+(a<<1));
frags=get_int(packet+3+ARMS*2);
deaths=get_int(packet+7+ARMS*2);
current_weapon=get_int16(packet+11+2*ARMS);
weapons=get_int16(packet+13+2*ARMS);
n=15+2*ARMS;
break;
case P_MESSAGE:
if (l < 3)
break; /* invalid packet */
n = 3 + strlen(packet + 2);
break;
case P_END:
if (l<2)printf("Game terminated.\n");
else printf("Game terminated by %s.\n",packet+1);
n=2+strlen(packet+1);
shut_down(1);
case P_INFO:
if (l<=5)break;
l=6;
for (a=0;a<packet[5]&&a<TOP_PLAYERS_N;a++)
{
int x;
x=strlen(packet+l+9)+1;
l+=x+9;
}
n=l;
break;
case P_EXPLODE_GRENADE:
case P_EXPLODE_BFG:
{
unsigned int i,j;
struct object_list *p;
int b;
if (l<9)break;
n=9;
i=get_int(packet+1);
j=get_int(packet+5);
p=find_in_table(j);
if (!p)break;
for (b=0;b<N_SHRAPNELS_EXPLODE;b++)
{
double angle=(double)b*2*M_PI/N_SHRAPNELS_EXPLODE;
int spd=add_int(mul_int(my_and(mul_int(weapon[WEAPON_GRENADE].speed,b+1),15),16),100);
new_obj(
i,
T_SHRAPNEL,
SHRAPNEL_TTL,
0,
0,
WEAPON_GRENADE,
p->member.x,
p->member.y,
p->member.xspeed+mul(spd,float2double(cos(angle))),
p->member.yspeed+mul(spd,float2double(sin(angle))),
0);
i++;
}
delete_obj(j);
}
break;
case P_CHANGE_LEVEL:
{
char *md5;
int a;
char p;
if (l<38)break; /* invalid packet */
a=get_int(packet+1);
if (level_number==a)goto level_changed;
level_number=a;
md5=md5_level(level_number);
if (strcmp((char *)md5,packet+5)) /* MD5s differ */
{
mem_free(md5);
ERROR("Invalid MD5 sum. Can't change level. Exiting...");
send_quit();
shut_down(1);
}
mem_free(md5);
/* OK we can change it */
change_level();
level_changed:
p=P_LEVEL_ACCEPTED;
send_packet(&p,1,(struct sockaddr *)(&server),my_id,0);
n=38;
}
break;
}
return n;
}
/* read packet from socket */
static void read_data(void)
{
fd_set rfds;
struct timeval tv;
struct sockaddr_in client;
static char packet[MAX_PACKET_LENGTH];
int a=sizeof(client);
int l;
tv.tv_sec=0;
tv.tv_usec=0;
FD_ZERO(&rfds);
FD_SET(fd,&rfds);
while(select(fd+1,&rfds,0,0,&tv))
{
if ((l=recv_packet(packet,MAX_PACKET_LENGTH,(struct sockaddr*)(&client),&a,1,my_id,0))<0)
return; /* something's strange */
process_packet(packet,l);
}
}
/* handle fatal signal (sigabrt, sigsegv, ...) */
static void signal_handler(int signum)
{
if (connected)send_quit();
shut_down(0);