webperimental: killstack decides stack protects.
[freeciv.git] / ai / aitraits.c
blobd89e8f470034296a390b47bb0884b95feb4f4106
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)
6 any later version.
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 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
18 /* utility */
19 #include "mem.h"
20 #include "rand.h"
22 /* common */
23 #include "game.h"
24 #include "player.h"
25 #include "traits.h"
27 #include "aitraits.h"
29 /**************************************************************************
30 Initialize ai traits for player
31 **************************************************************************/
32 void ai_traits_init(struct player *pplayer)
34 enum trait tr;
36 pplayer->ai_common.traits = fc_realloc(pplayer->ai_common.traits,
37 sizeof(struct ai_trait) * TRAIT_COUNT);
39 for (tr = trait_begin(); tr != trait_end(); tr = trait_next(tr)) {
40 int min = pplayer->nation->server.traits[tr].min;
41 int max = pplayer->nation->server.traits[tr].max;
43 switch (game.server.trait_dist) {
44 case TDM_FIXED:
45 pplayer->ai_common.traits[tr].val = pplayer->nation->server.traits[tr].fixed;
46 break;
47 case TDM_EVEN:
48 pplayer->ai_common.traits[tr].val = fc_rand(max + 1 - min) + min;
49 break;
51 pplayer->ai_common.traits[tr].mod = 0;
55 /**************************************************************************
56 Free resources associated with player ai traits.
57 **************************************************************************/
58 void ai_traits_close(struct player *pplayer)
60 FC_FREE(pplayer->ai_common.traits);
62 pplayer->ai_common.traits = NULL;
65 /**************************************************************************
66 Get current value of player trait
67 **************************************************************************/
68 int ai_trait_get_value(enum trait tr, struct player *pplayer)
70 int val = pplayer->ai_common.traits[tr].val + pplayer->ai_common.traits[tr].mod;
72 /* Clip so that value is at least 1, and maximum is
73 * TRAIT_DEFAULT_VALUE as many times as TRAIT_DEFAULT value is
74 * minimum value of 1 ->
75 * minimum is default / TRAIT_DEFAULT_VALUE,
76 * maximum is default * TRAIT_DEFAULT_VALUE */
77 val = CLIP(1, val, TRAIT_MAX_VALUE);
79 return val;