File: team.c

package info (click to toggle)
freeciv 2.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 212,508 kB
  • sloc: ansic: 443,831; cpp: 29,541; sh: 7,982; makefile: 7,886; python: 1,933; xml: 945
file content (513 lines) | stat: -rw-r--r-- 16,329 bytes parent folder | download | duplicates (2)
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
/********************************************************************** 
 Freeciv - Copyright (C) 2005 - The Freeciv Project
   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, 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.
***********************************************************************/

#ifdef HAVE_CONFIG_H
#include <fc_config.h>
#endif

#include <stdlib.h>

/* utility */
#include "fcintl.h"
#include "log.h"
#include "shared.h"
#include "support.h"

/* common */
#include "game.h"
#include "player.h"
#include "team.h"

struct team_slot {
  struct team *team;
  char *defined_name;                   /* Defined by the ruleset. */
  char *rule_name;                      /* Usable untranslated name. */
#ifdef ENABLE_NLS
  char *name_translation;               /* Translated name. */
#endif
};

struct team {
  struct player_list *plrlist;
  struct team_slot *slot;
};

static struct {
  struct team_slot *slots;
  int used_slots;
} team_slots;

/****************************************************************************
  Initialise all team slots.
****************************************************************************/
void team_slots_init(void)
{
  int i;

  /* Init team slots and names. */
  team_slots.slots = fc_calloc(team_slot_count(), sizeof(*team_slots.slots));
  /* Can't use the defined functions as the needed data will be
   * defined here. */
  for (i = 0; i < team_slot_count(); i++) {
    struct team_slot *tslot = team_slots.slots + i;

    tslot->team = NULL;
    tslot->defined_name = NULL;
    tslot->rule_name = NULL;
#ifdef ENABLE_NLS
    tslot->name_translation = NULL;
#endif
  }
  team_slots.used_slots = 0;
}

/****************************************************************************
  Returns TRUE if the team slots have been initialized.
****************************************************************************/
bool team_slots_initialised(void)
{
  return (team_slots.slots != NULL);
}

/****************************************************************************
  Remove all team slots.
****************************************************************************/
void team_slots_free(void)
{
  team_slots_iterate(tslot) {
    if (NULL != tslot->team) {
      team_destroy(tslot->team);
    }
    if (NULL != tslot->defined_name) {
      free(tslot->defined_name);
    }
    if (NULL != tslot->rule_name) {
      free(tslot->rule_name);
    }
#ifdef ENABLE_NLS
    if (NULL != tslot->name_translation) {
      free(tslot->name_translation);
    }
#endif
  } team_slots_iterate_end;
  free(team_slots.slots);
  team_slots.slots = NULL;

  team_slots.used_slots = 0;
}

/****************************************************************************
  Returns the total number of team slots (including used slots).
****************************************************************************/
int team_slot_count(void)
{
  return (MAX_NUM_TEAM_SLOTS);
}

/****************************************************************************
  Returns the first team slot.
****************************************************************************/
struct team_slot *team_slot_first(void)
{
  return team_slots.slots;
}

/****************************************************************************
  Returns the next team slot.
****************************************************************************/
struct team_slot *team_slot_next(struct team_slot *tslot)
{
  tslot++;
  return (tslot < team_slots.slots + team_slot_count() ? tslot : NULL);
}


/****************************************************************************
  Returns the index of the team slots.
****************************************************************************/
int team_slot_index(const struct team_slot *tslot)
{
  fc_assert_ret_val(team_slots_initialised(), -1);
  fc_assert_ret_val(tslot != NULL, -1);

  return tslot - team_slots.slots;
}

/****************************************************************************
  Returns the team corresponding to the slot. If the slot is not used, it
  will return NULL. See also team_slot_is_used().
****************************************************************************/
struct team *team_slot_get_team(const struct team_slot *tslot)
{
  fc_assert_ret_val(team_slots_initialised(), NULL);
  fc_assert_ret_val(tslot != NULL, NULL);

  return tslot->team;
}

/****************************************************************************
  Returns TRUE is this slot is "used" i.e. corresponds to a
  valid, initialized team that exists in the game.
****************************************************************************/
bool team_slot_is_used(const struct team_slot *tslot)
{
  /* No team slot available, if the game is not initialised. */
  if (!team_slots_initialised()) {
    return FALSE;
  }

  return NULL != tslot->team;
}

/****************************************************************************
  Return the possibly unused and uninitialized team slot.
****************************************************************************/
struct team_slot *team_slot_by_number(int team_id)
{
  if (!team_slots_initialised()
      || !(0 <= team_id && team_id < team_slot_count())) {
    return NULL;
  }

  return team_slots.slots + team_id;
}

