Rev 172 | Go to most recent revision | 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; |
||
| 179 | pmbaty | 18 | static bool is_internet_available = false; |
| 19 | static bool is_chessserver_reachable = false; |
||
| 1 | pmbaty | 20 | |
| 21 | |||
| 22 | // prototypes of local functions |
||
| 23 | static void StartThread_ThisDialog (void *thread_parms); |
||
| 24 | static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam); |
||
| 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; |
| 1 | pmbaty | 35 | _beginthread (StartThread_ThisDialog, 0, NULL); // so fire up a new one |
| 36 | |||
| 37 | the_scene.gui.want_spinwheel = true; // start spinning wheel |
||
| 38 | |||
| 39 | return; // return as soon as the thread is fired up |
||
| 40 | } |
||
| 41 | |||
| 42 | |||
| 43 | void DialogBox_NewGame_Validated (void) |
||
| 44 | { |
||
| 45 | // callback function called by the main game thread when the dialog box is validated |
||
| 46 | |||
| 47 | // remember this callback is no longer to be called |
||
| 48 | is_dialogbox_newgame_validated = false; |
||
| 49 | |||
| 50 | Scene_Shutdown (&the_scene); // release scene |
||
| 51 | Board_Shutdown (&the_board); // release chess game |
||
| 52 | |||
| 53 | if (newgame_type == GAMETYPE_ONLINE) |
||
| 54 | { |
||
| 172 | pmbaty | 55 | Board_Init (&the_board, PLAYER_HUMAN, PLAYER_INTERNET, L"standard", FENSTARTUP_STANDARDCHESS); // we want an online opponent |
| 1 | pmbaty | 56 | the_board.game_state = STATE_UNKNOWN; // game does NOT start immediately |
| 57 | } |
||
| 58 | else if (newgame_type == GAMETYPE_COMPUTER) |
||
| 59 | { |
||
| 172 | pmbaty | 60 | Board_Init (&the_board, PLAYER_HUMAN, PLAYER_COMPUTER, L"standard", FENSTARTUP_STANDARDCHESS); // we want a computer opponent |
| 1 | pmbaty | 61 | the_board.game_state = STATE_PLAYING; // game starts immediately |
| 62 | } |
||
| 63 | else |
||
| 64 | { |
||
| 172 | pmbaty | 65 | Board_Init (&the_board, PLAYER_HUMAN, PLAYER_HUMAN, L"standard", FENSTARTUP_STANDARDCHESS); // we want a human opponent |
| 1 | pmbaty | 66 | the_board.game_state = STATE_PLAYING; // game starts immediately |
| 75 | pmbaty | 67 | DialogBox_RenameSides (); // pop up the opponents naming dialog box |
| 1 | pmbaty | 68 | } |
| 69 | Scene_Init (&the_scene, &the_board); // initialize scene |
||
| 70 | |||
| 71 | SetWindowText (hMainWnd, PROGRAM_NAME); // update window title |
||
| 72 | the_board.reevaluate = true; // evaluate the new board |
||
| 73 | the_scene.update = true; // update scene |
||
| 74 | |||
| 75 | return; // finished, a new game of the chosen type is being fired up |
||
| 76 | } |
||
| 77 | |||
| 78 | |||
| 179 | pmbaty | 79 | static void StartThread_TestConnectionInBackground (void *thread_parms) |
| 80 | { |
||
| 81 | // this function runs in a separate thread and tests the connectivity to the chess server in the background |
||
| 82 | |||
| 83 | char icmp_send_buffer[32] = "Chess Giants connectivity test"; |
||
| 84 | void *icmp_recv_buffer[sizeof (ICMP_ECHO_REPLY) + sizeof (icmp_send_buffer)]; |
||
| 85 | char ascii_hostname[256]; |
||
| 86 | struct hostent *hostinfo; |
||
| 87 | uint32_t server_ipaddr; |
||
| 88 | HANDLE icmp_descriptor; |
||
| 89 | |||
| 90 | // since this is a synchronous operation, it's a good idea to do this on this separate thread |
||
| 91 | ConvertTo7BitASCII (ascii_hostname, sizeof (ascii_hostname), options.network.server_address); |
||
| 92 | if ((hostinfo = gethostbyname (ascii_hostname)) != NULL) |
||
| 93 | { |
||
| 94 | // DO NOT CONNECT TO THE SERVER AND DISCONNECT IMMEDIATELY ELSE IT WILL RESULT IN AN IP BAN. Use an ICMP ping instead. |
||
| 95 | server_ipaddr = inet_addr (inet_ntoa (*(struct in_addr *) hostinfo->h_addr_list[0])); |
||
| 96 | icmp_descriptor = IcmpCreateFile (); |
||
| 97 | if ((icmp_descriptor != INVALID_HANDLE_VALUE) |
||
| 98 | && (IcmpSendEcho (icmp_descriptor, server_ipaddr, icmp_send_buffer, sizeof (icmp_send_buffer), NULL, icmp_recv_buffer, sizeof (icmp_recv_buffer), 5000) != 0) |
||
| 99 | && (((ICMP_ECHO_REPLY *) icmp_recv_buffer)->Status == 0)) |
||
| 100 | is_chessserver_reachable = true; // on success, remember we could reach the chess server |
||
| 101 | } |
||
| 102 | |||
| 103 | return; // _endthread() implied |
||
| 104 | } |
||
| 105 | |||
| 106 | |||
| 1 | pmbaty | 107 | static void StartThread_ThisDialog (void *thread_parms) |
| 108 | { |
||
| 109 | // this function runs in a separate thread, for that's the only way (seemingly) |
||
| 110 | // to implement a non-modal message box using the Common Controls library. |
||
| 111 | |||
| 179 | pmbaty | 112 | is_internet_available = false; // assume Internet doesn't work until told otherwise |
| 113 | is_chessserver_reachable = false; |
||
| 114 | |||
| 115 | // start the thread that will tell whether Internet works |
||
| 116 | _beginthread (StartThread_TestConnectionInBackground, 0, NULL); // so fire up a new one |
||
| 117 | |||
| 1 | pmbaty | 118 | // display the dialog box |
| 119 | newgame_type = DialogBox (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hMainWnd, DialogProc_ThisDialog); |
||
| 120 | if (newgame_type > 0) |
||
| 121 | is_dialogbox_newgame_validated = true; |
||
| 140 | pmbaty | 122 | is_dialogbox_displayed = false; |
| 1 | pmbaty | 123 | |
| 124 | pmbaty | 124 | the_board.reevaluate = true; // refresh the GUI buttons if needed |
| 1 | pmbaty | 125 | return; // _endthread() implied |
| 126 | } |
||
| 127 | |||
| 128 | |||
| 129 | static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam) |
||
| 130 | { |
||
| 131 | // message handler for the dialog box |
||
| 132 | |||
| 133 | unsigned short wParam_hiword; |
||
| 134 | unsigned short wParam_loword; |
||
| 135 | |||
| 136 | // filter out the commonly used message values |
||
| 137 | wParam_hiword = HIWORD (wParam); |
||
| 138 | wParam_loword = LOWORD (wParam); |
||
| 139 | |||
| 179 | pmbaty | 140 | // any message will do (mouse move, etc.) -- is the chess server reachable ? |
| 141 | if (!is_internet_available && is_chessserver_reachable) |
||
| 142 | { |
||
| 143 | is_internet_available = true; // remember we no longer need to look after that (FLAG THIS BEFORE ANY SendMessage() HAPPENS!) |
||
| 144 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), LOCALIZE (L"NewGame_OnlineDescription")); |
||
| 145 | EnableWindow (GetDlgItem (hWnd, BUTTON_NEWGAME_ONLINE), true); |
||
| 146 | EnableWindow (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), true); |
||
| 147 | } |
||
| 148 | |||
| 1 | pmbaty | 149 | // have we just fired up this window ? |
| 150 | if (message == WM_INITDIALOG) |
||
| 151 | { |
||
| 152 | // center the window |
||
| 153 | CenterWindow (hWnd, hMainWnd); |
||
| 154 | |||
| 155 | // set dialog icons (small one for title bar & big one for task manager) |
||
| 156 | SendMessage (hWnd, WM_SETICON, ICON_SMALL, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN))); |
||
| 157 | SendMessage (hWnd, WM_SETICON, ICON_BIG, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN))); |
||
| 158 | |||
| 159 | // set window title and control texts |
||
| 160 | SetWindowText (hWnd, LOCALIZE (L"NewGame_Title")); |
||
| 161 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_QUESTION), LOCALIZE (L"NewGame_Question")); |
||
| 162 | |||
| 163 | // if computer is connected to a network, enable online mode, else don't. |
||
| 164 | SetWindowText (GetDlgItem (hWnd, BUTTON_NEWGAME_ONLINE), LOCALIZE (L"NewGame_Online")); |
||
| 179 | pmbaty | 165 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), LOCALIZE (L"Error_NetworkInitializationFailed")); |
| 166 | EnableWindow (GetDlgItem (hWnd, BUTTON_NEWGAME_ONLINE), false); |
||
| 167 | EnableWindow (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), false); |
||
| 1 | pmbaty | 168 | |
| 169 | SetWindowText (GetDlgItem (hWnd, BUTTON_NEWGAME_COMPUTER), LOCALIZE (L"NewGame_Computer")); |
||
| 170 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_COMPUTERDESCRIPTION), LOCALIZE (L"NewGame_ComputerDescription")); |
||
| 171 | |||
| 172 | SetWindowText (GetDlgItem (hWnd, BUTTON_NEWGAME_HUMAN), LOCALIZE (L"NewGame_Human")); |
||
| 173 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_HUMANDESCRIPTION), LOCALIZE (L"NewGame_HumanDescription")); |
||
| 174 | |||
| 175 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_STATUSBAR), LOCALIZE (L"NewGame_StatusBar")); |
||
| 176 | |||
| 177 | // convert the status bar message to a hyperlink |
||
| 178 | ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_NEWGAME_STATUSBAR)); |
||
| 179 | |||
| 180 | // and stop the center message display |
||
| 181 | the_scene.gui.want_spinwheel = false; |
||
| 182 | } |
||
| 183 | |||
| 184 | // else did we click the close button on the title bar or hit the escape key ? |
||
| 185 | else if (message == WM_CLOSE) |
||
| 186 | EndDialog (hWnd, 0); // close the dialog box |
||
| 187 | |||
| 188 | // else did we take action on one of the controls ? |
||
| 189 | else if (message == WM_COMMAND) |
||
| 190 | { |
||
| 191 | // did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key) |
||
| 192 | if (wParam_loword == IDCANCEL) |
||
| 193 | EndDialog (hWnd, 0); // close the dialog box |
||
| 194 | |||
| 195 | // else was it the new online game button ? |
||
| 196 | else if (wParam_loword == BUTTON_NEWGAME_ONLINE) |
||
| 197 | EndDialog (hWnd, GAMETYPE_ONLINE); // close the dialog box and return a "new online game" code |
||
| 198 | |||
| 199 | // else was it the new game versus computer button ? |
||
| 200 | else if (wParam_loword == BUTTON_NEWGAME_COMPUTER) |
||
| 201 | EndDialog (hWnd, GAMETYPE_COMPUTER); // close the dialog box and return a "new game versus computer" code |
||
| 202 | |||
| 203 | // else was it the new game versus human button ? |
||
| 204 | else if (wParam_loword == BUTTON_NEWGAME_HUMAN) |
||
| 205 | EndDialog (hWnd, GAMETYPE_TWOPLAYERS); // close the dialog box and return a "new game versus human" code |
||
| 206 | |||
| 207 | // else was it the status bar hyperlink ? |
||
| 208 | else if (wParam_loword == STATICTEXT_NEWGAME_STATUSBAR) |
||
| 209 | ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized |
||
| 210 | } |
||
| 211 | |||
| 212 | // call the default dialog message processing function to keep things going |
||
| 213 | return (false); |
||
| 214 | } |