#include "MW4DUIHeaders.hpp"
#include <MW4\MWTable.hpp>
#include <MW4\MWApplication.hpp>
#include <MW4\MWMission.hpp>
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <time.h>
#include "DedicatedUI.hpp"
#include "resource.h"
#include <buildnum\buildnum.h>
#include "..\ScriptStrings\resource.h" // scriptstrings.h - jcem

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

int CDedicatedServerUI::valid_count;
char CDedicatedServerUI::teamstring[9][8];
// MSL ADD MECH
char CDedicatedServerUI::mechlist[64][256];
char CDedicatedServerUI::denied_error[13][256];
int CDedicatedServerUI::mech_ids[];
int CDedicatedServerUI::skin_ids[];
int CDedicatedServerUI::team_ids[];
int CDedicatedServerUI::status[];
int CDedicatedServerUI::accepted[];
int CDedicatedServerUI::i_am_a_bot[];
int CDedicatedServerUI::player_number[];
int CDedicatedServerUI::bot_number[];
char* CDedicatedServerUI::print_names[];
char* CDedicatedServerUI::faction_name[];
Stuff::Scalar CDedicatedServerUI::tonnages[];
char CDedicatedServerUI::base_names[17][256];
int CDedicatedServerUI::player_ping[17];
int CDedicatedServerUI::host_last_num = -1;
int CDedicatedServerUI::host_last_val = -1;
int CDedicatedServerUI::host_last_status = 0;
int CDedicatedServerUI::host_last_ready = 0;
int CDedicatedServerUI::host_last_team = -1;
int CDedicatedServerUI::host_last_skin = -1;
int CDedicatedServerUI::host_last_teams_num = 0;
int CDedicatedServerUI::host_bot_last_val[256];
int CDedicatedServerUI::host_bot_last_skin[256];
int CDedicatedServerUI::host_bot_last_player_num[256];
char ** CDedicatedServerUI::mechs = NULL;
int *CDedicatedServerUI::bStock = NULL;
int *CDedicatedServerUI::stock_array = NULL;
int *CDedicatedServerUI::variant_array = NULL;
int CDedicatedServerUI::current_mech = 0;
int CDedicatedServerUI::min_ton_allteams = 0;
int CDedicatedServerUI::max_ton_allteams = 15;
int CDedicatedServerUI::total_max_ton_allteams = 1600;
int CDedicatedServerUI::team_roster_count[];
int CDedicatedServerUI::team_legal_to_join[];
int CDedicatedServerUI::max_players = 0;
int CDedicatedServerUI::max_bots = 0;
int CDedicatedServerUI::num_of_players = 0;
int CDedicatedServerUI::game_state = 0;
int CDedicatedServerUI::host_bot_from_file[];
int CDedicatedServerUI::bots_exist = 0;
int CDedicatedServerUI::fill_game = 0;

#define NUM_CALLSIGNS 64
extern char* BotNames[];
extern int BotLevels[];

int custom_to_standard[30] = {2,3,0,1,4,5,6,8,11,13,14,15,16,17,18,19,21,24,26};
int standard_to_custom[30];
int selection_to_standard[30];
int standard_to_selection[30];
int last_selected_game = -1;
int last_selected_map = -1;
bool kick_player = false;

HMENU g_hMenuMechs = NULL;
HMENU g_hMenuVariants[30] = {NULL};
HMENU g_hMenuTeams = NULL;
HMENU g_hKickMechs = NULL;
HANDLE g_hThreadEditor = NULL;

#define ID_MECH_START 40000
#define ID_KICKMECH_START 50000

int CALLBACK MyEditWordBreakProc(LPTSTR test, int curPos, int len, int action)
{
	switch (action)
	{
	case WB_ISDELIMITER:
		return 0;
	case WB_LEFT:
		return curPos-1;
	case WB_RIGHT:
		return curPos+1;
	}
	return curPos;
}

