[Freeciv-Dev] (PR#15846) use Q_ for groups
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
<URL: http://bugs.freeciv.org/Ticket/Display.html?id=15846 >
This patch (as requested in PR#14449) allows groups to use Q_().
There is a substantial overhaul of the groups design included. This is
partially needed for the reason discussed in that ticket: we have to
have a master copy of group names so that the prefixed strings aren't
needed everywhere. It also necessitates a new manditory capability.
This version of the patch is for 2.1. Only small changes
(packets.def/capstr.c) are needed for the development version. The
patch is intended for both (although if this isn't needed for 2.1, we
can just apply it to the dev version).
I haven't actually tested it with translated strings. I won't commit it
until someone reports that it works with the Q_() translations.
-jason
Index: server/ruleset.c
===================================================================
--- server/ruleset.c (revision 11752)
+++ server/ruleset.c (working copy)
@@ -2026,7 +2026,11 @@
pl->num_groups = dim;
pl->groups = fc_malloc(sizeof(*(pl->groups)) * dim);
for (j = 0; j < dim; j++) {
- pl->groups[j] = add_new_nation_group(groups[j]);
+ pl->groups[j] = find_nation_group_by_name_orig(groups[j]);
+ if (!pl->groups[j]) {
+ freelog(LOG_FATAL, "Unknown group %s for nation %s.",
+ groups[j], pl->name);
+ }
}
free(groups);
@@ -2902,9 +2906,16 @@
void send_ruleset_nations(struct conn_list *dest)
{
struct packet_ruleset_nation packet;
+ struct packet_ruleset_nation_groups groups_packet;
struct nation_type *n;
int i, k;
+ groups_packet.ngroups = get_nation_groups_count();
+ nation_groups_iterate(pgroup) {
+ sz_strlcpy(groups_packet.groups[pgroup->index], pgroup->name);
+ } nation_groups_iterate_end;
+ lsend_packet_ruleset_nation_groups(dest, &groups_packet);
+
assert(sizeof(packet.init_techs) == sizeof(n->init_techs));
assert(ARRAY_SIZE(packet.init_techs) == ARRAY_SIZE(n->init_techs));
@@ -2934,9 +2945,9 @@
sz_strlcpy(packet.legend, n->legend);
/* client needs only the names */
- packet.group_count = n->num_groups;
+ packet.ngroups = n->num_groups;
for (i = 0; i < n->num_groups; i++) {
- sz_strlcpy(packet.group_name[i], n->groups[i]->name);
+ packet.groups[i] = n->groups[i]->index;
}
lsend_packet_ruleset_nation(dest, &packet);
Index: server/plrhand.c
===================================================================
--- server/plrhand.c (revision 11752)
+++ server/plrhand.c (working copy)
@@ -1293,7 +1293,7 @@
}
}
for (i = 0; i < n1->num_groups; i++) {
- if (nation_in_group(n2, n1->groups[i]->name)) {
+ if (is_nation_in_group(n2, n1->groups[i])) {
sum += n1->groups[i]->match;
}
}
Index: data/default/nations.ruleset
===================================================================
--- data/default/nations.ruleset (revision 11752)
+++ data/default/nations.ruleset (working copy)
@@ -17,34 +17,34 @@
; group
[ngroup_ancient]
-name="Ancient"
+name=_("?nationgroup:Ancient")
match=2
[ngroup_medieval]
-name="Medieval"
+name=_("?nationgroup:Medieval")
match=2
[ngroup_modern]
-name="Modern"
+name=_("?nationgroup:Modern")
match=1
[ngroup_african]
-name="African"
+name=_("?nationgroup:African")
match=0
[ngroup_american]
-name="American"
+name=_("?nationgroup:American")
match=0
[ngroup_asian]
-name="Asian"
+name=_("?nationgroup:Asian")
match=0
[ngroup_european]
-name="European"
+name=_("?nationgroup:European")
match=0
[ngroup_oceanian]
-name="Oceanian"
+name=_("?nationgroup:Oceanian")
match=0
[ngroup_transitional]
-name="Transitional"
+name=_("?nationgroup:Transitional")
match=2
[ngroup_fictional]
-name="Fictional"
+name=_("?nationgroup:Fictional")
match=0
; Below: nations data loaded from ruleset files for
Index: common/packets.def
===================================================================
--- common/packets.def (revision 11752)
+++ common/packets.def (working copy)
@@ -242,7 +242,7 @@
Spaceship
Ruleset
-The last used packet number is 117.
+The last used packet number is 118.
****************************************************/
@@ -1154,6 +1154,11 @@
UINT8 fallout_tile_penalty[O_MAX]; /* % taken from output if polluted */
end
+PACKET_RULESET_NATION_GROUPS=118;sc,lsend
+ UINT8 ngroups;
+ STRING groups[MAX_NUM_NATION_GROUPS:ngroups][MAX_LEN_NAME];
+end
+
PACKET_RULESET_NATION=102;sc,lsend
NATION id; key
@@ -1175,8 +1180,8 @@
BOOL is_available, is_playable, is_barbarian;
- UINT8 group_count;
- STRING group_name[MAX_NUM_NATION_GROUPS:group_count][MAX_LEN_NAME];
+ UINT8 ngroups;
+ UINT8 groups[MAX_NUM_NATION_GROUPS:ngroups];
end
PACKET_RULESET_CITY=103;sc,lsend
Index: common/capstr.c
===================================================================
--- common/capstr.c (revision 11752)
+++ common/capstr.c (working copy)
@@ -73,7 +73,7 @@
* are not directly related to the capability strings discussed here.)
*/
-/* +2.1 is the base capability string.
+/* +2.1b is the base capability string.
*
* - No new manditory capabilities can be added to the release branch; doing
* so would break network capability of supposedly "compatible" releases.
@@ -82,7 +82,7 @@
* as long as possible. We want to maintain network compatibility with
* the stable branch for as long as possible.
*/
-#define CAPABILITY "+2.1"
+#define CAPABILITY "+2.1b"
void init_our_capability(void)
{
Index: common/nation.c
===================================================================
--- common/nation.c (revision 11752)
+++ common/nation.c (working copy)
@@ -343,46 +343,89 @@
}
/***************************************************************
- 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
+ Add new group into the array of groups.
***************************************************************/
-struct nation_group* add_new_nation_group(const char* name)
+struct nation_group *add_new_nation_group(const char *name)
{
int i;
+ struct nation_group *group;
+
+ if (strlen(name) >= ARRAY_SIZE(group->name)) {
+ freelog(LOG_FATAL, "Too-long group name %s.", group->name);
+ exit(EXIT_FAILURE);
+ }
+
for (i = 0; i < num_nation_groups; i++) {
- if (mystrcasecmp(name, nation_groups[i].name) == 0) {
- return &nation_groups[i];
+ if (mystrcasecmp(Qn_(name), Qn_(nation_groups[i].name)) == 0) {
+ freelog(LOG_FATAL, "Duplicate group name %s.",
+ Qn_(group->name));
+ exit(EXIT_FAILURE);
}
}
- if (i == MAX_NUM_NATION_GROUPS) {
- die("Too many groups of nations");
+ if (num_nation_groups == MAX_NUM_NATION_GROUPS) {
+ freelog(LOG_FATAL, "Too many groups of nations (%d is the maximum)",
+ MAX_NUM_NATION_GROUPS);
+ exit(EXIT_FAILURE);
}
- sz_strlcpy(nation_groups[i].name, name);
- nation_groups[i].match = 0;
- return &nation_groups[num_nation_groups++];
+
+ group = nation_groups + num_nation_groups;
+ group->index = num_nation_groups;
+ sz_strlcpy(group->name, name);
+ group->match = 0;
+
+ num_nation_groups++;
+
+ return group;
}
-/***************************************************************
-***************************************************************/
+/****************************************************************************
+ Return the number of nation groups.
+****************************************************************************/
int get_nation_groups_count(void)
{
return num_nation_groups;
}
+/****************************************************************************
+ Return a specific nation group, by index.
+****************************************************************************/
struct nation_group* get_nation_group_by_id(int id)
{
- return &nation_groups[id];
+ if (id >= 0 && id < num_nation_groups) {
+ return &nation_groups[id];
+ } else {
+ return NULL;
+ }
}
-/***************************************************************
+/****************************************************************************
+ Return a specific nation group, by (untranslated) name.
+
+ Returns NULL if no group is found.
+****************************************************************************/
+struct nation_group *find_nation_group_by_name_orig(const char *name)
+{
+ int i;
+
+ for (i = 0; i < num_nation_groups; i++) {
+ if (mystrcasecmp(Qn_(name), Qn_(nation_groups[i].name)) == 0) {
+ return nation_groups + i;
+ }
+ }
+
+ return NULL;
+}
+
+/****************************************************************************
Check if the given nation is in a given group
-***************************************************************/
-bool nation_in_group(struct nation_type* nation, const char* group_name)
+****************************************************************************/
+bool is_nation_in_group(struct nation_type *nation,
+ struct nation_group *group)
{
int i;
+
for (i = 0; i < nation->num_groups; i++) {
- if (mystrcasecmp(nation->groups[i]->name, group_name) == 0) {
+ if (nation->groups[i] == group) {
return TRUE;
}
}
Index: common/nation.h
===================================================================
--- common/nation.h (revision 11752)
+++ common/nation.h (working copy)
@@ -58,6 +58,8 @@
};
struct nation_group {
+ int index;
+
char name[MAX_LEN_NAME];
/* How much the AI will try to select a nation in the same group */
@@ -130,12 +132,24 @@
void nation_city_names_free(struct city_name *city_names);
int get_nation_city_style(const struct nation_type *nation);
-struct nation_group* add_new_nation_group(const char* name);
+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);
+struct nation_group *find_nation_group_by_name_orig(const char *name);
+bool is_nation_in_group(struct nation_type *nation,
+ struct nation_group *group);
-bool nation_in_group(struct nation_type* nation, const char* group_name);
+#define nation_groups_iterate(pgroup) \
+{ \
+ int _index; \
+ \
+ for (_index = 0; _index < get_nation_groups_count(); _index++) { \
+ struct nation_group *pgroup = get_nation_group_by_id(_index);
+#define nation_groups_iterate_end \
+ } \
+}
+
bool can_conn_edit_players_nation(const struct connection *pconn,
const struct player *pplayer);
Index: client/gui-gtk-2.0/dialogs.c
===================================================================
--- client/gui-gtk-2.0/dialogs.c (revision 11752)
+++ client/gui-gtk-2.0/dialogs.c (working copy)
@@ -671,7 +671,7 @@
continue;
}
- if (group != NULL && !nation_in_group(pnation, group->name)) {
+ if (group != NULL && !is_nation_in_group(pnation, group)) {
continue;
}
@@ -728,7 +728,7 @@
for (i = 0; i <= get_nation_groups_count(); i++) {
struct nation_group* group = (i == 0 ? NULL: get_nation_group_by_id(i -
1));
nation_list = create_list_of_nations_in_group(group, i);
- group_name_label = gtk_label_new(group ? _(group->name) : _("All"));
+ group_name_label = gtk_label_new(group ? Q_(group->name) : _("All"));
gtk_notebook_append_page(GTK_NOTEBOOK(notebook), nation_list,
group_name_label);
}
Index: client/packhand.c
===================================================================
--- client/packhand.c (revision 11752)
+++ client/packhand.c (working copy)
@@ -2404,6 +2404,20 @@
}
/**************************************************************************
+ Handle the list of groups, sent by the ruleset.
+**************************************************************************/
+void handle_ruleset_nation_groups(struct packet_ruleset_nation_groups *packet)
+{
+ int i;
+
+ for (i = 0; i < packet->ngroups; i++) {
+ struct nation_group *group = add_new_nation_group(packet->groups[i]);
+
+ assert(group != NULL && group->index == i);
+ }
+}
+
+/**************************************************************************
...
**************************************************************************/
void handle_ruleset_nation(struct packet_ruleset_nation *p)
@@ -2448,10 +2462,15 @@
pl->legend = mystrdup("");
}
- pl->num_groups = p->group_count;
+ pl->num_groups = p->ngroups;
pl->groups = malloc(sizeof(*(pl->groups)) * pl->num_groups);
- for (i = 0; i < p->group_count; i++) {
- pl->groups[i] = add_new_nation_group(p->group_name[i]);
+ for (i = 0; i < p->ngroups; i++) {
+ pl->groups[i] = get_nation_group_by_id(p->groups[i]);
+ if (!pl->groups[i]) {
+ freelog(LOG_FATAL, "Unknown nation group %d for nation %s.",
+ p->groups[i], pl->name);
+ exit(EXIT_FAILURE);
+ }
}
pl->is_available = p->is_available;
- [Freeciv-Dev] (PR#15846) use Q_ for groups,
Jason Short <=
|
|