BlueGit

projects-timfalken-com microchess

Files

microchess / web / lib / HexGrid.php

HexGrid.php

feat: scaffold Synhex hex tactics on the Microchess lobby shell
timfa project_86226080_bot_b6e0858ac0a12dc6d49a8d25b3b0be9b · pushed by timfa · 2 months ago
24f20754 History
HexGrid.php 8.0 KiB Download Raw
1<?php
2declare(strict_types=1);
3
4/**
5 * Pointy-top hex math using axial (q, r) and odd-r offset (col, row).
6 * Direction indices: 0 E, 1 NE, 2 NW, 3 W, 4 SW, 5 SE.
7 */
8final class HexGrid
9{
10 public const DIRS = [[1, 0], [1, -1], [0, -1], [-1, 0], [-1, 1], [0, 1]];
11
12 public static function axial(int $col, int $row): array
13 {
14 return [$col - intdiv($row, 2), $row];
15 }
16
17 public static function offset(int $q, int $r): array
18 {
19 return [$q + intdiv($r, 2), $r];
20 }
21
22 public static function key(int $q, int $r): string
23 {
24 return $q.','.$r;
25 }
26
27 public static function parse(string $key): array
28 {
29 $p = explode(',', $key, 2);
30 return [(int)$p[0], (int)$p[1]];
31 }
32
33 public static function inBounds(int $q, int $r, int $cols, int $rows): bool
34 {
35 [$col, $row] = self::offset($q, $r);
36 return $col >= 0 && $col < $cols && $row >= 0 && $row < $rows;
37 }
38
39 public static function neighbors(int $q, int $r): array
40 {
41 $out = [];
42 foreach (self::DIRS as [$dq, $dr]) {
43 $out[] = [$q + $dq, $r + $dr];
44 }
45 return $out;
46 }
47
48 public static function neighbor(int $q, int $r, int $dir): array
49 {
50 $d = self::DIRS[($dir % 6 + 6) % 6];
51 return [$q + $d[0], $r + $d[1]];
52 }
53
54 public static function distance(int $q1, int $r1, int $q2, int $r2): int
55 {
56 return intdiv(abs($q1 - $q2) + abs($q1 + $r1 - $q2 - $r2) + abs($r1 - $r2), 2);
57 }
58
59 public static function cubeRound(float $x, float $z, float $y): array
60 {
61 $rx = (int)round($x);
62 $rz = (int)round($z);
63 $ry = (int)round($y);
64 $dx = abs($rx - $x);
65 $dz = abs($rz - $z);
66 $dy = abs($ry - $y);
67 if ($dx > $dz && $dx > $dy) {
68 $rx = -$rz - $ry;
69 } elseif ($dz > $dy) {
70 $rz = -$rx - $ry;
71 }
72 return [$rx, $rz];
73 }
74
75 /** Inclusive hex line from A to B. */
76 public static function line(int $q1, int $r1, int $q2, int $r2): array
77 {
78 $n = self::distance($q1, $r1, $q2, $r2);
79 if ($n === 0) {
80 return [[$q1, $r1]];
81 }
82 $out = [];
83 $y1 = -$q1 - $r1;
84 $y2 = -$q2 - $r2;
85 for ($i = 0; $i <= $n; $i++) {
86 $t = $i / $n;
87 $out[] = self::cubeRound(
88 $q1 + ($q2 - $q1) * $t,
89 $r1 + ($r2 - $r1) * $t,
90 $y1 + ($y2 - $y1) * $t
91 );
92 }
93 return $out;
94 }
95
96 /** Hexes from origin stepping in a facing, excluding origin. */
97 public static function ray(int $q, int $r, int $dir, int $length): array
98 {
99 $out = [];
100 for ($i = 0; $i < $length; $i++) {
101 [$q, $r] = self::neighbor($q, $r, $dir);
102 $out[] = [$q, $r];
103 }
104 return $out;
105 }
106
107 /**
108 * LoS is blocked by walls on intermediate hexes. Endpoints may be occupied.
109 * @param array<string,true> $walls
110 */
111 public static function los(int $q1, int $r1, int $q2, int $r2, array $walls): bool
112 {
113 $line = self::line($q1, $r1, $q2, $r2);
114 $last = count($line) - 1;
115 for ($i = 1; $i < $last; $i++) {
116 if (isset($walls[self::key($line[$i][0], $line[$i][1])])) {
117 return false;
118 }
119 }
120 return true;
121 }
122
123 public static function pixel(int $q, int $r): array
124 {
125 return [sqrt(3) * ($q + $r / 2), 1.5 * $r];
126 }
127
128 public static function facingAngle(int $dir): float
129 {
130 $d = self::DIRS[($dir % 6 + 6) % 6];
131 [$x, $y] = self::pixel($d[0], $d[1]);
132 return atan2($y, $x);
133 }
134
135 public static function bearing(int $q1, int $r1, int $q2, int $r2): float
136 {
137 [$x1, $y1] = self::pixel($q1, $r1);
138 [$x2, $y2] = self::pixel($q2, $r2);
139 return atan2($y2 - $y1, $x2 - $x1);
140 }
141
142 public static function angleDiff(float $a, float $b): float
143 {
144 $d = fmod($a - $b + M_PI, 2 * M_PI);
145 if ($d < 0) {
146 $d += 2 * M_PI;
147 }
148 return $d - M_PI;
149 }
150
151 public static function inCone(int $uq, int $ur, int $facing, int $tq, int $tr, float $halfDeg): bool
152 {
153 if ($uq === $tq && $ur === $tr) {
154 return false;
155 }
156 return abs(self::angleDiff(self::bearing($uq, $ur, $tq, $tr), self::facingAngle($facing))) <= deg2rad($halfDeg) + 1e-9;
157 }
158
159 public static function dirToward(int $q1, int $r1, int $q2, int $r2): int
160 {
161 $best = 0;
162 $bestAbs = INF;
163 $bearing = self::bearing($q1, $r1, $q2, $r2);
164 for ($d = 0; $d < 6; $d++) {
165 $abs = abs(self::angleDiff($bearing, self::facingAngle($d)));
166 if ($abs < $bestAbs) {
167 $bestAbs = $abs;
168 $best = $d;
169 }
170 }
171 return $best;
172 }
173
174 public static function onFacingLine(int $q, int $r, int $facing, int $tq, int $tr, int $length): bool
175 {
176 foreach (self::ray($q, $r, $facing, $length) as [$qq, $rr]) {
177 if ($qq === $tq && $rr === $tr) {
178 return true;
179 }
180 }
181 return false;
182 }
183
184 /**
185 * A* path across walkable hexes. Returns inclusive hex list or null.
186 * @param array<string,true> $walkable
187 */
188 public static function path(int $q1, int $r1, int $q2, int $r2, array $walkable): ?array
189 {
190 $start = self::key($q1, $r1);
191 $goal = self::key($q2, $r2);
192 if (!isset($walkable[$start]) || !isset($walkable[$goal])) {
193 return null;
194 }
195 if ($start === $goal) {
196 return [[$q1, $r1]];
197 }
198 $open = [$start => 0];
199 $came = [];
200 $g = [$start => 0];
201 while ($open) {
202 $current = array_key_first($open);
203 $best = $open[$current];
204 foreach ($open as $k => $f) {
205 if ($f < $best) {
206 $current = $k;
207 $best = $f;
208 }
209 }
210 unset($open[$current]);
211 if ($current === $goal) {
212 $path = [];
213 while ($current !== $start) {
214 $path[] = self::parse($current);
215 $current = $came[$current];
216 }
217 $path[] = [$q1, $r1];
218 return array_reverse($path);
219 }
220 [$cq, $cr] = self::parse($current);
221 foreach (self::neighbors($cq, $cr) as [$nq, $nr]) {
222 $nk = self::key($nq, $nr);
223 if (!isset($walkable[$nk])) {
224 continue;
225 }
226 $tg = $g[$current] + 1;
227 if (!isset($g[$nk]) || $tg < $g[$nk]) {
228 $came[$nk] = $current;
229 $g[$nk] = $tg;
230 $open[$nk] = $tg + self::distance($nq, $nr, $q2, $r2);
231 }
232 }
233 }
234 return null;
235 }
236
237 /**
238 * @return array<string,true>
239 */
240 public static function allHexes(int $cols, int $rows): array
241 {
242 $out = [];
243 for ($row = 0; $row < $rows; $row++) {
244 for ($col = 0; $col < $cols; $col++) {
245 [$q, $r] = self::axial($col, $row);
246 $out[self::key($q, $r)] = true;
247 }
248 }
249 return $out;
250 }
251}
252
253/** Deterministic 32-bit RNG used by wall generation and combat. */
254final class SynhexRng
255{
256 private int $state;
257
258 public function __construct(int $seed)
259 {
260 $this->state = $seed | 1;
261 }
262
263 public function next(): int
264 {
265 $x = $this->state;
266 $x = ($x ^ ($x << 13)) & 0x7fffffff;
267 $x = ($x ^ ($x >> 17)) & 0x7fffffff;
268 $x = ($x ^ ($x << 5)) & 0x7fffffff;
269 return $this->state = $x;
270 }
271
272 public function below(int $n): int
273 {
274 return $n <= 0 ? 0 : $this->next() % $n;
275 }
276
277 public function chance(int $percent): bool
278 {
279 if ($percent <= 0) {
280 return false;
281 }
282 if ($percent >= 100) {
283 return true;
284 }
285 return $this->below(100) < $percent;
286 }
287
288 public function unit(): float
289 {
290 return $this->next() / 0x7fffffff;
291 }
292}