forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
units_utility.cpp
120 lines (108 loc) · 2.84 KB
/
units_utility.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "units_utility.h"
#include "options.h"
const char *velocity_units( const units_type vel_units )
{
if( get_option<std::string>( "USE_METRIC_SPEEDS" ) == "mph" ) {
return _( "mph" );
} else if( get_option<std::string>( "USE_METRIC_SPEEDS" ) == "t/t" ) {
//~ vehicle speed tiles per turn
return _( "t/t" );
} else {
switch( vel_units ) {
case VU_VEHICLE:
return _( "km/h" );
case VU_WIND:
return _( "m/s" );
}
}
return "error: unknown units!";
}
units::angle normalize( units::angle a, units::angle mod )
{
a = units::fmod( a, mod );
if( a < 0_degrees ) {
a += mod;
}
return a;
}
const char *weight_units()
{
return get_option<std::string>( "USE_METRIC_WEIGHTS" ) == "lbs" ? _( "lbs" ) : _( "kg" );
}
const char *volume_units_abbr()
{
const std::string vol_units = get_option<std::string>( "VOLUME_UNITS" );
if( vol_units == "c" ) {
return pgettext( "Volume unit", "c" );
} else if( vol_units == "l" ) {
return pgettext( "Volume unit", "L" );
} else {
return pgettext( "Volume unit", "qt" );
}
}
const char *volume_units_long()
{
const std::string vol_units = get_option<std::string>( "VOLUME_UNITS" );
if( vol_units == "c" ) {
return _( "cup" );
} else if( vol_units == "l" ) {
return _( "liter" );
} else {
return _( "quart" );
}
}
double convert_velocity( int velocity, const units_type vel_units )
{
const std::string type = get_option<std::string>( "USE_METRIC_SPEEDS" );
// internal units to mph conversion
double ret = static_cast<double>( velocity ) / 100;
if( type == "km/h" ) {
switch( vel_units ) {
case VU_VEHICLE:
// mph to km/h conversion
ret *= 1.609f;
break;
case VU_WIND:
// mph to m/s conversion
ret *= 0.447f;
break;
}
} else if( type == "t/t" ) {
ret /= 4;
}
return ret;
}
double convert_weight( const units::mass &weight )
{
double ret = to_gram( weight );
if( get_option<std::string>( "USE_METRIC_WEIGHTS" ) == "kg" ) {
ret /= 1000;
} else {
ret /= 453.6;
}
return ret;
}
double convert_volume( int volume )
{
return convert_volume( volume, nullptr );
}
double convert_volume( int volume, int *out_scale )
{
double ret = volume;
int scale = 0;
const std::string vol_units = get_option<std::string>( "VOLUME_UNITS" );
if( vol_units == "c" ) {
ret *= 0.004;
scale = 1;
} else if( vol_units == "l" ) {
ret *= 0.001;
scale = 2;
} else {
ret *= 0.00105669;
scale = 2;
}
if( out_scale != nullptr ) {
*out_scale = scale;
}
return ret;
}