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 /* Nation group structure. */
47 struct name_translation name
;
51 /* Only used in the server (./server/). */
53 /* How much the AI will try to select a nation in the same group */
58 /* Only used at the client. */
64 static struct nation_type
*nations
= NULL
;
66 static int num_nation_sets
;
67 static struct nation_set nation_sets
[MAX_NUM_NATION_SETS
];
68 static int num_nation_groups
;
69 static struct nation_group nation_groups
[MAX_NUM_NATION_GROUPS
];
71 /****************************************************************************
72 Runs action if the nation is not valid.
73 ****************************************************************************/
75 #define NATION_CHECK(pnation, action) \
76 fc_assert_action(nation_check(pnation, \
77 log_do_output_for_level(LOG_ERROR), \
78 __FILE__, __FUNCTION__, __FC_LINE__), \
81 #define NATION_CHECK(pnation, action) /* Do Nothing. */
84 /****************************************************************************
85 Returns TRUE if the nation is valid, else, print an error message and
87 ****************************************************************************/
88 static inline bool nation_check(const struct nation_type
*pnation
,
89 bool do_output
, const char *file
,
90 const char *function
, int line
)
92 if (0 == nation_count()) {
94 do_log(file
, function
, line
, TRUE
, LOG_ERROR
,
95 "Function called before nations setup.");
99 if (NULL
== pnation
) {
101 do_log(file
, function
, line
, TRUE
, LOG_ERROR
,
102 "This function has NULL nation argument.");
106 if (pnation
->item_number
< 0
107 || pnation
->item_number
>= nation_count()
108 || &nations
[nation_index(pnation
)] != pnation
) {
110 do_log(file
, function
, line
, TRUE
, LOG_ERROR
,
111 "This function has bad nation number %d (count %d).",
112 pnation
->item_number
, nation_count());
119 /****************************************************************************
120 Returns the nation that has the given (translated) plural noun.
121 Returns NO_NATION_SELECTED if none match.
122 ****************************************************************************/
123 struct nation_type
*nation_by_translated_plural(const char *name
)
125 nations_iterate(pnation
) {
126 if (0 == strcmp(nation_plural_translation(pnation
), name
)) {
129 } nations_iterate_end
;
131 return NO_NATION_SELECTED
;
134 /****************************************************************************
135 Returns the nation that has the given (untranslated) rule name (adjective).
136 Returns NO_NATION_SELECTED if none match.
137 ****************************************************************************/
138 struct nation_type
*nation_by_rule_name(const char *name
)
140 const char *qname
= Qn_(name
);
142 nations_iterate(pnation
) {
143 if (0 == fc_strcasecmp(nation_rule_name(pnation
), qname
)) {
146 } nations_iterate_end
;
148 return NO_NATION_SELECTED
;
151 /****************************************************************************
152 Return the (untranslated) rule name of the nation (adjective form).
153 You don't have to free the return pointer.
154 ****************************************************************************/
155 const char *nation_rule_name(const struct nation_type
*pnation
)
157 NATION_CHECK(pnation
, return "");
158 return rule_name(&pnation
->adjective
);
161 /****************************************************************************
162 Return the (translated) adjective for the given nation.
163 You don't have to free the return pointer.
164 ****************************************************************************/
165 const char *nation_adjective_translation(const struct nation_type
*pnation
)
167 NATION_CHECK(pnation
, return "");
168 return name_translation(&pnation
->adjective
);
171 /****************************************************************************
172 Return the (translated) plural noun of the given nation.
173 You don't have to free the return pointer.
174 ****************************************************************************/
175 const char *nation_plural_translation(const struct nation_type
*pnation
)
177 NATION_CHECK(pnation
, return "");
178 return name_translation(&pnation
->noun_plural
);
181 /****************************************************************************
182 Return the (translated) adjective for the given nation of a player.
183 You don't have to free the return pointer.
184 ****************************************************************************/
185 const char *nation_adjective_for_player(const struct player
*pplayer
)
187 return nation_adjective_translation(nation_of_player(pplayer
));
190 /****************************************************************************
191 Return the (translated) plural noun of the given nation of a player.
192 You don't have to free the return pointer.
193 ****************************************************************************/
194 const char *nation_plural_for_player(const struct player
*pplayer
)
196 return nation_plural_translation(nation_of_player(pplayer
));
199 /****************************************************************************
200 Return whether a nation is "pickable" -- whether players can select it
202 (Client only function -- on the server, use client_can_pick_nation().)
203 ****************************************************************************/
204 bool is_nation_pickable(const struct nation_type
*nation
)
206 fc_assert_ret_val(!is_server(), FALSE
);
207 return nation
->client
.is_pickable
;
210 /****************************************************************************
211 Return whether a nation is "playable"; i.e., whether a human player can
212 choose this nation. Barbarian and observer nations are not playable.
214 This does not check whether a nation is "used" or "available".
215 ****************************************************************************/
216 bool is_nation_playable(const struct nation_type
*nation
)
218 NATION_CHECK(nation
, return FALSE
);
219 return nation
->is_playable
;
222 /****************************************************************************
223 Returns which kind of barbarians can use this nation.
225 This does not check whether a nation is "used" or "available".
226 ****************************************************************************/
227 enum barbarian_type
nation_barbarian_type(const struct nation_type
*nation
)
229 NATION_CHECK(nation
, return NOT_A_BARBARIAN
);
230 return nation
->barb_type
;
234 /****************************************************************************
236 ****************************************************************************/
237 struct nation_leader
{
242 /****************************************************************************
243 Returns the list the nation leader names.
244 ****************************************************************************/
245 const struct nation_leader_list
*
246 nation_leaders(const struct nation_type
*pnation
)
248 NATION_CHECK(pnation
, return NULL
);
249 return pnation
->leaders
;
252 /****************************************************************************
253 Create a new leader for the nation.
254 ****************************************************************************/
255 struct nation_leader
*nation_leader_new(struct nation_type
*pnation
,
256 const char *name
, bool is_male
)
258 struct nation_leader
*pleader
;
260 NATION_CHECK(pnation
, return NULL
);
261 pleader
= fc_malloc(sizeof(*pleader
));
262 pleader
->name
= fc_strdup(name
);
263 pleader
->is_male
= is_male
;
265 nation_leader_list_append(pnation
->leaders
, pleader
);
269 /****************************************************************************
270 Destroy a nation leader created with nation_leader_new().
271 ****************************************************************************/
272 static void nation_leader_destroy(struct nation_leader
*pleader
)
278 /****************************************************************************
279 Returns the nation leader structure which match 'name' or NULL if not
281 ****************************************************************************/
282 struct nation_leader
*
283 nation_leader_by_name(const struct nation_type
*pnation
, const char *name
)
285 NATION_CHECK(pnation
, return NULL
);
286 nation_leader_list_iterate(pnation
->leaders
, pleader
) {
287 if (0 == fc_strcasecmp(name
, pleader
->name
)) {
290 } nation_leader_list_iterate_end
;
294 /****************************************************************************
295 Return the name of the nation leader.
296 ****************************************************************************/
297 const char *nation_leader_name(const struct nation_leader
*pleader
)
299 fc_assert_ret_val(NULL
!= pleader
, NULL
);
300 return pleader
->name
;
303 /****************************************************************************
304 Return the sex of the nation leader.
305 ****************************************************************************/
306 bool nation_leader_is_male(const struct nation_leader
*pleader
)
308 fc_assert_ret_val(NULL
!= pleader
, TRUE
);
309 return pleader
->is_male
;
312 /****************************************************************************
313 Return translated version of nation legend.
314 ****************************************************************************/
315 const char *nation_legend_translation(const struct nation_type
*pnation
,
318 if (pnation
->translation_domain
== NULL
) {
322 return DG_(pnation
->translation_domain
, legend
);
325 /****************************************************************************
326 Nation default cities. The nation_city structure holds information about
327 a default choice for the city name. The 'name' field is, of course, just
328 the name for the city. The 'river' and 'terrain' fields are entries
329 recording whether the terrain is present near the city - we give higher
330 priority to cities which have matching terrain. In the case of a river we
331 only care if the city is _on_ the river, for other terrain features we
332 give the bonus if the city is close to the terrain.
334 This is controlled through the nation's ruleset like this:
335 cities = "Washington (ocean, river, swamp)", "New York (!mountains)"
336 ****************************************************************************/
339 enum nation_city_preference river
;
340 enum nation_city_preference terrain
[MAX_NUM_TERRAINS
];
343 /****************************************************************************
344 Return the default cities of the nation (server only function).
345 ****************************************************************************/
346 const struct nation_city_list
*
347 nation_cities(const struct nation_type
*pnation
)
349 NATION_CHECK(pnation
, return NULL
);
350 fc_assert_ret_val(is_server(), NULL
);
352 return pnation
->server
.default_cities
;
355 /****************************************************************************
356 Create a new default city for the nation (server only function).
357 ****************************************************************************/
358 struct nation_city
*nation_city_new(struct nation_type
*pnation
,
361 struct nation_city
*pncity
;
363 NATION_CHECK(pnation
, return NULL
);
364 fc_assert_ret_val(is_server(), NULL
);
366 fc_assert(0 == NCP_NONE
);
367 pncity
= fc_calloc(1, sizeof(*pncity
)); /* Set NCP_NONE. */
368 pncity
->name
= fc_strdup(name
);
370 nation_city_list_append(pnation
->server
.default_cities
, pncity
);
374 /****************************************************************************
375 Destroy a default nation city created with nation_city_new().
376 ****************************************************************************/
377 static void nation_city_destroy(struct nation_city
*pncity
)
383 /****************************************************************************
384 Reverts the nation city preference.
385 ****************************************************************************/
386 enum nation_city_preference
387 nation_city_preference_revert(enum nation_city_preference prefer
)
398 log_error("%s(): Wrong nation_city_preference variant (%d).",
399 __FUNCTION__
, prefer
);
403 /****************************************************************************
404 Set the default nation city preference for the terrain.
405 ****************************************************************************/
406 void nation_city_set_terrain_preference(struct nation_city
*pncity
,
407 const struct terrain
*pterrain
,
408 enum nation_city_preference prefer
)
410 fc_assert_ret(NULL
!= pncity
);
411 fc_assert_ret(NULL
!= pterrain
);
412 pncity
->terrain
[terrain_index(pterrain
)] = prefer
;
415 /****************************************************************************
416 Set the default nation city preference about rivers.
417 ****************************************************************************/
418 void nation_city_set_river_preference(struct nation_city
*pncity
,
419 enum nation_city_preference prefer
)
421 fc_assert_ret(NULL
!= pncity
);
422 pncity
->river
= prefer
;
425 /****************************************************************************
426 Return the name of the default nation city.
427 ****************************************************************************/
428 const char *nation_city_name(const struct nation_city
*pncity
)
430 fc_assert_ret_val(NULL
!= pncity
, NULL
);
434 /****************************************************************************
435 Return the default nation city preference for the terrain.
436 ****************************************************************************/
437 enum nation_city_preference
438 nation_city_terrain_preference(const struct nation_city
*pncity
,
439 const struct terrain
*pterrain
)
441 fc_assert_ret_val(NULL
!= pncity
, NCP_DISLIKE
);
442 fc_assert_ret_val(NULL
!= pterrain
, NCP_DISLIKE
);
443 return pncity
->terrain
[terrain_index(pterrain
)];
446 /****************************************************************************
447 Return the default nation city preference for rivers.
448 ****************************************************************************/
449 enum nation_city_preference
450 nation_city_river_preference(const struct nation_city
*pncity
)
452 fc_assert_ret_val(NULL
!= pncity
, NCP_DISLIKE
);
453 return pncity
->river
;
457 /****************************************************************************
458 Return the nation of a player.
459 ****************************************************************************/
460 struct nation_type
*nation_of_player(const struct player
*pplayer
)
462 fc_assert_ret_val(NULL
!= pplayer
, NULL
);
463 NATION_CHECK(pplayer
->nation
, return NULL
);
464 return pplayer
->nation
;
467 /****************************************************************************
468 Return the nation of the player who owns the city.
469 ****************************************************************************/
470 struct nation_type
*nation_of_city(const struct city
*pcity
)
472 fc_assert_ret_val(pcity
!= NULL
, NULL
);
473 return nation_of_player(city_owner(pcity
));
476 /****************************************************************************
477 Return the nation of the player who owns the unit.
478 ****************************************************************************/
479 struct nation_type
*nation_of_unit(const struct unit
*punit
)
481 fc_assert_ret_val(punit
!= NULL
, NULL
);
482 return nation_of_player(unit_owner(punit
));
485 /****************************************************************************
486 Return the nation with the given index.
488 This function returns NULL for an out-of-range index (some callers
490 ****************************************************************************/
491 struct nation_type
*nation_by_number(const Nation_type_id nation
)
493 if (nation
< 0 || nation
>= game
.control
.nation_count
) {
496 return nations
+ nation
;
499 /**************************************************************************
500 Return the nation index.
501 **************************************************************************/
502 Nation_type_id
nation_number(const struct nation_type
*pnation
)
504 fc_assert_ret_val(NULL
!= pnation
, -1);
505 return pnation
->item_number
;
508 /**************************************************************************
509 Return the nation index.
511 Currently same as nation_number(), paired with nation_count()
512 indicates use as an array index.
513 **************************************************************************/
514 Nation_type_id
nation_index(const struct nation_type
*pnation
)
516 fc_assert_ret_val(NULL
!= pnation
, -1);
517 return pnation
- nations
;
520 /****************************************************************************
521 Return the number of nations.
522 ****************************************************************************/
523 Nation_type_id
nation_count(void)
525 return game
.control
.nation_count
;
529 /****************************************************************************
531 ****************************************************************************/
533 struct iterator vtable
;
534 struct nation_type
*p
, *end
;
536 #define NATION_ITER(p) ((struct nation_iter *)(p))
538 /****************************************************************************
539 Implementation of iterator 'sizeof' function.
540 ****************************************************************************/
541 size_t nation_iter_sizeof(void)
543 return sizeof(struct nation_iter
);
546 /****************************************************************************
547 Implementation of iterator 'next' function.
548 ****************************************************************************/
549 static void nation_iter_next(struct iterator
*iter
)
551 NATION_ITER(iter
)->p
++;
554 /****************************************************************************
555 Implementation of iterator 'get' function.
556 ****************************************************************************/
557 static void *nation_iter_get(const struct iterator
*iter
)
559 return NATION_ITER(iter
)->p
;
562 /****************************************************************************
563 Implementation of iterator 'valid' function.
564 ****************************************************************************/
565 static bool nation_iter_valid(const struct iterator
*iter
)
567 struct nation_iter
*it
= NATION_ITER(iter
);
568 return it
->p
< it
->end
;
571 /****************************************************************************
572 Implementation of iterator 'init' function.
573 ****************************************************************************/
574 struct iterator
*nation_iter_init(struct nation_iter
*it
)
576 it
->vtable
.next
= nation_iter_next
;
577 it
->vtable
.get
= nation_iter_get
;
578 it
->vtable
.valid
= nation_iter_valid
;
580 it
->end
= nations
+ nation_count();
584 /****************************************************************************
585 Allocate resources associated with the given nation.
586 ****************************************************************************/
587 static void nation_init(struct nation_type
*pnation
)
589 memset(pnation
, 0, sizeof(*pnation
));
591 pnation
->item_number
= pnation
- nations
;
592 pnation
->translation_domain
= NULL
;
593 pnation
->leaders
= nation_leader_list_new_full(nation_leader_destroy
);
594 pnation
->sets
= nation_set_list_new();
595 pnation
->groups
= nation_group_list_new();
598 pnation
->server
.default_cities
=
599 nation_city_list_new_full(nation_city_destroy
);
600 pnation
->server
.civilwar_nations
= nation_list_new();
601 pnation
->server
.parent_nations
= nation_list_new();
602 pnation
->server
.conflicts_with
= nation_list_new();
603 /* server.rgb starts out NULL */
604 pnation
->server
.traits
= fc_calloc(TRAIT_COUNT
,
605 sizeof(*pnation
->server
.traits
));
609 /****************************************************************************
610 De-allocate resources associated with the given nation.
611 ****************************************************************************/
612 static void nation_free(struct nation_type
*pnation
)
614 free(pnation
->legend
);
615 FC_FREE(pnation
->translation_domain
);
616 nation_leader_list_destroy(pnation
->leaders
);
617 nation_set_list_destroy(pnation
->sets
);
618 nation_group_list_destroy(pnation
->groups
);
621 nation_city_list_destroy(pnation
->server
.default_cities
);
622 nation_list_destroy(pnation
->server
.civilwar_nations
);
623 nation_list_destroy(pnation
->server
.parent_nations
);
624 nation_list_destroy(pnation
->server
.conflicts_with
);
625 rgbcolor_destroy(pnation
->server
.rgb
);
626 FC_FREE(pnation
->server
.traits
);
629 memset(pnation
, 0, sizeof(*pnation
));
632 /****************************************************************************
633 Allocate space for the given number of nations.
634 ****************************************************************************/
635 void nations_alloc(int num
)
639 nations
= fc_malloc(sizeof(*nations
) * num
);
640 game
.control
.nation_count
= num
;
642 for (i
= 0; i
< num
; i
++) {
643 nation_init(nations
+ i
);
647 /****************************************************************************
648 De-allocate the currently allocated nations.
649 ****************************************************************************/
650 void nations_free(void)
654 if (NULL
== nations
) {
658 for (i
= 0; i
< game
.control
.nation_count
; i
++) {
659 nation_free(nations
+ i
);
664 game
.control
.nation_count
= 0;
667 /****************************************************************************
668 Returns nation's city style.
669 ****************************************************************************/
670 int city_style_of_nation(const struct nation_type
*pnation
)
672 NATION_CHECK(pnation
, return 0);
673 return pnation
->city_style
;
676 /****************************************************************************
677 Returns nation's player color preference, or NULL if none.
678 Server only function.
679 ****************************************************************************/
680 const struct rgbcolor
*nation_color(const struct nation_type
*pnation
)
682 NATION_CHECK(pnation
, return NULL
);
683 return pnation
->server
.rgb
;
686 /****************************************************************************
687 Return the number of nation sets.
688 ****************************************************************************/
689 int nation_set_count(void)
691 return num_nation_sets
;
694 /****************************************************************************
695 Return the nation set index.
696 ****************************************************************************/
697 int nation_set_index(const struct nation_set
*pset
)
699 fc_assert_ret_val(NULL
!= pset
, -1);
700 return pset
- nation_sets
;
703 /****************************************************************************
704 Return the nation set index.
705 ****************************************************************************/
706 int nation_set_number(const struct nation_set
*pset
)
708 return nation_set_index(pset
);
711 /****************************************************************************
712 Add new set into the array of nation sets.
713 ****************************************************************************/
714 struct nation_set
*nation_set_new(const char *set_name
,
715 const char *set_rule_name
,
716 const char *set_description
)
718 struct nation_set
*pset
;
720 if (MAX_NUM_NATION_SETS
<= num_nation_sets
) {
721 log_error("Too many nation sets (%d is the maximum).",
722 MAX_NUM_NATION_SETS
);
726 /* Print the name and truncate if needed. */
727 pset
= nation_sets
+ num_nation_sets
;
728 names_set(&pset
->name
, NULL
, set_name
, set_rule_name
);
729 (void) sz_loud_strlcpy(pset
->description
, set_description
,
730 "Nation set description \"%s\" too long; truncating.");
731 if (NULL
!= nation_set_by_rule_name(rule_name(&pset
->name
))) {
732 log_error("Duplicate nation set name %s.", rule_name(&pset
->name
));
735 if (NULL
!= nation_group_by_rule_name(rule_name(&pset
->name
))) {
736 log_error("Nation set name %s is already used for a group.",
737 rule_name(&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(&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
);
795 return rule_name(&pset
->name
);
798 /****************************************************************************
799 Return the translated name of a nation set.
800 You don't have to free the return pointer.
801 ****************************************************************************/
802 const char *nation_set_name_translation(const struct nation_set
*pset
)
804 fc_assert_ret_val(NULL
!= pset
, NULL
);
805 return name_translation(&pset
->name
);
808 /****************************************************************************
809 Return the (untranslated) user description of a nation set.
810 You don't have to free the return pointer.
811 ****************************************************************************/
812 const char *nation_set_description(const struct nation_set
*pset
)
814 fc_assert_ret_val(NULL
!= pset
, NULL
);
815 return pset
->description
;
818 /****************************************************************************
819 Check if the given nation is in a given set
820 ****************************************************************************/
821 bool nation_is_in_set(const struct nation_type
*pnation
,
822 const struct nation_set
*pset
)
824 fc_assert_ret_val(NULL
!= pnation
, FALSE
);
826 nation_set_list_iterate(pnation
->sets
, aset
) {
830 } nation_set_list_iterate_end
;
834 /****************************************************************************
835 Returns the nation set that would be selected by the given value of the
836 'nationset' server setting.
837 This differs from nation_set_by_rule_name() for the empty string, where
838 the first (ruleset default) nationset will be used; and similarly for
839 a nationset not matched in the ruleset.
840 The knowledge of the interpretation of this setting encapsulated here is
841 required on both server and client.
842 ****************************************************************************/
843 struct nation_set
*nation_set_by_setting_value(const char *setting
)
845 struct nation_set
*pset
= NULL
;
847 if (strlen(setting
) > 0) {
848 pset
= nation_set_by_rule_name(setting
);
851 /* Either no nation set specified, or the specified one isn't in the
852 * current ruleset. Default to the first nation set specified by
854 pset
= nation_set_by_number(0);
856 fc_assert(pset
!= NULL
);
861 /****************************************************************************
863 ****************************************************************************/
864 struct nation_set_iter
{
865 struct iterator vtable
;
866 struct nation_set
*p
, *end
;
868 #define NATION_SET_ITER(p) ((struct nation_set_iter *)(p))
870 /****************************************************************************
871 Implementation of iterator 'sizeof' function.
872 ****************************************************************************/
873 size_t nation_set_iter_sizeof(void)
875 return sizeof(struct nation_set_iter
);
878 /****************************************************************************
879 Implementation of iterator 'next' function.
880 ****************************************************************************/
881 static void nation_set_iter_next(struct iterator
*iter
)
883 NATION_SET_ITER(iter
)->p
++;
886 /****************************************************************************
887 Implementation of iterator 'get' function.
888 ****************************************************************************/
889 static void *nation_set_iter_get(const struct iterator
*iter
)
891 return NATION_SET_ITER(iter
)->p
;
894 /****************************************************************************
895 Implementation of iterator 'valid' function.
896 ****************************************************************************/
897 static bool nation_set_iter_valid(const struct iterator
*iter
)
899 struct nation_set_iter
*it
= NATION_SET_ITER(iter
);
900 return it
->p
< it
->end
;
903 /****************************************************************************
904 Implementation of iterator 'init' function.
905 ****************************************************************************/
906 struct iterator
*nation_set_iter_init(struct nation_set_iter
*it
)
908 it
->vtable
.next
= nation_set_iter_next
;
909 it
->vtable
.get
= nation_set_iter_get
;
910 it
->vtable
.valid
= nation_set_iter_valid
;
912 it
->end
= nation_sets
+ nation_set_count();
916 /****************************************************************************
917 Return the number of nation groups.
918 ****************************************************************************/
919 int nation_group_count(void)
921 return num_nation_groups
;
924 /****************************************************************************
925 Return the nation group index.
926 ****************************************************************************/
927 int nation_group_index(const struct nation_group
*pgroup
)
929 fc_assert_ret_val(NULL
!= pgroup
, -1);
930 return pgroup
- nation_groups
;
933 /****************************************************************************
934 Return the nation group index.
935 ****************************************************************************/
936 int nation_group_number(const struct nation_group
*pgroup
)
938 return nation_group_index(pgroup
);
941 /****************************************************************************
942 Add new group into the array of groups.
943 ****************************************************************************/
944 struct nation_group
*nation_group_new(const char *name
)
946 struct nation_group
*pgroup
;
948 if (MAX_NUM_NATION_GROUPS
<= num_nation_groups
) {
949 log_error("Too many nation groups (%d is the maximum).",
950 MAX_NUM_NATION_GROUPS
);
954 /* Print the name and truncate if needed. */
955 pgroup
= nation_groups
+ num_nation_groups
;
956 name_set(&pgroup
->name
, NULL
, name
);
957 if (NULL
!= nation_group_by_rule_name(rule_name(&pgroup
->name
))) {
958 log_error("Duplicate nation group name %s.", rule_name(&pgroup
->name
));
961 if (NULL
!= nation_set_by_rule_name(rule_name(&pgroup
->name
))) {
962 log_error("Nation group name %s is already used for a set.",
963 rule_name(&pgroup
->name
));
968 pgroup
->server
.match
= 0;
975 /****************************************************************************
976 Return the nation group with the given index.
978 This function returns NULL for an out-of-range index (some callers
980 ****************************************************************************/
981 struct nation_group
*nation_group_by_number(int id
)
983 if (id
< 0 || id
>= num_nation_groups
) {
986 return nation_groups
+ id
;
989 /****************************************************************************
990 Return the nation group that has the given (untranslated) rule name.
991 Returns NULL if no group is found.
992 ****************************************************************************/
993 struct nation_group
*nation_group_by_rule_name(const char *name
)
995 const char *qname
= Qn_(name
);
997 nation_groups_iterate(pgroup
) {
998 if (0 == fc_strcasecmp(rule_name(&pgroup
->name
), qname
)) {
1001 } nation_groups_iterate_end
;
1006 /****************************************************************************
1007 Set how much the AI will try to select a nation in the same group.
1008 Server only function.
1009 ****************************************************************************/
1010 void nation_group_set_match(struct nation_group
*pgroup
, int match
)
1012 fc_assert_ret(is_server());
1013 fc_assert_ret(NULL
!= pgroup
);
1014 pgroup
->server
.match
= match
;
1017 /****************************************************************************
1018 Return the untranslated name of a nation group (including qualifier,
1020 You usually want nation_group_rule_name() instead.
1021 You don't have to free the return pointer.
1022 ****************************************************************************/
1023 const char *nation_group_untranslated_name(const struct nation_group
*pgroup
)
1025 fc_assert_ret_val(NULL
!= pgroup
, NULL
);
1026 return untranslated_name(&pgroup
->name
);
1029 /****************************************************************************
1030 Return the (untranslated) rule name of a nation group.
1031 You don't have to free the return pointer.
1032 ****************************************************************************/
1033 const char *nation_group_rule_name(const struct nation_group
*pgroup
)
1035 fc_assert_ret_val(NULL
!= pgroup
, NULL
);
1036 return rule_name(&pgroup
->name
);
1039 /****************************************************************************
1040 Return the translated name of a nation group.
1041 You don't have to free the return pointer.
1042 ****************************************************************************/
1043 const char *nation_group_name_translation(const struct nation_group
*pgroup
)
1045 fc_assert_ret_val(NULL
!= pgroup
, NULL
);
1046 return name_translation(&pgroup
->name
);
1049 /****************************************************************************
1050 Check if the given nation is in a given group
1051 ****************************************************************************/
1052 bool nation_is_in_group(const struct nation_type
*pnation
,
1053 const struct nation_group
*pgroup
)
1055 fc_assert_ret_val(NULL
!= pnation
, FALSE
);
1057 nation_group_list_iterate(pnation
->groups
, agroup
) {
1058 if (agroup
== pgroup
) {
1061 } nation_group_list_iterate_end
;
1065 /****************************************************************************
1066 Nation group iterator.
1067 ****************************************************************************/
1068 struct nation_group_iter
{
1069 struct iterator vtable
;
1070 struct nation_group
*p
, *end
;
1072 #define NATION_GROUP_ITER(p) ((struct nation_group_iter *)(p))
1074 /****************************************************************************
1075 Implementation of iterator 'sizeof' function.
1076 ****************************************************************************/
1077 size_t nation_group_iter_sizeof(void)
1079 return sizeof(struct nation_group_iter
);
1082 /****************************************************************************
1083 Implementation of iterator 'next' function.
1084 ****************************************************************************/
1085 static void nation_group_iter_next(struct iterator
*iter
)
1087 NATION_GROUP_ITER(iter
)->p
++;
1090 /****************************************************************************
1091 Implementation of iterator 'get' function.
1092 ****************************************************************************/
1093 static void *nation_group_iter_get(const struct iterator
*iter
)
1095 return NATION_GROUP_ITER(iter
)->p
;
1098 /****************************************************************************
1099 Implementation of iterator 'valid' function.
1100 ****************************************************************************/
1101 static bool nation_group_iter_valid(const struct iterator
*iter
)
1103 struct nation_group_iter
*it
= NATION_GROUP_ITER(iter
);
1104 return it
->p
< it
->end
;
1107 /****************************************************************************
1108 Implementation of iterator 'init' function.
1109 ****************************************************************************/
1110 struct iterator
*nation_group_iter_init(struct nation_group_iter
*it
)
1112 it
->vtable
.next
= nation_group_iter_next
;
1113 it
->vtable
.get
= nation_group_iter_get
;
1114 it
->vtable
.valid
= nation_group_iter_valid
;
1115 it
->p
= nation_groups
;
1116 it
->end
= nation_groups
+ nation_group_count();
1117 return ITERATOR(it
);
1120 /****************************************************************************
1121 Initializes all nation set/group data.
1122 ****************************************************************************/
1123 void nation_sets_groups_init(void)
1125 num_nation_sets
= num_nation_groups
= 0;
1128 /****************************************************************************
1129 Frees and resets all nation set/group data.
1130 ****************************************************************************/
1131 void nation_sets_groups_free(void)
1133 num_nation_sets
= num_nation_groups
= 0;
1136 /****************************************************************************
1137 Return TRUE iff the editor is allowed to edit the player's nation in
1139 ****************************************************************************/
1140 bool can_conn_edit_players_nation(const struct connection
*pconn
,
1141 const struct player
*pplayer
)
1143 return (can_conn_edit(pconn
)
1144 || (game
.info
.is_new_game
1145 && ((!pconn
->observer
&& pconn
->playing
== pplayer
)
1146 || pconn
->access_level
>= ALLOW_CTRL
)));
1149 /****************************************************************************
1150 Returns how much two nations look good in the same game.
1151 Nations in the same group are considered to match, if that nation group
1152 has a 'match' greater than zero.
1153 Negative return value means that we really really don't want these
1154 nations together. This is dictated by "conflicts_with" in individual
1155 nation definitions. (If 'ignore_conflicts' is set, conflicts are not
1156 taken into account at all.)
1157 Server side function.
1158 ****************************************************************************/
1159 int nations_match(const struct nation_type
*pnation1
,
1160 const struct nation_type
*pnation2
,
1161 bool ignore_conflicts
)
1163 bool in_conflict
= FALSE
;
1166 fc_assert_ret_val(is_server(), -1);
1167 NATION_CHECK(pnation1
, return -1);
1168 NATION_CHECK(pnation2
, return -1);
1170 if (!ignore_conflicts
) {
1171 nation_list_iterate(pnation1
->server
.conflicts_with
, pnation0
) {
1172 if (pnation0
== pnation2
) {
1174 sum
= 1; /* Be sure to returns something negative. */
1177 } nation_list_iterate_end
;
1180 nation_list_iterate(pnation2
->server
.conflicts_with
, pnation0
) {
1181 if (pnation0
== pnation1
) {
1183 sum
= 1; /* Be sure to returns something negative. */
1186 } nation_list_iterate_end
;
1190 nation_group_list_iterate(pnation1
->groups
, pgroup
) {
1191 if (nation_is_in_group(pnation2
, pgroup
)) {
1192 sum
+= pgroup
->server
.match
;
1194 } nation_group_list_iterate_end
;
1196 return (in_conflict
? -sum
: sum
);