Skip to content

Commit

Permalink
refactor: weather_manager::temperatrue to units::temperature
Browse files Browse the repository at this point in the history
  • Loading branch information
scarf005 committed Apr 5, 2023
1 parent 6e7e9ab commit 819dcce
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "string_formatter.h"
#include "type_id.h"
#include "units.h"
#include "units_temperature.h"
#include "visitable.h"
#include "weighted_list.h"

Expand Down Expand Up @@ -81,7 +82,7 @@ template <typename E> struct enum_traits;

enum class character_stat : char;

#define MAX_CLAIRVOYANCE 40
static constexpr int MAX_CLAIRVOYANCE = 40;

enum vision_modes {
DEBUG_NIGHTVISION,
Expand Down Expand Up @@ -2038,7 +2039,7 @@ class Character : public Creature, public visitable<Character>
/** Drenches the player with water, saturation is the percent gotten wet */
void drench( int saturation, const body_part_set &flags, bool ignore_waterproof );
/** Recalculates morale penalty/bonus from wetness based on mutations, equipment and temperature */
void apply_wetness_morale( int temperature );
void apply_wetness_morale( const units::temperature &temperature );
std::vector<std::string> short_description_parts() const;
std::string short_description() const;
int print_info( const catacurses::window &w, int vStart, int vLines, int column ) const override;
Expand Down
3 changes: 2 additions & 1 deletion src/game_constants.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include "units_temperature.h"
#ifndef CATA_SRC_GAME_CONSTANTS_H
#define CATA_SRC_GAME_CONSTANTS_H

Expand Down Expand Up @@ -146,7 +147,7 @@ static constexpr int HORDE_VISIBILITY_SIZE = 3;
* Base starting spring temperature in F used for climate, weather and temperature calculation.
* New England base spring temperature = 65F/18C rounded to int.
*/
static constexpr int SPRING_TEMPERATURE = 65;
static constexpr units::temperature SPRING_TEMPERATURE = 18_c;

/**
* Used to limit the random seed during noise calculation. A large value flattens the noise generator to zero.
Expand Down
2 changes: 1 addition & 1 deletion src/gamemode_defense.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ defense_game::defense_game()
bool defense_game::init()
{
calendar::turn = calendar::turn_zero + 12_hours; // Start at noon
get_weather().temperature = 65;
get_weather().temperature = SPRING_TEMPERATURE;
if( !g->u.create( character_type::CUSTOM ) ) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/gamemode_tutorial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ bool tutorial_game::init()
calendar::turn = calendar::turn_zero + 12_hours;
tutorials_seen.clear();
g->scent.reset();
get_weather().temperature = 65;
get_weather().temperature = SPRING_TEMPERATURE;
// We use a Z-factor of 10 so that we don't plop down tutorial rooms in the
// middle of the "real" game world
character_funcs::normalize( you );
Expand Down
7 changes: 5 additions & 2 deletions src/suffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include "translations.h"
#include "type_id.h"
#include "units.h"
#include "units_temperature.h"
#include "weather.h"

static const bionic_id bio_advreactor( "bio_advreactor" );
Expand Down Expand Up @@ -1806,7 +1807,7 @@ void Character::drench( int saturation, const body_part_set &flags, bool ignore_
}
}

void Character::apply_wetness_morale( int temperature )
void Character::apply_wetness_morale( const units::temperature &celsius_temperature )
{
// First, a quick check if we have any wetness to calculate morale from
// Faster than checking all worn items for friendliness
Expand All @@ -1817,8 +1818,10 @@ void Character::apply_wetness_morale( int temperature )
return;
}

int temperature = units::celsius_to_fahrenheit( celsius_temperature.value() );
// Normalize temperature to [-1.0,1.0]
temperature = std::max( 0, std::min( 100, temperature ) );
temperature = std::clamp( temperature, 0, 100 );

const double global_temperature_mod = -1.0 + ( 2.0 * temperature / 100.0 );

int total_morale = 0;
Expand Down
9 changes: 4 additions & 5 deletions src/weather.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ weather_manager::weather_manager()
lightning_active = false;
weather_override = weather_type_id::NULL_ID();
nextweather = calendar::before_time_starts;
temperature = 0;
temperature = 0_c;
weather_id = weather_type_id::NULL_ID();
}

Expand All @@ -1067,7 +1067,7 @@ void weather_manager::update_weather()
weather_override = weather_type_id::NULL_ID();
}
sfx::do_ambient();
temperature = units::to_fahrenheit( w.temperature );
temperature = w.temperature;
lightning_active = false;
// Check weather every few turns, instead of every turn.
// TODO: predict when the weather changes and use that time.
Expand Down Expand Up @@ -1118,9 +1118,8 @@ int weather_manager::get_temperature( const tripoint &location ) const
temp_mod += get_convection_temperature( location );
}
//underground temperature = average New England temperature = 43F/6C rounded to int
const int temp = ( location.z < 0 ? units::to_fahrenheit( average_annual_termperature ) :
temperature ) +
( g->new_game ? 0 : g->m.get_temperature( location ) + temp_mod );
const int temp = units::to_fahrenheit( location.z < 0 ? average_annual_termperature : temperature )
+ ( g->new_game ? 0 : g->m.get_temperature( location ) + temp_mod );

temperature_cache.emplace( std::make_pair( location, temp ) );
return temp;
Expand Down
4 changes: 2 additions & 2 deletions src/weather.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ class weather_manager

// Updates the temperature and weather pattern
void update_weather();
// The air temperature in Fahrenheit
int temperature = 0;
// The air temperature
units::temperature temperature = 0_c;
units::temperature water_temperature = 0_c;
bool lightning_active = false;
// Weather pattern
Expand Down
2 changes: 1 addition & 1 deletion tests/behavior_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ TEST_CASE( "check_npc_behavior_tree", "[npc][behavior]" )
CHECK( npc_needs.tick( &oracle ) == "idle" );
SECTION( "Freezing" ) {
weather_manager &weather = get_weather();
weather.temperature = -100;
weather.temperature = -70_c;
weather.clear_temp_cache();
test_npc.update_bodytemp( get_map(), weather );
CHECK( npc_needs.tick( &oracle ) == "idle" );
Expand Down
4 changes: 2 additions & 2 deletions tests/player_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ static void test_temperature_spread( player &p,
{
const auto thresholds = bodytemp_voronoi();
for( int i = 0; i < 7; i++ ) {
get_weather().temperature = to_fahrenheit( air_temperatures[i] );
get_weather().temperature = air_temperatures[i];
get_weather().clear_temp_cache();
CAPTURE( air_temperatures[i] );
CAPTURE( get_weather().temperature );
Expand Down Expand Up @@ -322,7 +322,7 @@ static std::array<units::temperature, bodytemps.size()> find_temperature_points(
std::vector<temperatures_wrapper> all_converged_temperatures;
all_converged_temperatures.resize( max_air_temp - min_air_temp, temperatures_wrapper( {} ) );
for( int i = min_air_temp; i < max_air_temp; i++ ) {
get_weather().temperature = i;
get_weather().temperature = units::from_fahrenheit( i );
get_weather().clear_temp_cache();
all_converged_temperatures[i - min_air_temp] = converge_temperature( p, 10000 );
int converged_torso_temp = all_converged_temperatures[i - min_air_temp][0];
Expand Down
3 changes: 2 additions & 1 deletion tests/rot_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
#include "map_helpers.h"
#include "game.h" // Just for get_convection_temperature(), TODO: Remove
#include "point.h"
#include "units_temperature.h"
#include "weather.h"

static const furn_str_id f_atomic_freezer( "f_atomic_freezer" );

static void set_map_temperature( weather_manager &weather, int new_temperature )
{
weather.temperature = new_temperature;
weather.temperature = units::from_fahrenheit( new_temperature );
weather.clear_temp_cache();
}

Expand Down

0 comments on commit 819dcce

Please sign in to comment.