#include "MW4DUIHeaders.hpp"
#include <windows.h>
#include "DedicatedUI.hpp"
#include "resource.h"
#include <buildnum\buildnum.h>
#include "..\ScriptStrings\resource.h" // scriptstrings.h - jcem

extern HINSTANCE g_hInst;
extern HWND g_hWndMain;
extern HWND g_hWndSetup;
extern HWND g_hWndLobby;
extern HWND g_hWndServer;
extern DWORD g_langResources;
extern HANDLE g_hThreadEditor;
extern int MechMessageBoxWin32(char *szTitle, char *szMessage, UINT uType=MB_OK);


INT CALLBACK CDedicatedServerUI::RestrictionProc(
  HWND hwndDlg,  // handle to dialog box
  UINT uMsg,     // message
  WPARAM wParam, // first message parameter
  LPARAM lParam  // second message parameter
)
{
    switch( uMsg )
    {
	case WM_INITDIALOG: 
		{
		int* intParams[2];
		int param_id = ALLOWED_MECH_PARAMETER;
		int i;
		intParams[0] = &param_id;
		intParams[1] = &i;
		
		//set value for checkboxes
		for (i= 0; i <= IDC_CHECK_MECH20-IDC_CHECK_MECH0; i++)
		{
			int allowed = MW4Shell::GetLocalNetworkMissionParamater(NULL, 2, (void**)intParams);
			SendMessage(GetDlgItem(hwndDlg, i+IDC_CHECK_MECH0), BM_SETCHECK, allowed?BST_UNCHECKED:BST_CHECKED, 0);
		}

		int index;
		param_id = ALLOWED_BEAM_WEAPON_PARAMETER;
		for (index=0, i=FirstBeamID; i<=LastBeamID; i++, index++)
		{
			int allowed = MW4Shell::GetLocalNetworkMissionParamater(NULL, 2, (void**)intParams);
			SendMessage(GetDlgItem(hwndDlg, index+IDC_CHECK_BEAM0), BM_SETCHECK, allowed?BST_UNCHECKED:BST_CHECKED, 0);
		}

		param_id = ALLOWED_MISSILE_WEAPON_PARAMETER;
		for (index=0, i=FirstMissileID; i<=LastMissileID; i++, index++)
		{
			int allowed = MW4Shell::GetLocalNetworkMissionParamater(NULL, 2, (void**)intParams);
			SendMessage(GetDlgItem(hwndDlg, index+IDC_CHECK_MISSILE0), BM_SETCHECK, allowed?BST_UNCHECKED:BST_CHECKED, 0);
		}

		param_id = ALLOWED_PROJECTILE_WEAPON_PARAMETER;
		for (index=0, i=FirstProjectileID; i<=LastProjectileID; i++, index++)
		{
			int allowed = MW4Shell::GetLocalNetworkMissionParamater(NULL, 2, (void**)intParams);
			SendMessage(GetDlgItem(hwndDlg, index+IDC_CHECK_BALLISTIC0), BM_SETCHECK, allowed?BST_UNCHECKED:BST_CHECKED, 0);
		}

		param_id = ALLOWED_GENERIC_SUBSYSTEM_PARAMETER;
		for (index=0, i=FirstGenericSubsystemID; i<=LastGenericSubsystemID; i++, index++)
		{
			int allowed = MW4Shell::GetLocalNetworkMissionParamater(NULL, 2, (void**)intParams);
			SendMessage(GetDlgItem(hwndDlg, index+IDC_CHECK_COMP0), BM_SETCHECK, allowed?BST_UNCHECKED:BST_CHECKED, 0);
		}

		return TRUE;
		}

	case WM_COMMAND:
		switch( LOWORD(wParam) )
		{
		case IDCANCEL:
			EndDialog( hwndDlg, TRUE );
			break;
		case IDOK:
		{
			int* intParams[3];
			int param_id = ALLOWED_MECH_PARAMETER;
			int i;
			int allowed;
			intParams[0] = &param_id;
			intParams[1] = &i;
			intParams[2] = &allowed;

			//set value for checkboxes
			for (i = 0; i <= IDC_CHECK_MECH20-IDC_CHECK_MECH0; i++)
			{
				allowed = SendMessage(GetDlgItem(hwndDlg, i+IDC_CHECK_MECH0), BM_GETCHECK, 0, 0)==BST_UNCHECKED;
				MW4Shell::SetNetworkMissionParamater(NULL, 3, (void**)intParams);
			}
			
			int index;
			param_id = ALLOWED_BEAM_WEAPON_PARAMETER;;
			for (index=0, i=FirstBeamID; i<=LastBeamID; i++, index++)
			{
				allowed = (SendMessage(GetDlgItem(hwndDlg, index+IDC_CHECK_BEAM0), BM_GETCHECK, 0, 0)==BST_UNCHECKED);
				MW4Shell::SetNetworkMissionParamater(NULL, 3, (void**)intParams);
			}
			
			param_id = ALLOWED_MISSILE_WEAPON_PARAMETER;
			for (index=0, i=FirstMissileID; i<=LastMissileID; i++, index++)
			{
				allowed = (SendMessage(GetDlgItem(hwndDlg, index+IDC_CHECK_MISSILE0), BM_GETCHECK, 0, 0)==BST_UNCHECKED);
				MW4Shell::SetNetworkMissionParamater(NULL, 3, (void**)intParams);
			}
			
			param_id = ALLOWED_PROJECTILE_WEAPON_PARAMETER;
			for (index=0, i=FirstProjectileID; i<=LastProjectileID; i++, index++)
			{
				allowed = (SendMessage(GetDlgItem(hwndDlg, index+IDC_CHECK_BALLISTIC0), BM_GETCHECK, 0, 0)==BST_UNCHECKED);
				MW4Shell::SetNetworkMissionParamater(NULL, 3, (void**)intParams);
			}
			
			param_id = ALLOWED_GENERIC_SUBSYSTEM_PARAMETER;
			for (index=0, i=FirstGenericSubsystemID; i<=LastGenericSubsystemID; i++, index++)
			{
				allowed = (SendMessage(GetDlgItem(hwndDlg, index+IDC_CHECK_COMP0), BM_GETCHECK, 0, 0)==BST_UNCHECKED);
				MW4Shell::SetNetworkMissionParamater(NULL, 3, (void**)intParams);
			}
			
			EndDialog( hwndDlg, TRUE );
			break;
		}
		}
		break;
	}
	return FALSE;
}

