File: network_menu.cpp

package info (click to toggle)
warmux 1%3A11.04.1%2Brepack2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 126,388 kB
  • sloc: cpp: 186,040; xml: 8,909; sh: 3,358; makefile: 1,052; ansic: 713
file content (532 lines) | stat: -rw-r--r-- 16,028 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/******************************************************************************
 *  Warmux is a convivial mass murder game.
 *  Copyright (C) 2001-2011 Warmux Team.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 ******************************************************************************
 * Game menu
 *****************************************************************************/
#include <WARMUX_index_server.h>
#include <WARMUX_team_config.h>

#include "menu/network_menu.h"

#include "menu/network_teams_selection_box.h"
#include "menu/map_selection_box.h"
#include "game/config.h"
#include "game/game.h"
#include "game/game_mode.h"
#include "graphic/video.h"
#include "gui/button.h"
#include "gui/check_box.h"
#include "gui/combo_box.h"
#include "gui/grid_box.h"
#include "gui/label.h"
#include "gui/msg_box.h"
#include "gui/picture_widget.h"
#include "gui/spin_button.h"
#include "gui/tabs.h"
#include "gui/talk_box.h"
#include "gui/text_box.h"
#include "include/action_handler.h"
#include "include/app.h"
#include "include/constant.h"
#include "network/network.h"
#include "network/network_client.h"
#include "network/network_server.h"
#include "sound/jukebox.h"
#include "team/teams_list.h"
#include "team/team.h"
#include "tool/resource_manager.h"

static const uint MARGIN_TOP    = 5;
static const uint MARGIN_SIDE   = 5;
static const uint MARGIN_BOTTOM = 40;

NetworkMenu::NetworkMenu()
  : Menu("menu/bg_network")
  , opt_game_mode(NULL)
{
  waiting_for_server = false;

  Point2i pointZero(W_UNDEF, W_UNDEF);

  Surface& window = GetMainWindow();

  // Calculate main box size
  int chat_box_height = window.GetHeight()/4;
  int team_box_height = 180;
  int mainBoxWidth = window.GetWidth() - 2*MARGIN_SIDE;
  int mainBoxHeight = window.GetHeight() - MARGIN_TOP - MARGIN_BOTTOM - 2*MARGIN_SIDE - chat_box_height;
  int mapsHeight = mainBoxHeight - team_box_height - 60;
  int multitabsWidth = mainBoxWidth;
  bool multitabs = false;

  if (window.GetWidth() > 640 && mapsHeight > 200) {
    multitabs = true;
    multitabsWidth = mainBoxWidth - 20;
    mapsHeight = 200;
    team_box_height = mainBoxHeight - 200 - 60;
  } else {
    mapsHeight = mainBoxHeight - 60;
    team_box_height = mainBoxHeight - 60;
  }

  tabs = new MultiTabs(Point2i(mainBoxWidth, mainBoxHeight));

  // ################################################
  // ##  TEAM AND MAP SELECTION
  // ################################################
  team_box = new NetworkTeamsSelectionBox(Point2i(multitabsWidth-4, team_box_height), multitabs);

  map_box = new MapSelectionBox(Point2i(multitabsWidth, mapsHeight),
				 multitabs, !Network::GetInstance()->IsGameMaster());

  if (!multitabs) {
    tabs->AddNewTab("TAB_Team", _("Teams"), team_box);
    tabs->AddNewTab("TAB_Map", _("Map"), map_box);
  } else {
    VBox *box = new VBox(mainBoxWidth, false, false, true);
    std::string tabs_title = _("Teams") + std::string(" - ");
    tabs_title += _("Map");

    box->AddWidget(team_box);
    box->AddWidget(map_box);
    tabs->AddNewTab("TAB_Team_Map", tabs_title, box);
  }

  tabs->SetPosition(MARGIN_SIDE, MARGIN_TOP);
  widgets.AddWidget(tabs);
  widgets.Pack();

  // ################################################
  // ##  PLAYERS INFORMATION
  // ################################################

  Box* bottom_box = new HBox(chat_box_height, false, false, true);
  bottom_box->SetNoBorder();

  Box* options_box = new VBox(200, true, true);

  mode_label = new Label("", 0, Font::FONT_MEDIUM, Font::FONT_BOLD,
                         primary_red_color, Text::ALIGN_LEFT_TOP, true);
  options_box->AddWidget(mode_label);

  play_in_loop = new CheckBox(_("Play several times"), W_UNDEF, true);
  options_box->AddWidget(play_in_loop);

  connected_players = new Label(Format(ngettext("%i player connected", "%i players connected", 0), 0),
                                0, Font::FONT_SMALL, Font::FONT_BOLD);
  options_box->AddWidget(connected_players);

  initialized_players = new Label(Format(ngettext("%i player ready", "%i players ready", 0), 0),
                                  0, Font::FONT_SMALL, Font::FONT_BOLD);
  options_box->AddWidget(initialized_players);

  options_box->Pack();
  bottom_box->AddWidget(options_box);

  // ################################################
  // ##  CHAT BOX
  // ################################################

  msg_box = new TalkBox(Point2i(mainBoxWidth - options_box->GetSizeX() - MARGIN_SIDE, chat_box_height),
                        Font::FONT_SMALL, Font::FONT_BOLD);
  msg_box->SetPosition(options_box->GetPositionX() + options_box->GetSizeX() + MARGIN_SIDE,
                       options_box->GetPositionY());

  bottom_box->AddWidget(msg_box);
  bottom_box->SetPosition(MARGIN_SIDE, tabs->GetPositionY() + tabs->GetSizeY() + MARGIN_SIDE);

  widgets.AddWidget(bottom_box);

  if (!Network::GetInstance()->IsServer()) {

    // First %s will be replaced with the name of the network game,
    // second %s by the server hostname
    msg_box->NewMessage(Format(_("Welcome to %s on %s!"),
                               Network::GetInstance()->GetGameName().c_str(),
                               ((NetworkClient*)Network::GetInstance())->GetServerAddress().c_str())
                        , c_red);
  }

  if (!Network::GetInstance()->IsGameMaster()) {
    // Client Mode
    mode_label->SetText(_("Client mode"));
    initialized_players->SetVisible(false);
    msg_box->NewMessage(_("Don't forget to validate once you have selected your team(s)!"), c_red);

  } else if (Network::GetInstance()->IsServer()) {
    // Server Mode
    AddGameModeTab();
    mode_label->SetText(_("Server mode"));
  } else {
    // The first player to connect to a headless server assumes the game master role
    SetGameMasterCallback();
  }
  widgets.Pack();
}

