Crafting definition in scripts
[oweals/minetest.git] / src / craftdef.h
1 /*
2 Minetest-c55
3 Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef CRAFTDEF_HEADER
21 #define CRAFTDEF_HEADER
22
23 #include <string>
24 #include <iostream>
25 #include <vector>
26 class IGameDef;
27 class InventoryItem;
28
29 struct CraftPointerInput
30 {
31         unsigned int width;
32         std::vector<InventoryItem*> items;
33
34         CraftPointerInput(unsigned int width_, const std::vector<InventoryItem*> &items_):
35                 width(width_),
36                 items(items_)
37         {}
38         CraftPointerInput():
39                 width(0)
40         {}
41         ~CraftPointerInput();
42         unsigned int height() const{
43                 return (items.size() + width - 1) / width;
44         }
45 };
46
47 struct CraftInput
48 {
49         unsigned int width;
50         std::vector<std::string> items;
51
52         CraftInput(unsigned int width_, const std::vector<std::string> &items_):
53                 width(width_),
54                 items(items_)
55         {}
56         CraftInput():
57                 width(0)
58         {}
59         unsigned int height() const{
60                 return (items.size() + width - 1) / width;
61         }
62         std::string dump() const;
63 };
64
65 struct CraftDefinition
66 {
67         std::string output;
68         CraftInput input;
69
70         CraftDefinition(){}
71         CraftDefinition(const std::string &output_, unsigned int width_,
72                         const std::vector<std::string> &input_):
73                 output(output_),
74                 input(width_, input_)
75         {}
76         
77         std::string dump() const;
78         void serialize(std::ostream &os) const;
79         void deSerialize(std::istream &is);
80 };
81
82 class ICraftDefManager
83 {
84 public:
85         ICraftDefManager(){}
86         virtual ~ICraftDefManager(){}
87         virtual InventoryItem* getCraftResult(const CraftPointerInput &input_cpi,
88                         IGameDef *gamedef) const=0;
89         
90         virtual void serialize(std::ostream &os)=0;
91 };
92
93 class IWritableCraftDefManager : public ICraftDefManager
94 {
95 public:
96         IWritableCraftDefManager(){}
97         virtual ~IWritableCraftDefManager(){}
98         virtual InventoryItem* getCraftResult(const CraftPointerInput &input_cpi,
99                         IGameDef *gamedef) const=0;
100                         
101         virtual void registerCraft(const CraftDefinition &def)=0;
102         virtual void clear()=0;
103
104         virtual void serialize(std::ostream &os)=0;
105         virtual void deSerialize(std::istream &is)=0;
106 };
107
108 IWritableCraftDefManager* createCraftDefManager();
109
110 #endif
111