INT CALLBACK CDedicatedServerUI::HostSetupProc(
  HWND hwndDlg,  // handle to dialog box
  UINT uMsg,     // message
  WPARAM wParam, // first message parameter
  LPARAM lParam  // second message parameter
)
{
    switch( uMsg )
    {
	case WM_INITDIALOG: 
		{
			InitHostSetup(hwndDlg);
			return TRUE;
		}

	case WM_COMMAND:
		switch( HIWORD(wParam) )
		{
		case BN_CLICKED:
			switch (LOWORD(wParam)) {
			case IDC_CHECK_PASSWORD:
				OnCheckPassword(hwndDlg);
				return 0;
			case IDC_CHECK_JOINPROGRESS:
				OnCheckJoinInProgress(hwndDlg);
				return 0;
			case IDC_CHECK_SERVERRECYCLE:
				OnCheckServerRecycle(hwndDlg);
				return 0;
			case IDC_CHECK_RESTRICT:
				EnableWindow(GetDlgItem(hwndDlg, IDC_BUTTON_RESTRICT),
					SendMessage(GetDlgItem(hwndDlg, IDC_CHECK_RESTRICT), BM_GETCHECK, 0, 0)==BST_CHECKED);
				return 0;

			}
			break;
		case CBN_SELCHANGE:
			switch (LOWORD(wParam)) {
			case IDC_COMBO_MAXPLAYERS:
				OnMaxPlayerChange(hwndDlg);
				return 0;
			}
			break;
		}

		switch( LOWORD(wParam) )
		{
			case IDC_SERVER_NAME:
			case IDC_EDIT_PASSWORD:
			{
				OnCheckPassword(hwndDlg);
				return 0;
			}
			case IDC_BUTTON_RESTRICT:
			{
				DialogBox(g_hInst, 
					MAKEINTRESOURCE(IDD_RESTRICTIONS),
					g_hWndMain,
					RestrictionProc);
				return 0;
			}
			case IDSTART:
			{
				HCURSOR oldCursor = GetCursor();
				HCURSOR cursor = LoadCursor(NULL, IDC_WAIT);
				SetCursor(cursor);

				if (!HostSetupOnStart(hwndDlg))
					return 0;

				if (!g_hWndLobby) {
					g_hWndLobby = ::CreateDialog(g_hInst, 
						MAKEINTRESOURCE(IDD_GAMELOBBY),
						g_hWndMain,
						GameLobbyProc);
				}
				else {
					SendMessage(g_hWndLobby, WM_INITDIALOG, 0, 0);
				}
				EndDialog(hwndDlg, TRUE);
				ShowWindow(g_hWndLobby, SW_SHOW);
				SetCursor(oldCursor);
				return 0;
			}
			case IDBACK:
			{
				EndDialog( hwndDlg, TRUE );
				ShowWindow(g_hWndServer, SW_SHOW);
				return 0;
			}
			case IDEDITMISSION:
			{
				if (g_hThreadEditor && WAIT_TIMEOUT != ::WaitForSingleObject(g_hThreadEditor, 0)) {
					g_hThreadEditor = NULL;
				}
				if (!g_hThreadEditor) {
					STARTUPINFO sInfo;
					::ZeroMemory(&sInfo, sizeof(STARTUPINFO));
					sInfo.cb = sizeof(STARTUPINFO);
					sInfo.dwFlags = STARTF_USESHOWWINDOW;
					sInfo.wShowWindow = SW_SHOW;
					
					PROCESS_INFORMATION pInfo;
					if (::CreateProcess(NULL, "nfoeditor.exe", NULL, NULL, FALSE, 0, NULL, NULL, &sInfo, &pInfo)) {
						g_hThreadEditor = pInfo.hThread;
					}
				}
				return 0;
			}
		}
		return 0;

	case WM_CLOSE:
		Quit();
		EndDialog( hwndDlg, TRUE );
		break;
	}
	return FALSE;
}

