Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor aiming UI and its target selection / aim preservation #39785

Merged
merged 48 commits into from
May 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
dd72c80
Use separate functions for modes & derive relevant info from arguments
olanti-p Apr 17, 2020
d8bfb43
Pass arguments as class members
olanti-p Apr 17, 2020
949a0f5
Remove duplicate code for window creation, input context setup, setti…
olanti-p Apr 17, 2020
6a0b030
Deduplicate checks fired when npc/monster has been selected as target
olanti-p Apr 17, 2020
3529ad7
Consolidate movement of aim point and center of view
olanti-p Apr 18, 2020
7965e6d
Cache critter under cursor; deduplicate 'set_as_target';
olanti-p Apr 18, 2020
5764f04
Deduplicate and refine 'confirm_non_enemy_target'
olanti-p Apr 18, 2020
e5e1475
Deduplicate and refine target cycling
olanti-p Apr 18, 2020
74cbe45
Extract some variables used by FIRE and SPELL modes
olanti-p Apr 18, 2020
64e9345
Refactor how penalty from moving aim point applies to current aim
olanti-p Apr 18, 2020
b677379
Deduplicate event loop
olanti-p Apr 19, 2020
1625c61
Deduplicate terrain drawing; fix cursor-related visual bug.
olanti-p Apr 19, 2020
fc74251
Refactor drawing of window base and list of controls
olanti-p Apr 19, 2020
304a542
Break up and reorganize rendering of panels inside aiming window
olanti-p Apr 19, 2020
6516d41
Re-implement reloading guns from aiming UI
olanti-p Apr 20, 2020
7a0d7fb
Handle empty gun modes and gun modes with different range
olanti-p Apr 20, 2020
b67a646
Some minor improvements & cleanup
olanti-p Apr 20, 2020
6a0e7b7
Refactored display of vehicle turrets in range;
olanti-p Apr 20, 2020
69c2ec9
Keep 'snap-to-target' state across turns
olanti-p Apr 20, 2020
e5dee87
Refactor target selection; save aim when killing target
olanti-p Apr 20, 2020
7bb5020
Remove old comments
olanti-p Apr 20, 2020
46f2157
Extract window creation and input context setup into function
olanti-p Apr 20, 2020
eca972b
Replace enum TARGET_MODE_* with enum class TargetMode
olanti-p Apr 20, 2020
f394840
Show info about creatures only if player can see them
olanti-p Apr 20, 2020
117918c
Merge branch 'master' into target_ui-as-class
olanti-p Apr 20, 2020
dc93ba3
Update list of targets when range changes
olanti-p Apr 21, 2020
82342c3
Prevent an exploit when firing from monster list
olanti-p Apr 21, 2020
5907ca0
Remove debug output
olanti-p Apr 21, 2020
ca0ea7c
Disable switching aim mode when you can't aim/shoot
olanti-p Apr 21, 2020
80097f3
For compact version, draw over bottom border
olanti-p Apr 21, 2020
a6d5eb2
Invalidate aim point when moving
olanti-p Apr 21, 2020
0a61cb2
Fix player's sprite disappearing when changing Z levels
olanti-p Apr 21, 2020
3e8d092
Appease clang-tidy
olanti-p Apr 21, 2020
b97c960
Merge branch 'master' into target_ui-as-class
olanti-p Apr 21, 2020
1bd24b0
Fix incorrect usage of ui_adaptor::disable_uis_below
olanti-p Apr 22, 2020
9acbb8b
Update src/ranged.cpp
olanti-p Apr 22, 2020
53a4cf4
When extremely short on space, collapse blank lines reserved for target.
olanti-p Apr 22, 2020
661a841
Merge commit 'd1c39eb0d81c202e7a7b69d57988d2c2d9242fa5' into target_u…
olanti-p Apr 30, 2020
1197d21
Migrate '[?] show help' from master
olanti-p Apr 30, 2020
584f058
Dynamically shrink list of controls
olanti-p May 1, 2020
7a6b3d7
Migrate the rest of the changes from master
olanti-p May 1, 2020
327846c
Merge branch 'master' into target_ui-as-class
olanti-p May 1, 2020
7ec443b
Some positioning-related fixes
olanti-p May 1, 2020
d452cac
Remove a duplicate check
olanti-p May 1, 2020
dc64250
Better comments
olanti-p May 1, 2020
5ec7c37
Don't re-confirm attack when aim-and-fire takes multiple turns
olanti-p May 1, 2020
a3073c6
Fix regression related to aim point not snapping to nearest target wh…
olanti-p May 1, 2020
42a145d
Appease clang-tidy
olanti-p May 1, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Pass arguments as class members
  • Loading branch information
