PL
games/dmmotor / src / Dm / MapData.php · commit 9d248a4
<?php/* * dmmotor — the map structures (r_defs.h) and their loading (p_setup.c of linuxdoom-1.10). * Copyright (C) 1993-1996 by id Software, Inc.; PHP port for dmmotor. GPL v2 (see LICENSE). * Coordinates are floats in map units (the original uses 16.16 fixed point). */namespace App\Dm;final class MapData{    public const BOXTOP = 0;    public const BOXBOTTOM = 1;    public const BOXLEFT = 2;    public const BOXRIGHT = 3;    /** @var list<Vertex> */    public array $vertexes = [];    /** @var list<Sector> */    public array $sectors = [];    /** @var list<Side> */    public array $sides = [];    /** @var list<Line> */    public array $lines = [];    /** @var list<Seg> */    public array $segs = [];    /** @var list<Subsector> */    public array $subsectors = [];    /** @var list<Node> */    public array $nodes = [];    /** @var list<MapThing> */    public array $things = [];    // blockmap (128 x 128 map unit blocks)    public float $bmapOrgX = 0;    public float $bmapOrgY = 0;    public int $bmapWidth = 0;    public int $bmapHeight = 0;    /** @var list<list<int>> line numbers of each block */    public array $blockLines = [];    /** @var array<int,?\App\Dm\Play\Mobj> first thing of each block (linked by bnext) */    public array $blockLinks = [];    public string $reject = '';    public function __construct(public readonly string $name)    {    }    public static function load(Wad $wad, Gfx $gfx, string $name): self    {        $map = new self($name);        $base = $wad->check($name);        if ($base < 0) {            throw new \RuntimeException("map $name not found");        }        $lump = fn (int $i) => $wad->lumpNum($base + $i);        // ML_THINGS 1, LINEDEFS 2, SIDEDEFS 3, VERTEXES 4, SEGS 5, SSECTORS 6, NODES 7, SECTORS 8, REJECT 9, BLOCKMAP 10        $map->loadVertexes($lump(4));        $map->loadSectors($lump(8), $gfx);        $map->loadSides($lump(3), $gfx);        $map->loadLines($lump(2));        $map->loadSubsectors($lump(6));        $map->loadNodes($lump(7));        $map->loadSegs($lump(5));        $map->loadThings($lump(1));        $map->reject = $lump(9);        $map->loadBlockmap($lump(10));        $map->groupLines();        return $map;    }    private function loadVertexes(string $d): void    {        $n = intdiv(strlen($d), 4);        $v = $n > 0 ? array_values(unpack('s' . ($n * 2), $d)) : [];        for ($i = 0; $i < $n; $i++) {            $this->vertexes[] = new Vertex((float) $v[$i * 2], (float) $v[$i * 2 + 1]);        }    }    private function loadSectors(string $d, Gfx $gfx): void    {        $n = intdiv(strlen($d), 26);        for ($i = 0; $i < $n; $i++) {            $r = unpack('sfloor/sceil/a8fpic/a8cpic/slight/sspecial/stag', $d, $i * 26);            $s = new Sector();            $s->id = $i;            $s->floorHeight = $r['floor'];            $s->ceilingHeight = $r['ceil'];            $s->floorPic = $gfx->flatNumber(rtrim($r['fpic'], "\0"));            $s->ceilingPic = $gfx->flatNumber(rtrim($r['cpic'], "\0"));            $s->lightLevel = $r['light'];            $s->special = $r['special'];            $s->tag = $r['tag'];            $this->sectors[] = $s;        }    }    private function loadSides(string $d, Gfx $gfx): void    {        $n = intdiv(strlen($d), 30);        for ($i = 0; $i < $n; $i++) {            $r = unpack('sxoff/syoff/a8top/a8bottom/a8mid/vsector', $d, $i * 30);            $side = new Side($this->sectors[$r['sector']] ?? $this->sectors[0]);            $side->textureOffset = $r['xoff'];            $side->rowOffset = $r['yoff'];            $side->topTexture = $gfx->textureNumber(rtrim($r['top'], "\0"));            $side->bottomTexture = $gfx->textureNumber(rtrim($r['bottom'], "\0"));            $side->midTexture = $gfx->textureNumber(rtrim($r['mid'], "\0"));            $this->sides[] = $side;        }    }    private function loadLines(string $d): void    {        $n = intdiv(strlen($d), 14);        for ($i = 0; $i < $n; $i++) {            $r = unpack('vv1/vv2/sflags/sspecial/stag/vs1/vs2', $d, $i * 14);            $v1 = $this->vertexes[$r['v1']];            $v2 = $this->vertexes[$r['v2']];            $l = new Line($v1, $v2);            $l->id = $i;            $l->flags = $r['flags'];            $l->special = $r['special'];            $l->tag = $r['tag'];            $l->dx = $v2->x - $v1->x;            $l->dy = $v2->y - $v1->y;            if ($l->dx == 0) {                $l->slopeType = Line::ST_VERTICAL;            } elseif ($l->dy == 0) {                $l->slopeType = Line::ST_HORIZONTAL;            } else {                $l->slopeType = ($l->dy / $l->dx > 0) ? Line::ST_POSITIVE : Line::ST_NEGATIVE;            }            $l->bbox = [max($v1->y, $v2->y), min($v1->y, $v2->y), min($v1->x, $v2->x), max($v1->x, $v2->x)];            $s1 = $r['s1'] === 0xFFFF ? -1 : $r['s1'];            $s2 = $r['s2'] === 0xFFFF ? -1 : $r['s2'];            $l->sidenum = [$s1, $s2];            $l->frontSector = $s1 >= 0 ? $this->sides[$s1]->sector : null;            $l->backSector = $s2 >= 0 ? $this->sides[$s2]->sector : null;            $this->lines[] = $l;        }    }    private function loadSubsectors(string $d): void    {        $n = intdiv(strlen($d), 4);        for ($i = 0; $i < $n; $i++) {            $r = unpack('vnum/vfirst', $d, $i * 4);            $this->subsectors[] = new Subsector($r['num'], $r['first']);        }    }    private function loadNodes(string $d): void    {        $n = intdiv(strlen($d), 28);        for ($i = 0; $i < $n; $i++) {            $r = array_values(unpack('s12a/v2b', $d, $i * 28));            $this->nodes[] = new Node(                $r[0], $r[1], $r[2], $r[3],                [[(float) $r[4], (float) $r[5], (float) $r[6], (float) $r[7]], [(float) $r[8], (float) $r[9], (float) $r[10], (float) $r[11]]],                [$r[12], $r[13]],            );        }    }    private function loadSegs(string $d): void    {        $n = intdiv(strlen($d), 12);        for ($i = 0; $i < $n; $i++) {            $r = unpack('vv1/vv2/sangle/vline/vside/soffset', $d, $i * 12);            $line = $this->lines[$r['line']];            $side = $this->sides[$line->sidenum[$r['side']]];            $seg = new Seg($this->vertexes[$r['v1']], $this->vertexes[$r['v2']], $side, $line, $side->sector);            $seg->angle = ($r['angle'] & 0xFFFF) / 65536 * 2 * M_PI;            $seg->offset = $r['offset'];            if ($line->flags & Line::ML_TWOSIDED) {                $other = $line->sidenum[$r['side'] ^ 1];                $seg->backSector = $other >= 0 ? $this->sides[$other]->sector : null;            }            $this->segs[] = $seg;        }        foreach ($this->subsectors as $ss) {            $ss->sector = $this->segs[$ss->firstLine]->side->sector;        }    }    private function loadThings(string $d): void    {        $n = intdiv(strlen($d), 10);        for ($i = 0; $i < $n; $i++) {            $r = unpack('sx/sy/sangle/stype/soptions', $d, $i * 10);            $this->things[] = new MapThing($r['x'], $r['y'], $r['angle'], $r['type'], $r['options']);        }    }    private function loadBlockmap(string $d): void    {        $words = array_values(unpack('s*', $d));        $this->bmapOrgX = $words[0];        $this->bmapOrgY = $words[1];        $this->bmapWidth = $words[2];        $this->bmapHeight = $words[3];        $count = $this->bmapWidth * $this->bmapHeight;        for ($i = 0; $i < $count; $i++) {            $offset = $words[4 + $i] & 0xFFFF;            $list = [];            // the list starts with a 0 and ends with -1            for ($j = $offset + 1; isset($words[$j]) && $words[$j] !== -1; $j++) {                $list[] = $words[$j] & 0xFFFF;            }            $this->blockLines[] = $list;            $this->blockLinks[] = null;        }    }    /** P_GroupLines: the lines of each sector, its bounding box and sound origin. */    private function groupLines(): void    {        foreach ($this->lines as $l) {            if ($l->frontSector !== null) {                $l->frontSector->lines[] = $l;            }            if ($l->backSector !== null && $l->backSector !== $l->frontSector) {                $l->backSector->lines[] = $l;            }        }        foreach ($this->sectors as $s) {            $box = [-INF, INF, INF, -INF];            foreach ($s->lines as $l) {                foreach ([$l->v1, $l->v2] as $v) {                    $box[0] = max($box[0], $v->y);                    $box[1] = min($box[1], $v->y);                    $box[2] = min($box[2], $v->x);                    $box[3] = max($box[3], $v->x);                }            }            if ($s->lines === []) {                $box = [0, 0, 0, 0];            }            $s->bbox = $box;            $s->soundOrgX = ($box[3] + $box[2]) / 2;            $s->soundOrgY = ($box[0] + $box[1]) / 2;        }    }    /** R_PointInSubsector: walks down the BSP tree. */    public function pointInSubsector(float $x, float $y): Subsector    {        if ($this->nodes === []) {            return $this->subsectors[0];        }        $num = count($this->nodes) - 1;        while (!($num & 0x8000)) {            $node = $this->nodes[$num];            $num = $node->children[self::pointOnSide($x, $y, $node)];        }        return $this->subsectors[$num & 0x7FFF];    }    /** R_PointOnSide: 0 = front (right), 1 = back. */    public static function pointOnSide(float $x, float $y, Node $node): int    {        if ($node->dx == 0) {            return $x <= $node->x ? ($node->dy > 0 ? 1 : 0) : ($node->dy < 0 ? 1 : 0);        }        if ($node->dy == 0) {            return $y <= $node->y ? ($node->dx < 0 ? 1 : 0) : ($node->dx > 0 ? 1 : 0);        }        $left = $node->dy * ($x - $node->x);        $right = ($y - $node->y) * $node->dx;        return $right < $left ? 0 : 1;    }}