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