olanti-p committed Apr 18, 2020
commit d8bfb43bd36c9a9bf77c3d12272a55f5bb8982a7
2 changes: 1 addition & 1 deletion src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4780,7 +4780,7 @@ void activity_handlers::spellcasting_finish( player_activity *act, player *p )
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 = target_handler::mode_spell( &spell_being_cast, no_fail,
std::vector<tripoint> trajectory = target_handler::mode_spell( *p, &spell_being_cast, no_fail,
no_mana );
if( !trajectory.empty() ) {
target = trajectory.back();
Expand Down
131 changes: 85 additions & 46 deletions src/ranged.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,53 +113,81 @@ enum target_mode : int {
TARGET_MODE_SPELL
};

// TODO: Remove these forward declarations when done refactoring
namespace target_handler
class target_ui
{
std::vector<tripoint> target_ui( player &pc, target_mode mode,
item *relevant, int range,
const itype *ammo = nullptr,
turret_data *turret = nullptr,
vehicle *veh = nullptr,
const std::vector<vehicle_part *> &vturrets = std::vector<vehicle_part *>()
);
std::vector<tripoint> target_ui( spell_id sp, bool no_fail = false,
bool no_mana = false );
std::vector<tripoint> target_ui( spell &casting, bool no_fail = false,
bool no_mana = false );
}
public:
// Interface mode
target_mode mode = TARGET_MODE_FIRE;
// Weapon being fired/thrown
item *relevant = nullptr;
// Cached selection range from player's position
int range = 0;
// Cached current ammo to display
const itype *ammo = nullptr;
// Turret being manually fired
turret_data *turret = nullptr;
// Turrets being fired (via vehicle controls)
const std::vector<vehicle_part *> *vturrets = nullptr;
// Vehicle that turrets belong to
vehicle *veh = nullptr;
// Spell being cast
spell *casting = nullptr;
// Spell cannot fail
bool no_fail = false;
// Spell does not require mana
bool no_mana = false;

target_handler::trajectory run( player &pc );

private:
// TODO: break down these functions into methods
std::vector<tripoint> run_normal_ui_old( player &pc );
std::vector<tripoint> run_spell_ui_old( player &pc );
};

target_handler::trajectory target_handler::mode_fire( player &pc, item *weapon )
{
target_ui ui = target_ui();
ui.mode = TARGET_MODE_FIRE;
ui.relevant = weapon;
gun_mode gun = weapon->gun_current_mode();
int range = gun.target->gun_range( &pc );
const itype *ammo = gun->ammo_data();
ui.range = gun.target->gun_range( &pc );
ui.ammo = gun->ammo_data();

return target_ui( pc, TARGET_MODE_FIRE, weapon, range, ammo );
return ui.run( pc );
}

target_handler::trajectory target_handler::mode_throw( player &pc, item *relevant,
bool blind_throwing )
{
target_mode mode = blind_throwing ? TARGET_MODE_THROW_BLIND : TARGET_MODE_THROW;
int range = pc.throw_range( *relevant );
target_ui ui = target_ui();
ui.mode = blind_throwing ? TARGET_MODE_THROW_BLIND : TARGET_MODE_THROW;
ui.relevant = relevant;
ui.range = pc.throw_range( *relevant );

return target_ui( pc, mode, relevant, range );
return ui.run( pc );
}

target_handler::trajectory target_handler::mode_reach( player &pc, item *weapon )
{
int range = weapon->current_reach_range( pc );
return target_ui( pc, TARGET_MODE_REACH, weapon, range );
target_ui ui = target_ui();
ui.mode = TARGET_MODE_REACH;
ui.relevant = weapon;
ui.range = weapon->current_reach_range( pc );

return ui.run( pc );
}

target_handler::trajectory target_handler::mode_turret_manual( player &pc, turret_data *turret )
{
item *turret_base = &*turret->base();
int range = turret->range();
const itype *ammo = turret->ammo_data();

return target_ui( pc, TARGET_MODE_TURRET_MANUAL, turret_base, range, ammo, turret );
target_ui ui = target_ui();
ui.mode = TARGET_MODE_TURRET_MANUAL;
ui.turret = turret;
ui.relevant = &*turret->base();
ui.range = turret->range();
ui.ammo = turret->ammo_data();

return ui.run( pc );
}

target_handler::trajectory target_handler::mode_turrets( player &pc, vehicle *veh,
Expand All @@ -181,19 +209,31 @@ target_handler::trajectory target_handler::mode_turrets( player &pc, vehicle *ve
range_total = std::max( range_total, res );
}

return target_ui( pc, TARGET_MODE_TURRET, nullptr, range_total, nullptr, nullptr, veh, *turrets );
target_ui ui = target_ui();
ui.mode = TARGET_MODE_TURRET;
ui.veh = veh;
ui.vturrets = turrets;
ui.range = range_total;

return ui.run( pc );
}

target_handler::trajectory target_handler::mode_spell( spell *casting, bool no_fail,
target_handler::trajectory target_handler::mode_spell( player &pc, spell *casting, bool no_fail,
bool no_mana )
{
return target_ui( *casting, no_fail, no_mana );
target_ui ui = target_ui();
ui.mode = TARGET_MODE_SPELL;
ui.casting = casting;
ui.no_fail = no_fail;
ui.no_mana = no_mana;

return ui.run( pc );
}

target_handler::trajectory target_handler::mode_spell( spell_id sp, bool no_fail,
target_handler::trajectory target_handler::mode_spell( player &pc, spell_id sp, bool no_fail,
bool no_mana )
{
return target_ui( sp, no_fail, no_mana );
return mode_spell( pc, &g->u.magic.get_spell( sp ), no_fail, no_mana );
}

bool targeting_data::is_valid() const
Expand Down Expand Up @@ -1437,9 +1477,7 @@ static void update_targets( player &pc, int range, std::vector<Creature *> &targ
}

// TODO: Shunt redundant drawing code elsewhere
std::vector<tripoint> target_handler::target_ui( player &pc, target_mode mode,
item *relevant, int range, const itype *ammo, turret_data *turret, vehicle *veh,
const std::vector<vehicle_part *> &vturrets )
std::vector<tripoint> target_ui::run_normal_ui_old( player &pc )
{
// TODO: this should return a reference to a static vector which is cleared on each call.
static const std::vector<tripoint> empty_result{};
Expand Down Expand Up @@ -1710,7 +1748,7 @@ std::vector<tripoint> target_handler::target_ui( player &pc, target_mode mode,
predicted_delay );
}
} else if( mode == TARGET_MODE_TURRET ) {
list_turrets_in_range( veh, vturrets, w_target, line_number, dst );
list_turrets_in_range( veh, *vturrets, w_target, line_number, dst );
} else if( mode == TARGET_MODE_THROW && relevant ) {
draw_throw_aim( pc, w_target, line_number, ctxt, *relevant, dst, false );
} else if( mode == TARGET_MODE_THROW_BLIND && relevant ) {
Expand Down Expand Up @@ -2027,17 +2065,9 @@ std::vector<tripoint> target_handler::target_ui( player &pc, target_mode mode,
return ret;
}

// magic mod
std::vector<tripoint> target_handler::target_ui( spell_id sp, const bool no_fail,
const bool no_mana )
std::vector<tripoint> target_ui::run_spell_ui_old( player &pc )
{
return target_ui( g->u.magic.get_spell( sp ), no_fail, no_mana );
}
// does not have a targeting mode because we know this is the spellcasting version of this function
std::vector<tripoint> target_handler::target_ui( spell &casting, const bool no_fail,
const bool no_mana )
{
player &pc = g->u;
spell &casting = *this->casting; // TODO: make code use pointer
if( !no_mana && !casting.can_cast( pc ) ) {
pc.add_msg_if_player( m_bad, _( "You don't have enough %s to cast this spell" ),
casting.energy_string() );
Expand Down Expand Up @@ -2757,3 +2787,12 @@ double player::gun_value( const item &weap, int ammo ) const
capacity_factor );
return std::max( 0.0, gun_value );
}

target_handler::trajectory target_ui::run( player &pc )
{
if( mode == TARGET_MODE_SPELL ) {
return run_spell_ui_old( pc );
} else {
return run_normal_ui_old( pc );
}
}
4 changes: 2 additions & 2 deletions src/ranged.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ trajectory mode_turret_manual( player &pc, turret_data *turret );
trajectory mode_turrets( player &pc, vehicle *veh, const std::vector<vehicle_part *> *turrets );

/** Casting a spell */
trajectory mode_spell( spell *casting, bool no_fail, bool no_mana );
trajectory mode_spell( spell_id sp, bool no_fail, bool no_mana );
trajectory mode_spell( player &pc, spell *casting, bool no_fail, bool no_mana );
trajectory mode_spell( player &pc, spell_id sp, bool no_fail, bool no_mana );
}

int range_with_even_chance_of_good_hit( int dispersion );
Expand Down