@echo off
setlocal EnableDelayedExpansion
cd /d "%~dp0"
title Netcade - Dedicated Server + Clients
color 0B

REM ===========================================================================
REM  NETCADE MULTIPLAYER TEST LAUNCHER
REM
REM  Launches a REAL dedicated server and REAL clients as SEPARATE PROGRAMS,
REM  over a REAL socket, with SIMULATED LAG.
REM
REM  WHY THIS EXISTS
REM  ---------------
REM  Play-In-Editor runs your server and client inside ONE program, so they
REM  share memory. Code that replicates nothing can still look like it works.
REM  Localhost also gives you 0ms ping and 0% packet loss, which no human has.
REM
REM  "Run Under One Process" is an EDITOR preference. This .bat ignores it
REM  completely - it starts two separate .exe processes at the OS level. You
REM  cannot accidentally misconfigure your way back into the lie.
REM
REM  Network emulation works in DEVELOPMENT builds only, never in Shipping.
REM  Epic's own test conditions: 500ms round-trip, 10% packet loss.
REM ===========================================================================

REM ------------------- EDIT THESE -------------------
set "UE=C:\Program Files\Epic Games\UE_5.8\Engine\Binaries\Win64\UnrealEditor.exe"
set "PROJECT=%~dp0NetcadeYT.uproject"
set "MAP=Lvl_ThirdPerson"
set "PORT=7777"
set "MAXTRIES=120"
REM --------------------------------------------------

if not exist "%UE%" (
  echo.
  echo   Can't find Unreal at:  %UE%
  echo   Open this .bat in Notepad and fix the UE= line at the top.
  echo.
  pause & exit /b 1
)
if not exist "%PROJECT%" (
  echo.
  echo   Can't find the project:  %PROJECT%
  echo   Put this .bat next to your .uproject file.
  echo.
  pause & exit /b 1
)

echo.
echo   =========================================================
echo     NETCADE  -  DEDICATED SERVER + CLIENTS
echo   =========================================================

REM ---------------- how many clients ----------------
echo.
set "CLIENTS="
set /p "CLIENTS=  How many clients? [1-6, default 2]: "
if "%CLIENTS%"=="" set "CLIENTS=2"
set /a CLIENTS=CLIENTS+0
if %CLIENTS% LSS 1 set "CLIENTS=1"
if %CLIENTS% GTR 6 set "CLIENTS=6"

REM ---------------- network conditions ----------------
echo.
echo   Network conditions:
echo.
echo     1) Perfect      0 ms          - localhost. This is the lie.
echo     2) Good        40 ms          - fibre, same country
echo     3) Average    120 ms,  1%%     - a normal player
echo     4) Bad        300 ms,  3%%     - wifi, far away
echo     5) Brutal     500 ms, 10%%     - Epic's own test conditions
echo     6) Custom
echo.
set "NET="
set /p "NET=  Choose [3]: "
if "%NET%"=="" set "NET=3"

REM PktLag is ONE-WAY delay in ms. Applied to BOTH ends, so the round-trip
REM ping you actually feel is roughly DOUBLE the number below.
set "EMU="
if "%NET%"=="1" set "EMU="
if "%NET%"=="2" set "EMU=-PktLag=20 -PktLagVariance=5"
if "%NET%"=="3" set "EMU=-PktLag=60 -PktLagVariance=15 -PktLoss=1"
if "%NET%"=="4" set "EMU=-PktLag=150 -PktLagVariance=40 -PktLoss=3"
if "%NET%"=="5" set "EMU=-PktLag=250 -PktLagVariance=50 -PktLoss=10"

if "%NET%"=="6" (
  echo.
  set /p "LAG=  One-way lag in ms [120]: "
  set /p "VAR=  Lag variance in ms [30]: "
  set /p "LOSS=  Packet loss %% [2]: "
  if "!LAG!"==""  set "LAG=120"
  if "!VAR!"==""  set "VAR=30"
  if "!LOSS!"=="" set "LOSS=2"
  set "EMU=-PktLag=!LAG! -PktLagVariance=!VAR! -PktLoss=!LOSS!"
)

echo.
echo   ------------------------------------------------
echo     Map      : %MAP%
echo     Clients  : %CLIENTS%
if defined EMU (
  echo     Network  : %EMU%
  echo                ^(server AND clients - round trip is about 2x the lag^)
) else (
  echo     Network  : none. Zero lag. This is not a real test.
)
echo   ------------------------------------------------
echo.
pause

REM ---------------- dedicated server ----------------
echo.
echo   [1/2] Dedicated server ...
netstat -an | findstr ":%PORT%" >nul 2>&1
if not errorlevel 1 (
    echo         Already listening on %PORT% - reusing it.
    goto clients
)

echo         Starting headless dedicated server ...
start "DEDICATED SERVER" "%UE%" "%PROJECT%" %MAP% -server -port=%PORT% -log %EMU%

echo         Waiting for port %PORT% ^(first run compiles shaders - be patient^) ...
set /a TRIES=0
:waitloop
ping -n 3 127.0.0.1 >nul
netstat -an | findstr ":%PORT%" >nul 2>&1
if not errorlevel 1 goto serverready
set /a TRIES+=1
<nul set /p "=."
if !TRIES! GEQ %MAXTRIES% (
    echo.
    echo         Server never bound the port. Check its log window.
    echo.
    pause & exit /b 1
)
goto waitloop

:serverready
echo.
echo         Server is listening on %PORT%.

REM ---------------- clients ----------------
:clients
echo.
echo   [2/2] Launching %CLIENTS% client^(s^) ...
for /l %%i in (1,1,%CLIENTS%) do (
  echo         client #%%i ...
  start "CLIENT %%i" "%UE%" "%PROJECT%" 127.0.0.1:%PORT% -game -log -windowed -ResX=1280 -ResY=720 %EMU%
  if not %%i==%CLIENTS% ping -n 4 127.0.0.1 >nul
)

echo.
echo   ================================================
echo     Running. Server + %CLIENTS% client(s).
echo.
echo     Change lag LIVE in any client console (~ key):
echo        NetEmulation.PktLag 400
echo        NetEmulation.PktLoss 15
echo        NetEmulation.Off
echo   ================================================
echo.
echo     *** PRESS ANY KEY IN THIS WINDOW TO KILL   ***
echo     *** THE SERVER AND EVERY CLIENT AT ONCE.   ***
echo.
pause >nul

REM ---------------- teardown ----------------
REM Kills every UnrealEditor process this .bat started (server + all clients),
REM but leaves your actual Unreal EDITOR alone -- the editor is UnrealEditor.exe
REM too, so we only kill the ones running with -game or -server on the cmdline.
echo.
echo   Shutting everything down...

for /f "skip=1 tokens=1" %%p in ('wmic process where "name='UnrealEditor.exe'" get ProcessId 2^>nul') do (
  for /f "delims=" %%c in ('wmic process where "ProcessId=%%p" get CommandLine /value 2^>nul ^| find "="') do (
    echo %%c | find /i "-server" >nul && taskkill /F /PID %%p >nul 2>&1
    echo %%c | find /i "-game"   >nul && taskkill /F /PID %%p >nul 2>&1
  )
)

echo   Done. Server and clients terminated. Your editor is untouched.
timeout /t 2 /nobreak >nul
endlocal
