-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathauto_pickup.h
169 lines (128 loc) · 3.97 KB
/
auto_pickup.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
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
#pragma once
#ifndef AUTO_PICKUP_H
#define AUTO_PICKUP_H
#include <array>
#include <functional>
#include <iosfwd>
#include <string>
#include <unordered_map>
#include <vector>
#include "enums.h"
class JsonOut;
class JsonIn;
class item;
struct itype;
namespace auto_pickup
{
/**
* The currently-active set of auto-pickup rules, in a form that allows quick
* lookup. When this is filled (by @ref auto_pickup::create_rule()), every
* item existing in the game that matches a rule (either white- or blacklist)
* is added as the key, with RULE_WHITELISTED or RULE_BLACKLISTED as the values.
*/
class cache : public std::unordered_map<std::string, rule_state>
{
public:
/// Defines whether this cache has been filled.
bool ready = false;
/// Temporary data used while filling the cache.
std::unordered_map<std::string, const itype *> temp_items;
};
/**
* A single entry in the list of auto pickup entries @ref rule_list.
* The data contained can be edited by the player and determines what to pick/ignore.
*/
class rule
{
public:
std::string sRule;
bool bActive = false;
bool bExclude = false;
rule() = default;
rule( const std::string &r, const bool a, const bool e ) : sRule( r ), bActive( a ), bExclude( e ) {
}
void serialize( JsonOut &jsout ) const;
void deserialize( JsonIn &jsin );
void test_pattern() const;
};
/**
* A list of rules. This is primarily a container with a few convenient functions (like saving/loading).
*/
class rule_list : public std::vector<rule>
{
public:
void serialize( JsonOut &jsout ) const;
void deserialize( JsonIn &jsin );
void load_legacy_rules( std::istream &fin );
void refresh_map_items( cache &map_items ) const;
void create_rule( cache &map_items, const std::string &to_match );
void create_rule( cache &map_items, const item &it );
};
class user_interface
{
public:
class tab
{
public:
std::string title;
rule_list new_rules;
std::reference_wrapper<rule_list> rules;
tab( const std::string &t, rule_list &r ) : title( t ), new_rules( r ), rules( r ) { }
};
std::string title;
std::vector<tab> tabs;
bool is_autopickup = false;
void show();
bool bStuffChanged = false;
};
class base_settings
{
protected:
mutable cache map_items;
void invalidate();
private:
virtual void refresh_map_items( cache &map_items ) const = 0;
void recreate() const;
public:
virtual ~base_settings() = default;
rule_state check_item( const std::string &sItemName ) const;
};
class player_settings : public base_settings
{
private:
void load( bool bCharacter );
bool save( bool bCharacter );
bool load_legacy( bool bCharacter );
rule_list global_rules;
rule_list character_rules;
void refresh_map_items( cache &map_items ) const override;
public:
~player_settings() override = default;
void create_rule( const item *it );
bool has_rule( const item *it );
void add_rule( const item *it );
void remove_rule( const item *it );
void clear_character_rules();
void show();
bool save_character();
bool save_global();
void load_character();
void load_global();
bool empty() const;
};
class npc_settings : public base_settings
{
private:
rule_list rules;
void refresh_map_items( cache &map_items ) const override;
public:
~npc_settings() override = default;
void create_rule( const std::string &to_match );
void show( const std::string &name );
void serialize( JsonOut &jsout ) const;
void deserialize( JsonIn &jsin );
bool empty() const;
};
} // namespace auto_pickup
auto_pickup::player_settings &get_auto_pickup();
#endif