INT CALLBACK CDedicatedServerUI::GameLobbyProc(
  HWND hwndDlg,  // handle to dialog box
  UINT uMsg,     // message
  WPARAM wParam, // first message parameter
  LPARAM lParam  // second message parameter
)
{
    switch( uMsg )
    {
	case WM_INITDIALOG: 
		InitGameLobby(hwndDlg);
		g_hWndLobby = hwndDlg;
		EnableMissionEdit(g_hThreadEditor==NULL);
		return TRUE;

	case WM_TIMER:
		if (wParam == g_chatTimer) {
			// If we are running
			if (game_state == 2)
				MW4Shell::CheckGUNAdvertise(NULL, 0, NULL);
			GameLobbyUpdateChat(hwndDlg);
			GameLobbyUpdatePlayers(hwndDlg);

			if (g_hThreadEditor && WAIT_TIMEOUT != ::WaitForSingleObject(g_hThreadEditor, 0)) {
				g_hThreadEditor = NULL;

				if (!game_state) {
					EnableMissionEdit(TRUE);
					last_selected_map = -1;
					OnMapChange(hwndDlg);
				}
			}
			
			MWApplication *app = MWApplication::GetInstance();
			NetMissionParameters::MWNetMissionParameters *params = app->GetLocalNetParams();
			Stuff::Scalar time = 0.0f;
			if (game_state == 2) {
				MWMission *mission = Cast_Object(MWMission*, MWMission::GetInstance());
				time = mission->GetEndMissionTime();
				if ((int)(time) <= 0) {
					game_state = 0;
					MWApplication::GetInstance()->recycleTimerOn = false;
				}
				ShowWindow(GetDlgItem(hwndDlg, IDC_STATIC_TIMEREMAIN), SW_SHOW);
				ShowWindow(GetDlgItem(hwndDlg, IDC_STATIC_TIMETOLAUNCH), SW_HIDE);
				ShowWindow(GetDlgItem(hwndDlg, IDC_STATIC_GAMETIME), SW_SHOW);
			}
			else if (game_state == 0){
				if (app->recycleTimerOn) {
					time = (Stuff::Scalar)(params->m_recycleDelay - (gos_GetElapsedTime() - app->recycleStartTime));
					ShowWindow(GetDlgItem(hwndDlg, IDC_STATIC_TIMEREMAIN), SW_HIDE);
					ShowWindow(GetDlgItem(hwndDlg, IDC_STATIC_TIMETOLAUNCH), SW_SHOW);
					ShowWindow(GetDlgItem(hwndDlg, IDC_STATIC_GAMETIME), SW_SHOW);
				}
				else {
					ShowWindow(GetDlgItem(hwndDlg, IDC_STATIC_TIMEREMAIN), SW_HIDE);
					ShowWindow(GetDlgItem(hwndDlg, IDC_STATIC_TIMETOLAUNCH), SW_HIDE);
					ShowWindow(GetDlgItem(hwndDlg, IDC_STATIC_GAMETIME), SW_HIDE);
				}
			}
			int hours = (int)(time / 3600);
			time -= (hours * 3600);
			
			int minutes = (int)(time / 60);
			time -= (minutes * 60);
			
			int seconds = (int)(time);											
			
			char buffer[128];
			sprintf(buffer,"%02d:%02d:%02d", hours, minutes, seconds);
			SetWindowText(GetDlgItem(hwndDlg, IDC_STATIC_GAMETIME), buffer);

			// Check recycle timer for auto start
			if (game_state == 0){
				if (!app->recycleTimerOn && params->m_serverRecycle)
				{
					app->recycleTimerOn = true;
					app->recycleStartTime = gos_GetElapsedTime();
				}
				else if (app->recycleTimerOn && !params->m_serverRecycle)
				{
					app->recycleTimerOn = false;
				}
				
				if (app->recycleTimerOn)
				{
					if ((gos_GetElapsedTime() - app->recycleStartTime) > params->m_recycleDelay)
					{
						GameLobbyOnLaunch(hwndDlg);
						app->recycleTimerOn = false;
					}
				}
			}
		}
		return 0;

	case WM_CLOSE:
	case WM_DESTROY:
		KillTimer(hwndDlg, g_chatTimer);
		{
		for (int i=0; i<MW4Shell::Instance->m_mechCount; i++) {
			FREE_PTR(mechs[i]);
		}
		FREE_PTR(mechs);
		FREE_PTR(bStock);
		FREE_PTR(stock_array);
		FREE_PTR(variant_array);

		for (i=0; i<17; i++) {
			FREE_PTR(print_names[i]);
			FREE_PTR(faction_name[i]);
		}

		if (g_hMenuMechs) {
			DestroyMenu(g_hKickMechs);
			g_hKickMechs = NULL;
		}
		if (g_hKickMechs) {
			DestroyMenu(g_hKickMechs);
			g_hKickMechs = NULL;
		}

		Quit();
		EndDialog(hwndDlg, TRUE);
		return 0;
		}

	case WM_COMMAND:		

		if (LOWORD(wParam)>=IDC_BUTTON_CHANGETEAM0 && LOWORD(wParam)<IDC_BUTTON_CHANGETEAM0+16)
		{
			if (!::IsWindowVisible(GetDlgItem(hwndDlg,LOWORD(wParam))))
				return 0;
			int sel = LOWORD(wParam)-IDC_BUTTON_CHANGETEAM0;
			current_mech = sel;
			DWORD pos = ::GetMessagePos();
			int xPos = GET_X_LPARAM(pos);
			int yPos = GET_Y_LPARAM(pos);
			::TrackPopupMenu(g_hMenuTeams, 0, xPos, yPos, 0, hwndDlg, NULL);
			
			return 0;
		}

		if (LOWORD(wParam)>=IDC_BUTTON_CHANGE0 && LOWORD(wParam)<IDC_BUTTON_CHANGE0+16)
		{
			if (!::IsWindowVisible(GetDlgItem(hwndDlg,LOWORD(wParam))))
				return 0;
			int sel = LOWORD(wParam)-IDC_BUTTON_CHANGE0;
			current_mech = sel;
			DWORD pos = ::GetMessagePos();
			int xPos = GET_X_LPARAM(pos);
			int yPos = GET_Y_LPARAM(pos);
			::TrackPopupMenu(g_hMenuMechs, 0, xPos, yPos, 0, hwndDlg, NULL);
			
			return 0;
		}

		if (LOWORD(wParam)>=ID_KICKMECH_START && LOWORD(wParam)<ID_KICKMECH_START+valid_count)
		{
			int sel = LOWORD(wParam)-ID_KICKMECH_START;
			int* intParam[1];
			int param;
			intParam[0] = &param;
			if (i_am_a_bot[sel]) {
				param = bot_number[sel];
				MW4Shell::RemoveBot(NULL, 1, (void**)intParam);
				host_bot_last_val[bot_number[sel]] = -1;
				host_bot_last_player_num[bot_number[sel]] = -1;
				host_bot_last_skin[bot_number[sel]] = 0;
				host_bot_from_file[bot_number[sel]] = 0;
			}
			else {
				param = player_number[sel];
				if (kick_player)
					MW4Shell::KickPlayer(NULL, 1, (void**)intParam);
				else
					MW4Shell::BanPlayer(NULL, 1, (void**)intParam);
			}
			return 0;
		}

		if (LOWORD(wParam)>=ID_MECH_START && LOWORD(wParam) < ID_MECH_START+MW4Shell::Instance->m_mechCount)
		{
			int sel = LOWORD(wParam)-ID_MECH_START;
			mech_ids[current_mech] = sel;
			return 0;
		}

		if (LOWORD(wParam)>=ID_TEAMS_1 && LOWORD(wParam) < ID_TEAMS)
		{
			int sel = LOWORD(wParam)-ID_TEAMS_1;
			if (team_legal_to_join[sel]) {
	
				int* intParam[2];
				int temp = bot_number[current_mech];;
				int value = sel;
				intParam[0] = &temp;
				intParam[1] = &value;
				MW4Shell::SetTeamBot(NULL, 2, (void**)intParam);
				int accepted = MW4Shell::GetBotVehicleAccepted(NULL, 1, (void**)intParam);
				if (!accepted)
				{
					OnBotError();
				}
			}
			else {
				char temp[STRING_SIZE];
				LoadString(g_hInst, IDS_STRING_TEAMFULL, temp, STRING_SIZE);
				MechMessageBoxWin32(gos_GetResourceString(g_langResources, IDS_GN_ERROR), temp);
			}

			return 0;
		}

		switch( LOWORD(wParam) )
		{
		case IDLAUNCH:
			GameLobbyOnLaunch(hwndDlg);
			return 0;

		case IDDISCONNECT:
			KillTimer(hwndDlg, g_chatTimer);
			EndDialog(hwndDlg, TRUE);
			MW4Shell::Instance->currentShellRunning = MW4Shell::NoShellRunning;
			Application::GetInstance()->QueStopGame();
			Application::GetInstance()->StopNetworking();
			MWApplication::GetInstance()->GetLocalNetParams()->ResetParameters();
			MWApplication::GetInstance()->GetServerNetParams()->ResetParameters();
			last_selected_game = -1;
			last_selected_map = -1;
			if (g_hWndServer) {
				SendMessage(g_hWndServer, WM_INITDIALOG, 0, 0);
			}
			else {
				g_hWndServer = ::CreateDialog(g_hInst, 
					MAKEINTRESOURCE(IDD_DEDICATEDSERVER),
					g_hWndMain,
					DedicatedServerProc);
			}
			ShowWindow(g_hWndServer, SW_SHOW);
			return 0;
		
		case IDEXIT:
			game_state = 0;
			MWApplication::GetInstance()->recycleTimerOn = false;
			EnableWindow(GetDlgItem(g_hWndLobby, IDDISCONNECT), FALSE);
			EnableWindow(GetDlgItem(g_hWndLobby, IDEXIT), FALSE);
			EnableWindow(GetDlgItem(g_hWndLobby, IDC_BUTTON_KICK), FALSE);
			EnableWindow(GetDlgItem(g_hWndLobby, IDC_BUTTON_BAN), FALSE);

			Verify(Application::GetInstance()->GetApplicationState() == ApplicationStateEngine::RunningGameState);
			Application::GetInstance()->QueStopGame();
			return 0;

		case IDEDITMISSION:
			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)) {
					EnableMissionEdit(FALSE);
					g_hThreadEditor = pInfo.hThread;
				}
			}
			return 0;

		case IDC_EDIT_CHATENTRY: 
			{
			char temp[STRING_SIZE];
			GetDlgItemText(hwndDlg, IDC_EDIT_CHATENTRY, temp, STRING_SIZE);
			if (strlen(temp)>100 && temp[strlen(temp)-1] != '\n') {
				temp[strlen(temp)-1] = '\0';
				SetDlgItemText(hwndDlg, IDC_EDIT_CHATENTRY, temp);
				SendMessage(GetDlgItem(hwndDlg, IDC_EDIT_CHATENTRY), EM_SETSEL, strlen(temp), strlen(temp));
			}

			if (temp[strlen(temp)-1] == '\n') {				
				GameLobbyOnChat(hwndDlg);
			}
			return 0;
			}
		case IDC_EDIT_FRAGLIMIT: 
			OnFragLimitChange(hwndDlg);
			return 0;
		case IDC_EDIT_LIFELIMIT:
			OnLifeLimitChange(hwndDlg);
			return 0;
		case IDC_BUTTON_ADDBOT:
			{
				EnableWindow(GetDlgItem(hwndDlg, IDC_BUTTON_ADDBOT), FALSE);
				char* name = (char*)gos_Malloc(STRING_SIZE);
				GetDlgItemText(hwndDlg, IDC_EDIT_BOTNAME, name, STRING_SIZE);
				char* realName = name;
				while (*realName == ' ')
					realName ++;
				if (strlen(realName) == 0) {
					char temp[STRING_SIZE];
					LoadString(g_hInst, IDS_STRING_NOBOTNAME, temp, STRING_SIZE);
					MechMessageBoxWin32(Application::GetInstance()->GetLocString(IDS_NETUI_ERROR), temp);
					return 0;
				}				
				int level = GetDlgItemInt(hwndDlg, IDC_EDIT_BOTSKILL, NULL, FALSE);
				SetDlgItemInt(hwndDlg, IDC_EDIT_BOTSKILL, level, FALSE);

				char* clan = (char*)gos_Malloc(STRING_SIZE);
				sprintf(clan, "%s%d", gos_GetResourceString(g_langResources, IDS_MP_LOBBY_BOT_LEVEL), level);
				
				int stockSel = SendMessage(GetDlgItem(hwndDlg, IDC_COMBO_MECHS), CB_GETCURSEL, 0,0);
				int variantSel = SendMessage(GetDlgItem(hwndDlg, IDC_COMBO_MECHS2), CB_GETCURSEL, 0,0);
				int mechId = stock_array[stockSel] + variant_array[variantSel];

				int teamId = SendMessage(GetDlgItem(hwndDlg, IDC_COMBO_TEAM), CB_GETCURSEL, 0,0);
				if (!IsWindowVisible(GetDlgItem(hwndDlg, IDC_COMBO_TEAM)))
					teamId = -1;

				int botId = AddBot(realName, level, clan, mechId, teamId);
				host_bot_from_file[botId] = 0;

				FREE_PTR(name);
				FREE_PTR(clan);
				
				int bot_number = rand()%NUM_CALLSIGNS;
				SetDlgItemText(hwndDlg, IDC_EDIT_BOTNAME, BotNames[bot_number]); 
				return 0;
			}

		case IDC_BUTTON_KICK:
			kick_player = true;
			ShowKickBanMenu(hwndDlg, kick_player);
			return 0;

		case IDC_BUTTON_BAN:
			kick_player = false;
			ShowKickBanMenu(hwndDlg, kick_player);
			return 0;
			
		case ID_PLAYERS_TEAMS:
			{
			DWORD pos = ::GetMessagePos();
			int xPos = GET_X_LPARAM(pos);
			int yPos = GET_Y_LPARAM(pos);
			::TrackPopupMenu(g_hMenuTeams, 0, xPos, yPos, 0, hwndDlg, NULL);
			return 0;
			}
		}

		switch(HIWORD(wParam))
		{
		case BN_CLICKED:
			switch (LOWORD(wParam)) {
			case IDC_CHECK_FRAGLIMIT:
				OnCheckFragLimit(hwndDlg);
				break;
			case IDC_CHECK_LIFELIMIT:
				OnCheckNumLives(hwndDlg);
				break;
			}
			return 0;

		case CBN_SELCHANGE:
			switch (LOWORD(wParam)) {
			case IDC_COMBO_MECHS:
				OnMechSelChange(hwndDlg);
				break;
			case IDC_COMBO_TEAMSEL:
				OnTeamSelChange(hwndDlg);
				break;
			case IDC_COMBO_TEAMMAXTON:
				OnTeamMaxTonChange(hwndDlg);
				break;
			case IDC_COMBO_TEAMTOTMAXTON:
				OnTeamMaxTotalTonChange(hwndDlg);
				break;
			case IDC_COMBO_TEAMMINTON:
				OnTeamMinTonChange(hwndDlg);
				break;
			case IDC_COMBO_GAMETYPE:
				OnGameTypeChange(hwndDlg);
				break;
			case IDC_COMBO_MAP:
				OnMapChange(hwndDlg);
				break;
			case IDC_COMBO_WEATHER:
				OnWeatherChange(hwndDlg);
				break;
			case IDC_COMBO_VISIBILITY:
				OnVisibilityChange(hwndDlg);
				break;
			case IDC_COMBO_TIMEOFDAY:
				OnTimeOfDayChange(hwndDlg);
				break;
			case IDC_COMBO_TIMELIMIT:
				OnTimeLimitChange(hwndDlg);
				break;
			case IDC_COMBO_REVIEW:
				OnReviewChange(hwndDlg);
				break;
			case IDC_COMBO_STOCK:
				OnStockMechChange(hwndDlg);
				break;
			case IDC_COMBO_RADAR:
				OnRadarChange(hwndDlg);
				break;
			case IDC_COMBO_MAXTON:
				OnMaxTonnageChange(hwndDlg);
				break;
			}
			return 0;
		}
		return 0;
	}

	return FALSE;
}

int game_types[] = {
	IDS_HLM_ATTRITION, 
	IDS_HLM_TATTRITION, 
	IDS_HLM_DESTRUCTION, 
	IDS_HLM_TDESTRUCTION, 
	IDS_HLM_CTF, 
	IDS_HLM_KOH, 
	IDS_HLM_TKOH, 
	IDS_HLM_STB, 
	IDS_HLM_ESCORT,
	IDS_HLM_CDESTRUCTION,
	IDS_HLM_CTDESTRUCTION,
	IDS_HLM_CATTRITION,
	IDS_HLM_CTATTRITION,
	IDS_HLM_CCTF,
	IDS_HLM_CKOH,
	IDS_HLM_CTKOH,
	IDS_HLM_CSTB,
	IDS_HLM_CESCORT,
	IDS_HLM_CUNDEFINED
};

