-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathclient.c
2407 lines (2131 loc) · 59.2 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
/* When compiling with X support don't forget to define XWINDOW symbol */
#ifndef WIN32
#include "config.h"
#endif
#include <stdio.h>
#include <signal.h>
#include <math.h>
#ifdef HAVE_PTHREAD_H
#include <pthread.h>
#endif
#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 "blit.h"
#include "sprite.h"
#include "data.h"
#include "cfg.h"
#include "net.h"
#include "hash.h"
#include "console.h"
#include "time.h"
#include "math.h"
#include "help.h"
#include "getopt.h"
#include "error.h"
#ifdef XWINDOW
#include "x.h"
#endif
#undef PAUSE
#ifdef WIN32
int consoleApp=1;
#endif
/* settings */
#define OVERRIDE_FLAG_HOST 0x01
#define OVERRIDE_FLAG_NAME 0x02
#define OVERRIDE_FLAG_PORT 0x04
#define OVERRIDE_FLAG_COLOR 0x08
struct config {
int argc;
char **argv;
char host[MAX_HOST_LEN+1];
char name[MAX_NAME_LEN+1];
int port;
int color;
char override;
};
struct {
int flags;
char *text;
} errormsg = {0, ""};
/* my health, armor, frags, deaths, ammo, ... and ID */
unsigned char health,armor;
unsigned int frags,deaths;
unsigned short ammo[ARMS];
unsigned short current_weapon;
unsigned short weapons;
unsigned char creep;
unsigned char autorun,autocreep;
int my_id;
/* connection with server */
int connected;
int level_number=-1;
/* networking */
int fd; /* socket */
struct sockaddr_in server; /* server address */
/* objects */
struct object_list objects;
struct object_list *last_obj;
struct it* hero;
/* important sprites */
int hit_sprite;
int title_sprite;
int bulge_sprite;
int shrapnel_sprite[N_SHRAPNELS],bfgbit_sprite[N_SHRAPNELS],bloodrain_sprite[N_SHRAPNELS],jetpack_sprite,fire_sprite,chain_sprite;
int level_sprites_start; /* number of first sprite in the level (all other level sprites follow) */
unsigned long_long game_start_offset; /* time difference between game start on this machine and on server */
int gsbi = 0;
/* top players table */
struct
{
char name[MAX_NAME_LEN+1];
int frags,deaths;
unsigned char color;
}top_players[TOP_PLAYERS_N];
/* # of active players in the game */
int active_players;
#define KBD_RIGHT (1 << 0)
#define KBD_LEFT (1 << 1)
#define KBD_JUMP (1 << 2)
#define KBD_CREEP (1 << 3)
#define KBD_SPEED (1 << 4)
#define KBD_FIRE (1 << 5)
#define KBD_DOWN_LADDER (1 << 6)
#define KBD_JETPACK (1 << 7)
struct /* keyboard status */
{
unsigned char status, weapon;
} keyboard_status;
/* message */
struct msgline_type
{
unsigned long_long time;
char color;
char *msg;
}msg_line[N_MESSAGES];
int last_message;
char error_message[1024];
unsigned char set_size; /* -s option was on the command line */
/* convert key to key with shift */
int my_toupper(int c)
{
switch(c)
{
case '1': return '!';
case '2': return '@';
case '3': return '#';
case '4': return '$';
case '5': return '%';
case '6': return '^';
case '7': return '&';
case '8': return '*';
case '9': return '(';
case '0': return ')';
case '-': return '_';
case '=': return '+';
case '\\': return '|';
case '[': return '{';
case ']': return '}';
case ';': return ':';
case '\'': return '"';
case ',': return '<';
case '.': return '>';
case '/': return '?';
case '`': return '~';
default: return toupper(c);
}
}
void wait_for_enter(void)
{
c_update_kbd();
while (!c_was_pressed(K_ENTER))
{
c_wait_for_key();
c_update_kbd();
}
}
/* load configure file from player's home directory */
int read_cfg_entry(FILE *stream, char *retval, int len)
{
int l;
if (!fgets(retval,len+2,stream))
return 0;
l = strlen(retval);
if (retval[l-1] == 10)
retval[l-1] = 0;
return l;
}
int load_default_cfg(struct config *cfg, char state)
{
char *host = "localhost";
char *name = "Player";
/* we dont want to loose argc and argv here ... */
memcpy(cfg->host, host, strlen(host));
memcpy(cfg->name, name, strlen(name));
cfg->port = 6666;
cfg->color = C_D_GREY;
errormsg.flags = (state >> 1) ? E_DEFAULTS : 0;
errormsg.text = "Error loading configuration file, defaults loaded. Press ENTER.";
return state;
}
int load_cfg(struct config *cfg)
{
FILE *stream;
int a;
char txt[256];
#ifndef WIN32
snprintf(txt, sizeof(txt), "%s/%s",getenv("HOME"),CFG_FILE);
stream=fopen(txt,"r");
#else
snprintf(txt, sizeof(txt), "./%s", CFG_FILE);
fopen_s(&stream, txt, "r");
#endif
if (!stream)
return load_default_cfg(cfg, 1);
if (a = read_cfg_entry(stream, txt, MAX_HOST_LEN))
memcpy(cfg->host,txt,a+1);
else
return load_default_cfg(cfg, 2);
if (a = read_cfg_entry(stream, txt, MAX_NAME_LEN))
memcpy(cfg->name,txt,a+1);
else
return load_default_cfg(cfg, 3);
if (a = read_cfg_entry(stream, txt, MAX_PORT_LEN))
cfg->port = strtol(txt,0,10);
else
return load_default_cfg(cfg, 4);
if (a = read_cfg_entry(stream, txt, 4))
cfg->color = strtol(txt,0,10);
else
return load_default_cfg(cfg, 5);
fclose(stream);
return 0;
}
/* save configure file to player's home */
void save_cfg(struct config *cfg)
{
FILE *stream;
char txt[256];
struct config dcfg;
memcpy(&dcfg, cfg, sizeof(struct config));
#ifndef WIN32
snprintf(txt, sizeof(txt), "%s/%s",getenv("HOME"),CFG_FILE);
#else
snprintf(txt, sizeof(txt), "./%s",CFG_FILE);
#endif
if (cfg->override) {
load_cfg(&dcfg);
if (!(cfg->override & OVERRIDE_FLAG_HOST))
memcpy(dcfg.host, cfg->host, strlen(cfg->host));
if (!(cfg->override & OVERRIDE_FLAG_NAME))
memcpy(dcfg.name, cfg->name, strlen(cfg->name));
if (!(cfg->override & OVERRIDE_FLAG_PORT))
dcfg.port = cfg->port;
if (!(cfg->override & OVERRIDE_FLAG_COLOR))
dcfg.color = cfg->color;
}
#ifndef WIN32
stream=fopen(txt,"w");
#else
fopen_s(&stream, txt, "w");
#endif
if (!stream)
return;
fprintf(stream, "%s\n%s\n%d\n%d\n", dcfg.host, dcfg.name,
dcfg.port, dcfg.color);
fclose(stream);
}
void scroll_messages(void)
{
int a;
if (last_message<0)return;
for (a=0;a<=last_message-1;a++)
{
msg_line[a].time=msg_line[a+1].time;
msg_line[a].color=msg_line[a+1].color;
msg_line[a].msg=mem_realloc(msg_line[a].msg,strlen(msg_line[a+1].msg)+1);
memcpy(msg_line[a].msg,msg_line[a+1].msg,strlen(msg_line[a+1].msg)+1);
}
mem_free(msg_line[last_message].msg);
msg_line[last_message].msg=0;
last_message--;
}
void add_message(char *message, unsigned char flags)
{
int last;
if (last_message == N_MESSAGES - 1)
scroll_messages();
last = last_message + 1;
msg_line[last].time = get_time() + MESSAGE_TTL;
switch (flags) {
case M_CHAT:
msg_line[last].color = C_YELLOW;
break;
case M_INFO:
msg_line[last].color = C_CYAN;
break;
case M_ENTER:
msg_line[last].color = C_GREEN;
break;
case M_LEAVE:
msg_line[last].color = C_D_RED;
break;
case M_AMMO:
msg_line[last].color = C_GREY;
break;
case M_WEAPON:
msg_line[last].color = C_BLUE;
break;
case M_ITEM:
msg_line[last].color = C_MAGENTA;
break;
case M_DEATH:
msg_line[last].color = C_RED;
break;
default:
msg_line[last].color = MESSAGE_COLOR;
break;
}
msg_line[last].msg = mem_alloc(strlen(message) + 1);
if (!msg_line[last].msg)
return; /* not such a fatal errror */
memcpy(msg_line[last].msg, message, strlen(message) + 1);
last_message = last;
}
void print_messages(void)
{
int a;
for (a=0;a<=last_message;a++)
print2screen(0,a,msg_line[a].color,msg_line[a].msg);
}
/* throw out old messages */
void update_messages(unsigned long_long time)
{
int a,b;
for(a=0,b=0;a<=last_message;a++)
if (msg_line[b].time<=time)scroll_messages();
else b++;
}
/* destroys all objects and messages */
void clean_memory(void)
{
int a;
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;
/* delete messages */
for (a=0;a<=last_message;a++)
mem_free(msg_line[a].msg);
msg_line[last_message].msg=0;
last_message=-1;
}
/* shut down the client */
void shut_down(int x)
{
struct object_list *o;
int a;
c_shutdown();
free_sprites(0);
free_area();
if (!x&&hero)delete_obj(hero->id);
/* delete all objects except hero */
for (o=&objects;o->next;)
delete_obj(o->next->member.id);
/* delete messages */
for (a=0;a<=last_message;a++)
mem_free(msg_line[a].msg);
msg_line[last_message].msg=0;
shutdown_sprites();
free_packet_buffer();
check_memory_leaks();
if (x)EXIT(0);
}
/* find address of server and fill the server address structure */
char * find_server(struct config *cfg)
{
struct hostent *h;
h=gethostbyname(cfg->host);
if (!h)
return "Error: Can't resolve server address.";
server.sin_family=AF_INET;
server.sin_port=htons(cfg->port);
server.sin_addr=*((struct in_addr*)(h->h_addr_list[0]));
return 0;
}
/* initialize socket */
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 */
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;
if(fd) close(fd);
}
#undef MAX_COUNT
void server_error(char *msg, int err)
{
errormsg.text = msg;
errormsg.flags = err;
}
/* initiate connection with server */
int contact_server(struct config *cfg)
{
char *name = cfg->name;
int color = cfg->color;
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;
memcpy(packet+5,name,l);
send_packet(packet,l+5,(struct sockaddr*)(&server),my_id,0);
if (!select(fd+1,&fds,NULL,NULL,&tv)) {
server_error("No reply within 4 seconds. Press ENTER.", E_CONN);
return 1;
}
if ((r=recv_packet(packet,256,0,0,1,0,0))<0)
{
if (errno==EINTR) {
server_error("Server hung up. Press ENTER.", E_CONN);
return 1;
} else {
server_error("Connection error. Press ENTER.", E_CONN);
return 1;
}
}
switch(*packet)
{
case P_PLAYER_REFUSED:
switch(packet[1])
{
case E_INCOMPATIBLE_VERSION:
server_error("Incompatible client version. Connection refused. Press Enter.", E_CONN);
return 1;
case E_NAME_IN_USE:
server_error("Another player on this server uses the same name. Press Enter.", E_CONN);
return 1;
default:
server_error("Connection refused. Press ENTER.", E_CONN);
return 1;
}
case P_PLAYER_ACCEPTED:
my_id=get_int(packet+35);
if (r<41) {
send_quit();
server_error("Incompatible server version. Givin' up. Press Enter.", E_CONN);
return 1;
}
maj=packet[39];
min=packet[40];
if (maj!=VERSION_MAJOR||min<MIN_SERVER_VERSION_MINOR) {
send_quit();
server_error("Incompatible server version. Givin' up. Press Enter.", E_CONN);
return 1;
}
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[WEAPON_GUN]=weapon[WEAPON_GUN].basic_ammo;
ammo[WEAPON_CHAINSAW]=weapon[WEAPON_CHAINSAW].basic_ammo;
current_weapon=0;
weapons=WEAPON_MASK_GUN |
WEAPON_MASK_GRENADE |
WEAPON_MASK_CHAINSAW |
WEAPON_MASK_BLOODRAIN; /* gun, grenades, chainsaw and blodrain */
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:
server_error("Connection error. Press ENTER.", E_CONN);
return 1;
}
server_error("",
#ifdef HAVE_LIBPTHREAD
E_CONN_SUCC
#else
E_NONE
#endif
);
return 0;
}
/* send me top X players */
void send_info_request(void)
{
char packet;
packet=P_INFO;
send_packet(&packet,1,(struct sockaddr*)(&server),my_id,0);
}
/* I want to be born again */
void send_reenter_game(void)
{
char packet;
packet=P_REENTER_GAME;
send_packet(&packet,1,(struct sockaddr*)(&server),my_id,0);
}
/* send end of game to server */
void end_game(void)
{
char packet;
packet=P_END;
send_packet(&packet,1,(struct sockaddr*)(&server),my_id,0);
}
/* send chat message */
void send_message(char *msg, char flags)
{
static char packet[MAX_MESSAGE_LENGTH + 2];
int len;
len = strlen(msg) + 1;
packet[0] = P_MESSAGE;
packet[1] = flags;
memcpy(packet + 2, msg, len);
send_packet(packet, len + 2, (struct sockaddr *)(&server), my_id, 0);
}
/* send end of game to server */
void send_keyboard(void)
{
char packet[3];
packet[0]=P_KEYBOARD;
packet[1]=keyboard_status.status;
packet[2]=keyboard_status.weapon;
send_packet(packet,3,(struct sockaddr*)(&server),my_id,0);
}
void reset_keyboard(void)
{
keyboard_status.status=0;
keyboard_status.weapon=0;
}
/* recompute object positions */
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 &&
p->next->member.type!=T_BFGCELL){ /* 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;}
}
}
}
/* 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 ||
p->next->member.type == T_JETFIRE) &&
(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;
}
}
}
#define FIRE_SHOOTING weapon[current_weapon].cadence-4+HOLD_GUN_AFTER_SHOOT
/* hero's next anim position when walking to the right */
int _next_anim_right(int pos,int status, int ttl)
{
int start,offset;
if (pos<=18)
start=10; /* normal */
else
{
if (pos<=46)start=(status & S_CHAINSAW)?97:38; /* holding gun */
else
{
if (pos<=55)start=(status & S_CHAINSAW)?106:47; /* shooting */
else start=64; /* creeping */
}
}
offset=pos-start;
if (status & S_CREEP)
start=64; /* creeping */
else
{
if (status & S_SHOOTING)
{
if (ttl>=FIRE_SHOOTING) start=(status & S_CHAINSAW)?106:47; /* shooting */
else start=(status & S_CHAINSAW)?97:38; /* holding a gun */
}
else start=10; /* normal */
}
return (status & S_CREEP)?((offset+1)&7)+start:start+(offset&7)+1;
}
/* hero's next anim position when walking to the left */
int _next_anim_left(int pos,int status, int ttl)
{
int start,offset;
if (pos<=8)start=0; /* normal */
else
{
if (pos<=28)start=(status & S_CHAINSAW)?79:20; /* holding gun */
else
{
if (pos<=37)start=(status & S_CHAINSAW)?88:29; /* shooting */
else start=56; /* creeping */
}
}
offset=pos-start;
if (status & S_CREEP)
start=56; /* creeping */
else
{
if (status & S_SHOOTING)
{
if (ttl>=FIRE_SHOOTING) start=(status & S_CHAINSAW)?88:29; /* shooting */
else start=(status & S_CHAINSAW)?79:20; /* holding a gun */
}
else start=0; /* normal */
}
return (status & S_CREEP)?((offset+1)&7)+start:start+(offset&7)+1;
}
/* update hero animating position */
void update_anim(struct it* obj)
{
if (!(obj->status & S_WALKING)) /* not walking */
switch((obj->status & (S_LOOKLEFT | S_LOOKRIGHT)))
{
case 0: /* look at me */
obj->anim_pos=(obj->status & S_CREEP)?72:9;
break;
case S_LOOKLEFT: /* look left */
if (obj->status & S_SHOOTING)
{
if (obj->status & S_GRENADE)
obj->anim_pos = (obj->ttl>=((weapon[WEAPON_GRENADE].cadence>>1)+HOLD_GUN_AFTER_SHOOT)) ?
73 : (obj->ttl>=((weapon[WEAPON_GRENADE].cadence>>2)+HOLD_GUN_AFTER_SHOOT)) ?
74 : ((obj->ttl>=HOLD_GUN_AFTER_SHOOT) ? 75 : 0);
else if (obj->status & S_CHAINSAW) /* maniak ma motorovku */
obj->anim_pos=(obj->ttl>=FIRE_SHOOTING) ? 88 : 79;
else
obj->anim_pos=(obj->ttl>=FIRE_SHOOTING) ? 29 : 20;
} else
obj->anim_pos=0;
if (obj->status & S_CREEP)
obj->anim_pos=56;
break;
case S_LOOKRIGHT: /* look right */
if (obj->status & S_SHOOTING)
{
if (obj->status & S_GRENADE)
obj->anim_pos = (obj->ttl>=((weapon[WEAPON_GRENADE].cadence>>1)+HOLD_GUN_AFTER_SHOOT)) ?
76 : (obj->ttl>=((weapon[WEAPON_GRENADE].cadence>>2)+HOLD_GUN_AFTER_SHOOT)) ?
77 : ((obj->ttl>=HOLD_GUN_AFTER_SHOOT) ? 78 : 10);
else if (obj->status & S_CHAINSAW) /* maniak ma motorovku */
obj->anim_pos=(obj->ttl>=FIRE_SHOOTING) ? 106 : 97;
else
obj->anim_pos=(obj->ttl>=FIRE_SHOOTING) ? 47 : 38;
} else
obj->anim_pos=10;
if (obj->status & S_CREEP)
obj->anim_pos=64;
break;
}
else /* walking */
{
switch (obj->status & (S_LOOKLEFT | S_LOOKRIGHT))
{
case S_LOOKLEFT: /* walk left */
obj->anim_pos=_next_anim_left(obj->anim_pos,obj->status,obj->ttl);
break;
case S_LOOKRIGHT: /* walk right */
obj->anim_pos=_next_anim_right(obj->anim_pos,obj->status,obj->ttl);
break;
}
}
}
/* draw scene into screenbuffer*/
void draw_scene(void)
{
struct object_list *p;
unsigned char fore;
#ifdef TRI_D
if (TRI_D_ON)
{
tri_d=1;
show_window(double2int(hero->x)-SCREEN_XOFFSET,double2int(hero->y)-SCREEN_YOFFSET);
tri_d=0;
}
#endif
show_window(double2int(hero->x)-SCREEN_XOFFSET,double2int(hero->y)-SCREEN_YOFFSET);
for (fore=0;fore<=1;fore++)
{
for(p=&objects;p->next;p=p->next)
{
if ((obj_attr[p->next->member.type].foreground)!=fore)continue;
if (&(p->next->member)==hero)continue;
if (p->next->member.type==T_PLAYER)update_anim(&(p->next->member));
else {p->next->member.anim_pos++;p->next->member.anim_pos%=sprites[p->next->member.sprite].n_steps;}
if (p->next->member.status == WEAPON_CHAINSAW) continue;
if (!(p->next->member.status & S_DEAD)&&!(p->next->member.status & S_INVISIBLE)) /* don't draw hidden objects and dead players */
{
#ifdef TRI_D
if(TRI_D_ON)
{
tri_d=1;
put_sprite(
double2int(p->next->member.x-hero->x)+fore+SCREEN_XOFFSET,
double2int(p->next->member.y-hero->y)+SCREEN_YOFFSET,
sprites[p->next->member.sprite].positions+sprites[p->next->member.sprite].steps[p->next->member.anim_pos],
1
);
tri_d=0;
}
#endif
put_sprite(
double2int(p->next->member.x-hero->x)+SCREEN_XOFFSET,
double2int(p->next->member.y-hero->y)+SCREEN_YOFFSET,
sprites[p->next->member.sprite].positions+sprites[p->next->member.sprite].steps[p->next->member.anim_pos],
1
);
if (p->next->member.type == T_PLAYER) {
if ((p->next->member.status & S_JETPACK_ON) && !(p->next->member.status & S_CREEP)) {
if (p->next->member.status & S_LOOKRIGHT)
put_sprite(
double2int(p->next->member.x-hero->x)+SCREEN_XOFFSET-1,
double2int(p->next->member.y-hero->y)+SCREEN_YOFFSET+4,
sprites[jetpack_sprite].positions+sprites[jetpack_sprite].steps[1], 1);
else if (p->next->member.status & S_LOOKLEFT)
put_sprite(
double2int(p->next->member.x-hero->x)+SCREEN_XOFFSET+PLAYER_WIDTH-4,
double2int(p->next->member.y-hero->y)+SCREEN_YOFFSET+4,
sprites[jetpack_sprite].positions+sprites[jetpack_sprite].steps[0], 1);
}
if ((gsbi += 10) > 1000) {
gsbi++;
gsbi %= 3;
}
if (p->next->member.status & S_ONFIRE) {
if (p->next->member.status & S_CREEP)
put_sprite(
double2int(p->next->member.x-hero->x)+SCREEN_XOFFSET-5,
double2int(p->next->member.y-hero->y)+SCREEN_YOFFSET-2,
sprites[fire_sprite].positions+sprites[fire_sprite].steps[gsbi % 3 + 3], 1);