<?php/* * dmmotor — what the scenes share: the WAD, the graphics, the level, the 3D view, the 320 x 200 screen, * and its upload to SDL as an 8-bit BMP (the palette changes cost nothing: only the BMP header changes). * Copyright (C) 1993-1996 by id Software, Inc.; PHP port for dmmotor. GPL v2 (see LICENSE). */namespace App\Service;use App\Dm\Automap;use App\Dm\Gfx;use App\Dm\Music;use App\Dm\Play\World;use App\Dm\Render\Framebuffer;use App\Dm\Render\View;use App\Dm\Sounds;use App\Dm\StatusBar;use App\Dm\Strings;use App\Dm\Wad;use App\Dm\WadFile;use JumPHP\Audio\PcmStream;use JumPHP\Audio\SoundManager;use JumPHP\Renderer\Renderer;use JumPHP\Renderer\Sdl;final class DmGame{ public Wad $wad; public Gfx $gfx; public Framebuffer $fb; public View $view; public World $world; public StatusBar $statusBar; public Automap $automap; public Music $music; /** mouse turning: radians per pixel of the view, 0 = off */ public float $mouseSpeed = 0.012; /** a touch screen was used: the mouse does not turn */ public bool $touchSeen = false; public ?Sounds $sounds = null; public bool $loaded = false; /** a level is being played (the automatic pause, when the window loses the focus, is only for it) */ public bool $inLevel = false; /** the WAD chosen (an id of the WadCatalog: freedoom1, freedoom2, or a WAD of the wads/ folder) */ public string $iwad = ''; /** the first map of the WAD chosen: [episode, map] (a mod may start anywhere) */ public array $firstMap = [1, 1]; /** Phase 2: maps MAP01..MAP32, no episodes */ public bool $commercial = false; /** what the player chose in the menus */ public int $skill = World::SK_MEDIUM; public bool $alwaysRun = true; /** @var array<int,string> palette number => BMP header + palette */ private array $bmpHeads = []; public function __construct(public WadCatalog $catalog) { } /** Finds the WADs (assets/wads and wads/). $path: maps a game file to its real path. */ public function scan(callable $path): void { $this->catalog->scan(array_map(fn (string $f) => $path($f), WadCatalog::FOLDERS)); } /** * Loads a WAD of the catalog (the one already loaded when $iwad is null); a mod (PWAD) is loaded over its * base game. $path: maps a game file to its real path. */ public function load(callable $path, SoundManager $sm, ?string $iwad = null): void { if ($this->catalog->ids() === []) { $this->scan($path); } $iwad ??= $this->iwad !== '' ? $this->iwad : 'freedoom1'; if ($this->catalog->file($iwad) === null || !$this->catalog->isFree($iwad)) { $iwad = $this->catalog->ids()[0] ?? throw new \RuntimeException('no WAD in assets/wads or wads/'); } if ($this->loaded && $this->iwad === $iwad) { return; } $file = $this->catalog->file($iwad); // a WAD of the server is in memory once downloaded (WadCatalog::download), a local one is read from its file $open = fn (string $id): Wad => ($data = $this->catalog->data($id)) !== null ? new Wad($data) : Wad::fromParts($this->catalog->file($id)->base); if ($this->catalog->isMod($iwad)) { $base = $this->catalog->baseFor($iwad) ?? throw new \RuntimeException("no base game for the mod $iwad"); $this->wad = $open($base); $this->wad->merge($open($iwad)); } else { $this->wad = $open($iwad); } $this->iwad = $iwad; $this->commercial = $this->wad->has('MAP01'); $maps = $file->maps() ?: $this->wad->maps(); $first = $maps[0] ?? ($this->commercial ? 'MAP01' : 'E1M1'); $this->firstMap = str_starts_with($first, 'MAP') ? [1, (int) substr($first, 3)] : [(int) $first[1], (int) $first[3]]; Strings::load($this->wad); $this->gfx = new Gfx($this->wad); $this->fb = new Framebuffer(320, 200); $this->view = new View($this->gfx, $this->fb); $this->world = new World($this->wad, $this->gfx); $this->world->commercial = $this->commercial; $this->statusBar = new StatusBar($this->gfx); $this->automap = new Automap(); $this->music = new Music($this->wad); $this->sounds = new Sounds($this->wad, $sm); $this->loaded = true; } /** Shows the screen buffer, stretched to the 4:3 view like on a CRT. */ public function present(Renderer $r, Sdl $sdl, int $palette = 0): void { $head = $this->bmpHeads[$palette] ??= $this->bmpHead($palette); $bmp = $head . $this->fb->pixels; $rw = \SDL_RWFromConstMem($bmp, strlen($bmp)); $surface = \SDL_LoadBMP_RW($rw, 1); if ($surface) { $sdl->createTextureFromSurface('dm-frame', $surface); \SDL_FreeSurface($surface); } if ($sdl->hasTexture('dm-frame')) { $r->copy($sdl->getTexture('dm-frame')['texture'], null, 0, 0, $r->getWidth(), $r->getHeight()); } } private function bmpHead(int $palette): string { $full = $this->fb->toBmp($this->gfx->palettes[$palette] ?? $this->gfx->palettes[0]); return substr($full, 0, strlen($full) - strlen($this->fb->pixels)); } /** Plays the sounds the level made this tic. */ public function playSounds(): void { $mo = $this->world->player->mo; $lx = $mo !== null ? $mo->x : 0.0; $ly = $mo !== null ? $mo->y : 0.0; foreach ($this->world->soundQueue as [$sfx, $x, $y]) { $this->sounds?->play($sfx, $x, $y, $lx, $ly); } $this->world->soundQueue = []; } /** The songs of Phase 2's maps (S_music of sounds.c, mus_runnin...). */ private const MUSIC_PHASE2 = ['RUNNIN', 'STALKS', 'COUNTD', 'BETWEE', 'DOOM', 'THE_DA', 'SHAWN', 'DDTBLU', 'IN_CIT', 'DEAD', 'STLKS2', 'THEDA2', 'DOOM2', 'DDTBL2', 'RUNNI2', 'DEAD2', 'STLKS3', 'ROMERO', 'SHAWN2', 'MESSAG', 'COUNT2', 'DDTBL3', 'AMPIE', 'THEDA3', 'ADRIAN', 'MESSG2', 'ROMER2', 'TENSE', 'SHAWN3', 'OPENIN', 'EVIL', 'ULTIMA']; /** S_ChangeMusic: 'title', 'intermission', 'finale' or a level. */ public function changeMusic(PcmStream $pcm, string $what, int $episode = 1, int $map = 1): void { $name = match ($what) { 'title' => $this->commercial ? 'D_DM2TTL' : 'D_INTRO', 'intermission' => $this->commercial ? 'D_DM2INT' : 'D_INTER', 'finale' => $this->commercial ? 'D_READ_M' : 'D_VICTOR', default => $this->commercial ? 'D_' . (self::MUSIC_PHASE2[$map - 1] ?? 'RUNNIN') : "D_E{$episode}M{$map}", }; if (!$this->wad->has($name) || $this->music->playing === $name) { return; } $pcm->clear(); $this->music->play($name, $this->wad->lump($name)); } /** Keeps the sound card a little ahead (called every frame). */ public function pumpMusic(PcmStream $pcm): void { if ($this->music->playing === '' || !$pcm->isAvailable()) { return; } for ($i = 0; $i < 4 && $pcm->buffered() < 0.25; $i++) { $pcm->push($this->music->render(1024), Music::RATE, 1); } } /** The level's name for the automap (HUSTR_* of DEHACKED). */ public function levelName(int $episode, int $map): string { return Strings::get($this->commercial ? "HUSTR_$map" : "HUSTR_E{$episode}M{$map}", $this->world->mapName($episode, $map)); } /** The sky texture of a level. */ public function skyName(int $episode, int $map): string { if ($this->commercial) { return $map < 12 ? 'SKY1' : ($map < 21 ? 'SKY2' : 'SKY3'); } return 'SKY' . max(1, min(4, $episode)); }}