utility.h: Change Buffer's interface to be more compatible with SharedBuffer's interf...
[oweals/minetest.git] / src / materials.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010 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 MATERIALS_HEADER
21 #define MATERIALS_HEADER
22
23 /*
24         Material properties
25 */
26
27 #include "common_irrlicht.h"
28 #include <string>
29
30 struct DiggingProperties
31 {
32         DiggingProperties():
33                 diggable(false),
34                 time(0.0),
35                 wear(0)
36         {
37         }
38         DiggingProperties(bool a_diggable, float a_time, u16 a_wear):
39                 diggable(a_diggable),
40                 time(a_time),
41                 wear(a_wear)
42         {
43         }
44         bool diggable;
45         // Digging time in seconds
46         float time;
47         // Caused wear
48         u16 wear;
49 };
50
51 /*
52         This is a bad way of determining mining characteristics.
53         TODO: Get rid of this and set up some attributes like toughness,
54               fluffyness, and a funciton to calculate time and durability loss
55               (and sound? and whatever else) from them
56 */
57 class DiggingPropertiesList
58 {
59 public:
60         DiggingPropertiesList()
61         {
62         }
63
64         void set(const std::string toolname,
65                         const DiggingProperties &prop)
66         {
67                 m_digging_properties[toolname] = prop;
68         }
69
70         DiggingProperties get(const std::string toolname)
71         {
72                 core::map<std::string, DiggingProperties>::Node *n;
73                 n = m_digging_properties.find(toolname);
74                 if(n == NULL)
75                 {
76                         // Not diggable by this tool, try to get defaults
77                         n = m_digging_properties.find("");
78                         if(n == NULL)
79                         {
80                                 // Not diggable at all
81                                 return DiggingProperties();
82                         }
83                 }
84                 // Return found properties
85                 return n->getValue();
86         }
87
88         void clear()
89         {
90                 m_digging_properties.clear();
91         }
92
93 private:
94         // toolname="": default properties (digging by hand)
95         // Key is toolname
96         core::map<std::string, DiggingProperties> m_digging_properties;
97 };
98
99 // For getting the default properties, set tool=""
100 DiggingProperties getDiggingProperties(u16 material, const std::string &tool);
101
102 #endif
103