Display: refactor out [playing|viewing]_side

Now defined in terms of [playing|viewing]_team().side(). Much clearer. No more damn +1 math.
This commit is contained in:
Charles Dang 2024-08-31 20:31:26 -04:00
parent 4c6580250a
commit ac9f0674d7
18 changed files with 37 additions and 50 deletions

View file

@ -124,15 +124,6 @@ public:
const team& playing_team() const;
const team& viewing_team() const;
/**
* The 1-based equivalent of the 0-based viewing_team_index() function. This is the side-number that
* WML uses.
*
* TODO: provide a better interface in a better place (consistent base numbers, and not in a GUI
* class).
*/
int viewing_side() const { return static_cast<int>(currentTeam_ + 1); }
/**
* Sets the team controlled by the player using the computer.
* Data from this team will be displayed in the game status.
@ -220,7 +211,6 @@ public:
/** Virtual functions shadowed in game_display. These are needed to generate reports easily, without dynamic casting. Hope to factor out eventually. */
virtual const map_location & displayed_unit_hex() const { return map_location::null_location(); }
virtual int playing_side() const { return -100; } //In this case give an obviously wrong answer to fail fast, since this could actually cause a big bug. */
virtual const std::set<std::string>& observers() const { static const std::set<std::string> fake_obs = std::set<std::string> (); return fake_obs; }
/**

View file

@ -122,7 +122,7 @@ std::unique_ptr<editor_action> mouse_action_unit::up_left(editor_display& disp,
const unit_type &ut = *new_unit_type;
unit_race::GENDER gender = ut.genders().front();
unit_ptr new_unit = unit::create(ut, disp.viewing_side(), true, gender);
unit_ptr new_unit = unit::create(ut, disp.viewing_team().side(), true, gender);
auto action = std::make_unique<editor_action_unit>(hex, *new_unit);
return action;
}
@ -153,7 +153,7 @@ void mouse_action_unit::set_unit_mouse_overlay(editor_display& disp, const unit_
{
std::stringstream filename;
filename << u.image() << "~RC(" << u.flag_rgb() << '>'
<< team::get_side_color_id(disp.viewing_side()) << ')';
<< team::get_side_color_id(disp.viewing_team().side()) << ')';
disp.set_mouseover_hex_overlay(image::get_texture(filename.str()));
}

View file

@ -77,7 +77,7 @@ void unit_palette::setup_item(
{
std::stringstream filename;
filename << u.image() << "~RC(" << u.flag_rgb() << '>'
<< team::get_side_color_id(gui_.viewing_side()) << ')';
<< team::get_side_color_id(gui_.viewing_team().side()) << ')';
base_image = image::get_texture(filename.str());
if(!base_image) {

View file

@ -198,9 +198,6 @@ public:
static int& debug_highlight(const map_location& loc);
static void clear_debug_highlights() { debugHighlights_.clear(); }
/** The playing team is the team whose turn it is. */
virtual int playing_side() const override { return static_cast<int>(activeTeam_) + 1; }
std::string current_team_name() const;
display_chat_manager & get_chat_manager() { return *chat_man_; }

View file

