summaryrefslogtreecommitdiffstats
path: root/src/FurnaceRecipe.cpp
blob: 4e7f13e6a0e73ccba951a7d912dd23bbf2116394 (plain) (blame)
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337

#include "Globals.h"  // NOTE: MSVC stupidness requires this to be the same across all modules

#include "FurnaceRecipe.h"
#include "Item.h"

#include <fstream>

#define FURNACE_RECIPE_FILE FILE_IO_PREFIX "furnace.txt"





typedef std::list<cFurnaceRecipe::cRecipe> RecipeList;
typedef std::list<cFurnaceRecipe::cFuel> FuelList;





struct cFurnaceRecipe::sFurnaceRecipeState
{
	RecipeList Recipes;
	FuelList Fuel;
};





cFurnaceRecipe::cFurnaceRecipe()
	: m_pState(new sFurnaceRecipeState)
{
	ReloadRecipes();
}





cFurnaceRecipe::~cFurnaceRecipe()
{
	ClearRecipes();
	delete m_pState;
	m_pState = nullptr;
}





void cFurnaceRecipe::ReloadRecipes(void)
{
	ClearRecipes();
	LOGD("Loading furnace recipes...");

	std::ifstream f(FURNACE_RECIPE_FILE, std::ios::in);
	if (!f.good())
	{
		LOG("Could not open the furnace recipes file \"%s\". No furnace recipes are available.", FURNACE_RECIPE_FILE);
		return;
	}

	unsigned int LineNum = 0;
	AString ParsingLine;

	while (std::getline(f, ParsingLine))
	{
		LineNum++;
		if (ParsingLine.empty())
		{
			// There is a problem here on Android. Text files transferred from another OS may have a newline representation Android's implementation of getline doesn't expect
			// Thus, part of a newline may be left in ParsingLine. ::empty() thus thinks the string isn't empty, and the below code outputs interesting errors since it was passed a nearly empty string
			// Ref: https://stackoverflow.com/questions/6089231/getting-std-ifstream-to-handle-lf-cr-and-crlf
			// TODO: There is a solution in the above reference, but it isn't very pretty. Fix it somehow.
			continue;
		}

		// Remove comments from the line:
		size_t FirstCommentSymbol = ParsingLine.find('#');
		if ((FirstCommentSymbol != AString::npos) && (FirstCommentSymbol != 0))
		{
			ParsingLine.erase(ParsingLine.begin() + static_cast<const long>(FirstCommentSymbol), ParsingLine.end());
		}

		switch (ParsingLine[0])
		{
			case '#':
			{
				// Comment
				break;
			}

			case '!':
			{
				AddFuelFromLine(ParsingLine, LineNum);
				break;
			}

			default:
			{
				AddRecipeFromLine(ParsingLine, LineNum);
				break;
			}
		}  // switch (ParsingLine[0])
	}  // while (getline(ParsingLine))

	LOG("Loaded " SIZE_T_FMT " furnace recipes and " SIZE_T_FMT " fuels", m_pState->Recipes.size(), m_pState->Fuel.size());
}





void cFurnaceRecipe::AddFuelFromLine(const AString & a_Line, unsigned int a_LineNum)
{
	AString Line(a_Line);
	Line.erase(Line.begin());  // Remove the beginning "!"
	Line.erase(std::remove_if(Line.begin(), Line.end(), isspace), Line.end());

	std::unique_ptr<cItem> Item = cpp14::make_unique<cItem>();
	int BurnTime;

	const AStringVector & Sides = StringSplit(Line, "=");
	if (Sides.size() != 2)
	{
		LOGWARNING("furnace.txt: line %d: A single '=' was expected, got " SIZE_T_FMT, a_LineNum, Sides.size() - 1);
		LOGINFO("Offending line: \"%s\"", a_Line.c_str());
		return;
	}

	if (!ParseItem(Sides[0], *Item))
	{
		LOGWARNING("furnace.txt: line %d: Cannot parse item \"%s\".", a_LineNum, Sides[0].c_str());
		LOGINFO("Offending line: \"%s\"", a_Line.c_str());
		return;
	}

	if (!StringToInteger<int>(Sides[1], BurnTime))
	{
		LOGWARNING("furnace.txt: line %d: Cannot parse burn time.", a_LineNum);
		LOGINFO("Offending line: \"%s\"", a_Line.c_str());
		return;
	}

	// Add to fuel list:
	cFuel Fuel;
	Fuel.In = Item.release();
	Fuel.BurnTime = BurnTime;
	m_pState->Fuel.push_back(Fuel);
}





