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