From be821bcd44d53e6eba94c6d7eae4a7c2ce2f916e Mon Sep 17 00:00:00 2001 From: John Bytheway Date: Sat, 17 Aug 2019 20:08:57 -0400 Subject: [PATCH 1/7] Add overloads to game, line, map functions --- src/game.h | 5 ++++- src/line.h | 3 +++ src/map.h | 11 +++++++++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/game.h b/src/game.h index d0f4c57e16e70..f36af86f26a58 100644 --- a/src/game.h +++ b/src/game.h @@ -125,6 +125,7 @@ struct w_map { }; 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. @@ -640,6 +641,7 @@ class game /** open vehicle interaction screen */ void exam_vehicle( vehicle &veh, int cx = 0, int cy = 0 ); + void exam_vehicle( vehicle &veh, const point &cp ); // Forcefully close a door at p. // The function checks for creatures/items/vehicles at that point and @@ -799,13 +801,14 @@ 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 diff --git a/src/line.h b/src/line.h index 1e26d48cde12b..94d211e7dd370 100644 --- a/src/line.h +++ b/src/line.h @@ -48,6 +48,7 @@ inline constexpr unsigned make_xyz_unit( const int x, const int y, const int z ) // This more general version of this function gives correct values for larger inputs. unsigned make_xyz( int x, int y, int z ); +unsigned make_xyz( const tripoint & ); enum direction : unsigned { ABOVENORTHWEST = make_xyz_unit( -1, -1, -1 ), @@ -82,6 +83,8 @@ enum direction : unsigned { }; direction direction_from( int x, int y, int z = 0 ) noexcept; +direction direction_from( const point &p ) noexcept; +direction direction_from( const tripoint &p ) noexcept; direction direction_from( int x1, int y1, int x2, int y2 ) noexcept; direction direction_from( const tripoint &p, const tripoint &q ); diff --git a/src/map.h b/src/map.h index 81ff1cdc813a8..ed10f8bd35f52 100644 --- a/src/map.h +++ b/src/map.h @@ -345,16 +345,17 @@ class map * @param update_vehicles If true, add vehicles to the vehicle cache. */ void load( int wx, int wy, int wz, bool update_vehicles ); - void load( const tripoint &p, const bool update_vehicles ) { + void load( const tripoint &p, bool update_vehicles ) { load( p.x, p.y, p.z, update_vehicles ); } /** - * Shift the map along the vector (sx,sy). + * Shift the map along the vector s. * This is like loading the map with coordinates derived from the current * position of the map (@ref abs_sub) plus the shift vector. * Note: the map must have been loaded before this can be called. */ void shift( int sx, int sy ); + void shift( const point &s ); /** * Moves the map vertically to (not by!) newz. * Does not actually shift anything, only forces cache updates. @@ -1052,6 +1053,8 @@ class map bool process_fields(); // See fields.cpp bool process_fields_in_submap( submap *current_submap, int submap_x, int submap_y, int submap_z ); // See fields.cpp + bool process_fields_in_submap( submap *current_submap, + const tripoint &submap_pos ); // See fields.cpp /** * Apply field effects to the creature when it's on a square with fields. */ @@ -1628,6 +1631,8 @@ class map template void function_over( const tripoint &start, const tripoint &end, Functor fun ) const; template + void function_over( int stx, int sty, int stz, const tripoint &end, Functor fun ) const; + template void function_over( int stx, int sty, int stz, int enx, int eny, int enz, Functor fun ) const; /*@}*/ @@ -1705,6 +1710,8 @@ class map template void shift_bitset_cache( std::bitset &cache, int sx, int sy ); +template +void shift_bitset_cache( std::bitset &cache, const point &s ); std::vector closest_points_first( int radius, const point ¢er ); // Does not build "piles" - does the same as above functions, except in tripoints From 63fcc096e75ee84e77273aa0e699e911d20dd9d9 Mon Sep 17 00:00:00 2001 From: John Bytheway Date: Sat, 17 Aug 2019 20:53:25 -0400 Subject: [PATCH 2/7] Refactor callers --- src/activity_handlers.cpp | 2 +- src/animation.cpp | 2 +- src/game.cpp | 12 +- src/line.cpp | 6 +- src/map.cpp | 4 +- src/map_field.cpp | 2 +- tests/line_test.cpp | 240 +++++++++++++++++++------------------- tests/map_memory.cpp | 16 +-- tests/vehicle_drag.cpp | 4 +- 9 files changed, 144 insertions(+), 144 deletions(-) diff --git a/src/activity_handlers.cpp b/src/activity_handlers.cpp index 67f70e09d18ce..c0c4221f90c21 100644 --- a/src/activity_handlers.cpp +++ b/src/activity_handlers.cpp @@ -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"; diff --git a/src/animation.cpp b/src/animation.cpp index 3f63e4662a8b3..71eebdbe54400 100644 --- a/src/animation.cpp +++ b/src/animation.cpp @@ -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; } diff --git a/src/game.cpp b/src/game.cpp index 0cc82daa4a1d8..0f30389c6b8cf 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -3335,7 +3335,7 @@ void game::draw_critter( const Creature &critter, const tripoint ¢er ) { 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() ) { @@ -3897,7 +3897,7 @@ void game::mon_info( const catacurses::window &w, int hor_padding ) 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 ) { @@ -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> npcs_to_bring; @@ -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 @@ -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 ); diff --git a/src/line.cpp b/src/line.cpp index 86b820c643d67..4e310938c3a63 100644 --- a/src/line.cpp +++ b/src/line.cpp @@ -396,18 +396,18 @@ std::vector continue_line( const std::vector &line, const in direction direction_from( const int x, const int y, const int z ) noexcept { - return static_cast( make_xyz( x, y, z ) ); + return static_cast( make_xyz( tripoint( x, y, z ) ) ); } direction direction_from( const int x1, const int y1, const int x2, const int y2 ) noexcept { - return direction_from( x2 - x1, y2 - y1 ); + return direction_from( point( x2 - x1, y2 - y1 ) ); } direction direction_from( const tripoint &p, const tripoint &q ) { // Note: Z-coordinate has to be inverted either here or in direction definitions - return direction_from( q.x - p.x, q.y - p.y, -( q.z - p.z ) ); + return direction_from( tripoint( q.x - p.x, q.y - p.y, -( q.z - p.z ) ) ); } point direction_XY( const direction dir ) diff --git a/src/map.cpp b/src/map.cpp index eb0fb09b6960a..f113928e76421 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -6565,8 +6565,8 @@ void map::shift( const int sx, const int sy ) // Clear vehicle list and rebuild after shift clear_vehicle_cache( gridz ); clear_vehicle_list( gridz ); - shift_bitset_cache( get_cache( gridz ).map_memory_seen_cache, sx, sy ); - shift_bitset_cache( get_cache( gridz ).field_cache, sx, sy ); + shift_bitset_cache( get_cache( gridz ).map_memory_seen_cache, point( sx, sy ) ); + shift_bitset_cache( get_cache( gridz ).field_cache, point( sx, sy ) ); if( sx >= 0 ) { for( int gridx = 0; gridx < my_MAPSIZE; gridx++ ) { if( sy >= 0 ) { diff --git a/src/map_field.cpp b/src/map_field.cpp index 9f0847c4b36b5..355c80dc633dd 100644 --- a/src/map_field.cpp +++ b/src/map_field.cpp @@ -136,7 +136,7 @@ bool map::process_fields() for( int y = 0; y < my_MAPSIZE; y++ ) { if( field_cache[ x + y * MAPSIZE ] ) { submap *const current_submap = get_submap_at_grid( { x, y, z } ); - const bool cur_dirty = process_fields_in_submap( current_submap, x, y, z ); + const bool cur_dirty = process_fields_in_submap( current_submap, tripoint( x, y, z ) ); zlev_dirty |= cur_dirty; } } diff --git a/tests/line_test.cpp b/tests/line_test.cpp index 8f36c1ee822a5..3de4df608c216 100644 --- a/tests/line_test.cpp +++ b/tests/line_test.cpp @@ -124,128 +124,128 @@ TEST_CASE( "Test bounds for mapping x/y/z/ offsets to direction enum" ) // Test the unit square values at distance 1 and 2. // Test the multiples of 30deg at 60 squares. // Test 22 deg to either side of the cardinal directions. - REQUIRE( make_xyz( -1, -1, -1 ) == ABOVENORTHWEST ); - REQUIRE( make_xyz( -2, -2, -2 ) == ABOVENORTHWEST ); - REQUIRE( make_xyz( -30, -60, -1 ) == ABOVENORTHWEST ); - REQUIRE( make_xyz( -60, -60, -1 ) == ABOVENORTHWEST ); - REQUIRE( make_xyz( -60, -30, -1 ) == ABOVENORTHWEST ); - REQUIRE( make_xyz( -1, -1, 0 ) == NORTHWEST ); - REQUIRE( make_xyz( -2, -2, 0 ) == NORTHWEST ); - REQUIRE( make_xyz( -60, -60, 0 ) == NORTHWEST ); - REQUIRE( make_xyz( -1, -1, 1 ) == BELOWNORTHWEST ); - REQUIRE( make_xyz( -2, -2, 2 ) == BELOWNORTHWEST ); - REQUIRE( make_xyz( -30, -60, 1 ) == BELOWNORTHWEST ); - REQUIRE( make_xyz( -60, -60, 1 ) == BELOWNORTHWEST ); - REQUIRE( make_xyz( -60, -30, 1 ) == BELOWNORTHWEST ); - REQUIRE( make_xyz( 0, -1, -1 ) == ABOVENORTH ); - REQUIRE( make_xyz( 0, -2, -2 ) == ABOVENORTH ); - REQUIRE( make_xyz( -22, -60, -1 ) == ABOVENORTH ); - REQUIRE( make_xyz( 0, -60, -1 ) == ABOVENORTH ); - REQUIRE( make_xyz( 22, -60, -1 ) == ABOVENORTH ); - REQUIRE( make_xyz( 0, -1, 0 ) == NORTH ); - REQUIRE( make_xyz( 0, -2, 0 ) == NORTH ); - REQUIRE( make_xyz( -22, -60, 0 ) == NORTH ); - REQUIRE( make_xyz( 0, -60, 0 ) == NORTH ); - REQUIRE( make_xyz( 22, -60, 0 ) == NORTH ); - REQUIRE( make_xyz( 0, -1, 1 ) == BELOWNORTH ); - REQUIRE( make_xyz( 0, -2, 2 ) == BELOWNORTH ); - REQUIRE( make_xyz( -22, -60, 1 ) == BELOWNORTH ); - REQUIRE( make_xyz( 0, -60, 1 ) == BELOWNORTH ); - REQUIRE( make_xyz( 22, -60, 1 ) == BELOWNORTH ); - REQUIRE( make_xyz( 1, -1, -1 ) == ABOVENORTHEAST ); - REQUIRE( make_xyz( 2, -2, -2 ) == ABOVENORTHEAST ); - REQUIRE( make_xyz( 30, -60, -1 ) == ABOVENORTHEAST ); - REQUIRE( make_xyz( 60, -60, -1 ) == ABOVENORTHEAST ); - REQUIRE( make_xyz( 60, -30, -1 ) == ABOVENORTHEAST ); - REQUIRE( make_xyz( 1, -1, 0 ) == NORTHEAST ); - REQUIRE( make_xyz( 2, -2, 0 ) == NORTHEAST ); - REQUIRE( make_xyz( 30, -60, 0 ) == NORTHEAST ); - REQUIRE( make_xyz( 60, -60, 0 ) == NORTHEAST ); - REQUIRE( make_xyz( 60, -30, 0 ) == NORTHEAST ); - REQUIRE( make_xyz( 1, -1, 1 ) == BELOWNORTHEAST ); - REQUIRE( make_xyz( 2, -2, 2 ) == BELOWNORTHEAST ); - REQUIRE( make_xyz( 30, -60, 1 ) == BELOWNORTHEAST ); - REQUIRE( make_xyz( 60, -60, 1 ) == BELOWNORTHEAST ); - REQUIRE( make_xyz( 60, -30, 1 ) == BELOWNORTHEAST ); + REQUIRE( make_xyz( tripoint( -1, -1, -1 ) ) == ABOVENORTHWEST ); + REQUIRE( make_xyz( tripoint( -2, -2, -2 ) ) == ABOVENORTHWEST ); + REQUIRE( make_xyz( tripoint( -30, -60, -1 ) ) == ABOVENORTHWEST ); + REQUIRE( make_xyz( tripoint( -60, -60, -1 ) ) == ABOVENORTHWEST ); + REQUIRE( make_xyz( tripoint( -60, -30, -1 ) ) == ABOVENORTHWEST ); + REQUIRE( make_xyz( tripoint_north_west ) == NORTHWEST ); + REQUIRE( make_xyz( tripoint( -2, -2, 0 ) ) == NORTHWEST ); + REQUIRE( make_xyz( tripoint( -60, -60, 0 ) ) == NORTHWEST ); + REQUIRE( make_xyz( tripoint( -1, -1, 1 ) ) == BELOWNORTHWEST ); + REQUIRE( make_xyz( tripoint( -2, -2, 2 ) ) == BELOWNORTHWEST ); + REQUIRE( make_xyz( tripoint( -30, -60, 1 ) ) == BELOWNORTHWEST ); + REQUIRE( make_xyz( tripoint( -60, -60, 1 ) ) == BELOWNORTHWEST ); + REQUIRE( make_xyz( tripoint( -60, -30, 1 ) ) == BELOWNORTHWEST ); + REQUIRE( make_xyz( tripoint( 0, -1, -1 ) ) == ABOVENORTH ); + REQUIRE( make_xyz( tripoint( 0, -2, -2 ) ) == ABOVENORTH ); + REQUIRE( make_xyz( tripoint( -22, -60, -1 ) ) == ABOVENORTH ); + REQUIRE( make_xyz( tripoint( 0, -60, -1 ) ) == ABOVENORTH ); + REQUIRE( make_xyz( tripoint( 22, -60, -1 ) ) == ABOVENORTH ); + REQUIRE( make_xyz( tripoint_north ) == NORTH ); + REQUIRE( make_xyz( tripoint( 0, -2, 0 ) ) == NORTH ); + REQUIRE( make_xyz( tripoint( -22, -60, 0 ) ) == NORTH ); + REQUIRE( make_xyz( tripoint( 0, -60, 0 ) ) == NORTH ); + REQUIRE( make_xyz( tripoint( 22, -60, 0 ) ) == NORTH ); + REQUIRE( make_xyz( tripoint( 0, -1, 1 ) ) == BELOWNORTH ); + REQUIRE( make_xyz( tripoint( 0, -2, 2 ) ) == BELOWNORTH ); + REQUIRE( make_xyz( tripoint( -22, -60, 1 ) ) == BELOWNORTH ); + REQUIRE( make_xyz( tripoint( 0, -60, 1 ) ) == BELOWNORTH ); + REQUIRE( make_xyz( tripoint( 22, -60, 1 ) ) == BELOWNORTH ); + REQUIRE( make_xyz( tripoint( 1, -1, -1 ) ) == ABOVENORTHEAST ); + REQUIRE( make_xyz( tripoint( 2, -2, -2 ) ) == ABOVENORTHEAST ); + REQUIRE( make_xyz( tripoint( 30, -60, -1 ) ) == ABOVENORTHEAST ); + REQUIRE( make_xyz( tripoint( 60, -60, -1 ) ) == ABOVENORTHEAST ); + REQUIRE( make_xyz( tripoint( 60, -30, -1 ) ) == ABOVENORTHEAST ); + REQUIRE( make_xyz( tripoint_north_east ) == NORTHEAST ); + REQUIRE( make_xyz( tripoint( 2, -2, 0 ) ) == NORTHEAST ); + REQUIRE( make_xyz( tripoint( 30, -60, 0 ) ) == NORTHEAST ); + REQUIRE( make_xyz( tripoint( 60, -60, 0 ) ) == NORTHEAST ); + REQUIRE( make_xyz( tripoint( 60, -30, 0 ) ) == NORTHEAST ); + REQUIRE( make_xyz( tripoint( 1, -1, 1 ) ) == BELOWNORTHEAST ); + REQUIRE( make_xyz( tripoint( 2, -2, 2 ) ) == BELOWNORTHEAST ); + REQUIRE( make_xyz( tripoint( 30, -60, 1 ) ) == BELOWNORTHEAST ); + REQUIRE( make_xyz( tripoint( 60, -60, 1 ) ) == BELOWNORTHEAST ); + REQUIRE( make_xyz( tripoint( 60, -30, 1 ) ) == BELOWNORTHEAST ); - REQUIRE( make_xyz( -1, 0, -1 ) == ABOVEWEST ); - REQUIRE( make_xyz( -2, 0, -2 ) == ABOVEWEST ); - REQUIRE( make_xyz( -60, -22, -1 ) == ABOVEWEST ); - REQUIRE( make_xyz( -60, 0, -1 ) == ABOVEWEST ); - REQUIRE( make_xyz( -60, 22, -1 ) == ABOVEWEST ); - REQUIRE( make_xyz( -1, 0, 0 ) == WEST ); - REQUIRE( make_xyz( -2, 0, 0 ) == WEST ); - REQUIRE( make_xyz( -60, -22, 0 ) == WEST ); - REQUIRE( make_xyz( -60, 0, 0 ) == WEST ); - REQUIRE( make_xyz( -60, 22, 0 ) == WEST ); - REQUIRE( make_xyz( -1, 0, 1 ) == BELOWWEST ); - REQUIRE( make_xyz( -2, 0, 2 ) == BELOWWEST ); - REQUIRE( make_xyz( -60, -22, 1 ) == BELOWWEST ); - REQUIRE( make_xyz( -60, 0, 1 ) == BELOWWEST ); - REQUIRE( make_xyz( -60, 22, 1 ) == BELOWWEST ); - REQUIRE( make_xyz( 0, 0, -1 ) == ABOVECENTER ); - REQUIRE( make_xyz( 0, 0, -2 ) == ABOVECENTER ); - REQUIRE( make_xyz( 0, 0, 0 ) == CENTER ); - REQUIRE( make_xyz( 0, 0, 1 ) == BELOWCENTER ); - REQUIRE( make_xyz( 0, 0, 2 ) == BELOWCENTER ); - REQUIRE( make_xyz( 1, 0, -1 ) == ABOVEEAST ); - REQUIRE( make_xyz( 2, 0, -2 ) == ABOVEEAST ); - REQUIRE( make_xyz( 60, -22, -1 ) == ABOVEEAST ); - REQUIRE( make_xyz( 60, 0, -1 ) == ABOVEEAST ); - REQUIRE( make_xyz( 60, 22, -1 ) == ABOVEEAST ); - REQUIRE( make_xyz( 1, 0, 0 ) == EAST ); - REQUIRE( make_xyz( 2, 0, 0 ) == EAST ); - REQUIRE( make_xyz( 60, -22, 0 ) == EAST ); - REQUIRE( make_xyz( 60, 0, 0 ) == EAST ); - REQUIRE( make_xyz( 60, 22, 0 ) == EAST ); - REQUIRE( make_xyz( 1, 0, 1 ) == BELOWEAST ); - REQUIRE( make_xyz( 2, 0, 2 ) == BELOWEAST ); - REQUIRE( make_xyz( 60, -22, 1 ) == BELOWEAST ); - REQUIRE( make_xyz( 60, 0, 1 ) == BELOWEAST ); - REQUIRE( make_xyz( 60, 22, 1 ) == BELOWEAST ); + REQUIRE( make_xyz( tripoint( -1, 0, -1 ) ) == ABOVEWEST ); + REQUIRE( make_xyz( tripoint( -2, 0, -2 ) ) == ABOVEWEST ); + REQUIRE( make_xyz( tripoint( -60, -22, -1 ) ) == ABOVEWEST ); + REQUIRE( make_xyz( tripoint( -60, 0, -1 ) ) == ABOVEWEST ); + REQUIRE( make_xyz( tripoint( -60, 22, -1 ) ) == ABOVEWEST ); + REQUIRE( make_xyz( tripoint_west ) == WEST ); + REQUIRE( make_xyz( tripoint( -2, 0, 0 ) ) == WEST ); + REQUIRE( make_xyz( tripoint( -60, -22, 0 ) ) == WEST ); + REQUIRE( make_xyz( tripoint( -60, 0, 0 ) ) == WEST ); + REQUIRE( make_xyz( tripoint( -60, 22, 0 ) ) == WEST ); + REQUIRE( make_xyz( tripoint( -1, 0, 1 ) ) == BELOWWEST ); + REQUIRE( make_xyz( tripoint( -2, 0, 2 ) ) == BELOWWEST ); + REQUIRE( make_xyz( tripoint( -60, -22, 1 ) ) == BELOWWEST ); + REQUIRE( make_xyz( tripoint( -60, 0, 1 ) ) == BELOWWEST ); + REQUIRE( make_xyz( tripoint( -60, 22, 1 ) ) == BELOWWEST ); + REQUIRE( make_xyz( tripoint_below ) == ABOVECENTER ); + REQUIRE( make_xyz( tripoint( 0, 0, -2 ) ) == ABOVECENTER ); + REQUIRE( make_xyz( tripoint_zero ) == CENTER ); + REQUIRE( make_xyz( tripoint_above ) == BELOWCENTER ); + REQUIRE( make_xyz( tripoint( 0, 0, 2 ) ) == BELOWCENTER ); + REQUIRE( make_xyz( tripoint( 1, 0, -1 ) ) == ABOVEEAST ); + REQUIRE( make_xyz( tripoint( 2, 0, -2 ) ) == ABOVEEAST ); + REQUIRE( make_xyz( tripoint( 60, -22, -1 ) ) == ABOVEEAST ); + REQUIRE( make_xyz( tripoint( 60, 0, -1 ) ) == ABOVEEAST ); + REQUIRE( make_xyz( tripoint( 60, 22, -1 ) ) == ABOVEEAST ); + REQUIRE( make_xyz( tripoint_east ) == EAST ); + REQUIRE( make_xyz( tripoint( 2, 0, 0 ) ) == EAST ); + REQUIRE( make_xyz( tripoint( 60, -22, 0 ) ) == EAST ); + REQUIRE( make_xyz( tripoint( 60, 0, 0 ) ) == EAST ); + REQUIRE( make_xyz( tripoint( 60, 22, 0 ) ) == EAST ); + REQUIRE( make_xyz( tripoint( 1, 0, 1 ) ) == BELOWEAST ); + REQUIRE( make_xyz( tripoint( 2, 0, 2 ) ) == BELOWEAST ); + REQUIRE( make_xyz( tripoint( 60, -22, 1 ) ) == BELOWEAST ); + REQUIRE( make_xyz( tripoint( 60, 0, 1 ) ) == BELOWEAST ); + REQUIRE( make_xyz( tripoint( 60, 22, 1 ) ) == BELOWEAST ); - REQUIRE( make_xyz( -1, 1, -1 ) == ABOVESOUTHWEST ); - REQUIRE( make_xyz( -2, 2, -2 ) == ABOVESOUTHWEST ); - REQUIRE( make_xyz( -30, 60, -1 ) == ABOVESOUTHWEST ); - REQUIRE( make_xyz( -60, 60, -1 ) == ABOVESOUTHWEST ); - REQUIRE( make_xyz( -60, 30, -1 ) == ABOVESOUTHWEST ); - REQUIRE( make_xyz( -1, 1, 0 ) == SOUTHWEST ); - REQUIRE( make_xyz( -2, 2, 0 ) == SOUTHWEST ); - REQUIRE( make_xyz( -30, 60, 0 ) == SOUTHWEST ); - REQUIRE( make_xyz( -60, 60, 0 ) == SOUTHWEST ); - REQUIRE( make_xyz( -60, 30, 0 ) == SOUTHWEST ); - REQUIRE( make_xyz( -1, 1, 1 ) == BELOWSOUTHWEST ); - REQUIRE( make_xyz( -2, 2, 2 ) == BELOWSOUTHWEST ); - REQUIRE( make_xyz( -30, 60, 1 ) == BELOWSOUTHWEST ); - REQUIRE( make_xyz( -60, 60, 1 ) == BELOWSOUTHWEST ); - REQUIRE( make_xyz( -60, 30, 1 ) == BELOWSOUTHWEST ); - REQUIRE( make_xyz( 0, 1, -1 ) == ABOVESOUTH ); - REQUIRE( make_xyz( 0, 2, -2 ) == ABOVESOUTH ); - REQUIRE( make_xyz( 0, 60, -1 ) == ABOVESOUTH ); - REQUIRE( make_xyz( 0, 1, 0 ) == SOUTH ); - REQUIRE( make_xyz( -22, 60, 0 ) == SOUTH ); - REQUIRE( make_xyz( 0, 60, 0 ) == SOUTH ); - REQUIRE( make_xyz( 22, 60, 0 ) == SOUTH ); - REQUIRE( make_xyz( 0, 1, 1 ) == BELOWSOUTH ); - REQUIRE( make_xyz( 0, 2, 2 ) == BELOWSOUTH ); - REQUIRE( make_xyz( -22, 60, 1 ) == BELOWSOUTH ); - REQUIRE( make_xyz( 0, 60, 1 ) == BELOWSOUTH ); - REQUIRE( make_xyz( 22, 60, 1 ) == BELOWSOUTH ); - REQUIRE( make_xyz( 1, 1, -1 ) == ABOVESOUTHEAST ); - REQUIRE( make_xyz( 2, 2, -2 ) == ABOVESOUTHEAST ); - REQUIRE( make_xyz( 30, 60, -1 ) == ABOVESOUTHEAST ); - REQUIRE( make_xyz( 60, 60, -1 ) == ABOVESOUTHEAST ); - REQUIRE( make_xyz( 60, 30, -1 ) == ABOVESOUTHEAST ); - REQUIRE( make_xyz( 1, 1, 0 ) == SOUTHEAST ); - REQUIRE( make_xyz( 2, 2, 0 ) == SOUTHEAST ); - REQUIRE( make_xyz( 30, 60, 0 ) == SOUTHEAST ); - REQUIRE( make_xyz( 60, 60, 0 ) == SOUTHEAST ); - REQUIRE( make_xyz( 60, 30, 0 ) == SOUTHEAST ); - REQUIRE( make_xyz( 1, 1, 1 ) == BELOWSOUTHEAST ); - REQUIRE( make_xyz( 2, 2, 2 ) == BELOWSOUTHEAST ); - REQUIRE( make_xyz( 30, 60, 1 ) == BELOWSOUTHEAST ); - REQUIRE( make_xyz( 60, 60, 1 ) == BELOWSOUTHEAST ); - REQUIRE( make_xyz( 60, 30, 1 ) == BELOWSOUTHEAST ); + REQUIRE( make_xyz( tripoint( -1, 1, -1 ) ) == ABOVESOUTHWEST ); + REQUIRE( make_xyz( tripoint( -2, 2, -2 ) ) == ABOVESOUTHWEST ); + REQUIRE( make_xyz( tripoint( -30, 60, -1 ) ) == ABOVESOUTHWEST ); + REQUIRE( make_xyz( tripoint( -60, 60, -1 ) ) == ABOVESOUTHWEST ); + REQUIRE( make_xyz( tripoint( -60, 30, -1 ) ) == ABOVESOUTHWEST ); + REQUIRE( make_xyz( tripoint_south_west ) == SOUTHWEST ); + REQUIRE( make_xyz( tripoint( -2, 2, 0 ) ) == SOUTHWEST ); + REQUIRE( make_xyz( tripoint( -30, 60, 0 ) ) == SOUTHWEST ); + REQUIRE( make_xyz( tripoint( -60, 60, 0 ) ) == SOUTHWEST ); + REQUIRE( make_xyz( tripoint( -60, 30, 0 ) ) == SOUTHWEST ); + REQUIRE( make_xyz( tripoint( -1, 1, 1 ) ) == BELOWSOUTHWEST ); + REQUIRE( make_xyz( tripoint( -2, 2, 2 ) ) == BELOWSOUTHWEST ); + REQUIRE( make_xyz( tripoint( -30, 60, 1 ) ) == BELOWSOUTHWEST ); + REQUIRE( make_xyz( tripoint( -60, 60, 1 ) ) == BELOWSOUTHWEST ); + REQUIRE( make_xyz( tripoint( -60, 30, 1 ) ) == BELOWSOUTHWEST ); + REQUIRE( make_xyz( tripoint( 0, 1, -1 ) ) == ABOVESOUTH ); + REQUIRE( make_xyz( tripoint( 0, 2, -2 ) ) == ABOVESOUTH ); + REQUIRE( make_xyz( tripoint( 0, 60, -1 ) ) == ABOVESOUTH ); + REQUIRE( make_xyz( tripoint_south ) == SOUTH ); + REQUIRE( make_xyz( tripoint( -22, 60, 0 ) ) == SOUTH ); + REQUIRE( make_xyz( tripoint( 0, 60, 0 ) ) == SOUTH ); + REQUIRE( make_xyz( tripoint( 22, 60, 0 ) ) == SOUTH ); + REQUIRE( make_xyz( tripoint( 0, 1, 1 ) ) == BELOWSOUTH ); + REQUIRE( make_xyz( tripoint( 0, 2, 2 ) ) == BELOWSOUTH ); + REQUIRE( make_xyz( tripoint( -22, 60, 1 ) ) == BELOWSOUTH ); + REQUIRE( make_xyz( tripoint( 0, 60, 1 ) ) == BELOWSOUTH ); + REQUIRE( make_xyz( tripoint( 22, 60, 1 ) ) == BELOWSOUTH ); + REQUIRE( make_xyz( tripoint( 1, 1, -1 ) ) == ABOVESOUTHEAST ); + REQUIRE( make_xyz( tripoint( 2, 2, -2 ) ) == ABOVESOUTHEAST ); + REQUIRE( make_xyz( tripoint( 30, 60, -1 ) ) == ABOVESOUTHEAST ); + REQUIRE( make_xyz( tripoint( 60, 60, -1 ) ) == ABOVESOUTHEAST ); + REQUIRE( make_xyz( tripoint( 60, 30, -1 ) ) == ABOVESOUTHEAST ); + REQUIRE( make_xyz( tripoint_south_east ) == SOUTHEAST ); + REQUIRE( make_xyz( tripoint( 2, 2, 0 ) ) == SOUTHEAST ); + REQUIRE( make_xyz( tripoint( 30, 60, 0 ) ) == SOUTHEAST ); + REQUIRE( make_xyz( tripoint( 60, 60, 0 ) ) == SOUTHEAST ); + REQUIRE( make_xyz( tripoint( 60, 30, 0 ) ) == SOUTHEAST ); + REQUIRE( make_xyz( tripoint( 1, 1, 1 ) ) == BELOWSOUTHEAST ); + REQUIRE( make_xyz( tripoint( 2, 2, 2 ) ) == BELOWSOUTHEAST ); + REQUIRE( make_xyz( tripoint( 30, 60, 1 ) ) == BELOWSOUTHEAST ); + REQUIRE( make_xyz( tripoint( 60, 60, 1 ) ) == BELOWSOUTHEAST ); + REQUIRE( make_xyz( tripoint( 60, 30, 1 ) ) == BELOWSOUTHEAST ); } TEST_CASE( "squares_closer_to_test" ) diff --git a/tests/map_memory.cpp b/tests/map_memory.cpp index 20f8a7cbaabe5..21f43811b122a 100644 --- a/tests/map_memory.cpp +++ b/tests/map_memory.cpp @@ -168,56 +168,56 @@ TEST_CASE( "shift_map_memory_seen_cache" ) GIVEN( "all bits are set" ) { test_cache.set(); WHEN( "positive x shift" ) { - shift_bitset_cache( test_cache, 1, 0 ); + shift_bitset_cache( test_cache, point_east ); THEN( "last 12 columns are 0, rest are 1" ) { check_quadrants( test_cache, last_twelve, 0, true, false, true, false ); } } WHEN( "negative x shift" ) { - shift_bitset_cache( test_cache, -1, 0 ); + shift_bitset_cache( test_cache, point_west ); THEN( "first 12 columns are 0, rest are 1" ) { check_quadrants( test_cache, first_twelve, 0, false, true, false, true ); } } WHEN( "positive y shift" ) { - shift_bitset_cache( test_cache, 0, 1 ); + shift_bitset_cache( test_cache, point_south ); THEN( "last 12 rows are 0, rest are 1" ) { check_quadrants( test_cache, 0, last_twelve, true, true, false, false ); } } WHEN( "negative y shift" ) { - shift_bitset_cache( test_cache, 0, -1 ); + shift_bitset_cache( test_cache, point_north ); THEN( "first 12 rows are 0, rest are 1" ) { check_quadrants( test_cache, 0, first_twelve, false, false, true, true ); } } WHEN( "positive x, positive y shift" ) { - shift_bitset_cache( test_cache, 1, 1 ); + shift_bitset_cache( test_cache, point_south_east ); THEN( "last 12 columns and rows are 0, rest are 1" ) { check_quadrants( test_cache, last_twelve, last_twelve, true, false, false, false ); } } WHEN( "positive x, negative y shift" ) { - shift_bitset_cache( test_cache, 1, -1 ); + shift_bitset_cache( test_cache, point_north_east ); THEN( "last 12 columns and first 12 rows are 0, rest are 1" ) { check_quadrants( test_cache, last_twelve, first_twelve, false, false, true, false ); } } WHEN( "negative x, positive y shift" ) { - shift_bitset_cache( test_cache, -1, 1 ); + shift_bitset_cache( test_cache, point_south_west ); THEN( "first 12 columns and last 12 rows are 0, rest are 1" ) { check_quadrants( test_cache, first_twelve, last_twelve, false, true, false, false ); } } WHEN( "negative x, negative y shift" ) { - shift_bitset_cache( test_cache, -1, -1 ); + shift_bitset_cache( test_cache, point_north_west ); THEN( "first 12 columns and rows are 0, rest are 1" ) { check_quadrants( test_cache, first_twelve, first_twelve, false, false, false, true ); diff --git a/tests/vehicle_drag.cpp b/tests/vehicle_drag.cpp index bdbe9968da129..cbb083ad5b423 100644 --- a/tests/vehicle_drag.cpp +++ b/tests/vehicle_drag.cpp @@ -54,8 +54,8 @@ static void clear_game_drag( const ter_id &terrain ) g->m.invalidate_map_cache( 0 ); g->m.build_map_cache( 0, true ); // hard force a rebuild of caches - g->m.shift( 0, 1 ); - g->m.shift( 0, -1 ); + g->m.shift( point_south ); + g->m.shift( point_north ); } From 4db80c78d05b31fc13ea84908b3de96d04b5ebbb Mon Sep 17 00:00:00 2001 From: John Bytheway Date: Sat, 17 Aug 2019 22:39:37 -0400 Subject: [PATCH 3/7] Port function implementations --- src/game.cpp | 18 ++++----- src/game.h | 5 +-- src/line.cpp | 19 +++++++--- src/line.h | 2 - src/map.cpp | 93 +++++++++++++++++++++-------------------------- src/map.h | 18 +-------- src/map_field.cpp | 16 ++++---- 7 files changed, 75 insertions(+), 96 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index 0f30389c6b8cf..a409fc6987b7a 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -243,9 +243,9 @@ std::unique_ptr 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. @@ -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 ); @@ -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; diff --git a/src/game.h b/src/game.h index f36af86f26a58..fbcd343d6ca22 100644 --- a/src/game.h +++ b/src/game.h @@ -124,7 +124,6 @@ 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 @@ -640,8 +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 ); + 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 @@ -807,7 +805,6 @@ class game * 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: /** diff --git a/src/line.cpp b/src/line.cpp index 4e310938c3a63..ecd0605763800 100644 --- a/src/line.cpp +++ b/src/line.cpp @@ -310,17 +310,19 @@ double atan2_degrees( const point &p ) } // This more general version of this function gives correct values for larger values. -unsigned make_xyz( const int x, const int y, const int z ) +unsigned make_xyz( const tripoint &p ) { static constexpr double sixteenth_arc = M_PI / 8; - int vertical_position = ( ( z > 0 ) ? 2u : ( z < 0 ) ? 1u : 0u ) * 9u; - if( x == 0 && y == 0 ) { + int vertical_position = ( ( p.z > 0 ) ? 2u : ( p.z < 0 ) ? 1u : 0u ) * 9u; + if( p.xy() == point_zero ) { return vertical_position; } // Get the arctan of the angle and divide by approximately 22.5 deg to get the octant. // the angle is in, then truncate it and map to the right direction. // You can read 'octant' as being "number of 22.5 degree sections away from due south". - int octant = atan2( x, y ) / sixteenth_arc; + // FIXME: atan2 normally takes arguments in ( y, x ) order. This is + // passing ( x, y ). + int octant = atan2( p.x, p.y ) / sixteenth_arc; switch( octant ) { case 0: return SOUTH + vertical_position; @@ -394,9 +396,14 @@ std::vector continue_line( const std::vector &line, const in return line_to( line.back(), move_along_line( line.back(), line, distance ) ); } -direction direction_from( const int x, const int y, const int z ) noexcept +direction direction_from( const point &p ) noexcept { - return static_cast( make_xyz( tripoint( x, y, z ) ) ); + return static_cast( make_xyz( tripoint( p, 0 ) ) ); +} + +direction direction_from( const tripoint &p ) noexcept +{ + return static_cast( make_xyz( p ) ); } direction direction_from( const int x1, const int y1, const int x2, const int y2 ) noexcept diff --git a/src/line.h b/src/line.h index 94d211e7dd370..65b7eeddda445 100644 --- a/src/line.h +++ b/src/line.h @@ -47,7 +47,6 @@ inline constexpr unsigned make_xyz_unit( const int x, const int y, const int z ) } // This more general version of this function gives correct values for larger inputs. -unsigned make_xyz( int x, int y, int z ); unsigned make_xyz( const tripoint & ); enum direction : unsigned { @@ -82,7 +81,6 @@ enum direction : unsigned { BELOWSOUTHEAST = make_xyz_unit( 1, 1, 1 ), }; -direction direction_from( int x, int y, int z = 0 ) noexcept; direction direction_from( const point &p ) noexcept; direction direction_from( const tripoint &p ) noexcept; direction direction_from( int x1, int y1, int x2, int y2 ) noexcept; diff --git a/src/map.cpp b/src/map.cpp index f113928e76421..a1ec34f095197 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -6454,14 +6454,14 @@ void map::save() } } -void map::load( const int wx, const int wy, const int wz, const bool update_vehicle ) +void map::load( const tripoint &w, const bool update_vehicle ) { for( auto &traps : traplocs ) { traps.clear(); } field_furn_locs.clear(); submaps_with_active_items.clear(); - set_abs_sub( tripoint( wx, wy, wz ) ); + set_abs_sub( w ); for( int gridx = 0; gridx < my_MAPSIZE; gridx++ ) { for( int gridy = 0; gridy < my_MAPSIZE; gridy++ ) { loadn( point( gridx, gridy ), update_vehicle ); @@ -6499,10 +6499,10 @@ void map::shift_traps( const tripoint &shift ) } template -void shift_bitset_cache( std::bitset &cache, const int sx, const int sy ) +void shift_bitset_cache( std::bitset &cache, const point &s ) { // sx shifts by MULTIPLIER rows, sy shifts by MULTIPLIER columns. - int shift_amount = sx * MULTIPLIER + sy * SIZE * MULTIPLIER; + int shift_amount = s.x * MULTIPLIER + s.y * SIZE * MULTIPLIER; if( shift_amount > 0 ) { cache >>= static_cast( shift_amount ); } else if( shift_amount < 0 ) { @@ -6510,10 +6510,10 @@ void shift_bitset_cache( std::bitset &cache, const int sx, const int } // Shifting in the y direction shifted in 0 values, no no additional clearing is necessary, but // a shift in the x direction makes values "wrap" to the next row, and they need to be zeroed. - if( sx == 0 ) { + if( s.x == 0 ) { return; } - const size_t x_offset = sx > 0 ? SIZE - MULTIPLIER : 0; + const size_t x_offset = s.x > 0 ? SIZE - MULTIPLIER : 0; for( size_t y = 0; y < SIZE; ++y ) { size_t y_offset = y * SIZE; for( size_t x = 0; x < MULTIPLIER; ++x ) { @@ -6524,34 +6524,32 @@ void shift_bitset_cache( std::bitset &cache, const int sx, const int template void shift_bitset_cache( std::bitset &cache, - const int sx, const int sy ); + const point &s ); template void -shift_bitset_cache( std::bitset &cache, const int sx, const int sy ); +shift_bitset_cache( std::bitset &cache, const point &s ); -void map::shift( const int sx, const int sy ) +void map::shift( const point &sp ) { // Special case of 0-shift; refresh the map - if( sx == 0 && sy == 0 ) { + if( sp == point_zero ) { return; // Skip this? } - const int absx = get_abs_sub().x; - const int absy = get_abs_sub().y; - const int wz = get_abs_sub().z; + const tripoint abs = get_abs_sub(); - set_abs_sub( tripoint( absx + sx, absy + sy, wz ) ); + set_abs_sub( abs + sp ); // if player is in vehicle, (s)he must be shifted with vehicle too if( g->u.in_vehicle ) { - g->u.setx( g->u.posx() - sx * SEEX ); - g->u.sety( g->u.posy() - sy * SEEY ); + g->u.setx( g->u.posx() - sp.x * SEEX ); + g->u.sety( g->u.posy() - sp.y * SEEY ); } - shift_traps( tripoint( sx, sy, 0 ) ); + shift_traps( tripoint( sp, 0 ) ); vehicle *remoteveh = g->remoteveh(); - const int zmin = zlevels ? -OVERMAP_DEPTH : wz; - const int zmax = zlevels ? OVERMAP_HEIGHT : wz; + const int zmin = zlevels ? -OVERMAP_DEPTH : abs.z; + const int zmax = zlevels ? OVERMAP_HEIGHT : abs.z; for( int gridz = zmin; gridz <= zmax; gridz++ ) { for( vehicle *veh : get_cache( gridz ).vehicle_list ) { veh->zones_dirty = true; @@ -6565,18 +6563,18 @@ void map::shift( const int sx, const int sy ) // Clear vehicle list and rebuild after shift clear_vehicle_cache( gridz ); clear_vehicle_list( gridz ); - shift_bitset_cache( get_cache( gridz ).map_memory_seen_cache, point( sx, sy ) ); - shift_bitset_cache( get_cache( gridz ).field_cache, point( sx, sy ) ); - if( sx >= 0 ) { + shift_bitset_cache( get_cache( gridz ).map_memory_seen_cache, sp ); + shift_bitset_cache( get_cache( gridz ).field_cache, sp ); + if( sp.x >= 0 ) { for( int gridx = 0; gridx < my_MAPSIZE; gridx++ ) { - if( sy >= 0 ) { + if( sp.y >= 0 ) { for( int gridy = 0; gridy < my_MAPSIZE; gridy++ ) { if( gridx == 0 || gridy == 0 ) { - submaps_with_active_items.erase( { absx + gridx, absy + gridy, gridz } ); + submaps_with_active_items.erase( { abs.x + gridx, abs.y + gridy, gridz } ); } - if( gridx + sx < my_MAPSIZE && gridy + sy < my_MAPSIZE ) { + if( gridx + sp.x < my_MAPSIZE && gridy + sp.y < my_MAPSIZE ) { copy_grid( tripoint( gridx, gridy, gridz ), - tripoint( gridx + sx, gridy + sy, gridz ) ); + tripoint( gridx + sp.x, gridy + sp.y, gridz ) ); update_vehicle_list( get_submap_at_grid( {gridx, gridy, gridz} ), gridz ); } else { loadn( tripoint( gridx, gridy, gridz ), true ); @@ -6585,11 +6583,11 @@ void map::shift( const int sx, const int sy ) } else { // sy < 0; work through it backwards for( int gridy = my_MAPSIZE - 1; gridy >= 0; gridy-- ) { if( gridx == 0 || gridy == my_MAPSIZE - 1 ) { - submaps_with_active_items.erase( { absx + gridx, absy + gridy, gridz } ); + submaps_with_active_items.erase( { abs.x + gridx, abs.y + gridy, gridz } ); } - if( gridx + sx < my_MAPSIZE && gridy + sy >= 0 ) { + if( gridx + sp.x < my_MAPSIZE && gridy + sp.y >= 0 ) { copy_grid( tripoint( gridx, gridy, gridz ), - tripoint( gridx + sx, gridy + sy, gridz ) ); + tripoint( gridx + sp.x, gridy + sp.y, gridz ) ); update_vehicle_list( get_submap_at_grid( { gridx, gridy, gridz } ), gridz ); } else { loadn( tripoint( gridx, gridy, gridz ), true ); @@ -6599,14 +6597,14 @@ void map::shift( const int sx, const int sy ) } } else { // sx < 0; work through it backwards for( int gridx = my_MAPSIZE - 1; gridx >= 0; gridx-- ) { - if( sy >= 0 ) { + if( sp.y >= 0 ) { for( int gridy = 0; gridy < my_MAPSIZE; gridy++ ) { if( gridx == my_MAPSIZE - 1 || gridy == 0 ) { - submaps_with_active_items.erase( { absx + gridx, absy + gridy, gridz } ); + submaps_with_active_items.erase( { abs.x + gridx, abs.y + gridy, gridz } ); } - if( gridx + sx >= 0 && gridy + sy < my_MAPSIZE ) { + if( gridx + sp.x >= 0 && gridy + sp.y < my_MAPSIZE ) { copy_grid( tripoint( gridx, gridy, gridz ), - tripoint( gridx + sx, gridy + sy, gridz ) ); + tripoint( gridx + sp.x, gridy + sp.y, gridz ) ); update_vehicle_list( get_submap_at_grid( { gridx, gridy, gridz } ), gridz ); } else { loadn( tripoint( gridx, gridy, gridz ), true ); @@ -6615,11 +6613,11 @@ void map::shift( const int sx, const int sy ) } else { // sy < 0; work through it backwards for( int gridy = my_MAPSIZE - 1; gridy >= 0; gridy-- ) { if( gridx == my_MAPSIZE - 1 || gridy == my_MAPSIZE - 1 ) { - submaps_with_active_items.erase( { absx + gridx, absy + gridy, gridz } ); + submaps_with_active_items.erase( { abs.x + gridx, abs.y + gridy, gridz } ); } - if( gridx + sx >= 0 && gridy + sy >= 0 ) { + if( gridx + sp.x >= 0 && gridy + sp.y >= 0 ) { copy_grid( tripoint( gridx, gridy, gridz ), - tripoint( gridx + sx, gridy + sy, gridz ) ); + tripoint( gridx + sp.x, gridy + sp.y, gridz ) ); update_vehicle_list( get_submap_at_grid( { gridx, gridy, gridz } ), gridz ); } else { loadn( tripoint( gridx, gridy, gridz ), true ); @@ -6638,7 +6636,7 @@ void map::shift( const int sx, const int sy ) std::set old_cache = std::move( support_cache_dirty ); support_cache_dirty.clear(); for( const auto &pt : old_cache ) { - support_cache_dirty.insert( pt + point( -sx * SEEX, -sy * SEEY ) ); + support_cache_dirty.insert( pt + point( -sp.x * SEEX, -sp.y * SEEY ) ); } } } @@ -8227,22 +8225,15 @@ void map::creature_on_trap( Creature &c, const bool may_avoid ) template void map::function_over( const tripoint &start, const tripoint &end, Functor fun ) const -{ - function_over( start.x, start.y, start.z, end.x, end.y, end.z, fun ); -} - -template -void map::function_over( const int stx, const int sty, const int stz, - const int enx, const int eny, const int enz, Functor fun ) const { // start and end are just two points, end can be "before" start // Also clip the area to map area - const int minx = std::max( std::min( stx, enx ), 0 ); - const int miny = std::max( std::min( sty, eny ), 0 ); - const int minz = std::max( std::min( stz, enz ), -OVERMAP_DEPTH ); - const int maxx = std::min( std::max( stx, enx ), SEEX * my_MAPSIZE - 1 ); - const int maxy = std::min( std::max( sty, eny ), SEEY * my_MAPSIZE - 1 ); - const int maxz = std::min( std::max( stz, enz ), OVERMAP_HEIGHT ); + const int minx = std::max( std::min( start.x, end.x ), 0 ); + const int miny = std::max( std::min( start.y, end.y ), 0 ); + const int minz = std::max( std::min( start.z, end.z ), -OVERMAP_DEPTH ); + const int maxx = std::min( std::max( start.x, end.x ), SEEX * my_MAPSIZE - 1 ); + const int maxy = std::min( std::max( start.y, end.y ), SEEY * my_MAPSIZE - 1 ); + const int maxz = std::min( std::max( start.z, end.z ), OVERMAP_HEIGHT ); // Submaps that contain the bounding points const int min_smx = minx / SEEX; @@ -8316,7 +8307,7 @@ void map::scent_blockers( std::array, MAPSIZE_Y> &bl return ITER_CONTINUE; }; - function_over( min.x, min.y, abs_sub.z, max.x, max.y, abs_sub.z, fill_values ); + function_over( tripoint( min, abs_sub.z ), tripoint( max, abs_sub.z ), fill_values ); const rectangle local_bounds( min, max ); diff --git a/src/map.h b/src/map.h index ed10f8bd35f52..ca6253c88c4d7 100644 --- a/src/map.h +++ b/src/map.h @@ -338,23 +338,17 @@ class map * the @ref mapbuffer can not deliver the requested submap (as it does * not exist on disc). * This must be called before the map can be used at all! - * @param wx global coordinates of the submap at grid[0]. This + * @param p global coordinates of the submap at grid[0]. This * is in submap coordinates. - * @param wy see wx - * @param wz see wx, this is the z-level * @param update_vehicles If true, add vehicles to the vehicle cache. */ - void load( int wx, int wy, int wz, bool update_vehicles ); - void load( const tripoint &p, bool update_vehicles ) { - load( p.x, p.y, p.z, update_vehicles ); - } + void load( const tripoint &p, bool update_vehicles ); /** * Shift the map along the vector s. * This is like loading the map with coordinates derived from the current * position of the map (@ref abs_sub) plus the shift vector. * Note: the map must have been loaded before this can be called. */ - void shift( int sx, int sy ); void shift( const point &s ); /** * Moves the map vertically to (not by!) newz. @@ -1051,8 +1045,6 @@ class map //Spawns byproducts from items destroyed in fire. void create_burnproducts( const tripoint &p, const item &fuel, const units::mass &burned_mass ); bool process_fields(); // See fields.cpp - bool process_fields_in_submap( submap *current_submap, - int submap_x, int submap_y, int submap_z ); // See fields.cpp bool process_fields_in_submap( submap *current_submap, const tripoint &submap_pos ); // See fields.cpp /** @@ -1630,10 +1622,6 @@ class map /*@{*/ template void function_over( const tripoint &start, const tripoint &end, Functor fun ) const; - template - void function_over( int stx, int sty, int stz, const tripoint &end, Functor fun ) const; - template - void function_over( int stx, int sty, int stz, int enx, int eny, int enz, Functor fun ) const; /*@}*/ /** @@ -1708,8 +1696,6 @@ class map bool need_draw_lower_floor( const tripoint &p ); }; -template -void shift_bitset_cache( std::bitset &cache, int sx, int sy ); template void shift_bitset_cache( std::bitset &cache, const point &s ); diff --git a/src/map_field.cpp b/src/map_field.cpp index 355c80dc633dd..b23dc7bec4eb3 100644 --- a/src/map_field.cpp +++ b/src/map_field.cpp @@ -365,9 +365,9 @@ This is the general update function for field effects. This should only be calle If you need to insert a new field behavior per unit time add a case statement in the switch below. */ bool map::process_fields_in_submap( submap *const current_submap, - const int submap_x, const int submap_y, const int submap_z ) + const tripoint &submap ) { - scent_block sblk( submap_x, submap_y, submap_z, g->scent ); + scent_block sblk( submap.x, submap.y, submap.z, g->scent ); // This should be true only when the field changes transparency // More correctly: not just when the field is opaque, but when it changes state @@ -378,7 +378,7 @@ bool map::process_fields_in_submap( submap *const current_submap, field_entry *tmpfld = nullptr; tripoint thep; - thep.z = submap_z; + thep.z = submap.z; // Initialize the map tile wrapper maptile map_tile( current_submap, 0, 0 ); @@ -389,8 +389,8 @@ bool map::process_fields_in_submap( submap *const current_submap, for( locy = 0; locy < SEEY; locy++ ) { // This is a translation from local coordinates to submap coordinates. // All submaps are in one long 1d array. - thep.x = locx + submap_x * SEEX; - thep.y = locy + submap_y * SEEY; + thep.x = locx + submap.x * SEEX; + thep.y = locy + submap.y * SEEY; // A const reference to the tripoint above, so that the code below doesn't accidentally change it const tripoint &p = thep; // Get a reference to the field variable from the submap; @@ -1299,10 +1299,10 @@ bool map::process_fields_in_submap( submap *const current_submap, } const int minz = zlevels ? -OVERMAP_DEPTH : abs_sub.z; const int maxz = zlevels ? OVERMAP_HEIGHT : abs_sub.z; - for( int z = std::max( submap_z - 1, minz ); z <= std::min( submap_z + 1, maxz ); ++z ) { + for( int z = std::max( submap.z - 1, minz ); z <= std::min( submap.z + 1, maxz ); ++z ) { auto &field_cache = get_cache( z ).field_cache; - for( int y = std::max( submap_y - 1, 0 ); y <= std::min( submap_y + 1, MAPSIZE - 1 ); ++y ) { - for( int x = std::max( submap_x - 1, 0 ); x <= std::min( submap_x + 1, MAPSIZE - 1 ); ++x ) { + for( int y = std::max( submap.y - 1, 0 ); y <= std::min( submap.y + 1, MAPSIZE - 1 ); ++y ) { + for( int x = std::max( submap.x - 1, 0 ); x <= std::min( submap.x + 1, MAPSIZE - 1 ); ++x ) { if( get_submap_at_grid( { x, y, z } )->field_count > 0 ) { field_cache.set( x + y * MAPSIZE ); } else { From 9dc6451721cbe175addfd4763883594602b302aa Mon Sep 17 00:00:00 2001 From: John Bytheway Date: Sun, 18 Aug 2019 16:26:25 -0400 Subject: [PATCH 4/7] Add overloads in line.h --- src/line.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/line.h b/src/line.h index 65b7eeddda445..fab466e1cc6a0 100644 --- a/src/line.h +++ b/src/line.h @@ -84,6 +84,8 @@ enum direction : unsigned { direction direction_from( const point &p ) noexcept; direction direction_from( const tripoint &p ) noexcept; direction direction_from( int x1, int y1, int x2, int y2 ) noexcept; +direction direction_from( const point &p1, int x2, int y2 ) noexcept; +direction direction_from( const point &p1, const point &p2 ) noexcept; direction direction_from( const tripoint &p, const tripoint &q ); point direction_XY( direction dir ); @@ -99,6 +101,10 @@ std::string direction_suffix( const tripoint &p, const tripoint &q ); */ void bresenham( int x1, int y1, int x2, int y2, int t, const std::function &interact ); +void bresenham( const point &p1, int x2, int y2, int t, + const std::function &interact ); +void bresenham( const point &p1, const point &p2, int t, + const std::function &interact ); void bresenham( const tripoint &loc1, const tripoint &loc2, int t, int t2, const std::function &interact ); @@ -106,18 +112,22 @@ tripoint move_along_line( const tripoint &loc, const std::vector &line int distance ); // The "t" value decides WHICH Bresenham line is used. std::vector line_to( int x1, int y1, int x2, int y2, int t = 0 ); +std::vector line_to( const point &p1, int x2, int y2, int t = 0 ); std::vector line_to( const point &p1, const point &p2, int t = 0 ); // t and t2 decide which Bresenham line is used. std::vector line_to( const tripoint &loc1, const tripoint &loc2, int t = 0, int t2 = 0 ); // sqrt(dX^2 + dY^2) float trig_dist( int x1, int y1, int x2, int y2 ); +float trig_dist( const point &p1, int x2, int y2 ); float trig_dist( const point &loc1, const point &loc2 ); float trig_dist( const tripoint &loc1, const tripoint &loc2 ); // Roguelike distance; minimum of dX and dY int square_dist( int x1, int y1, int x2, int y2 ); +int square_dist( const point &p1, int x2, int y2 ); int square_dist( const point &loc1, const point &loc2 ); int square_dist( const tripoint &loc1, const tripoint &loc2 ); int rl_dist( int x1, int y1, int x2, int y2 ); +int rl_dist( const point &p1, int x2, int y2 ); int rl_dist( const tripoint &loc1, const tripoint &loc2 ); int rl_dist( const point &a, const point &b ); // Sum of distance in both axes @@ -131,6 +141,8 @@ double atan2_degrees( const point & ); float get_normalized_angle( const point &start, const point &end ); std::vector continue_line( const std::vector &line, int distance ); std::vector squares_in_direction( int x1, int y1, int x2, int y2 ); +std::vector squares_in_direction( const point &p1, int x2, int y2 ); +std::vector squares_in_direction( const point &p1, const point &p2 ); // Returns a vector of squares adjacent to @from that are closer to @to than @from is. // Currently limited to the same z-level as @from. std::vector squares_closer_to( const tripoint &from, const tripoint &to ); From ee8d5721bffe8e173a713a0c7299b8467e6f0161 Mon Sep 17 00:00:00 2001 From: John Bytheway Date: Sun, 18 Aug 2019 19:58:15 -0400 Subject: [PATCH 5/7] Refactor callers of line functions --- src/creature.cpp | 5 ++--- src/drawing_primitives.cpp | 4 ++-- src/faction_camp.cpp | 16 ++++++++-------- src/game.cpp | 16 ++++++++-------- src/iuse.cpp | 4 ++-- src/map.cpp | 6 +++--- src/map_extras.cpp | 24 ++++++++++++------------ src/map_field.cpp | 2 +- src/mapgen.cpp | 19 ++++++++++--------- src/mapgen_functions.cpp | 20 ++++++++++---------- src/melee.cpp | 4 ++-- src/mission_util.cpp | 3 +-- src/monattack.cpp | 2 +- src/monmove.cpp | 4 ++-- src/overmap.cpp | 21 +++++++++++---------- src/player.cpp | 4 ++-- src/sounds.cpp | 4 ++-- src/timed_event.cpp | 2 +- src/vehicle.cpp | 6 +++--- tests/line_test.cpp | 11 ++++++----- tests/shadowcasting_test.cpp | 2 +- 21 files changed, 90 insertions(+), 89 deletions(-) diff --git a/src/creature.cpp b/src/creature.cpp index 8cdd92008f948..7b8daee17e97e 100644 --- a/src/creature.cpp +++ b/src/creature.cpp @@ -768,14 +768,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" ); diff --git a/src/drawing_primitives.cpp b/src/drawing_primitives.cpp index e379118f3b638..b87e6738883d1 100644 --- a/src/drawing_primitives.cpp +++ b/src/drawing_primitives.cpp @@ -36,7 +36,7 @@ void draw_rough_circle( std::functionset, 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 ) ); } } @@ -58,7 +58,7 @@ void draw_circle( std::functionset, 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 ) ); } } diff --git a/src/faction_camp.cpp b/src/faction_camp.cpp index 8095996251070..a743ee394daee 100644 --- a/src/faction_camp.cpp +++ b/src/faction_camp.cpp @@ -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?) @@ -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 ); @@ -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 pos_inv = tgt_inv.items_with( []( const item & itm ) { return !itm.can_revive(); @@ -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 pos_inv = tgt_inv.items_with( []( const item & itm ) { return !itm.can_revive(); @@ -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 ); @@ -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; @@ -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 ); @@ -3101,7 +3101,7 @@ std::vector om_companion_path( const tripoint &start, int range_start, std::vector 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 ); diff --git a/src/game.cpp b/src/game.cpp index a409fc6987b7a..9aff79a300d0b 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -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( mouse_target ) ) { if( !u.sees( *mon ) ) { add_msg( _( "Nothing relevant here." ) ); @@ -3194,8 +3194,8 @@ struct npc_dist_to_player { bool operator()( const std::shared_ptr &a, const std::shared_ptr &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() ); } }; @@ -3893,7 +3893,7 @@ 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( c ); const auto p = dynamic_cast( 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; @@ -7507,8 +7507,8 @@ game::vmenu_ret game::list_items( const std::vector &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 { @@ -10022,7 +10022,7 @@ static cata::optional point_selection_menu( const std::vectorglobal_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(); } diff --git a/src/map.cpp b/src/map.cpp index a1ec34f095197..a1a0951c57f52 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -6093,7 +6093,7 @@ bool map::sees( const tripoint &F, const tripoint &T, const int range, int &bres // Ugly `if` for now if( !fov_3d || F.z == T.z ) { - bresenham( F.x, F.y, T.x, T.y, bresenham_slope, + bresenham( F.xy(), T.xy(), bresenham_slope, [this, &visible, &T]( const point & new_point ) { // Exit before checking the last square, it's still visible even if opaque. if( new_point.x == T.x && new_point.y == T.y ) { @@ -6329,12 +6329,12 @@ bool map::clear_path( const tripoint &f, const tripoint &t, const int range, } if( f.z == t.z ) { - if( ( range >= 0 && range < rl_dist( f.x, f.y, t.x, t.y ) ) || + if( ( range >= 0 && range < rl_dist( f.xy(), t.xy() ) ) || !inbounds( t ) ) { return false; // Out of range! } bool is_clear = true; - bresenham( f.x, f.y, t.x, t.y, 0, + bresenham( f.xy(), t.xy(), 0, [this, &is_clear, cost_min, cost_max, &t]( const point & new_point ) { // Exit before checking the last square, it's still reachable even if it is an obstacle. if( new_point.x == t.x && new_point.y == t.y ) { diff --git a/src/map_extras.cpp b/src/map_extras.cpp index 9ae22763c80a7..697452e67cab0 100644 --- a/src/map_extras.cpp +++ b/src/map_extras.cpp @@ -941,7 +941,7 @@ static void mx_minefield( map &m, const tripoint &abs_sub ) } //33% chance to spawn empty magazines used by soldiers - std::vector empty_magazines_locations = line_to( 15, 5, 20, 5 ); + std::vector empty_magazines_locations = line_to( point( 15, 5 ), point( 20, 5 ) ); for( auto &i : empty_magazines_locations ) { if( one_in( 3 ) ) { m.spawn_item( { i, abs_sub.z }, "stanag30" ); @@ -951,7 +951,7 @@ static void mx_minefield( map &m, const tripoint &abs_sub ) //Horizontal line of barbed wire fence line( &m, t_fence_barbed, 0, 9, SEEX * 2, 9 ); - std::vector barbed_wire = line_to( 0, 9, SEEX * 2, 9 ); + std::vector barbed_wire = line_to( point( 0, 9 ), point( SEEX * 2, 9 ) ); for( auto &i : barbed_wire ) { //10% chance to spawn corpses of bloody people/zombies on every tile of barbed wire fence if( one_in( 10 ) ) { @@ -1008,7 +1008,7 @@ static void mx_minefield( map &m, const tripoint &abs_sub ) line( &m, t_fence_barbed, 3, 13, 3, 17 ); line( &m, t_fence_barbed, SEEX * 2 - 4, 13, SEEX * 2 - 4, 17 ); - std::vector barbed_wire = line_to( 3, 13, SEEX * 2 - 4, 13 ); + std::vector barbed_wire = line_to( point( 3, 13 ), point( SEEX * 2 - 4, 13 ) ); for( auto &i : barbed_wire ) { //10% chance to spawn corpses of bloody people/zombies on every tile of barbed wire fence if( one_in( 10 ) ) { @@ -1048,7 +1048,7 @@ static void mx_minefield( map &m, const tripoint &abs_sub ) } //33% chance to spawn empty magazines used by soldiers - std::vector empty_magazines_locations = line_to( 5, 16, 18, 16 ); + std::vector empty_magazines_locations = line_to( point( 5, 16 ), point( 18, 16 ) ); for( auto &i : empty_magazines_locations ) { if( one_in( 3 ) ) { m.spawn_item( { i, abs_sub.z }, "stanag30" ); @@ -1123,7 +1123,7 @@ static void mx_minefield( map &m, const tripoint &abs_sub ) //33% chance for a crazy maniac ramming the tent with some unfortunate inside if( one_in( 3 ) ) { //Blood and gore - std::vector blood_track = line_to( 1, 6, 8, 6 ); + std::vector blood_track = line_to( point( 1, 6 ), point( 8, 6 ) ); for( auto &i : blood_track ) { m.add_field( { i, abs_sub.z }, fd_blood, 1 ); } @@ -1140,7 +1140,7 @@ static void mx_minefield( map &m, const tripoint &abs_sub ) line_furn( &m, f_null, 10, 7, 10, 8 ); //Spill sand from damaged sandbags - std::vector sandbag_positions = squares_in_direction( 10, 7, 11, 8 ); + std::vector sandbag_positions = squares_in_direction( point( 10, 7 ), point( 11, 8 ) ); for( auto &i : sandbag_positions ) { m.spawn_item( { i, abs_sub.z }, "bag_canvas", rng( 5, 13 ) ); m.spawn_item( { i, abs_sub.z }, "material_sand", rng( 3, 8 ) ); @@ -1157,7 +1157,7 @@ static void mx_minefield( map &m, const tripoint &abs_sub ) } //33% chance to spawn empty magazines used by soldiers - std::vector empty_magazines_locations = line_to( 9, 3, 9, 13 ); + std::vector empty_magazines_locations = line_to( point( 9, 3 ), point( 9, 13 ) ); for( auto &i : empty_magazines_locations ) { if( one_in( 3 ) ) { m.spawn_item( { i, abs_sub.z }, "stanag30" ); @@ -1189,14 +1189,14 @@ static void mx_minefield( map &m, const tripoint &abs_sub ) } //33% chance to spawn empty magazines used by soldiers - std::vector empty_magazines_locations = line_to( 9, 16, 9, 20 ); + std::vector empty_magazines_locations = line_to( point( 9, 16 ), point( 9, 20 ) ); for( auto &i : empty_magazines_locations ) { if( one_in( 3 ) ) { m.spawn_item( { i, abs_sub.z }, "stanag30" ); } } - std::vector barbed_wire = line_to( 12, 3, 12, 20 ); + std::vector barbed_wire = line_to( point( 12, 3 ), point( 12, 20 ) ); for( auto &i : barbed_wire ) { //10% chance to spawn corpses of bloody people/zombies on every tile of barbed wire fence if( one_in( 10 ) ) { @@ -1269,7 +1269,7 @@ static void mx_minefield( map &m, const tripoint &abs_sub ) } //33% chance to spawn empty magazines used by soldiers - std::vector empty_magazines_locations = line_to( 15, 2, 15, 8 ); + std::vector empty_magazines_locations = line_to( point( 15, 2 ), point( 15, 8 ) ); for( auto &i : empty_magazines_locations ) { if( one_in( 3 ) ) { m.spawn_item( { i, abs_sub.z }, "stanag30" ); @@ -1398,7 +1398,7 @@ static void place_fumarole( map &m, int x1, int y1, int x2, int y2, std::set ignited; - std::vector fumarole = line_to( x1, y1, x2, y2, 0 ); + std::vector fumarole = line_to( point( x1, y1 ), point( x2, y2 ), 0 ); for( auto &i : fumarole ) { m.ter_set( i, t_lava ); @@ -1517,7 +1517,7 @@ static void mx_portal_in( map &m, const tripoint &abs_sub ) const int rad = 10; for( int i = x - rad; i <= x + rad; i++ ) { for( int j = y - rad; j <= y + rad; j++ ) { - if( trig_dist( x, y, i, j ) + rng( 0, 3 ) <= rad ) { + if( trig_dist( point( x, y ), point( i, j ) ) + rng( 0, 3 ) <= rad ) { const tripoint loc( i, j, abs_sub.z ); dead_vegetation_parser( m, loc ); m.adjust_radiation( loc.xy(), rng( 20, 40 ) ); diff --git a/src/map_field.cpp b/src/map_field.cpp index b23dc7bec4eb3..bf3beefb01282 100644 --- a/src/map_field.cpp +++ b/src/map_field.cpp @@ -1228,7 +1228,7 @@ bool map::process_fields_in_submap( submap *const current_submap, clear_path( p, g->u.pos(), 10, 0, 100 ) ) { std::vector candidate_positions = - squares_in_direction( p.x, p.y, g->u.posx(), g->u.posy() ); + squares_in_direction( p.xy(), point( g->u.posx(), g->u.posy() ) ); for( point &candidate_position : candidate_positions ) { field &target_field = get_field( tripoint( candidate_position, p.z ) ); diff --git a/src/mapgen.cpp b/src/mapgen.cpp index 3db4ebff11d77..26aa4012a3d5c 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -2442,7 +2442,8 @@ bool jmapgen_setmap::apply( const mapgendata &dat, const point &offset, mission } break; case JMAPGEN_SETMAP_LINE_TRAP: { - const std::vector line = line_to( x_get(), y_get(), x2_get(), y2_get(), 0 ); + const std::vector line = line_to( point( x_get(), y_get() ), point( x2_get(), y2_get() ), + 0 ); for( auto &i : line ) { // TODO: the trap_id should be stored separately and not be wrapped in an jmapgen_int mtrap_set( &m, i.x, i.y, trap_id( val.get() ) ); @@ -2450,7 +2451,8 @@ bool jmapgen_setmap::apply( const mapgendata &dat, const point &offset, mission } break; case JMAPGEN_SETMAP_LINE_RADIATION: { - const std::vector line = line_to( x_get(), y_get(), x2_get(), y2_get(), 0 ); + const std::vector line = line_to( point( x_get(), y_get() ), point( x2_get(), y2_get() ), + 0 ); for( auto &i : line ) { m.set_radiation( i, static_cast( val.get() ) ); } @@ -4348,7 +4350,7 @@ void map::draw_silo( const oter_id &terrain_type, mapgendata &dat, const time_po if( dat.zlevel == 0 ) { // We're on ground level for( int i = 0; i < SEEX * 2; i++ ) { for( int j = 0; j < SEEY * 2; j++ ) { - if( trig_dist( i, j, SEEX, SEEY ) <= 6 ) { + if( trig_dist( point( i, j ), point( SEEX, SEEY ) ) <= 6 ) { ter_set( point( i, j ), t_metal_floor ); } else { ter_set( point( i, j ), dat.groundcover() ); @@ -4389,14 +4391,14 @@ void map::draw_silo( const oter_id &terrain_type, mapgendata &dat, const time_po } else { // We are NOT above ground. for( int i = 0; i < SEEX * 2; i++ ) { for( int j = 0; j < SEEY * 2; j++ ) { - if( trig_dist( i, j, SEEX, SEEY ) > 7 ) { + if( trig_dist( point( i, j ), point( SEEX, SEEY ) ) > 7 ) { ter_set( point( i, j ), t_rock ); - } else if( trig_dist( i, j, SEEX, SEEY ) > 5 ) { + } else if( trig_dist( point( i, j ), point( SEEX, SEEY ) ) > 5 ) { ter_set( point( i, j ), t_metal_floor ); if( one_in( 30 ) ) { add_field( {i, j, abs_sub.z}, fd_nuke_gas, 2 ); } - } else if( trig_dist( i, j, SEEX, SEEY ) == 5 ) { + } else if( trig_dist( point( i, j ), point( SEEX, SEEY ) ) == 5 ) { ter_set( point( i, j ), t_hole ); } else { ter_set( point( i, j ), t_missile ); @@ -7877,8 +7879,7 @@ void silo_rooms( map *m ) int best_dist = 999; int closest = 0; for( size_t i = 1; i < rooms.size(); i++ ) { - int dist = trig_dist( first_room_position.x, first_room_position.y, rooms[i].first.x, - rooms[i].first.y ); + int dist = trig_dist( first_room_position, rooms[i].first ); if( dist < best_dist ) { best_dist = dist; closest = i; @@ -8192,7 +8193,7 @@ void map::create_anomaly( const tripoint &cp, artifact_natural_property prop, bo for( int i = cx - 5; i <= cx + 5; i++ ) { for( int j = cy - 5; j <= cy + 5; j++ ) { if( furn( point( i, j ) ) == f_rubble ) { - add_field( {i, j, abs_sub.z}, fd_fire_vent, 1 + ( rl_dist( cx, cy, i, j ) % 3 ) ); + add_field( {i, j, abs_sub.z}, fd_fire_vent, 1 + ( rl_dist( point( cx, cy ), point( i, j ) ) % 3 ) ); } } } diff --git a/src/mapgen_functions.cpp b/src/mapgen_functions.cpp index 746e580d910ed..dd0995957d1bc 100644 --- a/src/mapgen_functions.cpp +++ b/src/mapgen_functions.cpp @@ -648,7 +648,7 @@ void mapgen_fungal_bloom( map *m, oter_id, mapgendata dat, const time_point &, f ( void )dat; for( int i = 0; i < SEEX * 2; i++ ) { for( int j = 0; j < SEEY * 2; j++ ) { - if( one_in( rl_dist( i, j, 12, 12 ) * 4 ) ) { + if( one_in( rl_dist( point( i, j ), point( 12, 12 ) ) * 4 ) ) { m->ter_set( point( i, j ), t_marloss ); } else if( one_in( 10 ) ) { if( one_in( 3 ) ) { @@ -698,7 +698,7 @@ void mapgen_fungal_flowers( map *m, oter_id, mapgendata dat, const time_point &, ( void )dat; for( int i = 0; i < SEEX * 2; i++ ) { for( int j = 0; j < SEEY * 2; j++ ) { - if( one_in( rl_dist( i, j, 12, 12 ) * 6 ) ) { + if( one_in( rl_dist( point( i, j ), point( 12, 12 ) ) * 6 ) ) { m->ter_set( point( i, j ), t_fungus ); m->furn_set( point( i, j ), f_flower_marloss ); } else if( one_in( 10 ) ) { @@ -1517,10 +1517,10 @@ void mapgen_sewer_four_way( map *m, oter_id, mapgendata dat, const time_point &t } else { m->ter_set( point( i, j ), t_sewage ); } - if( rn == 0 && ( trig_dist( i, j, SEEX - 1, SEEY - 1 ) <= 6 || - trig_dist( i, j, SEEX - 1, SEEY ) <= 6 || - trig_dist( i, j, SEEX, SEEY - 1 ) <= 6 || - trig_dist( i, j, SEEX, SEEY ) <= 6 ) ) { + if( rn == 0 && ( trig_dist( point( i, j ), point( SEEX - 1, SEEY - 1 ) ) <= 6 || + trig_dist( point( i, j ), point( SEEX - 1, SEEY ) ) <= 6 || + trig_dist( point( i, j ), point( SEEX, SEEY - 1 ) ) <= 6 || + trig_dist( point( i, j ), point( SEEX, SEEY ) ) <= 6 ) ) { m->ter_set( point( i, j ), t_sewage ); } if( rn == 0 && ( i == SEEX - 1 || i == SEEX ) && ( j == SEEY - 1 || j == SEEY ) ) { @@ -3008,7 +3008,7 @@ void mapgen_cave( map *m, oter_id, mapgendata dat, const time_point &turn, float origy = rng( SEEY - 1, SEEY ), hermx = rng( SEEX - 6, SEEX + 5 ), hermy = rng( SEEX - 6, SEEY + 5 ); - std::vector bloodline = line_to( origx, origy, hermx, hermy, 0 ); + std::vector bloodline = line_to( point( origx, origy ), point( hermx, hermy ), 0 ); for( auto &ii : bloodline ) { madd_field( m, ii.x, ii.y, fd_blood, 2 ); } @@ -3052,7 +3052,7 @@ void mapgen_cave( map *m, oter_id, mapgendata dat, const time_point &turn, float pathx = ( one_in( 2 ) ? SEEX - 8 : SEEX + 7 ); pathy = rng( SEEY - 6, SEEY + 5 ); } - std::vector pathline = line_to( pathx, pathy, SEEX - 1, SEEY - 1, 0 ); + std::vector pathline = line_to( point( pathx, pathy ), point( SEEX - 1, SEEY - 1 ), 0 ); for( auto &ii : pathline ) { square( m, t_dirt, ii.x, ii.y, ii.x + 1, ii.y + 1 ); @@ -3109,7 +3109,7 @@ void mapgen_cave_rat( map *m, oter_id, mapgendata dat, const time_point &turn, f // Now draw some extra passages! do { int tox = ( one_in( 2 ) ? 2 : SEEX * 2 - 3 ), toy = rng( 2, SEEY * 2 - 3 ); - std::vector path = line_to( centerx, SEEY - 1, tox, toy, 0 ); + std::vector path = line_to( point( centerx, SEEY - 1 ), point( tox, toy ), 0 ); for( auto &i : path ) { for( int cx = i.x - 1; cx <= i.x + 1; cx++ ) { for( int cy = i.y - 1; cy <= i.y + 1; cy++ ) { @@ -3613,7 +3613,7 @@ static void mapgen_ants_generic( map *m, oter_id terrain_type, mapgendata dat, } while( m->ter( point( x, y ) ) == t_rock ); for( int i = x - cw; i <= x + cw; i++ ) { for( int j = y - cw; j <= y + cw; j++ ) { - if( trig_dist( x, y, i, j ) <= cw ) { + if( trig_dist( point( x, y ), point( i, j ) ) <= cw ) { m->ter_set( point( i, j ), t_rock_floor ); } } diff --git a/src/melee.cpp b/src/melee.cpp index cb403adb72c88..227c299147d2b 100644 --- a/src/melee.cpp +++ b/src/melee.cpp @@ -1960,14 +1960,14 @@ void player_hit_message( player *attacker, const std::string &message, //player hits monster melee SCT.add( t.posx(), t.posy(), - direction_from( 0, 0, t.posx() - attacker->posx(), t.posy() - attacker->posy() ), + direction_from( point_zero, point( t.posx() - attacker->posx(), t.posy() - attacker->posy() ) ), get_hp_bar( dam, t.get_hp_max(), true ).first, m_good, sSCTmod, gmtSCTcolor ); if( t.get_hp() > 0 ) { SCT.add( t.posx(), t.posy(), - direction_from( 0, 0, t.posx() - attacker->posx(), t.posy() - attacker->posy() ), + direction_from( point_zero, point( t.posx() - attacker->posx(), t.posy() - attacker->posy() ) ), get_hp_bar( t.get_hp(), t.get_hp_max(), true ).first, m_good, //~ "hit points", used in scrolling combat text _( "hp" ), m_neutral, diff --git a/src/mission_util.cpp b/src/mission_util.cpp index 7fda72f1deab1..15485ba041745 100644 --- a/src/mission_util.cpp +++ b/src/mission_util.cpp @@ -155,8 +155,7 @@ tripoint mission_util::target_closest_lab_entrance( const tripoint &origin, int underground.z = 0; tripoint closest; - if( square_dist( surface.x, surface.y, origin.x, origin.y ) <= square_dist( underground.x, - underground.y, origin.x, origin.y ) ) { + if( square_dist( surface.xy(), origin.xy() ) <= square_dist( underground.xy(), origin.xy() ) ) { closest = surface; } else { closest = underground; diff --git a/src/monattack.cpp b/src/monattack.cpp index 2998fc601c8ba..210f2b688efa6 100644 --- a/src/monattack.cpp +++ b/src/monattack.cpp @@ -3452,7 +3452,7 @@ bool mattack::searchlight( monster *z ) } } - if( rl_dist( x, y, zposx, zposy ) > 50 ) { + if( rl_dist( point( x, y ), point( zposx, zposy ) ) > 50 ) { if( x > zposx ) { x--; } diff --git a/src/monmove.cpp b/src/monmove.cpp index e7d0506d1e28d..0f2f5b8c2fe9f 100644 --- a/src/monmove.cpp +++ b/src/monmove.cpp @@ -1156,7 +1156,7 @@ static std::vector get_bashing_zone( const tripoint &bashee, const tri zone.reserve( 3 * maxdepth ); tripoint previous = bashee; for( const tripoint &p : path ) { - std::vector swath = squares_in_direction( previous.x, previous.y, p.x, p.y ); + std::vector swath = squares_in_direction( previous.xy(), p.xy() ); for( point q : swath ) { zone.push_back( tripoint( q, bashee.z ) ); } @@ -1765,7 +1765,7 @@ bool monster::will_reach( const point &p ) } if( can_hear() && wandf > 0 && rl_dist( wander_pos.xy(), p ) <= 2 && - rl_dist( posx(), posy(), wander_pos.x, wander_pos.y ) <= wandf ) { + rl_dist( point( posx(), posy() ), wander_pos.xy() ) <= wandf ) { return true; } diff --git a/src/overmap.cpp b/src/overmap.cpp index 8b171055c5e05..a1c2a6cb4600e 100644 --- a/src/overmap.cpp +++ b/src/overmap.cpp @@ -1822,7 +1822,7 @@ void mongroup::wander( const overmap &om ) // Find a nearby city to return to.. for( const city &check_city : om.cities ) { // Check if this is the nearest city so far. - int distance = rl_dist( check_city.pos.x * 2, check_city.pos.y * 2, pos.x, pos.y ); + int distance = rl_dist( point( check_city.pos.x * 2, check_city.pos.y * 2 ), pos.xy() ); if( !target_city || distance < target_distance ) { target_distance = distance; target_city = &check_city; @@ -2100,8 +2100,8 @@ void overmap::place_forest_trails() // center point and use that. point actual_center_point = *std::min_element( forest_points.begin(), forest_points.end(), [¢er_point]( const point & lhs, const point & rhs ) { - return square_dist( lhs.x, lhs.y, center_point.x, center_point.y ) < square_dist( rhs.x, rhs.y, - center_point.x, center_point.y ); + return square_dist( lhs, center_point ) < square_dist( rhs, + center_point ); } ); // Figure out how many random points we'll add to our trail system, based on the forest @@ -2344,7 +2344,7 @@ void overmap::place_lakes() if( !ter( p )->is_river() ) { continue; } - const int distance = square_dist( lake_connection_point.x, lake_connection_point.y, x, y ); + const int distance = square_dist( lake_connection_point, point( x, y ) ); if( distance < closest_distance || closest_distance < 0 ) { closest_point = p.xy(); closest_distance = distance; @@ -2866,8 +2866,7 @@ void overmap::place_building( const tripoint &p, om_direction::type dir, const c const tripoint building_pos = p + om_direction::displace( dir ); const om_direction::type building_dir = om_direction::opposite( dir ); - const int town_dist = ( trig_dist( building_pos.x, building_pos.y, town.pos.x, - town.pos.y ) * 100 ) / std::max( town.size, 1 ); + const int town_dist = ( trig_dist( building_pos.xy(), town.pos ) * 100 ) / std::max( town.size, 1 ); for( size_t retries = 10; retries > 0; --retries ) { const overmap_special_id building_tid = pick_random_building_to_place( town_dist ); @@ -3461,7 +3460,8 @@ void overmap::connect_closest_points( const std::vector &points, int z, int closest = -1; int k = 0; for( size_t j = i + 1; j < points.size(); j++ ) { - const int distance = trig_dist( points[i].x, points[i].y, points[j].x, points[j].y ); + const int distance = trig_dist( point( points[i].x, points[i].y ), point( points[j].x, + points[j].y ) ); if( distance < closest || closest < 0 ) { closest = distance; k = j; @@ -4033,8 +4033,8 @@ void overmap::place_specials( overmap_special_batch &enabled_specials ) // in the 5x5 area surrounding the initial overmap, bounding the amount of work we will do. for( point candidate_addr : closest_points_first( 2, custom_overmap_specials.get_origin() ) ) { if( !overmap_buffer.has( candidate_addr ) ) { - int current_distance = square_dist( pos().x, pos().y, - candidate_addr.x, candidate_addr.y ); + int current_distance = square_dist( pos(), + candidate_addr ); if( nearest_candidates.empty() || current_distance == previous_distance ) { nearest_candidates.push_back( candidate_addr ); previous_distance = current_distance; @@ -4257,7 +4257,8 @@ void overmap::add_mon_group( const mongroup &group ) int xpop = 0; for( int x = -rad; x <= rad; x++ ) { for( int y = -rad; y <= rad; y++ ) { - const int dist = group.diffuse ? square_dist( x, y, 0, 0 ) : trig_dist( x, y, 0, 0 ); + const int dist = group.diffuse ? square_dist( point( x, y ), point_zero ) : trig_dist( point( x, + y ), point_zero ); if( dist > rad ) { continue; } diff --git a/src/player.cpp b/src/player.cpp index 42e819a425446..31eb1571abad3 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -740,7 +740,7 @@ void player::apply_persistent_morale() const int max_dist = 5; for( int dx = -max_dist; dx <= max_dist; dx++ ) { for( int dy = -max_dist; dy <= max_dist; dy++ ) { - const float dist = rl_dist( 0, 0, dx, dy ); + const float dist = rl_dist( point_zero, point( dx, dy ) ); if( dist > max_dist ) { continue; } @@ -2980,7 +2980,7 @@ dealt_damage_instance player::deal_damage( Creature *source, body_part bp, if( is_player() && source ) { //monster hits player melee 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( dam, get_hp_max( player::bp_to_hp( bp ) ) ).first, m_bad, body_part_name( bp ), m_neutral ); } diff --git a/src/sounds.cpp b/src/sounds.cpp index abd3a9e256a11..6f0a84dbd463e 100644 --- a/src/sounds.cpp +++ b/src/sounds.cpp @@ -161,7 +161,7 @@ static std::vector cluster_sounds( std::vector( 10 ) ), static_cast( log( recent_sounds.size() ) ) ); const size_t stopping_point = recent_sounds.size() - num_seed_clusters; - const size_t max_map_distance = rl_dist( 0, 0, MAPSIZE_X, MAPSIZE_Y ); + const size_t max_map_distance = rl_dist( point_zero, point( MAPSIZE_X, MAPSIZE_Y ) ); // Randomly choose cluster seeds. for( size_t i = recent_sounds.size(); i > stopping_point; i-- ) { size_t index = rng( 0, i - 1 ); @@ -311,7 +311,7 @@ void sounds::process_sound_markers( player *p ) for( const auto &sound_event_pair : sounds_since_last_turn ) { const tripoint &pos = sound_event_pair.first; const sound_event &sound = sound_event_pair.second; - const int distance_to_sound = rl_dist( p->pos().x, p->pos().y, pos.x, pos.y ) + + const int distance_to_sound = rl_dist( p->pos().xy(), pos.xy() ) + abs( p->pos().z - pos.z ) * 10; const int raw_volume = sound.volume; diff --git a/src/timed_event.cpp b/src/timed_event.cpp index 25ecd0b3faa57..ffb42f3bb576f 100644 --- a/src/timed_event.cpp +++ b/src/timed_event.cpp @@ -256,7 +256,7 @@ void timed_event::actualize() y = rng( g->u.posy() - 5, g->u.posy() + 5 ); tries++; } while( tries < 20 && !g->is_empty( {x, y, g->u.posz()} ) && - rl_dist( x, y, g->u.posx(), g->u.posy() ) <= 2 ); + rl_dist( point( x, y ), point( g->u.posx(), g->u.posy() ) ) <= 2 ); if( tries < 20 ) { g->summon_mon( montype, tripoint( x, y, g->u.posz() ) ); } diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 269590dd6b404..759b13317e656 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -663,8 +663,8 @@ void vehicle::smash( float hp_percent_loss_min, float hp_percent_loss_max, int roll = dice( 1, 1000 ); int pct_af = ( percent_of_parts_to_affect * 1000.0f ); if( roll < pct_af ) { - float dist = 1.0f - trig_dist( damage_origin.x, damage_origin.y, part.precalc[0].x, - part.precalc[0].y ) / damage_size; + float dist = 1.0f - trig_dist( damage_origin, point( part.precalc[0].x, + part.precalc[0].y ) ) / damage_size; dist = clamp( dist, 0.0f, 1.0f ); if( damage_size == 0 ) { dist = 1.0f; @@ -5263,7 +5263,7 @@ void vehicle::damage_all( int dmg1, int dmg2, damage_type type, const point &imp for( const vpart_reference &vp : get_all_parts() ) { const size_t p = vp.part_index(); - int distance = 1 + square_dist( vp.mount().x, vp.mount().y, impact.x, impact.y ); + int distance = 1 + square_dist( vp.mount(), impact ); if( distance > 1 && part_info( p ).location == part_location_structure && !part_info( p ).has_flag( "PROTRUSION" ) ) { damage_direct( p, rng( dmg1, dmg2 ) / ( distance * distance ), type ); diff --git a/tests/line_test.cpp b/tests/line_test.cpp index 3de4df608c216..f7dc61528ce4e 100644 --- a/tests/line_test.cpp +++ b/tests/line_test.cpp @@ -293,8 +293,8 @@ TEST_CASE( "squares_closer_to_test" ) static void line_to_comparison( const int iterations ) { - REQUIRE( trig_dist( 0, 0, 0, 0 ) == 0 ); - REQUIRE( trig_dist( 0, 0, 1, 0 ) == 1 ); + REQUIRE( trig_dist( point_zero, point_zero ) == 0 ); + REQUIRE( trig_dist( point_zero, point_east ) == 1 ); const int seed = time( nullptr ); std::srand( seed ); @@ -306,7 +306,8 @@ static void line_to_comparison( const int iterations ) const int y2 = rng( -COORDINATE_RANGE, COORDINATE_RANGE ); int t1 = 0; int t2 = 0; - REQUIRE( line_to( x1, y1, x2, y2, t1 ) == canonical_line_to( x1, y1, x2, y2, t2 ) ); + REQUIRE( line_to( point( x1, y1 ), point( x2, y2 ), t1 ) == canonical_line_to( x1, y1, x2, y2, + t2 ) ); } { @@ -319,7 +320,7 @@ static void line_to_comparison( const int iterations ) int count1 = 0; const auto start1 = std::chrono::high_resolution_clock::now(); while( count1 < iterations ) { - line_to( x1, y1, x2, y2, t1 ); + line_to( point( x1, y1 ), point( x2, y2 ), t1 ); count1++; } const auto end1 = std::chrono::high_resolution_clock::now(); @@ -359,7 +360,7 @@ TEST_CASE( "line_to_boundaries" ) const int st( ( ideal_start_offset > 0 ) - ( ideal_start_offset < 0 ) ); const int max_start_offset = std::abs( ideal_start_offset ) * 2 + 1; for( int k = -1; k <= max_start_offset; ++k ) { - auto line = line_to( 0, 0, i, j, k * st ); + auto line = line_to( point_zero, point( i, j ), k * st ); if( line.back() != point( i, j ) ) { WARN( "Expected (" << i << "," << j << ") but got (" << line.back().x << "," << line.back().y << ") with t == " << k ); diff --git a/tests/shadowcasting_test.cpp b/tests/shadowcasting_test.cpp index 970472e00837e..d02473abef254 100644 --- a/tests/shadowcasting_test.cpp +++ b/tests/shadowcasting_test.cpp @@ -88,7 +88,7 @@ static bool bresenham_visibility_check( } bool visible = true; const int junk = 0; - bresenham( x, y, offsetX, offsetY, junk, + bresenham( point( x, y ), point( offsetX, offsetY ), junk, [&transparency_cache, &visible]( const point & new_point ) { if( transparency_cache[new_point.x][new_point.y] <= LIGHT_TRANSPARENCY_SOLID ) { From 4296512e970e1357af81488d3c9e14219d8dd45c Mon Sep 17 00:00:00 2001 From: John Bytheway Date: Sun, 18 Aug 2019 20:11:34 -0400 Subject: [PATCH 6/7] Add abs( tripoint ) overload --- src/point.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/point.h b/src/point.h index 2b780c76ab140..5d3ec219df33b 100644 --- a/src/point.h +++ b/src/point.h @@ -300,6 +300,11 @@ inline point abs( const point &p ) return point( abs( p.x ), abs( p.y ) ); } +inline tripoint abs( const tripoint &p ) +{ + return tripoint( abs( p.x ), abs( p.y ), abs( p.z ) ); +} + static constexpr tripoint tripoint_min { INT_MIN, INT_MIN, INT_MIN }; static constexpr tripoint tripoint_max{ INT_MAX, INT_MAX, INT_MAX }; From e85578b7cb95863d6155797b89f737a79bcb2117 Mon Sep 17 00:00:00 2001 From: John Bytheway Date: Sun, 18 Aug 2019 20:11:59 -0400 Subject: [PATCH 7/7] Port line function implementations --- src/line.cpp | 96 +++++++++++++++++++--------------------------------- src/line.h | 19 ++--------- 2 files changed, 37 insertions(+), 78 deletions(-) diff --git a/src/line.cpp b/src/line.cpp index ecd0605763800..a68ef3011a39a 100644 --- a/src/line.cpp +++ b/src/line.cpp @@ -16,49 +16,47 @@ extern bool trigdist; -void bresenham( const int x1, const int y1, const int x2, const int y2, int t, +void bresenham( const point &p1, const point &p2, int t, const std::function &interact ) { // The slope components. - const int dx = x2 - x1; - const int dy = y2 - y1; + const point d = p2 - p1; // Signs of slope values. - const int sx = ( dx == 0 ) ? 0 : sgn( dx ); - const int sy = ( dy == 0 ) ? 0 : sgn( dy ); + const int sx = ( d.x == 0 ) ? 0 : sgn( d.x ); + const int sy = ( d.y == 0 ) ? 0 : sgn( d.y ); // Absolute values of slopes x2 to avoid rounding errors. - const int ax = abs( dx ) * 2; - const int ay = abs( dy ) * 2; + const point a = abs( d ) * 2; - point cur( x1, y1 ); + point cur = p1; - if( ax == ay ) { - while( cur.x != x2 ) { + if( a.x == a.y ) { + while( cur.x != p2.x ) { cur.y += sy; cur.x += sx; if( !interact( cur ) ) { break; } } - } else if( ax > ay ) { - while( cur.x != x2 ) { + } else if( a.x > a.y ) { + while( cur.x != p2.x ) { if( t > 0 ) { cur.y += sy; - t -= ax; + t -= a.x; } cur.x += sx; - t += ay; + t += a.y; if( !interact( cur ) ) { break; } } } else { - while( cur.y != y2 ) { + while( cur.y != p2.y ) { if( t > 0 ) { cur.x += sx; - t -= ay; + t -= a.y; } cur.y += sy; - t += ax; + t += a.x; if( !interact( cur ) ) { break; } @@ -198,16 +196,16 @@ void bresenham( const tripoint &loc1, const tripoint &loc2, int t, int t2, //Trying to pull points out of a tripoint vector is messy and //probably slow, so leaving two full functions for now -std::vector line_to( const int x1, const int y1, const int x2, const int y2, int t ) +std::vector line_to( const point &p1, const point &p2, int t ) { std::vector line; // Preallocate the number of cells we need instead of allocating them piecewise. - const int numCells = square_dist( tripoint( x1, y1, 0 ), tripoint( x2, y2, 0 ) ); + const int numCells = square_dist( p1, p2 ); if( numCells == 0 ) { - line.push_back( {x1, y1} ); + line.push_back( p1 ); } else { line.reserve( numCells ); - bresenham( x1, y1, x2, y2, t, [&line]( const point & new_point ) { + bresenham( p1, p2, t, [&line]( const point & new_point ) { line.push_back( new_point ); return true; } ); @@ -215,11 +213,6 @@ std::vector line_to( const int x1, const int y1, const int x2, const int return line; } -std::vector line_to( const point &p1, const point &p2, const int t ) -{ - return line_to( p1.x, p1.y, p2.x, p2.y, t ); -} - std::vector line_to( const tripoint &loc1, const tripoint &loc2, int t, int t2 ) { std::vector line; @@ -237,11 +230,6 @@ std::vector line_to( const tripoint &loc1, const tripoint &loc2, int return line; } -float trig_dist( const int x1, const int y1, const int x2, const int y2 ) -{ - return trig_dist( tripoint( x1, y1, 0 ), tripoint( x2, y2, 0 ) ); -} - float trig_dist( const point &loc1, const point &loc2 ) { return trig_dist( tripoint( loc1, 0 ), tripoint( loc2, 0 ) ); @@ -254,30 +242,16 @@ float trig_dist( const tripoint &loc1, const tripoint &loc2 ) ( ( loc1.z - loc2.z ) * ( loc1.z - loc2.z ) ) ); } -int square_dist( const int x1, const int y1, const int x2, const int y2 ) -{ - return square_dist( point( x1, y1 ), point( x2, y2 ) ); -} - int square_dist( const point &loc1, const point &loc2 ) { - const int dx = abs( loc1.x - loc2.x ); - const int dy = abs( loc1.y - loc2.y ); - return dx > dy ? dx : dy; + const point d = abs( loc1 - loc2 ); + return std::max( d.x, d.y ); } int square_dist( const tripoint &loc1, const tripoint &loc2 ) { - const int dx = abs( loc1.x - loc2.x ); - const int dy = abs( loc1.y - loc2.y ); - const int dz = abs( loc1.z - loc2.z ); - int maxDxDy = ( dx > dy ? dx : dy ); // Sloppy, but should be quick. - return ( maxDxDy > dz ? maxDxDy : dz ); // Too bad it doesn't scale. -} - -int rl_dist( const int x1, const int y1, const int x2, const int y2 ) -{ - return rl_dist( tripoint( x1, y1, 0 ), tripoint( x2, y2, 0 ) ); + const tripoint d = abs( loc1 - loc2 ); + return std::max( { d.x, d.y, d.z } ); } int rl_dist( const point &a, const point &b ) @@ -406,9 +380,9 @@ direction direction_from( const tripoint &p ) noexcept return static_cast( make_xyz( p ) ); } -direction direction_from( const int x1, const int y1, const int x2, const int y2 ) noexcept +direction direction_from( const point &p1, const point &p2 ) noexcept { - return direction_from( point( x2 - x1, y2 - y1 ) ); + return direction_from( p2 - p1 ); } direction direction_from( const tripoint &p, const tripoint &q ) @@ -557,24 +531,24 @@ std::vector squares_closer_to( const tripoint &from, const tripoint &t // Returns a vector of the adjacent square in the direction of the target, // and the two squares flanking it. -std::vector squares_in_direction( const int x1, const int y1, const int x2, const int y2 ) +std::vector squares_in_direction( const point &p1, const point &p2 ) { int junk = 0; - point center_square = line_to( x1, y1, x2, y2, junk )[0]; + point center_square = line_to( p1, p2, junk )[0]; std::vector adjacent_squares; adjacent_squares.push_back( center_square ); - if( x1 == center_square.x ) { + if( p1.x == center_square.x ) { // Horizontally adjacent. - adjacent_squares.push_back( point( x1 + 1, center_square.y ) ); - adjacent_squares.push_back( point( x1 - 1, center_square.y ) ); - } else if( y1 == center_square.y ) { + adjacent_squares.push_back( point( p1.x + 1, center_square.y ) ); + adjacent_squares.push_back( point( p1.x - 1, center_square.y ) ); + } else if( p1.y == center_square.y ) { // Vertically adjacent. - adjacent_squares.push_back( point( center_square.x, y1 + 1 ) ); - adjacent_squares.push_back( point( center_square.x, y1 - 1 ) ); + adjacent_squares.push_back( point( center_square.x, p1.y + 1 ) ); + adjacent_squares.push_back( point( center_square.x, p1.y - 1 ) ); } else { // Diagonally adjacent. - adjacent_squares.push_back( point( x1, center_square.y ) ); - adjacent_squares.push_back( point( center_square.x, y1 ) ); + adjacent_squares.push_back( point( p1.x, center_square.y ) ); + adjacent_squares.push_back( point( center_square.x, p1.y ) ); } return adjacent_squares; } diff --git a/src/line.h b/src/line.h index fab466e1cc6a0..752bcc35fda3e 100644 --- a/src/line.h +++ b/src/line.h @@ -83,8 +83,6 @@ enum direction : unsigned { direction direction_from( const point &p ) noexcept; direction direction_from( const tripoint &p ) noexcept; -direction direction_from( int x1, int y1, int x2, int y2 ) noexcept; -direction direction_from( const point &p1, int x2, int y2 ) noexcept; direction direction_from( const point &p1, const point &p2 ) noexcept; direction direction_from( const tripoint &p, const tripoint &q ); @@ -99,10 +97,6 @@ std::string direction_suffix( const tripoint &p, const tripoint &q ); * The actual Bresenham algorithm in 2D and 3D, everything else should call these * and pass in an interact functor to iterate across a line between two points. */ -void bresenham( int x1, int y1, int x2, int y2, int t, - const std::function &interact ); -void bresenham( const point &p1, int x2, int y2, int t, - const std::function &interact ); void bresenham( const point &p1, const point &p2, int t, const std::function &interact ); void bresenham( const tripoint &loc1, const tripoint &loc2, int t, int t2, @@ -111,25 +105,18 @@ void bresenham( const tripoint &loc1, const tripoint &loc2, int t, int t2, tripoint move_along_line( const tripoint &loc, const std::vector &line, int distance ); // The "t" value decides WHICH Bresenham line is used. -std::vector line_to( int x1, int y1, int x2, int y2, int t = 0 ); -std::vector line_to( const point &p1, int x2, int y2, int t = 0 ); std::vector line_to( const point &p1, const point &p2, int t = 0 ); // t and t2 decide which Bresenham line is used. std::vector line_to( const tripoint &loc1, const tripoint &loc2, int t = 0, int t2 = 0 ); // sqrt(dX^2 + dY^2) -float trig_dist( int x1, int y1, int x2, int y2 ); -float trig_dist( const point &p1, int x2, int y2 ); float trig_dist( const point &loc1, const point &loc2 ); float trig_dist( const tripoint &loc1, const tripoint &loc2 ); // Roguelike distance; minimum of dX and dY -int square_dist( int x1, int y1, int x2, int y2 ); -int square_dist( const point &p1, int x2, int y2 ); int square_dist( const point &loc1, const point &loc2 ); int square_dist( const tripoint &loc1, const tripoint &loc2 ); -int rl_dist( int x1, int y1, int x2, int y2 ); -int rl_dist( const point &p1, int x2, int y2 ); -int rl_dist( const tripoint &loc1, const tripoint &loc2 ); +// Choose between the above two according to the "circular distances" option int rl_dist( const point &a, const point &b ); +int rl_dist( const tripoint &loc1, const tripoint &loc2 ); // Sum of distance in both axes int manhattan_dist( const point &loc1, const point &loc2 ); @@ -140,8 +127,6 @@ double atan2_degrees( const point & ); // Get the magnitude of the slope ranging from 0.0 to 1.0 float get_normalized_angle( const point &start, const point &end ); std::vector continue_line( const std::vector &line, int distance ); -std::vector squares_in_direction( int x1, int y1, int x2, int y2 ); -std::vector squares_in_direction( const point &p1, int x2, int y2 ); std::vector squares_in_direction( const point &p1, const point &p2 ); // Returns a vector of squares adjacent to @from that are closer to @to than @from is. // Currently limited to the same z-level as @from.