From 7f1d9508afb5c2fb768fe9f386187cb2132b81bc Mon Sep 17 00:00:00 2001 From: Tobias Rehbein Date: Sun, 5 Jan 2025 22:13:09 +0100 Subject: [PATCH] Unify code used to sell improvements from cities and economic view --- client/repodlgs_common.cpp | 50 +++++++++++++++++++++++++++++------- client/repodlgs_common.h | 4 +++ client/views/view_cities.cpp | 30 ++++++++++++---------- 3 files changed, 62 insertions(+), 22 deletions(-) diff --git a/client/repodlgs_common.cpp b/client/repodlgs_common.cpp index 296dcdebe7..34a521962c 100644 --- a/client/repodlgs_common.cpp +++ b/client/repodlgs_common.cpp @@ -13,7 +13,6 @@ // utility #include "fcintl.h" -#include "log.h" #include "support.h" // fc_snprintf() // common @@ -167,6 +166,34 @@ void get_economy_report_units_data(struct unit_entry *entries, void sell_all_improvements(const struct impr_type *pimprove, bool redundant_only, char *message, size_t message_sz) +{ + if (nullptr == client.conn.playing) { + return; + } + + QList cities; + city_list_iterate(client.conn.playing->cities, pcity) + { + cities.append(pcity); + } + city_list_iterate_end; + + sell_all_improvements_for_cities(cities, pimprove, redundant_only, message, + message_sz); +} + +/** + Sell all improvements of the given type in cities. If + "redundant_only" is specified then only those improvements that are replaced + will be sold. + + The "message" string will be filled with a GUI-friendly message about + what was sold. +*/ +void sell_all_improvements_for_cities(QList cities, + const struct impr_type *pimprove, + bool redundant_only, char *message, + size_t message_sz) { int count = 0, gold = 0; @@ -175,16 +202,21 @@ void sell_all_improvements(const struct impr_type *pimprove, return; } - city_list_iterate(client.conn.playing->cities, pcity) - { - if (!pcity->did_sell && city_has_building(pcity, pimprove) - && (!redundant_only || is_improvement_redundant(pcity, pimprove))) { - count++; - gold += impr_sell_gold(pimprove); - city_sell_improvement(pcity, improvement_number(pimprove)); + for (auto pcity : cities) { + int city_id = pcity->id; + if (nullptr == game_city_by_number(city_id)) { + continue; + } + + if (pcity->did_sell || !city_has_building(pcity, pimprove) + || (redundant_only && !is_improvement_redundant(pcity, pimprove))) { + continue; } + + count++; + gold += impr_sell_gold(pimprove); + city_sell_improvement(pcity, improvement_number(pimprove)); } - city_list_iterate_end; if (count > 0) { // FIXME: plurality of count is ignored! diff --git a/client/repodlgs_common.h b/client/repodlgs_common.h index edb54bb999..6ba8124809 100644 --- a/client/repodlgs_common.h +++ b/client/repodlgs_common.h @@ -48,6 +48,10 @@ void get_economy_report_units_data(struct unit_entry *entries, void sell_all_improvements(const struct impr_type *pimprove, bool redundant_only, char *message, size_t message_sz); +void sell_all_improvements_for_cities(QList cities, + const struct impr_type *pimprove, + bool redundant_only, char *message, + size_t message_sz); void disband_all_units(const struct unit_type *punittype, bool in_cities_only, char *message, size_t message_sz); diff --git a/client/views/view_cities.cpp b/client/views/view_cities.cpp index 045e8f44c6..2fa6849535 100644 --- a/client/views/view_cities.cpp +++ b/client/views/view_cities.cpp @@ -21,7 +21,6 @@ #include "fcintl.h" // common #include "citydlg_common.h" -#include "game.h" #include "global_worklist.h" // client #include "cityrep_g.h" @@ -32,6 +31,7 @@ #include "icons.h" #include "page_game.h" #include "qtg_cxxside.h" +#include "repodlgs_common.h" #include "top_bar.h" #include "views/view_cities.h" #include "views/view_map.h" @@ -720,24 +720,28 @@ void city_widget::sell(const struct impr_type *building) hud_message_box *ask = new hud_message_box(king()->central_wdg); QString imprname = improvement_name_translation(building); - QString buf = - QString(_("Are you sure you want to sell the %1?")).arg(imprname); + QString buf; + if (selected_cities.size() == 1) { + buf = QString(_("Are you sure you want to sell the %1?")).arg(imprname); + } else { + buf = QString(_("Do you really wish to sell every %1?")).arg(imprname); + } + ask->setStandardButtons(QMessageBox::Cancel | QMessageBox::Yes); ask->setDefaultButton(QMessageBox::No); ask->button(QMessageBox::Yes)->setText(_("Yes Sell")); ask->set_text_title(buf, _("Sell?")); ask->setAttribute(Qt::WA_DeleteOnClose); connect(ask, &hud_message_box::accepted, this, [=]() { - for (auto *pcity : selected_cities) { - int city_id = pcity->id; - if (nullptr == game_city_by_number(city_id)) { - continue; - } - - if (!pcity->did_sell && city_has_building(pcity, building)) { - city_sell_improvement(pcity, impr_id); - } - } + char buf[1024]; + sell_all_improvements_for_cities(selected_cities, building, false, buf, + sizeof(buf)); + + hud_message_box *result = new hud_message_box(king()->central_wdg); + result->set_text_title(buf, _("Sell Results")); + result->setStandardButtons(QMessageBox::Ok); + result->setAttribute(Qt::WA_DeleteOnClose); + result->show(); }); ask->show();