]> git.lizzy.rs Git - dragonfireclient.git/blob - src/clientobject.h
569e9eca6905857cfa69943fd34c28a8eccdff4c
[dragonfireclient.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 /*
39         SmoothTranslator
40 */
41
42 struct SmoothTranslator
43 {
44         v3f vect_old;
45         f32 anim_counter;
46         f32 anim_time;
47         f32 anim_time_counter;
48         v3f vect_show;
49         v3f vect_aim;
50
51         SmoothTranslator():
52                 vect_old(0,0,0),
53                 anim_counter(0),
54                 anim_time(0),
55                 anim_time_counter(0),
56                 vect_show(0,0,0),
57                 vect_aim(0,0,0)
58         {}
59
60         void init(v3f vect)
61         {
62                 vect_old = vect;
63                 vect_show = vect;
64                 vect_aim = vect;
65         }
66
67         void update(v3f vect_new)
68         {
69                 vect_old = vect_show;
70                 vect_aim = vect_new;
71                 if(anim_time < 0.001 || anim_time > 1.0)
72                         anim_time = anim_time_counter;
73                 else
74                         anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
75                 anim_time_counter = 0;
76                 anim_counter = 0;
77         }
78
79         void translate(f32 dtime)
80         {
81                 anim_time_counter = anim_time_counter + dtime;
82                 anim_counter = anim_counter + dtime;
83                 v3f vect_move = vect_aim - vect_old;
84                 f32 moveratio = 1.0;
85                 if(anim_time > 0.001)
86                         moveratio = anim_time_counter / anim_time;
87                 // Move a bit less than should, to avoid oscillation
88                 moveratio = moveratio * 0.8;
89                 if(moveratio > 1.5)
90                         moveratio = 1.5;
91                 vect_show = vect_old + vect_move * moveratio;
92         }
93 };
94
95 class ClientEnvironment;
96
97 class ClientActiveObject : public ActiveObject
98 {
99 public:
100         ClientActiveObject(u16 id);
101         virtual ~ClientActiveObject();
102
103         virtual void addToScene(scene::ISceneManager *smgr){}
104         virtual void removeFromScene(){}
105         // 0 <= light_at_pos <= LIGHT_SUN
106         virtual void updateLight(u8 light_at_pos){}
107         virtual v3s16 getLightPosition(){return v3s16(0,0,0);}
108         virtual core::aabbox3d<f32>* getSelectionBox(){return NULL;}
109         virtual core::aabbox3d<f32>* getCollisionBox(){return NULL;}
110         virtual v3f getPosition(){return v3f(0,0,0);}
111         
112         // Step object in time
113         virtual void step(float dtime, ClientEnvironment *env){}
114         
115         // Process a message sent by the server side object
116         virtual void processMessage(const std::string &data){}
117
118         virtual std::string infoText() {return "";}
119
120         /*
121                 This takes the return value of
122                 ServerActiveObject::getClientInitializationData
123         */
124         virtual void initialize(const std::string &data){}
125         
126         // Create a certain type of ClientActiveObject
127         static ClientActiveObject* create(u8 type);
128
129 protected:
130         // Used for creating objects based on type
131         typedef ClientActiveObject* (*Factory)();
132         static void registerType(u16 type, Factory f);
133 private:
134         // Used for creating objects based on type
135         static core::map<u16, Factory> m_types;
136 };
137
138 struct DistanceSortedActiveObject
139 {
140         ClientActiveObject *obj;
141         f32 d;
142
143         DistanceSortedActiveObject(ClientActiveObject *a_obj, f32 a_d)
144         {
145                 obj = a_obj;
146                 d = a_d;
147         }
148
149         bool operator < (DistanceSortedActiveObject &other)
150         {
151                 return d < other.d;
152         }
153 };
154
155 /*
156         TestCAO
157 */
158
159 class TestCAO : public ClientActiveObject
160 {
161 public:
162         TestCAO();
163         virtual ~TestCAO();
164         
165         u8 getType() const
166         {
167                 return ACTIVEOBJECT_TYPE_TEST;
168         }
169         
170         static ClientActiveObject* create();
171
172         void addToScene(scene::ISceneManager *smgr);
173         void removeFromScene();
174         void updateLight(u8 light_at_pos);
175         v3s16 getLightPosition();
176         void updateNodePos();
177
178         void step(float dtime, ClientEnvironment *env);
179
180         void processMessage(const std::string &data);
181
182 private:
183         scene::IMeshSceneNode *m_node;
184         v3f m_position;
185 };
186
187 /*
188         ItemCAO
189 */
190
191 class ItemCAO : public ClientActiveObject
192 {
193 public:
194         ItemCAO();
195         virtual ~ItemCAO();
196         
197         u8 getType() const
198         {
199                 return ACTIVEOBJECT_TYPE_ITEM;
200         }
201         
202         static ClientActiveObject* create();
203
204         void addToScene(scene::ISceneManager *smgr);
205         void removeFromScene();
206         void updateLight(u8 light_at_pos);
207         v3s16 getLightPosition();
208         void updateNodePos();
209
210         void step(float dtime, ClientEnvironment *env);
211
212         void processMessage(const std::string &data);
213
214         void initialize(const std::string &data);
215         
216         core::aabbox3d<f32>* getSelectionBox()
217                 {return &m_selection_box;}
218         v3f getPosition()
219                 {return m_position;}
220
221 private:
222         core::aabbox3d<f32> m_selection_box;
223         scene::IMeshSceneNode *m_node;
224         v3f m_position;
225         std::string m_inventorystring;
226 };
227
228 /*
229         RatCAO
230 */
231
232 class RatCAO : public ClientActiveObject
233 {
234 public:
235         RatCAO();
236         virtual ~RatCAO();
237         
238         u8 getType() const
239         {
240                 return ACTIVEOBJECT_TYPE_RAT;
241         }
242         
243         static ClientActiveObject* create();
244
245         void addToScene(scene::ISceneManager *smgr);
246         void removeFromScene();
247         void updateLight(u8 light_at_pos);
248         v3s16 getLightPosition();
249         void updateNodePos();
250
251         void step(float dtime, ClientEnvironment *env);
252
253         void processMessage(const std::string &data);
254
255         void initialize(const std::string &data);
256         
257         core::aabbox3d<f32>* getSelectionBox()
258                 {return &m_selection_box;}
259         v3f getPosition()
260                 {return m_position;}
261
262 private:
263         core::aabbox3d<f32> m_selection_box;
264         scene::IMeshSceneNode *m_node;
265         v3f m_position;
266         float m_yaw;
267         SmoothTranslator pos_translator;
268 };
269
270 #endif
271