forked from elonafoobar/elonafoobar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautopick.cpp
149 lines (119 loc) · 3.27 KB
/
autopick.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
#include "autopick.hpp"
#include "elona.hpp"
#include "util.hpp"
#include "variables.hpp"
namespace elona
{
autopick& autopick::instance()
{
static autopick the_instance;
return the_instance;
}
void autopick::load(const std::string& player_id)
{
clear();
for (const auto directory : {filesystem::dir::save(player_id),
filesystem::dir::save(),
filesystem::dir::exe()})
{
for (const auto filename :
{u8"autopick", u8"autopick.txt", u8"autopick.txt.txt"})
{
const auto filepath = directory / filename;
bool file_exists = try_load(filepath);
if (file_exists)
break;
}
}
}
void autopick::clear()
{
matchers.clear();
}
bool autopick::try_load(const fs::path& filepath)
{
if (!fs::exists(filepath))
return false;
for (const auto& line : fileutil::read_by_line{filepath})
{
if (line.empty())
continue;
if (strutil::starts_with(line, u8"#"))
continue;
std::string line_ = line;
operation op{operation::type_t::pick_up};
if (strutil::starts_with(line_, u8"*=")
|| strutil::starts_with(line_, u8"=*"))
{
op.type = operation::type_t::save_and_no_drop;
line_ = line_.substr(2);
}
else
{
switch (line_.front())
{
case '~':
op.type = operation::type_t::do_nothing;
line_ = line_.substr(1);
break;
case '*':
op.type = operation::type_t::save;
line_ = line_.substr(1);
break;
case '=':
op.type = operation::type_t::no_drop;
line_ = line_.substr(1);
break;
case '!':
op.type = operation::type_t::destroy;
line_ = line_.substr(1);
break;
case '+':
op.type = operation::type_t::open;
line_ = line_.substr(1);
break;
default: break;
}
}
// xxx:sound.wav?
if (line_.back() == '?')
{
op.show_prompt = true;
line_ = line_.substr(0, line_.size() - 1);
}
// sound
const auto colon = line_.find(':');
if (colon != std::string::npos)
{
op.sound = line_.substr(colon + 1);
line_ = line_.substr(0, colon);
}
// xxx?:sound.wav
if (line_.back() == '?')
{
op.show_prompt = true;
line_ = line_.substr(0, line_.size() - 1);
}
matchers.emplace_back(std::string{line_}, op);
}
return true;
}
autopick::operation autopick::get_operation(const item& ci)
{
for (const auto& m : matchers)
{
if (m.matches(ci))
{
return m.op;
}
}
return operation{};
}
bool autopick::matcher::matches(const item& ci) const
{
const auto id = ci.id;
const auto name = cnvitemname(id);
return ci.identification_state != identification_state_t::unidentified
&& name.find(text) != std::string::npos;
}
} // namespace elona