Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
NetUnitDlg.cpp
Go to the documentation of this file.
1 /* Project Starshatter 5.0
2  Destroyer Studios LLC
3  Copyright (C) 1997-2007. All Rights Reserved.
4 
5  SUBSYSTEM: Stars.exe
6  FILE: NetUnitDlg.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 "NetUnitDlg.h"
17 #include "NetClientConfig.h"
18 #include "ConfirmDlg.h"
19 #include "MenuScreen.h"
20 #include "Starshatter.h"
21 #include "Campaign.h"
22 #include "Mission.h"
23 #include "Ship.h"
24 #include "Player.h"
25 #include "Campaign.h"
26 #include "ShipDesign.h"
27 
28 #include "NetAddr.h"
29 #include "NetLobbyClient.h"
30 #include "NetLobbyServer.h"
31 #include "NetUser.h"
32 #include "NetChat.h"
33 
34 #include "DataLoader.h"
35 #include "Video.h"
36 #include "Keyboard.h"
37 #include "MachineInfo.h"
38 
39 // +--------------------------------------------------------------------+
40 // DECLARE MAPPING FUNCTIONS:
41 
42 DEF_MAP_CLIENT(NetUnitDlg, OnSelect);
43 DEF_MAP_CLIENT(NetUnitDlg, OnUnit);
45 DEF_MAP_CLIENT(NetUnitDlg, OnUnMap);
47 DEF_MAP_CLIENT(NetUnitDlg, OnBanConfirm);
48 DEF_MAP_CLIENT(NetUnitDlg, OnApply);
49 DEF_MAP_CLIENT(NetUnitDlg, OnCancel);
50 
51 // +--------------------------------------------------------------------+
52 
54 : FormWindow(s, 0, 0, s->Width(), s->Height()), manager(mgr),
55 net_lobby(0), unit_index(-1)
56 {
57  last_chat = 0;
58  host_mode = false;
59 
60  Init(def);
61 }
62 
64 {
65 }
66 
67 // +--------------------------------------------------------------------+
68 
69 void
71 {
73  lst_units = (ListBox*) FindControl(202);
74  lst_chat = (ListBox*) FindControl(211);
75  edt_chat = (EditBox*) FindControl(212);
76 
78 
79  if (edt_chat)
80  edt_chat->SetText("");
81 
82  btn_select = (Button*) FindControl(206);
84 
85  btn_map = (Button*) FindControl(203);
87 
88  btn_unmap = (Button*) FindControl(204);
90 
91  btn_ban = (Button*) FindControl(205);
92 
93  if (btn_ban) {
96  }
97 
100 
101  btn_cancel = (Button*) FindControl(2);
103 }
104 
105 // +--------------------------------------------------------------------+
106 
107 void
109 {
110  if (!IsShown()) {
112 
113  // clear server data:
114  if (lst_players) {
118  }
119 
120  if (lst_units) {
123  lst_units->SetLeading(2);
124  }
125 
126  if (lst_chat) lst_chat->ClearItems();
127  last_chat = 0;
128 
129  if (btn_apply)
130  btn_apply->SetEnabled(false);
131 
133  host_mode = false;
134 
135  if (net_lobby) {
137  }
138 
139  if (host_mode) {
140  btn_select->Hide();
141  btn_map->Show();
142  btn_unmap->Show();
143  btn_ban->Show();
144  }
145  else {
146  btn_select->Show();
147  btn_map->Hide();
148  btn_unmap->Hide();
149  btn_ban->Hide();
150  }
151  }
152 }
153 
154 // +--------------------------------------------------------------------+
155 
156 void
158 {
159  ExecLobbyFrame();
160 
161  if (!net_lobby)
162  return;
163 
164  Text player_name;
165 
167  player_name = Player::GetCurrentPlayer()->Name();
168 
169  if (btn_select) {
170  bool enable = false;
171 
172  if (lst_players && lst_units && btn_select->IsVisible()) {
173  int sel_unit = lst_units->GetSelection();
174 
175  enable = sel_unit >= 0 &&
176  lst_units->GetItemText(sel_unit).length() == 0;
177  }
178 
179  btn_select->SetEnabled(enable);
180  }
181 
182  if (btn_map) {
183  bool enable = false;
184 
185  if (lst_players && lst_units && btn_map->IsVisible()) {
186  int sel_play = lst_players->GetSelection();
187  int sel_unit = lst_units->GetSelection();
188 
189  enable = sel_unit >= 0 && sel_play >= 0 &&
190  lst_units->GetItemText(sel_unit).length() == 0;
191 
192  if (enable && !host_mode) {
193  NetUser* u = (NetUser*) lst_players->GetItemData(sel_play);
194  if (!u || u->Name() != player_name)
195  enable = false;
196  }
197  }
198 
199  btn_map->SetEnabled(enable);
200  }
201 
202  if (btn_unmap) {
203  bool enable = false;
204 
205  if (lst_players && lst_units && btn_unmap->IsVisible()) {
206  int sel_play = lst_players->GetSelection();
207  int sel_unit = lst_units->GetSelection();
208 
209  enable = sel_unit >= 0 && lst_units->GetItemText(sel_unit).length() > 0;
210 
211  if (enable && !host_mode) {
212  NetUser* u = (NetUser*) lst_units->GetItemData(sel_unit);
213  if (!u || u->Name() != Player::GetCurrentPlayer()->Name())
214  enable = false;
215  }
216  }
217 
218  btn_unmap->SetEnabled(enable);
219  }
220 
221  if (btn_ban) {
222  bool enable = false;
223 
224  if (lst_players && lst_units && host_mode && btn_ban->IsVisible()) {
225  int sel_play = lst_players->GetSelection();
226  int sel_unit = lst_units->GetSelection();
227 
228  enable = sel_play >= 0 && sel_unit < 0;
229 
230  if (enable) {
231  NetUser* u = (NetUser*) lst_players->GetItemData(sel_play);
232  if (u && u->Name() == player_name)
233  enable = false;
234  }
235  }
236 
237  btn_ban->SetEnabled(enable);
238  }
239 
240  if (btn_apply) {
241  bool ok_to_start = net_lobby->IsMapped(player_name);
242 
243  NetUser* host = net_lobby->GetHost();
244  if (host && !net_lobby->IsMapped(host->Name()))
245  ok_to_start = false;
246 
247  btn_apply->SetEnabled(ok_to_start);
248  }
249 
250  if (Keyboard::KeyDown(VK_RETURN)) {
251  if (edt_chat && edt_chat->GetText().length() > 0) {
253  edt_chat->SetText("");
254  }
255  }
256 
258  GetChat();
259  GetUnits();
260  GetAvailable();
261 }
262 
263 // +--------------------------------------------------------------------+
264 
265 void
267 {
269 
270  if (!net_lobby) {
272  }
273 
274  else if (net_lobby->GetLastError() != 0) {
275  if (net_lobby->IsClient()) {
276  if (stars)
277  stars->StopLobby();
278 
279  net_lobby = 0;
281  }
282  }
283 }
284 
285 // +--------------------------------------------------------------------+
286 
287 static bool assignment_change = false;
288 static int num_users = 0;
289 
290 void
292 {
293  if (net_lobby && lst_units) {
295  List<NetUnitEntry>& units = iter.container();
296  List<NetUser>& users = net_lobby->GetUsers();
297 
298  if (users.size() != num_users) {
299  assignment_change = true;
300  num_users = users.size();
301  }
302 
303  if (units.size() != lst_units->NumItems()) {
304  assignment_change = true;
305  }
306 
307  else if (lst_units->NumItems()) {
308  for (int i = 0; i < units.size(); i++) {
309  NetUnitEntry* e = units.at(i);
310  Text user_name = e->GetUserName();
311  NetUser* u = net_lobby->FindUserByName(user_name);
312 
313  if (lst_units->GetItemData(i) != (DWORD) u)
314  assignment_change = true;
315  }
316  }
317  }
318 }
319 
320 void
322 {
323  if (!lst_players) return;
324 
325  Text player_name;
326 
328  player_name = Player::GetCurrentPlayer()->Name();
329 
330  if (net_lobby) {
331  List<NetUser> available_users;
332 
334  if (u) {
335  bool assigned = false;
337  while (++iter) {
338  NetUnitEntry* unit = iter.value();
339  if (unit->GetUserName() == u->Name())
340  assigned = true;
341  }
342 
343  if (!assigned)
344  available_users.append(u);
345  }
346 
348  while (++iter) {
349  NetUser* u = iter.value();
350  bool assigned = false;
352  while (++iter) {
353  NetUnitEntry* unit = iter.value();
354  if (unit->GetUserName() == u->Name())
355  assigned = true;
356  }
357 
358  if (!assigned) {
359  available_users.append(u);
360  }
361  }
362 
363  if (available_users.size() != lst_players->NumItems()) {
364  assignment_change = true;
366 
367  for (int i = 0; i < available_users.size(); i++) {
368  NetUser* u = available_users[i];
369 
370  Text name = Player::RankAbrv(u->Rank());
371  name += " ";
372  name += u->Name();
373 
374  lst_players->AddItemWithData(name.data(), (DWORD) u);
375 
376  if (!host_mode && u->Name() == player_name) {
378  }
379  }
380  }
381  }
382 }
383 
384 // +--------------------------------------------------------------------+
385 
386 void
388 {
389  if (!lst_units) return;
390 
391  if (net_lobby) {
393  List<NetUnitEntry>& units = iter.container();
394  List<NetUser>& users = net_lobby->GetUsers();
395 
396  if (assignment_change) {
398 
399  for (int i = 0; i < units.size(); i++) {
400  NetUnitEntry* e = units.at(i);
401 
402  char name[64];
403  char team[16];
404 
405  if (e->GetIndex())
406  sprintf_s(name, "%s %d", e->GetElemName().data(), e->GetIndex());
407  else
408  strcpy_s(name, e->GetElemName().data());
409 
410  sprintf_s(team, "%d", e->GetIFF());
411 
412  Text user_name = e->GetUserName();
413 
414  NetUser* u = net_lobby->FindUserByName(user_name);
415  if (u) {
416  user_name = Player::RankAbrv(u->Rank());
417  user_name += " ";
418  user_name += u->Name();
419  }
420 
421  int count = lst_units->AddItemWithData(user_name, (DWORD) u);
422  lst_units->SetItemText(count-1, 1, name);
423  lst_units->SetItemText(count-1, 2, e->GetDesign());
424 
425  if (lst_units->NumColumns() > 4) {
427  lst_units->SetItemText(count-1, 4, team);
428  }
429  else if (lst_units->NumColumns() > 3) {
430  lst_units->SetItemText(count-1, 3, team);
431  }
432  }
433  }
434  }
435 
436  assignment_change = false;
437 }
438 
439 // +--------------------------------------------------------------------+
440 
441 void
443 {
444  if (!lst_chat) return;
445 
446  if (net_lobby) {
447  int last_item = lst_chat->NumItems() - 1;
448  int count = 0;
449  bool added = false;
450 
452  while (++iter) {
453  NetChatEntry* c = iter.value();
454 
455  if (count++ > last_item) {
456  int n = lst_chat->AddItem(c->GetUser());
457  lst_chat->SetItemText(n-1, 1, c->GetMessage());
458  added = true;
459  }
460  }
461 
462  if (added)
464  }
465 }
466 
467 
468 void
470 {
471  if (msg.length() < 1) return;
472 
473  Player* player = Player::GetCurrentPlayer();
474 
475  if (msg[0] >= '0' && msg[0] <= '9') {
476  if (player) {
477  Text macro = player->ChatMacro(msg[0] - '0');
478 
479  if (macro.length())
480  msg = macro;
481  }
482  }
483 
484  if (net_lobby)
485  net_lobby->AddChat(0, msg);
486 }
487 
488 // +--------------------------------------------------------------------+
489 
490 void
492 {
493  if (!lst_units || host_mode) return;
494 
495  static DWORD unit_click_time = 0;
496 
497  int list_index = lst_units->GetListIndex();
498 
499  // double-click:
500  if (list_index == unit_index && Game::RealTime() - unit_click_time < 350) {
501  OnSelect(0);
502  }
503 
504  unit_click_time = Game::RealTime();
505  unit_index = list_index;
506 }
507 
508 // +--------------------------------------------------------------------+
509 
510 void
512 {
513  if (!lst_players || !lst_units) return;
514 
515  Text player_name;
516 
518  player_name = Player::GetCurrentPlayer()->Name();
519 
520  int sel_unit = lst_units->GetSelection();
521 
522  if (net_lobby) {
523  net_lobby->MapUnit(sel_unit, player_name, host_mode);
525  }
526 
527  assignment_change = true;
528 
529  GetUnits();
530  GetAvailable();
531 }
532 
533 // +--------------------------------------------------------------------+
534 
535 void
537 {
538  if (!lst_players || !lst_units) return;
539 
540  int sel_player = lst_players->GetSelection();
541  int sel_unit = lst_units->GetSelection();
542 
543  if (net_lobby) {
544  NetUser* u = (NetUser*) lst_players->GetItemData(sel_player);
545  net_lobby->MapUnit(sel_unit, u->Name(), host_mode);
547  }
548 
549  assignment_change = true;
550 
551  GetUnits();
552  GetAvailable();
553 }
554 
555 void
557 {
558  if (!lst_players || !lst_units) return;
559 
560  if (net_lobby) {
563  }
564 
565  assignment_change = true;
566 
567  GetUnits();
568  GetAvailable();
569 }
570 
571 void
573 {
574  if (!lst_players) return;
575 
576  int sel_player = lst_players->GetSelection();
577 
578  if (net_lobby) {
579  NetUser* u = (NetUser*) lst_players->GetItemData(sel_player);
580 
581  ConfirmDlg* confirm = manager->GetConfirmDlg();
582  if (confirm) {
583  char msg[512];
584  sprintf_s(msg, Game::GetText("NetUnitDlg.are-you-sure").data(), u->Name().data());
585  confirm->SetMessage(msg);
586  confirm->SetTitle(Game::GetText("NetUnitDlg.confirm-ban"));
587  confirm->SetParentControl(btn_ban);
588 
590  }
591 
592  else {
593  OnBanConfirm(event);
594  }
595  }
596 }
597 
598 void
600 {
601  if (!lst_players) return;
602 
603  int sel_player = lst_players->GetSelection();
604 
605  if (net_lobby) {
606  NetUser* u = (NetUser*) lst_players->GetItemData(sel_player);
607  net_lobby->BanUser(u);
609  }
610 
611  GetUnits();
612  GetAvailable();
613 }
614 
615 void
617 {
618  bool ok = false;
619 
620  if (net_lobby) {
621  Mission* mission = net_lobby->GetSelectedMission();
622 
623  if (mission) {
624  net_lobby->GameStart();
625  ok = true;
626  }
627  }
628 
629  if (!ok) OnCancel(0);
630 }
631 
632 void
634 {
635  if (net_lobby) {
637  net_lobby = 0;
638  }
639 
641 }