File: achievements.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 (395 lines) | stat: -rw-r--r-- 11,863 bytes parent folder | download | duplicates (2)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/***********************************************************************
 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
   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 "rand.h"
#include "shared.h"

/* common */
#include "citizens.h"
#include "city.h"
#include "culture.h"
#include "game.h"
#include "map.h"
#include "player.h"
#include "spaceship.h"

#include "achievements.h"

static struct achievement achievements[MAX_ACHIEVEMENT_TYPES];

/****************************************************************
  Initialize achievements.
****************************************************************/
void achievements_init(void)
{
  int i;

  for (i = 0; i < ARRAY_SIZE(achievements); i++) {
    achievements[i].id = i;
    achievements[i].first = NULL;
    achievements[i].value = 0;
    achievements[i].culture = 0;
    BV_CLR_ALL(achievements[i].achievers);
    achievements[i].first_msg = NULL;
    achievements[i].cons_msg = NULL;
  }
}

/****************************************************************************
  Free the memory associated with achievements
****************************************************************************/
void achievements_free(void)
{
  int i;

  for (i = 0; i < ARRAY_SIZE(achievements); i++) {
    if (achievements[i].first_msg != NULL) {
      FC_FREE(achievements[i].first_msg);
    }
    if (achievements[i].cons_msg != NULL) {
      FC_FREE(achievements[i].cons_msg);
    }
  }
}

/**************************************************************************
  Return the achievement id.
**************************************************************************/
int achievement_number(const struct achievement *pach)
{
  fc_assert_ret_val(NULL != pach, -1);

  return pach->id;
}

/**************************************************************************
  Return the achievement index.
**************************************************************************/
int achievement_index(const struct achievement *pach)
{
  fc_assert_ret_val(NULL != pach, -1);

  return pach - achievements;
}

/****************************************************************************
  Return achievements of given id.
****************************************************************************/
struct achievement *achievement_by_number(int id)
{
  fc_assert_ret_val(id >= 0 && id < game.control.num_achievement_types, NULL);

  return &achievements[id];
}

/****************************************************************************
  Return translated name of this achievement type.
****************************************************************************/
const char *achievement_name_translation(struct achievement *pach)
{
  return name_translation_get(&pach->name);
}

/****************************************************************************
  Return untranslated name of this achievement type.
****************************************************************************/
const char *achievement_rule_name(struct achievement *pach)
{
  return rule_name_get(&pach->name);
}

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

  achievements_iterate(pach) {
    if (!fc_strcasecmp(achievement_rule_name(pach), qs)) {
      return pach;
    }
  } achievements_iterate_end;

  return NULL;
}

/****************************************************************************
  Check if some player has now achieved the achievement and return the player
  in question.
****************************************************************************/
struct player *achievement_plr(struct achievement *ach,
                               struct player_list *achievers)
{
  struct player *credited = NULL;

  players_iterate(pplayer) {
    if (achievement_check(ach, pplayer)) {
      if (!ach->unique) {
        pplayer->history += ach->culture;
        BV_SET(ach->achievers, player_index(pplayer));
      }
      player_list_append(achievers, pplayer);
    }
  } players_iterate_end;

  if (ach->first != NULL) {
    /* Already have first one credited. */
    return NULL;
  }

  if (player_list_size(achievers) > 0) {
    /* If multiple players achieved at the same turn, randomly select one
     * as the one who won the race. */
    credited = player_list_get(achievers, fc_rand(player_list_size(achievers)));

    ach->first = credited;

    if (ach->unique) {
      /* For !ach->unique achievements culture was already added above. */
      credited->history += ach->culture;
    }

    /* Mark the selected player as the only one having the achievement */
    BV_SET(ach->achievers, player_index(credited));
  }

  return credited;
}

