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

Use point in Creature, monster, and player APIs #33115

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1535,9 +1535,9 @@ units::mass Creature::weight_capacity() const
/*
* Drawing-related functions
*/
void Creature::draw( const catacurses::window &w, int origin_x, int origin_y, bool inverted ) const
void Creature::draw( const catacurses::window &w, const point &origin, bool inverted ) const
{
draw( w, tripoint( origin_x, origin_y, posz() ), inverted );
draw( w, tripoint( origin, posz() ), inverted );
}

void Creature::draw( const catacurses::window &w, const tripoint &origin, bool inverted ) const
Expand Down
3 changes: 2 additions & 1 deletion src/creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class JsonOut;
struct tripoint;
class time_duration;
class player;
struct point;

enum damage_type : int;
enum m_flag : int;
Expand Down Expand Up @@ -491,7 +492,7 @@ class Creature

int moves;
bool underwater;
void draw( const catacurses::window &w, int origin_x, int origin_y, bool inverted ) const;
void draw( const catacurses::window &w, const point &origin, bool inverted ) const;
void draw( const catacurses::window &w, const tripoint &origin, bool inverted ) const;
/**
* Write information about this creature.
Expand Down
2 changes: 1 addition & 1 deletion src/editmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ void editmap::uber_draw_ter( const catacurses::window &w, map *m )
if( game_map ) {
Creature *critter = g->critter_at( p );
if( critter != nullptr ) {
critter->draw( w, center.x, center.y, false );
critter->draw( w, center.xy(), false );
} else {
m->drawsq( w, g->u, p, false, draw_itm, center, false, true );
}
Expand Down
37 changes: 18 additions & 19 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3327,7 +3327,7 @@ void game::draw_critter( const Creature &critter, const tripoint &center )
return;
}
if( u.sees( critter ) || &critter == &u ) {
critter.draw( w_terrain, center.x, center.y, false );
critter.draw( w_terrain, center.xy(), false );
return;
}

Expand Down Expand Up @@ -10220,11 +10220,10 @@ void game::vertical_move( int movez, bool force )
stored_mount = *u.mounted_creature.get();
}
if( !m.has_zlevels() ) {
const int to_x = u.posx();
const int to_y = u.posy();
const tripoint to = u.pos();
for( monster &critter : all_monsters() ) {
int turns = critter.turns_to_reach( to_x, to_y );
if( turns < 10 && coming_to_stairs.size() < 8 && critter.will_reach( to_x, to_y )
int turns = critter.turns_to_reach( to.xy() );
if( turns < 10 && coming_to_stairs.size() < 8 && critter.will_reach( to.xy() )
&& !slippedpast ) {
critter.staircount = 10 + turns;
critter.on_unload();
Expand Down Expand Up @@ -10584,43 +10583,43 @@ point game::update_map( player &p )

point game::update_map( int &x, int &y )
{
int shiftx = 0;
int shifty = 0;
point shift;

while( x < HALF_MAPSIZE_X ) {
x += SEEX;
shiftx--;
shift.x--;
}
while( x >= HALF_MAPSIZE_X + SEEX ) {
x -= SEEX;
shiftx++;
shift.x++;
}
while( y < HALF_MAPSIZE_Y ) {
y += SEEY;
shifty--;
shift.y--;
}
while( y >= HALF_MAPSIZE_Y + SEEY ) {
y -= SEEY;
shifty++;
shift.y++;
}

if( shiftx == 0 && shifty == 0 ) {
if( shift == point_zero ) {
// adjust player position
u.setpos( tripoint( x, y, get_levz() ) );
// Not actually shifting the submaps, all the stuff below would do nothing
return point_zero;
}

// this handles loading/unloading submaps that have scrolled on or off the viewport
m.shift( shiftx, shifty );
m.shift( shift.x, shift.y );

// Shift monsters
shift_monsters( shiftx, shifty, 0 );
u.shift_destination( -shiftx * SEEX, -shifty * SEEY );
shift_monsters( shift.x, shift.y, 0 );
const point shift_ms = sm_to_ms_copy( shift );
u.shift_destination( -shift_ms );

// Shift NPCs
for( auto it = active_npc.begin(); it != active_npc.end(); ) {
( *it )->shift( shiftx, shifty );
( *it )->shift( shift.x, shift.y );
if( ( *it )->posx() < 0 - SEEX * 2 || ( *it )->posy() < 0 - SEEX * 2 ||
( *it )->posx() > SEEX * ( MAPSIZE + 2 ) || ( *it )->posy() > SEEY * ( MAPSIZE + 2 ) ) {
//Remove the npc from the active list. It remains in the overmap list.
Expand All @@ -10631,7 +10630,7 @@ point game::update_map( int &x, int &y )
}
}

scent.shift( shiftx * SEEX, shifty * SEEY );
scent.shift( shift_ms.x, shift_ms.y );

// Also ensure the player is on current z-level
// get_levz() should later be removed, when there is no longer such a thing
Expand Down Expand Up @@ -10661,7 +10660,7 @@ point game::update_map( int &x, int &y )
// Update what parts of the world map we can see
update_overmap_seen();

return point( shiftx, shifty );
return shift;
}

void game::update_overmap_seen()
Expand Down Expand Up @@ -10959,7 +10958,7 @@ void game::shift_monsters( const int shiftx, const int shifty, const int shiftz
}
for( monster &critter : all_monsters() ) {
if( shiftx != 0 || shifty != 0 ) {
critter.shift( shiftx, shifty );
critter.shift( point( shiftx, shifty ) );
}

if( m.inbounds( critter.pos() ) && ( shiftz == 0 || m.has_zlevels() ) ) {
Expand Down
17 changes: 8 additions & 9 deletions src/monmove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1690,7 +1690,7 @@ void monster::knock_back_to( const tripoint &to )
Make sure that non-smashing monsters won't "teleport" through windows
Injure monsters if they're gonna be walking through pits or whatever
*/
bool monster::will_reach( int x, int y )
bool monster::will_reach( const point &p )
{
monster_attitude att = attitude( &g->u );
if( att != MATT_FOLLOW && att != MATT_ATTACK && att != MATT_FRIEND && att != MATT_ZLAVE ) {
Expand All @@ -1701,37 +1701,36 @@ bool monster::will_reach( int x, int y )
return false;
}

if( ( has_flag( MF_IMMOBILE ) || has_flag( MF_RIDEABLE_MECH ) ) && ( posx() != x ||
posy() != y ) ) {
if( ( has_flag( MF_IMMOBILE ) || has_flag( MF_RIDEABLE_MECH ) ) && ( pos().xy() != p ) ) {
return false;
}

auto path = g->m.route( pos(), tripoint( x, y, posz() ), get_pathfinding_settings() );
auto path = g->m.route( pos(), tripoint( p, posz() ), get_pathfinding_settings() );
if( path.empty() ) {
return false;
}

if( has_flag( MF_SMELLS ) && g->scent.get( pos() ) > 0 &&
g->scent.get( { x, y, posz() } ) > g->scent.get( pos() ) ) {
g->scent.get( { p, posz() } ) > g->scent.get( pos() ) ) {
return true;
}

if( can_hear() && wandf > 0 && rl_dist( wander_pos.x, wander_pos.y, x, y ) <= 2 &&
if( can_hear() && wandf > 0 && rl_dist( wander_pos.xy(), p ) <= 2 &&
rl_dist( posx(), posy(), wander_pos.x, wander_pos.y ) <= wandf ) {
return true;
}

if( can_see() && sees( tripoint( x, y, posz() ) ) ) {
if( can_see() && sees( tripoint( p, posz() ) ) ) {
return true;
}

return false;
}

int monster::turns_to_reach( int x, int y )
int monster::turns_to_reach( const point &p )
{
// This function is a(n old) temporary hack that should soon be removed
auto path = g->m.route( pos(), tripoint( x, y, posz() ), get_pathfinding_settings() );
auto path = g->m.route( pos(), tripoint( p, posz() ), get_pathfinding_settings() );
if( path.empty() ) {
return 999;
}
Expand Down
16 changes: 6 additions & 10 deletions src/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -858,17 +858,13 @@ void monster::set_goal( const tripoint &p )
goal = p;
}

void monster::shift( int sx, int sy )
{
const int xshift = sx * SEEX;
const int yshift = sy * SEEY;
position.x -= xshift;
position.y -= yshift;
goal.x -= xshift;
goal.y -= yshift;
void monster::shift( const point &sm_shift )
{
const point ms_shift = sm_to_ms_copy( sm_shift );
position -= ms_shift;
goal -= ms_shift;
if( wandf > 0 ) {
wander_pos.x -= xshift;
wander_pos.y -= yshift;
wander_pos -= ms_shift;
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/monster.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class monster : public Creature
Creature *attack_target(); // Returns the creature at the end of plans (if hostile)

// Movement
void shift( int sx, int sy ); // Shifts the monster to the appropriate submap
void shift( const point &sp ); // Shifts the monster to the appropriate submap
void set_goal( const tripoint &p );
// Updates current pos AND our plans
bool wander(); // Returns true if we have no plans
Expand All @@ -170,8 +170,8 @@ class monster : public Creature
*/
bool can_move_to( const tripoint &p ) const;

bool will_reach( int x, int y ); // Do we have plans to get to (x, y)?
int turns_to_reach( int x, int y ); // How long will it take?
bool will_reach( const point &p ); // Do we have plans to get to (x, y)?
int turns_to_reach( const point &p ); // How long will it take?

// Go in a straight line to p
void set_dest( const tripoint &p );
Expand Down
8 changes: 3 additions & 5 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10894,16 +10894,14 @@ bool player::defer_move( const tripoint &next )
return true;
}

void player::shift_destination( int shiftx, int shifty )
void player::shift_destination( const point &shift )
{
if( next_expected_position ) {
next_expected_position->x += shiftx;
next_expected_position->y += shifty;
*next_expected_position += shift;
}

for( auto &elem : auto_move_route ) {
elem.x += shiftx;
elem.y += shifty;
elem += shift;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -1508,7 +1508,7 @@ class player : public Character
std::vector<tripoint> &get_auto_move_route();
action_id get_next_auto_move_direction();
bool defer_move( const tripoint &next );
void shift_destination( int shiftx, int shifty );
void shift_destination( const point &shift );
void forced_dismount();
void dismount();

Expand Down