Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
NetLobbyDlg.cpp
Go to the documentation of this file.
1 /* Project Starshatter 4.5
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: Stars.exe
6  FILE: NetLobbyDlg.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Main Menu Dialog Active Window class
13 */
14 
15 #include "MemDebug.h"
16 #include "NetLobbyDlg.h"
17 #include "NetUnitDlg.h"
18 #include "NetClientConfig.h"
19 #include "MenuScreen.h"
20 #include "Starshatter.h"
21 #include "Ship.h"
22 #include "Player.h"
23 #include "Campaign.h"
24 
25 #include "NetAddr.h"
26 #include "NetLobbyClient.h"
27 #include "NetLobbyServer.h"
28 #include "NetUser.h"
29 #include "NetChat.h"
30 
31 #include "DataLoader.h"
32 #include "Video.h"
33 #include "Keyboard.h"
34 #include "MachineInfo.h"
35 
36 // +--------------------------------------------------------------------+
37 // DECLARE MAPPING FUNCTIONS:
38 
39 DEF_MAP_CLIENT(NetLobbyDlg, OnCampaignSelect);
40 DEF_MAP_CLIENT(NetLobbyDlg, OnMissionSelect);
41 DEF_MAP_CLIENT(NetLobbyDlg, OnApply);
42 DEF_MAP_CLIENT(NetLobbyDlg, OnCancel);
43 
44 // +--------------------------------------------------------------------+
45 
47 : FormWindow(s, 0, 0, s->Width(), s->Height()), manager(mgr),
48 net_lobby(0)
49 {
51  selected_mission = 0;
52  last_chat = 0;
53  host_mode = false;
54 
55  Init(def);
56 }
57 
59 {
60 }
61 
62 // +--------------------------------------------------------------------+
63 
64 void
66 {
69 
72 
73  txt_desc = FindControl(202);
75  lst_chat = (ListBox*) FindControl(211);
76  edt_chat = (EditBox*) FindControl(212);
77 
78  if (edt_chat)
79  edt_chat->SetText("");
80 
81  apply = (Button*) FindControl(1);
83 
84  cancel = (Button*) FindControl(2);
86 }
87 
88 // +--------------------------------------------------------------------+
89 
90 void
92 {
93  if (!IsShown()) {
94  // clear server data:
98  if (txt_desc) txt_desc->SetText("");
99  if (apply) apply->SetEnabled(false);
100 
101  if (lst_missions) {
104  }
105 
106  selected_campaign = 0;
107  selected_mission = 0;
108  last_chat = 0;
109 
111 
113 
114  if (!net_lobby) {
116  if (stars)
117  stars->StartLobby();
118 
120  }
121 
122  if (net_lobby) {
123  if (net_lobby->IsServer()) {
124  host_mode = true;
125 
126  NetUser* user = net_lobby->GetLocalUser();
127 
128  if (!user) {
129  Player* player = Player::GetCurrentPlayer();
130  if (player) {
131  user = new(__FILE__,__LINE__) NetUser(player);
132  user->SetHost(true);
133  }
134  else {
135  ::Print("NetLobbyDlg::Show() Host mode - no current player?\n");
136  }
137  }
138 
139  net_lobby->SetLocalUser(user);
140  }
141 
142  SelectMission();
143  }
144  }
145 }
146 
147 // +--------------------------------------------------------------------+
148 
149 void
151 {
152  ExecLobbyFrame();
153 
154  if (Keyboard::KeyDown(VK_RETURN)) {
155  if (edt_chat && edt_chat->GetText().length() > 0) {
157  edt_chat->SetText("");
158  }
159  }
160 
161  GetPlayers();
162  GetChat();
163 
164  if (lst_campaigns) {
165  if (lst_campaigns->NumItems() < 1)
166  GetMissions();
167  else
169  }
170 }
171 
172 // +--------------------------------------------------------------------+
173 
174 void
176 {
177  if (net_lobby && net_lobby->GetLastError() != 0) {
178  if (net_lobby->IsClient()) {
180  if (stars)
181  stars->StopLobby();
182 
183  net_lobby = 0;
185  }
186  }
187 }
188 
189 // +--------------------------------------------------------------------+
190 
191 void
193 {
194  if (!lst_players) return;
195 
196  if (net_lobby) {
198 
200  if (u) {
201  Text name = Player::RankAbrv(u->Rank());
202  name += " ";
203  name += u->Name();
204 
205  int count = lst_players->AddItem(u->IsHost() ? "*" : " ");
206  lst_players->SetItemText(count-1, 1, name);
207  host_mode = true;
208  }
209 
211  while (++iter) {
212  NetUser* u = iter.value();
213  int count = lst_players->AddItem(u->IsHost() ? "*" : " ");
214 
215  Text name = Player::RankAbrv(u->Rank());
216  name += " ";
217  name += u->Name();
218 
219  lst_players->SetItemText(count-1, 1, name);
220 
221  if (Player::GetCurrentPlayer()->Name() == u->Name())
222  host_mode = u->IsHost();
223  }
224  }
225 }
226 
227 // +--------------------------------------------------------------------+
228 
229 void
231 {
232  if (!lst_chat) return;
233 
234  if (net_lobby) {
235  int last_item = lst_chat->NumItems() - 1;
236  int count = 0;
237  bool added = false;
238 
240  while (++iter) {
241  NetChatEntry* c = iter.value();
242 
243  if (count++ > last_item) {
244  int n = lst_chat->AddItem(c->GetUser());
245  lst_chat->SetItemText(n-1, 1, c->GetMessage());
246  added = true;
247  }
248  }
249 
250  if (added)
252  }
253 }
254 
255 void
257 {
258  if (msg.length() < 1) return;
259 
260  Player* player = Player::GetCurrentPlayer();
261 
262  if (msg[0] >= '0' && msg[0] <= '9') {
263  if (player) {
264  Text macro = player->ChatMacro(msg[0] - '0');
265 
266  if (macro.length())
267  msg = macro;
268  }
269  }
270 
271  if (net_lobby)
273 }
274 
275 // +--------------------------------------------------------------------+
276 
277 void
279 {
280  if (!lst_campaigns || !lst_missions)
281  return;
282 
283  if (net_lobby) {
285 
287 
288  if (campaigns.size()) {
289  ListIter<NetCampaignInfo> c_iter = campaigns;
290  while (++c_iter) {
291  NetCampaignInfo* c = c_iter.value();
293  }
294 
296  NetCampaignInfo* c = campaigns[0];
297 
299 
300  ListIter<MissionInfo> m_iter = c->missions;
301  while (++m_iter) {
302  MissionInfo* m = m_iter.value();
304  }
305  }
306  }
307 }
308 
309 // +--------------------------------------------------------------------+
310 
311 void
313 {
314  if (!lst_campaigns || !lst_missions) return;
315 
316  if (net_lobby) {
318  int id = net_lobby->GetSelectedMissionID();
319 
322 
324 
325  for (int i = 0; i < campaigns.size(); i++) {
326  NetCampaignInfo* c = campaigns[i];
327 
328  if (c->id == selected_campaign) {
330  OnCampaignSelect(0);
331 
332  for (int j = 0; j < c->missions.size(); j++) {
333  MissionInfo* m = c->missions[j];
334 
335  if (m->id == selected_mission) {
337  OnMissionSelect(0);
338  }
339  }
340  }
341  }
342  }
343  else if (selected_campaign) {
344  selected_campaign = 0;
345  selected_mission = 0;
346  }
347 
350 
351  if (!host_mode)
353  }
354 }
355 
356 // +--------------------------------------------------------------------+
357 
358 void
360 {
361  if (!lst_campaigns || !lst_missions) return;
362 
363  bool selected = false;
364 
365  if (net_lobby) {
366  int c_index = lst_campaigns->GetSelectedIndex();
367  int m_index = lst_missions->GetSelection();
368 
370 
371  if (c_index >= 0 && c_index < campaigns.size() && m_index >= 0) {
372  NetCampaignInfo* c = campaigns[c_index];
373 
374  if (m_index < c->missions.size()) {
375  MissionInfo* m = c->missions[m_index];
376 
377  DWORD id = (c->id << NET_CAMPAIGN_SHIFT) +
378  (m->id & NET_MISSION_MASK);
379 
381  selected = true;
382  }
383  }
384 
385  if (!selected)
387  }
388 }
389 
390 // +--------------------------------------------------------------------+
391 
392 void
394 {
395  if (net_lobby) {
397 
398  if (lst_campaigns && lst_missions && campaigns.size()) {
399  int index = lst_campaigns->GetSelectedIndex();
400 
401  if (index >= 0 && index < campaigns.size()) {
402  NetCampaignInfo* c = campaigns[index];
403 
405  txt_desc->SetText("");
406 
408  while (++iter) {
409  MissionInfo* m = iter.value();
411  }
412  }
413  }
414  }
415 }
416 
417 void
419 {
420  if (net_lobby) {
422 
423  if (lst_campaigns && lst_missions && txt_desc && campaigns.size()) {
424  txt_desc->SetText("");
425 
426  if (host_mode && apply)
427  apply->SetEnabled(false);
428 
429  int c_index = lst_campaigns->GetSelectedIndex();
430  int m_index = lst_missions->GetSelection();
431 
432  if (c_index >= 0 && c_index < campaigns.size()) {
433  NetCampaignInfo* c = campaigns[c_index];
434 
435  if (m_index >= 0 && m_index < c->missions.size()) {
436  MissionInfo* m = c->missions[m_index];
438 
439  if (host_mode && apply)
440  apply->SetEnabled(true);
441  }
442  }
443  }
444  }
445 }
446 
447 // +--------------------------------------------------------------------+
448 
449 void
451 {
452  if (host_mode)
453  SelectMission();
454 
456 
457  NetUnitDlg* unit_dlg = manager->GetNetUnitDlg();
458  if (unit_dlg)
459  unit_dlg->SetHostMode(host_mode);
460 }
461 
462 void
464 {
465  if (net_lobby) {
467  net_lobby = 0;
468 
470  if (stars)
471  stars->StopLobby();
472  }
473 
475 }