Skip to content

Commit 561118c

Browse files
starswaitforussolcloud
authored andcommitted
Flammable performance improvements
1 parent 0312c3d commit 561118c

6 files changed

Lines changed: 71 additions & 7 deletions

File tree

server/src/Core/Graph.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace cs\Core;
44

55
use GraphPHP\Graph\DiGraph;
6+
use GraphPHP\Node\Node;
67

78
final class Graph extends DiGraph
89
{
10+
/** @var array<string,string[]> */
11+
private array $neighbors;
912

1013
public function getNodesCount(): int
1114
{
@@ -17,4 +20,23 @@ public function getEdgeCount(): int
1720
return count($this->edges);
1821
}
1922

23+
/** @return Node[] */
24+
public function getGeneratedNeighbors(string $nodeId): array
25+
{
26+
$neighbors = [];
27+
foreach ($this->neighbors[$nodeId] ?? [] as $nodeKey) {
28+
$neighbors[] = $this->getNodeById($nodeKey) ?? throw new GameException("Node '{$nodeKey}' not found");
29+
}
30+
31+
return $neighbors;
32+
}
33+
34+
public function generateNeighbors(): void
35+
{
36+
$this->neighbors = [];
37+
foreach ($this->edges as $edge) {
38+
$this->neighbors[$edge->getSource()->getId()][] = $edge->getTarget()->getId();
39+
}
40+
}
41+
2042
}

server/src/Core/PathFinder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ public function buildNavigationMesh(Point $start, int $objectHeight): void
187187
}
188188
}
189189

190+
public function saveAndClear(): self
191+
{
192+
$this->visited = [];
193+
$this->graph->generateNeighbors();
194+
return $this;
195+
}
196+
190197
public function getGraph(): Graph
191198
{
192199
return $this->graph;

server/src/Core/World.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -514,12 +514,27 @@ public function checkFlameDamage(GrillEvent $fire, int $tickId): void
514514
continue;
515515
}
516516

517+
$pp = $player->getReferenceToPosition();
518+
$playerHeight = $player->getHeadHeight();
519+
$playerRadius = $player->getBoundingRadius();
520+
if (
521+
$pp->y > $fire->boundaryMax->y
522+
|| $pp->y + $playerHeight < $fire->boundaryMin->y
523+
|| !Collision::circleWithRect(
524+
$pp->x, $pp->z, $playerRadius,
525+
$fire->boundaryMin->x, $fire->boundaryMax->x,
526+
$fire->boundaryMin->z, $fire->boundaryMax->z,
527+
)
528+
) {
529+
continue;
530+
}
531+
517532
foreach ($fire->flames as $flame) {
518533
if (!Collision::pointWithCylinder(
519534
$flame->highestPoint,
520-
$player->getReferenceToPosition(),
521-
$player->getBoundingRadius(),
522-
$player->getHeadHeight())
535+
$pp,
536+
$playerRadius,
537+
$playerHeight)
523538
) {
524539
continue;
525540
}
@@ -646,7 +661,7 @@ public function buildNavigationMesh(int $tileSize, int $objectHeight): PathFinde
646661
$pathFinder->buildNavigationMesh($point, $objectHeight);
647662
}
648663

649-
return $pathFinder;
664+
return $pathFinder->saveAndClear();
650665
}
651666

652667
public function checkXSideWallCollision(Point $bottomCenter, int $height, int $radius): ?Wall

server/src/Event/GrillEvent.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ final class GrillEvent extends Event implements ForOneRoundMax
3030
private readonly int $damageCoolDownTickCount;
3131
private readonly int $maxFlameCount;
3232

33+
public readonly Point $boundaryMin;
34+
public readonly Point $boundaryMax;
35+
3336
/** @var SplQueue<Node> $queue */
3437
private SplQueue $queue;
3538
/** @var array<string,bool> */
@@ -59,6 +62,8 @@ public function __construct(
5962
}
6063

6164
$this->id = "grill-{$this->initiator->getId()}-{$this->world->getTickId()}";
65+
$this->boundaryMin = $start->clone();
66+
$this->boundaryMax = $start->clone();
6267
$this->queue = new SplQueue();
6368
$this->queue->enqueue($startNode);
6469
$this->igniteFlames();
@@ -100,6 +105,17 @@ public function process(int $tick): void
100105
private function igniteFlames(): void
101106
{
102107
foreach ($this->loadFlames() as $candidate) {
108+
$this->boundaryMin->set(
109+
min($this->boundaryMin->x, $candidate->x - $this->flameRadius),
110+
min($this->boundaryMin->y, $candidate->y - 0),
111+
min($this->boundaryMin->z, $candidate->z - $this->flameRadius),
112+
);
113+
$this->boundaryMax->set(
114+
max($this->boundaryMax->x, $candidate->x + $this->flameRadius),
115+
max($this->boundaryMax->y, $candidate->y + $this->flameHeight),
116+
max($this->boundaryMax->z, $candidate->z + $this->flameRadius),
117+
);
118+
103119
$flame = new Flame($candidate, $this->flameRadius, $this->flameHeight);
104120
$this->flames[] = $flame;
105121
$this->lastFlameSpawnTickId = $this->world->getTickId();
@@ -119,16 +135,17 @@ private function loadFlames(): array
119135
$output = [];
120136
while (!$this->queue->isEmpty() && count($output) < min($this->spawnFlameCount, $loadCount)) {
121137
$current = $this->queue->dequeue();
122-
if (array_key_exists($current->getId(), $this->visited)) {
138+
$currentKey = $current->getId();
139+
if (array_key_exists($currentKey, $this->visited)) {
123140
continue;
124141
}
125142

126-
$this->visited[$current->getId()] = true;
143+
$this->visited[$currentKey] = true;
127144
/** @var Point $point */
128145
$point = $current->getData();
129146
$output[] = $point;
130147

131-
foreach ($this->graph->getNeighbors($current) as $node) {
148+
foreach ($this->graph->getGeneratedNeighbors($currentKey) as $node) {
132149
$this->queue->enqueue($node);
133150
}
134151
}

test/og/World/NavigationMeshTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public function testSimple(): void
6161
$startNode = $path->getGraph()->getNodeById($start->hash());
6262
$this->assertNotNull($startNode);
6363
$this->assertCount(3, $path->getGraph()->getNeighbors($startNode));
64+
$path->getGraph()->generateNeighbors();
65+
$this->assertCount(3, $path->getGraph()->getGeneratedNeighbors($startNode->getId()));
6466
}
6567

6668
public function testBoundary(): void

www/assets/js/Game.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ export class Game {
284284
return;
285285
}
286286
if (item.slot === InventorySlot.SLOT_GRENADE_MOLOTOV || item.slot === InventorySlot.SLOT_GRENADE_HE) {
287+
this.removeGrenade(throwableId)
287288
return;
288289
}
289290

0 commit comments

Comments
 (0)