/****************************************************************************
  Does a linear search for a (defined) team name. Returns NULL when none
  match.
****************************************************************************/
struct team_slot *team_slot_by_rule_name(const char *team_name)
{
  fc_assert_ret_val(team_name != NULL, NULL);

  team_slots_iterate(tslot) {
    const char *tname = team_slot_rule_name(tslot);

    if (NULL != tname && 0 == fc_strcasecmp(tname, team_name)) {
      return tslot;
    }
  } team_slots_iterate_end;

  return NULL;
}

/****************************************************************************
  Creates a default name for this team slot.
****************************************************************************/
static inline void team_slot_create_default_name(struct team_slot *tslot)
{
  char buf[MAX_LEN_NAME];

  fc_assert(NULL == tslot->defined_name);
  fc_assert(NULL == tslot->rule_name);
#ifdef ENABLE_NLS
  fc_assert(NULL == tslot->name_translation);
#endif

  fc_snprintf(buf, sizeof(buf), "Team %d", team_slot_index(tslot));
  tslot->rule_name = fc_strdup(buf);

#ifdef ENABLE_NLS
  fc_snprintf(buf, sizeof(buf), _("Team %d"), team_slot_index(tslot));
  tslot->name_translation = fc_strdup(buf);
#endif

  log_verbose("No name defined for team %d! Creating a default name: %s.",
              team_slot_index(tslot), tslot->rule_name);
}

/****************************************************************************
  Returns the name (untranslated) of the slot. Creates a default one if it
  doesn't exist currently.
****************************************************************************/
const char *team_slot_rule_name(const struct team_slot *tslot)
{
  fc_assert_ret_val(team_slots_initialised(), NULL);
  fc_assert_ret_val(NULL != tslot, NULL);

  if (NULL == tslot->rule_name) {
    /* Get the team slot as changeable (not _const_) struct. */
    struct team_slot *changeable
      = team_slot_by_number(team_slot_index(tslot));
    team_slot_create_default_name(changeable);
    return changeable->rule_name;
  }

  return tslot->rule_name;
}

/****************************************************************************
  Returns the name (translated) of the slot. Creates a default one if it
  doesn't exist currently.
****************************************************************************/
const char *team_slot_name_translation(const struct team_slot *tslot)
{
#ifdef ENABLE_NLS
  fc_assert_ret_val(team_slots_initialised(), NULL);
  fc_assert_ret_val(NULL != tslot, NULL);

  if (NULL == tslot->name_translation) {
    /* Get the team slot as changeable (not _const_) struct. */
    struct team_slot *changeable
      = team_slot_by_number(team_slot_index(tslot));
    team_slot_create_default_name(changeable);
    return changeable->name_translation;
  }

  return tslot->name_translation;
#else
  return team_slot_rule_name(tslot);
#endif /* ENABLE_NLS */
}

/****************************************************************************
  Returns the name defined in the ruleset for this slot. It may return NULL
  if the ruleset didn't defined a such name.
****************************************************************************/
const char *team_slot_defined_name(const struct team_slot *tslot)
{
  fc_assert_ret_val(team_slots_initialised(), NULL);
  fc_assert_ret_val(NULL != tslot, NULL);

  return tslot->defined_name;
}

/****************************************************************************
  Set the name defined in the ruleset for this slot.
****************************************************************************/
void team_slot_set_defined_name(struct team_slot *tslot,
                                const char *team_name)
{
  fc_assert_ret(team_slots_initialised());
  fc_assert_ret(NULL != tslot);
  fc_assert_ret(NULL != team_name);

  if (NULL != tslot->defined_name) {
    free(tslot->defined_name);
  }
  tslot->defined_name = fc_strdup(team_name);

  if (NULL != tslot->rule_name) {
    free(tslot->rule_name);
  }
  tslot->rule_name = fc_strdup(Qn_(team_name));

#ifdef ENABLE_NLS
  if (NULL != tslot->name_translation) {
    free(tslot->name_translation);
  }
  tslot->name_translation = fc_strdup(Q_(team_name));
#endif
}


/****************************************************************************
  Creates a new team for the slot. If slot is NULL, it will lookup to a
  free slot. If the slot already used, then just return the team.
****************************************************************************/
struct team *team_new(struct team_slot *tslot)
{
  struct team *pteam;

  fc_assert_ret_val(team_slots_initialised(), NULL);

  if (NULL == tslot) {
    team_slots_iterate(aslot) {
      if (!team_slot_is_used(aslot)) {
        tslot = aslot;
        break;
      }
    } team_slots_iterate_end;

    fc_assert_ret_val(NULL != tslot, NULL);
  } else if (NULL != tslot->team) {
    return tslot->team;
  }