int join_times[9] = {1, 2, 5, 10, 15, 30, 45, 1000};
int recycle_times[9] = {30, 60, 120, 300, 600};
void CDedicatedServerUI::InitHostSetup(HWND hWnd)
{
	FREE_PTR(ServerIPAddress);
	ServerIPAddress = (char *) gos_Malloc(STRING_SIZE);
	Str_Copy(ServerIPAddress, Environment.ZoneMatchServerIP, STRING_SIZE);
	GetHostOptions(NULL, 0, NULL);
	
	SetGameNameDefault(NULL, 0, NULL);
	SetPlayerNameDefault(NULL, 0, NULL);
	SetDlgItemText(hWnd, IDC_SERVER_NAME, GameName);
	SendMessage(GetDlgItem(hWnd, IDC_SERVER_NAME), EM_LIMITTEXT, 24, 0);

	SendMessage(GetDlgItem(hWnd, IDC_EDIT_PASSWORD), EM_LIMITTEXT, 16, 0);
	OnCheckPassword(hWnd);

	int* intParam[2];
	int temp = REPORTSTATS_PARAMETER;
	int value;
	intParam[0] = &temp;
	intParam[1] = &value;

	SendMessage(GetDlgItem(hWnd, IDC_CHECK_STATSTOZONE), BM_SETCHECK,
		MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam)?BST_CHECKED:BST_UNCHECKED, 0);

	SendMessage(GetDlgItem(hWnd, IDC_COMBO_TIMELIMIT), CB_RESETCONTENT, 0 ,0);
	MString limits[] = {"1 ", "2 ", "5 ", "10 ", "15 ", "30 ", "45 "};
	for (int i=0; i<7; i++) {
		MString temp = limits[i] + gos_GetResourceString(g_langResources,IDS_MP_HG_MIN);
		SendMessage(GetDlgItem(hWnd, IDC_COMBO_TIMELIMIT), CB_INSERTSTRING, i, (LPARAM)(char*)temp);
	}
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_TIMELIMIT), CB_INSERTSTRING, 7, (LPARAM)(char*)gos_GetResourceString(g_langResources,IDS_IA_UNLIMITED));
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_TIMELIMIT), CB_SETCURSEL, 7, 0);

	temp = JOIN_IN_PROGRESS_CUTOFF_PARAMETER;
	if (MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam) != 0)
	{
		temp = JOIN_IN_PROGRESS_CUTOFF_TIME_PARAMETER;
		int value = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
		for (int z = 0; z< 7; z++)
		{
			if (join_times[z] >= value)
			{
				SendMessage(GetDlgItem(hWnd, IDC_COMBO_TIMELIMIT), CB_SETCURSEL, z, 0);
				break;
			}
		}
	}

	temp = JOIN_IN_PROGRESS_PARAMETER;
	int join = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	SendMessage(GetDlgItem(hWnd, IDC_CHECK_JOINPROGRESS), BM_SETCHECK, join?BST_CHECKED:BST_UNCHECKED, 0);
	EnableWindow(GetDlgItem(hWnd, IDC_COMBO_TIMELIMIT), join?TRUE:FALSE);

	// Always check dedicated
	SendMessage(GetDlgItem(hWnd, IDC_CHECK_DEDICATED), BM_SETCHECK, BST_CHECKED, 0);


	SendMessage(GetDlgItem(hWnd, IDC_COMBO_MAPDELAY), CB_RESETCONTENT, 0 ,0);
	MString str = MString("30 ")+gos_GetResourceString(g_langResources,IDS_MP_HG_SECONDS);
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_MAPDELAY), CB_INSERTSTRING, 0, (LPARAM)(char*)str);
	str = MString("1 ")+gos_GetResourceString(g_langResources,IDS_MP_HG_MIN);
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_MAPDELAY), CB_INSERTSTRING, 1, (LPARAM)(char*)str);
	str = MString("2 ")+gos_GetResourceString(g_langResources,IDS_MP_HG_MIN);
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_MAPDELAY), CB_INSERTSTRING, 2, (LPARAM)(char*)str);
	str = MString("5 ")+gos_GetResourceString(g_langResources,IDS_MP_HG_MIN);
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_MAPDELAY), CB_INSERTSTRING, 3, (LPARAM)(char*)str);
	str = MString("10 ")+gos_GetResourceString(g_langResources,IDS_MP_HG_MIN);
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_MAPDELAY), CB_INSERTSTRING, 4, (LPARAM)(char*)str);

	SendMessage(GetDlgItem(hWnd, IDC_COMBO_MAPDELAY), CB_SETCURSEL, 2, 0);

	temp = RECYCLE_DELAY_PARAMETER;
	int delay = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	for (int z = 0; z< 5; z++)
	{
		if (recycle_times[z] >= delay)
		{
			SendMessage(GetDlgItem(hWnd, IDC_COMBO_MAPDELAY), CB_SETCURSEL, z, 0);
			break;
		}
	}
	
	//temp = SERVER_RECYCLE_PARAMETER;
	//int recycle = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	int recycle = 1;
	SendMessage(GetDlgItem(hWnd, IDC_CHECK_SERVERRECYCLE), BM_SETCHECK, recycle?BST_CHECKED:BST_UNCHECKED, 0);
	EnableWindow(GetDlgItem(hWnd, IDC_COMBO_MAPDELAY), recycle?TRUE:FALSE);

	temp = USE_MAP_CYCLE_PARAMETER;
	int mayCycle = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	SendMessage(GetDlgItem(hWnd, IDC_CHECK_MAPCYCLELIST), BM_SETCHECK, mayCycle?BST_CHECKED:BST_UNCHECKED, 0);

	temp = ALLOW_DECAL_TRANSFER_PARAMETER;
	int decal = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	SendMessage(GetDlgItem(hWnd, IDC_CHECK_CUSTOMDECAL), BM_SETCHECK, decal?BST_CHECKED:BST_UNCHECKED, 0);

	temp = ALLOW_3RD_PERSON_PARAMETER;
	int firstPerson = !MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	SendMessage(GetDlgItem(hWnd, IDC_CHECK_FIRSTPERSON), BM_SETCHECK, firstPerson?BST_CHECKED:BST_UNCHECKED, 0);

	temp = UNLIMITED_AMMO_PARAMETER;
	int limitedAmmo = !MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	SendMessage(GetDlgItem(hWnd, IDC_CHECK_LIMITEDAMMO), BM_SETCHECK, limitedAmmo?BST_CHECKED:BST_UNCHECKED, 0);

	temp = HEAT_PARAMETER;
	int heat = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	SendMessage(GetDlgItem(hWnd, IDC_CHECK_HEATMANAGE), BM_SETCHECK, heat?BST_CHECKED:BST_UNCHECKED, 0);

	temp = FRIENDLY_FIRE_PERCENTGE_PARAMETER;
	int friendly = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	SendMessage(GetDlgItem(hWnd, IDC_CHECK_FRIENDLYFIRE), BM_SETCHECK, friendly?BST_CHECKED:BST_UNCHECKED, 0);

	temp = SPLASH_ON_PARAMETER;
	int splash = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	SendMessage(GetDlgItem(hWnd, IDC_CHECK_SPLASHDAMAGE), BM_SETCHECK, splash?BST_CHECKED:BST_UNCHECKED, 0);

	temp = WEAPON_JAM_PARAMETER;
	int weapon_jam = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	SendMessage(GetDlgItem(hWnd, IDC_CHECK_WEAPONJAM), BM_SETCHECK, weapon_jam?BST_CHECKED:BST_UNCHECKED, 0);

	temp = AMMO_BAY_FIRE_PARAMETER;
	int ammo_bay_fire = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	SendMessage(GetDlgItem(hWnd, IDC_CHECK_AMMOBAYFIRE), BM_SETCHECK, ammo_bay_fire?BST_CHECKED:BST_UNCHECKED, 0);

	temp = ADVANCE_MODE_PARAMETER;
	int advance_mode = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	SendMessage(GetDlgItem(hWnd, IDC_CHECK_ADVANCEMODE), BM_SETCHECK, advance_mode?BST_CHECKED:BST_UNCHECKED, 0);

	temp = ARMOR_MODE_PARAMETER;
	int armor_mode = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	SendMessage(GetDlgItem(hWnd, IDC_CHECK_ARMORMODE), BM_SETCHECK, armor_mode?BST_CHECKED:BST_UNCHECKED, 0);

	temp = FORCE_RESPAWN_PARAMETER;
	int respawn = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	SendMessage(GetDlgItem(hWnd, IDC_CHECK_FORCERESPAWN), BM_SETCHECK, respawn?BST_CHECKED:BST_UNCHECKED, 0);

	temp = ALLOW_DEAD_CHAT_PARAMETER;
	int noTalk = !MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	SendMessage(GetDlgItem(hWnd, IDC_CHECK_DEADMECHNOTALK), BM_SETCHECK, noTalk?BST_CHECKED:BST_UNCHECKED, 0);
	
	temp = DEAD_MECH_CANT_SEE_PARAMETER;
	int noSee = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	SendMessage(GetDlgItem(hWnd, IDC_CHECK_DEADMECHNOSEE), BM_SETCHECK, noSee?BST_CHECKED:BST_UNCHECKED, 0);

	int nRestrict = MW4Shell::CheckForLocalRestrictions(NULL, 0, NULL);
	SendMessage(GetDlgItem(hWnd, IDC_CHECK_RESTRICT), BM_SETCHECK, nRestrict?BST_CHECKED:BST_UNCHECKED, 0);
	EnableWindow(GetDlgItem(hWnd, IDC_BUTTON_RESTRICT), nRestrict?TRUE:FALSE);

	SendMessage(GetDlgItem(hWnd, IDC_CHECK_ADVERTISE), BM_SETCHECK, AdvertiseThisGame?BST_CHECKED:BST_UNCHECKED, 0);

	SendMessage(GetDlgItem(hWnd, IDC_COMBO_MAXPLAYERS), CB_RESETCONTENT, 0 ,0);
	for (i=2; i<=16; i++) {
		char temp[4];
		itoa(i, temp, 10);
		SendMessage(GetDlgItem(hWnd, IDC_COMBO_MAXPLAYERS), CB_INSERTSTRING, i-2, (LPARAM)temp);
	}

	temp = MAX_PLAYERS_PARAMETER;
	int maxPlayers = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);

	// Server Bandwidth restriction suggestion
	// Server Bandwidth (upload minimum)	# of Players
	// Modem					6
	// 128Kb 					12
	// 256Kb or greater			16
	if (MWApplication::MyConnectionSpeed < 4 && maxPlayers > 6)
		maxPlayers = 6;
	if (MWApplication::MyConnectionSpeed == 4 && maxPlayers > 12)
		maxPlayers = 12;

	if (maxPlayers>=2)
		SendMessage(GetDlgItem(hWnd, IDC_COMBO_MAXPLAYERS), CB_SETCURSEL, maxPlayers-2, 0);
	else
		SendMessage(GetDlgItem(hWnd, IDC_COMBO_MAXPLAYERS), CB_SETCURSEL, 14, 0);
	Environment.NetworkMaxPlayers = maxPlayers;
	value = maxPlayers;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);

	temp = MAX_BOTS_PARAMETER;
	int maxBots = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	SendMessage(GetDlgItem(hWnd, IDC_CHECK_KICKBOT), BM_SETCHECK, maxBots<maxPlayers, 0);
}

