[Freeciv-Dev] (PR#13044) new files for teams
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
<URL: http://bugs.freeciv.org/Ticket/Display.html?id=13044 >
This patch adds two new files team.[ch] for holding the team structures.
This code really shouldn't be lumped in with nations.
-jason
Index: common/Makefile.am
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/Makefile.am,v
retrieving revision 1.58
diff -u -r1.58 Makefile.am
--- common/Makefile.am 11 May 2005 19:31:38 -0000 1.58
+++ common/Makefile.am 11 May 2005 20:27:43 -0000
@@ -54,6 +54,8 @@
spaceship.h \
specialist.c \
specialist.h \
+ team.c \
+ team.h \
tech.c \
tech.h \
terrain.c \
Index: common/nation.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/nation.c,v
retrieving revision 1.48
diff -u -r1.48 nation.c
--- common/nation.c 5 May 2005 18:32:51 -0000 1.48
+++ common/nation.c 11 May 2005 20:27:43 -0000
@@ -12,7 +12,7 @@
***********************************************************************/
/**********************************************************************
- Functions for handling the nations and teams.
+ Functions for handling the nations.
***********************************************************************/
#ifdef HAVE_CONFIG_H
@@ -33,7 +33,6 @@
#include "nation.h"
static struct nation_type *nations = NULL;
-static struct team teams[MAX_NUM_TEAMS];
static int num_nation_groups;
static struct nation_group nation_groups[MAX_NUM_NATION_GROUPS];
@@ -314,111 +313,6 @@
}
/***************************************************************
- Returns the id of a team given its name, or TEAM_NONE if
- not found.
-***************************************************************/
-Team_type_id team_find_by_name(const char *team_name)
-{
- assert(team_name != NULL);
-
- team_iterate(pteam) {
- if(mystrcasecmp(team_name, pteam->name) == 0) {
- return pteam->id;
- }
- } team_iterate_end;
-
- return TEAM_NONE;
-}
-
-/***************************************************************
- Returns pointer to a team given its id
-***************************************************************/
-struct team *team_get_by_id(Team_type_id id)
-{
- assert(id == TEAM_NONE || (id < MAX_NUM_TEAMS && id >= 0));
- if (id == TEAM_NONE) {
- return NULL;
- }
- return &teams[id];
-}
-
-/***************************************************************
- Set a player to a team. Removes previous team affiliation,
- creates a new team if it does not exist.
-***************************************************************/
-void team_add_player(struct player *pplayer, const char *team_name)
-{
- Team_type_id team_id, i;
-
- assert(pplayer != NULL && team_name != NULL);
-
- /* find or create team */
- team_id = team_find_by_name(team_name);
- if (team_id == TEAM_NONE) {
- /* see if we have another team available */
- for (i = 0; i < MAX_NUM_TEAMS; i++) {
- if (teams[i].id == TEAM_NONE) {
- team_id = i;
- break;
- }
- }
- /* check if too many teams */
- if (team_id == TEAM_NONE) {
- die("Impossible: Too many teams!");
- }
- /* add another team */
- teams[team_id].id = team_id;
- sz_strlcpy(teams[team_id].name, team_name);
- }
- pplayer->team = team_id;
-}
-
-/***************************************************************
- Removes a player from a team, and removes the team if empty of
- players
-***************************************************************/
-void team_remove_player(struct player *pplayer)
-{
- bool others = FALSE;
-
- if (pplayer->team == TEAM_NONE) {
- return;
- }
-
- assert(pplayer->team < MAX_NUM_TEAMS && pplayer->team >= 0);
-
- /* anyone else using my team? */
- players_iterate(aplayer) {
- if (aplayer->team == pplayer->team && aplayer != pplayer) {
- others = TRUE;
- break;
- }
- } players_iterate_end;
-
- /* no other team members left? remove team! */
- if (!others) {
- teams[pplayer->team].id = TEAM_NONE;
- }
- pplayer->team = TEAM_NONE;
-}
-
-/***************************************************************
- Initializes team structure
-***************************************************************/
-void team_init()
-{
- Team_type_id i;
-
- assert(TEAM_NONE < 0 || TEAM_NONE >= MAX_NUM_TEAMS);
-
- for (i = 0; i < MAX_NUM_TEAMS; i++) {
- /* mark as unused */
- teams[i].id = TEAM_NONE;
- teams[i].name[0] = '\0';
- }
-}
-
-/***************************************************************
Add new group into the array of groups. If a group with
the same name already exists don't create new one, but return
old one
Index: common/nation.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/nation.h,v
retrieving revision 1.47
diff -u -r1.47 nation.h
--- common/nation.h 7 May 2005 14:03:51 -0000 1.47
+++ common/nation.h 11 May 2005 20:27:43 -0000
@@ -32,9 +32,6 @@
*/
#define MAX_NUM_LEADERS MAX_NUM_ITEMS
-#define MAX_NUM_TEAMS MAX_NUM_PLAYERS
-#define TEAM_NONE 255
-
#define MAX_NUM_NATION_GROUPS 128
/*
@@ -108,11 +105,6 @@
bool is_unavailable, is_used;
};
-struct team {
- char name[MAX_LEN_NAME];
- Team_type_id id; /* equal to array index if active, else TEAM_NONE */
-};
-
Nation_type_id find_nation_by_name(const char *name);
Nation_type_id find_nation_by_name_orig(const char *name);
const char *get_nation_name(Nation_type_id nation);
@@ -129,12 +121,6 @@
void nation_city_names_free(struct city_name *city_names);
int get_nation_city_style(Nation_type_id nation);
-void team_init(void);
-Team_type_id team_find_by_name(const char *team_name);
-struct team *team_get_by_id(Team_type_id id);
-void team_add_player(struct player *pplayer, const char *team_name);
-void team_remove_player(struct player *pplayer);
-
struct nation_group* add_new_nation_group(const char* name);
int get_nation_groups_count(void);
struct nation_group* get_nation_group_by_id(int id);
@@ -154,18 +140,4 @@
} \
}
-#define team_iterate(PI_team) \
-{ \
- struct team *PI_team; \
- Team_type_id PI_p_itr; \
- for (PI_p_itr = 0; PI_p_itr < MAX_NUM_TEAMS; PI_p_itr++) { \
- PI_team = team_get_by_id(PI_p_itr); \
- if (PI_team->id == TEAM_NONE) { \
- continue; \
- }
-
-#define team_iterate_end \
- } \
-}
-
#endif /* FC__NATION_H */
Index: common/packets.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/packets.h,v
retrieving revision 1.164
diff -u -r1.164 packets.h
--- common/packets.h 11 May 2005 00:13:22 -0000 1.164
+++ common/packets.h 11 May 2005 20:27:43 -0000
@@ -25,6 +25,7 @@
#include "requirements.h"
#include "shared.h" /* MAX_LEN_NAME, MAX_LEN_ADDR */
#include "spaceship.h"
+#include "team.h"
#include "unittype.h"
#include "worklist.h"
Index: common/team.c
===================================================================
RCS file: common/team.c
diff -N common/team.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ common/team.c 11 May 2005 20:27:43 -0000
@@ -0,0 +1,137 @@
+/**********************************************************************
+ 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 "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.
+****************************************************************************/
+Team_type_id team_find_by_name(const char *team_name)
+{
+ assert(team_name != NULL);
+
+ team_iterate(pteam) {
+ if (mystrcasecmp(team_name, pteam->name) == 0) {
+ return pteam->id;
+ }
+ } team_iterate_end;
+
+ return TEAM_NONE;
+}
+
+/****************************************************************************
+ Returns pointer to a team given its id
+****************************************************************************/
+struct team *team_get_by_id(Team_type_id id)
+{
+ assert(id == TEAM_NONE || (id < MAX_NUM_TEAMS && id >= 0));
+ if (id == TEAM_NONE) {
+ return NULL;
+ }
+ return &teams[id];
+}
+
+/****************************************************************************
+ Set a player to a team. Removes previous team affiliation,
+ creates a new team if it does not exist.
+****************************************************************************/
+void team_add_player(struct player *pplayer, const char *team_name)
+{
+ Team_type_id team_id, i;
+
+ assert(pplayer != NULL && team_name != NULL);
+
+ /* find or create team */
+ team_id = team_find_by_name(team_name);
+ if (team_id == TEAM_NONE) {
+ /* see if we have another team available */
+ for (i = 0; i < MAX_NUM_TEAMS; i++) {
+ if (teams[i].id == TEAM_NONE) {
+ team_id = i;
+ break;
+ }
+ }
+ /* check if too many teams */
+ if (team_id == TEAM_NONE) {
+ die("Impossible: Too many teams!");
+ }
+ /* add another team */
+ teams[team_id].id = team_id;
+ sz_strlcpy(teams[team_id].name, team_name);
+ }
+ pplayer->team = team_id;
+}
+
+/****************************************************************************
+ Removes a player from a team, and removes the team if empty of
+ players
+****************************************************************************/
+void team_remove_player(struct player *pplayer)
+{
+ bool others = FALSE;
+
+ if (pplayer->team == TEAM_NONE) {
+ return;
+ }
+
+ assert(pplayer->team < MAX_NUM_TEAMS && pplayer->team >= 0);
+
+ /* anyone else using my team? */
+ players_iterate(aplayer) {
+ if (aplayer->team == pplayer->team && aplayer != pplayer) {
+ others = TRUE;
+ break;
+ }
+ } players_iterate_end;
+
+ /* no other team members left? remove team! */
+ if (!others) {
+ teams[pplayer->team].id = TEAM_NONE;
+ }
+ pplayer->team = TEAM_NONE;
+}
+
+/****************************************************************************
+ Initializes team structure
+****************************************************************************/
+void team_init(void)
+{
+ Team_type_id i;
+
+ assert(TEAM_NONE < 0 || TEAM_NONE >= MAX_NUM_TEAMS);
+
+ for (i = 0; i < MAX_NUM_TEAMS; i++) {
+ /* mark as unused */
+ teams[i].id = TEAM_NONE;
+ teams[i].name[0] = '\0';
+ }
+}
Index: common/team.h
===================================================================
RCS file: common/team.h
diff -N common/team.h
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ common/team.h 11 May 2005 20:27:43 -0000
@@ -0,0 +1,48 @@
+/**********************************************************************
+ 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.
+***********************************************************************/
+
+#ifndef FC__TEAM_H
+#define FC__TEAM_H
+
+#include "fc_types.h"
+
+#define MAX_NUM_TEAMS MAX_NUM_PLAYERS
+#define TEAM_NONE 255
+
+struct team {
+ Team_type_id id; /* equal to array index if active, else TEAM_NONE */
+ char name[MAX_LEN_NAME];
+};
+
+void team_init(void);
+Team_type_id team_find_by_name(const char *team_name);
+struct team *team_get_by_id(Team_type_id id);
+void team_add_player(struct player *pplayer, const char *team_name);
+void team_remove_player(struct player *pplayer);
+
+#define team_iterate(pteam) \
+{ \
+ struct team *pteam; \
+ Team_type_id PI_p_itr; \
+ \
+ for (PI_p_itr = 0; PI_p_itr < MAX_NUM_TEAMS; PI_p_itr++) { \
+ pteam = team_get_by_id(PI_p_itr); \
+ if (pteam->id == TEAM_NONE) { \
+ continue; \
+ }
+
+#define team_iterate_end \
+ } \
+}
+
+#endif /* FC__TEAM_H */
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Freeciv-Dev] (PR#13044) new files for teams,
Jason Short <=
|
|