void CDedicatedServerUI::InitGameLobby(HWND hWnd)
{
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_GAMETYPE), CB_RESETCONTENT, 0,0);

	void *params[3];
	int param0;
	int param1;
	params[0] = (void*)&param0;
	params[1] = (void*)&param1;

	int index = 0;
	for (int i=0; i<sizeof(game_types)/sizeof(int); i++) {
		param0 = custom_to_standard[i];
		if (MW4Shell::Instance->GetScenarioCount(NULL, 1, (void**)params)) {
			selection_to_standard[index] = custom_to_standard[i];
			SendMessage(GetDlgItem(hWnd, IDC_COMBO_GAMETYPE), CB_INSERTSTRING, index++,(LPARAM)gos_GetResourceString(g_langResources,game_types[i]));
		}
	}
	
	for (int fillerror = i; fillerror < 30; fillerror++)
	{
		selection_to_standard[fillerror] = 19;
		custom_to_standard[fillerror] = 19;
	}
	for (int switch_num = 0; switch_num < 30; switch_num++)
	{
		standard_to_selection[selection_to_standard[switch_num]] = switch_num;
		standard_to_custom[custom_to_standard[switch_num]] = switch_num;
	}

	for (i = 0; i < 256; i++)
	{
		host_bot_last_val[i] = -1;
		host_bot_last_player_num[i] = -1;
		host_bot_last_skin[i] = 0;
		host_bot_from_file[i] = 0;
	}

	for (i=0; i<17; i++) {
		print_names[i] = NULL;
		faction_name[i] = NULL;
		team_ids[i] = -1;
		mech_ids[i] = 0;
		skin_ids[i] = 0;
	}

	MW4Shell::Instance->InitMPScreen();
				
	param0 = MAX_PLAYERS_PARAMETER;
	max_players = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, params);

	param0 = MAX_BOTS_PARAMETER;
	max_bots = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, params);

	if (!mechs || !bStock || !stock_array || !variant_array) {
		FREE_PTR(mechs);
		FREE_PTR(bStock);
		FREE_PTR(stock_array);
		FREE_PTR(variant_array);

		bStock = (int *)gos_Malloc(MW4Shell::Instance->m_mechCount*sizeof(int));
		stock_array = (int *)gos_Malloc(MW4Shell::Instance->m_mechCount*sizeof(int));
		variant_array = (int *)gos_Malloc(MW4Shell::Instance->m_mechCount*sizeof(int));
		mechs = (char **)gos_Malloc(MW4Shell::Instance->m_mechCount*sizeof(char*));
		for (i=0; i<MW4Shell::Instance->m_mechCount; i++)
			mechs[i] = NULL;
		param0 = 0;
		params[1] = (void*)mechs;
		params[2] = (void*)bStock;
		MW4Shell::Instance->GetMechs((void**)params, 3);
	}

	if (!g_hMenuMechs) {
		g_hMenuMechs = ::CreatePopupMenu();
		
		MENUITEMINFO itemInfo;
		::memset(&itemInfo, 0, sizeof(MENUITEMINFO));
		itemInfo.cbSize = sizeof(MENUITEMINFO);
		itemInfo.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
		itemInfo.fType = MFT_STRING;
		itemInfo.fState = MFS_ENABLED;
		
		int index = 0;
		for (i=0; i<MW4Shell::Instance->m_mechCount; i++) {
			if (bStock[i]) {
				index++;
			}
			else {
				if (g_hMenuVariants[index-1] == NULL) {
					g_hMenuVariants[index-1] = ::CreatePopupMenu();
					itemInfo.dwTypeData = gos_GetResourceString(g_langResources, IDS_MP_LOBBY_STOCK);
					itemInfo.wID = ID_MECH_START+i-1;
					InsertMenuItem(g_hMenuVariants[index-1],0,TRUE,&itemInfo);
				}
				char baseName[256] = "";
				GetMechBaseName(mechs[i], baseName);
				itemInfo.dwTypeData = mechs[i] + strlen(baseName) + 1;
				itemInfo.wID = ID_MECH_START+i;
				InsertMenuItem(g_hMenuVariants[index-1],i-(index-1),TRUE,&itemInfo);
			}
		}
		
		index = 0;
		for (i=0; i<MW4Shell::Instance->m_mechCount; i++) {
			itemInfo.wID = ID_MECH_START+i;
			if (bStock[i]) {
				if (g_hMenuVariants[index]) {
					itemInfo.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE | MIIM_SUBMENU;
					itemInfo.hSubMenu = g_hMenuVariants[index];
				}
				else
					itemInfo.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
				itemInfo.dwTypeData = mechs[i];
				InsertMenuItem(g_hMenuMechs,index,TRUE,&itemInfo);
				index++;
			}
		}
	}

	Str_Copy(denied_error[0],  "", 64);
	for (i=IDS_MP_LOBBY_BUILD_ERROR_UNK; i<=IDS_MP_LOBBY_BUILD_ERROR_SUBSYS; i++)
		Str_Copy(denied_error[i-IDS_MP_LOBBY_BUILD_ERROR_UNK+1], gos_GetResourceString(g_langResources, i), 64);
	Str_Copy(denied_error[i-IDS_MP_LOBBY_BUILD_ERROR_UNK+1], gos_GetResourceString(g_langResources, IDS_MP_LOBBY_BUILD_ERROR_WEIGHT_TOTALMAX), 64);
// MSL 5.02
//	for (i=DNL_ARGUS; i<=DNL_VULTURE; i++)
//		Str_Copy(mechlist[i-DNL_ARGUS], gos_GetResourceString(g_langResources, i), 32);
	for (i=0; i<=DNL_ZEUS; i++)
	Str_Copy(mechlist[i-0], gos_GetResourceString(g_langResources, i), 32);
// MSL 5.02
//	Str_Copy(mechlist[21], gos_GetResourceString(g_langResources, IDS_MP_LOBBY_CAMERA),32);
//	Str_Copy(mechlist[22], gos_GetResourceString(g_langResources, IDS_MP_LOBBY_NONE),32);
	Str_Copy(mechlist[DNL_ZEUS + 1], gos_GetResourceString(g_langResources, IDS_MP_LOBBY_CAMERA),32);
	Str_Copy(mechlist[DNL_ZEUS + 2], gos_GetResourceString(g_langResources, IDS_MP_LOBBY_NONE),32);

	for (i=0; i<8; i++) {
		sprintf(teamstring[i], "%d", i+1); 
	}
	Str_Copy(teamstring[8],gos_GetResourceString(g_langResources, IDS_MP_LOBBY_DASH),8);
	g_hMenuTeams = ::GetSubMenu(::LoadMenu(g_hInst, MAKEINTRESOURCE(IDR_MENU_PLAYERMAN)), 1);
	
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_TEAMMAXTON), CB_RESETCONTENT, 0,0);
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_TEAMMINTON), CB_RESETCONTENT, 0,0);
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_TEAMTOTMAXTON), CB_RESETCONTENT, 0,0);
	MString tonstring[] = {"25","30","35","40","45","50","55","60","65","70","75","80","85","90","95","100"};
	MString totstring[] = {"100","200","300","400","500","600","700","800","900","1000","1100","1200","1300","1400","1500","1600"};
	for (i=0; i<16; i++) {
		SendMessage(GetDlgItem(hWnd, IDC_COMBO_TEAMMAXTON), CB_INSERTSTRING, i,(LPARAM)(char*)tonstring[i]);
		SendMessage(GetDlgItem(hWnd, IDC_COMBO_TEAMMINTON), CB_INSERTSTRING, i,(LPARAM)(char*)tonstring[i]);
		SendMessage(GetDlgItem(hWnd, IDC_COMBO_TEAMTOTMAXTON), CB_INSERTSTRING, i,(LPARAM)(char*)totstring[i]);
	}
	
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_WEATHER), CB_RESETCONTENT, 0,0);
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_WEATHER), CB_INSERTSTRING, 0, (LPARAM)gos_GetResourceString(g_langResources,IDS_GN_OFF));
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_WEATHER), CB_INSERTSTRING, 1, (LPARAM)gos_GetResourceString(g_langResources,IDS_GN_ON));

	SendMessage(GetDlgItem(hWnd, IDC_COMBO_VISIBILITY), CB_RESETCONTENT, 0,0);
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_VISIBILITY), CB_INSERTSTRING, 0, (LPARAM)gos_GetResourceString(g_langResources,IDS_GN_DEFAULT_LC));
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_VISIBILITY), CB_INSERTSTRING, 1, (LPARAM)gos_GetResourceString(g_langResources,IDS_MP_HLM_CLEAR));
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_VISIBILITY), CB_INSERTSTRING, 2, (LPARAM)gos_GetResourceString(g_langResources,IDS_MP_HLM_LIGHTFOG));
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_VISIBILITY), CB_INSERTSTRING, 3, (LPARAM)gos_GetResourceString(g_langResources,IDS_MP_HLM_HEAVYFOG));
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_VISIBILITY), CB_INSERTSTRING, 4, (LPARAM)gos_GetResourceString(g_langResources,IDS_MP_HLM_PSOUPFOG));

	SendMessage(GetDlgItem(hWnd, IDC_COMBO_TIMEOFDAY), CB_RESETCONTENT, 0,0);
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_TIMEOFDAY), CB_INSERTSTRING, 0, (LPARAM)gos_GetResourceString(g_langResources,IDS_MP_HLM_DAY));
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_TIMEOFDAY), CB_INSERTSTRING, 1, (LPARAM)gos_GetResourceString(g_langResources,IDS_MP_HLM_NIGHT));

	SendMessage(GetDlgItem(hWnd, IDC_COMBO_TIMELIMIT), CB_RESETCONTENT, 0,0);
	char* lengths[] = {"1", "2", "5", "10", "15", "30", "60", "90", "120"};
	for (i=0; i<9; i++)
		SendMessage(GetDlgItem(hWnd, IDC_COMBO_TIMELIMIT), CB_INSERTSTRING, i, (LPARAM)lengths[i]);

	SendMessage(GetDlgItem(hWnd, IDC_COMBO_STOCK), CB_RESETCONTENT, 0,0);
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_STOCK), CB_INSERTSTRING, 0, (LPARAM)gos_GetResourceString(g_langResources,IDS_GN_OFF));
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_STOCK), CB_INSERTSTRING, 1, (LPARAM)gos_GetResourceString(g_langResources,IDS_GN_ON));

	SendMessage(GetDlgItem(hWnd, IDC_COMBO_REVIEW), CB_RESETCONTENT, 0,0);
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_REVIEW), CB_INSERTSTRING, 0, (LPARAM)gos_GetResourceString(g_langResources,IDS_GN_OFF));
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_REVIEW), CB_INSERTSTRING, 1, (LPARAM)gos_GetResourceString(g_langResources,IDS_GN_ON));

	SendMessage(GetDlgItem(hWnd, IDC_COMBO_MAXTON), CB_RESETCONTENT, 0,0);
	for (i=30; i<=100; i+=5) {
		char temp[8];
		itoa(i, temp, 10);
		MString tons = MString(temp)+" "+gos_GetResourceString(g_langResources,IDS_ML_WP_TONS);
		SendMessage(GetDlgItem(hWnd, IDC_COMBO_MAXTON), CB_INSERTSTRING, (i-30)/5, (LPARAM)(char*)tons);
	}

	SendMessage(GetDlgItem(hWnd, IDC_COMBO_RADAR), CB_RESETCONTENT, 0,0);
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_RADAR), CB_INSERTSTRING,0, (LPARAM)gos_GetResourceString(g_langResources,IDS_OM_NORMAL));
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_RADAR), CB_INSERTSTRING,1, (LPARAM)gos_GetResourceString(g_langResources,IDS_MP_HLM_TEAMONLY));
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_RADAR), CB_INSERTSTRING,2, (LPARAM)gos_GetResourceString(g_langResources,IDS_MP_HLM_NORADAR));
	
	NotationFile startup_ini("options.ini", NotationFile::Standard, true);
	Stuff::Page *page = startup_ini.FindPage("server");
	if (page) {
		page->GetEntry("FillGame", &fill_game);
	}
	SendMessage(GetDlgItem(hWnd, IDC_CHECK_FILLTEAM), BM_SETCHECK, fill_game?BST_CHECKED:BST_UNCHECKED, 0);

	HWND playerList = GetDlgItem(hWnd, IDC_LIST_PLAYERS);
	RECT rc;
	::GetWindowRect(playerList, &rc);
	SendMessage(playerList, LB_SETCOLUMNWIDTH, (rc.right-rc.left)/6, 0);
	SendMessage(playerList, LB_SETCOUNT, 16*6, 0);

	srand( (unsigned)time(NULL));
	int bot_number = rand()%NUM_CALLSIGNS;
	SetDlgItemInt(hWnd, IDC_EDIT_BOTSKILL, BotLevels[bot_number], FALSE);
	SetDlgItemText(hWnd, IDC_EDIT_BOTNAME, BotNames[bot_number]); 
	SendMessage(GetDlgItem(hWnd, IDC_EDIT_BOTNAME), EM_LIMITTEXT, 16, 0);

	SendMessage(GetDlgItem(hWnd, IDC_COMBO_MECHS), CB_RESETCONTENT, 0 ,0);
	index = 0;
	for (i=0; i<MW4Shell::Instance->m_mechCount; i++) {
		if (bStock[i]) {
			SendMessage(GetDlgItem(hWnd, IDC_COMBO_MECHS), CB_INSERTSTRING, index, (LPARAM)mechs[i]);
			stock_array[index] = i;
			index++;
		}
	}
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_MECHS), CB_SETCURSEL, 1,0);
	OnMechSelChange(hWnd);

	SendMessage(GetDlgItem(hWnd, IDC_EDIT_CHATENTRY), EM_LIMITTEXT, 102, 0);
	SendMessage(GetDlgItem(hWnd, IDC_EDIT_BOTSKILL), EM_LIMITTEXT, 1, 0);
	SendMessage(GetDlgItem(hWnd, IDC_EDIT_FRAGLIMIT), EM_LIMITTEXT, 2, 0);
	SendMessage(GetDlgItem(hWnd, IDC_EDIT_LIFELIMIT), EM_LIMITTEXT, 2, 0);
	//SendMessage(GetDlgItem(hWnd, IDC_LIST_CHAT), EM_SETWORDBREAKPROC, 0, (LPARAM)MyEditWordBreakProc);
	::SetTimer(hWnd, g_chatTimer, 1000, NULL);

	SetNextGame(hWnd);
}

