From b355bdeccecf727d30e48634df9b5d424db570bc Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Mon, 4 Jun 2012 12:08:20 +0000 Subject: Added the new recipe parser, parsing the crafting.txt file. Included are a few recipes. The old parser still works, but will be replaced soon. git-svn-id: http://mc-server.googlecode.com/svn/trunk@549 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/CraftingRecipes.h | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 source/CraftingRecipes.h (limited to 'source/CraftingRecipes.h') diff --git a/source/CraftingRecipes.h b/source/CraftingRecipes.h new file mode 100644 index 000000000..bf85534b1 --- /dev/null +++ b/source/CraftingRecipes.h @@ -0,0 +1,82 @@ + +// CraftingRecipes.h + +// Interfaces to the cCraftingRecipes class representing the storage of crafting recipes + + + + +#pragma once + +#include "cItem.h" + + + + + +class cCraftingRecipes +{ +public: + static const int MAX_GRID_WIDTH = 3; + static const int MAX_GRID_HEIGHT = 3; + + cCraftingRecipes(void); + ~cCraftingRecipes(); + + /// Offers an item resulting from the crafting grid setup. Doesn't modify the grid + cItem Offer(const cItem * a_CraftingGrid, int a_GridWidth, int a_GridHeight); + + /// Crafts the item resulting from the crafting grid setup. Modifies the grid, returns the crafted item + cItem Craft(cItem * a_CraftingGrid, int a_GridWidth, int a_GridHeight); + +protected: + + struct cRecipeSlot + { + cItem m_Item; + int x, y; // 1..3, or -1 for "any" + } ; + typedef std::vector cRecipeSlots; + + // A single recipe, stored. Each recipe is normalized right after parsing (NormalizeIngredients()) + struct cRecipe + { + cRecipeSlots m_Ingredients; + cItem m_Result; + + // Size of the regular items in the recipe; "anywhere" items are excluded: + int m_Width; + int m_Height; + } ; + typedef std::vector cRecipes; + + cRecipes m_Recipes; + + void LoadRecipes(void); + void ClearRecipes(void); + + /// Parses the recipe line and adds it into m_Recipes. a_LineNum is used for diagnostic warnings only + void AddRecipeLine(int a_LineNum, const AString & a_RecipeLine); + + /// Parses an item string in the format "[^]", returns true if successful. + bool ParseItem(const AString & a_String, cItem & a_Item); + + /// Parses one ingredient and adds it to the specified recipe. Returns true if successful. + bool ParseIngredient(const AString & a_String, cRecipe * a_Recipe); + + /// Moves the recipe to top-left corner, sets its MinWidth / MinHeight + void NormalizeIngredients(cRecipe * a_Recipe); + + /// Finds a recipe matching the crafting grid. Returns a newly allocated recipe (with all its coords set) or NULL if not found. Caller must delete return value! + cRecipe * FindRecipe(const cItem * a_CraftingGrid, int a_GridWidth, int a_GridHeight); + + /// Same as FindRecipe, but the grid is guaranteed to be of minimal dimensions needed + cRecipe * FindRecipeCropped(const cItem * a_CraftingGrid, int a_GridWidth, int a_GridHeight, int a_GridStride); + + /// Checks if the grid matches the specified recipe, offset by the specified offsets. Returns a matched cRecipe * if so, or NULL if not matching. Caller must delete the return value! + cRecipe * MatchRecipe(const cItem * a_CraftingGrid, int a_GridWidth, int a_GridHeight, int a_GridStride, const cRecipe * a_Recipe, int a_OffsetX, int a_OffsetY); +} ; + + + + -- cgit v1.2.3