Cuberite
A lightweight, fast and extensible game server for Minecraft
CraftingRecipes.h
Go to the documentation of this file.
1 
2 // CraftingRecipes.h
3 
4 // Interfaces to the cCraftingRecipes class representing the storage of crafting recipes
5 
6 
7 
8 
9 #pragma once
10 
11 #include "Item.h"
12 
13 // fwd: cPlayer.h
14 class cPlayer;
15 
16 
17 
18 
19 
20 class cCraftingGrid // tolua_export
21 { // tolua_export
22 public:
23  cCraftingGrid(const cCraftingGrid & a_Original);
24  cCraftingGrid(int a_Width, int a_Height); // tolua_export
25  cCraftingGrid(const cItem * a_Items, int a_Width, int a_Height);
27 
28  // tolua_begin
29  int GetWidth (void) const {return m_Width; }
30  int GetHeight(void) const {return m_Height; }
31  cItem & GetItem (int x, int y) const;
32  void SetItem (int x, int y, ENUM_ITEM_TYPE a_ItemType, char a_ItemCount, short a_ItemHealth);
33  void SetItem (int x, int y, const cItem & a_Item);
34  void Clear (void);
35 
37  void ConsumeGrid(const cCraftingGrid & a_Grid);
38 
40  void Dump(void);
41 
42  // tolua_end
43 
44  cItem * GetItems(void) const {return m_Items; }
45 
47  void CopyToItems(cItem * a_Items) const;
48 
49 protected:
50 
51  int m_Width;
52  int m_Height;
54 } ; // tolua_export
55 
56 
57 
58 
59 
60 class cCraftingRecipe // tolua_export
61 { // tolua_export
62 public:
63  cCraftingRecipe(const cCraftingGrid & a_CraftingGrid);
64 
65  // tolua_begin
66  void Clear (void);
67  int GetIngredientsWidth (void) const {return m_Ingredients.GetWidth(); }
68  int GetIngredientsHeight(void) const {return m_Ingredients.GetHeight(); }
69  cItem & GetIngredient (int x, int y) const {return m_Ingredients.GetItem(x, y); }
70  const cItem & GetResult (void) const {return m_Result; }
71  void SetResult (ENUM_ITEM_TYPE a_ItemType, char a_ItemCount, short a_ItemHealth);
72  void SetResult (const cItem & a_Item)
73  {
74  m_Result = a_Item;
75  }
76 
77  void SetIngredient (int x, int y, ENUM_ITEM_TYPE a_ItemType, char a_ItemCount, short a_ItemHealth)
78  {
79  m_Ingredients.SetItem(x, y, a_ItemType, a_ItemCount, a_ItemHealth);
80  }
81 
82  void SetIngredient (int x, int y, const cItem & a_Item)
83  {
84  m_Ingredients.SetItem(x, y, a_Item);
85  }
86 
88  void ConsumeIngredients(cCraftingGrid & a_CraftingGrid);
89 
91  void Dump(void);
92  // tolua_end
93 
94 protected:
95 
96  cCraftingGrid m_Ingredients; // Adjusted to correspond to the input crafting grid!
98 } ; // tolua_export
99 
100 
101 
102 
118 {
119 public:
120  static const int MAX_GRID_WIDTH = 3;
121  static const int MAX_GRID_HEIGHT = 3;
122 
123  cCraftingRecipes(void);
125 
127  void GetRecipe(cPlayer & a_Player, cCraftingGrid & a_CraftingGrid, cCraftingRecipe & a_Recipe);
128 
130  std::vector<UInt32> FindNewRecipesForItem(const cItem & a_Item, const std::set<cItem, cItem::sItemCompare> & a_KnownItems);
131 
132  struct cRecipeSlot
133  {
135  int x, y; // 1..3, or -1 for "any"
136  } ;
137  typedef std::vector<cRecipeSlot> cRecipeSlots;
138 
141  struct cRecipe
142  {
146 
147  // Size of the regular items in the recipe; "anywhere" items are excluded:
148  int m_Width;
149  int m_Height;
150  } ;
151 
153  cRecipe * GetRecipeById(UInt32 a_RecipeId);
154 
156  const std::map<AString, UInt32> & GetRecipeNameMap();
157 
158 protected:
159 
160  typedef std::vector<cRecipe *> cRecipes;
161 
163 
164  void LoadRecipes(void);
165  void ClearRecipes(void);
166 
168  void AddRecipeLine(int a_LineNum, const AString & a_RecipeLine);
169 
171  bool ParseItem(const AString & a_String, cItem & a_Item);
172 
174  bool ParseIngredient(const AString & a_String, cRecipe * a_Recipe);
175 
177  void NormalizeIngredients(cRecipe * a_Recipe);
178 
180  cRecipe * FindRecipe(const cItem * a_CraftingGrid, int a_GridWidth, int a_GridHeight);
181 
183  cRecipe * FindRecipeCropped(const cItem * a_CraftingGrid, int a_GridWidth, int a_GridHeight, int a_GridStride);
184 
186  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);
187 
189  void HandleFireworks(const cItem * a_CraftingGrid, cCraftingRecipes::cRecipe * a_Recipe, int a_GridStride, int a_OffsetX, int a_OffsetY);
190 
192  void HandleDyedLeather(const cItem * a_CraftingGrid, cCraftingRecipes::cRecipe * a_Recipe, int a_GridStride, int a_GridWidth, int a_GridHeight);
193 
194 private:
196  std::map<AString, UInt32> m_RecipeNameMap;
197 
204  const cRecipe * a_Recipe,
205  const cItem & a_NewItem,
206  const std::set<cItem, cItem::sItemCompare> & a_KnownItems
207  );
208 
210  void PopulateRecipeNameMap(void);
211 } ;
ENUM_ITEM_TYPE
Definition: BlockType.h:295
unsigned int UInt32
Definition: Globals.h:157
std::string AString
Definition: StringUtils.h:11
cCraftingGrid(const cCraftingGrid &a_Original)
void SetItem(int x, int y, ENUM_ITEM_TYPE a_ItemType, char a_ItemCount, short a_ItemHealth)
cItem * GetItems(void) const
void Dump(void)
Dumps the entire crafting grid using LOGD()
int GetWidth(void) const
void CopyToItems(cItem *a_Items) const
Copies internal contents into the item array specified.
void ConsumeGrid(const cCraftingGrid &a_Grid)
Removes items in a_Grid from m_Items[] (used by cCraftingRecipe::ConsumeIngredients())
int GetHeight(void) const
cItem & GetItem(int x, int y) const
cItem & GetIngredient(int x, int y) const
void Dump(void)
Dumps the entire recipe using LOGD()
void SetIngredient(int x, int y, const cItem &a_Item)
void SetIngredient(int x, int y, ENUM_ITEM_TYPE a_ItemType, char a_ItemCount, short a_ItemHealth)
void ConsumeIngredients(cCraftingGrid &a_CraftingGrid)
Consumes ingredients from the crafting grid specified.
int GetIngredientsWidth(void) const
const cItem & GetResult(void) const
int GetIngredientsHeight(void) const
void SetResult(ENUM_ITEM_TYPE a_ItemType, char a_ItemCount, short a_ItemHealth)
cCraftingRecipe(const cCraftingGrid &a_CraftingGrid)
cCraftingGrid m_Ingredients
void SetResult(const cItem &a_Item)
The crafting recipes are the configurations to build a result item out of a set of ingredient items.
static const int MAX_GRID_HEIGHT
std::vector< cRecipeSlot > cRecipeSlots
void HandleFireworks(const cItem *a_CraftingGrid, cCraftingRecipes::cRecipe *a_Recipe, int a_GridStride, int a_OffsetX, int a_OffsetY)
Searches for anything firework related, and does the data setting if appropriate.
static const int MAX_GRID_WIDTH
void GetRecipe(cPlayer &a_Player, cCraftingGrid &a_CraftingGrid, cCraftingRecipe &a_Recipe)
Returns the recipe for current crafting grid.
void NormalizeIngredients(cRecipe *a_Recipe)
Moves the recipe to top-left corner, sets its MinWidth / MinHeight.
const std::map< AString, UInt32 > & GetRecipeNameMap()
Gets a map of all recipes with name and recipe id.
std::map< AString, UInt32 > m_RecipeNameMap
Mapping the minecraft recipe names to the internal cuberite recipe Ids.
bool IsNewCraftableRecipe(const cRecipe *a_Recipe, const cItem &a_NewItem, const std::set< cItem, cItem::sItemCompare > &a_KnownItems)
Checks if all ingredients of the a_Recipe are within the a_KnownItems list and if the a_NewItem is pa...
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)
Checks if the grid matches the specified recipe, offset by the specified offsets.
cRecipe * GetRecipeById(UInt32 a_RecipeId)
Returns the recipe by id.
cRecipe * FindRecipeCropped(const cItem *a_CraftingGrid, int a_GridWidth, int a_GridHeight, int a_GridStride)
Same as FindRecipe, but the grid is guaranteed to be of minimal dimensions needed.
void HandleDyedLeather(const cItem *a_CraftingGrid, cCraftingRecipes::cRecipe *a_Recipe, int a_GridStride, int a_GridWidth, int a_GridHeight)
Searches for anything dye related for leather, calculates the appropriate color value,...
cRecipe * FindRecipe(const cItem *a_CraftingGrid, int a_GridWidth, int a_GridHeight)
Finds a recipe matching the crafting grid.
std::vector< cRecipe * > cRecipes
bool ParseItem(const AString &a_String, cItem &a_Item)
Parses an item string in the format "<ItemType>[^<Damage>]", returns true if successful.
bool ParseIngredient(const AString &a_String, cRecipe *a_Recipe)
Parses one ingredient and adds it to the specified recipe.
std::vector< UInt32 > FindNewRecipesForItem(const cItem &a_Item, const std::set< cItem, cItem::sItemCompare > &a_KnownItems)
Find recipes and returns the RecipeIds which contain the new item and all ingredients are in the know...
void PopulateRecipeNameMap(void)
Populates the RecipeNameMap.
void AddRecipeLine(int a_LineNum, const AString &a_RecipeLine)
Parses the recipe line and adds it into m_Recipes.
A single recipe, stored.
Definition: Player.h:29
Definition: Item.h:37