Rev 179 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | pmbaty | 1 | // dialog_newgame.cpp |
| 2 | |||
| 3 | #include "../common.h" |
||
| 4 | |||
| 5 | |||
| 6 | // dialog template |
||
| 7 | #define THIS_DIALOG DIALOG_NEWGAME |
||
| 8 | |||
| 9 | |||
| 10 | // game type definitons |
||
| 11 | #define GAMETYPE_ONLINE 1 |
||
| 12 | #define GAMETYPE_COMPUTER 2 |
||
| 13 | #define GAMETYPE_TWOPLAYERS 3 |
||
| 14 | |||
| 15 | |||
| 16 | // global variables used in this module only |
||
| 17 | static int newgame_type = 0; |
||
| 180 | pmbaty | 18 | static HWND hThisDialogWnd = NULL; |
| 1 | pmbaty | 19 | |
| 20 | |||
| 21 | // prototypes of local functions |
||
| 22 | static void StartThread_ThisDialog (void *thread_parms); |
||
| 23 | static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam); |
||
| 180 | pmbaty | 24 | static void StartThread_TestConnectionInBackground (void *thread_parms); |
| 1 | pmbaty | 25 | |
| 26 | |||
| 27 | void DialogBox_NewGame (void) |
||
| 28 | { |
||
| 29 | // helper function to fire up the modeless dialog box |
||
| 30 | |||
| 21 | pmbaty | 31 | // HACK to prevent the player to click and block the view angles while the slide-in is not finished |
| 32 | command_ignoretime = current_time + 2.0f; // allow 2 seconds |
||
| 33 | |||
| 140 | pmbaty | 34 | is_dialogbox_displayed = true; |
| 180 | pmbaty | 35 | hThisDialogWnd = NULL; |
| 1 | pmbaty | 36 | _beginthread (StartThread_ThisDialog, 0, NULL); // so fire up a new one |
| 37 | |||
| 180 | pmbaty | 38 | // start the thread that will tell whether Internet works |
| 39 | _beginthread (StartThread_TestConnectionInBackground, 0, NULL); |
||
| 40 | |||
| 1 | pmbaty | 41 | the_scene.gui.want_spinwheel = true; // start spinning wheel |
| 42 | |||
| 43 | return; // return as soon as the thread is fired up |
||
| 44 | } |
||
| 45 | |||
| 46 | |||
| 47 | void DialogBox_NewGame_Validated (void) |
||
| 48 | { |
||
| 49 | // callback function called by the main game thread when the dialog box is validated |
||
| 50 | |||
| 51 | // remember this callback is no longer to be called |
||
| 52 | is_dialogbox_newgame_validated = false; |
||
| 53 | |||
| 54 | Scene_Shutdown (&the_scene); // release scene |
||
| 55 | Board_Shutdown (&the_board); // release chess game |
||
| 56 | |||
| 57 | if (newgame_type == GAMETYPE_ONLINE) |
||
| 58 | { |
||
| 172 | pmbaty | 59 | Board_Init (&the_board, PLAYER_HUMAN, PLAYER_INTERNET, L"standard", FENSTARTUP_STANDARDCHESS); // we want an online opponent |
| 1 | pmbaty | 60 | the_board.game_state = STATE_UNKNOWN; // game does NOT start immediately |
| 61 | } |
||
| 62 | else if (newgame_type == GAMETYPE_COMPUTER) |
||
| 63 | { |
||
| 172 | pmbaty | 64 | Board_Init (&the_board, PLAYER_HUMAN, PLAYER_COMPUTER, L"standard", FENSTARTUP_STANDARDCHESS); // we want a computer opponent |
| 1 | pmbaty | 65 | the_board.game_state = STATE_PLAYING; // game starts immediately |
| 66 | } |
||
| 67 | else |
||
| 68 | { |
||
| 172 | pmbaty | 69 | Board_Init (&the_board, PLAYER_HUMAN, PLAYER_HUMAN, L"standard", FENSTARTUP_STANDARDCHESS); // we want a human opponent |
| 1 | pmbaty | 70 | the_board.game_state = STATE_PLAYING; // game starts immediately |
| 75 | pmbaty | 71 | DialogBox_RenameSides (); // pop up the opponents naming dialog box |
| 1 | pmbaty | 72 | } |
| 73 | Scene_Init (&the_scene, &the_board); // initialize scene |
||
| 74 | |||
| 75 | SetWindowText (hMainWnd, PROGRAM_NAME); // update window title |
||
| 76 | the_board.reevaluate = true; // evaluate the new board |
||
| 77 | the_scene.update = true; // update scene |
||
| 78 | |||
| 79 | return; // finished, a new game of the chosen type is being fired up |
||
| 80 | } |
||
| 81 | |||
| 82 | |||
| 83 | static void StartThread_ThisDialog (void *thread_parms) |
||
| 84 | { |
||
| 85 | // this function runs in a separate thread, for that's the only way (seemingly) |
||
| 86 | // to implement a non-modal message box using the Common Controls library. |
||
| 87 | |||
| 88 | // display the dialog box |
||
| 89 | newgame_type = DialogBox (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hMainWnd, DialogProc_ThisDialog); |
||
| 90 | if (newgame_type > 0) |
||
| 91 | is_dialogbox_newgame_validated = true; |
||
| 140 | pmbaty | 92 | is_dialogbox_displayed = false; |
| 1 | pmbaty | 93 | |
| 124 | pmbaty | 94 | the_board.reevaluate = true; // refresh the GUI buttons if needed |
| 1 | pmbaty | 95 | return; // _endthread() implied |
| 96 | } |
||
| 97 | |||
| 98 | |||
| 99 | static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam) |
||
| 100 | { |
||
| 101 | // message handler for the dialog box |
||
| 102 | |||
| 103 | unsigned short wParam_hiword; |
||
| 104 | unsigned short wParam_loword; |
||
| 105 | |||
| 106 | // filter out the commonly used message values |
||
| 107 | wParam_hiword = HIWORD (wParam); |
||
| 108 | wParam_loword = LOWORD (wParam); |
||
| 109 | |||
| 110 | // have we just fired up this window ? |
||
| 111 | if (message == WM_INITDIALOG) |
||
| 112 | { |
||
| 180 | pmbaty | 113 | hThisDialogWnd = hWnd; // save the dialog handle |
| 114 | |||
| 1 | pmbaty | 115 | // center the window |
| 116 | CenterWindow (hWnd, hMainWnd); |
||
| 117 | |||
| 118 | // set dialog icons (small one for title bar & big one for task manager) |
||
| 119 | SendMessage (hWnd, WM_SETICON, ICON_SMALL, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN))); |
||
| 120 | SendMessage (hWnd, WM_SETICON, ICON_BIG, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN))); |
||
| 121 | |||
| 122 | // set window title and control texts |
||
| 123 | SetWindowText (hWnd, LOCALIZE (L"NewGame_Title")); |
||
| 124 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_QUESTION), LOCALIZE (L"NewGame_Question")); |
||
| 125 | |||
| 126 | // if computer is connected to a network, enable online mode, else don't. |
||
| 127 | SetWindowText (GetDlgItem (hWnd, BUTTON_NEWGAME_ONLINE), LOCALIZE (L"NewGame_Online")); |
||
| 179 | pmbaty | 128 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), LOCALIZE (L"Error_NetworkInitializationFailed")); |
| 129 | EnableWindow (GetDlgItem (hWnd, BUTTON_NEWGAME_ONLINE), false); |
||
| 130 | EnableWindow (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), false); |
||
| 1 | pmbaty | 131 | |
| 132 | SetWindowText (GetDlgItem (hWnd, BUTTON_NEWGAME_COMPUTER), LOCALIZE (L"NewGame_Computer")); |
||
| 133 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_COMPUTERDESCRIPTION), LOCALIZE (L"NewGame_ComputerDescription")); |
||
| 134 | |||
| 135 | SetWindowText (GetDlgItem (hWnd, BUTTON_NEWGAME_HUMAN), LOCALIZE (L"NewGame_Human")); |
||
| 136 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_HUMANDESCRIPTION), LOCALIZE (L"NewGame_HumanDescription")); |
||
| 137 | |||
| 138 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_STATUSBAR), LOCALIZE (L"NewGame_StatusBar")); |
||
| 139 | |||
| 140 | // convert the status bar message to a hyperlink |
||
| 141 | ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_NEWGAME_STATUSBAR)); |
||
| 142 | |||
| 143 | // and stop the center message display |
||
| 144 | the_scene.gui.want_spinwheel = false; |
||
| 145 | } |
||
| 146 | |||
| 180 | pmbaty | 147 | // else have we been notified that the chess server is reachable ? |
| 148 | else if (message == WM_USER) |
||
| 149 | { |
||
| 150 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), LOCALIZE (L"NewGame_OnlineDescription")); |
||
| 151 | EnableWindow (GetDlgItem (hWnd, BUTTON_NEWGAME_ONLINE), true); |
||
| 152 | EnableWindow (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), true); |
||
| 153 | } |
||
| 154 | |||
| 1 | pmbaty | 155 | // else did we click the close button on the title bar or hit the escape key ? |
| 156 | else if (message == WM_CLOSE) |
||
| 180 | pmbaty | 157 | { |
| 158 | hThisDialogWnd = NULL; // forget about the dialog handle |
||
| 1 | pmbaty | 159 | EndDialog (hWnd, 0); // close the dialog box |
| 180 | pmbaty | 160 | } |
| 1 | pmbaty | 161 | |
| 162 | // else did we take action on one of the controls ? |
||
| 163 | else if (message == WM_COMMAND) |
||
| 164 | { |
||
| 165 | // did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key) |
||
| 166 | if (wParam_loword == IDCANCEL) |
||
| 167 | EndDialog (hWnd, 0); // close the dialog box |
||
| 168 | |||
| 169 | // else was it the new online game button ? |
||
| 170 | else if (wParam_loword == BUTTON_NEWGAME_ONLINE) |
||
| 171 | EndDialog (hWnd, GAMETYPE_ONLINE); // close the dialog box and return a "new online game" code |
||
| 172 | |||
| 173 | // else was it the new game versus computer button ? |
||
| 174 | else if (wParam_loword == BUTTON_NEWGAME_COMPUTER) |
||
| 175 | EndDialog (hWnd, GAMETYPE_COMPUTER); // close the dialog box and return a "new game versus computer" code |
||
| 176 | |||
| 177 | // else was it the new game versus human button ? |
||
| 178 | else if (wParam_loword == BUTTON_NEWGAME_HUMAN) |
||
| 179 | EndDialog (hWnd, GAMETYPE_TWOPLAYERS); // close the dialog box and return a "new game versus human" code |
||
| 180 | |||
| 181 | // else was it the status bar hyperlink ? |
||
| 182 | else if (wParam_loword == STATICTEXT_NEWGAME_STATUSBAR) |
||
| 183 | ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized |
||
| 184 | } |
||
| 185 | |||
| 186 | // call the default dialog message processing function to keep things going |
||
| 187 | return (false); |
||
| 188 | } |
||
| 180 | pmbaty | 189 | |
| 190 | |||
| 191 | static void StartThread_TestConnectionInBackground (void *thread_parms) |
||
| 192 | { |
||
| 193 | // this function runs in a separate thread and tests the connectivity to the chess server in the background |
||
| 194 | |||
| 195 | char icmp_send_buffer[32] = "Chess Giants connectivity test"; |
||
| 196 | void *icmp_recv_buffer[sizeof (ICMP_ECHO_REPLY) + sizeof (icmp_send_buffer)]; |
||
| 197 | char ascii_hostname[256]; |
||
| 198 | struct hostent *hostinfo; |
||
| 199 | uint32_t server_ipaddr; |
||
| 200 | HANDLE icmp_descriptor; |
||
| 201 | int wait_count; |
||
| 202 | int try_count; |
||
| 203 | |||
| 204 | // since this is a synchronous operation, it's a good idea to do this on this separate thread |
||
| 205 | ConvertTo7BitASCII (ascii_hostname, sizeof (ascii_hostname), options.network.server_address); |
||
| 206 | if ((hostinfo = gethostbyname (ascii_hostname)) != NULL) |
||
| 207 | { |
||
| 208 | // DO NOT CONNECT TO THE SERVER AND DISCONNECT IMMEDIATELY ELSE IT WILL RESULT IN AN IP BAN. Use an ICMP ping instead. |
||
| 209 | server_ipaddr = inet_addr (inet_ntoa (*(struct in_addr *) hostinfo->h_addr_list[0])); |
||
| 210 | icmp_descriptor = IcmpCreateFile (); |
||
| 211 | for (try_count = 0; try_count < 4; try_count++) |
||
| 212 | { |
||
| 213 | if ((icmp_descriptor != INVALID_HANDLE_VALUE) |
||
| 214 | && (IcmpSendEcho (icmp_descriptor, server_ipaddr, icmp_send_buffer, sizeof (icmp_send_buffer), NULL, icmp_recv_buffer, sizeof (icmp_recv_buffer), 5000) != 0) |
||
| 215 | && (((ICMP_ECHO_REPLY *) icmp_recv_buffer)->Status == 0)) |
||
| 216 | { |
||
| 217 | for (wait_count = 0; wait_count < 100; wait_count++) |
||
| 218 | { |
||
| 219 | if (IsWindow (hThisDialogWnd)) |
||
| 220 | { |
||
| 221 | PostMessage (hThisDialogWnd, WM_USER, 0, 0); // on success, remember we could reach the chess server |
||
| 222 | break; |
||
| 223 | } |
||
| 224 | Sleep (100); |
||
| 225 | } |
||
| 226 | break; // one reply is enough |
||
| 227 | } |
||
| 228 | Sleep (1000); // wait 1 second before retrying |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | return; // _endthread() implied |
||
| 233 | } |