From 53aabd970f8044e35a5ef21ac96ed4a65a9a3be9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awomir=20Lach?= Date: Sun, 17 Apr 2022 10:34:26 +0200 Subject: [PATCH 2/3] =?UTF-8?q?OSDN!41121=20S=C5=82awomir=20Lach=20=20-=20Add=20routines=20to=20load=20counters=20fro?= =?UTF-8?q?m=20ruleset=20Basic=20routines=20to=20load=20counter=20definiti?= =?UTF-8?q?ons=20from=20ruleset=20are=20added=20by=20this=20patch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/ruleset.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/server/ruleset.c b/server/ruleset.c index 49101a2d7a..8877494f23 100644 --- a/server/ruleset.c +++ b/server/ruleset.c @@ -38,6 +38,7 @@ #include "base.h" #include "capability.h" #include "city.h" +#include "counters.h" #include "effects.h" #include "extras.h" #include "fc_types.h" @@ -108,6 +109,7 @@ #define ACHIEVEMENT_SECTION_PREFIX "achievement_" #define ACTION_ENABLER_SECTION_PREFIX "actionenabler_" #define MULTIPLIER_SECTION_PREFIX "multiplier_" +#define COUNTER_SECTION_PREFIX "counter_" #define check_name(name) (check_strlen(name, MAX_LEN_NAME, NULL)) #define check_cityname(name) (check_strlen(name, MAX_LEN_CITYNAME, NULL)) @@ -1406,6 +1408,41 @@ static bool load_game_names(struct section_file *file, section_list_destroy(sec); } + if (ok) { + + sec = secfile_sections_by_name_prefix(file, COUNTER_SECTION_PREFIX); + + nval = (NULL != sec ? section_list_size(sec) : 0); + if (nval > MAX_COUNTERS) { + int num = nval; /* No "size_t" to printf */ + + ruleset_error(LOG_ERROR, + "\"%s\": Too many counters (%d, max %d)", + filename, num, MAX_GOODS_TYPES); + section_list_destroy(sec); + ok = FALSE; + } else { + } + + if (ok) { + int count_idx; + + for (count_idx = 0; count_idx < nval; ++count_idx) { + struct counter *pcount = counter_by_id(count_idx); + const char *sec_name + = section_name(section_list_get(sec, counter_index(pcount))); + + if (!ruleset_load_names(&pcount->name, NULL, file, sec_name)) { + ruleset_error(LOG_ERROR, "\"%s\": Cannot load counters names", + filename); + ok = FALSE; + break; + } + } + } + section_list_destroy(sec); + } + return ok; } @@ -7544,6 +7581,63 @@ static bool load_ruleset_game(struct section_file *file, bool act, section_list_destroy(sec); } + if (ok) { + sec = secfile_sections_by_name_prefix(file, COUNTER_SECTION_PREFIX); + + if (sec != NULL) { + int num = section_list_size(sec); + int curr_; + + for (curr_ = 0; curr_ < num; ++curr_) { + + struct counter *pcount = counter_by_id(curr_); + const char *sec_name = section_name(section_list_get(sec, curr_)); + const char *counter_type = secfile_lookup_str_default(file, NULL, + "%s.type", + sec_name); + const char *counter_target = secfile_lookup_str_default(file, NULL, + "%s.range", + sec_name); + + enum counter_behaviour cb = counter_behaviour_by_name(counter_type, + fc_strcasecmp); + if (!counter_behaviour_is_valid(cb)) { + ruleset_error(LOG_ERROR, "\"%s\" unknown counter type \"%s\".", + filename, counter_type); + ok = FALSE; + break; + } + + enum counter_target tg = counter_target_by_name(counter_target, + fc_strcasecmp); + if (!counter_target_is_valid(tg)) { + ruleset_error(LOG_ERROR, "\"%s\" unknown counter range \"%s\".", + filename, counter_target); + ok = FALSE; + break; + } + + if (!ruleset_load_names(&pcount->name, NULL, file, sec_name)) { + ruleset_error(LOG_ERROR, "\"%s\": Cannot load counter names", + filename); + ok = FALSE; + break; + } + + pcount->type = cb; + pcount->checkpoint = secfile_lookup_int_default(file, 1, + "%s.checkpoint", + sec_name); + pcount->target = CTGT_CITY; + pcount->index = curr_; + pcount->def = secfile_lookup_int_default(file, 0, + "%s.def", + sec_name); + attach_city_counter(pcount); + } + } + } + /* secfile_check_unused() is not here, but only after also settings section * has been loaded. */ -- 2.35.1