forked from longturn/freeciv21
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruleset.cpp
8865 lines (7696 loc) · 280 KB
/
ruleset.cpp
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*__ ___ ***************************************
/ \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
\_ \ / __/ contributors. This file is part of Freeciv21.
_\ \ / /__ Freeciv21 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 3 of the License,
| or (at your option) any later version.
_/ /\ You should have received a copy of the GNU
/o) (o/\ \_ General Public License along with Freeciv21.
\_____/ / If not, see https://www.gnu.org/licenses/.
\____/ ********************************************************/
#ifdef HAVE_CONFIG_H
#include <fc_config.h>
#endif
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
// utility
#include "bitvector.h"
#include "deprecations.h"
#include "fcintl.h"
#include "registry.h"
#include "shared.h"
#include "support.h"
// common
#include "achievements.h"
#include "actions.h"
#include "ai.h"
#include "base.h"
#include "capability.h"
#include "city.h"
#include "effects.h"
#include "extras.h"
#include "fc_types.h"
#include "featured_text.h"
#include "game.h"
#include "government.h"
#include "map.h"
#include "movement.h"
#include "multipliers.h"
#include "name_translation.h"
#include "nation.h"
#include "packets.h"
#include "player.h"
#include "requirements.h"
#include "rgbcolor.h"
#include "road.h"
#include "specialist.h"
#include "style.h"
#include "tech.h"
#include "traderoutes.h"
#include "unit.h"
#include "unittype.h"
// server
#include "citytools.h"
#include "notify.h"
#include "plrhand.h"
#include "rscompat.h"
#include "rssanity.h"
#include "settings.h"
#include "srv_main.h"
/* server/advisors */
#include "advruleset.h"
/* server/scripting */
#include "script_server.h"
#include "ruleset.h"
// RULESET_SUFFIX already used, no leading dot here
#define RULES_SUFFIX "ruleset"
#define SCRIPT_SUFFIX "lua"
#define ADVANCE_SECTION_PREFIX "advance_"
#define TECH_CLASS_SECTION_PREFIX "techclass_"
#define BUILDING_SECTION_PREFIX "building_"
#define CITYSTYLE_SECTION_PREFIX "citystyle_"
#define MUSICSTYLE_SECTION_PREFIX "musicstyle_"
#define EFFECT_SECTION_PREFIX "effect_"
#define GOVERNMENT_SECTION_PREFIX "government_"
#define NATION_SET_SECTION_PREFIX "nset" // without underscore?
#define NATION_GROUP_SECTION_PREFIX "ngroup" // without underscore?
#define NATION_SECTION_PREFIX "nation" // without underscore?
#define STYLE_SECTION_PREFIX "style_"
#define CLAUSE_SECTION_PREFIX "clause_"
#define EXTRA_SECTION_PREFIX "extra_"
#define BASE_SECTION_PREFIX "base_"
#define ROAD_SECTION_PREFIX "road_"
#define RESOURCE_SECTION_PREFIX "resource_"
#define GOODS_SECTION_PREFIX "goods_"
#define SPECIALIST_SECTION_PREFIX "specialist_"
#define TERRAIN_SECTION_PREFIX "terrain_"
#define UNIT_CLASS_SECTION_PREFIX "unitclass_"
#define UNIT_SECTION_PREFIX "unit_"
#define DISASTER_SECTION_PREFIX "disaster_"
#define ACHIEVEMENT_SECTION_PREFIX "achievement_"
#define ACTION_ENABLER_SECTION_PREFIX "actionenabler_"
#define MULTIPLIER_SECTION_PREFIX "multiplier_"
#define check_name(name) (check_strlen(name, MAX_LEN_NAME, NULL))
#define check_cityname(name) (check_strlen(name, MAX_LEN_CITYNAME, NULL))
// avoid re-reading files
static const char name_too_long[] = "Name \"%s\" too long; truncating.";
#define MAX_SECTION_LABEL 64
#define section_strlcpy(dst, src) \
(void) loud_strlcpy(dst, src, MAX_SECTION_LABEL, name_too_long)
static char (*resource_sections)[MAX_SECTION_LABEL] = NULL;
static char (*terrain_sections)[MAX_SECTION_LABEL] = NULL;
static char (*extra_sections)[MAX_SECTION_LABEL] = NULL;
static char (*base_sections)[MAX_SECTION_LABEL] = NULL;
static char (*road_sections)[MAX_SECTION_LABEL] = NULL;
static struct requirement_vector reqs_list;
static bool load_rulesetdir(const char *rsdir, bool compat_mode,
rs_conversion_logger logger, bool act,
bool buffer_script, bool load_luadata);
static struct section_file *openload_ruleset_file(const char *whichset,
const char *rsdir);
static bool load_game_names(struct section_file *file,
struct rscompat_info *compat);
static bool load_tech_names(struct section_file *file,
struct rscompat_info *compat);
static bool load_unit_names(struct section_file *file,
struct rscompat_info *compat);
static bool load_building_names(struct section_file *file,
struct rscompat_info *compat);
static bool load_government_names(struct section_file *file,
struct rscompat_info *compat);
static bool load_terrain_names(struct section_file *file,
struct rscompat_info *compat);
static bool load_style_names(struct section_file *file,
struct rscompat_info *compat);
static bool load_nation_names(struct section_file *file,
struct rscompat_info *compat);
static bool
load_city_name_list(struct section_file *file, struct nation_type *pnation,
const char *secfile_str1, const char *secfile_str2,
const char **allowed_terrains, size_t atcount);
static bool load_ruleset_techs(struct section_file *file,
struct rscompat_info *compat);
static bool load_ruleset_units(struct section_file *file,
struct rscompat_info *compat);
static bool load_ruleset_buildings(struct section_file *file,
struct rscompat_info *compat);
static bool load_ruleset_governments(struct section_file *file,
struct rscompat_info *compat);
static bool load_ruleset_terrain(struct section_file *file,
struct rscompat_info *compat);
static bool load_ruleset_styles(struct section_file *file,
struct rscompat_info *compat);
static bool load_ruleset_cities(struct section_file *file,
struct rscompat_info *compat);
static bool load_ruleset_effects(struct section_file *file,
struct rscompat_info *compat);
static bool load_ruleset_game(struct section_file *file, bool act,
struct rscompat_info *compat);
static void send_ruleset_tech_classes(struct conn_list *dest);
static void send_ruleset_techs(struct conn_list *dest);
static void send_ruleset_unit_classes(struct conn_list *dest);
static void send_ruleset_units(struct conn_list *dest);
static void send_ruleset_buildings(struct conn_list *dest);
static void send_ruleset_terrain(struct conn_list *dest);
static void send_ruleset_resources(struct conn_list *dest);
static void send_ruleset_extras(struct conn_list *dest);
static void send_ruleset_bases(struct conn_list *dest);
static void send_ruleset_roads(struct conn_list *dest);
static void send_ruleset_goods(struct conn_list *dest);
static void send_ruleset_governments(struct conn_list *dest);
static void send_ruleset_styles(struct conn_list *dest);
static void send_ruleset_clauses(struct conn_list *dest);
static void send_ruleset_musics(struct conn_list *dest);
static void send_ruleset_cities(struct conn_list *dest);
static void send_ruleset_game(struct conn_list *dest);
static void send_ruleset_team_names(struct conn_list *dest);
static bool load_ruleset_veteran(struct section_file *file, const char *path,
struct veteran_system **vsystem, char *err,
size_t err_len, bool compat);
static int secfile_lookup_int_default_min_max(struct section_file *file,
int def, int min, int max,
const char *path, ...)
fc__attribute((__format__(__printf__, 5, 6)));
Q_LOGGING_CATEGORY(ruleset_category, "freeciv.ruleset")
char *script_buffer = NULL;
char *parser_buffer = NULL;
/**
datafilename() wrapper: tries to match in two ways.
Returns NULL on failure, the (statically allocated) filename on success.
*/
static QString valid_ruleset_filename(const char *subdir, const char *name,
const char *extension, bool optional)
{
char filename[512];
QString dfilename;
fc_assert_ret_val(subdir && name && extension, NULL);
fc_snprintf(filename, sizeof(filename), "%s/%s.%s", subdir, name,
extension);
qCDebug(ruleset_category, "Trying \"%s\".", filename);
dfilename = fileinfoname(get_data_dirs(), filename);
if (!dfilename.isEmpty()) {
return dfilename;
}
fc_snprintf(filename, sizeof(filename), "default/%s.%s", name, extension);
qCDebug(ruleset_category, "Trying \"%s\": default ruleset directory.",
filename);
dfilename = fileinfoname(get_data_dirs(), filename);
if (!dfilename.isEmpty()) {
return dfilename;
}
fc_snprintf(filename, sizeof(filename), "%s_%s.%s", subdir, name,
extension);
qCDebug(ruleset_category,
"Trying \"%s\": alternative ruleset filename syntax.", filename);
dfilename = fileinfoname(get_data_dirs(), filename);
if (!dfilename.isEmpty()) {
return dfilename;
} else if (!optional) {
qCCritical(ruleset_category,
// TRANS: message about an installation error.
_("Could not find a readable \"%s.%s\" ruleset file."), name,
extension);
}
return NULL;
}
/**
Return current script.lua buffer.
*/
char *get_script_buffer() { return script_buffer; }
/**
Return current parser.lua buffer.
*/
char *get_parser_buffer() { return parser_buffer; }
/**
Do initial section_file_load on a ruleset file.
"whichset" = "techs", "units", "buildings", "terrain", ...
*/
static struct section_file *openload_ruleset_file(const char *whichset,
const char *rsdir)
{
QString sfilename;
QString dfilename =
valid_ruleset_filename(rsdir, whichset, RULES_SUFFIX, false);
struct section_file *secfile;
if (dfilename.isEmpty()) {
return NULL;
}
/* Need to save a copy of the filename for following message, since
section_file_load() may call datafilename() for includes. */
sfilename = dfilename;
secfile = secfile_load(sfilename, false);
if (secfile == NULL) {
qCCritical(ruleset_category, "Could not load ruleset '%s':\n%s",
qUtf8Printable(sfilename), secfile_error());
}
return secfile;
}
/**
Parse script file.
*/
static enum fc_tristate openload_script_file(const char *whichset,
const char *rsdir,
char **buffer, bool optional)
{
QString dfilename =
valid_ruleset_filename(rsdir, whichset, SCRIPT_SUFFIX, optional);
if (dfilename.isEmpty()) {
return optional ? TRI_MAYBE : TRI_NO;
}
if (buffer == NULL) {
if (!script_server_do_file(NULL, qUtf8Printable(dfilename))) {
qCCritical(ruleset_category, "\"%s\": could not load ruleset script.",
qUtf8Printable(dfilename));
return TRI_NO;
}
} else {
script_server_load_file(qUtf8Printable(dfilename), buffer);
}
return TRI_YES;
}
/**
Load optional luadata.txt
*/
static struct section_file *openload_luadata_file(const char *rsdir)
{
struct section_file *secfile;
QString sfilename;
QString dfilename = valid_ruleset_filename(rsdir, "luadata", "txt", true);
if (dfilename.isEmpty()) {
return NULL;
}
/* Need to save a copy of the filename for following message, since
section_file_load() may call datafilename() for includes. */
sfilename = dfilename;
secfile = secfile_load(sfilename, false);
if (secfile == NULL) {
qCCritical(ruleset_category, "Could not load luadata '%s':\n%s",
qUtf8Printable(sfilename), secfile_error());
}
return secfile;
}
/**
Load a requirement list. The list is returned as a static vector
(callers need not worry about freeing anything).
*/
struct requirement_vector *lookup_req_list(struct section_file *file,
struct rscompat_info *compat,
const char *sec, const char *sub,
const char *rfor)
{
const char *type, *name;
int j;
const char *filename;
filename = secfile_name(file);
requirement_vector_reserve(&reqs_list, 0);
for (j = 0; (type = secfile_lookup_str_default(file, NULL, "%s.%s%d.type",
sec, sub, j));
j++) {
char buf[MAX_LEN_NAME];
const char *range;
bool survives, present, quiet;
struct entry *pentry;
struct requirement req;
if (!(pentry =
secfile_entry_lookup(file, "%s.%s%d.name", sec, sub, j))) {
qCCritical(ruleset_category, "%s", secfile_error());
return NULL;
}
name = NULL;
switch (entry_type_get(pentry)) {
case ENTRY_BOOL: {
bool val;
if (entry_bool_get(pentry, &val)) {
fc_snprintf(buf, sizeof(buf), "%d", val);
name = buf;
}
} break;
case ENTRY_INT: {
int val;
if (entry_int_get(pentry, &val)) {
fc_snprintf(buf, sizeof(buf), "%d", val);
name = buf;
}
} break;
case ENTRY_STR:
(void) entry_str_get(pentry, &name);
break;
case ENTRY_FLOAT:
fc_assert(entry_type_get(pentry) != ENTRY_FLOAT);
qCCritical(ruleset_category,
"\"%s\": trying to have an floating point entry as a "
"requirement name in '%s.%s%d'.",
filename, sec, sub, j);
break;
case ENTRY_FILEREFERENCE:
fc_assert(entry_type_get(pentry) != ENTRY_FILEREFERENCE);
break;
case ENTRY_ILLEGAL:
fc_assert(entry_type_get(pentry) != ENTRY_ILLEGAL);
break;
}
if (NULL == name) {
qCCritical(ruleset_category,
"\"%s\": error in handling requirement name for '%s.%s%d'.",
filename, sec, sub, j);
return NULL;
}
if (!(range = secfile_lookup_str(file, "%s.%s%d.range", sec, sub, j))) {
qCCritical(ruleset_category, "%s", secfile_error());
return NULL;
}
survives = false;
if ((pentry =
secfile_entry_lookup(file, "%s.%s%d.survives", sec, sub, j))
&& !entry_bool_get(pentry, &survives)) {
qCCritical(ruleset_category,
"\"%s\": invalid boolean value for survives for "
"'%s.%s%d'.",
filename, sec, sub, j);
}
present = true;
if ((pentry = secfile_entry_lookup(file, "%s.%s%d.present", sec, sub, j))
&& !entry_bool_get(pentry, &present)) {
qCCritical(ruleset_category,
"\"%s\": invalid boolean value for present for "
"'%s.%s%d'.",
filename, sec, sub, j);
}
quiet = false;
if ((pentry = secfile_entry_lookup(file, "%s.%s%d.quiet", sec, sub, j))
&& !entry_bool_get(pentry, &quiet)) {
qCCritical(ruleset_category,
"\"%s\": invalid boolean value for quiet for "
"'%s.%s%d'.",
filename, sec, sub, j);
}
if (compat->compat_mode) {
if (!fc_strcasecmp(type, universals_n_name(VUT_UTFLAG))) {
name = rscompat_utype_flag_name_3_1(compat, name);
}
}
if (compat->compat_mode) {
name = rscompat_req_name_3_1(type, name);
}
req = req_from_str(type, range, survives, present, quiet, name);
if (req.source.kind == universals_n_invalid()) {
qCCritical(ruleset_category,
"\"%s\" [%s] has invalid or unknown req: "
"\"%s\" \"%s\".",
filename, sec, type, name);
return NULL;
}
requirement_vector_append(&reqs_list, req);
}
if (j > MAX_NUM_REQS) {
qCCritical(ruleset_category,
"Too many (%d) requirements for %s. Max is %d", j, rfor,
MAX_NUM_REQS);
return NULL;
}
return &reqs_list;
}
/**
Load combat bonus list
*/
static bool lookup_cbonus_list(struct rscompat_info *compat,
struct combat_bonus_list *list,
struct section_file *file, const char *sec,
const char *sub)
{
const char *flag;
int j;
const char *filename;
bool success = true;
filename = secfile_name(file);
for (j = 0; (flag = secfile_lookup_str_default(file, NULL, "%s.%s%d.flag",
sec, sub, j));
j++) {
auto *bonus = new combat_bonus;
const char *type;
bonus->flag = unit_type_flag_id_by_name(
rscompat_utype_flag_name_3_1(compat, flag), fc_strcasecmp);
if (!unit_type_flag_id_is_valid(bonus->flag)) {
qCritical("\"%s\": unknown flag name \"%s\" in '%s.%s'.", filename,
flag, sec, sub);
FC_FREE(bonus);
success = false;
continue;
}
type = secfile_lookup_str(file, "%s.%s%d.type", sec, sub, j);
bonus->type = combat_bonus_type_by_name(type, fc_strcasecmp);
if (!combat_bonus_type_is_valid(bonus->type)) {
qCritical("\"%s\": unknown bonus type \"%s\" in '%s.%s'.", filename,
type, sec, sub);
FC_FREE(bonus);
success = false;
continue;
}
if (!secfile_lookup_int(file, &bonus->value, "%s.%s%d.value", sec, sub,
j)) {
qCritical("\"%s\": failed to get value from '%s.%s%d'.", filename, sec,
sub, j);
FC_FREE(bonus);
success = false;
continue;
}
bonus->quiet = secfile_lookup_bool_default(file, false, "%s.%s%d.quiet",
sec, sub, j);
combat_bonus_list_append(list, bonus);
}
return success;
}
/**
Lookup a string prefix.entry in the file and return the corresponding
advances pointer. If (!required), return A_NEVER for match "Never" or
can't match. If (required), die when can't match. Note the first tech
should have name "None" so that will always match.
If description is not NULL, it is used in the warning message
instead of prefix (eg pass unit->name instead of prefix="units2.u27")
*/
static bool lookup_tech(struct section_file *file, struct advance **result,
const char *prefix, const char *entry,
const char *filename, const char *description)
{
const char *sval;
sval = secfile_lookup_str_default(file, NULL, "%s.%s", prefix, entry);
if (!sval || !strcmp(sval, "Never")) {
*result = A_NEVER;
} else {
*result = advance_by_rule_name(sval);
if (A_NEVER == *result) {
qCCritical(ruleset_category, "\"%s\" %s %s: couldn't match \"%s\".",
filename, (description ? description : prefix), entry,
sval);
return false;
}
}
return true;
}
/**
Lookup a string prefix.entry in the file and return the corresponding
improvement pointer. Return B_NEVER for match "None" or
can't match.
If description is not NULL, it is used in the warning message
instead of prefix (eg pass unit->name instead of prefix="units2.u27")
*/
static bool lookup_building(struct section_file *file, const char *prefix,
const char *entry, struct impr_type **result,
const char *filename, const char *description)
{
const char *sval;
bool ok = true;
sval = secfile_lookup_str_default(file, NULL, "%s.%s", prefix, entry);
if (!sval || strcmp(sval, "None") == 0) {
*result = B_NEVER;
} else {
*result = improvement_by_rule_name(sval);
if (B_NEVER == *result) {
qCCritical(ruleset_category, "\"%s\" %s %s: couldn't match \"%s\".",
filename, (description ? description : prefix), entry,
sval);
ok = false;
}
}
return ok;
}
/**
Lookup a prefix.entry string vector in the file and fill in the
array, which should hold MAX_NUM_UNIT_LIST items. The output array is
either NULL terminated or full (contains MAX_NUM_UNIT_LIST
items). If the vector is not found and the required parameter is set,
we report it as an error, otherwise we just punt.
*/
static bool lookup_unit_list(struct section_file *file, const char *prefix,
const char *entry, struct unit_type **output,
const char *filename)
{
const char **slist;
size_t nval;
int i;
bool ok = true;
// pre-fill with NULL:
for (i = 0; i < MAX_NUM_UNIT_LIST; i++) {
output[i] = NULL;
}
slist = secfile_lookup_str_vec(file, &nval, "%s.%s", prefix, entry);
if (nval == 0) {
// 'No vector' is considered same as empty vector
NFCPP_FREE(slist);
return true;
}
if (nval > MAX_NUM_UNIT_LIST) {
qCCritical(ruleset_category,
"\"%s\": string vector %s.%s too long (%d, max %d)", filename,
prefix, entry, (int) nval, MAX_NUM_UNIT_LIST);
ok = false;
} else if (nval == 1 && strcmp(slist[0], "") == 0) {
delete[] slist;
return true;
}
if (ok) {
for (i = 0; i < nval; i++) {
const char *sval = slist[i];
struct unit_type *punittype = unit_type_by_rule_name(sval);
if (!punittype) {
qCCritical(ruleset_category,
"\"%s\" %s.%s (%d): couldn't match \"%s\".", filename,
prefix, entry, i, sval);
ok = false;
break;
}
output[i] = punittype;
log_debug("\"%s\" %s.%s (%d): %s (%d)", filename, prefix, entry, i,
sval, utype_number(punittype));
}
}
delete[] slist;
return ok;
}
/**
Lookup a prefix.entry string vector in the file and fill in the
array, which should hold MAX_NUM_TECH_LIST items. The output array is
either A_LAST terminated or full (contains MAX_NUM_TECH_LIST
items). All valid entries of the output array are guaranteed to
exist.
*/
static bool lookup_tech_list(struct section_file *file, const char *prefix,
const char *entry, int *output,
const char *filename)
{
const char **slist;
size_t nval;
int i;
bool ok = true;
// pre-fill with A_LAST:
for (i = 0; i < MAX_NUM_TECH_LIST; i++) {
output[i] = A_LAST;
}
slist = secfile_lookup_str_vec(file, &nval, "%s.%s", prefix, entry);
if (slist == NULL) {
return true;
} else if (nval == 0) {
FCPP_FREE(slist);
return true;
} else if (nval > MAX_NUM_TECH_LIST) {
qCCritical(ruleset_category,
"\"%s\": string vector %s.%s too long (%d, max %d)", filename,
prefix, entry, (int) nval, MAX_NUM_TECH_LIST);
ok = false;
}
if (ok) {
if (nval == 1 && strcmp(slist[0], "") == 0) {
FCPP_FREE(slist);
return true;
}
for (i = 0; i < nval && ok; i++) {
const char *sval = slist[i];
struct advance *padvance = advance_by_rule_name(sval);
if (NULL == padvance) {
qCCritical(ruleset_category,
"\"%s\" %s.%s (%d): couldn't match \"%s\".", filename,
prefix, entry, i, sval);
ok = false;
}
if (!valid_advance(padvance)) {
qCCritical(ruleset_category, "\"%s\" %s.%s (%d): \"%s\" is removed.",
filename, prefix, entry, i, sval);
ok = false;
}
if (ok) {
output[i] = advance_number(padvance);
log_debug("\"%s\" %s.%s (%d): %s (%d)", filename, prefix, entry, i,
sval, advance_number(padvance));
}
}
}
FCPP_FREE(slist);
return ok;
}
/**
Lookup a prefix.entry string vector in the file and fill in the
array, which should hold MAX_NUM_BUILDING_LIST items. The output array is
either B_LAST terminated or full (contains MAX_NUM_BUILDING_LIST
items). [All valid entries of the output array are guaranteed to pass
improvement_exist()?]
*/
static bool lookup_building_list(struct section_file *file,
const char *prefix, const char *entry,
int *output, const char *filename)
{
const char **slist;
size_t nval;
int i;
bool ok = true;
// pre-fill with B_LAST:
for (i = 0; i < MAX_NUM_BUILDING_LIST; i++) {
output[i] = B_LAST;
}
slist = secfile_lookup_str_vec(file, &nval, "%s.%s", prefix, entry);
if (nval > MAX_NUM_BUILDING_LIST) {
qCCritical(ruleset_category,
"\"%s\": string vector %s.%s too long (%d, max %d)", filename,
prefix, entry, (int) nval, MAX_NUM_BUILDING_LIST);
ok = false;
} else if (nval == 0 || (nval == 1 && strcmp(slist[0], "") == 0)) {
if (slist != NULL) {
FCPP_FREE(slist);
}
return true;
}
if (ok) {
for (i = 0; i < nval; i++) {
const char *sval = slist[i];
struct impr_type *pimprove = improvement_by_rule_name(sval);
if (NULL == pimprove) {
qCCritical(ruleset_category,
"\"%s\" %s.%s (%d): couldn't match \"%s\".", filename,
prefix, entry, i, sval);
ok = false;
break;
}
output[i] = improvement_number(pimprove);
log_debug("%s.%s,%d %s %d", prefix, entry, i, sval, output[i]);
}
}
delete[] slist;
return ok;
}
/**
Lookup a string prefix.entry in the file and set result to the
corresponding unit_type.
If description is not NULL, it is used in the warning message
instead of prefix (eg pass unit->name instead of prefix="units2.u27")
*/
static bool lookup_unit_type(struct section_file *file, const char *prefix,
const char *entry,
const struct unit_type **result,
const char *filename, const char *description)
{
const char *sval;
sval = secfile_lookup_str_default(file, "None", "%s.%s", prefix, entry);
if (strcmp(sval, "None") == 0) {
*result = NULL;
} else {
*result = unit_type_by_rule_name(sval);
if (*result == NULL) {
qCCritical(ruleset_category, "\"%s\" %s %s: couldn't match \"%s\".",
filename, (description ? description : prefix), entry,
sval);
return false;
}
}
return true;
}
/**
Lookup entry in the file and return the corresponding government index.
filename is for error message.
*/
static struct government *lookup_government(struct section_file *file,
const char *entry,
const char *filename,
struct government *fallback)
{
const char *sval;
struct government *gov;
sval = secfile_lookup_str_default(file, NULL, "%s", entry);
if (!sval) {
gov = fallback;
} else {
gov = government_by_rule_name(sval);
}
if (!gov) {
qCCritical(ruleset_category, "\"%s\" %s: couldn't match \"%s\".",
filename, entry, sval);
}
return gov;
}
/**
Lookup optional string, returning allocated memory or NULL.
*/
static char *lookup_string(struct section_file *file, const char *prefix,
const char *suffix)
{
const char *sval = secfile_lookup_str(file, "%s.%s", prefix, suffix);
if (NULL != sval) {
char copy[strlen(sval) + 1];
qstrcpy(copy, sval);
remove_leading_trailing_spaces(copy);
if (strlen(copy) > 0) {
return fc_strdup(copy);
}
}
return NULL;
}
/**
Stores the string vector from a normal vector. If size == -1, it will
assume it is a NULL terminated vector.
*/
static void strvec_store(QVector<QString> *psv, const char *const *vec,
size_t size)
{
if (size == static_cast<size_t>(-1)) {
psv->clear();
for (; *vec; vec++) {
psv->append(*vec);
}
} else {
size_t i;
psv->resize(size);
for (i = 0; i < size; i++, vec++) {
psv->replace(i, *vec);
}
}
}
/**
Lookup optional string vector, returning allocated memory or NULL.
*/
static QVector<QString> *lookup_strvec(struct section_file *file,
const char *prefix,
const char *suffix)
{
size_t dim;
const char **vec =
secfile_lookup_str_vec(file, &dim, "%s.%s", prefix, suffix);
if (NULL != vec) {
QVector<QString> *dest = new QVector<QString>;
strvec_store(dest, vec, dim);
delete[] vec;
return dest;
}
return NULL;
}
/**
Look up the resource section name and return its pointer.
*/
static struct extra_type *
lookup_resource(const char *filename, const char *name, const char *jsection)
{
struct extra_type *pres;
pres = extra_type_by_rule_name(name);
if (pres == NULL) {
qCCritical(ruleset_category, "\"%s\" [%s] has unknown \"%s\".", filename,
jsection, name);
}
return pres;
}
/**
Look up the terrain by name and return its pointer.
filename is for error message.
*/
static bool lookup_terrain(struct section_file *file, const char *entry,
const char *filename, struct terrain *pthis,
struct terrain **result)
{
const int j = terrain_index(pthis);
const char *jsection = terrain_sections[j];
const char *name = secfile_lookup_str(file, "%s.%s", jsection, entry);
struct terrain *pterr;
if (NULL == name || *name == '\0' || (0 == strcmp(name, "none"))
|| (0 == strcmp(name, "no"))) {
*result = T_NONE;
return true;
}
if (0 == strcmp(name, "yes")) {
*result = pthis;
return true;
}
pterr = terrain_by_rule_name(name);
*result = pterr;
if (pterr == NULL) {
qCCritical(ruleset_category, "\"%s\" [%s] has unknown \"%s\".",
secfile_name(file), jsection, name);
return false;
}
return true;
}
/**
Look up a value comparable to activity_count (road_time, etc).
item_name describes the thing which has the time property, if non-NULL,
for any error message.
Returns FALSE if not found in secfile, but TRUE even if validation failed.
Sets *ok to FALSE if validation failed, leaves it alone otherwise.
*/
static bool lookup_time(const struct section_file *secfile, int *turns,
const char *sec_name, const char *property_name,
const char *filename, const char *item_name,
bool *ok)
{
// Assumes that PACKET_UNIT_INFO.activity_count in packets.def is UINT16
const int max_turns = 65535 / ACTIVITY_FACTOR;
if (!secfile_lookup_int(secfile, turns, "%s.%s", sec_name,
property_name)) {
return false;
}
if (*turns > max_turns) {
qCCritical(ruleset_category,
"\"%s\": \"%s\": \"%s\" value %d too large (max %d)",
filename, item_name ? item_name : sec_name, property_name,
*turns, max_turns);
*ok = false;
}
return true; // we found _something
}
/**
Load "name" and (optionally) "rule_name" into a struct name_translation.
*/
static bool ruleset_load_names(struct name_translation *pname,
const char *domain, struct section_file *file,
const char *sec_name)
{
const char *name = secfile_lookup_str(file, "%s.name", sec_name);
const char *rule_name = secfile_lookup_str(file, "%s.rule_name", sec_name);
if (!name) {
qCCritical(ruleset_category, "\"%s\" [%s]: no \"name\" specified.",
secfile_name(file), sec_name);
return false;
}
names_set(pname, domain, name, rule_name);
return true;
}
/**