Freeciv-3.1: /fast/freeciv/nightly/S3_1/common/ai.c Source File

Freeciv-3.1

Loading...
Searching...
No Matches
ai.c
Go to the documentation of this file.
1/****************************************************************************
2 Freeciv - Copyright (C) 2004 - The Freeciv Team
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.
7
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****************************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#include <string.h>
19
20/* utility */
21#include "fcintl.h"
22#include "log.h" /* fc_assert */
23#include "mem.h"
24#include "timing.h"
25
26/* common */
27#include "player.h"
28
29#include "ai.h"
30
31 static struct ai_type ai_types[FREECIV_AI_MOD_LAST];
32
33 static int ai_type_count = 0;
34
35#ifdef DEBUG_AITIMERS
36struct ai_timer {
37 int count;
38 struct timer *timer;
39};
40
41static struct ai_timer *ai_timer_get(const struct ai_type *ai);
42static struct ai_timer *ai_timer_player_get(const struct player *pplayer);
43
44static struct ai_timer *aitimers = NULL;
45static struct ai_timer *aitimer_plrs = NULL;
46
47/*************************************************************************/
50void ai_timer_init(void)
51{
52 int i;
53
54 fc_assert_ret(aitimers == NULL);
55 fc_assert_ret(aitimer_plrs == NULL);
56
57 aitimers = fc_calloc(FREECIV_AI_MOD_LAST, sizeof(*aitimers));
58 for (i = 0; i < FREECIV_AI_MOD_LAST; i++) {
59 struct ai_timer *aitimer = aitimers + i;
60
61 aitimer->count = 0;
62 aitimer->timer = NULL;
63 }
64
65 aitimer_plrs = fc_calloc(FREECIV_AI_MOD_LAST * MAX_NUM_PLAYER_SLOTS,
66 sizeof(*aitimer_plrs));
67 for (i = 0; i < FREECIV_AI_MOD_LAST * MAX_NUM_PLAYER_SLOTS; i++) {
68 struct ai_timer *aitimer = aitimer_plrs + i;
69
70 aitimer->count = 0;
71 aitimer->timer = NULL;
72 }
73}
74
75/*************************************************************************/
78void ai_timer_free(void)
79{
80 int i,j;
81 struct ai_timer *aitimer;
82
83 for (i = 0; i < FREECIV_AI_MOD_LAST; i++) {
84 struct ai_type *ai = get_ai_type(i);
85
86 aitimer = aitimers + i;
87
88 if (aitimer->timer) {
89 log_normal("AI timer stats: [%15.3f] ---- (AI type: %s)",
90 timer_read_seconds(aitimer->timer), ai_name(ai));
91 timer_destroy(aitimer->timer);
92 }
93
94 for (j = 0; j < MAX_NUM_PLAYER_SLOTS; j++) {
95 aitimer = aitimer_plrs + j * FREECIV_AI_MOD_LAST + i;
96
97 if (aitimer->timer) {
98 log_normal("AI timer stats: [%15.3f] P%03d (AI type: %s)",
99 timer_read_seconds(aitimer->timer), j, ai_name(ai));
100 timer_destroy(aitimer->timer);
101 }
102 }
103 }
104 free(aitimers);
105 aitimers = NULL;
106
107 free(aitimer_plrs);
108 aitimer_plrs = NULL;
109}
110
111/*************************************************************************/
114static struct ai_timer *ai_timer_get(const struct ai_type *ai)
115{
116 struct ai_timer *aitimer;
117
118 fc_assert_ret_val(ai != NULL, NULL);
119 fc_assert_ret_val(aitimers != NULL, NULL);
120
121 aitimer = aitimers + ai_type_number(ai);
122
123 if (!aitimer->timer) {
124 aitimer->timer = timer_new(TIMER_CPU, TIMER_DEBUG);
125 }
126
127 return aitimer;
128}
129
130/*************************************************************************/
133static struct ai_timer *ai_timer_player_get(const struct player *pplayer)
134{
135 struct ai_timer *aitimer;
136
137 fc_assert_ret_val(pplayer != NULL, NULL);
138 fc_assert_ret_val(pplayer->ai != NULL, NULL);
139 fc_assert_ret_val(aitimer_plrs != NULL, NULL);
140
141 aitimer = aitimer_plrs + (player_index(pplayer) * FREECIV_AI_MOD_LAST
142 + ai_type_number(pplayer->ai));
143
144 if (!aitimer->timer) {
145 aitimer->timer = timer_new(TIMER_CPU, TIMER_DEBUG);
146 }
147
148 return aitimer;
149}
150
151/*************************************************************************/
154void ai_timer_start(const struct ai_type *ai)
155{
156 struct ai_timer *aitimer = ai_timer_get(ai);
157
158 fc_assert_ret(aitimer != NULL);
159 fc_assert_ret(aitimer->timer != NULL);
160
161 if (aitimer->count == 0) {
162 log_debug("AI timer start [%15.3f] ---- (AI type: %s)",
163 timer_read_seconds(aitimer->timer), ai_name(ai));
164 timer_start(aitimer->timer);
165 } else {
166 log_debug("AI timer =====> [depth: %3d] ---- (AI type: %s)",
167 aitimer->count, ai_name(ai));
168 }
169 aitimer->count++;
170}
171
172/*************************************************************************/
175void ai_timer_stop(const struct ai_type *ai)
176{
177 struct ai_timer *aitimer = ai_timer_get(ai);
178
179 fc_assert_ret(aitimer != NULL);
180 fc_assert_ret(aitimer->timer != NULL);
181
182 if (aitimer->count > 0) {
183 if (aitimer->count == 1) {
184 timer_stop(aitimer->timer);
185 log_debug("AI timer stop [%15.3f] ---- (AI type: %s)",
186 timer_read_seconds(aitimer->timer), ai_name(ai));
187 } else {
188 log_debug("AI timer =====> [depth: %3d] ---- (AI type: %s)",
189 aitimer->count, ai_name(ai));
190 }
191 aitimer->count--;
192 } else {
193 log_debug("AI timer missing? ---- (AI type: %s)",
194 ai_name(ai));
195 }
196}
197
198/*************************************************************************/
201void ai_timer_player_start(const struct player *pplayer)
202{
203 struct ai_timer *aitimer = ai_timer_player_get(pplayer);
204
205 fc_assert_ret(aitimer != NULL);
206 fc_assert_ret(aitimer->timer != NULL);
207
208 if (aitimer->count == 0) {
209 log_debug("AI timer start [%15.3f] P%03d (AI type: %s) %s",
210 timer_read_seconds(aitimer->timer), player_index(pplayer),
211 ai_name(pplayer->ai), player_name(pplayer));
212 timer_start(aitimer->timer);
213 } else {
214 log_debug("AI timer =====> [depth: %3d] P%03d (AI type: %s) %s",
215 aitimer->count, player_index(pplayer), ai_name(pplayer->ai),
216 player_name(pplayer));
217 }
218 aitimer->count++;
219}
220
221/*************************************************************************/
224void ai_timer_player_stop(const struct player *pplayer)
225{
226 struct ai_timer *aitimer = ai_timer_player_get(pplayer);
227
228 fc_assert_ret(aitimer != NULL);
229 fc_assert_ret(aitimer->timer != NULL);
230
231 if (aitimer->count > 0) {
232 if (aitimer->count == 1) {
233 timer_stop(aitimer->timer);
234 log_debug("AI timer stop [%15.3f] P%03d (AI type: %s) %s",
235 timer_read_seconds(aitimer->timer), player_index(pplayer),
236 ai_name(pplayer->ai), player_name(pplayer));
237 } else {
238 log_debug("AI timer =====> [depth: %3d] P%03d (AI type: %s) %s",
239 aitimer->count, player_index(pplayer), ai_name(pplayer->ai),
240 player_name(pplayer));
241 }
242 aitimer->count--;
243 } else {
244 log_debug("AI timer missing? P%03d (AI type: %s) %s",
245 player_index(pplayer), ai_name(pplayer->ai),
246 player_name(pplayer));
247 }
248}
249#endif /* DEBUG_AITIMERS */
250
251/*************************************************************************/
254 struct ai_type *get_ai_type(int id)
255{
256 fc_assert(id >= 0 && id < FREECIV_AI_MOD_LAST);
257
258 return &ai_types[id];
259}
260
261/*************************************************************************/
264 void init_ai(struct ai_type *ai)
265{
266 memset(ai, 0, sizeof(*ai));
267}
268
269/*************************************************************************/
272 int ai_type_number(const struct ai_type *ai)
273{
274 int ainbr = ai - ai_types;
275
276 fc_assert_ret_val(ainbr >= 0 && ainbr < FREECIV_AI_MOD_LAST, 0);
277
278 return ainbr;
279}
280
281/*************************************************************************/
284 struct ai_type *ai_type_by_name(const char *search)
285{
286 ai_type_iterate(ai) {
287 if (!fc_strcasecmp(ai_name(ai), search)) {
288 return ai;
289 }
290 } ai_type_iterate_end;
291
292 return NULL;
293}
294
295/*************************************************************************/
298 struct ai_type *ai_type_alloc(void)
299{
300 if (ai_type_count >= FREECIV_AI_MOD_LAST) {
301 log_error(_("Too many AI modules. Max is %d."),
302 FREECIV_AI_MOD_LAST);
303
304 return NULL;
305 }
306
307 return get_ai_type(ai_type_count++);
308}
309
310/*************************************************************************/
313 void ai_type_dealloc(void)
314{
315 ai_type_count--;
316}
317
318/*************************************************************************/
321 int ai_type_get_count(void)
322{
323 return ai_type_count;
324}
325
326/*************************************************************************/
329 const char *ai_name(const struct ai_type *ai)
330{
331 fc_assert_ret_val(ai, NULL);
332 return ai->name;
333}
334
335/*************************************************************************/
340 const char *ai_type_name_or_fallback(const char *orig_name)
341{
342 if (orig_name == NULL) {
343 return NULL;
344 }
345
346 if (ai_type_by_name(orig_name) != NULL) {
347 return orig_name;
348 }
349
350 if (!strcmp("tex", orig_name)
351 || !strcmp("threaded", orig_name)) {
352 struct ai_type *fb;
353
354 fb = ai_type_by_name("classic");
355
356 if (fb != NULL) {
357 /* Get pointer to persistent name of the ai_type */
358 return ai_name(fb);
359 }
360 }
361
362 return NULL;
363}
364
365/*************************************************************************/
368 const char *ai_level_name_update_cb(const char *old)
369{
370#ifndef FREECIV_DEBUG
371 /* No experimental level in !FREECIV_DEBUG build */
372 if (!fc_strcasecmp("Experimental", old)) {
373 /* Convert it to hard */
374 return ai_level_name(AI_LEVEL_HARD);
375 }
376#endif /* FREECIV_DEBUG */
377
378 return old;
379}
ai_level_name_update_cb
const char * ai_level_name_update_cb(const char *old)
Definition ai.c:368
init_ai
void init_ai(struct ai_type *ai)
Definition ai.c:264
ai_type_count
static int ai_type_count
Definition ai.c:33
ai_name
const char * ai_name(const struct ai_type *ai)
Definition ai.c:329
ai_type_alloc
struct ai_type * ai_type_alloc(void)
Definition ai.c:298
ai_type_name_or_fallback
const char * ai_type_name_or_fallback(const char *orig_name)
Definition ai.c:340
ai_type_by_name
struct ai_type * ai_type_by_name(const char *search)
Definition ai.c:284
ai_type_get_count
int ai_type_get_count(void)
Definition ai.c:321
ai_type_dealloc
void ai_type_dealloc(void)
Definition ai.c:313
get_ai_type
struct ai_type * get_ai_type(int id)
Definition ai.c:254
ai_types
static struct ai_type ai_types[FREECIV_AI_MOD_LAST]
Definition ai.c:31
ai_type_number
int ai_type_number(const struct ai_type *ai)
Definition ai.c:272
ai.h
ai_timer_start
#define ai_timer_start(...)
Definition ai.h:356
ai_timer_stop
#define ai_timer_stop(...)
Definition ai.h:357
ai_timer_free
#define ai_timer_free(...)
Definition ai.h:355
ai_timer_player_start
#define ai_timer_player_start(...)
Definition ai.h:358
ai_timer_init
#define ai_timer_init(...)
Definition ai.h:354
ai_timer_player_stop
#define ai_timer_player_stop(...)
Definition ai.h:359
ai_type_iterate_end
#define ai_type_iterate_end
Definition ai.h:369
ai_type_iterate
#define ai_type_iterate(NAME_ai)
Definition ai.h:362
id
int int id
Definition editgui_g.h:28
MAX_NUM_PLAYER_SLOTS
#define MAX_NUM_PLAYER_SLOTS
Definition fc_types.h:32
fcintl.h
_
#define _(String)
Definition fcintl.h:67
log.h
fc_assert_ret
#define fc_assert_ret(condition)
Definition log.h:191
fc_assert
#define fc_assert(condition)
Definition log.h:176
fc_assert_ret_val
#define fc_assert_ret_val(condition, val)
Definition log.h:194
log_debug
#define log_debug(message,...)
Definition log.h:115
log_normal
#define log_normal(message,...)
Definition log.h:107
log_error
#define log_error(message,...)
Definition log.h:103
mem.h
fc_calloc
#define fc_calloc(n, esz)
Definition mem.h:38
player_name
const char * player_name(const struct player *pplayer)
Definition player.c:886
player_index
int player_index(const struct player *pplayer)
Definition player.c:820
player.h
aitimer
static struct timer * aitimer[AIT_LAST][2]
Definition srv_log.c:43
ai_timer
ai_timer
Definition srv_log.h:40
ai_type
Definition ai.h:50
ai_type::name
char name[MAX_LEN_NAME]
Definition ai.h:51
player
Definition player.h:249
player::ai
const struct ai_type * ai
Definition player.h:289
timer
Definition timing.c:81
fc_strcasecmp
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:189
timer_new
struct timer * timer_new(enum timer_timetype type, enum timer_use use)
Definition timing.c:157
timer_destroy
void timer_destroy(struct timer *t)
Definition timing.c:191
timer_start
void timer_start(struct timer *t)
Definition timing.c:224
timer_stop
void timer_stop(struct timer *t)
Definition timing.c:268
timer_read_seconds
double timer_read_seconds(struct timer *t)
Definition timing.c:344
timing.h
TIMER_DEBUG
#define TIMER_DEBUG
Definition timing.h:59
TIMER_CPU
@ TIMER_CPU
Definition timing.h:40