BlueGit

projects-timfalken-com microchess

Files

microchess / web / tests / synhex.php

synhex.php

feat: scaffold Synhex hex tactics on the Microchess lobby shell
timfa project_86226080_bot_b6e0858ac0a12dc6d49a8d25b3b0be9b · pushed by timfa · 2 months ago
24f20754 History
synhex.php 4.9 KiB Download Raw
1<?php
2declare(strict_types=1);
3require dirname(__DIR__).'/lib/HexGrid.php';
4require dirname(__DIR__).'/lib/SynhexSim.php';
5require dirname(__DIR__).'/lib/SynhexMatch.php';
6$checks=0;
7function check(bool $ok,string $label): void {global $checks;$checks++;if(!$ok)throw new RuntimeException($label);}
8
9$cfg=SynhexMatch::config();
10check($cfg['grid_cols']===17 && $cfg['grid_rows']===14,'Documented 17×14 odd-r board');
11check($cfg['walk_range']===5 && $cfg['act_duration_ms']===3000,'Walk 5 hexes / 3s act');
12check($cfg['base_accuracy']===15,'Base accuracy 15%');
13check($cfg['weapon_shotgun_pellets']>=2,'Shotgun fires multiple pellets');
14check($cfg['vision_cone_half_deg']===60,'120° cone (half-angle 60)');
15check(count($cfg['spawn']['w'])===4 && count($cfg['spawn']['b'])===4,'Four units per side');
16check($cfg['spawn']['w'][0]['role']==='vip' && $cfg['spawn']['w'][0]['weapon']==='pistol','VIP pistol');
17$weapons=array_column($cfg['spawn']['w'],'weapon');
18check($weapons===['pistol','smg','smg','shotgun'],'VIP + 2 SMG + shotgun');
19
20check(HexGrid::distance(0,0,1,0)===1,'East neighbor is 1');
21check(HexGrid::distance(0,0,-1,0)===1,'West neighbor is 1');
22[$q,$r]=HexGrid::axial(3,4);[$c,$row]=HexGrid::offset($q,$r);
23check($c===3 && $row===4,'Offset/axial round-trip');
24check(HexGrid::inCone(0,0,0,2,0,60),'Forward hex is in cone');
25check(!HexGrid::inCone(0,0,0,-2,0,60),'Rear hex is outside cone');
26$walls=[HexGrid::key(1,0)=>true];
27check(!HexGrid::los(0,0,2,0,$walls),'Wall blocks LoS');
28check(HexGrid::los(0,0,0,1,[]),'Open LoS');
29$walk=HexGrid::allHexes(8,6);
30unset($walk[HexGrid::key(...HexGrid::axial(3,2))]);
31[$q1,$r1]=HexGrid::axial(0,2);[$q2,$r2]=HexGrid::axial(6,2);
32$path=HexGrid::path($q1,$r1,$q2,$r2,$walk);
33check($path!==null && count($path)>2,'A* walks around a wall');
34
35$s=SynhexMatch::initial(24,$cfg);
36check(count($s['units'])===8,'Eight operatives');
37check($s['grid']['cols']===17 && $s['phase']==='planning','Planning on configured grid');
38check(count(array_filter($s['units'],fn($u)=>$u['role']==='vip'))===2,'Two VIPs');
39check(SynhexMatch::isYourTurn($s,'w') && SynhexMatch::isYourTurn($s,'b'),'Both plan at once');
40foreach ($s['walls'] as $key) check(is_string($key) && str_contains($key,','),'Walls stored as q,r keys');
41
42$s=SynhexMatch::commit($s,'w',['units'=>[]],$cfg);
43check(!SynhexMatch::isYourTurn($s,'w') && SynhexMatch::isYourTurn($s,'b'),'First lock waits for opponent');
44$s=SynhexMatch::commit($s,'b',['units'=>[]],$cfg);
45check(($s['phase']??'')==='playback','Both locks run the act');
46check((int)$s['timeline']['durationMs']===3000,'Playback lasts one act');
47check(isset($s['timeline']['snapshots'][0]['units']),'Timeline has snapshots');
48$again=SynhexMatch::initial(24,$cfg);
49$again=SynhexMatch::commit($again,'w',['units'=>[]],$cfg);
50$again=SynhexMatch::commit($again,'b',['units'=>[]],$cfg);
51check($again['timeline']['shots']===$s['timeline']['shots'],'Same seed is deterministic');
52
53$s=SynhexMatch::ackPlayback($s,'w');
54check(!SynhexMatch::isYourTurn($s,'w') && SynhexMatch::isYourTurn($s,'b'),'Playback ack waits for opponent');
55$s=SynhexMatch::ackPlayback($s,'b');
56check($s['phase']==='planning' || $s['phase']==='ended','After both Play, next plan or ended');
57
58$tiny=$cfg;
59$tiny['grid_cols']=7;$tiny['grid_rows']=5;$tiny['wall_density']=0;$tiny['wall_gen_attempts']=1;
60$tiny['spawn']=['w'=>[['id'=>'vip','role'=>'vip','weapon'=>'pistol','col'=>1,'row'=>2,'facing'=>0]],'b'=>[['id'=>'vip','role'=>'vip','weapon'=>'pistol','col'=>5,'row'=>2,'facing'=>3]]];
61$tiny['base_accuracy']=100;$tiny['accuracy_cap']=100;$tiny['shot_range']=10;$tiny['vision_range']=10;
62$duel=SynhexMatch::initial(7,$tiny);
63$duel=SynhexMatch::commit($duel,'w',['units'=>[]],$tiny);
64$duel=SynhexMatch::commit($duel,'b',['units'=>[]],$tiny);
65check($duel['timeline']['shots']!==[],'Facing VIPs fire when accuracy is certain');
66check($duel['result']!==null,'VIP death produces a result after the act');
67
68$shotCfg=$cfg;$shotCfg['weapon_shotgun_pellets']=5;
69$st=SynhexMatch::initial(3,$shotCfg);
70$shot=null;
71foreach ($st['units'] as $u) if ($u['weapon']==='shotgun') $shot=$u;
72check($shot!==null,'Shotgun unit exists');
73$alive=array_values(array_filter($st['units'],fn($u)=>$u['team']!==$shot['team']));
74$target=$alive[0];
75$aims=new ReflectionClass(SynhexSim::class);
76$m=$aims->getMethod('pelletAims');
77$m->setAccessible(true);
78$points=$m->invoke(null,$shot,$target,5,(int)$shotCfg['weapon_shotgun_spread']);
79check(count($points)===5,'Shotgun volley has five rays');
80
81$bad=SynhexMatch::initial(1,$cfg);
82try {SynhexMatch::sanitizePlan($bad,'w',['units'=>['w-vip'=>['path'=>[['q'=>99,'r'=>99]]]]],$cfg);check(false,'Rejects illegal path');}
83catch (DomainException) {check(true,'Rejects illegal path');}
84
85$view=SynhexMatch::clientView($st,'w',$cfg);
86check(isset($view['grid'],$view['units'],$view['rules']['walkRange']),'Client view exposes board + tunables');
87$own=array_filter($view['units'],fn($u)=>$u['team']==='w' && empty($u['hologram']));
88check(count($own)===4,'Viewer always sees own units');
89
90echo "Synhex: $checks assertions passed.\n";