Skip to content

Commit

Permalink
Factor out unit_is_on_stand_by and reduce code duplication
Browse files Browse the repository at this point in the history
This check is used in the logic to find the nearest unit in
`view_units.cpp` and the code to cycle units in `hudwidget.cpp`.
  • Loading branch information
blabber committed Jan 4, 2025
1 parent 26d8fab commit 34f3cc3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
22 changes: 21 additions & 1 deletion client/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,14 +357,34 @@ static void ask_server_for_actions(struct unit *punit)
}

/**
Return TRUE iff this unit is in focus.
Return TRUE if this unit is in focus.
*/
bool unit_is_in_focus(const struct unit *punit)
{
const auto &focus = get_units_in_focus();
return std::find(focus.begin(), focus.end(), punit) != focus.end();
}

/**
Returns TRUE if this unit is on stand by.
A unit is on stand by, if it is idle or sentried and not busy
building anything.
*/
bool unit_is_on_stand_by(const struct unit *punit)
{
if (ACTIVITY_IDLE != punit->activity
&& ACTIVITY_SENTRY != punit->activity) {
return false;
}

if (!can_unit_do_activity(punit, ACTIVITY_IDLE)) {
return false;
}

return true;
}

/**
Return TRUE iff a unit on this tile is in focus.
*/
Expand Down
1 change: 1 addition & 0 deletions client/control.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ void wakeup_sentried_units(struct tile *ptile);
void clear_unit_orders(struct unit *punit);

bool unit_is_in_focus(const struct unit *punit);
bool unit_is_on_stand_by(const struct unit *punit);
struct unit *get_focus_unit_on_tile(const struct tile *ptile);
struct unit *head_of_units_in_focus();
std::vector<unit *> &get_units_in_focus();
Expand Down
11 changes: 1 addition & 10 deletions client/hudwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1787,16 +1787,7 @@ static bool cycle_units_predicate(const struct unit *current_unit,
return false;
}

if (ACTIVITY_IDLE != candidate->activity
&& ACTIVITY_SENTRY != candidate->activity) {
return false;
}

if (!can_unit_do_activity(candidate, ACTIVITY_IDLE)) {
return false;
}

return true;
return unit_is_on_stand_by(candidate);
}

/**
Expand Down
7 changes: 2 additions & 5 deletions client/views/view_units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,11 +550,8 @@ void units_view::find_nearest()
if ((punit = find_nearest_unit(utype, ptile))) {
queen()->mapview_wdg->center_on_tile(punit->tile);

if (ACTIVITY_IDLE == punit->activity
|| ACTIVITY_SENTRY == punit->activity) {
if (can_unit_do_activity(punit, ACTIVITY_IDLE)) {
unit_focus_set_and_select(punit);
}
if (unit_is_on_stand_by(punit)) {
unit_focus_set_and_select(punit);
}
}

Expand Down

0 comments on commit 34f3cc3

Please sign in to comment.