From 1a04505fcd50d250eb547227b74b31c25e1ae8e8 Mon Sep 17 00:00:00 2001 From: Louis Moureaux Date: Thu, 4 Aug 2022 00:05:48 +0200 Subject: [PATCH] Use std::vector for the list of focused units I would like to have some decent container to use in #805 (if several units are selected, I want the right click menu to apply to all of them, which will require a container). Change focus_units (=get_units_in_focus()) to be a std::vector. This has implications in a lot of places, which are also modified to use std::vector. --- client/citydlg.cpp | 23 +-- client/client_main.cpp | 2 +- client/climisc.cpp | 15 +- client/climisc.h | 2 +- client/control.cpp | 299 +++++++++++++------------------------ client/control.h | 13 +- client/dialogs.cpp | 35 ++--- client/dialogs.h | 9 +- client/goto.cpp | 6 +- client/goto.h | 2 +- client/gotodlg.cpp | 12 +- client/hudwidget.cpp | 3 +- client/include/dialogs_g.h | 2 - client/include/mapview_g.h | 2 +- client/mapctrl_common.cpp | 8 +- client/mapview.cpp | 6 +- client/menu.cpp | 76 ++++------ client/packhand.cpp | 7 +- client/text.cpp | 26 ++-- client/text.h | 10 +- client/tilespec.cpp | 11 +- common/unitlist.cpp | 98 +++++------- common/unitlist.h | 34 +++-- 23 files changed, 256 insertions(+), 445 deletions(-) diff --git a/client/citydlg.cpp b/client/citydlg.cpp index 5297c7466f..2b54daeb79 100644 --- a/client/citydlg.cpp +++ b/client/citydlg.cpp @@ -656,9 +656,6 @@ void unit_list_item::create_menu() m_menu = new QMenu; - auto *units = unit_list_new(); - unit_list_append(units, m_unit); - auto *activate_and_close_action = new QAction(_("Activate unit"), this); connect(activate_and_close_action, &QAction::triggered, this, &unit_list_item::activate_and_close_dialog); @@ -693,20 +690,20 @@ void unit_list_item::create_menu() m_menu->addAction(change_homecity_action); } - if (units_can_load(units)) { + if (units_can_load({m_unit})) { auto *load_action = new QAction(_("Load"), this); connect(load_action, &QAction::triggered, this, &unit_list_item::load); m_menu->addAction(load_action); } - if (units_can_unload(units)) { + if (units_can_unload({m_unit})) { auto *unload_action = new QAction(_("Unload"), this); connect(unload_action, &QAction::triggered, this, &unit_list_item::unload); m_menu->addAction(unload_action); } - if (units_are_occupied(units)) { + if (units_are_occupied({m_unit})) { auto *unload_all_action = new QAction(_("Unload All From Transporter"), this); connect(unload_all_action, &QAction::triggered, this, @@ -714,14 +711,12 @@ void unit_list_item::create_menu() m_menu->addAction(unload_all_action); } - if (units_can_upgrade(units)) { + if (units_can_upgrade({m_unit})) { auto *upgrade_action = new QAction(_("Upgrade Unit"), this); connect(upgrade_action, &QAction::triggered, this, &unit_list_item::upgrade); m_menu->addAction(upgrade_action); } - - unit_list_destroy(units); } /** @@ -738,10 +733,7 @@ bool unit_list_item::can_issue_orders() const void unit_list_item::disband() { if (can_issue_orders()) { - auto *units = unit_list_new(); - unit_list_append(units, m_unit); - popup_disband_dialog(units); - unit_list_destroy(units); + popup_disband_dialog({m_unit}); } } @@ -781,10 +773,7 @@ void unit_list_item::unload_all() void unit_list_item::upgrade() { if (can_issue_orders()) { - auto *units = unit_list_new(); - unit_list_append(units, m_unit); - popup_upgrade_dialog(units); - unit_list_destroy(units); + popup_upgrade_dialog({m_unit}); } } diff --git a/client/client_main.cpp b/client/client_main.cpp index c9ee962f9a..6fc2480faa 100644 --- a/client/client_main.cpp +++ b/client/client_main.cpp @@ -916,7 +916,7 @@ void set_client_state(enum client_states newstate) update_info_label(); unit_focus_update(); - update_unit_info_label(nullptr); + update_unit_info_label({}); break; } diff --git a/client/climisc.cpp b/client/climisc.cpp index 878d08f81b..131377ef6f 100644 --- a/client/climisc.cpp +++ b/client/climisc.cpp @@ -17,6 +17,7 @@ ***********************************************************************/ #include +#include #include #include #include @@ -1054,19 +1055,13 @@ void cityrep_buy(struct city *pcity) /** Returns TRUE if any of the units can do the connect activity. */ -bool can_units_do_connect(struct unit_list *punits, +bool can_units_do_connect(const std::vector &units, enum unit_activity activity, struct extra_type *tgt) { - unit_list_iterate(punits, punit) - { - if (can_unit_do_connect(punit, activity, tgt)) { - return true; - } - } - unit_list_iterate_end; - - return false; + return std::any_of(units.begin(), units.end(), [&](const auto unit) { + return can_unit_do_connect(unit, activity, tgt); + }); } /** diff --git a/client/climisc.h b/client/climisc.h index 09c80a2cdd..66e3181707 100644 --- a/client/climisc.h +++ b/client/climisc.h @@ -95,7 +95,7 @@ struct city *get_nearest_city(const struct unit *punit, int *sq_dist); void cityrep_buy(struct city *pcity); -bool can_units_do_connect(struct unit_list *punits, +bool can_units_do_connect(const std::vector &units, enum unit_activity activity, struct extra_type *tgt); diff --git a/client/control.cpp b/client/control.cpp index 8f402c16fd..b73fd2ea6e 100644 --- a/client/control.cpp +++ b/client/control.cpp @@ -13,6 +13,7 @@ #include #include +#include // utility #include "bitvector.h" #include "fcintl.h" @@ -72,7 +73,7 @@ static int disband_unit_alternatives[3] = { int num_units_below = MAX_NUM_UNITS_BELOW; // current_focus points to the current unit(s) in focus -static struct unit_list *current_focus = nullptr; +static auto current_focus = std::vector(); /* The previously focused unit(s). Focus can generally be recalled * with keypad 5 (or the equivalent). */ @@ -135,7 +136,6 @@ void control_init() { int i; - current_focus = unit_list_new(); previous_focus = unit_list_new(); urgent_focus_queue = unit_list_new(); @@ -152,8 +152,7 @@ void control_free() { int i; - unit_list_destroy(current_focus); - current_focus = nullptr; + current_focus.clear(); unit_list_destroy(previous_focus); previous_focus = nullptr; unit_list_destroy(urgent_focus_queue); @@ -171,15 +170,12 @@ void control_free() /** Returns list of units currently in focus. */ -struct unit_list *get_units_in_focus() { return current_focus; } +std::vector &get_units_in_focus() { return current_focus; } /** Return the number of units currently in focus (0 or more). */ -int get_num_units_in_focus() -{ - return (nullptr != current_focus ? unit_list_size(current_focus) : 0); -} +int get_num_units_in_focus() { return current_focus.size(); } /** Store the focus unit(s). This is used so that we can return to the @@ -189,11 +185,9 @@ static void store_previous_focus() { if (get_num_units_in_focus() > 0) { unit_list_clear(previous_focus); - unit_list_iterate(get_units_in_focus(), punit) - { + for (auto punit : get_units_in_focus()) { unit_list_append(previous_focus, punit); } - unit_list_iterate_end; } } @@ -225,7 +219,8 @@ void control_unit_killed(struct unit *punit) goto_unit_killed(punit); - unit_list_remove(get_units_in_focus(), punit); + auto &focus = get_units_in_focus(); + focus.erase(std::find(focus.begin(), focus.end(), punit)); if (get_num_units_in_focus() < 1) { clear_hover_state(); } @@ -278,12 +273,13 @@ void unit_register_battlegroup(struct unit *punit) activity => The connect activity (ACTIVITY_IRRIGATE, etc.) order => The last order (ORDER_PERFORM_ACTION, ORDER_LAST, etc.) */ -void set_hover_state(struct unit_list *punits, enum cursor_hover_state state, +void set_hover_state(const std::vector &units, + enum cursor_hover_state state, enum unit_activity activity, struct extra_type *tgt, int last_tgt, int last_sub_tgt, action_id action, enum unit_orders order) { - fc_assert_ret((punits && unit_list_size(punits) > 0) + fc_assert_ret(!units.empty() || (state == HOVER_NONE || state == HOVER_DEBUG_TILE)); fc_assert_ret(state == HOVER_CONNECT || activity == ACTIVITY_LAST); fc_assert_ret((state == HOVER_GOTO || state == HOVER_GOTO_SEL_TGT) @@ -315,7 +311,7 @@ void set_hover_state(struct unit_list *punits, enum cursor_hover_state state, */ void clear_hover_state() { - set_hover_state(nullptr, HOVER_NONE, ACTIVITY_LAST, nullptr, NO_TARGET, + set_hover_state({}, HOVER_NONE, ACTIVITY_LAST, nullptr, NO_TARGET, NO_TARGET, ACTION_NONE, ORDER_LAST); } @@ -371,7 +367,8 @@ static void ask_server_for_actions(struct unit *punit) */ bool unit_is_in_focus(const struct unit *punit) { - return unit_list_search(get_units_in_focus(), punit) != nullptr; + const auto &focus = get_units_in_focus(); + return std::find(focus.begin(), focus.end(), punit) != focus.end(); } /** @@ -379,13 +376,11 @@ bool unit_is_in_focus(const struct unit *punit) */ struct unit *get_focus_unit_on_tile(const struct tile *ptile) { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { if (unit_tile(punit) == ptile) { return punit; } } - unit_list_iterate_end; return nullptr; } @@ -395,7 +390,8 @@ struct unit *get_focus_unit_on_tile(const struct tile *ptile) */ struct unit *head_of_units_in_focus() { - return unit_list_get(current_focus, 0); + const auto &list = get_units_in_focus(); + return list.empty() ? nullptr : list.front(); } /** @@ -433,7 +429,7 @@ void auto_center_on_focus_unit() */ static void current_focus_append(struct unit *punit) { - unit_list_append(current_focus, punit); + get_units_in_focus().push_back(punit); punit->client.focus_status = FOCUS_AVAIL; refresh_unit_mapcanvas(punit, unit_tile(punit), true, false); @@ -495,22 +491,18 @@ void unit_focus_set(struct unit *punit) } // Close the action selection dialog if the actor unit lose focus. - unit_list_iterate(current_focus, punit_old) - { + for (const auto punit_old : get_units_in_focus()) { if (action_selection_actor_unit() == punit_old->id) { action_selection_close(); } } - unit_list_iterate_end; /* Redraw the old focus unit (to fix blinking or remove the selection * circle). */ - unit_list_iterate(current_focus, punit_old) - { + for (const auto punit_old : get_units_in_focus()) { refresh_unit_mapcanvas(punit_old, unit_tile(punit_old), true, false); } - unit_list_iterate_end; - unit_list_clear(current_focus); + get_units_in_focus().clear(); if (!can_client_change_view()) { /* This function can be called to set the focus to nullptr when @@ -631,8 +623,7 @@ void unit_focus_advance() clear_hover_state(); - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { /* * Is the unit which just lost focus a non-AI unit? If yes this * enables the auto end turn. @@ -642,7 +633,6 @@ void unit_focus_advance() break; } } - unit_list_iterate_end; if (unit_list_size(urgent_focus_queue) > 0) { // Try top of the urgent list. @@ -736,8 +726,7 @@ void unit_focus_update() /* iterate zero times for no units in focus, * otherwise quit for any of the conditions. */ - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { if ((punit->activity == ACTIVITY_IDLE || punit->activity == ACTIVITY_GOTO || unit_has_orders(punit)) && punit->moves_left > 0 && !punit->done_moving @@ -745,7 +734,6 @@ void unit_focus_update() return; } } - unit_list_iterate_end; unit_focus_advance(); } @@ -786,13 +774,11 @@ unit *find_visible_unit(const ::tile *ptile) } // If the unit in focus is at this tile, show that on top - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { if (punit != punit_moving && unit_tile(punit) == ptile) { return punit; } } - unit_list_iterate_end; // If a city is here, return nothing (unit hidden by city). if (tile_city(ptile)) { @@ -844,14 +830,12 @@ int blink_active_unit() blink_timer.start(blink_time); - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { /* We flush to screen directly here. This is most likely faster * since these drawing operations are all small but may be spread * out widely. */ refresh_unit_mapcanvas(punit, unit_tile(punit), false, true); } - unit_list_iterate_end; } return blink_time - blink_timer.remainingTime(); @@ -924,20 +908,19 @@ int blink_turn_done_button() be enough information to know whether to redraw -- instead redraw every time. (Could store enough info to know, but is it worth it?) */ -void update_unit_pix_label(struct unit_list *punitlist) +void update_unit_pix_label(const std::vector &units) { int i; /* Check for any change in the unit's state. This assumes that a unit's * orders cannot be changed directly but must be removed and then reset. */ - if (punitlist && unit_list_size(punitlist) > 0 - && C_S_OVER != client_state()) { + if (!units.empty() && C_S_OVER != client_state()) { /* There used to be a complicated and bug-prone check here to see if * the unit had actually changed. This was misguided since the stacked * units (below) are redrawn in any case. Unless we write a general * system for unit updates here we might as well just redraw it every * time. */ - struct unit *punit = unit_list_get(punitlist, 0); + struct unit *punit = units.front(); set_unit_icon(-1, punit); @@ -1032,14 +1015,12 @@ void action_selection_next_in_focus(const int old_actor_id) old = game_unit_by_number(old_actor_id); // Go to the next unit in focus that needs a decision. - unit_list_iterate(get_units_in_focus(), funit) - { - if (old != funit && should_ask_server_for_actions(funit)) { - ask_server_for_actions(funit); + for (const auto punit : get_units_in_focus()) { + if (old != punit && should_ask_server_for_actions(punit)) { + ask_server_for_actions(punit); return; } } - unit_list_iterate_end; } /** @@ -1067,11 +1048,11 @@ void action_decision_request(struct unit *actor_unit) void request_unit_goto(enum unit_orders last_order, action_id act_id, int sub_tgt_id) { - struct unit_list *punits = get_units_in_focus(); + const auto focus = get_units_in_focus(); fc_assert_ret(act_id == ACTION_NONE || last_order == ORDER_PERFORM_ACTION); - if (unit_list_size(punits) == 0) { + if (focus.empty()) { return; } @@ -1079,8 +1060,7 @@ void request_unit_goto(enum unit_orders last_order, action_id act_id, // An action has been specified. fc_assert_ret(action_id_exists(act_id)); - unit_list_iterate(punits, punit) - { + for (const auto punit : focus) { if (!unit_can_do_action(punit, act_id)) { /* This unit can't perform the action specified in the last * order. */ @@ -1104,15 +1084,14 @@ void request_unit_goto(enum unit_orders last_order, action_id act_id, return; } } - unit_list_iterate_end; } if (hover_state != HOVER_GOTO && hover_state != HOVER_GOTO_SEL_TGT) { - set_hover_state(punits, HOVER_GOTO, ACTIVITY_LAST, nullptr, NO_TARGET, + set_hover_state(focus, HOVER_GOTO, ACTIVITY_LAST, nullptr, NO_TARGET, sub_tgt_id, act_id, last_order); - enter_goto_state(punits); + enter_goto_state(focus); create_line_at_mouse_pos(); - update_unit_info_label(punits); + update_unit_info_label(focus); control_mouse_cursor(nullptr); } else { fc_assert_ret(goto_is_active()); @@ -1125,18 +1104,12 @@ void request_unit_goto(enum unit_orders last_order, action_id act_id, /** Return TRUE if at least one of the units can do an attack at the tile. */ -static bool can_units_attack_at(struct unit_list *punits, +static bool can_units_attack_at(const std::vector &units, const struct tile *ptile) { - unit_list_iterate(punits, punit) - { - if (is_attack_unit(punit) && can_unit_attack_tile(punit, ptile)) { - return true; - } - } - unit_list_iterate_end; - - return false; + return std::any_of(units.begin(), units.end(), [&](const auto *unit) { + return is_attack_unit(unit) && can_unit_attack_tile(unit, ptile); + }); } /** @@ -1148,7 +1121,7 @@ void control_mouse_cursor(struct tile *ptile) { struct unit *punit = nullptr; struct city *pcity = nullptr; - struct unit_list *active_units = get_units_in_focus(); + const auto &active_units = get_units_in_focus(); enum cursor_type mouse_cursor_type = CURSOR_DEFAULT; if (!gui_options.enable_cursor_changes) { @@ -1418,9 +1391,9 @@ bool can_unit_do_connect(struct unit *punit, enum unit_activity activity, void request_unit_connect(enum unit_activity activity, struct extra_type *tgt) { - struct unit_list *punits = get_units_in_focus(); + const auto &focus = get_units_in_focus(); - if (!can_units_do_connect(punits, activity, tgt)) { + if (!can_units_do_connect(focus, activity, tgt)) { return; } @@ -1428,11 +1401,11 @@ void request_unit_connect(enum unit_activity activity, || (connect_tgt != tgt && (activity == ACTIVITY_GEN_ROAD || activity == ACTIVITY_IRRIGATE))) { - set_hover_state(punits, HOVER_CONNECT, activity, tgt, NO_TARGET, + set_hover_state(focus, HOVER_CONNECT, activity, tgt, NO_TARGET, NO_TARGET, ACTION_NONE, ORDER_LAST); - enter_goto_state(punits); + enter_goto_state(focus); create_line_at_mouse_pos(); - update_unit_info_label(punits); + update_unit_info_label(focus); control_mouse_cursor(nullptr); } else { fc_assert_ret(goto_is_active()); @@ -1487,8 +1460,7 @@ void request_unit_airlift(struct unit *punit, struct city *pcity) */ void request_units_return() { - unit_list_iterate(get_units_in_focus(), unit) - { + for (const auto unit : get_units_in_focus()) { // Find a path to the closest city auto finder = freeciv::path_finder(unit); if (auto path = finder.find_path( @@ -1529,7 +1501,6 @@ void request_units_return() } } } - unit_list_iterate_end; } /** @@ -1561,22 +1532,21 @@ void request_unit_wakeup(struct unit *punit) /** Select all units based on the given list of units and the selection modes. */ -void request_unit_select(struct unit_list *punits, +void request_unit_select(const std::vector &punits, enum unit_select_type_mode seltype, enum unit_select_location_mode selloc) { const struct player *pplayer; const struct tile *ptile; - struct unit *punit_first; QSet tile_table; QSet type_table; QSet cont_table; - if (!can_client_change_view() || !punits || unit_list_size(punits) < 1) { + if (!can_client_change_view() || punits.empty()) { return; } - punit_first = unit_list_get(punits, 0); + const auto punit_first = punits.front(); if (seltype == SELTYPE_SINGLE) { unit_focus_set(punit_first); @@ -1585,8 +1555,7 @@ void request_unit_select(struct unit_list *punits, pplayer = unit_owner(punit_first); - unit_list_iterate(punits, punit) - { + for (const auto punit : punits) { if (seltype == SELTYPE_SAME) { type_table.insert(unit_type_get(punit)); } @@ -1598,7 +1567,6 @@ void request_unit_select(struct unit_list *punits, cont_table.insert(tile_continent(ptile)); } } - unit_list_iterate_end; if (selloc == SELLOC_TILE) { for (const auto *hash_tile : tile_table) { @@ -2089,16 +2057,15 @@ void request_unit_caravan_action(struct unit *punit, action_id action) Have the player select what tile to paradrop to. Once selected a paradrop request will be sent to server. */ -void request_unit_paradrop(struct unit_list *punits) +void request_unit_paradrop(const std::vector &units) { bool can = false; struct tile *offender = nullptr; - if (unit_list_size(punits) == 0) { + if (units.empty()) { return; } - unit_list_iterate(punits, punit) - { + for (const auto punit : units) { if (can_unit_paradrop(punit)) { can = true; break; @@ -2107,16 +2074,14 @@ void request_unit_paradrop(struct unit_list *punits) offender = unit_tile(punit); } } - unit_list_iterate_end; if (can) { - create_event(unit_tile(unit_list_get(punits, 0)), E_BEGINNER_HELP, - ftc_client, + create_event(unit_tile(units.front()), E_BEGINNER_HELP, ftc_client, // TRANS: paradrop target tile. _("Click on a tile to paradrop to it.")); - set_hover_state(punits, HOVER_PARADROP, ACTIVITY_LAST, nullptr, - NO_TARGET, NO_TARGET, ACTION_NONE, ORDER_LAST); - update_unit_info_label(punits); + set_hover_state(units, HOVER_PARADROP, ACTIVITY_LAST, nullptr, NO_TARGET, + NO_TARGET, ACTION_NONE, ORDER_LAST); + update_unit_info_label(units); } else { create_event(offender, E_BAD_COMMAND, ftc_client, _("Only paratrooper units can do this.")); @@ -2128,17 +2093,17 @@ void request_unit_paradrop(struct unit_list *punits) */ void request_unit_patrol() { - struct unit_list *punits = get_units_in_focus(); + const auto &focus = get_units_in_focus(); - if (unit_list_size(punits) == 0) { + if (focus.empty()) { return; } if (hover_state != HOVER_PATROL) { - set_hover_state(punits, HOVER_PATROL, ACTIVITY_LAST, nullptr, NO_TARGET, + set_hover_state(focus, HOVER_PATROL, ACTIVITY_LAST, nullptr, NO_TARGET, NO_TARGET, ACTION_NONE, ORDER_LAST); - update_unit_info_label(punits); - enter_goto_state(punits); + update_unit_info_label(focus); + enter_goto_state(focus); create_line_at_mouse_pos(); } else { fc_assert_ret(goto_is_active()); @@ -2347,19 +2312,14 @@ void request_center_focus_unit() } /** - Set units in list to waiting focus. If they are current focus units, - advance focus. + Set units in list to waiting focus and advance focus. */ -void request_units_wait(struct unit_list *punits) +void request_units_wait(const std::vector &units) { - unit_list_iterate(punits, punit) - { + for (auto punit : units) { punit->client.focus_status = FOCUS_WAIT; } - unit_list_iterate_end; - if (punits == get_units_in_focus()) { - unit_focus_advance(); - } + unit_focus_advance(); } /** @@ -2369,21 +2329,17 @@ void request_unit_move_done() { if (get_num_units_in_focus() > 0) { enum unit_focus_status new_status = FOCUS_DONE; - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { /* If any of the focused units are busy, keep all of them * in focus; another tap of the key will dismiss them */ if (punit->activity != ACTIVITY_IDLE) { new_status = FOCUS_WAIT; } } - unit_list_iterate_end; - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { clear_unit_orders(punit); punit->client.focus_status = new_status; } - unit_list_iterate_end; if (new_status == FOCUS_DONE) { unit_focus_advance(); } @@ -2489,8 +2445,7 @@ void do_move_unit(struct unit *punit, struct unit *target_unit) */ static void do_unit_act_sel_vs(struct tile *ptile) { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { if (utype_may_act_at_all(unit_type_get(punit))) { /* Have the server record that an action decision is wanted for * this unit against this tile. */ @@ -2498,7 +2453,6 @@ static void do_unit_act_sel_vs(struct tile *ptile) tile_index(ptile)); } } - unit_list_iterate_end; } /** @@ -2507,7 +2461,7 @@ static void do_unit_act_sel_vs(struct tile *ptile) void do_map_click(struct tile *ptile, enum quickselect_type qtype) { struct city *pcity = tile_city(ptile); - struct unit_list *punits = get_units_in_focus(); + const auto &units = get_units_in_focus(); bool maybe_goto = false; if (hover_state != HOVER_NONE) { @@ -2518,8 +2472,9 @@ void do_map_click(struct tile *ptile, enum quickselect_type qtype) do_unit_goto(ptile); return; case HOVER_PARADROP: - unit_list_iterate(punits, punit) { do_unit_paradrop_to(punit, ptile); } - unit_list_iterate_end; + for (const auto punit : units) { + do_unit_paradrop_to(punit, ptile); + } break; case HOVER_CONNECT: do_unit_connect(ptile, connect_activity, connect_tgt); @@ -2804,13 +2759,11 @@ void do_unit_connect(struct tile *ptile, enum unit_activity activity, */ void key_cancel_action() { - struct unit_list *punits = get_units_in_focus(); - switch (hover_state) { case HOVER_GOTO_SEL_TGT: - set_hover_state(punits, HOVER_GOTO, connect_activity, connect_tgt, - goto_last_tgt, goto_last_sub_tgt, goto_last_action, - goto_last_order); + set_hover_state(get_units_in_focus(), HOVER_GOTO, connect_activity, + connect_tgt, goto_last_tgt, goto_last_sub_tgt, + goto_last_action, goto_last_order); break; case HOVER_GOTO: case HOVER_PATROL: @@ -2887,13 +2840,11 @@ void key_recall_previous_focus_unit() */ void key_unit_move(enum direction8 gui_dir) { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { enum direction8 map_dir = gui_to_map_dir(gui_dir); request_move_unit_direction(punit, map_dir); } - unit_list_iterate_end; } /** @@ -2916,8 +2867,7 @@ void key_unit_action_select() return; } - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { ptile = unit_tile(punit); if (utype_may_act_at_all(unit_type_get(punit)) && ptile) { /* Have the server record that an action decision is wanted for this @@ -2926,7 +2876,6 @@ void key_unit_action_select() tile_index(ptile)); } } - unit_list_iterate_end; } /** @@ -2938,7 +2887,7 @@ void key_unit_action_select() */ void key_unit_action_select_tgt() { - struct unit_list *punits = get_units_in_focus(); + const auto &focus = get_units_in_focus(); if (hover_state == HOVER_ACT_SEL_TGT) { /* The 2nd key press means that the actor should target its own @@ -2947,7 +2896,7 @@ void key_unit_action_select_tgt() // Target tile selected. Clean up hover state. clear_hover_state(); - update_unit_info_label(punits); + update_unit_info_label(focus); return; } else if (hover_state == HOVER_GOTO_SEL_TGT) { @@ -2959,33 +2908,31 @@ void key_unit_action_select_tgt() // Target tile selected. Clean up hover state. clear_hover_state(); - update_unit_info_label(punits); + update_unit_info_label(focus); return; } else if (hover_state == HOVER_GOTO && action_id_exists(goto_last_action)) { struct action *paction = action_by_number(goto_last_action); - create_event(unit_tile(unit_list_get(punits, 0)), E_BEGINNER_HELP, - ftc_client, + create_event(unit_tile(focus.front()), E_BEGINNER_HELP, ftc_client, // TRANS: Perform action inside a goto. _("Click on a tile to do %s against it."), qUtf8Printable(action_name_translation(paction))); - set_hover_state(punits, HOVER_GOTO_SEL_TGT, connect_activity, - connect_tgt, goto_last_tgt, goto_last_sub_tgt, - goto_last_action, goto_last_order); + set_hover_state(focus, HOVER_GOTO_SEL_TGT, connect_activity, connect_tgt, + goto_last_tgt, goto_last_sub_tgt, goto_last_action, + goto_last_order); return; } - create_event(unit_tile(unit_list_get(punits, 0)), E_BEGINNER_HELP, - ftc_client, + create_event(unit_tile(focus.front()), E_BEGINNER_HELP, ftc_client, // TRANS: "Do..." action selection dialog target. _("Click on a tile to act against it. " "Press 'd' again to act against own tile.")); - set_hover_state(punits, HOVER_ACT_SEL_TGT, ACTIVITY_LAST, nullptr, + set_hover_state(focus, HOVER_ACT_SEL_TGT, ACTIVITY_LAST, nullptr, NO_TARGET, NO_TARGET, ACTION_NONE, ORDER_LAST); } @@ -3016,25 +2963,21 @@ void key_unit_unload_all() { struct unit *pnext_focus = nullptr, *plast; - unit_list_iterate(get_units_in_focus(), punit) - { + for (auto punit : get_units_in_focus()) { plast = request_unit_unload_all(punit); if (plast) { pnext_focus = plast; } } - unit_list_iterate_end; if (pnext_focus) { - unit_list_iterate(get_units_in_focus(), punit) - { + for (auto punit : get_units_in_focus()) { /* Unfocus the ships, and advance the focus to the last unloaded unit. * If there is no unit unloaded (which shouldn't happen, but could if * the caller doesn't check if the transporter is loaded), the we * don't do anything. */ punit->client.focus_status = FOCUS_WAIT; } - unit_list_iterate_end; unit_focus_set(pnext_focus); } } @@ -3049,11 +2992,9 @@ void key_unit_wait() { request_units_wait(get_units_in_focus()); } */ void key_unit_wakeup_others() { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { request_unit_wakeup(punit); } - unit_list_iterate_end; } /** @@ -3061,8 +3002,7 @@ void key_unit_wakeup_others() */ void key_unit_airbase() { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { struct base_type *pbase = get_base_by_gui_type(BASE_GUI_AIRBASE, punit, unit_tile(punit)); @@ -3072,7 +3012,6 @@ void key_unit_airbase() request_new_unit_activity_targeted(punit, ACTIVITY_BASE, pextra); } } - unit_list_iterate_end; } /** @@ -3080,13 +3019,11 @@ void key_unit_airbase() */ void key_unit_auto_explore() { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { if (can_unit_do_activity(punit, ACTIVITY_EXPLORE)) { request_unit_ssa_set(punit, SSA_AUTOEXPLORE); } } - unit_list_iterate_end; } /** @@ -3095,13 +3032,11 @@ void key_unit_auto_explore() */ void key_unit_auto_settle() { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { if (can_unit_do_autosettlers(punit)) { request_unit_autosettlers(punit); } } - unit_list_iterate_end; } /** @@ -3109,11 +3044,9 @@ void key_unit_auto_settle() */ void key_unit_convert() { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { request_unit_convert(punit); } - unit_list_iterate_end; } /** @@ -3129,13 +3062,11 @@ void key_unit_fallout() */ void key_unit_fortify() { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { if (can_unit_do_activity(punit, ACTIVITY_FORTIFYING)) { request_new_unit_activity(punit, ACTIVITY_FORTIFYING); } } - unit_list_iterate_end; } /** @@ -3143,8 +3074,7 @@ void key_unit_fortify() */ void key_unit_fortress() { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { struct base_type *pbase = get_base_by_gui_type(BASE_GUI_FORTRESS, punit, unit_tile(punit)); @@ -3154,7 +3084,6 @@ void key_unit_fortress() request_new_unit_activity_targeted(punit, ACTIVITY_BASE, pextra); } } - unit_list_iterate_end; } /** @@ -3162,11 +3091,9 @@ void key_unit_fortress() */ void key_unit_homecity() { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { request_unit_change_homecity(punit); } - unit_list_iterate_end; } /** @@ -3174,8 +3101,7 @@ void key_unit_homecity() */ static void key_unit_extra(enum unit_activity act, enum extra_cause cause) { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { struct extra_type *tgt = next_extra_for_tile(unit_tile(punit), cause, unit_owner(punit), punit); @@ -3183,7 +3109,6 @@ static void key_unit_extra(enum unit_activity act, enum extra_cause cause) request_new_unit_activity_targeted(punit, act, tgt); } } - unit_list_iterate_end; } /** @@ -3192,8 +3117,7 @@ static void key_unit_extra(enum unit_activity act, enum extra_cause cause) static void key_unit_clean(enum unit_activity act, enum extra_rmcause rmcause) { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { struct extra_type *tgt = prev_extra_in_tile(unit_tile(punit), rmcause, unit_owner(punit), punit); @@ -3201,7 +3125,6 @@ static void key_unit_clean(enum unit_activity act, request_new_unit_activity_targeted(punit, act, tgt); } } - unit_list_iterate_end; } /** @@ -3217,13 +3140,11 @@ void key_unit_irrigate() */ void key_unit_cultivate() { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { if (can_unit_do_activity(punit, ACTIVITY_CULTIVATE)) { request_new_unit_activity(punit, ACTIVITY_CULTIVATE); } } - unit_list_iterate_end; } /** @@ -3236,13 +3157,11 @@ void key_unit_mine() { key_unit_extra(ACTIVITY_MINE, EC_MINE); } */ void key_unit_plant() { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { if (can_unit_do_activity(punit, ACTIVITY_PLANT)) { request_new_unit_activity(punit, ACTIVITY_PLANT); } } - unit_list_iterate_end; } /** @@ -3250,13 +3169,11 @@ void key_unit_plant() */ void key_unit_pillage() { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { if (can_unit_do_activity(punit, ACTIVITY_PILLAGE)) { request_unit_pillage(punit); } } - unit_list_iterate_end; } /** @@ -3264,13 +3181,11 @@ void key_unit_pillage() */ void key_unit_sentry() { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { if (can_unit_do_activity(punit, ACTIVITY_SENTRY)) { request_new_unit_activity(punit, ACTIVITY_SENTRY); } } - unit_list_iterate_end; } /** @@ -3278,13 +3193,11 @@ void key_unit_sentry() */ void key_unit_transform() { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { if (can_unit_do_activity(punit, ACTIVITY_TRANSFORM)) { request_new_unit_activity(punit, ACTIVITY_TRANSFORM); } } - unit_list_iterate_end; } /** diff --git a/client/control.h b/client/control.h index eb480f64f5..884f242262 100644 --- a/client/control.h +++ b/client/control.h @@ -68,7 +68,8 @@ void do_unit_connect(struct tile *ptile, enum unit_activity activity, void do_map_click(struct tile *ptile, enum quickselect_type qtype); void control_mouse_cursor(struct tile *ptile); -void set_hover_state(struct unit_list *punits, enum cursor_hover_state state, +void set_hover_state(const std::vector &units, + enum cursor_hover_state state, enum unit_activity connect_activity, struct extra_type *tgt, int last_tgt, int goto_last_sub_tgt, action_id goto_last_action, @@ -98,7 +99,7 @@ void request_unit_fortify(struct unit *punit); void request_unit_goto(enum unit_orders last_order, action_id act_id, int sub_tgt_id); void request_unit_move_done(); -void request_unit_paradrop(struct unit_list *punits); +void request_unit_paradrop(const std::vector &units); void request_unit_patrol(); void request_unit_pillage(struct unit *punit); void request_unit_sentry(struct unit *punit); @@ -107,7 +108,7 @@ void request_unit_airlift(struct unit *punit, struct city *pcity); void request_units_return(); void request_unit_upgrade(struct unit *punit); void request_unit_convert(struct unit *punit); -void request_units_wait(struct unit_list *punits); +void request_units_wait(const std::vector &units); void request_unit_wakeup(struct unit *punit); enum unit_select_type_mode { SELTYPE_SINGLE, SELTYPE_SAME, SELTYPE_ALL }; @@ -118,7 +119,7 @@ enum unit_select_location_mode { SELLOC_WORLD // World. }; -void request_unit_select(struct unit_list *punits, +void request_unit_select(const std::vector &punits, enum unit_select_type_mode seltype, enum unit_select_location_mode selloc); @@ -142,7 +143,7 @@ void clear_unit_orders(struct unit *punit); bool unit_is_in_focus(const struct unit *punit); struct unit *get_focus_unit_on_tile(const struct tile *ptile); struct unit *head_of_units_in_focus(); -struct unit_list *get_units_in_focus(); +std::vector &get_units_in_focus(); int get_num_units_in_focus(); void unit_focus_set(struct unit *punit); @@ -154,7 +155,7 @@ void unit_focus_advance(); void unit_focus_update(); void auto_center_on_focus_unit(); -void update_unit_pix_label(struct unit_list *punitlist); +void update_unit_pix_label(const std::vector &units); unit *find_visible_unit(const ::tile *ptile); void set_units_in_combat(struct unit *pattacker, struct unit *pdefender); diff --git a/client/dialogs.cpp b/client/dialogs.cpp index adfbd9a583..91d6b8fb39 100644 --- a/client/dialogs.cpp +++ b/client/dialogs.cpp @@ -1554,12 +1554,7 @@ static void unit_upgrade(QVariant data1, QVariant data2) if ((punit = game_unit_by_number(actor_id)) && nullptr != game_city_by_number(tgt_city_id)) { - struct unit_list *as_list; - - as_list = unit_list_new(); - unit_list_append(as_list, punit); - popup_upgrade_dialog(as_list); - unit_list_destroy(as_list); + popup_upgrade_dialog({punit}); } } @@ -3339,22 +3334,19 @@ void popup_pillage_dialog(struct unit *punit, bv_extras extras) /** Disband Message box contructor */ -disband_box::disband_box(struct unit_list *punits, QWidget *parent) - : hud_message_box(parent) +disband_box::disband_box(const std::vector &punits, QWidget *parent) + : hud_message_box(parent), cpunits(punits) { QString str; QPushButton *pb; setAttribute(Qt::WA_DeleteOnClose); setModal(false); - cpunits = unit_list_new(); - unit_list_iterate(punits, punit) { unit_list_append(cpunits, punit); } - unit_list_iterate_end; str = QString(PL_("Are you sure you want to disband that %1 unit?", "Are you sure you want to disband those %1 units?", - unit_list_size(punits))) - .arg(unit_list_size(punits)); + punits.size())) + .arg(punits.size()); pb = addButton(_("Yes"), QMessageBox::AcceptRole); addButton(_("No"), QMessageBox::RejectRole); set_text_title(str, _("Disband units")); @@ -3368,24 +3360,22 @@ disband_box::disband_box(struct unit_list *punits, QWidget *parent) */ void disband_box::disband_clicked() { - unit_list_iterate(cpunits, punit) - { + for (const auto punit : cpunits) { if (unit_can_do_action(punit, ACTION_DISBAND_UNIT)) { request_unit_disband(punit); } } - unit_list_iterate_end; } /** Destructor for disband box */ -disband_box::~disband_box() { unit_list_destroy(cpunits); } +disband_box::~disband_box() = default; /** Pops up a dialog to confirm disband of the unit(s). */ -void popup_disband_dialog(struct unit_list *punits) +void popup_disband_dialog(const std::vector &punits) { disband_box *ask = new disband_box(punits, king()->central_wdg); ask->show(); @@ -3748,20 +3738,21 @@ void show_tech_gained_dialog(Tech_type_id tech) { Q_UNUSED(tech) } /** Popup dialog for upgrade units */ -void popup_upgrade_dialog(struct unit_list *punits) +void popup_upgrade_dialog(const std::vector &punits) { char buf[512]; hud_message_box *ask = new hud_message_box(king()->central_wdg); QString title; QVector *punit_ids; - if (!punits || unit_list_size(punits) == 0) { + if (punits.empty()) { return; } punit_ids = new QVector(); - unit_list_iterate(punits, punit) { punit_ids->push_back(punit->id); } - unit_list_iterate_end; + for (const auto punit : punits) { + punit_ids->push_back(punit->id); + } if (!get_units_upgrade_info(buf, sizeof(buf), punits)) { title = _("Upgrade Unit!"); diff --git a/client/dialogs.h b/client/dialogs.h index 871b3ee679..2974526d23 100644 --- a/client/dialogs.h +++ b/client/dialogs.h @@ -88,10 +88,11 @@ class qfc_dialog : public QDialog { ***************************************************************************/ class disband_box : public hud_message_box { Q_OBJECT - struct unit_list *cpunits; + const std::vector cpunits; public: - explicit disband_box(struct unit_list *punits, QWidget *parent = 0); + explicit disband_box(const std::vector &punits, + QWidget *parent = 0); ~disband_box() override; private slots: void disband_clicked(); @@ -219,7 +220,7 @@ private slots: void popup_revolution_dialog(struct government *government = nullptr); void revolution_response(struct government *government); -void popup_upgrade_dialog(struct unit_list *punits); -void popup_disband_dialog(struct unit_list *punits); +void popup_upgrade_dialog(const std::vector &punits); +void popup_disband_dialog(const std::vector &punits); bool try_default_unit_action(QVariant q1, QVariant q2); bool try_default_city_action(QVariant q1, QVariant q2); diff --git a/client/goto.cpp b/client/goto.cpp index 26291790b3..04a2d9b931 100644 --- a/client/goto.cpp +++ b/client/goto.cpp @@ -241,7 +241,7 @@ static void goto_fill_parameter_base(struct pf_parameter *parameter, Enter the goto state: activate, prepare PF-template and add the initial part. */ -void enter_goto_state(struct unit_list *punits) +void enter_goto_state(const std::vector &units) { fc_assert_ret(!goto_is_active()); @@ -249,13 +249,11 @@ void enter_goto_state(struct unit_list *punits) cancel_selection_rectangle(); // Initialize path finders - unit_list_iterate(punits, punit) - { + for (const auto punit : units) { // This calls path_finder::path_finder(punit) behind the scenes // Need to do this because path_finder isn't copy-assignable goto_finders.emplace(punit->id, punit); } - unit_list_iterate_end; } /** diff --git a/client/goto.h b/client/goto.h index ab89a416fd..85d2357838 100644 --- a/client/goto.h +++ b/client/goto.h @@ -29,7 +29,7 @@ enum goto_tile_state { void init_client_goto(); void free_client_goto(); -void enter_goto_state(struct unit_list *punits); +void enter_goto_state(const std::vector &units); void exit_goto_state(); void goto_unit_killed(struct unit *punit); diff --git a/client/gotodlg.cpp b/client/gotodlg.cpp index 9c5d79bc42..8e271a43d6 100644 --- a/client/gotodlg.cpp +++ b/client/gotodlg.cpp @@ -147,14 +147,12 @@ void goto_dialog::item_selected(const QItemSelection &sl, dest = game_city_by_number(city_id); center_tile_mapcanvas(city_tile(dest)); can_airlift = false; - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { if (unit_can_airlift_to(punit, dest)) { can_airlift = true; break; } } - unit_list_iterate_end; if (can_airlift) { airlift_city->setEnabled(true); @@ -273,13 +271,11 @@ void goto_dialog::airlift_to() pdest = game_city_by_number( goto_tab->item(goto_tab->currentRow(), 0)->data(Qt::UserRole).toInt()); if (pdest) { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { if (unit_can_airlift_to(punit, pdest)) { request_unit_airlift(punit, pdest); } } - unit_list_iterate_end; } } @@ -297,11 +293,9 @@ void goto_dialog::go_to_city() pdest = game_city_by_number( goto_tab->item(goto_tab->currentRow(), 0)->data(Qt::UserRole).toInt()); if (pdest) { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { send_goto_tile(punit, pdest->tile); } - unit_list_iterate_end; } } diff --git a/client/hudwidget.cpp b/client/hudwidget.cpp index 031507dfe3..fa2ab06c55 100644 --- a/client/hudwidget.cpp +++ b/client/hudwidget.cpp @@ -647,8 +647,7 @@ void hud_units::update_actions(unit_list *punits) QString::number(unit_type_get(punit)->hp)); num = unit_list_size(punit->tile->units); snum = QString::number(unit_list_size(punit->tile->units) - 1); - if (unit_list_size(get_units_in_focus()) > 1) { - int n = unit_list_size(get_units_in_focus()); + if (const auto n = get_units_in_focus().size(); n > 1) { // TRANS: preserve leading space; always at least 2 text_str = text_str diff --git a/client/include/dialogs_g.h b/client/include/dialogs_g.h index 8c439a0255..c7095ddb5f 100644 --- a/client/include/dialogs_g.h +++ b/client/include/dialogs_g.h @@ -65,8 +65,6 @@ void popup_bribe_dialog(struct unit *actor, struct unit *punit, int cost, void popup_sabotage_dialog(struct unit *actor, struct city *pcity, const struct action *paction); void popup_pillage_dialog(struct unit *punit, bv_extras extras); -void popup_upgrade_dialog(struct unit_list *punits); -void popup_disband_dialog(struct unit_list *punits); void popup_tileset_suggestion_dialog(void); void popup_soundset_suggestion_dialog(void); void popup_musicset_suggestion_dialog(void); diff --git a/client/include/mapview_g.h b/client/include/mapview_g.h index 14c0a812f9..ab295f98ac 100644 --- a/client/include/mapview_g.h +++ b/client/include/mapview_g.h @@ -23,7 +23,7 @@ #include "mapview_common.h" void update_info_label(void); -void update_unit_info_label(struct unit_list *punitlist); +void update_unit_info_label(const std::vector &unit_list); void update_mouse_cursor(enum cursor_type new_cursor_type); void update_turn_done_button(bool do_restore); void update_city_descriptions(void); diff --git a/client/mapctrl_common.cpp b/client/mapctrl_common.cpp index 69495a5ece..106d18d5b5 100644 --- a/client/mapctrl_common.cpp +++ b/client/mapctrl_common.cpp @@ -348,7 +348,6 @@ void update_turn_done_button_state() void update_line(int canvas_x, int canvas_y) { struct tile *ptile; - struct unit_list *punits; switch (hover_state) { case HOVER_GOTO: @@ -360,11 +359,10 @@ void update_line(int canvas_x, int canvas_y) break; case HOVER_GOTO_SEL_TGT: ptile = canvas_pos_to_tile(canvas_x, canvas_y); - punits = get_units_in_focus(); fc_assert_ret(ptile); - set_hover_state(punits, hover_state, connect_activity, connect_tgt, - ptile->index, goto_last_sub_tgt, goto_last_action, - goto_last_order); + set_hover_state(get_units_in_focus(), hover_state, connect_activity, + connect_tgt, ptile->index, goto_last_sub_tgt, + goto_last_action, goto_last_order); break; case HOVER_NONE: case HOVER_PARADROP: diff --git a/client/mapview.cpp b/client/mapview.cpp index 2a6f5a09a0..04055f541c 100644 --- a/client/mapview.cpp +++ b/client/mapview.cpp @@ -247,8 +247,8 @@ void map_view::show_debugger() connect(m_debugger, &freeciv::tileset_debugger::tile_picking_requested, [](bool active) { if (active) { - set_hover_state(nullptr, HOVER_DEBUG_TILE, ACTIVITY_LAST, - nullptr, NO_TARGET, NO_TARGET, ACTION_NONE, + set_hover_state({}, HOVER_DEBUG_TILE, ACTIVITY_LAST, nullptr, + NO_TARGET, NO_TARGET, ACTION_NONE, ORDER_LAST); } else if (!active && hover_state == HOVER_DEBUG_TILE) { clear_hover_state(); @@ -445,7 +445,7 @@ void map_view::find_place(int pos_x, int pos_y, int &w, int &h, int wdth, prompt etc). And it may call update_unit_pix_label() to update the icons for units on this tile. */ -void update_unit_info_label(struct unit_list *punitlist) +void update_unit_info_label(const std::vector &unit_list) { if (queen()->unitinfo_wdg->isVisible()) { queen()->unitinfo_wdg->update_actions(nullptr); diff --git a/client/menu.cpp b/client/menu.cpp index 8dcc999f45..64168abe0b 100644 --- a/client/menu.cpp +++ b/client/menu.cpp @@ -1406,7 +1406,6 @@ void mr_menu::update_airlift_menu() void mr_menu::update_roads_menu() { QAction *act; - struct unit_list *punits = nullptr; bool enabled = false; if (!initialized) { @@ -1423,7 +1422,6 @@ void mr_menu::update_roads_menu() return; } - punits = get_units_in_focus(); extra_type_by_cause_iterate(EC_ROAD, pextra) { if (pextra->buildable) { @@ -1435,8 +1433,8 @@ void mr_menu::update_roads_menu() act->setData(road_id); QObject::connect(act, &QAction::triggered, [this, road_id]() { slot_build_path(road_id); }); - if (can_units_do_activity_targeted(punits, ACTIVITY_GEN_ROAD, - pextra)) { + if (can_units_do_activity_targeted(get_units_in_focus(), + ACTIVITY_GEN_ROAD, pextra)) { act->setEnabled(true); enabled = true; } else { @@ -1457,7 +1455,6 @@ void mr_menu::update_roads_menu() void mr_menu::update_bases_menu() { QAction *act; - struct unit_list *punits = nullptr; bool enabled = false; if (!initialized) { return; @@ -1475,7 +1472,6 @@ void mr_menu::update_bases_menu() return; } - punits = get_units_in_focus(); extra_type_by_cause_iterate(EC_BASE, pextra) { if (pextra->buildable) { @@ -1487,7 +1483,8 @@ void mr_menu::update_bases_menu() act->setData(base_id); QObject::connect(act, &QAction::triggered, [this, base_id]() { slot_build_base(base_id); }); - if (can_units_do_activity_targeted(punits, ACTIVITY_BASE, pextra)) { + if (can_units_do_activity_targeted(get_units_in_focus(), ACTIVITY_BASE, + pextra)) { act->setEnabled(true); enabled = true; } else { @@ -1545,21 +1542,19 @@ void mr_menu::nonunit_sensitivity() } } -static struct extra_type *next_extra(struct unit_list *punits, +static struct extra_type *next_extra(const std::vector &punits, extra_cause cause) { struct extra_type *pextra = nullptr; /* FIXME: this overloading doesn't work well with multiple focus * units. */ - unit_list_iterate(punits, builder) - { + for (const auto builder : punits) { pextra = next_extra_for_tile(unit_tile(builder), cause, unit_owner(builder), builder); if (pextra != nullptr) { break; } } - unit_list_iterate_end; return pextra; } @@ -1570,7 +1565,6 @@ void mr_menu::menus_sensitive() { QList keys; QMultiHash::iterator i; - struct unit_list *punits = nullptr; struct road_type *proad; struct extra_type *tgt; bool city_on_tile = false; @@ -1598,7 +1592,7 @@ void mr_menu::menus_sensitive() return; } - punits = get_units_in_focus(); + const auto &punits = get_units_in_focus(); city_on_tile = any_unit_in_city(punits); units_all_same_tile = units_on_the_same_tile(punits); @@ -1647,7 +1641,7 @@ void mr_menu::menus_sensitive() } if (units_all_same_tile) { - struct unit *punit = unit_list_get(punits, 0); + struct unit *punit = punits.front(); pterrain = tile_terrain(unit_tile(punit)); @@ -1673,7 +1667,7 @@ void mr_menu::menus_sensitive() i.value()->setEnabled(true); } if (units_all_same_tile) { - struct unit *punit = unit_list_get(punits, 0); + struct unit *punit = punits.front(); pterrain = tile_terrain(unit_tile(punit)); @@ -1699,7 +1693,7 @@ void mr_menu::menus_sensitive() i.value()->setEnabled(true); } if (units_all_same_tile) { - struct unit *punit = unit_list_get(punits, 0); + struct unit *punit = punits.front(); pterrain = tile_terrain(unit_tile(punit)); if (pterrain->irrigation_result != T_NONE @@ -1722,7 +1716,7 @@ void mr_menu::menus_sensitive() i.value()->setEnabled(true); } if (units_all_same_tile) { - struct unit *punit = unit_list_get(punits, 0); + struct unit *punit = punits.front(); pterrain = tile_terrain(unit_tile(punit)); if (pterrain->mining_result != T_NONE @@ -1747,9 +1741,7 @@ void mr_menu::menus_sensitive() break; } if (units_all_same_tile) { - struct unit *punit = unit_list_get(punits, 0); - pterrain = tile_terrain(unit_tile(punit)); - punit = unit_list_get(punits, 0); + struct unit *punit = punits.front(); pterrain = tile_terrain(unit_tile(punit)); if (pterrain->transform_result != T_NONE && pterrain->transform_result != pterrain) { @@ -1990,8 +1982,7 @@ void mr_menu::slot_show_map() */ void mr_menu::slot_build_city() { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { /* FIXME: this can provide different actions for different units... * not good! */ /* Enable the button for adding to a city in all cases, so we @@ -2003,7 +1994,6 @@ void mr_menu::slot_build_city() request_unit_caravan_action(punit, ACTION_HELP_WONDER); } } - unit_list_iterate_end; } /** @@ -2016,8 +2006,7 @@ void mr_menu::slot_clean_fallout() { key_unit_fallout(); } */ void mr_menu::slot_clean_pollution() { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { /* FIXME: this can provide different actions for different units... * not good! */ struct extra_type *pextra; @@ -2032,7 +2021,6 @@ void mr_menu::slot_clean_pollution() key_unit_paradrop(); } } - unit_list_iterate_end; } /** @@ -2116,8 +2104,7 @@ void mr_menu::slot_auto_settler() { key_unit_auto_settle(); } */ void mr_menu::slot_build_road() { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { /* FIXME: this can provide different actions for different units... * not good! */ struct extra_type *tgt = next_extra_for_tile(unit_tile(punit), EC_ROAD, @@ -2134,7 +2121,6 @@ void mr_menu::slot_build_road() request_unit_caravan_action(punit, ACTION_TRADE_ROUTE); } } - unit_list_iterate_end; } /** @@ -2185,8 +2171,7 @@ void mr_menu::slot_rename() if (get_num_units_in_focus() != 1) { return; } - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { auto ask = new hud_input_box(king()->central_wdg); ask->set_text_title_definput(_("New unit name:"), _("Rename Unit"), @@ -2204,7 +2189,6 @@ void mr_menu::slot_rename() }); ask->show(); } - unit_list_iterate_end; } /** @@ -2337,25 +2321,23 @@ void mr_menu::slot_delayed_goto() delayed_order = true; dg = D_GOTO; - struct unit_list *punits = get_units_in_focus(); - if (unit_list_size(punits) == 0) { + const auto &focus = get_units_in_focus(); + if (focus.empty()) { return; } if (hover_state != HOVER_GOTO) { - set_hover_state(punits, HOVER_GOTO, ACTIVITY_LAST, nullptr, NO_TARGET, + set_hover_state(focus, HOVER_GOTO, ACTIVITY_LAST, nullptr, NO_TARGET, NO_TARGET, ACTION_NONE, ORDER_LAST); - enter_goto_state(punits); + enter_goto_state(focus); create_line_at_mouse_pos(); control_mouse_cursor(nullptr); } - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : focus) { i++; unit_item = new qfc_delayed_unit_item(dg, punit->id); units_list.add(unit_item); units_list.nr_units = i; } - unit_list_iterate_end; } /** @@ -2396,11 +2378,9 @@ void mr_menu::slot_execute_orders() */ void mr_menu::slot_load() { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { request_transport(punit, unit_tile(punit)); } - unit_list_iterate_end; } /** @@ -2423,11 +2403,9 @@ void mr_menu::slot_set_home() { key_unit_homecity(); } */ void mr_menu::slot_unload() { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { request_unit_unload(punit); } - unit_list_iterate_end; } /** @@ -2784,8 +2762,7 @@ void mr_menu::slot_help(const QString &topic) *****************************************************************/ void mr_menu::slot_build_path(int id) { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { extra_type_by_cause_iterate(EC_ROAD, pextra) { if (pextra->buildable && pextra->id == id @@ -2796,7 +2773,6 @@ void mr_menu::slot_build_path(int id) } extra_type_by_cause_iterate_end; } - unit_list_iterate_end; } /**************************************************************** @@ -2804,8 +2780,7 @@ void mr_menu::slot_build_path(int id) *****************************************************************/ void mr_menu::slot_build_base(int id) { - unit_list_iterate(get_units_in_focus(), punit) - { + for (const auto punit : get_units_in_focus()) { extra_type_by_cause_iterate(EC_BASE, pextra) { if (pextra->buildable && pextra->id == id @@ -2815,7 +2790,6 @@ void mr_menu::slot_build_base(int id) } extra_type_by_cause_iterate_end; } - unit_list_iterate_end; } /** diff --git a/client/packhand.cpp b/client/packhand.cpp index 5860371b00..f37696f965 100644 --- a/client/packhand.cpp +++ b/client/packhand.cpp @@ -583,7 +583,6 @@ void handle_city_info(const struct packet_city_info *packet) bool shield_stock_changed = false; bool production_changed = false; bool trade_routes_changed = false; - struct unit_list *pfocus_units = get_units_in_focus(); struct city *pcity = game_city_by_number(packet->id); struct tile_list *worked_tiles = nullptr; struct tile *pcenter = index_to_tile(&(wld.map), packet->tile); @@ -878,14 +877,12 @@ void handle_city_info(const struct packet_city_info *packet) // Update focus unit info label if necessary. if (name_changed) { - unit_list_iterate(pfocus_units, pfocus_unit) - { + for (const auto pfocus_unit : get_units_in_focus()) { if (pfocus_unit->homecity == pcity->id) { - update_unit_info_label(pfocus_units); + update_unit_info_label(get_units_in_focus()); break; } } - unit_list_iterate_end; } // Update the science dialog if necessary. diff --git a/client/text.cpp b/client/text.cpp index b0c36298db..00ce6c0d9d 100644 --- a/client/text.cpp +++ b/client/text.cpp @@ -317,8 +317,7 @@ const QString popup_info_text(struct tile *ptile) + qendl(); } - unit_list_iterate(get_units_in_focus(), pfocus_unit) - { + for (const auto &pfocus_unit : get_units_in_focus()) { struct city *hcity = game_city_by_number(pfocus_unit->homecity); if (utype_can_do_action(unit_type_get(pfocus_unit), ACTION_TRADE_ROUTE) @@ -332,7 +331,6 @@ const QString popup_info_text(struct tile *ptile) + qendl(); } } - unit_list_iterate_end; } { const char *infratext = get_infrastructure_text(ptile->extras); @@ -437,8 +435,7 @@ const QString popup_info_text(struct tile *ptile) } } - unit_list_iterate(get_units_in_focus(), pfocus_unit) - { + for (const auto pfocus_unit : get_units_in_focus()) { int att_chance = FC_INFINITY, def_chance = FC_INFINITY; bool found = false; @@ -465,7 +462,6 @@ const QString popup_info_text(struct tile *ptile) + qendl(); } } - unit_list_iterate_end; /* TRANS: A is attack power, D is defense power, FP is firepower, * HP is hitpoints (current and max). */ @@ -602,7 +598,7 @@ const QString unit_description(struct unit *punit) for those that can. Returns nullptr if an airlift is not possible for any of the units. */ -const QString get_airlift_text(const struct unit_list *punits, +const QString get_airlift_text(const std::vector &units, const struct city *pdest) { QString str; @@ -615,8 +611,7 @@ const QString get_airlift_text(const struct unit_list *punits, } best = AL_IMPOSSIBLE; int cur = 0, max = 0; - unit_list_iterate(punits, punit) - { + for (const auto punit : units) { enum texttype tthis = AL_IMPOSSIBLE; enum unit_airlift_result result; @@ -684,7 +679,6 @@ const QString get_airlift_text(const struct unit_list *punits, // Now take the most optimistic view. best = MAX(best, tthis); } - unit_list_iterate_end; switch (best) { case AL_IMPOSSIBLE: @@ -1115,20 +1109,19 @@ const QString get_info_label_text_popup() Returns TRUE iff any units can be upgraded. */ bool get_units_upgrade_info(char *buf, size_t bufsz, - struct unit_list *punits) + const std::vector &punits) { - if (unit_list_size(punits) == 0) { + if (punits.empty()) { fc_snprintf(buf, bufsz, _("No units to upgrade!")); return false; - } else if (unit_list_size(punits) == 1) { - return (UU_OK == unit_upgrade_info(unit_list_front(punits), buf, bufsz)); + } else if (punits.size() == 1) { + return (UU_OK == unit_upgrade_info(punits.front(), buf, bufsz)); } else { int upgrade_cost = 0; int num_upgraded = 0; int min_upgrade_cost = FC_INFINITY; - unit_list_iterate(punits, punit) - { + for (const auto punit : punits) { if (unit_owner(punit) == client_player() && UU_OK == unit_upgrade_test(punit, false)) { const struct unit_type *from_unittype = unit_type_get(punit); @@ -1142,7 +1135,6 @@ bool get_units_upgrade_info(char *buf, size_t bufsz, min_upgrade_cost = MIN(min_upgrade_cost, cost); } } - unit_list_iterate_end; if (num_upgraded == 0) { fc_snprintf(buf, bufsz, _("None of these units may be upgraded.")); return false; diff --git a/client/text.h b/client/text.h index 92ed6bfbd7..764cc493c0 100644 --- a/client/text.h +++ b/client/text.h @@ -10,6 +10,12 @@ **************************************************************************/ #pragma once +#include "fc_types.h" + +#include + +#include + struct player_spaceship; /**************************************************************************** @@ -19,7 +25,7 @@ const QString get_tile_output_text(const struct tile *ptile); const QString popup_info_text(struct tile *ptile); const QString get_nearest_city_text(struct city *pcity, int sq_dist); const QString unit_description(struct unit *punit); -const QString get_airlift_text(const struct unit_list *punits, +const QString get_airlift_text(const std::vector &units, const struct city *pdest); const QString science_dialog_text(); const QString get_science_target_text(double *percent); @@ -27,7 +33,7 @@ const QString get_science_goal_text(Tech_type_id goal); const QString get_info_label_text(bool moreinfo); const QString get_info_label_text_popup(); bool get_units_upgrade_info(char *buf, size_t bufsz, - struct unit_list *punits); + const std::vector &punits); const QString get_spaceship_descr(struct player_spaceship *pship); QString get_score_text(const struct player *pplayer); diff --git a/client/tilespec.cpp b/client/tilespec.cpp index feb1cd96d2..9e2b19697b 100644 --- a/client/tilespec.cpp +++ b/client/tilespec.cpp @@ -4238,7 +4238,6 @@ static void fill_grid_sprite_array(const struct tileset *t, bool known[NUM_EDGE_TILES], city[NUM_EDGE_TILES]; bool unit[NUM_EDGE_TILES], worked[NUM_EDGE_TILES]; int i; - struct unit_list *pfocus_units = get_units_in_focus(); for (i = 0; i < NUM_EDGE_TILES; i++) { int dummy_x, dummy_y; @@ -4248,8 +4247,7 @@ static void fill_grid_sprite_array(const struct tileset *t, known[i] = tile && client_tile_get_known(tile) != TILE_UNKNOWN; unit[i] = false; if (tile && !citymode) { - unit_list_iterate(pfocus_units, pfocus_unit) - { + for (const auto pfocus_unit : get_units_in_focus()) { if (unit_drawn_with_city_outline(pfocus_unit, false)) { struct tile *utile = unit_tile(pfocus_unit); int radius = game.info.init_city_radius_sq @@ -4265,7 +4263,6 @@ static void fill_grid_sprite_array(const struct tileset *t, } } } - unit_list_iterate_end; } worked[i] = false; @@ -4342,16 +4339,12 @@ static void fill_grid_sprite_array(const struct tileset *t, if (gui_options.draw_native && citymode == nullptr) { bool native = true; - struct unit_list *pfocus_units = get_units_in_focus(); - - unit_list_iterate(pfocus_units, pfocus) - { + for (const auto pfocus : get_units_in_focus()) { if (!is_native_tile(unit_type_get(pfocus), ptile)) { native = false; break; } } - unit_list_iterate_end; if (!native) { if (t->sprites.grid.nonnative != nullptr) { diff --git a/common/unitlist.cpp b/common/unitlist.cpp index c4a35418c1..e05734b8b5 100644 --- a/common/unitlist.cpp +++ b/common/unitlist.cpp @@ -86,16 +86,14 @@ void unit_list_sort_ord_city(struct unit_list *punitlist) /** Return TRUE if the function returns true for any of the units. */ -bool can_units_do(const struct unit_list *punits, +bool can_units_do(const std::vector &units, bool(can_fn)(const struct unit *punit)) { - unit_list_iterate(punits, punit) - { + for (const auto punit : units) { if (can_fn(punit)) { return true; } } - unit_list_iterate_end; return false; } @@ -103,20 +101,18 @@ bool can_units_do(const struct unit_list *punits, /** Returns TRUE if any of the units can do the activity. */ -bool can_units_do_activity(const struct unit_list *punits, +bool can_units_do_activity(const std::vector &units, enum unit_activity activity) { // Make sure nobody uses these old activities any more fc_assert_ret_val( activity != ACTIVITY_FORTRESS && activity != ACTIVITY_AIRBASE, false); - unit_list_iterate(punits, punit) - { + for (const auto punit : units) { if (can_unit_do_activity(punit, activity)) { return true; } } - unit_list_iterate_end; return false; } @@ -124,17 +120,15 @@ bool can_units_do_activity(const struct unit_list *punits, /** Returns TRUE if any of the units can do the targeted activity. */ -bool can_units_do_activity_targeted(const struct unit_list *punits, +bool can_units_do_activity_targeted(const std::vector &units, enum unit_activity activity, struct extra_type *pextra) { - unit_list_iterate(punits, punit) - { + for (const auto punit : units) { if (can_unit_do_activity_targeted(punit, activity, pextra)) { return true; } } - unit_list_iterate_end; return false; } @@ -142,10 +136,9 @@ bool can_units_do_activity_targeted(const struct unit_list *punits, /** Returns TRUE if any of the units can build any road. */ -bool can_units_do_any_road(const struct unit_list *punits) +bool can_units_do_any_road(const std::vector &units) { - unit_list_iterate(punits, punit) - { + for (const auto punit : units) { extra_type_by_cause_iterate(EC_ROAD, pextra) { struct road_type *proad = extra_road_get(pextra); @@ -156,7 +149,6 @@ bool can_units_do_any_road(const struct unit_list *punits) } extra_type_by_cause_iterate_end; } - unit_list_iterate_end; return false; } @@ -164,11 +156,10 @@ bool can_units_do_any_road(const struct unit_list *punits) /** Returns TRUE if any of the units can build base with given gui_type. */ -bool can_units_do_base_gui(const struct unit_list *punits, +bool can_units_do_base_gui(const std::vector &units, enum base_gui_type base_gui) { - unit_list_iterate(punits, punit) - { + for (const auto punit : units) { struct base_type *pbase = get_base_by_gui_type(base_gui, punit, unit_tile(punit)); @@ -177,7 +168,6 @@ bool can_units_do_base_gui(const struct unit_list *punits, return true; } } - unit_list_iterate_end; return false; } @@ -188,16 +178,14 @@ bool can_units_do_base_gui(const struct unit_list *punits, If has_flag is false, returns true iff any of the units don't have the flag. */ -bool units_have_type_flag(const struct unit_list *punits, +bool units_have_type_flag(const std::vector &units, enum unit_type_flag_id flag, bool has_flag) { - unit_list_iterate(punits, punit) - { + for (const auto punit : units) { if (EQ(has_flag, unit_has_type_flag(punit, flag))) { return true; } } - unit_list_iterate_end; return false; } @@ -205,19 +193,17 @@ bool units_have_type_flag(const struct unit_list *punits, /** Does the list contain any cityfounder units */ -bool units_contain_cityfounder(const struct unit_list *punits) +bool units_contain_cityfounder(const std::vector &units) { if (game.scenario.prevent_new_cities) { return false; } - unit_list_iterate(punits, punit) - { + for (const auto punit : units) { if (EQ(true, unit_can_do_action(punit, ACTION_FOUND_CITY))) { return true; } } - unit_list_iterate_end; return false; } @@ -229,16 +215,14 @@ bool units_contain_cityfounder(const struct unit_list *punits) If has_flag is false, returns true iff any of the units are unable do the specified action. */ -bool units_can_do_action(const struct unit_list *punits, action_id act_id, +bool units_can_do_action(const std::vector &units, action_id act_id, bool can_do) { - unit_list_iterate(punits, punit) - { + for (const auto punit : units) { if (EQ(can_do, unit_can_do_action(punit, act_id))) { return true; } } - unit_list_iterate_end; return false; } @@ -246,15 +230,13 @@ bool units_can_do_action(const struct unit_list *punits, action_id act_id, /** Return TRUE iff any of the units is a transporter that is occupied. */ -bool units_are_occupied(const struct unit_list *punits) +bool units_are_occupied(const std::vector &units) { - unit_list_iterate(punits, punit) - { + for (const auto punit : units) { if (get_transporter_occupancy(punit) > 0) { return true; } } - unit_list_iterate_end; return false; } @@ -262,15 +244,13 @@ bool units_are_occupied(const struct unit_list *punits) /** Returns TRUE iff any of these units can load. */ -bool units_can_load(const struct unit_list *punits) +bool units_can_load(const std::vector &units) { - unit_list_iterate(punits, punit) - { + for (const auto punit : units) { if (unit_can_load(punit)) { return true; } } - unit_list_iterate_end; return false; } @@ -278,17 +258,15 @@ bool units_can_load(const struct unit_list *punits) /** Return TRUE iff any of these units can unload. */ -bool units_can_unload(const struct unit_list *punits) +bool units_can_unload(const std::vector &units) { - unit_list_iterate(punits, punit) - { + for (const auto punit : units) { if (unit_transported(punit) && can_unit_unload(punit, unit_transport_get(punit)) && can_unit_exist_at_tile(&(wld.map), punit, unit_tile(punit))) { return true; } } - unit_list_iterate_end; return false; } @@ -297,16 +275,14 @@ bool units_can_unload(const struct unit_list *punits) Return TRUE iff any of the units' tiles have the activity running on them. */ -bool units_have_activity_on_tile(const struct unit_list *punits, +bool units_have_activity_on_tile(const std::vector &units, enum unit_activity activity) { - unit_list_iterate(punits, punit) - { + for (const auto punit : units) { if (is_unit_activity_on_tile(activity, unit_tile(punit))) { return true; } } - unit_list_iterate_end; return false; } @@ -315,15 +291,13 @@ bool units_have_activity_on_tile(const struct unit_list *punits, Return TRUE iff any of the units can be upgraded to another unit type (for money) */ -bool units_can_upgrade(const struct unit_list *punits) +bool units_can_upgrade(const std::vector &units) { - unit_list_iterate(punits, punit) - { + for (const auto punit : units) { if (UU_OK == unit_upgrade_test(punit, false)) { return true; } } - unit_list_iterate_end; return false; } @@ -331,39 +305,35 @@ bool units_can_upgrade(const struct unit_list *punits) /** Return TRUE iff any of the units can convert to another unit type */ -bool units_can_convert(const struct unit_list *punits) +bool units_can_convert(const std::vector &units) { - unit_list_iterate(punits, punit) - { + for (const auto punit : units) { if (utype_can_do_action(unit_type_get(punit), ACTION_CONVERT) && unit_can_convert(punit)) { return true; } } - unit_list_iterate_end; return false; } // Return TRUE if any of the units is in city -bool any_unit_in_city(const struct unit_list *punits) +bool any_unit_in_city(const std::vector &units) { - unit_list_iterate(punits, punit) - { + for (const auto punit : units) { if (tile_city(unit_tile(punit))) { return true; } } - unit_list_iterate_end; + return false; } -bool units_on_the_same_tile(const struct unit_list *punits) +bool units_on_the_same_tile(const std::vector &units) { struct tile *ptile = nullptr; - unit_list_iterate(punits, punit) - { + for (const auto punit : units) { if (!ptile) { ptile = punit->tile; } @@ -371,6 +341,6 @@ bool units_on_the_same_tile(const struct unit_list *punits) return false; } } - unit_list_iterate_end; + return true; } diff --git a/common/unitlist.h b/common/unitlist.h index 80b2e768fb..dc5bd5c650 100644 --- a/common/unitlist.h +++ b/common/unitlist.h @@ -15,6 +15,8 @@ #include "unit.h" // for diplomat_actions #include "unittype.h" // for unit_type_flag_id +#include + // get 'struct unit_list' and related functions: #define SPECLIST_TAG unit #define SPECLIST_TYPE struct unit @@ -60,28 +62,28 @@ struct unit *unit_list_find(const struct unit_list *punitlist, int unit_id); void unit_list_sort_ord_map(struct unit_list *punitlist); void unit_list_sort_ord_city(struct unit_list *punitlist); -bool can_units_do(const struct unit_list *punits, +bool can_units_do(const std::vector &units, bool(can_fn)(const struct unit *punit)); -bool can_units_do_activity(const struct unit_list *punits, +bool can_units_do_activity(const std::vector &units, enum unit_activity activity); -bool can_units_do_activity_targeted(const struct unit_list *punits, +bool can_units_do_activity_targeted(const std::vector &units, enum unit_activity activity, struct extra_type *pextra); -bool can_units_do_any_road(const struct unit_list *punits); -bool can_units_do_base_gui(const struct unit_list *punits, +bool can_units_do_any_road(const std::vector &units); +bool can_units_do_base_gui(const std::vector &units, enum base_gui_type base_gui); -bool units_have_type_flag(const struct unit_list *punits, +bool units_have_type_flag(const std::vector &units, enum unit_type_flag_id flag, bool has_flag); -bool units_contain_cityfounder(const struct unit_list *punits); -bool units_can_do_action(const struct unit_list *punits, action_id act_id, +bool units_contain_cityfounder(const std::vector &units); +bool units_can_do_action(const std::vector &units, action_id act_id, bool can_do); -bool units_are_occupied(const struct unit_list *punits); -bool units_can_load(const struct unit_list *punits); -bool units_can_unload(const struct unit_list *punits); -bool units_have_activity_on_tile(const struct unit_list *punits, +bool units_are_occupied(const std::vector &units); +bool units_can_load(const std::vector &units); +bool units_can_unload(const std::vector &units); +bool units_have_activity_on_tile(const std::vector &units, enum unit_activity activity); -bool units_can_upgrade(const struct unit_list *punits); -bool units_can_convert(const struct unit_list *punits); -bool any_unit_in_city(const struct unit_list *punits); -bool units_on_the_same_tile(const struct unit_list *punits); +bool units_can_upgrade(const std::vector &units); +bool units_can_convert(const std::vector &units); +bool any_unit_in_city(const std::vector &units); +bool units_on_the_same_tile(const std::vector &units);