void CDedicatedServerUI::GameLobbyOnLaunch(HWND hWnd) 
{
	fill_game = (SendMessage(GetDlgItem(hWnd, IDC_CHECK_FILLTEAM), BM_GETCHECK, 0, 0)==BST_CHECKED)?1:0;

	NotationFile startup_ini("options.ini", NotationFile::Standard, true);
	Stuff::Page *page = startup_ini.FindPage("server");
	if (page) {
		page->SetEntry("FillGame", fill_game);
		startup_ini.Save();
	}

	if (fill_game) {
		HWND hWndWait = CreateDialog(g_hInst, MAKEINTRESOURCE(IDD_WAITADDBOTS), g_hWndMain, NULL);
		RECT rcDesk, rc;
		GetWindowRect(g_hWndMain, &rcDesk);
		GetWindowRect(hWndWait, &rc);
		int w = rc.right-rc.left;
		int h = rc.bottom-rc.top;
		rc.left = rcDesk.left + ((rcDesk.right-rcDesk.left)-w)/2;
		rc.top = rcDesk.top + ((rcDesk.bottom-rcDesk.top)-h)/2;
		MoveWindow(hWndWait, rc.left, rc.top, w, h, FALSE);
		ShowWindow(hWndWait, SW_SHOW);
		
		MSG	msg;
		while( PeekMessage(&msg,NULL,0,0,PM_REMOVE) )
		{
			if( msg.message==WM_QUIT ) 
				break;
			
			TranslateMessage( &msg );
			DispatchMessage( &msg );
		}
		
		int tempIds[256];
		int tempMechIds[256];
		int numBots = MW4Shell::FillBotsForAllTeams(tempIds, tempMechIds);
		for (int i=0; i<numBots; i++) {
			if (tempIds[i]>=0 && tempIds[i]<MWApplication::Maximum_Lancemates) {
				host_bot_from_file[tempIds[i]] = 0;
				host_bot_last_val[tempIds[i]] = tempMechIds[i];
				host_bot_last_skin[tempIds[i]] = 4;
				host_bot_last_player_num[tempIds[i]] = 255;
			}
		}
		EndDialog(hWndWait, TRUE);
	}

	void* intParam[2];
	int temp = NIGHT_TIME_PARAMETER;
	char need_time_of_day[STRING_SIZE];
	intParam[0] = (void*)&temp;
	intParam[1] = (void*)&need_time_of_day;

	if (MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam) == 1)
		Str_Copy(need_time_of_day, gos_GetResourceString(g_langResources,IDS_MP_LOBBY_NEED_TIME_NIGHT), STRING_SIZE);
	else
		Str_Copy(need_time_of_day, gos_GetResourceString(g_langResources,IDS_MP_LOBBY_NEED_TIME_DAY), STRING_SIZE);
				
	MW4Shell::Shell_CallbackHandler(NULL, 2, (void**)intParam);

	MWApplication *app = MWApplication::GetInstance();
	app->StartNetGame();
}

void CDedicatedServerUI::OnGameTypeChange(HWND hWnd)
{
	int sel = SendMessage(GetDlgItem(hWnd, IDC_COMBO_GAMETYPE), CB_GETCURSEL, 0, 0);
	if (sel == CB_ERR)
		return;

	int* intParam[2];
	int temp = RULE_TYPE_PARAMETER;
	int value = selection_to_standard[sel];
	intParam[0] = &temp;
	intParam[1] = &value;

	if (last_selected_game == value)
		return;

	if (bots_exist && (value != 0) && (value != 1) && (value != 2) && (value != 3) &&
		value < 13)
	{
		if (IDYES==MechMessageBoxWin32(gos_GetResourceString(g_langResources,IDS_GN_ERROR),
									  gos_GetResourceString(g_langResources,IDS_HLM_NO_BOTS),
									  MB_YESNO)) {
			MW4Shell::RemoveAllBots(NULL, 0, NULL);
			for (int a=0;a<255;a++) {
				host_bot_last_val[a] = -1;
				host_bot_last_player_num[a] = -1;
				host_bot_last_skin[a] = 0;
				host_bot_from_file[a] = 0;
			}
		}
		else {
			temp = RULE_TYPE_PARAMETER;
			int temp_game = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
			SendMessage(GetDlgItem(hWnd, IDC_COMBO_GAMETYPE), CB_SETCURSEL, standard_to_selection[temp_game], 0);
			last_selected_game = temp_game;
			temp = temp_game;
			MW4Shell::GetGameTypeString(NULL, 1, (void**)intParam);
			return;
		}
	}

	last_selected_game = value;
	temp = RULE_TYPE_PARAMETER;
	value = selection_to_standard[sel];
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**) intParam);
	MW4Shell::Instance->InitNetworkScenarios(NULL, 0, NULL);
	
	temp = selection_to_standard[sel];
	MW4Shell::GetGameTypeString(NULL, 1, (void**)intParam);
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_MAP), CB_RESETCONTENT, 0, 0);
	for (int i = 0; i < MW4Shell::Instance->networkScenarioCount; ++i)
	{
		MString scenario_name = MW4Shell::Instance->scenarios[i].scenarioName;
		SendMessage(GetDlgItem(hWnd, IDC_COMBO_MAP), CB_INSERTSTRING, i, (LPARAM)(char*)scenario_name);
	}
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_MAP), CB_SETCURSEL, last_selected_map, 0);
	
	EnableWindow(GetDlgItem(hWnd, IDC_COMBO_MAP), MW4Shell::Instance->networkScenarioCount);
	EnableWindow(GetDlgItem(hWnd, IDLAUNCH), MW4Shell::Instance->networkScenarioCount);
	
	last_selected_map = -1;
	OnMapChange(hWnd);
}

void CDedicatedServerUI::OnMapChange(HWND hWnd)
{
	int value = SendMessage(GetDlgItem(hWnd, IDC_COMBO_MAP), CB_GETCURSEL, 0, 0);
	if (last_selected_map == value && value != CB_ERR)
		return;

	last_selected_map = value;

	int* intParam[2];

	// Remove bots from previous nfo file
	for (int a=0; a<255; a++) {
		if (host_bot_from_file[a]) {
			intParam[0] = &a;
			MW4Shell::RemoveBot(NULL, 1, (void**)intParam);
			host_bot_last_val[a] = -1;
			host_bot_last_player_num[a] = -1;
			host_bot_last_skin[a] = 0;
			host_bot_from_file[a] = 0;
		}
	}

	if (value == CB_ERR) 
		return;

	int temp = MAP_ID_PARAMETER;
	intParam[0] = &temp;
	intParam[1] = &value;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);

	// Force a nfo reload
	MW4Shell::Instance->selectedScenario = -1;
	MW4Shell::SelectNetworkScenarioDirect(value);
	temp = TEAM_ALLOWED_PARAMETER;
	BOOL team_val = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);	
	if (team_val)
		OnTeamSelChange(hWnd);

	if ((last_selected_game>=0 && last_selected_game<=3) ||
		last_selected_game>=13) {
		int tempIds[256];
		int tempMechIds[256];
		int numBots = MW4Shell::AddBots(value, tempIds, tempMechIds);
		for (int i=0; i<numBots; i++) {
			if (tempIds[i]>=0 && tempIds[i]<MWApplication::Maximum_Lancemates) {
				host_bot_from_file[tempIds[i]] = 1;
				host_bot_last_val[tempIds[i]] = tempMechIds[i];
				host_bot_last_skin[tempIds[i]] = 4;
				host_bot_last_player_num[tempIds[i]] = 255;
			}
		}
	}

	// Update mission parameters
	temp = WEATHER_PARAMETER;
	int weather = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_WEATHER), CB_SETCURSEL, weather, 0);

	temp = VISIBILITY_PARAMETER;
	int visibility = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_VISIBILITY), CB_SETCURSEL, visibility, 0);

	temp = NIGHT_TIME_PARAMETER;
	int nightTime = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	temp = ShellSetTime;
	value = nightTime;
	MW4Shell::Instance->SetTime((void**)intParam, 2);
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_TIMEOFDAY), CB_SETCURSEL, nightTime, 0);

	temp = GAME_LENGTH_PARAMETER;
	int timeLimit = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	if (timeLimit == 0) 
		SendMessage(GetDlgItem(hWnd, IDC_COMBO_TIMELIMIT), CB_SETCURSEL, 0, 0);
	else
	{
		for (int doh = 0; doh< 9; doh++)
		{
			char temp[8];
			SendMessage(GetDlgItem(hWnd, IDC_COMBO_TIMELIMIT), CB_GETLBTEXT, doh, (LPARAM)temp);
			if (::atoi(temp) >= timeLimit)
			{
				SendMessage(GetDlgItem(hWnd, IDC_COMBO_TIMELIMIT), CB_SETCURSEL, doh, 0);
				break;
			}
		}
	}

	temp = KILL_LIMIT_NUMBER_PARAMETER;
	int killLimit = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	if (killLimit <= 0)
		killLimit = 1;
	SetDlgItemInt(hWnd, IDC_EDIT_FRAGLIMIT, killLimit, FALSE);

	temp = RESPAWN_LIMIT_NUMBER_PARAMETER;
	int life = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam) + 1;
	SetDlgItemInt(hWnd,IDC_EDIT_LIFELIMIT, life, FALSE);

	temp = ONLY_STOCK_MECH_PARAMETER;
	int stock = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_STOCK), CB_SETCURSEL, stock, 0);

	temp = PLAY_MISSION_REVIEW_PARAMETER;
	int review = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_REVIEW), CB_SETCURSEL, review, 0);
	
	temp = MAXIMUM_TONNAGE_PARAMETER;
	int tons = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_MAXTON), CB_SETCURSEL, (tons-30)/5, 0);

	temp = KILL_LIMIT_PARAMETER;
	int bFragLimit = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	SendMessage(GetDlgItem(hWnd, IDC_CHECK_FRAGLIMIT), BM_SETCHECK, bFragLimit>0?BST_CHECKED:BST_UNCHECKED, 0);
	EnableWindow(GetDlgItem(hWnd, IDC_EDIT_FRAGLIMIT), bFragLimit>0);

	temp = RESPAWN_LIMIT_PARAMETER;
	int bLifeLimit = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	SendMessage(GetDlgItem(hWnd, IDC_CHECK_LIFELIMIT), BM_SETCHECK, bLifeLimit>0?BST_CHECKED:BST_UNCHECKED, 0);
	EnableWindow(GetDlgItem(hWnd, IDC_EDIT_LIFELIMIT), bLifeLimit>0);
	
	temp = RADAR_MODE_PARAMETER;
	int radar = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_RADAR), CB_SETCURSEL, radar, 0);
}