void NetworkMenu::AddGameModeTab()
{
  ASSERT(!opt_game_mode);

  Box *box = new GridBox(4, 4, 0, false);

  Point2i option_size(114, 114);
  std::string selected_gamemode = Config::GetInstance()->GetGameMode();

  // Using the game mode editor but currently we are not able to send
  // custom parameters to client
  opt_game_mode = new ComboBox(_("Game mode"), "menu/game_mode", option_size,
				 GameMode::ListGameModes(), selected_gamemode);
  box->AddWidget(opt_game_mode);

  tabs->AddNewTab("TAB_Game", _("Game"), box);
}

void NetworkMenu::signal_begin_run()
{
  ActionHandler::GetInstance()->ExecFrameLessActions();
  RequestSavedTeams();
}

void NetworkMenu::RequestSavedTeams()
{
  const std::list<ConfigTeam> & team_list = Config::GetInstance()->AccessNetworkTeamsList();
  std::list<ConfigTeam>::const_iterator it;

  if (team_list.size() > 0) {
    for (it = team_list.begin(); it != team_list.end(); it++) {
      ActionHandler::GetInstance()->NewRequestTeamAction(*it);
    }
  } else {
    team_box->RequestTeam();
  }
}

void NetworkMenu::SaveOptions()
{
  // map
  map_box->ValidMapSelection();

  // team
  team_box->ValidTeamsSelection();

  //Save options in XML
//  Config::GetInstance()->Save();

  if (Network::GetInstance()->IsGameMaster()) {
    Config::GetInstance()->SetGameMode(opt_game_mode->GetValue());
  }
}

void NetworkMenu::PrepareForNewGame()
{
  msg_box->Clear();
  b_ok->SetVisible(true);

  Network::GetInstance()->SetState(WNet::NETWORK_NEXT_GAME);
  Network::GetInstance()->SendNetworkState();

  // to choose another random map
  if (Network::GetInstance()->IsGameMaster())
    map_box->ChangeMapDelta(0);

  RedrawMenu();
}

