Freeciv21
Develop your civilization from humble roots to a global empire
aitraits.cpp
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 
14 // utility
15 #include "rand.h"
16 
17 // common
18 #include "game.h"
19 #include "nation.h"
20 #include "player.h"
21 #include "traits.h"
22 
23 #include "aitraits.h"
24 
28 void ai_traits_init(struct player *pplayer)
29 {
30  enum trait tr;
31 
32  pplayer->ai_common.traits = std::vector<ai_trait>(TRAIT_COUNT);
33 
34  for (tr = trait_begin(); tr != trait_end(); tr = trait_next(tr)) {
35  int min = pplayer->nation->server.traits[tr].min;
36  int max = pplayer->nation->server.traits[tr].max;
37 
38  switch (game.server.trait_dist) {
39  case TDM_FIXED:
40  pplayer->ai_common.traits[tr].val =
41  pplayer->nation->server.traits[tr].fixed;
42  break;
43  case TDM_EVEN:
44  pplayer->ai_common.traits[tr].val = fc_rand(max + 1 - min) + min;
45  break;
46  }
47  pplayer->ai_common.traits[tr].mod = 0;
48  }
49 }
50 
54 void ai_traits_close(struct player *pplayer)
55 {
56  pplayer->ai_common.traits.clear();
57 }
58 
62 int ai_trait_get_value(enum trait tr, struct player *pplayer)
63 {
64  int val =
65  pplayer->ai_common.traits[tr].val + pplayer->ai_common.traits[tr].mod;
66 
67  /* Clip so that value is at least 1, and maximum is
68  * TRAIT_DEFAULT_VALUE as many times as TRAIT_DEFAULT value is
69  * minimum value of 1 ->
70  * minimum is default / TRAIT_DEFAULT_VALUE,
71  * maximum is default * TRAIT_DEFAULT_VALUE */
72  val = CLIP(1, val, TRAIT_MAX_VALUE);
73 
74  return val;
75 }
void ai_traits_init(struct player *pplayer)
Initialize ai traits for player.
Definition: aitraits.cpp:28
int ai_trait_get_value(enum trait tr, struct player *pplayer)
Get current value of player trait.
Definition: aitraits.cpp:62
void ai_traits_close(struct player *pplayer)
Free resources associated with player ai traits.
Definition: aitraits.cpp:54
@ TDM_FIXED
Definition: fc_types.h:869
@ TDM_EVEN
Definition: fc_types.h:869
struct civ_game game
Definition: game.cpp:47
#define fc_rand(_size)
Definition: rand.h:16
#define CLIP(lower, current, upper)
Definition: shared.h:51
struct civ_game::@28::@32 server
struct nation_type::@48::@50 server
std::vector< ai_trait > traits
Definition: player.h:119
Definition: player.h:231
struct player_ai ai_common
Definition: player.h:270
struct nation_type * nation
Definition: player.h:242
#define TRAIT_MAX_VALUE
Definition: traits.h:28