File: style.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 (260 lines) | stat: -rw-r--r-- 7,768 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
/***********************************************************************
 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 "mem.h"

/* common */
#include "fc_types.h"
#include "game.h"
#include "name_translation.h"


#include "style.h"

static struct nation_style *styles = NULL;

static struct music_style *music_styles = NULL;

/****************************************************************************
  Initialise styles structures.
****************************************************************************/
void styles_alloc(int count)
{
  int i;

  styles = fc_malloc(count * sizeof(struct nation_style));

  for (i = 0; i < count; i++) {
    styles[i].id = i;
  }
}

/****************************************************************************
  Free the memory associated with styles
****************************************************************************/
void styles_free(void)
{
  FC_FREE(styles);
  styles = NULL;
}

/**************************************************************************
  Return the number of styles.
**************************************************************************/
int style_count(void)
{
  return game.control.num_styles;
}

/**************************************************************************
  Return the style id.
**************************************************************************/
int style_number(const struct nation_style *pstyle)
{
  fc_assert_ret_val(NULL != pstyle, -1);

  return pstyle->id;
}

/**************************************************************************
  Return the style index.
**************************************************************************/
int style_index(const struct nation_style *pstyle)
{
  fc_assert_ret_val(NULL != pstyle, -1);

  return pstyle - styles;
}

/****************************************************************************
  Return style of given id.
****************************************************************************/
struct nation_style *style_by_number(int id)
{
  fc_assert_ret_val(id >= 0 && id < game.control.num_styles, NULL);

  return &styles[id];
}

/**************************************************************************
  Return the (translated) name of the style.
  You don't have to free the return pointer.
**************************************************************************/
const char *style_name_translation(const struct nation_style *pstyle)
{
  return name_translation_get(&pstyle->name);
}

/**************************************************************************
  Return the (untranslated) rule name of the style.
  You don't have to free the return pointer.
**************************************************************************/
const char *style_rule_name(const struct nation_style *pstyle)
{
  return rule_name_get(&pstyle->name);
}

/**************************************************************************
  Returns style matching rule name or NULL if there is no style
  with such name.
**************************************************************************/
struct nation_style *style_by_rule_name(const char *name)
{
  const char *qs = Qn_(name);

  styles_iterate(pstyle) {
    if (!fc_strcasecmp(style_rule_name(pstyle), qs)) {
      return pstyle;
    }
  } styles_iterate_end;

  return NULL;
}

/****************************************************************************
  Initialise music styles structures.
****************************************************************************/
void music_styles_alloc(int count)
{
  int i;

  music_styles = fc_malloc(count * sizeof(struct music_style));

  for (i = 0; i < count; i++) {
    music_styles[i].id = i;
    requirement_vector_init(&(music_styles[i].reqs));
  }
}

/****************************************************************************
  Free the memory associated with music styles
****************************************************************************/
void music_styles_free(void)
{
  music_styles_iterate(pmus) {
    requirement_vector_free(&(pmus->reqs));
  } music_styles_iterate_end;

  FC_FREE(music_styles);
  music_styles = NULL;
}

/**************************************************************************
  Return the music style id.
**************************************************************************/
int music_style_number(const struct music_style *pms)
{
  fc_assert_ret_val(NULL != pms, -1);

  return pms->id;
}

/****************************************************************************
  Return music style of given id.
****************************************************************************/
struct music_style *music_style_by_number(int id)
{
  fc_assert_ret_val(id >= 0 && id < game.control.num_music_styles, NULL);

  if (music_styles == NULL) {
    return NULL;
  }

  return &music_styles[id];
}

/****************************************************************************
  Return music style for player
****************************************************************************/
struct music_style *player_music_style(struct player *plr)
{
  struct music_style *best = NULL;

  music_styles_iterate(pms) {
    if (are_reqs_active(plr, NULL, NULL, NULL, NULL,
                        NULL, NULL, NULL, NULL, &pms->reqs,
                        RPT_CERTAIN)) {
      best = pms;
    }
  } music_styles_iterate_end;

  return best;
}

/**************************************************************************
  Evaluate which style should be used to draw a city.
**************************************************************************/
int style_of_city(const struct city *pcity)
{
  return pcity->style;
}

/**************************************************************************
  Return basic city style representing nation style.
**************************************************************************/
int basic_city_style_for_style(struct nation_style *pstyle)
{
  enum fc_tristate style_style;
  int i;

  for (i = game.control.styles_count - 1; i >= 0; i--) {
    style_style = TRI_MAYBE;

    requirement_vector_iterate(&city_styles[i].reqs, preq) {
      if (preq->source.kind == VUT_STYLE
          && preq->source.value.style == pstyle
          && style_style != TRI_NO) {
        style_style = TRI_YES;
      } else {
        /* No any other requirements allowed at the moment.
         * TODO: Allow some other reqs */
        style_style = TRI_NO;
        break;
      }
    } requirement_vector_iterate_end;

    if (style_style == TRI_YES) {
      break;
    }
  }

  if (style_style == TRI_YES) {
    return i;
  }

  return -1;
}

/**************************************************************************
  Return citystyle of the city.
**************************************************************************/
int city_style(struct city *pcity)
{
  int i;
  struct player *plr = city_owner(pcity);

  for (i = game.control.styles_count - 1; i >= 0; i--) {
    if (are_reqs_active(plr, NULL, pcity, NULL, city_tile(pcity),
                        NULL, NULL, NULL, NULL, &city_styles[i].reqs,
                        RPT_CERTAIN)) {
      return i;
    }
  }

  return 0;
}