-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathobjects.c
788 lines (641 loc) · 14.4 KB
/
objects.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
/* :ts=8 bk=0
*
* objects.c: Object handling.
*
* Leo L. Schwab 9305.25
*/
#include <types.h>
#include <mem.h>
#include <operamath.h>
#include <graphics.h>
#include "castle.h"
#include "objects.h"
#include "anim.h"
#include "imgfile.h"
#include "sound.h"
#include "app_proto.h"
/***************************************************************************
* Globals.
*/
extern MapEntry levelmap[WORLDSIZ][WORLDSIZ];
extern CCB ccbpool[];
extern CCB *curccb;
extern Vertex unitsquare[], xformsquare[];
extern Vector plusx, plusz;
extern Vertex obverts[], xfobverts[], *curobv;
extern int32 nobverts;
extern Vertex playerpos;
extern int32 playerhealth;
extern int32 nkeys;
extern frac16 damagefade;
extern uint32 linebuf[];
extern uint32 ccbextra;
extern int32 cy;
Object **obtab;
int32 obtabsiz;
/***************************************************************************
* Forward references (see end of file).
*/
extern ObDef *obdefs[];
extern int32 nobdefs;
/***************************************************************************
* Code, mon.
*/
#if 0
/* I don't appear to use this anymore... */
void
generateobjects (numobs)
int32 numobs;
{
register MapEntry *me;
register ObDef *od;
register Object *ob, **obt;
register int x, z;
InitData id;
if (!(obtab = malloc (sizeof (Object *) * numobs)))
die ("Can't allocate object table.\n");
obtabsiz = numobs;
obt = obtab;
for (z = WORLDSIZ; --z >= 0; ) {
for (x = WORLDSIZ; --x >= 0; ) {
me = &levelmap[z][x];
/*
* MapEntries initially contain a pointer to the
* ObDef. This is used to generate an instance.
*/
if (!(od = (ObDef *) me->me_Obs))
continue;
id.id_MapEntry = me;
id.id_XIdx = x;
id.id_ZIdx = z;
if (!(ob = (Object *) ((od->od_Func) (NULL,
OP_CREATEOB,
NULL))))
die ("Can't create object.\n");
if ((od->od_Func) (ob, OP_INITOB, &id) < 0)
die ("Error initializing object.\n");
me->me_Obs = ob; // Stomp over ObDef
me->me_Flags |= MEF_ARTWORK; // Do I need this?
*obt++ = ob;
}
}
}
#endif
void
freeobjects ()
{
register ObDef *od;
register Object *ob, **obt;
register int i;
if (obtab) {
obt = obtab;
for (i = obtabsiz; --i >= 0; ) {
if (ob = *obt++) {
od = ob->ob_Def;
if ((od->od_Func) (ob, OP_DELETEOB, NULL)< 0)
die ("Failed to delete object.\n");
}
}
freetype (obtab); obtab = NULL;
}
}
struct Object *
createStdObject (od, type, size, flags, dupsrc)
struct ObDef *od;
int type, size, flags;
struct Object *dupsrc;
{
register Object *ob;
if (ob = malloctype (size, MEMTYPE_FILL)) {
od->od_ObCount++;
if (dupsrc && dupsrc->ob_Type == type) {
/* Make a copy of supplied object. */
*ob = *dupsrc;
ob->ob_Next = NULL;
} else {
ob->ob_Def = od;
ob->ob_Type = type;
ob->ob_State = OBS_INVALID;
ob->ob_Flags = flags;
}
}
return (ob);
}
void
deleteStdObject (ob)
register struct Object *ob;
{
ob->ob_Def->od_ObCount--;
freetype (ob);
}
void
initobdefs ()
{
register ObDef *od;
register int i;
for (i = nobdefs; --i >= 0; ) {
od = obdefs[i];
if (od->od_ObCount) {
/*
* od_ObCount was set up by the level loader to
* inform us whether or not we need the object.
* Clear it and initialize the object.
*/
od->od_ObCount = 0;
(od->od_Func) (NULL, OP_FIATLUX, NULL);
}
}
}
void
destructobdefs ()
{
register ObDef *od;
register int i;
for (i = nobdefs; --i >= 0; ) {
od = obdefs[i];
(od->od_Func) (NULL, OP_DESTRUCT, NULL);
}
}
void
registerobs (vo)
struct VisOb *vo;
{
register Object *ob, *next;
register int32 (*func)();
register int retval;
ob = vo->vo_ME->me_Obs;
while (ob) {
next = ob->ob_Next;
if (ob->ob_State && (ob->ob_Flags & OBF_REGISTER)) {
if (func = ob->ob_Def->od_Func)
retval = func (ob, OP_REGISTER, vo);
}
ob = next;
}
}
void
renderobs (vo)
struct VisOb *vo;
{
register Object *ob, *next;
register int32 (*func)();
register int retval;
ob = vo->vo_ME->me_Obs;
while (ob) {
next = ob->ob_Next;
if (ob->ob_State && (ob->ob_Flags & OBF_RENDER)) {
if (func = ob->ob_Def->od_Func)
retval = func (ob, OP_RENDER, vo);
}
ob = next;
}
}
void
moveobjects (nframes)
int nframes;
{
register Object *ob, **obt;
register int i;
int32 (*func)();
obt = obtab;
for (i = obtabsiz; --i >= 0; ) {
ob = *obt++;
if (ob && ob->ob_State && (ob->ob_Flags & OBF_MOVE)) {
if (func = ob->ob_Def->od_Func)
func (ob, OP_MOVE, nframes);
}
}
}
void
removeobfromme (ob, me)
struct Object *ob;
struct MapEntry *me;
{
register Object *tstob;
tstob = me->me_Obs;
if (tstob == ob) {
me->me_Obs = ob->ob_Next;
return;
}
while (tstob) {
if (tstob->ob_Next == ob) {
tstob->ob_Next = ob->ob_Next;
return;
}
tstob = tstob->ob_Next;
}
}
void
addobtome (ob, me)
struct Object *ob;
struct MapEntry *me;
{
/*
* Simple insertion at the head. Sorting has to be done at
* projection.
*/
ob->ob_Next = me->me_Obs;
me->me_Obs = ob;
}
void
removeobfromslot (ob)
register struct Object *ob;
{
register int i;
register Object **obt;
for (i = obtabsiz, obt = obtab; --i >= 0; obt++) {
if (*obt == ob) {
*obt = NULL;
break;
}
}
}
void
takedamage (hitpoints)
int32 hitpoints;
{
if ((playerhealth -= hitpoints) < 0)
/* Should I play death sound here or out in gamemain()? */
playerhealth = 0;
if (hitpoints) {
if (hitpoints <= 3)
playsound (SFX_OUCHSMALL);
else if (hitpoints <= 10)
playsound (SFX_OUCHMED);
else
playsound (SFX_OUCHBIG);
if ((damagefade += hitpoints << 11) > 0xC000)
damagefade = 0xC000;
}
}
/***************************************************************************
* Compute approximation to 2D and 3D euclidian distance.
* Extracted from Graphics Gems volume 1.
* (Am I actually going to use these?)
*/
frac16
approx2dist (x1, y1, x2, y2)
register frac16 x1, y1, x2, y2;
{
if ((x2 -= x1) < 0) x2 = -x2;
if ((y2 -= y1) < 0) y2 = -y2;
if (x2 > y2)
return (x2 + y2 - (y2 >> 1));
else
return (x2 + y2 - (x2 >> 1));
}
frac16
approx3dist (x1, y1, z1, x2, y2, z2)
register frac16 x1, y1, z1, x2, y2, z2;
{
/*
* Compute abs([xyz]2 - [xyz]1);
*/
if ((x1 -= x2) < 0) x1 = -x1;
if ((y1 -= y2) < 0) y1 = -y1;
if ((z1 -= z2) < 0) z1 = -z1;
/*
* Sort x1, y1, z1 into descending order.
*/
if (x1 < y1) {
x1 ^= y1; y1 ^= x1; x1 ^= y1; /* Swap */
}
if (x1 < z1) {
x1 ^= z1; z1 ^= x1; x1 ^= z1; /* Swap */
}
/*
* Compute approximation: d == max + 5/16 * med + 1/4 * min;
*/
y1 = (y1 + (y1 << 2)) >> 4; /* y1 * 5 / 16 */
z1 >>= 2;
return (x1 + y1 + z1);
}
/***************************************************************************
* Animation cycling tools.
*/
int32
nextanimframe (ad, fnum, nvbls)
register struct AnimDef *ad;
int32 fnum, nvbls;
{
while (--nvbls >= 0) {
if ((ad->ad_Counter -= ad->ad_FPS) <= 0) {
ad->ad_Counter += VBLANKFREQ;
fnum++;
}
}
return (fnum);
}
/***************************************************************************
* Object Processors.
***************************************************************************
* Each routine has knowledge of how many verticies it uses and how to use
* them.
*/
int
setupanimarray (
struct CelArray **captr,
int *animseq,
struct CCB **ccbp
)
{
CelArray *ca;
int i,count;
for (count = 0; animseq[count] >= 0; count++)
;
if (count) {
// printf("* count = %d\n",count);
if (!(ca = alloccelarray (count)))
die ("Can't allocate monster's celarray.\n");
// printf("Got here\n");
*captr = ca;
ca->ncels = count;
ca->ca_Buffer = NULL;
ca->ca_Type = 0;
for (i = 0; i < count; i++ ) {
ca->celptrs[i] = ccbp[animseq[i]];
}
} else
*captr++ = NULL;
}
/***************************************************************************
* Simple door.
*/
#define SWINGANGSTEP (ONE_F16 * 2)
#define MAXSWINGANG ((64 - 5) * ONE_F16)
#define DOORWIDE HALF_F16
#define PROBEDISTQ (25 * ONE_F16 / 4) /* 6.25 */
#define WALLIMGBASE 54 /* After the walls. */
#define DOORVERTS 16
typedef struct DefNSDoor {
struct ObDef od;
struct ImageEntry *dd_IE; // Imagery for door (?).
ubyte dd_SpareFlags[6];
ubyte dd_RegIdxs[6];
ubyte dd_ImgIdxs[6];
} DefNSDoor;
typedef struct ObNSDoor {
struct Object ob;
frac16 od_SwingAng;
struct MapEntry *od_ME;
ubyte od_XIdx,
od_ZIdx;
ubyte od_Unlocked;
} ObNSDoor;
static int32 simpleNSdoor (struct ObNSDoor *, int, void *);
struct DefNSDoor def_NSDoor = {
{ simpleNSdoor,
OTYP_NSDOOR,
0,
0,
0,
0,
NULL
},
NULL,
{ VISF_WEST, VISF_WEST, VISF_EAST, VISF_EAST, 0, 0 },
{ 0, 3, 6, 2, 5, 8 },
{ 1, 3, 1, 3, 4, 6 }
};
struct DefNSDoor def_EWDoor = {
{ simpleNSdoor,
OTYP_EWDOOR,
0,
0,
0,
0,
NULL
},
NULL,
{ VISF_NORTH, VISF_NORTH, VISF_SOUTH, VISF_SOUTH, 0, 0 },
{ 6, 7, 8, 0, 1, 2 },
{ 0, 2, 0, 2, 5, 7 }
};
static int32
simpleNSdoor (od, op, dat)
register struct ObNSDoor *od;
int op;
void *dat;
{
static ubyte rendidxs[6][2] = {
{ 0, 1 },
{ 1, 2 },
{ 5, 4 },
{ 4, 3 },
{ 1, 6 },
{ 7, 4 },
};
switch (op) {
case OP_FIATLUX:
/* Remains to be defined. */
/* Load separate art files? */
case OP_DESTRUCT:
/* TBD */
break;
case OP_CREATEOB:
{ register ObDef *def;
def = (ObDef *) od;
return ((int32) createStdObject (def,
def->od_Type,
sizeof (ObNSDoor),
0,
dat));
}
case OP_DELETEOB:
deleteStdObject ((Object *) od);
break;
case OP_INITOB:
{
register InitData *id;
id = dat;
od->ob.ob_State = OBS_CLOSED;
od->ob.ob_Flags = OBF_MOVE | OBF_REGISTER | OBF_RENDER |
OBF_PROBE;
od->od_ME = id->id_MapEntry;
od->od_XIdx = id->id_XIdx;
od->od_ZIdx = id->id_ZIdx;
od->od_SwingAng = 0;
od->od_Unlocked = FALSE; // ### EXPAND THIS OUT!!
break;
}
case OP_MOVE:
{
register int nframes;
nframes = (int) dat;
if (od->ob.ob_State == OBS_OPENING) {
od->od_SwingAng += SWINGANGSTEP * nframes;
if (od->od_SwingAng >= MAXSWINGANG) {
od->od_SwingAng = MAXSWINGANG;
od->ob.ob_State = OBS_OPEN;
od->od_ME->me_Flags &=
~(MEF_WALKSOLID | MEF_SHOTSOLID);
}
} else if (od->ob.ob_State == OBS_CLOSING) {
od->od_SwingAng -= SWINGANGSTEP * nframes;
if (od->od_SwingAng <= 0) {
od->od_SwingAng = 0;
od->ob.ob_State = OBS_CLOSED;
playsound (SFX_CLOSEDOOR);
}
}
break;
}
case OP_REGISTER:
{
register Vertex *dv; // Hi, Dave!
register frac16 c, s;
ubyte *regidxs;
Vector trans;
if (nobverts + DOORVERTS > NOBVERTS)
break;
od->ob.ob_VertIdx = nobverts;
dv = obverts + nobverts;
regidxs = ((DefNSDoor *) (od->ob.ob_Def))->dd_RegIdxs;
/* Excuse me while I misuse c and s for a moment... */
for (c = 0; c < 6; c++) {
s = *regidxs++;
*dv++ = unitsquare[s];
*dv++ = unitsquare[s+9];
}
/*
* Compute and plot positions of doors.
*/
c = MulSF16 (DOORWIDE, CosF16 (od->od_SwingAng));
s = MulSF16 (DOORWIDE, SinF16 (od->od_SwingAng));
if (od->ob.ob_Type == OTYP_EWDOOR) {
/*
* East-west door.
*/
dv[0].X = dv[1].X = unitsquare[7].X + s;
dv[0].Z = dv[1].Z = unitsquare[7].Z - c;
dv[0].Y = 0; dv[1].Y = -ONE_F16;
dv += 2;
dv[0].X = dv[1].X = unitsquare[1].X + s;
dv[0].Z = dv[1].Z = c;
dv[0].Y = 0; dv[1].Y = -ONE_F16;
} else {
/*
* North-south door.
*/
dv[0].X = dv[1].X = c;
dv[0].Z = dv[1].Z = unitsquare[3].Z + s;
dv[0].Y = 0; dv[1].Y = -ONE_F16;
dv += 2;
dv[0].X = dv[1].X = unitsquare[5].X - c;
dv[0].Z = dv[1].Z = unitsquare[5].Z + s;
dv[0].Y = 0; dv[1].Y = -ONE_F16;
}
trans.X = Convert32_F16 (od->od_XIdx);
trans.Y = 0;
trans.Z = Convert32_F16 (od->od_ZIdx);
translatemany (&trans, obverts + od->ob.ob_VertIdx, 16);
nobverts += DOORVERTS;
break;
}
case OP_RENDER:
{
register int i, nvo;
register ubyte *spareflags, *imgidxs;
VisOb vo[6], *vop, *argvo;
CCB *prevccb;
argvo = dat;
spareflags = ((DefNSDoor *) (od->ob.ob_Def))->dd_SpareFlags;
imgidxs = ((DefNSDoor *) (od->ob.ob_Def))->dd_ImgIdxs;
vop = vo;
nvo = 0;
for (i = 6; --i >= 0; ) {
if (argvo->vo_VisFlags & spareflags[i])
continue;
vop->vo_LIdx = rendidxs[i][0];
vop->vo_RIdx = rendidxs[i][1];
vop->vo_MEFlags = 0;
vop->vo_ImgIdx = imgidxs[i] + WALLIMGBASE;
vop->vo_ME = NULL;
vop++;
nvo++;
}
prevccb = curccb;
buildcellist (vo, nvo,
obverts + od->ob.ob_VertIdx,
xfobverts + od->ob.ob_VertIdx,
FALSE);
if (prevccb != curccb)
/* Something was actually rendered. */
od->ob.ob_Flags |= OBF_SAWME;
break;
}
case OP_PROBE:
{
register frac16 distq;
register int state;
distq = SquareSF16 (playerpos.X -
Convert32_F16 (od->od_XIdx) - HALF_F16) +
SquareSF16 (playerpos.Z -
Convert32_F16 (od->od_ZIdx) - HALF_F16);
if (distq >= PROBEDISTQ)
/* Not close enough. */
break;
state = od->ob.ob_State;
if (state == OBS_CLOSING || state == OBS_CLOSED) {
if (od->od_Unlocked) {
opendoor: od->ob.ob_State = OBS_OPENING;
playsound (SFX_OPENDOOR);
} else if (nkeys) {
nkeys--;
od->od_Unlocked = TRUE;
goto opendoor; // Look up;
} else
playsound (SFX_DOORLOCKED);
}
else if ((state == OBS_OPENING || state == OBS_OPEN) &&
(ConvertF16_32 (playerpos.X) != od->od_XIdx ||
ConvertF16_32 (playerpos.Z) != od->od_ZIdx))
{
od->ob.ob_State = OBS_CLOSING;
od->od_ME->me_Flags |= MEF_WALKSOLID | MEF_SHOTSOLID;
}
return (TRUE);
}
}
return (0);
}
void
toggledoor (ob)
struct Object *ob;
{
register ObNSDoor *od;
register int state;
od = (ObNSDoor *) ob;
state = od->ob.ob_State;
if (state == OBS_CLOSING || state == OBS_CLOSED)
od->ob.ob_State = OBS_OPENING;
else if ((state == OBS_OPENING || state == OBS_OPEN) &&
(ConvertF16_32 (playerpos.X) != od->od_XIdx ||
ConvertF16_32 (playerpos.Z) != od->od_ZIdx))
{
od->ob.ob_State = OBS_CLOSING;
od->od_ME->me_Flags |= MEF_WALKSOLID | MEF_SHOTSOLID;
}
}
/***************************************************************************
* A very important static table.
*/
extern ObDef def_Zombie; // These are lies.
extern ObDef def_George;
extern ObDef def_Head;
extern ObDef def_Spider;
extern ObDef def_Powerup;
extern ObDef def_Exit;
extern ObDef def_Trigger;
ObDef *obdefs[] = {
(ObDef *) &def_Zombie,
(ObDef *) &def_George,
(ObDef *) &def_Head,
(ObDef *) &def_Spider,
(ObDef *) &def_NSDoor,
(ObDef *) &def_Powerup,
(ObDef *) &def_Exit,
(ObDef *) &def_Trigger,
};
#define NOBDEFS (sizeof (obdefs) / sizeof (ObDef *))
int32 nobdefs = NOBDEFS;