forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mood_face.cpp
119 lines (99 loc) · 2.92 KB
/
mood_face.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
#include "mood_face.h"
#include "avatar.h"
#include "debug.h"
#include "generic_factory.h"
#include "json.h"
#include "mutation.h"
#include "options.h"
static const mood_face_id mood_face_DEFAULT( "DEFAULT" );
static const mood_face_id mood_face_DEFAULT_HORIZONTAL( "DEFAULT_HORIZONTAL" );
namespace
{
generic_factory<mood_face> mood_face_factory( "mood_face" );
} // namespace
template<>
const mood_face &mood_face_id::obj() const
{
return mood_face_factory.obj( *this );
}
/** @relates string_id */
template<>
bool mood_face_id::is_valid() const
{
return mood_face_factory.is_valid( *this );
}
void mood_face::load_mood_faces( const JsonObject &jo, const std::string &src )
{
mood_face_factory.load( jo, src );
}
void mood_face::reset()
{
mood_face_factory.reset();
}
void mood_face::load( const JsonObject &jo, const std::string & )
{
mandatory( jo, was_loaded, "values", values_ );
std::sort( values_.begin(), values_.end(),
[]( const mood_face_value & valueA, const mood_face_value & valueB ) {
return valueA.value() > valueB.value();
} );
}
const std::vector<mood_face> &mood_face::get_all()
{
return mood_face_factory.get_all();
}
void mood_face_value::load( const JsonObject &jo )
{
mandatory( jo, was_loaded, "value", value_ );
mandatory( jo, was_loaded, "face", face_ );
}
void mood_face_value::deserialize( JsonIn &jsin )
{
JsonObject data = jsin.get_object();
load( data );
}
const mood_face_id &avatar::character_mood_face( bool clear_cache ) const
{
static cata::optional<mood_face_id> mood_face_cache;
static bool mood_face_horizontal = false;
const bool option_horizontal = get_option<std::string>( "MORALE_STYLE" ) == "horizontal";
if( clear_cache ) {
mood_face_cache.reset();
}
if( mood_face_cache.has_value() && mood_face_horizontal == option_horizontal ) {
return mood_face_cache.value();
}
mood_face_horizontal = option_horizontal;
std::string face_type;
for( const trait_id &mut : get_mutations() ) {
if( !mut->threshold ) {
continue;
}
face_type = mut.str();
break;
}
// Valid: DEFAULT and DEFAULT_HORIZONTAL
if( face_type.empty() ) {
face_type = "DEFAULT";
}
if( mood_face_horizontal ) {
face_type.append( "_HORIZONTAL" );
}
// Valid: THRESH_MUT and THRESH_MUT_HORIZONTAL
// Example: THRESH_BIRD and THRESH_BIRD_HORIZONTAL
for( const mood_face &face_id : mood_face::get_all() ) {
if( face_id.getId().str() == face_type ) {
mood_face_cache = face_id.getId();
break;
}
}
// If we didn't get it first try, we're not getting it again
if( !mood_face_cache.has_value() ) {
if( mood_face_horizontal ) {
mood_face_cache = mood_face_DEFAULT_HORIZONTAL;
} else {
mood_face_cache = mood_face_DEFAULT;
}
}
return mood_face_cache.value();
}