Freeciv-3.3
Loading...
Searching...
No Matches
achievements.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18/* utility */
19#include "fcintl.h"
20#include "log.h"
21#include "rand.h"
22#include "shared.h"
23
24/* common */
25#include "citizens.h"
26#include "city.h"
27#include "culture.h"
28#include "game.h"
29#include "map.h"
30#include "player.h"
31#include "spaceship.h"
32
33#include "achievements.h"
34
36
37/************************************************************************/
55
56/************************************************************************/
60{
61 int i;
62
63 for (i = 0; i < ARRAY_SIZE(achievements); i++) {
64 if (achievements[i].first_msg != NULL) {
66 }
67 if (achievements[i].cons_msg != NULL) {
69 }
70 }
71}
72
73/**********************************************************************/
77{
79
80 return pach->id;
81}
82
83/**********************************************************************/
87{
89
90 return pach - achievements;
91}
92
93/**********************************************************************/
97{
99
100 return &achievements[id];
101}
102
103/**********************************************************************/
107{
108 return name_translation_get(&pach->name);
109}
110
111/**********************************************************************/
115{
116 return rule_name_get(&pach->name);
117}
118
119/**********************************************************************/
124{
125 const char *qs = Qn_(name);
126
129 return pach;
130 }
132
133 return NULL;
134}
135
136/**********************************************************************/
141 struct player_list *achievers)
142{
143 struct player *credited = NULL;
144
145 players_iterate(pplayer) {
146 if (achievement_check(ach, pplayer)) {
147 if (!ach->unique) {
148 pplayer->history += ach->culture;
149 BV_SET(ach->achievers, player_index(pplayer));
150 }
151 player_list_append(achievers, pplayer);
152 }
154
155 if (ach->first != NULL) {
156 /* Already have first one credited. */
157 return NULL;
158 }
159
160 if (player_list_size(achievers) > 0) {
161 /* If multiple players achieved at the same turn, randomly select one
162 * as the one who won the race. */
163 credited = player_list_get(achievers, fc_rand(player_list_size(achievers)));
164
165 ach->first = credited;
166
167 if (ach->unique) {
168 /* For !ach->unique achievements culture was already added above. */
169 credited->history += ach->culture;
170 }
171
172 /* Mark the selected player as the only one having the achievement */
174 }
175
176 return credited;
177}
178
179/**********************************************************************/
182bool achievement_check(struct achievement *ach, struct player *pplayer)
183{
184 if ((ach->unique && ach->first != NULL)
185 || (BV_ISSET(ach->achievers, player_index(pplayer)))) {
186 /* It was already achieved */
187 return FALSE;
188 }
189
190 switch (ach->type) {
192 return pplayer->spaceship.state == SSHIP_LAUNCHED;
193 case ACHIEVEMENT_MAP:
194 {
195 int max_unknown;
196 int required;
197 int total;
198 int known = 0;
199 int unknown = 0;
200
201 /* We calculate max_unknown first for getting the
202 * rounding correctly.
203 * Consider 50 tile map from which we want 25% known.
204 * 50 * 25% = 12.5. Would we round that number of tiles
205 * down, we would get < 25% that's minimum requirement.
206 * Instead we round down (50 - 12.5 = 37.5) -> 37 and then
207 * get the minimum number of full tiles as 50 - 37 = 13. */
208 total = map_num_tiles();
209 max_unknown = (total * (100 - ach->value)) / 100;
210 required = total - max_unknown;
211
212 whole_map_iterate(&(wld.map), ptile) {
213 bool this_is_known = FALSE;
214
215 if (is_server()) {
216 if (dbv_isset(&pplayer->tile_known, tile_index(ptile))) {
218 }
219 } else {
220 /* Client */
221 if (ptile->terrain != T_UNKNOWN) {
223 }
224 }
225
226 if (this_is_known) {
227 known++;
228 if (known >= required) {
229 return TRUE;
230 }
231 } else {
232 unknown++;
233 if (unknown >= max_unknown) {
234 return FALSE;
235 }
236 }
238 }
239
240 return FALSE;
242 {
244 int count = 0;
245
247
248 city_list_iterate(pplayer->cities, pcity) {
249 citizens_iterate(pcity, pslot, pnat) {
250 int idx = player_index(player_slot_get_player(pslot));
251
252 if (!BV_ISSET(seen_citizens, idx)) {
253 BV_SET(seen_citizens, idx);
254 count++;
255 if (count >= ach->value) {
256 /* There's at least value different nationalities. */
257 return TRUE;
258 }
259 }
262 }
263
264 return FALSE;
266 city_list_iterate(pplayer->cities, pcity) {
267 if (city_culture(pcity) >= ach->value) {
268 return TRUE;
269 }
271
272 return FALSE;
274 if (player_culture(pplayer) >= ach->value) {
275 return TRUE;
276 }
277
278 return FALSE;
280 return ((int)fc_rand(10000) < ach->value);
281 case ACHIEVEMENT_HUTS:
282 return pplayer->server.huts >= ach->value;
284 city_list_iterate(pplayer->cities, pcity) {
285 if (city_size_get(pcity) >= ach->value) {
286 return TRUE;
287 }
289
290 return FALSE;
292 return get_literacy(pplayer) >= ach->value;
294 {
295 bool *seen = fc_calloc(wld.map.num_continents, sizeof(bool));
296 int count = 0;
297
298 whole_map_iterate(&(wld.map), ptile) {
299 bool this_is_known = FALSE;
300
301 if (is_server()) {
302 if (dbv_isset(&pplayer->tile_known, tile_index(ptile))) {
304 }
305 } else {
306 /* Client */
307 if (ptile->terrain != T_UNKNOWN) {
309 }
310 }
311
312 if (this_is_known) {
313 /* FIXME: This makes the assumption that fogged tiles belonged
314 * to their current continent when they were last seen. */
315 if (ptile->continent > 0 && !seen[ptile->continent - 1]) {
316 if (++count >= ach->value) {
317 free(seen);
318 return TRUE;
319 }
320 seen[ptile->continent - 1] = TRUE;
321 }
322 }
324
325 free(seen);
326 return FALSE;
327 }
329 return pplayer->score.units_killed >= ach->value;
330 case ACHIEVEMENT_RICH:
331 return pplayer->economic.gold >= ach->value;
333 break;
334 }
335
336 log_error("achievement_check(): Illegal achievement type %d", ach->type);
337
338 return FALSE;
339}
340
341/**********************************************************************/
345{
346 fc_assert(pach->first_msg != NULL);
347
348 return _(pach->first_msg);
349}
350
351/**********************************************************************/
355{
356 fc_assert(pach->cons_msg != NULL);
357
358 return _(pach->cons_msg);
359}
360
361/**********************************************************************/
365 const struct player *pplayer)
366{
367 if (pplayer == NULL) {
368 return FALSE;
369 }
370
371 return BV_ISSET(pach->achievers, player_index(pplayer));
372}
373
374/**********************************************************************/
378{
379 return pach->first != NULL;
380}
381
382/**********************************************************************/
386int get_literacy(const struct player *pplayer)
387{
388 int pop = pplayer->score.population;
389
390 if (pop <= 0) {
391 return 0;
392 } else if (pop >= 10000) {
393 return pplayer->score.literacy / (pop / 100);
394 } else {
395 return (pplayer->score.literacy * 100) / pop;
396 }
397}
bool achievement_check(struct achievement *ach, struct player *pplayer)
void achievements_free(void)
bool achievement_player_has(const struct achievement *pach, const struct player *pplayer)
static struct achievement achievements[MAX_ACHIEVEMENT_TYPES]
int achievement_index(const struct achievement *pach)
struct player * achievement_plr(struct achievement *ach, struct player_list *achievers)
struct achievement * achievement_by_number(int id)
int get_literacy(const struct player *pplayer)
const char * achievement_later_msg(struct achievement *pach)
int achievement_number(const struct achievement *pach)
const char * achievement_first_msg(struct achievement *pach)
const char * achievement_rule_name(struct achievement *pach)
struct achievement * achievement_by_rule_name(const char *name)
const char * achievement_name_translation(struct achievement *pach)
void achievements_init(void)
bool achievement_claimed(const struct achievement *pach)
#define achievements_iterate_end
#define achievements_iterate(_ach_)
bool dbv_isset(const struct dbv *pdbv, int bit)
Definition bitvector.c:120
#define BV_CLR_ALL(bv)
Definition bitvector.h:103
#define BV_SET(bv, bit)
Definition bitvector.h:89
#define BV_ISSET(bv, bit)
Definition bitvector.h:86
#define citizens_iterate_end
Definition citizens.h:54
#define citizens_iterate(_pcity, _pslot, _nationality)
Definition citizens.h:48
#define city_list_iterate(citylist, pcity)
Definition city.h:505
static citizens city_size_get(const struct city *pcity)
Definition city.h:566
#define city_list_iterate_end
Definition city.h:507
char * incite_cost
Definition comments.c:76
int city_culture(const struct city *pcity)
Definition culture.c:29
int player_culture(const struct player *plr)
Definition culture.c:49
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit int const struct action *paction struct unit struct city * pcity
Definition dialogs_g.h:78
int int id
Definition editgui_g.h:28
static bool is_server(void)
#define MAX_ACHIEVEMENT_TYPES
Definition fc_types.h:53
#define _(String)
Definition fcintl.h:67
#define Qn_(String)
Definition fcintl.h:89
struct civ_game game
Definition game.c:61
struct world wld
Definition game.c:62
const char * name
Definition inputfile.c:127
#define fc_assert(condition)
Definition log.h:177
#define fc_assert_ret_val(condition, val)
Definition log.h:195
#define log_error(message,...)
Definition log.h:104
int map_num_tiles(void)
Definition map.c:1152
#define whole_map_iterate(_map, _tile)
Definition map.h:573
#define whole_map_iterate_end
Definition map.h:582
#define fc_calloc(n, esz)
Definition mem.h:38
#define FC_FREE(ptr)
Definition mem.h:41
static const char * rule_name_get(const struct name_translation *ptrans)
static const char * name_translation_get(const struct name_translation *ptrans)
int player_index(const struct player *pplayer)
Definition player.c:829
struct player * player_slot_get_player(const struct player_slot *pslot)
Definition player.c:437
#define players_iterate_end
Definition player.h:542
#define players_iterate(_pplayer)
Definition player.h:537
#define fc_rand(_size)
Definition rand.h:56
#define ARRAY_SIZE(x)
Definition shared.h:85
@ SSHIP_LAUNCHED
Definition spaceship.h:85
char * first_msg
enum achievement_type type
struct player * first
bv_player achievers
char * cons_msg
bool ruledit_disabled
struct packet_ruleset_control control
Definition game.h:83
Continent_id num_continents
Definition map_types.h:83
int units_killed
Definition player.h:105
int population
Definition player.h:96
int literacy
Definition player.h:100
enum spaceship_state state
Definition spaceship.h:108
struct city_list * cities
Definition player.h:281
struct dbv tile_known
Definition player.h:310
int huts
Definition player.h:349
struct player::@73::@75 server
struct player_economic economic
Definition player.h:284
struct player_spaceship spaceship
Definition player.h:286
struct player_score score
Definition player.h:283
int history
Definition player.h:316
struct civ_map map
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:186
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
#define T_UNKNOWN
Definition terrain.h:62
#define tile_index(_pt_)
Definition tile.h:89