@ -197,7 +197,7 @@ void show_unit_list(display& gui)
const unit_map& units = gui.get_units();
for(unit_map::const_iterator i = units.begin(); i != units.end(); ++i) {
if(i->side() != gui.viewing_side()) {
if(i->side() != gui.viewing_team().side()) {
continue;
}

View file

@ -379,7 +379,7 @@ bool play_controller::hotkey_handler::can_execute_command(const hotkey::ui_comma
return !events::commands_disabled &&
menu_handler_.current_unit().valid() &&
!(menu_handler_.current_unit()->unrenamable()) &&
menu_handler_.current_unit()->side() == gui()->viewing_side() &&
menu_handler_.current_unit()->side() == gui()->viewing_team().side() &&
play_controller_.get_teams()[menu_handler_.current_unit()->side() - 1].is_local_human();
default:
@ -516,7 +516,7 @@ bool play_controller::hotkey_handler::in_context_menu(const hotkey::ui_command&
case hotkey::HOTKEY_RECALL: {
// last_hex_ is set by mouse_events::mouse_motion
const map_location & last_hex = mouse_handler_.get_last_hex();
const int viewing_side = gui()->viewing_side();
const int viewing_side = gui()->viewing_team().side();
// A quick check to save us having to create the future map and
// possibly loop through all units.

View file

@ -53,21 +53,21 @@ void playsingle_controller::hotkey_handler::recruit(){
if (!browse())
menu_handler_.recruit(play_controller_.current_side(), mouse_handler_.get_last_hex());
else if (whiteboard_manager_->is_active())
menu_handler_.recruit(gui()->viewing_side(), mouse_handler_.get_last_hex());
menu_handler_.recruit(gui()->viewing_team().side(), mouse_handler_.get_last_hex());
}
void playsingle_controller::hotkey_handler::repeat_recruit(){
if (!browse())
menu_handler_.repeat_recruit(play_controller_.current_side(), mouse_handler_.get_last_hex());
else if (whiteboard_manager_->is_active())
menu_handler_.repeat_recruit(gui()->viewing_side(), mouse_handler_.get_last_hex());
menu_handler_.repeat_recruit(gui()->viewing_team().side(), mouse_handler_.get_last_hex());
}
void playsingle_controller::hotkey_handler::recall(){
if (!browse())
menu_handler_.recall(play_controller_.current_side(), mouse_handler_.get_last_hex());
else if (whiteboard_manager_->is_active())
menu_handler_.recall(gui()->viewing_side(), mouse_handler_.get_last_hex());
menu_handler_.recall(gui()->viewing_team().side(), mouse_handler_.get_last_hex());
}
void playsingle_controller::hotkey_handler::toggle_shroud_updates(){

View file

@ -644,7 +644,7 @@ void menu_handler::terrain_description(mouse_handler& mousehandler)
void menu_handler::rename_unit()
{
const unit_map::iterator un = current_unit();
if(un == pc_.get_units().end() || gui_->viewing_side() != un->side()) {
if(un == pc_.get_units().end() || gui_->viewing_team().side() != un->side()) {
return;
}
@ -817,7 +817,7 @@ void menu_handler::label_terrain(mouse_handler& mousehandler, bool team_only)
if(team_only) {
team_name = gui_->labels().team_name();
} else {
color = team::get_side_color(gui_->viewing_side());
color = team::get_side_color(gui_->viewing_team().side());
}
const terrain_label* res = gui_->labels().set_label(loc, label, gui_->viewing_team_index(), team_name, color);
if(res) {
@ -1328,7 +1328,7 @@ void menu_handler::send_chat_message(const std::string& message, bool allies_onl
ss << time;
cfg["time"] = ss.str();
const int side = board().is_observer() ? 0 : gui_->viewing_side();
const int side = board().is_observer() ? 0 : gui_->viewing_team().side();
if(!board().is_observer()) {
cfg["side"] = side;
}
@ -1464,7 +1464,7 @@ void console_handler::do_droid()
std::transform(action.begin(), action.end(), action.begin(), tolower);
// default to the current side if empty
const unsigned int side = side_s.empty() ? team_num_ : lexical_cast_default<unsigned int>(side_s);
const bool is_your_turn = menu_handler_.pc_.current_side() == static_cast<int>(menu_handler_.gui_->viewing_side());
const bool is_your_turn = menu_handler_.pc_.current_side() == static_cast<int>(menu_handler_.gui_->viewing_team().side());
utils::string_map symbols;
symbols["side"] = std::to_string(side);

View file

@ -706,7 +706,7 @@ map_location mouse_handler::current_unit_attacks_from(const map_location& loc) c
}
// The selected unit must at least belong to the player currently controlling this client.
source_eligible &= source_unit->side() == gui_->viewing_side();
source_eligible &= source_unit->side() == gui_->viewing_team().side();
if(!source_eligible) {
return map_location();
}
@ -715,7 +715,7 @@ map_location mouse_handler::current_unit_attacks_from(const map_location& loc) c
// - If whiteboard is enabled, we allow planning attacks outside of player's turn
// - If whiteboard is disabled, it must be the turn of the player controlling this client
if(!wb_active) {
source_eligible &= gui_->viewing_side() == pc_.current_side();
source_eligible &= gui_->viewing_team().side() == pc_.current_side();
if(!source_eligible) {
return map_location();
}
@ -1128,7 +1128,7 @@ void mouse_handler::select_hex(const map_location& hex, const bool browse, const
gui().set_route(nullptr);
// Selection have impact only if we are not observing and it's our unit
if((!commands_disabled || pc_.get_whiteboard()->is_active()) && unit->side() == gui().viewing_side()) {
if((!commands_disabled || pc_.get_whiteboard()->is_active()) && unit->side() == gui().viewing_team().side()) {
if(!(browse || pc_.get_whiteboard()->unit_has_actions(&*unit))) {
sound::play_UI_sound("select-unit.wav");
@ -1257,7 +1257,7 @@ std::size_t mouse_handler::move_unit_along_route(const std::vector<map_location>
if(pc_.get_map().is_keep(steps.front())) {
unit_map::const_iterator const u = pc_.get_units().find(steps.front());
if(u && u->can_recruit() && u->side() == gui().viewing_side()
if(u && u->can_recruit() && u->side() == gui().viewing_team().side()
&& !pc_.get_whiteboard()->allow_leader_to_move(*u)) {
gui2::show_transient_message("",
_("You cannot move your leader away from the keep with some planned recruits or recalls left."));

View file

@ -363,7 +363,7 @@ void play_controller::fire_prestart()
void play_controller::refresh_objectives() const
{
const config cfg("side", gui_->viewing_side());
const config cfg("side", gui_->viewing_team().side());
gamestate().lua_kernel_->run_wml_action("show_objectives", vconfig(cfg),
game_events::queued_event("_from_interface", "", map_location(), map_location(), config()));
}

View file

@ -492,7 +492,7 @@ void playmp_controller::process_network_side_drop_impl(const config& side_drop_c
const int side_drop = side_drop_c["side_num"].to_int(0);
std::size_t index = side_drop -1;
player_type_changed_ |= side_drop == game_display::get_singleton()->playing_side();
player_type_changed_ |= side_drop == game_display::get_singleton()->playing_team().side();
if (index >= gamestate().board_.teams().size()) {
ERR_NW << "unknown side " << side_drop << " is dropping game";
@ -654,7 +654,7 @@ void playmp_controller::process_network_change_controller_impl(const config& cha
get_whiteboard()->on_change_controller(side,tm);
player_type_changed_ |= game_display::get_singleton()->playing_side() == side && (was_local || tm.is_local());
player_type_changed_ |= game_display::get_singleton()->playing_team().side() == side && (was_local || tm.is_local());
}
void playmp_controller::send_actions()

View file

@ -817,7 +817,7 @@ void playsingle_controller::update_viewing_player()
if(replay_controller_ && replay_controller_->is_controlling_view()) {
replay_controller_->update_viewing_player();
} else if(int side_num = find_viewing_side()) {
if(side_num != gui_->viewing_side() || gui_->show_everything()) {
if(side_num != gui_->viewing_team().side() || gui_->show_everything()) {
update_gui_to_player(side_num - 1);
}
}

View file

@ -148,7 +148,7 @@ static unit_const_ptr get_selected_unit_ptr(const reports::context& rc)
static config gray_inactive(const reports::context& rc, const std::string &str, const std::string& tooltip = "")
{
if ( rc.screen().viewing_side() == rc.screen().playing_side() )
if ( rc.screen().viewing_team().side() == rc.screen().playing_team().side() )
return text_report(str, tooltip);
return text_report(span_color(font::GRAY_COLOR) + str + naps, tooltip);
@ -672,7 +672,7 @@ static config unit_moves(const reports::context& rc, const unit* u, bool is_visi
std::set<terrain_movement> terrain_moves;
if (u->side() == rc.screen().playing_side()) {
if (u->side() == rc.screen().playing_team().side()) {
movement_frac = static_cast<double>(u->movement_left()) / std::max<int>(1, u->total_movement());
if (movement_frac > 1.0)
movement_frac = 1.0;
@ -781,7 +781,7 @@ static int attack_info(const reports::context& rc, const attack_type &at, config
};
{
auto ctx = at.specials_context(u.shared_from_this(), hex, u.side() == rc.screen().playing_side());
auto ctx = at.specials_context(u.shared_from_this(), hex, u.side() == rc.screen().playing_team().side());
int base_damage = at.damage();
int specials_damage = at.modified_damage();
int damage_multiplier = 100;
@ -979,7 +979,7 @@ static int attack_info(const reports::context& rc, const attack_type &at, config
{
//If we have a second unit, do the 2-unit specials_context
bool attacking = (u.side() == rc.screen().playing_side());
bool attacking = (u.side() == rc.screen().playing_team().side());
auto ctx = (sec_u == nullptr) ? at.specials_context_for_listing(attacking) :
at.specials_context(u.shared_from_this(), sec_u->shared_from_this(), hex, sec_u->get_location(), attacking, sec_u_weapon);
@ -1453,7 +1453,7 @@ REPORT_GENERATOR(gold, rc)
if (rc.wb())
fake_gold -= rc.wb()->get_spent_gold_for(viewing_team.side());
char const *end = naps;
if (viewing_team.side() != rc.screen().playing_side()) {
if (viewing_team.side() != rc.screen().playing_team().side()) {
str << span_color(font::GRAY_COLOR);
}
else if (fake_gold < 0) {
@ -1486,7 +1486,7 @@ REPORT_GENERATOR(villages, rc)
REPORT_GENERATOR(num_units, rc)
{
return gray_inactive(rc, std::to_string(rc.dc().side_units(rc.screen().viewing_side())), _("Units") + "\n\n" + _("The total number of units on your side."));
return gray_inactive(rc, std::to_string(rc.dc().side_units(rc.screen().viewing_team().side())), _("Units") + "\n\n" + _("The total number of units on your side."));
}
REPORT_GENERATOR(upkeep, rc)
@ -1511,7 +1511,7 @@ REPORT_GENERATOR(income, rc)
const team& viewing_team = rc.screen().viewing_team();
team_data td(rc.dc(), viewing_team);
char const *end = naps;
if (viewing_team.side() != rc.screen().playing_side()) {
if (viewing_team.side() != rc.screen().playing_team().side()) {
if (td.net_income < 0) {
td.net_income = - td.net_income;
str << span_color(font::GRAY_COLOR);
@ -1714,7 +1714,7 @@ REPORT_GENERATOR(side_playing, rc)
const team &active_team = rc.screen().playing_team();
std::string flag_icon = active_team.flag_icon();
std::string old_rgb = game_config::flag_rgb;
std::string new_rgb = team::get_side_color_id(rc.screen().playing_side());
std::string new_rgb = team::get_side_color_id(rc.screen().playing_team().side());
std::string mods = "~RC(" + old_rgb + ">" + new_rgb + ")";
if (flag_icon.empty())
flag_icon = game_config::images::flag_icon;
@ -1773,7 +1773,7 @@ REPORT_GENERATOR(report_countdown, rc)
std::ostringstream str;
sec = viewing_team.countdown_time() / 1000;
char const *end = naps;
if (viewing_team.side() != rc.screen().playing_side())
if (viewing_team.side() != rc.screen().playing_team().side())
str << span_color(font::GRAY_COLOR);
else if (sec < 60)
str << "<span foreground=\"#c80000\">";

View file

@ -243,7 +243,7 @@ namespace {
static int intf_get_viewing_side(lua_State *L)
{
if(const display* disp = display::get_singleton()) {
lua_pushinteger(L, disp->viewing_side());
lua_pushinteger(L, disp->viewing_team().side());
lua_pushboolean(L, disp->show_everything());
return 2;
}

View file

@ -230,7 +230,7 @@ void unit_mover::replace_temporary(unit_ptr u)
u->set_hidden(true);
// Update cached data.
is_enemy_ = resources::gameboard->get_team(u->side()).is_enemy(disp_->viewing_side());
is_enemy_ = resources::gameboard->get_team(u->side()).is_enemy(disp_->viewing_team().side());
}

View file

@ -687,7 +687,7 @@ void manager::create_temp_move()
if (!temp_moved_unit) temp_moved_unit =
future_visible_unit(resources::controller->get_mouse_handler_base().get_last_hex(), viewer_side());
if (!temp_moved_unit) return;
if (temp_moved_unit->side() != display::get_singleton()->viewing_side()) return;
if (temp_moved_unit->side() != display::get_singleton()->viewing_team().side()) return;
/*
* DONE CHECKING PRE-CONDITIONS, CREATE THE TEMP MOVE
@ -878,7 +878,7 @@ bool manager::save_recruit(const std::string& name, int side_num, const map_loca
bool created_planned_recruit = false;
if (active_ && !executing_actions_ && !resources::controller->is_linger_mode()) {
if (side_num != display::get_singleton()->viewing_side())
if (side_num != display::get_singleton()->viewing_team().side())
{
LOG_WB <<"manager::save_recruit called for a different side than viewing side.";
created_planned_recruit = false;
@ -907,7 +907,7 @@ bool manager::save_recall(const unit& unit, int side_num, const map_location& re
if (active_ && !executing_actions_ && !resources::controller->is_linger_mode())
{
if (side_num != display::get_singleton()->viewing_side())
if (side_num != display::get_singleton()->viewing_team().side())
{
LOG_WB <<"manager::save_recall called for a different side than viewing side.";
created_planned_recall = false;

View file

@ -545,7 +545,7 @@ int move::calculate_moves_left(unit& u)
// @todo: find a better treatment of movement points when defining moves out-of-turn
if(u.movement_left() - route_->move_cost < 0
&& resources::controller->current_side() == display::get_singleton()->viewing_side()) {
&& resources::controller->current_side() == display::get_singleton()->viewing_team().side()) {
WRN_WB << shared_from_this() << " defined with insufficient movement left.";
}

View file

@ -36,7 +36,7 @@
namespace wb {
int viewer_side()
{
return display::get_singleton()->viewing_side();
return display::get_singleton()->viewing_team().side();
}
side_actions_ptr viewer_actions()