ebdcb948eae6f44585726596b147d07370a5372c
[oweals/minetest.git] / src / clientobject.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010-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 CLIENTOBJECT_HEADER
21 #define CLIENTOBJECT_HEADER
22
23 #include "common_irrlicht.h"
24 #include "activeobject.h"
25
26 /*
27
28 Some planning
29 -------------
30
31 * Client receives a network packet with information of added objects
32   in it
33 * Client supplies the information to its ClientEnvironment
34 * The environment adds the specified objects to itself
35
36 */
37
38 class ClientActiveObject : public ActiveObject
39 {
40 public:
41         ClientActiveObject(u16 id);
42         virtual ~ClientActiveObject();
43
44         virtual void addToScene(scene::ISceneManager *smgr){}
45         virtual void removeFromScene(){}
46         // 0 <= light_at_pos <= LIGHT_SUN
47         virtual void updateLight(u8 light_at_pos){}
48         virtual v3s16 getLightPosition(){return v3s16(0,0,0);}
49         virtual core::aabbox3d<f32>* getSelectionBox(){return NULL;}
50         virtual core::aabbox3d<f32>* getCollisionBox(){return NULL;}
51         virtual v3f getPosition(){return v3f(0,0,0);}
52         
53         // Step object in time
54         virtual void step(float dtime){}
55         
56         // Process a message sent by the server side object
57         virtual void processMessage(const std::string &data){}
58
59         /*
60                 This takes the return value of
61                 ServerActiveObject::getClientInitializationData
62         */
63         virtual void initialize(const std::string &data){}
64         
65         // Create a certain type of ClientActiveObject
66         static ClientActiveObject* create(u8 type);
67
68 protected:
69         typedef ClientActiveObject* (*Factory)();
70         static void registerType(u16 type, Factory f);
71 private:
72         static core::map<u16, Factory> m_types;
73 };
74
75 struct DistanceSortedActiveObject
76 {
77         ClientActiveObject *obj;
78         f32 d;
79
80         DistanceSortedActiveObject(ClientActiveObject *a_obj, f32 a_d)
81         {
82                 obj = a_obj;
83                 d = a_d;
84         }
85
86         bool operator < (DistanceSortedActiveObject &other)
87         {
88                 return d < other.d;
89         }
90 };
91
92 /*
93         TestCAO
94 */
95
96 class TestCAO : public ClientActiveObject
97 {
98 public:
99         TestCAO();
100         virtual ~TestCAO();
101         
102         u8 getType() const
103         {
104                 return ACTIVEOBJECT_TYPE_TEST;
105         }
106         
107         static ClientActiveObject* create();
108
109         void addToScene(scene::ISceneManager *smgr);
110         void removeFromScene();
111         void updateLight(u8 light_at_pos);
112         v3s16 getLightPosition();
113         void updateNodePos();
114
115         void step(float dtime);
116
117         void processMessage(const std::string &data);
118
119 private:
120         scene::IMeshSceneNode *m_node;
121         v3f m_position;
122 };
123
124 /*
125         ItemCAO
126 */
127
128 class ItemCAO : public ClientActiveObject
129 {
130 public:
131         ItemCAO();
132         virtual ~ItemCAO();
133         
134         u8 getType() const
135         {
136                 return ACTIVEOBJECT_TYPE_ITEM;
137         }
138         
139         static ClientActiveObject* create();
140
141         void addToScene(scene::ISceneManager *smgr);
142         void removeFromScene();
143         void updateLight(u8 light_at_pos);
144         v3s16 getLightPosition();
145         void updateNodePos();
146
147         void step(float dtime);
148
149         void processMessage(const std::string &data);
150
151         void initialize(const std::string &data);
152         
153         core::aabbox3d<f32>* getSelectionBox()
154                 {return &m_selection_box;}
155         v3f getPosition()
156                 {return m_position;}
157
158 private:
159         core::aabbox3d<f32> m_selection_box;
160         scene::IMeshSceneNode *m_node;
161         v3f m_position;
162         std::string m_inventorystring;
163 };
164
165 #endif
166