Skip to content

Commit

Permalink
Merge pull request #33351 from jbytheway/point_in_game_line_map
Browse files Browse the repository at this point in the history
Use point in game, line, map APIs
  • Loading branch information
ZhilkinSerg authored Aug 19, 2019
2 parents 9b884d0 + e85578b commit c9e4519
Show file tree
Hide file tree
Showing 30 changed files with 360 additions and 391 deletions.
2 changes: 1 addition & 1 deletion src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1968,7 +1968,7 @@ void activity_handlers::vehicle_finish( player_activity *act, player *p )
g->refresh_all();
// TODO: Z (and also where the activity is queued)
// Or not, because the vehicle coordinates are dropped anyway
g->exam_vehicle( vp->vehicle(), act->values[ 2 ], act->values[ 3 ] );
g->exam_vehicle( vp->vehicle(), point( act->values[ 2 ], act->values[ 3 ] ) );
return;
} else {
dbg( D_ERROR ) << "game:process_activity: ACT_VEHICLE: vehicle not found";
Expand Down
2 changes: 1 addition & 1 deletion src/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ void draw_sct_curses( game &g )
const int dy = off.y + text.getPosY();
const int dx = off.x + text.getPosX();

if( !is_valid_in_w_terrain( dx, dy ) ) {
if( !is_valid_in_w_terrain( point( dx, dy ) ) ) {
continue;
}

Expand Down
5 changes: 2 additions & 3 deletions src/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -777,14 +777,13 @@ void Creature::deal_projectile_attack( Creature *source, dealt_projectile_attack
if( source->is_player() ) {
//player hits monster ranged
SCT.add( posx(), posy(),
direction_from( 0, 0, posx() - source->posx(), posy() - source->posy() ),
direction_from( point_zero, point( posx() - source->posx(), posy() - source->posy() ) ),
get_hp_bar( dealt_dam.total_damage(), get_hp_max(), true ).first,
m_good, message, gmtSCTcolor );

if( get_hp() > 0 ) {
SCT.add( posx(), posy(),
direction_from( 0, 0, posx() - source->posx(),
posy() - source->posy() ),
direction_from( point_zero, point( posx() - source->posx(), posy() - source->posy() ) ),
get_hp_bar( get_hp(), get_hp_max(), true ).first, m_good,
//~ "hit points", used in scrolling combat text
_( "hp" ), m_neutral, "hp" );
Expand Down
4 changes: 2 additions & 2 deletions src/drawing_primitives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void draw_rough_circle( std::function<void( const point & )>set, const point &p,
{
for( int i = p.x - rad; i <= p.x + rad; i++ ) {
for( int j = p.y - rad; j <= p.y + rad; j++ ) {
if( trig_dist( p.x, p.y, i, j ) + rng( 0, 3 ) <= rad ) {
if( trig_dist( p, point( i, j ) ) + rng( 0, 3 ) <= rad ) {
set( point( i, j ) );
}
}
Expand All @@ -58,7 +58,7 @@ void draw_circle( std::function<void( const point & )>set, const point &p, int r
{
for( int i = p.x - rad; i <= p.x + rad; i++ ) {
for( int j = p.y - rad; j <= p.y + rad; j++ ) {
if( trig_dist( p.x, p.y, i, j ) <= rad ) {
if( trig_dist( p, point( i, j ) ) <= rad ) {
set( point( i, j ) );
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/faction_camp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1497,7 +1497,7 @@ void basecamp::start_cut_logs()
sample_npc.set_fake( true );
int tree_est = om_cutdown_trees_est( forest, 50 );
int tree_young_est = om_harvest_ter_est( sample_npc, forest, ter_id( "t_tree_young" ), 50 );
int dist = rl_dist( forest.x, forest.y, omt_pos.x, omt_pos.y );
int dist = rl_dist( forest.xy(), omt_pos.xy() );
//Very roughly what the player does + 6 hours for prep, clean up, breaks
time_duration chop_time = 6_hours + 1_hours * tree_est + 7_minutes * tree_young_est;
//Generous to believe the NPC can move ~ 2 logs or ~8 heavy sticks (3 per young tree?)
Expand Down Expand Up @@ -1554,7 +1554,7 @@ void basecamp::start_clearcut()
sample_npc.set_fake( true );
int tree_est = om_cutdown_trees_est( forest, 95 );
int tree_young_est = om_harvest_ter_est( sample_npc, forest, ter_id( "t_tree_young" ), 95 );
int dist = rl_dist( forest.x, forest.y, omt_pos.x, omt_pos.y );
int dist = rl_dist( forest.xy(), omt_pos.xy() );
//Very roughly what the player does + 6 hours for prep, clean up, breaks
time_duration chop_time = 6_hours + 1_hours * tree_est + 7_minutes * tree_young_est;
time_duration travel_time = companion_travel_time_calc( forest, omt_pos, 0_minutes, 2 );
Expand Down Expand Up @@ -1588,7 +1588,7 @@ void basecamp::start_setup_hide_site()
popup( _( "Forests, swamps, and fields are valid hide site locations." ) );
tripoint forest = om_target_tile( omt_pos, 10, 90, hide_locations, true, true, omt_pos, true );
if( forest != tripoint( -999, -999, -999 ) ) {
int dist = rl_dist( forest.x, forest.y, omt_pos.x, omt_pos.y );
int dist = rl_dist( forest.xy(), omt_pos.xy() );
inventory tgt_inv = g->u.inv;
std::vector<item *> pos_inv = tgt_inv.items_with( []( const item & itm ) {
return !itm.can_revive();
Expand Down Expand Up @@ -1627,7 +1627,7 @@ void basecamp::start_relay_hide_site()
popup( _( "You must select an existing hide site." ) );
tripoint forest = om_target_tile( omt_pos, 10, 90, hide_locations, true, true, omt_pos, true );
if( forest != tripoint( -999, -999, -999 ) ) {
int dist = rl_dist( forest.x, forest.y, omt_pos.x, omt_pos.y );
int dist = rl_dist( forest.xy(), omt_pos.xy() );
inventory tgt_inv = g->u.inv;
std::vector<item *> pos_inv = tgt_inv.items_with( []( const item & itm ) {
return !itm.can_revive();
Expand Down Expand Up @@ -1743,7 +1743,7 @@ void basecamp::start_fortifications( std::string &bldg_exp, bool by_radio )
}
trips += 2;
build_time += making.batch_duration();
dist += rl_dist( fort_om.x, fort_om.y, omt_pos.x, omt_pos.y );
dist += rl_dist( fort_om.xy(), omt_pos.xy() );
travel_time += companion_travel_time_calc( fort_om, omt_pos, 0_minutes, 2 );
}
time_duration total_time = base_camps::to_workdays( travel_time + build_time );
Expand Down Expand Up @@ -2474,7 +2474,7 @@ bool basecamp::survey_return()
return false;
}

int dist = rl_dist( where.x, where.y, omt_pos.x, omt_pos.y );
int dist = rl_dist( where.xy(), omt_pos.xy() );
if( dist != 1 ) {
popup( _( "You must select a tile within %d range of the camp" ), 1 );
return false;
Expand Down Expand Up @@ -2896,7 +2896,7 @@ tripoint om_target_tile( const tripoint &omt_pos, int min_range, int range,
if( where == overmap::invalid_tripoint ) {
return tripoint( -999, -999, -999 );
}
int dist = rl_dist( where.x, where.y, omt_pos.x, omt_pos.y );
int dist = rl_dist( where.xy(), omt_pos.xy() );
if( dist > range || dist < min_range ) {
popup( _( "You must select a target between %d and %d range from the base. Range: %d" ),
min_range, range, dist );
Expand Down Expand Up @@ -3101,7 +3101,7 @@ std::vector<tripoint> om_companion_path( const tripoint &start, int range_start,
std::vector<tripoint> note_pts = line_to( last, spt );
scout_points.insert( scout_points.end(), note_pts.begin(), note_pts.end() );
om_line_mark( last, spt );
range -= rl_dist( spt.x, spt.y, last.x, last.y );
range -= rl_dist( spt.xy(), last.xy() );
last = spt;

oter_id &omt_ref = overmap_buffer.ter( last );
Expand Down
46 changes: 23 additions & 23 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ std::unique_ptr<game> g;
//The one and only uistate instance
uistatedata uistate;

bool is_valid_in_w_terrain( int x, int y )
bool is_valid_in_w_terrain( const point &p )
{
return x >= 0 && x < TERRAIN_WINDOW_WIDTH && y >= 0 && y < TERRAIN_WINDOW_HEIGHT;
return p.x >= 0 && p.x < TERRAIN_WINDOW_WIDTH && p.y >= 0 && p.y < TERRAIN_WINDOW_HEIGHT;
}

// This is the main game set-up process.
Expand Down Expand Up @@ -2501,8 +2501,8 @@ bool game::try_get_right_click_action( action_id &act, const tripoint &mouse_tar
return false;
}

const bool is_adjacent = square_dist( mouse_target.x, mouse_target.y, u.posx(), u.posy() ) <= 1;
const bool is_self = square_dist( mouse_target.x, mouse_target.y, u.posx(), u.posy() ) <= 0;
const bool is_adjacent = square_dist( mouse_target.xy(), point( u.posx(), u.posy() ) ) <= 1;
const bool is_self = square_dist( mouse_target.xy(), point( u.posx(), u.posy() ) ) <= 0;
if( const monster *const mon = critter_at<monster>( mouse_target ) ) {
if( !u.sees( *mon ) ) {
add_msg( _( "Nothing relevant here." ) );
Expand Down Expand Up @@ -3194,8 +3194,8 @@ struct npc_dist_to_player {
bool operator()( const std::shared_ptr<npc> &a, const std::shared_ptr<npc> &b ) const {
const tripoint apos = a->global_omt_location();
const tripoint bpos = b->global_omt_location();
return square_dist( ppos.x, ppos.y, apos.x, apos.y ) <
square_dist( ppos.x, ppos.y, bpos.x, bpos.y );
return square_dist( ppos.xy(), apos.xy() ) <
square_dist( ppos.xy(), bpos.xy() );
}
};

Expand Down Expand Up @@ -3335,7 +3335,7 @@ void game::draw_critter( const Creature &critter, const tripoint &center )
{
const int my = POSY + ( critter.posy() - center.y );
const int mx = POSX + ( critter.posx() - center.x );
if( !is_valid_in_w_terrain( mx, my ) ) {
if( !is_valid_in_w_terrain( point( mx, my ) ) ) {
return;
}
if( critter.posz() != center.z && m.has_zlevels() ) {
Expand Down Expand Up @@ -3893,11 +3893,11 @@ void game::mon_info( const catacurses::window &w, int hor_padding )
for( auto &c : u.get_visible_creatures( MAPSIZE_X ) ) {
const auto m = dynamic_cast<monster *>( c );
const auto p = dynamic_cast<npc *>( c );
const auto dir_to_mon = direction_from( view.x, view.y, c->posx(), c->posy() );
const auto dir_to_mon = direction_from( view.xy(), point( c->posx(), c->posy() ) );
const int mx = POSX + ( c->posx() - view.x );
const int my = POSY + ( c->posy() - view.y );
int index = 8;
if( !is_valid_in_w_terrain( mx, my ) ) {
if( !is_valid_in_w_terrain( point( mx, my ) ) ) {
// for compatibility with old code, see diagram below, it explains the values for index,
// also might need revisiting one z-levels are in.
switch( dir_to_mon ) {
Expand Down Expand Up @@ -5063,9 +5063,9 @@ void game::use_item( int pos )
u.invalidate_crafting_inventory();
}

void game::exam_vehicle( vehicle &veh, int cx, int cy )
void game::exam_vehicle( vehicle &veh, const point &c )
{
auto act = veh_interact::run( veh, point( cx, cy ) );
auto act = veh_interact::run( veh, c );
if( act ) {
u.moves = 0;
u.assign_activity( act );
Expand Down Expand Up @@ -7507,8 +7507,8 @@ game::vmenu_ret game::list_items( const std::vector<map_item_stack> &item_list )
const int y = iter->vIG[iThisPage].pos.y;
mvwprintz( w_items, point( width - 6 - numw, iNum - iStartPos ),
iNum == iActive ? c_light_green : c_light_gray,
"%*d %s", numw, rl_dist( 0, 0, x, y ),
direction_name_short( direction_from( 0, 0, x, y ) ) );
"%*d %s", numw, rl_dist( point_zero, point( x, y ) ),
direction_name_short( direction_from( point_zero, point( x, y ) ) ) );
++iter;
}
} else {
Expand Down Expand Up @@ -10022,7 +10022,7 @@ static cata::optional<tripoint> point_selection_menu( const std::vector<tripoint
int num = 0;
for( const tripoint &pt : pts ) {
// TODO: Sort the menu so that it can be used with numpad directions
const std::string &direction = direction_name( direction_from( upos.x, upos.y, pt.x, pt.y ) );
const std::string &direction = direction_name( direction_from( upos.xy(), pt.xy() ) );
// TODO: Inform player what is on said tile
// But don't just print terrain name (in many cases it will be "open air")
pmenu.addentry( num++, true, MENU_AUTOASSIGN, _( "Climb %s" ), direction );
Expand Down Expand Up @@ -10234,7 +10234,7 @@ void game::vertical_move( int movez, bool force )
if( mons != nullptr ) {
critter_tracker->remove( *mons );
}
shift_monsters( 0, 0, movez );
shift_monsters( tripoint( 0, 0, movez ) );
}

std::vector<std::shared_ptr<npc>> npcs_to_bring;
Expand Down Expand Up @@ -10514,7 +10514,7 @@ void game::vertical_shift( const int z_after )
m.set_transparency_cache_dirty( z_before );
m.set_outside_cache_dirty( z_before );
m.load( tripoint( get_levx(), get_levy(), z_after ), true );
shift_monsters( 0, 0, z_after - z_before );
shift_monsters( tripoint( 0, 0, z_after - z_before ) );
reload_npcs();
} else {
// Shift the map itself
Expand Down Expand Up @@ -10607,10 +10607,10 @@ point game::update_map( int &x, int &y )
}

// this handles loading/unloading submaps that have scrolled on or off the viewport
m.shift( shift.x, shift.y );
m.shift( shift );

// Shift monsters
shift_monsters( shift.x, shift.y, 0 );
shift_monsters( tripoint( shift, 0 ) );
const point shift_ms = sm_to_ms_copy( shift );
u.shift_destination( -shift_ms );

Expand Down Expand Up @@ -10949,18 +10949,18 @@ void game::despawn_monster( monster &critter )
critter.set_hp( 0 );
}

void game::shift_monsters( const int shiftx, const int shifty, const int shiftz )
void game::shift_monsters( const tripoint &shift )
{
// If either shift argument is non-zero, we're shifting.
if( shiftx == 0 && shifty == 0 && shiftz == 0 ) {
if( shift == tripoint_zero ) {
return;
}
for( monster &critter : all_monsters() ) {
if( shiftx != 0 || shifty != 0 ) {
critter.shift( point( shiftx, shifty ) );
if( shift.xy() != point_zero ) {
critter.shift( shift.xy() );
}

if( m.inbounds( critter.pos() ) && ( shiftz == 0 || m.has_zlevels() ) ) {
if( m.inbounds( critter.pos() ) && ( shift.z == 0 || m.has_zlevels() ) ) {
// We're inbounds, so don't despawn after all.
// No need to shift Z-coordinates, they are absolute
continue;
Expand Down
8 changes: 4 additions & 4 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ struct w_map {
catacurses::window win;
};

bool is_valid_in_w_terrain( int x, int y );
bool is_valid_in_w_terrain( const point &p );

// There is only one game instance, so losing a few bytes of memory
// due to padding is not much of a concern.
Expand Down Expand Up @@ -639,7 +639,7 @@ class game
void set_safe_mode( safe_mode_type mode );

/** open vehicle interaction screen */
void exam_vehicle( vehicle &veh, int cx = 0, int cy = 0 );
void exam_vehicle( vehicle &veh, const point &cp = point_zero );

// Forcefully close a door at p.
// The function checks for creatures/items/vehicles at that point and
Expand Down Expand Up @@ -799,13 +799,13 @@ class game
void replace_stair_monsters();
void update_stair_monsters();
/**
* Shift all active monsters, the shift vector (x,y,z) is the number of
* Shift all active monsters, the shift vector is the number of
* shifted submaps. Monsters that are outside of the reality bubble after
* shifting are despawned.
* Note on z-levels: this works with vertical shifts, but currently all
* monsters are despawned upon a vertical shift.
*/
void shift_monsters( int shiftx, int shifty, int shiftz );
void shift_monsters( const tripoint &shift );
public:
/**
* Despawn a specific monster, it's stored on the overmap. Also removes
Expand Down
4 changes: 2 additions & 2 deletions src/iuse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2089,8 +2089,8 @@ int iuse::directional_antenna( player *p, item *it, bool, const tripoint & )
}
// Report direction.
const auto player_pos = p->global_sm_location();
direction angle = direction_from( player_pos.x, player_pos.y,
tref.abs_sm_pos.x, tref.abs_sm_pos.y );
direction angle = direction_from( player_pos.xy(),
tref.abs_sm_pos );
add_msg( _( "The signal seems strongest to the %s." ), direction_name( angle ) );
return it->type->charges_to_use();
}
Expand Down
Loading

0 comments on commit c9e4519

Please sign in to comment.