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 rectangle and box types #41231

Merged
merged 2 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 6 additions & 5 deletions src/cata_tiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,8 @@ void cata_tiles::draw( const point &dest, const tripoint &center, 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):

Expand All @@ -1318,7 +1319,7 @@ void cata_tiles::draw( const point &dest, const tripoint &center, 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 {
Expand All @@ -1330,7 +1331,7 @@ void cata_tiles::draw( const point &dest, const tripoint &center, 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;
}
}
Expand Down Expand Up @@ -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;
}

Expand Down
10 changes: 5 additions & 5 deletions src/editmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -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 );
}
}
Expand Down Expand Up @@ -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 );
}
}
Expand Down Expand Up @@ -2005,8 +2005,8 @@ void editmap::mapgen_retarget()
if( const cata::optional<tripoint> 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();
Expand Down
8 changes: 4 additions & 4 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4041,7 +4041,7 @@ std::unordered_set<tripoint> 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<tripoint> &fishable_terrain ) {
Expand All @@ -4057,7 +4057,7 @@ std::unordered_set<tripoint> 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;
}

Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions src/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1334,8 +1334,8 @@ cata::optional<tripoint> 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;
}

Expand Down Expand Up @@ -1368,13 +1368,13 @@ std::pair<point, bool> 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 );
}

Expand Down
4 changes: 2 additions & 2 deletions src/iuse_software_kitten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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] ) {
Expand Down
10 changes: 5 additions & 5 deletions src/lightmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 ) ) {
Expand Down Expand Up @@ -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] ) {
Expand Down Expand Up @@ -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] ) {
Expand Down
22 changes: 11 additions & 11 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4461,8 +4461,8 @@ std::vector<tripoint> 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 );
}
}
Expand Down Expand Up @@ -7425,19 +7425,19 @@ 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
{
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
Expand Down Expand Up @@ -7650,15 +7650,15 @@ 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() ) {
tripoint p = v.pos + vp.part().precalc[0];
if( p.z != start.z ) {
break;
}
if( !bounds.contains_inclusive( p ) ) {
if( !bounds.contains( p ) ) {
continue;
}

Expand Down Expand Up @@ -8112,7 +8112,7 @@ void map::scent_blockers( std::array<std::array<bool, MAPSIZE_X>, 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

Expand All @@ -8121,7 +8121,7 @@ void map::scent_blockers( std::array<std::array<bool, MAPSIZE_X>, 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;
}
}
Expand All @@ -8133,7 +8133,7 @@ void map::scent_blockers( std::array<std::array<bool, MAPSIZE_X>, 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;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/mapgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions src/mapgen_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -3256,7 +3256,7 @@ void mapgen_lake_shore( mapgendata &dat )
std::vector<point> 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
Expand Down Expand Up @@ -3296,7 +3296,7 @@ void mapgen_lake_shore( mapgendata &dat )
std::unordered_set<point> 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;
Expand Down
6 changes: 3 additions & 3 deletions src/overmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/overmap_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() ) ) {
Expand Down
4 changes: 2 additions & 2 deletions src/point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}
Expand Down
Loading