Skip to content

Commit

Permalink
[CR] NPC spawn with reasonable stored_kcal (#64718)
Browse files Browse the repository at this point in the history
* NPC spawn with healthy stored_kcal

* Set stored kcal before mutate

* Use a chi_squared distribution

* Cleanup
  • Loading branch information
SurFlurer authored Apr 30, 2023
1 parent 8068425 commit 3010c62
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,12 @@ void npc::randomize( const npc_class_id &type )

set_body();
recalc_hp();

randomize_height();
int days_since_cata = to_days<int>( calendar::turn - calendar::start_of_cataclysm );
double time_influence = days_since_cata >= 180 ? 3.0 : 6.0 - 3.0 * days_since_cata / 180.0;
double weight_percent = std::clamp<double>( chi_squared_roll( time_influence ) / 5.0,
0.2, 5.0 );
set_stored_kcal( weight_percent * get_healthy_kcal() );
starting_weapon( myclass );
starting_clothes( *this, myclass, male );
starting_inv( *this, myclass );
Expand Down Expand Up @@ -882,7 +887,6 @@ void npc::randomize( const npc_class_id &type )
}

set_base_age( rng( 18, 55 ) );
randomize_height();

// Add eocs
effect_on_conditions::load_new_character( *this );
Expand Down
7 changes: 7 additions & 0 deletions src/rng.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ double exponential_roll( double lambda )
std::exponential_distribution<>::param_type( lambda ) );
}

double chi_squared_roll( double trial_num )
{
static std::chi_squared_distribution<double> rng_chi_squared_dist;
return rng_chi_squared_dist( rng_get_engine(),
std::chi_squared_distribution<>::param_type( trial_num ) );
}

double rng_exponential( double min, double mean )
{
const double adjusted_mean = mean - min;
Expand Down
2 changes: 2 additions & 0 deletions src/rng.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ inline double rng_normal( double hi )

double normal_roll( double mean, double stddev );

double chi_squared_roll( double trial_num );

double rng_exponential( double min, double mean );

inline double rng_exponential( double mean )
Expand Down

0 comments on commit 3010c62

Please sign in to comment.