IT
games/dmmotor / src / Dm / Play / Enemy.php · commit 9d248a4
<?php/* * dmmotor — the monsters' thinking: waking up, chasing, attacking, dying (p_enemy.c of linuxdoom-1.10). * Copyright (C) 1993-1996 by id Software, Inc.; PHP port for dmmotor. GPL v2 (see LICENSE). * * The A_* methods are called by the frames of Info::STATES (their names come from info.c). */namespace App\Dm\Play;use App\Dm\Info;use App\Dm\Line;use App\Dm\Sector;trait Enemy{    // directions: east, north-east, north, north-west, west, south-west, south, south-east, none    private const DI_NODIR = 8;    private const OPPOSITE = [4, 5, 6, 7, 0, 1, 2, 3, 8];    private const DIAGS = [3, 1, 5, 7];    private const XSPEED = [1, 0.71716, 0, -0.71716, -1, -0.71716, 0, 0.71716];    private const YSPEED = [0, 0.71716, 1, 0.71716, 0, -0.71716, -1, -0.71716];    private ?Mobj $soundTarget = null;    /** P_NoiseAlert: a gun shot wakes the monsters of the sectors the sound reaches. */    public function noiseAlert(Mobj $target, Mobj $emitter): void    {        $this->soundTarget = $target;        $this->validCount++;        $this->recursiveSound($emitter->subsector->sector, 0);    }    private function recursiveSound(Sector $sec, int $soundBlocks): void    {        if ($sec->validCount === $this->validCount && $sec->soundTraversed <= $soundBlocks + 1) {            return;   // already flooded        }        $sec->validCount = $this->validCount;        $sec->soundTraversed = $soundBlocks + 1;        $sec->soundTarget = $this->soundTarget;        foreach ($sec->lines as $check) {            if (!($check->flags & Line::ML_TWOSIDED)) {                continue;            }            $this->lineOpening($check);            if ($this->openRange <= 0) {                continue;   // a closed door            }            $other = $check->frontSector === $sec ? $check->backSector : $check->frontSector;            if ($other === null) {                continue;            }            if ($check->flags & Line::ML_SOUNDBLOCK) {                if (!$soundBlocks) {                    $this->recursiveSound($other, 1);                }            } else {                $this->recursiveSound($other, $soundBlocks);            }        }    }    private function checkMeleeRange(Mobj $actor): bool    {        $pl = $actor->target;        if ($pl === null) {            return false;        }        $dist = self::approxDistance($pl->x - $actor->x, $pl->y - $actor->y);        if ($dist >= self::MELEERANGE - 20 + $pl->info['radius']) {            return false;        }        return $this->checkSight($actor, $pl);    }    private function checkMissileRange(Mobj $actor): bool    {        if (!$this->checkSight($actor, $actor->target)) {            return false;        }        if ($actor->flags & Info::MF_JUSTHIT) {            // the target just hit the monster: fight back            $actor->flags &= ~Info::MF_JUSTHIT;            return true;        }        if ($actor->reactionTime) {            return false;   // do not attack yet        }        $dist = self::approxDistance($actor->x - $actor->target->x, $actor->y - $actor->target->y) - 64;        if (!$actor->info['meleestate']) {            $dist -= 128;   // no melee attack: fire more        }        $dist = (int) $dist;        if ($actor->type === Info::MT_UNDEAD) {            if ($dist < 196) {                return false;            }            $dist >>= 1;        }        if ($actor->type === Info::MT_CYBORG || $actor->type === Info::MT_SPIDER || $actor->type === Info::MT_SKULL) {            $dist >>= 1;        }        if ($dist > 200) {            $dist = 200;        }        if ($actor->type === Info::MT_CYBORG && $dist > 160) {            $dist = 160;        }        return Random::p() >= $dist;    }    /** P_Move: one step in its direction; opens doors on the way. */    private function monsterMove(Mobj $actor): bool    {        if ($actor->moveDir === self::DI_NODIR) {            return false;        }        $speed = $actor->info['speed'];        $tryX = $actor->x + $speed * self::XSPEED[$actor->moveDir];        $tryY = $actor->y + $speed * self::YSPEED[$actor->moveDir];        if (!$this->tryMove($actor, $tryX, $tryY)) {            if (($actor->flags & Info::MF_FLOAT) && $this->floatOk) {                // flying monsters go up or down to pass                $actor->z += $actor->z < $this->tmFloorZ ? self::FLOATSPEED : -self::FLOATSPEED;                $actor->flags |= Info::MF_INFLOAT;                return true;            }            if ($this->specHit === []) {                return false;            }            $actor->moveDir = self::DI_NODIR;            $good = false;            foreach (array_reverse($this->specHit) as $ld) {                if ($this->useSpecialLine($actor, $ld, 0)) {                    $good = true;   // it opened a door                }            }            return $good;        }        $actor->flags &= ~Info::MF_INFLOAT;        if (!($actor->flags & Info::MF_FLOAT)) {            $actor->z = $actor->floorZ;        }        return true;    }    private function tryWalk(Mobj $actor): bool    {        if (!$this->monsterMove($actor)) {            return false;        }        $actor->moveCount = Random::p() & 15;        return true;    }    private function newChaseDir(Mobj $actor): void    {        if ($actor->target === null) {            return;        }        $oldDir = $actor->moveDir;        $turnaround = self::OPPOSITE[$oldDir];        $dx = $actor->target->x - $actor->x;        $dy = $actor->target->y - $actor->y;        $d1 = $dx > 10 ? 0 : ($dx < -10 ? 4 : self::DI_NODIR);        $d2 = $dy < -10 ? 6 : ($dy > 10 ? 2 : self::DI_NODIR);        // try the direct route        if ($d1 !== self::DI_NODIR && $d2 !== self::DI_NODIR) {            $actor->moveDir = self::DIAGS[(($dy < 0) ? 2 : 0) + ($dx > 0 ? 1 : 0)];            if ($actor->moveDir !== $turnaround && $this->tryWalk($actor)) {                return;            }        }        // try the other directions        if (Random::p() > 200 || abs($dy) > abs($dx)) {            [$d1, $d2] = [$d2, $d1];        }        if ($d1 === $turnaround) {            $d1 = self::DI_NODIR;        }        if ($d2 === $turnaround) {            $d2 = self::DI_NODIR;        }        if ($d1 !== self::DI_NODIR) {            $actor->moveDir = $d1;            if ($this->tryWalk($actor)) {                return;            }        }        if ($d2 !== self::DI_NODIR) {            $actor->moveDir = $d2;            if ($this->tryWalk($actor)) {                return;            }        }        // there is no direct path to the player: pick another direction        if ($oldDir !== self::DI_NODIR) {            $actor->moveDir = $oldDir;            if ($this->tryWalk($actor)) {                return;            }        }        $order = (Random::p() & 1) ? [0, 1, 2, 3, 4, 5, 6, 7] : [7, 6, 5, 4, 3, 2, 1, 0];        foreach ($order as $tdir) {            if ($tdir !== $turnaround) {                $actor->moveDir = $tdir;                if ($this->tryWalk($actor)) {                    return;                }            }        }        if ($turnaround !== self::DI_NODIR) {            $actor->moveDir = $turnaround;            if ($this->tryWalk($actor)) {                return;            }        }        $actor->moveDir = self::DI_NODIR;   // cannot move    }    /** P_LookForPlayers (one player): true when the monster sees him (in front, or anywhere with $allAround). */    private function lookForPlayers(Mobj $actor, bool $allAround): bool    {        $player = $this->player;        if ($player->health <= 0 || $player->mo === null) {            return false;        }        if (!$this->checkSight($actor, $player->mo)) {            return false;        }        if (!$allAround) {            $an = self::normAngle(self::pointToAngle($actor->x, $actor->y, $player->mo->x, $player->mo->y) - $actor->angle);            if ($an > M_PI / 2 && $an < 3 * M_PI / 2) {                $dist = self::approxDistance($player->mo->x - $actor->x, $player->mo->y - $actor->y);                if ($dist > self::MELEERANGE) {                    return false;   // behind its back                }            }        }        $actor->target = $player->mo;        return true;    }    /** A random variant of the "I see you" sounds. */    private function seeSound(int $sound): int    {        $posit1 = $this->sfx('posit1');        $bgsit1 = $this->sfx('bgsit1');        if ($sound >= $posit1 && $sound <= $posit1 + 2) {            return $posit1 + Random::p() % 3;        }        if ($sound >= $bgsit1 && $sound <= $bgsit1 + 1) {            return $bgsit1 + Random::p() % 2;        }        return $sound;    }    // ---------------------------------------------------------------- actions    public function A_Look(Mobj $actor): void    {        $actor->threshold = 0;   // any shot will wake it up        $targ = $actor->subsector->sector->soundTarget;        $see = false;        if ($targ !== null && ($targ->flags & Info::MF_SHOOTABLE)) {            $actor->target = $targ;            $see = !($actor->flags & Info::MF_AMBUSH) || $this->checkSight($actor, $targ);        }        if (!$see && !$this->lookForPlayers($actor, false)) {            return;        }        if ($actor->info['seesound']) {            $sound = $this->seeSound($actor->info['seesound']);            $boss = $actor->type === Info::MT_SPIDER || $actor->type === Info::MT_CYBORG;            $this->startSound($boss ? null : $actor, $sound);        }        $this->setMobjState($actor, $actor->info['seestate']);    }    public function A_Chase(Mobj $actor): void    {        if ($actor->reactionTime) {            $actor->reactionTime--;        }        // modify the target threshold        if ($actor->threshold) {            if ($actor->target === null || $actor->target->health <= 0) {                $actor->threshold = 0;            } else {                $actor->threshold--;            }        }        // turn towards the movement direction        if ($actor->moveDir < 8) {            $eighth = M_PI / 4;            $actor->angle = floor(self::normAngle($actor->angle) / $eighth + 1e-9) * $eighth;            $delta = self::normAngle($actor->angle - $actor->moveDir * $eighth);            if ($delta > M_PI) {                $delta -= 2 * M_PI;            }            if ($delta > 1e-6) {                $actor->angle -= $eighth;            } elseif ($delta < -1e-6) {                $actor->angle += $eighth;            }        }        if ($actor->target === null || !($actor->target->flags & Info::MF_SHOOTABLE)) {            // look for a new target            if ($this->lookForPlayers($actor, true)) {                return;            }            $this->setMobjState($actor, $actor->info['spawnstate']);            return;        }        // do not attack twice in a row        if ($actor->flags & Info::MF_JUSTATTACKED) {            $actor->flags &= ~Info::MF_JUSTATTACKED;            if ($this->skill !== self::SK_NIGHTMARE) {                $this->newChaseDir($actor);            }            return;        }        // check for a melee attack        if ($actor->info['meleestate'] && $this->checkMeleeRange($actor)) {            $this->startSound($actor, $actor->info['attacksound']);            $this->setMobjState($actor, $actor->info['meleestate']);            return;        }        // check for a missile attack        if ($actor->info['missilestate']) {            if (!($this->skill < self::SK_NIGHTMARE && $actor->moveCount) && $this->checkMissileRange($actor)) {                $this->setMobjState($actor, $actor->info['missilestate']);                $actor->flags |= Info::MF_JUSTATTACKED;                return;            }        }        // chase towards the player        if (--$actor->moveCount < 0 || !$this->monsterMove($actor)) {            $this->newChaseDir($actor);        }        if ($actor->info['activesound'] && Random::p() < 3) {            $this->startSound($actor, $actor->info['activesound']);        }    }    public function A_FaceTarget(Mobj $actor): void    {        if ($actor->target === null) {            return;        }        $actor->flags &= ~Info::MF_AMBUSH;        $actor->angle = self::pointToAngle($actor->x, $actor->y, $actor->target->x, $actor->target->y);        if ($actor->target->flags & Info::MF_SHADOW) {            $actor->angle += (Random::p() - Random::p()) * (M_PI / 1024);   // << 21        }    }    public function A_PosAttack(Mobj $actor): void    {        if ($actor->target === null) {            return;        }        $this->A_FaceTarget($actor);        $angle = $actor->angle;        $slope = $this->aimLineAttack($actor, $angle, self::MISSILERANGE);        $this->startSound($actor, $this->sfx('pistol'));        $angle += (Random::p() - Random::p()) * (M_PI / 2048);        $damage = ((Random::p() % 5) + 1) * 3;        $this->lineAttack($actor, $angle, self::MISSILERANGE, $slope, $damage);    }    public function A_SPosAttack(Mobj $actor): void    {        if ($actor->target === null) {            return;        }        $this->startSound($actor, $this->sfx('shotgn'));        $this->A_FaceTarget($actor);        $bangle = $actor->angle;        $slope = $this->aimLineAttack($actor, $bangle, self::MISSILERANGE);        for ($i = 0; $i < 3; $i++) {            $angle = $bangle + (Random::p() - Random::p()) * (M_PI / 2048);            $damage = ((Random::p() % 5) + 1) * 3;            $this->lineAttack($actor, $angle, self::MISSILERANGE, $slope, $damage);        }    }    public function A_CPosAttack(Mobj $actor): void    {        if ($actor->target === null) {            return;        }        $this->startSound($actor, $this->sfx('shotgn'));        $this->A_FaceTarget($actor);        $bangle = $actor->angle;        $slope = $this->aimLineAttack($actor, $bangle, self::MISSILERANGE);        $angle = $bangle + (Random::p() - Random::p()) * (M_PI / 2048);        $damage = ((Random::p() % 5) + 1) * 3;        $this->lineAttack($actor, $angle, self::MISSILERANGE, $slope, $damage);    }    public function A_CPosRefire(Mobj $actor): void    {        $this->A_FaceTarget($actor);        if (Random::p() < 40) {            return;        }        if ($actor->target === null || $actor->target->health <= 0 || !$this->checkSight($actor, $actor->target)) {            $this->setMobjState($actor, $actor->info['seestate']);        }    }    public function A_SpidRefire(Mobj $actor): void    {        $this->A_FaceTarget($actor);        if (Random::p() < 10) {            return;        }        if ($actor->target === null || $actor->target->health <= 0 || !$this->checkSight($actor, $actor->target)) {            $this->setMobjState($actor, $actor->info['seestate']);        }    }    public function A_BspiAttack(Mobj $actor): void    {        if ($actor->target === null) {            return;        }        $this->A_FaceTarget($actor);        $this->spawnMissile($actor, $actor->target, Info::MT_ARACHPLAZ);    }    public function A_TroopAttack(Mobj $actor): void    {        if ($actor->target === null) {            return;        }        $this->A_FaceTarget($actor);        if ($this->checkMeleeRange($actor)) {            $this->startSound($actor, $this->sfx('claw'));            $damage = (Random::p() % 8 + 1) * 3;            $this->damageMobj($actor->target, $actor, $actor, $damage);            return;        }        $this->spawnMissile($actor, $actor->target, Info::MT_TROOPSHOT);    }    public function A_SargAttack(Mobj $actor): void    {        if ($actor->target === null) {            return;        }        $this->A_FaceTarget($actor);        if ($this->checkMeleeRange($actor)) {            $damage = ((Random::p() % 10) + 1) * 4;            $this->damageMobj($actor->target, $actor, $actor, $damage);        }    }    public function A_HeadAttack(Mobj $actor): void    {        if ($actor->target === null) {            return;        }        $this->A_FaceTarget($actor);        if ($this->checkMeleeRange($actor)) {            $damage = (Random::p() % 6 + 1) * 10;            $this->damageMobj($actor->target, $actor, $actor, $damage);            return;        }        $this->spawnMissile($actor, $actor->target, Info::MT_HEADSHOT);    }    public function A_CyberAttack(Mobj $actor): void    {        if ($actor->target === null) {            return;        }        $this->A_FaceTarget($actor);        $this->spawnMissile($actor, $actor->target, Info::MT_ROCKET);    }    public function A_BruisAttack(Mobj $actor): void    {        if ($actor->target === null) {            return;        }        if ($this->checkMeleeRange($actor)) {            $this->startSound($actor, $this->sfx('claw'));            $damage = (Random::p() % 8 + 1) * 10;            $this->damageMobj($actor->target, $actor, $actor, $damage);            return;        }        $this->spawnMissile($actor, $actor->target, Info::MT_BRUISERSHOT);    }    public function A_SkelMissile(Mobj $actor): void    {        if ($actor->target === null) {            return;        }        $this->A_FaceTarget($actor);        $actor->z += 16;        $mo = $this->spawnMissile($actor, $actor->target, Info::MT_TRACER);        $actor->z -= 16;        $mo->x += $mo->momX;        $mo->y += $mo->momY;        $mo->tracer = $actor->target;    }    public function A_Tracer(Mobj $actor): void    {        if ($this->levelTime & 3) {            return;        }        $this->spawnPuff($actor->x, $actor->y, $actor->z);        $th = $this->spawnMobj($actor->x - $actor->momX, $actor->y - $actor->momY, $actor->z, Info::MT_SMOKE);        $th->momZ = 1;        $th->tics = max(1, $th->tics - (Random::p() & 3));        $dest = $actor->tracer;        if ($dest === null || $dest->health <= 0) {            return;        }        $exact = self::pointToAngle($actor->x, $actor->y, $dest->x, $dest->y);        $turn = M_PI * 2 * 0xC000000 / 4294967296;        $delta = self::normAngle($exact - $actor->angle);        if ($delta > M_PI) {            $actor->angle = $delta - 2 * M_PI < -$turn ? $actor->angle - $turn : $exact;        } else {            $actor->angle = $delta > $turn ? $actor->angle + $turn : $exact;        }        $speed = (float) $actor->info['speed'];        $actor->momX = $speed * cos($actor->angle);        $actor->momY = $speed * sin($actor->angle);        $dist = max(1, (int) (self::approxDistance($dest->x - $actor->x, $dest->y - $actor->y) / $speed));        $slope = ($dest->z + 40 - $actor->z) / $dist;        $actor->momZ += $slope < $actor->momZ ? -0.125 : 0.125;    }    public function A_SkelWhoosh(Mobj $actor): void    {        if ($actor->target === null) {            return;        }        $this->A_FaceTarget($actor);        $this->startSound($actor, $this->sfx('skeswg'));    }    public function A_SkelFist(Mobj $actor): void    {        if ($actor->target === null) {            return;        }        $this->A_FaceTarget($actor);        if ($this->checkMeleeRange($actor)) {            $damage = ((Random::p() % 10) + 1) * 6;            $this->startSound($actor, $this->sfx('skepch'));            $this->damageMobj($actor->target, $actor, $actor, $damage);        }    }    public function A_SkullAttack(Mobj $actor): void    {        if ($actor->target === null) {            return;        }        $dest = $actor->target;        $actor->flags |= Info::MF_SKULLFLY;        $this->startSound($actor, $actor->info['attacksound']);        $this->A_FaceTarget($actor);        $speed = 20.0;        $actor->momX = $speed * cos($actor->angle);        $actor->momY = $speed * sin($actor->angle);        $dist = max(1, (int) (self::approxDistance($dest->x - $actor->x, $dest->y - $actor->y) / $speed));        $actor->momZ = ($dest->z + $dest->height / 2 - $actor->z) / $dist;    }    public function A_Scream(Mobj $actor): void    {        $sound = $actor->info['deathsound'];        if (!$sound) {            return;        }        $podth1 = $this->sfx('podth1');        $bgdth1 = $this->sfx('bgdth1');        if ($sound >= $podth1 && $sound <= $podth1 + 2) {            $sound = $podth1 + Random::p() % 3;        } elseif ($sound >= $bgdth1 && $sound <= $bgdth1 + 1) {            $sound = $bgdth1 + Random::p() % 2;        }        $boss = $actor->type === Info::MT_SPIDER || $actor->type === Info::MT_CYBORG;        $this->startSound($boss ? null : $actor, $sound);    }    public function A_XScream(Mobj $actor): void    {        $this->startSound($actor, $this->sfx('slop'));    }    public function A_Pain(Mobj $actor): void    {        $this->startSound($actor, $actor->info['painsound']);    }    public function A_Fall(Mobj $actor): void    {        $actor->flags &= ~Info::MF_SOLID;   // the body can be walked over    }    public function A_Explode(Mobj $thing): void    {        $this->radiusAttack($thing, $thing->target, 128);    }    /** The end of a boss level: E1M8 lowers the floors tagged 666, E2M8 / E3M8 end the episode, MAP07 opens. */    public function A_BossDeath(Mobj $mo): void    {        $ep = $this->episode;        $map = $this->mapNum;        if ($this->commercial) {            $ok = $map === 7 && ($mo->type === Info::MT_FATSO || $mo->type === Info::MT_BABY);        } else {            $ok = match ($ep) {                1 => $map === 8 && $mo->type === Info::MT_BRUISER,                2 => $map === 8 && $mo->type === Info::MT_CYBORG,                3 => $map === 8 && $mo->type === Info::MT_SPIDER,                4 => ($map === 6 && $mo->type === Info::MT_CYBORG) || ($map === 8 && $mo->type === Info::MT_SPIDER),                default => $map === 8,            };        }        if (!$ok || $this->player->health <= 0) {            return;        }        foreach ($this->thinkers as $t) {            if ($t instanceof Mobj && !$t->removed && $t !== $mo && $t->type === $mo->type && $t->health > 0) {                return;   // another boss is still alive            }        }        if ($this->commercial) {            if ($mo->type === Info::MT_FATSO) {                $this->doFloorByTag(666, FloorMove::LOWER_TO_LOWEST);            } else {                $this->doFloorByTag(667, FloorMove::RAISE_TO_TEXTURE);            }            return;        }        if ($ep === 1 || ($ep === 4 && $map === 8)) {            $this->doFloorByTag(666, FloorMove::LOWER_TO_LOWEST);            return;        }        if ($ep === 4 && $map === 6) {            $this->doDoorByTag(666, Door::BLAZE_OPEN);            return;        }        $this->exitLevel();    }    /** A_KeenDie: when all the hanging captives are dead, the doors tagged 666 open. */    public function A_KeenDie(Mobj $mo): void    {        $this->A_Fall($mo);        foreach ($this->thinkers as $t) {            if ($t instanceof Mobj && !$t->removed && $t !== $mo && $t->type === $mo->type && $t->health > 0) {                return;            }        }        $this->doDoorByTag(666, Door::OPEN);    }    // ---------------------------------------------------------------- the monsters of Phase 2    private ?Mobj $corpseHit = null;    private float $vileTryX = 0;    private float $vileTryY = 0;    /** A_VileChase: raises a corpse it walks over, else chases. */    public function A_VileChase(Mobj $actor): void    {        if ($actor->moveDir !== self::DI_NODIR) {            $speed = $actor->info['speed'];            $this->vileTryX = $actor->x + $speed * self::XSPEED[$actor->moveDir];            $this->vileTryY = $actor->y + $speed * self::YSPEED[$actor->moveDir];            [$xl, $yl] = $this->blockOf($this->vileTryX - self::MAXRADIUS * 2, $this->vileTryY - self::MAXRADIUS * 2);            [$xh, $yh] = $this->blockOf($this->vileTryX + self::MAXRADIUS * 2, $this->vileTryY + self::MAXRADIUS * 2);            for ($bx = $xl; $bx <= $xh; $bx++) {                for ($by = $yl; $by <= $yh; $by++) {                    $this->corpseHit = null;                    if ($this->blockThingsIterator($bx, $by, fn (Mobj $th) => $this->vileCheck($th))) {                        continue;                    }                    $corpse = $this->foundCorpse();                    if ($corpse === null) {                        continue;                    }                    // got one: raise it                    $temp = $actor->target;                    $actor->target = $corpse;                    $this->A_FaceTarget($actor);                    $actor->target = $temp;                    $this->setMobjState($actor, Info::S_VILE_HEAL1);                    $this->startSound($corpse, $this->sfx('slop'));                    $info = $corpse->info;                    $this->setMobjState($corpse, $info['raisestate']);                    $corpse->height *= 4;                    $corpse->flags = $info['flags'];                    $corpse->health = $info['spawnhealth'];                    $corpse->target = null;                    return;                }            }        }        $this->A_Chase($actor);    }    /** the corpse vileCheck() found (set inside the blockmap callback) */    private function foundCorpse(): ?Mobj    {        return $this->corpseHit;    }    private function vileCheck(Mobj $thing): bool    {        if (!($thing->flags & Info::MF_CORPSE) || $thing->tics !== -1 || $thing->info['raisestate'] === Info::S_NULL) {            return true;        }        $maxDist = $thing->info['radius'] + Info::MOBJINFO[Info::MT_VILE]['radius'];        if (abs($thing->x - $this->vileTryX) > $maxDist || abs($thing->y - $this->vileTryY) > $maxDist) {            return true;   // not touching        }        $thing->momX = $thing->momY = 0;        $thing->height *= 4;        $fits = $this->checkPosition($thing, $thing->x, $thing->y);        $thing->height /= 4;        if (!$fits) {            return true;        }        $this->corpseHit = $thing;        return false;    }    public function A_VileStart(Mobj $actor): void    {        $this->startSound($actor, $this->sfx('vilatk'));    }    public function A_StartFire(Mobj $actor): void    {        $this->startSound($actor, $this->sfx('flamst'));        $this->A_Fire($actor);    }    public function A_FireCrackle(Mobj $actor): void    {        $this->startSound($actor, $this->sfx('flame'));        $this->A_Fire($actor);    }    /** The arch-vile's fire stays in front of its victim. */    public function A_Fire(Mobj $actor): void    {        $dest = $actor->tracer;        if ($dest === null || $actor->target === null || !$this->checkSight($actor->target, $dest)) {            return;        }        $this->unsetThingPosition($actor);        $actor->x = $dest->x + 24 * cos($dest->angle);        $actor->y = $dest->y + 24 * sin($dest->angle);        $actor->z = $dest->z;        $this->setThingPosition($actor);    }    public function A_VileTarget(Mobj $actor): void    {        if ($actor->target === null) {            return;        }        $this->A_FaceTarget($actor);        // (the original spawns it at target x, target x: a typo, harmless since A_Fire moves it at once)        $fog = $this->spawnMobj($actor->target->x, $actor->target->y, $actor->target->z, Info::MT_FIRE);        $actor->tracer = $fog;        $fog->target = $actor;        $fog->tracer = $actor->target;        $this->A_Fire($fog);    }    public function A_VileAttack(Mobj $actor): void    {        $target = $actor->target;        if ($target === null) {            return;        }        $this->A_FaceTarget($actor);        if (!$this->checkSight($actor, $target)) {            return;        }        $this->startSound($actor, $this->sfx('barexp'));        $this->damageMobj($target, $actor, $actor, 20);        $target->momZ = 1000 / max(1, $target->info['mass']);        $fire = $actor->tracer;        if ($fire === null) {            return;        }        // move the fire between the vile and the player        $fire->x = $target->x - 24 * cos($actor->angle);        $fire->y = $target->y - 24 * sin($actor->angle);        $this->radiusAttack($fire, $actor, 70);    }    private const FATSPREAD = M_PI / 2 / 8;    public function A_FatRaise(Mobj $actor): void    {        $this->A_FaceTarget($actor);        $this->startSound($actor, $this->sfx('manatk'));    }    private function fatShot(Mobj $actor, float $turn): void    {        $mo = $this->spawnMissile($actor, $actor->target, Info::MT_FATSHOT);        $mo->angle += $turn;        $speed = (float) $mo->info['speed'];        $mo->momX = $speed * cos($mo->angle);        $mo->momY = $speed * sin($mo->angle);    }    public function A_FatAttack1(Mobj $actor): void    {        if ($actor->target === null) {            return;        }        $this->A_FaceTarget($actor);        $actor->angle += self::FATSPREAD;        $this->spawnMissile($actor, $actor->target, Info::MT_FATSHOT);        $this->fatShot($actor, self::FATSPREAD);    }    public function A_FatAttack2(Mobj $actor): void    {        if ($actor->target === null) {            return;        }        $this->A_FaceTarget($actor);        $actor->angle -= self::FATSPREAD;        $this->spawnMissile($actor, $actor->target, Info::MT_FATSHOT);        $this->fatShot($actor, -self::FATSPREAD * 2);    }    public function A_FatAttack3(Mobj $actor): void    {        if ($actor->target === null) {            return;        }        $this->A_FaceTarget($actor);        $this->fatShot($actor, -self::FATSPREAD / 2);        $this->fatShot($actor, self::FATSPREAD / 2);    }    /** A_PainShootSkull: a pain elemental spits a lost soul (at most 20 in the level). */    private function painShootSkull(Mobj $actor, float $angle): void    {        $count = 0;        foreach ($this->thinkers as $t) {            if ($t instanceof Mobj && !$t->removed && $t->type === Info::MT_SKULL) {                $count++;            }        }        if ($count > 20) {            return;        }        $prestep = 4 + 3 * ($actor->info['radius'] + Info::MOBJINFO[Info::MT_SKULL]['radius']) / 2;        $skull = $this->spawnMobj($actor->x + $prestep * cos($angle), $actor->y + $prestep * sin($angle), $actor->z + 8, Info::MT_SKULL);        if (!$this->tryMove($skull, $skull->x, $skull->y)) {            $this->damageMobj($skull, $actor, $actor, 10000);   // spawned in a wall            return;        }        $skull->target = $actor->target;        $this->A_SkullAttack($skull);    }    public function A_PainAttack(Mobj $actor): void    {        if ($actor->target === null) {            return;        }        $this->A_FaceTarget($actor);        $this->painShootSkull($actor, $actor->angle);    }    public function A_PainDie(Mobj $actor): void    {        $this->A_Fall($actor);        $this->painShootSkull($actor, $actor->angle + M_PI / 2);        $this->painShootSkull($actor, $actor->angle + M_PI);        $this->painShootSkull($actor, $actor->angle + 3 * M_PI / 2);    }    // ---------------------------------------------------------------- the last boss (MAP30)    /** @var list<Mobj> */    private array $brainTargets = [];    private int $brainTargetOn = 0;    private bool $brainEasy = false;    public function A_BrainAwake(Mobj $mo): void    {        $this->brainTargets = [];        $this->brainTargetOn = 0;        foreach ($this->thinkers as $t) {            if ($t instanceof Mobj && !$t->removed && $t->type === Info::MT_BOSSTARGET) {                $this->brainTargets[] = $t;            }        }        $this->startSound(null, $this->sfx('bossit'));    }    public function A_BrainPain(Mobj $mo): void    {        $this->startSound(null, $this->sfx('bospn'));    }    private function brainRocket(float $x, float $y): void    {        $th = $this->spawnMobj($x, $y, Random::p() * 2, Info::MT_ROCKET);        $th->momZ = Random::p() / 128;        $this->setMobjState($th, Info::S_BRAINEXPLODE1);        $th->tics = max(1, $th->tics - (Random::p() & 7));    }    public function A_BrainScream(Mobj $mo): void    {        for ($x = $mo->x - 196; $x < $mo->x + 320; $x += 8) {            $this->brainRocket($x, $mo->y - 320);        }        $this->startSound(null, $this->sfx('bosdth'));    }    public function A_BrainExplode(Mobj $mo): void    {        $this->brainRocket($mo->x + (Random::p() - Random::p()) / 32, $mo->y);    }    public function A_BrainDie(Mobj $mo): void    {        $this->exitLevel();    }    public function A_BrainSpit(Mobj $mo): void    {        $this->brainEasy = !$this->brainEasy;        if ($this->skill <= self::SK_EASY && !$this->brainEasy) {            return;        }        if ($this->brainTargets === []) {            return;        }        $targ = $this->brainTargets[$this->brainTargetOn];        $this->brainTargetOn = ($this->brainTargetOn + 1) % count($this->brainTargets);        $cube = $this->spawnMissile($mo, $targ, Info::MT_SPAWNSHOT);        $cube->target = $targ;        $tics = max(1, Info::STATES[$cube->state][2]);        $cube->reactionTime = $cube->momY != 0 ? (int) (($targ->y - $mo->y) / $cube->momY / $tics) : 1;        $this->startSound(null, $this->sfx('bospit'));    }    public function A_SpawnSound(Mobj $mo): void    {        $this->startSound($mo, $this->sfx('boscub'));        $this->A_SpawnFly($mo);    }    /** The cube lands: a random monster appears in a burst of fire. */    public function A_SpawnFly(Mobj $mo): void    {        if (--$mo->reactionTime > 0) {            return;   // still flying        }        $targ = $mo->target;        if ($targ === null) {            $this->removeMobj($mo);            return;        }        $fog = $this->spawnMobj($targ->x, $targ->y, $targ->z, Info::MT_SPAWNFIRE);        $this->startSound($fog, $this->sfx('telept'));        $r = Random::p();        $type = match (true) {            $r < 50 => Info::MT_TROOP,            $r < 90 => Info::MT_SERGEANT,            $r < 120 => Info::MT_SHADOWS,            $r < 130 => Info::MT_PAIN,            $r < 160 => Info::MT_HEAD,            $r < 162 => Info::MT_VILE,            $r < 172 => Info::MT_UNDEAD,            $r < 192 => Info::MT_BABY,            $r < 222 => Info::MT_FATSO,            $r < 246 => Info::MT_KNIGHT,            default => Info::MT_BRUISER,        };        $new = $this->spawnMobj($targ->x, $targ->y, $targ->z, $type);        if ($this->lookForPlayers($new, true)) {            $this->setMobjState($new, $new->info['seestate']);        }        $this->teleportMove($new, $new->x, $new->y);        $this->removeMobj($mo);    }    public function A_Hoof(Mobj $mo): void    {        $this->startSound($mo, $this->sfx('hoof'));        $this->A_Chase($mo);    }    public function A_Metal(Mobj $mo): void    {        $this->startSound($mo, $this->sfx('metal'));        $this->A_Chase($mo);    }    public function A_BabyMetal(Mobj $mo): void    {        $this->startSound($mo, $this->sfx('bspwlk'));        $this->A_Chase($mo);    }    public function A_PlayerScream(Mobj $mo): void    {        $this->startSound($mo, $this->sfx($this->commercial && $mo->health < -50 ? 'pdiehi' : 'pldeth'));    }}