File: specialist.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 (224 lines) | stat: -rw-r--r-- 7,402 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
/***********************************************************************
 Freeciv - Copyright (C) 2005 - The Freeciv Project
   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

/* utility */
#include "fcintl.h"
#include "log.h"
#include "string_vector.h"

/* common */
#include "city.h"
#include "effects.h"
#include "game.h"

#include "specialist.h"

struct specialist specialists[SP_MAX];
int default_specialist;

/****************************************************************************
  Initialize data for specialists.
****************************************************************************/
void specialists_init(void)
{
  int i;

  for (i = 0; i < ARRAY_SIZE(specialists); i++) {
    struct specialist *p = &specialists[i];

    p->item_number = i;

    requirement_vector_init(&p->reqs);
  }
}

/****************************************************************************
  Free data for specialists.
****************************************************************************/
void specialists_free(void)
{
  int i;

  for (i = 0; i < ARRAY_SIZE(specialists); i++) {
    struct specialist *p = &specialists[i];

    requirement_vector_free(&p->reqs);
    if (NULL != p->helptext) {
      strvec_destroy(p->helptext);
      p->helptext = NULL;
    }
  }
}

/**************************************************************************
  Return the number of specialist_types.
**************************************************************************/
Specialist_type_id specialist_count(void)
{
  return game.control.num_specialist_types;
}

/****************************************************************************
  Return the specialist index.

  Currently same as specialist_number(), paired with specialist_count()
  indicates use as an array index.
****************************************************************************/
Specialist_type_id specialist_index(const struct specialist *sp)
{
  fc_assert_ret_val(NULL != sp, -1);
  return sp - specialists;
}

/****************************************************************************
  Return the specialist item_number.
****************************************************************************/
Specialist_type_id specialist_number(const struct specialist *sp)
{
  fc_assert_ret_val(NULL != sp, -1);
  return sp->item_number;
}

/****************************************************************************
  Return the specialist pointer for the given number.
****************************************************************************/
struct specialist *specialist_by_number(const Specialist_type_id id)
{
  if (id < 0 || id >= game.control.num_specialist_types) {
    return NULL;
  }
  return &specialists[id];
}

/****************************************************************************
  Return the specialist type with the given (untranslated!) rule name.
  Returns NULL if none match.
****************************************************************************/
struct specialist *specialist_by_rule_name(const char *name)
{
  const char *qname = Qn_(name);

  specialist_type_iterate(i) {
    struct specialist *sp = specialist_by_number(i);
    if (0 == fc_strcasecmp(specialist_rule_name(sp), qname)) {
      return sp;
    }
  } specialist_type_iterate_end;

  return NULL;
}

/****************************************************************************
  Return the specialist type with the given (translated, plural) name.
  Returns NULL if none match.
****************************************************************************/
struct specialist *specialist_by_translated_name(const char *name)
{
  specialist_type_iterate(i) {
    struct specialist *sp = specialist_by_number(i);
    if (0 == strcmp(specialist_plural_translation(sp), name)) {
      return sp;
    }
  } specialist_type_iterate_end;

  return NULL;
}

/**************************************************************************
  Return the (untranslated) rule name of the specialist type.
  You don't have to free the return pointer.
**************************************************************************/
const char *specialist_rule_name(const struct specialist *sp)
{
  return rule_name_get(&sp->name);
}

/**************************************************************************
  Return the (translated, plural) name of the specialist type.
  You don't have to free the return pointer.
**************************************************************************/
const char *specialist_plural_translation(const struct specialist *sp)
{
  return name_translation_get(&sp->name);
}

/**************************************************************************
  Return the (translated) abbreviation of the specialist type.
  You don't have to free the return pointer.
**************************************************************************/
const char *specialist_abbreviation_translation(const struct specialist *sp)
{
  return name_translation_get(&sp->abbreviation);
}

/****************************************************************************
  Return a string containing all the specialist abbreviations, for instance
  "E/S/T".
  You don't have to free the return pointer.
****************************************************************************/
const char *specialists_abbreviation_string(void)
{
  static char buf[5 * SP_MAX];

  buf[0] = '\0';

  specialist_type_iterate(sp) {
    char *separator = (buf[0] == '\0') ? "" : "/";

    cat_snprintf(buf, sizeof(buf), "%s%s", separator,
                 specialist_abbreviation_translation(specialist_by_number(sp)));
  } specialist_type_iterate_end;

  return buf;
}

/****************************************************************************
  Return a string showing the number of specialists in the array.

  For instance with a city with (0,3,1) specialists call

    specialists_string(pcity->specialists);

  and you'll get "0/3/1".
****************************************************************************/
const char *specialists_string(const citizens *specialist_list)
{
  static char buf[5 * SP_MAX];

  buf[0] = '\0';

  specialist_type_iterate(sp) {
    char *separator = (buf[0] == '\0') ? "" : "/";

    cat_snprintf(buf, sizeof(buf), "%s%d", separator, specialist_list[sp]);
  } specialist_type_iterate_end;

  return buf;
}

/****************************************************************************
  Return the output for the specialist type with this output type.
****************************************************************************/
int get_specialist_output(const struct city *pcity,
			  Specialist_type_id sp, Output_type_id otype)
{
  struct specialist *pspecialist = &specialists[sp];
  struct output_type *poutput = get_output_type(otype);

  return get_city_specialist_output_bonus(pcity, pspecialist, poutput,
					  EFT_SPECIALIST_OUTPUT);
}