  /* Now create the team. */
  log_debug("Create team for slot %d.", team_slot_index(tslot));
  pteam = fc_calloc(1, sizeof(*pteam));
  pteam->slot = tslot;
  tslot->team = pteam;

  /* Set default values. */
  pteam->plrlist = player_list_new();

  /* Increase number of teams. */
  team_slots.used_slots++;

  return pteam;
}

/****************************************************************************
  Destroys a team.
****************************************************************************/
void team_destroy(struct team *pteam)
{
  struct team_slot *tslot;

  fc_assert_ret(team_slots_initialised());
  fc_assert_ret(NULL != pteam);
  fc_assert(0 == player_list_size(pteam->plrlist));

  tslot = pteam->slot;
  fc_assert(tslot->team == pteam);

  player_list_destroy(pteam->plrlist);
  free(pteam);
  tslot->team = NULL;
  team_slots.used_slots--;
}

/****************************************************************************
  Return the current number of teams.
****************************************************************************/
int team_count(void)
{
  return team_slots.used_slots;
}

/****************************************************************************
  Return the team index.
****************************************************************************/
int team_index(const struct team *pteam)
{
  return team_number(pteam);
}

/****************************************************************************
  Return the team index/number/id.
****************************************************************************/
int team_number(const struct team *pteam)
{
  fc_assert_ret_val(NULL != pteam, -1);
  return team_slot_index(pteam->slot);
}

/****************************************************************************
  Return struct team pointer for the given team index.
****************************************************************************/
struct team *team_by_number(const int team_id)
{
  const struct team_slot *tslot = team_slot_by_number(team_id);

  return (NULL != tslot ? team_slot_get_team(tslot) : NULL);
}

/****************************************************************************
  Returns the name (untranslated) of the team.
****************************************************************************/
const char *team_rule_name(const struct team *pteam)
{
  fc_assert_ret_val(NULL != pteam, NULL);

  return team_slot_rule_name(pteam->slot);
}

/****************************************************************************
  Returns the name (translated) of the team.
****************************************************************************/
const char *team_name_translation(const struct team *pteam)
{
  fc_assert_ret_val(NULL != pteam, NULL);

  return team_slot_name_translation(pteam->slot);
}

/****************************************************************************
  Set in 'buf' the name of the team 'pteam' in a format like
  "team <team_name>". To avoid to see "team Team 0", it only prints the
  the team number when the name of this team is not defined in the ruleset.
****************************************************************************/
int team_pretty_name(const struct team *pteam, char *buf, size_t buf_len)
{
  if (NULL != pteam) {
    if (NULL != pteam->slot->defined_name) {
      /* TRANS: %s is ruleset-chosen team name (e.g. "Red") */
      return fc_snprintf(buf, buf_len, _("team %s"),
                         team_slot_name_translation(pteam->slot));
    } else {
      return fc_snprintf(buf, buf_len, _("team %d"), team_number(pteam));
    }
  }

  /* No need to translate, it's an error. */
  fc_strlcpy(buf, "(null team)", buf_len);
  return -1;
}

/****************************************************************************
  Returns the member list of the team.
****************************************************************************/
const struct player_list *team_members(const struct team *pteam)
{
  fc_assert_ret_val(NULL != pteam, NULL);

  return pteam->plrlist;
}

/****************************************************************************
  Set a player to a team.  Removes the previous team affiliation if
  necessary.
****************************************************************************/
void team_add_player(struct player *pplayer, struct team *pteam)
{
  fc_assert_ret(pplayer != NULL);

  if (pteam == NULL) {
    pteam = team_new(NULL);
  }

  fc_assert_ret(pteam != NULL);

  if (pteam == pplayer->team) {
    /* It is the team of the player. */
    return;
  }

  log_debug("Adding player %d/%s to team %s.", player_number(pplayer),
            pplayer->username, team_rule_name(pteam));

  /* Remove the player from the old team, if any. */
  team_remove_player(pplayer);

  /* Put the player on the new team. */
  pplayer->team = pteam;
  player_list_append(pteam->plrlist, pplayer);
}

/****************************************************************************
  Remove the player from the team.  This should only be called when deleting
  a player; since every player must always be on a team.

  Note in some very rare cases a player may not be on a team.  It's safe
  to call this function anyway.
****************************************************************************/
void team_remove_player(struct player *pplayer)
{
  struct team *pteam;

  if (pplayer->team) {
    pteam = pplayer->team;

    log_debug("Removing player %d/%s from team %s (%d)",
              player_number(pplayer), player_name(pplayer),
              team_rule_name(pteam), player_list_size(pteam->plrlist));
    player_list_remove(pteam->plrlist, pplayer);

    if (player_list_size(pteam->plrlist) == 0) {
      team_destroy(pteam);
    }
  }
  pplayer->team = NULL;
}