Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
298 serge 1
// Emacs style mode select   -*- C++ -*-
2
//-----------------------------------------------------------------------------
3
//
4
// $Id:$
5
//
6
// Copyright (C) 1993-1996 by id Software, Inc.
7
//
8
// This source is available for distribution and/or modification
9
// only under the terms of the DOOM Source Code License as
10
// published by id Software. All rights reserved.
11
//
12
// The source is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
15
// for more details.
16
//
17
// $Log:$
18
//
19
// DESCRIPTION:
20
//	Do all the WAD I/O, get map description,
21
//	set up initial state and misc. LUTs.
22
//
23
//-----------------------------------------------------------------------------
24
 
25
static const char
26
rcsid[] = "$Id: p_setup.c,v 1.5 1997/02/03 22:45:12 b1 Exp $";
27
 
28
 
29
#include 
30
 
31
#include "z_zone.h"
32
 
33
#include "m_swap.h"
34
#include "m_bbox.h"
35
 
36
#include "g_game.h"
37
 
38
#include "i_system.h"
39
#include "w_wad.h"
40
 
41
#include "doomdef.h"
42
#include "p_local.h"
43
 
44
#include "s_sound.h"
45
 
46
#include "doomstat.h"
47
 
48
 
49
void	P_SpawnMapThing (mapthing_t*	mthing);
50
 
51
 
52
//
53
// MAP related Lookup tables.
54
// Store VERTEXES, LINEDEFS, SIDEDEFS, etc.
55
//
56
int		numvertexes;
57
vertex_t*	vertexes;
58
 
59
int		numsegs;
60
seg_t*		segs;
61
 
62
int		numsectors;
63
sector_t*	sectors;
64
 
65
int		numsubsectors;
66
subsector_t*	subsectors;
67
 
68
int		numnodes;
69
node_t*		nodes;
70
 
71
int		numlines;
72
line_t*		lines;
73
 
74
int		numsides;
75
side_t*		sides;
76
 
77
 
78
// BLOCKMAP
79
// Created from axis aligned bounding box
80
// of the map, a rectangular array of
81
// blocks of size ...
82
// Used to speed up collision detection
83
// by spatial subdivision in 2D.
84
//
85
// Blockmap size.
86
int		bmapwidth;
87
int		bmapheight;	// size in mapblocks
88
short*		blockmap;	// int for larger maps
89
// offsets in blockmap are from here
90
short*		blockmaplump;
91
// origin of block map
92
fixed_t		bmaporgx;
93
fixed_t		bmaporgy;
94
// for thing chains
95
mobj_t**	blocklinks;
96
 
97
 
98
// REJECT
99
// For fast sight rejection.
100
// Speeds up enemy AI by skipping detailed
101
//  LineOf Sight calculation.
102
// Without special effect, this could be
103
//  used as a PVS lookup as well.
104
//
105
byte*		rejectmatrix;
106
 
107
 
108
// Maintain single and multi player starting spots.
109
#define MAX_DEATHMATCH_STARTS	10
110
 
111
mapthing_t	deathmatchstarts[MAX_DEATHMATCH_STARTS];
112
mapthing_t*	deathmatch_p;
113
mapthing_t	playerstarts[MAXPLAYERS];
114
 
115
 
116
 
117
 
118
 