void CDedicatedServerUI::OnMaxPlayerChange(HWND hWnd)
{
	int maxPlayers = SendMessage(GetDlgItem(hWnd, IDC_COMBO_MAXPLAYERS), CB_GETCURSEL, 0, 0) + 2;
	int* intParam[2];
	int parameter = MAX_PLAYERS_PARAMETER;
	intParam[0] = &parameter;
	intParam[1] = &maxPlayers;
	
	// Server Bandwidth restriction suggestion
	// Server Bandwidth (upload minimum)	# of Players
	// Modem					6
	// 128Kb 					12
	// 256Kb or greater			16
	if ((MWApplication::MyConnectionSpeed < 4 && maxPlayers > 6) || 
		(MWApplication::MyConnectionSpeed == 4 && maxPlayers > 12))
	{
		MString errmsg = MString(gos_GetResourceString(g_langResources, IDS_MP_HOST_SETUP_MAX_PLAYER));
		if (IDNO==MechMessageBoxWin32(gos_GetResourceString(g_langResources, IDS_GN_ERROR), errmsg, MB_YESNO)) 
		{
			int maxPlayers = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
			SendMessage(GetDlgItem(hWnd, IDC_COMBO_MAXPLAYERS), CB_SETCURSEL, maxPlayers-2, 0);
			return;
		}		
	}
	
	Environment.NetworkMaxPlayers = maxPlayers;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);
}

