Skip to content

Commit

Permalink
Introduced untyped coordinate operations to typed ones (#77098)
Browse files Browse the repository at this point in the history
* Introduced untyped coordinate operations to typed ones

* Missed rotate usages

* demanded changes
  • Loading branch information
PatrikLundell authored Oct 25, 2024
1 parent 90c878c commit eda59f2
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/active_item_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ void active_item_cache::rotate_locations( int turns, const point_rel_ms &dim )
for( std::pair<const int, std::list<item_reference>> &pair : active_items ) {
for( item_reference &ir : pair.second ) {
// Should 'rotate' be propaged up to the typed coordinates?
ir.location = point_rel_ms( ir.location.raw().rotate( turns, dim.raw() ) );
ir.location = ir.location.rotate( turns, dim.raw() );
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11393,14 +11393,14 @@ action_id Character::get_next_auto_move_direction()

if( next_expected_position ) {
// Difference between where the character is and where we expect them to be after last auto-move.
tripoint diff = ( pos_bub() - *next_expected_position ).raw().abs();
tripoint_rel_ms diff = ( pos_bub() - *next_expected_position ).abs();
// This might differ by 1 (in z-direction), since the character might have crossed a ramp,
// which teleported them a tile up or down. If the error is in x or y direction, we might as well
// give them a turn to recover, as the move still might be vaild.
// We cut off at 1 since 2 definitely results in an invalid move.
// If the character is still stumbling or stuck,
// they will cancel the auto-move on the next cycle (as the distance increases).
if( std::max( { diff.x, diff.y, diff.z } ) > 1 ) {
if( std::max( { diff.x(), diff.y(), diff.z()} ) > 1 ) {
// We're off course, possibly stumbling or stuck, cancel auto move
return ACTION_NULL;
}
Expand Down
12 changes: 6 additions & 6 deletions src/clzones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1497,15 +1497,15 @@ void _rotate_zone( map &target_map, zone_data &zone, int turns )
z_start.x() + z_end.x() == a_end.x() ) {
return;
}
point z_l_start = z_start.xy().raw().rotate( turns, dim );
point z_l_end = z_end.xy().raw().rotate( turns, dim );
point_bub_ms z_l_start = z_start.xy().rotate( turns, dim );
point_bub_ms z_l_end = z_end.xy().rotate( turns, dim );
tripoint_abs_ms first =
target_map.getglobal( tripoint_bub_ms( std::min( z_l_start.x, z_l_end.x ),
std::min( z_l_start.y, z_l_end.y ),
target_map.getglobal( tripoint_bub_ms( std::min( z_l_start.x(), z_l_end.x() ),
std::min( z_l_start.y(), z_l_end.y() ),
z_start.z() ) );
tripoint_abs_ms second =
target_map.getglobal( tripoint_bub_ms( std::max( z_l_start.x, z_l_end.x ),
std::max( z_l_start.y, z_l_end.y ),
target_map.getglobal( tripoint_bub_ms( std::max( z_l_start.x(), z_l_end.x() ),
std::max( z_l_start.y(), z_l_end.y() ),
z_end.z() ) );
zone.set_position( std::make_pair( first.raw(), second.raw() ), false, true, false, true );
}
Expand Down
16 changes: 16 additions & 0 deletions src/coordinates.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,14 @@ class coord_point_ob : public
return this_as_point( this->raw().xy() );
}

constexpr auto abs() const {
return coord_point_ob( this->raw().abs() );
}

constexpr auto rotate( int turns, const point &dim = point_south_east ) const {
return coord_point_ob( this->raw().rotate( turns, dim ) );
}

friend inline this_as_ob operator+( const coord_point_ob &l, const point &r ) {
return this_as_ob( l.raw() + r );
}
Expand Down Expand Up @@ -406,6 +414,14 @@ constexpr inline auto operator-(
return coord_point_ob<PointResult, origin::relative, Scale>( l.raw() - r.raw() );
}

template<typename Point, origin Origin, scale Scale>
constexpr inline auto operator-(
const coord_point_ob<Point, Origin, Scale> &l )
{
using PointResult = decltype( Point() );
return coord_point_ob<PointResult, Origin, Scale>( - l.raw() );
}

// Only relative points can be multiplied by a constant
template<typename Point, scale Scale>
constexpr inline coord_point_ob<Point, origin::relative, Scale> operator*(
Expand Down
7 changes: 3 additions & 4 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11395,8 +11395,7 @@ bool game::phasing_move_enchant( const tripoint &dest_loc, const int phase_dista

bool game::can_move_furniture( tripoint fdest, const tripoint &dp )
{
// TODO: Fix when unary operation available
const bool pulling_furniture = dp.xy() == ( point_rel_ms_zero - u.grab_point.xy() ).raw();
const bool pulling_furniture = dp.xy() == -u.grab_point.xy().raw();
const bool has_floor = m.has_floor_or_water( fdest );
creature_tracker &creatures = get_creature_tracker();
bool is_ramp_or_road = m.has_flag( ter_furn_flag::TFLAG_RAMP_DOWN, fdest ) ||
Expand Down Expand Up @@ -12819,8 +12818,8 @@ void game::update_overmap_seen()
continue;
}
// If circular distances are enabled, scale overmap distances by the diagonality of the sight line.
point abs_delta = delta.raw().abs();
int max_delta = std::max( abs_delta.x, abs_delta.y );
point_rel_omt abs_delta = delta.abs();
int max_delta = std::max( abs_delta.x(), abs_delta.y() );
const float multiplier = trigdist ? std::sqrt( h_squared ) / max_delta : 1;
const std::vector<tripoint_abs_omt> line = line_to( ompos, p );
float sight_points = dist;
Expand Down
21 changes: 7 additions & 14 deletions src/grab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ bool game::grabbed_veh_move( const tripoint_rel_ms &dp )
}
const vehicle *veh_under_player = veh_pointer_or_null( m.veh_at( u.pos_bub() ) );
if( grabbed_vehicle == veh_under_player ) {
// TODO: Fix when unary operation available
u.grab_point = tripoint_rel_ms_zero - dp;
u.grab_point = - dp;
return false;
}

// TODO: Fix when unary operation available
tripoint_rel_ms dp_veh = tripoint_rel_ms_zero - u.grab_point;
tripoint_rel_ms dp_veh = - u.grab_point;
const tripoint_rel_ms prev_grab = u.grab_point;
tripoint_rel_ms next_grab = u.grab_point;
const tileray initial_veh_face = grabbed_vehicle->face;
Expand All @@ -63,8 +61,7 @@ bool game::grabbed_veh_move( const tripoint_rel_ms &dp )
pushing = true;
} else if( std::abs( dp.x() + dp_veh.x() ) != 2 && std::abs( dp.y() + dp_veh.y() ) != 2 ) {
// Not actually moving the vehicle, don't do the checks
// TODO: Fix when unary operation available
u.grab_point = tripoint_rel_ms_zero - ( dp + dp_veh );
u.grab_point = - ( dp + dp_veh );
return false;
} else if( ( dp.x() == prev_grab.x() || dp.y() == prev_grab.y() ) &&
next_grab.x() != 0 && next_grab.y() != 0 ) {
Expand All @@ -73,13 +70,11 @@ bool game::grabbed_veh_move( const tripoint_rel_ms &dp )
dp_veh.x() = dp.x() == -dp_veh.x() ? 0 : dp_veh.x();
dp_veh.y() = dp.y() == -dp_veh.y() ? 0 : dp_veh.y();

// TODO: Fix when unary operation available
next_grab = tripoint_rel_ms_zero - dp_veh;
next_grab = - dp_veh;
zigzag = true;
} else {
// We are pulling the vehicle
// TODO: Fix when unary operation available
next_grab = tripoint_rel_ms_zero - dp;
next_grab = - dp;
pulling = true;
}

Expand Down Expand Up @@ -276,10 +271,8 @@ bool game::grabbed_veh_move( const tripoint_rel_ms &dp )
// Try to place the vehicle in the position player just left rather than "flattening" the zig-zag
tripoint_rel_ms final_dp_veh = get_move_dir( dp_veh, next_grab );
if( final_dp_veh == tripoint_rel_ms_min && zigzag ) {
// TODO: Fix when unary operation available
final_dp_veh = get_move_dir( tripoint_rel_ms_zero - prev_grab, tripoint_rel_ms_zero - dp );
// TODO: Fix when unary operation available
next_grab = tripoint_rel_ms_zero - dp;
final_dp_veh = get_move_dir( - prev_grab, - dp );
next_grab = - dp;
}

if( final_dp_veh == tripoint_rel_ms_min ) {
Expand Down
14 changes: 7 additions & 7 deletions src/line.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,26 @@ void bresenham( const point_bub_ms &p1, const point_bub_ms &p2, int t,
// Signs of slope values.
const point s( ( d.x() == 0 ) ? 0 : sgn( d.x() ), ( d.y() == 0 ) ? 0 : sgn( d.y() ) );
// Absolute values of slopes x2 to avoid rounding errors.
const point a = d.raw().abs() * 2;
const point_rel_ms a = d.abs() * 2;

point_bub_ms cur = p1;

if( a.x == a.y ) {
if( a.x() == a.y() ) {
while( cur.x() != p2.x() ) {
cur.y() += s.y;
cur.x() += s.x;
if( !interact( cur ) ) {
break;
}
}
} else if( a.x > a.y ) {
} else if( a.x() > a.y() ) {
while( cur.x() != p2.x() ) {
if( t > 0 ) {
cur.y() += s.y;
t -= a.x;
t -= a.x();
}
cur.x() += s.x;
t += a.y;
t += a.y();
if( !interact( cur ) ) {
break;
}
Expand All @@ -58,10 +58,10 @@ void bresenham( const point_bub_ms &p1, const point_bub_ms &p2, int t,
while( cur.y() != p2.y() ) {
if( t > 0 ) {
cur.x() += s.x;
t -= a.y;
t -= a.y();
}
cur.y() += s.y;
t += a.x;
t += a.x();
if( !interact( cur ) ) {
break;
}
Expand Down
4 changes: 2 additions & 2 deletions src/mapgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7237,7 +7237,7 @@ void map::rotate( int turns )
// Translate bubble -> global -> current map.
const point_bub_ms old( bub_from_abs( get_map().getglobal( np.pos_bub() ).xy() ) );

const point_bub_ms new_pos( old.raw().rotate( turns, {SEEX * 2, SEEY * 2} ) );
const point_bub_ms new_pos( old.rotate( turns, {SEEX * 2, SEEY * 2} ) );
np.spawn_at_precise( getglobal( tripoint_bub_ms( new_pos, sq.z() ) ) );
}

Expand Down Expand Up @@ -7326,7 +7326,7 @@ void map::rotate( int turns )
if( queued_point_sm.y() % 2 != 0 ) {
old.y() += SEEY;
}
const point_bub_ms new_pos( old.raw().rotate( turns, {SEEX * 2, SEEY * 2} ) );
const point_bub_ms new_pos( old.rotate( turns, {SEEX * 2, SEEY * 2} ) );
queued_points[queued_point.first] = getglobal( tripoint_bub_ms( new_pos,
queued_point.second.z() ) );
}
Expand Down
2 changes: 1 addition & 1 deletion src/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2571,7 +2571,7 @@ bool vehicle::split_vehicles( map &here,
new_vehicle->refresh();
} else {
// include refresh
new_vehicle->shift_parts( here, point_zero - mnt_offset );
new_vehicle->shift_parts( here, - mnt_offset );
}

// update the precalc points
Expand Down
2 changes: 1 addition & 1 deletion tests/submap_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ TEST_CASE( "submap_rotation2", "[submap]" )
for( int x = 0; x < SEEX; x++ ) {
for( int y = 0; y < SEEY; y++ ) {
point_sm_ms p( x, y );
point_sm_ms p_after_rotation = point_sm_ms( p.raw().rotate( rotation_turns, {SEEX, SEEY} ) );
point_sm_ms p_after_rotation = p.rotate( rotation_turns, {SEEX, SEEY} );

CAPTURE( p, p_after_rotation );
CHECK( sm.get_radiation( p_after_rotation ) == sm_copy.get_radiation( p ) );
Expand Down

0 comments on commit eda59f2

Please sign in to comment.