// dialog_newgame.cpp
#include "../common.h"
// dialog template
#define THIS_DIALOG DIALOG_NEWGAME
// game type definitons
#define GAMETYPE_ONLINE 1
#define GAMETYPE_COMPUTER 2
#define GAMETYPE_TWOPLAYERS 3
// global variables used in this module only
static int newgame_type = 0;
static HWND hThisDialogWnd = NULL;
// prototypes of local functions
static void StartThread_ThisDialog (void *thread_parms);
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
static void StartThread_TestConnectionInBackground (void *thread_parms);
void DialogBox_NewGame (void)
{
// helper function to fire up the modeless dialog box
// HACK to prevent the player to click and block the view angles while the slide-in is not finished
command_ignoretime = current_time + 2.0f; // allow 2 seconds
is_dialogbox_displayed = true;
hThisDialogWnd = NULL;
_beginthread (StartThread_ThisDialog, 0, NULL); // so fire up a new one
// start the thread that will tell whether Internet works
_beginthread (StartThread_TestConnectionInBackground, 0, NULL);
the_scene.gui.want_spinwheel = true; // start spinning wheel
return; // return as soon as the thread is fired up
}
void DialogBox_NewGame_Validated (void)
{
// callback function called by the main game thread when the dialog box is validated
// remember this callback is no longer to be called
is_dialogbox_newgame_validated = false;
Scene_Shutdown (&the_scene); // release scene
Board_Shutdown (&the_board); // release chess game
if (newgame_type == GAMETYPE_ONLINE)
{
Board_Init (&the_board, PLAYER_HUMAN, PLAYER_INTERNET, L"standard", FENSTARTUP_STANDARDCHESS); // we want an online opponent
the_board.game_state = STATE_UNKNOWN; // game does NOT start immediately
}
else if (newgame_type == GAMETYPE_COMPUTER)
{
Board_Init (&the_board, PLAYER_HUMAN, PLAYER_COMPUTER, L"standard", FENSTARTUP_STANDARDCHESS); // we want a computer opponent
the_board.game_state = STATE_PLAYING; // game starts immediately
}
else
{
Board_Init (&the_board, PLAYER_HUMAN, PLAYER_HUMAN, L"standard", FENSTARTUP_STANDARDCHESS); // we want a human opponent
the_board.game_state = STATE_PLAYING; // game starts immediately
DialogBox_RenameSides (); // pop up the opponents naming dialog box
}
Scene_Init (&the_scene, &the_board); // initialize scene
SetWindowText (hMainWnd, PROGRAM_NAME); // update window title
the_board.reevaluate = true; // evaluate the new board
the_scene.update = true; // update scene
return; // finished, a new game of the chosen type is being fired up
}
static void StartThread_ThisDialog (void *thread_parms)
{
// this function runs in a separate thread, for that's the only way (seemingly)
// to implement a non-modal message box using the Common Controls library.
// display the dialog box
newgame_type = DialogBox (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hMainWnd, DialogProc_ThisDialog);
if (newgame_type > 0)
is_dialogbox_newgame_validated = true;
is_dialogbox_displayed = false;
the_board.reevaluate = true; // refresh the GUI buttons if needed
return; // _endthread() implied
}
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
{
// message handler for the dialog box
unsigned short wParam_hiword;
unsigned short wParam_loword;
// filter out the commonly used message values
wParam_hiword = HIWORD (wParam);
wParam_loword = LOWORD (wParam);
// have we just fired up this window ?
if (message == WM_INITDIALOG)
{
hThisDialogWnd = hWnd; // save the dialog handle
// center the window
CenterWindow (hWnd, hMainWnd);
// set dialog icons (small one for title bar & big one for task manager)
SendMessage (hWnd, WM_SETICON, ICON_SMALL, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
SendMessage (hWnd, WM_SETICON, ICON_BIG, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
// set window title and control texts
SetWindowText (hWnd, LOCALIZE (L"NewGame_Title"));
Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_QUESTION), LOCALIZE (L"NewGame_Question"));
// if computer is connected to a network, enable online mode, else don't.
SetWindowText (GetDlgItem (hWnd, BUTTON_NEWGAME_ONLINE), LOCALIZE (L"NewGame_Online"));
Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), LOCALIZE (L"Error_NetworkInitializationFailed"));
EnableWindow (GetDlgItem (hWnd, BUTTON_NEWGAME_ONLINE), false);
EnableWindow (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), false);
SetWindowText (GetDlgItem (hWnd, BUTTON_NEWGAME_COMPUTER), LOCALIZE (L"NewGame_Computer"));
Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_COMPUTERDESCRIPTION), LOCALIZE (L"NewGame_ComputerDescription"));
SetWindowText (GetDlgItem (hWnd, BUTTON_NEWGAME_HUMAN), LOCALIZE (L"NewGame_Human"));
Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_HUMANDESCRIPTION), LOCALIZE (L"NewGame_HumanDescription"));
Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_STATUSBAR), LOCALIZE (L"NewGame_StatusBar"));
// convert the status bar message to a hyperlink
ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_NEWGAME_STATUSBAR));
// and stop the center message display
the_scene.gui.want_spinwheel = false;
}
// else have we been notified that the chess server is reachable ?
else if (message == WM_USER)
{
Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), LOCALIZE (L"NewGame_OnlineDescription"));
EnableWindow (GetDlgItem (hWnd, BUTTON_NEWGAME_ONLINE), true);
EnableWindow (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), true);
}
// else did we click the close button on the title bar or hit the escape key ?
else if (message == WM_CLOSE)
{
hThisDialogWnd = NULL; // forget about the dialog handle
EndDialog (hWnd, 0); // close the dialog box
}
// else did we take action on one of the controls ?
else if (message == WM_COMMAND)
{
// did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key)
if (wParam_loword == IDCANCEL)
EndDialog (hWnd, 0); // close the dialog box
// else was it the new online game button ?
else if (wParam_loword == BUTTON_NEWGAME_ONLINE)
EndDialog (hWnd, GAMETYPE_ONLINE); // close the dialog box and return a "new online game" code
// else was it the new game versus computer button ?
else if (wParam_loword == BUTTON_NEWGAME_COMPUTER)
EndDialog (hWnd, GAMETYPE_COMPUTER); // close the dialog box and return a "new game versus computer" code
// else was it the new game versus human button ?
else if (wParam_loword == BUTTON_NEWGAME_HUMAN)
EndDialog (hWnd, GAMETYPE_TWOPLAYERS); // close the dialog box and return a "new game versus human" code
// else was it the status bar hyperlink ?
else if (wParam_loword == STATICTEXT_NEWGAME_STATUSBAR)
ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized
}
// call the default dialog message processing function to keep things going
return (false);
}
static void StartThread_TestConnectionInBackground (void *thread_parms)
{
// this function runs in a separate thread and tests the connectivity to the chess server in the background
char icmp_send_buffer[32] = "Chess Giants connectivity test";
void *icmp_recv_buffer[sizeof (ICMP_ECHO_REPLY) + sizeof (icmp_send_buffer)];
char ascii_hostname[256];
struct hostent *hostinfo;
uint32_t server_ipaddr;
HANDLE icmp_descriptor;
int wait_count;
int try_count;
// since this is a synchronous operation, it's a good idea to do this on this separate thread
ConvertTo7BitASCII (ascii_hostname, sizeof (ascii_hostname), options.network.server_address);
if ((hostinfo = gethostbyname (ascii_hostname)) != NULL)
{
// DO NOT CONNECT TO THE SERVER AND DISCONNECT IMMEDIATELY ELSE IT WILL RESULT IN AN IP BAN. Use an ICMP ping instead.
server_ipaddr = inet_addr (inet_ntoa (*(struct in_addr *) hostinfo->h_addr_list[0]));
icmp_descriptor = IcmpCreateFile ();
for (try_count = 0; try_count < 4; try_count++)
{
if ((icmp_descriptor != INVALID_HANDLE_VALUE)
&& (IcmpSendEcho (icmp_descriptor, server_ipaddr, icmp_send_buffer, sizeof (icmp_send_buffer), NULL, icmp_recv_buffer, sizeof (icmp_recv_buffer), 5000) != 0)
&& (((ICMP_ECHO_REPLY *) icmp_recv_buffer)->Status == 0))
{
for (wait_count = 0; wait_count < 100; wait_count++)
{
if (IsWindow (hThisDialogWnd))
{
PostMessage (hThisDialogWnd, WM_USER, 0, 0); // on success, remember we could reach the chess server
break;
}
Sleep (100);
}
break; // one reply is enough
}
Sleep (1000); // wait 1 second before retrying
}
}
return; // _endthread() implied
}