void CDedicatedServerUI::OnCheckServerRecycle(HWND hWnd)
{
	int value = (SendMessage(GetDlgItem(hWnd, IDC_CHECK_SERVERRECYCLE), BM_GETCHECK, 0, 0)==BST_CHECKED)?1:0;
	EnableWindow(GetDlgItem(hWnd, IDC_COMBO_MAPDELAY), value);
}

void CDedicatedServerUI::OnCheckJoinInProgress(HWND hWnd)
{
	int value = (SendMessage(GetDlgItem(hWnd, IDC_CHECK_JOINPROGRESS), BM_GETCHECK, 0, 0)==BST_CHECKED)?1:0;
	EnableWindow(GetDlgItem(hWnd, IDC_COMBO_TIMELIMIT), value);
}

void CDedicatedServerUI::OnCheckPassword(HWND hWnd)
{
	int value = (SendMessage(GetDlgItem(hWnd, IDC_CHECK_PASSWORD), BM_GETCHECK, 0, 0)==BST_CHECKED)?1:0;
	EnableWindow(GetDlgItem(hWnd, IDC_EDIT_PASSWORD), value);
	char temp[STRING_SIZE];
	GetDlgItemText(hWnd, IDC_EDIT_PASSWORD, temp, STRING_SIZE);

	char gameName[STRING_SIZE];
	GetDlgItemText(hWnd, IDC_SERVER_NAME, gameName, STRING_SIZE);

	EnableWindow(GetDlgItem(hWnd, IDSTART), strlen(gameName)>0 && (strlen(temp)>0||(!value)));
}