bool NetworkMenu::signal_ok()
{
  if (!Network::GetInstance()->IsGameMaster()) {
    // Check the user have selected a team:
    bool found = false;

    for (std::vector<Team*>::iterator team = GetTeamsList().playing_list.begin();
         team != GetTeamsList().playing_list.end();
         team++) {
      if ((*team)->IsLocalHuman()) {
        found = true;
        break;
      }
    }

    if (!found) {
      msg_box->NewMessage(_("You won't be able to play before selecting a team!"));
      goto error;
    }

    // Wait for the game master, and stay in the menu map / team can still be changed
    WaitingForGameMaster();

    if (Network::GetInstance()->IsGameMaster()) {
      goto error;
    }

  } else {
    if (GetTeamsList().playing_list.size() <= 1) {
      msg_box->NewMessage(Format(ngettext("There is only %i team.",
                                          "There are only %i teams.",
                                          GetTeamsList().playing_list.size()),
                                 GetTeamsList().playing_list.size()), c_red);
      goto error;
    }
    if (Network::GetInstance()->GetNbPlayersConnected() == 0) {
      msg_box->NewMessage(_("You are alone. :-/"), c_red);
      goto error;
    }
    if (Network::GetInstance()->GetNbPlayersConnected() != Network::GetInstance()->GetNbPlayersWithState(Player::STATE_INITIALIZED)) {
      int nbr = Network::GetInstance()->GetNbPlayersConnected() - Network::GetInstance()->GetNbPlayersWithState(Player::STATE_INITIALIZED);
      std::string pl = Format(ngettext("Wait! %i player is not ready yet!", "Wait! %i players are not ready yet!", nbr), nbr);
      msg_box->NewMessage(pl, c_red);
      goto error;
    }
  }

  // Disconnection :-/
  if (!Network::IsConnected()) {
    Network::GetInstance()->network_menu = NULL;
    return true;

  } else {

    // Starting the game :-)
    SaveOptions();
    play_ok_sound();

    if (Network::GetInstance()->IsServer())
      IndexServer::GetInstance()->Disconnect();

    if (Network::GetInstance()->IsGameMaster())
      GameMode::GetInstance()->Load();

    Game::GetInstance()->Start();

    if (Network::GetInstance()->IsConnected()
        && Network::GetInstance()->GetNbPlayersConnected() != 0
        && play_in_loop->GetValue()) {
      PrepareForNewGame();
      return false;
    }

    Network::GetInstance()->network_menu = NULL;
  }

  Network::Disconnect();
  return true;

 error:
  play_error_sound();
  return false;
}

void NetworkMenu::key_ok()
{
  // return was pressed while chat texbox still had focus (player wants to send his msg)
  if (msg_box->TextHasFocus()) {
    msg_box->SendChatMsg();
    return;
  }

  IndexServer::GetInstance()->Disconnect();

  Menu::key_ok();
}

bool NetworkMenu::signal_cancel()
{
  Network::Disconnect();
  return true;
}

void NetworkMenu::Draw(const Point2i& /*mousePosition*/)
{
  if (Network::GetInstance()->IsConnected()) {
    //Refresh the number of connected players:
    int nbr = Network::GetInstance()->GetNbPlayersConnected() + 1;
    std::string pl = Format(ngettext("%i player connected", "%i players connected", nbr), nbr);
    if (connected_players->GetText() != pl)
      connected_players->SetText(pl);

    if (Network::GetInstance()->IsGameMaster()) {
      //Refresh the number of players ready (does not work on client):
      nbr = Network::GetInstance()->GetNbPlayersWithState(Player::STATE_INITIALIZED);
      if (Network::GetInstance()->GetState() == WNet::NETWORK_MENU_OK)
        nbr++;

      pl = Format(ngettext("%i player ready", "%i players ready", nbr), nbr);
      if (initialized_players->GetText() != pl) {
        initialized_players->SetText(pl);
        msg_box->NewMessage(pl, c_red);
        if (Network::GetInstance()->GetNbPlayersConnected() ==
            Network::GetInstance()->GetNbPlayersWithState(Player::STATE_INITIALIZED)) {
          msg_box->NewMessage(_("The others are waiting for you! Wake up! :-)"), c_red);
          JukeBox::GetInstance()->Play("default", "menu/newcomer");
        }
        else if (Network::GetInstance()->GetNbPlayersConnected() == 0) {
          msg_box->NewMessage(_("You are alone. :-/"), c_red);
        }
      }
    }
  }
  else {
    close_menu = true;
  }
  ActionHandler * action_handler = ActionHandler::GetInstance();
  action_handler->ExecFrameLessActions();
}

