[docs] Replace cyrillic 'с' with latin 'c' in register names
[kolibrios.git] / contrib / other / sdldoom-1.10 / p_setup.c
blobe5d3667fd1e6e90085476017884e46ef5a817086
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.
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.
17 // $Log:$
19 // DESCRIPTION:
20 // Do all the WAD I/O, get map description,
21 // set up initial state and misc. LUTs.
23 //-----------------------------------------------------------------------------
25 static const char
26 rcsid[] = "$Id: p_setup.c,v 1.5 1997/02/03 22:45:12 b1 Exp $";
29 #include <math.h>
31 #include "z_zone.h"
33 #include "m_swap.h"
34 #include "m_bbox.h"
36 #include "g_game.h"
38 #include "i_system.h"
39 #include "w_wad.h"
41 #include "doomdef.h"
42 #include "p_local.h"
44 #include "s_sound.h"
46 #include "doomstat.h"
49 void P_SpawnMapThing (mapthing_t* mthing);
53 // MAP related Lookup tables.
54 // Store VERTEXES, LINEDEFS, SIDEDEFS, etc.
56 int numvertexes;
57 vertex_t* vertexes;
59 int numsegs;
60 seg_t* segs;
62 int numsectors;
63 sector_t* sectors;
65 int numsubsectors;
66 subsector_t* subsectors;
68 int numnodes;
69 node_t* nodes;
71 int numlines;
72 line_t* lines;
74 int numsides;
75 side_t* sides;
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.
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;
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.
105 byte* rejectmatrix;
108 // Maintain single and multi player starting spots.
109 #define MAX_DEATHMATCH_STARTS 10
111 mapthing_t deathmatchstarts[MAX_DEATHMATCH_STARTS];
112 mapthing_t* deathmatch_p;
113 mapthing_t playerstarts[MAXPLAYERS];
120 // P_LoadVertexes
122 void P_LoadVertexes (int lump)
124 byte* data;
125 int i;
126 mapvertex_t* ml;
127 vertex_t* li;
129 // Determine number of lumps:
130 // total lump length / vertex record length.
131 numvertexes = W_LumpLength (lump) / sizeof(mapvertex_t);
133 // Allocate zone memory for buffer.
134 vertexes = Z_Malloc (numvertexes*sizeof(vertex_t),PU_LEVEL,0);
136 // Load data into cache.
137 data = W_CacheLumpNum (lump,PU_STATIC);
139 ml = (mapvertex_t *)data;
140 li = vertexes;
142 // Copy and convert vertex coordinates,
143 // internal representation as fixed.
144 for (i=0 ; i<numvertexes ; i++, li++, ml++)
146 li->x = SHORT(ml->x)<<FRACBITS;
147 li->y = SHORT(ml->y)<<FRACBITS;
150 // Free buffer memory.
151 Z_Free (data);
157 // P_LoadSegs
159 void P_LoadSegs (int lump)
161 byte* data;
162 int i;
163 mapseg_t* ml;
164 seg_t* li;
165 line_t* ldef;
166 int linedef;
167 int side;
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);
174 ml = (mapseg_t *)data;
175 li = segs;
176 for (i=0 ; i<numsegs ; i++, li++, ml++)
178 li->v1 = &vertexes[SHORT(ml->v1)];
179 li->v2 = &vertexes[SHORT(ml->v2)];
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;
195 Z_Free (data);
200 // P_LoadSubsectors
202 void P_LoadSubsectors (int lump)
204 byte* data;
205 int i;
206 mapsubsector_t* ms;
207 subsector_t* ss;
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);
213 ms = (mapsubsector_t *)data;
214 memset (subsectors,0, numsubsectors*sizeof(subsector_t));
215 ss = subsectors;
217 for (i=0 ; i<numsubsectors ; i++, ss++, ms++)
219 ss->numlines = SHORT(ms->numsegs);
220 ss->firstline = SHORT(ms->firstseg);
223 Z_Free (data);
229 // P_LoadSectors
231 void P_LoadSectors (int lump)
233 byte* data;
234 int i;
235 mapsector_t* ms;
236 sector_t* ss;
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);
243 ms = (mapsector_t *)data;
244 ss = sectors;
245 for (i=0 ; i<numsectors ; i++, ss++, ms++)
247 ss->floorheight = SHORT(ms->floorheight)<<FRACBITS;
248 ss->ceilingheight = SHORT(ms->ceilingheight)<<FRACBITS;
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;
257 Z_Free (data);
262 // P_LoadNodes
264 void P_LoadNodes (int lump)
266 byte* data;
267 int i;
268 int j;
269 int k;
270 mapnode_t* mn;
271 node_t* no;
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);
277 mn = (mapnode_t *)data;
278 no = nodes;
280 for (i=0 ; i<numnodes ; i++, no++, mn++)
282 no->x = SHORT(mn->x)<<FRACBITS;
283 no->y = SHORT(mn->y)<<FRACBITS;
284 no->dx = SHORT(mn->dx)<<FRACBITS;
285 no->dy = SHORT(mn->dy)<<FRACBITS;
286 for (j=0 ; j<2 ; j++)
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])<<FRACBITS;
294 Z_Free (data);
299 // P_LoadThings
301 void P_LoadThings (int lump)
303 byte* data;
304 int i;
305 mapthing_t* mt;
306 int numthings;
307 boolean spawn;
309 data = W_CacheLumpNum (lump,PU_STATIC);
310 numthings = W_LumpLength (lump) / sizeof(mapthing_t);
312 mt = (mapthing_t *)data;
313 for (i=0 ; i<numthings ; i++, mt++)
315 spawn = true;
317 // Do not spawn cool, new monsters if !commercial
318 if ( gamemode != commercial)
320 switch(mt->type)
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;
336 if (spawn == false)
337 break;
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);
346 P_SpawnMapThing (mt);
349 Z_Free (data);
354 // P_LoadLineDefs
355 // Also counts secret lines for intermissions.
357 void P_LoadLineDefs (int lump)
359 byte* data;
360 int i;
361 maplinedef_t* mld;
362 line_t* ld;
363 vertex_t* v1;
364 vertex_t* v2;
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);
371 mld = (maplinedef_t *)data;
372 ld = lines;
373 for (i=0 ; i<numlines ; i++, mld++, ld++)
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;
383 if (!ld->dx)
384 ld->slopetype = ST_VERTICAL;
385 else if (!ld->dy)
386 ld->slopetype = ST_HORIZONTAL;
387 else
389 if (FixedDiv (ld->dy , ld->dx) > 0)
390 ld->slopetype = ST_POSITIVE;
391 else
392 ld->slopetype = ST_NEGATIVE;
395 if (v1->x < v2->x)
397 ld->bbox[BOXLEFT] = v1->x;
398 ld->bbox[BOXRIGHT] = v2->x;
400 else
402 ld->bbox[BOXLEFT] = v2->x;
403 ld->bbox[BOXRIGHT] = v1->x;
406 if (v1->y < v2->y)
408 ld->bbox[BOXBOTTOM] = v1->y;
409 ld->bbox[BOXTOP] = v2->y;
411 else
413 ld->bbox[BOXBOTTOM] = v2->y;
414 ld->bbox[BOXTOP] = v1->y;
417 ld->sidenum[0] = SHORT(mld->sidenum[0]);
418 ld->sidenum[1] = SHORT(mld->sidenum[1]);
420 if (ld->sidenum[0] != -1)
421 ld->frontsector = sides[ld->sidenum[0]].sector;
422 else
423 ld->frontsector = 0;
425 if (ld->sidenum[1] != -1)
426 ld->backsector = sides[ld->sidenum[1]].sector;
427 else
428 ld->backsector = 0;
431 Z_Free (data);
436 // P_LoadSideDefs
438 void P_LoadSideDefs (int lump)
440 byte* data;
441 int i;
442 mapsidedef_t* msd;
443 side_t* sd;
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);
450 msd = (mapsidedef_t *)data;
451 sd = sides;
452 for (i=0 ; i<numsides ; i++, msd++, sd++)
454 sd->textureoffset = SHORT(msd->textureoffset)<<FRACBITS;
455 sd->rowoffset = SHORT(msd->rowoffset)<<FRACBITS;
456 sd->toptexture = R_TextureNumForName(msd->toptexture);
457 sd->bottomtexture = R_TextureNumForName(msd->bottomtexture);
458 sd->midtexture = R_TextureNumForName(msd->midtexture);
459 sd->sector = &sectors[SHORT(msd->sector)];
462 Z_Free (data);
467 // P_LoadBlockMap
469 void P_LoadBlockMap (int lump)
471 int i;
472 int count;
474 blockmaplump = W_CacheLumpNum (lump,PU_LEVEL);
475 blockmap = blockmaplump+4;
476 count = W_LumpLength (lump)/2;
478 for (i=0 ; i<count ; i++)
479 blockmaplump[i] = SHORT(blockmaplump[i]);
481 bmaporgx = blockmaplump[0]<<FRACBITS;
482 bmaporgy = blockmaplump[1]<<FRACBITS;
483 bmapwidth = blockmaplump[2];
484 bmapheight = blockmaplump[3];
486 // clear out mobj chains
487 count = sizeof(*blocklinks)* bmapwidth*bmapheight;
488 blocklinks = Z_Malloc (count,PU_LEVEL, 0);
489 memset (blocklinks, 0, count);
495 // P_GroupLines
496 // Builds sector line lists and subsector sector numbers.
497 // Finds block bounding boxes for sectors.
499 void P_GroupLines (void)
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;
512 // look up sector number for each subsector
513 ss = subsectors;
514 for (i=0 ; i<numsubsectors ; i++, ss++)
516 seg = &segs[ss->firstline];
517 ss->sector = seg->sidedef->sector;
520 // count number of lines in each sector
521 li = lines;
522 total = 0;
523 for (i=0 ; i<numlines ; i++, li++)
525 total++;
526 li->frontsector->linecount++;
528 if (li->backsector && li->backsector != li->frontsector)
530 li->backsector->linecount++;
531 total++;
535 // build line tables for each sector
536 linebuffer = Z_Malloc (total*4, PU_LEVEL, 0);
537 sector = sectors;
538 for (i=0 ; i<numsectors ; i++, sector++)
540 M_ClearBox (bbox);
541 sector->lines = linebuffer;
542 li = lines;
543 for (j=0 ; j<numlines ; j++, li++)
545 if (li->frontsector == sector || li->backsector == sector)
547 *linebuffer++ = li;
548 M_AddToBox (bbox, li->v1->x, li->v1->y);
549 M_AddToBox (bbox, li->v2->x, li->v2->y);
552 if (linebuffer - sector->lines != sector->linecount)
553 I_Error ("P_GroupLines: miscounted");
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;
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;
564 block = (bbox[BOXBOTTOM]-bmaporgy-MAXRADIUS)>>MAPBLOCKSHIFT;
565 block = block < 0 ? 0 : block;
566 sector->blockbox[BOXBOTTOM]=block;
568 block = (bbox[BOXRIGHT]-bmaporgx+MAXRADIUS)>>MAPBLOCKSHIFT;
569 block = block >= bmapwidth ? bmapwidth-1 : block;
570 sector->blockbox[BOXRIGHT]=block;
572 block = (bbox[BOXLEFT]-bmaporgx-MAXRADIUS)>>MAPBLOCKSHIFT;
573 block = block < 0 ? 0 : block;
574 sector->blockbox[BOXLEFT]=block;
581 // P_SetupLevel
583 void
584 P_SetupLevel
585 ( int episode,
586 int map,
587 int playermask,
588 skill_t skill)
590 int i;
591 char lumpname[9];
592 int lumpnum;
594 totalkills = totalitems = totalsecret = wminfo.maxfrags = 0;
595 wminfo.partime = 180;
596 for (i=0 ; i<MAXPLAYERS ; i++)
598 players[i].killcount = players[i].secretcount
599 = players[i].itemcount = 0;
602 // Initial height of PointOfView
603 // will be set by player think.
604 players[consoleplayer].viewz = 1;
606 // Make sure all sounds are stopped before Z_FreeTags.
607 S_Start ();
610 #if 0 // UNUSED
611 if (debugfile)
613 Z_FreeTags (PU_LEVEL, MAXINT);
614 Z_FileDumpHeap (debugfile);
616 else
617 #endif
618 Z_FreeTags (PU_LEVEL, PU_PURGELEVEL-1);
621 // UNUSED W_Profile ();
622 P_InitThinkers ();
624 // if working with a devlopment map, reload it
625 W_Reload ();
627 // find map name
628 if ( gamemode == commercial)
630 if (map<10)
631 sprintf (lumpname,"map0%i", map);
632 else
633 sprintf (lumpname,"map%i", map);
635 else
637 lumpname[0] = 'E';
638 lumpname[1] = '0' + episode;
639 lumpname[2] = 'M';
640 lumpname[3] = '0' + map;
641 lumpname[4] = 0;
644 lumpnum = W_GetNumForName (lumpname);
646 leveltime = 0;
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);
654 P_LoadLineDefs (lumpnum+ML_LINEDEFS);
655 P_LoadSubsectors (lumpnum+ML_SSECTORS);
656 P_LoadNodes (lumpnum+ML_NODES);
657 P_LoadSegs (lumpnum+ML_SEGS);
659 rejectmatrix = W_CacheLumpNum (lumpnum+ML_REJECT,PU_LEVEL);
660 P_GroupLines ();
662 bodyqueslot = 0;
663 deathmatch_p = deathmatchstarts;
664 P_LoadThings (lumpnum+ML_THINGS);
666 // if deathmatch, randomly spawn the active players
667 if (deathmatch)
669 for (i=0 ; i<MAXPLAYERS ; i++)
670 if (playeringame[i])
672 players[i].mo = NULL;
673 G_DeathMatchSpawnPlayer (i);
678 // clear special respawning que
679 iquehead = iquetail = 0;
681 // set up world state
682 P_SpawnSpecials ();
684 // build subsector connect matrix
685 // UNUSED P_ConnectSubsectors ();
687 // preload graphics
688 if (precache)
689 R_PrecacheLevel ();
691 //printf ("free memory: 0x%x\n", Z_FreeMemory());
698 // P_Init
700 void P_Init (void)
702 P_InitSwitchList ();
703 P_InitPicAnims ();
704 R_InitSprites (sprnames);