-
Notifications
You must be signed in to change notification settings - Fork 0
/
WARLOCK.C
1966 lines (1222 loc) · 47.8 KB
/
WARLOCK.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
// I N C L U D E S ///////////////////////////////////////////////////////////
#include <io.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <bios.h>
#include <fcntl.h>
#include <memory.h>
#include <malloc.h>
#include <math.h>
#include <string.h>
#include <graph.h>
#include "graphics.h" // load our graphics library
#include "sndlib.h" // load our sound library
// P R O T O T Y P E S //////////////////////////////////////////////////////
void Blit_Char_D(int xc,int yc,char c,int color);
void Blit_String_D(int x,int y,int color, char *string);
void _interrupt _far New_Key_Int(void);
void Melt(void);
void Render_Sliver(sprite_ptr sprite,long scale, int column);
void Timer(int clicks);
void Create_Scale_Data(int scale, int *row);
void Build_Tables(void);
void Allocate_World(void);
int Load_World(char *file);
void Demo_Setup(void);
void Ray_Caster(long x,long y,long view_angle);
void Wait_For_Vsync(void);
void Draw_Ground(void);
int Get_Input(void);
void Destroy_Door(int x_cell, int y_cell, int command);
// A S S E M B L Y E X T E R N S ///////////////////////////////////////////
extern far Draw_Ground_32(); // renders the sky and ground
extern far Render_Buffer_32(); // copies the double buffer to the video buffer
extern far Render_Sliver_32(); // draws a textured "sliver"
// T Y P E S ////////////////////////////////////////////////////////////////
typedef long fixed; // fixed point is 32 bits
// S T R U C T U R E S //////////////////////////////////////////////////////
// this structure is used for the worm or melt effect
typedef struct worm_typ
{
int y; // current y position of worm
int color; // color of worm
int speed; // speed of worm
int counter; // counts time until movement
} worm, *worm_ptr;
// D E F I N E S /////////////////////////////////////////////////////////////
// #define MAKING_DEMO 1 // this flag is used to turn on the demo record
// option. this is for developers only
// keyboard stuff
#define KEYBOARD_INT 0x09 // the keyboard interrupt vector
#define KEY_BUFFER 0x60 // keyboard buffer area
#define KEY_CONTROL 0x61 // keyboard control register
#define INT_CONTROL 0x20 // interrupt control register
// make and break codes for the arrow keys
#define MAKE_RIGHT 77
#define MAKE_LEFT 75
#define MAKE_UP 72
#define MAKE_DOWN 80
#define BREAK_RIGHT 205
#define BREAK_LEFT 203
#define BREAK_UP 200
#define BREAK_DOWN 208
// indices into arrow key state table
#define INDEX_UP 0
#define INDEX_DOWN 1
#define INDEX_RIGHT 2
#define INDEX_LEFT 3
// these dataums are used as the records for the demo data
#define DEMO_RIGHT 1
#define DEMO_LEFT 2
#define DEMO_UP 4
#define DEMO_DOWN 8
// these are for the door system
#define DOOR_DEAD 0 // the door is gone
#define DOOR_DYING 1 // the door is phasing
#define PROCESS_DOOR_DESTROY 0 // tells the door engine to continue processing
#define START_DOOR_DESTROY 1 // telsl the door engine to begin
#define MAX_LENGTH_DEMO 2048 // maximum length a demo sequence can be
#define END_OF_DEMO 255 // used in the demo file to flag EOF
#define VGA_INPUT_STATUS_1 0x3DA // vga status reg 1, bit 3 is the vsync
// when 1 - retrace in progress
// when 0 - no retrace
#define VGA_VSYNC_MASK 0x08 // masks off unwanted bits of status reg
// #define DEBUG 1 // used to toggle debugging
#define OVERBOARD 52 // the closest a player can get to a wall
#define INTERSECTION_FOUND 1 // used by ray caster to flag an intersection
#define MAX_SCALE 201 // maximum size and wall "sliver" can be
#define WINDOW_HEIGHT 152 // height of the game view window
#define WINDOW_MIDDLE 76 // the center or horizon of the view window
#define VERTICAL_SCALE 13312 // used to scale the "slivers" to get proper
// perspective and aspect ratio
// these are for the sound FX's
#define NUM_SOUNDS 5 // maximum number of loaded sounds
#define BACK_SOUNDS 4 // larget index to backgrounbd sound
#define SOUND_LAUGH 0
#define SOUND_MOAN 1
#define SOUND_WIND 2
#define SOUND_GROWL 3
#define SOUND_DOOR 4
// constants used to represent angles for the ray caster
#define ANGLE_0 0
#define ANGLE_1 5
#define ANGLE_2 10
#define ANGLE_4 20
#define ANGLE_5 25
#define ANGLE_6 30
#define ANGLE_15 80
#define ANGLE_30 160
#define ANGLE_45 240
#define ANGLE_60 320
#define ANGLE_90 480
#define ANGLE_135 720
#define ANGLE_180 960
#define ANGLE_225 1200
#define ANGLE_270 1440
#define ANGLE_315 1680
#define ANGLE_360 1920 // note: the circle has been broken up into 1920
// sub-arcs
#define STEP_LENGTH 15 // number of units player moves foward or backward
#define WORLD_ROWS 64 // number of rows in the game world
#define WORLD_COLUMNS 64 // number of columns in the game world
#define CELL_X_SIZE 64 // size of a cell in the gamw world
#define CELL_Y_SIZE 64
#define CELL_X_SIZE_FP 6 // log base 2 of 64 (used for quick division)
#define CELL_Y_SIZE_FP 6
// size of overall game world
#define WORLD_X_SIZE (WORLD_COLUMNS * CELL_X_SIZE)
#define WORLD_Y_SIZE (WORLD_ROWS * CELL_Y_SIZE)
// G L O B A L S /////////////////////////////////////////////////////////////
void (_interrupt _far *Old_Key_Isr)(); // holds old keyboard interrupt handler
unsigned int far *clock = (unsigned int far *)0x0000046CL; // pointer to internal
// 18.2 clicks/sec
// world map of nxn cells, each cell is 64x64 pixels
char far *world[WORLD_ROWS]; // pointer to matrix of cells that make up
// world
float far *tan_table; // tangent tables used to compute initial
float far *inv_tan_table; // intersections with ray
float far *y_step; // x and y steps, used to find intersections
float far *x_step; // after initial one is found
float far *cos_table; // used to cacell out fishbowl effect
float far *inv_cos_table; // used to compute distances by calculating
float far *inv_sin_table; // the hypontenuse
int *scale_table[MAX_SCALE+1]; // table with pre-computed scale indices
worm worms[320]; // used to make the screen melt
sprite object; // general sprite object used by everyone
pcx_picture walls_pcx, // holds the wall textures
controls_pcx, // holds the control panel at bottom of screen
intro_pcx; // holds the intro screen
int demo_mode=1; // toogles demo mode on and off. Note: this
// must be 0 to record a demo
// parmeter block used by assembly language sliver engine
char far *sliver_texture; // pointer to texture being rendered
int sliver_column; // index into texture i.e. which column of texture
int sliver_top; // starting Y position to render at
int sliver_scale; // overall height of sliver
int sliver_ray; // current ray being cast
int sliver_clip; // index into texture after clipping
int *scale_row; // row of scale value look up table to use
// keyboard stuff
int raw_key; // the global raw keyboard data aquired from the ISR
int key_table[4] = {0,0,0,0}; // the key state table for the motion keys
// the player
int player_x, // the players X position
player_y, // the players Y position
player_view_angle; // the current view angle of the player
unsigned char far *demo; // table of data for demo mode
// if the code gets enabled it allocates various data to create a demo file
#if MAKING_DEMO
unsigned char demo_out[MAX_LENGTH_DEMO]; // digitized output file
unsigned char demo_word=0; // packed demo packet
int demo_out_index=0; // number of motions in file
FILE *fp, *fopen(); // general file stuff
#endif
// used for color FX
RGB_color red_glow; // red glowing objects
int red_glow_index = 254; // index of color register to glow
// variables to track status of a door
int door_state = DOOR_DEAD; // state of door
int door_clock = 0; // global door clock, counts
// number of frames to do door
// animation
// sound system stuff
char far *sounds[NUM_SOUNDS]; // pointers to sound files
unsigned char sound_lengths[NUM_SOUNDS]; // length of each sound
// F U N C T I O N S /////////////////////////////////////////////////////////
void Blit_Char_D(int xc,int yc,char c,int color)
{
// this function uses the rom 8x8 character set to blit a character to the
// double buffer, also it blits the character in two colors
int offset,x,y;
char far *work_char;
unsigned char bit_mask = 0x80;
// compute starting offset in rom character lookup table
work_char = (char far *)(rom_char_set + c * CHAR_HEIGHT);
// compute offset of character in video buffer
offset = (yc << 8) + (yc << 6) + xc;
for (y=0; y<CHAR_HEIGHT; y++)
{
// reset bit mask
bit_mask = 0x80;
// test if it's time to change colors
if (y==(CHAR_HEIGHT/2))
color-=8; // change to lower intensity
for (x=0; x<CHAR_WIDTH; x++)
{
// test for transparent pixel i.e. 0, if not transparent then draw
if ((*work_char & bit_mask))
double_buffer[offset+x] = (char)color;
// shift bit mask
bit_mask = (bit_mask>>1);
} // end for x
// move to next line in video buffer and in rom character data area
offset += SCREEN_WIDTH;
work_char++;
} // end for y
} // end Blit_Char_D
//////////////////////////////////////////////////////////////////////////////
void Blit_String_D(int x,int y,int color, char *string)
{
// this function blits an entire string to the double buffer
// It calls blit_char_d
int index;
for (index=0; string[index]!=0; index++)
{
Blit_Char_D(x+(index<<3),y,string[index],color);
} /* end while */
} /* end Blit_String_D */
//////////////////////////////////////////////////////////////////////////////
void _interrupt _far New_Key_Int(void)
{
// this function links into the keyboard interrupt and takes over. it is called
// when a key is pressed. Note: how it differs from the one were saw in the
// chapter on I/O. It has been modified to take into consideration the demo
// mode of the system
_asm
{
sti ; re-enable interrupts
in al, KEY_BUFFER ; get the key that was pressed
xor ah,ah ; zero out upper 8 bits of AX
mov raw_key, ax ; store the key in global
in al, KEY_CONTROL ; set the control register
or al, 82h ; set the proper bits to reset the FF
out KEY_CONTROL,al ; send the new data back to the control register
and al,7fh
out KEY_CONTROL,al ; complete the reset
mov al,20h
out INT_CONTROL,al ; re-enable interrupts
; when this baby hits 88 mph, your gonna see
; some serious @#@#$%
} // end inline assembly
// now for some C to update the arrow state table
// process the key and update the table (only if not in demo mode)
if (!demo_mode)
{
switch(raw_key)
{
case MAKE_UP: // pressing up
{
key_table[INDEX_UP] = 1;
} break;
case MAKE_DOWN: // pressing down
{
key_table[INDEX_DOWN] = 1;
} break;
case MAKE_RIGHT: // pressing right
{
key_table[INDEX_RIGHT] = 1;
} break;
case MAKE_LEFT: // pressing left
{
key_table[INDEX_LEFT] = 1;
} break;
case BREAK_UP: // releasing up
{
key_table[INDEX_UP] = 0;
} break;
case BREAK_DOWN: // releasing down
{
key_table[INDEX_DOWN] = 0;
} break;
case BREAK_RIGHT: // releasing right
{
key_table[INDEX_RIGHT] = 0;
} break;
case BREAK_LEFT: // releasing left
{
key_table[INDEX_LEFT] = 0;
} break;
default: break;
} // end switch
} // end if in demo mode
} // end New_Key_Int
//////////////////////////////////////////////////////////////////////////////
void Melt(void)
{
// this function "melts" the screen by moving little worms at different speeds
// down the screen. These worms change to the color they are eating
int index,ticks=0;
// initialize the worms
for (index=0; index<160; index++)
{
worms[index].color = Get_Pixel(index,0);
worms[index].speed = 3 + rand()%9;
worms[index].y = 0;
worms[index].counter = 0;
// draw the worm
Plot_Pixel_Fast((index<<1),0,(char)worms[index].color);
Plot_Pixel_Fast((index<<1),1,(char)worms[index].color);
Plot_Pixel_Fast((index<<1),2,(char)worms[index].color);
Plot_Pixel_Fast((index<<1)+1,0,(char)worms[index].color);
Plot_Pixel_Fast((index<<1)+1,1,(char)worms[index].color);
Plot_Pixel_Fast((index<<1)+1,2,(char)worms[index].color);
} // end index
// do screen melt
while(++ticks<1800)
{
// process each worm
for (index=0; index<320; index++)
{
// is it time to move worm
if (++worms[index].counter == worms[index].speed)
{
// reset counter
worms[index].counter = 0;
worms[index].color = Get_Pixel(index,worms[index].y+4);
// has worm hit bottom?
if (worms[index].y < 193)
{
Plot_Pixel_Fast((index<<1),worms[index].y,0);
Plot_Pixel_Fast((index<<1),worms[index].y+1,(char)worms[index].color);
Plot_Pixel_Fast((index<<1),worms[index].y+2,(char)worms[index].color);
Plot_Pixel_Fast((index<<1),worms[index].y+3,(char)worms[index].color);
Plot_Pixel_Fast((index<<1)+1,worms[index].y,0);
Plot_Pixel_Fast((index<<1)+1,worms[index].y+1,(char)worms[index].color);
Plot_Pixel_Fast((index<<1)+1,worms[index].y+2,(char)worms[index].color);
Plot_Pixel_Fast((index<<1)+1,worms[index].y+3,(char)worms[index].color);
worms[index].y++;
} // end if worm isn't at bottom yet
} // end if time to move worm
} // end index
// accelerate melt
if (!(ticks % 500))
{
for (index=0; index<160; index++)
worms[index].speed--;
} // end if time to accelerate melt
Wait_For_Vsync();
} // end while
} // end Melt
///////////////////////////////////////////////////////////////////////////////
#if 0
void Render_Sliver(sprite_ptr sprite,long scale, int column)
{
// this function will scale a single sliver of texture data. it uses fixed point
// numbers.
char far *work_sprite;
int work_offset=0,offset,x,y,scale_int;
unsigned char data;
fixed scale_index,scale_step;
scale_int = scale;
scale = (scale<<8);
scale_index = 0;
scale_step = (fixed)(((fixed)64) << 16) / scale;
// alias a pointer to sprite for ease of access
work_sprite = sprite->frames[sprite->curr_frame];
// compute offset of sprite in video buffer
offset = (sprite->y << 8) + (sprite->y << 6) + sprite->x;
for (y=0; y<scale_int; y++)
{
double_buffer[offset] = work_sprite[work_offset+column];
scale_index+=scale_step;
offset += SCREEN_WIDTH;
work_offset = ((scale_index & 0xff00)>>2);
} // end for y
} // end Draw_Sliver
#endif
///////////////////////////////////////////////////////////////////////////////
#if 0
Render_Sliver(sprite_ptr sprite,int scale, int column)
{
// this is yet another version of the sliver scaler, however it uses look up
// tables with pre-computed scale indices. in the end I converted this to
// assembly for speed
char far *work_sprite;
int far *row;
int work_offset=0,offset,y,scale_off;
unsigned char data;
// alias proper data row
row = scale_table[scale];
if (scale>(WINDOW_HEIGHT-1))
{
scale_off = (scale-(WINDOW_HEIGHT-1)) >> 1;
scale=(WINDOW_HEIGHT-1);
sprite->y = 0;
}
// alias a pointer to sprite for ease of access
work_sprite = sprite->frames[sprite->curr_frame];
// compute offset of sprite in video buffer
offset = (sprite->y << 8) + (sprite->y << 6) + sprite->x;
for (y=0; y<scale; y++)
{
double_buffer[offset] = work_sprite[work_offset+column];
offset += SCREEN_WIDTH;
work_offset = row[y+scale_off];
} // end for y
} // end Draw_Sliver
#endif
///////////////////////////////////////////////////////////////////////////////
void Timer(int clicks)
{
// this function uses the internal time keeper timer i.e. the one that goes
// at 18.2 clicks/sec to to a time delay. You can find a 32 bit value of
// this timer at 0000:046Ch
unsigned int now;
// get current time
now = *clock;
// wait till time has gone past current time plus the amount we eanted to
// wait. Note each click is approx. 55 milliseconds.
while(abs(*clock - now) < clicks){}
} // end Timer
///////////////////////////////////////////////////////////////////////////////
void Create_Scale_Data(int scale, int *row)
{
// this function synthesizes the scaling of a texture sliver to all possible
// sizes and creates a huge look up table of the data.
int y;
float y_scale_index=0,
y_scale_step;
// compute scale step or number of source pixels to map to destination/cycle
y_scale_step = (float)64/(float)scale;
y_scale_index+=y_scale_step;
for (y=0; y<scale; y++)
{
// place data into proper array position for later use
row[y] = ((int)(y_scale_index+.5)) * CELL_X_SIZE;
// test if we slightly went overboard
if (row[y] > 63*CELL_X_SIZE) row[y] = 63*CELL_X_SIZE;
// next index please
y_scale_index+=y_scale_step;
} // end for y
} // end Create_Scale_Data
///////////////////////////////////////////////////////////////////////////////
void Build_Tables(void)
{
// this function builds all the look up tables for the system
int ang,scale;
float rad_angle;
// allocate memory for all look up tables
// tangent tables equivalent to slopes
tan_table = (float far *)_fmalloc(sizeof(float) * (ANGLE_360+1) );
inv_tan_table = (float far *)_fmalloc(sizeof(float) * (ANGLE_360+1) );
// step tables used to find next intersections, equivalent to slopes
// times width and height of cell
y_step = (float far *)_fmalloc(sizeof(float) * (ANGLE_360+1) );
x_step = (float far *)_fmalloc(sizeof(float) * (ANGLE_360+1) );
// cos table used to fix view distortion caused by caused by radial projection
cos_table = (float far *)_fmalloc(sizeof(float) * (ANGLE_360+1) );
// 1/cos and 1/sin tables used to compute distance of intersection very
// quickly
inv_cos_table = (float far *)_fmalloc(sizeof(float) * (ANGLE_360+1) );
inv_sin_table = (float far *)_fmalloc(sizeof(float) * (ANGLE_360+1) );
// create the lookup tables for the scaler
// there have the form of an array of pointers, where each pointer points
// another another array of data where the 'data' are the scale indices
for (scale=0; scale<=MAX_SCALE; scale++)
{
scale_table[scale] = (int *)malloc(scale*sizeof(int)+1);
} // end for scale
// create tables, sit back for a sec!
for (ang=ANGLE_0; ang<=ANGLE_360; ang++)
{
rad_angle = (float)((3.272e-4) + ang * 2*3.141592654/ANGLE_360);
tan_table[ang] = (float)tan(rad_angle);
inv_tan_table[ang] = (float)(1/tan_table[ang]);
// tangent has the incorrect signs in all quadrants except 1, so
// manually fix the signs of each quadrant since the tangent is
// equivalent to the slope of a line and if the tangent is wrong
// then the ray that is case will be wrong
if (ang>=ANGLE_0 && ang<ANGLE_180)
{
y_step[ang] = (float)(fabs(tan_table[ang] * CELL_Y_SIZE));
}
else
y_step[ang] = (float)(-fabs(tan_table[ang] * CELL_Y_SIZE));
if (ang>=ANGLE_90 && ang<ANGLE_270)
{
x_step[ang] = (float)(-fabs(inv_tan_table[ang] * CELL_X_SIZE));
}
else
{
x_step[ang] = (float)(fabs(inv_tan_table[ang] * CELL_X_SIZE));
}
// create the sin and cosine tables to copute distances
inv_cos_table[ang] = (float)(1/cos(rad_angle));
inv_sin_table[ang] = (float)(1/sin(rad_angle));
} // end for ang
// create view filter table. There is a cosine wave modulated on top of
// the view distance as a side effect of casting from a fixed point.
// to cancell this effect out, we multiple by the inverse of the cosine
// and the result is the proper scale. Without this we would see a
// fishbowl effect, which might be desired in some cases?
for (ang=-ANGLE_30; ang<=ANGLE_30; ang++)
{
rad_angle = (float)((3.272e-4) + ang * 2*3.141592654/ANGLE_360);
cos_table[ang+ANGLE_30] = (float)(VERTICAL_SCALE/cos(rad_angle));
} // end for
// build the scaler table. This table holds MAX_SCALE different arrays. Each
// array consists of the pre-computed indices for an object to be scaled
for (scale=1; scale<=MAX_SCALE; scale++)
{
// create the indices for this scale
Create_Scale_Data(scale, (int *)scale_table[scale]);
} // end for scale
} // end Build_Tables
/////////////////////////////////////////////////////////////////////////////
void Allocate_World(void)
{
// this function allocates the memory for the world
int index;
// allocate each row
for (index=0; index<WORLD_ROWS; index++)
{
world[index] = (char far *)_fmalloc(WORLD_COLUMNS+1);
} // end for index
} // end Allocate_World
////////////////////////////////////////////////////////////////////////////////
int Load_World(char *file)
{
// this function opens the input file and loads the world data from it
FILE *fp, *fopen();
int row,column;
char ch;
// open the file
if (!(fp = fopen(file,"r")))
return(0);
// load in the data
for (row=0; row<WORLD_ROWS; row++)
{
// load in the next row
for (column=0; column<WORLD_COLUMNS; column++)
{
while((ch = getc(fp))==10){} // filter out CR
// translate character to integer
if (ch == ' ')
ch=0;
else
ch = ch - '0';
// insert data into world
world[(WORLD_ROWS-1) - row][column] = ch;
} // end for column
// process the row
} // end for row
// close the file
fclose(fp);
return(1);
} // end Load_World
////////////////////////////////////////////////////////////////////////////////
void Demo_Setup(void)
{
// this function allocates the demo mode storage area and loads the demo mode
// data
FILE *fp_demo, *fopen();
int index=0;
unsigned char data;
// allocate storage for demo mode
demo = (unsigned char far*)_fmalloc(MAX_LENGTH_DEMO);
// open up demo file
fp_demo = fopen("demo.dat","rb");
// load data
while((data=getc(fp_demo))!=END_OF_DEMO)
{
demo[index++] = data;
} // end while
// place end of demo flag in data
demo[index] = END_OF_DEMO;
// close file
fclose(fp_demo);
} // end Demo_Setup
/////////////////////////////////////////////////////////////////////////////
void Ray_Caster(long x,long y,long view_angle)
{
// This is the heart of the system. it casts out 320 rays and builds the
// 3-D image from their intersections with the walls. It was derived from
// the previous version used in "RAY.C", however, it has been extremely
// optimized for speed by the use of many more lookup tables and fixed
// point math
int
cell_x, // the current cell that the ray is in
cell_y,
ray, // the current ray being cast 0-320
casting=2, // tracks the progress of the X and Y component of the ray
x_hit_type, // records the block that was intersected, used to figure
y_hit_type, // out which texture to use
x_bound, // the next vertical and horizontal intersection point
y_bound,
next_y_cell, // used to figure out the quadrant of the ray
next_x_cell,
xray=0, // tracks the progress of a ray looking for Y interesctions
yray=0, // tracks the progress of a ray looking for X interesctions
x_delta, // the amount needed to move to get to the next cell
y_delta, // position
xb_save,
yb_save,
xi_save, // used to save exact x and y intersection points
yi_save,
scale;
long
cast=0,
dist_x, // the distance of the x and y ray intersections from
dist_y; // the viewpoint
float xi, // used to track the x and y intersections
yi;
// S E C T I O N 1 /////////////////////////////////////////////////////////v
// initialization
// compute starting angle from player. Field of view is 60 degrees, so
// subtract half of that current view angle
if ( (view_angle-=ANGLE_30) < 0)
{
view_angle=ANGLE_360 + view_angle;
} // end if
// loop through all 320 rays
for (ray=319; ray>=0; ray--)
{
// S E C T I O N 2 /////////////////////////////////////////////////////////
// compute first x intersection
// need to know which half plane we are casting from relative to Y axis
if (view_angle >= ANGLE_0 && view_angle < ANGLE_180)
{
// compute first horizontal line that could be intersected with ray