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

Sane-ify in-vehicle movement size checks #74004

Merged
merged 6 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Common Creature::can_move_to_vehicle_tile definition
  • Loading branch information
RenechCDDA committed May 22, 2024
commit fc043afe7788988103d11860ce1463d8ede46c60
80 changes: 80 additions & 0 deletions src/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ static const damage_type_id damage_heat( "heat" );
static const efftype_id effect_all_fours( "all_fours" );
static const efftype_id effect_blind( "blind" );
static const efftype_id effect_bounced( "bounced" );
static const efftype_id effect_cramped_space( "cramped_space" );
static const efftype_id effect_downed( "downed" );
static const efftype_id effect_foamcrete_slow( "foamcrete_slow" );
static const efftype_id effect_invisibility( "invisibility" );
Expand Down Expand Up @@ -192,6 +193,85 @@ void Creature::setpos( const tripoint &p )
on_move( old_loc );
}

static units::volume size_to_volume( creature_size size_class )
{
if( size_class == creature_size::tiny ) {
return 7499_ml;
} else if( size_class == creature_size::small ) {
return 46249_ml;
} else if( size_class == creature_size::medium ) {
return 107999_ml;
} else if( size_class == creature_size::large ) {
return 483749_ml;
}
return DEFAULT_TILE_VOLUME - 1_ml;
}

bool Creature::can_move_to_vehicle_tile( const tripoint_abs_ms &loc, bool &cramped ) const
{
map &here = get_map();
const optional_vpart_position vp_there = here.veh_at( loc );
if( !vp_there ) {
return true;
}

const monster *mon = as_monster();

vehicle &veh = vp_there->vehicle();

std::vector<vehicle_part *> cargo_parts;
cargo_parts = veh.get_parts_at( here.bub_from_abs( loc ), "CARGO", part_status_flag::any );

units::volume capacity = 0_ml;
units::volume free_cargo = 0_ml;
for( vehicle_part *part : cargo_parts ) {
vehicle_stack contents = veh.get_items( *part );
if( !vp_there.part_with_feature( "CARGO_PASSABLE", false ) &&
!vp_there.part_with_feature( "APPLIANCE", false ) &&
!vp_there.part_with_feature( "OBSTACLE", false ) ) {
capacity += contents.max_volume();
free_cargo += contents.free_volume();
}
}
if( capacity > 0_ml ) {
// First, we'll try to squeeze in. Open-topped vehicle parts have more room to step over cargo.
if( !veh.enclosed_at( here.getlocal( loc ) ) ) {
free_cargo *= 1.2;
}
const creature_size size = get_size();
units::volume critter_volume;
if( mon ) {
critter_volume = mon->get_volume();
} else {
critter_volume = size_to_volume( size );
}

if( critter_volume > free_cargo ) {
return false;
}

if( critter_volume > size_to_volume( creature_size::large ) &&
!vp_there.part_with_feature( "HUGE_OK", false ) ) {
return false;
}

if( critter_volume < free_cargo * 1.33 ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Am I reading this right? If the creature's volume is less than a third over the free cargo space and isn't a monster, snake, blob or fish-type then it's cramped? Shouldn't the check be for if the creature's volume greater than a third over free cargo space?

Copy link
Member Author

Choose a reason for hiding this comment

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

Reading it right, interpreting it wrong. If the critter is greater than the free cargo size, it can't move into this tile in the first place - we return false earlier in the function. https://github.com/CleverRaven/Cataclysm-DDA/pull/74004/files#diff-5a026da090b6664f91c5f3bc4a9d0844cff966ef6fa5785d258b3aa47b370a2cR253

But yeah this logic is confusing, and part of the reason why I wanted to get rid of the huge block of volume comparisons shown.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, yeah, point. I was so focused on chasing down what was setting cramped to true I failed to remember what the whole function was about.

That said, I'm reasonably sure this particular check is the culprit when it comes to #74068 since commenting out cramped = true; and recompiling prevents the behavior reported there from occurring.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, the equation should be free_cargo * 0.75 < critter_volume instead,
thus we set cramped when free_cargo * 0.75 < critter_volume && critter_volume <= free_cargo

I'm already working on it tonight, this whole thing is still a mess.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just tested critter_volume * 0.75 < free_cargo && critter_volume <= free_cargo and it's still setting cramped to true.

if( !mon || !( mon->type->bodytype == "snake" || mon->type->bodytype == "blob" ||
mon->type->bodytype == "fish" ||
has_flag( mon_flag_PLASTIC ) || has_flag( mon_flag_SMALL_HIDER ) ) ) {
cramped = true;
}
}

if( size == creature_size::huge && !vp_there.part_with_feature( "AISLE", false ) &&
!vp_there.part_with_feature( "HUGE_OK", false ) ) {
cramped = true;
}
}

return true;
}

void Creature::move_to( const tripoint_abs_ms &loc )
{
const tripoint_abs_ms old_loc = get_location();
Expand Down
2 changes: 2 additions & 0 deletions src/creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ class Creature : public viewer
return get_location().z();
}
void setpos( const tripoint &p );
/** Checks if the creature fits into a given tile */
bool can_move_to_vehicle_tile( const tripoint_abs_ms &loc, bool &cramped ) const;
/** Moves the creature to the given location and calls the on_move() handler. */
void move_to( const tripoint_abs_ms &loc );

Expand Down