1 /***********************************************************************
2 Freeciv - Copyright (C) 2003 - 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 creating barbarians in huts, land and sea
16 Started by Jerzy Klek <jekl@altavista.net>
17 with more ideas from Falk Hueffner
18 ***********************************************************************/
21 #include <fc_config.h>
38 #include "government.h"
49 #include "citytools.h"
55 #include "stdinhand.h"
56 #include "techtools.h"
58 #include "unittools.h"
64 #include "difficulty.h"
66 #include "barbarian.h"
68 #define BARBARIAN_INITIAL_VISION_RADIUS 3
69 #define BARBARIAN_INITIAL_VISION_RADIUS_SQ 9
71 /**************************************************************************
72 Is player a land barbarian?
73 **************************************************************************/
74 bool is_land_barbarian(struct player
*pplayer
)
76 return (pplayer
->ai_common
.barbarian_type
== LAND_BARBARIAN
);
79 /**************************************************************************
80 Is player a sea barbarian?
81 **************************************************************************/
82 bool is_sea_barbarian(struct player
*pplayer
)
84 return (pplayer
->ai_common
.barbarian_type
== SEA_BARBARIAN
);
87 /**************************************************************************
88 Creates the land/sea barbarian player and inits some stuff. If
89 barbarian player already exists, return player pointer. If barbarians
90 are dead, revive them with a new leader :-)
92 Dead barbarians forget the map and lose the money.
93 **************************************************************************/
94 struct player
*create_barbarian_player(enum barbarian_type type
)
96 struct player
*barbarians
;
97 struct nation_type
*nation
;
98 struct research
*presearch
;
100 players_iterate(old_barbs
) {
101 if ((type
== LAND_BARBARIAN
&& is_land_barbarian(old_barbs
))
102 || (type
== SEA_BARBARIAN
&& is_sea_barbarian(old_barbs
))) {
103 if (!old_barbs
->is_alive
) {
104 old_barbs
->economic
.gold
= 0;
105 old_barbs
->is_alive
= TRUE
;
106 player_status_reset(old_barbs
);
108 /* Free old name so pick_random_player_name() can select it again.
109 * This is needed in case ruleset defines just one leader for
110 * barbarian nation. */
111 old_barbs
->name
[0] = '\0';
112 server_player_set_name(old_barbs
,
113 pick_random_player_name(nation_of_player(old_barbs
)));
114 sz_strlcpy(old_barbs
->username
, _(ANON_USER_NAME
));
115 old_barbs
->unassigned_user
= TRUE
;
116 /* I need to make them to forget the map, I think */
117 whole_map_iterate(ptile
) {
118 map_clear_known(ptile
, old_barbs
);
119 } whole_map_iterate_end
;
121 old_barbs
->economic
.gold
+= 100; /* New leader, new money */
125 } players_iterate_end
;
127 /* make a new player, or not */
128 barbarians
= server_create_player(-1, default_ai_type_name(), NULL
, FALSE
);
132 server_player_init(barbarians
, TRUE
, TRUE
);
134 nation
= pick_a_nation(NULL
, FALSE
, FALSE
, type
);
136 /* Ruleset loading time checks should guarantee that there always is
137 suitable nation available */
138 fc_assert(nation
!= NULL
);
140 player_nation_defaults(barbarians
, nation
, TRUE
);
141 if (game_was_started()) {
142 /* Find a color for the new player. */
143 assign_player_colors();
146 server
.nbarbarians
++;
148 sz_strlcpy(barbarians
->username
, _(ANON_USER_NAME
));
149 barbarians
->unassigned_user
= TRUE
;
150 barbarians
->is_connected
= FALSE
;
151 barbarians
->government
= init_government_of_nation(nation
);
152 fc_assert(barbarians
->revolution_finishes
< 0);
153 barbarians
->server
.got_first_city
= FALSE
;
154 barbarians
->economic
.gold
= 100;
156 barbarians
->phase_done
= TRUE
;
159 barbarians
->ai_controlled
= TRUE
;
160 barbarians
->ai_common
.barbarian_type
= type
;
161 set_ai_level_directer(barbarians
, game
.info
.skill_level
);
163 presearch
= research_get(barbarians
);
164 init_tech(presearch
, TRUE
);
165 give_initial_techs(presearch
, 0);
167 /* Ensure that we are at war with everyone else */
168 players_iterate(pplayer
) {
169 if (pplayer
!= barbarians
) {
170 player_diplstate_get(pplayer
, barbarians
)->type
= DS_WAR
;
171 player_diplstate_get(barbarians
, pplayer
)->type
= DS_WAR
;
173 } players_iterate_end
;
175 CALL_PLR_AI_FUNC(gained_control
, barbarians
, barbarians
);
177 log_verbose("Created barbarian %s, player %d", player_name(barbarians
),
178 player_number(barbarians
));
179 notify_player(NULL
, NULL
, E_UPRISING
, ftc_server
,
180 _("%s gain a leader by the name %s. Dangerous "
181 "times may lie ahead."),
182 nation_plural_for_player(barbarians
),
183 player_name(barbarians
));
185 send_player_all_c(barbarians
, NULL
);
186 /* Send research info after player info, else the client will complain
187 * about invalid team. */
188 send_research_info(presearch
, NULL
);
193 /**************************************************************************
194 (Re)initialize direction checked status array based on terrain class.
195 **************************************************************************/
196 static void init_dir_checked_status(bool *checked
,
197 enum terrain_class
*terrainc
,
198 enum terrain_class tclass
)
202 for (dir
= 0; dir
< 8; dir
++) {
203 if (terrainc
[dir
] == tclass
) {
204 checked
[dir
] = FALSE
;
211 /**************************************************************************
212 Return random directory from not yet checked ones.
213 **************************************************************************/
214 static int random_unchecked_direction(int possibilities
, const bool *checked
)
219 int num
= fc_rand(possibilities
);
220 for (i
= 0; i
<= num
; i
++) {
231 /**************************************************************************
232 Unleash barbarians means give barbarian player some units and move them
233 out of the hut, unless there's no place to go.
235 Barbarian unit deployment algorithm: If enough free land around, deploy
236 on land, if not enough land but some sea free, load some of them on
237 boats, otherwise (not much land and no sea) kill enemy unit and stay in
238 a village. The return value indicates if the explorer survived entering
240 **************************************************************************/
241 bool unleash_barbarians(struct tile
*ptile
)
243 struct player
*barbarians
;
246 bool alive
= TRUE
; /* explorer survived */
247 enum terrain_class terrainc
[8];
248 struct tile
*dir_tiles
[8];
254 bool barbarian_stays
= FALSE
;
256 /* FIXME: When there is no L_BARBARIAN unit,
257 * but L_BARBARIAN_TECH is already available,
258 * we should unleash those.
259 * Doesn't affect any ruleset I'm aware of. */
260 if (BARBS_DISABLED
== game
.server
.barbarianrate
261 || game
.info
.turn
< game
.server
.onsetbarbarian
262 || num_role_units(L_BARBARIAN
) == 0) {
263 unit_list_iterate_safe((ptile
)->units
, punit
) {
264 wipe_unit(punit
, ULR_BARB_UNLEASH
, NULL
);
265 } unit_list_iterate_safe_end
;
269 barbarians
= create_barbarian_player(LAND_BARBARIAN
);
274 adv_data_phase_init(barbarians
, TRUE
);
275 CALL_PLR_AI_FUNC(phase_begin
, barbarians
, barbarians
, TRUE
);
277 unit_cnt
= 3 + fc_rand(4);
278 for (i
= 0; i
< unit_cnt
; i
++) {
279 struct unit_type
*punittype
280 = find_a_unit_type(L_BARBARIAN
, L_BARBARIAN_TECH
);
282 /* If unit cannot live on this tile, we just don't create one.
283 * Maybe find_a_unit_type() should take tile parameter, so
284 * we could get suitable unit if one exist. */
285 if (is_native_tile(punittype
, ptile
)) {
286 struct unit
*barb_unit
;
288 barb_unit
= create_unit(barbarians
, ptile
, punittype
, 0, 0, -1);
289 log_debug("Created barbarian unit %s", utype_rule_name(punittype
));
290 send_unit_info(NULL
, barb_unit
);
294 /* Get information about surrounding terrains in terrain class level.
295 * Only needed if we consider moving units away to random directions. */
296 for (dir
= 0; dir
< 8; dir
++) {
297 dir_tiles
[dir
] = mapstep(ptile
, dir
);
298 if (dir_tiles
[dir
] == NULL
) {
299 terrainc
[dir
] = terrain_class_invalid();
300 } else if (!is_non_allied_unit_tile(dir_tiles
[dir
], barbarians
)) {
301 if (is_ocean_tile(dir_tiles
[dir
])) {
302 terrainc
[dir
] = TC_OCEAN
;
305 terrainc
[dir
] = TC_LAND
;
309 terrainc
[dir
] = terrain_class_invalid();
313 if (land_tiles
>= 3) {
314 /* Enough land, scatter guys around */
315 unit_list_iterate_safe((ptile
)->units
, punit2
) {
316 if (unit_owner(punit2
) == barbarians
) {
317 bool dest_found
= FALSE
;
319 /* Initialize checked status for checking free land tiles */
320 init_dir_checked_status(checked
, terrainc
, TC_LAND
);
322 /* Search tile to move to */
323 for (checked_count
= 0; !dest_found
&& checked_count
< land_tiles
;
325 int rdir
= random_unchecked_direction(land_tiles
- checked_count
, checked
);
327 if (unit_can_move_to_tile(punit2
, dir_tiles
[rdir
], TRUE
)) {
329 (void) unit_move_handling(punit2
, dir_tiles
[rdir
],
331 log_debug("Moved barbarian unit from (%d, %d) to (%d, %d)",
332 TILE_XY(ptile
), TILE_XY(dir_tiles
[rdir
]));
336 checked
[rdir
] = TRUE
;
339 /* This barbarian failed to move out of hut tile. */
340 barbarian_stays
= TRUE
;
343 } unit_list_iterate_safe_end
;
346 if (ocean_tiles
> 0) {
347 /* maybe it's an island, try to get on boats */
348 struct tile
*btile
= NULL
; /* Boat tile */
350 /* Initialize checked status for checking Ocean tiles */
351 init_dir_checked_status(checked
, terrainc
, TC_OCEAN
);
353 /* Search tile for boat. We always create just one boat. */
354 for (checked_count
= 0; btile
== NULL
&& checked_count
< ocean_tiles
;
356 struct unit_type
*boat
;
357 int rdir
= random_unchecked_direction(ocean_tiles
- checked_count
, checked
);
359 boat
= find_a_unit_type(L_BARBARIAN_BOAT
, -1);
360 if (is_native_tile(boat
, dir_tiles
[rdir
])) {
361 (void) create_unit(barbarians
, dir_tiles
[rdir
], boat
, 0, 0, -1);
362 btile
= dir_tiles
[rdir
];
365 checked
[rdir
] = TRUE
;
369 /* We do have a boat. Try to get everybody in */
370 unit_list_iterate_safe((ptile
)->units
, punit2
) {
371 if (unit_owner(punit2
) == barbarians
) {
372 if (unit_can_move_to_tile(punit2
, btile
, TRUE
)) {
374 (void) unit_move_handling(punit2
, btile
, TRUE
, TRUE
, NULL
);
377 } unit_list_iterate_safe_end
;
380 /* Move rest of the barbarians to random land tiles */
381 unit_list_iterate_safe((ptile
)->units
, punit2
) {
382 if (unit_owner(punit2
) == barbarians
) {
383 bool dest_found
= FALSE
;
385 /* Initialize checked status for checking Land tiles */
386 init_dir_checked_status(checked
, terrainc
, TC_LAND
);
388 /* Search tile to move to */
389 for (checked_count
= 0; !dest_found
&& checked_count
< land_tiles
;
392 rdir
= random_unchecked_direction(land_tiles
- checked_count
, checked
);
394 if (unit_can_move_to_tile(punit2
, dir_tiles
[rdir
], TRUE
)) {
396 (void) unit_move_handling(punit2
, dir_tiles
[rdir
],
401 checked
[rdir
] = TRUE
;
404 /* This barbarian failed to move out of hut tile. */
405 barbarian_stays
= TRUE
;
408 } unit_list_iterate_safe_end
;
410 /* The village is surrounded! Barbarians cannot leave. */
411 barbarian_stays
= TRUE
;
415 if (barbarian_stays
) {
416 /* There's barbarian in this village! Kill the explorer. */
417 unit_list_iterate_safe((ptile
)->units
, punit2
) {
418 if (unit_owner(punit2
) != barbarians
) {
419 wipe_unit(punit2
, ULR_BARB_UNLEASH
, NULL
);
422 send_unit_info(NULL
, punit2
);
424 } unit_list_iterate_safe_end
;
427 /* FIXME: I don't know if this is needed */
429 map_show_circle(barbarians
, ptile
, BARBARIAN_INITIAL_VISION_RADIUS_SQ
);
435 /**************************************************************************
436 Is sea not further than a couple of tiles away from land?
437 **************************************************************************/
438 static bool is_near_land(struct tile
*tile0
)
440 square_iterate(tile0
, 4, ptile
) {
441 if (!is_ocean_tile(ptile
)) {
444 } square_iterate_end
;
449 /**************************************************************************
450 Return this or a neighbouring tile that is free of any units
451 **************************************************************************/
452 static struct tile
*find_empty_tile_nearby(struct tile
*ptile
)
454 square_iterate(ptile
, 1, tile1
) {
455 if (unit_list_size(tile1
->units
) == 0) {
458 } square_iterate_end
;
463 /**************************************************************************
464 The barbarians are summoned at a randomly chosen place if:
465 1. It's not closer than MIN_UNREST_DIST and not further than
466 MAX_UNREST_DIST from the nearest city. City owner is called 'victim'
468 2. The place or a neighbouring tile must be empty to deploy the units.
469 3. If it's the sea it shouldn't be far from the land. (questionable)
470 4. Place must be known to the victim
471 5. The uprising chance depends also on the victim empire size, its
472 government (civil_war_chance) and barbarian difficulty level.
473 6. The number of land units also depends slightly on victim's empire
474 size and barbarian difficulty level.
475 Q: The empire size is used so there are no uprisings in the beginning
476 of the game (year is not good as it can be customized), but it seems
477 a bit unjust if someone is always small. So maybe it should rather
478 be an average number of cities (all cities/player num)? Depending
479 on the victim government type is also questionable.
480 **************************************************************************/
481 static void try_summon_barbarians(void)
483 struct tile
*ptile
, *utile
;
487 struct player
*barbarians
, *victim
;
488 struct unit_type
*leader_type
;
489 int barb_count
, really_created
= 0;
490 bool hut_present
= FALSE
;
494 /* We attempt the summons on a particular, random position. If this is
495 * an invalid position then the summons simply fails this time. This means
496 * that a particular tile's chance of being summoned on is independent of
497 * all the other tiles on the map - which is essential for balanced
499 ptile
= rand_map_pos();
501 if (terrain_has_flag(tile_terrain(ptile
), TER_NO_BARBS
)) {
505 if (!(pc
= find_closest_city(ptile
, NULL
, NULL
, FALSE
, FALSE
, FALSE
, FALSE
,
511 victim
= city_owner(pc
);
513 dist
= real_map_distance(ptile
, pc
->tile
);
514 log_debug("Closest city (to %d,%d) is %s (at %d,%d) distance %d.",
515 TILE_XY(ptile
), city_name_get(pc
), TILE_XY(pc
->tile
), dist
);
516 if (dist
> MAX_UNREST_DIST
|| dist
< MIN_UNREST_DIST
) {
520 /* I think Sea Raiders can come out of unknown sea territory */
521 if (!(utile
= find_empty_tile_nearby(ptile
))
522 || (!map_is_known(utile
, victim
)
523 && !is_ocean_tile(utile
))
524 || !is_near_land(utile
)) {
528 fc_assert(1 < game
.server
.barbarianrate
);
530 /* do not harass small civs - in practice: do not uprise at the beginning */
531 if ((int)fc_rand(30) + 1 >
532 (int)city_list_size(victim
->cities
) * (game
.server
.barbarianrate
- 1)
533 || fc_rand(100) > get_player_bonus(victim
, EFT_CIVIL_WAR_CHANCE
)) {
536 log_debug("Barbarians are willing to fight");
538 /* Remove huts in place of uprising */
539 extra_type_by_cause_iterate(EC_HUT
, pextra
) {
540 if (tile_has_extra(utile
, pextra
)) {
541 tile_extra_rm_apply(utile
, pextra
);
544 } extra_type_by_cause_iterate_end
;
547 update_tile_knowledge(utile
);
550 city_count
= city_list_size(victim
->cities
);
551 city_max
= UPRISE_CIV_SIZE
;
554 while (city_max
<= city_count
) {
556 city_max
*= 1.2 + UPRISE_CIV_SIZE
;
559 barb_count
= fc_rand(3) + uprise
* game
.server
.barbarianrate
;
560 leader_type
= get_role_unit(L_BARBARIAN_LEADER
, 0);
562 if (!is_ocean_tile(utile
)) {
563 /* land (disembark) barbarians */
564 barbarians
= create_barbarian_player(LAND_BARBARIAN
);
568 for (i
= 0; i
< barb_count
; i
++) {
569 struct unit_type
*punittype
570 = find_a_unit_type(L_BARBARIAN
, L_BARBARIAN_TECH
);
572 /* If unit cannot live on this tile, we just don't create one.
573 * Maybe find_a_unit_type() should take tile parameter, so
574 * we could get suitable unit if one exist. */
575 if (is_native_tile(punittype
, utile
)) {
576 (void) create_unit(barbarians
, utile
, punittype
, 0, 0, -1);
578 log_debug("Created barbarian unit %s",utype_rule_name(punittype
));
582 if (is_native_tile(leader_type
, utile
)) {
583 (void) create_unit(barbarians
, utile
,
584 leader_type
, 0, 0, -1);
587 } else { /* sea raiders - their units will be veteran */
589 struct unit_type
*boat
;
592 barbarians
= create_barbarian_player(SEA_BARBARIAN
);
596 /* Setup data phase if it's not already set up. Created ferries may
598 We don't know if create_barbarian_player() above created completely
599 new player or did it just return existing one. If it was existing
600 one, phase has already been set up at turn begin and will be closed
601 at turn end. If this is completely new player, we have to take care
602 of both opening and closing the data phase. Return value of
603 adv_data_phase_init() tells us if data phase was already initialized
604 at turn beginning. */
605 miniphase
= adv_data_phase_init(barbarians
, TRUE
);
607 CALL_PLR_AI_FUNC(phase_begin
, barbarians
, barbarians
, TRUE
);
610 boat
= find_a_unit_type(L_BARBARIAN_BOAT
,-1);
612 if (is_native_tile(boat
, utile
)
613 && (!utype_has_flag(boat
, UTYF_TRIREME
) || is_safe_ocean(utile
))) {
616 ptrans
= create_unit(barbarians
, utile
, boat
, 0, 0, -1);
618 cap
= get_transporter_capacity(ptrans
);
620 /* Fill boat with barb_count barbarians at max, leave space for leader */
621 for (i
= 0; i
< cap
- 1 && i
< barb_count
; i
++) {
622 struct unit_type
*barb
623 = find_a_unit_type(L_BARBARIAN_SEA
, L_BARBARIAN_SEA_TECH
);
625 if (can_unit_type_transport(boat
, utype_class(barb
))) {
626 (void) create_unit_full(barbarians
, utile
, barb
, 0, 0, -1, -1,
629 log_debug("Created barbarian unit %s", utype_rule_name(barb
));
633 if (can_unit_type_transport(boat
, utype_class(leader_type
))) {
634 (void) create_unit_full(barbarians
, utile
, leader_type
, 0, 0,
641 CALL_PLR_AI_FUNC(phase_finished
, barbarians
, barbarians
);
642 adv_data_phase_done(barbarians
);
646 if (really_created
== 0) {
647 /* No barbarians found suitable spot */
651 /* Is this necessary? create_unit_full already sends unit info. */
652 unit_list_iterate(utile
->units
, punit2
) {
653 send_unit_info(NULL
, punit2
);
654 } unit_list_iterate_end
;
656 /* to let them know where to get you */
657 map_show_circle(barbarians
, utile
, BARBARIAN_INITIAL_VISION_RADIUS_SQ
);
658 map_show_circle(barbarians
, pc
->tile
, BARBARIAN_INITIAL_VISION_RADIUS_SQ
);
660 /* There should probably be a different message about Sea Raiders */
661 if (is_land_barbarian(barbarians
)) {
662 notify_player(victim
, utile
, E_UPRISING
, ftc_server
,
663 _("Native unrest near %s led by %s."),
665 player_name(barbarians
));
666 } else if (map_is_known_and_seen(utile
, victim
, V_MAIN
)) {
667 notify_player(victim
, utile
, E_UPRISING
, ftc_server
,
668 _("Sea raiders seen near %s!"),
673 /**************************************************************************
674 Summon barbarians out of the blue. Try more times for more difficult
675 levels - which means there can be more than one uprising in one year!
676 **************************************************************************/
677 void summon_barbarians(void)
681 if (BARBS_DISABLED
== game
.server
.barbarianrate
682 || BARBS_HUTS_ONLY
== game
.server
.barbarianrate
) {
686 if (game
.info
.turn
< game
.server
.onsetbarbarian
) {
690 n
= map_num_tiles() / MAP_FACTOR
;
692 /* Allow barbarians on maps smaller than MAP_FACTOR */
696 for (i
= 0; i
< n
* (game
.server
.barbarianrate
- 1); i
++) {
697 try_summon_barbarians();