void CDedicatedServerUI::OnWeatherChange(HWND hWnd)
{
	int* intParam[2];
	int temp = WEATHER_PARAMETER;
	int value = SendMessage(GetDlgItem(hWnd, IDC_COMBO_WEATHER), CB_GETCURSEL, 0, 0);
	intParam[0] = &temp;
	intParam[1] = &value;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);
}

void CDedicatedServerUI::OnVisibilityChange(HWND hWnd)
{
	int* intParam[2];
	int temp = VISIBILITY_PARAMETER;
	int value = SendMessage(GetDlgItem(hWnd, IDC_COMBO_VISIBILITY), CB_GETCURSEL, 0, 0);
	intParam[0] = &temp;
	intParam[1] = &value;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);
}

void CDedicatedServerUI::OnTimeOfDayChange(HWND hWnd)
{
	int* intParam[2];
	int temp = NIGHT_TIME_PARAMETER;
	int value = SendMessage(GetDlgItem(hWnd, IDC_COMBO_TIMEOFDAY), CB_GETCURSEL, 0, 0);
	intParam[0] = &temp;
	intParam[1] = &value;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);

	temp = ShellSetTime;
	MW4Shell::Instance->SetTime((void**)intParam, 2);
}

void CDedicatedServerUI::OnTimeLimitChange(HWND hWnd)
{
	int* intParam[2];
	int temp = GAME_LENGTH_PARAMETER;
	char tempTime[8];
	int sel = SendMessage(GetDlgItem(hWnd, IDC_COMBO_TIMELIMIT), CB_GETCURSEL, 0, 0);
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_TIMELIMIT), CB_GETLBTEXT, sel, (LPARAM)tempTime);
	int value = atoi(tempTime);
	intParam[0] = &temp;
	intParam[1] = &value;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);
}

void CDedicatedServerUI::OnFragLimitChange(HWND hWnd)
{
	int* intParam[2];
	int temp = KILL_LIMIT_NUMBER_PARAMETER;
	int value = GetDlgItemInt(hWnd, IDC_EDIT_FRAGLIMIT, NULL, FALSE);

	if (value<=0) {
		value =	1;
		SetDlgItemInt(hWnd, IDC_EDIT_FRAGLIMIT, value, FALSE);
	}
	intParam[0] = &temp;
	intParam[1] = &value;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);
}

void CDedicatedServerUI::OnLifeLimitChange(HWND hWnd)
{
	int* intParam[2];
	int temp = RESPAWN_LIMIT_NUMBER_PARAMETER;
	int value = GetDlgItemInt(hWnd, IDC_EDIT_LIFELIMIT, NULL, FALSE);
	if (value<=0) {
		value =	0;
		SetDlgItemInt(hWnd, IDC_EDIT_LIFELIMIT, 1, FALSE);
	}
	else value--;
	intParam[0] = &temp;
	intParam[1] = &value;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);
}

void CDedicatedServerUI::OnStockMechChange(HWND hWnd)
{
	int* intParam[2];
	int temp = ONLY_STOCK_MECH_PARAMETER;
	int value = SendMessage(GetDlgItem(hWnd, IDC_COMBO_STOCK), CB_GETCURSEL, 0, 0);
	intParam[0] = &temp;
	intParam[1] = &value;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);
	for (int a=0; a<16; a++)
		host_bot_last_val[a] = -1;
}

void CDedicatedServerUI::OnReviewChange(HWND hWnd)
{
	int* intParam[2];
	int temp = PLAY_MISSION_REVIEW_PARAMETER;
	int value = SendMessage(GetDlgItem(hWnd, IDC_COMBO_REVIEW), CB_GETCURSEL, 0, 0);
	intParam[0] = &temp;
	intParam[1] = &value;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);
}

void CDedicatedServerUI::OnRadarChange(HWND hWnd)
{
	int* intParam[2];
	int temp = RADAR_MODE_PARAMETER;
	int value = SendMessage(GetDlgItem(hWnd, IDC_COMBO_RADAR), CB_GETCURSEL, 0, 0);
	intParam[0] = &temp;
	intParam[1] = &value;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);
}

void CDedicatedServerUI::OnMaxTonnageChange(HWND hWnd)
{
	int* intParam[2];
	int temp = MAXIMUM_TONNAGE_PARAMETER;
	int value = 30+5*SendMessage(GetDlgItem(hWnd, IDC_COMBO_MAXTON), CB_GETCURSEL, 0, 0);
	intParam[0] = &temp;
	intParam[1] = &value;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);
	for (int a=0; a<16; a++)
		host_bot_last_val[a] = -1;
}

void CDedicatedServerUI::OnCheckNumLives(HWND hWnd)
{		
	int* intParam[2];
	int temp = RESPAWN_LIMIT_PARAMETER;
	int value = (SendMessage(GetDlgItem(hWnd, IDC_CHECK_LIFELIMIT), BM_GETCHECK, 0, 0)==BST_CHECKED)?1:0;
	intParam[0] = &temp;
	intParam[1] = &value;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);
	EnableWindow(GetDlgItem(hWnd, IDC_EDIT_LIFELIMIT), value);
}

void CDedicatedServerUI::OnCheckFragLimit(HWND hWnd)
{		
	int* intParam[2];
	int temp = KILL_LIMIT_PARAMETER;
	int value = (SendMessage(GetDlgItem(hWnd, IDC_CHECK_FRAGLIMIT), BM_GETCHECK, 0, 0)==BST_CHECKED)?1:0;
	intParam[0] = &temp;
	intParam[1] = &value;
	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParam);
	EnableWindow(GetDlgItem(hWnd, IDC_EDIT_FRAGLIMIT), value);
}

void CDedicatedServerUI::GameLobbyOnChat(HWND hWnd)
{
	int out_going_count = 0;

	for (int loopit = 0; loopit < 255; loopit++)
	{	
		int* intParam[1];
		int temp = loopit;
		intParam[0] = &temp;
		int valid_player = MW4Shell::IsPlayerConnectionValid(NULL, 1, (void**)intParam);

		if (valid_player)
		{
			MW4Shell::Instance->m_outgiongChatToPlayer[out_going_count] = loopit;
			out_going_count++;
		}
	}

	char temp[STRING_SIZE];
	GetDlgItemText(hWnd, IDC_EDIT_CHATENTRY, temp, STRING_SIZE);

	while (strlen(temp)>0 && (temp[strlen(temp)-1] <= 0x20 || temp[strlen(temp)-1] > 0x7f)) {
		temp[strlen(temp)-1] = '\0';
	}

	if (strcmp(temp, "")!=0) {
		MW4Shell::Instance->m_outgoingChatToPlayerCount = out_going_count;
		MW4Shell::Instance->m_outgoingChat = temp;
		MW4Shell::Instance->SendBroadcastChat(NULL, 0, NULL);
	}
	SetDlgItemText(hWnd, IDC_EDIT_CHATENTRY, "");
}

void CDedicatedServerUI::GameLobbyUpdateChat(HWND hWnd)
{
	HWND hWndChat = GetDlgItem(hWnd, IDC_LIST_CHAT);
	
	if (MW4Shell::Instance->m_currentChatCount > 0)
	{
		MString temp_val = MW4Shell::Instance->m_chatFrom[MW4Shell::Instance->m_currentChatCount-1];
		temp_val += ":> ";
		temp_val += MW4Shell::Instance->m_chatArray[MW4Shell::Instance->m_currentChatCount-1];
		temp_val += "\n";

		char chatEntry[STRING_SIZE];
		ZeroMemory(chatEntry, STRING_SIZE);
		WORD* pSize = (WORD*)&chatEntry[0];
		*pSize = STRING_SIZE;
		SendMessage(hWndChat, EM_GETLINE, MW4Shell::Instance->m_currentChatCount-1, (LPARAM) chatEntry);
		if (strncmp(chatEntry, temp_val, temp_val.GetLength()) == 0)
		{
		}
		else
		{
			SendMessage(hWndChat, WM_SETTEXT, 0, (LPARAM)"");
			for (int x = 0;x < MW4Shell::Instance->m_currentChatCount; x++)
			{
				MString temp_val = MW4Shell::Instance->m_chatFrom[x];
				temp_val += ":> ";
				temp_val += MW4Shell::Instance->m_chatArray[x];
				temp_val += "\n";
				SendMessage(hWndChat, EM_REPLACESEL, 0, (LPARAM)(char*)temp_val);
			}
			SendMessage(hWndChat, EM_SCROLLCARET, 0, 0);
		}
	}
}

