Skip to content

Commit

Permalink
Merge pull request #39785 from olanti-p/target_ui-as-class
Browse files Browse the repository at this point in the history
Refactor aiming UI and its target selection / aim preservation
  • Loading branch information
ifreund authored May 3, 2020
2 parents 8cea0fc + 42a145d commit 1d61507
Show file tree
Hide file tree
Showing 11 changed files with 1,746 additions and 1,460 deletions.
5 changes: 2 additions & 3 deletions src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4704,14 +4704,13 @@ void activity_handlers::spellcasting_finish( player_activity *act, player *p )
const bool no_mana = act->get_value( 2 ) == 0;

// choose target for spell (if the spell has a range > 0)

target_handler th;
tripoint target = p->pos();
bool target_is_valid = false;
if( spell_being_cast.range() > 0 && !spell_being_cast.is_valid_target( target_none ) &&
!spell_being_cast.has_flag( RANDOM_TARGET ) ) {
do {
std::vector<tripoint> trajectory = th.target_ui( spell_being_cast, no_fail, no_mana );
std::vector<tripoint> trajectory = target_handler::mode_spell( *p, spell_being_cast, no_fail,
no_mana );
if( !trajectory.empty() ) {
target = trajectory.back();
target_is_valid = spell_being_cast.is_valid_target( *p, target );
Expand Down
31 changes: 10 additions & 21 deletions src/avatar_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -901,13 +901,10 @@ void avatar_action::aim_do_turn( avatar &you, map &m )
}
}

int range = gun.target->gun_range( &you );
const itype *ammo = gun->ammo_data();

g->temp_exit_fullscreen();
m.draw( g->w_terrain, you.pos() );
std::vector<tripoint> trajectory = target_handler().target_ui( you, TARGET_MODE_FIRE, weapon, range,
ammo );
bool reload_requested;
target_handler::trajectory trajectory = target_handler::mode_fire( you, *weapon, reload_requested );

//may be changed in target_ui
gun = weapon->gun_current_mode();
Expand All @@ -922,6 +919,11 @@ void avatar_action::aim_do_turn( avatar &you, map &m )
you.moves = previous_moves;
}
g->reenter_fullscreen();

if( reload_requested ) {
// Reload the gun / select different arrows
g->reload_wielded( true );
}
return;
}
// Recenter our view
Expand Down Expand Up @@ -982,18 +984,9 @@ void avatar_action::fire_turret_manual( avatar &you, map &m, turret_data &turret
return;
}

item *turret_base = &*turret.base();

g->temp_exit_fullscreen();
g->m.draw( g->w_terrain, you.pos() );
std::vector<tripoint> trajectory = target_handler().target_ui(
you,
TARGET_MODE_TURRET_MANUAL,
turret_base,
turret.range(),
turret.ammo_data(),
&turret
);
target_handler::trajectory trajectory = target_handler::mode_turret_manual( you, turret );

if( !trajectory.empty() ) {
// Recenter our view
Expand Down Expand Up @@ -1187,12 +1180,8 @@ void avatar_action::plthrow( avatar &you, item_location loc,
g->temp_exit_fullscreen();
g->m.draw( g->w_terrain, you.pos() );

const target_mode throwing_target_mode = blind_throw_from_pos ? TARGET_MODE_THROW_BLIND :
TARGET_MODE_THROW;
// target_ui() sets x and y, or returns empty vector if we canceled (by pressing Esc)
std::vector<tripoint> trajectory = target_handler().target_ui( you, throwing_target_mode,
&you.weapon,
range );
target_handler::trajectory trajectory = target_handler::mode_throw( you, you.weapon,
blind_throw_from_pos.has_value() );

// If we previously shifted our position, put ourselves back now that we've picked our target.
if( blind_throw_from_pos ) {
Expand Down
8 changes: 5 additions & 3 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7875,6 +7875,7 @@ game::vmenu_ret game::list_monsters( const std::vector<Creature *> &monster_list
} else if( action == "fire" ) {
if( cCurMon != nullptr && rl_dist( u.pos(), cCurMon->pos() ) <= max_gun_range ) {
u.last_target = shared_from( *cCurMon );
u.recoil = MAX_RECOIL;
u.view_offset = stored_view_offset;
trail_start = trail_end = cata::nullopt;
invalidate_main_ui_adaptor();
Expand Down Expand Up @@ -8530,14 +8531,14 @@ void game::reload_item()
reload( item_loc );
}

void game::reload_wielded()
void game::reload_wielded( bool prompt )
{
if( u.weapon.is_null() || !u.weapon.is_reloadable() ) {
add_msg( _( "You aren't holding something you can reload." ) );
return;
}
item_location item_loc = item_location( u, &u.weapon );
reload( item_loc );
reload( item_loc, prompt );
}

void game::reload_weapon( bool try_everything )
Expand Down Expand Up @@ -9096,8 +9097,9 @@ bool game::walk_move( const tripoint &dest_loc )
u.burn_move_stamina( 0.50 * ( previous_moves - u.moves ) );
}
}
// Max out recoil
// Max out recoil & reset aim point
u.recoil = MAX_RECOIL;
u.last_target_pos = cata::nullopt;

// Print a message if movement is slow
const int mcost_to = m.move_cost( dest_loc ); //calculate this _after_ calling grabbed_move
Expand Down
3 changes: 1 addition & 2 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ class game
friend class editmap;
friend class advanced_inventory;
friend class main_menu;
friend class target_handler;
public:
game();
~game();
Expand Down Expand Up @@ -787,7 +786,7 @@ class game
void reload( item_location &loc, bool prompt = false, bool empty = true );
public:
void reload_item(); // Reload an item
void reload_wielded();
void reload_wielded( bool prompt = false );
void reload_weapon( bool try_everything = true ); // Reload a wielded gun/tool 'r'
// Places the player at the specified point; hurts feet, lists items etc.
point place_player( const tripoint &dest );
Expand Down
35 changes: 10 additions & 25 deletions src/handle_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1279,17 +1279,16 @@ static void read()
}
}

