Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(UI): add Simple and Compact compasses for small screens #6037

Merged
merged 12 commits into from
Feb 9, 2025
56 changes: 29 additions & 27 deletions src/panels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@
{
const tripoint_abs_omt curs = u.global_omt_location();
overmap_ui::draw_overmap_chunk( w_minimap, u, curs, point_zero, 7, 7 );
}

Check warning on line 297 in src/panels.cpp

View workflow job for this annotation

GitHub Actions / build

Prefer constructing 'point' from named constant 'point_north_west' rather than explicit integer arguments. [cata-use-named-point-constants]

static void decorate_panel( const std::string &name, const catacurses::window &w )
{
Expand Down Expand Up @@ -1755,38 +1755,41 @@
g->mon_info( w );
wnoutrefresh( w );
}

// Forward declarations
std::string direction_to_enemy_improved( const tripoint &enemy_pos, const tripoint &player_pos );
void check( const char *msg, std::function < auto( const tripoint &,
const tripoint & ) -> std::string > fn );

// Improved direction function
std::string direction_to_enemy_improved( const tripoint &enemy_pos, const tripoint &player_pos )
{
// Really close to optimal 22.5 degree line
// Calculated from the continued fraction estimate of cos(22.5deg) / sin(22.5deg)
// Constants based on cos(22.5°) / sin(22.5°) approximation
constexpr int x0 = 80782;
constexpr int y0 = 33461;

// Sections:
struct wedge_range {
const char *direction;

int x0, y0;
int x1, y1;
};

constexpr std::array<wedge_range, 8> wedges = {{
{ "N", -y0, -x0, y0, -x0 },
{"NE", y0, -x0, x0, -y0 },
{ "E", x0, -y0, x0, y0 },
{"SE", x0, y0, y0, x0 },
{ "S", y0, x0, -y0, x0 },
{"SW", -y0, x0, -x0, y0 },
{ "W", -x0, y0, -x0, -y0 },
{"NW", -x0, -y0, -y0, -x0 }
{ "N", -y0, -x0, y0, -x0 },
{ "NE", y0, -x0, x0, -y0 },
{ "E", x0, -y0, x0, y0 },
{ "SE", x0, y0, y0, x0 },
{ "S", y0, x0, -y0, x0 },
{ "SW", -y0, x0, -x0, y0 },
{ "W", -x0, y0, -x0, -y0 },
{ "NW", -x0, -y0, -y0, -x0 }
}
};

auto between = []( int cx, int cy, const wedge_range & wr ) {
auto side_of_sign = []( int ax, int ay, int bx, int by ) {
int dot = ax * by - ay * bx;

return ( dot > 0 ) - ( dot < 0 ); // Returns [-1, 0, +1] sign of dot product
return ( dot > 0 ) - ( dot < 0 );
};

int dot_ab = side_of_sign( wr.x0, wr.y0, wr.x1, wr.y1 );
Expand All @@ -1795,10 +1798,11 @@

return ( dot_ab == dot_ac ) && ( dot_ab == dot_cb );
};

const int dx = enemy_pos.x - player_pos.x;
const int dy = enemy_pos.y - player_pos.y;

for( const auto &wr : wedges ) {

Check warning on line 1805 in src/panels.cpp

View workflow job for this annotation

GitHub Actions / build

'wedge_range' defines fields 'x0' and 'y0'. Consider combining into a single point field. [cata-xy]

Check warning on line 1805 in src/panels.cpp

View workflow job for this annotation

GitHub Actions / build

'wedge_range' defines fields 'x1' and 'y1'. Consider combining into a single point field. [cata-xy]
if( between( dx, dy, wr ) ) {
return wr.direction;
}
Expand All @@ -1806,52 +1810,50 @@
return "--";
}

// Corrected check function
void check( const char *msg, std::function<std::string( const tripoint &, const tripoint & )> fn )
Copy link
Collaborator

@OrenAudeles OrenAudeles Feb 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the check function entirely. What I suggested was to only replace the contents of your direction_to_enemy function with the contents of direction_to_enemy_improved. All other code was for testing that the output is reasonable.

Copy link
Contributor Author

@Lamandus Lamandus Feb 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should have said that... 😆

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah looking back I didn't communicate as effectively as I should have what I was suggesting. We got it fixed in the end, though, so the PR process works. "Team work makes the dream work" 😆

{
printf( msg );
printf( "%s\n", msg ); // Secure printf usage

constexpr int dim = 21;
constexpr int mid = dim / 2;
std::string grid[dim][dim];
grid[mid][mid] = "--";

std::map<std::string, int> counts;

Check warning on line 1823 in src/panels.cpp

View workflow job for this annotation

GitHub Actions / build

'operator()' has parameters 'cx' and 'cy'. Consider combining into a single point parameter. [cata-xy]

Check warning on line 1824 in src/panels.cpp

View workflow job for this annotation

GitHub Actions / build

'operator()' has parameters 'ax' and 'ay'. Consider combining into a single point parameter. [cata-xy]

Check warning on line 1824 in src/panels.cpp

View workflow job for this annotation

GitHub Actions / build

'operator()' has parameters 'bx' and 'by'. Consider combining into a single point parameter. [cata-xy]
constexpr int r2 = mid * mid;

for( int y = 0; y < dim; ++y ) {
for( int x = 0; x < dim; ++x ) {
grid[y][x] = "--";
if( x == mid && y == mid ) {
continue;
}

int d2 = ( x - mid ) * ( x - mid ) + ( y - mid ) * ( y - mid );
if( false || d2 < r2 ) {
grid[y][x] = fn( { x, y, 0}, {mid, mid, 0} );
if( d2 < r2 ) {
grid[y][x] = fn( { x, y, 0 }, { mid, mid, 0 } );
counts[grid[y][x]] += 1;
} else {
grid[y][x] = "--";
}
}
}

for( int y = 0; y < dim; ++y ) {
for( int x = 0; x < dim; ++x ) {
printf( "%*.*s ", 2, 2, grid[y][x].c_str() );
printf( "%2s ", grid[y][x].c_str() );
}
printf( "\n" );
}

printf( "Tiles per direction:\n" );
for( const auto &p : counts ) {
printf( "%*.*s %d\n", 2, 2, p.first.c_str(), p.second );
printf( "%2s %d\n", p.first.c_str(), p.second );
}
printf( "\n\n" );
printf( "\n" );
}

int main()
{
check( "Improved\n", direction_to_enemy_improved );

return 0;
}

static void draw_simple_compass( avatar &u, const catacurses::window &w )
{
Expand Down Expand Up @@ -1906,7 +1908,7 @@
int c_speed = static_cast<int>( convert_velocity( veh->velocity, VU_VEHICLE ) );
int offset = get_int_digits( c_speed );
const std::string type = get_option<std::string>( "USE_METRIC_SPEEDS" );
mvwprintz( w, point( 12, 0 ), c_light_gray, "%s :", type );

Check warning on line 1911 in src/panels.cpp

View workflow job for this annotation

GitHub Actions / build

Prefer constructing 'point' from named constant 'point_zero' rather than explicit integer arguments. [cata-use-named-point-constants]
mvwprintz( w, point( 19, 0 ), col_vel, "%d", c_speed );
if( veh->cruise_on ) {
mvwprintz( w, point( 20 + offset, 0 ), c_light_gray, "%s", ">" );
Expand Down
Loading