119
//
120
// P_LoadVertexes
121
//
122
void P_LoadVertexes (int lump)
123
{
124
    byte*		data;
125
    int			i;
126
    mapvertex_t*	ml;
127
    vertex_t*		li;
128
 
129
    // Determine number of lumps:
130
    //  total lump length / vertex record length.
131
    numvertexes = W_LumpLength (lump) / sizeof(mapvertex_t);
132
 
133
    // Allocate zone memory for buffer.
134
    vertexes = Z_Malloc (numvertexes*sizeof(vertex_t),PU_LEVEL,0);
135
 
136
    // Load data into cache.
137
    data = W_CacheLumpNum (lump,PU_STATIC);
138
 
139
    ml = (mapvertex_t *)data;
140
    li = vertexes;
141
 
142
    // Copy and convert vertex coordinates,
143
    // internal representation as fixed.
144
    for (i=0 ; i
145
    {
146
	li->x = SHORT(ml->x)<
147
	li->y = SHORT(ml->y)<
148
    }
149
 
150
    // Free buffer memory.
151
    Z_Free (data);
152
}
153
 
154
 
155
 
156
//
157
// P_LoadSegs
158
//
159
void P_LoadSegs (int lump)
160
{
161
    byte*		data;
162
    int			i;
163
    mapseg_t*		ml;
164
    seg_t*		li;
165
    line_t*		ldef;
166
    int			linedef;
167
    int			side;
168
 
169
    numsegs = W_LumpLength (lump) / sizeof(mapseg_t);
170
    segs = Z_Malloc (numsegs*sizeof(seg_t),PU_LEVEL,0);
171
    memset (segs, 0, numsegs*sizeof(seg_t));
172
    data = W_CacheLumpNum (lump,PU_STATIC);
173
 
174
    ml = (mapseg_t *)data;
175
    li = segs;
176
    for (i=0 ; i
177
    {
178
	li->v1 = &vertexes[SHORT(ml->v1)];
179
	li->v2 = &vertexes[SHORT(ml->v2)];
180
 
181
	li->angle = (SHORT(ml->angle))<<16;
182
	li->offset = (SHORT(ml->offset))<<16;
183
	linedef = SHORT(ml->linedef);
184
	ldef = &lines[linedef];
185
	li->linedef = ldef;
186
	side = SHORT(ml->side);
187
	li->sidedef = &sides[ldef->sidenum[side]];
188
	li->frontsector = sides[ldef->sidenum[side]].sector;
189
	if (ldef-> flags & ML_TWOSIDED)
190
	    li->backsector = sides[ldef->sidenum[side^1]].sector;
191
	else
192
	    li->backsector = 0;
193
    }
194
 
195
    Z_Free (data);
196
}
197
 
198
 
199
//
200
// P_LoadSubsectors
201
//
202
void P_LoadSubsectors (int lump)
203
{
204
    byte*		data;
205
    int			i;
206
    mapsubsector_t*	ms;
207
    subsector_t*	ss;
208
 
209
    numsubsectors = W_LumpLength (lump) / sizeof(mapsubsector_t);
210
    subsectors = Z_Malloc (numsubsectors*sizeof(subsector_t),PU_LEVEL,0);
211
    data = W_CacheLumpNum (lump,PU_STATIC);
212
 
213
    ms = (mapsubsector_t *)data;
214
    memset (subsectors,0, numsubsectors*sizeof(subsector_t));
215
    ss = subsectors;
216
 
217
    for (i=0 ; i
218
    {
219
	ss->numlines = SHORT(ms->numsegs);
220
	ss->firstline = SHORT(ms->firstseg);
221
    }
222
 
223
    Z_Free (data);
224
}
225
 
226
 
227
 
228
//
229
// P_LoadSectors
230
//
231
void P_LoadSectors (int lump)
232
{
233
    byte*		data;
234
    int			i;
235
    mapsector_t*	ms;
236
    sector_t*		ss;
237
 
238
    numsectors = W_LumpLength (lump) / sizeof(mapsector_t);
239
    sectors = Z_Malloc (numsectors*sizeof(sector_t),PU_LEVEL,0);
240
    memset (sectors, 0, numsectors*sizeof(sector_t));
241
    data = W_CacheLumpNum (lump,PU_STATIC);
242
 
243
    ms = (mapsector_t *)data;
244
    ss = sectors;
245
    for (i=0 ; i
246
    {
247
	ss->floorheight = SHORT(ms->floorheight)<
248
	ss->ceilingheight = SHORT(ms->ceilingheight)<
249
	ss->floorpic = R_FlatNumForName(ms->floorpic);
250
	ss->ceilingpic = R_FlatNumForName(ms->ceilingpic);
251
	ss->lightlevel = SHORT(ms->lightlevel);
252
	ss->special = SHORT(ms->special);
253
	ss->tag = SHORT(ms->tag);
254
	ss->thinglist = NULL;
255
    }
256
 
257
    Z_Free (data);
258
}
259
 
260
 
261
//
262
// P_LoadNodes
263
//
264
void P_LoadNodes (int lump)
265
{
266
    byte*	data;
267
    int		i;
268
    int		j;
269
    int		k;
270
    mapnode_t*	mn;
271
    node_t*	no;
272
 
273
    numnodes = W_LumpLength (lump) / sizeof(mapnode_t);
274
    nodes = Z_Malloc (numnodes*sizeof(node_t),PU_LEVEL,0);
275
    data = W_CacheLumpNum (lump,PU_STATIC);
276
 
277
    mn = (mapnode_t *)data;
278
    no = nodes;
279
 
280
    for (i=0 ; i
281
    {
282
	no->x = SHORT(mn->x)<
283
	no->y = SHORT(mn->y)<
284
	no->dx = SHORT(mn->dx)<
285
	no->dy = SHORT(mn->dy)<
286
	for (j=0 ; j<2 ; j++)
287
	{
288
	    no->children[j] = SHORT(mn->children[j]);
289
	    for (k=0 ; k<4 ; k++)
290
		no->bbox[j][k] = SHORT(mn->bbox[j][k])<
291
	}
292
    }
293
 
294
    Z_Free (data);
295
}
296
 
297
 
298
//
299
// P_LoadThings
300
//
301
void P_LoadThings (int lump)
302
{
303
    byte*		data;
304
    int			i;
305
    mapthing_t*		mt;
306
    int			numthings;
307
    boolean		spawn;
308
 
309
    data = W_CacheLumpNum (lump,PU_STATIC);
310
    numthings = W_LumpLength (lump) / sizeof(mapthing_t);
311
 
312
    mt = (mapthing_t *)data;
313
    for (i=0 ; i
314
    {
315
	spawn = true;
316
 
317
	// Do not spawn cool, new monsters if !commercial
318
	if ( gamemode != commercial)
319
	{
320
	    switch(mt->type)
321
	    {
322
	      case 68:	// Arachnotron
323
	      case 64:	// Archvile
324
	      case 88:	// Boss Brain
325
	      case 89:	// Boss Shooter
326
	      case 69:	// Hell Knight
327
	      case 67:	// Mancubus
328
	      case 71:	// Pain Elemental
329
	      case 65:	// Former Human Commando
330
	      case 66:	// Revenant
331
	      case 84:	// Wolf SS
332
		spawn = false;
333
		break;
334
	    }
335
	}
336
	if (spawn == false)
337
	    break;
338
 
339
	// Do spawn all other stuff.
340
	mt->x = SHORT(mt->x);
341
	mt->y = SHORT(mt->y);
342
	mt->angle = SHORT(mt->angle);
343
	mt->type = SHORT(mt->type);
344
	mt->options = SHORT(mt->options);
345
 
346
	P_SpawnMapThing (mt);
347
    }
348
 
349
    Z_Free (data);
350
}
351
 
352
 
353
//
354
// P_LoadLineDefs
355
// Also counts secret lines for intermissions.
356
//
357
void P_LoadLineDefs (int lump)
358
{
359
    byte*		data;
360
    int			i;
361
    maplinedef_t*	mld;
362
    line_t*		ld;
363
    vertex_t*		v1;
364
    vertex_t*		v2;
365
 
366
    numlines = W_LumpLength (lump) / sizeof(maplinedef_t);
367
    lines = Z_Malloc (numlines*sizeof(line_t),PU_LEVEL,0);
368
    memset (lines, 0, numlines*sizeof(line_t));
369
    data = W_CacheLumpNum (lump,PU_STATIC);
370
 
371
    mld = (maplinedef_t *)data;
372
    ld = lines;
373
    for (i=0 ; i
374
    {
375
	ld->flags = SHORT(mld->flags);
376
	ld->special = SHORT(mld->special);
377
	ld->tag = SHORT(mld->tag);
378
	v1 = ld->v1 = &vertexes[SHORT(mld->v1)];
379
	v2 = ld->v2 = &vertexes[SHORT(mld->v2)];
380
	ld->dx = v2->x - v1->x;
381
	ld->dy = v2->y - v1->y;
382
 
383
	if (!ld->dx)
384
	    ld->slopetype = ST_VERTICAL;
385
	else if (!ld->dy)
386
	    ld->slopetype = ST_HORIZONTAL;
387
	else
388
	{
389
	    if (FixedDiv (ld->dy , ld->dx) > 0)
390
		ld->slopetype = ST_POSITIVE;
391
	    else
392
		ld->slopetype = ST_NEGATIVE;
393
	}
394
 
395
	if (v1->x < v2->x)
396
	{
397
	    ld->bbox[BOXLEFT] = v1->x;
398
	    ld->bbox[BOXRIGHT] = v2->x;
399
	}
400
	else
401
	{
402
	    ld->bbox[BOXLEFT] = v2->x;
403
	    ld->bbox[BOXRIGHT] = v1->x;
404
	}
405
 
406
	if (v1->y < v2->y)
407
	{
408
	    ld->bbox[BOXBOTTOM] = v1->y;
409
	    ld->bbox[BOXTOP] = v2->y;
410
	}
411
	else
412
	{
413
	    ld->bbox[BOXBOTTOM] = v2->y;
414
	    ld->bbox[BOXTOP] = v1->y;
415
	}
416
 
417
	ld->sidenum[0] = SHORT(mld->sidenum[0]);
418
	ld->sidenum[1] = SHORT(mld->sidenum[1]);
419
 
420
	if (ld->sidenum[0] != -1)
421
	    ld->frontsector = sides[ld->sidenum[0]].sector;
422
	else
423
	    ld->frontsector = 0;
424
 
425
	if (ld->sidenum[1] != -1)
426
	    ld->backsector = sides[ld->sidenum[1]].sector;
427
	else
428
	    ld->backsector = 0;
429
    }
430
 
431
    Z_Free (data);
432
}
433
 
434
 
435
//
436
// P_LoadSideDefs
437
//
438
void P_LoadSideDefs (int lump)
439
{
440
    byte*		data;
441
    int			i;
442
    mapsidedef_t*	msd;
443
    side_t*		sd;
444
 
445
    numsides = W_LumpLength (lump) / sizeof(mapsidedef_t);
446
    sides = Z_Malloc (numsides*sizeof(side_t),PU_LEVEL,0);
447
    memset (sides, 0, numsides*sizeof(side_t));
448
    data = W_CacheLumpNum (lump,PU_STATIC);
449
 
450
    msd = (mapsidedef_t *)data;
451
    sd = sides;
452
    for (i=0 ; i
453
    {
454
	sd->textureoffset = SHORT(msd->textureoffset)<
455
	sd->rowoffset = SHORT(msd->rowoffset)<
456
	sd->toptexture = R_TextureNumForName(msd->toptexture);
457
	sd->bottomtexture = R_TextureNumForName(msd->bottomtexture);
458
	sd->midtexture = R_TextureNumForName(msd->midtexture);
459
	sd->sector = §ors[SHORT(msd->sector)];
460
    }
461
 
462
    Z_Free (data);
463
}
464
 
465
 
466
//
467
// P_LoadBlockMap
468
//
469
void P_LoadBlockMap (int lump)
470
{
471
    int		i;
472
    int		count;
473
 
474
    blockmaplump = W_CacheLumpNum (lump,PU_LEVEL);
475
    blockmap = blockmaplump+4;
476
    count = W_LumpLength (lump)/2;
477
 
478
    for (i=0 ; i
479
	blockmaplump[i] = SHORT(blockmaplump[i]);
480
 
481
    bmaporgx = blockmaplump[0]<
482
    bmaporgy = blockmaplump[1]<
483
    bmapwidth = blockmaplump[2];
484
    bmapheight = blockmaplump[3];
485
 
486
    // clear out mobj chains
487
    count = sizeof(*blocklinks)* bmapwidth*bmapheight;
488
    blocklinks = Z_Malloc (count,PU_LEVEL, 0);
489
    memset (blocklinks, 0, count);
490
}
491
 
492
 
493
 
494
//
495
// P_GroupLines
496
// Builds sector line lists and subsector sector numbers.
497
// Finds block bounding boxes for sectors.
498
//
499
void P_GroupLines (void)
500
{
501
    line_t**		linebuffer;
502
    int			i;
503
    int			j;
504
    int			total;
505
    line_t*		li;
506
    sector_t*		sector;
507
    subsector_t*	ss;
508
    seg_t*		seg;
509
    fixed_t		bbox[4];
510
    int			block;
511
 
512
    // look up sector number for each subsector
513
    ss = subsectors;
514
    for (i=0 ; i
515
    {
516
	seg = &segs[ss->firstline];
517
	ss->sector = seg->sidedef->sector;
518
    }
519
 
520
    // count number of lines in each sector
521
    li = lines;
522
    total = 0;
523
    for (i=0 ; i
524
    {
525
	total++;
526
	li->frontsector->linecount++;
527
 
528
	if (li->backsector && li->backsector != li->frontsector)
529
	{
530
	    li->backsector->linecount++;
531
	    total++;
532
	}
533
    }
534
 
535
    // build line tables for each sector
536
    linebuffer = Z_Malloc (total*4, PU_LEVEL, 0);
537
    sector = sectors;
538
    for (i=0 ; i
539
    {
540
	M_ClearBox (bbox);
541
	sector->lines = linebuffer;
542
	li = lines;
543
	for (j=0 ; j
544
	{
545
	    if (li->frontsector == sector || li->backsector == sector)
546
	    {
547
		*linebuffer++ = li;
548
		M_AddToBox (bbox, li->v1->x, li->v1->y);
549
		M_AddToBox (bbox, li->v2->x, li->v2->y);
550
	    }
551
	}
552
	if (linebuffer - sector->lines != sector->linecount)
553
	    I_Error ("P_GroupLines: miscounted");
554
 
555
	// set the degenmobj_t to the middle of the bounding box
556
	sector->soundorg.x = (bbox[BOXRIGHT]+bbox[BOXLEFT])/2;
557
	sector->soundorg.y = (bbox[BOXTOP]+bbox[BOXBOTTOM])/2;
558
 
559
	// adjust bounding box to map blocks
560
	block = (bbox[BOXTOP]-bmaporgy+MAXRADIUS)>>MAPBLOCKSHIFT;
561
	block = block >= bmapheight ? bmapheight-1 : block;
562
	sector->blockbox[BOXTOP]=block;
563
 
564
	block = (bbox[BOXBOTTOM]-bmaporgy-MAXRADIUS)>>MAPBLOCKSHIFT;
565
	block = block < 0 ? 0 : block;
566
	sector->blockbox[BOXBOTTOM]=block;
567
 
568
	block = (bbox[BOXRIGHT]-bmaporgx+MAXRADIUS)>>MAPBLOCKSHIFT;
569
	block = block >= bmapwidth ? bmapwidth-1 : block;
570
	sector->blockbox[BOXRIGHT]=block;
571
 
572
	block = (bbox[BOXLEFT]-bmaporgx-MAXRADIUS)>>MAPBLOCKSHIFT;
573
	block = block < 0 ? 0 : block;
574
	sector->blockbox[BOXLEFT]=block;
575
    }
576
 
577
}
578
 
579
 
580
//
581
// P_SetupLevel
582
//
583
void
584
P_SetupLevel
585
( int		episode,
586
  int		map,
587
  int		playermask,
588
  skill_t	skill)
589
{
590
    int		i;
591
    char	lumpname[9];
592
    int		lumpnum;
593
 
594
    totalkills = totalitems = totalsecret = wminfo.maxfrags = 0;
595
    wminfo.partime = 180;
596
    for (i=0 ; i
597
    {
598
	players[i].killcount = players[i].secretcount
599
	    = players[i].itemcount = 0;
600
    }
601
 
602
    // Initial height of PointOfView
603
    // will be set by player think.
604
    players[consoleplayer].viewz = 1;
605
 
606
    // Make sure all sounds are stopped before Z_FreeTags.
607
    S_Start ();
608
 
609
 
610
#if 0 // UNUSED
611
    if (debugfile)
612
    {
613
	Z_FreeTags (PU_LEVEL, MAXINT);
614
	Z_FileDumpHeap (debugfile);
615
    }
616
    else
617
#endif
618
	Z_FreeTags (PU_LEVEL, PU_PURGELEVEL-1);
619
 
620
 
621
    // UNUSED W_Profile ();
622
    P_InitThinkers ();
623
 
624
    // if working with a devlopment map, reload it
625
    W_Reload ();
626
 
627
    // find map name
628
    if ( gamemode == commercial)
629
    {
630
	if (map<10)
631
	    sprintf (lumpname,"map0%i", map);
632
	else
633
	    sprintf (lumpname,"map%i", map);
634
    }
635
    else
636
    {
637
	lumpname[0] = 'E';
638
	lumpname[1] = '0' + episode;
639
	lumpname[2] = 'M';
640
	lumpname[3] = '0' + map;
641
	lumpname[4] = 0;
642
    }
643
 
644
    lumpnum = W_GetNumForName (lumpname);
645
 
646
    leveltime = 0;
647
 
648
    // note: most of this ordering is important
649
    P_LoadBlockMap (lumpnum+ML_BLOCKMAP);
650
    P_LoadVertexes (lumpnum+ML_VERTEXES);
651
    P_LoadSectors (lumpnum+ML_SECTORS);
652
    P_LoadSideDefs (lumpnum+ML_SIDEDEFS);
653
 
654
    P_LoadLineDefs (lumpnum+ML_LINEDEFS);
655
    P_LoadSubsectors (lumpnum+ML_SSECTORS);
656
    P_LoadNodes (lumpnum+ML_NODES);
657
    P_LoadSegs (lumpnum+ML_SEGS);
658
 
659
    rejectmatrix = W_CacheLumpNum (lumpnum+ML_REJECT,PU_LEVEL);
660
    P_GroupLines ();
661
 
662
    bodyqueslot = 0;
663
    deathmatch_p = deathmatchstarts;
664
    P_LoadThings (lumpnum+ML_THINGS);
665
 
666
    // if deathmatch, randomly spawn the active players
667
    if (deathmatch)
668
    {
669
	for (i=0 ; i
670
	    if (playeringame[i])
671
	    {
672
		players[i].mo = NULL;
673
		G_DeathMatchSpawnPlayer (i);
674
	    }
675
 
676
    }
677
 
678
    // clear special respawning que
679
    iquehead = iquetail = 0;
680
 
681
    // set up world state
682
    P_SpawnSpecials ();
683
 
684
    // build subsector connect matrix
685
    //	UNUSED P_ConnectSubsectors ();
686
 
687
    // preload graphics
688
    if (precache)
689
	R_PrecacheLevel ();
690
 
691
    //printf ("free memory: 0x%x\n", Z_FreeMemory());
692
 
693
}
694
 
695
 
696
 
697
//
698
// P_Init
699
//
700
void P_Init (void)
701
{
702
    P_InitSwitchList ();
703
    P_InitPicAnims ();
704
    R_InitSprites (sprnames);
705
}
706
 
707
 
708