summaryrefslogtreecommitdiffstats
path: root/source/cFurnaceRecipe.cpp
diff options
context:
space:
mode:
authorcedeel@gmail.com <cedeel@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6>2012-06-14 15:06:06 +0200
committercedeel@gmail.com <cedeel@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6>2012-06-14 15:06:06 +0200
commit92c59963f82f81aa3202657e7fdbb2592924ede3 (patch)
treeb7eb2474528a4998fa102e3ec9119b908cee08b4 /source/cFurnaceRecipe.cpp
parentAdded HOOK_WEATHER_CHANGE. (diff)
downloadcuberite-92c59963f82f81aa3202657e7fdbb2592924ede3.tar
cuberite-92c59963f82f81aa3202657e7fdbb2592924ede3.tar.gz
cuberite-92c59963f82f81aa3202657e7fdbb2592924ede3.tar.bz2
cuberite-92c59963f82f81aa3202657e7fdbb2592924ede3.tar.lz
cuberite-92c59963f82f81aa3202657e7fdbb2592924ede3.tar.xz
cuberite-92c59963f82f81aa3202657e7fdbb2592924ede3.tar.zst
cuberite-92c59963f82f81aa3202657e7fdbb2592924ede3.zip
Diffstat (limited to 'source/cFurnaceRecipe.cpp')
-rw-r--r--source/cFurnaceRecipe.cpp480
1 files changed, 240 insertions, 240 deletions
diff --git a/source/cFurnaceRecipe.cpp b/source/cFurnaceRecipe.cpp
index b17ec51cd..3f6416d92 100644
--- a/source/cFurnaceRecipe.cpp
+++ b/source/cFurnaceRecipe.cpp
@@ -1,241 +1,241 @@
-
-#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
-
-#include "cFurnaceRecipe.h"
-#include "cItem.h"
-
-#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;
-}
-
-
-
-
-
-void cFurnaceRecipe::ReloadRecipes()
-{
- ClearRecipes();
- LOG("-- Loading furnace recipes --");
-
- std::ifstream f;
- char a_File[] = "furnace.txt";
- f.open(a_File, std::ios::in);
- std::string input;
-
- if( !f.good() )
- {
- f.close();
- LOG("Could not open file for recipes: %s", a_File);
- return;
- }
-
- bool bSyntaxError = false;
- while( f.good() )
- {
- 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 ); }
- if( f.eof() ) break;
- 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
- float BurnTime;
- 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;
- }
-
- float CookTime;
- 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 );
- }
- if( bSyntaxError )
- {
- LOGERROR("ERROR: FurnaceRecipe, syntax error" );
- }
- LOG("Got %i furnace recipes, and %i fuels.", m_pState->Recipes.size(), m_pState->Fuel.size() );
-
- LOG("-- Done loading furnace recipes --");
-}
-
-
-
-
-
-void cFurnaceRecipe::ClearRecipes()
-{
- for( RecipeList::iterator itr = m_pState->Recipes.begin(); itr != m_pState->Recipes.end(); ++itr )
- {
- Recipe R = *itr;
- delete R.In;
- delete R.Out;
- }
- m_pState->Recipes.clear();
-
- for( FuelList::iterator itr = m_pState->Fuel.begin(); itr != m_pState->Fuel.end(); ++itr )
- {
- Fuel F = *itr;
- delete F.In;
- }
- 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_ItemID == a_Ingredient.m_ItemID) && (R.In->m_ItemCount <= a_Ingredient.m_ItemCount ) )
- {
- if( BestRecipe && (BestRecipe->In->m_ItemCount > R.In->m_ItemCount) )
- {
- continue;
- }
- else
- {
- BestRecipe = &R;
- }
- }
- }
- return BestRecipe;
-}
-
-float cFurnaceRecipe::GetBurnTime( const cItem & a_Fuel ) const
-{
- float BestFuel = 0.f;
- for( FuelList::const_iterator itr = m_pState->Fuel.begin(); itr != m_pState->Fuel.end(); ++itr )
- {
- const Fuel & F = *itr;
- if( (F.In->m_ItemID == a_Fuel.m_ItemID) && (F.In->m_ItemCount <= a_Fuel.m_ItemCount ) )
- {
- if( BestFuel > 0.f && (BestFuel > F.BurnTime ) )
- {
- continue;
- }
- else
- {
- BestFuel = F.BurnTime;
- }
- }
- }
- return BestFuel;
+
+#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
+
+#include "cFurnaceRecipe.h"
+#include "cItem.h"
+
+#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;
+}
+
+
+
+
+
+void cFurnaceRecipe::ReloadRecipes()
+{
+ ClearRecipes();
+ LOG("-- Loading furnace recipes --");
+
+ std::ifstream f;
+ char a_File[] = "furnace.txt";
+ f.open(a_File, std::ios::in);
+ std::string input;
+
+ if( !f.good() )
+ {
+ f.close();
+ LOG("Could not open file for recipes: %s", a_File);
+ return;
+ }
+
+ bool bSyntaxError = false;
+ while( f.good() )
+ {
+ 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 ); }
+ if( f.eof() ) break;
+ 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
+ float BurnTime;
+ 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;
+ }
+
+ float CookTime;
+ 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 );
+ }
+ if( bSyntaxError )
+ {
+ LOGERROR("ERROR: FurnaceRecipe, syntax error" );
+ }
+ LOG("Got %i furnace recipes, and %i fuels.", m_pState->Recipes.size(), m_pState->Fuel.size() );
+
+ LOG("-- Done loading furnace recipes --");
+}
+
+
+
+
+
+void cFurnaceRecipe::ClearRecipes()
+{
+ for( RecipeList::iterator itr = m_pState->Recipes.begin(); itr != m_pState->Recipes.end(); ++itr )
+ {
+ Recipe R = *itr;
+ delete R.In;
+ delete R.Out;
+ }
+ m_pState->Recipes.clear();
+
+ for( FuelList::iterator itr = m_pState->Fuel.begin(); itr != m_pState->Fuel.end(); ++itr )
+ {
+ Fuel F = *itr;
+ delete F.In;
+ }
+ 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_ItemID == a_Ingredient.m_ItemID) && (R.In->m_ItemCount <= a_Ingredient.m_ItemCount ) )
+ {
+ if( BestRecipe && (BestRecipe->In->m_ItemCount > R.In->m_ItemCount) )
+ {
+ continue;
+ }
+ else
+ {
+ BestRecipe = &R;
+ }
+ }
+ }
+ return BestRecipe;
+}
+
+float cFurnaceRecipe::GetBurnTime( const cItem & a_Fuel ) const
+{
+ float BestFuel = 0.f;
+ for( FuelList::const_iterator itr = m_pState->Fuel.begin(); itr != m_pState->Fuel.end(); ++itr )
+ {
+ const Fuel & F = *itr;
+ if( (F.In->m_ItemID == a_Fuel.m_ItemID) && (F.In->m_ItemCount <= a_Fuel.m_ItemCount ) )
+ {
+ if( BestFuel > 0.f && (BestFuel > F.BurnTime ) )
+ {
+ continue;
+ }
+ else
+ {
+ BestFuel = F.BurnTime;
+ }
+ }
+ }
+ return BestFuel;
} \ No newline at end of file