|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stu\Lib\Map\VisualPanel\LssBlockade; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use OutOfBoundsException; |
|
9
|
|
|
|
|
10
|
|
|
class LssBlockadeGrid |
|
11
|
|
|
{ |
|
12
|
|
|
private int $minX; |
|
13
|
|
|
private int $minY; |
|
14
|
|
|
private int $width; |
|
15
|
|
|
private int $height; |
|
16
|
|
|
private int $obsX; |
|
17
|
|
|
private int $obsY; |
|
18
|
|
|
|
|
19
|
|
|
/** @var bool[][] $blocked[$xIdx][$yIdx] */ |
|
20
|
|
|
private array $blocked = []; |
|
21
|
|
|
|
|
22
|
17 |
|
public function __construct( |
|
23
|
|
|
int $minX, |
|
24
|
|
|
int $maxX, |
|
25
|
|
|
int $minY, |
|
26
|
|
|
int $maxY, |
|
27
|
|
|
int $obsX, |
|
28
|
|
|
int $obsY |
|
29
|
|
|
) { |
|
30
|
17 |
|
if ($minX > $maxX || $minY > $maxY) { |
|
31
|
|
|
throw new InvalidArgumentException('min‑Koordinaten müssen ≤ max sein.'); |
|
32
|
|
|
} |
|
33
|
17 |
|
if ($obsX < $minX || $obsX > $maxX || $obsY < $minY || $obsY > $maxY) { |
|
34
|
|
|
throw new InvalidArgumentException('Observer liegt nicht im angegebenen Rechteck.'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
17 |
|
$this->minX = $minX; |
|
38
|
17 |
|
$this->minY = $minY; |
|
39
|
17 |
|
$this->width = $maxX - $minX + 1; |
|
40
|
17 |
|
$this->height = $maxY - $minY + 1; |
|
41
|
17 |
|
$this->obsX = $obsX; |
|
42
|
17 |
|
$this->obsY = $obsY; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
17 |
|
private function idxX(int $worldX): int |
|
46
|
|
|
{ |
|
47
|
17 |
|
return $worldX - $this->minX; |
|
48
|
|
|
} |
|
49
|
17 |
|
private function idxY(int $worldY): int |
|
50
|
|
|
{ |
|
51
|
17 |
|
return $worldY - $this->minY; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
17 |
|
private function inRange(int $x, int $y): bool |
|
55
|
|
|
{ |
|
56
|
17 |
|
return $x >= $this->minX && $x < $this->minX + $this->width |
|
57
|
17 |
|
&& $y >= $this->minY && $y < $this->minY + $this->height; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
10 |
|
public function setBlocked(int $x, int $y): void |
|
61
|
|
|
{ |
|
62
|
10 |
|
if (!$this->inRange($x, $y)) { |
|
63
|
|
|
throw new OutOfBoundsException("Koordinate ($x,$y) liegt außerhalb des Grids"); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
10 |
|
$this->blocked[$this->idxX($x)][$this->idxY($y)] = true; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
17 |
|
public function isVisible(int $x, int $y): bool |
|
70
|
|
|
{ |
|
71
|
17 |
|
if (!$this->inRange($x, $y)) { |
|
72
|
1 |
|
return false; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
17 |
|
$ixT = $this->idxX($x); |
|
76
|
17 |
|
$iyT = $this->idxY($y); |
|
77
|
17 |
|
if (!empty($this->blocked[$ixT][$iyT])) { |
|
78
|
3 |
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
17 |
|
$dx = $x - $this->obsX; |
|
82
|
17 |
|
$dy = $y - $this->obsY; |
|
83
|
17 |
|
$sx = $dx >= 0 ? 1 : -1; |
|
84
|
17 |
|
$sy = $dy >= 0 ? 1 : -1; |
|
85
|
17 |
|
$dx = abs($dx); |
|
86
|
17 |
|
$dy = abs($dy); |
|
87
|
|
|
|
|
88
|
17 |
|
$err = $dx - $dy; // Bresenham‑Setup |
|
89
|
17 |
|
$cx = $this->obsX; |
|
90
|
17 |
|
$cy = $this->obsY; |
|
91
|
|
|
|
|
92
|
17 |
|
while ($cx !== $x || $cy !== $y) { |
|
93
|
|
|
|
|
94
|
16 |
|
$e2 = $err << 1; // = 2*err |
|
95
|
16 |
|
$nx = $cx; |
|
96
|
16 |
|
$ny = $cy; |
|
97
|
|
|
|
|
98
|
16 |
|
if ($e2 > -$dy) { // Schritt in x‑Richtung |
|
99
|
16 |
|
$err -= $dy; |
|
100
|
16 |
|
$nx += $sx; |
|
101
|
|
|
} |
|
102
|
16 |
|
if ($e2 < $dx) { // Schritt in y‑Richtung |
|
103
|
15 |
|
$err += $dx; |
|
104
|
15 |
|
$ny += $sy; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
16 |
|
if ($cx !== $nx && $cy !== $ny) { |
|
108
|
15 |
|
$side1Blocked = !empty($this->blocked[$this->idxX($cx)][$this->idxY($ny)]); |
|
109
|
15 |
|
$side2Blocked = !empty($this->blocked[$this->idxX($nx)][$this->idxY($cy)]); |
|
110
|
15 |
|
if ($side1Blocked || $side2Blocked) { |
|
111
|
8 |
|
return false; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
11 |
|
if (!empty($this->blocked[$this->idxX($nx)][$this->idxY($ny)])) { |
|
116
|
4 |
|
return false; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
10 |
|
$cx = $nx; |
|
120
|
10 |
|
$cy = $ny; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
11 |
|
return true; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|