Freeciv21
Develop your civilization from humble roots to a global empire
ai.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 1996-2020 Freeciv21 and Freeciv contributors. This file is
3  /\/\ part of Freeciv21. Freeciv21 is free software: you can
4  \_\ _..._ redistribute it and/or modify it under the terms of the
5  (" )(_..._) GNU General Public License as published by the Free
6  ^^ // \\ Software Foundation, either version 3 of the License,
7  or (at your option) any later version. You should have
8 received a copy of the GNU General Public License along with Freeciv21.
9  If not, see https://www.gnu.org/licenses/.
10  */
11 
12 #include <fc_config.h>
13 
14 #include <cstring>
15 
16 // utility
17 #include "fcintl.h"
18 #include "log.h" // fc_assert
19 #include "timing.h"
20 
21 // common
22 #include "ai.h"
23 #include "player.h"
24 
25 static struct ai_type ai_types[FREECIV_AI_MOD_LAST];
26 
27 static int ai_type_count = 0;
28 
32 struct ai_type *get_ai_type(int id)
33 {
34  fc_assert(id >= 0 && id < FREECIV_AI_MOD_LAST);
35 
36  return &ai_types[id];
37 }
38 
42 void init_ai(struct ai_type *ai) { memset(ai, 0, sizeof(*ai)); }
43 
47 int ai_type_number(const struct ai_type *ai)
48 {
49  int ainbr = ai - ai_types;
50 
51  fc_assert_ret_val(ainbr >= 0 && ainbr < FREECIV_AI_MOD_LAST, 0);
52 
53  return ainbr;
54 }
55 
59 struct ai_type *ai_type_by_name(const char *search)
60 {
61  ai_type_iterate(ai)
62  {
63  if (!fc_strcasecmp(ai_name(ai), search)) {
64  return ai;
65  }
66  }
68 
69  return nullptr;
70 }
71 
76 {
77  if (ai_type_count >= FREECIV_AI_MOD_LAST) {
78  qCritical(_("Too many AI modules. Max is %d."), FREECIV_AI_MOD_LAST);
79 
80  return nullptr;
81  }
82 
83  return get_ai_type(ai_type_count++);
84 }
85 
90 
95 
99 const char *ai_name(const struct ai_type *ai)
100 {
101  fc_assert_ret_val(ai, nullptr);
102  return ai->name;
103 }
104 
110 const char *ai_type_name_or_fallback(const char *orig_name)
111 {
112  if (orig_name == nullptr) {
113  return nullptr;
114  }
115 
116  if (ai_type_by_name(orig_name) != nullptr) {
117  return orig_name;
118  }
119 
120  auto fb = ai_type_by_name("classic");
121  if (fb != nullptr) {
122  qWarning(_("Unknown AI type %s, using \"classic\" instead"), orig_name);
123  // Get pointer to persistent name of the ai_type
124  return ai_name(fb);
125  }
126 
127  return nullptr;
128 }
void init_ai(struct ai_type *ai)
Initializes AI structure.
Definition: ai.cpp:42
static int ai_type_count
Definition: ai.cpp:27
struct ai_type * get_ai_type(int id)
Returns ai_type of given id.
Definition: ai.cpp:32
const char * ai_name(const struct ai_type *ai)
Return the name of the ai type.
Definition: ai.cpp:99
void ai_type_dealloc()
Free latest ai_type.
Definition: ai.cpp:89
static struct ai_type ai_types[FREECIV_AI_MOD_LAST]
Definition: ai.cpp:25
struct ai_type * ai_type_by_name(const char *search)
Find ai type with given name.
Definition: ai.cpp:59
struct ai_type * ai_type_alloc()
Return next free ai_type.
Definition: ai.cpp:75
const char * ai_type_name_or_fallback(const char *orig_name)
Return usable ai type name, if possible.
Definition: ai.cpp:110
int ai_type_get_count()
Return number of ai types.
Definition: ai.cpp:94
int ai_type_number(const struct ai_type *ai)
Returns id of the given ai_type.
Definition: ai.cpp:47
#define ai_type_iterate_end
Definition: ai.h:376
#define ai_type_iterate(NAME_ai)
Definition: ai.h:369
#define _(String)
Definition: fcintl.h:50
#define fc_assert(condition)
Definition: log.h:89
#define fc_assert_ret_val(condition, val)
Definition: log.h:114
Definition: ai.h:42
char name[MAX_LEN_NAME]
Definition: ai.h:43
int fc_strcasecmp(const char *str0, const char *str1)
Compare strings like strcmp(), but ignoring case.
Definition: support.cpp:89