| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | /** Match lifecycle: spawn, plans, fog, commit/playback. */ |
| 5 | final class SynhexMatch |
| 6 | { |
| 7 | public static function config(): array |
| 8 | { |
| 9 | if (function_exists('gameConfig')) { |
| 10 | return gameConfig(); |
| 11 | } |
| 12 | return require __DIR__.'/game-config.php'; |
| 13 | } |
| 14 | |
| 15 | public static function initial(?int $seed = null, ?array $cfg = null): array |
| 16 | { |
| 17 | $cfg ??= self::config(); |
| 18 | $seed ??= random_int(1, 0x7ffffffe); |
| 19 | $cols = (int)$cfg['grid_cols']; |
| 20 | $rows = (int)$cfg['grid_rows']; |
| 21 | $units = []; |
| 22 | $spawns = []; |
| 23 | foreach (['w', 'b'] as $team) { |
| 24 | foreach ($cfg['spawn'][$team] as $spec) { |
| 25 | [$q, $r] = HexGrid::axial((int)$spec['col'], (int)$spec['row']); |
| 26 | $id = $team.'-'.$spec['id']; |
| 27 | $units[] = [ |
| 28 | 'id' => $id, |
| 29 | 'team' => $team, |
| 30 | 'role' => $spec['role'], |
| 31 | 'weapon' => $spec['weapon'], |
| 32 | 'q' => $q, |
| 33 | 'r' => $r, |
| 34 | 'facing' => (int)$spec['facing'], |
| 35 | 'alive' => true, |
| 36 | 'remaining' => [], |
| 37 | 'stationaryPrevAct' => true, |
| 38 | 'shotTargetPrevAct' => null, |
| 39 | 'deathTick' => null, |
| 40 | ]; |
| 41 | $spawns[] = [$q, $r]; |
| 42 | } |
| 43 | } |
| 44 | $walls = self::generateWalls($seed, $cfg, $spawns); |
| 45 | $lastKnown = ['w' => [], 'b' => []]; |
| 46 | foreach ($units as $u) { |
| 47 | $other = $u['team'] === 'w' ? 'b' : 'w'; |
| 48 | $lastKnown[$other][$u['id']] = ['q' => $u['q'], 'r' => $u['r'], 'facing' => $u['facing'], 'alive' => true]; |
| 49 | } |
| 50 | return [ |
| 51 | 'phase' => 'planning', |
| 52 | 'act' => 0, |
| 53 | 'seed' => $seed, |
| 54 | 'grid' => ['cols' => $cols, 'rows' => $rows, 'orientation' => $cfg['hex_orientation'], 'offset' => $cfg['hex_offset']], |
| 55 | 'walls' => $walls, |
| 56 | 'units' => $units, |
| 57 | 'initialUnits' => $units, |
| 58 | 'plans' => ['w' => null, 'b' => null], |
| 59 | 'drafts' => ['w' => null, 'b' => null], |
| 60 | 'committed' => ['w' => false, 'b' => false], |
| 61 | 'acked' => ['w' => false, 'b' => false], |
| 62 | 'timeline' => null, |
| 63 | 'history' => [], |
| 64 | 'lastKnown' => $lastKnown, |
| 65 | 'result' => null, |
| 66 | ]; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @param list<array{0:int,1:int}> $spawns |
| 71 | * @return list<string> |
| 72 | */ |
| 73 | public static function generateWalls(int $seed, array $cfg, array $spawns): array |
| 74 | { |
| 75 | $cols = (int)$cfg['grid_cols']; |
| 76 | $rows = (int)$cfg['grid_rows']; |
| 77 | $density = (float)$cfg['wall_density']; |
| 78 | $clear = (int)$cfg['wall_clear_radius']; |
| 79 | $attempts = max(1, (int)$cfg['wall_gen_attempts']); |
| 80 | $all = HexGrid::allHexes($cols, $rows); |
| 81 | $protected = []; |
| 82 | foreach ($spawns as [$q, $r]) { |
| 83 | foreach ($all as $key => $_) { |
| 84 | [$qq, $rr] = HexGrid::parse($key); |
| 85 | if (HexGrid::distance($q, $r, $qq, $rr) <= $clear) { |
| 86 | $protected[$key] = true; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | $start = HexGrid::key($spawns[0][0], $spawns[0][1]); |
| 91 | $mid = intdiv(count($spawns), 2); |
| 92 | $goal = HexGrid::key($spawns[$mid][0], $spawns[$mid][1]); |
| 93 | $rng = new SynhexRng($seed); |
| 94 | $walls = []; |
| 95 | for ($try = 0; $try < $attempts; $try++) { |
| 96 | $walls = []; |
| 97 | foreach ($all as $key => $_) { |
| 98 | if (isset($protected[$key])) { |
| 99 | continue; |
| 100 | } |
| 101 | if ($rng->unit() < $density) { |
| 102 | $walls[$key] = true; |
| 103 | } |
| 104 | } |
| 105 | $walkable = $all; |
| 106 | foreach ($walls as $key => $_) { |
| 107 | unset($walkable[$key]); |
| 108 | } |
| 109 | $midCol = intdiv($cols, 2); |
| 110 | if ($density > 0) { |
| 111 | for ($row = 2; $row < $rows - 2; $row++) { |
| 112 | if ($row % 3 === 0) { |
| 113 | continue; |
| 114 | } |
| 115 | [$q, $r] = HexGrid::axial($midCol, $row); |
| 116 | $k = HexGrid::key($q, $r); |
| 117 | if (!isset($protected[$k])) { |
| 118 | $walls[$k] = true; |
| 119 | unset($walkable[$k]); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | [$sq, $sr] = HexGrid::parse($start); |
| 124 | [$gq, $gr] = HexGrid::parse($goal); |
| 125 | if (HexGrid::path($sq, $sr, $gq, $gr, $walkable)) { |
| 126 | break; |
| 127 | } |
| 128 | $density *= 0.85; |
| 129 | } |
| 130 | return array_keys($walls); |
| 131 | } |
| 132 | |
| 133 | public static function isYourTurn(array $s, string $color): bool |
| 134 | { |
| 135 | if (($s['phase'] ?? '') === 'planning') { |
| 136 | return empty($s['committed'][$color]); |
| 137 | } |
| 138 | if (($s['phase'] ?? '') === 'playback') { |
| 139 | return empty($s['acked'][$color]); |
| 140 | } |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | public static function playersToNotify(array $g, array $s): array |
| 145 | { |
| 146 | $out = []; |
| 147 | foreach (['w', 'b'] as $color) { |
| 148 | if (self::isYourTurn($s, $color)) { |
| 149 | $out[] = $color === 'w' ? $g['white'] : ($g['white'] === $g['a'] ? $g['b'] : $g['a']); |
| 150 | } |
| 151 | } |
| 152 | return $out; |
| 153 | } |
| 154 | |
| 155 | public static function walkable(array $s): array |
| 156 | { |
| 157 | $all = HexGrid::allHexes($s['grid']['cols'], $s['grid']['rows']); |
| 158 | foreach ($s['walls'] as $key) { |
| 159 | unset($all[$key]); |
| 160 | } |
| 161 | return $all; |
| 162 | } |
| 163 | |
| 164 | public static function sanitizePlan(array $s, string $color, mixed $plan, array $cfg): array |
| 165 | { |
| 166 | if (!is_array($plan)) { |
| 167 | $plan = []; |
| 168 | } |
| 169 | $unitsIn = $plan['units'] ?? $plan; |
| 170 | if (!is_array($unitsIn)) { |
| 171 | throw new DomainException('Invalid plan.'); |
| 172 | } |
| 173 | $own = []; |
| 174 | foreach ($s['units'] as $u) { |
| 175 | if ($u['team'] === $color && !empty($u['alive'])) { |
| 176 | $own[$u['id']] = $u; |
| 177 | } |
| 178 | } |
| 179 | $walkable = self::walkable($s); |
| 180 | $out = []; |
| 181 | foreach ($unitsIn as $id => $spec) { |
| 182 | if (!is_string($id) || !isset($own[$id]) || !is_array($spec)) { |
| 183 | continue; |
| 184 | } |
| 185 | $path = []; |
| 186 | $prevQ = $own[$id]['q']; |
| 187 | $prevR = $own[$id]['r']; |
| 188 | $rawPath = $spec['path'] ?? []; |
| 189 | if (!is_array($rawPath)) { |
| 190 | throw new DomainException('Invalid path.'); |
| 191 | } |
| 192 | foreach ($rawPath as $step) { |
| 193 | if (!is_array($step) || !isset($step['q'], $step['r'])) { |
| 194 | throw new DomainException('Invalid waypoint.'); |
| 195 | } |
| 196 | $q = (int)$step['q']; |
| 197 | $r = (int)$step['r']; |
| 198 | if (!isset($walkable[HexGrid::key($q, $r)]) || HexGrid::distance($prevQ, $prevR, $q, $r) !== 1) { |
| 199 | throw new DomainException('Path must walk adjacent open hexes.'); |
| 200 | } |
| 201 | $item = ['q' => $q, 'r' => $r]; |
| 202 | if (array_key_exists('facing', $step) && $step['facing'] !== null && $step['facing'] !== '') { |
| 203 | $facing = (int)$step['facing']; |
| 204 | if ($facing < 0 || $facing > 5) { |
| 205 | throw new DomainException('Facing must be 0–5.'); |
| 206 | } |
| 207 | $item['facing'] = $facing; |
| 208 | } |
| 209 | $path[] = $item; |
| 210 | $prevQ = $q; |
| 211 | $prevR = $r; |
| 212 | } |
| 213 | $entry = ['path' => $path]; |
| 214 | if (isset($spec['facing']) && is_int($spec['facing'])) { |
| 215 | if ($spec['facing'] < 0 || $spec['facing'] > 5) { |
| 216 | throw new DomainException('Facing must be 0–5.'); |
| 217 | } |
| 218 | $entry['facing'] = $spec['facing']; |
| 219 | } |
| 220 | $out[$id] = $entry; |
| 221 | } |
| 222 | return ['units' => $out]; |
| 223 | } |
| 224 | |
| 225 | public static function saveDraft(array $s, string $color, mixed $plan, array $cfg): array |
| 226 | { |
| 227 | if (($s['phase'] ?? '') !== 'planning' || !empty($s['committed'][$color])) { |
| 228 | throw new DomainException('Plans are locked for this act.'); |
| 229 | } |
| 230 | $s['drafts'][$color] = self::sanitizePlan($s, $color, $plan, $cfg); |
| 231 | return $s; |
| 232 | } |
| 233 | |
| 234 | public static function commit(array $s, string $color, mixed $plan, array $cfg): array |
| 235 | { |
| 236 | if (($s['phase'] ?? '') !== 'planning') { |
| 237 | throw new DomainException('This act is not in planning.'); |
| 238 | } |
| 239 | if (!empty($s['committed'][$color])) { |
| 240 | throw new DomainException('You have already locked this plan.'); |
| 241 | } |
| 242 | $s['plans'][$color] = self::sanitizePlan($s, $color, $plan, $cfg); |
| 243 | $s['drafts'][$color] = $s['plans'][$color]; |
| 244 | $s['committed'][$color] = true; |
| 245 | if (!empty($s['committed']['w']) && !empty($s['committed']['b'])) { |
| 246 | $s = SynhexSim::runAct($s, $cfg); |
| 247 | } |
| 248 | return $s; |
| 249 | } |
| 250 | |
| 251 | public static function ackPlayback(array $s, string $color): array |
| 252 | { |
| 253 | if (($s['phase'] ?? '') !== 'playback') { |
| 254 | throw new DomainException('There is no act to play back.'); |
| 255 | } |
| 256 | $s['acked'][$color] = true; |
| 257 | if (!empty($s['acked']['w']) && !empty($s['acked']['b'])) { |
| 258 | if (!empty($s['result'])) { |
| 259 | $s['phase'] = 'ended'; |
| 260 | } else { |
| 261 | $s['phase'] = 'planning'; |
| 262 | $s['timeline'] = null; |
| 263 | $s['acked'] = ['w' => false, 'b' => false]; |
| 264 | foreach (['w', 'b'] as $team) { |
| 265 | $draft = ['units' => []]; |
| 266 | foreach ($s['units'] as $u) { |
| 267 | if ($u['team'] === $team && !empty($u['alive']) && !empty($u['remaining'])) { |
| 268 | $draft['units'][$u['id']] = ['path' => $u['remaining']]; |
| 269 | } |
| 270 | } |
| 271 | $s['drafts'][$team] = $draft['units'] ? $draft : null; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | return $s; |
| 276 | } |
| 277 | |
| 278 | public static function visibleUnits(array $s, string $color, array $cfg): array |
| 279 | { |
| 280 | $out = []; |
| 281 | $walls = array_fill_keys($s['walls'], true); |
| 282 | $byId = []; |
| 283 | foreach ($s['units'] as $u) { |
| 284 | $byId[$u['id']] = $u; |
| 285 | } |
| 286 | $revealAll = ($s['act'] === 0 && ($s['phase'] ?? '') === 'planning' && !empty($cfg['see_enemies_at_match_start'])); |
| 287 | foreach ($s['units'] as $u) { |
| 288 | if ($u['team'] === $color) { |
| 289 | $row = $u; |
| 290 | $row['hologram'] = false; |
| 291 | $row['known'] = true; |
| 292 | $out[] = $row; |
| 293 | continue; |
| 294 | } |
| 295 | $visible = $revealAll || self::seenNow($byId, $u, $color, $walls, $cfg); |
| 296 | if ($visible) { |
| 297 | $row = $u; |
| 298 | $row['hologram'] = false; |
| 299 | $row['known'] = true; |
| 300 | $out[] = $row; |
| 301 | continue; |
| 302 | } |
| 303 | $ghost = $s['lastKnown'][$color][$u['id']] ?? null; |
| 304 | if ($ghost) { |
| 305 | $out[] = [ |
| 306 | 'id' => $u['id'], |
| 307 | 'team' => $u['team'], |
| 308 | 'role' => $u['role'], |
| 309 | 'weapon' => $u['weapon'], |
| 310 | 'q' => $ghost['q'], |
| 311 | 'r' => $ghost['r'], |
| 312 | 'facing' => $ghost['facing'], |
| 313 | 'alive' => $ghost['alive'] ?? true, |
| 314 | 'hologram' => true, |
| 315 | 'known' => true, |
| 316 | ]; |
| 317 | } |
| 318 | } |
| 319 | return $out; |
| 320 | } |
| 321 | |
| 322 | private static function seenNow(array $byId, array $enemy, string $viewer, array $walls, array $cfg): bool |
| 323 | { |
| 324 | if (empty($enemy['alive'])) { |
| 325 | return false; |
| 326 | } |
| 327 | foreach ($byId as $u) { |
| 328 | if ($u['team'] !== $viewer || empty($u['alive'])) { |
| 329 | continue; |
| 330 | } |
| 331 | $d = HexGrid::distance($u['q'], $u['r'], $enemy['q'], $enemy['r']); |
| 332 | if ($d === 0 || $d > (int)$cfg['vision_range']) { |
| 333 | continue; |
| 334 | } |
| 335 | if (HexGrid::inCone($u['q'], $u['r'], (int)$u['facing'], $enemy['q'], $enemy['r'], (float)$cfg['vision_cone_half_deg']) |
| 336 | && HexGrid::los($u['q'], $u['r'], $enemy['q'], $enemy['r'], $walls)) { |
| 337 | return true; |
| 338 | } |
| 339 | } |
| 340 | return false; |
| 341 | } |
| 342 | |
| 343 | public static function clientView(array $s, string $color, array $cfg): array |
| 344 | { |
| 345 | $view = [ |
| 346 | 'phase' => $s['phase'], |
| 347 | 'act' => $s['act'], |
| 348 | 'grid' => $s['grid'], |
| 349 | 'walls' => $s['walls'], |
| 350 | 'units' => self::visibleUnits($s, $color, $cfg), |
| 351 | 'committed' => !empty($s['committed'][$color]), |
| 352 | 'opponentCommitted' => !empty($s['committed'][$color === 'w' ? 'b' : 'w']), |
| 353 | 'acked' => !empty($s['acked'][$color]), |
| 354 | 'opponentAcked' => !empty($s['acked'][$color === 'w' ? 'b' : 'w']), |
| 355 | 'draft' => $s['drafts'][$color] ?? $s['plans'][$color] ?? ['units' => []], |
| 356 | 'timeline' => ($s['phase'] === 'playback') ? $s['timeline'] : null, |
| 357 | 'result' => $s['result'] ?? null, |
| 358 | 'rules' => [ |
| 359 | 'walkRange' => (int)$cfg['walk_range'], |
| 360 | 'actDurationMs' => (int)$cfg['act_duration_ms'], |
| 361 | 'visionConeHalfDeg' => (float)$cfg['vision_cone_half_deg'], |
| 362 | 'weapons' => [ |
| 363 | 'pistol' => ['rofMs' => (int)$cfg['weapon_pistol_rof_ms']], |
| 364 | 'smg' => ['rofMs' => (int)$cfg['weapon_smg_rof_ms']], |
| 365 | 'shotgun' => ['rofMs' => (int)$cfg['weapon_shotgun_rof_ms'], 'pellets' => (int)$cfg['weapon_shotgun_pellets']], |
| 366 | ], |
| 367 | ], |
| 368 | ]; |
| 369 | return $view; |
| 370 | } |
| 371 | |
| 372 | public static function replayFrames(array $s): array |
| 373 | { |
| 374 | $frames = [['act' => 0, 'units' => $s['initialUnits'] ?? $s['units'], 'walls' => $s['walls'], 'grid' => $s['grid'], 'timeline' => null]]; |
| 375 | foreach ($s['history'] ?? [] as $i => $act) { |
| 376 | $frames[] = [ |
| 377 | 'act' => $i + 1, |
| 378 | 'units' => $act['endUnits'] ?? $s['units'], |
| 379 | 'walls' => $s['walls'], |
| 380 | 'grid' => $s['grid'], |
| 381 | 'timeline' => $act, |
| 382 | ]; |
| 383 | } |
| 384 | return $frames; |
| 385 | } |
| 386 | |
| 387 | public static function unitBar(array $s): array |
| 388 | { |
| 389 | $cfg = self::config(); |
| 390 | $totals = ['w' => 0, 'b' => 0]; |
| 391 | foreach ($s['units'] as $u) { |
| 392 | if (empty($u['alive'])) { |
| 393 | continue; |
| 394 | } |
| 395 | $totals[$u['team']] += ($u['role'] === 'vip') ? (int)$cfg['unit_bar_vip'] : (int)$cfg['unit_bar_soldier']; |
| 396 | } |
| 397 | return $totals; |
| 398 | } |
| 399 | } |
projects-timfalken-com microchess