-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.c
1812 lines (1663 loc) · 51.7 KB
/
engine.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
/* Engine */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <math.h>
#include "engine.h"
struct Game game;
static int dir_x[4]={-1,0,1,0},dir_y[4]={0,-1,0,1};
int editMode=0;
#define TP_ANIMATED 1
#define TP_MIRROR_BELOW 2
#define TP_MIRROR_BESIDE 4
#define TP_MAP_MODULUS 8 // Pick subtile with map location in mind
#define TP_NPC 16
#define TP_SUBSTRATE 32 // You can stand on it, can't walk through it
#define TP_SLOPELEFT 64
#define TP_SLOPERIGHT 128
#define TP_SLIPPERY 256
struct TileProp tileProp[]={
{'!',M_WALKRIGHT1,3,3,5,TP_ANIMATED|TP_MIRROR_BELOW,16,5,16,43},
{'0',M_FIRELEFT,3,3,1,TP_MIRROR_BESIDE,16,5,16,43},
{'0',M_SHOOTUPLEFT,3,3,1,TP_MIRROR_BESIDE,16,5,16,43},
{'0',M_SHOOTDOWNLEFT,3,3,1,TP_MIRROR_BESIDE,16,5,16,43},
{'0',M_DEAD,3,3,1,0,16,5,16,43},
{'+',M_FRUIT1,1,4,1,TP_MAP_MODULUS,0,0,16,16},
{'^',M_CHECKPOINT,1,2,1,0,0,0,16,32},
{'{',M_BIRDRIGHT1,1,1,2,TP_MIRROR_BELOW|TP_NPC,0,0,16,16},
{'E',M_EVILDUCKLEFT1,3,2,2,TP_MIRROR_BESIDE|TP_NPC,0,0,48,32},
{'s',M_SHOOTERRIGHT1,2,2,2,TP_MIRROR_BELOW|TP_NPC,0,0,32,32},
{'t',M_TRAMPOLINE1,4,1,1,0,0,0,64,16}, // Done as special case :-(
{'u',M_TRAMPOLINE2,4,1,1,0,0,0,64,16},
{'$',M_CHEST1,2,2,4,TP_ANIMATED,0,0,32,32},
{'*',M_CHICKRIGHT1,1,1,2,TP_ANIMATED|TP_NPC|TP_MIRROR_BELOW,0,-3,16,19},
{'Y',M_YINGYANG,1,1,1,0,0,0,16,16},
{'S',M_SHIELD,1,1,1,0,0,0,16,16},
{'B',M_BOMB,1,1,1,0,6,6,5,5},
{'h',M_MINIHEALTH,1,1,1,0,0,0,16,16},
{'H',M_FULLHEALTH,1,1,1,0,0,0,16,16},
{'T',M_TIMEBONUS,1,1,1,0,0,0,16,16},
{'L',M_LIFEBONUS,1,1,1,0,0,0,16,16},
{'/',M_SLOPELEFT,2,1,1,TP_SUBSTRATE|TP_SLOPELEFT,0,0,32,16},
{'>',M_SLOPERIGHT,2,1,1,TP_SUBSTRATE|TP_SLOPERIGHT,0,0,32,16},
{'|',M_GENTLESLOPELEFT,3,1,1,TP_SUBSTRATE|TP_SLOPELEFT,0,0,48,16},
{'}',M_GENTLESLOPERIGHT,3,1,1,TP_SUBSTRATE|TP_SLOPERIGHT,0,0,48,16},
{'(',M_45SLOPELEFT,1,1,1,TP_SUBSTRATE|TP_SLOPELEFT,0,0,16,16},
{')',M_45SLOPERIGHT,1,1,1,TP_SUBSTRATE|TP_SLOPERIGHT,0,0,16,16},
{'a',M_SLIPPERY45LEFT,1,1,1,TP_SUBSTRATE|TP_SLOPELEFT|TP_SLIPPERY,0,0,16,16},
{'b',M_SLIPPERY45RIGHT,1,1,1,TP_SUBSTRATE|TP_SLOPERIGHT|TP_SLIPPERY,0,0,16,16},
{'<',M_STEEPSLOPELEFT,1,2,1,TP_SUBSTRATE|TP_SLOPELEFT,0,0,16,32},
{'\\',M_STEEPSLOPERIGHT,1,2,1,TP_SUBSTRATE|TP_SLOPERIGHT,0,0,16,32},
{'#',M_SOLIDGROUND,2,3,1,TP_MAP_MODULUS|TP_SUBSTRATE,0,0,16,16},
{'.',M_SECRETAREA,2,3,1,TP_MAP_MODULUS|TP_SUBSTRATE,0,0,16,16},
{'1',M_PLATFORMTL,1,1,1,TP_SUBSTRATE,0,0,16,16},
{'2',M_PLATFORMTL+1,1,1,1,TP_SUBSTRATE,0,0,16,16},
{'3',M_PLATFORMTL+2,1,1,1,TP_SUBSTRATE,0,0,16,16},
{'4',M_PLATFORMTL+16,1,1,1,0,0,0,16,16},
{'5',M_PLATFORMTL+17,1,1,1,0,0,0,16,16},
{'6',M_PLATFORMTL+18,1,1,1,0,0,0,16,16},
{'7',M_PLATFORMTL+32,1,1,1,0,0,0,16,16},
{'8',M_PLATFORMTL+33,1,1,1,0,0,0,16,16},
{'9',M_PLATFORMTL+34,1,1,1,0,0,0,16,16},
{'v',M_BOMBABLEWALL,1,1,1,TP_SUBSTRATE,0,0,16,16},
{':',M_SLIPPERYL,1,1,1,TP_SUBSTRATE|TP_SLIPPERY,0,0,16,16},
{';',M_SLIPPERYR,1,1,1,TP_SUBSTRATE|TP_SLIPPERY,0,0,16,16},
{'_',M_SLIPPERYT,1,1,1,TP_SUBSTRATE|TP_SLIPPERY,0,0,16,16},
{'m',M_POUNDPLATFORM,1,1,1,TP_SUBSTRATE,0,0,16,16},
{'w',M_ROCKETPLATFORM,1,1,1,TP_SUBSTRATE,0,0,16,16},
{'i',M_INVISIBLEPLATFORM,1,1,2,TP_SUBSTRATE,0,0,16,16},
{'?',M_EXIT,3,1,1,0,0,0,48,16},
{'O',M_RING,3,3,1,0,0,0,48,48},
{'Q',M_EXPLODINGGROUND,1,1,3,TP_SUBSTRATE|TP_ANIMATED,0,0,16,16},
{'j',M_DECORATION1,3,3,1,0,0,0,48,48},
{'M',M_FANCYSOLIDGROUND,2,3,1,TP_SUBSTRATE|TP_MAP_MODULUS,0,0,16,16},
{',',M_FANCYSKY,2,3,1,0,0,0,32},
{'k',M_DECORATION2,3,3,1,0,0,0,48,48},
{'l',M_TREE,2,3,1,0,0,0,32,48},
{'f',M_FENCE,2,3,1,0,0,0,32,48},
{'z',M_HEROSHOT,1,1,1,0,6,6,5,5},
{'x',M_ENEMYSHOT,1,1,1,0,6,6,5,5},
//{'c',M_HEALTHMETER,1,1,1,0,0,0,16,16},
{'C',M_WALLCLIMBERRIGHT,1,2,2,TP_NPC,0,0,16,32},
{'D',M_WALLCLIMBERLEFT,1,2,2,TP_NPC,0,0,16,32},
{'g',M_YELLOWKEY,1,1,1,0,0,0,16,16},
{'n',M_GREENKEY,1,1,1,0,0,0,16,16},
{'p',M_BLUEKEY,1,1,1,0,0,0,16,16},
{'r',M_REDKEY,1,1,1,0,0,0,16,16},
{'G',M_YELLOWDOOR1,2,3,2,TP_SUBSTRATE,0,0,32,48},
{'N',M_GREENDOOR1,2,3,2,TP_SUBSTRATE,0,0,32,48},
{'P',M_BLUEDOOR1,2,3,2,TP_SUBSTRATE,0,0,32,48},
{'R',M_REDDOOR1,2,3,2,TP_SUBSTRATE,0,0,32,48},
{'A',M_TELEPORTATION,3,3,2,TP_NPC|TP_ANIMATED,0,0,48,48},
{'x',M_YELLOWTELEPORTER,3,3,1,0,0,0,48,48},
{'X',M_GREENTELEPORTER,3,3,1,0,0,0,48,48},
{'z',M_BLUETELEPORTER,3,3,1,0,0,0,48,48},
{'Z',M_REDTELEPORTER,3,3,1,0,0,0,48,48},
//{0,0,0,0,0,0,0,0,0,0}
};
#define MAXCHAR 256
#define MAXTILE 512
int mapCharToTile[MAXCHAR];
struct TileProp *mapTileToProp[MAXTILE];
#define PRECISION 480
#define JUMPINITIAL 1000
#define ROCKETINITIAL 2000
#define PATROLDIST 4
#define TERMINALVELOCITY 1000
#define HIGHGRAVITY 28
#define LOWGRAVITY 13
#define HORIZ_ACCEL 5
#define HORIZ_DECEL 7
#define HORIZ_SKID 23 // Deceleration when pointing the joystick
// opposite to the direction of motion.
#define MAX_SPEED 900
static char *defaultlevel=
" \n"
" TTT H H BBB ++++ + + +++ \n"
" T H H B + ++ + + + \n"
" T HHH BBB +++ + + + + + \n"
" T H H B + + ++ + + \n"
" T H H BBB ++++ + + +++ \n"
" !!! 12223 \n"
" !!! 45556 \n"
" !!! 45556 \n"
"###### #################78889 \n"
"###### ################################# \n"
"###### #### ++++++ ################ ##\n"
"###### #### ++++++ ###### ###\n"
"####### .... ++++++ ###### +++ ######\n"
"####### .... ++++++ ###### $$ $$ ########\n"
"#######tttt .... ++++++ ###### $$ $$ ##########\n"
"#####################################################\n"
;
void set_tile(int x,int y,int tile)
{
x+=game.l/TILEHEIGHT;
y+=game.t/TILEWIDTH;
if(x<0 || x>=game.w || y<0 || y>=game.h) return;
game.map[x+y*game.w]=tile;
}
struct TileProp *tile_to_prop(int tile)
{
if(tile<0 || tile>=MAXTILE) return NULL;
return mapTileToProp[tile];
}
int ch_to_tile(int ch)
{
if(ch<0 || ch>=MAXCHAR) return -1;
return mapCharToTile[ch];
}
void set_tiles(int x,int y,int tile)
{
struct TileProp *prop;
int i,j;
if(tile<0 || tile>=MAXTILE) {
set_tile(x,y,-1);
return;
}
prop=mapTileToProp[tile];
if(prop==NULL) {
set_tile(x,y,-1);
return;
}
if(prop->flags&TP_MAP_MODULUS) {
int tilex=x%prop->gfxw;
int tiley=y%prop->gfxh;
if(prop->gfxh==1) tilex=(x+y)%prop->gfxw; // Special case
if(prop->gfxw==1) tiley=(x+y)%prop->gfxh; // Special case
set_tile(x,y,prop->id+tilex+TILESACROSS*tiley);
return;
}
// Otherwise draw whole object.
for(j=0;j<prop->gfxh;j++) {
for(i=0;i<prop->gfxw;i++) {
set_tile(x+i,y+j,prop->id+i+TILESACROSS*j);
}
}
}
void new_game()
{
int i;
int mapped=0;
game.level=1;
game.lives=5;
game.score=0;
game.invalidate=0;
game.map=0;
// Initialize maps:
for(i=0;i<MAXCHAR;i++) mapCharToTile[i]=-1;
for(i=0;i<MAXTILE;i++) mapTileToProp[i]=NULL;
// Now mark all of the ones in the list
for(i=0;i<sizeof(tileProp)/sizeof(struct TileProp);i++) {
int x,y;
mapCharToTile[tileProp[i].ch]=tileProp[i].id;
for(y=0;y<tileProp[i].gfxh*(tileProp[i].flags&TP_MIRROR_BELOW?2:1);y++) {
for(x=0;x<tileProp[i].gfxw*tileProp[i].frames*(tileProp[i].flags&TP_MIRROR_BESIDE?2:1);x++) {
// ASSERT((mapTileToProp[tileProp[i].id+x+y*TILESACROSS]-tileProp))==-1);
mapTileToProp[tileProp[i].id+x+y*TILESACROSS]=tileProp+i;
mapped++;
}
}
}
#ifdef _DEBUG
printf("char to tile table:\n");
for(i=0;i<MAXCHAR;i++) {
if(mapCharToTile[i]<0) continue;
if(i>32 && i<127) printf("%c=",i);
printf("%d\n",mapCharToTile[i]);
}
printf("tile to prop table:\n");
for(i=0;i<MAXCHAR;i++) {
printf("%02x ",(mapTileToProp[i]-tileProp)&255);
if((i%16)==15) printf("\n");
}
#endif
// ASSERT(mapped==MAXTILE);
game.player.prop = mapTileToProp[M_WALKRIGHT1];
}
int test_npc(int x,int y)
{
int i;
for(i=0;i<game.npcCount;i++) {
// Overlapping
if( x >= game.npc[i].x - game.npc[i].prop->offsetx &&
x < game.npc[i].x - game.npc[i].prop->offsetx +
TILEWIDTH * game.npc[i].prop->gfxw &&
y >= game.npc[i].y - game.npc[i].prop->offsety &&
y < game.npc[i].y - game.npc[i].prop->offsety +
TILEHEIGHT * game.npc[i].prop->gfxh) return i;
}
return -1;
}
struct TileProp *tile_prop( int tilex, int tiley) {
int m;
if( tilex < 0 || tilex >= game.w) return NULL;
if( tiley < 0 || tiley >= game.h) return NULL;
m = game.map[tilex + tiley * game.w];
if( m < 0) return NULL;
return mapTileToProp[m];
}
// This function decides whether the given tile contains solid substrate.
int is_solid( struct TileProp *p, int tilex, int tiley, int allsolid) {
struct TileProp *o;
if( !(p->flags & TP_SUBSTRATE)) return 0;
if( allsolid) return 1;
if( tiley == 0) return 1;
if( p->flags & (TP_SLOPELEFT|TP_SLOPERIGHT)) {
if( p->gfxh > 1) return 1;
} else {
o = tile_prop(tilex,tiley-1);
if( o && (o->flags & TP_SUBSTRATE)) return 1;
}
o = tile_prop(tilex,tiley+1);
return o &&
(o->flags&(TP_SUBSTRATE|TP_SLOPERIGHT|TP_SLOPELEFT)) == TP_SUBSTRATE;
}
// This function computes the coordinate position where a wide ray
// would intersect with an object. The ray is specified as minimum
// coordinate on the axis perpendicular to motion, ray width, and
// direction. The ray is assumed to proceed from infinity. The return
// value is a coordinate on the axis parallel to motion. The object
// is assumed to have top-left corner at 0,0.
//
// It is the responsibility of the caller to use a simple boundbox check
// to ensure that an intersection exists. Otherwise, the results of
// this function are undefined. (It may even crash.)
//
// In the special case where the ray intersects but is allowed to pass
// through the object, this function returns -1.
//
int wide_ray_intersect( struct TileProp *obj, int tilex, int tiley,
int mincoord, int breadth, int dir, int allsolid)
{
int x;
// Make the bottom edge of the trampoline downwards-impassable
if( dir==D_DOWN && (obj->id==M_TRAMPOLINE1 || obj->id==M_TRAMPOLINE2)) {
return obj->h;
}
if( !(obj->flags & TP_SUBSTRATE)) return -1;
// Disallow downward passage into any type of substrate
if( dir == D_DOWN) {
if( obj->flags & (TP_SLOPERIGHT|TP_SLOPELEFT)) {
x = (obj->flags & TP_SLOPERIGHT)?mincoord:obj->w-mincoord-breadth;
if( x < 0) return 0;
return x * obj->h / obj->w;
}
// A filled substrate tile - intersection point is at top
return 0;
}
// Disallow upward passage into solid substrate
if( dir == D_UP) {
if( is_solid(obj, tilex, tiley, allsolid)) return obj->h;
return -1;
}
// Disallow horizontal passage through the face of a slope.
if( ((obj->flags & TP_SLOPERIGHT) && dir == D_LEFT) ||
((obj->flags & TP_SLOPELEFT) && dir == D_RIGHT)) {
// If the slope is non-solid, then only the character's
// toes are prohibited from entering.
if( mincoord + breadth > obj->h &&
!is_solid(obj,tilex,tiley, allsolid)) return -1;
x = ((mincoord + breadth) * obj->w + obj->h - 1) / obj->h;
if( x > obj->w) x = obj->w;
return (obj->flags & TP_SLOPERIGHT) ? x : obj->w - x;
}
if( !is_solid(obj, tilex, tiley, allsolid)) return -1;
// Disallow horizontal passage into solid substrate
return dir == D_LEFT ? obj->w : 0;
}
// This function determines whether a rectangular object can travel a
// certain distance in a certain cardinal direction without colliding with
// terrain. If a collision would occur, it returns the distance prior to
// the collision. Otherwise it returns distance.
int offset_motion_limit( struct Character *c, int offset_x, int offset_y,
int distance, int dir)
{
struct TileProp *t, *p = c->prop;
int mindist = -1, o, i, edge, dist, h, step;
int major, minor, mintile, maxtile, from, limit;
int c_x = c->x + offset_x, c_y = c->y + offset_y;
// Just an optimization
if( distance == 0) return 0;
if( dir == D_UP) {
from = (c_y - 1) / TILEHEIGHT;
limit = (c_y - distance) / TILEHEIGHT - 1;
} else if( dir == D_DOWN) {
from = (c_y + p->h) / TILEHEIGHT;
limit = (c_y + p->h + distance - 1) / TILEHEIGHT + 1;
} else if( dir == D_LEFT) {
from = (c_x - 1) / TILEWIDTH;
limit = (c_x - distance) / TILEWIDTH - 1;
} else {
from = (c_x + p->w) / TILEWIDTH;
limit = (c_x + p->w + distance - 1) / TILEWIDTH + 1;
}
step = (dir == D_UP || dir == D_LEFT) ? -1 : 1;
h = (dir == D_LEFT || dir == D_RIGHT);
mintile = h ? c_y / TILEHEIGHT : c_x / TILEWIDTH;
maxtile = h ? (c_y+p->h-1)/TILEHEIGHT : (c_x+p->w-1)/TILEWIDTH;
for( major = from; major != limit; major+=step) { // y
for( minor = mintile; minor <= maxtile; minor++) { // x
t = tile_prop( h?major:minor, h?minor:major);
if( !t) continue;
if( t->flags & TP_MAP_MODULUS) o = 0;
else o = game.map[(h?major:minor)+(h?minor:major)*game.w] - t->id;
i = wide_ray_intersect( t, h?major:minor, h?minor:major, h
? (c_y - (minor - o/TILESACROSS) * TILEHEIGHT)
: (c_x - (minor - o%TILESACROSS) * TILEWIDTH),
h ? p->h : p->w, dir, c!=&game.player);
if( i != -1) {
if( h) {
edge = i + TILEWIDTH * (major - o % TILESACROSS);
dist = (dir==D_LEFT) ? c_x - edge : edge - (c_x+p->w);
} else {
edge = i + TILEHEIGHT * (major - o / TILESACROSS);
dist = (dir==D_UP) ? c_y - edge : edge - (c_y+p->h);
}
if( dist >= 0 && (mindist == -1 || dist < mindist))
mindist = dist;
}
}
if( mindist != -1) {
if( mindist < distance) distance = mindist;
break;
}
}
if( dir == D_LEFT && c->x - p->offsetx + offset_x - distance < 0)
distance = c->x - p->offsetx + offset_x;
if( dir == D_RIGHT &&
c->x-p->offsetx+offset_x+p->gfxw*TILEWIDTH+distance>game.w*TILEWIDTH)
distance = (game.w-p->gfxw)*TILEWIDTH - (c->x-p->offsetx+offset_x);
return distance;
}
int motion_limit( struct Character *c, int distance, int dir)
{
return offset_motion_limit( c, 0, 0, distance, dir);
}
// Check whether the character is standing on solid ground
int landed(struct Character *c) {
return c->y_velocity >= 0 &&
motion_limit( c, 1, D_DOWN) == 0;
}
struct TileProp *landed_on( struct Character *c) {
struct TileProp *t, *p = c->prop;
int i;
if( !landed( c)) return NULL;
for( i = 0; i <= p->w/TILEWIDTH/2; i++) {
t = tile_prop((c->x+p->w/2)/TILEWIDTH + i,(c->y+p->h)/TILEHEIGHT);
if( t && (t->flags & TP_SUBSTRATE)) return t;
t = tile_prop((c->x+p->w/2)/TILEWIDTH - i - 1,(c->y+p->h)/TILEHEIGHT);
if( t && (t->flags & TP_SUBSTRATE)) return t;
}
return mapTileToProp[M_SOLIDGROUND];
}
void accelerate( struct Character *c, int joystick_dir)
{
struct TileProp *t;
int dir = joystick_dir, accel, slipping;
if( c->x_velocity < 0) dir = D_LEFT;
if( c->x_velocity > 0) dir = D_RIGHT;
// Ignore up and down directions
if( joystick_dir != D_LEFT && joystick_dir != D_RIGHT) joystick_dir=D_NONE;
t = landed_on(c);
slipping = (t && (t->flags & TP_SLIPPERY));
if( joystick_dir != D_NONE) {
accel = dir_x[joystick_dir] * (slipping ? HORIZ_ACCEL / 2 :
(dir==joystick_dir ? (t?HORIZ_ACCEL:HORIZ_ACCEL*3/4):HORIZ_SKID));
} else if(t && dir != D_NONE) {
accel = -dir_x[dir] * (slipping ? HORIZ_ACCEL / 4 : HORIZ_DECEL);
} else {
accel = 0;
}
c->x_velocity += accel;
if( c->x_velocity < -MAX_SPEED) c->x_velocity = -MAX_SPEED;
if( c->x_velocity > MAX_SPEED) c->x_velocity = MAX_SPEED;
// Disallow instantaneous change of direction
if( c->x_velocity < 0 && dir == D_RIGHT ||
c->x_velocity > 0 && dir == D_LEFT) c->x_velocity = 0;
}
void handle_x_momentum( struct Character *c)
{
struct TileProp *p = c->prop;
int dir, proposed_dist, remain, backoff, dist, maxup, maxdown;
if( c->x_velocity == 0) return;
dir = (c->x_velocity < 0 ? D_LEFT : D_RIGHT);
proposed_dist = (dir == D_LEFT ? -c->x_velocity : c->x_velocity);
// Butt the character up against the edge of its current pixel
remain = (dir == D_LEFT ? c->x_residue : PRECISION-1-c->x_residue);
if( proposed_dist <= remain) {
// We aren't going far enough to reach the edge of the current pixel.
c->x_residue += (dir == D_LEFT ? -proposed_dist : proposed_dist);
return;
}
c->x_residue += (dir == D_LEFT ? -remain : remain);
proposed_dist -= remain;
backoff = PRECISION - 1 - (proposed_dist - 1) % PRECISION;
proposed_dist = (proposed_dist + PRECISION - 1) / PRECISION;
dist = motion_limit( c, proposed_dist, dir);
c->x += (dir == D_LEFT ? -dist : dist);
proposed_dist -= dist;
while( proposed_dist > 0) {
dist = 0;
// If we try to take a step up, will we bump our head?
maxup = motion_limit( c, 2, D_UP);
if( maxup >= 1) {
dist = offset_motion_limit( c, 0, -1, proposed_dist, dir);
if( dist) {
// Walk up a step of height 1
c->y --;
c->x += (dir == D_LEFT ? -dist : dist);
} else if( maxup >= 2) {
dist = offset_motion_limit( c, 0, -2, proposed_dist, dir);
if( dist) {
// Walk up a step of height 2
c->y -= 2;
c->x += (dir == D_LEFT ? -dist : dist);
}
}
}
if( dist == 0) break;
proposed_dist -= dist;
}
if( proposed_dist == 0) {
c->x_residue += (dir == D_LEFT ? backoff : -backoff);
} else {
// We hit a wall
c->x_velocity = 0;
}
// If the player is one or two pixels above ground and his
// y_velocity is 0, he's probably just walking downhill. Lower him
// so the falling logic doesn't kick in.
if( c->y_velocity == 0) {
maxdown = motion_limit( c, 3, D_DOWN);
if( maxdown < 3) c->y += maxdown;
}
}
void handle_y_momentum( struct Character *c)
{
struct TileProp *p = c->prop;
int dir, proposed_y, proposed_residue, proposed_dist, dist;
proposed_y = c->y + c->y_velocity / PRECISION;
proposed_residue = c->y_residue += c->y_velocity % PRECISION;
if( proposed_residue < 0) {
proposed_residue += PRECISION;
proposed_y --;
} else if( proposed_residue >= PRECISION) {
proposed_residue -= PRECISION;
proposed_y ++;
}
dir = (proposed_y < c->y ? D_UP : D_DOWN);
proposed_dist = (dir == D_UP ? c->y-proposed_y : proposed_y-c->y);
dist = motion_limit(c, proposed_dist, dir);
if( dist < proposed_dist) {
c->y = (dir == D_UP ? c->y - dist : c->y + dist);
c->y_residue = 0;
c->y_velocity = 0;
} else {
c->y = proposed_y;
c->y_residue = proposed_residue;
}
}
void apply_gravity( struct Character *c)
{
if( !landed( c)) {
// The player is falling - accelerate downwards
c->y_velocity += c->gravity;
if( c->y_velocity > TERMINALVELOCITY) {
// Terminal velocity
c->y_velocity = TERMINALVELOCITY;
}
}
}
int add_npc(int type,int x,int y,int dir,int id)
{
int i;
i=game.npcCount;
if(i>=MAXNPC) return -1;
//printf("Adding type %d at %d,%d dir=%d [prop=%d; id=%d]\n",type,x,y,dir,(mapTileToProp[id]-tileProp),id);
game.npcCount++;
game.npc[i].type=type;
game.npc[i].x=x;
game.npc[i].x_residue=PRECISION/2;
game.npc[i].y=y;
game.npc[i].y_residue=PRECISION/2;
game.npc[i].homex=x;
game.npc[i].homey=y;
game.npc[i].facing=dir;
game.npc[i].x_velocity=0;
game.npc[i].y_velocity=0;
game.npc[i].gravity=0;
game.npc[i].frame=0;
game.npc[i].health=100;
game.npc[i].prop=mapTileToProp[id];
game.npc[i].delay=0;
return i;
}
int add_efx(int type,int x,int y,int delay)
{
int i=game.efxCount;
if(i>=MAXEFX) return -1;
#if 1
// Find left edge of trampoline
for(;;) {
int id=-1;
if(x>=0 && x<game.w && y>=0 && y<game.h) id=game.map[x+game.w*y];
struct TileProp *tileProp=tile_to_prop(id);
if(tileProp==NULL) return -1; // Corrupt mapping
if(tileProp->id==id) break;
x--;
}
game.effect[i].type=type;
game.effect[i].x=x;
game.effect[i].y=y;
game.effect[i].tileProp=tile_to_prop(game.map[x+game.w*y]);
game.effect[i].delay=delay;
game.efxCount++;
#endif
return i;
}
int find_efx(int x,int y)
{
#if 1
int i;
for(i=0;i<game.efxCount;i++) {
int dx=x-game.effect[i].x;
int dy=y-game.effect[i].y;
if(dx>=0 && dx<game.effect[i].tileProp->gfxw && dy>=0 && dy<game.effect[i].tileProp->gfxh) {
return i;
}
}
#endif
return -1;
}
void new_level(int level)
{
char buf[256];
sprintf(buf,"map/level%03d.txt",level);
load_level(buf);
game.level=level;
play_sound(S_LEVELSONG);
play_sound(S_LEVELSTART);
game.mode=MODE_GETREADY;
}
void construct_level(int w,int h) {
int i;
if(game.map) free(game.map);
if(w<SCREENWIDTH) w=SCREENWIDTH;
if(h<SCREENHEIGHT) h=SCREENHEIGHT;
//printf("Decided on %dx%d map\n",w,h);
game.map=(short *)malloc(w*h*sizeof(short));
game.w=w;
game.h=h;
game.t=0;
game.l=0;
game.level=1;
game.npcCount=0; // Reset for new level.
game.efxCount=0; // Reset for new level.
game.checkpointX=0;
game.checkpointY=0;
game.collected=0;
game.available=0; // Number of pieces of fruit to collect.
game.bonus[0]=0;
game.bonus[1]=0;
game.bonus[2]=0;
game.activeTeleport=-1;
for(i=0;i<4;i++) {
game.key[i]=0;
}
for(i=0;i<8;i++) {
game.teleporterX[i]=-1;
game.teleporterY[i]=-1;
}
for(i=0;i<w*h;i++) game.map[i]=-1;
}
void add_teleporter(int teleporterNo,int x,int y)
{
if(game.teleporterX[teleporterNo*2]==-1) {
game.teleporterX[teleporterNo*2]=x;
game.teleporterY[teleporterNo*2]=y;
} else {
game.teleporterX[teleporterNo*2+1]=x;
game.teleporterY[teleporterNo*2+1]=y;
}
}
void load_level(const char *fname)
{
FILE *file;
int len=0;
unsigned char *data=0;
int i,j,k;
int x,y;
int w=0,h=0;
int bol=0;
int attr=0;
enum ParseMode { PM_BOL,PM_COMMENT,PM_LINE,PM_ATTRIBUTE } pm=PM_BOL;
const char *buf=fname;
file=fopen(buf,"rb");
if(file) {
fseek(file,0,SEEK_END);
len=ftell(file);
fseek(file,0,SEEK_SET);
if(len>0) {
data=(unsigned char *)malloc(len+1);
data[len]=0;
fread(data,len,1,file);
fclose(file); // Slurped it in.
} else {
fprintf(stderr,"Could not load empty file %s\n",buf);
fclose(file);
}
} else {
fprintf(stderr,"Could not find needed file %s\n",buf);
}
if(data==0) {
fprintf(stderr,"Loading default level\n%s\n",defaultlevel);
// Default map to the rescue.
data=(unsigned char *)malloc(strlen(defaultlevel)+1);
len=strlen(defaultlevel);
strcpy((char *)data,defaultlevel);
}
// Now figure out what it says:
// Pass 1 through file:
for(i=0;i<len;i++) {
if(data[i]=='"') {
pm=PM_COMMENT;
} else if(data[i]=='\r' || data[i]=='\n') {
if(pm==PM_LINE) {
if(w<i-bol) w=i-bol;
h++;
}
pm=PM_BOL;
bol=i+1;
} else if(pm==PM_COMMENT) {
// Skip comment
} else if(data[i]=='=') {
attr=i+1;
pm=PM_ATTRIBUTE;
} else if(pm!=PM_ATTRIBUTE) {
pm=PM_LINE;
}
}
if(pm==PM_LINE) {
if(w<i-bol) w=i-bol;
h++;
}
// Now we should have the map size.
//printf("Loading a %dx%d map...\n",w,h);
construct_level(w,h);
// Now create the path sections
x=0;
y=0;
pm=PM_BOL;
bol=0;
for(i=0;i<len;i++) {
if(data[i]=='"') {
pm=PM_COMMENT;
} else if(data[i]=='\r' || data[i]=='\n') {
if(pm==PM_ATTRIBUTE) {
// Should add it to the attributes list.
} else if(pm==PM_LINE) {
y++;
x=0;
}
pm=PM_BOL;
bol=i+1;
} else if(pm==PM_COMMENT) {
// Skip comment
} else if(pm==PM_ATTRIBUTE) {
// Collect attribute at the end
} else if(data[i]=='=') {
attr=i+1;
pm=PM_ATTRIBUTE;
x=0;
} else if(pm!=PM_ATTRIBUTE) {
int id=mapCharToTile[data[i]];
struct TileProp *prop=NULL;
if(id>=0) prop=mapTileToProp[id];
if(!prop && data[i]!=' ') printf("Error loading cell %d,%d: %c\n",x,y,data[i]);
pm=PM_LINE;
if(prop && game.map[x+w*y]==-1) {
if(prop->flags&TP_MAP_MODULUS) {
int tilex=(x%prop->gfxw);
int tiley=(y%prop->gfxh);
int value;
if(prop->gfxh==1) tilex=(x+y)%prop->gfxw; // Special case
if(prop->gfxw==1) tiley=(x+y)%prop->gfxh; // Special case
value=prop->id+tilex+TILESACROSS*tiley;
//printf("(prop-tileProp)=%d: Tiling @ %d,%d, %d + picking offset +%d+%d ==> %d\n",(prop-tileProp),x,y,prop->id,tilex,tiley,value);
game.map[x+w*y]=value;
} else if(prop->flags&TP_NPC && editMode==0) {
int type=T_CHICK;
switch(prop->id) {
case M_CHICKRIGHT1:
type=T_CHICK;
break;
case M_SHOOTERRIGHT1:
type=T_SHOOTER;
break;
case M_BIRDRIGHT1:
type=T_BIRD;
break;
case M_WALLCLIMBERLEFT:
case M_WALLCLIMBERRIGHT:
type=T_BUG;
break;
case M_EVILDUCKLEFT1:
type=T_EVILDUCK;
break;
case M_TRAMPOLINE1:
case M_TRAMPOLINE2:
type=T_TRAMPOLINE;
break;
}
if(test_npc(x*TILEWIDTH,y*TILEHEIGHT)==-1) {
add_npc(type,x*TILEWIDTH+mapTileToProp[id]->offsetx,
y*TILEHEIGHT+mapTileToProp[id]->offsety,D_LEFT,id);
}
} else if(prop == tileProp && editMode==0) {
// We've found the start point for the level.
if(game.checkpointX==0 && game.checkpointY==0) {
game.checkpointX=x;
game.checkpointY=y;
//printf("Set start point to: %d,%d (\"%c\")\n",game.checkpointX,game.checkpointY,data[i]);
}
} else { // Fill in the tiles for the whole object.
for(j=0;j<prop->gfxh;j++) {
for(k=0;k<prop->gfxw;k++) {
game.map[x+k+(y+j)*w]=id+k+TILESACROSS*j;
}
}
switch(prop->id) {
case M_REDTELEPORTER: add_teleporter(3,x,y); break;
case M_GREENTELEPORTER: add_teleporter(2,x,y); break;
case M_BLUETELEPORTER: add_teleporter(1,x,y); break;
case M_YELLOWTELEPORTER: add_teleporter(0,x,y); break;
}
}
if(prop->id==M_FRUIT1) {
game.available++;
}
}
x++;
}
}
game.player.health=100;
game.player.invincible=0;
game.player.x=game.checkpointX*TILEWIDTH+game.player.prop->offsetx;
game.player.x_residue=PRECISION/2;
game.player.y=game.checkpointY*TILEHEIGHT+game.player.prop->offsety;
game.player.y_residue=PRECISION/2;
game.player.x_velocity=0;
game.player.y_velocity=0;
game.player.gravity=HIGHGRAVITY;
game.player.facing=D_RIGHT;
game.invalidate=2;
// if(((game.level/3)%2)==0) play_sound(S_RUSH);
// else play_sound(S_ALONE);
#ifdef _DEBUG
for(j=0;j<h;j++) {
for(i=0;i<w;i++) {
int cell=game.map[i+j*w];
if(cell<0) printf("?");
else printf("%c",*mapTileToProp[cell]);
}
printf("\n");
}
#endif
}
void save_level(const char *fname)
{
int x,y;
FILE *file;
int blank=0;
file=fopen(fname,"w");
for(y=0;y<game.h;y++) {
for(x=0;x<game.w;x++) {
if(game.map[x+y*game.w]==-1) {
fprintf(file," ");
blank++;
} else {
int tile=game.map[x+y*game.w];
//fprintf(file,"t=%d;p=%d;",tile,prop);
fprintf(file,"%c",mapTileToProp[tile]->ch);
}
}
fprintf(file,"\n");
}
fclose(file);
printf("%d of %d tiles are sky.\n",blank,game.w*game.h);
}
void draw_map()
{
int x,y,id,i;
struct TileProp *prop;
if(game.map==0) return;
//printf("Drawing at: %d,%d @+%d+%d\n",game.w,game.h,game.l,game.t);
for(y=0;y<=SCREENHEIGHT;y++) {
for(x=0;x<=SCREENWIDTH;x++) {
int pos=game.l/TILEWIDTH+x+(game.t/TILEHEIGHT+y)*game.w;
if(pos<0 || pos>=game.w*game.h) pos=0;
draw_tile(game.map[pos],
x*TILEWIDTH-(game.l%TILEWIDTH),
y*TILEHEIGHT-(game.t%TILEHEIGHT));
}
}
if(game.invalidate>0) game.invalidate--;
prop=game.player.prop;
//printf("Hero prop=%d; width=%d; frame=%d; ",prop-tileProp,prop->.w,game.player.frame);
id=prop->id+prop->gfxw*game.player.frame;
if(game.player.facing==D_LEFT && (prop->flags&TP_MIRROR_BELOW)) {
id+=prop->gfxh*TILESACROSS;
}
if(game.player.facing==D_LEFT && (prop->flags&TP_MIRROR_BESIDE)) {
id+=prop->gfxw*prop->frames;
}
//printf("Hero id=%d\n",id);
if( (game.player.invincible % 20) < 10) {
draw_tile_rect(id,game.player.x-game.player.prop->offsetx-game.l,game.player.y-game.player.prop->offsety-game.t,prop->gfxw,prop->gfxh);
}
if( game.player.invincible > 0) game.player.invincible --;
// Now draw all the npcs:
for(i=0;i<game.npcCount;i++) {
prop=game.npc[i].prop;
//printf("Hero prop=%d; width=%d; frame=%d; ",prop-tileProp,prop->w,game.player.frame);
id=prop->id;
if(prop->flags & TP_MAP_MODULUS) {
id+=(game.npc[i].frame%prop->gfxw) +
(game.npc[i].frame/prop->gfxw)*TILESACROSS;
} else {
id+=prop->gfxw*game.npc[i].frame;
}
if(game.npc[i].facing==D_LEFT && (prop->flags&TP_MIRROR_BELOW)) {
id+=prop->gfxh*TILESACROSS;
}
if(game.npc[i].facing==D_LEFT && (prop->flags&TP_MIRROR_BESIDE)) {
id+=prop->gfxw*prop->frames;
}
if(prop->flags & TP_MAP_MODULUS) {
draw_tile_rect(id,game.npc[i].x-game.npc[i].prop->offsetx-game.l,game.npc[i].y-game.npc[i].prop->offsety-game.t,1,1);
} else {
draw_tile_rect(id,game.npc[i].x-game.npc[i].prop->offsetx-game.l,game.npc[i].y-game.npc[i].prop->offsety-game.t,prop->gfxw,prop->gfxh);
}
}
#if 0
// Now decode the number into decimal.
int num;
num=game.level;
draw_number((num/10)%10,TILEWIDTH*3,0);
draw_number(num%10,TILEWIDTH*4,0);
num=game.score;
draw_number((num/1000)%10,TILEWIDTH*8,0);
draw_number((num/100)%10,TILEWIDTH*9,0);
draw_number((num/10)%10,TILEWIDTH*10,0);
draw_number(num%10,TILEWIDTH*11,0);
num=game.lives;
if(num<0) num=0;
draw_number(num%10,TILEWIDTH*18,0);
if(game.lives<1) {
for(i=0;i<5;i++) {
// Game Over