File: ai.c

package info (click to toggle)
freeciv 2.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 212,508 kB
  • sloc: ansic: 443,831; cpp: 29,541; sh: 7,982; makefile: 7,886; python: 1,933; xml: 945
file content (361 lines) | stat: -rw-r--r-- 10,973 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/****************************************************************************
 Freeciv - Copyright (C) 2004 - The Freeciv Team
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
****************************************************************************/

#ifdef HAVE_CONFIG_H
#include <fc_config.h>
#endif

#include <string.h>

/* utility */
#include "fcintl.h"
#include "log.h"                /* fc_assert */
#include "mem.h"
#include "timing.h"

/* common */
#include "ai.h"
#include "player.h"

static struct ai_type ai_types[FREECIV_AI_MOD_LAST];

static int ai_type_count = 0;

#ifdef DEBUG_AITIMERS
struct ai_timer {
  int count;
  struct timer *timer;
};

static struct ai_timer *ai_timer_get(const struct ai_type *ai);
static struct ai_timer *ai_timer_player_get(const struct player *pplayer);

static struct ai_timer *aitimers = NULL;
static struct ai_timer *aitimer_plrs = NULL;

/*****************************************************************************
  Allocate memory for Start the timer for the AI of a player.
*****************************************************************************/
void ai_timer_init(void)
{
  int i;

  fc_assert_ret(aitimers == NULL);
  fc_assert_ret(aitimer_plrs == NULL);

  aitimers = fc_calloc(FREECIV_AI_MOD_LAST, sizeof(*aitimers));
  for (i = 0; i < FREECIV_AI_MOD_LAST; i++) {
    struct ai_timer *aitimer = aitimers + i;
    aitimer->count = 0;
    aitimer->timer = NULL;
  }

  aitimer_plrs = fc_calloc(FREECIV_AI_MOD_LAST * MAX_NUM_PLAYER_SLOTS,
                           sizeof(*aitimer_plrs));
  for (i = 0; i < FREECIV_AI_MOD_LAST * MAX_NUM_PLAYER_SLOTS; i++) {
    struct ai_timer *aitimer = aitimer_plrs + i;
    aitimer->count = 0;
    aitimer->timer = NULL;
  }
}

/*****************************************************************************
  Free resources allocated for the AI timers.
*****************************************************************************/
void ai_timer_free(void)
{
  int i,j;
  struct ai_timer *aitimer;

  for (i = 0; i < FREECIV_AI_MOD_LAST; i++) {
    struct ai_type *ai = get_ai_type(i);

    aitimer = aitimers + i;

    if (aitimer->timer) {
      log_normal("AI timer stats: [%15.3f] ---- (AI type: %s)",
                 timer_read_seconds(aitimer->timer), ai_name(ai));
      timer_destroy(aitimer->timer);
    }

    for (j = 0; j < MAX_NUM_PLAYER_SLOTS; j++) {
      aitimer = aitimer_plrs + j * FREECIV_AI_MOD_LAST + i;

      if (aitimer->timer) {
        log_normal("AI timer stats: [%15.3f] P%03d (AI type: %s)",
                   timer_read_seconds(aitimer->timer), j, ai_name(ai));
        timer_destroy(aitimer->timer);
      }
    }
  }
  free(aitimers);
  aitimers = NULL;

  free(aitimer_plrs);
  aitimer_plrs = NULL;
}

/*****************************************************************************
  Get the timer for the AI.
*****************************************************************************/
static struct ai_timer *ai_timer_get(const struct ai_type *ai)
{
  struct ai_timer *aitimer;

  fc_assert_ret_val(ai != NULL, NULL);

  fc_assert_ret_val(aitimers != NULL, NULL);

  aitimer = aitimers + ai_type_number(ai);

  if (!aitimer->timer) {
    aitimer->timer = timer_new(TIMER_CPU, TIMER_DEBUG);
  }

  return aitimer;
}

/*****************************************************************************
  Get the timer for the AI of a player.
*****************************************************************************/
static struct ai_timer *ai_timer_player_get(const struct player *pplayer)
{
  struct ai_timer *aitimer;

  fc_assert_ret_val(pplayer != NULL, NULL);
  fc_assert_ret_val(pplayer->ai != NULL, NULL);

  fc_assert_ret_val(aitimer_plrs != NULL, NULL);

  aitimer = aitimer_plrs + (player_index(pplayer) * FREECIV_AI_MOD_LAST
                            + ai_type_number(pplayer->ai));

  if (!aitimer->timer) {
    aitimer->timer = timer_new(TIMER_CPU, TIMER_DEBUG);
  }

  return aitimer;
}

/*****************************************************************************
  Start the timer for the AI.
*****************************************************************************/
void ai_timer_start(const struct ai_type *ai)
{
  struct ai_timer *aitimer = ai_timer_get(ai);

  fc_assert_ret(aitimer != NULL);
  fc_assert_ret(aitimer->timer != NULL);

  if (aitimer->count == 0) {
    log_debug("AI timer start  [%15.3f] ---- (AI type: %s)",
              timer_read_seconds(aitimer->timer), ai_name(ai));
    timer_start(aitimer->timer);
  } else {
    log_debug("AI timer =====> [depth: %3d]      ---- (AI type: %s)",
              aitimer->count, ai_name(ai));
  }
  aitimer->count++;
}

