File: network_teams_selection_box.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 (377 lines) | stat: -rw-r--r-- 10,858 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
/******************************************************************************
 *  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
 ******************************************************************************
 *  Network teams selection box
 *****************************************************************************/

#include "menu/teams_selection_box.h"
#include "menu/network_teams_selection_box.h"
#include "menu/team_box.h"
#include "game/config.h"
#include "game/game_mode.h"
#include "gui/label.h"
#include "gui/picture_widget.h"
#include "gui/spin_button.h"
#include "gui/spin_button_picture.h"
#include "gui/text_box.h"
#include "include/action_handler.h"
#include "network/network.h"
#include "team/teams_list.h"
#include "team/team.h"
#include "tool/string_tools.h"

NetworkTeamsSelectionBox::NetworkTeamsSelectionBox(const Point2i &_size, bool w_border) :
  TeamsSelectionBox(_size, true, w_border)
{
  local_teams_nb->SetMaxValue(GameMode::GetInstance()->GetMaxTeamsPerNetworkPlayer());

  GetTeamsList().Clear();

  // No selected team(s) by default
  for (uint i=0; i<teams_selections.size(); i++) {
    teams_selections.at(i)->ClearTeam();
  }
}

Widget* NetworkTeamsSelectionBox::ClickUp(const Point2i &mousePosition, uint button)
{
  if (!Contains(mousePosition))
    return NULL;

  uint current_nb_teams = local_teams_nb->GetValue();

  if (local_teams_nb->ClickUp(mousePosition, button)){
    int delta =  local_teams_nb->GetValue() - current_nb_teams;
    local_teams_nb->SetValue(current_nb_teams);
    if (delta > 0) {
      RequestTeam();
    } else if (delta < 0){
      RequestTeamRemoval();
    }
  } else {
    Widget *w = (list_box) ? list_box->ClickUp(mousePosition, button)
                           : WidgetList::ClickUp(mousePosition, button);

    for (uint i=0; i<teams_selections.size() ; i++) {

      if (teams_selections[i]->Contains(mousePosition) &&
          teams_selections[i]->IsLocal()) {
        Widget * at = teams_selections[i];
        Rectanglei r(at->GetPosition(), Point2i(38,38));

        if (r.Contains(mousePosition)) {
          if (button == Mouse::BUTTON_LEFT() || button == SDL_BUTTON_WHEELDOWN) {
            NextTeam(i);
          } else if (button == Mouse::BUTTON_RIGHT() || button == SDL_BUTTON_WHEELUP) {
            PrevTeam(i);
          }
          return at;
        } else {
          Rectanglei r2(teams_selections.at(i)->GetPositionX(),
                        teams_selections.at(i)->GetPositionY() + 39,
                        38,
                        30);
          if (r2.Contains(mousePosition)) {
            teams_selections[i]->SwitchPlayerType();
            return at;
          }
        }

        return w;
      }
    }

    return w;
  }

  return NULL;
}

const std::string NetworkTeamsSelectionBox::GetLocalPlayerName()
{
  std::string result;
#ifdef WIN32
  // The username might be in NLS !
  char* name = LocaleToUTF8(getenv("USERNAME"));
  result = name;
  delete[] name;
#elif defined(ANDROID)
  result = "Android";
#elif defined(__SYMBIAN32__)
  result = "Symbian";
#else
  result = getenv("USER");
#endif
  return result;
}

void NetworkTeamsSelectionBox::PrevTeam(uint i)
{
  ASSERT(teams_selections.at(i)->GetTeam() != NULL);

  bool to_continue;
  Team* tmp;
  int previous_index = -1, index;
  uint max_teams = GameMode::GetInstance()->GetMaxTeamsPerNetworkPlayer();

  GetTeamsList().FindById(teams_selections.at(i)->GetTeam()->GetId(), previous_index);

  index = previous_index-1;

  do {
    to_continue = false;

    // select the last team if we are outside list
    if (index < 0)
      index = int(GetTeamsList().full_list.size())-1;

    // Get the team at current index
    tmp = GetTeamsList().FindByIndex(index);

    // Check if that team is already selected
    for (uint j = 0; j < max_teams; j++) {
      if (j!= i && tmp == teams_selections.at(j)->GetTeam()) {
        index--;
        to_continue = true;
        break;
      }
    }

    // We have found a team which is not selected
    if (tmp != NULL && !to_continue) {
      SetLocalTeam(i, *tmp);
    }
  } while (index != previous_index && to_continue);
}

void NetworkTeamsSelectionBox::NextTeam(uint i)
{
  bool to_continue;
  Team* tmp;
  int previous_index = -1, index;
  uint max_teams = GameMode::GetInstance()->GetMaxTeamsPerNetworkPlayer();

  if (teams_selections.at(i)->GetTeam())
    GetTeamsList().FindById(teams_selections.at(i)->GetTeam()->GetId(), previous_index);

  index = previous_index+1;

  do {
    to_continue = false;

    // select the first team if we are outside list
    if (index >= int(GetTeamsList().full_list.size()))
      index = 0;

    // Get the team at current index
    tmp = GetTeamsList().FindByIndex(index);

    // Check if that team is already selected
    for (uint j = 0; j < max_teams; j++) {
      if (j!= i && tmp == teams_selections.at(j)->GetTeam()) {
        index++;
        to_continue = true;
        break;
      }
    }

    // We have found a team which is not selected
    if (tmp != NULL && !to_continue) {
      if (teams_selections.at(i)->GetTeam() == NULL)
        tmp->SetPlayerName(GetLocalPlayerName());
      SetLocalTeam(i, *tmp);
    }
  } while (index != previous_index && to_continue);
}

