forked from DFHack/stonesense
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMaterialMatcher.h
194 lines (170 loc) · 5.58 KB
/
MaterialMatcher.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
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
#pragma once
#include "Types.h"
#include <map>
#include <string>
#include "modules/Materials.h"
#include "df/builtin_mats.h"
#include "df/creature_raw.h"
#include "df/plant_growth.h"
#include "df/plant_raw.h"
#include "df/world.h"
template<class T>
class MaterialMatcher
{
private:
struct MaterialMatch
{
T item;
int difference;
};
std::map<DFHack::t_matglossPair, MaterialMatch> matList;
void check_match(T input, std::string token, std::string id, int16_t type, int32_t index);
public:
T* get(DFHack::t_matglossPair);
void set_material(T input, std::string token);
void set_growth(T input, std::string token);
//Wipes the slate clean
void clear();
};
int FuzzyCompare(std::string source, std::string target);
//--------------------------------------------------------
template<class T>
void MaterialMatcher<T>::check_match(T input, std::string token, std::string id, int16_t type, int32_t index)
{
DFHack::t_matglossPair pair;
pair.type = type;
pair.index = index;
int match = FuzzyCompare(token, id);
if (match >= 0 && (!matList.count(pair) || matList.at(pair).difference > match))
{
matList[pair].item = input;
matList[pair].difference = match;
}
}
template<class T>
T* MaterialMatcher<T>::get(DFHack::t_matglossPair matPair)
{
if (matList.count(matPair))
return &matList[matPair].item;
else return NULL;
}
// Simple helper function to check if there are any wildcards present
// If not, we can save ourselves a lot of trouble
inline bool contains_wildcard(const std::string &str)
{
return (str.find_first_of("*?") != std::string::npos);
}
template<class T>
void MaterialMatcher<T>::set_material(T input, std::string token)
{
using DFHack::MaterialInfo;
using df::global::world;
MaterialInfo mat;
std::vector<std::string> tokens;
split_string(&tokens, token, ":");
bool match_id = (tokens.size() >= 2) && !contains_wildcard(tokens[1]);
// If there are no wildcards anywhere, then just do a direct lookup
if (!contains_wildcard(token))
{
if (mat.find(token))
check_match(input, token, mat.getToken(), mat.type, mat.index);
return;
}
// Only scan inorganics if the token looks like it's actually pointing at one
if ((tokens[0] == "INORGANIC") || contains_wildcard(tokens[0]))
{
for (size_t i = 0; i < world->raws.inorganics.size(); i++)
{
mat.decode(0, i);
check_match(input, token, mat.getToken(), 0, i);
}
}
FOR_ENUM_ITEMS(builtin_mats, i)
{
int k = -1;
if (i == df::builtin_mats::COAL)
k = 1;
for (int j = -1; j <= k; j++)
{
mat.decode(i, j);
check_match(input, token, mat.getToken(), i, j);
}
}
// Same for creatures - no point in evaluating something that won't possibly match
if ((tokens[0] == "CREATURE") || contains_wildcard(tokens[0]))
{
for (size_t i = 0; i < world->raws.creatures.all.size(); i++)
{
auto creature = world->raws.creatures.all.at(i);
// Furthermore, don't bother looking at creature materials if the creature itself is wrong
if (match_id && (creature->creature_id != tokens[1]))
continue;
for (size_t j = 0; j < creature->material.size(); j++)
{
mat.decode(j + MaterialInfo::CREATURE_BASE, i);
check_match(input, token, mat.getToken(), j + MaterialInfo::CREATURE_BASE, i);
}
}
}
// And the same goes for plants
if ((tokens[0] == "PLANT") || contains_wildcard(tokens[0]))
{
for (size_t i = 0; i < world->raws.plants.all.size(); i++)
{
auto plant = world->raws.plants.all.at(i);
// And skip plants that obviously don't match
if (match_id && (plant->id != tokens[1]))
continue;
for (size_t j = 0; j < plant->material.size(); j++)
{
mat.decode(j + MaterialInfo::PLANT_BASE, i);
check_match(input, token, mat.getToken(), j + MaterialInfo::PLANT_BASE, i);
}
}
}
}
template<class T>
void MaterialMatcher<T>::set_growth(T input, std::string token)
{
using df::global::world;
static const char* growth_locations[] = {
"TWIGS",
"LIGHT_BRANCHES",
"HEAVY_BRANCHES",
"TRUNK",
"ROOTS",
"CAP",
"SAPLING",
"SHRUB"
};
std::vector<std::string> tokens;
split_string(&tokens, token, ":");
bool match_plant = (tokens.size() >= 1) && (!contains_wildcard(tokens[0]));
bool match_growth = (tokens.size() >= 2) && (!contains_wildcard(tokens[1]));
for (size_t i = 0; i < world->raws.plants.all.size(); i++)
{
auto pp = world->raws.plants.all.at(i);
if (!pp)
continue;
if (match_plant && (pp->id != tokens[0]))
continue;
check_match(input, token, pp->id + ":BASE", -1, int32_t(i));
for (size_t g = 0; g < pp->growths.size(); g++)
{
auto growth = pp->growths[g];
if (!growth)
continue;
if (match_growth && (growth->id != tokens[1]))
continue;
for (int l = 0; l < int(sizeof(growth_locations) / sizeof(growth_locations[0])); l++)
{
check_match(input, token, pp->id + ":" + growth->id + ":" + growth_locations[l], g * 10 + l, int32_t(i));
}
}
}
}
template<class T>
void MaterialMatcher<T>::clear()
{
matList.clear();
}