// Perform a reach attach
// range - the range of the current weapon.
// u - player
static void reach_attack( int range, player &u )
// Perform a reach attach using wielded weapon
static void reach_attack( player &u )
{
g->temp_exit_fullscreen();
g->m.draw( g->w_terrain, u.pos() );
std::vector<tripoint> trajectory = target_handler().target_ui( u, TARGET_MODE_REACH, &u.weapon,
range );
if( !trajectory.empty() ) {
u.reach_attack( trajectory.back() );

target_handler::trajectory traj = target_handler::mode_reach( u, u.weapon );

if( !traj.empty() ) {
u.reach_attack( traj.back() );
}
g->draw_ter();
wrefresh( g->w_terrain );
Expand Down Expand Up @@ -1349,31 +1348,17 @@ static void fire()

if( u.weapon.is_gun() && !u.weapon.gun_current_mode().melee() ) {
avatar_action::fire_wielded_weapon( g->u, g->m );
} else if( u.weapon.has_flag( flag_REACH_ATTACK ) ) {
int range = u.weapon.has_flag( flag_REACH3 ) ? 3 : 2;
if( u.has_effect( effect_relax_gas ) ) {
if( one_in( 8 ) ) {
add_msg( m_good, _( "Your willpower asserts itself, and so do you!" ) );
reach_attack( range, u );
} else {
u.moves -= rng( 2, 8 ) * 10;
add_msg( m_bad, _( "You're too pacified to strike anything…" ) );
}
} else {
reach_attack( range, u );
}
} else if( u.weapon.is_gun() && u.weapon.gun_current_mode().flags.count( flag_REACH_ATTACK ) ) {
int range = u.weapon.gun_current_mode().qty;
} else if( u.weapon.current_reach_range( u ) > 1 ) {
if( u.has_effect( effect_relax_gas ) ) {
if( one_in( 8 ) ) {
add_msg( m_good, _( "Your willpower asserts itself, and so do you!" ) );
reach_attack( range, u );
reach_attack( u );
} else {
u.moves -= rng( 2, 8 ) * 10;
add_msg( m_bad, _( "You're too pacified to strike anything…" ) );
}
} else {
reach_attack( range, u );
reach_attack( u );
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4904,6 +4904,24 @@ int item::reach_range( const Character &guy ) const
return res;
}

int item::current_reach_range( const Character &guy ) const
{
int res = 1;

if( has_flag( flag_REACH_ATTACK ) ) {
res = has_flag( flag_REACH3 ) ? 3 : 2;
}

if( is_gun() && !is_gunmod() ) {
gun_mode gun = gun_current_mode();
if( !( guy.is_npc() && gun.flags.count( "NPC_AVOID" ) ) && gun.melee() ) {
res = std::max( res, gun.qty );
}
}

return res;
}

void item::unset_flags()
{
item_tags.clear();
Expand Down
13 changes: 12 additions & 1 deletion src/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -599,9 +599,20 @@ class item : public visitable<item>
skill_id melee_skill() const;
/*@}*/

/** Max range weapon usable for melee attack accounting for player/NPC abilities */
/*
* Max range of melee attack this weapon can be used for.
* Accounts for character's abilities and installed gun mods.
* Guaranteed to be at least 1
*/
int reach_range( const Character &guy ) const;

/*
* Max range of melee attack this weapon can be used for in its current state.
* Accounts for character's abilities and installed gun mods.
* Guaranteed to be at least 1
*/
int current_reach_range( const Character &guy ) const;

/**
* Sets time until activation for an item that will self-activate in the future.
**/
Expand Down
2 changes: 2 additions & 0 deletions src/melee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,8 @@ void player::reach_attack( const tripoint &p )
int target_size = critter != nullptr ? critter->get_size() : 2;
// Reset last target pos
last_target_pos = cata::nullopt;
// Max out recoil
recoil = MAX_RECOIL;

int move_cost = attack_speed( weapon );
int skill = std::min( 10, get_skill_level( skill_stabbing ) );
Expand Down
Loading

0 comments on commit 1d61507

Please sign in to comment.