void CDedicatedServerUI::GameLobbyUpdatePlayers(HWND hWnd)
{
	static bool running = false;
	if (running) return;

	running = true;
	for (int i=0; i<17; i++) {
		FREE_PTR(print_names[i]);
		FREE_PTR(faction_name[i]);
		strcpy(base_names[i], "");
	}

	for (int loopit = 0;loopit <8; loopit++)
	{
		team_roster_count[loopit] = 0;
		team_legal_to_join[loopit] = 0;
	}
	team_legal_to_join[8] = TRUE;

	num_of_players = 0;

	// used to prevent user from launching team game without people being on teams	
	BOOL block_launch_or_ready = FALSE;	
	
	int skin_id;   ///////VERY IMPORTANT FOR NEW SKIN INTERFACE
	int player_num = 0;
	int valid_player;
	
	int team;
	int ready;
	int chasis;
	int launched;
	
	int return_value = 0;
		
	valid_count = 0;

	void *params[2];
	int param0;
	int param1;
	params[0] = (void*)&param0;
	params[1] = (void*)&param1;

	param0 = TEAM_ALLOWED_PARAMETER;
	BOOL team_val = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, params);	

	MWApplication *app = MWApplication::GetInstance();
	for(int a=0;a<255;a++)
	{
		param0 = a;
		valid_player = MW4Shell::IsPlayerConnectionValid(NULL, 1, params);
		if (a!=1 && valid_player) //Don't think I need this > && dedicated_server_flag = FALSE
		{
			char *temp_name = NULL;

			// make sure even when player moves around list, the pleyer_num change appropriately
			if (a == MW4Shell::MyPlayerId(NULL, 0, NULL))
				player_num = valid_count;
			
			param0 = a;
			params[1] = (void*) &temp_name;
			MW4Shell::GetPlayerConnectionName(NULL, 2, params);

			params[1] = (void*) &faction_name[valid_count];
			MW4Shell::GetPlayerConnectionClan(NULL, 2, params);

			params[1] = (void*) &temp_name;
			ready = MW4Shell::GetPlayerConnectionReady(NULL, 2, params);

			player_ping[valid_count] = MW4Shell::GetPlayerPing(NULL, 1, params);

			launched = MW4Shell::GetPlayerConnectionLaunched(NULL, 2, params);
			chasis = MW4Shell::GetPlayerConnectionChasis(NULL, 2, params);
			team = MW4Shell::GetPlayerConnectionTeam(NULL, 2, params);
			skin_id = MW4Shell::GetPlayerConnectionSkin(NULL, 1, params);
						
			i_am_a_bot[valid_count] = 0;
			player_number[valid_count] = a;
			
			{
				// fill it with Jerry's values		
				
				mech_ids[valid_count] = chasis;
				team_ids[valid_count] = team;

				if (skin_id > -1 && skin_id < 37)
				{
					skin_ids[valid_count] = skin_id+1;
				}
				
				if (ready)
				{
					status[valid_count] = 2;
				}
				else if (app->servedConnectionData[a].clientVehicleAccepted)
				{
					status[valid_count] = 1;
				}
				else 
				{
					status[valid_count] = 0;
				}
			}

			print_names[valid_count] = temp_name;
			tonnages[valid_count] = app->servedConnectionData[a].tonnage;
			num_of_players++;
			team_roster_count[team]++;				
			valid_count++;				
		}
	}

	bots_exist = 0;

	// Update the Bots
	int num_bots = 0;
	int report_error = 1;
	for(a=0;a<16;a++)
	{
		void *params[2];
		params[0] = (void*) &a;
		valid_player = MW4Shell::GetBotValid(NULL, 1, params);
		if (valid_player)
		{
			num_bots++;
			bots_exist = 1;

			char *temp_name = NULL;

			params[1] = (void*) &temp_name;
			MW4Shell::GetBotName(NULL, 2, params);
			
			params[1] = (void*) &faction_name[valid_count];
			MW4Shell::GetBotClan(NULL, 2, params);
			
			ready = 1;
			launched = 0;
			chasis = MW4Shell::GetBotChasis(NULL, 1, params);
			team = MW4Shell::GetBotTeam(NULL, 1, params);
			
			skin_ids[valid_count] = 4;			
			i_am_a_bot[valid_count] = 1;
			bot_number[valid_count] = a;
			team_ids[valid_count] = team;

			if ((host_bot_last_player_num[a] != valid_count) && (host_bot_last_player_num[a] != -1))
			{
				mech_ids[valid_count] = host_bot_last_val[a];
				skin_ids[valid_count] = host_bot_last_skin[a];
				host_bot_last_skin[a] = 0;
			}
			else
			{
				host_bot_last_skin[a] = skin_ids[valid_count];
				
				if ((mech_ids[valid_count] == 0) || (mech_ids[valid_count] > MW4Shell::Instance->m_mechCount-1))
				{
					if (host_bot_last_val[a] == -1)
					{
						int mech_index = 1;
						return_value = 0;
						accepted[valid_count] = 0;
						while (((return_value == 0) || (accepted[valid_count] == 0)) && (mech_index < 22))
						{
							mech_ids[valid_count] = mech_index;
							return_value = SetMechBot(a, mech_index);
							mech_index++;
							
							accepted[valid_count] = MW4Shell::GetBotVehicleAccepted(NULL, 1, params);
						}
						
						if (!accepted[valid_count])
						{		
							OnBotError();
							mech_ids[valid_count] = 1;
						}
						
						host_bot_last_val[a] = mech_ids[valid_count];
					}
					else
					{
						mech_ids[valid_count] = host_bot_last_val[a];
					}
				}
				else
				{
					if (mech_ids[valid_count] != host_bot_last_val[a])
					{
						accepted[valid_count] = 0;
						return_value = SetMechBot(a, mech_ids[valid_count]);
						accepted[valid_count] = MW4Shell::GetBotVehicleAccepted(NULL, 1, params);
						if (return_value != 0)
						{
							if (!accepted[valid_count])
							{
								if (report_error)
								{
									OnBotError();
									report_error = 0;
								}
							}
							
							host_bot_last_val[a] = mech_ids[valid_count];
						}
					}
				}
			}
					
			if (team_val == TRUE) {
				int* intParam[1];
				int temp = TEAM_COUNT_PARAMETER;
				intParam[0] = &temp;
				int teams = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);		
				if (team_ids[valid_count] >= teams && mech_ids[valid_count] != 0)
					block_launch_or_ready = TRUE;
			}

			host_bot_last_player_num[a] = valid_count;
			status[valid_count] = accepted[valid_count];
			print_names[valid_count] = temp_name;
			GetMechBaseName(mechs[mech_ids[valid_count]], base_names[valid_count]);
			tonnages[valid_count] = app->lancemateConnectionData[a].tonnage;
			num_of_players++;
			team_roster_count[team]++;				
			valid_count++;			
		}
	}
	
	// Remove bots for required open slots
	if (game_state==0 && bots_exist && num_of_players > max_bots){
		int bot_to_kick[16];
		for (a=0;a<16;a++)
		{
			bot_to_kick[a] = a;
		}
		int m, n, temp;
		for (a=0;a<8;a++)
		{
			m = rand()%16;
			n = rand()%16;
			temp = bot_to_kick[m];
			bot_to_kick[m] = bot_to_kick[n];
			bot_to_kick[n] = temp;
		}
		for(a=0;a<16;a++)
		{
			if (bots_exist && num_of_players > max_bots) {
				void *params[2];
				params[0] = (void*) &bot_to_kick[a];
				valid_player = MW4Shell::GetBotValid(NULL, 1, params);
				if (valid_player)
				{
					MW4Shell::RemoveBot(NULL, 1, params);
					num_bots--;
					num_of_players--;
					valid_count--;			
				}
			}
			else
				break;
		}
	}

	// Update the Bots
	for(a=0;a<16;a++)
	{
		void *params[2];
		params[0] = (void*) &a;
		valid_player = MW4Shell::GetBotValid(NULL, 1, params);
		if (valid_player) {
			accepted[host_bot_last_player_num[a]] = MW4Shell::GetBotVehicleAccepted(NULL, 1, params);
			status[host_bot_last_player_num[a]] = accepted[host_bot_last_player_num[a]];
			
			if (!accepted[host_bot_last_player_num[a]])
				block_launch_or_ready = TRUE;
		}
	}
	
	ShowWindow(GetDlgItem(hWnd, IDLAUNCH), (!block_launch_or_ready && !game_state)?SW_SHOW:SW_HIDE);
	ShowWindow(GetDlgItem(hWnd, IDEXIT), (game_state)?SW_SHOW:SW_HIDE);
	ShowWindow(GetDlgItem(hWnd, IDC_STATIC_DENIED), block_launch_or_ready?SW_SHOW:SW_HIDE);

	EnableWindow(GetDlgItem(hWnd, IDC_BUTTON_ADDBOT), 
		((last_selected_game>=0 && last_selected_game<=3) || last_selected_game>=13) &&
		  num_of_players<max_bots && !game_state);

	GameLobbyUpdateTeamUI(hWnd, team_val);
	GameLobbyDisplayPlayers(hWnd);
	running = false;
}

void CDedicatedServerUI::GameLobbyDisplayPlayers(HWND hWnd)
{
	char szPlayers[8];
	sprintf(szPlayers, "%d/%d", num_of_players, max_players);
	SetWindowText(GetDlgItem(hWnd, IDC_STATIC_PLAYERS), szPlayers);

	void *params[2];
	int param0;
	int param1;
	params[0] = (void*)&param0;
	params[1] = (void*)&param1;

	param0 = TEAM_ALLOWED_PARAMETER;
	BOOL team_val = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, params);	
	::ShowWindow(GetDlgItem(hWnd, IDC_STATIC_TEAM), team_val == TRUE);
	::ShowWindow(GetDlgItem(hWnd, IDC_STATIC_SKIN), team_val != TRUE);

	HWND playerList = GetDlgItem(hWnd, IDC_LIST_PLAYERS);
	SendMessage(playerList, LB_RESETCONTENT, 0, 0);

	// Add player names
	for (int i=0; i<valid_count; i++)
	{
		SendMessage(playerList, LB_INSERTSTRING, i, (LPARAM)print_names[i]);
	}

	for (i=valid_count; i<16; i++)
	{
		SendMessage(playerList, LB_INSERTSTRING, i, (LPARAM)" ");
	}

	// Add the mech names
	for (i=0; i<valid_count; i++)
	{
		if (!i_am_a_bot[i])
			SendMessage(playerList, LB_INSERTSTRING, i+16, (LPARAM)(char*)mechlist[mech_ids[i]]);
		else {
			if (bStock[mech_ids[i]])
				SendMessage(playerList, LB_INSERTSTRING, i+16, (LPARAM)(char*)mechs[mech_ids[i]]);
			else {
				SendMessage(playerList, LB_INSERTSTRING, i+16, (LPARAM)base_names[i]);
			}
		}
	}

	for (i=valid_count; i<16; i++)
	{
		SendMessage(playerList, LB_INSERTSTRING, i+16, (LPARAM)" ");
	}

	// Add variant names
	for (i=0; i<valid_count; i++)
	{
		if (i_am_a_bot[i]) {		
			if (bStock[mech_ids[i]])
				SendMessage(playerList, LB_INSERTSTRING, i+16*2, (LPARAM)gos_GetResourceString(g_langResources, IDS_MP_LOBBY_STOCK));
			else {
				char* variantName = mechs[mech_ids[i]] + strlen(base_names[i]) + 1;
				SendMessage(playerList, LB_INSERTSTRING, i+16*2, (LPARAM)variantName);
			}
		}
		else
			SendMessage(playerList, LB_INSERTSTRING, i+16*2, (LPARAM)"");
	}

	for (i=valid_count; i<16; i++)
	{
		SendMessage(playerList, LB_INSERTSTRING, i+16*2, (LPARAM)" ");
	}

	for (i=0; i<valid_count; i++)
	{
		char temp[8];
		sprintf(temp, "%4.1f", tonnages[i]);
		SendMessage(playerList, LB_INSERTSTRING, i+16*3, (LPARAM)temp);
	}

	for (i=valid_count; i<16; i++)
	{
		SendMessage(playerList, LB_INSERTSTRING, i+16*3, (LPARAM)" ");
	}

	// Add teams/skins
	for (i=0; i<valid_count; i++)
	{
		MString teamSkinLevel;
		if (team_val == TRUE)
		{
			teamSkinLevel = teamstring[team_ids[i]];
		}
		else
		{
			char id[4];
			itoa(skin_ids[i], id, 10);
			teamSkinLevel = id;
		}
		if (i_am_a_bot[i])
			teamSkinLevel += (MString)" (BL:" + faction_name[i][strlen(faction_name[i])-1] + ")";
		SendMessage(playerList, LB_INSERTSTRING, i+16*4, (LPARAM)(char*)teamSkinLevel);
	}

	for (i=valid_count; i<16; i++)
	{
		SendMessage(playerList, LB_INSERTSTRING, i+16*4, (LPARAM)" ");
	}

	char szAccepted[STRING_SIZE];
	char szDenied[STRING_SIZE];
	char szReady[STRING_SIZE];
	LoadString(g_hInst, IDS_STRING_ACCEPTED, szAccepted, STRING_SIZE);
	LoadString(g_hInst, IDS_STRING_DENIED, szDenied, STRING_SIZE);
	LoadString(g_hInst, IDS_STRING_READY, szReady, STRING_SIZE);

	for (i=0; i<valid_count; i++)
	{
		Stuff::MString szStatus;
		switch (status[i]) {
		case 2:
			szStatus = szReady;
			break;
		case 1:
			szStatus = szAccepted;
			break;
		case 0:
			szStatus = szDenied;
			break;
		}

		if (!i_am_a_bot[i])
		{
			char szPing[STRING_SIZE];
			sprintf(szPing, " %i", player_ping[i]);
			szStatus += szPing;
		}
		SendMessage(playerList, LB_INSERTSTRING, i+16*5, (LPARAM)(char*)szStatus);
	}

	for (i=0; i<16; i++)
	{
		ShowWindow(GetDlgItem(hWnd, IDC_BUTTON_CHANGE0+i), (i<valid_count&&i_am_a_bot[i])?SW_SHOW:SW_HIDE);
		ShowWindow(GetDlgItem(hWnd, IDC_BUTTON_CHANGETEAM0+i), (i<valid_count&&team_val&&i_am_a_bot[i])?SW_SHOW:SW_HIDE);
	}
	ShowWindow(GetDlgItem(hWnd, IDC_STATIC_CHANGEMECH), valid_count>0?SW_SHOW:SW_HIDE);
	ShowWindow(GetDlgItem(hWnd, IDC_STATIC_CHANGETEAM), (valid_count>0&&team_val)?SW_SHOW:SW_HIDE);
}

