Skip to content

Commit

Permalink
Inline macros and use const refs for governemnts
Browse files Browse the repository at this point in the history
  • Loading branch information
psampathkumar authored and jwrober committed Jul 25, 2022
1 parent 2d534d5 commit 77f1662
Show file tree
Hide file tree
Showing 17 changed files with 161 additions and 223 deletions.
10 changes: 4 additions & 6 deletions client/helpdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<b>" + str + "</b> "
+ link_me(government_name_translation(pgov),
+ link_me(government_name_translation(&pgov),
HELP_GOVERNMENT);
tb->setText(str.trimmed());
connect(tb, &QLabel::linkActivated, this,
Expand All @@ -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)
{
Expand Down
20 changes: 8 additions & 12 deletions client/reqtree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1096,21 +1094,20 @@ QList<req_tooltip_help *> *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
Expand All @@ -1121,8 +1118,7 @@ QList<req_tooltip_help *> *draw_reqtree(struct reqtree *tree,
}
}
requirement_vector_iterate_end;
}
governments_iterate_end;
} // government iterate - gov
}
}

Expand Down
5 changes: 3 additions & 2 deletions client/tilespec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 7 additions & 12 deletions common/government.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

// utility
#include "fcintl.h"
#include "iterator.h"
#include "log.h"
#include "shared.h"
#include "support.h"
Expand All @@ -33,13 +32,11 @@ std::vector<government> 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;
}
Expand All @@ -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;
}
Expand Down
17 changes: 0 additions & 17 deletions common/government.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
20 changes: 8 additions & 12 deletions common/helpdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)."),
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down
36 changes: 16 additions & 20 deletions server/advisors/advdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand All @@ -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);
Expand All @@ -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;
Expand Down
10 changes: 4 additions & 6 deletions server/rssanity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 77f1662

Please sign in to comment.