2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "FurnaceRecipe.h"
|
|
|
|
#include "Item.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
void cFurnaceRecipe::ReloadRecipes(void)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
ClearRecipes();
|
2013-09-28 15:36:01 -04:00
|
|
|
LOGD("Loading furnace recipes...");
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
std::ifstream f;
|
|
|
|
char a_File[] = "furnace.txt";
|
|
|
|
f.open(a_File, std::ios::in);
|
|
|
|
std::string input;
|
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
if (!f.good())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
f.close();
|
2013-06-16 16:24:07 -04:00
|
|
|
LOG("Could not open the furnace recipes file \"%s\"", a_File);
|
2012-06-14 09:06:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
// TODO: Replace this messy parse with a high-level-structured one (ReadLine / ProcessLine)
|
2012-06-14 09:06:06 -04:00
|
|
|
bool bSyntaxError = false;
|
2013-06-16 16:24:07 -04:00
|
|
|
while (f.good())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
char c;
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// comments
|
|
|
|
f >> c;
|
|
|
|
f.unget();
|
|
|
|
if( c == '#' )
|
|
|
|
{
|
|
|
|
while( f.good() && c != '\n' )
|
|
|
|
{
|
|
|
|
f.get( c );
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Line breaks
|
|
|
|
f.get( c );
|
|
|
|
while( f.good() && ( c == '\n' || c == '\r' ) ) { f.get( c ); }
|
2013-06-16 16:24:07 -04:00
|
|
|
if (f.eof())
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
f.unget();
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Check for fuel
|
|
|
|
f >> c;
|
|
|
|
if( c == '!' ) // It's fuel :)
|
|
|
|
{
|
|
|
|
// Read item
|
|
|
|
int IItemID = 0, IItemCount = 0, IItemHealth = 0;
|
|
|
|
f >> IItemID;
|
|
|
|
f >> c; if( c != ':' ) { bSyntaxError = true; break; }
|
|
|
|
f >> IItemCount;
|
|
|
|
|
|
|
|
// Optional health
|
|
|
|
f >> c;
|
|
|
|
if( c != ':' )
|
|
|
|
f.unget();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
f >> IItemHealth;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Burn time
|
2013-06-16 16:24:07 -04:00
|
|
|
int BurnTime;
|
2012-06-14 09:06:06 -04:00
|
|
|
f >> c; if( c != '=' ) { bSyntaxError = true; break; }
|
|
|
|
f >> BurnTime;
|
|
|
|
|
|
|
|
// Add to fuel list
|
|
|
|
Fuel F;
|
|
|
|
F.In = new cItem( (ENUM_ITEM_ID) IItemID, (char)IItemCount, (short)IItemHealth );
|
|
|
|
F.BurnTime = BurnTime;
|
|
|
|
m_pState->Fuel.push_back( F );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
f.unget();
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Read items
|
|
|
|
int IItemID = 0, IItemCount = 0, IItemHealth = 0;
|
|
|
|
f >> IItemID;
|
|
|
|
f >> c; if( c != ':' ) { bSyntaxError = true; break; }
|
|
|
|
f >> IItemCount;
|
|
|
|
|
|
|
|
// Optional health
|
|
|
|
f >> c;
|
|
|
|
if( c != ':' )
|
|
|
|
f.unget();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
f >> IItemHealth;
|
|
|
|
}
|
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
int CookTime;
|
2012-06-14 09:06:06 -04:00
|
|
|
f >> c; if( c != '@' ) { bSyntaxError = true; break; }
|
|
|
|
f >> CookTime;
|
|
|
|
|
|
|
|
int OItemID = 0, OItemCount = 0, OItemHealth = 0;
|
|
|
|
f >> c; if( c != '=' ) { bSyntaxError = true; break; }
|
|
|
|
f >> OItemID;
|
|
|
|
f >> c; if( c != ':' ) { bSyntaxError = true; break; }
|
|
|
|
f >> OItemCount;
|
|
|
|
|
|
|
|
// Optional health
|
|
|
|
f >> c;
|
|
|
|
if( c != ':' )
|
|
|
|
f.unget();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
f >> OItemHealth;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 = CookTime;
|
|
|
|
m_pState->Recipes.push_back( R );
|
|
|
|
}
|
2013-06-16 16:24:07 -04:00
|
|
|
if (bSyntaxError)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
LOGERROR("ERROR: FurnaceRecipe, syntax error" );
|
|
|
|
}
|
2014-03-12 13:34:50 -04:00
|
|
|
LOG("Loaded " SIZE_T_FMT " furnace recipes and " SIZE_T_FMT " fuels", m_pState->Recipes.size(), m_pState->Fuel.size());
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
void cFurnaceRecipe::ClearRecipes(void)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-06-16 16:24:07 -04:00
|
|
|
for (RecipeList::iterator itr = m_pState->Recipes.begin(); itr != m_pState->Recipes.end(); ++itr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
Recipe R = *itr;
|
|
|
|
delete R.In;
|
|
|
|
delete R.Out;
|
|
|
|
}
|
|
|
|
m_pState->Recipes.clear();
|
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
for (FuelList::iterator itr = m_pState->Fuel.begin(); itr != m_pState->Fuel.end(); ++itr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
Fuel F = *itr;
|
|
|
|
delete F.In;
|
|
|
|
}
|
|
|
|
m_pState->Fuel.clear();
|
|
|
|
}
|
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const cFurnaceRecipe::Recipe * cFurnaceRecipe::GetRecipeFrom(const cItem & a_Ingredient) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-06-16 16:24:07 -04:00
|
|
|
const Recipe * BestRecipe = 0;
|
|
|
|
for (RecipeList::const_iterator itr = m_pState->Recipes.begin(); itr != m_pState->Recipes.end(); ++itr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
const Recipe & R = *itr;
|
2013-06-16 16:24:07 -04:00
|
|
|
if ((R.In->m_ItemType == a_Ingredient.m_ItemType) && (R.In->m_ItemCount <= a_Ingredient.m_ItemCount))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-06-16 16:24:07 -04:00
|
|
|
if (BestRecipe && (BestRecipe->In->m_ItemCount > R.In->m_ItemCount))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BestRecipe = &R;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return BestRecipe;
|
|
|
|
}
|
|
|
|
|
2013-06-16 16:24:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cFurnaceRecipe::GetBurnTime(const cItem & a_Fuel) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-06-16 16:24:07 -04:00
|
|
|
int BestFuel = 0;
|
|
|
|
for (FuelList::const_iterator itr = m_pState->Fuel.begin(); itr != m_pState->Fuel.end(); ++itr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
const Fuel & F = *itr;
|
2013-06-16 16:24:07 -04:00
|
|
|
if ((F.In->m_ItemType == a_Fuel.m_ItemType) && (F.In->m_ItemCount <= a_Fuel.m_ItemCount))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-06-16 16:24:07 -04:00
|
|
|
if (BestFuel > 0 && (BestFuel > F.BurnTime))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BestFuel = F.BurnTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return BestFuel;
|
2013-06-16 16:24:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|