33  Map Setup & Level Initialization

This tour examines how DOOM loads and initializes map data. We’ll look at the setup functions declared in p_setup.h and their implementation in p_setup.c, covering level initialization and startup configuration.

**Two functions handle level initialization:

• P_SetupLevel loads and configures a specific game level • P_Init prepares the global gameplay state at startup**
linuxdoom-1.10/p_setup.h lines 32–41

P_SetupLevel performs all per‐level initialization: it clears previous data, reads map geometry and spawn data, and sets up world specials. P_Init handles one‐time engine startup tasks related to level mechanics (switch list, animations, sprites).

P_SetupLevel loads map geometry and builds structures for collision detection and rendering. The following steps show the loading sequence.
linuxdoom-1.10/p_setup.c lines 583–590

In P_CheckSight, rejectmatrix provides a quick visibility test between sectors. After computing bytenum and bitnum from the sectors being checked, line 320 tests rejectmatrix[bytenum] & bitnum. A nonzero result means the sectors cannot see each other, allowing the function to return early without performing more complex geometry checks.
linuxdoom-1.10/p_setup.c lines 607–624

P_CheckSight uses rejectmatrix for fast visibility tests between sectors. If rejectmatrix indicates no visibility is possible, the function returns early before doing expensive geometry checks.
linuxdoom-1.10/p_setup.c lines 644–651

The REJECT matrix is loaded for visibility checks. P_GroupLines() organizes lines into sector-based lists for collision detection and rendering.

The deathmatch flag modifies how P_SetupLevel spawns players and items in the level.
linuxdoom-1.10/p_sight.c lines 319–322

deathmatchstarts stores up to 10 spawn points for deathmatch mode, populated from type-11 mapthings.
linuxdoom-1.10/p_setup.c lines 659–661

deathmatch_p tracks deathmatch spawn points during level loading.

Resets counters and initializes spawn points, then loads all map entities from the THINGS lump.
linuxdoom-1.10/doomstat.h lines 94–96

Spawns players at random locations when in deathmatch mode.
linuxdoom-1.10/p_setup.c lines 109–111

The setup process finalizes by clearing the respawn queue and calling P_SpawnSpecials() to initialize special sector behaviors and effects.
linuxdoom-1.10/p_setup.c lines 112–112

P_SpawnSpecials performs three main tasks: 1. Sets up deathmatch timer if -avg or -timer parameters are present 2. Scans sectors to spawn thinkers for lights, doors, and platforms based on sector.special 3. Processes line specials and initializes arrays
linuxdoom-1.10/p_setup.c lines 662–665

Case 1 handles flickering lights by creating a light-flash thinker for the sector.
linuxdoom-1.10/p_setup.c lines 666–675

Case 2 creates fast strobe lighting effects in the game world.
linuxdoom-1.10/p_setup.c lines 678–683

The code identifies lines with special effect 48, adding them to linespeciallist for handling scrolling walls.

P_SpawnSpecials resets all ceiling and platform movers by clearing the activeceilings and activeplats arrays.
linuxdoom-1.10/p_spec.c lines 1278–1281

P_Init initializes the game engine’s visual elements at startup, handling switch textures, animations, and sprite definitions.
linuxdoom-1.10/p_spec.c lines 1341–1345

And that’s it! You’ve seen the core engine’s approach to map setup - from WAD loading to world initialization.
linuxdoom-1.10/p_spec.c lines 1351–1354