Skip to content

Commit

Permalink
Backport #77446 for 0.H (Pathfinding accounts for SMALL_PASSAGE) (#77550
Browse files Browse the repository at this point in the history
)

* Backport pathfinding fixes for large creatures
  • Loading branch information
harakka authored Nov 6, 2024
1 parent f929eb3 commit 31b113c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ Character::Character() :
name.clear();
custom_profession.clear();

*path_settings = pathfinding_settings{ 0, 1000, 1000, 0, true, true, true, true, false, true };
*path_settings = pathfinding_settings{ 0, 1000, 1000, 0, true, true, true, true, false, true, creature_size::medium };

move_mode = move_mode_walk;
next_expected_position = std::nullopt;
Expand Down
4 changes: 4 additions & 0 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10215,6 +10215,10 @@ void map::update_pathfinding_cache( const tripoint &p ) const
cur_value |= PF_SHARP;
}

if( terrain.has_flag( ter_furn_flag::TFLAG_SMALL_PASSAGE ) ) {
cur_value |= PF_SMALL_PASSAGE;
}

cache.special[p.x][p.y] = cur_value;
}

Expand Down
2 changes: 2 additions & 0 deletions src/mutation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "options.h"
#include "output.h"
#include "overmapbuffer.h"
#include "pathfinding.h"
#include "pimpl.h"
#include "player_activity.h"
#include "rng.h"
Expand Down Expand Up @@ -457,6 +458,7 @@ void Character::recalculate_size()
} else {
size_class = creature_size::medium;
}
path_settings->size = size_class;
}

void Character::mutation_effect( const trait_id &mut, const bool worn_destroyed_override )
Expand Down
3 changes: 2 additions & 1 deletion src/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ npc::npc()
patience = 0;
attitude = NPCATT_NULL;

*path_settings = pathfinding_settings( 0, 1000, 1000, 10, true, true, true, true, false, true );
*path_settings = pathfinding_settings( 0, 1000, 1000, 10, true, true, true, true, false, true,
get_size() );
for( direction threat_dir : npc_threat_dir ) {
ai_cache.threat_map[ threat_dir ] = 0.0f;
}
Expand Down
22 changes: 20 additions & 2 deletions src/pathfinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ std::vector<tripoint> map::straight_route( const tripoint &f, const tripoint &t
const pathfinding_cache &pf_cache = get_pathfinding_cache_ref( f.z );
// Check all points for any special case (including just hard terrain)
if( std::any_of( ret.begin(), ret.end(), [&pf_cache]( const tripoint & p ) {
constexpr pf_special non_normal = PF_SLOW | PF_WALL | PF_VEHICLE | PF_TRAP | PF_SHARP;
constexpr pf_special non_normal = PF_SLOW | PF_WALL | PF_VEHICLE | PF_TRAP | PF_SHARP |
PF_SMALL_PASSAGE;
return pf_cache.special[p.x][p.y] & non_normal;
} ) ) {
ret.clear();
Expand Down Expand Up @@ -256,7 +257,8 @@ std::vector<tripoint> map::route( const tripoint &f, const tripoint &t,

bool done = false;

constexpr pf_special non_normal = PF_SLOW | PF_WALL | PF_VEHICLE | PF_TRAP | PF_SHARP;
constexpr pf_special non_normal = PF_SLOW | PF_WALL | PF_VEHICLE | PF_TRAP | PF_SHARP |
PF_SMALL_PASSAGE;
do {
tripoint cur = pf.get_next();

Expand Down Expand Up @@ -331,6 +333,22 @@ std::vector<tripoint> map::route( const tripoint &f, const tripoint &t,
continue;
}

if( settings.size ) {
if( p_special & PF_SMALL_PASSAGE && settings.size > creature_size::medium ) {
layer.closed[index] = true;
continue;
}
if( doors &&
( ( terrain.open && terrain.open->has_flag( ter_furn_flag::TFLAG_SMALL_PASSAGE ) ) ||
( furniture.open && furniture.open->has_flag( ter_furn_flag::TFLAG_SMALL_PASSAGE ) ) ||
// Windows with curtains need to be opened twice
( terrain.open->open && terrain.open->open->has_flag( ter_furn_flag::TFLAG_SMALL_PASSAGE ) ) ) &&
settings.size > creature_size::medium ) {
layer.closed[index] = true;
continue;
}
}

newg += cost;
if( cost == 0 ) {
if( climb_cost > 0 && p_special & PF_CLIMBABLE ) {
Expand Down
10 changes: 8 additions & 2 deletions src/pathfinding.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
#ifndef CATA_SRC_PATHFINDING_H
#define CATA_SRC_PATHFINDING_H

#include <optional>

#include "coordinates.h"
#include "game_constants.h"
#include "mdarray.h"
#include "character.h"

enum pf_special : int {
PF_NORMAL = 0x00, // Plain boring tile (grass, dirt, floor etc.)
Expand All @@ -16,6 +19,7 @@ enum pf_special : int {
PF_UPDOWN = 0x20, // Stairs, ramp etc.
PF_CLIMBABLE = 0x40, // 0 move cost but can be climbed on examine
PF_SHARP = 0x80, // sharp items (barbed wire, etc)
PF_SMALL_PASSAGE = 0x100, // Only medium or smaller characters can fit
};

constexpr pf_special operator | ( pf_special lhs, pf_special rhs )
Expand Down Expand Up @@ -66,14 +70,16 @@ struct pathfinding_settings {
bool avoid_rough_terrain = false;
bool avoid_sharp = false;

std::optional<creature_size> size = std::nullopt;

pathfinding_settings() = default;
pathfinding_settings( const pathfinding_settings & ) = default;

pathfinding_settings( int bs, int md, int ml, int cc, bool aod, bool aud, bool at, bool acs,
bool art, bool as )
bool art, bool as, std::optional<creature_size> sz = std::nullopt )
: bash_strength( bs ), max_dist( md ), max_length( ml ), climb_cost( cc ),
allow_open_doors( aod ), allow_unlock_doors( aud ), avoid_traps( at ), allow_climb_stairs( acs ),
avoid_rough_terrain( art ), avoid_sharp( as ) {}
avoid_rough_terrain( art ), avoid_sharp( as ), size( sz ) {}

pathfinding_settings &operator=( const pathfinding_settings & ) = default;
};
Expand Down

0 comments on commit 31b113c

Please sign in to comment.