Freeciv21
Develop your civilization from humble roots to a global empire
animals.cpp
Go to the documentation of this file.
1 /*
2 \^~~~~\ ) ( /~~~~^/ * _ Copyright (c) 1996-2020 Freeciv21 and
3  ) *** \ {**} / *** ( * _ {o} _ Freeciv contributors. This file is
4  ) *** \_ ^^ _/ *** ( * {o}{o}{o} part of Freeciv21. Freeciv21 is free
5  ) **** vv **** ( * ~\ | /~software: you can redistribute it and/or
6  )_**** ****_( * OoO modify it under the terms of the GNU
7  )*** m m ***( * /|\ General Public License as published
8  by the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version. You should have received a copy of
10  the GNU General Public License along with Freeciv21.
11  If not, see https://www.gnu.org/licenses/.
12  */
13 
14 // common
15 #include "ai.h"
16 #include "game.h"
17 #include "map.h"
18 #include "movement.h"
19 #include "nation.h"
20 #include "player.h"
21 #include "research.h"
22 #include "tile.h"
23 #include "unittype.h"
24 
25 // server
26 #include "aiiface.h"
27 #include "plrhand.h"
28 #include "srv_main.h"
29 #include "techtools.h"
30 #include "unittools.h"
31 
32 // ai
33 #include "difficulty.h"
34 
35 #include "animals.h"
36 
40 static const struct unit_type *animal_for_terrain(struct terrain *pterr)
41 {
42  return pterr->animal;
43 }
44 
48 static void place_animal(struct player *plr)
49 {
50  struct tile *ptile = rand_map_pos(&(wld.map));
51  const struct unit_type *ptype;
52 
53  extra_type_by_rmcause_iterate(ERM_ENTER, pextra)
54  {
55  if (tile_has_extra(ptile, pextra)) {
56  // Animals should not displace huts
57  // FIXME: might HUT_NOTHING animals appear here?
58  return;
59  }
60  }
62 
63  if (unit_list_size(ptile->units) > 0 || tile_city(ptile)) {
64  return;
65  }
66  adjc_iterate(&(wld.map), ptile, padj)
67  {
68  if (unit_list_size(padj->units) > 0 || tile_city(padj)) {
69  // No animals next to start units or start city
70  return;
71  }
72  }
74 
75  ptype = animal_for_terrain(tile_terrain(ptile));
76 
77  if (ptype != nullptr) {
78  struct unit *punit;
79 
80  fc_assert_ret(can_exist_at_tile(&(wld.map), ptype, ptile));
81 
82  punit = create_unit(plr, ptile, ptype, 0, 0, -1);
83 
84  send_unit_info(nullptr, punit);
85  }
86 }
87 
92 {
93  struct nation_type *anination;
94  struct player *plr;
95  struct research *presearch;
96  int i;
97 
98  if (wld.map.server.animals <= 0) {
99  return;
100  }
101 
102  anination = pick_a_nation(nullptr, false, true, ANIMAL_BARBARIAN);
103 
104  if (anination == NO_NATION_SELECTED) {
105  return;
106  }
107 
108  plr = server_create_player(-1, default_ai_type_name(), nullptr, false);
109  if (plr == nullptr) {
110  return;
111  }
112  server_player_init(plr, true, true);
113 
114  player_set_nation(plr, anination);
115  player_nation_defaults(plr, anination, true);
116 
118 
119  server.nbarbarians++;
120 
122  plr->unassigned_user = true;
123  plr->is_connected = false;
124  plr->government = init_government_of_nation(anination);
125  plr->economic.gold = 100;
126 
127  plr->phase_done = true;
128 
129  set_as_ai(plr);
130  plr->ai_common.barbarian_type = ANIMAL_BARBARIAN;
131  set_ai_level_directer(plr, ai_level(game.info.skill_level));
132 
133  presearch = research_get(plr);
134  init_tech(presearch, true);
135  give_initial_techs(presearch, 0);
136 
137  // Ensure that we are at war with everyone else
138  players_iterate(pplayer)
139  {
140  if (pplayer != plr) {
141  player_diplstate_get(pplayer, plr)->type = DS_WAR;
142  player_diplstate_get(plr, pplayer)->type = DS_WAR;
143  }
144  }
146 
147  CALL_PLR_AI_FUNC(gained_control, plr, plr);
148 
149  send_player_all_c(plr, nullptr);
150  /* Send research info after player info, else the client will complain
151  * about invalid team. */
152  send_research_info(presearch, nullptr);
153 
154  for (i = 0;
155  i < wld.map.xsize * wld.map.ysize * wld.map.server.animals / 1000;
156  i++) {
157  place_animal(plr);
158  }
159 }
#define CALL_PLR_AI_FUNC(_func, _player,...)
Definition: ai.h:383
const char * default_ai_type_name()
Return name of default ai type.
Definition: aiiface.cpp:265
static void place_animal(struct player *plr)
Try to add one animal to the map.
Definition: animals.cpp:48
void create_animals()
Create animal kingdom player and his units.
Definition: animals.cpp:91
static const struct unit_type * animal_for_terrain(struct terrain *pterr)
Return suitable animal type for the terrain.
Definition: animals.cpp:40
void set_ai_level_directer(struct player *pplayer, enum ai_level level)
Set an AI level and related quantities, with no feedback.
Definition: difficulty.cpp:36
#define extra_type_by_rmcause_iterate_end
Definition: extras.h:330
#define extra_type_by_rmcause_iterate(_rmcause, _extra)
Definition: extras.h:324
#define _(String)
Definition: fcintl.h:50
struct civ_game game
Definition: game.cpp:47
struct world wld
Definition: game.cpp:48
#define fc_assert_ret(condition)
Definition: log.h:112
struct tile * rand_map_pos(const struct civ_map *nmap)
Random square anywhere on the map.
Definition: map.cpp:1026
#define adjc_iterate_end
Definition: map.h:358
#define adjc_iterate(nmap, center_tile, itr_tile)
Definition: map.h:351
bool can_exist_at_tile(const struct civ_map *nmap, const struct unit_type *utype, const struct tile *ptile)
Return TRUE iff a unit of the given unit type can "exist" at this location.
Definition: movement.cpp:236
struct government * init_government_of_nation(const struct nation_type *pnation)
Returns initial government type for this nation.
Definition: nation.cpp:545
#define NO_NATION_SELECTED
Definition: nation.h:21
bool player_set_nation(struct player *pplayer, struct nation_type *pnation)
Set the player's nation to the given nation (may be nullptr).
Definition: player.cpp:780
struct player_diplstate * player_diplstate_get(const struct player *plr1, const struct player *plr2)
Returns diplomatic state type between two players.
Definition: player.cpp:288
#define players_iterate_end
Definition: player.h:520
#define players_iterate(_pplayer)
Definition: player.h:514
#define ANON_USER_NAME
Definition: player.h:31
#define set_as_ai(plr)
Definition: player.h:229
void send_player_all_c(struct player *src, struct conn_list *dest)
Send all information about a player (player_info and all player_diplstates) to the given connections.
Definition: plrhand.cpp:1031
struct player * server_create_player(int player_id, const char *ai_tname, struct rgbcolor *prgbcolor, bool allow_ai_type_fallbacking)
Creates a new, uninitialized, used player slot.
Definition: plrhand.cpp:1776
void assign_player_colors()
Permanently assign colors to any players that don't already have them.
Definition: plrhand.cpp:1608
struct nation_type * pick_a_nation(const struct nation_list *choices, bool ignore_conflicts, bool needs_startpos, enum barbarian_type barb_type)
This function returns a random-ish nation that is suitable for 'barb_type' and is usable (not already...
Definition: plrhand.cpp:2377
void server_player_init(struct player *pplayer, bool initmap, bool needs_team)
Initialize ANY newly-created player on the server.
Definition: plrhand.cpp:1494
struct research * research_get(const struct player *pplayer)
Returns the research structure associated with the player.
Definition: research.cpp:110
void player_nation_defaults(struct player *pplayer, struct nation_type *pnation, bool set_name)
Set nation for player with nation default values.
Definition: srv_main.cpp:2545
struct packet_game_info info
Definition: game.h:80
int xsize
Definition: map_types.h:73
int ysize
Definition: map_types.h:73
struct civ_map::@39::@41 server
enum barbarian_type barbarian_type
Definition: player.h:115
enum diplstate_type type
Definition: player.h:193
Definition: player.h:231
struct player_ai ai_common
Definition: player.h:270
char username[MAX_LEN_NAME]
Definition: player.h:234
bool is_connected
Definition: player.h:278
struct government * government
Definition: player.h:240
struct player_economic economic
Definition: player.h:266
bool phase_done
Definition: player.h:245
bool unassigned_user
Definition: player.h:235
Definition: servers.h:55
const struct unit_type * animal
Definition: terrain.h:216
Definition: tile.h:42
struct unit_list * units
Definition: tile.h:50
Definition: unit.h:134
struct civ_map map
Definition: world_object.h:21
#define sz_strlcpy(dest, src)
Definition: support.h:140
void init_tech(struct research *research, bool update)
Initializes tech data for the research.
Definition: techtools.cpp:1075
void send_research_info(const struct research *presearch, const struct conn_list *dest)
Send research info for 'presearch' to 'dest'.
Definition: techtools.cpp:273
void give_initial_techs(struct research *presearch, int num_random_techs)
Gives global (read from the game ruleset file) and nation (read from the nation ruleset files) initia...
Definition: techtools.cpp:1179
struct city * tile_city(const struct tile *ptile)
Return the city on this tile (or nullptr), checking for city center.
Definition: tile.cpp:72
#define tile_terrain(_tile)
Definition: tile.h:93
#define tile_has_extra(ptile, pextra)
Definition: tile.h:130
struct unit * create_unit(struct player *pplayer, struct tile *ptile, const struct unit_type *type, int veteran_level, int homecity_id, int moves_left)
Wrapper of the below.
Definition: unittools.cpp:1762
void send_unit_info(struct conn_list *dest, struct unit *punit)
Send the unit to the players who need the info.
Definition: unittools.cpp:2808