forked from hackndev/0verkill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
3203 lines (2835 loc) · 96.9 KB
/
server.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 <sys/types.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
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#else
#include <time.h>
#include <winsock.h>
#endif
#include <stdlib.h>
#include <signal.h>
#include <math.h>
#include <errno.h>
#ifdef HAVE_FLOAT_H
#include <float.h>
#endif
#ifndef __USE_GNU
#define __USE_GNU
#endif
#include <string.h>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#ifdef __EMX__
#define INCL_DOS
#include <os2.h>
#endif
#include "data.h"
#include "server.h"
#include "net.h"
#include "cfg.h"
#include "hash.h"
#include "time.h"
#include "getopt.h"
#include "math.h"
#include "error.h"
unsigned short port=DEFAULT_PORT; /* game port (not a gameport ;-) )*/
int level_number=0; /* line number in the LEVEL_FILE */
int fd; /* socket */
int n_players; /* highest ID of player */
int active_players=0; /* # of players in the game */
unsigned int id=0; /* my ID */
unsigned long_long game_start; /* time of game start */
/* important sprites */
int grenade_sprite,bullet_sprite,slug_sprite,shell_sprite,shotgun_shell_sprite,
mess1_sprite,mess2_sprite,mess3_sprite,mess4_sprite,noise_sprite,
bfgcell_sprite;
int shrapnel_sprite[N_SHRAPNELS],bfgbit_sprite[N_SHRAPNELS],bloodrain_sprite[N_SHRAPNELS],jetfire_sprite;
int nonquitable=0; /* 1=clients can't abort game pressing F12 (request is ignored) */
unsigned long_long last_tick;
unsigned long_long last_player_left=0,last_packet_came=0;
char *level_checksum=0; /* MD5 sum of the level */
short meatcounter=0;
unsigned short weapons_order[ARMS]={4,0,3,1,2,5};
struct birthplace_type
{
int x,y;
}*birthplace=DUMMY;
int n_birthplaces=0;
/* list of players */
struct player_list
{
struct player_list *next,*prev;
struct player member;
}players;
/* time queue of respawning objects */
struct queue_list
{
struct queue_list* next;
unsigned long_long time;
struct it member;
}time_queue;
#ifdef WIN32
#define SERVICE_NAME "0verkill_017"
#define SERVICE_DISP_NAME "0verkill Server 0.17"
int consoleApp=0; /* 0 kdyz to bezi jako server na pozadi */
/***************************************************/
/* WINDOWS NT SERVICE INSTALLATION/RUNNING/REMOVAL */
#include <winsvc.h>
int server(void);
SERVICE_STATUS ssStatus;
SERVICE_STATUS_HANDLE sshStatusHandle;
DWORD globErr=ERROR_SUCCESS;
TCHAR szErr[2048];
int hServerExitEvent=0; /* close server flag */
LPTSTR GetLastErrorText(LPTSTR lpszBuf, DWORD dwSize) {
DWORD dwRet;
LPTSTR lpszTemp=NULL;
globErr=GetLastError();
dwRet=FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY, NULL,
globErr, LANG_NEUTRAL, (LPTSTR)&lpszTemp, 0, NULL);
/* supplied buffer is not long enough */
if (( !dwRet )||( (long)dwSize < (long)dwRet+14 ))
snprintf(lpszBuf, sizeof(lpszBuf), ": %u", globErr);
else {
lpszTemp[lstrlen(lpszTemp)-2] = TEXT('\0');
CharToOem(lpszTemp, lpszTemp);
snprintf(lpszBuf, sizeof(lpszBuf), ": %u: %s", globErr, lpszTemp);
}
if ( lpszTemp )
LocalFree((HLOCAL)lpszTemp);
return lpszBuf;
}
void CmdInstallService(LPCTSTR pszUserName, LPCTSTR pszUserPassword, LPCTSTR pszDependencies) {
SC_HANDLE schService;
SC_HANDLE schSCManager;
TCHAR szPath[2048];
printf("Installing %s as ", SERVICE_NAME);
if ( pszUserName==NULL )
printf("LOCAL_SYSTEM");
else {
printf("\"%s\"", pszUserName);
if ( pszUserPassword )
printf("/\"%s\"", pszUserPassword);
};
printf("...\r\n");
if ( GetModuleFileName(NULL, szPath, 2046)==0 ) {
printf("Unable to install %s%s\r\n", SERVICE_NAME, GetLastErrorText(szErr, 2046));
return;
};
schSCManager=OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if ( schSCManager ) {
schService=CreateService(
schSCManager, /* SCManager database */
SERVICE_NAME, /* name of service */
SERVICE_DISP_NAME, /* name to display */
SERVICE_ALL_ACCESS, /* desired access */
SERVICE_WIN32_OWN_PROCESS, /* service type */
SERVICE_DEMAND_START, /* start type */
SERVICE_ERROR_NORMAL, /* error control type */
szPath, /* service's binary */
NULL, /* no load ordering group */
NULL, /* no tag identifier */
pszDependencies, /* dependencies */
pszUserName, /* account */
pszUserPassword); /* password */
if ( schService ) {
printf("%s was installed successfully.\r\n", SERVICE_NAME);
globErr=ERROR_SUCCESS;
CloseServiceHandle(schService);
}
else
printf("CreateService failed%s\r\n", GetLastErrorText(szErr, 2046));
CloseServiceHandle(schSCManager);
}
else
printf("OpenSCManager failed%s\r\n", GetLastErrorText(szErr, 2046));
}
void CmdRemoveService() {
SC_HANDLE schService,
schSCManager;
schSCManager=OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if ( schSCManager ) {
schService=OpenService(schSCManager, SERVICE_NAME, SERVICE_ALL_ACCESS);
if ( schService ) {
if ( ControlService(schService, SERVICE_CONTROL_STOP, &ssStatus) ) {
printf("Stopping %s...\r\n", SERVICE_NAME);
Sleep(500);
while( QueryServiceStatus(schService, &ssStatus) ) {
if ( ssStatus.dwCurrentState==SERVICE_STOP_PENDING ) {
printf(".");
Sleep(500);
}
else
break;
};
if ( ssStatus.dwCurrentState == SERVICE_STOPPED )
printf("\r\n%s stopped successfully.\r\n", SERVICE_NAME);
else
printf("\r\n%s failed to stop.\r\n", SERVICE_NAME);
}
else
printf("ControlService failed%s\r\n", GetLastErrorText(szErr, 2046));
if ( DeleteService(schService) ) {
globErr=ERROR_SUCCESS;
printf("%s removed.\r\n", SERVICE_NAME);
}
else
printf("DeleteService failed%s\r\n", GetLastErrorText(szErr, 2046));
CloseServiceHandle(schService);
}
else
printf("OpenService failed%s\r\n", GetLastErrorText(szErr, 2046));
CloseServiceHandle(schSCManager);
}
else
printf("OpenSCManager failed%s\r\n", GetLastErrorText(szErr, 2046));
}
void AddToMessageLog(LPTSTR lpszMsg, int infoOnly) {
TCHAR szMsg[2048];
HANDLE hEventSource;
LPTSTR lpszStrings[2];
hEventSource=RegisterEventSource(NULL, SERVICE_NAME);
snprintf(szMsg, sizeof(szMsg), infoOnly ? "%s notification:" : "%s error: %u", SERVICE_NAME, globErr);
lpszStrings[0]=szMsg;
lpszStrings[1]=lpszMsg;
if ( hEventSource!=INVALID_HANDLE_VALUE ) {
ReportEvent(hEventSource, infoOnly ? EVENTLOG_INFORMATION_TYPE : EVENTLOG_ERROR_TYPE, 0, 0, NULL, 2, 0, (LPCTSTR*)lpszStrings, NULL);
DeregisterEventSource(hEventSource);
};
};
BOOL ReportStatusToSCMgr(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint) {
static DWORD dwCheckPoint=1;
BOOL fResult=TRUE;
if ( dwCurrentState==SERVICE_START_PENDING )
ssStatus.dwControlsAccepted=0;
else
ssStatus.dwControlsAccepted=SERVICE_ACCEPT_STOP;
ssStatus.dwCurrentState=dwCurrentState;
ssStatus.dwWin32ExitCode=dwWin32ExitCode;
ssStatus.dwWaitHint=dwWaitHint;
if (( dwCurrentState==SERVICE_RUNNING )||
( dwCurrentState==SERVICE_STOPPED ))
ssStatus.dwCheckPoint=0;
else
ssStatus.dwCheckPoint=dwCheckPoint++;
if ( !(fResult=SetServiceStatus(sshStatusHandle, &ssStatus)) ) {
globErr=GetLastError();
AddToMessageLog("SetServiceStatus failed", 0);
}
return fResult;
}
void ServiceStop() {
hServerExitEvent=1;
raise(SIGINT);
}
void WINAPI ServiceCtrl(DWORD dwCtrlCode) {
switch( dwCtrlCode ) {
case SERVICE_CONTROL_STOP:
ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR, 500);
ServiceStop();
return;
};
if ( hServerExitEvent )
ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR, 0);
else
ReportStatusToSCMgr(SERVICE_RUNNING, NO_ERROR, 0);
}
void ServiceStart(DWORD dwArgc, LPTSTR *lpszArgv) {
if ( dwArgc>1 )
SetCurrentDirectory(lpszArgv[1]);
else {
char path[1024],
*p2;
GetModuleFileName(NULL, path, sizeof(path));
p2=path+strlen(path);
while(( p2>=path )&&( *p2!='\\' )&&( *p2!='/' ))
p2--;
*p2=0;
SetCurrentDirectory(path);
}
server();
}
void WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv) {
if ( !(sshStatusHandle=RegisterServiceCtrlHandler(SERVICE_NAME, ServiceCtrl)) )
return;
ssStatus.dwServiceType=SERVICE_WIN32_OWN_PROCESS;
ssStatus.dwServiceSpecificExitCode=0;
if ( !ReportStatusToSCMgr(SERVICE_START_PENDING, NO_ERROR, 3000))
goto Cleanup;
nonquitable=1;
ServiceStart(dwArgc, lpszArgv);
Cleanup:
ReportStatusToSCMgr(SERVICE_STOPPED, globErr, 0);
return;
};
/* WINDOWS NT SERVICE INSTALLATION/REMOVAL */
/*******************************************/
#endif
/* load dynamic data */
static void load_dynamic(char * filename)
{
FILE * stream;
static char line[1024];
char *p,*q,*name;
int n,x,y,t;
#ifndef WIN32
if (!(stream=fopen(filename,"rb")))
#else
if (!fopen_s(&stream, filename, "rb"))
#endif
{
ERROR("Can't open file \"%s\"!\n",filename);
EXIT(1);
}
while(fgets(line,1024,stream))
{
p=line;
_skip_ws(&p);
for (name=p;(*p)!=' '&&(*p)!=9&&(*p)!=10&&(*p);p++);
if (!(*p))continue;
*p=0;p++;
_skip_ws(&p);
if ((t=_convert_type(*p))<0){ERROR("Unknown object type '%c'.\n",*p);EXIT(1);}
p++;
_skip_ws(&p);
x=strtol(p,&q,0);
_skip_ws(&q);
y=strtol(q,&p,0);
if (t==TYPE_BIRTHPLACE) /* birthplace */
{
n_birthplaces++;
birthplace=mem_realloc(birthplace,sizeof(struct birthplace_type)*n_birthplaces);
if (!birthplace){ERROR("Error: Not enough memory.\n");EXIT(1);}
birthplace[n_birthplaces-1].x=x;
birthplace[n_birthplaces-1].y=y;
continue;
}
if (find_sprite(name,&n)){ERROR("Unknown bitmap name \"%s\"!\n",name);EXIT(1);}
new_obj(id,t,0,n,0,0,int2double(x),int2double(y),0,0,0);
id++;
}
fclose(stream);
if (!n_birthplaces){ERROR("Error: No birthplaces.\n");EXIT(1);}
}
/* write a message to stdout or stderr */
static void message(char *msg,int output)
{
time_t t;
struct tm tm;
static char timestamp[64];
#ifdef WIN32
if ( !consoleApp )
return;
#endif
t=time(0);
#ifndef WIN32
tm=*(localtime(&t));
#else
localtime_s(&tm, &t);
#endif
/* time stamp is in format day.month.year hour:minute:second */
snprintf(timestamp,64,"%2d.%2d.%d %02d:%02d:%02d ",tm.tm_mday,tm.tm_mon+1,tm.tm_year+1900,tm.tm_hour,tm.tm_min,tm.tm_sec);
switch (output)
{
case 1:
printf("%s%s",timestamp,msg);
fflush(stdout);
break;
case 2:
fprintf(stderr,"%s%s",timestamp,msg);
fflush(stderr);
break;
}
}
/* switch weapon to the player's best one */
static int select_best_weapon(struct player* p)
{
unsigned short t[ARMS]={4,0,1,3,2,5};
int a;
for (a=ARMS-1;a>=0;a--)
if ((p->weapons&(1<<t[a]))&&p->ammo[t[a]])return t[a];
return p->current_weapon;
}
/* Bakta modify - better weapon selection routines */
/* test if weapon is better than the one he wields */
static int is_weapon_better(struct player* p, int weapon)
{
int i;
int cur=0, test=0;
for(i=ARMS-1;i>=0;i--)
{
if(weapons_order[i]==p->current_weapon)
cur = i;
if(weapons_order[i]==weapon)
test = i;
}
return cur<test;
}
/* test if player p has weapon and can use it (has ammo for it) */
static int is_weapon_usable(struct player* p, int weapon)
{
return !!((p->weapons&(1<<weapon))&&p->ammo[weapon]);
}
/* test if player p has weapon and has no ammo for it */
static int is_weapon_empty(struct player* p, int weapon)
{
return !!((p->weapons&(1<<weapon))&&!p->ammo[weapon]);
}
/* initialize socket */
static void init_socket(void)
{
struct sockaddr_in server;
fd=socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
if (fd<0)
{
ERROR("Error: Can't create socket!\n");
EXIT(1);
}
server.sin_family=AF_INET;
server.sin_port=htons(port);
server.sin_addr.s_addr=INADDR_ANY;
if (bind(fd,(struct sockaddr*)&server,sizeof(server)))
{
ERROR("Error: Can't bind socket to port %d!\n",port);
EXIT(1);
}
}
/* selects random birthplace and returns */
static void find_birthplace(int *x,int *y)
{
int a=random()%n_birthplaces;
*x=birthplace[a].x;
*y=birthplace[a].y;
}
/* return index of hero with color num in sprites field */
/* if there's not such color, returns -1 */
static int select_hero(int num)
{
static char txt[32];
int a;
snprintf(txt, sizeof(txt), "hero%d",num);
if (find_sprite(txt,&a))return -1;
return a;
}
/* initialize player */
static void init_player(struct player* p,int x,int y)
{
int a;
p->health=100;
p->health_ep=ILLNESS_SPEED;
p->armor=0;
p->current_weapon=0;
p->weapons= WEAPON_MASK_GUN |
WEAPON_MASK_GRENADE |
WEAPON_MASK_CHAINSAW |
WEAPON_MASK_BLOODRAIN; /* he has only gun, grenades, chainsaw and bloodrain */
p->invisibility_counter=0;
for (a=0;a<ARMS;a++)
p->ammo[a]=0;
p->ammo[WEAPON_GUN]=weapon[WEAPON_GUN].basic_ammo;
p->ammo[WEAPON_CHAINSAW]=weapon[WEAPON_CHAINSAW].basic_ammo;
p->obj->xspeed=0;
p->obj->yspeed=0;
p->obj->status=0;
p->obj->ttl=0;
p->obj->anim_pos=0;
p->obj->x=x;
p->obj->y=y;
}
/* completely add player to server's database */
/* player starts on x,y coordinates */
static int add_player(unsigned char color, char *name,struct sockaddr_in *address,int x, int y)
{
int h;
struct player_list *cp;
/* alloc memory for new player */
cp=mem_alloc(sizeof(struct player_list));
if (!cp)
{message("Not enough memory.\n",2);return 1;}
cp->member.name=mem_alloc(strlen(name)+1);
if (!cp->member.name)
{mem_free(cp);message("Not enough memory.\n",2);return 1;}
/* fill in the structure */
cp->member.color=color;
cp->member.frags=0;
cp->member.deaths=0;
cp->member.keyboard_status.status=0;
cp->member.keyboard_status.weapon=0;
cp->member.id=n_players+1; /* player numbering starts from 1, 0 is server's ID */
cp->member.last_update=get_time();
memcpy(cp->member.name,name,strlen(name)+1);
cp->member.address=*address;
cp->member.packet_pos=0;
cp->member.current_level=-1;
/* create new object "player" */
h=select_hero(cp->member.color);
if (h<0)
{mem_free(cp->member.name);mem_free(cp);message ("No such color.\n",1);return 1;}
cp->member.obj=new_obj(id,T_PLAYER,0,h,0,0,int2double(x),int2double(y),0,0,(&(cp->member)));
if (!cp->member.obj)
{mem_free(cp->member.name);mem_free(cp);message("Can't create object.\n",1);return 1;}
id++;
init_player(&(cp->member),int2double(x),int2double(y));
/* that's all */
cp->prev=last_player;
cp->next=0;
last_player->next=cp;
last_player=cp;
return 0;
}
static void send_chunk_to_player(struct player *player)
{
static char p[MAX_PACKET_LENGTH];
if (!(player->packet_pos))return;
if (player->packet_pos>MAX_PACKET_LENGTH-1)return;
p[0]=P_CHUNK;
memcpy(p+1,player->packet,player->packet_pos);
send_packet(p,(player->packet_pos)+1,(struct sockaddr*)(&(player->address)),0,player->id);
player->packet_pos=0;
}
static void send_chunk_packet_to_player(char *packet, int len, struct player *player)
{
if (len>MAX_PACKET_LENGTH-1)return;
if ((player->packet_pos)+len>MAX_PACKET_LENGTH-1)send_chunk_to_player(player);
memcpy((player->packet)+(player->packet_pos),packet,len);
player->packet_pos+=len;
}
static void send_chunks(void)
{
struct player_list* p;
for (p=&players;p->next;p=p->next)
send_chunk_to_player(&(p->next->member));
}
/* send a packet to all players except one */
/* if not_this_player is null, sends to all players */
static void sendall(char *packet,int len, struct player * not_this_player)
{
struct player_list* p;
if (!not_this_player)
for (p=&players;p->next;p=p->next)
send_packet(packet,len,(struct sockaddr*)(&(p->next->member.address)),0,p->next->member.id);
else
for (p=&players;p->next;p=p->next)
if ((&(p->next->member))!=not_this_player)
send_packet(packet,len,(struct sockaddr*)(&(p->next->member.address)),0,p->next->member.id);
}
/* send a packet to all players except one */
/* if not_this_player is null, sends to all players */
static void sendall_chunked(char *packet,int len, struct player * not_this_player)
{
struct player_list* p;
if (!not_this_player)
for (p=&players;p->next;p=p->next)
send_chunk_packet_to_player(packet,len,&(p->next->member));
else
for (p=&players;p->next;p=p->next)
if ((&(p->next->member))!=not_this_player)
send_chunk_packet_to_player(packet,len,&(p->next->member));
}
static int which_update(struct it *obj,int old_x,int old_y,int old_x_speed,int old_y_speed,unsigned int old_status,int old_ttl)
{
int a=0;
if (old_ttl==obj->ttl)
{
if (old_x_speed==obj->xspeed&&old_y_speed==obj->yspeed)a=5; /* update pos and status */
if (old_x==obj->x&&old_y==obj->y)a=4; /* update speed and status */
}
if (old_status==obj->status&&old_ttl==obj->ttl)
{
a=1; /* update speed+coords */
if (old_x_speed==obj->xspeed&&old_y_speed==obj->yspeed)a=3; /* update pos */
if (old_x==obj->x&&old_y==obj->y)
{
if(a==3)return -1; /* nothing to update */
a=2; /* update speed */
}
}
return a;
}
/* send a packet to all players except one */
/* if not_this_player is null, sends to all players */
/* type is:
0=full update
1=update speed+coordinates
2=update speed
3=update coordinates
4=update speed + status
5=update coordinates + status
6=update speed + status + ttl
7=update coordinates + status + ttl
*/
static void sendall_update_object(struct it* obj, struct player * not_this_player,int type)
{
struct player_list* p;
static char packet[32];
int offset = 0;
switch (type)
{
case 0: /* all */
packet[offset++]=P_UPDATE_OBJECT;
put_int(packet,obj->id,&offset);
packet[offset++]=obj->update_counter;
put_int(packet,obj->x,&offset);
put_int(packet,obj->y,&offset);
put_int(packet,obj->xspeed,&offset);
put_int(packet,obj->yspeed,&offset);
put_int(packet,obj->status,&offset);
put_int16(packet,obj->ttl,&offset);
break;
case 1: /* coordinates and speed */
packet[offset++]=P_UPDATE_OBJECT_POS;
put_int(packet,obj->id,&offset);
packet[offset++]=obj->update_counter;
put_int(packet,obj->x,&offset);
put_int(packet,obj->y,&offset);
put_int(packet,obj->xspeed,&offset);
put_int(packet,obj->yspeed,&offset);
break;
case 2: /* speed */
packet[offset++]=P_UPDATE_OBJECT_SPEED;
put_int(packet,obj->id,&offset);
packet[offset++]=obj->update_counter;
put_int(packet,obj->xspeed,&offset);
put_int(packet,obj->yspeed,&offset);
break;
case 3: /* coordinates */
packet[offset++]=P_UPDATE_OBJECT_COORDS;
put_int(packet,obj->id,&offset);
packet[offset++]=obj->update_counter;
put_int(packet,obj->x,&offset);
put_int(packet,obj->y,&offset);
break;
case 4: /* speed and status */
packet[offset++]=P_UPDATE_OBJECT_SPEED_STATUS;
put_int(packet,obj->id,&offset);
packet[offset++]=obj->update_counter;
put_int(packet,obj->xspeed,&offset);
put_int(packet,obj->yspeed,&offset);
put_int(packet,obj->status,&offset);
break;
case 5: /* coordinates and status */
packet[offset++]=P_UPDATE_OBJECT_COORDS_STATUS;
put_int(packet,obj->id,&offset);
packet[offset++]=obj->update_counter;
put_int(packet,obj->x,&offset);
put_int(packet,obj->y,&offset);
put_int(packet,obj->status,&offset);
break;
case 6: /* speed and status and ttl */
packet[offset++]=P_UPDATE_OBJECT_SPEED_STATUS_TTL;
put_int(packet,obj->id,&offset);
packet[offset++]=obj->update_counter;
put_int(packet,obj->xspeed,&offset);
put_int(packet,obj->yspeed,&offset);
put_int(packet,obj->status,&offset);
put_int16(packet,obj->ttl,&offset);
break;
case 7: /* coordinates and status and ttl */
packet[offset++]=P_UPDATE_OBJECT_COORDS_STATUS_TTL;
put_int(packet,obj->id,&offset);
packet[offset++]=obj->update_counter;
put_int(packet,obj->x,&offset);
put_int(packet,obj->y,&offset);
put_int16(packet,obj->status,&offset);
put_int16(packet,obj->ttl,&offset);
break;
default: /* don't update */
return;
}
obj->update_counter++;
if (!not_this_player)
for (p=&players;p->next;p=p->next)
send_chunk_packet_to_player(packet,offset,&(p->next->member));
else
for (p=&players;p->next;p=p->next)
if ((&(p->next->member))!=not_this_player)
send_chunk_packet_to_player(packet,offset,&(p->next->member));
}
/* send update status packet to all players (except not_this_player if this is !NULL)*/
static void sendall_update_status(struct it* obj, struct player * not_this_player)
{
struct player_list* p;
static char packet[32];
int offset = 0;
packet[offset++]=P_UPDATE_STATUS;
put_int(packet,obj->id,&offset);
put_int(packet,obj->status,&offset);
if (!not_this_player)
for (p=&players;p->next;p=p->next)
send_chunk_packet_to_player(packet,offset,&(p->next->member));
else
for (p=&players;p->next;p=p->next)
if ((&(p->next->member))!=not_this_player)
send_chunk_packet_to_player(packet,offset,&(p->next->member));
}
/* send hit packet to all players except not_this player (if !NULL)*/
static void sendall_hit(unsigned long id,unsigned char direction,unsigned char xoffs,unsigned char yoffs,struct player* not_this_player)
{
struct player_list* p;
static char packet[8];
int offset = 0;
packet[offset++]=P_HIT;
put_int(packet,id,&offset);
packet[offset++]=direction;
packet[offset++]=xoffs;
packet[offset++]=yoffs;
if (!not_this_player)
for (p=&players;p->next;p=p->next)
send_chunk_packet_to_player(packet,offset,&(p->next->member));
else
for (p=&players;p->next;p=p->next)
if ((&(p->next->member))!=not_this_player)
send_chunk_packet_to_player(packet,offset,&(p->next->member));
}
/* sort players according to frags */
static int _qsort_cmp(const void *a,const void *b)
{
int fa,fb,da,db;
fa=(*((struct player**)a))->frags;
fb=(*((struct player**)b))->frags;
da=(*((struct player**)a))->deaths;
db=(*((struct player**)b))->deaths;
if (fa>fb)return -1;
if (fa==fb)
{
if (da<db)return -1;
if (da>db)return 1;
return 0;
}
return 1;
}
/* send info how many players are in the game and top score */
/* id=recipient's id */
/* if addr is NULL info is sent to all */
static void send_info(struct sockaddr *addr,int id)
{
char *packet;
int p=active_players>TOP_PLAYERS_N?TOP_PLAYERS_N:active_players;
int a,offset = 0,l;
struct player **t; /* table for qsort */
struct player_list *q;
/* alloc memory */
t=mem_alloc(active_players*sizeof(struct player*));
if (!t)return;
packet=mem_alloc(1+4+1+p*(MAX_NAME_LEN+4+4));
if (!packet)return;
/* fill table for quicksort */
for (a=0,q=(&players);q->next;a++,q=q->next)
t[a]=&(q->next->member);
/* sort players */
qsort(t,active_players,sizeof(struct player*),_qsort_cmp);
/* create packet header */
packet[offset++]=P_INFO;
put_int(packet,active_players,&offset);
packet[offset++]=p;
/* put players into packet */
for (a=0;a<p;a++)
{
put_int(packet,(t[a])->frags,&offset);
put_int(packet,(t[a])->deaths,&offset);
packet[offset++]=t[a]->color;
memcpy(packet+offset,(t[a])->name,l=strlen((t[a])->name)+1);
offset+=l;
}
if (!addr)
sendall(packet,offset,0);
else send_packet(packet,offset,addr,0,id);
mem_free(packet);
mem_free(t);
}
/* returns the actual length of the packet to be sent */
static int build_message_packet(char packet[], int maxlen, char *name, char *msg, char flags)
{
int n, len = 0;
/* assert(maxlen > 2); */
packet[0] = P_MESSAGE;
packet[1] = flags;
len += 2;
if (!name)
n = snprintf(packet + len, maxlen - len, "%s", msg);
else
n = snprintf(packet + len, maxlen - len, "%s> %s", name, msg);
/* account for the space the text consumed in the packet; could have been truncated */
len += min(n + 1, maxlen - len);
return len;
}
/* send message to a player */
/* name is name of player who sent the message (chat), NULL means it's from server */
static void send_message(struct player* player, char *name, char *msg, char flags)
{
static char packet[256];
int len;
len = build_message_packet(packet, sizeof(packet), name, msg, flags);
send_chunk_packet_to_player(packet, len, player);
}
/* similar to send_message but sends to all except one or two players */
static void sendall_message(char *name, char *msg,struct player *not1,struct player* not2, char flags)
{
static char packet[256];
int len;
struct player_list* p;
len = build_message_packet(packet, sizeof(packet), name, msg, flags);
for (p=&players;p->next;p=p->next)
if ((!not1||(&(p->next->member))!=not1)&&(!not2||(&(p->next->member))!=not2))
send_chunk_packet_to_player(packet, len, &(p->next->member));
}
static void sendall_bell(void)
{
static char packet;
struct player_list* p;
packet=P_BELL;
for (p=&players;p->next;p=p->next)
send_chunk_packet_to_player(&packet,1,&(p->next->member));
}
#if 0
/* send new object information to given address */
static void send_new_obj(struct sockaddr* address, struct it* obj,int id)
{
static char packet[32];
int offset = 0;
packet[offset++]=P_NEW_OBJ;
put_int(packet,obj->id,&offset);
put_int16(packet,obj->sprite,&offset);
put_int(packet,obj->x,&offset);
put_int(packet,obj->y,&offset);
put_int(packet,obj->xspeed,&offset);
put_int(packet,obj->yspeed,&offset);
put_int(packet,obj->status,&offset);
packet[offset++]=obj->type;
put_int16(packet,obj->ttl,&offset);
send_packet(packet,offset,address,0,id);
}
#endif
/* send new object information to given address */
static void send_new_obj_chunked(struct player* player, struct it* obj)
{
static char packet[32];
int offset = 0;
packet[offset++]=P_NEW_OBJ;
put_int(packet,obj->id,&offset);
put_int16(packet,obj->sprite,&offset);
put_int(packet,obj->x,&offset);
put_int(packet,obj->y,&offset);
put_int(packet,obj->xspeed,&offset);
put_int(packet,obj->yspeed,&offset);
put_int(packet,obj->status,&offset);
packet[offset++]=obj->type;
put_int16(packet,obj->ttl,&offset);
send_chunk_packet_to_player(packet,offset,player);
}
/* send player update to given player */
static void send_update_player(struct player* p)
{
static char packet[32];
int a,offset = 0;
packet[offset++]=P_UPDATE_PLAYER;
packet[offset++]=p->health;
packet[offset++]=p->armor;
for (a=0;a<ARMS;a++)
put_int16(packet,p->ammo[a],&offset);
put_int(packet,p->frags,&offset);
put_int(packet,p->deaths,&offset);
put_int16(packet,p->current_weapon,&offset);
put_int16(packet,p->weapons,&offset);
send_chunk_packet_to_player(packet,offset,p);
}
/* send a packet to all players except one */