void CDedicatedServerUI::GameLobbyUpdateTeamUI(HWND hWnd, BOOL team_val)
{
	ShowWindow(GetDlgItem(hWnd, IDC_COMBO_TEAMSEL), team_val?SW_SHOW:SW_HIDE);
	ShowWindow(GetDlgItem(hWnd, IDC_COMBO_TEAM), team_val?SW_SHOW:SW_HIDE);
	ShowWindow(GetDlgItem(hWnd, IDC_STATIC_TEAMSEL), team_val?SW_SHOW:SW_HIDE);
	ShowWindow(GetDlgItem(hWnd, IDC_STATIC_TEAMBOT), team_val?SW_SHOW:SW_HIDE);
	ShowWindow(GetDlgItem(hWnd, IDC_COMBO_TEAMMINTON), team_val?SW_SHOW:SW_HIDE);
	ShowWindow(GetDlgItem(hWnd, IDC_COMBO_TEAMMAXTON), team_val?SW_SHOW:SW_HIDE);
	ShowWindow(GetDlgItem(hWnd, IDC_COMBO_TEAMTOTMAXTON), team_val?SW_SHOW:SW_HIDE);
	ShowWindow(GetDlgItem(hWnd, IDC_EDIT_TEAMCURTOTTON), team_val?SW_SHOW:SW_HIDE);
	ShowWindow(GetDlgItem(hWnd, IDC_STATIC_TEAMMINTON), team_val?SW_SHOW:SW_HIDE);
	ShowWindow(GetDlgItem(hWnd, IDC_STATIC_TEAMMAXTON), team_val?SW_SHOW:SW_HIDE);
	ShowWindow(GetDlgItem(hWnd, IDC_STATIC_TOTMAXTON), team_val?SW_SHOW:SW_HIDE);
	ShowWindow(GetDlgItem(hWnd, IDC_STATIC_CURTOTTON), team_val?SW_SHOW:SW_HIDE);

	ShowWindow(GetDlgItem(hWnd, IDC_COMBO_MAXTON), team_val?SW_HIDE:SW_SHOW);
	ShowWindow(GetDlgItem(hWnd, IDC_STATIC_MAXTON), team_val?SW_HIDE:SW_SHOW);

	if (team_val == TRUE)
	{
		int sel = SendMessage(GetDlgItem(hWnd, IDC_COMBO_TEAMSEL), CB_GETCURSEL, 0, 0);
		if (sel != SendMessage(GetDlgItem(hWnd, IDC_COMBO_TEAMSEL), CB_GETCOUNT, 0, 0)-1)
		{
			MWApplication *app = MWApplication::GetInstance();
			Stuff::Scalar tonnage = app->GetCurrentTeamTotalTonage(sel);
			char temp[8];
			sprintf(temp, "%4.1f", tonnage);
			SetDlgItemText(hWnd, IDC_EDIT_TEAMCURTOTTON, temp);
		}
		else
			SetDlgItemText(hWnd, IDC_EDIT_TEAMCURTOTTON, "");	

		int* intParam[2];
		int temp = TEAM_COUNT_PARAMETER;
		int value;
		intParam[0] = &temp;
		intParam[1] = &value;
		int teams = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);		
				
		if (teams != host_last_teams_num)
		{
			SendMessage(GetDlgItem(hWnd, IDC_COMBO_TEAM), CB_RESETCONTENT, 0, 0);
			SendMessage(GetDlgItem(hWnd, IDC_COMBO_TEAMSEL), CB_RESETCONTENT, 0, 0);
			for (int i = 0; i<teams; i++) {
				::EnableMenuItem(g_hMenuTeams, ID_TEAMS_1+i, MF_ENABLED);
				SendMessage(GetDlgItem(hWnd, IDC_COMBO_TEAM), CB_INSERTSTRING, i, (LPARAM)teamstring[i]);
				SendMessage(GetDlgItem(hWnd, IDC_COMBO_TEAMSEL), CB_INSERTSTRING, i, (LPARAM)teamstring[i]);
			}
			SendMessage(GetDlgItem(hWnd, IDC_COMBO_TEAM), CB_INSERTSTRING, i, (LPARAM)teamstring[8]);
			SendMessage(GetDlgItem(hWnd, IDC_COMBO_TEAM), CB_SETCURSEL, 0,0);
			SendMessage(GetDlgItem(hWnd, IDC_COMBO_TEAMSEL), CB_INSERTSTRING, i, (LPARAM)(char*)gos_GetResourceString(g_langResources, IDS_MP_LOBBY_ALL_TEAMS));
			SendMessage(GetDlgItem(hWnd, IDC_COMBO_TEAMSEL), CB_SETCURSEL, 0,0);
			OnTeamSelChange(hWnd);
			for (i = teams; i<8; i++) {
				::EnableMenuItem(g_hMenuTeams, ID_TEAMS_1+i, MF_GRAYED);
			}
			host_last_teams_num = teams;
		}

		void* params[3];
		int param0 = TEAM_PARAMETER;
		int param1;
		int param2 = TEAM_MAX_PLAYERS;
		params[0] = (void**)&param0;
		params[1] = (void**)&param1;
		params[2] = (void**)&param2;
		int max_team_players;
		for (int loopey = 0;loopey <8; loopey++)
		{
			param1 = loopey;
			max_team_players = MW4Shell::GetLocalNetworkMissionParamater(NULL, 3, params);
					
			if (team_roster_count[loopey] >= max_team_players)
				team_legal_to_join[loopey] = FALSE;
			else
				team_legal_to_join[loopey] = TRUE;
		}
		team_legal_to_join[8] = TRUE;
	}

	else 
		host_last_teams_num = 0;
}

int CDedicatedServerUI::SetMechBot(int botNumber, int mech_index)
{
	void* params[3];
	int param0 = botNumber;
	int param1 = mech_index;
	char* param2 = mechs[mech_index];
	params[0] = (void**)&param0;
	params[1] = (void**)&param1;
	params[2] = (void**)&param2;

	return MW4Shell::SetMechBot(NULL, 3, params);
}

void CDedicatedServerUI::OnTeamSelChange(HWND hWndDlg)
{
	int sel = SendMessage(GetDlgItem(hWndDlg, IDC_COMBO_TEAMSEL), CB_GETCURSEL, 0, 0);
	if (sel == SendMessage(GetDlgItem(hWndDlg, IDC_COMBO_TEAMSEL), CB_GETCOUNT, 0, 0)-1)
	{
		SendMessage(GetDlgItem(hWndDlg, IDC_COMBO_TEAMMAXTON), CB_SETCURSEL, max_ton_allteams, 0);
		SendMessage(GetDlgItem(hWndDlg, IDC_COMBO_TEAMMINTON), CB_SETCURSEL, min_ton_allteams, 0);
		SendMessage(GetDlgItem(hWndDlg, IDC_COMBO_TEAMTOTMAXTON), CB_SETCURSEL, total_max_ton_allteams/100-1, 0);
	}

	else
	{
		int team_param = TEAM_PARAMETER;
		int min_tonnage = TEAM_MIN_TONNAGE;
		int max_tonnage = TEAM_MAX_TONNAGE;
		int tot_max_tonnage = TEAM_TOTAL_MAX_TONNAGE;
		void* params[3];
		params[0] = (void*)&team_param;
		params[1] = (void*)&sel;
		params[2] = (void*)&min_tonnage;

		int cal_val = MW4Shell::GetLocalNetworkMissionParamater(NULL, 3, params);
		Clamp(cal_val, 25, 100);
		SendMessage(GetDlgItem(hWndDlg, IDC_COMBO_TEAMMINTON), CB_SETCURSEL, (cal_val-25)/5, 0);

		params[2] = (void*)&max_tonnage;
		cal_val = MW4Shell::GetLocalNetworkMissionParamater(NULL, 3, params);
		Clamp(cal_val, 25, 100);
		SendMessage(GetDlgItem(hWndDlg, IDC_COMBO_TEAMMAXTON), CB_SETCURSEL, (cal_val-25)/5, 0);

		params[2] = (void*)&tot_max_tonnage;
		cal_val = MW4Shell::GetLocalNetworkMissionParamater(NULL, 3, params);
		Clamp(cal_val, 100, 1600);
		SendMessage(GetDlgItem(hWndDlg, IDC_COMBO_TEAMTOTMAXTON), CB_SETCURSEL, cal_val/100-1, 0);
	}
}

void CDedicatedServerUI::OnTeamMinTonChange(HWND hWndDlg)
{
	int selMin = SendMessage(GetDlgItem(hWndDlg, IDC_COMBO_TEAMMINTON), CB_GETCURSEL, 0, 0);
	int selMax = SendMessage(GetDlgItem(hWndDlg, IDC_COMBO_TEAMMAXTON), CB_GETCURSEL, 0, 0);
	if (selMin > selMax) {
		selMin = selMax;
		SendMessage(GetDlgItem(hWndDlg, IDC_COMBO_TEAMMINTON), CB_SETCURSEL, selMin, 0);
	}

	int temp_min = 25 + (5*selMin);
	int team_param = TEAM_PARAMETER;
	int min_tonnage = TEAM_MIN_TONNAGE;
	void* params[4];
	params[0] = (void*)&team_param;
	params[2] = (void*)&min_tonnage;
	params[3] = (void*)&temp_min;

	int sel = SendMessage(GetDlgItem(hWndDlg, IDC_COMBO_TEAMSEL), CB_GETCURSEL, 0, 0);
	if (sel == SendMessage(GetDlgItem(hWndDlg, IDC_COMBO_TEAMSEL), CB_GETCOUNT, 0, 0)-1)
	{
				
		for (int loza = 0; loza <8; loza++)
		{
			params[1] = (void*)&loza;
			MW4Shell::SetNetworkMissionParamater(NULL, 4, params);
		}
		
		min_ton_allteams = selMin;
	}
	
	else
	{
		params[1] = (void*)&sel;
		MW4Shell::SetNetworkMissionParamater(NULL, 4, params);
	}

	for (int a=0; a<16; a++)
		host_bot_last_val[a] = -1;
}

