-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathcata_utility.cpp
555 lines (484 loc) · 15.5 KB
/
cata_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#include "cata_utility.h"
#include <cctype>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <exception>
#include <iterator>
#include <memory>
#include <sstream>
#include <stdexcept>
#include "debug.h"
#include "filesystem.h"
#include "json.h"
#include "mapsharing.h"
#include "options.h"
#include "output.h"
#include "rng.h"
#include "translations.h"
#include "units.h"
#include "catacharset.h"
static double pow10( unsigned int n )
{
double ret = 1;
double tmp = 10;
while( n ) {
if( n & 1 ) {
ret *= tmp;
}
tmp *= tmp;
n >>= 1;
}
return ret;
}
double round_up( double val, unsigned int dp )
{
// Some implementations of std::pow does not return the accurate result even
// for small powers of 10, so we use a specialized routine to calculate them.
const double denominator = pow10( dp );
return std::ceil( denominator * val ) / denominator;
}
int modulo( int v, int m )
{
// C++11: negative v and positive m result in negative v%m (or 0),
// but this is supposed to be mathematical modulo: 0 <= v%m < m,
const int r = v % m;
// Adding m in that (and only that) case.
return r >= 0 ? r : r + m;
}
bool isBetween( int test, int down, int up )
{
return test > down && test < up;
}
bool lcmatch( const std::string &str, const std::string &qry )
{
if( std::locale().name() != "en_US.UTF-8" && std::locale().name() != "C" ) {
auto &f = std::use_facet<std::ctype<wchar_t>>( std::locale() );
std::wstring wneedle = utf8_to_wstr( qry );
std::wstring whaystack = utf8_to_wstr( str );
f.tolower( &whaystack[0], &whaystack[0] + whaystack.size() );
f.tolower( &wneedle[0], &wneedle[0] + wneedle.size() );
return whaystack.find( wneedle ) != std::wstring::npos;
}
std::string needle;
needle.reserve( qry.size() );
std::transform( qry.begin(), qry.end(), std::back_inserter( needle ), tolower );
std::string haystack;
haystack.reserve( str.size() );
std::transform( str.begin(), str.end(), std::back_inserter( haystack ), tolower );
return haystack.find( needle ) != std::string::npos;
}
bool lcmatch( const translation &str, const std::string &qry )
{
return lcmatch( str.translated(), qry );
}
bool match_include_exclude( const std::string &text, std::string filter )
{
size_t iPos;
bool found = false;
if( filter.empty() ) {
return false;
}
do {
iPos = filter.find( ',' );
std::string term = iPos == std::string::npos ? filter : filter.substr( 0, iPos );
const bool exclude = term.substr( 0, 1 ) == "-";
if( exclude ) {
term = term.substr( 1 );
}
if( ( !found || exclude ) && lcmatch( text, term ) ) {
if( exclude ) {
return false;
}
found = true;
}
if( iPos != std::string::npos ) {
filter = filter.substr( iPos + 1, filter.size() );
}
} while( iPos != std::string::npos );
return found;
}
// --- Library functions ---
// This stuff could be moved elsewhere, but there
// doesn't seem to be a good place to put it right now.
double logarithmic( double t )
{
return 1 / ( 1 + exp( -t ) );
}
double logarithmic_range( int min, int max, int pos )
{
const double LOGI_CUTOFF = 4;
const double LOGI_MIN = logarithmic( -LOGI_CUTOFF );
const double LOGI_MAX = logarithmic( +LOGI_CUTOFF );
const double LOGI_RANGE = LOGI_MAX - LOGI_MIN;
if( min >= max ) {
debugmsg( "Invalid interval (%d, %d).", min, max );
return 0.0;
}
// Anything beyond (min,max) gets clamped.
if( pos <= min ) {
return 1.0;
} else if( pos >= max ) {
return 0.0;
}
// Normalize the pos to [0,1]
double range = max - min;
double unit_pos = ( pos - min ) / range;
// Scale and flip it to [+LOGI_CUTOFF,-LOGI_CUTOFF]
double scaled_pos = LOGI_CUTOFF - 2 * LOGI_CUTOFF * unit_pos;
// Get the raw logistic value.
double raw_logistic = logarithmic( scaled_pos );
// Scale the output to [0,1]
return ( raw_logistic - LOGI_MIN ) / LOGI_RANGE;
}
int bound_mod_to_vals( int val, int mod, int max, int min )
{
if( val + mod > max && max != 0 ) {
mod = std::max( max - val, 0 );
}
if( val + mod < min && min != 0 ) {
mod = std::min( min - val, 0 );
}
return mod;
}
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!";
}
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;
}
double temp_to_celsius( double fahrenheit )
{
return ( ( fahrenheit - 32.0 ) * 5.0 / 9.0 );
}
double temp_to_kelvin( double fahrenheit )
{
return temp_to_celsius( fahrenheit ) + 273.15;
}
double kelvin_to_fahrenheit( double kelvin )
{
return 1.8 * ( kelvin - 273.15 ) + 32;
}
double clamp_to_width( double value, int width, int &scale )
{
return clamp_to_width( value, width, scale, nullptr );
}
double clamp_to_width( double value, int width, int &scale, bool *out_truncated )
{
if( out_truncated != nullptr ) {
*out_truncated = false;
}
if( value >= std::pow( 10.0, width ) ) {
// above the maximum number we can fit in the width without decimal
// show the biggest number we can without decimal
// flag as truncated
value = std::pow( 10.0, width ) - 1.0;
scale = 0;
if( out_truncated != nullptr ) {
*out_truncated = true;
}
} else if( scale > 0 ) {
for( int s = 1; s <= scale; s++ ) {
// 1 decimal separator + "s"
int scale_width = 1 + s;
if( width > scale_width && value >= std::pow( 10.0, width - scale_width ) ) {
// above the maximum number we can fit in the width with "s" decimals
// show this number with one less decimal than "s"
scale = s - 1;
break;
}
}
}
return value;
}
float multi_lerp( const std::vector<std::pair<float, float>> &points, float x )
{
size_t i = 0;
while( i < points.size() && points[i].first <= x ) {
i++;
}
if( i == 0 ) {
return points.front().second;
} else if( i >= points.size() ) {
return points.back().second;
}
// How far are we along the way from last threshold to current one
const float t = ( x - points[i - 1].first ) /
( points[i].first - points[i - 1].first );
// Linear interpolation of values at relevant thresholds
return ( t * points[i].second ) + ( ( 1 - t ) * points[i - 1].second );
}
void write_to_file( const std::string &path, const std::function<void( std::ostream & )> &writer )
{
// Any of the below may throw. ofstream_wrapper will clean up the temporary path on its own.
ofstream_wrapper fout( path, std::ios::binary );
writer( fout.stream() );
fout.close();
}
bool write_to_file( const std::string &path, const std::function<void( std::ostream & )> &writer,
const char *const fail_message )
{
try {
write_to_file( path, writer );
return true;
} catch( const std::exception &err ) {
if( fail_message ) {
popup( _( "Failed to write %1$s to \"%2$s\": %3$s" ), fail_message, path.c_str(), err.what() );
}
return false;
}
}
ofstream_wrapper::ofstream_wrapper( const std::string &path, const std::ios::openmode mode )
: path( path )
{
open( mode );
}
ofstream_wrapper::~ofstream_wrapper()
{
try {
close();
} catch( ... ) {
// ignored in destructor
}
}
std::istream &safe_getline( std::istream &ins, std::string &str )
{
str.clear();
std::istream::sentry se( ins, true );
std::streambuf *sb = ins.rdbuf();
while( true ) {
int c = sb->sbumpc();
switch( c ) {
case '\n':
return ins;
case '\r':
if( sb->sgetc() == '\n' ) {
sb->sbumpc();
}
return ins;
case EOF:
if( str.empty() ) {
ins.setstate( std::ios::eofbit );
}
return ins;
default:
str += static_cast<char>( c );
}
}
}
bool read_from_file( const std::string &path, const std::function<void( std::istream & )> &reader )
{
try {
std::ifstream fin( path, std::ios::binary );
if( !fin ) {
throw std::runtime_error( "opening file failed" );
}
reader( fin );
if( fin.bad() ) {
throw std::runtime_error( "reading file failed" );
}
return true;
} catch( const std::exception &err ) {
debugmsg( _( "Failed to read from \"%1$s\": %2$s" ), path.c_str(), err.what() );
return false;
}
}
bool read_from_file_json( const std::string &path, const std::function<void( JsonIn & )> &reader )
{
return read_from_file( path, [&reader]( std::istream & fin ) {
JsonIn jsin( fin );
reader( jsin );
} );
}
bool read_from_file( const std::string &path, JsonDeserializer &reader )
{
return read_from_file_json( path, [&reader]( JsonIn & jsin ) {
reader.deserialize( jsin );
} );
}
bool read_from_file_optional( const std::string &path,
const std::function<void( std::istream & )> &reader )
{
// Note: slight race condition here, but we'll ignore it. Worst case: the file
// exists and got removed before reading it -> reading fails with a message
// Or file does not exists, than everything works fine because it's optional anyway.
return file_exist( path ) && read_from_file( path, reader );
}
bool read_from_file_optional_json( const std::string &path,
const std::function<void( JsonIn & )> &reader )
{
return read_from_file_optional( path, [&reader]( std::istream & fin ) {
JsonIn jsin( fin );
reader( jsin );
} );
}
bool read_from_file_optional( const std::string &path, JsonDeserializer &reader )
{
return read_from_file_optional_json( path, [&reader]( JsonIn & jsin ) {
reader.deserialize( jsin );
} );
}
std::string obscure_message( const std::string &str, std::function<char()> f )
{
//~ translators: place some random 1-width characters here in your language if possible, or leave it as is
std::string gibberish_narrow = _( "abcdefghijklmnopqrstuvwxyz" );
std::string gibberish_wide =
//~ translators: place some random 2-width characters here in your language if possible, or leave it as is
_( "に坂索トし荷測のンおク妙免イロコヤ梅棋厚れ表幌" );
std::wstring w_gibberish_narrow = utf8_to_wstr( gibberish_narrow );
std::wstring w_gibberish_wide = utf8_to_wstr( gibberish_wide );
std::wstring w_str = utf8_to_wstr( str );
// a trailing NULL terminator is necessary for utf8_width function
char transformation[2] = { 0 };
for( size_t i = 0; i < w_str.size(); ++i ) {
transformation[0] = f();
std::string this_char = wstr_to_utf8( std::wstring( 1, w_str[i] ) );
if( transformation[0] == -1 ) {
continue;
} else if( transformation[0] == 0 ) {
if( utf8_width( this_char ) == 1 ) {
w_str[i] = random_entry( w_gibberish_narrow );
} else {
w_str[i] = random_entry( w_gibberish_wide );
}
} else {
// Only support the case e.g. replace current character to symbols like # or ?
if( utf8_width( transformation ) != 1 ) {
debugmsg( "target character isn't narrow" );
}
// A 2-width wide character in the original string should be replace by two narrow characters
w_str.replace( i, 1, utf8_to_wstr( std::string( utf8_width( this_char ), transformation[0] ) ) );
}
}
std::string result = wstr_to_utf8( w_str );
if( utf8_width( str ) != utf8_width( result ) ) {
debugmsg( "utf8_width differ between original string and obscured string" );
}
return result;
}
std::string serialize_wrapper( const std::function<void( JsonOut & )> &callback )
{
std::ostringstream buffer;
JsonOut jsout( buffer );
callback( jsout );
return buffer.str();
}
void deserialize_wrapper( const std::function<void( JsonIn & )> &callback, const std::string &data )
{
std::istringstream buffer( data );
JsonIn jsin( buffer );
callback( jsin );
}
bool string_starts_with( const std::string &s1, const std::string &s2 )
{
return s1.compare( 0, s2.size(), s2 ) == 0;
}
bool string_ends_with( const std::string &s1, const std::string &s2 )
{
return s1.size() >= s2.size() &&
s1.compare( s1.size() - s2.size(), s2.size(), s2 ) == 0;
}
std::string join( const std::vector<std::string> &strings, const std::string &joiner )
{
std::ostringstream buffer;
for( auto a = strings.begin(); a != strings.end(); ++a ) {
if( a != strings.begin() ) {
buffer << joiner;
}
buffer << *a;
}
return buffer.str();
}