Skip to content

Commit

Permalink
JSONize material wind resistance
Browse files Browse the repository at this point in the history
There's no reason for this to be hardcoded, and dynamically constructing
the ids every turn (for Character::update_bodytemp) is actually a
significant performance sink.
Move it to JSON, experience performance improvements.
  • Loading branch information
anothersimulacrum committed Aug 13, 2021
1 parent 842529e commit 78ef2ea
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 12 deletions.
7 changes: 7 additions & 0 deletions data/json/materials.json
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@
"fire_resist": 1,
"elec_resist": 2,
"chip_resist": 8,
"wind_resist": 90,
"repaired_with": "bone",
"salvaged_into": "skewer_bone",
"dmg_adj": [ "scratched", "cut", "cracked", "shattered" ],
Expand Down Expand Up @@ -360,6 +361,7 @@
"fire_resist": 2,
"elec_resist": 2,
"chip_resist": 10,
"wind_resist": 90,
"repaired_with": "chitin_piece",
"salvaged_into": "chitin_piece",
"dmg_adj": [ "scratched", "cut", "cracked", "shattered" ],
Expand Down Expand Up @@ -429,6 +431,7 @@
"fire_resist": 0,
"elec_resist": 2,
"chip_resist": 6,
"wind_resist": 70,
"repaired_with": "cotton_patchwork",
"salvaged_into": "cotton_patchwork",
"dmg_adj": [ "ripped", "torn", "shredded", "tattered" ],
Expand Down Expand Up @@ -1077,6 +1080,7 @@
"fire_resist": 2,
"elec_resist": 2,
"chip_resist": 10,
"wind_resist": 90,
"repaired_with": "leather",
"salvaged_into": "leather",
"dmg_adj": [ "scratched", "cut", "shredded", "tattered" ],
Expand Down Expand Up @@ -1187,6 +1191,7 @@
"fire_resist": 20,
"elec_resist": 4,
"chip_resist": 8,
"wind_resist": 90,
"repaired_with": "nomex",
"salvaged_into": "nomex",
"dmg_adj": [ "ripped", "torn", "shredded", "tattered" ],
Expand Down Expand Up @@ -1325,6 +1330,7 @@
"fire_resist": 1,
"elec_resist": 2,
"chip_resist": 6,
"wind_resist": 90,
"repaired_with": "plastic_chunk",
"salvaged_into": "plastic_chunk",
"dmg_adj": [ "scratched", "cut", "cracked", "shattered" ],
Expand Down Expand Up @@ -1955,6 +1961,7 @@
"fire_resist": 0,
"elec_resist": 2,
"chip_resist": 6,
"wind_resist": 60,
"repaired_with": "felt_patch",
"salvaged_into": "felt_patch",
"dmg_adj": [ "ripped", "torn", "shredded", "tattered" ],
Expand Down
1 change: 1 addition & 0 deletions doc/JSON_INFO.md
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,7 @@ When you sort your inventory by category, these are the categories that are disp
| `dmg_adj` | Description added to damaged item in ascending severity.
| `dmg_adj` | Adjectives used to describe damage states of a material.
| `density` | Affects vehicle collision damage, with denser parts having the advantage over less-dense parts.
| `wind_resist` | Percentage 0-100. How effective this material is at stopping wind from getting through. Higher values are better. If none of the materials an item is made of specify a value, a default of 99 is assumed.
| `vitamins` | Vitamins in a material. Usually overridden by item specific values. An integer percentage of ideal daily value.
| `specific_heat_liquid` | Specific heat of a material when not frozen (J/(g K)). Default 4.186 - water.
| `specific_heat_solid` | Specific heat of a material when frozen (J/(g K)). Default 2.108 - water.
Expand Down
13 changes: 1 addition & 12 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4795,18 +4795,7 @@ std::map<bodypart_id, int> Character::get_wind_resistance( const std::map <bodyp

for( const item *it : on_bp.second ) {
const item &i = *it;
if( i.made_of( material_id( "leather" ) ) || i.made_of( material_id( "plastic" ) ) ||
i.made_of( material_id( "bone" ) ) ||
i.made_of( material_id( "chitin" ) ) || i.made_of( material_id( "nomex" ) ) ) {
penalty = 10; // 90% effective
} else if( i.made_of( material_id( "cotton" ) ) ) {
penalty = 30;
} else if( i.made_of( material_id( "wool" ) ) ) {
penalty = 40;
} else {
penalty = 1; // 99% effective
}

penalty = 100 - i.wind_resist();
coverage = std::max( 0, i.get_coverage( bp ) - penalty );
totalExposed *= ( 1.0 - coverage / 100.0 ); // Coverage is between 0 and 1?
}
Expand Down
24 changes: 24 additions & 0 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7439,6 +7439,30 @@ bool item::is_broken() const
return has_flag( flag_ITEM_BROKEN );
}

