NL
games/dmmotor / src / Dm / Play / Weapons.php · commit 9d248a4
<?php/* * dmmotor — the player's weapons on the screen and their shots (p_pspr.c of linuxdoom-1.10). * Copyright (C) 1993-1996 by id Software, Inc.; PHP port for dmmotor. GPL v2 (see LICENSE). */namespace App\Dm\Play;use App\Dm\Info;trait Weapons{    private const LOWERSPEED = 6.0;    private const RAISESPEED = 6.0;    private const WEAPONBOTTOM = 128.0;    private const WEAPONTOP = 32.0;    private const BFGCELLS = 40;    private float $bulletSlope = 0;    public function setPsprite(Player $player, int $position, int $state): void    {        $psp = $player->psprites[$position];        do {            if ($state === Info::S_NULL) {                $psp->state = -1;   // object removed itself                break;            }            [, , $tics, $action, $next] = Info::STATES[$state];            $psp->state = $state;            $psp->tics = $tics;            if ($action !== null && method_exists($this, $action)) {                $this->$action($player, $psp);                if ($psp->state === -1) {                    break;                }            }            $state = Info::STATES[$psp->state][4];        } while ($psp->tics === 0);    }    private function bringUpWeapon(Player $player): void    {        if ($player->pendingWeapon === Player::WP_NOCHANGE) {            $player->pendingWeapon = $player->readyWeapon;        }        if ($player->pendingWeapon === Player::WP_CHAINSAW) {            $this->startSound($player->mo, $this->sfx('sawup'));        }        $newState = Player::WEAPONINFO[$player->pendingWeapon][1];        $player->pendingWeapon = Player::WP_NOCHANGE;        $player->psprites[Player::PS_WEAPON]->sy = self::WEAPONBOTTOM;        $this->setPsprite($player, Player::PS_WEAPON, $newState);    }    /** P_CheckAmmo: false (and a switch to another weapon) when the ready weapon is out of ammo. */    private function checkAmmo(Player $player): bool    {        $ammo = Player::WEAPONINFO[$player->readyWeapon][0];        $count = match ($player->readyWeapon) {            Player::WP_BFG => self::BFGCELLS,            Player::WP_SUPERSHOTGUN => 2,            default => 1,        };        if ($ammo === Player::AM_NOAMMO || $player->ammo[$ammo] >= $count) {            return true;        }        $w = $player->weaponOwned;        $a = $player->ammo;        $player->pendingWeapon = match (true) {            $w[Player::WP_PLASMA] && $a[Player::AM_CELL] > 0 => Player::WP_PLASMA,            $this->commercial && $w[Player::WP_SUPERSHOTGUN] && $a[Player::AM_SHELL] > 2 => Player::WP_SUPERSHOTGUN,            $w[Player::WP_CHAINGUN] && $a[Player::AM_CLIP] > 0 => Player::WP_CHAINGUN,            $w[Player::WP_SHOTGUN] && $a[Player::AM_SHELL] > 0 => Player::WP_SHOTGUN,            $a[Player::AM_CLIP] > 0 => Player::WP_PISTOL,            $w[Player::WP_CHAINSAW] => Player::WP_CHAINSAW,            $w[Player::WP_MISSILE] && $a[Player::AM_MISL] > 0 => Player::WP_MISSILE,            $w[Player::WP_BFG] && $a[Player::AM_CELL] > 40 => Player::WP_BFG,            default => Player::WP_FIST,        };        $this->setPsprite($player, Player::PS_WEAPON, Player::WEAPONINFO[$player->readyWeapon][2]);        return false;    }    private function fireWeapon(Player $player): void    {        if (!$this->checkAmmo($player)) {            return;        }        $this->setMobjState($player->mo, Info::S_PLAY_ATK1);        $this->setPsprite($player, Player::PS_WEAPON, Player::WEAPONINFO[$player->readyWeapon][4]);        $this->noiseAlert($player->mo, $player->mo);    }    /** P_DropWeapon: the player died. */    public function dropWeapon(Player $player): void    {        $this->setPsprite($player, Player::PS_WEAPON, Player::WEAPONINFO[$player->readyWeapon][2]);    }    public function A_WeaponReady(Player $player, PSprite $psp): void    {        $mo = $player->mo;        if ($mo->state === Info::S_PLAY_ATK1 || $mo->state === Info::S_PLAY_ATK2) {            $this->setMobjState($mo, Info::S_PLAY);        }        if ($player->readyWeapon === Player::WP_CHAINSAW && $psp->state === Info::S_SAW) {            $this->startSound($mo, $this->sfx('sawidl'));        }        // change weapon (pending weapon should already be validated)        if ($player->pendingWeapon !== Player::WP_NOCHANGE || !$player->health) {            $this->setPsprite($player, Player::PS_WEAPON, Player::WEAPONINFO[$player->readyWeapon][2]);            return;        }        if ($player->cmd->attack) {            if (!$player->attackDown || ($player->readyWeapon !== Player::WP_MISSILE && $player->readyWeapon !== Player::WP_BFG)) {                $player->attackDown = true;                $this->fireWeapon($player);                return;            }        } else {            $player->attackDown = false;        }        // bob the weapon with the movement        $angle = (128 * $this->levelTime) % 8192 * (2 * M_PI / 8192);        $psp->sx = 1 + $player->bob * cos($angle);        $angle = ((128 * $this->levelTime) % 4096) * (2 * M_PI / 8192);        $psp->sy = self::WEAPONTOP + $player->bob * sin($angle);    }    public function A_ReFire(Player $player, PSprite $psp): void    {        if ($player->cmd->attack && $player->pendingWeapon === Player::WP_NOCHANGE && $player->health) {            $player->refire++;            $this->fireWeapon($player);        } else {            $player->refire = 0;            $this->checkAmmo($player);        }    }    public function A_CheckReload(Player $player, PSprite $psp): void    {        $this->checkAmmo($player);    }    public function A_Lower(Player $player, PSprite $psp): void    {        $psp->sy += self::LOWERSPEED;        if ($psp->sy < self::WEAPONBOTTOM) {            return;        }        if ($player->playerState === Player::DEAD) {            $psp->sy = self::WEAPONBOTTOM;            return;        }        if (!$player->health) {            $this->setPsprite($player, Player::PS_WEAPON, Info::S_NULL);            return;        }        $player->readyWeapon = $player->pendingWeapon;        $this->bringUpWeapon($player);    }    public function A_Raise(Player $player, PSprite $psp): void    {        $psp->sy -= self::RAISESPEED;        if ($psp->sy > self::WEAPONTOP) {            return;        }        $psp->sy = self::WEAPONTOP;        $this->setPsprite($player, Player::PS_WEAPON, Player::WEAPONINFO[$player->readyWeapon][3]);    }    public function A_GunFlash(Player $player, PSprite $psp): void    {        $this->setMobjState($player->mo, Info::S_PLAY_ATK2);        $this->setPsprite($player, Player::PS_FLASH, Player::WEAPONINFO[$player->readyWeapon][5]);    }    public function A_Punch(Player $player, PSprite $psp): void    {        $damage = (Random::p() % 10 + 1) << 1;        if ($player->powers[Player::PW_STRENGTH]) {            $damage *= 10;        }        $mo = $player->mo;        $angle = $mo->angle + (Random::p() - Random::p()) * (M_PI / 8192);        $slope = $this->aimLineAttack($mo, $angle, self::MELEERANGE);        $this->lineAttack($mo, $angle, self::MELEERANGE, $slope, $damage);        if (($t = $this->aimed()) !== null) {            $this->startSound($mo, $this->sfx('punch'));            $mo->angle = self::pointToAngle($mo->x, $mo->y, $t->x, $t->y);        }    }    public function A_Saw(Player $player, PSprite $psp): void    {        $damage = 2 * (Random::p() % 10 + 1);        $mo = $player->mo;        $angle = $mo->angle + (Random::p() - Random::p()) * (M_PI / 8192);        $slope = $this->aimLineAttack($mo, $angle, self::MELEERANGE + 1);        $this->lineAttack($mo, $angle, self::MELEERANGE + 1, $slope, $damage);        $t = $this->aimed();        if ($t === null) {            $this->startSound($mo, $this->sfx('sawful'));            return;        }        $this->startSound($mo, $this->sfx('sawhit'));        // turn to face the target        $angle = self::pointToAngle($mo->x, $mo->y, $t->x, $t->y);        $delta = self::normAngle($angle - $mo->angle);        $step20 = M_PI / 2 / 20;        $step21 = M_PI / 2 / 21;        if ($delta > M_PI) {            $mo->angle = $delta - 2 * M_PI < -$step20 ? $angle + $step21 : $mo->angle - $step20;        } else {            $mo->angle = $delta > $step20 ? $angle - $step21 : $mo->angle + $step20;        }        $mo->flags |= Info::MF_JUSTATTACKED;    }    public function A_FireMissile(Player $player, PSprite $psp): void    {        $player->ammo[Player::WEAPONINFO[$player->readyWeapon][0]]--;        $this->spawnPlayerMissile($player->mo, Info::MT_ROCKET);    }    public function A_FireBFG(Player $player, PSprite $psp): void    {        $player->ammo[Player::WEAPONINFO[$player->readyWeapon][0]] -= self::BFGCELLS;        $this->spawnPlayerMissile($player->mo, Info::MT_BFG);    }    public function A_FirePlasma(Player $player, PSprite $psp): void    {        $player->ammo[Player::WEAPONINFO[$player->readyWeapon][0]]--;        $this->setPsprite($player, Player::PS_FLASH, Player::WEAPONINFO[$player->readyWeapon][5] + (Random::p() & 1));        $this->spawnPlayerMissile($player->mo, Info::MT_PLASMA);    }    /** P_BulletSlope: auto-aim up and down at what is in front (a little to the sides too). */    private function bulletSlope(Mobj $mo): void    {        $an = $mo->angle;        $this->bulletSlope = $this->aimLineAttack($mo, $an, 16 * 64);        if ($this->aimed() === null) {            $an += self::ANG_SPREAD;            $this->bulletSlope = $this->aimLineAttack($mo, $an, 16 * 64);            if ($this->aimed() === null) {                $an -= 2 * self::ANG_SPREAD;                $this->bulletSlope = $this->aimLineAttack($mo, $an, 16 * 64);            }        }    }    private function gunShot(Mobj $mo, bool $accurate): void    {        $damage = 5 * (Random::p() % 3 + 1);        $angle = $mo->angle;        if (!$accurate) {            $angle += (Random::p() - Random::p()) * (M_PI / 8192);        }        $this->lineAttack($mo, $angle, self::MISSILERANGE, $this->bulletSlope, $damage);    }    public function A_FirePistol(Player $player, PSprite $psp): void    {        $this->startSound($player->mo, $this->sfx('pistol'));        $this->setMobjState($player->mo, Info::S_PLAY_ATK2);        $player->ammo[Player::WEAPONINFO[$player->readyWeapon][0]]--;        $this->setPsprite($player, Player::PS_FLASH, Player::WEAPONINFO[$player->readyWeapon][5]);        $this->bulletSlope($player->mo);        $this->gunShot($player->mo, !$player->refire);    }    public function A_FireShotgun(Player $player, PSprite $psp): void    {        $this->startSound($player->mo, $this->sfx('shotgn'));        $this->setMobjState($player->mo, Info::S_PLAY_ATK2);        $player->ammo[Player::WEAPONINFO[$player->readyWeapon][0]]--;        $this->setPsprite($player, Player::PS_FLASH, Player::WEAPONINFO[$player->readyWeapon][5]);        $this->bulletSlope($player->mo);        for ($i = 0; $i < 7; $i++) {            $this->gunShot($player->mo, false);        }    }    public function A_FireShotgun2(Player $player, PSprite $psp): void    {        $this->startSound($player->mo, $this->sfx('dshtgn'));        $this->setMobjState($player->mo, Info::S_PLAY_ATK2);        $player->ammo[Player::WEAPONINFO[$player->readyWeapon][0]] -= 2;        $this->setPsprite($player, Player::PS_FLASH, Player::WEAPONINFO[$player->readyWeapon][5]);        $this->bulletSlope($player->mo);        for ($i = 0; $i < 20; $i++) {            $damage = 5 * (Random::p() % 3 + 1);            $angle = $player->mo->angle + (Random::p() - Random::p()) * (M_PI / 4096);   // << 19            $slope = $this->bulletSlope + (Random::p() - Random::p()) / 2048;          // << 5            $this->lineAttack($player->mo, $angle, self::MISSILERANGE, $slope, $damage);        }    }    public function A_OpenShotgun2(Player $player, PSprite $psp): void    {        $this->startSound($player->mo, $this->sfx('dbopn'));    }    public function A_LoadShotgun2(Player $player, PSprite $psp): void    {        $this->startSound($player->mo, $this->sfx('dbload'));    }    public function A_CloseShotgun2(Player $player, PSprite $psp): void    {        $this->startSound($player->mo, $this->sfx('dbcls'));        $this->A_ReFire($player, $psp);    }    public function A_FireCGun(Player $player, PSprite $psp): void    {        $this->startSound($player->mo, $this->sfx('pistol'));        $ammo = Player::WEAPONINFO[$player->readyWeapon][0];        if (!$player->ammo[$ammo]) {            return;        }        $this->setMobjState($player->mo, Info::S_PLAY_ATK2);        $player->ammo[$ammo]--;        $this->setPsprite($player, Player::PS_FLASH, Player::WEAPONINFO[$player->readyWeapon][5] + $psp->state - Info::S_CHAIN1);        $this->bulletSlope($player->mo);        $this->gunShot($player->mo, !$player->refire);    }    public function A_Light0(Player $player, PSprite $psp): void    {        $player->extraLight = 0;    }    public function A_Light1(Player $player, PSprite $psp): void    {        $player->extraLight = 1;    }    public function A_Light2(Player $player, PSprite $psp): void    {        $player->extraLight = 2;    }    /** The BFG ball explodes: 40 tracers from the shooter, in front of him. */    public function A_BFGSpray(Mobj $mo): void    {        if ($mo->target === null) {            return;        }        for ($i = 0; $i < 40; $i++) {            $an = $mo->angle - M_PI / 4 + (M_PI / 2 / 40) * $i;            $this->aimLineAttack($mo->target, $an, 16 * 64);            $t = $this->aimed();            if ($t === null) {                continue;            }            $this->spawnMobj($t->x, $t->y, $t->z + $t->height / 4, Info::MT_EXTRABFG);            $damage = 0;            for ($j = 0; $j < 15; $j++) {                $damage += (Random::p() & 7) + 1;            }            $this->damageMobj($t, $mo->target, $mo->target, $damage);        }    }    public function A_BFGsound(Player $player, PSprite $psp): void    {        $this->startSound($player->mo, $this->sfx('bfg'));    }    public function setupPsprites(Player $player): void    {        foreach ($player->psprites as $psp) {            $psp->state = -1;        }        $player->pendingWeapon = $player->readyWeapon;        $this->bringUpWeapon($player);    }    public function movePsprites(Player $player): void    {        foreach ($player->psprites as $i => $psp) {            if ($psp->state >= 0 && $psp->tics !== -1) {                $psp->tics--;                if ($psp->tics <= 0) {                    $this->setPsprite($player, $i, Info::STATES[$psp->state][4]);                }            }        }        $player->psprites[Player::PS_FLASH]->sx = $player->psprites[Player::PS_WEAPON]->sx;        $player->psprites[Player::PS_FLASH]->sy = $player->psprites[Player::PS_WEAPON]->sy;    }}