d45556269743d6d878b83e93ec5a27877f94f2bf
[oweals/minetest.git] / src / tool.cpp
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 #include "tool.h"
21 #include "irrlichttypes.h"
22 #include "log.h"
23 #include <ostream>
24
25 class CToolDefManager: public IToolDefManager
26 {
27 public:
28         virtual ~CToolDefManager()
29         {
30                 for(core::map<std::string, ToolDefinition*>::Iterator
31                                 i = m_tool_definitions.getIterator();
32                                 i.atEnd() == false; i++){
33                         delete i.getNode()->getValue();
34                 }
35         }
36         virtual bool registerTool(std::string toolname, const ToolDefinition &def)
37         {
38                 infostream<<"registerTool: registering tool \""<<toolname<<"\""<<std::endl;
39                 core::map<std::string, ToolDefinition*>::Node *n;
40                 n = m_tool_definitions.find(toolname);
41                 if(n != NULL){
42                         errorstream<<"registerTool: registering tool \""<<toolname
43                                         <<"\" failed: name is already registered"<<std::endl;
44                         return false;
45                 }
46                 m_tool_definitions[toolname] = new ToolDefinition(def);
47                 return true;
48         }
49         virtual ToolDefinition* getToolDefinition(const std::string &toolname)
50         {
51                 core::map<std::string, ToolDefinition*>::Node *n;
52                 n = m_tool_definitions.find(toolname);
53                 if(n == NULL)
54                         return NULL;
55                 return n->getValue();
56         }
57         virtual std::string getImagename(const std::string &toolname)
58         {
59                 ToolDefinition *def = getToolDefinition(toolname);
60                 if(def == NULL)
61                         return "";
62                 return def->imagename;
63         }
64         virtual ToolDiggingProperties getDiggingProperties(
65                         const std::string &toolname)
66         {
67                 ToolDefinition *def = getToolDefinition(toolname);
68                 // If tool does not exist, just return an impossible
69                 if(def == NULL){
70                         // If tool does not exist, try empty name
71                         ToolDefinition *def = getToolDefinition("");
72                         if(def == NULL) // If that doesn't exist either, return default
73                                 return ToolDiggingProperties();
74                         return def->properties;
75                 }
76                 return def->properties;
77         }
78 private:
79         // Key is name
80         core::map<std::string, ToolDefinition*> m_tool_definitions;
81 };
82
83 IToolDefManager* createToolDefManager()
84 {
85         return new CToolDefManager();
86 }
87