Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Volumetric smokes #17

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions server/src/Core/Collision.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,37 @@ public static function pointWithBox(Point $point, Box $box): bool
return true;
}

public static function pointWithBoxBoundary(Point $point, Point $boxMin, Point $boxMax): bool
{
if ($point->y > $boxMax->y || $point->y < $boxMin->y) {
return false;
}
if ($point->x > $boxMax->x || $point->x < $boxMin->x) {
return false;
}
if ($point->z > $boxMax->z || $point->z < $boxMin->z) {
return false;
}

return true;
}

public static function boxWithBox(Point $boundaryAMin, Point $boundaryAMax, Point $boundaryBMin, Point $boundaryBMax): bool
{
if ($boundaryAMin->y > $boundaryBMax->y || $boundaryBMin->y > $boundaryAMax->y) {
return false;
}

if (
$boundaryAMax->x >= $boundaryBMin->x
&& $boundaryAMin->x <= $boundaryBMax->x
&& $boundaryAMax->z >= $boundaryBMin->z
&& $boundaryAMin->z <= $boundaryBMax->z
) {
return true;
}

return false;
}

}
20 changes: 20 additions & 0 deletions server/src/Core/Column.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace cs\Core;

final class Column
{

public bool $active = true;
public readonly Point $highestPoint;
public readonly Point $boundaryMin;
public readonly Point $boundaryMax;

public function __construct(public readonly Point $center, public readonly int $radius, public readonly int $height)
{
$this->highestPoint = $this->center->clone()->addY($this->height);
$this->boundaryMin = $this->center->clone()->addX(-$this->radius)->addZ(-$this->radius);
$this->boundaryMax = $this->center->clone()->addX($this->radius)->addZ($this->radius)->addY($this->height);
}

}
15 changes: 0 additions & 15 deletions server/src/Core/Flame.php

This file was deleted.

6 changes: 6 additions & 0 deletions server/src/Core/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use cs\Event\RoundEndCoolDownEvent;
use cs\Event\RoundEndEvent;
use cs\Event\RoundStartEvent;
use cs\Event\SmokeEvent;
use cs\Event\SoundEvent;
use cs\Event\ThrowEvent;
use cs\Interface\ForOneRoundMax;
Expand Down Expand Up @@ -258,6 +259,11 @@ public function addGrillEvent(GrillEvent $event): void
$this->addEvent($event);
}

public function addSmokeEvent(SmokeEvent $event): void
{
$this->addEvent($event);
}

public function addDropEvent(DropEvent $event): void
{
$this->addEvent($event);
Expand Down
13 changes: 13 additions & 0 deletions server/src/Core/Sequence.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace cs\Core;

final class Sequence
{
private static int $value = 0;

public static function next(): string
{
return 'id-' . ++self::$value;
}
}
105 changes: 96 additions & 9 deletions server/src/Core/World.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use cs\Equipment\Bomb;
use cs\Equipment\Grenade;
use cs\Equipment\HighExplosive;
use cs\Equipment\Smoke;
use cs\Event\DropEvent;
use cs\Event\GrillEvent;
use cs\Event\SmokeEvent;
use cs\Event\SoundEvent;
use cs\Event\ThrowEvent;
use cs\Interface\Flammable;
Expand Down Expand Up @@ -39,6 +41,10 @@ class World
private array $spawnPositionTakes = [];
/** @var array<int,Point[]> */
private array $spawnCandidates;
/** @var array<string,SmokeEvent> */
private array $activeSmokes = [];
/** @var array<string,GrillEvent> */
private array $activeMolotovs = [];
private Bomb $bomb;
private int $lastBombActionTick = -1;
private int $lastBombPlayerId = -1;
Expand All @@ -53,6 +59,8 @@ public function __construct(private Game $game)

public function roundReset(): void
{
$this->activeSmokes = [];
$this->activeMolotovs = [];
$this->spawnCandidates = [];
$this->spawnPositionTakes = [];
$this->dropItems = [];
Expand Down Expand Up @@ -481,10 +489,34 @@ public function throw(ThrowEvent $event): void
if ($event->item instanceof Flammable) {
$this->processFlammableExplosion($event->getPlayer(), $event->getPositionClone(), $event->item);
}
if ($event->item instanceof Smoke) {
$this->processSmokeExpansion($event->getPlayer(), $event->getPositionClone(), $event->item);
}
};
$this->game->addThrowEvent($event);
}

