Freeciv-3.3
Loading...
Searching...
No Matches
team.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 2005 - The Freeciv Project
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#include <stdlib.h>
19
20/* utility */
21#include "fcintl.h"
22#include "log.h"
23#include "shared.h"
24#include "support.h"
25
26/* common */
27#include "game.h"
28#include "player.h"
29#include "team.h"
30
31struct team_slot {
32 struct team *team;
33 char *defined_name; /* Defined by the ruleset. */
34 char *rule_name; /* Usable untranslated name. */
35#ifdef FREECIV_ENABLE_NLS
36 char *name_translation; /* Translated name. */
37#endif
38};
39
40struct team {
42 struct team_slot *slot;
43};
44
45static struct {
49
50/************************************************************************/
54{
55 int i;
56
57 /* Init team slots and names. */
58 team_slots.slots = fc_calloc(team_slot_count(), sizeof(*team_slots.slots));
59 /* Can't use the defined functions as the needed data will be
60 * defined here. */
61 for (i = 0; i < team_slot_count(); i++) {
62 struct team_slot *tslot = team_slots.slots + i;
63
64 tslot->team = NULL;
65 tslot->defined_name = NULL;
66 tslot->rule_name = NULL;
67#ifdef FREECIV_ENABLE_NLS
68 tslot->name_translation = NULL;
69#endif
70 }
71 team_slots.used_slots = 0;
72}
73
74/************************************************************************/
78{
79 return (team_slots.slots != NULL);
80}
81
82/************************************************************************/
86{
88 if (NULL != tslot->team) {
89 team_destroy(tslot->team);
90 }
91 if (NULL != tslot->defined_name) {
92 free(tslot->defined_name);
93 }
94 if (NULL != tslot->rule_name) {
95 free(tslot->rule_name);
96 }
97#ifdef FREECIV_ENABLE_NLS
98 if (NULL != tslot->name_translation) {
99 free(tslot->name_translation);
100 }
101#endif /* FREECIV_ENABLE_NLS */
103 free(team_slots.slots);
104 team_slots.slots = NULL;
105
106 team_slots.used_slots = 0;
107}
108
109/************************************************************************/
113{
114 return (MAX_NUM_TEAM_SLOTS);
115}
116
117/************************************************************************/
121{
122 return team_slots.slots;
123}
124
125/************************************************************************/
129{
130 tslot++;
131 return (tslot < team_slots.slots + team_slot_count() ? tslot : NULL);
132}
133
134
135/************************************************************************/
139{
142
143 return tslot - team_slots.slots;
144}
145
146/************************************************************************/
151{
154
155 return tslot->team;
156}
157
158/************************************************************************/
163{
164 /* No team slot available, if the game is not initialised. */
165 if (!team_slots_initialised()) {
166 return FALSE;
167 }
168
169 return NULL != tslot->team;
170}
171
172/************************************************************************/
175struct team_slot *team_slot_by_number(int team_id)
176{
178 || !(0 <= team_id && team_id < team_slot_count())) {
179 return NULL;
180 }
181
182 return team_slots.slots + team_id;
183}
184
185/************************************************************************/
189struct team_slot *team_slot_by_rule_name(const char *team_name)
190{
191 fc_assert_ret_val(team_name != NULL, NULL);
192
194 const char *tname = team_slot_rule_name(tslot);
195
196 if (NULL != tname && 0 == fc_strcasecmp(tname, team_name)) {
197 return tslot;
198 }
200
201 return NULL;
202}
203
204/************************************************************************/
208{
209 char buf[MAX_LEN_NAME];
210
211 fc_assert(NULL == tslot->defined_name);
212 fc_assert(NULL == tslot->rule_name);
213#ifdef FREECIV_ENABLE_NLS
214 fc_assert(NULL == tslot->name_translation);
215#endif /* FREECIV_ENABLE_NLS */
216
217 fc_snprintf(buf, sizeof(buf), "Team %d", team_slot_index(tslot) + 1);
218 tslot->rule_name = fc_strdup(buf);
219
220#ifdef FREECIV_ENABLE_NLS
221 fc_snprintf(buf, sizeof(buf), _("Team %d"), team_slot_index(tslot) + 1);
222 tslot->name_translation = fc_strdup(buf);
223#endif /* FREECIV_ENABLE_NLS */
224
225 log_verbose("No name defined for team %d! Creating a default name: %s.",
226 team_slot_index(tslot), tslot->rule_name);
227}
228
229/************************************************************************/
233const char *team_slot_rule_name(const struct team_slot *tslot)
234{
237
238 if (NULL == tslot->rule_name) {
239 /* Get the team slot as changeable (not _const_) struct. */
240 struct team_slot *changeable
243 return changeable->rule_name;
244 }
245
246 return tslot->rule_name;
247}
248
249/************************************************************************/
254{
257
258#ifdef FREECIV_ENABLE_NLS
259 if (NULL == tslot->name_translation) {
260 /* Get the team slot as changeable (not _const_) struct. */
261 struct team_slot *changeable
263
265 return changeable->name_translation;
266 }
267
268 return tslot->name_translation;
269#else /* FREECIV_ENABLE_NLS */
271#endif /* FREECIV_ENABLE_NLS */
272}
273
274/************************************************************************/
278const char *team_slot_defined_name(const struct team_slot *tslot)
279{
282
283 return tslot->defined_name;
284}
285
286/************************************************************************/
290 const char *team_name)
291{
294 fc_assert_ret(NULL != team_name);
295
296 if (NULL != tslot->defined_name) {
297 free(tslot->defined_name);
298 }
299 tslot->defined_name = fc_strdup(team_name);
300
301 if (NULL != tslot->rule_name) {
302 free(tslot->rule_name);
303 }
304 tslot->rule_name = fc_strdup(Qn_(team_name));
305
306#ifdef FREECIV_ENABLE_NLS
307 if (NULL != tslot->name_translation) {
308 free(tslot->name_translation);
309 }
310 tslot->name_translation = fc_strdup(Q_(team_name));
311#endif /* FREECIV_ENABLE_NLS */
312}
313
314/************************************************************************/
318struct team *team_new(struct team_slot *tslot)
319{
320 struct team *pteam;
321
323
324 if (tslot == nullptr) {
326 if (!team_slot_is_used(aslot)) {
327 tslot = aslot;
328 break;
329 }
331
332 if (tslot == nullptr) {
333 return nullptr;
334 }
335 } else if (tslot->team != nullptr) {
336 return tslot->team;
337 }
338
339 /* Now create the team. */
340 log_debug("Create team for slot %d.", team_slot_index(tslot));
341 pteam = fc_calloc(1, sizeof(*pteam));
342 pteam->slot = tslot;
343 tslot->team = pteam;
344
345 /* Set default values. */
346 pteam->plrlist = player_list_new();
347
348 /* Increase number of teams. */
349 team_slots.used_slots++;
350
351 return pteam;
352}
353
354/************************************************************************/
358{
359 struct team_slot *tslot;
360
362 fc_assert(0 == player_list_size(pteam->plrlist));
363
364 tslot = pteam->slot;
365 fc_assert(tslot->team == pteam);
366
367 player_list_destroy(pteam->plrlist);
368 free(pteam);
369 tslot->team = NULL;
370 team_slots.used_slots--;
371}
372
373/************************************************************************/
376int team_count(void)
377{
378 return team_slots.used_slots;
379}
380
381/************************************************************************/
384int team_index(const struct team *pteam)
385{
386 return team_number(pteam);
387}
388
389/************************************************************************/
392int team_number(const struct team *pteam)
393{
395 return team_slot_index(pteam->slot);
396}
397
398/************************************************************************/
401struct team *team_by_number(const int team_id)
402{
403 const struct team_slot *tslot = team_slot_by_number(team_id);
404
405 return (NULL != tslot ? team_slot_get_team(tslot) : NULL);
406}
407
408/************************************************************************/
411const char *team_rule_name(const struct team *pteam)
412{
414
415 return team_slot_rule_name(pteam->slot);
416}
417
418/************************************************************************/
421const char *team_name_translation(const struct team *pteam)
422{
424
425 return team_slot_name_translation(pteam->slot);
426}
427
428/************************************************************************/
433int team_pretty_name(const struct team *pteam, char *buf, size_t buf_len)
434{
435 if (NULL != pteam) {
436 if (NULL != pteam->slot->defined_name) {
437 /* TRANS: %s is ruleset-chosen team name (e.g. "Red") */
438 return fc_snprintf(buf, buf_len, _("team %s"),
440 } else {
441 /* The generated name already contains word "Team" so don't repeat it.
442 * Also note that number of teams may have changed since the name
443 * was originally generated, so the number in it can be
444 * something else than current team_number(). */
446 }
447 }
448
449 /* No need to translate, it's an error. */
450 fc_strlcpy(buf, "(null team)", buf_len);
451 return -1;
452}
453
454/************************************************************************/
457const struct player_list *team_members(const struct team *pteam)
458{
460
461 return pteam->plrlist;
462}
463
464/************************************************************************/
468bool team_add_player(struct player *pplayer, struct team *pteam)
469{
470 fc_assert_ret_val(pplayer != nullptr, FALSE);
471
472 if (pteam == NULL) {
474 } else if (pteam == pplayer->team) {
475 /* It is the team of the player. */
476 return TRUE;
477 }
478
479 if (pteam == nullptr) {
480 return FALSE;
481 }
482
483 log_debug("Adding player %d/%s to team %s.", player_number(pplayer),
484 pplayer->username, team_rule_name(pteam));
485
486 /* Remove the player from the old team, if any. */
487 team_remove_player(pplayer);
488
489 /* Put the player on the new team. */
490 pplayer->team = pteam;
491 player_list_append(pteam->plrlist, pplayer);
492
493 return TRUE;
494}
495
496/************************************************************************/
503void team_remove_player(struct player *pplayer)
504{
505 struct team *pteam;
506
507 if (pplayer->team != nullptr) {
508 pteam = pplayer->team;
509
510 log_debug("Removing player %d/%s from team %s (%d)",
511 player_number(pplayer), player_name(pplayer),
513 player_list_remove(pteam->plrlist, pplayer);
514
515 if (player_list_size(pteam->plrlist) == 0) {
517 }
518 }
519
520 pplayer->team = NULL;
521}
char * incite_cost
Definition comments.c:76
#define MAX_LEN_NAME
Definition fc_types.h:67
#define Q_(String)
Definition fcintl.h:70
#define _(String)
Definition fcintl.h:67
#define Qn_(String)
Definition fcintl.h:89
#define fc_assert_ret(condition)
Definition log.h:192
#define log_verbose(message,...)
Definition log.h:110
#define fc_assert(condition)
Definition log.h:177
#define fc_assert_ret_val(condition, val)
Definition log.h:195
#define log_debug(message,...)
Definition log.h:116
#define fc_calloc(n, esz)
Definition mem.h:38
#define fc_strdup(str)
Definition mem.h:43
int player_number(const struct player *pplayer)
Definition player.c:837
const char * player_name(const struct player *pplayer)
Definition player.c:895
char username[MAX_LEN_NAME]
Definition player.h:252
struct team * team
Definition player.h:261
char * defined_name
Definition team.c:33
char * rule_name
Definition team.c:34
struct team * team
Definition team.c:32
Definition team.c:40
struct team_slot * slot
Definition team.c:42
struct player_list * plrlist
Definition team.c:41
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
size_t fc_strlcpy(char *dest, const char *src, size_t n)
Definition support.c:777
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:186
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
int team_count(void)
Definition team.c:376
int team_index(const struct team *pteam)
Definition team.c:384
struct team * team_by_number(const int team_id)
Definition team.c:401
const char * team_name_translation(const struct team *pteam)
Definition team.c:421
struct team_slot * team_slot_by_number(int team_id)
Definition team.c:175
void team_slots_free(void)
Definition team.c:85
const char * team_rule_name(const struct team *pteam)
Definition team.c:411
static void team_slot_create_default_name(struct team_slot *tslot)
Definition team.c:207
struct team * team_slot_get_team(const struct team_slot *tslot)
Definition team.c:150
int used_slots
Definition team.c:47
const char * team_slot_name_translation(const struct team_slot *tslot)
Definition team.c:253
static struct @82 team_slots
struct team_slot * team_slot_first(void)
Definition team.c:120
const char * team_slot_defined_name(const struct team_slot *tslot)
Definition team.c:278
int team_number(const struct team *pteam)
Definition team.c:392
void team_destroy(struct team *pteam)
Definition team.c:357
bool team_slots_initialised(void)
Definition team.c:77
struct team_slot * team_slot_next(struct team_slot *tslot)
Definition team.c:128
bool team_add_player(struct player *pplayer, struct team *pteam)
Definition team.c:468
struct team * team_new(struct team_slot *tslot)
Definition team.c:318
int team_pretty_name(const struct team *pteam, char *buf, size_t buf_len)
Definition team.c:433
int team_slot_index(const struct team_slot *tslot)
Definition team.c:138
struct team_slot * team_slot_by_rule_name(const char *team_name)
Definition team.c:189
const struct player_list * team_members(const struct team *pteam)
Definition team.c:457
void team_slots_init(void)
Definition team.c:53
void team_remove_player(struct player *pplayer)
Definition team.c:503
int team_slot_count(void)
Definition team.c:112
bool team_slot_is_used(const struct team_slot *tslot)
Definition team.c:162
void team_slot_set_defined_name(struct team_slot *tslot, const char *team_name)
Definition team.c:289
const char * team_slot_rule_name(const struct team_slot *tslot)
Definition team.c:233
struct team_slot * slots
Definition team.c:46
#define team_slots_iterate_end
Definition team.h:77
#define team_slots_iterate(_tslot)
Definition team.h:72
#define MAX_NUM_TEAM_SLOTS
Definition team.h:27