Subversion Repositories Games.Chess Giants

Rev

Rev 172 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 172 Rev 179
Line 13... Line 13...
13
#define GAMETYPE_TWOPLAYERS 3
13
#define GAMETYPE_TWOPLAYERS 3
14
 
14
 
15
 
15
 
16
// global variables used in this module only
16
// global variables used in this module only
17
static int newgame_type = 0;
17
static int newgame_type = 0;
18
static char ascii_hostname[256];
18
static bool is_internet_available = false;
-
 
19
static bool is_chessserver_reachable = false;
19
 
20
 
20
 
21
 
21
// prototypes of local functions
22
// prototypes of local functions
22
static void StartThread_ThisDialog (void *thread_parms);
23
static void StartThread_ThisDialog (void *thread_parms);
23
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
24
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
Line 70... Line 71...
70
   SetWindowText (hMainWnd, PROGRAM_NAME); // update window title
71
   SetWindowText (hMainWnd, PROGRAM_NAME); // update window title
71
   the_board.reevaluate = true; // evaluate the new board
72
   the_board.reevaluate = true; // evaluate the new board
72
   the_scene.update = true; // update scene
73
   the_scene.update = true; // update scene
73
 
74
 
74
   return; // finished, a new game of the chosen type is being fired up
75
   return; // finished, a new game of the chosen type is being fired up
-
 
76
}
-
 
77
 
-
 
78
 
-
 
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
75
}
104
}
76
 
105
 
77
 
106
 
78
static void StartThread_ThisDialog (void *thread_parms)
107
static void StartThread_ThisDialog (void *thread_parms)
79
{
108
{
80
   // this function runs in a separate thread, for that's the only way (seemingly)
109
   // this function runs in a separate thread, for that's the only way (seemingly)
81
   // to implement a non-modal message box using the Common Controls library.
110
   // to implement a non-modal message box using the Common Controls library.
-
 
111
 
-
 
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
82
 
117
 
83
   // display the dialog box
118
   // display the dialog box
84
   newgame_type = DialogBox (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hMainWnd, DialogProc_ThisDialog);
119
   newgame_type = DialogBox (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hMainWnd, DialogProc_ThisDialog);
85
   if (newgame_type > 0)
120
   if (newgame_type > 0)
86
      is_dialogbox_newgame_validated = true;
121
      is_dialogbox_newgame_validated = true;
Line 92... Line 127...
92
 
127
 
93
 
128
 
94
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
129
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
95
{
130
{
96
   // message handler for the dialog box
131
   // message handler for the dialog box
97
 
132
 
98
   unsigned long connection_state;
-
 
99
   unsigned short wParam_hiword;
133
   unsigned short wParam_hiword;
100
   unsigned short wParam_loword;
134
   unsigned short wParam_loword;
101
 
135
 
102
   // filter out the commonly used message values
136
   // filter out the commonly used message values
103
   wParam_hiword = HIWORD (wParam);
137
   wParam_hiword = HIWORD (wParam);
104
   wParam_loword = LOWORD (wParam);
138
   wParam_loword = LOWORD (wParam);
-
 
139
 
-
 
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
   }
105
 
148
 
106
   // have we just fired up this window ?
149
   // have we just fired up this window ?
107
   if (message == WM_INITDIALOG)
150
   if (message == WM_INITDIALOG)
108
   {
151
   {
109
      // center the window
152
      // center the window
Line 117... Line 160...
117
      SetWindowText (hWnd, LOCALIZE (L"NewGame_Title"));
160
      SetWindowText (hWnd, LOCALIZE (L"NewGame_Title"));
118
      Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_QUESTION), LOCALIZE (L"NewGame_Question"));
161
      Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_QUESTION), LOCALIZE (L"NewGame_Question"));
119
 
162
 
120
      // if computer is connected to a network, enable online mode, else don't.
163
      // if computer is connected to a network, enable online mode, else don't.
121
      SetWindowText (GetDlgItem (hWnd, BUTTON_NEWGAME_ONLINE), LOCALIZE (L"NewGame_Online"));
164
      SetWindowText (GetDlgItem (hWnd, BUTTON_NEWGAME_ONLINE), LOCALIZE (L"NewGame_Online"));
122
      if (InternetGetConnectedState (&connection_state, 0)
-
 
123
          && (ConvertTo7BitASCII (ascii_hostname, sizeof (ascii_hostname), options.network.server_address), gethostbyname (ascii_hostname) != NULL))
-
 
124
      {
-
 
125
         Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), LOCALIZE (L"NewGame_OnlineDescription"));
-
 
126
         EnableWindow (GetDlgItem (hWnd, BUTTON_NEWGAME_ONLINE), true);
-
 
127
         EnableWindow (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), true);
-
 
128
      }
-
 
129
      else
-
 
130
      {
-
 
131
         Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), LOCALIZE (L"Error_NetworkInitializationFailed"));
165
      Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), LOCALIZE (L"Error_NetworkInitializationFailed"));
132
         EnableWindow (GetDlgItem (hWnd, BUTTON_NEWGAME_ONLINE), false);
166
      EnableWindow (GetDlgItem (hWnd, BUTTON_NEWGAME_ONLINE), false);
133
         EnableWindow (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), false);
167
      EnableWindow (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), false);
134
      }
-
 
135
 
168
 
136
      SetWindowText (GetDlgItem (hWnd, BUTTON_NEWGAME_COMPUTER), LOCALIZE (L"NewGame_Computer"));
169
      SetWindowText (GetDlgItem (hWnd, BUTTON_NEWGAME_COMPUTER), LOCALIZE (L"NewGame_Computer"));
137
      Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_COMPUTERDESCRIPTION), LOCALIZE (L"NewGame_ComputerDescription"));
170
      Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_COMPUTERDESCRIPTION), LOCALIZE (L"NewGame_ComputerDescription"));
138
 
171
 
139
      SetWindowText (GetDlgItem (hWnd, BUTTON_NEWGAME_HUMAN), LOCALIZE (L"NewGame_Human"));
172
      SetWindowText (GetDlgItem (hWnd, BUTTON_NEWGAME_HUMAN), LOCALIZE (L"NewGame_Human"));