private function processSmokeExpansion(Player $initiator, Point $epicentre, Smoke $item): void
{
if ($this->grenadeNavMesh === null) {
$this->regenerateNavigationMeshes();
}
assert($this->grenadeNavMesh !== null);

$epicentreFloor = $epicentre->clone()->addY(-$item->getBoundingRadius());
$floorNavmeshPoint = $this->grenadeNavMesh->findTile($epicentreFloor, $item->getBoundingRadius());

$event = new SmokeEvent(
$initiator, $item, $this, $this->grenadeNavMesh->tileSizeHalf,
$this->grenadeNavMesh->colliderHeight, $this->grenadeNavMesh->getGraph(), $floorNavmeshPoint,
);
$event->onComplete[] = function (SmokeEvent $event) {
unset($this->activeSmokes[$event->id]);
};
$this->activeSmokes[$event->id] = $event;
$this->game->addSmokeEvent($event);
}

public function processFlammableExplosion(Player $thrower, Point $epicentre, Flammable $item): void
{
if ($this->grenadeNavMesh === null) {
Expand All @@ -495,12 +527,49 @@ public function processFlammableExplosion(Player $thrower, Point $epicentre, Fla
$epicentreFloor = $epicentre->clone()->addY(-$item->getBoundingRadius());
$floorNavmeshPoint = $this->grenadeNavMesh->findTile($epicentreFloor, $item->getBoundingRadius());

$this->game->addGrillEvent(
new GrillEvent(
$thrower, $item, $this, $this->grenadeNavMesh->tileSizeHalf,
$this->grenadeNavMesh->colliderHeight, $this->grenadeNavMesh->getGraph(), $floorNavmeshPoint,
)
$event = new GrillEvent(
$thrower, $item, $this, $this->grenadeNavMesh->tileSizeHalf,
$this->grenadeNavMesh->colliderHeight, $this->grenadeNavMesh->getGraph(), $floorNavmeshPoint,
);
$event->onComplete[] = function (GrillEvent $event) {
unset($this->activeMolotovs[$event->id]);
};
$this->activeMolotovs[$event->id] = $event;
$this->game->addGrillEvent($event);
}

public function smokeTryToExtinguishFlames(Column $smoke): void
{
foreach ($this->activeMolotovs as $fire) {
if (!Collision::boxWithBox($smoke->boundaryMin, $smoke->boundaryMax, $fire->boundaryMin, $fire->boundaryMax)
) {
continue;
}

foreach ($fire->parts as $flame) {
if ($flame->active && Collision::boxWithBox($smoke->boundaryMin, $smoke->boundaryMax, $flame->boundaryMin, $flame->boundaryMax)) {
$fire->extinguish($flame);
}
}
}
}

public function flameCanIgnite(Column $flame): bool
{
foreach ($this->activeSmokes as $smoke) {
if (!Collision::boxWithBox($smoke->boundaryMin, $smoke->boundaryMax, $flame->boundaryMin, $flame->boundaryMax)
) {
continue;
}

foreach ($smoke->parts as $smokePart) {
if (Collision::boxWithBox($smokePart->boundaryMin, $smokePart->boundaryMax, $flame->boundaryMin, $flame->boundaryMax)) {
return false;
}
}
}

return true;
}

public function checkFlameDamage(GrillEvent $fire, int $tickId): void
Expand All @@ -526,8 +595,8 @@ public function checkFlameDamage(GrillEvent $fire, int $tickId): void
continue;
}

foreach ($fire->flames as $flame) {
if (!Collision::pointWithCylinder(
foreach ($fire->parts as $flame) {
if (!$flame->active || !Collision::pointWithCylinder(
$flame->highestPoint,
$pp,
$playerRadius,
Expand All @@ -537,22 +606,40 @@ public function checkFlameDamage(GrillEvent $fire, int $tickId): void
}

$fire->playerHit($playerId, $tickId);
$damage = $fire->item->calculateDamage($player->getArmorType() !== ArmorType::NONE);
$damage = $fire->getItem()->calculateDamage($player->getArmorType() !== ArmorType::NONE);
assert($fire->item instanceof Item);
$this->playerHit(
$player->getCentrePoint(), $player, $fire->initiator, SoundType::FLAME_PLAYER_HIT,
$fire->item, $flame->center, $damage
);
$player->lowerHealth($damage);
if (!$player->isAlive()) {
$this->playerDiedToFlame($fire->initiator, $player, $fire->item);
$this->playerDiedToFlame($fire->initiator, $player, $fire->getItem());
}

break;
}
}
}

public function isCollisionWithMolotov(Point $pos): bool
{
foreach ($this->activeMolotovs as $molotov) {
if (!Collision::pointWithBoxBoundary($pos, $molotov->boundaryMin, $molotov->boundaryMax)
) {
continue;
}

foreach ($molotov->parts as $flame) {
if ($flame->active && Collision::pointWithCylinder($pos, $flame->center, 3 * $flame->radius, $flame->height)) {
return true;
}
}
}

return false;
}

private function processHighExplosiveBlast(Player $thrower, Point $epicentre, HighExplosive $item): void
{
$maxBlastDistance = $item->getMaxBlastRadius();
Expand Down
2 changes: 2 additions & 0 deletions server/src/Enum/SoundType.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ enum SoundType: int
case ITEM_DROP_LAND = 21;
case FLAME_EXTINGUISH = 22;
case FLAME_PLAYER_HIT = 23;
case SMOKE_SPAWN = 24;
case SMOKE_FADE = 25;

}
19 changes: 17 additions & 2 deletions server/src/Equipment/Smoke.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,30 @@
namespace cs\Equipment;

use cs\Enum\InventorySlot;
use cs\Interface\Volumetric;

class Smoke extends Grenade
class Smoke extends Grenade implements Volumetric
{

public const MAX_TIME_MS = 18_000;
protected int $price = 300;

public function getSlot(): InventorySlot
{
return InventorySlot::SLOT_GRENADE_SMOKE;
}

public function getSpawnAreaMetersSquared(): int
{
return 120;
}

public function getMaxTimeMs(): int
{
return self::MAX_TIME_MS;
}

public function getMaxAreaMetersSquared(): int
{
return 210_000;
}
}
3 changes: 2 additions & 1 deletion server/src/Event/DropEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use cs\Core\Item;
use cs\Core\Player;
use cs\Core\Point;
use cs\Core\Sequence;
use cs\Core\Util;
use cs\Core\World;
use cs\Enum\SoundType;
Expand All @@ -27,7 +28,7 @@ class DropEvent extends Event implements ForOneRoundMax

public function __construct(private readonly Player $player, private readonly Item $item, private readonly World $world)
{
$this->id = "drop-{$this->player->getId()}-{$this->item->getId()}-{$this->world->getTickId()}";
$this->id = Sequence::next();
$this->origin = $this->player->getSightPositionClone();
$this->dropItem = new DropItem($this->id, $this->item, $this->origin->clone());
$this->angleHorizontal = $player->getSight()->getRotationHorizontal();
Expand Down
2 changes: 1 addition & 1 deletion server/src/Event/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function reset(): void
$this->onComplete = [];
}

protected function runOnCompleteHooks(): void
public final function runOnCompleteHooks(): void
{
foreach ($this->onComplete as $func) {
call_user_func($func, $this);
Expand Down
1 change: 1 addition & 0 deletions server/src/Event/EventList.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ final class EventList
ThrowEvent::class => 11,
DropEvent::class => 12,
GrillEvent::class => 13,
SmokeEvent::class => 14,
];

}
Loading
Loading