/****************************************************************************
  Check if player has now achieved the achievement.
****************************************************************************/
bool achievement_check(struct achievement *ach, struct player *pplayer)
{
  if ((ach->unique && ach->first != NULL)
      || (BV_ISSET(ach->achievers, player_index(pplayer)))) {
    /* It was already achieved */
    return FALSE;
  }

  switch(ach->type) {
  case ACHIEVEMENT_SPACESHIP:
    return pplayer->spaceship.state == SSHIP_LAUNCHED;
  case ACHIEVEMENT_MAP:
    {
      int max_unknown;
      int required;
      int total;
      int known = 0;
      int unknown = 0;

      /* We calculate max_unknown first for getting the
       * rounding correctly.
       * Consider 50 tile map from which we want 25% known.
       * 50 * 25% = 12.5. Would we round that number of tiles
       * down, we would get < 25% that's minimum requirement.
       * Instead we round down (50 - 12.5 = 37.5) -> 37 and then
       * get the minimum number of full tiles as 50 - 37 = 13. */
      total = map_num_tiles();
      max_unknown = (total * (100 - ach->value)) / 100;
      required = total - max_unknown;

      whole_map_iterate(ptile) {
        bool this_is_known = FALSE;

        if (is_server()) {
          if (dbv_isset(&pplayer->tile_known, tile_index(ptile))) {
            this_is_known = TRUE;
          }
        } else {
          /* Client */
          if (ptile->terrain != T_UNKNOWN) {
            this_is_known = TRUE;
          }
        }

        if (this_is_known) {
          known++;
          if (known >= required) {
            return TRUE;
          }
        } else {
          unknown++;
          if (unknown >= max_unknown) {
            return FALSE;
          }
        }
      } whole_map_iterate_end;
    }

    return FALSE;
  case ACHIEVEMENT_MULTICULTURAL:
    {
      bv_player seen_citizens;
      int count = 0;

      BV_CLR_ALL(seen_citizens);

      city_list_iterate(pplayer->cities, pcity) {
        citizens_iterate(pcity, pslot, pnat) {
          int idx = player_index(player_slot_get_player(pslot));

          if (!BV_ISSET(seen_citizens, idx)) {
            BV_SET(seen_citizens, idx);
            count++;
            if (count >= ach->value) {
              /* There's at least value different nationalities. */
              return TRUE;
            }
          }
        } citizens_iterate_end;
      } city_list_iterate_end;
    }

    return FALSE;
  case ACHIEVEMENT_CULTURED_CITY:
    city_list_iterate(pplayer->cities, pcity) {
      if (city_culture(pcity) >= ach->value) {
        return TRUE;
      }
    } city_list_iterate_end;

    return FALSE;
  case ACHIEVEMENT_CULTURED_NATION:
    if (player_culture(pplayer) >= ach->value) {
      return TRUE;
    }

    return FALSE;
  case ACHIEVEMENT_LUCKY:
    return (fc_rand(10000) < ach->value);
  case ACHIEVEMENT_HUTS:
    return pplayer->server.huts >= ach->value;
  case ACHIEVEMENT_METROPOLIS:
    city_list_iterate(pplayer->cities, pcity) {
      if (city_size_get(pcity) >= ach->value) {
        return TRUE;
      }
    } city_list_iterate_end;

    return FALSE;
  case ACHIEVEMENT_LITERATE:
    return get_literacy(pplayer) >= ach->value;
  case ACHIEVEMENT_LAND_AHOY:
    {
      bool seen[game.map.num_continents];
      int i;
      int count = 0;

      for (i = 0; i < game.map.num_continents; i++) {
        seen[i] = FALSE;
      }

      whole_map_iterate(ptile) {
        bool this_is_known = FALSE;

        if (is_server()) {
          if (dbv_isset(&pplayer->tile_known, tile_index(ptile))) {
            this_is_known = TRUE;
          }
        } else {
          /* Client */
          if (ptile->terrain != T_UNKNOWN) {
            this_is_known = TRUE;
          }
        }

        if (this_is_known) {
          /* FIXME: This makes the assumption that fogged tiles belonged
           *        to their current continent when they were last seen. */
          if (ptile->continent > 0 && !seen[ptile->continent - 1]) {
            if (++count >= ach->value) {
              return TRUE;
            }
            seen[ptile->continent - 1] = TRUE;
          }
        }
      } whole_map_iterate_end;

      return FALSE;
    }
  case ACHIEVEMENT_COUNT:
    break;
  }

  log_error("achievement_check(): Illegal achievement type %d", ach->type);

  return FALSE;
}

/****************************************************************************
  Return message to send to first player gaining the achievement.
****************************************************************************/
const char *achievement_first_msg(struct achievement *pach)
{
  fc_assert(pach->first_msg != NULL);

  return _(pach->first_msg);
}

/****************************************************************************
  Return message to send to other players gaining the achievement.
****************************************************************************/
const char *achievement_later_msg(struct achievement *pach)
{
  fc_assert(pach->cons_msg != NULL);

  return _(pach->cons_msg);
}

/****************************************************************************
  Has the given player got the achievement?
****************************************************************************/
bool achievement_player_has(const struct achievement *pach,
                            const struct player *pplayer)
{
  if (pplayer == NULL) {
    return FALSE;
  }

  return BV_ISSET(pach->achievers, player_index(pplayer));
}

/****************************************************************************
  Has anybody got the achievement?
****************************************************************************/
bool achievement_claimed(const struct achievement *pach)
{
  return pach->first != NULL;
}

/****************************************************************************
  Literacy score calculated one way. See also get_literacy2() for
  alternative way.
****************************************************************************/
int get_literacy(const struct player *pplayer)
{
  int pop = civ_population(pplayer);

  if (pop <= 0) {
    return 0;
  } else if (pop >= 10000) {
    return pplayer->score.literacy / (pop / 100);
  } else {
    return (pplayer->score.literacy * 100) / pop;
  }
}