OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4GameLobby.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 // the ingame-lobby
17 
18 #include "C4Include.h"
20 #include "gui/C4GameLobby.h"
21 
22 #include "c4group/C4Components.h"
23 #include "control/C4GameControl.h"
24 #include "game/C4Application.h"
26 #include "gui/C4ChatDlg.h"
27 #include "gui/C4GameOptions.h"
28 #include "gui/C4MessageInput.h"
30 #include "network/C4Network2.h"
32 
33 namespace C4GameLobby
34 {
35 
36  bool UserAbort = false;
37 
38 // ----------- C4PacketCountdown ---------------------------------------------
39 
41  {
42  pComp->Value(mkNamingAdapt(iCountdown, "Countdown", 0));
43  }
44 
46  {
47  const char *szCountdownMsg;
48  if (iCountdown < AlmostStartCountdownTime && !fInitialMsg) szCountdownMsg = "%d..."; else szCountdownMsg = LoadResStr("IDS_PRC_COUNTDOWN");
49  return FormatString(szCountdownMsg, (int)iCountdown);
50  }
51 
52 
53 // ----------- C4PacketSetScenarioParameter ---------------------------------------------
54 
56  {
58  pComp->Value(mkNamingAdapt(Value, "Value", 0));
59  }
60 
61 
62 // ----------- ScenDescs ---------------------------------------------
63 
64  ScenDesc::ScenDesc(const C4Rect &rcBounds, bool fActive) : C4GUI::Window(), fDescFinished(false)
65  {
66  // build components
67  SetBounds(rcBounds);
68  C4GUI::ComponentAligner caMain(GetClientRect(), 0,0, true);
69  AddElement(pDescBox = new C4GUI::TextWindow(caMain.GetAll(), 0, 0, 0, 100, 4096, "", true));
70  pDescBox->SetDecoration(false, false, nullptr, true);
71  // initial update to set current data
72  if (fActive) Activate();
73  }
74 
75  void ScenDesc::Update()
76  {
77  // scenario present?
79  if (!pRes) return; // something's wrong
82  pDescBox->ClearText(false);
83  if (pRes->isComplete())
84  {
85  C4Group ScenarioFile;
86  if (!ScenarioFile.Open(pRes->getFile()))
87  {
88  pDescBox->AddTextLine("scenario file load error", &rTextFont, C4GUI_MessageFontClr, false, true);
89  }
90  else
91  {
92  // load desc
93  C4ComponentHost DefDesc;
95  pDescBox->AddTextLine(DefDesc.GetData(), &rTextFont, C4GUI_MessageFontClr, false, true, &rTitleFont);
96  else
97  pDescBox->AddTextLine(Game.ScenarioTitle.getData(), &rTitleFont, C4GUI_CaptionFontClr, false, true);
98  }
99  // okay, done loading. No more updates.
100  fDescFinished = true;
101  Deactivate();
102  }
103  else
104  {
105  pDescBox->AddTextLine(FormatString(LoadResStr("IDS_MSG_SCENARIODESC_LOADING"), (int) pRes->getPresentPercent()).getData(),
106  &rTextFont, C4GUI_MessageFontClr, false, true);
107  }
108  pDescBox->UpdateHeight();
109  }
110 
112  {
113  // final desc set? no update then
114  if (fDescFinished) return;
115  // register timer if necessary
116  Application.Add(this);
117  // force an update
118  Update();
119  }
120 
122  {
123  // release timer if set
124  Application.Remove(this);
125  }
126 
127 // ----------- MainDlg -----------------------------------------------------------------------------
128 
130  : C4GUI::FullscreenDialog(!Game.ScenarioTitle ?
131  (const char *) LoadResStr("IDS_DLG_LOBBY"):
132  FormatString("%s - %s", Game.ScenarioTitle.getData(), LoadResStr("IDS_DLG_LOBBY")).getData(),
133  Game.ScenarioTitle.getData()),
134  pPlayerList(nullptr), pResList(nullptr), pChatBox(nullptr), pRightTabLbl(nullptr), pRightTab(nullptr),
135  pEdt(nullptr), btnRun(nullptr), btnPlayers(nullptr), btnResources(nullptr), btnTeams(nullptr), btnChat(nullptr)
136  {
137  // key bindings
138  pKeyHistoryUp = new C4KeyBinding(C4KeyCodeEx(K_UP ), "LobbyChatHistoryUp" , KEYSCOPE_Gui, new C4GUI::DlgKeyCBEx<MainDlg, bool>(*this, true , &MainDlg::KeyHistoryUpDown), C4CustomKey::PRIO_CtrlOverride);
139  pKeyHistoryDown= new C4KeyBinding(C4KeyCodeEx(K_DOWN), "LobbyChatHistoryDown", KEYSCOPE_Gui, new C4GUI::DlgKeyCBEx<MainDlg, bool>(*this, false, &MainDlg::KeyHistoryUpDown), C4CustomKey::PRIO_CtrlOverride);
140  // timer
141  Application.Add(this);
142  // indents / sizes
143  int32_t iDefBtnHeight = 32;
144  int32_t iIndentX1, iIndentX2, iIndentX3;
145  int32_t iIndentY1, iIndentY2, iIndentY3, iIndentY4;
146  int32_t iClientListWdt;
147  if (GetClientRect().Wdt > 500)
148  {
149  // normal dlg
150  iIndentX1 = 10; // lower button area
151  iIndentX2 = 20; // center area (chat)
152  iIndentX3 = 5; // client/player list
153  iClientListWdt = GetClientRect().Wdt / 3;
154  }
155  else
156  {
157  // small dlg
158  iIndentX1 = 2; // lower button area
159  iIndentX2 = 2; // center area (chat)
160  iIndentX3 = 1; // client/player list
161  iClientListWdt = GetClientRect().Wdt / 2;
162  }
163  if (GetClientRect().Hgt > 320)
164  {
165  // normal dlg
166  iIndentY1 = 16; // lower button area
167  iIndentY2 = 20; // status bar offset
168  iIndentY3 = 8; // center area (chat)
169  iIndentY4 = 8; // client/player list
170  }
171  else
172  {
173  // small dlg
174  iIndentY1 = 2; // lower button area
175  iIndentY2 = 2; // status bar offset
176  iIndentY3 = 1; // center area (chat)
177  iIndentY4 = 1; // client/player list
178  }
179  // set subtitle ToolTip
180  if (pSubTitle)
181  pSubTitle->SetToolTip(LoadResStr("IDS_DLG_SCENARIOTITLE"));
182  C4GUI::Label *pLbl;
183  // main screen components
184  C4GUI::ComponentAligner caMain(GetClientRect(), 0,0,true);
185  caMain.GetFromBottom(iIndentY2);
186  // lower button-area
187  C4GUI::ComponentAligner caBottom(caMain.GetFromBottom(iDefBtnHeight+iIndentY1*2), iIndentX1,iIndentY1);
188  // add buttons
190  btnExit = new C4GUI::CallbackButton<MainDlg>(LoadResStr("IDS_DLG_EXIT"), caBottom.GetFromLeft(100), &MainDlg::OnExitBtn);
191  if (fHost)
192  {
193  btnRun = new C4GUI::CallbackButton<MainDlg>(LoadResStr("IDS_DLG_GAMEGO"), caBottom.GetFromRight(100), &MainDlg::OnRunBtn);
194  checkReady = nullptr;
195  }
196  else
197  {
198  checkReady = new C4GUI::CheckBox(caBottom.GetFromRight(90), LoadResStr("IDS_DLG_READY"), false);
200  caBottom.GetFromRight(90);
201  }
202  pGameOptionButtons = new C4GameOptionButtons(caBottom.GetCentered(caBottom.GetInnerWidth(), std::min<int32_t>(C4GUI_IconExHgt, caBottom.GetHeight())), true, fHost, true);
203 
204  // players / resources sidebar
205  C4GUI::ComponentAligner caRight(caMain.GetFromRight(iClientListWdt), iIndentX3,iIndentY4);
207  caRight.ExpandTop(iIndentY4*2 + 1); // undo margin, so client list is located directly under label
208  pRightTab = new C4GUI::Tabular(caRight.GetAll(), C4GUI::Tabular::tbNone);
209  C4GUI::Tabular::Sheet *pPlayerSheet = pRightTab->AddSheet(LoadResStr("IDS_DLG_PLAYERS"));
210  C4GUI::Tabular::Sheet *pResSheet = pRightTab->AddSheet(LoadResStr("IDS_DLG_RESOURCES"));
211  C4GUI::Tabular::Sheet *pOptionsSheet = pRightTab->AddSheet(LoadResStr("IDS_DLG_OPTIONS"));
212  C4GUI::Tabular::Sheet *pScenarioSheet = pRightTab->AddSheet(LoadResStr("IDS_DLG_SCENARIO"));
213  pPlayerList = new C4PlayerInfoListBox(pPlayerSheet->GetContainedClientRect(), C4PlayerInfoListBox::PILBM_LobbyClientSort);
214  pPlayerSheet->AddElement(pPlayerList);
215  pResList = new C4Network2ResDlg(pResSheet->GetContainedClientRect(), false);
216  pResSheet->AddElement(pResList);
217  pOptionsList = new C4GameOptionsList(pResSheet->GetContainedClientRect(), false, C4GameOptionsList::GOLS_Lobby);
218  pOptionsSheet->AddElement(pOptionsList);
219  pScenarioInfo = new ScenDesc(pResSheet->GetContainedClientRect(), false);
220  pScenarioSheet->AddElement(pScenarioInfo);
221  pRightTabLbl->SetContextHandler(new C4GUI::CBContextHandler<C4GameLobby::MainDlg>(this, &MainDlg::OnRightTabContext));
222  pRightTabLbl->SetClickFocusControl(pPlayerList);
223 
224  bool fHasTeams = Game.Teams.IsMultiTeams();
225  bool fHasChat = C4ChatDlg::IsChatActive();
226  int32_t iBtnNum = 4+fHasTeams+fHasChat;
227  if (fHasTeams)
228  {
229  btnTeams = new C4GUI::CallbackButton<MainDlg, C4GUI::IconButton>(C4GUI::Ico_Team, pRightTabLbl->GetToprightCornerRect(16, 16, 4, 4, --iBtnNum), LoadResStr("IDS_DLG_PLAYERSBYTEAM"), &MainDlg::OnTabTeams);
230  }
231  btnPlayers = new C4GUI::CallbackButton<MainDlg, C4GUI::IconButton>(C4GUI::Ico_Player, pRightTabLbl->GetToprightCornerRect(16,16,4,4,--iBtnNum), LoadResStr("IDS_DLG_PLAYERS"), &MainDlg::OnTabPlayers);
232  btnResources = new C4GUI::CallbackButton<MainDlg, C4GUI::IconButton>(C4GUI::Ico_Resource, pRightTabLbl->GetToprightCornerRect(16,16,4,4,--iBtnNum), LoadResStr("IDS_DLG_RESOURCES"), &MainDlg::OnTabRes);
233  btnOptions = new C4GUI::CallbackButton<MainDlg, C4GUI::IconButton>(C4GUI::Ico_Options, pRightTabLbl->GetToprightCornerRect(16,16,4,4,--iBtnNum), LoadResStr("IDS_DLG_OPTIONS"), &MainDlg::OnTabOptions);
234  btnScenario = new C4GUI::CallbackButton<MainDlg, C4GUI::IconButton>(C4GUI::Ico_Gfx, pRightTabLbl->GetToprightCornerRect(16,16,4,4,--iBtnNum), LoadResStr("IDS_DLG_SCENARIO"), &MainDlg::OnTabScenario);
235  if (fHasChat)
236  btnChat = new C4GUI::CallbackButton<MainDlg, C4GUI::IconButton>(C4GUI::Ico_Ex_Chat, pRightTabLbl->GetToprightCornerRect(16,16,4,4,--iBtnNum), LoadResStr("IDS_CTL_CHAT"), &MainDlg::OnBtnChat);
237 
238  // update labels and tooltips for player list
239  UpdateRightTab();
240 
241  // chat area
242  C4GUI::ComponentAligner caCenter(caMain.GetAll(), iIndentX2, iIndentY3);
243  // chat input box
244  C4GUI::ComponentAligner caChat(caCenter.GetFromBottom(C4GUI::Edit::GetDefaultEditHeight()), 0,0);
245  pLbl = new C4GUI::WoodenLabel(LoadResStr("IDS_CTL_CHAT"), caChat.GetFromLeft(40), C4GUI_CaptionFontClr, &::GraphicsResource.TextFont);
246  pEdt = new C4GUI::CallbackEdit<MainDlg>(caChat.GetAll(), this, &MainDlg::OnChatInput);
247  pEdt->SetToolTip(LoadResStr("IDS_DLGTIP_CHAT")); pLbl->SetToolTip(LoadResStr("IDS_DLGTIP_CHAT"));
248  pLbl->SetClickFocusControl(pEdt);
249  // log box
250  pChatBox = new C4GUI::TextWindow(caCenter.GetAll());
251  // add components in tab-order
252  AddElement(pChatBox);
253  AddElement(pLbl); AddElement(pEdt); // chat
254 
255  AddElement(pRightTabLbl);
256  if (btnTeams) AddElement(btnTeams);
257  AddElement(btnPlayers);
258  AddElement(btnResources);
259  AddElement(btnOptions);
260  AddElement(btnScenario);
261  if (btnChat) AddElement(btnChat);
262 
263  AddElement(pRightTab);
264  AddElement(btnExit); btnExit->SetToolTip(LoadResStr("IDS_DLGTIP_EXIT"));
265  AddElement(pGameOptionButtons);
266  if (fHost)
267  {
268  AddElement(btnRun);
269  btnRun->SetToolTip(LoadResStr("IDS_DLGTIP_GAMEGO"));
270  }
271  else
272  {
273  AddElement(checkReady);
274  checkReady->SetToolTip(LoadResStr("IDS_DLGTIP_READY"));
275  }
276  // set initial focus
277  SetFocus(pEdt, false);
278 
279  // stuff
280  eCountdownState = CDS_None;
281  iBackBufferIndex = -1;
282 
283  // initial player list update
284  UpdatePlayerList();
285  }
286 
288  {
289  Application.Remove(this);
290  delete pKeyHistoryUp;
291  delete pKeyHistoryDown;
292  }
293 
295  {
296  // abort dlg
297  Close(false);
298  }
299 
301  {
302  bool rIsOn = static_cast<C4GUI::CheckBox *>(pCheckBox)->GetChecked();
304  }
305 
306  void MainDlg::SetCountdownState(CountdownState eToState, int32_t iTimer)
307  {
308  // no change?
309  if (eToState == eCountdownState) return;
310  // changing away from countdown?
311  if (eCountdownState == CDS_Countdown)
312  {
313  StopSoundEffect("Structures::Elevator::Moving", nullptr);
314  if (eToState != CDS_Start) StartSoundEffect("Liquids::Pshshsh");
315  }
316  // change to game start?
317  if (eToState == CDS_Start)
318  {
319  // announce it!
320  StartSoundEffect("Fire::Blast3");
321  }
322  else if (eToState == CDS_Countdown)
323  {
324  StartSoundEffect("Fire::Fuse");
325  }
326  if (eToState == CDS_Countdown || eToState == CDS_LongCountdown)
327  {
328  // game start notify
330  if (!eCountdownState)
331  {
332  // host update start button to be abort button
333  if (btnRun) btnRun->SetText(LoadResStr("IDS_DLG_CANCEL"));
334  }
335  }
336  // countdown abort?
337  if (!eToState)
338  {
339  // host update start button to be start button again
340  if (btnRun) btnRun->SetText(LoadResStr("IDS_DLG_GAMEGO"));
341  // countdown abort message
342  OnLog(LoadResStr("IDS_PRC_STARTABORTED"), C4GUI_LogFontClr2);
343  }
344  // set new state
345  eCountdownState = eToState;
346  // update stuff (makes team sel and fair crew btn available)
347  pGameOptionButtons->SetCountdown(IsCountdown());
348  UpdatePlayerList();
349  }
350 
352  {
353  // determine new countdown state
354  int32_t iTimer = 0;
355  CountdownState eNewState;
356  if (Pkt.IsAbort())
357  eNewState = CDS_None;
358  else
359  {
360  iTimer = Pkt.GetCountdown();
361  if (!iTimer)
362  eNewState = CDS_Start; // game is about to be started (boom)
363  else if (iTimer <= AlmostStartCountdownTime)
364  eNewState = CDS_Countdown; // eToState
365  else
366  eNewState = CDS_LongCountdown;
367  }
368  bool fWasCountdown = !!eCountdownState;
369  SetCountdownState(eNewState, iTimer);
370  // display countdown (except last, which ends the lobby anyway)
371  if (iTimer)
372  {
373  // first countdown message
374  OnLog(Pkt.GetCountdownMsg(!fWasCountdown).getData(), C4GUI_LogFontClr2);
375  StartSoundEffect("UI::Tick");
376  }
377  }
378 
380  {
381  // flag as countdown if countdown running or game is about to start
382  // so team choice, etc. will not become available in the last split-second
383  return eCountdownState >= CDS_Countdown;
384  }
385 
386  void MainDlg::OnClosed(bool fOK)
387  {
388  // lobby aborted by user: remember not to display error log
389  if (!fOK)
390  C4GameLobby::UserAbort = true;
391  // finish countdown if running
392  // (may not be finished if status change packet from host is faster than the countdown-initiate)
393  if (eCountdownState) SetCountdownState(fOK ? CDS_Start : CDS_None, 0);
394  }
395 
397  {
398  // only for host
399  if (!::Network.isHost()) return;
400  // already started? then abort
401  if (eCountdownState) { ::Network.AbortLobbyCountdown(); return; }
402  // otherwise start, utilizing correct countdown time
404  }
405 
406  void MainDlg::Start(int32_t iCountdownTime)
407  {
408  // network savegame resumes: Warn if not all players have been associated
409  if (Game.C4S.Head.SaveGame)
411  {
412  StdStrBuf sMsg; sMsg.Ref(LoadResStr("IDS_MSG_NOTALLSAVEGAMEPLAYERSHAVE"));
413  if (!GetScreen()->ShowMessageModal(sMsg.getData(), LoadResStr("IDS_MSG_FREESAVEGAMEPLRS"), C4GUI::MessageDialog::btnYesNo, C4GUI::Ico_SavegamePlayer, &Config.Startup.HideMsgPlrNoTakeOver))
414  return;
415  }
416  // validate countdown time
417  iCountdownTime = ValidatedCountdownTime(iCountdownTime);
418  // either direct start...
419  if (!iCountdownTime)
420  ::Network.Start();
421  else
422  // ...or countdown
423  ::Network.StartLobbyCountdown(iCountdownTime);
424  }
425 
426  C4GUI::Edit::InputResult MainDlg::OnChatInput(C4GUI::Edit *edt, bool fPasting, bool fPastingMore)
427  {
428  // get edit text
429  C4GUI::Edit *pEdt = reinterpret_cast<C4GUI::Edit *>(edt);
430  const char *szInputText = pEdt->GetText();
431  // no input?
432  if (!szInputText || !*szInputText)
433  {
434  // do some error sound then
435  C4GUI::GUISound("UI::Error");
436  }
437  else
438  {
439  // store input in backbuffer before processing commands
440  // because those might kill the edit field
441  ::MessageInput.StoreBackBuffer(szInputText);
442  ::MessageInput.ProcessInput(szInputText);
443  }
444  // clear edit field after text has been processed
445  pEdt->SelectAll(); pEdt->DeleteSelection();
446  // reset backbuffer-index of chat history
447  iBackBufferIndex = -1;
448  // OK, on we go. Leave edit intact
449  return C4GUI::Edit::IR_None;
450  }
451 
452  void MainDlg::OnClientJoin(C4Client *pNewClient)
453  {
454  // update list
455  UpdatePlayerList();
456  }
457 
459  {
460  }
461 
462  void MainDlg::OnClientPart(C4Client *pPartClient)
463  {
464  // update list
465  UpdatePlayerList();
466  }
467 
468  void MainDlg::HandlePacket(char cStatus, const C4PacketBase *pPacket, C4Network2Client *pClient)
469  {
470  // note that player info update packets are not handled by this function,
471  // but by player info list and then forwarded to MainDlg::OnPlayerUpdate
472  // this is necessary because there might be changes (e.g. duplicate colors)
473  // done by player info list
474  // besides, this releases the lobby from doing any host/client-specializations
475 #define GETPKT(type, name) \
476  assert(pPacket); const type &name = \
477  static_cast<const type &>(*pPacket);
478  switch (cStatus)
479  {
480  case PID_LobbyCountdown: // initiate or abort countdown
481  {
483  // do countdown
484  OnCountdownPacket(Pkt);
485  }
486  break;
487  case PID_SetScenarioParameter: // set a scenario parameter value
488  {
490  ::Game.Parameters.ScenarioParameters.SetValue(Pkt.GetID(), Pkt.GetValue(), false);
491  // reflect updated value immediately on clients
492  if (pRightTab->GetActiveSheetIndex() == SheetIdx_Options) if (pOptionsList) pOptionsList->Update();
493  }
494  };
495 #undef GETPKT
496  }
497 
498  bool MainDlg::OnMessage(C4Client *pOfClient, const char *szMessage)
499  {
500  // output message should be prefixed with client already
501  const char *szMsgBuf = szMessage;
502  // 2do: log with player colors?
503  if (pChatBox && !pOfClient->IsIgnored())
504  {
505  pChatBox->AddTextLine(szMsgBuf, &::GraphicsResource.TextFont, ::Network.Players.GetClientChatColor(pOfClient ? pOfClient->getID() : Game.Clients.getLocalID(), true) | C4GUI_MessageFontAlpha, true, true);
506  pChatBox->ScrollToBottom();
507  }
508  // log it
509  LogSilent(szMsgBuf);
510  // done, success
511  return true;
512  }
513 
515  {
516  // show that someone played a sound
517  if (pOfClient && pPlayerList)
518  {
519  pPlayerList->SetClientSoundIcon(pOfClient->getID());
520  }
521  }
522 
523  void MainDlg::OnLog(const char *szLogMsg, DWORD dwClr)
524  {
525  if (pChatBox)
526  {
527  pChatBox->AddTextLine(szLogMsg, &::GraphicsResource.TextFont, dwClr, true, true);
528  pChatBox->ScrollToBottom();
529  }
530  }
531 
532  void MainDlg::OnError(const char *szErrMsg)
533  {
534  if (pChatBox)
535  {
536  StartSoundEffect("UI::Error");
537  pChatBox->AddTextLine(szErrMsg, &::GraphicsResource.TextFont, C4GUI_ErrorFontClr, true, true);
538  pChatBox->ScrollToBottom();
539  }
540  }
541 
543  {
544  UpdatePlayerList();
545  }
546 
547  void MainDlg::UpdatePlayerList()
548  {
549  // this updates ping label texts and teams
550  if (pPlayerList) pPlayerList->Update();
551  }
552 
554  {
555  // create context menu
556  C4GUI::ContextMenu *pMenu = new C4GUI::ContextMenu();
557  // players/resources
558  C4GUI::Tabular::Sheet *pPlayerSheet = pRightTab->GetSheet(0);
559  C4GUI::Tabular::Sheet *pResSheet = pRightTab->GetSheet(1);
560  C4GUI::Tabular::Sheet *pOptionsSheet = pRightTab->GetSheet(2);
561  pMenu->AddItem(pPlayerSheet->GetTitle(), pPlayerSheet->GetToolTip(), C4GUI::Ico_Player,
563  if (Game.Teams.IsMultiTeams())
564  {
565  StdCopyStrBuf strShowTeamsDesc(LoadResStr("IDS_MSG_SHOWTEAMS_DESC"));
566  pMenu->AddItem(LoadResStr("IDS_MSG_SHOWTEAMS"), strShowTeamsDesc.getData(), C4GUI::Ico_Team,
567  new C4GUI::CBMenuHandler<MainDlg>(this, &MainDlg::OnCtxTabTeams));
568  }
569  pMenu->AddItem(pResSheet->GetTitle(), pResSheet->GetToolTip(), C4GUI::Ico_Resource,
570  new C4GUI::CBMenuHandler<MainDlg>(this, &MainDlg::OnCtxTabRes));
571  pMenu->AddItem(pOptionsSheet->GetTitle(), pOptionsSheet->GetToolTip(), C4GUI::Ico_Options,
572  new C4GUI::CBMenuHandler<MainDlg>(this, &MainDlg::OnCtxTabOptions));
573  // open it
574  return pMenu;
575  }
576 
577  void MainDlg::OnClientAddPlayer(const char *szFilename, int32_t idClient)
578  {
579  // check client number
580  if (idClient != Game.Clients.getLocalID())
581  {
582  LobbyError(FormatString(LoadResStr("IDS_ERR_JOINPLR_NOLOCALCLIENT"), szFilename, idClient).getData());
583  return;
584  }
585  // player join - check filename
586  if (!ItemExists(szFilename))
587  {
588  LobbyError(FormatString(LoadResStr("IDS_ERR_JOINPLR_NOFILE"), szFilename).getData());
589  return;
590  }
591  // join!
593  }
594 
596  {
597  if (pPlayerList) pPlayerList->SetMode(C4PlayerInfoListBox::PILBM_LobbyClientSort);
598  if (pRightTab)
599  {
600  pRightTab->SelectSheet(SheetIdx_PlayerList, true);
601  UpdateRightTab();
602  }
603  }
604 
606  {
607  if (pPlayerList) pPlayerList->SetMode(C4PlayerInfoListBox::PILBM_LobbyTeamSort);
608  if (pRightTab)
609  {
610  pRightTab->SelectSheet(SheetIdx_PlayerList, true);
611  UpdateRightTab();
612  }
613  }
614 
616  {
617  if (pRightTab)
618  {
619  pRightTab->SelectSheet(SheetIdx_Res, true);
620  UpdateRightTab();
621  }
622  }
623 
625  {
626  if (pRightTab)
627  {
628  pRightTab->SelectSheet(SheetIdx_Options, true);
629  UpdateRightTab();
630  }
631  }
632 
634  {
635  if (pRightTab)
636  {
637  pRightTab->SelectSheet(SheetIdx_Scenario, true);
638  UpdateRightTab();
639  }
640  }
641 
643  {
644  if (!pRightTabLbl || !pRightTab || !pRightTab->GetActiveSheet() || !pPlayerList) return;
645  // copy active sheet data to label
646  pRightTabLbl->SetText(pRightTab->GetActiveSheet()->GetTitle());
647  pRightTabLbl->SetToolTip(pRightTab->GetActiveSheet()->GetToolTip());
648  // update
649  if (pRightTab->GetActiveSheetIndex() == SheetIdx_PlayerList) UpdatePlayerList();
650  if (pRightTab->GetActiveSheetIndex() == SheetIdx_Res) pResList->Activate(); else pResList->Deactivate();
651  if (pRightTab->GetActiveSheetIndex() == SheetIdx_Options) pOptionsList->Activate(); else pOptionsList->Deactivate();
652  if (pRightTab->GetActiveSheetIndex() == SheetIdx_Scenario) pScenarioInfo->Activate(); else pScenarioInfo->Deactivate();
653  // update selection buttons
654  if (btnPlayers) btnPlayers->SetHighlight(pRightTab->GetActiveSheetIndex() == SheetIdx_PlayerList && pPlayerList->GetMode() == C4PlayerInfoListBox::PILBM_LobbyClientSort);
655  if (btnResources) btnResources->SetHighlight(pRightTab->GetActiveSheetIndex() == SheetIdx_Res);
656  if (btnTeams) btnTeams->SetHighlight(pRightTab->GetActiveSheetIndex() == SheetIdx_PlayerList && pPlayerList->GetMode() == C4PlayerInfoListBox::PILBM_LobbyTeamSort);
657  if (btnOptions) btnOptions->SetHighlight(pRightTab->GetActiveSheetIndex() == SheetIdx_Options);
658  if (btnScenario) btnScenario->SetHighlight(pRightTab->GetActiveSheetIndex() == SheetIdx_Scenario);
659  }
660 
662  {
663  // open chat dialog
665  }
666 
668  {
669  // chat input only
670  if (!IsFocused(pEdt)) return false;
671  pEdt->SelectAll(); pEdt->DeleteSelection();
672  const char *szPrevInput = ::MessageInput.GetBackBuffer(fUp ? (++iBackBufferIndex) : (--iBackBufferIndex));
673  if (!szPrevInput || !*szPrevInput)
674  iBackBufferIndex = -1;
675  else
676  {
677  pEdt->InsertText(szPrevInput, true);
678  pEdt->SelectAll();
679  }
680  return true;
681  }
682 
684  {
685  // if the password setting has changed, make sure the buttons reflect this change
686  pGameOptionButtons->UpdatePasswordBtn();
687  }
688 
689  int32_t MainDlg::ValidatedCountdownTime(int32_t iTimeout)
690  {
691  // no negative timeouts
692  if (iTimeout < 0) iTimeout = 5;
693  // in leage mode, there must be at least five seconds timeout
694  if (Game.Parameters.isLeague() && iTimeout < 5) iTimeout = 5;
695  return iTimeout;
696  }
697 
699  {
700  pChatBox->ClearText(true);
701  }
702 
703  void LobbyError(const char *szErrorMsg)
704  {
705  // get lobby
706  MainDlg *pLobby = ::Network.GetLobby();
707  if (pLobby) pLobby->OnError(szErrorMsg);
708  else Log(szErrorMsg);
709  }
710 
711 
712  /* Countdown */
713 
714  Countdown::Countdown(int32_t iStartTimer) : iStartTimer(iStartTimer)
715  {
716  // only on network hosts
717  assert(::Network.isHost());
718  // ctor: Init; sends initial countdown packet
719  C4PacketCountdown pck(iStartTimer);
721  // also process on host
722  MainDlg *pLobby = ::Network.GetLobby();
723  if (pLobby)
724  {
725  pLobby->OnCountdownPacket(pck);
726  }
727  else
728  {
729  // no lobby: Message to log for dedicated/console hosts
730  Log(pck.GetCountdownMsg().getData());
731  }
732 
733  // init timer callback
734  Application.Add(this);
735  }
736 
738  {
739  // release timer
740  Application.Remove(this);
741  }
742 
744  {
745  // count down
746  iStartTimer = std::max<int32_t>(iStartTimer - 1, 0);
747  // only send "important" start timer numbers to all clients
748  if (iStartTimer <= AlmostStartCountdownTime || // last seconds
749  (iStartTimer <= 600 && !(iStartTimer % 10)) || // last minute: 10s interval
750  !(iStartTimer % 60)) // otherwise, minute interval
751  {
752  C4PacketCountdown pck(iStartTimer);
754  // also process on host
755  MainDlg *pLobby = ::Network.GetLobby();
756  if (pLobby)
757  pLobby->OnCountdownPacket(pck);
758  else if (iStartTimer)
759  {
760  // no lobby: Message to log for dedicated/console hosts
761  Log(pck.GetCountdownMsg().getData());
762  }
763  }
764  // countdown done
765  if (!iStartTimer)
766  {
767 #ifdef USE_CONSOLE
768  // Dedicated server: if there are not enough players for this game, abort and quit the application
770  || ::Network.Clients.Count() <= 2)
771  {
772  Log(LoadResStr("IDS_MSG_NOTENOUGHPLAYERSFORTHISRO")); // it would also be nice to send this message to all clients...
773  Application.Quit();
774  }
775  // Start the game
776  else
777 #endif // USE_CONSOLE
778  ::Network.Start();
779  }
780  }
781 
783  {
784  // host sends packets
785  if (!::Network.isHost()) return;
788  // also process on host
789  MainDlg *pLobby = ::Network.GetLobby();
790  if (pLobby)
791  {
792  pLobby->OnCountdownPacket(pck);
793  }
794  else
795  {
796  // no lobby: Message to log for dedicated/console hosts
797  Log(LoadResStr("IDS_PRC_STARTABORTED"));
798  }
799  }
800 
801 } // end of namespace
802 
803 
const char * getData() const
Definition: StdBuf.h:442
const char * GetTitle()
Definition: C4Gui.h:1624
bool IsFocused(Control *pCtrl) override
Definition: C4Gui.h:2113
class C4GameLobby::MainDlg * GetLobby() const
Definition: C4Network2.h:216
bool fHost
Definition: TstC4NetIO.cpp:31
void AddItem(const char *szText, const char *szToolTip=nullptr, Icons icoIcon=Ico_None, MenuHandler *pMenuHandler=nullptr, ContextHandler *pSubmenuHandler=nullptr)
Definition: C4Gui.h:1873
C4PlayerInfoList & RestorePlayerInfos
Definition: C4Game.h:72
bool Start()
Definition: C4Network2.cpp:505
void OnClientPart(C4Client *pPartClient)
#define C4GUI_CaptionFontClr
Definition: C4Gui.h:37
C4Config Config
Definition: C4Config.cpp:833
#define GETPKT(type, name)
void GUISound(const char *szSound)
Definition: C4Gui.cpp:1175
C4ConfigStartup Startup
Definition: C4Config.h:260
void GetAll(C4Rect &rcOut)
Definition: C4Gui.cpp:1125
void SetCountdown(bool fToVal)
bool LogSilent(const char *szMessage, bool fConsole)
Definition: C4Log.cpp:124
void SetDecoration(bool fDrawBG, bool fDrawFrame, ScrollBarFacets *pToGfx, bool fAutoScroll)
Definition: C4Gui.h:1752
bool isHost() const
Definition: C4Network2.h:209
const char * GetBackBuffer(int32_t iIndex)
#define C4GUI_MessageFontClr
Definition: C4Gui.h:43
void AbortLobbyCountdown()
int32_t GetPlayerCount() const
void DeleteSelection()
Definition: C4GuiEdit.cpp:149
C4Game Game
Definition: C4Globals.cpp:52
C4GUI::Edit::InputResult OnChatInput(C4GUI::Edit *edt, bool fPasting, bool fPastingMore)
bool KeyHistoryUpDown(bool fUp)
C4Scenario C4S
Definition: C4Game.h:74
C4ConfigGeneral General
Definition: C4Config.h:251
StdCopyStrBuf ScenarioTitle
Definition: C4Game.h:103
const char * GetData() const
void OnClientJoin(C4Client *pNewClient)
ScenDesc(const C4Rect &rcBounds, bool fActive)
Definition: C4GameLobby.cpp:64
#define C4GUI_IconExHgt
Definition: C4Gui.h:96
bool isComplete() const
bool AddElement(Element *pChild, int32_t iIndent=0)
void SetToolTip(const char *szNewTooltip, bool is_immediate=false)
Definition: C4Gui.cpp:409
void HandlePacket(char cStatus, const C4PacketBase *pBasePkt, C4Network2Client *pClient)
void SelectAll()
Definition: C4GuiEdit.cpp:641
bool BroadcastMsgToClients(const C4NetIOPacket &rPkt)
void Close(bool fOK)
void OnSec1Timer() override
const char * GetText()
Definition: C4Gui.h:1338
void OnCountdownPacket(const C4PacketCountdown &Pkt)
Definition: C4Rect.h:27
C4MessageInput MessageInput
void OnSec1Timer() override
void AddElement(Element *pChild)
bool IsMultiTeams() const
Definition: C4Teams.h:162
void StopSoundEffect(const char *szSndName, C4Object *pObj)
int32_t HideMsgPlrNoTakeOver
Definition: C4Config.h:186
C4Rect GetContainedClientRect()
Definition: C4Gui.h:448
int32_t GetMinPlayer()
Definition: C4Scenario.cpp:141
C4GraphicsResource GraphicsResource
void SelectSheet(int32_t iIndex, bool fByUser)
C4GameParameters & Parameters
Definition: C4Game.h:67
void Add(StdSchedulerProc *pProc)
C4TeamList & Teams
Definition: C4Game.h:70
void OnLog(const char *szLogMsg, DWORD dwClr=C4GUI_LogFontClr)
const char * GetToolTip()
Definition: C4Gui.cpp:423
void StartLobbyCountdown(int32_t iCountdownTime)
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
void OnClientAddPlayer(const char *szFilename, int32_t idClient)
void LobbyError(const char *szErrorMsg)
StdNamingAdapt< T > mkNamingAdapt(T &&rValue, const char *szName)
Definition: StdAdaptors.h:92
int32_t getID() const
Definition: C4Client.h:105
bool OnMessage(C4Client *pOfClient, const char *szMessage)
C4SoundInstance * StartSoundEffect(const char *szSndName, bool fLoop, int32_t iVolume, C4Object *pObj, int32_t iCustomFalloffDistance, int32_t iPitch, C4SoundModifier *modifier)
int32_t Wdt
Definition: C4Rect.h:30
static bool LoadComponentHost(C4ComponentHost *host, C4Group &hGroup, const char *szFilename, const char *szLanguage)
Definition: C4Language.cpp:232
void OnClientSound(C4Client *pOfClient)
const char * getFile() const
bool SaveGame
Definition: C4Scenario.h:71
void OnCtxTabPlayers(C4GUI::Element *pListItem)
Definition: C4GameLobby.h:118
void StoreBackBuffer(const char *szMessage)
C4Network2 Network
Definition: C4Globals.cpp:53
bool InsertText(const char *szText, bool fUser)
Definition: C4GuiEdit.cpp:163
static int32_t GetDefaultHeight(CStdFont *pUseFont=nullptr)
C4ScenarioParameters ScenarioParameters
DWORD GetClientChatColor(int idForClient, bool fLobby) const
int32_t getLocalID() const
Definition: C4Client.h:171
C4GameControl Control
C4NetIOPacket MkC4NetIOPacket(char cStatus, const class C4PacketBase &Pkt, const C4NetIO::addr_t &addr=C4NetIO::addr_t())
Definition: C4PacketBase.h:40
C4ConfigLobby Lobby
Definition: C4Config.h:256
bool Open(const char *szGroupName, bool fCreate=false)
Definition: C4Group.cpp:514
void DoInput(C4PacketType eCtrlType, C4ControlPacket *pPkt, C4ControlDeliveryType eDelivery)
void OnRunBtn(C4GUI::Control *btn)
C4Rect & GetClientRect() override
Definition: C4Gui.h:864
C4PlayerInfo * FindUnassociatedRestoreInfo(const C4PlayerInfoList &rRestoreInfoList)
void OnCtxTabRes(C4GUI::Element *pListItem)
Definition: C4GameLobby.h:122
void OnTabRes(C4GUI::Control *btn)
void UpdateHeight()
Definition: C4Gui.h:1750
void OnError(const char *szErrMsg)
#define C4GUI_MessageFontAlpha
Definition: C4Gui.h:44
void OnTabScenario(C4GUI::Control *btn)
Sheet * GetSheet(int32_t iIndex)
Definition: C4Gui.h:1706
void Start(int32_t iCountdownTime)
void OnReadyCheck(C4GUI::Element *pCheckBox)
static int32_t GetDefaultEditHeight()
Definition: C4GuiEdit.cpp:113
int32_t CountdownTime
Definition: C4Config.h:197
void Value(const T &rStruct)
Definition: StdCompiler.h:161
void ScrollToBottom()
Definition: C4Gui.h:1742
char LanguageEx[CFG_MaxString+1]
Definition: C4Config.h:37
int32_t GetCountdown() const
Definition: C4GameLobby.h:44
C4GUI::ContextMenu * OnRightTabContext(C4GUI::Element *pLabel, int32_t iX, int32_t iY)
void OnClientConnect(C4Client *pClient, C4Network2IOConnection *pConn)
void Quit() override
void Ref(const char *pnData)
Definition: StdBuf.h:455
void Remove(StdSchedulerProc *pProc)
#define C4GUI_LogFontClr2
Definition: C4Gui.h:52
void CompileFunc(StdCompiler *pComp) override
Definition: C4GameLobby.cpp:55
void SetOnChecked(BaseCallbackHandler *pCB)
void OnTabTeams(C4GUI::Control *btn)
C4Network2ClientList Clients
Definition: C4Network2.h:116
C4PlayerInfoList & PlayerInfos
Definition: C4Game.h:71
void OnCtxTabOptions(C4GUI::Element *pListItem)
Definition: C4GameLobby.h:124
void CompileFunc(StdCompiler *pComp) override
Definition: C4GameLobby.cpp:40
void SetFocus(Control *pCtrl, bool fByMouse)
StdStrBuf GetCountdownMsg(bool fInitialMsg=false) const
Definition: C4GameLobby.cpp:45
void SetBounds(const C4Rect &rcNewBound)
Definition: C4Gui.h:446
static C4ChatDlg * ShowChat()
Definition: C4ChatDlg.cpp:966
bool IsIgnored() const
Definition: C4Client.h:114
static bool IsChatActive()
Definition: C4ChatDlg.cpp:996
Sheet * GetActiveSheet()
Definition: C4Gui.h:1707
Countdown(int32_t iStartTimer)
#define C4CFN_ScenarioDesc
Definition: C4Components.h:88
void OnTabOptions(C4GUI::Control *btn)
const int ALeft
Definition: C4Surface.h:41
void NotifyUserIfInactive()
Definition: C4App.cpp:89
C4Network2Players Players
Definition: C4Network2.h:119
void OnClosed(bool fOK) override
void OnTabPlayers(C4GUI::Control *btn)
bool Log(const char *szMessage)
Definition: C4Log.cpp:202
C4ClientList & Clients
Definition: C4Game.h:69
void SetMode(Mode eNewMode)
bool isLeague() const
C4Network2Res::Ref getNetRes() const
void AddTextLine(const char *szText, CStdFont *pFont, DWORD dwClr, bool fDoUpdate, bool fMakeReadableOnBlack, CStdFont *pCaptionFont=nullptr)
Definition: C4Gui.h:1740
StdParameterAdapt< T, P > mkParAdapt(T &&rObj, P &&rPar)
Definition: StdAdaptors.h:458
bool GetFromRight(int32_t iWdt, int32_t iHgt, C4Rect &rcOut)
Definition: C4Gui.cpp:1093
#define C4GUI_ErrorFontClr
Definition: C4Gui.h:53
Sheet * AddSheet(const char *szTitle, int32_t icoTitle=Ico_None)
void OnBtnChat(C4GUI::Control *btn)
virtual Screen * GetScreen()
Definition: C4Gui.cpp:289
void OnCtxTabTeams(C4GUI::Element *pListItem)
Definition: C4GameLobby.h:120
void OnExitBtn(C4GUI::Control *btn)
C4SHead Head
Definition: C4Scenario.h:230
const char * AtRelativePath(const char *szFilename)
Definition: C4Config.cpp:656
bool JoinLocalPlayer(const char *szLocalPlayerFilename, bool initial=false)
bool ItemExists(const char *szItemName)
Definition: StdFile.h:75
uint32_t DWORD
int32_t getPresentPercent() const
C4Application Application
Definition: C4Globals.cpp:44
bool GetFromBottom(int32_t iHgt, int32_t iWdt, C4Rect &rcOut)
Definition: C4Gui.cpp:1109
void SetClientSoundIcon(int32_t iForClientID)
const int32_t AlmostStartCountdownTime
Definition: C4GameLobby.h:27
void SetText(const char *szToText, bool fAllowHotkey=true)
Definition: C4GuiLabels.cpp:74
int32_t GetActiveSheetIndex()
void ClearText(bool fDoUpdate)
Definition: C4Gui.h:1744
void SetValue(const char *id, int32_t value, bool only_if_larger)
bool ProcessInput(const char *szText)
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270