void cFurnaceRecipe::AddRecipeFromLine(const AString & a_Line, unsigned int a_LineNum)
{
	AString Line(a_Line);
	Line.erase(std::remove_if(Line.begin(), Line.end(), isspace), Line.end());

	int CookTime = 200;
	std::unique_ptr<cItem> InputItem = cpp14::make_unique<cItem>();
	std::unique_ptr<cItem> OutputItem = cpp14::make_unique<cItem>();

	const AStringVector & Sides = StringSplit(Line, "=");
	if (Sides.size() != 2)
	{
		LOGWARNING("furnace.txt: line %d: A single '=' was expected, got " SIZE_T_FMT, a_LineNum, Sides.size() - 1);
		LOGINFO("Offending line: \"%s\"", a_Line.c_str());
		return;
	}

	const AStringVector & InputSplit = StringSplit(Sides[0], "@");
	if (!ParseItem(InputSplit[0], *InputItem))
	{
		LOGWARNING("furnace.txt: line %d: Cannot parse input item \"%s\".", a_LineNum, InputSplit[0].c_str());
		LOGINFO("Offending line: \"%s\"", a_Line.c_str());
		return;
	}

	if (InputSplit.size() > 1)
	{
		if (!StringToInteger<int>(InputSplit[1], CookTime))
		{
			LOGWARNING("furnace.txt: line %d: Cannot parse cook time \"%s\".", a_LineNum, InputSplit[1].c_str());
			LOGINFO("Offending line: \"%s\"", a_Line.c_str());
			return;
		}
	}

	if (!ParseItem(Sides[1], *OutputItem))
	{
		LOGWARNING("furnace.txt: line %d: Cannot parse output item \"%s\".", a_LineNum, Sides[1].c_str());
		LOGINFO("Offending line: \"%s\"", a_Line.c_str());
		return;
	}

	cRecipe Recipe;
	Recipe.In = InputItem.release();
	Recipe.Out = OutputItem.release();
	Recipe.CookTime = CookTime;
	m_pState->Recipes.push_back(Recipe);
}





bool cFurnaceRecipe::ParseItem(const AString & a_String, cItem & a_Item)
{
	AString ItemString = a_String;

	const AStringVector & SplitAmount = StringSplit(ItemString, ",");
	ItemString = SplitAmount[0];

	const AStringVector & SplitMeta = StringSplit(ItemString, ":");
	ItemString = SplitMeta[0];

	if (!StringToItem(ItemString, a_Item))
	{
		return false;
	}

	if (SplitAmount.size() > 1)
	{
		if (!StringToInteger<char>(SplitAmount[1].c_str(), a_Item.m_ItemCount))
		{
			return false;
		}
	}

	if (SplitMeta.size() > 1)
	{
		if (!StringToInteger<short>(SplitMeta[1].c_str(), a_Item.m_ItemDamage))
		{
			return false;
		}
	}
	return true;
}





void cFurnaceRecipe::ClearRecipes(void)
{
	for (RecipeList::iterator itr = m_pState->Recipes.begin(); itr != m_pState->Recipes.end(); ++itr)
	{
		cRecipe Recipe = *itr;
		delete Recipe.In;
		Recipe.In = nullptr;
		delete Recipe.Out;
		Recipe.Out = nullptr;
	}
	m_pState->Recipes.clear();

	for (FuelList::iterator itr = m_pState->Fuel.begin(); itr != m_pState->Fuel.end(); ++itr)
	{
		cFuel Fuel = *itr;
		delete Fuel.In;
		Fuel.In = nullptr;
	}
	m_pState->Fuel.clear();
}





const cFurnaceRecipe::cRecipe * cFurnaceRecipe::GetRecipeFrom(const cItem & a_Ingredient) const
{
	const cRecipe * BestRecipe = nullptr;
	for (RecipeList::const_iterator itr = m_pState->Recipes.begin(); itr != m_pState->Recipes.end(); ++itr)
	{
		const cRecipe & Recipe = *itr;
		if ((Recipe.In->m_ItemType == a_Ingredient.m_ItemType) && (Recipe.In->m_ItemCount <= a_Ingredient.m_ItemCount))
		{
			if (BestRecipe && (BestRecipe->In->m_ItemCount > Recipe.In->m_ItemCount))
			{
				continue;
			}
			else
			{
				BestRecipe = &Recipe;
			}
		}
	}
	return BestRecipe;
}





bool cFurnaceRecipe::IsFuel(const cItem & a_Item) const
{
	for (auto & Fuel : m_pState->Fuel)
	{
		if ((Fuel.In->m_ItemType == a_Item.m_ItemType) && (Fuel.In->m_ItemCount <= a_Item.m_ItemCount))
		{
			return true;
		}
	}
	return false;
}





int cFurnaceRecipe::GetBurnTime(const cItem & a_Fuel) const
{
	int BestFuel = 0;
	for (FuelList::const_iterator itr = m_pState->Fuel.begin(); itr != m_pState->Fuel.end(); ++itr)
	{
		const cFuel & Fuel = *itr;
		if ((Fuel.In->m_ItemType == a_Fuel.m_ItemType) && (Fuel.In->m_ItemCount <= a_Fuel.m_ItemCount))
		{
			if (BestFuel > 0 && (BestFuel > Fuel.BurnTime))
			{
				continue;
			}
			else
			{
				BestFuel = Fuel.BurnTime;
			}
		}
	}
	return BestFuel;
}