TR
games/dmmotor / src / Dm / Automap.php · commit 9d248a4
<?php/* * dmmotor — the map of the level, drawn over the view (after am_map.c of linuxdoom-1.10): the walls seen so far * (all of them with the computer map), red for walls, brown for steps, yellow for ceiling changes, the player's * arrow; it follows the player and zooms. * Copyright (C) 1993-1996 by id Software, Inc.; PHP port for dmmotor. GPL v2 (see LICENSE). */namespace App\Dm;use App\Dm\Play\Player;use App\Dm\Play\World;use App\Dm\Render\Framebuffer;final class Automap{    private const WALLCOLOR = 176;    private const FDWALLCOLOR = 64;    private const CDWALLCOLOR = 231;    private const SECRETCOLOR = 176;    private const UNSEENCOLOR = 99;    private const PLAYERCOLOR = 209;    private const BACKGROUND = 0;    public bool $active = false;    /** map units per screen pixel */    private float $scale = 0.2;    /** the player's arrow (am_map.c player_arrow), in map units */    private const ARROW = [        [-14, 0, 16, 0], [16, 0, 10, 4], [16, 0, 10, -4], [-14, 0, -18, 4], [-14, 0, -18, -4],        [-10, 0, -14, 4], [-10, 0, -14, -4],    ];    public function reset(World $world): void    {        $this->active = false;        $this->scale = 0.2;    }    public function zoom(float $factor): void    {        $this->scale = max(0.02, min(1.5, $this->scale * $factor));    }    public function draw(Framebuffer $fb, World $world, int $height, string $name, Gfx $gfx): void    {        $fb->fillRect(0, 0, $fb->width, $height, self::BACKGROUND);        $p = $world->player;        $mo = $p->mo;        $cx = $fb->width / 2;        $cy = $height / 2;        $s = $this->scale;        $allMap = $p->powers[Player::PW_ALLMAP] > 0;        foreach ($world->map->lines as $line) {            $color = -1;            if ($line->flags & Line::ML_MAPPED) {                if ($line->flags & Line::ML_DONTDRAW) {                    continue;                }                if ($line->backSector === null) {                    $color = self::WALLCOLOR;                } elseif ($line->flags & Line::ML_SECRET) {                    $color = self::SECRETCOLOR;                } elseif ($line->backSector->floorHeight != $line->frontSector->floorHeight) {                    $color = self::FDWALLCOLOR;                } elseif ($line->backSector->ceilingHeight != $line->frontSector->ceilingHeight) {                    $color = self::CDWALLCOLOR;                }            } elseif ($allMap && !($line->flags & Line::ML_DONTDRAW)) {                $color = self::UNSEENCOLOR;            }            if ($color < 0) {                continue;            }            $this->line($fb, $height,                $cx + ($line->v1->x - $mo->x) * $s, $cy - ($line->v1->y - $mo->y) * $s,                $cx + ($line->v2->x - $mo->x) * $s, $cy - ($line->v2->y - $mo->y) * $s, $color);        }        // the arrow, turned with the player, at least 16 pixels long        $as = max($s, 0.5);        $cos = cos($mo->angle);        $sin = sin($mo->angle);        foreach (self::ARROW as [$x1, $y1, $x2, $y2]) {            $this->line($fb, $height,                $cx + ($x1 * $cos - $y1 * $sin) * $as, $cy - ($x1 * $sin + $y1 * $cos) * $as,                $cx + ($x2 * $cos - $y2 * $sin) * $as, $cy - ($x2 * $sin + $y2 * $cos) * $as, self::PLAYERCOLOR);        }        StatusBar::drawText($gfx, $fb, 0, $height - 10, $name);    }    /** A clipped line of one colour (Cohen-Sutherland + Bresenham, like AM_drawFline). */    private function line(Framebuffer $fb, int $height, float $x1, float $y1, float $x2, float $y2, int $color): void    {        $w = $fb->width;        $xmax = $w - 1;        $ymax = $height - 1;        $code = fn (float $x, float $y): int => ($x < 0 ? 1 : ($x > $xmax ? 2 : 0)) | ($y < 0 ? 4 : ($y > $ymax ? 8 : 0));        $c1 = $code($x1, $y1);        $c2 = $code($x2, $y2);        while ($c1 | $c2) {            if ($c1 & $c2) {                return;            }            $c = $c1 ?: $c2;            if ($c & 4) {                $x = $x1 + ($x2 - $x1) * (0 - $y1) / ($y2 - $y1);                $y = 0;            } elseif ($c & 8) {                $x = $x1 + ($x2 - $x1) * ($ymax - $y1) / ($y2 - $y1);                $y = $ymax;            } elseif ($c & 1) {                $y = $y1 + ($y2 - $y1) * (0 - $x1) / ($x2 - $x1);                $x = 0;            } else {                $y = $y1 + ($y2 - $y1) * ($xmax - $x1) / ($x2 - $x1);                $x = $xmax;            }            if ($c === $c1) {                [$x1, $y1, $c1] = [$x, $y, $code($x, $y)];            } else {                [$x2, $y2, $c2] = [$x, $y, $code($x, $y)];            }        }        $x1 = (int) round($x1);        $y1 = (int) round($y1);        $x2 = (int) round($x2);        $y2 = (int) round($y2);        $dx = abs($x2 - $x1);        $dy = -abs($y2 - $y1);        $sx = $x1 < $x2 ? 1 : -1;        $sy = $y1 < $y2 ? 1 : -1;        $err = $dx + $dy;        $ch = chr($color);        $px = &$fb->pixels;        while (true) {            $px[$y1 * $w + $x1] = $ch;            if ($x1 === $x2 && $y1 === $y2) {                break;            }            $e2 = 2 * $err;            if ($e2 >= $dy) {                $err += $dy;                $x1 += $sx;            }            if ($e2 <= $dx) {                $err += $dx;                $y1 += $sy;            }        }    }}