<?php/* * dmmotor — pickups, damage and deaths (p_inter.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;use App\Dm\Strings;trait Interaction{ private const BONUSADD = 6; private const BASETHRESHOLD = 100; private const INVULNTICS = 30 * 35; private const INVISTICS = 60 * 35; private const INFRATICS = 120 * 35; private const IRONTICS = 60 * 35; /** P_GiveAmmo: $num clips (0 = half a clip, dropped by a monster). */ private function giveAmmo(Player $player, int $ammo, int $num): bool { if ($ammo === Player::AM_NOAMMO || $player->ammo[$ammo] === $player->maxAmmo[$ammo]) { return false; } $num = $num ? $num * Player::CLIPAMMO[$ammo] : intdiv(Player::CLIPAMMO[$ammo], 2); if ($this->skill === self::SK_BABY || $this->skill === self::SK_NIGHTMARE) { $num <<= 1; // double ammo in trainer mode and nightmare } $old = $player->ammo[$ammo]; $player->ammo[$ammo] = min($player->maxAmmo[$ammo], $old + $num); if ($old) { return true; } // the first ammo of a kind: switch to its weapon $ready = $player->readyWeapon; switch ($ammo) { case Player::AM_CLIP: if ($ready === Player::WP_FIST) { $player->pendingWeapon = $player->weaponOwned[Player::WP_CHAINGUN] ? Player::WP_CHAINGUN : Player::WP_PISTOL; } break; case Player::AM_SHELL: if (($ready === Player::WP_FIST || $ready === Player::WP_PISTOL) && $player->weaponOwned[Player::WP_SHOTGUN]) { $player->pendingWeapon = Player::WP_SHOTGUN; } break; case Player::AM_CELL: if (($ready === Player::WP_FIST || $ready === Player::WP_PISTOL) && $player->weaponOwned[Player::WP_PLASMA]) { $player->pendingWeapon = Player::WP_PLASMA; } break; case Player::AM_MISL: if ($ready === Player::WP_FIST && $player->weaponOwned[Player::WP_MISSILE]) { $player->pendingWeapon = Player::WP_MISSILE; } break; } return true; } private function giveWeapon(Player $player, int $weapon, bool $dropped): bool { $ammo = Player::WEAPONINFO[$weapon][0]; $gaveAmmo = $ammo !== Player::AM_NOAMMO && $this->giveAmmo($player, $ammo, $dropped ? 1 : 2); if ($player->weaponOwned[$weapon]) { return $gaveAmmo; } $player->weaponOwned[$weapon] = true; $player->pendingWeapon = $weapon; return true; } private function giveBody(Player $player, int $num): bool { if ($player->health >= self::MAXHEALTH) { return false; } $player->health = min(self::MAXHEALTH, $player->health + $num); $player->mo->health = $player->health; return true; } private function giveArmor(Player $player, int $type): bool { $hits = $type * 100; if ($player->armorPoints >= $hits) { return false; } $player->armorType = $type; $player->armorPoints = $hits; return true; } private function giveCard(Player $player, int $card): void { if ($player->cards[$card]) { return; } $player->bonusCount = self::BONUSADD; $player->cards[$card] = true; } private function givePower(Player $player, int $power): bool { switch ($power) { case Player::PW_INVULNERABILITY: $player->powers[$power] = self::INVULNTICS; return true; case Player::PW_INVISIBILITY: $player->powers[$power] = self::INVISTICS; $player->mo->flags |= Info::MF_SHADOW; return true; case Player::PW_INFRARED: $player->powers[$power] = self::INFRATICS; return true; case Player::PW_IRONFEET: $player->powers[$power] = self::IRONTICS; return true; case Player::PW_STRENGTH: $this->giveBody($player, 100); $player->powers[$power] = 1; return true; } if ($player->powers[$power]) { return false; // already got it } $player->powers[$power] = 1; return true; } /** P_TouchSpecialThing: the player walked over an item. */ public function touchSpecialThing(Mobj $special, Mobj $toucher): void { $delta = $special->z - $toucher->z; if ($delta > $toucher->height || $delta < -8) { return; // out of reach } $player = $toucher->player; if ($player === null || $toucher->health <= 0) { return; } $sound = $this->sfx('itemup'); $msg = null; $dropped = (bool) ($special->flags & Info::MF_DROPPED); switch (Info::SPRITES[$special->sprite]) { case 'ARM1': if (!$this->giveArmor($player, 1)) { return; } $msg = 'GOTARMOR'; break; case 'ARM2': if (!$this->giveArmor($player, 2)) { return; } $msg = 'GOTMEGA'; break; case 'BON1': $player->health = min(200, $player->health + 1); $player->mo->health = $player->health; $msg = 'GOTHTHBONUS'; break; case 'BON2': $player->armorPoints = min(200, $player->armorPoints + 1); if (!$player->armorType) { $player->armorType = 1; } $msg = 'GOTARMBONUS'; break; case 'SOUL': $player->health = min(200, $player->health + 100); $player->mo->health = $player->health; $msg = 'GOTSUPER'; $sound = $this->sfx('getpow'); break; case 'BKEY': $msg = $player->cards[Player::IT_BLUECARD] ? null : 'GOTBLUECARD'; $this->giveCard($player, Player::IT_BLUECARD); break; case 'YKEY': $msg = $player->cards[Player::IT_YELLOWCARD] ? null : 'GOTYELWCARD'; $this->giveCard($player, Player::IT_YELLOWCARD); break; case 'RKEY': $msg = $player->cards[Player::IT_REDCARD] ? null : 'GOTREDCARD'; $this->giveCard($player, Player::IT_REDCARD); break; case 'BSKU': $msg = $player->cards[Player::IT_BLUESKULL] ? null : 'GOTBLUESKUL'; $this->giveCard($player, Player::IT_BLUESKULL); break; case 'YSKU': $msg = $player->cards[Player::IT_YELLOWSKULL] ? null : 'GOTYELWSKUL'; $this->giveCard($player, Player::IT_YELLOWSKULL); break; case 'RSKU': $msg = $player->cards[Player::IT_REDSKULL] ? null : 'GOTREDSKULL'; $this->giveCard($player, Player::IT_REDSKULL); break; case 'STIM': if (!$this->giveBody($player, 10)) { return; } $msg = 'GOTSTIM'; break; case 'MEDI': if (!$this->giveBody($player, 25)) { return; } $msg = $player->health < 25 ? 'GOTMEDINEED' : 'GOTMEDIKIT'; break; case 'PINV': if (!$this->givePower($player, Player::PW_INVULNERABILITY)) { return; } $msg = 'GOTINVUL'; $sound = $this->sfx('getpow'); break; case 'PSTR': if (!$this->givePower($player, Player::PW_STRENGTH)) { return; } $msg = 'GOTBERSERK'; if ($player->readyWeapon !== Player::WP_FIST) { $player->pendingWeapon = Player::WP_FIST; } $sound = $this->sfx('getpow'); break; case 'PINS': if (!$this->givePower($player, Player::PW_INVISIBILITY)) { return; } $msg = 'GOTINVIS'; $sound = $this->sfx('getpow'); break; case 'SUIT': if (!$this->givePower($player, Player::PW_IRONFEET)) { return; } $msg = 'GOTSUIT'; $sound = $this->sfx('getpow'); break; case 'PMAP': if (!$this->givePower($player, Player::PW_ALLMAP)) { return; } $msg = 'GOTMAP'; $sound = $this->sfx('getpow'); break; case 'PVIS': if (!$this->givePower($player, Player::PW_INFRARED)) { return; } $msg = 'GOTVISOR'; $sound = $this->sfx('getpow'); break; case 'CLIP': if (!$this->giveAmmo($player, Player::AM_CLIP, $dropped ? 0 : 1)) { return; } $msg = 'GOTCLIP'; break; case 'AMMO': if (!$this->giveAmmo($player, Player::AM_CLIP, 5)) { return; } $msg = 'GOTCLIPBOX'; break; case 'ROCK': if (!$this->giveAmmo($player, Player::AM_MISL, 1)) { return; } $msg = 'GOTROCKET'; break; case 'BROK': if (!$this->giveAmmo($player, Player::AM_MISL, 5)) { return; } $msg = 'GOTROCKBOX'; break; case 'CELL': if (!$this->giveAmmo($player, Player::AM_CELL, 1)) { return; } $msg = 'GOTCELL'; break; case 'CELP': if (!$this->giveAmmo($player, Player::AM_CELL, 5)) { return; } $msg = 'GOTCELLBOX'; break; case 'SHEL': if (!$this->giveAmmo($player, Player::AM_SHELL, 1)) { return; } $msg = 'GOTSHELLS'; break; case 'SBOX': if (!$this->giveAmmo($player, Player::AM_SHELL, 5)) { return; } $msg = 'GOTSHELLBOX'; break; case 'BPAK': if (!$player->backpack) { foreach ($player->maxAmmo as $i => $max) { $player->maxAmmo[$i] = $max * 2; } $player->backpack = true; } for ($i = 0; $i < 4; $i++) { $this->giveAmmo($player, $i, 1); } $msg = 'GOTBACKPACK'; break; case 'BFUG': if (!$this->giveWeapon($player, Player::WP_BFG, false)) { return; } $msg = 'GOTBFG9000'; $sound = $this->sfx('wpnup'); break; case 'MGUN': if (!$this->giveWeapon($player, Player::WP_CHAINGUN, $dropped)) { return; } $msg = 'GOTCHAINGUN'; $sound = $this->sfx('wpnup'); break; case 'CSAW': if (!$this->giveWeapon($player, Player::WP_CHAINSAW, false)) { return; } $msg = 'GOTCHAINSAW'; $sound = $this->sfx('wpnup'); break; case 'LAUN': if (!$this->giveWeapon($player, Player::WP_MISSILE, false)) { return; } $msg = 'GOTLAUNCHER'; $sound = $this->sfx('wpnup'); break; case 'PLAS': if (!$this->giveWeapon($player, Player::WP_PLASMA, false)) { return; } $msg = 'GOTPLASMA'; $sound = $this->sfx('wpnup'); break; case 'MEGA': $player->health = 200; $player->mo->health = 200; $this->giveArmor($player, 2); $msg = 'GOTMSPHERE'; $sound = $this->sfx('getpow'); break; case 'SGN2': if (!$this->giveWeapon($player, Player::WP_SUPERSHOTGUN, $dropped)) { return; } $msg = 'GOTSHOTGUN2'; $sound = $this->sfx('wpnup'); break; case 'SHOT': if (!$this->giveWeapon($player, Player::WP_SHOTGUN, $dropped)) { return; } $msg = 'GOTSHOTGUN'; $sound = $this->sfx('wpnup'); break; default: return; } if ($msg !== null) { $player->message = Strings::get($msg); } if ($special->flags & Info::MF_COUNTITEM) { $player->itemCount++; } $this->removeMobj($special); $player->bonusCount += self::BONUSADD; $this->startSound(null, $sound); } private function killMobj(?Mobj $source, Mobj $target): void { $target->flags &= ~(Info::MF_SHOOTABLE | Info::MF_FLOAT | Info::MF_SKULLFLY); if ($target->type !== Info::MT_SKULL) { $target->flags &= ~Info::MF_NOGRAVITY; } $target->flags |= Info::MF_CORPSE | Info::MF_DROPOFF; $target->height /= 4; if ($target->flags & Info::MF_COUNTKILL) { $this->player->killCount++; // killed by the player, or by another monster } if ($target->player !== null) { $target->flags &= ~Info::MF_SOLID; $target->player->playerState = Player::DEAD; $this->dropWeapon($target->player); } if ($target->health < -$target->info['spawnhealth'] && $target->info['xdeathstate']) { $this->setMobjState($target, $target->info['xdeathstate']); } else { $this->setMobjState($target, $target->info['deathstate']); } $target->tics -= Random::p() & 3; if ($target->tics < 1) { $target->tics = 1; } // drop stuff $item = match ($target->type) { Info::MT_WOLFSS, Info::MT_POSSESSED => Info::MT_CLIP, Info::MT_SHOTGUY => Info::MT_SHOTGUN, Info::MT_CHAINGUY => Info::MT_CHAINGUN, default => -1, }; if ($item >= 0) { $mo = $this->spawnMobj($target->x, $target->y, self::ONFLOORZ, $item); $mo->flags |= Info::MF_DROPPED; } } /** * P_DamageMobj: $inflictor hurts $target (the missile, the puff...), $source is who shot (the target of the * monster's anger); both null for the floor or a crusher. */ public function damageMobj(Mobj $target, ?Mobj $inflictor, ?Mobj $source, int $damage): void { if (!($target->flags & Info::MF_SHOOTABLE) || $target->health <= 0) { return; } if ($target->flags & Info::MF_SKULLFLY) { $target->momX = $target->momY = $target->momZ = 0; } $player = $target->player; if ($player !== null && $this->skill === self::SK_BABY) { $damage >>= 1; // take half damage in trainer mode } // thrust away from the inflictor if ($inflictor !== null && !($target->flags & Info::MF_NOCLIP) && ($source === null || $source->player === null || $source->player->readyWeapon !== Player::WP_CHAINSAW)) { $ang = self::pointToAngle($inflictor->x, $inflictor->y, $target->x, $target->y); $thrust = $damage * 12.5 / max(1, $target->info['mass']); // make it fall forwards sometimes if ($damage < 40 && $damage > $target->health && $target->z - $inflictor->z > 64 && (Random::p() & 1)) { $ang += M_PI; $thrust *= 4; } $target->momX += $thrust * cos($ang); $target->momY += $thrust * sin($ang); } if ($player !== null) { // the end of the game: the last level does not let the player die from its floor if ($target->subsector->sector->special === 11 && $damage >= $target->health) { $damage = $target->health - 1; } if ($damage < 1000 && (($player->cheats & 2) || $player->powers[Player::PW_INVULNERABILITY])) { return; } if ($player->armorType) { $saved = $player->armorType === 1 ? intdiv($damage, 3) : intdiv($damage, 2); if ($player->armorPoints <= $saved) { $saved = $player->armorPoints; // the armor is used up $player->armorType = 0; } $player->armorPoints -= $saved; $damage -= $saved; } $player->health = max(0, $player->health - $damage); $player->attacker = $source; $player->damageCount = min(100, $player->damageCount + $damage); } $target->health -= $damage; if ($target->health <= 0) { $this->killMobj($source, $target); return; } if (Random::p() < $target->info['painchance'] && !($target->flags & Info::MF_SKULLFLY)) { $target->flags |= Info::MF_JUSTHIT; // fight back! $this->setMobjState($target, $target->info['painstate']); } $target->reactionTime = 0; // we are awake now if ((!$target->threshold || $target->type === Info::MT_VILE) && $source !== null && $source !== $target && $source->type !== Info::MT_VILE) { // chase the one who hurt it $target->target = $source; $target->threshold = self::BASETHRESHOLD; if ($target->state === $target->info['spawnstate'] && $target->info['seestate'] !== Info::S_NULL) { $this->setMobjState($target, $target->info['seestate']); } } }}