void CDedicatedServerUI::OnTeamMaxTonChange(HWND hWndDlg)
{
	int selMin = SendMessage(GetDlgItem(hWndDlg, IDC_COMBO_TEAMMINTON), CB_GETCURSEL, 0, 0);
	int selMax = SendMessage(GetDlgItem(hWndDlg, IDC_COMBO_TEAMMAXTON), CB_GETCURSEL, 0, 0);
	if (selMin > selMax) {
		selMax = selMin;
		SendMessage(GetDlgItem(hWndDlg, IDC_COMBO_TEAMMAXTON), CB_SETCURSEL, selMax, 0);
	}

	int temp_max = 25 + (5*selMax);
	int team_param = TEAM_PARAMETER;
	int max_tonnage = TEAM_MAX_TONNAGE;
	void* params[4];
	params[0] = (void*)&team_param;
	params[2] = (void*)&max_tonnage;
	params[3] = (void*)&temp_max;

	int sel = SendMessage(GetDlgItem(hWndDlg, IDC_COMBO_TEAMSEL), CB_GETCURSEL, 0, 0);
	if (sel == SendMessage(GetDlgItem(hWndDlg, IDC_COMBO_TEAMSEL), CB_GETCOUNT, 0, 0)-1)
	{
				
		for (int loza = 0; loza <8; loza++)
		{
			params[1] = (void*)&loza;
			MW4Shell::SetNetworkMissionParamater(NULL, 4, params);
		}
		
		max_ton_allteams = selMax;
	}
	
	else
	{
		params[1] = (void*)&sel;
		MW4Shell::SetNetworkMissionParamater(NULL, 4, params);
	}

	for (int a=0; a<16; a++)
		host_bot_last_val[a] = -1;
}

void CDedicatedServerUI::OnTeamMaxTotalTonChange(HWND hWndDlg)
{
	int temp_max;
	temp_max = SendMessage(GetDlgItem(hWndDlg, IDC_COMBO_TEAMTOTMAXTON), CB_GETCURSEL, 0,0);
	temp_max = 100*(temp_max+1);
	Clamp(temp_max, 100, 1600);

	int team_param = TEAM_PARAMETER;
	int max_tonnage = TEAM_TOTAL_MAX_TONNAGE;
	void* params[4];
	params[0] = (void*)&team_param;
	params[2] = (void*)&max_tonnage;
	params[3] = (void*)&temp_max;

	int sel = SendMessage(GetDlgItem(hWndDlg, IDC_COMBO_TEAMSEL), CB_GETCURSEL, 0, 0);
	if (sel == SendMessage(GetDlgItem(hWndDlg, IDC_COMBO_TEAMSEL), CB_GETCOUNT, 0, 0)-1)
	{
				
		for (int loza = 0; loza <8; loza++)
		{
			params[1] = (void*)&loza;
			MW4Shell::SetNetworkMissionParamater(NULL, 4, params);
		}
		
		total_max_ton_allteams = temp_max;
	}
	
	else
	{
		params[1] = (void*)&sel;
		MW4Shell::SetNetworkMissionParamater(NULL, 4, params);
	}

	for (int a=0; a<16; a++)
		host_bot_last_val[a] = -1;
}

void CDedicatedServerUI::OnLockServer(HWND hWnd)
{
	int lock = CLOSED_GAME_PARAMETER;
	int lock_val = (SendMessage(GetDlgItem(hWnd, IDC_CHECK_LOCKSERVER), BM_GETCHECK,0,0)==BST_CHECKED)?1:0;
	
	int* intParams[2];
	intParams[0] = &lock;
	intParams[1] = &lock_val;

	MW4Shell::SetNetworkMissionParamater(NULL, 2, (void**)intParams);
}

void CDedicatedServerUI::OnBotError()
{
	int bot_last_error_count = MW4Shell::GetBotVehicleRejectionReasonCount(NULL, 0, NULL) - 1;
	int bot_denied_parameter = 0;
	int* intParams[2];
	intParams[0] = &bot_last_error_count;
	intParams[1] = &bot_denied_parameter;

	int bot_last_error = MW4Shell::GetBotVehicleRejectionReason(NULL, 2, (void**)intParams);

	if (bot_last_error != NO_TEAM_BUILDERROR)
	{
		MechMessageBoxWin32(gos_GetResourceString(g_langResources, IDS_GN_ERROR), denied_error[bot_last_error]);
	}
}

int CDedicatedServerUI::AddBot(char* name, int level, char* clan, int mechId, int teamId)
{
	void* params[3];
	params[0] = (void*)&level;
	params[1] = (void*)&name;
	params[2] = (void*)&clan;
	int bot_id = MW4Shell::AddBot(NULL, 3, params);				
	
	if (bot_id == -1)
	{
		MechMessageBoxWin32(
			gos_GetResourceString(g_langResources, IDS_GN_ERROR), 
			gos_GetResourceString(g_langResources, IDS_MP_LOBBY_ERROR_GAME_FULL));
	}
				
	int return_value = SetMechBot(bot_id, mechId);
	if (return_value != 0)
	{
		host_bot_last_val[bot_id] = mechId;
		host_bot_last_skin[bot_id] = 4;
		host_bot_last_player_num[bot_id] = valid_count+1;
	}

	params[0] = &bot_id;
	if (teamId>=0 && teamId<9) {
		if (team_legal_to_join[teamId]) {
			params[1] = (void**)&teamId;
			MW4Shell::SetTeamBot(NULL, 2, (void**)params);
		}
		else {
			char temp[STRING_SIZE];
			LoadString(g_hInst, IDS_STRING_TEAMFULL, temp, STRING_SIZE);
			MechMessageBoxWin32(gos_GetResourceString(g_langResources, IDS_GN_ERROR), temp);
		}
	}	

	int accepted = MW4Shell::GetBotVehicleAccepted(NULL, 1, params);
	if (!accepted)
	{
		OnBotError();
	}
			
	return bot_id;
}

void CDedicatedServerUI::OnMechSelChange(HWND hWnd)
{
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_MECHS2), CB_RESETCONTENT, 0,0);
	int sel = SendMessage(GetDlgItem(hWnd, IDC_COMBO_MECHS), CB_GETCURSEL, 0,0);
	if (sel == 0) {
		SendMessage(GetDlgItem(hWnd, IDC_COMBO_MECHS), CB_SETCURSEL, 1,0);
		sel = SendMessage(GetDlgItem(hWnd, IDC_COMBO_MECHS), CB_GETCURSEL, 0,0);
	}
	int mech_index = stock_array[sel];
	int index = 0;
	for (int i=0; i<MW4Shell::Instance->m_mechCount; i++)
	{
		if (strncmp(mechs[mech_index], mechs[i], strlen(mechs[mech_index])) == 0)
		{
			if (bStock[i])
				SendMessage(GetDlgItem(hWnd, IDC_COMBO_MECHS2), CB_INSERTSTRING, 0,(LPARAM)gos_GetResourceString(g_langResources, IDS_MP_LOBBY_STOCK));
			else
				SendMessage(GetDlgItem(hWnd, IDC_COMBO_MECHS2), CB_INSERTSTRING, index,(LPARAM)(mechs[i]+strlen(mechs[mech_index])+1));
			variant_array[index] = i - mech_index;
			index++;
		}
	}
	SendMessage(GetDlgItem(hWnd, IDC_COMBO_MECHS2), CB_SETCURSEL, 0,0);
}

void CDedicatedServerUI::GetMechBaseName(char *full_name, char* base_name)
{
	MWTable::TableIterator *mech_iterator;
	Check_Object(MWApplication::GetInstance()->m_mechTable);

	mech_iterator = MWApplication::GetInstance()->m_mechTable->MakeTableIterator();
	MWTableEntry *mech_entry;
	while((mech_entry = mech_iterator->ReadAndNext()) != NULL)
	{
		Check_Object(mech_entry);
		
		if (strnicmp(full_name, (const char *)mech_entry->GetEntryName(), 
					 mech_entry->GetEntryName().GetLength()) == 0 &&
			mech_entry->GetEntryName().GetLength()>strlen(base_name)) {
			Str_Copy(
				base_name, 
				(const char *)mech_entry->GetEntryName(), 
				mech_entry->GetEntryName().GetLength() + 1);
			
		}
	}
	delete mech_iterator;
}

void CDedicatedServerUI::SetNextGame(HWND hWnd)
{
	MW4Shell::Instance->InitNetworkScenarios(NULL, 0, NULL);

	int* intParam[2];
	int temp;
	int value;
	intParam[0] = &temp;
	intParam[1] = &value;
	temp = USE_MAP_CYCLE_PARAMETER;
	if (MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam))
	{
		temp = RULE_TYPE_PARAMETER;
		int temp_game = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
		SendMessage(GetDlgItem(hWnd, IDC_COMBO_GAMETYPE), CB_RESETCONTENT, 0, 0);
		SendMessage(GetDlgItem(hWnd, IDC_COMBO_GAMETYPE), CB_ADDSTRING, 0,(LPARAM)gos_GetResourceString(g_langResources,game_types[standard_to_custom[temp_game]]));
		SendMessage(GetDlgItem(hWnd, IDC_COMBO_GAMETYPE), CB_SETCURSEL, 0, 0);
		temp = temp_game;
		MW4Shell::GetGameTypeString(NULL, 1, (void**)intParam);

		last_selected_game = -1;
		last_selected_map = 0;
	}
	else
	{
		temp = RULE_TYPE_PARAMETER;
		int temp_game = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
		SendMessage(GetDlgItem(hWnd, IDC_COMBO_GAMETYPE), CB_SETCURSEL, standard_to_selection[temp_game], 0);
		temp = temp_game;
		MW4Shell::GetGameTypeString(NULL, 1, (void**)intParam);

		temp = MAP_ID_PARAMETER;
		int temp_map = MW4Shell::GetLocalNetworkMissionParamater(NULL, 1, (void**)intParam);
		SendMessage(GetDlgItem(hWnd, IDC_COMBO_MAP), CB_SETCURSEL, temp_map, 0);

		last_selected_game = -1;
		last_selected_map = temp_map;
	}
	OnGameTypeChange(hWnd);
}

void CDedicatedServerUI::ShowKickBanMenu(HWND hwndDlg, bool kick)
{
	if (g_hKickMechs) {
		DestroyMenu(g_hKickMechs);
		g_hKickMechs = NULL;
	}
	
	g_hKickMechs = ::CreatePopupMenu();
	
	MENUITEMINFO itemInfo;
	::memset(&itemInfo, 0, sizeof(MENUITEMINFO));
	itemInfo.cbSize = sizeof(MENUITEMINFO);
	itemInfo.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
	itemInfo.fType = MFT_STRING;
	itemInfo.fState = MFS_ENABLED;
	
	for (int i=0; i<valid_count; i++) {
		if (kick) {
			itemInfo.wID = ID_KICKMECH_START+i;
			itemInfo.dwTypeData = print_names[i];
			InsertMenuItem(g_hKickMechs,i,TRUE,&itemInfo);
			if (i_am_a_bot[i])
				EnableMenuItem(g_hKickMechs,i,(game_state==0)?MF_BYPOSITION|MF_ENABLED:MF_BYPOSITION|MF_GRAYED);
		}
		else {
			if (!i_am_a_bot[i]) {							
				itemInfo.wID = ID_KICKMECH_START+i;
				itemInfo.dwTypeData = print_names[i];
				InsertMenuItem(g_hKickMechs,i,TRUE,&itemInfo);
			}
		}					
	}
	
	DWORD pos = ::GetMessagePos();
	int xPos = GET_X_LPARAM(pos);
	int yPos = GET_Y_LPARAM(pos);
	::TrackPopupMenu(g_hKickMechs, 0, xPos, yPos, 0, hwndDlg, NULL);
}