Units/Display: minor code cleanup

This commit is contained in:
Charles Dang 2018-04-20 15:28:42 +11:00
parent c7ad8153c7
commit 882aba3349

View file

@ -34,12 +34,16 @@
#define LOG_DP LOG_STREAM(info, display)
namespace unit_display
{
namespace
{
/**
* Returns a string whose first line is @a number, centered over a second line,
* which consists of @a text.
* If the number is 0, the first line is suppressed.
*/
static std::string number_and_text(int number, const std::string& text)
std::string number_and_text(int number, const std::string& text)
{
// Simple case.
if(number == 0) {
@ -65,7 +69,7 @@ static std::string number_and_text(int number, const std::string& text)
* @param temp_unit The unit to animate (historically, a temporary unit).
* @param disp The game display. Assumed neither locked nor faked.
*/
static void teleport_unit_between(const map_location& a, const map_location& b, unit& temp_unit, display& disp)
void teleport_unit_between(const map_location& a, const map_location& b, unit& temp_unit, display& disp)
{
if(disp.fogged(a) && disp.fogged(b)) {
return;
@ -115,7 +119,7 @@ static void teleport_unit_between(const map_location& a, const map_location& b,
* @returns The animation potential until this animation will finish.
* INT_MIN indicates that no animation is pending.
*/
static int move_unit_between(const map_location& a,
int move_unit_between(const map_location& a,
const map_location& b,
unit_ptr temp_unit,
unsigned int step_num,
@ -164,8 +168,13 @@ static int move_unit_between(const map_location& a,
return target_time;
}
namespace unit_display
bool do_not_show_anims(display* disp)
{
return !disp || disp->video().update_locked() || disp->video().faked();
}
} // end anon namespace
/**
* The path must remain unchanged for the life of this object.
*/
@ -447,8 +456,7 @@ void unit_mover::finish(unit_ptr u, map_location::DIRECTION dir)
u->set_hidden(was_hidden_);
temp_unit_ptr_->set_hidden(true);
events::mouse_handler* mousehandler = events::mouse_handler::get_singleton();
if(mousehandler) {
if(events::mouse_handler* mousehandler = events::mouse_handler::get_singleton()) {
mousehandler->invalidate_reachmap();
}
} else {
@ -496,9 +504,7 @@ void unit_draw_weapon(const map_location& loc,
unit* defender)
{
display* disp = display::get_singleton();
if(!disp || disp->video().update_locked() || disp->video().faked() || disp->fogged(loc)
|| preferences::show_combat() == false
) {
if(do_not_show_anims(disp) || disp->fogged(loc) || !preferences::show_combat()) {
return;
}
@ -524,9 +530,7 @@ void unit_sheath_weapon(const map_location& primary_loc,
unit* secondary_unit)
{
display* disp = display::get_singleton();
if(!disp || disp->video().update_locked() || disp->video().faked() || disp->fogged(primary_loc)
|| preferences::show_combat() == false
) {
if(do_not_show_anims(disp) || disp->fogged(primary_loc) || !preferences::show_combat()) {
return;
}
@ -565,9 +569,7 @@ void unit_die(const map_location& loc,
unit* winner)
{
display* disp = display::get_singleton();
if(!disp || disp->video().update_locked() || disp->video().faked() || disp->fogged(loc)
|| preferences::show_combat() == false
) {
if(do_not_show_anims(disp) || disp->fogged(loc) || !preferences::show_combat()) {
return;
}
@ -585,9 +587,8 @@ void unit_die(const map_location& loc,
animator.wait_for_end();
reset_helpers(winner, &loser);
events::mouse_handler* mousehandler = events::mouse_handler::get_singleton();
if(mousehandler) {
if(events::mouse_handler* mousehandler = events::mouse_handler::get_singleton()) {
mousehandler->invalidate_reachmap();
}
}
@ -605,9 +606,7 @@ void unit_attack(display* disp,
const std::string& att_text,
const std::vector<std::string>* extra_hit_sounds)
{
if(!disp || disp->video().update_locked() || disp->video().faked() || (disp->fogged(a) && disp->fogged(b))
|| preferences::show_combat() == false
) {
if(do_not_show_anims(disp) || (disp->fogged(a) && disp->fogged(b)) || !preferences::show_combat()) {
return;
}
@ -633,10 +632,6 @@ void unit_attack(display* disp,
def->set_facing(b.get_relative_dir(a));
defender.set_facing(b.get_relative_dir(a));
unit_animator animator;
unit_ability_list leaders = attacker.get_abilities("leadership");
unit_ability_list helpers = defender.get_abilities("resistance");
std::string text = number_and_text(damage, hit_text);
std::string text_2 = number_and_text(std::abs(drain_amount), att_text);
@ -649,6 +644,8 @@ void unit_attack(display* disp,
hit_type = unit_animation::hit_type::MISS;
}
unit_animator animator;
animator.add_animation(&attacker, "attack", att->get_location(), def->get_location(), damage, true, text_2,
(drain_amount >= 0) ? color_t(0, 255, 0) : color_t(255, 0, 0), hit_type, attack.shared_from_this(),
secondary_attack, swing);
@ -659,7 +656,7 @@ void unit_attack(display* disp,
animator.add_animation(&defender, defender_anim, def->get_location(), true, text, {255, 0, 0});
for(const unit_ability& ability : leaders) {
for(const unit_ability& ability : attacker.get_abilities("leadership")) {
if(ability.second == a) {
continue;
}
@ -676,7 +673,7 @@ void unit_attack(display* disp,
hit_type, attack.shared_from_this(), secondary_attack, swing);
}
for(const unit_ability& ability : helpers) {
for(const unit_ability& ability : defender.get_abilities("resistance")) {
if(ability.second == a) {
continue;
}
@ -738,8 +735,7 @@ void reset_helpers(const unit* attacker, const unit* defender)
const unit_map& units = disp->get_units();
if(attacker) {
unit_ability_list leaders = attacker->get_abilities("leadership");
for(const unit_ability& ability : leaders) {
for(const unit_ability& ability : attacker->get_abilities("leadership")) {
unit_map::const_iterator leader = units.find(ability.second);
assert(leader != units.end());
leader->anim_comp().set_standing();
@ -747,8 +743,7 @@ void reset_helpers(const unit* attacker, const unit* defender)
}
if(defender) {
unit_ability_list helpers = defender->get_abilities("resistance");
for(const unit_ability& ability : helpers) {
for(const unit_ability& ability : defender->get_abilities("resistance")) {
unit_map::const_iterator helper = units.find(ability.second);
assert(helper != units.end());
helper->anim_comp().set_standing();
@ -759,7 +754,7 @@ void reset_helpers(const unit* attacker, const unit* defender)
void unit_recruited(const map_location& loc, const map_location& leader_loc)
{
game_display* disp = game_display::get_singleton();
if(!disp || disp->video().update_locked() || disp->video().faked() || disp->fogged(loc)) {
if(do_not_show_anims(disp) || disp->fogged(loc)) {
return;
}
@ -798,7 +793,7 @@ void unit_healing(unit& healed, const std::vector<unit*>& healers, int healing,
game_display* disp = game_display::get_singleton();
const map_location& healed_loc = healed.get_location();
if(!disp || disp->video().update_locked() || disp->video().faked() || disp->fogged(healed_loc)) {
if(do_not_show_anims(disp) || disp->fogged(healed_loc)) {
return;
}