/*****************************************************************************
  Stop the timer for the AI.
*****************************************************************************/
void ai_timer_stop(const struct ai_type *ai)
{
  struct ai_timer *aitimer = ai_timer_get(ai);

  fc_assert_ret(aitimer != NULL);
  fc_assert_ret(aitimer->timer != NULL);

  if (aitimer->count > 0) {
    if (aitimer->count == 1) {
      timer_stop(aitimer->timer);
      log_debug("AI timer stop   [%15.3f] ---- (AI type: %s)",
                timer_read_seconds(aitimer->timer), ai_name(ai));
    } else {
      log_debug("AI timer =====> [depth: %3d]      ---- (AI type: %s)",
                aitimer->count, ai_name(ai));
    }
    aitimer->count--;
  } else {
    log_debug("AI timer missing?                 ---- (AI type: %s)",
              ai_name(ai));
  }
}

/*****************************************************************************
  Start the timer for the AI of a player.
*****************************************************************************/
void ai_timer_player_start(const struct player *pplayer)
{
  struct ai_timer *aitimer = ai_timer_player_get(pplayer);

  fc_assert_ret(aitimer != NULL);
  fc_assert_ret(aitimer->timer != NULL);

  if (aitimer->count == 0) {
    log_debug("AI timer start  [%15.3f] P%03d (AI type: %s) %s",
              timer_read_seconds(aitimer->timer), player_index(pplayer),
              ai_name(pplayer->ai), player_name(pplayer));
    timer_start(aitimer->timer);
  } else {
    log_debug("AI timer =====> [depth: %3d]      P%03d (AI type: %s) %s",
              aitimer->count, player_index(pplayer), ai_name(pplayer->ai),
              player_name(pplayer));
  }
  aitimer->count++;
}

/*****************************************************************************
  Stop the timer for the AI of a player.
*****************************************************************************/
void ai_timer_player_stop(const struct player *pplayer)
{
  struct ai_timer *aitimer = ai_timer_player_get(pplayer);

  fc_assert_ret(aitimer != NULL);
  fc_assert_ret(aitimer->timer != NULL);

  if (aitimer->count > 0) {
    if (aitimer->count == 1) {
      timer_stop(aitimer->timer);
      log_debug("AI timer stop   [%15.3f] P%03d (AI type: %s) %s",
                timer_read_seconds(aitimer->timer), player_index(pplayer),
                ai_name(pplayer->ai), player_name(pplayer));
    } else {
      log_debug("AI timer =====> [depth: %3d]      P%03d (AI type: %s) %s",
                aitimer->count, player_index(pplayer), ai_name(pplayer->ai),
                player_name(pplayer));
    }
    aitimer->count--;
  } else {
    log_debug("AI timer missing?                 P%03d (AI type: %s) %s",
              player_index(pplayer), ai_name(pplayer->ai),
              player_name(pplayer));
  }
}
#endif /* DEBUG_AITIMERS */

/***************************************************************
  Returns ai_type of given id.
***************************************************************/
struct ai_type *get_ai_type(int id)
{
  fc_assert(id >= 0 && id < FREECIV_AI_MOD_LAST);

  return &ai_types[id];
}

/***************************************************************
  Initializes AI structure.
***************************************************************/
void init_ai(struct ai_type *ai)
{
  memset(ai, 0, sizeof(*ai));
}

/***************************************************************
  Returns id of the given ai_type.
***************************************************************/
int ai_type_number(const struct ai_type *ai)
{
  int ainbr = ai - ai_types;

  fc_assert_ret_val(ainbr >= 0 && ainbr < FREECIV_AI_MOD_LAST, 0);

  return ainbr;
}

/***************************************************************
  Find ai type with given name.
***************************************************************/
struct ai_type *ai_type_by_name(const char *search)
{
  ai_type_iterate(ai) {
    if (!fc_strcasecmp(ai_name(ai), search)) {
      return ai;
    }
  } ai_type_iterate_end;

  return NULL;
}

/***************************************************************
  Return next free ai_type
***************************************************************/
struct ai_type *ai_type_alloc(void)
{
  if (ai_type_count >= FREECIV_AI_MOD_LAST) {
    log_error(_("Too many AI modules. Max is %d."),
              FREECIV_AI_MOD_LAST);

    return NULL;
  }

  return get_ai_type(ai_type_count++);
}

/***************************************************************
  Free latest ai_type
***************************************************************/
void ai_type_dealloc(void)
{
  ai_type_count--;
}

/***************************************************************
  Return number of ai types
***************************************************************/
int ai_type_get_count(void)
{
  return ai_type_count;
}

/*****************************************************************************
  Return the name of the ai type.
*****************************************************************************/
const char *ai_name(const struct ai_type *ai)
{
  fc_assert_ret_val(ai, NULL);
  return ai->name;
}

/*****************************************************************************
  Return usable ai type name, if possible. This is either the name
  given as parameter or some fallback name for it. NULL is returned if
  no name matches.
*****************************************************************************/
const char *ai_type_name_or_fallback(const char *orig_name)
{
  if (orig_name == NULL) {
    return NULL;
  }

  if (ai_type_by_name(orig_name) != NULL) {
    return orig_name;
  }

  if (!strcmp("threaded", orig_name)) {
    struct ai_type *fb;

    fb = ai_type_by_name("classic");

    if (fb != NULL) {
      /* Get pointer to persistent name of the ai_type */
      return ai_name(fb);
    }
  }

  return NULL;
}