File: team.c

package info (click to toggle)
freeciv 2.1.5-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 95,924 kB
  • ctags: 20,829
  • sloc: ansic: 231,256; sh: 4,872; makefile: 2,867; python: 1,259; yacc: 318
file content (176 lines) | stat: -rw-r--r-- 5,694 bytes parent folder | download
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
/********************************************************************** 
 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 <config.h>
#endif

#include <assert.h>
#include <stdlib.h>

#include "fcintl.h"
#include "log.h"
#include "shared.h"
#include "support.h"

#include "game.h"
#include "player.h"
#include "team.h"

/**********************************************************************
   Functions for handling teams.
***********************************************************************/

static struct team teams[MAX_NUM_TEAMS];

/****************************************************************************
  Returns the id of a team given its name, or TEAM_NONE if 
  not found.
****************************************************************************/
struct team *team_find_by_name(const char *team_name)
{
  int index;

  assert(team_name != NULL);
  assert(NUM_TEAMS <= MAX_NUM_TEAMS);

  /* Can't use team_iterate here since it skips empty teams. */
  for (index = 0; index < NUM_TEAMS; index++) {
    struct team *pteam = team_get_by_id(index);

    if (mystrcasecmp(team_name, team_get_name_orig(pteam)) == 0) {
      return pteam;
    }
  }

  return NULL;
}

/****************************************************************************
  Returns pointer to a team given its id
****************************************************************************/
struct team *team_get_by_id(Team_type_id id)
{
  if (id < 0 || id >= NUM_TEAMS) {
    return NULL;
  }
  return &teams[id];
}

/****************************************************************************
  Set a player to a team.  Removes the previous team affiliation if
  necessary.
****************************************************************************/
void team_add_player(struct player *pplayer, struct team *pteam)
{
  assert(pplayer != NULL);
  assert(pteam != NULL && &teams[pteam->index] == pteam);

  freelog(LOG_DEBUG, "Adding player %d/%s to team %s.",
	  pplayer->player_no, pplayer->username,
	  pteam ? team_get_name(pteam) : "(none)");

  /* Remove the player from the old team, if any.  The player's team should
   * only be NULL for a few instants after the player was created; after
   * that they should automatically be put on a team.  So although we
   * check for a NULL case here this is only needed for that one
   * situation. */
  team_remove_player(pplayer);

  /* Put the player on the new team. */
  pplayer->team = pteam;
  
  pteam->players++;
  assert(pteam->players <= MAX_NUM_PLAYERS + MAX_NUM_BARBARIANS);
}

/****************************************************************************
  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)
{
  if (pplayer->team) {
    freelog(LOG_DEBUG, "Removing player %d/%s from team %s (%d)",
	    pplayer->player_no, pplayer->username,
	    pplayer->team ? team_get_name(pplayer->team) : "(none)",
	    pplayer->team ? pplayer->team->players : 0);
    pplayer->team->players--;
    assert(pplayer->team->players >= 0);
  }
  pplayer->team = NULL;
}

/****************************************************************************
  Return the translated name of the team.
****************************************************************************/
const char *team_get_name(const struct team *pteam)
{
  return _(team_get_name_orig(pteam));
}

/****************************************************************************
  Return the untranslated name of the team.
****************************************************************************/
const char *team_get_name_orig(const struct team *pteam)
{
  if (!pteam) {
    /* TRANS: missing value */
    return N_("(none)");
  }
  return game.info.team_names_orig[pteam->index];
}

/****************************************************************************
  Returns the most empty team available.  This is the team that should be
  assigned to a newly-created player.
****************************************************************************/
struct team *find_empty_team(void)
{
  Team_type_id i;
  struct team *pbest = NULL;

  /* Can't use teams_iterate here since it skips empty teams! */
  for (i = 0; i < NUM_TEAMS; i++) {
    struct team *pteam = team_get_by_id(i);

    if (!pbest || pbest->players > pteam->players) {
      pbest = pteam;
    }
    if (pbest->players == 0) {
      /* No need to keep looking. */
      return pbest;
    }
  }

  return pbest;
}

/****************************************************************************
  Initializes team structure
****************************************************************************/
void teams_init(void)
{
  Team_type_id i;

  for (i = 0; i < MAX_NUM_TEAMS; i++) {
    /* mark as unused */
    teams[i].index = i;

    teams[i].players = 0;
    player_research_init(&(teams[i].research));
  }
}