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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#include "BrewingRecipes.h"
#include <fstream>
#define BREWING_RECIPE_FILE "brewing.txt"
cBrewingRecipes::cBrewingRecipes()
{
ReloadRecipes();
}
void cBrewingRecipes::ReloadRecipes(void)
{
ClearRecipes();
LOGD("Loading brewing recipes...");
std::ifstream f(BREWING_RECIPE_FILE, std::ios::in);
if (!f.good())
{
LOG("Could not open the brewing recipes file \"%s\". No brewing recipes are available.", BREWING_RECIPE_FILE);
return;
}
unsigned int LineNum = 0;
AString ParsingLine;
while (std::getline(f, ParsingLine))
{
LineNum++;
// Remove comments from the line:
size_t FirstCommentSymbol = ParsingLine.find('#');
if (FirstCommentSymbol != AString::npos)
{
ParsingLine.erase(FirstCommentSymbol);
}
if (IsOnlyWhitespace(ParsingLine))
{
// Ignore empty and whitespace only lines
continue;
}
AddRecipeFromLine(ParsingLine, LineNum);
} // while (getline(ParsingLine))
LOG("Loaded %zu brewing recipes", m_Recipes.size());
}
void cBrewingRecipes::AddRecipeFromLine(AString a_Line, unsigned int a_LineNum)
{
a_Line.erase(std::remove_if(a_Line.begin(), a_Line.end(), isspace), a_Line.end());
auto Recipe = cpp14::make_unique<cRecipe>();
const AStringVector & InputAndIngredient = StringSplit(a_Line, "+");
if (InputAndIngredient.size() != 2)
{
LOGWARNING("brewing.txt: line %d: A line with '+' was expected", a_LineNum);
LOGINFO("Offending line: \"%s\"", a_Line.c_str());
return;
}
const AStringVector & IngredientAndOutput = StringSplit(InputAndIngredient[1].c_str(), "=");
if (IngredientAndOutput.size() != 2)
{
LOGWARNING("brewing.txt: line %d: A line with '=' was expected", a_LineNum);
LOGINFO("Offending line: \"%s\"", a_Line.c_str());
return;
}
if (!ParseItem(IngredientAndOutput[0], Recipe->Ingredient))
{
LOGWARNING("brewing.txt: Parsing of the item didn't worked.");
LOGINFO("Offending line: \"%s\"", a_Line.c_str());
return;
}
if (!StringToInteger<short>(InputAndIngredient[0], Recipe->Input.m_ItemDamage))
{
LOGWARNING("brewing.txt: line %d: Cannot parse the damage value for the input item\"%s\".", a_LineNum, InputAndIngredient[0].c_str());
LOGINFO("Offending line: \"%s\"", a_Line.c_str());
return;
}
if (!StringToInteger<short>(IngredientAndOutput[1], Recipe->Output.m_ItemDamage))
{
LOGWARNING("brewing.txt: line %d: Cannot parse the damage value for the output item\"%s\".", a_LineNum, IngredientAndOutput[1].c_str());
LOGINFO("Offending line: \"%s\"", a_Line.c_str());
return;
}
m_Recipes.push_back(std::move(Recipe));
}
bool cBrewingRecipes::ParseItem(const AString & a_String, cItem & a_Item)
{
return StringToItem(a_String, a_Item);
}
void cBrewingRecipes::ClearRecipes(void)
{
m_Recipes.clear();
}
const cBrewingRecipes::cRecipe * cBrewingRecipes::GetRecipeFrom(const cItem & a_Input, const cItem & a_Ingredient) const
{
for (const auto & Recipe : m_Recipes)
{
if ((Recipe->Input.IsEqual(a_Input)) && (Recipe->Ingredient.IsEqual(a_Ingredient)))
{
return Recipe.get();
}
}
// Check for gunpowder
if (a_Ingredient.m_ItemType == E_ITEM_GUNPOWDER)
{
if (a_Input.m_ItemDamage & 0x2000)
{
// Create new recipe and add it to list
auto Recipe = cpp14::make_unique<cRecipe>();
Recipe->Input.m_ItemType = a_Input.m_ItemDamage;
Recipe->Output.m_ItemDamage = a_Input.m_ItemDamage + 8192;
Recipe->Ingredient.m_ItemType = E_ITEM_GUNPOWDER;
auto RecipePtr = Recipe.get();
m_Recipes.push_back(std::move(Recipe));
return RecipePtr;
}
return nullptr;
}
// Check for splash potion
if (a_Input.m_ItemDamage & 0x4000)
{
// Search for the drinkable potion, the ingredients are the same
short SplashItemDamage = a_Input.m_ItemDamage - 8192;
auto FoundRecipe = std::find_if(m_Recipes.cbegin(), m_Recipes.cend(), [&](const std::unique_ptr<cRecipe>& a_Recipe)
{
return (
(a_Recipe->Input.m_ItemDamage == SplashItemDamage) &&
(a_Recipe->Ingredient.IsEqual(a_Ingredient))
);
});
if (FoundRecipe == m_Recipes.cend())
{
return nullptr;
}
// Create new recipe and add it to list
auto Recipe = cpp14::make_unique<cRecipe>();
Recipe->Input.m_ItemDamage = a_Input.m_ItemDamage;
Recipe->Output.m_ItemDamage = (*FoundRecipe)->Output.m_ItemDamage + 8192;
Recipe->Ingredient.m_ItemType = (*FoundRecipe)->Ingredient.m_ItemType;
auto RecipePtr = Recipe.get();
m_Recipes.push_back(std::move(Recipe));
return RecipePtr;
}
return nullptr;
}
bool cBrewingRecipes::IsIngredient(const cItem & a_Ingredient) const
{
// Check for gunpowder
if (a_Ingredient.m_ItemType == E_ITEM_GUNPOWDER)
{
return true;
}
for (const auto & Recipe : m_Recipes)
{
if (Recipe->Ingredient.IsEqual(a_Ingredient))
{
return true;
}
}
return false;
}
bool cBrewingRecipes::IsBottle(const cItem & a_Item) const
{
return (a_Item.m_ItemType == E_ITEM_POTION);
}
bool cBrewingRecipes::IsFuel(const cItem & a_Item) const
{
return (a_Item.m_ItemType == E_ITEM_BLAZE_POWDER);
}
|