Skip to content

Commit

Permalink
Add possibility to define average_length for monsters in JSON.
Browse files Browse the repository at this point in the history
This change will allow to define length for monsters in JSON. It complements Kevins
fix for issue CleverRaven#40446. Along with that it also implements check for quartered corpse.

references CleverRaven#40446

This commit adds definition of "average_length" from JSON and adds it as
attribute longest_side of the monster type object. Referrence is then used
to define "length" in the item generation. Utilizing Kevins' addition
to default based on the items volume.
  • Loading branch information
spiderik committed May 11, 2020
1 parent d15aeee commit 30de543
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions data/json/monsters/fish.json
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@
"id": "mon_fish_rainbow_trout",
"type": "MONSTER",
"copy-from": "mon_fish_medium",
"average_length" : "16 cm",
"name": { "str": "rainbow trout" },
"looks_like": "mon_fish_trout",
"description": "A rainbow trout. A fish made popular by father-son fishing trips, except for the part where you have to gut it."
Expand Down
18 changes: 17 additions & 1 deletion src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4656,13 +4656,27 @@ units::mass item::weight( bool, bool integral ) const
return ret;
}

// corpse longest side helper for item::length
units::length item::corpse_length( const mtype *corpse ) const
{
units::length corpse_length = corpse->longest_side;
if( has_flag( flag_QUARTERED ) ) {
corpse_length /= 4;
}
if( corpse_length > 0_mm ) {
return corpse_length;
}
debugmsg( "invalid monster length for corpse sending default from volume" );
return units::default_length_from_volume<int>( corpse->volume );
}

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 corpse_length( corpse );
}
return type->longest_side;
}
Expand All @@ -4689,6 +4703,8 @@ units::volume item::corpse_volume( const mtype *corpse ) const
return 0_ml;
}



units::volume item::base_volume() const
{
if( is_null() ) {
Expand Down
4 changes: 4 additions & 0 deletions src/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,10 @@ class item : public visitable<item>
/** Volume check for corpses, helper for base_volume(). */
units::volume corpse_volume( const mtype *corpse ) const;


/** Length check for corpses, helper for length(). */
units::length corpse_length( const mtype *corpse ) const;

/** Required strength to be able to successfully lift the item unaided by equipment */
int lift_strength() const;

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

if( obj.longest_side == -1_mm ) {
obj.longest_side = units::default_length_from_volume<int>( obj.volume );
obj.longest_side = units::default_length_from_volume<int>( obj.volume );
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2673,6 +2673,12 @@ units::volume monster::get_volume() const
return units::operator*( type->volume, get_size() / type->size );
}

units::length monster::get_length() const
{
return units::operator*( type->longest_side, get_size() / type->size );
}


void monster::add_msg_if_npc( const std::string &msg ) const
{
if( g->u.sees( *this ) ) {
Expand Down
1 change: 1 addition & 0 deletions src/monster.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class monster : public Creature
units::mass get_weight() const override;
units::mass weight_capacity() const override;
units::volume get_volume() const;
units::length get_length() const;
int get_hp( hp_part ) const override;
int get_hp() const override;
int get_hp_max( hp_part ) const override;
Expand Down
1 change: 1 addition & 0 deletions src/monstergenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,7 @@ void mtype::load( const JsonObject &jo, const std::string &src )
assign( jo, "bodytype", bodytype );
assign( jo, "color", color );
assign( jo, "volume", volume, strict, 0_ml );
assign( jo, "average_length", longest_side );
assign( jo, "weight", weight, strict, 0_gram );

optional( jo, was_loaded, "phase", phase, make_flag_reader( gen.phase_map, "phase id" ), SOLID );
Expand Down
1 change: 1 addition & 0 deletions src/mtype.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ struct mtype {
nc_color color = c_white;
m_size size;
units::volume volume;
units::length longest_side = -1_mm;
units::mass weight;
phase_id phase;

Expand Down

0 comments on commit 30de543

Please sign in to comment.