Freeciv-3.4
Loading...
Searching...
No Matches
rscompat.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/* ANSI */
19#ifdef HAVE_STRING_H
20#include <string.h>
21#endif
22
23/* utility */
24#include "capability.h"
25#include "log.h"
26#include "registry.h"
27
28/* common */
29#include "actions.h"
30#include "effects.h"
31#include "fc_types.h"
32#include "game.h"
33#include "movement.h"
34#include "requirements.h"
35#include "unittype.h"
36
37/* server */
38#include "rssanity.h"
39#include "ruleload.h"
40#include "settings.h"
41
42#include "rscompat.h"
43
44#define MAXIMUM_CLAIMED_OCEAN_SIZE_3_2 (20)
45
46#define enough_new_user_flags(_new_flags_, _name_, \
47 _LAST_USER_FLAG_, _LAST_USER_FLAG_PREV_) \
48FC_STATIC_ASSERT((ARRAY_SIZE(_new_flags_) \
49 <= _LAST_USER_FLAG_ - _LAST_USER_FLAG_PREV_), \
50 not_enough_new_##_name_##_user_flags)
51
52#define UTYF_LAST_USER_FLAG_3_3 UTYF_USER_FLAG_53
53#define TECH_LAST_USER_FLAG_3_3 TECH_USER_10
54
55static int first_free_unit_type_user_flag(void);
56static int first_free_tech_user_flag(void);
57
58/**********************************************************************/
62{
63 memset(info, 0, sizeof(*info));
64}
65
66/**********************************************************************/
71 const char *filename,
72 const struct rscompat_info *info)
73{
74 const char *datafile_options;
75 bool ok = FALSE;
76 int format;
77
78 if (!(datafile_options = secfile_lookup_str(file, "datafile.options"))) {
79 log_fatal("\"%s\": ruleset capability problem:", filename);
81
82 return 0;
83 }
84
85 if (info->compat_mode) {
86 /* Check alternative capstr first, so that when we do the main capstr check,
87 * we already know that failures there are fatal (error message correct, can return
88 * immediately) */
89
92 ok = TRUE;
93 }
94 }
95
96 if (!ok) {
98 log_fatal("\"%s\": ruleset datafile appears incompatible:", filename);
99 log_fatal(" datafile options: %s", datafile_options);
100 log_fatal(" supported options: %s", RULESET_CAPABILITIES);
101 ruleset_error(NULL, LOG_ERROR, "Capability problem");
102
103 return 0;
104 }
106 log_fatal("\"%s\": ruleset datafile claims required option(s)"
107 " that we don't support:", filename);
108 log_fatal(" datafile options: %s", datafile_options);
109 log_fatal(" supported options: %s", RULESET_CAPABILITIES);
110 ruleset_error(NULL, LOG_ERROR, "Capability problem");
111
112 return 0;
113 }
114 }
115
116 if (!secfile_lookup_int(file, &format, "datafile.format_version")) {
117 log_error("\"%s\": lacking legal format_version field", filename);
119
120 return 0;
121 } else if (format == 0) {
122 log_error("\"%s\": Illegal format_version value", filename);
123 ruleset_error(NULL, LOG_ERROR, "Format version error");
124 }
125
126 return format;
127}
128/**********************************************************************/
136 const char *filename,
137 const struct rscompat_info *info)
138{
139 int format_version;
140
141 fc_assert_ret_val(info->version > 0, FALSE);
142
143 format_version = rscompat_check_capabilities(file, filename, info);
144 if (format_version <= 0) {
145 /* Already logged in rscompat_check_capabilities */
146 return FALSE;
147 }
148
149 if (format_version != info->version) {
150 log_fatal("\"%s\": ruleset datafile format version differs from"
151 " other ruleset datafile(s):", filename);
152 log_fatal(" datafile format version: %d", format_version);
153 log_fatal(" expected format version: %d", info->version);
154 ruleset_error(NULL, LOG_ERROR, "Inconsistent format versions");
155
156 return FALSE;
157 }
158
159 return TRUE;
160}
161
162/**********************************************************************/
169static bool
171{
172 struct req_vec_problem *problem;
173
175 /* Some changes requires starting to process an action's enablers from
176 * the beginning. */
177 bool needs_restart = FALSE;
178
180 /* A hard obligatory requirement is missing. */
181
182 int i;
183
184 if (problem->num_suggested_solutions == 0) {
185 /* Didn't get any suggestions about how to solve this. */
186
187 log_error("While adding hard obligatory reqs to action enabler"
188 " for %s: %s"
189 " Don't know how to fix it."
190 " Dropping it.",
191 action_rule_name(paction), problem->description);
192 ae->rulesave.ruledit_disabled = TRUE;
193
195 return TRUE;
196 }
197
198 /* Sanity check. */
199 fc_assert_ret_val(problem->num_suggested_solutions > 0,
201
202 /* Only append is supported for upgrade */
203 for (i = 0; i < problem->num_suggested_solutions; i++) {
204 if (problem->suggested_solutions[i].operation != RVCO_APPEND) {
205 /* A problem that isn't caused by missing obligatory hard
206 * requirements has been detected.
207 *
208 * Probably an old requirement that contradicted a hard requirement
209 * that wasn't documented by making it obligatory. In that case all
210 * suggested solutions has been applied to the enabler creating a
211 * new copy for each possible fulfillment of the new obligatory hard
212 * requirement.
213 *
214 * If another copy of the original enabler has survived this isn't
215 * an error. It probably isn't event an indication of a potential
216 * problem.
217 *
218 * If no possible solution survives the enabler was never in use
219 * because the action it self would have blocked it. In that case
220 * this is an error. */
221
222 log_warn("While adding hard obligatory reqs to action enabler"
223 " for %s: %s"
224 " Dropping it.",
225 action_rule_name(paction), problem->description);
226 ae->rulesave.ruledit_disabled = TRUE;
228 return TRUE;
229 }
230 }
231
232 for (i = 0; i < problem->num_suggested_solutions; i++) {
234
235 /* There can be more than one suggestion to apply. In that case both
236 * are applied to their own copy. The original should therefore be
237 * kept for now. */
239
240 /* Apply the solution. */
241 if (!req_vec_change_apply(&problem->suggested_solutions[i],
243 new_enabler)) {
244 log_error("While adding hard obligatory reqs to action enabler"
245 " for %s: %s"
246 "Failed to apply solution %s."
247 " Dropping it.",
248 action_rule_name(paction), problem->description,
250 &problem->suggested_solutions[i],
252 new_enabler->rulesave.ruledit_disabled = TRUE;
254 return TRUE;
255 }
256
257 if (problem->num_suggested_solutions - 1 == i) {
258 /* The last modification is to the original enabler. */
259 ae->action = new_enabler->action;
260 ae->rulesave.ruledit_disabled = new_enabler->rulesave.ruledit_disabled;
261 requirement_vector_copy(&ae->actor_reqs,
262 &new_enabler->actor_reqs);
263 requirement_vector_copy(&ae->target_reqs,
264 &new_enabler->target_reqs);
266 } else {
267 /* Register the new enabler */
269
270 /* This changes the number of action enablers. */
272 }
273 }
274
276
277 if (needs_restart) {
278 /* May need to apply future upgrades to the copies too. */
279 return TRUE;
280 }
281 }
282
283 return needs_restart;
284}
285
286/**********************************************************************/
291{
292 log_normal("action enablers: adding obligatory hard requirements.");
293 log_warn("More than one way to fulfill a new obligatory hard requirement"
294 " may exist."
295 " In that case the enabler is copied so each alternative"
296 " solution is applied to a copy of the enabler."
297 " If an action enabler becomes self contradicting after applying"
298 " a solution it is dropped."
299 " Note that other copies of the original enabler may have"
300 " survived even if one copy is dropped.");
301
302 action_iterate(act_id) {
304
305 do {
308 if (ae->rulesave.ruledit_disabled) {
309 /* Ignore disabled enablers */
310 continue;
311 }
313 /* Something important, probably the number of action enablers
314 * for this action, changed. Start over again on this action's
315 * enablers. */
317 break;
318 }
321
323}
324
325/**********************************************************************/
336{
337 if (info->version < RSFORMAT_3_4) {
338 int first_free;
339 int i;
340
341 /* Some unit type flags moved to the ruleset between 3.3 and 3.4.
342 * Add them back as user flags.
343 * XXX: ruleset might not need all of these, and may have enough
344 * flags of its own that these additional ones prevent conversion. */
345 const struct {
346 const char *name;
347 const char *helptxt;
348 } new_flags_34[] = {
349 };
350
353
354 /* Unit type flags. */
356
357 for (i = 0; i < ARRAY_SIZE(new_flags_34); i++) {
359 /* Can't add the user unit type flags. */
360 ruleset_error(nullptr, LOG_ERROR,
361 "Can't upgrade the ruleset. Not enough free unit type "
362 "user flags to add user flags for the unit type flags "
363 "that used to be hardcoded.");
364 return FALSE;
365 }
366 /* Shouldn't be possible for valid old ruleset to have flag names that
367 * clash with these ones */
370 ruleset_error(nullptr, LOG_ERROR,
371 "Ruleset had illegal user unit type flag '%s'",
373 return FALSE;
374 }
377 new_flags_34[i].helptxt);
378 }
379 }
380
381 if (info->version < RSFORMAT_3_4) {
382 int first_free;
383 int i;
384
385 /* Some tech flags moved to the ruleset between 3.3 and 3.4.
386 * Add them back as user flags.
387 * XXX: ruleset might not need all of these, and may have enough
388 * flags of its own that these additional ones prevent conversion. */
389 const struct {
390 const char *name;
391 const char *helptxt;
392 } new_flags_34[] = {
393 };
394
397
398 /* Tech flags. */
400
401 for (i = 0; i < ARRAY_SIZE(new_flags_34); i++) {
403 /* Can't add the user unit type flags. */
404 ruleset_error(nullptr, LOG_ERROR,
405 "Can't upgrade the ruleset. Not enough free tech "
406 "user flags to add user flags for the tech flags "
407 "that used to be hardcoded.");
408 return FALSE;
409 }
410 /* Shouldn't be possible for valid old ruleset to have flag names that
411 * clash with these ones */
414 ruleset_error(nullptr, LOG_ERROR,
415 "Ruleset had illegal user tech flag '%s'",
417 return FALSE;
418 }
421 new_flags_34[i].helptxt);
422 }
423 }
424
425 /* No errors encountered. */
426 return TRUE;
427}
428
429/**********************************************************************/
432static bool effect_list_compat_cb(struct effect *peffect, void *data)
433{
434 struct rscompat_info *info = (struct rscompat_info *)data;
435
436 if (info->version < RSFORMAT_3_4) {
437 if (peffect->type == EFT_UPKEEP_PCT) {
438
439 /* From old Upkeep_Factor to new Upkeep_Pct */
440 peffect->value *= 100;
441
443 bool gold_included = TRUE;
444 bool only_gold = FALSE;
445
447 if (preq->source.kind == VUT_OTYPE) {
448 if ((preq->source.value.outputtype != O_GOLD
449 && preq->present)
450 || (preq->source.value.outputtype == O_GOLD
451 && !preq->present)) {
453 } else if (preq->source.value.outputtype == O_GOLD) {
454 only_gold = TRUE;
455 }
456 }
458
459 if (gold_included) {
460 if (!only_gold) {
461 struct effect *copy = effect_copy(peffect, EFT_UPKEEP_PCT);
462
463 /* Split to gold-only and not-gold effects.
464 * Make sure the gold-only is the one we currently handle
465 * (and not the one we add), so we don't encounter it again
466 * when iterating forward. */
468 req_from_str("OutputType", "Local",
469 FALSE, TRUE, FALSE,
470 "Gold"));
472 req_from_str("OutputType", "Local",
473 FALSE, FALSE, FALSE,
474 "Gold"));
475 }
476
478 req_from_str("UnitState", "Local",
479 FALSE, TRUE, FALSE,
480 "HasHomeCity"));
481 }
482 }
483 }
484 }
485
486 /* Go to the next effect. */
487 return TRUE;
488}
489
490/**********************************************************************/
494{
495 struct action_enabler *enabler;
496
497 if (!info->compat_mode || info->version >= RSFORMAT_CURRENT) {
498 /* There isn't anything here that should be done outside of compat
499 * mode. */
500 return;
501 }
502
504 enabler->action = ACTION_FINISH_UNIT;
506
510
511 /* Upgrade existing effects. Done before new effects are added to prevent
512 * the new effects from being upgraded by accident. */
514
515 /* Make sure that all action enablers added or modified by the
516 * compatibility post processing fulfills all hard action requirements. */
518
519 /* The ruleset may need adjustments it didn't need before compatibility
520 * post processing.
521 *
522 * If this isn't done a user of ruleset compatibility that ends up using
523 * the rules risks bad rules. A user that saves the ruleset rather than
524 * using it risks an unexpected change on the next load and save. */
526}
527
528/**********************************************************************/
533{
534 int flag;
535
536 /* Find the first unused user defined unit type flag. */
537 for (flag = 0; flag < MAX_NUM_USER_UNIT_FLAGS; flag++) {
539 return flag;
540 }
541 }
542
543 /* All unit type user flags are taken. */
545}
546
547/**********************************************************************/
552{
553 int flag;
554
555 /* Find the first unused user defined tech flag. */
556 for (flag = 0; flag < MAX_NUM_USER_TECH_FLAGS; flag++) {
557 if (tech_flag_id_name_cb(flag + TECH_USER_1) == nullptr) {
558 return flag;
559 }
560 }
561
562 /* All tech user flags are taken. */
564}
565
566/**********************************************************************/
569const char *rscompat_effect_name_3_4(const char *old_name)
570{
571 if (!fc_strcasecmp("Upkeep_Factor", old_name)) {
572 return "Upkeep_Pct";
573 }
574
575 return old_name;
576}
struct req_vec_problem * action_enabler_suggest_repair(const struct action_enabler *enabler)
Definition actions.c:1918
const char * action_enabler_vector_by_number_name(req_vec_num_in_item vec)
Definition actions.c:2095
const char * action_rule_name(const struct action *action)
Definition actions.c:1237
void action_enabler_add(struct action_enabler *enabler)
Definition actions.c:1547
struct action_enabler * action_enabler_new(void)
Definition actions.c:1496
struct action_enabler * action_enabler_copy(const struct action_enabler *original)
Definition actions.c:1532
struct action_enabler_list * action_enablers_for_action(action_id action)
Definition actions.c:1580
struct requirement_vector * action_enabler_vector_by_number(const void *enabler, req_vec_num_in_item number)
Definition actions.c:2071
#define enabler_get_action(_enabler_)
Definition actions.h:186
#define action_enabler_list_iterate_end
Definition actions.h:194
#define action_iterate_end
Definition actions.h:218
#define action_enabler_list_iterate(action_enabler_list, aenabler)
Definition actions.h:192
#define action_iterate(_act_)
Definition actions.h:214
bool has_capabilities(const char *us, const char *them)
Definition capability.c:88
char * incite_cost
Definition comments.c:76
bool iterate_effect_cache(iec_cb cb, void *data)
Definition effects.c:1321
struct effect * effect_copy(struct effect *old, enum effect_type override_type)
Definition effects.c:246
@ O_GOLD
Definition fc_types.h:102
struct civ_game game
Definition game.c:62
const char * name
Definition inputfile.c:127
#define log_warn(message,...)
Definition log.h:106
#define fc_assert_ret_val(condition, val)
Definition log.h:195
#define log_fatal(message,...)
Definition log.h:101
#define log_normal(message,...)
Definition log.h:108
@ LOG_ERROR
Definition log.h:31
#define log_error(message,...)
Definition log.h:104
#define FC_FREE(ptr)
Definition mem.h:41
const char * secfile_error(void)
bool secfile_lookup_int(const struct section_file *secfile, int *ival, const char *path,...)
const char * secfile_lookup_str(const struct section_file *secfile, const char *path,...)
bool req_vec_change_apply(const struct req_vec_change *modification, requirement_vector_by_number getter, const void *parent_item)
const char * req_vec_change_translation(const struct req_vec_change *change, const requirement_vector_namer namer)
struct requirement req_from_str(const char *type, const char *range, bool survives, bool present, bool quiet, const char *value)
void req_vec_problem_free(struct req_vec_problem *issue)
#define requirement_vector_iterate_end
#define requirement_vector_iterate(req_vec, preq)
static bool rscompat_enabler_add_obligatory_hard_reqs(struct action_enabler *ae)
Definition rscompat.c:170
bool rscompat_names(struct rscompat_info *info)
Definition rscompat.c:335
#define TECH_LAST_USER_FLAG_3_3
Definition rscompat.c:53
#define UTYF_LAST_USER_FLAG_3_3
Definition rscompat.c:52
void rscompat_postprocess(struct rscompat_info *info)
Definition rscompat.c:493
const char * rscompat_effect_name_3_4(const char *old_name)
Definition rscompat.c:569
int rscompat_check_capabilities(struct section_file *file, const char *filename, const struct rscompat_info *info)
Definition rscompat.c:70
void rscompat_enablers_add_obligatory_hard_reqs(void)
Definition rscompat.c:290
static int first_free_tech_user_flag(void)
Definition rscompat.c:551
static bool effect_list_compat_cb(struct effect *peffect, void *data)
Definition rscompat.c:432
static int first_free_unit_type_user_flag(void)
Definition rscompat.c:532
void rscompat_init_info(struct rscompat_info *info)
Definition rscompat.c:61
bool rscompat_check_cap_and_version(struct section_file *file, const char *filename, const struct rscompat_info *info)
Definition rscompat.c:135
#define enough_new_user_flags(_new_flags_, _name_, _LAST_USER_FLAG_, _LAST_USER_FLAG_PREV_)
Definition rscompat.c:46
#define RULESET_COMPAT_CAP
Definition rscompat.h:27
bool autoadjust_ruleset_data(void)
Definition rssanity.c:1620
#define ruleset_error(logger, level, format,...)
Definition ruleload.h:58
#define RULESET_CAPABILITIES
Definition ruleload.h:24
#define RSFORMAT_CURRENT
Definition ruleload.h:37
#define RSFORMAT_3_4
Definition ruleload.h:36
#define ARRAY_SIZE(x)
Definition shared.h:85
struct civ_game::@32::@36 server
bool homeless_gold_upkeep
Definition game.h:131
struct civ_game::@32::@36::@37 deprecated
struct requirement_vector reqs
Definition effects.h:53
bool compat_mode
Definition rscompat.h:31
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
const char * tech_flag_id_name_cb(enum tech_flag_id flag)
Definition tech.c:433
void set_user_tech_flag_name(enum tech_flag_id id, const char *name, const char *helptxt)
Definition tech.c:404
#define MAX_NUM_USER_TECH_FLAGS
Definition tech.h:105
#define TECH_USER_LAST
Definition tech.h:103
const char * unit_type_flag_id_name_cb(enum unit_type_flag_id flag)
Definition unittype.c:1931
void set_user_unit_type_flag_name(enum unit_type_flag_id id, const char *name, const char *helptxt)
Definition unittype.c:1902
#define MAX_NUM_USER_UNIT_FLAGS
Definition unittype.h:336
#define UTYF_LAST_USER_FLAG
Definition unittype.h:335