forked from DFHack/stonesense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaterialMatcher.h
67 lines (58 loc) · 1.68 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
#pragma once
#include "RemoteFortressReader.pb.h"
#include "Types.h"
#include <map>
#include <string>
template<class T>
class MaterialMatcher
{
private:
struct MaterialMatch
{
T item;
int difference;
};
std::map<DFHack::t_matglossPair, MaterialMatch> matList;
public:
T* get(DFHack::t_matglossPair);
int set(T input, std::string token, google::protobuf::RepeatedPtrField< ::RemoteFortressReader::MaterialDefinition >* matTokenList);
//Wipes the slate clean
void clear();
};
int FuzzyCompare(std::string source, std::string target);
//--------------------------------------------------------
template<class T>
T* MaterialMatcher<T>::get(DFHack::t_matglossPair matPair)
{
if (matList.count(matPair))
return &matList[matPair].item;
else return NULL;
}
template<class T>
int MaterialMatcher<T>::set(T input, std::string token, google::protobuf::RepeatedPtrField< ::RemoteFortressReader::MaterialDefinition >* matTokenList)
{
int count = 0;
for (int i = 0; i < matTokenList->size(); i++)
{
int match = FuzzyCompare(token, matTokenList->Get(i).id());
if (match < 0)
continue;
DFHack::t_matglossPair pair;
pair.index = matTokenList->Get(i).mat_pair().mat_index();
pair.type = matTokenList->Get(i).mat_pair().mat_type();
if (matList.count(pair))
{
if (matList[pair].difference <= match) //dont't overwrite old ones that are equal.
continue;
}
matList[pair].item = input;
matList[pair].difference = match;
count++;
}
return 0;
}
template<class T>
void MaterialMatcher<T>::clear()
{
matList.clear();
}