Freeciv21
Develop your civilization from humble roots to a global empire
specialist.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 1996-2020 Freeciv21 and Freeciv contributors. This file
3  is part of Freeciv21. Freeciv21 is free software:
4 |\_/|,,_____,~~` you can redistribute it and/or modify it under the
5 (.".)~~ )`~}} terms of the GNU General Public License as published
6  \o/\ /---~\\ ~}} by the Free Software Foundation, either version 3 of
7  _// _// ~} the License, or (at your option) any later version.
8  You should have received a copy of the GNU General
9  Public License along with Freeciv21. If not, see
10  https://www.gnu.org/licenses/.
11  */
12 
13 // utility
14 #include "fcintl.h"
15 #include "log.h"
16 
17 // common
18 #include "city.h"
19 #include "effects.h"
20 #include "game.h"
21 
22 #include "specialist.h"
23 
26 
31 {
32  int i;
33 
34  for (i = 0; i < ARRAY_SIZE(specialists); i++) {
35  struct specialist *p = &specialists[i];
36 
37  p->item_number = i;
38  p->ruledit_disabled = false;
39 
40  requirement_vector_init(&p->reqs);
41  }
42 }
43 
48 {
49  int i;
50 
51  for (i = 0; i < ARRAY_SIZE(specialists); i++) {
52  struct specialist *p = &specialists[i];
53 
54  requirement_vector_free(&p->reqs);
55  delete p->helptext;
56  p->helptext = nullptr;
57  }
58 }
59 
64 {
65  return game.control.num_specialist_types;
66 }
67 
75 {
76  fc_assert_ret_val(nullptr != sp, 0);
77  return sp - specialists;
78 }
79 
84 {
85  fc_assert_ret_val(nullptr != sp, 0);
86  return sp->item_number;
87 }
88 
93 {
94  if (id < 0 || id >= game.control.num_specialist_types) {
95  return nullptr;
96  }
97  return &specialists[id];
98 }
99 
105 {
106  const char *qname = Qn_(name);
107 
109  {
110  struct specialist *sp = specialist_by_number(i);
111  if (0 == fc_strcasecmp(specialist_rule_name(sp), qname)) {
112  return sp;
113  }
114  }
116 
117  return nullptr;
118 }
119 
125 {
127  {
128  struct specialist *sp = specialist_by_number(i);
129  if (0 == strcmp(specialist_plural_translation(sp), name)) {
130  return sp;
131  }
132  }
134 
135  return nullptr;
136 }
137 
142 const char *specialist_rule_name(const struct specialist *sp)
143 {
144  return rule_name_get(&sp->name);
145 }
146 
151 const char *specialist_plural_translation(const struct specialist *sp)
152 {
153  return name_translation_get(&sp->name);
154 }
155 
160 const char *specialist_abbreviation_translation(const struct specialist *sp)
161 {
162  return name_translation_get(&sp->abbreviation);
163 }
164 
171 {
172  static char buf[5 * SP_MAX];
173 
174  buf[0] = '\0';
175 
177  {
178  const char *separator = (buf[0] == '\0') ? "" : "/";
179 
180  cat_snprintf(
181  buf, sizeof(buf), "%s%s", separator,
183  }
185 
186  return buf;
187 }
188 
198 const char *specialists_string(const citizens *specialist_list)
199 {
200  static char buf[5 * SP_MAX];
201 
202  buf[0] = '\0';
203 
205  {
206  const char *separator = (buf[0] == '\0') ? "" : "/";
207 
208  cat_snprintf(buf, sizeof(buf), "%s%d", separator, specialist_list[sp]);
209  }
211 
212  return buf;
213 }
214 
218 int get_specialist_output(const struct city *pcity, Specialist_type_id sp,
219  Output_type_id otype)
220 {
221  struct specialist *pspecialist = &specialists[sp];
222  struct output_type *poutput = get_output_type(otype);
223 
224  return get_city_specialist_output_bonus(pcity, pspecialist, poutput,
225  EFT_SPECIALIST_OUTPUT);
226 }
struct output_type * get_output_type(Output_type_id output)
Return the output type for this index.
Definition: city.cpp:611
int get_city_specialist_output_bonus(const struct city *pcity, const struct specialist *pspecialist, const struct output_type *poutput, enum effect_type effect_type)
Returns the effect bonus of a specialist in a city.
Definition: effects.cpp:703
unsigned char citizens
Definition: fc_types.h:305
#define SP_MAX
Definition: fc_types.h:324
int Specialist_type_id
Definition: fc_types.h:292
enum output_type_id Output_type_id
Definition: fc_types.h:295
#define Qn_(String)
Definition: fcintl.h:66
struct civ_game game
Definition: game.cpp:47
const char * name
Definition: inputfile.cpp:118
#define fc_assert_ret_val(condition, val)
Definition: log.h:114
static const char * rule_name_get(const struct name_translation *ptrans)
static const char * name_translation_get(const struct name_translation *ptrans)
#define ARRAY_SIZE(x)
Definition: shared.h:79
struct specialist specialists[SP_MAX]
Definition: specialist.cpp:24
struct specialist * specialist_by_rule_name(const char *name)
Return the specialist type with the given (untranslated!) rule name.
Definition: specialist.cpp:104
void specialists_free()
Free data for specialists.
Definition: specialist.cpp:47
const char * specialists_string(const citizens *specialist_list)
Return a string showing the number of specialists in the array.
Definition: specialist.cpp:198
const char * specialist_rule_name(const struct specialist *sp)
Return the (untranslated) rule name of the specialist type.
Definition: specialist.cpp:142
struct specialist * specialist_by_translated_name(const char *name)
Return the specialist type with the given (translated, plural) name.
Definition: specialist.cpp:124
void specialists_init()
Initialize data for specialists.
Definition: specialist.cpp:30
Specialist_type_id specialist_index(const struct specialist *sp)
Return the specialist index.
Definition: specialist.cpp:74
Specialist_type_id specialist_number(const struct specialist *sp)
Return the specialist item_number.
Definition: specialist.cpp:83
int default_specialist
Definition: specialist.cpp:25
const char * specialist_abbreviation_translation(const struct specialist *sp)
Return the (translated) abbreviation of the specialist type.
Definition: specialist.cpp:160
const char * specialists_abbreviation_string()
Return a string containing all the specialist abbreviations, for instance "E/S/T".
Definition: specialist.cpp:170
struct specialist * specialist_by_number(const Specialist_type_id id)
Return the specialist pointer for the given number.
Definition: specialist.cpp:92
Specialist_type_id specialist_count()
Return the number of specialist_types.
Definition: specialist.cpp:63
const char * specialist_plural_translation(const struct specialist *sp)
Return the (translated, plural) name of the specialist type.
Definition: specialist.cpp:151
int get_specialist_output(const struct city *pcity, Specialist_type_id sp, Output_type_id otype)
Return the output for the specialist type with this output type.
Definition: specialist.cpp:218
#define specialist_type_iterate_end
Definition: specialist.h:73
#define specialist_type_iterate(sp)
Definition: specialist.h:67
Definition: city.h:291
struct packet_ruleset_control control
Definition: game.h:74
int item_number
Definition: specialist.h:24
struct requirement_vector reqs
Definition: specialist.h:32
bool ruledit_disabled
Definition: specialist.h:27
struct name_translation name
Definition: specialist.h:25
QVector< QString > * helptext
Definition: specialist.h:34
struct name_translation abbreviation
Definition: specialist.h:26
int fc_strcasecmp(const char *str0, const char *str1)
Compare strings like strcmp(), but ignoring case.
Definition: support.cpp:89
int cat_snprintf(char *str, size_t n, const char *format,...)
cat_snprintf is like a combination of fc_snprintf and fc_strlcat; it does snprintf to the end of an e...
Definition: support.cpp:564