From 77f1662242f4d873ff92c0f2d8dcffd3bda2f50a Mon Sep 17 00:00:00 2001 From: Pranav Date: Sat, 16 Jul 2022 13:32:50 +0200 Subject: [PATCH] Inline macros and use const refs for governemnts --- client/helpdlg.cpp | 10 ++--- client/reqtree.cpp | 20 ++++------ client/tilespec.cpp | 5 ++- common/government.cpp | 19 ++++----- common/government.h | 17 --------- common/helpdata.cpp | 20 ++++------ server/advisors/advdata.cpp | 36 ++++++++---------- server/rssanity.cpp | 10 ++--- server/ruleset.cpp | 72 ++++++++++++++++------------------- server/savegame/savegame3.cpp | 10 ++--- server/techtools.cpp | 34 +++++++---------- tools/civmanual.cpp | 13 +++---- tools/ruledit/tab_gov.cpp | 34 +++++++---------- tools/ruledit/tab_misc.cpp | 6 +-- tools/ruledit/univ_value.cpp | 10 +++-- tools/ruledit/validity.cpp | 8 ++-- tools/ruleutil/rulesave.cpp | 60 ++++++++++++++--------------- 17 files changed, 161 insertions(+), 223 deletions(-) diff --git a/client/helpdlg.cpp b/client/helpdlg.cpp index 641ff3eceb..6a0e882a2d 100644 --- a/client/helpdlg.cpp +++ b/client/helpdlg.cpp @@ -1049,16 +1049,15 @@ void help_widget::set_topic_tech(const help_item *topic, const char *title) add_info_pixmap(spr); } - governments_iterate(pgov) - { - requirement_vector_iterate(&pgov->reqs, preq) + for (const auto &pgov : governments) { + requirement_vector_iterate(&pgov.reqs, preq) { if (VUT_ADVANCE == preq->source.kind && preq->source.value.advance == padvance) { tb = set_properties(this); str = _("Allows"); str = "" + str + " " - + link_me(government_name_translation(pgov), + + link_me(government_name_translation(&pgov), HELP_GOVERNMENT); tb->setText(str.trimmed()); connect(tb, &QLabel::linkActivated, this, @@ -1067,8 +1066,7 @@ void help_widget::set_topic_tech(const help_item *topic, const char *title) } } requirement_vector_iterate_end; - } - governments_iterate_end; + }; improvement_iterate(pimprove) { diff --git a/client/reqtree.cpp b/client/reqtree.cpp index 73b149c10e..c58a31aaa4 100644 --- a/client/reqtree.cpp +++ b/client/reqtree.cpp @@ -159,20 +159,18 @@ static void node_rectangle_minimum_size(struct tree_node *node, int *width, improvement_iterate_end; // governments - governments_iterate(gov) - { - requirement_vector_iterate(&(gov->reqs), preq) + for (const auto &gov : governments) { + requirement_vector_iterate(&gov.reqs, preq) { if (VUT_ADVANCE == preq->source.kind && advance_number(preq->source.value.advance) == node->tech) { - sprite = get_government_sprite(tileset, gov); + sprite = get_government_sprite(tileset, &gov); max_icon_height = MAX(max_icon_height, sprite->height()); icons_width_sum += sprite->width() + 2; } } requirement_vector_iterate_end; } - governments_iterate_end; } *height += max_icon_height; @@ -1096,21 +1094,20 @@ QList *draw_reqtree(struct reqtree *tree, } improvement_iterate_end; - governments_iterate(gov) - { - requirement_vector_iterate(&(gov->reqs), preq) + for (auto &gov : governments) { + requirement_vector_iterate(&gov.reqs, preq) { if (VUT_ADVANCE == preq->source.kind && advance_number(preq->source.value.advance) == node->tech) { - sprite = get_government_sprite(tileset, gov); + sprite = get_government_sprite(tileset, &gov); rttp = new req_tooltip_help(); rttp->rect = QRect(icon_startx, starty + text_h + 4 + (height - text_h - 4 - sprite->height()) / 2, sprite->width(), sprite->height()); - rttp->tgov = gov; + rttp->tgov = &gov; tt_help->append(rttp); p.drawPixmap(icon_startx, starty + text_h + 4 @@ -1121,8 +1118,7 @@ QList *draw_reqtree(struct reqtree *tree, } } requirement_vector_iterate_end; - } - governments_iterate_end; + } // government iterate - gov } } diff --git a/client/tilespec.cpp b/client/tilespec.cpp index cf816a1003..0f74d267cd 100644 --- a/client/tilespec.cpp +++ b/client/tilespec.cpp @@ -1078,8 +1078,9 @@ bool tilespec_reread(const char *new_tileset_name, tileset_setup_unit_type(tileset, punittype); } unit_type_iterate_end; - governments_iterate(gov) { tileset_setup_government(tileset, gov); } - governments_iterate_end; + for (auto &gov : governments) { + tileset_setup_government(tileset, &gov); + } extra_type_iterate(pextra) { tileset_setup_extra(tileset, pextra); } extra_type_iterate_end; for (auto &pnation : nations) { diff --git a/common/government.cpp b/common/government.cpp index 6e99780dd0..d45782b9dd 100644 --- a/common/government.cpp +++ b/common/government.cpp @@ -13,7 +13,6 @@ // utility #include "fcintl.h" -#include "iterator.h" #include "log.h" #include "shared.h" #include "support.h" @@ -33,13 +32,11 @@ std::vector governments; */ struct government *government_by_translated_name(const char *name) { - governments_iterate(gov) - { - if (0 == strcmp(government_name_translation(gov), name)) { - return gov; + for (auto &gov : governments) { + if (0 == strcmp(government_name_translation(&gov), name)) { + return &gov; } } - governments_iterate_end; return nullptr; } @@ -52,13 +49,11 @@ struct government *government_by_rule_name(const char *name) { const char *qname = Qn_(name); - governments_iterate(gov) - { - if (0 == fc_strcasecmp(government_rule_name(gov), qname)) { - return gov; + for (auto &gov : governments) { + if (0 == fc_strcasecmp(government_rule_name(&gov), qname)) { + return &gov; } - } - governments_iterate_end; + }; return nullptr; } diff --git a/common/government.h b/common/government.h index b50bb126fc..6f644fdcdc 100644 --- a/common/government.h +++ b/common/government.h @@ -93,21 +93,4 @@ bool can_change_to_government(struct player *pplayer, void governments_alloc(int num); void governments_free(); -// Iterate over government types. -#define governments_iterate(NAME_pgov) \ - for (auto &temp_gov : governments) { \ - government *NAME_pgov = &temp_gov; - -#define governments_iterate_end } - -#define governments_re_active_iterate(_p) \ - governments_iterate(_p) \ - { \ - if (!_p->ruledit_disabled) { - -#define governments_re_active_iterate_end \ - } \ - } \ - governments_iterate_end; - bool untargeted_revolution_allowed(); diff --git a/common/helpdata.cpp b/common/helpdata.cpp index ee7ed27515..a38db4e7cf 100644 --- a/common/helpdata.cpp +++ b/common/helpdata.cpp @@ -509,7 +509,7 @@ static bool insert_generated_text(char *outbuf, size_t outlen, the various cases where additional requirements apply. */ static void insert_allows_single(struct universal *psource, - struct requirement_vector *psubjreqs, + const requirement_vector *psubjreqs, const char *subjstr, const char *const *strs, char *buf, size_t bufsz, const char *prefix) @@ -597,8 +597,7 @@ static void insert_allows(struct universal *psource, char *buf, size_t bufsz, { buf[0] = '\0'; - governments_iterate(pgov) - { + for (const auto &pgov : governments) { static const char *const govstrs[] = { // TRANS: First %s is a government name. N_("?gov:Allows %s (with %s but no %s)."), @@ -610,11 +609,10 @@ static void insert_allows(struct universal *psource, char *buf, size_t bufsz, N_("?gov:Allows %s."), // TRANS: %s is a government name. N_("?gov:Prevents %s.")}; - insert_allows_single(psource, &pgov->reqs, - government_name_translation(pgov), govstrs, buf, + insert_allows_single(psource, &pgov.reqs, + government_name_translation(&pgov), govstrs, buf, bufsz, prefix); - } - governments_iterate_end; + } // governments iterate - gov improvement_iterate(pimprove) { @@ -869,16 +867,14 @@ void boot_help_texts(const nation_set *nations_to_show, specialist_type_iterate_end; break; case HELP_GOVERNMENT: - governments_iterate(gov) - { + for (const auto &gov : governments) { pitem = new_help_item(current_type); fc_snprintf(name, sizeof(name), "%*s%s", level, "", - government_name_translation(gov)); + government_name_translation(&gov)); pitem->topic = qstrdup(name); pitem->text = qstrdup(empty); category_nodes.append(pitem); - } - governments_iterate_end; + }; break; case HELP_IMPROVEMENT: improvement_iterate(pimprove) diff --git a/server/advisors/advdata.cpp b/server/advisors/advdata.cpp index a65c12c4b0..bbf31c9295 100644 --- a/server/advisors/advdata.cpp +++ b/server/advisors/advdata.cpp @@ -834,27 +834,26 @@ void adv_best_government(struct player *pplayer) if (adv->govt_reeval == 0) { const struct research *presearch = research_get(pplayer); - governments_iterate(gov) - { + for (auto &gov : governments) { adv_want val = 0; bool override = false; - if (gov == game.government_during_revolution) { + if (&gov == game.government_during_revolution) { continue; // pointless } - if (gov->ai.better - && can_change_to_government(pplayer, gov->ai.better)) { + if (gov.ai.better + && can_change_to_government(pplayer, gov.ai.better)) { continue; // we have better governments available } - CALL_PLR_AI_FUNC(gov_value, pplayer, pplayer, gov, &val, &override); + CALL_PLR_AI_FUNC(gov_value, pplayer, pplayer, &gov, &val, &override); if (!override) { int dist; adv_want bonus = 0; // in percentage int revolution_turns; - pplayer->government = gov; + pplayer->government = &gov; /* Ideally we should change tax rates here, but since * this is a rather big CPU operation, we'd rather not. */ check_player_max_rates(pplayer); @@ -879,7 +878,7 @@ void adv_best_government(struct player *pplayer) { struct action *paction = action_by_number(act); - if (!action_immune_government(gov, act)) { + if (!action_immune_government(&gov, act)) { /* This government doesn't provide immunity againt this * action. */ continue; @@ -991,7 +990,7 @@ void adv_best_government(struct player *pplayer) // FIXME: handle reqs other than technologies. dist = 0; - requirement_vector_iterate(&gov->reqs, preq) + requirement_vector_iterate(&gov.reqs, preq) { if (VUT_ADVANCE == preq->source.kind) { dist += MAX(1, research_goal_unknown_techs( @@ -1003,9 +1002,8 @@ void adv_best_government(struct player *pplayer) val = amortize(val, dist); } - adv->government_want[government_index(gov)] = val; // Save want + adv->government_want[government_index(&gov)] = val; // Save want } - governments_iterate_end; // Now reset our gov to it's real state. pplayer->government = current_gov; city_list_iterate(pplayer->cities, acity) @@ -1022,21 +1020,20 @@ void adv_best_government(struct player *pplayer) adv->govt_reeval--; // Figure out which government is the best for us this turn. - governments_iterate(gov) - { - int gi = government_index(gov); + for (auto &gov : governments) { + int gi = government_index(&gov); if (adv->government_want[gi] > best_val - && can_change_to_government(pplayer, gov)) { + && can_change_to_government(pplayer, &gov)) { best_val = adv->government_want[gi]; - adv->goal.revolution = gov; + adv->goal.revolution = &gov; } if (adv->government_want[gi] > adv->goal.govt.val) { - adv->goal.govt.gov = gov; + adv->goal.govt.gov = &gov; adv->goal.govt.val = adv->government_want[gi]; // FIXME: handle reqs other than technologies. adv->goal.govt.req = A_NONE; - requirement_vector_iterate(&gov->reqs, preq) + requirement_vector_iterate(&gov.reqs, preq) { if (VUT_ADVANCE == preq->source.kind) { adv->goal.govt.req = advance_number(preq->source.value.advance); @@ -1045,8 +1042,7 @@ void adv_best_government(struct player *pplayer) } requirement_vector_iterate_end; } - } - governments_iterate_end; + }; /* Goodness of the ideal gov is calculated relative to the goodness of the * best of the available ones. */ adv->goal.govt.val -= best_val; diff --git a/server/rssanity.cpp b/server/rssanity.cpp index 7c7b7d817f..69559f68aa 100644 --- a/server/rssanity.cpp +++ b/server/rssanity.cpp @@ -1005,16 +1005,14 @@ bool sanity_check_ruleset_data(bool ignore_retired) improvement_iterate_end; // Governments - governments_iterate(pgov) - { - if (!sanity_check_req_vec(&pgov->reqs, true, -1, - government_rule_name(pgov))) { + for (const auto &pgov : governments) { + if (!sanity_check_req_vec(&pgov.reqs, true, -1, + government_rule_name(&pgov))) { qCCritical(ruleset_category, "Governments have conflicting or invalid requirements!"); ok = false; } - } - governments_iterate_end; + }; // Specialists specialist_type_iterate(sp) diff --git a/server/ruleset.cpp b/server/ruleset.cpp index 4c9d29f21a..cc233607f8 100644 --- a/server/ruleset.cpp +++ b/server/ruleset.cpp @@ -3967,17 +3967,15 @@ static bool load_government_names(struct section_file *file, /* Government names are needed early so that get_government_by_name will * work. */ - governments_iterate(gov) - { + for (auto &gov : governments) { const char *sec_name = - section_name(section_list_get(sec, government_index(gov))); + section_name(section_list_get(sec, government_index(&gov))); - if (!ruleset_load_names(&gov->name, nullptr, file, sec_name)) { + if (!ruleset_load_names(&gov.name, nullptr, file, sec_name)) { ok = false; break; } - } - governments_iterate_end; + }; } section_list_destroy(sec); @@ -4040,12 +4038,11 @@ static bool load_ruleset_governments(struct section_file *file, government_number(game.government_during_revolution); // easy ones: - governments_iterate(g) - { - const int i = government_index(g); + for (auto &g : governments) { + const int i = government_index(&g); const char *sec_name = section_name(section_list_get(sec, i)); struct requirement_vector *reqs = lookup_req_list( - file, compat, sec_name, "reqs", government_rule_name(g)); + file, compat, sec_name, "reqs", government_rule_name(&g)); if (reqs == nullptr) { ok = false; @@ -4056,32 +4053,30 @@ static bool load_ruleset_governments(struct section_file *file, char entry[100]; fc_snprintf(entry, sizeof(entry), "%s.ai_better", sec_name); - g->ai.better = lookup_government(file, entry, filename, nullptr); - if (g->ai.better == nullptr) { + g.ai.better = lookup_government(file, entry, filename, nullptr); + if (g.ai.better == nullptr) { ok = false; break; } } else { - g->ai.better = nullptr; + g.ai.better = nullptr; } - requirement_vector_copy(&g->reqs, reqs); + requirement_vector_copy(&g.reqs, reqs); - sz_strlcpy(g->graphic_str, + sz_strlcpy(g.graphic_str, secfile_lookup_str(file, "%s.graphic", sec_name)); - sz_strlcpy(g->graphic_alt, + sz_strlcpy(g.graphic_alt, secfile_lookup_str(file, "%s.graphic_alt", sec_name)); - g->helptext = lookup_strvec(file, sec_name, "helptext"); - } - governments_iterate_end; + g.helptext = lookup_strvec(file, sec_name, "helptext"); + } // iterate governments - g } if (ok) { // titles - governments_iterate(g) - { + for (auto &g : governments) { const char *sec_name = - section_name(section_list_get(sec, government_index(g))); + section_name(section_list_get(sec, government_index(&g))); const char *male, *female; if (!(male = secfile_lookup_str(file, "%s.ruler_male_title", sec_name)) @@ -4090,21 +4085,20 @@ static bool load_ruleset_governments(struct section_file *file, qCCritical(ruleset_category, "Lack of default ruler titles for " "government \"%s\" (nb %d): %s", - government_rule_name(g), government_number(g), + government_rule_name(&g), government_number(&g), secfile_error()); ok = false; break; } else if (nullptr - == government_ruler_title_new(g, nullptr, male, female)) { + == government_ruler_title_new(&g, nullptr, male, female)) { qCCritical(ruleset_category, "Lack of default ruler titles for " "government \"%s\" (nb %d).", - government_rule_name(g), government_number(g)); + government_rule_name(&g), government_number(&g)); ok = false; break; } - } - governments_iterate_end; + } // iterate governments - g } section_list_destroy(sec); @@ -8159,31 +8153,30 @@ static void send_ruleset_governments(struct conn_list *dest) struct packet_ruleset_government_ruler_title title; int j; - governments_iterate(g) - { + for (auto &g : governments) { // send one packet_government - gov.id = government_number(g); + gov.id = government_number(&g); j = 0; - requirement_vector_iterate(&g->reqs, preq) { gov.reqs[j++] = *preq; } + requirement_vector_iterate(&g.reqs, preq) { gov.reqs[j++] = *preq; } requirement_vector_iterate_end; gov.reqs_count = j; - sz_strlcpy(gov.name, untranslated_name(&g->name)); - sz_strlcpy(gov.rule_name, rule_name_get(&g->name)); - sz_strlcpy(gov.graphic_str, g->graphic_str); - sz_strlcpy(gov.graphic_alt, g->graphic_alt); - packet_strvec_compute(gov.helptext, g->helptext); + sz_strlcpy(gov.name, untranslated_name(&g.name)); + sz_strlcpy(gov.rule_name, rule_name_get(&g.name)); + sz_strlcpy(gov.graphic_str, g.graphic_str); + sz_strlcpy(gov.graphic_alt, g.graphic_alt); + packet_strvec_compute(gov.helptext, g.helptext); lsend_packet_ruleset_government(dest, &gov); // Send one packet_government_ruler_title per ruler title. - for (auto *pruler_title : qAsConst(*government_ruler_titles(g))) { + for (auto *pruler_title : qAsConst(*government_ruler_titles(&g))) { { const struct nation_type *pnation = ruler_title_nation(pruler_title); - title.gov = government_number(g); + title.gov = government_number(&g); title.nation = pnation ? nation_index(pnation) : game.control.nation_count; sz_strlcpy(title.male_title, @@ -8193,8 +8186,7 @@ static void send_ruleset_governments(struct conn_list *dest) lsend_packet_ruleset_government_ruler_title(dest, &title); } } - } - governments_iterate_end; + } // iterate governments - g } /** diff --git a/server/savegame/savegame3.cpp b/server/savegame/savegame3.cpp index f6928954a0..0a89006285 100644 --- a/server/savegame/savegame3.cpp +++ b/server/savegame/savegame3.cpp @@ -2046,18 +2046,16 @@ static void sg_save_ruledata(struct savedata *saving) { int set_count = 0; - governments_iterate(pgov) - { + for (auto &pgov : governments) { char path[256]; fc_snprintf(path, sizeof(path), "ruledata.government%d", set_count++); - secfile_insert_str(saving->file, government_rule_name(pgov), "%s.name", + secfile_insert_str(saving->file, government_rule_name(&pgov), "%s.name", path); - secfile_insert_int(saving->file, pgov->changed_to_times, "%s.changes", + secfile_insert_int(saving->file, pgov.changed_to_times, "%s.changes", path); - } - governments_iterate_end; + }; } /** diff --git a/server/techtools.cpp b/server/techtools.cpp index 4d56c08544..a2fbd9b74b 100644 --- a/server/techtools.cpp +++ b/server/techtools.cpp @@ -380,22 +380,18 @@ void found_new_tech(struct research *presearch, Tech_type_id tech_found, had_embassies[i] = get_player_bonus(aplayer, EFT_HAVE_EMBASSIES); if (presearch != research_get(aplayer)) { - governments_iterate(pgov) - { - could_switch[i][government_index(pgov)] = false; + for (const auto &pgov : governments) { + could_switch[i][government_index(&pgov)] = false; } - governments_iterate_end; continue; } /* Memorize for the players sharing the research what government * they could switch on. */ - governments_iterate(pgov) - { - could_switch[i][government_index(pgov)] = - can_change_to_government(aplayer, pgov); + for (const auto &pgov : governments) { + could_switch[i][government_index(&pgov)] = + can_change_to_government(aplayer, &pgov); } - governments_iterate_end; } players_iterate_end; @@ -457,18 +453,16 @@ void found_new_tech(struct research *presearch, Tech_type_id tech_found, unit_list_refresh_vision(aplayer->units); // Notify a player about new governments available - governments_iterate(pgov) - { - if (!could_switch[i][government_index(pgov)] - && can_change_to_government(aplayer, pgov)) { + for (const auto &pgov : governments) { + if (!could_switch[i][government_index(&pgov)] + && can_change_to_government(aplayer, &pgov)) { notify_player(aplayer, nullptr, E_NEW_GOVERNMENT, ftc_server, _("Discovery of %s makes the government form %s" " available. You may want to start a revolution."), qUtf8Printable(advance_name), - government_name_translation(pgov)); + government_name_translation(&pgov)); } } - governments_iterate_end; } // For any player. @@ -782,13 +776,11 @@ pick_random_government(struct player *pplayer) struct government *picked = nullptr; int gov_nb = 0; - governments_iterate(pgov) - { - if (can_change_to_government(pplayer, pgov) && 0 == fc_rand(++gov_nb)) { - picked = pgov; + for (auto &pgov : governments) { + if (can_change_to_government(pplayer, &pgov) && 0 == fc_rand(++gov_nb)) { + picked = &pgov; } - } - governments_iterate_end; + }; fc_assert(nullptr != picked); return picked; } diff --git a/tools/civmanual.cpp b/tools/civmanual.cpp index cb3212d246..6538bc918d 100644 --- a/tools/civmanual.cpp +++ b/tools/civmanual.cpp @@ -543,19 +543,18 @@ static bool manual_command(struct tag_types *tag_info) fprintf(doc, _("%sFreeciv21 %s governments help (%s)%s\n\n"), tag_info->title_begin, VERSION_STRING, game.control.name, tag_info->title_end); - governments_iterate(pgov) - { + for (auto &pgov : governments) { char buf[64000]; - fprintf(doc, tag_info->item_begin, "gov", pgov->item_number); + fprintf(doc, tag_info->item_begin, "gov", pgov.item_number); fprintf(doc, "%s%s%s\n\n", tag_info->sect_title_begin, - government_name_translation(pgov), tag_info->sect_title_end); + government_name_translation(&pgov), + tag_info->sect_title_end); fprintf(doc, tag_info->subitem_begin, "helptext"); - helptext_government(buf, sizeof(buf), nullptr, nullptr, pgov); + helptext_government(buf, sizeof(buf), nullptr, nullptr, &pgov); fprintf(doc, "%s\n\n", buf); fprintf(doc, "%s", tag_info->subitem_end); fprintf(doc, "%s", tag_info->item_end); - } - governments_iterate_end; + }; break; case MANUAL_UNITS: diff --git a/tools/ruledit/tab_gov.cpp b/tools/ruledit/tab_gov.cpp index 56cc545a77..45bb5257ed 100644 --- a/tools/ruledit/tab_gov.cpp +++ b/tools/ruledit/tab_gov.cpp @@ -114,16 +114,14 @@ void tab_gov::refresh() { gov_list->clear(); - governments_iterate(pgov) - { - if (!pgov->ruledit_disabled) { - QListWidgetItem *item = - new QListWidgetItem(QString::fromUtf8(government_rule_name(pgov))); + for (auto &pgov : governments) { + if (!pgov.ruledit_disabled) { + QListWidgetItem *item = new QListWidgetItem( + QString::fromUtf8(government_rule_name(&pgov))); - gov_list->insertItem(government_index(pgov), item); + gov_list->insertItem(government_index(&pgov), item); } - } - governments_iterate_end; + }; } /** @@ -178,18 +176,16 @@ void tab_gov::name_given() QByteArray name_bytes; QByteArray rname_bytes; - governments_iterate(pgov) - { - if (pgov != selected && !pgov->ruledit_disabled) { + for (const auto &pgov : governments) { + if (&pgov != selected && !pgov.ruledit_disabled) { rname_bytes = rname->text().toUtf8(); - if (!strcmp(government_rule_name(pgov), rname_bytes.data())) { + if (!strcmp(government_rule_name(&pgov), rname_bytes.data())) { ui->display_msg(R__("A government with that rule name already " "exists!")); return; } } } - governments_iterate_end; if (same_name->isChecked()) { name->setText(rname->text()); @@ -245,18 +241,16 @@ void tab_gov::add_now() struct government *new_gov; // Try to reuse freed government slot - governments_iterate(pgov) - { - if (pgov->ruledit_disabled) { - if (initialize_new_gov(pgov)) { - pgov->ruledit_disabled = false; - update_gov_info(pgov); + for (auto &pgov : governments) { + if (pgov.ruledit_disabled) { + if (initialize_new_gov(&pgov)) { + pgov.ruledit_disabled = false; + update_gov_info(&pgov); refresh(); } return; } } - governments_iterate_end; // Try to add completely new government if (game.control.num_goods_types >= MAX_GOODS_TYPES) { diff --git a/tools/ruledit/tab_misc.cpp b/tools/ruledit/tab_misc.cpp index f4da3f9dfc..5dab010a93 100644 --- a/tools/ruledit/tab_misc.cpp +++ b/tools/ruledit/tab_misc.cpp @@ -306,9 +306,9 @@ void tab_misc::refresh_stats() specialist_type_re_active_iterate_end; stats->item(row++, 4)->setText(QString::number(count)); - count = 0; - governments_re_active_iterate(pgov) { count++; } - governments_re_active_iterate_end; + count = std::count_if(governments.begin(), governments.end(), + [](government &p) { return !p.ruledit_disabled; }); + stats->item(row++, 4)->setText(QString::number(count)); stats->item(row++, 4)->setText( diff --git a/tools/ruledit/univ_value.cpp b/tools/ruledit/univ_value.cpp index a4db62ef1e..73b70c0a75 100644 --- a/tools/ruledit/univ_value.cpp +++ b/tools/ruledit/univ_value.cpp @@ -241,11 +241,13 @@ void universal_kind_values(struct universal *univ, univ_kind_values_cb cb, advance_re_active_iterate_end; break; case VUT_GOVERNMENT: - governments_re_active_iterate(pgov) - { - cb(government_rule_name(pgov), univ->value.govern == pgov, data); + for (auto &pgov : governments) { + if (!pgov.ruledit_disabled) { + { + cb(government_rule_name(&pgov), univ->value.govern == &pgov, data); + } + } } - governments_re_active_iterate_end; break; case VUT_IMPROVEMENT: improvement_re_active_iterate(pimpr) diff --git a/tools/ruledit/validity.cpp b/tools/ruledit/validity.cpp index 7f0a2a768d..2022862923 100644 --- a/tools/ruledit/validity.cpp +++ b/tools/ruledit/validity.cpp @@ -81,14 +81,12 @@ static bool is_universal_needed(struct universal *uni, requirers_cb cb, } improvement_iterate_end; - governments_iterate(pgov) - { - if (universal_is_mentioned_by_requirements(&pgov->reqs, uni)) { - cb(government_rule_name(pgov), data); + for (auto &pgov : governments) { + if (universal_is_mentioned_by_requirements(&pgov.reqs, uni)) { + cb(government_rule_name(&pgov), data); needed = true; } } - governments_iterate_end; specialist_type_iterate(sp) { diff --git a/tools/ruleutil/rulesave.cpp b/tools/ruleutil/rulesave.cpp index a196250d70..3c425ada69 100644 --- a/tools/ruleutil/rulesave.cpp +++ b/tools/ruleutil/rulesave.cpp @@ -1478,41 +1478,43 @@ static bool save_governments_ruleset(const char *filename, const char *name) comment_govs(sfile); sect_idx = 0; - governments_re_active_iterate(pg) - { - char path[512]; + for (auto &pg : governments) { + if (!pg.ruledit_disabled) { + { + char path[512]; - fc_snprintf(path, sizeof(path), "government_%d", sect_idx++); + fc_snprintf(path, sizeof(path), "government_%d", sect_idx++); - save_name_translation(sfile, &(pg->name), path); + save_name_translation(sfile, &(pg.name), path); - secfile_insert_str(sfile, pg->graphic_str, "%s.graphic", path); - secfile_insert_str(sfile, pg->graphic_alt, "%s.graphic_alt", path); + secfile_insert_str(sfile, pg.graphic_str, "%s.graphic", path); + secfile_insert_str(sfile, pg.graphic_alt, "%s.graphic_alt", path); - save_reqs_vector(sfile, &(pg->reqs), path, "reqs"); + save_reqs_vector(sfile, &pg.reqs, path, "reqs"); - if (pg->ai.better != nullptr) { - save_gov_ref(sfile, pg->ai.better, path, "ai_better"); - } + if (pg.ai.better != nullptr) { + save_gov_ref(sfile, pg.ai.better, path, "ai_better"); + } - const auto prtitle = pg->ruler_titles->value(nullptr); - if (prtitle != nullptr) { - const char *title; + const auto prtitle = pg.ruler_titles->value(nullptr); + if (prtitle != nullptr) { + const char *title; - title = ruler_title_male_untranslated_name(prtitle); - if (title != nullptr) { - secfile_insert_str(sfile, title, "%s.ruler_male_title", path); - } + title = ruler_title_male_untranslated_name(prtitle); + if (title != nullptr) { + secfile_insert_str(sfile, title, "%s.ruler_male_title", path); + } - title = ruler_title_female_untranslated_name(prtitle); - if (title != nullptr) { - secfile_insert_str(sfile, title, "%s.ruler_female_title", path); + title = ruler_title_female_untranslated_name(prtitle); + if (title != nullptr) { + secfile_insert_str(sfile, title, "%s.ruler_female_title", path); + } + } + + save_strvec(sfile, pg.helptext, path, "helptext"); } } - - save_strvec(sfile, pg->helptext, path, "helptext"); - } - governments_re_active_iterate_end; + } // iterate governments - pg comment_policies(sfile); @@ -1677,12 +1679,11 @@ static bool save_nation(struct section_file *sfile, struct nation_type *pnat, } subsect_idx = 0; - governments_iterate(pgov) - { + for (auto &pgov : governments) { struct ruler_title *prtitle; - prtitle = pgov->ruler_titles->value(pnat); + prtitle = pgov.ruler_titles->value(pnat); if (prtitle) { - secfile_insert_str(sfile, government_rule_name(pgov), + secfile_insert_str(sfile, government_rule_name(&pgov), "%s.ruler_titles%d.government", path, subsect_idx); secfile_insert_str(sfile, ruler_title_male_untranslated_name(prtitle), "%s.ruler_titles%d.male_title", path, subsect_idx); @@ -1691,7 +1692,6 @@ static bool save_nation(struct section_file *sfile, struct nation_type *pnat, "%s.ruler_titles%d.female_title", path, subsect_idx++); } } - governments_iterate_end; secfile_insert_str(sfile, style_rule_name(pnat->style), "%s.style", path);