void NetworkTeamsSelectionBox::RequestTeam()
{
  ConfigTeam team_config;
  team_config.id = (*(GetTeamsList().full_list.begin()))->GetId();
  team_config.player_name = GetLocalPlayerName();
  team_config.nb_characters = GameMode::GetInstance()->nb_characters;
  team_config.ai = NO_AI_NAME;
  ActionHandler::GetInstance()->NewRequestTeamAction(team_config);
}

void NetworkTeamsSelectionBox::RequestTeamRemoval()
{
  int player_id = int(Network::GetInstance()->GetPlayer().GetId());
  Action* a = new Action(Action::ACTION_GAME_REQUEST_TEAM_REMOVAL);
  a->Push(player_id);
  ActionHandler::GetInstance()->NewAction(a);
}

bool NetworkTeamsSelectionBox::IsSelected(Team * team)
{
  uint max_teams = GameMode::GetInstance()->GetMaxTeamsPerNetworkPlayer();
  for (uint j = 0; j < max_teams ; j++) {
    if (team == teams_selections.at(j)->GetTeam()) {
      return true;
    }
  }
  return false;
}

bool NetworkTeamsSelectionBox::HasOpenTeamSlot()
{
  uint max_teams = GameMode::GetInstance()->GetMaxTeamsPerNetworkPlayer();
  for (uint j = 0; j < max_teams; j++) {
    if (!teams_selections.at(j)->GetTeam()) {
      return true;
    }
  }
  return false;
}

Team * NetworkTeamsSelectionBox::FindUnusedTeam(const std::string default_team_id)
{
  int index;
  Team * team = GetTeamsList().FindById(default_team_id, index);
  int team_count = GetTeamsList().full_list.size();
  int checked_teams = 0;
  while((!team || IsSelected(team)) && checked_teams <= team_count) {
    index++;
    if (index >= team_count)
      index = 0;
    team = GetTeamsList().FindByIndex(index);
    checked_teams++;
  }
  ASSERT(team);
  return team;
}

void NetworkTeamsSelectionBox::SetLocalTeam(uint i, Team& team)
{
  team.SetRemote(false);

  if (teams_selections.at(i)->GetTeam()) {
    teams_selections.at(i)->SetTeam(team, false);
  }
}

void NetworkTeamsSelectionBox::AddTeamCallback(const std::string& team_id)
{
  for (uint i=0; i < teams_selections.size(); i++) {
    if (!teams_selections.at(i)->GetTeam()) {
      int index;
      /* FindPlayingById should be faster */
      Team * tmp = GetTeamsList().FindById(team_id, index);

      teams_selections.at(i)->SetTeam(*tmp, true);
      break;
    }
  }

  // Count the current number of local teams
  uint nb_local_teams=0;
  for (uint i=0; i < teams_selections.size(); i++) {
    if (teams_selections.at(i)->GetTeam() && teams_selections.at(i)->IsLocal()) {
      nb_local_teams++;
    }
  }

  local_teams_nb->SetValue(nb_local_teams);
  UpdateNbTeams();
}

void NetworkTeamsSelectionBox::UpdateTeamCallback(const std::string& old_team_id,
                                                  const std::string& team_id)
{
  for (uint i=0; i < teams_selections.size(); i++) {
    if (teams_selections.at(i)->GetTeam() &&
        teams_selections.at(i)->GetTeam()->GetId() == old_team_id) {
      int index = 0;
      Team * tmp = GetTeamsList().FindById(team_id, index);

      // Force refresh of information
      teams_selections.at(i)->SetTeam(*tmp, true);
      break;
    }
  }
}

void NetworkTeamsSelectionBox::DelTeamCallback(const std::string& team_id)
{
  for (uint i=0; i < teams_selections.size(); i++) {
    if (teams_selections.at(i)->GetTeam() &&
        teams_selections.at(i)->GetTeam()->GetId() == team_id) {

      teams_selections.at(i)->ClearTeam();
      break;
    }
  }

  // Count the current number of local teams
  uint nb_local_teams=0;
  for (uint i=0; i < teams_selections.size(); i++) {
    if (teams_selections.at(i)->GetTeam() &&
        teams_selections.at(i)->IsLocal()) {
      nb_local_teams++;
    }
  }
  local_teams_nb->SetValue(nb_local_teams);
  UpdateNbTeams();
}


void NetworkTeamsSelectionBox::ValidTeamsSelection()
{
  std::list<uint> selection;

  uint nb_teams=0;
  for (uint i=0; i < teams_selections.size(); i++) {
    if (teams_selections.at(i)->GetTeam())
      nb_teams++;
  }

  if (nb_teams >= 2) {
    std::list<uint> selection;

    for (uint i=0; i < teams_selections.size(); i++) {
      if (teams_selections.at(i)->GetTeam()) {
        int index = -1;
        teams_selections.at(i)->ValidOptions();
        GetTeamsList().FindById(teams_selections.at(i)->GetTeam()->GetId(), index);
        if (index > -1)
          selection.push_back(uint(index));
      }
    }
    GetTeamsList().ChangeSelection (selection);
  }

  Config::GetInstance()->SetNetworkLocalTeams();
}

void NetworkTeamsSelectionBox::UpdateNbTeams()
{
  // If we are not running this special widget, this is useless
  if (!list_box)
    return;

  uint nb_teams = 0;
  for (uint i=0; i < teams_selections.size(); i++) {
    if (teams_selections.at(i)->GetTeam())
      nb_teams++;
  }

  list_box->SetNbTeams(nb_teams);
  // Fix scrollbox not being redrawn: this is performed *after* the
  // screen has been refreshed, because of the callbacks
  list_box->NeedRedrawing();
}