int item::wind_resist() const
{
std::vector<const material_type *> materials = made_of_types();
if( materials.empty() ) {
debugmsg( "Called item::wind_resist on an item (%s) made of nothing!", tname() );
return 99;
}

int best = -1;
for( const material_type *mat : materials ) {
cata::optional<int> resistance = mat->wind_resist();
if( resistance && *resistance > best ) {
best = *resistance;
}
}

// Default to 99% effective
if( best == -1 ) {
return 99;
}

return best;
}

std::set<fault_id> item::faults_potential() const
{
std::set<fault_id> res;
Expand Down
3 changes: 3 additions & 0 deletions src/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,9 @@ class item : public visitable

void set_last_temp_check( const time_point &pt );

/** How resistant clothes made of this material are to wind (0-100) */
int wind_resist() const;

/** What faults can potentially occur with this item? */
std::set<fault_id> faults_potential() const;

Expand Down
11 changes: 11 additions & 0 deletions src/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ void material_type::load( const JsonObject &jsobj, const std::string & )
mandatory( jsobj, was_loaded, "chip_resist", _chip_resist );
mandatory( jsobj, was_loaded, "density", _density );

optional( jsobj, was_loaded, "wind_resist", _wind_resist );
optional( jsobj, was_loaded, "specific_heat_liquid", _specific_heat_liquid );
optional( jsobj, was_loaded, "specific_heat_solid", _specific_heat_solid );
optional( jsobj, was_loaded, "latent_heat", _latent_heat );
Expand Down Expand Up @@ -119,6 +120,11 @@ void material_type::check() const
if( !item::type_is_defined( _repaired_with ) ) {
debugmsg( "invalid \"repaired_with\" %s for %s.", _repaired_with.c_str(), id.c_str() );
}

if( _wind_resist && ( *_wind_resist > 100 || *_wind_resist < 0 ) ) {
debugmsg( "Wind resistance outside of range (100%% to 0%%, is %d%%) for %s.", *_wind_resist,
id.str() );
}
}

material_id material_type::ident() const
Expand Down Expand Up @@ -222,6 +228,11 @@ int material_type::density() const
return _density;
}

cata::optional<int> material_type::wind_resist() const
{
return _wind_resist;
}

bool material_type::edible() const
{
return _edible;
Expand Down
3 changes: 3 additions & 0 deletions src/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class material_type
int _bullet_resist = 0;
int _chip_resist = 0; // Resistance to physical damage of the item itself
int _density = 1; // relative to "powder", which is 1
// How resistant this material is to wind as a percentage - 0 to 100
cata::optional<int> _wind_resist;
float _specific_heat_liquid = 4.186f;
float _specific_heat_solid = 2.108f;
float _latent_heat = 334.0f;
Expand Down Expand Up @@ -125,6 +127,7 @@ class material_type
float latent_heat() const;
float freeze_point() const;
int density() const;
cata::optional<int> wind_resist() const;
bool edible() const;
bool rotting() const;
bool soft() const;
Expand Down

0 comments on commit 78ef2ea

Please sign in to comment.