1 /***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
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)
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 ***********************************************************************/
14 /***********************************************************************
15 Functions for handling the nations.
16 ************************************************************************/
19 #include <fc_config.h>
29 #include "connection.h"
31 #include "government.h"
39 /* Nation set structure. */
41 struct name_translation name
;
42 char description
[MAX_LEN_MSG
];
45 static struct nation_type
*nations
= NULL
;
47 static int num_nation_sets
;
48 static struct nation_set nation_sets
[MAX_NUM_NATION_SETS
];
49 static int num_nation_groups
;
50 static struct nation_group nation_groups
[MAX_NUM_NATION_GROUPS
];
52 /****************************************************************************
53 Runs action if the nation is not valid.
54 ****************************************************************************/
56 #define NATION_CHECK(pnation, action) \
57 fc_assert_action(nation_check(pnation, \
58 log_do_output_for_level(LOG_ERROR), \
59 __FILE__, __FUNCTION__, __FC_LINE__), \
62 /****************************************************************************
63 Returns TRUE if the nation is valid, else, print an error message and
65 ****************************************************************************/
66 static inline bool nation_check(const struct nation_type
*pnation
,
67 bool do_output
, const char *file
,
68 const char *function
, int line
)
70 if (0 == nation_count()) {
72 do_log(file
, function
, line
, TRUE
, LOG_ERROR
,
73 "Function called before nations setup.");
77 if (NULL
== pnation
) {
79 do_log(file
, function
, line
, TRUE
, LOG_ERROR
,
80 "This function has NULL nation argument.");
84 if (pnation
->item_number
< 0
85 || pnation
->item_number
>= nation_count()
86 || &nations
[nation_index(pnation
)] != pnation
) {
88 do_log(file
, function
, line
, TRUE
, LOG_ERROR
,
89 "This function has bad nation number %d (count %d).",
90 pnation
->item_number
, nation_count());
98 #define NATION_CHECK(pnation, action) /* Do Nothing. */
101 /****************************************************************************
102 Returns the nation that has the given (translated) plural noun.
103 Returns NO_NATION_SELECTED if none match.
104 ****************************************************************************/
105 struct nation_type
*nation_by_translated_plural(const char *name
)
107 nations_iterate(pnation
) {
108 if (0 == strcmp(nation_plural_translation(pnation
), name
)) {
111 } nations_iterate_end
;
113 return NO_NATION_SELECTED
;
116 /****************************************************************************
117 Returns the nation that has the given (untranslated) rule name (adjective).
118 Returns NO_NATION_SELECTED if none match.
119 ****************************************************************************/
120 struct nation_type
*nation_by_rule_name(const char *name
)
122 const char *qname
= Qn_(name
);
124 nations_iterate(pnation
) {
125 if (0 == fc_strcasecmp(nation_rule_name(pnation
), qname
)) {
128 } nations_iterate_end
;
130 return NO_NATION_SELECTED
;
133 /****************************************************************************
134 Return the (untranslated) rule name of the nation (adjective form).
135 You don't have to free the return pointer.
136 ****************************************************************************/
137 const char *nation_rule_name(const struct nation_type
*pnation
)
139 NATION_CHECK(pnation
, return "");
141 return rule_name_get(&pnation
->adjective
);
144 /****************************************************************************
145 Return the (translated) adjective for the given nation.
146 You don't have to free the return pointer.
147 ****************************************************************************/
148 const char *nation_adjective_translation(const struct nation_type
*pnation
)
150 NATION_CHECK(pnation
, return "");
151 return name_translation_get(&pnation
->adjective
);
154 /****************************************************************************
155 Return the (translated) plural noun of the given nation.
156 You don't have to free the return pointer.
157 ****************************************************************************/
158 const char *nation_plural_translation(const struct nation_type
*pnation
)
160 NATION_CHECK(pnation
, return "");
161 return name_translation_get(&pnation
->noun_plural
);
164 /****************************************************************************
165 Return the (translated) adjective for the given nation of a player.
166 You don't have to free the return pointer.
167 ****************************************************************************/
168 const char *nation_adjective_for_player(const struct player
*pplayer
)
170 return nation_adjective_translation(nation_of_player(pplayer
));
173 /****************************************************************************
174 Return the (translated) plural noun of the given nation of a player.
175 You don't have to free the return pointer.
176 ****************************************************************************/
177 const char *nation_plural_for_player(const struct player
*pplayer
)
179 return nation_plural_translation(nation_of_player(pplayer
));
182 /****************************************************************************
183 Return whether a nation is "pickable" -- whether players can select it
185 (Client only function -- on the server, use client_can_pick_nation().)
186 ****************************************************************************/
187 bool is_nation_pickable(const struct nation_type
*nation
)
189 fc_assert_ret_val(!is_server(), FALSE
);
190 return nation
->client
.is_pickable
;
193 /****************************************************************************
194 Return whether a nation is "playable"; i.e., whether a human player can
195 choose this nation. Barbarian and observer nations are not playable.
197 This does not check whether a nation is "used" or "available".
198 ****************************************************************************/
199 bool is_nation_playable(const struct nation_type
*nation
)
201 NATION_CHECK(nation
, return FALSE
);
202 return nation
->is_playable
;
205 /****************************************************************************
206 Returns which kind of barbarians can use this nation.
208 This does not check whether a nation is "used" or "available".
209 ****************************************************************************/
210 enum barbarian_type
nation_barbarian_type(const struct nation_type
*nation
)
212 NATION_CHECK(nation
, return NOT_A_BARBARIAN
);
213 return nation
->barb_type
;
217 /****************************************************************************
219 ****************************************************************************/
220 struct nation_leader
{
225 /****************************************************************************
226 Returns the list the nation leader names.
227 ****************************************************************************/
228 const struct nation_leader_list
*
229 nation_leaders(const struct nation_type
*pnation
)
231 NATION_CHECK(pnation
, return NULL
);
232 return pnation
->leaders
;
235 /****************************************************************************
236 Create a new leader for the nation.
237 ****************************************************************************/
238 struct nation_leader
*nation_leader_new(struct nation_type
*pnation
,
239 const char *name
, bool is_male
)
241 struct nation_leader
*pleader
;
243 NATION_CHECK(pnation
, return NULL
);
244 pleader
= fc_malloc(sizeof(*pleader
));
245 pleader
->name
= fc_strdup(name
);
246 pleader
->is_male
= is_male
;
248 nation_leader_list_append(pnation
->leaders
, pleader
);
252 /****************************************************************************
253 Destroy a nation leader created with nation_leader_new().
254 ****************************************************************************/
255 static void nation_leader_destroy(struct nation_leader
*pleader
)
261 /****************************************************************************
262 Returns the nation leader structure which match 'name' or NULL if not
264 ****************************************************************************/
265 struct nation_leader
*
266 nation_leader_by_name(const struct nation_type
*pnation
, const char *name
)
268 NATION_CHECK(pnation
, return NULL
);
269 nation_leader_list_iterate(pnation
->leaders
, pleader
) {
270 if (0 == fc_strcasecmp(name
, pleader
->name
)) {
273 } nation_leader_list_iterate_end
;
277 /****************************************************************************
278 Return the name of the nation leader.
279 ****************************************************************************/
280 const char *nation_leader_name(const struct nation_leader
*pleader
)
282 fc_assert_ret_val(NULL
!= pleader
, NULL
);
283 return pleader
->name
;
286 /****************************************************************************
287 Return the sex of the nation leader.
288 ****************************************************************************/
289 bool nation_leader_is_male(const struct nation_leader
*pleader
)
291 fc_assert_ret_val(NULL
!= pleader
, TRUE
);
292 return pleader
->is_male
;
295 /****************************************************************************
296 Return translated version of nation legend.
297 ****************************************************************************/
298 const char *nation_legend_translation(const struct nation_type
*pnation
,
301 if (pnation
->translation_domain
== NULL
) {
305 return DG_(pnation
->translation_domain
, legend
);
308 /****************************************************************************
309 Nation default cities. The nation_city structure holds information about
310 a default choice for the city name. The 'name' field is, of course, just
311 the name for the city. The 'river' and 'terrain' fields are entries
312 recording whether the terrain is present near the city - we give higher
313 priority to cities which have matching terrain. In the case of a river we
314 only care if the city is _on_ the river, for other terrain features we
315 give the bonus if the city is close to the terrain.
317 This is controlled through the nation's ruleset like this:
318 cities = "Washington (ocean, river, swamp)", "New York (!mountains)"
319 ****************************************************************************/
322 enum nation_city_preference river
;
323 enum nation_city_preference terrain
[MAX_NUM_TERRAINS
];
326 /****************************************************************************
327 Return the default cities of the nation (server only function).
328 ****************************************************************************/
329 const struct nation_city_list
*
330 nation_cities(const struct nation_type
*pnation
)
332 NATION_CHECK(pnation
, return NULL
);
333 fc_assert_ret_val(is_server(), NULL
);
335 return pnation
->server
.default_cities
;
338 /****************************************************************************
339 Create a new default city for the nation (server only function).
340 ****************************************************************************/
341 struct nation_city
*nation_city_new(struct nation_type
*pnation
,
344 struct nation_city
*pncity
;
346 NATION_CHECK(pnation
, return NULL
);
347 fc_assert_ret_val(is_server(), NULL
);
349 fc_assert(0 == NCP_NONE
);
350 pncity
= fc_calloc(1, sizeof(*pncity
)); /* Set NCP_NONE. */
351 pncity
->name
= fc_strdup(name
);
353 nation_city_list_append(pnation
->server
.default_cities
, pncity
);
357 /****************************************************************************
358 Destroy a default nation city created with nation_city_new().
359 ****************************************************************************/
360 static void nation_city_destroy(struct nation_city
*pncity
)
366 /****************************************************************************
367 Reverts the nation city preference.
368 ****************************************************************************/
369 enum nation_city_preference
370 nation_city_preference_revert(enum nation_city_preference prefer
)
381 log_error("%s(): Wrong nation_city_preference variant (%d).",
382 __FUNCTION__
, prefer
);
386 /****************************************************************************
387 Set the default nation city preference for the terrain.
388 ****************************************************************************/
389 void nation_city_set_terrain_preference(struct nation_city
*pncity
,
390 const struct terrain
*pterrain
,
391 enum nation_city_preference prefer
)
393 fc_assert_ret(NULL
!= pncity
);
394 fc_assert_ret(NULL
!= pterrain
);
395 pncity
->terrain
[terrain_index(pterrain
)] = prefer
;
398 /****************************************************************************
399 Set the default nation city preference about rivers.
400 ****************************************************************************/
401 void nation_city_set_river_preference(struct nation_city
*pncity
,
402 enum nation_city_preference prefer
)
404 fc_assert_ret(NULL
!= pncity
);
405 pncity
->river
= prefer
;
408 /****************************************************************************
409 Return the name of the default nation city.
410 ****************************************************************************/
411 const char *nation_city_name(const struct nation_city
*pncity
)
413 fc_assert_ret_val(NULL
!= pncity
, NULL
);
417 /****************************************************************************
418 Return the default nation city preference for the terrain.
419 ****************************************************************************/
420 enum nation_city_preference
421 nation_city_terrain_preference(const struct nation_city
*pncity
,
422 const struct terrain
*pterrain
)
424 fc_assert_ret_val(NULL
!= pncity
, NCP_DISLIKE
);
425 fc_assert_ret_val(NULL
!= pterrain
, NCP_DISLIKE
);
426 return pncity
->terrain
[terrain_index(pterrain
)];
429 /****************************************************************************
430 Return the default nation city preference for rivers.
431 ****************************************************************************/
432 enum nation_city_preference
433 nation_city_river_preference(const struct nation_city
*pncity
)
435 fc_assert_ret_val(NULL
!= pncity
, NCP_DISLIKE
);
436 return pncity
->river
;
440 /****************************************************************************
441 Return the nation of a player.
442 ****************************************************************************/
443 struct nation_type
*nation_of_player(const struct player
*pplayer
)
445 fc_assert_ret_val(NULL
!= pplayer
, NULL
);
446 NATION_CHECK(pplayer
->nation
, return NULL
);
447 return pplayer
->nation
;
450 /****************************************************************************
451 Return the nation of the player who owns the city.
452 ****************************************************************************/
453 struct nation_type
*nation_of_city(const struct city
*pcity
)
455 fc_assert_ret_val(pcity
!= NULL
, NULL
);
456 return nation_of_player(city_owner(pcity
));
459 /****************************************************************************
460 Return the nation of the player who owns the unit.
461 ****************************************************************************/
462 struct nation_type
*nation_of_unit(const struct unit
*punit
)
464 fc_assert_ret_val(punit
!= NULL
, NULL
);
465 return nation_of_player(unit_owner(punit
));
468 /****************************************************************************
469 Return the nation with the given index.
471 This function returns NULL for an out-of-range index (some callers
473 ****************************************************************************/
474 struct nation_type
*nation_by_number(const Nation_type_id nation
)
476 if (nation
< 0 || nation
>= game
.control
.nation_count
) {
479 return nations
+ nation
;
482 /**************************************************************************
483 Return the nation index.
484 **************************************************************************/
485 Nation_type_id
nation_number(const struct nation_type
*pnation
)
487 fc_assert_ret_val(NULL
!= pnation
, -1);
488 return pnation
->item_number
;
491 /**************************************************************************
492 Return the nation index.
494 Currently same as nation_number(), paired with nation_count()
495 indicates use as an array index.
496 **************************************************************************/
497 Nation_type_id
nation_index(const struct nation_type
*pnation
)
499 fc_assert_ret_val(NULL
!= pnation
, -1);
500 return pnation
- nations
;
503 /****************************************************************************
504 Return the number of nations.
505 ****************************************************************************/
506 Nation_type_id
nation_count(void)
508 return game
.control
.nation_count
;
512 /****************************************************************************
514 ****************************************************************************/
516 struct iterator vtable
;
517 struct nation_type
*p
, *end
;
519 #define NATION_ITER(p) ((struct nation_iter *)(p))
521 /****************************************************************************
522 Implementation of iterator 'sizeof' function.
523 ****************************************************************************/
524 size_t nation_iter_sizeof(void)
526 return sizeof(struct nation_iter
);
529 /****************************************************************************
530 Implementation of iterator 'next' function.
531 ****************************************************************************/
532 static void nation_iter_next(struct iterator
*iter
)
534 NATION_ITER(iter
)->p
++;
537 /****************************************************************************
538 Implementation of iterator 'get' function.
539 ****************************************************************************/
540 static void *nation_iter_get(const struct iterator
*iter
)
542 return NATION_ITER(iter
)->p
;
545 /****************************************************************************
546 Implementation of iterator 'valid' function.
547 ****************************************************************************/
548 static bool nation_iter_valid(const struct iterator
*iter
)
550 struct nation_iter
*it
= NATION_ITER(iter
);
551 return it
->p
< it
->end
;
554 /****************************************************************************
555 Implementation of iterator 'init' function.
556 ****************************************************************************/
557 struct iterator
*nation_iter_init(struct nation_iter
*it
)
559 it
->vtable
.next
= nation_iter_next
;
560 it
->vtable
.get
= nation_iter_get
;
561 it
->vtable
.valid
= nation_iter_valid
;
563 it
->end
= nations
+ nation_count();
567 /****************************************************************************
568 Allocate resources associated with the given nation.
569 ****************************************************************************/
570 static void nation_init(struct nation_type
*pnation
)
572 memset(pnation
, 0, sizeof(*pnation
));
574 pnation
->item_number
= pnation
- nations
;
575 pnation
->translation_domain
= NULL
;
576 pnation
->leaders
= nation_leader_list_new_full(nation_leader_destroy
);
577 pnation
->sets
= nation_set_list_new();
578 pnation
->groups
= nation_group_list_new();
581 pnation
->server
.default_cities
=
582 nation_city_list_new_full(nation_city_destroy
);
583 pnation
->server
.civilwar_nations
= nation_list_new();
584 pnation
->server
.parent_nations
= nation_list_new();
585 pnation
->server
.conflicts_with
= nation_list_new();
586 /* server.rgb starts out NULL */
587 pnation
->server
.traits
= fc_calloc(TRAIT_COUNT
,
588 sizeof(*pnation
->server
.traits
));
592 /****************************************************************************
593 De-allocate resources associated with the given nation.
594 ****************************************************************************/
595 static void nation_free(struct nation_type
*pnation
)
597 free(pnation
->legend
);
598 FC_FREE(pnation
->translation_domain
);
599 nation_leader_list_destroy(pnation
->leaders
);
600 nation_set_list_destroy(pnation
->sets
);
601 nation_group_list_destroy(pnation
->groups
);
604 nation_city_list_destroy(pnation
->server
.default_cities
);
605 nation_list_destroy(pnation
->server
.civilwar_nations
);
606 nation_list_destroy(pnation
->server
.parent_nations
);
607 nation_list_destroy(pnation
->server
.conflicts_with
);
608 rgbcolor_destroy(pnation
->server
.rgb
);
609 FC_FREE(pnation
->server
.traits
);
612 memset(pnation
, 0, sizeof(*pnation
));
615 /****************************************************************************
616 Allocate space for the given number of nations.
617 ****************************************************************************/
618 void nations_alloc(int num
)
622 nations
= fc_malloc(sizeof(*nations
) * num
);
623 game
.control
.nation_count
= num
;
625 for (i
= 0; i
< num
; i
++) {
626 nation_init(nations
+ i
);
630 /****************************************************************************
631 De-allocate the currently allocated nations.
632 ****************************************************************************/
633 void nations_free(void)
637 if (NULL
== nations
) {
641 for (i
= 0; i
< game
.control
.nation_count
; i
++) {
642 nation_free(nations
+ i
);
647 game
.control
.nation_count
= 0;
650 /****************************************************************************
651 Returns initial government type for this nation.
652 Always returns non-NULL -- nation-specific government or failing that
653 ruleset default government.
654 ****************************************************************************/
655 struct government
*init_government_of_nation(const struct nation_type
*pnation
)
657 NATION_CHECK(pnation
, return game
.default_government
);
658 if (pnation
->init_government
) {
659 return pnation
->init_government
;
661 return game
.default_government
;
665 /****************************************************************************
666 Returns nation's style.
667 ****************************************************************************/
668 struct nation_style
*style_of_nation(const struct nation_type
*pnation
)
670 NATION_CHECK(pnation
, return 0);
671 return pnation
->style
;
674 /****************************************************************************
675 Returns nation's player color preference, or NULL if none.
676 Server only function.
677 ****************************************************************************/
678 const struct rgbcolor
*nation_color(const struct nation_type
*pnation
)
680 NATION_CHECK(pnation
, return NULL
);
681 return pnation
->server
.rgb
;
684 /****************************************************************************
685 Return the number of nation sets.
686 ****************************************************************************/
687 int nation_set_count(void)
689 return num_nation_sets
;
692 /****************************************************************************
693 Return the nation set index.
694 ****************************************************************************/
695 int nation_set_index(const struct nation_set
*pset
)
697 fc_assert_ret_val(NULL
!= pset
, -1);
698 return pset
- nation_sets
;
701 /****************************************************************************
702 Return the nation set index.
703 ****************************************************************************/
704 int nation_set_number(const struct nation_set
*pset
)
706 return nation_set_index(pset
);
709 /****************************************************************************
710 Add new set into the array of nation sets.
711 ****************************************************************************/
712 struct nation_set
*nation_set_new(const char *set_name
,
713 const char *set_rule_name
,
714 const char *set_description
)
716 struct nation_set
*pset
;
718 if (MAX_NUM_NATION_SETS
<= num_nation_sets
) {
719 log_error("Too many nation sets (%d is the maximum).",
720 MAX_NUM_NATION_SETS
);
724 /* Print the name and truncate if needed. */
725 pset
= nation_sets
+ num_nation_sets
;
726 names_set(&pset
->name
, NULL
, set_name
, set_rule_name
);
727 (void) sz_loud_strlcpy(pset
->description
, set_description
,
728 "Nation set description \"%s\" too long; truncating.");
730 if (NULL
!= nation_set_by_rule_name(rule_name_get(&pset
->name
))) {
731 log_error("Duplicate nation set name %s.", rule_name_get(&pset
->name
));
735 if (NULL
!= nation_group_by_rule_name(rule_name_get(&pset
->name
))) {
736 log_error("Nation set name %s is already used for a group.",
737 rule_name_get(&pset
->name
));
746 /****************************************************************************
747 Return the nation set with the given index.
749 This function returns NULL for an out-of-range index (some callers
751 ****************************************************************************/
752 struct nation_set
*nation_set_by_number(int id
)
754 if (id
< 0 || id
>= num_nation_sets
) {
757 return nation_sets
+ id
;
760 /****************************************************************************
761 Return the nation set that has the given (untranslated) rule name.
762 Returns NULL if no set is found.
763 ****************************************************************************/
764 struct nation_set
*nation_set_by_rule_name(const char *name
)
766 const char *qname
= Qn_(name
);
768 nation_sets_iterate(pset
) {
769 if (0 == fc_strcasecmp(rule_name_get(&pset
->name
), qname
)) {
772 } nation_sets_iterate_end
;
777 /****************************************************************************
778 Return the untranslated name of a nation set (including qualifier, if any).
779 You usually want nation_set_rule_name() instead.
780 You don't have to free the return pointer.
781 ****************************************************************************/
782 const char *nation_set_untranslated_name(const struct nation_set
*pset
)
784 fc_assert_ret_val(NULL
!= pset
, NULL
);
785 return untranslated_name(&pset
->name
);
788 /****************************************************************************
789 Return the (untranslated) rule name of a nation set.
790 You don't have to free the return pointer.
791 ****************************************************************************/
792 const char *nation_set_rule_name(const struct nation_set
*pset
)
794 fc_assert_ret_val(NULL
!= pset
, NULL
);
796 return rule_name_get(&pset
->name
);
799 /****************************************************************************
800 Return the translated name of a nation set.
801 You don't have to free the return pointer.
802 ****************************************************************************/
803 const char *nation_set_name_translation(const struct nation_set
*pset
)
805 fc_assert_ret_val(NULL
!= pset
, NULL
);
806 return name_translation_get(&pset
->name
);
809 /****************************************************************************
810 Return the (untranslated) user description of a nation set.
811 You don't have to free the return pointer.
812 ****************************************************************************/
813 const char *nation_set_description(const struct nation_set
*pset
)
815 fc_assert_ret_val(NULL
!= pset
, NULL
);
816 return pset
->description
;
819 /****************************************************************************
820 Check if the given nation is in a given set
821 ****************************************************************************/
822 bool nation_is_in_set(const struct nation_type
*pnation
,
823 const struct nation_set
*pset
)
825 fc_assert_ret_val(NULL
!= pnation
, FALSE
);
827 nation_set_list_iterate(pnation
->sets
, aset
) {
831 } nation_set_list_iterate_end
;
835 /****************************************************************************
836 Returns the nation set that would be selected by the given value of the
837 'nationset' server setting.
838 This differs from nation_set_by_rule_name() for the empty string, where
839 the first (ruleset default) nationset will be used; and similarly for
840 a nationset not matched in the ruleset.
841 The knowledge of the interpretation of this setting encapsulated here is
842 required on both server and client.
843 ****************************************************************************/
844 struct nation_set
*nation_set_by_setting_value(const char *setting
)
846 struct nation_set
*pset
= NULL
;
848 if (strlen(setting
) > 0) {
849 pset
= nation_set_by_rule_name(setting
);
852 /* Either no nation set specified, or the specified one isn't in the
853 * current ruleset. Default to the first nation set specified by
855 pset
= nation_set_by_number(0);
857 fc_assert(pset
!= NULL
);
862 /****************************************************************************
864 ****************************************************************************/
865 struct nation_set_iter
{
866 struct iterator vtable
;
867 struct nation_set
*p
, *end
;
869 #define NATION_SET_ITER(p) ((struct nation_set_iter *)(p))
871 /****************************************************************************
872 Implementation of iterator 'sizeof' function.
873 ****************************************************************************/
874 size_t nation_set_iter_sizeof(void)
876 return sizeof(struct nation_set_iter
);
879 /****************************************************************************
880 Implementation of iterator 'next' function.
881 ****************************************************************************/
882 static void nation_set_iter_next(struct iterator
*iter
)
884 NATION_SET_ITER(iter
)->p
++;
887 /****************************************************************************
888 Implementation of iterator 'get' function.
889 ****************************************************************************/
890 static void *nation_set_iter_get(const struct iterator
*iter
)
892 return NATION_SET_ITER(iter
)->p
;
895 /****************************************************************************
896 Implementation of iterator 'valid' function.
897 ****************************************************************************/
898 static bool nation_set_iter_valid(const struct iterator
*iter
)
900 struct nation_set_iter
*it
= NATION_SET_ITER(iter
);
901 return it
->p
< it
->end
;
904 /****************************************************************************
905 Implementation of iterator 'init' function.
906 ****************************************************************************/
907 struct iterator
*nation_set_iter_init(struct nation_set_iter
*it
)
909 it
->vtable
.next
= nation_set_iter_next
;
910 it
->vtable
.get
= nation_set_iter_get
;
911 it
->vtable
.valid
= nation_set_iter_valid
;
913 it
->end
= nation_sets
+ nation_set_count();
917 /****************************************************************************
918 Return the number of nation groups.
919 ****************************************************************************/
920 int nation_group_count(void)
922 return num_nation_groups
;
925 /****************************************************************************
926 Return the nation group index.
927 ****************************************************************************/
928 int nation_group_index(const struct nation_group
*pgroup
)
930 fc_assert_ret_val(NULL
!= pgroup
, -1);
931 return pgroup
- nation_groups
;
934 /****************************************************************************
935 Return the nation group index.
936 ****************************************************************************/
937 int nation_group_number(const struct nation_group
*pgroup
)
939 return nation_group_index(pgroup
);
942 /****************************************************************************
943 Add new group into the array of groups.
944 ****************************************************************************/
945 struct nation_group
*nation_group_new(const char *name
)
947 struct nation_group
*pgroup
;
949 if (MAX_NUM_NATION_GROUPS
<= num_nation_groups
) {
950 log_error("Too many nation groups (%d is the maximum).",
951 MAX_NUM_NATION_GROUPS
);
955 /* Print the name and truncate if needed. */
956 pgroup
= nation_groups
+ num_nation_groups
;
957 name_set(&pgroup
->name
, NULL
, name
);
958 if (NULL
!= nation_group_by_rule_name(rule_name_get(&pgroup
->name
))) {
959 log_error("Duplicate nation group name %s.", rule_name_get(&pgroup
->name
));
963 if (NULL
!= nation_set_by_rule_name(rule_name_get(&pgroup
->name
))) {
964 log_error("Nation group name %s is already used for a set.",
965 rule_name_get(&pgroup
->name
));
970 pgroup
->server
.match
= 0;
977 /****************************************************************************
978 Return the nation group with the given index.
980 This function returns NULL for an out-of-range index (some callers
982 ****************************************************************************/
983 struct nation_group
*nation_group_by_number(int id
)
985 if (id
< 0 || id
>= num_nation_groups
) {
988 return nation_groups
+ id
;
991 /****************************************************************************
992 Return the nation group that has the given (untranslated) rule name.
993 Returns NULL if no group is found.
994 ****************************************************************************/
995 struct nation_group
*nation_group_by_rule_name(const char *name
)
997 const char *qname
= Qn_(name
);
999 nation_groups_iterate(pgroup
) {
1000 if (0 == fc_strcasecmp(rule_name_get(&pgroup
->name
), qname
)) {
1003 } nation_groups_iterate_end
;
1008 /****************************************************************************
1009 Set whether this group should appear in the nation selection UI.
1010 ****************************************************************************/
1011 void nation_group_set_hidden(struct nation_group
*pgroup
, bool hidden
)
1013 fc_assert_ret(NULL
!= pgroup
);
1014 pgroup
->hidden
= hidden
;
1017 /****************************************************************************
1018 Set how much the AI will try to select a nation in the same group.
1019 Server only function.
1020 ****************************************************************************/
1021 void nation_group_set_match(struct nation_group
*pgroup
, int match
)
1023 fc_assert_ret(is_server());
1024 fc_assert_ret(NULL
!= pgroup
);
1025 pgroup
->server
.match
= match
;
1028 /****************************************************************************
1029 Return whether this group should appear in the nation selection UI.
1030 ****************************************************************************/
1031 bool is_nation_group_hidden(struct nation_group
*pgroup
)
1033 fc_assert_ret_val(NULL
!= pgroup
, TRUE
);
1034 return pgroup
->hidden
;
1037 /****************************************************************************
1038 Return the untranslated name of a nation group (including qualifier,
1040 You usually want nation_group_rule_name() instead.
1041 You don't have to free the return pointer.
1042 ****************************************************************************/
1043 const char *nation_group_untranslated_name(const struct nation_group
*pgroup
)
1045 fc_assert_ret_val(NULL
!= pgroup
, NULL
);
1046 return untranslated_name(&pgroup
->name
);
1049 /****************************************************************************
1050 Return the (untranslated) rule name of a nation group.
1051 You don't have to free the return pointer.
1052 ****************************************************************************/
1053 const char *nation_group_rule_name(const struct nation_group
*pgroup
)
1055 fc_assert_ret_val(NULL
!= pgroup
, NULL
);
1057 return rule_name_get(&pgroup
->name
);
1060 /****************************************************************************
1061 Return the translated name of a nation group.
1062 You don't have to free the return pointer.
1063 ****************************************************************************/
1064 const char *nation_group_name_translation(const struct nation_group
*pgroup
)
1066 fc_assert_ret_val(NULL
!= pgroup
, NULL
);
1067 return name_translation_get(&pgroup
->name
);
1070 /****************************************************************************
1071 Check if the given nation is in a given group
1072 ****************************************************************************/
1073 bool nation_is_in_group(const struct nation_type
*pnation
,
1074 const struct nation_group
*pgroup
)
1076 fc_assert_ret_val(NULL
!= pnation
, FALSE
);
1078 nation_group_list_iterate(pnation
->groups
, agroup
) {
1079 if (agroup
== pgroup
) {
1082 } nation_group_list_iterate_end
;
1086 /****************************************************************************
1087 Nation group iterator.
1088 ****************************************************************************/
1089 struct nation_group_iter
{
1090 struct iterator vtable
;
1091 struct nation_group
*p
, *end
;
1093 #define NATION_GROUP_ITER(p) ((struct nation_group_iter *)(p))
1095 /****************************************************************************
1096 Implementation of iterator 'sizeof' function.
1097 ****************************************************************************/
1098 size_t nation_group_iter_sizeof(void)
1100 return sizeof(struct nation_group_iter
);
1103 /****************************************************************************
1104 Implementation of iterator 'next' function.
1105 ****************************************************************************/
1106 static void nation_group_iter_next(struct iterator
*iter
)
1108 NATION_GROUP_ITER(iter
)->p
++;
1111 /****************************************************************************
1112 Implementation of iterator 'get' function.
1113 ****************************************************************************/
1114 static void *nation_group_iter_get(const struct iterator
*iter
)
1116 return NATION_GROUP_ITER(iter
)->p
;
1119 /****************************************************************************
1120 Implementation of iterator 'valid' function.
1121 ****************************************************************************/
1122 static bool nation_group_iter_valid(const struct iterator
*iter
)
1124 struct nation_group_iter
*it
= NATION_GROUP_ITER(iter
);
1125 return it
->p
< it
->end
;
1128 /****************************************************************************
1129 Implementation of iterator 'init' function.
1130 ****************************************************************************/
1131 struct iterator
*nation_group_iter_init(struct nation_group_iter
*it
)
1133 it
->vtable
.next
= nation_group_iter_next
;
1134 it
->vtable
.get
= nation_group_iter_get
;
1135 it
->vtable
.valid
= nation_group_iter_valid
;
1136 it
->p
= nation_groups
;
1137 it
->end
= nation_groups
+ nation_group_count();
1138 return ITERATOR(it
);
1141 /****************************************************************************
1142 Initializes all nation set/group data.
1143 ****************************************************************************/
1144 void nation_sets_groups_init(void)
1146 num_nation_sets
= num_nation_groups
= 0;
1149 /****************************************************************************
1150 Frees and resets all nation set/group data.
1151 ****************************************************************************/
1152 void nation_sets_groups_free(void)
1154 num_nation_sets
= num_nation_groups
= 0;
1157 /****************************************************************************
1158 Return TRUE iff the editor is allowed to edit the player's nation in
1160 ****************************************************************************/
1161 bool can_conn_edit_players_nation(const struct connection
*pconn
,
1162 const struct player
*pplayer
)
1164 return (can_conn_edit(pconn
)
1165 || (game
.info
.is_new_game
1166 && ((!pconn
->observer
&& pconn
->playing
== pplayer
)
1167 || pconn
->access_level
>= ALLOW_CTRL
)));
1170 /****************************************************************************
1171 Returns how much two nations look good in the same game.
1172 Nations in the same group are considered to match, if that nation group
1173 has a 'match' greater than zero.
1174 Negative return value means that we really really don't want these
1175 nations together. This is dictated by "conflicts_with" in individual
1176 nation definitions. (If 'ignore_conflicts' is set, conflicts are not
1177 taken into account at all.)
1178 Server side function.
1179 ****************************************************************************/
1180 int nations_match(const struct nation_type
*pnation1
,
1181 const struct nation_type
*pnation2
,
1182 bool ignore_conflicts
)
1184 bool in_conflict
= FALSE
;
1187 fc_assert_ret_val(is_server(), -1);
1188 NATION_CHECK(pnation1
, return -1);
1189 NATION_CHECK(pnation2
, return -1);
1191 if (!ignore_conflicts
) {
1192 nation_list_iterate(pnation1
->server
.conflicts_with
, pnation0
) {
1193 if (pnation0
== pnation2
) {
1195 sum
= 1; /* Be sure to returns something negative. */
1198 } nation_list_iterate_end
;
1201 nation_list_iterate(pnation2
->server
.conflicts_with
, pnation0
) {
1202 if (pnation0
== pnation1
) {
1204 sum
= 1; /* Be sure to returns something negative. */
1207 } nation_list_iterate_end
;
1211 nation_group_list_iterate(pnation1
->groups
, pgroup
) {
1212 if (nation_is_in_group(pnation2
, pgroup
)) {
1213 sum
+= pgroup
->server
.match
;
1215 } nation_group_list_iterate_end
;
1217 return (in_conflict
? -sum
: sum
);