From 2fc02d5183a13b2e611ea0ed805fb382dc1a2a25 Mon Sep 17 00:00:00 2001 From: John Bytheway Date: Thu, 11 Jun 2020 09:32:31 -0400 Subject: [PATCH 1/2] Refactor rectangle and box types Previously rectangle and box were just a min and max. Determining whether the upper bound should be considered to be inclusive was done by which function was called on it. I now regret that design choice, and am changing it. Now the inclusive vs half-open is part of the rectangle or box type, and the functions are generic. This leads to less repetition in the code. Also, the next step is to make it possible to iterate over rectangles (to simplify a bunch of our nested for loops), and this design fits much better for that. --- src/cata_tiles.cpp | 11 +++++----- src/editmap.cpp | 10 ++++----- src/game.cpp | 8 +++---- src/input.cpp | 4 ++-- src/iuse_software_kitten.cpp | 4 ++-- src/lightmap.cpp | 10 ++++----- src/map.cpp | 22 +++++++++---------- src/mapgen.cpp | 4 ++-- src/mapgen_functions.cpp | 6 +++--- src/overmap.cpp | 6 +++--- src/overmap_ui.cpp | 7 +++--- src/point.cpp | 4 ++-- src/point.h | 40 ++++++++++++++++++++++++----------- src/scent_map.cpp | 4 ++-- src/sdltiles.cpp | 4 ++-- src/vehicle.cpp | 4 ++-- tests/point_test.cpp | 41 +++++++++++++++++++----------------- 17 files changed, 105 insertions(+), 84 deletions(-) diff --git a/src/cata_tiles.cpp b/src/cata_tiles.cpp index 5b910e9d16482..2712cbe792d0b 100644 --- a/src/cata_tiles.cpp +++ b/src/cata_tiles.cpp @@ -1307,7 +1307,8 @@ void cata_tiles::draw( const point &dest, const tripoint ¢er, int width, int //Memorize everything the character just saw even if it wasn't displayed. for( int mem_y = min_visible_y; mem_y <= max_visible_y; mem_y++ ) { for( int mem_x = min_visible_x; mem_x <= max_visible_x; mem_x++ ) { - rectangle already_drawn( point( min_col, min_row ), point( max_col, max_row ) ); + half_open_rectangle already_drawn( point( min_col, min_row ), + point( max_col, max_row ) ); if( iso_mode ) { // calculate the screen position according to the drawing code above (division rounded down): @@ -1318,7 +1319,7 @@ void cata_tiles::draw( const point &dest, const tripoint ¢er, int width, int // \/ const int col = mem_y + mem_x + sx / 2 - o.y - o.x; const int row = mem_y - mem_x + sy / 2 - o.y + o.x; - if( already_drawn.contains_half_open( point( col, row ) ) ) { + if( already_drawn.contains( point( col, row ) ) ) { continue; } } else { @@ -1330,7 +1331,7 @@ void cata_tiles::draw( const point &dest, const tripoint ¢er, int width, int // \/ // col = mem_x - o.x // row = mem_y - o.y - if( already_drawn.contains_half_open( point( mem_x, mem_y ) - o ) ) { + if( already_drawn.contains( point( mem_x, mem_y ) - o ) ) { continue; } } @@ -1598,9 +1599,9 @@ bool cata_tiles::draw_from_id_string( std::string id, TILE_CATEGORY category, // check to make sure that we are drawing within a valid area // [0->width|height / tile_width|height] - rectangle screen_bounds( o, o + point( screentile_width, screentile_height ) ); + half_open_rectangle screen_bounds( o, o + point( screentile_width, screentile_height ) ); if( !tile_iso && - !screen_bounds.contains_half_open( pos.xy() ) ) { + !screen_bounds.contains( pos.xy() ) ) { return false; } diff --git a/src/editmap.cpp b/src/editmap.cpp index f947c1f1dbf35..8d74674110be0 100644 --- a/src/editmap.cpp +++ b/src/editmap.cpp @@ -58,7 +58,7 @@ static constexpr tripoint editmap_boundary_min( 0, 0, -OVERMAP_DEPTH ); static constexpr tripoint editmap_boundary_max( MAPSIZE_X, MAPSIZE_Y, OVERMAP_HEIGHT + 1 ); -static constexpr box editmap_boundaries( editmap_boundary_min, editmap_boundary_max ); +static constexpr half_open_box editmap_boundaries( editmap_boundary_min, editmap_boundary_max ); static const ter_id undefined_ter_id( -1 ); @@ -1517,7 +1517,7 @@ void editmap::recalc_target( shapetype shape ) int radius = rl_dist( origin, target ); for( const tripoint &p : g->m.points_in_radius( origin, radius ) ) { if( rl_dist( p, origin ) <= radius ) { - if( editmap_boundaries.contains_half_open( p ) ) { + if( editmap_boundaries.contains( p ) ) { target_list.push_back( p ); } } @@ -1548,7 +1548,7 @@ void editmap::recalc_target( shapetype shape ) for( int y = sy; y <= ey; y++ ) { if( shape == editmap_rect_filled || x == sx || x == ex || y == sy || y == ey ) { const tripoint p( x, y, z ); - if( editmap_boundaries.contains_half_open( p ) ) { + if( editmap_boundaries.contains( p ) ) { target_list.push_back( p ); } } @@ -2005,8 +2005,8 @@ void editmap::mapgen_retarget() if( const cata::optional vec = ctxt.get_direction( action ) ) { point vec_ms = omt_to_ms_copy( vec->xy() ); tripoint ptarget = target + vec_ms; - if( editmap_boundaries.contains_half_open( ptarget ) && - editmap_boundaries.contains_half_open( ptarget + point( SEEX, SEEY ) ) ) { + if( editmap_boundaries.contains( ptarget ) && + editmap_boundaries.contains( ptarget + point( SEEX, SEEY ) ) ) { target = ptarget; target_list.clear(); diff --git a/src/game.cpp b/src/game.cpp index f02c062ab1564..397299846c5a2 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -4041,7 +4041,7 @@ std::unordered_set game::get_fishable_locations( int distance, const t const tripoint fishing_boundary_min( fish_pos + point( -distance, -distance ) ); const tripoint fishing_boundary_max( fish_pos + point( distance, distance ) ); - const box fishing_boundaries( fishing_boundary_min, fishing_boundary_max ); + const inclusive_box fishing_boundaries( fishing_boundary_min, fishing_boundary_max ); const auto get_fishable_terrain = [&]( tripoint starting_point, std::unordered_set &fishable_terrain ) { @@ -4057,7 +4057,7 @@ std::unordered_set game::get_fishable_locations( int distance, const t } // This point is out of bounds, so bail. - if( !fishing_boundaries.contains_inclusive( current_point ) ) { + if( !fishing_boundaries.contains( current_point ) ) { continue; } @@ -11094,10 +11094,10 @@ point game::update_map( int &x, int &y ) // this handles loading/unloading submaps that have scrolled on or off the viewport // NOLINTNEXTLINE(cata-use-named-point-constants) - rectangle size_1( point( -1, -1 ), point( 1, 1 ) ); + inclusive_rectangle size_1( point( -1, -1 ), point( 1, 1 ) ); point remaining_shift = shift; while( remaining_shift != point_zero ) { - point this_shift = clamp_inclusive( remaining_shift, size_1 ); + point this_shift = clamp( remaining_shift, size_1 ); m.shift( this_shift ); remaining_shift -= this_shift; } diff --git a/src/input.cpp b/src/input.cpp index 9e5ecbc9f47f3..80f7e17095a78 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -1368,13 +1368,13 @@ std::pair input_context::get_coordinates_text( const catacurses::wi const point &win_size = dim.window_size_pixel; const point win_max = win_min + win_size; - const rectangle win_bounds( win_min, win_max ); + const half_open_rectangle win_bounds( win_min, win_max ); const point screen_pos = coordinate - win_min; const point selected( divide_round_down( screen_pos.x, fw ), divide_round_down( screen_pos.y, fh ) ); - if( !win_bounds.contains_half_open( coordinate ) ) { + if( !win_bounds.contains( coordinate ) ) { return std::make_pair( selected, false ); } diff --git a/src/iuse_software_kitten.cpp b/src/iuse_software_kitten.cpp index 20ba8cb642647..38d24779ae83d 100644 --- a/src/iuse_software_kitten.cpp +++ b/src/iuse_software_kitten.cpp @@ -455,8 +455,8 @@ void robot_finds_kitten::process_input() check.x++; } - constexpr rectangle bounds( point( 0, 3 ), point( rfkCOLS, rfkLINES ) ); - if( !bounds.contains_half_open( check ) ) { + constexpr half_open_rectangle bounds( point( 0, 3 ), point( rfkCOLS, rfkLINES ) ); + if( !bounds.contains( check ) ) { /* Can't move past edge */ } else if( rfkscreen[check.x][check.y] != EMPTY ) { switch( rfkscreen[check.x][check.y] ) { diff --git a/src/lightmap.cpp b/src/lightmap.cpp index 9f81f92f98108..478aa2b92fb61 100644 --- a/src/lightmap.cpp +++ b/src/lightmap.cpp @@ -48,7 +48,7 @@ static const efftype_id effect_onfire( "onfire" ); static constexpr point lightmap_boundary_min( point_zero ); static constexpr point lightmap_boundary_max( LIGHTMAP_CACHE_X, LIGHTMAP_CACHE_Y ); -const rectangle lightmap_boundaries( lightmap_boundary_min, lightmap_boundary_max ); +const half_open_rectangle lightmap_boundaries( lightmap_boundary_min, lightmap_boundary_max ); std::string four_quadrants::to_string() const { @@ -338,7 +338,7 @@ void map::generate_lightmap( const int zlev ) // Apply light sources for external/internal divide for( int i = 0; i < 4; ++i ) { point neighbour = p.xy() + point( dir_x[i], dir_y[i] ); - if( lightmap_boundaries.contains_half_open( neighbour ) + if( lightmap_boundaries.contains( neighbour ) && outside_cache[neighbour.x][neighbour.y] ) { if( light_transparency( p ) > LIGHT_TRANSPARENCY_SOLID ) { @@ -581,7 +581,7 @@ map::apparent_light_info map::apparent_light_helper( const level_cache &map_cach for( const offset_and_quadrants &oq : adjacent_offsets ) { const point neighbour = p.xy() + oq.offset; - if( !lightmap_boundaries.contains_half_open( neighbour ) ) { + if( !lightmap_boundaries.contains( neighbour ) ) { continue; } if( is_opaque( neighbour ) ) { @@ -1477,7 +1477,7 @@ void map::apply_light_ray( bool lit[LIGHTMAP_CACHE_X][LIGHTMAP_CACHE_Y], t += ay; // TODO: clamp coordinates to map bounds before this method is called. - if( lightmap_boundaries.contains_half_open( point( x, y ) ) ) { + if( lightmap_boundaries.contains( point( x, y ) ) ) { float current_transparency = transparency_cache[x][y]; bool is_opaque = ( current_transparency == LIGHT_TRANSPARENCY_SOLID ); if( !lit[x][y] ) { @@ -1509,7 +1509,7 @@ void map::apply_light_ray( bool lit[LIGHTMAP_CACHE_X][LIGHTMAP_CACHE_Y], y += dy; t += ax; - if( lightmap_boundaries.contains_half_open( point( x, y ) ) ) { + if( lightmap_boundaries.contains( point( x, y ) ) ) { float current_transparency = transparency_cache[x][y]; bool is_opaque = ( current_transparency == LIGHT_TRANSPARENCY_SOLID ); if( !lit[x][y] ) { diff --git a/src/map.cpp b/src/map.cpp index 8a821bac2820e..84513d771a10a 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -4461,8 +4461,8 @@ std::vector map::check_submap_active_item_consistency() } for( const tripoint &p : submaps_with_active_items ) { tripoint rel = p - abs_sub.xy(); - rectangle map( point_zero, point( MAPSIZE, MAPSIZE ) ); - if( !map.contains_half_open( rel.xy() ) ) { + half_open_rectangle map( point_zero, point( MAPSIZE, MAPSIZE ) ); + if( !map.contains( rel.xy() ) ) { result.push_back( p ); } } @@ -7425,9 +7425,9 @@ bool map::inbounds( const tripoint &p ) const static constexpr tripoint map_boundary_min( 0, 0, -OVERMAP_DEPTH ); static constexpr tripoint map_boundary_max( MAPSIZE_Y, MAPSIZE_X, OVERMAP_HEIGHT + 1 ); - static constexpr box map_boundaries( map_boundary_min, map_boundary_max ); + static constexpr half_open_box map_boundaries( map_boundary_min, map_boundary_max ); - return map_boundaries.contains_half_open( p ); + return map_boundaries.contains( p ); } bool tinymap::inbounds( const tripoint &p ) const @@ -7435,9 +7435,9 @@ bool tinymap::inbounds( const tripoint &p ) const constexpr tripoint map_boundary_min( 0, 0, -OVERMAP_DEPTH ); constexpr tripoint map_boundary_max( SEEY * 2, SEEX * 2, OVERMAP_HEIGHT + 1 ); - constexpr box map_boundaries( map_boundary_min, map_boundary_max ); + constexpr half_open_box map_boundaries( map_boundary_min, map_boundary_max ); - return map_boundaries.contains_half_open( p ); + return map_boundaries.contains( p ); } // set up a map just long enough scribble on it @@ -7650,7 +7650,7 @@ void map::build_obstacle_cache( const tripoint &start, const tripoint &end, } } VehicleList vehs = get_vehicles( start, end ); - const box bounds( start, end ); + const inclusive_box bounds( start, end ); // Cache all the vehicle stuff in one loop for( auto &v : vehs ) { for( const vpart_reference &vp : v.v->get_all_parts() ) { @@ -7658,7 +7658,7 @@ void map::build_obstacle_cache( const tripoint &start, const tripoint &end, if( p.z != start.z ) { break; } - if( !bounds.contains_inclusive( p ) ) { + if( !bounds.contains( p ) ) { continue; } @@ -8112,7 +8112,7 @@ void map::scent_blockers( std::array, MAPSIZE_Y> &bl function_over( tripoint( min, abs_sub.z ), tripoint( max, abs_sub.z ), fill_values ); - const rectangle local_bounds( min, max ); + const inclusive_rectangle local_bounds( min, max ); // Now vehicles @@ -8121,7 +8121,7 @@ void map::scent_blockers( std::array, MAPSIZE_Y> &bl vehicle &veh = *( wrapped_veh.v ); for( const vpart_reference &vp : veh.get_any_parts( VPFLAG_OBSTACLE ) ) { const tripoint part_pos = vp.pos(); - if( local_bounds.contains_inclusive( part_pos.xy() ) ) { + if( local_bounds.contains( part_pos.xy() ) ) { reduces_scent[part_pos.x][part_pos.y] = true; } } @@ -8133,7 +8133,7 @@ void map::scent_blockers( std::array, MAPSIZE_Y> &bl } const tripoint part_pos = vp.pos(); - if( local_bounds.contains_inclusive( part_pos.xy() ) ) { + if( local_bounds.contains( part_pos.xy() ) ) { reduces_scent[part_pos.x][part_pos.y] = true; } } diff --git a/src/mapgen.cpp b/src/mapgen.cpp index 1f3caa5c41576..ff3468f5b6eca 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -554,8 +554,8 @@ size_t mapgen_function_json_base::calc_index( const point &p ) const static bool common_check_bounds( const jmapgen_int &x, const jmapgen_int &y, const point &mapgensize, const JsonObject &jso ) { - rectangle bounds( point_zero, mapgensize ); - if( !bounds.contains_half_open( point( x.val, y.val ) ) ) { + half_open_rectangle bounds( point_zero, mapgensize ); + if( !bounds.contains( point( x.val, y.val ) ) ) { return false; } diff --git a/src/mapgen_functions.cpp b/src/mapgen_functions.cpp index ff35f95873e3b..398caaff03eaa 100644 --- a/src/mapgen_functions.cpp +++ b/src/mapgen_functions.cpp @@ -3247,7 +3247,7 @@ void mapgen_lake_shore( mapgendata &dat ) line_segments.push_back( { sw, nw } ); } - static constexpr rectangle map_boundaries( nw_corner, se_corner ); + static constexpr inclusive_rectangle map_boundaries( nw_corner, se_corner ); // This will draw our shallow water coastline from the "from" point to the "to" point. // It buffers the points a bit for a thicker line. It also clears any furniture that might @@ -3256,7 +3256,7 @@ void mapgen_lake_shore( mapgendata &dat ) std::vector points = line_to( from, to ); for( auto &p : points ) { for( const point &bp : closest_points_first( p, 1 ) ) { - if( !map_boundaries.contains_inclusive( bp ) ) { + if( !map_boundaries.contains( bp ) ) { continue; } // Use t_null for now instead of t_water_sh, because sometimes our extended terrain @@ -3296,7 +3296,7 @@ void mapgen_lake_shore( mapgendata &dat ) std::unordered_set visited; const auto should_fill = [&]( const point & p ) { - if( !map_boundaries.contains_inclusive( p ) ) { + if( !map_boundaries.contains( p ) ) { return false; } return m->ter( p ) != t_null; diff --git a/src/overmap.cpp b/src/overmap.cpp index 7658480314c2c..ff638de7fe869 100644 --- a/src/overmap.cpp +++ b/src/overmap.cpp @@ -1429,11 +1429,11 @@ bool overmap::inbounds( const tripoint &p, int clearance ) static constexpr tripoint overmap_boundary_min( 0, 0, -OVERMAP_DEPTH ); static constexpr tripoint overmap_boundary_max( OMAPX, OMAPY, OVERMAP_HEIGHT + 1 ); - static constexpr box overmap_boundaries( overmap_boundary_min, overmap_boundary_max ); - box stricter_boundaries = overmap_boundaries; + static constexpr half_open_box overmap_boundaries( overmap_boundary_min, overmap_boundary_max ); + half_open_box stricter_boundaries = overmap_boundaries; stricter_boundaries.shrink( tripoint( clearance, clearance, 0 ) ); - return stricter_boundaries.contains_half_open( p ); + return stricter_boundaries.contains( p ); } const scent_trace &overmap::scent_at( const tripoint &loc ) const diff --git a/src/overmap_ui.cpp b/src/overmap_ui.cpp index 28a8d66a79b32..253c30817f5fe 100644 --- a/src/overmap_ui.cpp +++ b/src/overmap_ui.cpp @@ -817,10 +817,11 @@ void draw( const catacurses::window &w, const catacurses::window &wbar, const tr draw_camp_labels( w, center ); } - rectangle screen_bounds( corner.xy(), corner.xy() + point( om_map_width, om_map_height ) ); + half_open_rectangle screen_bounds( corner.xy(), + corner.xy() + point( om_map_width, om_map_height ) ); - if( has_target && blink && !screen_bounds.contains_half_open( target.xy() ) ) { - point marker = clamp_half_open( target.xy(), screen_bounds ) - corner.xy(); + if( has_target && blink && !screen_bounds.contains( target.xy() ) ) { + point marker = clamp( target.xy(), screen_bounds ) - corner.xy(); std::string marker_sym = " "; switch( direction_from( center.xy(), target.xy() ) ) { diff --git a/src/point.cpp b/src/point.cpp index 18107f7771f35..56600f031a731 100644 --- a/src/point.cpp +++ b/src/point.cpp @@ -70,12 +70,12 @@ std::istream &operator>>( std::istream &is, tripoint &pos ) return is; } -point clamp_half_open( const point &p, const rectangle &r ) +point clamp( const point &p, const half_open_rectangle &r ) { return point( clamp( p.x, r.p_min.x, r.p_max.x - 1 ), clamp( p.y, r.p_min.y, r.p_max.y - 1 ) ); } -point clamp_inclusive( const point &p, const rectangle &r ) +point clamp( const point &p, const inclusive_rectangle &r ) { return point( clamp( p.x, r.p_min.x, r.p_max.x ), clamp( p.y, r.p_min.y, r.p_max.y ) ); } diff --git a/src/point.h b/src/point.h index b14fcec8d9b2d..445f5a3d0d1b2 100644 --- a/src/point.h +++ b/src/point.h @@ -233,25 +233,33 @@ struct rectangle { point p_max; constexpr rectangle() = default; constexpr rectangle( const point &P_MIN, const point &P_MAX ) : p_min( P_MIN ), p_max( P_MAX ) {} +}; + +struct half_open_rectangle : rectangle { + using rectangle::rectangle; - constexpr bool contains_half_open( const point &p ) const { + constexpr bool contains( const point &p ) const { return p.x >= p_min.x && p.x < p_max.x && p.y >= p_min.y && p.y < p_max.y; } +}; - constexpr bool contains_inclusive( const point &p ) const { +struct inclusive_rectangle : rectangle { + using rectangle::rectangle; + + constexpr bool contains( const point &p ) const { return p.x >= p_min.x && p.x <= p_max.x && p.y >= p_min.y && p.y <= p_max.y; } }; -// Clamp p to the half-open rectangle r. +// Clamp p to the rectangle r. // This independently clamps each coordinate of p to the bounds of the // rectangle. // Useful for example to round an arbitrary point to the nearest point on the // screen, or the nearest point in a particular submap. -point clamp_half_open( const point &p, const rectangle &r ); -point clamp_inclusive( const point &p, const rectangle &r ); +point clamp( const point &p, const half_open_rectangle &r ); +point clamp( const point &p, const inclusive_rectangle &r ); struct box { tripoint p_min; @@ -261,22 +269,30 @@ struct box { explicit constexpr box( const rectangle &R, int Z1, int Z2 ) : p_min( tripoint( R.p_min, Z1 ) ), p_max( tripoint( R.p_max, Z2 ) ) {} - constexpr bool contains_half_open( const tripoint &p ) const { + void shrink( const tripoint &amount ) { + p_min += amount; + p_max -= amount; + } +}; + +struct half_open_box : box { + using box::box; + + constexpr bool contains( const tripoint &p ) const { return p.x >= p_min.x && p.x < p_max.x && p.y >= p_min.y && p.y < p_max.y && p.z >= p_min.z && p.z < p_max.z; } +}; - constexpr bool contains_inclusive( const tripoint &p ) const { +struct inclusive_box : box { + using box::box; + + constexpr bool contains( const tripoint &p ) const { return p.x >= p_min.x && p.x <= p_max.x && p.y >= p_min.y && p.y <= p_max.y && p.z >= p_min.z && p.z <= p_max.z; } - - void shrink( const tripoint &amount ) { - p_min += amount; - p_max -= amount; - } }; static constexpr tripoint tripoint_zero { 0, 0, 0 }; diff --git a/src/scent_map.cpp b/src/scent_map.cpp index 7b65a238d92e7..b8c5d86e62a22 100644 --- a/src/scent_map.cpp +++ b/src/scent_map.cpp @@ -142,10 +142,10 @@ bool scent_map::inbounds( const tripoint &p ) const static constexpr point scent_map_boundary_min( point_zero ); static constexpr point scent_map_boundary_max( MAPSIZE_X, MAPSIZE_Y ); - static constexpr rectangle scent_map_boundaries( + static constexpr half_open_rectangle scent_map_boundaries( scent_map_boundary_min, scent_map_boundary_max ); - return scent_map_boundaries.contains_half_open( p.xy() ); + return scent_map_boundaries.contains( p.xy() ); } void scent_map::update( const tripoint ¢er, map &m ) diff --git a/src/sdltiles.cpp b/src/sdltiles.cpp index 3b5e27d038b1b..9ffc9381dc399 100644 --- a/src/sdltiles.cpp +++ b/src/sdltiles.cpp @@ -3797,8 +3797,8 @@ cata::optional input_context::get_coordinates( const catacurses::windo // Translate mouse coordinates to map coordinates based on tile size // Check if click is within bounds of the window we care about - const rectangle win_bounds( win_min, win_max ); - if( !win_bounds.contains_inclusive( coordinate ) ) { + const inclusive_rectangle win_bounds( win_min, win_max ); + if( !win_bounds.contains( coordinate ) ) { return cata::nullopt; } diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 7a24cb9978a1c..d8d0332221c81 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -4364,8 +4364,8 @@ bool vehicle::balanced_wheel_config() const // Check center of mass inside support of wheels (roughly) const point &com = local_center_of_mass(); - const rectangle support( min, max ); - return support.contains_inclusive( com ); + const inclusive_rectangle support( min, max ); + return support.contains( com ); } bool vehicle::valid_wheel_config() const diff --git a/tests/point_test.cpp b/tests/point_test.cpp index cf24f5abee8c6..596d9c6d103bd 100644 --- a/tests/point_test.cpp +++ b/tests/point_test.cpp @@ -6,36 +6,39 @@ TEST_CASE( "rectangle_containment", "[point]" ) { - rectangle r( point( 0, 0 ), point( 2, 2 ) ); // NOLINT(cata-use-named-point-constants) - CHECK( !r.contains_half_open( point( 0, -1 ) ) ); // NOLINT(cata-use-named-point-constants) - CHECK( r.contains_half_open( point( 0, 0 ) ) ); // NOLINT(cata-use-named-point-constants) - CHECK( r.contains_half_open( point( 0, 1 ) ) ); // NOLINT(cata-use-named-point-constants) - CHECK( !r.contains_half_open( point( 0, 2 ) ) ); - CHECK( !r.contains_half_open( point( 0, 3 ) ) ); - - CHECK( !r.contains_inclusive( point( 0, -1 ) ) ); // NOLINT(cata-use-named-point-constants) - CHECK( r.contains_inclusive( point( 0, 0 ) ) ); // NOLINT(cata-use-named-point-constants) - CHECK( r.contains_inclusive( point( 0, 1 ) ) ); // NOLINT(cata-use-named-point-constants) - CHECK( r.contains_inclusive( point( 0, 2 ) ) ); - CHECK( !r.contains_inclusive( point( 0, 3 ) ) ); + // NOLINTNEXTLINE(cata-use-named-point-constants) + half_open_rectangle r1( point( 0, 0 ), point( 2, 2 ) ); + CHECK( !r1.contains( point( 0, -1 ) ) ); // NOLINT(cata-use-named-point-constants) + CHECK( r1.contains( point( 0, 0 ) ) ); // NOLINT(cata-use-named-point-constants) + CHECK( r1.contains( point( 0, 1 ) ) ); // NOLINT(cata-use-named-point-constants) + CHECK( !r1.contains( point( 0, 2 ) ) ); + CHECK( !r1.contains( point( 0, 3 ) ) ); + + // NOLINTNEXTLINE(cata-use-named-point-constants) + inclusive_rectangle r2( point( 0, 0 ), point( 2, 2 ) ); + CHECK( !r2.contains( point( 0, -1 ) ) ); // NOLINT(cata-use-named-point-constants) + CHECK( r2.contains( point( 0, 0 ) ) ); // NOLINT(cata-use-named-point-constants) + CHECK( r2.contains( point( 0, 1 ) ) ); // NOLINT(cata-use-named-point-constants) + CHECK( r2.contains( point( 0, 2 ) ) ); + CHECK( !r2.contains( point( 0, 3 ) ) ); } TEST_CASE( "box_shrinks", "[point]" ) { - box b( tripoint_zero, tripoint( 3, 3, 3 ) ); + half_open_box b( tripoint_zero, tripoint( 3, 3, 3 ) ); CAPTURE( b ); - CHECK( b.contains_half_open( tripoint( 1, 0, 0 ) ) ); // NOLINT(cata-use-named-point-constants) - CHECK( b.contains_half_open( tripoint( 2, 1, 2 ) ) ); + CHECK( b.contains( tripoint( 1, 0, 0 ) ) ); // NOLINT(cata-use-named-point-constants) + CHECK( b.contains( tripoint( 2, 1, 2 ) ) ); b.shrink( tripoint( 1, 1, 0 ) ); // NOLINT(cata-use-named-point-constants) CAPTURE( b ); // Shrank in the x and y directions // NOLINTNEXTLINE(cata-use-named-point-constants) - CHECK( !b.contains_half_open( tripoint( 1, 0, 0 ) ) ); - CHECK( !b.contains_half_open( tripoint( 2, 1, 2 ) ) ); + CHECK( !b.contains( tripoint( 1, 0, 0 ) ) ); + CHECK( !b.contains( tripoint( 2, 1, 2 ) ) ); // Didn't shrink in the z direction // NOLINTNEXTLINE(cata-use-named-point-constants) - CHECK( b.contains_half_open( tripoint( 1, 1, 0 ) ) ); - CHECK( b.contains_half_open( tripoint( 1, 1, 2 ) ) ); + CHECK( b.contains( tripoint( 1, 1, 0 ) ) ); + CHECK( b.contains( tripoint( 1, 1, 2 ) ) ); } TEST_CASE( "point_to_from_string", "[point]" ) From 5138fe74f05cda5ac57633c9f5ee998243c9cf25 Mon Sep 17 00:00:00 2001 From: Kevin Granade Date: Thu, 11 Jun 2020 13:19:37 -0700 Subject: [PATCH 2/2] 1337 c0mp1l3 f1X --- src/input.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/input.cpp b/src/input.cpp index 80f7e17095a78..138222d62bc81 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -1334,8 +1334,8 @@ cata::optional input_context::get_coordinates( const catacurses::windo const point view_size( getmaxx( capture_win ), getmaxy( capture_win ) ); const point win_min( getbegx( capture_win ), getbegy( capture_win ) ); - const rectangle win_bounds( win_min, win_min + view_size ); - if( !win_bounds.contains_half_open( coordinate ) ) { + const half_open_rectangle win_bounds( win_min, win_min + view_size ); + if( !win_bounds.contains( coordinate ) ) { return cata::nullopt; }