bool CDedicatedServerUI::HostSetupOnStart(HWND hWnd)
{
	char temp[STRING_SIZE];
	GetDlgItemText(hWnd, IDC_SERVER_NAME, temp, STRING_SIZE);
	Str_Copy(GameName, temp, STRING_SIZE);
				
	if (SendMessage(GetDlgItem(hWnd, IDC_CHECK_PASSWORD), BM_GETCHECK, 0, 0)==BST_CHECKED) {
		GetDlgItemText(hWnd, IDC_EDIT_PASSWORD, temp, STRING_SIZE);
		Str_Copy(GamePassword, temp, STRING_SIZE);
	}
	else {
		Str_Copy(GamePassword, "", STRING_SIZE);
	}

	int* intParam[2];
	int parameter;
	int	value;
	intParam[0] = &parameter;
	intParam[1] = &value;

	parameter = REPORTSTATS_PARAMETER;
	value = (SendMessage(GetDlgItem(hWnd, IDC_CHECK_STATSTOZONE), BM_GETCHECK, 0, 0)==BST_CHECKED)?1:0;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);

	AdvertiseThisGame = (SendMessage(GetDlgItem(hWnd, IDC_CHECK_ADVERTISE), BM_GETCHECK, 0, 0)==BST_CHECKED)?1:0;

	int maxPlayers = GetDlgItemInt(hWnd, IDC_COMBO_MAXPLAYERS, NULL, FALSE);
	Environment.NetworkMaxPlayers = maxPlayers;
	parameter = MAX_PLAYERS_PARAMETER;
	value = maxPlayers;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);

	int maxBots = maxPlayers;
	if (SendMessage(GetDlgItem(hWnd, IDC_CHECK_KICKBOT), BM_GETCHECK, 0, 0)==BST_CHECKED)
		maxBots--;
	parameter = MAX_BOTS_PARAMETER;
	value = maxBots;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);

	parameter = JOIN_IN_PROGRESS_PARAMETER;
	value = (SendMessage(GetDlgItem(hWnd, IDC_CHECK_JOINPROGRESS), BM_GETCHECK, 0, 0)==BST_CHECKED)?1:0;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);
	int sel = SendMessage(GetDlgItem(hWnd, IDC_COMBO_TIMELIMIT), CB_GETCURSEL, 0, 0);
	if (sel == 7)
	{
		parameter = JOIN_IN_PROGRESS_CUTOFF_PARAMETER;
		value = FALSE;
		MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);
	}
	else
	{
		parameter = JOIN_IN_PROGRESS_CUTOFF_PARAMETER;
		value = TRUE;
		MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);

		parameter = JOIN_IN_PROGRESS_CUTOFF_TIME_PARAMETER;
		value = join_times[sel];
		MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);
	}

	parameter = ALLOW_DECAL_TRANSFER_PARAMETER;
	value = (SendMessage(GetDlgItem(hWnd, IDC_CHECK_CUSTOMDECAL), BM_GETCHECK, 0, 0)==BST_CHECKED)?1:0;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);

	parameter = DEDICATED_SERVER_PARAMETER;
	value = 1;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);

	parameter = USE_MAP_CYCLE_PARAMETER;
	value = (SendMessage(GetDlgItem(hWnd, IDC_CHECK_MAPCYCLELIST), BM_GETCHECK, 0, 0)==BST_CHECKED)?1:0;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);

	parameter = SERVER_RECYCLE_PARAMETER;
	value = (SendMessage(GetDlgItem(hWnd, IDC_CHECK_SERVERRECYCLE), BM_GETCHECK, 0, 0)==BST_CHECKED)?1:0;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);

	// Server to delay launch
	parameter = RECYCLE_DELAY_PARAMETER;
	sel = SendMessage(GetDlgItem(hWnd, IDC_COMBO_MAPDELAY), CB_GETCURSEL, 0, 0);
	switch(sel) {
	case 0:
		value = 30;
		break;
	case 1:
		value = 60;
		break;
	case 2:
		value = 120;
		break;
	case 3:
		value = (5*60);
		break;
	case 4:
		value = (10*60);
		break;
	}	
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);

	parameter = FORCE_RESPAWN_PARAMETER;
	value = (SendMessage(GetDlgItem(hWnd, IDC_CHECK_FORCERESPAWN), BM_GETCHECK, 0, 0)==BST_CHECKED)?1:0;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);

	parameter = UNLIMITED_AMMO_PARAMETER;
	value = (SendMessage(GetDlgItem(hWnd, IDC_CHECK_LIMITEDAMMO), BM_GETCHECK, 0, 0)==BST_CHECKED)?0:1;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);

	parameter = HEAT_PARAMETER;
	value = (SendMessage(GetDlgItem(hWnd, IDC_CHECK_HEATMANAGE), BM_GETCHECK, 0, 0)==BST_CHECKED)?1:0;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);

	parameter = FRIENDLY_FIRE_PERCENTGE_PARAMETER;
	value = (SendMessage(GetDlgItem(hWnd, IDC_CHECK_FRIENDLYFIRE), BM_GETCHECK, 0, 0)==BST_CHECKED)?100:0;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);

	parameter =	SPLASH_ON_PARAMETER;
	value = (SendMessage(GetDlgItem(hWnd, IDC_CHECK_SPLASHDAMAGE), BM_GETCHECK, 0, 0)==BST_CHECKED)?1:0;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);

	parameter =	WEAPON_JAM_PARAMETER;
	value = (SendMessage(GetDlgItem(hWnd, IDC_CHECK_WEAPONJAM), BM_GETCHECK, 0, 0)==BST_CHECKED)?1:0;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);

	parameter =	AMMO_BAY_FIRE_PARAMETER;
	value = (SendMessage(GetDlgItem(hWnd, IDC_CHECK_AMMOBAYFIRE), BM_GETCHECK, 0, 0)==BST_CHECKED)?1:0;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);

	parameter =	ADVANCE_MODE_PARAMETER;
	value = (SendMessage(GetDlgItem(hWnd, IDC_CHECK_ADVANCEMODE), BM_GETCHECK, 0, 0)==BST_CHECKED)?1:0;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);

	parameter =	ARMOR_MODE_PARAMETER;
	value = (SendMessage(GetDlgItem(hWnd, IDC_CHECK_ARMORMODE), BM_GETCHECK, 0, 0)==BST_CHECKED)?1:0;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);

	parameter =	ALLOW_DEAD_CHAT_PARAMETER;
	value = (SendMessage(GetDlgItem(hWnd, IDC_CHECK_DEADMECHNOTALK), BM_GETCHECK, 0, 0)==BST_CHECKED)?0:1;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);

	parameter =	DEAD_MECH_CANT_SEE_PARAMETER;
	value = (SendMessage(GetDlgItem(hWnd, IDC_CHECK_DEADMECHNOSEE), BM_GETCHECK, 0, 0)==BST_CHECKED)?1:0;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);

	parameter =	ALLOW_3RD_PERSON_PARAMETER;
	value = (SendMessage(GetDlgItem(hWnd, IDC_CHECK_FIRSTPERSON), BM_GETCHECK, 0, 0)==BST_CHECKED)?0:1;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);

	if (SendMessage(GetDlgItem(hWnd, IDC_CHECK_RESTRICT), BM_GETCHECK, 0, 0)!=BST_CHECKED)
		MW4Shell::ClearLocalRestrictions(NULL, 0, NULL);

	int error =  CreateSession(NULL, 0, NULL);
	if (error==0)	{
		// mdm - ping server management
		ShutdownPing(NULL, 0, NULL);
		StartPingServer(NULL, 0, NULL);
		
		MWApplication *app = Cast_Object(MWApplication *, Application::GetInstance());
		NetMissionParameters::MWNetMissionParameters *params = app->GetLocalNetParams();
		Str_Copy(params->m_serverName, GameName, STRING_SIZE);
		Str_Copy(params->m_serverIP, LocalIPAddress, STRING_SIZE);
		
		gos_NetDoneStartGame();
		// end the networking stuff
		DWORD networking;
		
		networking = reinterpret_cast<DWORD>(
			gos_NetInformation(gos_Networking, 0)
			);
		
		app->networkingFlag = (networking)?true:false;
		
		if (app->networkingFlag)
		{
			
			DWORD server =
				reinterpret_cast<DWORD>(
				gos_NetInformation(gos_AmITheServer, 0)
				);
			app->serverFlag = (server)?true:false;
			
			if (Network::GetInstance() == NULL)
			{
				gos_PushCurrentHeap(MechWarrior4::Heap);
				Verify(!Network::GetInstance());
				GlobalPointers::AddGlobalPointer(new Network, NetworkGlobalPointerIndex);
				Register_Object(Network::GetInstance());
				gos_PopCurrentHeap();
			}
			
			if (app->serverFlag)
			{
				// save the options that were set
				app->SaveServerOptions();
				
#if defined(LAB_ONLY)
				MWGameInfo::g_lastGameType = MWGameInfo::g_currentGameType;
				MWGameInfo::g_currentGameType = MWGameInfo::MultiServer;
#endif
				
				//app->LoadServerOptions();
				app->SendRequestConnectionMessage();
				for (int i = 0; i < Maximum_Lancemates; ++i)
				{
					app->lancemateConnectionData[i].Disconnect();
				}
				
				app->memoryDiffKiller.Init();
			}
			else
			{
				STOP(("Shouldn't be here!"));
			}
		}
		else
		{
			gos_NetEndGame();
			app->GetLocalNetParams()->ResetParameters();
			app->GetServerNetParams()->ResetParameters();
			STOP(("Shouldn't be here!"));
		}
		
		while (! MW4Shell::NetConnectedToServer(NULL, 0, NULL))
			Sleep(1000);

		return true;		
	}
	else
	{
		// 1=Bad game name
		// 2=Bad player name
		// 3=Failed
		// 4=Waiting for answer
		MString errmsg = "";
		switch ( error )
		{
		case 1:
		    errmsg = gos_GetResourceString(g_langResources, IDS_MP_HOST_SETUP_ERROR_BAD_GAME_NAME);
			break;
		case 2:
			errmsg = gos_GetResourceString(g_langResources, IDS_MP_HOST_SETUP_ERROR_BAD_PLAYER_NAME);
			break;
		case 3:
			errmsg = gos_GetResourceString(g_langResources, IDS_MP_HOST_SETUP_ERROR_UNKNOWN_FAILURE);
			break;
		case 4:
			errmsg = gos_GetResourceString(g_langResources, IDS_MP_HOST_SETUP_ERROR_WAITING_FOR_ANSWER);
			break;
		}
		errmsg = MString(gos_GetResourceString(g_langResources, IDS_MP_HOST_SETUP_ERROR_CREATING_GAME)) +
			":\n\n" + errmsg;
		MechMessageBoxWin32(gos_GetResourceString(g_langResources, IDS_GN_ERROR), errmsg);
		return false;
	}
}
