param([int]$Port = 18080, [string]$WorldId = 'world-001', [int]$Seed = 734291, [switch]$UseExisting) $ErrorActionPreference = 'Stop' $root = [IO.Path]::GetFullPath((Join-Path $PSScriptRoot '..')) if ($WorldId -notmatch '^[a-z][a-z0-9-]{0,30}$') { throw 'Invalid world ID.' } if (-not (Test-Path -LiteralPath (Join-Path $root 'client\dist\index.html'))) { throw 'Run npm ci and npm run build first.' } $listener = [Net.Sockets.TcpListener]::new([Net.IPAddress]::Loopback, $Port) try { $listener.Start() } finally { $listener.Stop() } New-Item -ItemType Directory -Force -Path (Join-Path $root '.local') | Out-Null $exe = Join-Path $root ".local\$WorldId.exe" $env:GOCACHE = Join-Path $root '.local\go-cache' if (-not $UseExisting) { $go = Join-Path $root '.tools\go\bin\go.exe' if (-not (Test-Path -LiteralPath $go)) { $go = (Get-Command go -ErrorAction Stop).Source } Push-Location (Join-Path $root 'server') try { & $go build -o $exe ./cmd/world; if ($LASTEXITCODE -ne 0) { throw 'Go build failed.' } } finally { Pop-Location } } if (-not (Test-Path -LiteralPath $exe)) { throw "Missing server binary: $exe" } $env:APP_ENV = 'development' $env:LISTEN_ADDR = "127.0.0.1:$Port" $env:PUBLIC_URL = "http://127.0.0.1:$Port" $env:CLIENT_DIR = Join-Path $root 'client\dist' $env:DATABASE_URL = 'file:' + ((Join-Path $root '.local\aeternalis.db') -replace '\\','/') $env:WORLD_ID = $WorldId $env:WORLD_SEED = "$Seed" $process = Start-Process -FilePath $exe -WorkingDirectory (Join-Path $root 'server') -WindowStyle Hidden -PassThru -RedirectStandardOutput (Join-Path $root ".local\$WorldId.stdout.log") -RedirectStandardError (Join-Path $root ".local\$WorldId.stderr.log") Write-Output "World ${WorldId}: http://127.0.0.1:$Port (PID $($process.Id))"