Skip to content

Commit

Permalink
Fix corpse length (CleverRaven#40454)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevingranade authored May 14, 2020
1 parent 076a210 commit e6eb0ff
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4685,6 +4685,9 @@ units::length item::length() const
if( made_of( LIQUID ) || is_soft() ) {
return 0_mm;
}
if( is_corpse() ) {
return units::default_length_from_volume<int>( corpse->volume );
}
return type->longest_side;
}

Expand Down
4 changes: 3 additions & 1 deletion src/item_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,9 @@ void Item_factory::finalize_pre( itype &obj )
}

if( obj.longest_side == -1_mm ) {
obj.longest_side = units::cube_to_volume<int>( obj.volume );
units::volume effective_volume = obj.count_by_charges() ?
( obj.volume / obj.stack_size ) : obj.volume;
obj.longest_side = units::default_length_from_volume<int>( effective_volume );
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/item_pocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void pocket_data::load( const JsonObject &jo )
mandatory( jo, was_loaded, "max_contains_volume", volume_capacity, volume_reader() );
mandatory( jo, was_loaded, "max_contains_weight", max_contains_weight, mass_reader() );
optional( jo, was_loaded, "max_item_length", max_item_length,
units::cube_to_volume( volume_capacity ) * M_SQRT2 );
units::default_length_from_volume( volume_capacity ) * M_SQRT2 );
}
optional( jo, was_loaded, "spoil_multiplier", spoil_multiplier, 1.0f );
optional( jo, was_loaded, "weight_multiplier", weight_multiplier, 1.0f );
Expand Down
2 changes: 1 addition & 1 deletion src/units.h
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ inline constexpr value_type to_kilometer( const quantity<value_type, length_in_m

// converts a volume as if it were a cube to the length of one side
template<typename value_type>
inline constexpr quantity<value_type, length_in_millimeter_tag> cube_to_volume(
inline constexpr quantity<value_type, length_in_millimeter_tag> default_length_from_volume(
const quantity<value_type, volume_in_milliliter_tag> &v )
{
return units::from_centimeter<int>(
Expand Down
46 changes: 46 additions & 0 deletions tests/item_test.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <cmath>
#include <initializer_list>
#include <limits>
#include <memory>
Expand All @@ -6,7 +7,9 @@
#include "catch/catch.hpp"
#include "enums.h"
#include "item.h"
#include "item_factory.h"
#include "itype.h"
#include "monstergenerator.h"
#include "ret_val.h"
#include "units.h"
#include "value_ptr.h"
Expand Down Expand Up @@ -157,3 +160,46 @@ TEST_CASE( "stacking_over_time", "[item]" )
}
}
}

static void assert_minimum_length_to_volume_ratio( const item &target )
{
if( target.type->get_id() == "null" ) {
return;
}
CAPTURE( target.type->get_id() );
CAPTURE( target.volume() );
CAPTURE( target.base_volume() );
CAPTURE( target.type->volume );
if( target.made_of( LIQUID ) || target.is_soft() ) {
CHECK( target.length() == 0_mm );
return;
}
if( target.volume() == 0_ml ) {
CHECK( target.length() == -1_mm );
return;
}
if( target.volume() == 1_ml ) {
CHECK( target.length() >= 0_mm );
return;
}
// Minimum possible length is if the item is a sphere.
const float minimal_diameter = std::cbrt( ( 3.0 * units::to_milliliter( target.base_volume() ) ) /
( 4.0 * M_PI ) );
CHECK( units::to_millimeter( target.length() ) >= minimal_diameter * 10.0 );
}

TEST_CASE( "item length sanity check", "[item]" )
{
for( const itype *type : item_controller->all() ) {
const item sample( type, 0, item::solitary_tag {} );
assert_minimum_length_to_volume_ratio( sample );
}
}

TEST_CASE( "corpse length sanity check", "[item]" )
{
for( const mtype &type : MonsterGenerator::generator().get_all_mtypes() ) {
const item sample = item::make_corpse( type.id );
assert_minimum_length_to_volume_ratio( sample );
}
}

0 comments on commit e6eb0ff

Please sign in to comment.