void NetworkMenu::DelTeamCallback(const std::string& team_id)
{
  if (close_menu)
    return;

  // Called from the action handler
  team_box->DelTeamCallback(team_id);
}

void NetworkMenu::AddTeamCallback(const std::string& team_id)
{
  if (close_menu)
    return;

  team_box->AddTeamCallback(team_id);
}

void NetworkMenu::UpdateTeamCallback(const std::string& old_team_id, const std::string& team_id)
{
  if (close_menu)
    return;

  team_box->UpdateTeamCallback(old_team_id, team_id);
}

void NetworkMenu::ChangeMapCallback()
{
  if (close_menu)
    return;

  map_box->ChangeMapCallback();
}

void NetworkMenu::SetGameMasterCallback()
{
  // We are becoming game master, updating the menu...
  AppWarmux::GetInstance()->video->SetWindowCaption(std::string("WarMUX ") +
                                                    Constants::WARMUX_VERSION + " - " +
                                                    _("Master mode"));
  AddGameModeTab();
  mode_label->SetText(_("Master mode"));
  connected_players->SetVisible(true);
  initialized_players->SetVisible(true);
  map_box->AllowSelection();
  b_ok->SetVisible(true); // make sure OK button is available if we had already clicked it

  widgets.Pack();
  tabs->NeedRedrawing();
  RedrawMenu();

  waiting_for_server = false;
  msg_box->NewMessage(_("You are the new turn master! Learn about your new powers by typing /help"), c_red);
  msg_box->NewMessage(_("Wait until some opponent(s) connect!"), c_red);
}

void NetworkMenu::ReceiveMsgCallback(const std::string& msg, const Color& color)
{
  msg_box->NewMessage(msg, color);
}

void NetworkMenu::SetMapsCallback(const std::vector<uint>& list)
{
  map_box->ChangeMapListCallback(list);
}

Team * NetworkMenu::FindUnusedTeam(const std::string default_team_id)
{
  return team_box->FindUnusedTeam(default_team_id);
}

bool NetworkMenu::HasOpenTeamSlot()
{
  return team_box->HasOpenTeamSlot();
}

// to be call from NetworkMenu::WaitingForGameMaster()
void NetworkMenu::HandleEvent(const SDL_Event& evnt)
{
  if (!waiting_for_server) {
    Menu::HandleEvent(evnt);
    return;
  }

  if (evnt.type == SDL_QUIT) {
    Menu::mouse_cancel();
  } else if (evnt.type == SDL_KEYDOWN) {
    switch (evnt.key.keysym.sym) {
    case SDLK_ESCAPE:
      Menu::mouse_cancel();
      break;
    case SDLK_RETURN:
    case SDLK_KP_ENTER:
      msg_box->SendChatMsg();
      break;
    case SDLK_F10:
      AppWarmux::GetInstance()->video->ToggleFullscreen();
      break;
    default:
      widgets.SendKey(evnt.key.keysym);
      break;
    }
  } else if (evnt.type == SDL_MOUSEBUTTONUP) {

    int x=0, y=0;
    SDL_GetMouseState( &x, &y );
    Point2i mousePosition(x, y);

    if (b_cancel->Contains(mousePosition))
      Menu::mouse_cancel();
  }
}

void NetworkMenu::WaitingForGameMaster()
{
  Network::GetInstance()->SetState(WNet::NETWORK_MENU_OK);

  // warn the server that we have validated the menu
  Network::GetInstance()->SendNetworkState();

  waiting_for_server = true;
  b_ok->SetVisible(false);
  actions_buttons->NeedRedrawing();

  msg_box->NewMessage(_("Waiting for the server. All you can do is cancel or chat!"), c_red);

  widgets.SetFocusOn(msg_box->GetTextBox());

  do {
    HandleEvents();

    int x=0, y=0;
    SDL_GetMouseState( &x, &y );
    Point2i mousePosition(x, y);

    Display(mousePosition);
    Menu::Display(mousePosition);

  } while (Network::GetInstance()->IsConnected() &&
           Network::GetInstance()->GetState() == WNet::NETWORK_MENU_OK);

  waiting_for_server = false;
}