summaryrefslogtreecommitdiffstats
path: root/src/FurnaceRecipe.cpp
blob: f8949d45f7e84ed8753b287067b3df5ba676e744 (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
338

#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 "furnace.txt"





typedef std::list< cFurnaceRecipe::Recipe > RecipeList;
typedef std::list< cFurnaceRecipe::Fuel > FuelList;





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





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





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





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\"", FURNACE_RECIPE_FILE);
		return;
	}
	
	unsigned int Line = 0;
	AString ParsingLine;

	while (std::getline(f, ParsingLine))
	{
		Line++;
		TrimString(ParsingLine);
		if (ParsingLine.empty())
		{
			continue;
		}

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

			case '!':
			{
				// Fuel
				int IItemID = 0, IItemCount = 0, IItemHealth = 0, IBurnTime = 0;
				AString::size_type BeginPos = 1; // Begin at one after exclamation mark (bang)

				if (
					!ReadMandatoryNumber(BeginPos, ":", ParsingLine, Line, IItemID) ||                       // Read item ID
					!ReadOptionalNumbers(BeginPos, ":", "=", ParsingLine, Line, IItemCount, IItemHealth) ||  // Read item count (and optionally health)
					!ReadMandatoryNumber(BeginPos, "0123456789", ParsingLine, Line, IBurnTime, true)         // Read item burn time - last value
				)
				{
					return;
				}

				// Add to fuel list:
				Fuel F;
				F.In = new cItem((ENUM_ITEM_ID)IItemID, (char)IItemCount, (short)IItemHealth);
				F.BurnTime = IBurnTime;
				m_pState->Fuel.push_back(F);
				break;
			}

			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
			{
				// Recipe
				int IItemID = 0, IItemCount = 0, IItemHealth = 0, IBurnTime = 0;
				int OItemID = 0, OItemCount = 0, OItemHealth = 0;
				AString::size_type BeginPos = 0; // Begin at start of line

				if (
					!ReadMandatoryNumber(BeginPos, ":", ParsingLine, Line, IItemID) ||                                  // Read item ID
					!ReadOptionalNumbers(BeginPos, ":", "@", ParsingLine, Line, IItemCount, IItemHealth) ||             // Read item count (and optionally health)
					!ReadMandatoryNumber(BeginPos, "=", ParsingLine, Line, IBurnTime) ||                                // Read item burn time
					!ReadMandatoryNumber(BeginPos, ":", ParsingLine, Line, OItemID) ||                                  // Read result ID
					!ReadOptionalNumbers(BeginPos, ":", "012456789", ParsingLine, Line, OItemCount, OItemHealth, true)  // Read result count (and optionally health) - last value
				)
				{
					return;
				}

				// Add to recipe list
				Recipe R;
				R.In = new cItem((ENUM_ITEM_ID)IItemID, (char)IItemCount, (short)IItemHealth);
				R.Out = new cItem((ENUM_ITEM_ID)OItemID, (char)OItemCount, (short)OItemHealth);
				R.CookTime = IBurnTime;
				m_pState->Recipes.push_back(R);
				break;
			}

			default:
			{
				LOGWARNING("Error in furnace recipes at line %d: Unexpected text: \"%s\". Ignoring line.",
					Line, ParsingLine.c_str()
				);
				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::PrintParseError(unsigned int a_Line, size_t a_Position, const AString & a_CharactersMissing)
{
	LOGWARN("Error parsing furnace recipes at line %i pos " SIZE_T_FMT ": missing '%s'", a_Line, a_Position, a_CharactersMissing.c_str());
}





bool cFurnaceRecipe::ReadMandatoryNumber(AString::size_type & a_Begin, const AString & a_Delimiter, const AString & a_Text, unsigned int a_Line, int & a_Value, bool a_IsLastValue)
{
	// TODO: replace atoi with std::stoi
	AString::size_type End;
	if (a_IsLastValue)
	{
		End = a_Text.find_first_not_of(a_Delimiter, a_Begin);
	}
	else
	{
		End = a_Text.find_first_of(a_Delimiter, a_Begin);
		if (End == AString::npos)
		{
			PrintParseError(a_Line, a_Begin, a_Delimiter);
			return false;
		}
	}
	
	// stoi won't throw an exception if the string is alphanumeric, we should check for this
	if (!DoesStringContainOnlyNumbers(a_Text.substr(a_Begin, End - a_Begin)))
	{
		PrintParseError(a_Line, a_Begin, "number");
		return false;
	}
	a_Value = atoi(a_Text.substr(a_Begin, End - a_Begin).c_str());

	a_Begin = End + 1; // Jump over delimiter
	return true;
}





bool cFurnaceRecipe::ReadOptionalNumbers(AString::size_type & a_Begin, const AString & a_DelimiterOne, const AString & a_DelimiterTwo, const AString & a_Text, unsigned int a_Line, int & a_ValueOne, int & a_ValueTwo, bool a_IsLastValue)
{
	// TODO: replace atoi with std::stoi
	AString::size_type End, Begin = a_Begin;

	End = a_Text.find_first_of(a_DelimiterOne, Begin);
	if (End != AString::npos)
	{
		if (DoesStringContainOnlyNumbers(a_Text.substr(Begin, End - Begin)))
		{
			a_ValueOne = std::atoi(a_Text.substr(Begin, End - Begin).c_str());
			Begin = End + 1;

			if (a_IsLastValue)
			{
				End = a_Text.find_first_not_of(a_DelimiterTwo, Begin);
			}
			else
			{
				End = a_Text.find_first_of(a_DelimiterTwo, Begin);
				if (End == AString::npos)
				{
					PrintParseError(a_Line, Begin, a_DelimiterTwo);
					return false;
				}
			}

			// stoi won't throw an exception if the string is alphanumeric, we should check for this
			if (!DoesStringContainOnlyNumbers(a_Text.substr(Begin, End - Begin)))
			{
				PrintParseError(a_Line, Begin, "number");
				return false;
			}
			a_ValueTwo = atoi(a_Text.substr(Begin, End - Begin).c_str());

			a_Begin = End + 1; // Jump over delimiter
			return true;
		}
		else
		{
			return ReadMandatoryNumber(a_Begin, a_DelimiterTwo, a_Text, a_Line, a_ValueOne, a_IsLastValue);
		}
	}
	
	return ReadMandatoryNumber(a_Begin, a_DelimiterTwo, a_Text, a_Line, a_ValueOne, a_IsLastValue);
}





bool cFurnaceRecipe::DoesStringContainOnlyNumbers(const AString & a_String)
{
	// TODO: replace this with std::all_of(a_String.begin(), a_String.end(), isdigit)
	return (a_String.find_first_not_of("0123456789") == AString::npos);
}





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

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





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





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 Fuel & F = *itr;
		if ((F.In->m_ItemType == a_Fuel.m_ItemType) && (F.In->m_ItemCount <= a_Fuel.m_ItemCount))
		{
			if (BestFuel > 0 && (BestFuel > F.BurnTime))
			{
				continue;
			}
			else
			{
				BestFuel = F.BurnTime;
			}
		}
	}
	return BestFuel;
}