1 // Emacs style mode select -*- C++ -*-
2 //-----------------------------------------------------------------------------
6 // Copyright (C) 1993-1996 by id Software, Inc.
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.
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
20 // Do all the WAD I/O, get map description,
21 // set up initial state and misc. LUTs.
23 //-----------------------------------------------------------------------------
26 rcsid[] = "$Id: p_setup.c,v 1.5 1997/02/03 22:45:12 b1 Exp $";
47 void P_SpawnMapThing (mapthing_t* mthing);
51 // MAP related Lookup tables.
52 // Store VERTEXES, LINEDEFS, SIDEDEFS, etc.
64 subsector_t* subsectors;
77 // Created from axis aligned bounding box
78 // of the map, a rectangular array of
80 // Used to speed up collision detection
81 // by spatial subdivision in 2D.
85 int bmapheight; // size in mapblocks
86 short* blockmap; // int for larger maps
87 // offsets in blockmap are from here
89 // origin of block map
97 // For fast sight rejection.
98 // Speeds up enemy AI by skipping detailed
99 // LineOf Sight calculation.
100 // Without special effect, this could be
101 // used as a PVS lookup as well.
106 // Maintain single and multi player starting spots.
107 #define MAX_DEATHMATCH_STARTS 10
109 mapthing_t deathmatchstarts[MAX_DEATHMATCH_STARTS];
110 mapthing_t* deathmatch_p;
111 mapthing_t playerstarts[MAXPLAYERS];
120 void P_LoadVertexes (int lump)
127 // Determine number of lumps:
128 // total lump length / vertex record length.
129 numvertexes = W_LumpLength (lump) / sizeof(mapvertex_t);
131 // Allocate zone memory for buffer.
132 vertexes = Z_Malloc (numvertexes*sizeof(vertex_t),PU_LEVEL,0);
134 // Load data into cache.
135 data = W_CacheLumpNum (lump,PU_STATIC);
137 ml = (mapvertex_t *)data;
140 // Copy and convert vertex coordinates,
141 // internal representation as fixed.
142 for (i=0 ; i<numvertexes ; i++, li++, ml++)
144 li->x = SHORT(ml->x)<<FRACBITS;
145 li->y = SHORT(ml->y)<<FRACBITS;
148 // Free buffer memory.
157 void P_LoadSegs (int lump)
167 numsegs = W_LumpLength (lump) / sizeof(mapseg_t);
168 segs = Z_Malloc (numsegs*sizeof(seg_t),PU_LEVEL,0);
169 memset (segs, 0, numsegs*sizeof(seg_t));
170 data = W_CacheLumpNum (lump,PU_STATIC);
172 ml = (mapseg_t *)data;
174 for (i=0 ; i<numsegs ; i++, li++, ml++)
176 li->v1 = &vertexes[SHORT(ml->v1)];
177 li->v2 = &vertexes[SHORT(ml->v2)];
179 li->angle = (SHORT(ml->angle))<<16;
180 li->offset = (SHORT(ml->offset))<<16;
181 linedef = SHORT(ml->linedef);
182 ldef = &lines[linedef];
184 side = SHORT(ml->side);
185 li->sidedef = &sides[ldef->sidenum[side]];
186 li->frontsector = sides[ldef->sidenum[side]].sector;
187 if (ldef-> flags & ML_TWOSIDED)
188 li->backsector = sides[ldef->sidenum[side^1]].sector;
200 void P_LoadSubsectors (int lump)
207 numsubsectors = W_LumpLength (lump) / sizeof(mapsubsector_t);
208 subsectors = Z_Malloc (numsubsectors*sizeof(subsector_t),PU_LEVEL,0);
209 data = W_CacheLumpNum (lump,PU_STATIC);
211 ms = (mapsubsector_t *)data;
212 memset (subsectors,0, numsubsectors*sizeof(subsector_t));
215 for (i=0 ; i<numsubsectors ; i++, ss++, ms++)
217 ss->numlines = SHORT(ms->numsegs);
218 ss->firstline = SHORT(ms->firstseg);
229 void P_LoadSectors (int lump)
236 numsectors = W_LumpLength (lump) / sizeof(mapsector_t);
237 sectors = Z_Malloc (numsectors*sizeof(sector_t),PU_LEVEL,0);
238 memset (sectors, 0, numsectors*sizeof(sector_t));
239 data = W_CacheLumpNum (lump,PU_STATIC);
241 ms = (mapsector_t *)data;
243 for (i=0 ; i<numsectors ; i++, ss++, ms++)
245 ss->floorheight = SHORT(ms->floorheight)<<FRACBITS;
246 ss->ceilingheight = SHORT(ms->ceilingheight)<<FRACBITS;
247 ss->floorpic = R_FlatNumForName(ms->floorpic);
248 ss->ceilingpic = R_FlatNumForName(ms->ceilingpic);
249 ss->lightlevel = SHORT(ms->lightlevel);
250 ss->special = SHORT(ms->special);
251 ss->tag = SHORT(ms->tag);
252 ss->thinglist = NULL;
262 void P_LoadNodes (int lump)
271 numnodes = W_LumpLength (lump) / sizeof(mapnode_t);
272 nodes = Z_Malloc (numnodes*sizeof(node_t),PU_LEVEL,0);
273 data = W_CacheLumpNum (lump,PU_STATIC);
275 mn = (mapnode_t *)data;
278 for (i=0 ; i<numnodes ; i++, no++, mn++)
280 no->x = SHORT(mn->x)<<FRACBITS;
281 no->y = SHORT(mn->y)<<FRACBITS;
282 no->dx = SHORT(mn->dx)<<FRACBITS;
283 no->dy = SHORT(mn->dy)<<FRACBITS;
284 for (j=0 ; j<2 ; j++)
286 no->children[j] = SHORT(mn->children[j]);
287 for (k=0 ; k<4 ; k++)
288 no->bbox[j][k] = SHORT(mn->bbox[j][k])<<FRACBITS;
299 void P_LoadThings (int lump)
307 data = W_CacheLumpNum (lump,PU_STATIC);
308 numthings = W_LumpLength (lump) / sizeof(mapthing_t);
310 mt = (mapthing_t *)data;
311 for (i=0 ; i<numthings ; i++, mt++)
315 // Do not spawn cool, new monsters if !commercial
316 if ( gamemode != commercial)
320 case 68: // Arachnotron
322 case 88: // Boss Brain
323 case 89: // Boss Shooter
324 case 69: // Hell Knight
326 case 71: // Pain Elemental
327 case 65: // Former Human Commando
337 // Do spawn all other stuff.
338 mt->x = SHORT(mt->x);
339 mt->y = SHORT(mt->y);
340 mt->angle = SHORT(mt->angle);
341 mt->type = SHORT(mt->type);
342 mt->options = SHORT(mt->options);
344 P_SpawnMapThing (mt);
353 // Also counts secret lines for intermissions.
355 void P_LoadLineDefs (int lump)
364 numlines = W_LumpLength (lump) / sizeof(maplinedef_t);
365 lines = Z_Malloc (numlines*sizeof(line_t),PU_LEVEL,0);
366 memset (lines, 0, numlines*sizeof(line_t));
367 data = W_CacheLumpNum (lump,PU_STATIC);
369 mld = (maplinedef_t *)data;
371 for (i=0 ; i<numlines ; i++, mld++, ld++)
373 ld->flags = SHORT(mld->flags);
374 ld->special = SHORT(mld->special);
375 ld->tag = SHORT(mld->tag);
376 v1 = ld->v1 = &vertexes[SHORT(mld->v1)];
377 v2 = ld->v2 = &vertexes[SHORT(mld->v2)];
378 ld->dx = v2->x - v1->x;
379 ld->dy = v2->y - v1->y;
382 ld->slopetype = ST_VERTICAL;
384 ld->slopetype = ST_HORIZONTAL;
387 if (FixedDiv (ld->dy , ld->dx) > 0)
388 ld->slopetype = ST_POSITIVE;
390 ld->slopetype = ST_NEGATIVE;
395 ld->bbox[BOXLEFT] = v1->x;
396 ld->bbox[BOXRIGHT] = v2->x;
400 ld->bbox[BOXLEFT] = v2->x;
401 ld->bbox[BOXRIGHT] = v1->x;
406 ld->bbox[BOXBOTTOM] = v1->y;
407 ld->bbox[BOXTOP] = v2->y;
411 ld->bbox[BOXBOTTOM] = v2->y;
412 ld->bbox[BOXTOP] = v1->y;
415 ld->sidenum[0] = SHORT(mld->sidenum[0]);
416 ld->sidenum[1] = SHORT(mld->sidenum[1]);
418 if (ld->sidenum[0] != -1)
419 ld->frontsector = sides[ld->sidenum[0]].sector;
423 if (ld->sidenum[1] != -1)
424 ld->backsector = sides[ld->sidenum[1]].sector;
436 void P_LoadSideDefs (int lump)
443 numsides = W_LumpLength (lump) / sizeof(mapsidedef_t);
444 sides = Z_Malloc (numsides*sizeof(side_t),PU_LEVEL,0);
445 memset (sides, 0, numsides*sizeof(side_t));
446 data = W_CacheLumpNum (lump,PU_STATIC);
448 msd = (mapsidedef_t *)data;
450 for (i=0 ; i<numsides ; i++, msd++, sd++)
452 sd->textureoffset = SHORT(msd->textureoffset)<<FRACBITS;
453 sd->rowoffset = SHORT(msd->rowoffset)<<FRACBITS;
454 sd->toptexture = R_TextureNumForName(msd->toptexture);
455 sd->bottomtexture = R_TextureNumForName(msd->bottomtexture);
456 sd->midtexture = R_TextureNumForName(msd->midtexture);
457 sd->sector = §ors[SHORT(msd->sector)];
467 void P_LoadBlockMap (int lump)
472 blockmaplump = W_CacheLumpNum (lump,PU_LEVEL);
473 blockmap = blockmaplump+4;
474 count = W_LumpLength (lump)/2;
476 for (i=0 ; i<count ; i++)
477 blockmaplump[i] = SHORT(blockmaplump[i]);
479 bmaporgx = blockmaplump[0]<<FRACBITS;
480 bmaporgy = blockmaplump[1]<<FRACBITS;
481 bmapwidth = blockmaplump[2];
482 bmapheight = blockmaplump[3];
484 // clear out mobj chains
485 count = sizeof(*blocklinks)* bmapwidth*bmapheight;
486 blocklinks = Z_Malloc (count,PU_LEVEL, 0);
487 memset (blocklinks, 0, count);
494 // Builds sector line lists and subsector sector numbers.
495 // Finds block bounding boxes for sectors.
497 void P_GroupLines (void)
510 // look up sector number for each subsector
512 for (i=0 ; i<numsubsectors ; i++, ss++)
514 seg = &segs[ss->firstline];
515 ss->sector = seg->sidedef->sector;
518 // count number of lines in each sector
521 for (i=0 ; i<numlines ; i++, li++)
524 li->frontsector->linecount++;
526 if (li->backsector && li->backsector != li->frontsector)
528 li->backsector->linecount++;
533 // build line tables for each sector
534 linebuffer = Z_Malloc (total*sizeof(line_t*), PU_LEVEL, 0);
536 for (i=0 ; i<numsectors ; i++, sector++)
539 sector->lines = linebuffer;
541 for (j=0 ; j<numlines ; j++, li++)
543 if (li->frontsector == sector || li->backsector == sector)
546 M_AddToBox (bbox, li->v1->x, li->v1->y);
547 M_AddToBox (bbox, li->v2->x, li->v2->y);
550 if (linebuffer - sector->lines != sector->linecount)
551 I_Error ("P_GroupLines: miscounted");
553 // set the degenmobj_t to the middle of the bounding box
554 sector->soundorg.x = (bbox[BOXRIGHT]+bbox[BOXLEFT])/2;
555 sector->soundorg.y = (bbox[BOXTOP]+bbox[BOXBOTTOM])/2;
557 // adjust bounding box to map blocks
558 block = (bbox[BOXTOP]-bmaporgy+MAXRADIUS)>>MAPBLOCKSHIFT;
559 block = block >= bmapheight ? bmapheight-1 : block;
560 sector->blockbox[BOXTOP]=block;
562 block = (bbox[BOXBOTTOM]-bmaporgy-MAXRADIUS)>>MAPBLOCKSHIFT;
563 block = block < 0 ? 0 : block;
564 sector->blockbox[BOXBOTTOM]=block;
566 block = (bbox[BOXRIGHT]-bmaporgx+MAXRADIUS)>>MAPBLOCKSHIFT;
567 block = block >= bmapwidth ? bmapwidth-1 : block;
568 sector->blockbox[BOXRIGHT]=block;
570 block = (bbox[BOXLEFT]-bmaporgx-MAXRADIUS)>>MAPBLOCKSHIFT;
571 block = block < 0 ? 0 : block;
572 sector->blockbox[BOXLEFT]=block;
590 totalkills = totalitems = totalsecret = wminfo.maxfrags = 0;
591 wminfo.partime = 180;
592 for (i=0 ; i<MAXPLAYERS ; i++)
594 players[i].killcount = players[i].secretcount
595 = players[i].itemcount = 0;
598 // Initial height of PointOfView
599 // will be set by player think.
600 players[consoleplayer].viewz = 1;
602 // Make sure all sounds are stopped before Z_FreeTags.
605 Z_FreeTags (PU_LEVEL, PU_PURGELEVEL-1);
607 // UNUSED W_Profile ();
610 // if working with a devlopment map, reload it
614 if ( gamemode == commercial)
617 sprintf (lumpname,"map0%i", map);
619 sprintf (lumpname,"map%i", map);
624 lumpname[1] = '0' + episode;
626 lumpname[3] = '0' + map;
630 lumpnum = W_GetNumForName (lumpname);
634 // note: most of this ordering is important
635 P_LoadBlockMap (lumpnum+ML_BLOCKMAP);
636 P_LoadVertexes (lumpnum+ML_VERTEXES);
637 P_LoadSectors (lumpnum+ML_SECTORS);
638 P_LoadSideDefs (lumpnum+ML_SIDEDEFS);
640 P_LoadLineDefs (lumpnum+ML_LINEDEFS);
641 P_LoadSubsectors (lumpnum+ML_SSECTORS);
642 P_LoadNodes (lumpnum+ML_NODES);
643 P_LoadSegs (lumpnum+ML_SEGS);
645 rejectmatrix = W_CacheLumpNum (lumpnum+ML_REJECT,PU_LEVEL);
649 deathmatch_p = deathmatchstarts;
650 P_LoadThings (lumpnum+ML_THINGS);
652 // if deathmatch, randomly spawn the active players
655 for (i=0 ; i<MAXPLAYERS ; i++)
658 printf("DBG: players[%d].mo = NULL\n", i);
659 players[i].mo = NULL;
660 G_DeathMatchSpawnPlayer (i);
665 // clear special respawning que
666 iquehead = iquetail = 0;
668 // set up world state
685 R_InitSprites (sprnames);