forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_snippets.h
132 lines (122 loc) · 5.72 KB
/
text_snippets.h
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
#pragma once
#ifndef CATA_SRC_TEXT_SNIPPETS_H
#define CATA_SRC_TEXT_SNIPPETS_H
#include <iosfwd>
#include <map>
#include <string>
#include <unordered_map>
#include <vector>
#include "optional.h"
#include "translations.h"
#include "dialogue.h"
#include "type_id.h"
class JsonArray;
class JsonObject;
class snippet_library
{
public:
/**
* Load snippet from the standalone entry, used by the @ref DynamicDataLoader.
*/
void load_snippet( const JsonObject &jsobj );
/**
* Load all snippet definitions in the json array into given category.
* Entries in the array can be simple strings, or json objects (for the
* later see add_snippet_from_json).
*/
void add_snippets_from_json( const std::string &category, const JsonArray &jarr );
/**
* Load a single snippet text from the json object. The object should have
* a "text" member with the text of the snippet.
* A "id" member is optional and if present gives the snippet text a id,
* stored in snippets_by_id.
*/
void add_snippet_from_json( const std::string &category, const JsonObject &jo );
void clear_snippets();
bool has_category( const std::string &category ) const;
/**
* Returns the snippet referenced by the id, or cata::nullopt if there
* is no snippet with such id.
* The tags in the snippet are not automatically expanded - you need to
* call `expand()` to do that.
*/
cata::optional<translation> get_snippet_by_id( const snippet_id &id ) const;
/**
* Returns the EOC connected with the snippet referenced by the id, or cata::nullopt if there
* is no snippet with such id.
*/
cata::optional<talk_effect_t<dialogue>> get_EOC_by_id( const snippet_id &id ) const;
/**
* Returns the name connected with the snippet referenced by the id, or cata::nullopt if there
* is no snippet with such id.
*/
cata::optional<translation> get_name_by_id( const snippet_id &id ) const;
/**
* Returns a reference to the snippet with the id, or a reference to an
* empty translation object if no such snippet exist.
*/
const translation &get_snippet_ref_by_id( const snippet_id &id ) const;
/**
* Returns true if there exists a snippet with the id
*/
bool has_snippet_with_id( const snippet_id &id ) const;
/**
* Expand the string by recursively replacing tags in angle brackets (<>)
* with random snippets from the tags' respective category. For example,
* if the string is "<okay><punc>", the function then replaces "<okay>"
* with a snippet from the "<okay>" category, and "<punc>" with a snippet
* from the "<punc>" category, and returns, for example, "okay!". If a tag
* does not have a corresponding category, it is copied to the final string,
* for example, "drop <yrwp>, <okay>?" may expand to "drop <yrwp>, okay?"
* because "<yrwp>" is a special tag that does not have a category defined
* in the JSON files.
*/
std::string expand( const std::string &str ) const;
/**
* Returns the id of a random snippet out of the given category.
* Snippets without an id will NOT be returned by this function.
* If there isn't any snippet in the category, or if none of the snippets
* in the category has an id, snippet_id::NULL_ID() is returned.
*/
snippet_id random_id_from_category( const std::string &cat ) const;
/**
* Returns a random snippet out of the given category. Both snippets with
* or without an id may be returned.
* If there isn't any snippet in the category, cata::nullopt is returned.
* The tags in the snippet are not automatically expanded - you need to
* call `expand()` to do that.
*/
cata::optional<translation> random_from_category( const std::string &cat ) const;
/**
* Use the given seed to select a random snippet from the category.
* For the same seed, the returned snippet stays the same *in a single
* game session*, but may change after reloading or restarting a game,
* due to layout changes in the JSON files, for example. If there isn't
* any snippet in the category, cata::nullopt is returned.
* The tags in the snippet are not automatically expanded - you need to
* call `expand()` to do that.
*
* TODO: make the result stay the same through different game sessions
*/
cata::optional<translation> random_from_category( const std::string &cat, unsigned int seed ) const;
/**
* Used only for legacy compatibility. `hash_to_id_migration` will be
* initialized if it hasn't been yet, and the snippet with the given
* hash is looked up and returned. If there's no longer any snippet with
* the given hash, snippet_id::NULL_ID() is returned instead.
*/
snippet_id migrate_hash_to_id( int hash );
private:
std::unordered_map<snippet_id, translation> snippets_by_id;
// front facing name
std::unordered_map<snippet_id, translation> name_by_id;
std::unordered_map<snippet_id, talk_effect_t<dialogue>> EOC_by_id;
struct category_snippets {
std::vector<snippet_id> ids;
std::vector<translation> no_id;
};
std::unordered_map<std::string, category_snippets> snippets_by_category;
cata::optional<std::map<int, snippet_id>> hash_to_id_migration;
};
extern snippet_library SNIPPET;
#endif // CATA_SRC_TEXT_SNIPPETS_H