mobv2
[oweals/minetest.git] / src / content_cao.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 CONTENT_CAO_HEADER
21 #define CONTENT_CAO_HEADER
22
23 #include "clientobject.h"
24 #include "content_object.h"
25 #include "utility.h" // For IntervalLimiter
26 class Settings;
27 #include "MyBillboardSceneNode.h"
28
29 /*
30         SmoothTranslator
31 */
32
33 struct SmoothTranslator
34 {
35         v3f vect_old;
36         f32 anim_counter;
37         f32 anim_time;
38         f32 anim_time_counter;
39         v3f vect_show;
40         v3f vect_aim;
41
42         SmoothTranslator():
43                 vect_old(0,0,0),
44                 anim_counter(0),
45                 anim_time(0),
46                 anim_time_counter(0),
47                 vect_show(0,0,0),
48                 vect_aim(0,0,0)
49         {}
50
51         void init(v3f vect)
52         {
53                 vect_old = vect;
54                 vect_show = vect;
55                 vect_aim = vect;
56         }
57
58         void update(v3f vect_new)
59         {
60                 vect_old = vect_show;
61                 vect_aim = vect_new;
62                 if(anim_time < 0.001 || anim_time > 1.0)
63                         anim_time = anim_time_counter;
64                 else
65                         anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
66                 anim_time_counter = 0;
67                 anim_counter = 0;
68         }
69
70         void translate(f32 dtime)
71         {
72                 anim_time_counter = anim_time_counter + dtime;
73                 anim_counter = anim_counter + dtime;
74                 v3f vect_move = vect_aim - vect_old;
75                 f32 moveratio = 1.0;
76                 if(anim_time > 0.001)
77                         moveratio = anim_time_counter / anim_time;
78                 // Move a bit less than should, to avoid oscillation
79                 moveratio = moveratio * 0.5;
80                 if(moveratio > 1.5)
81                         moveratio = 1.5;
82                 vect_show = vect_old + vect_move * moveratio;
83         }
84
85         bool is_moving()
86         {
87                 return ((anim_time_counter / anim_time) < 1.4);
88         }
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, ClientEnvironment *env);
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, ClientEnvironment *env);
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 /*
166         RatCAO
167 */
168
169 class RatCAO : public ClientActiveObject
170 {
171 public:
172         RatCAO();
173         virtual ~RatCAO();
174         
175         u8 getType() const
176         {
177                 return ACTIVEOBJECT_TYPE_RAT;
178         }
179         
180         static ClientActiveObject* create();
181
182         void addToScene(scene::ISceneManager *smgr);
183         void removeFromScene();
184         void updateLight(u8 light_at_pos);
185         v3s16 getLightPosition();
186         void updateNodePos();
187
188         void step(float dtime, ClientEnvironment *env);
189
190         void processMessage(const std::string &data);
191
192         void initialize(const std::string &data);
193         
194         core::aabbox3d<f32>* getSelectionBox()
195                 {return &m_selection_box;}
196         v3f getPosition()
197                 {return pos_translator.vect_show;}
198                 //{return m_position;}
199
200 private:
201         core::aabbox3d<f32> m_selection_box;
202         scene::IMeshSceneNode *m_node;
203         v3f m_position;
204         float m_yaw;
205         SmoothTranslator pos_translator;
206 };
207
208 /*
209         Oerkki1CAO
210 */
211
212 class Oerkki1CAO : public ClientActiveObject
213 {
214 public:
215         Oerkki1CAO();
216         virtual ~Oerkki1CAO();
217         
218         u8 getType() const
219         {
220                 return ACTIVEOBJECT_TYPE_OERKKI1;
221         }
222         
223         static ClientActiveObject* create();
224
225         void addToScene(scene::ISceneManager *smgr);
226         void removeFromScene();
227         void updateLight(u8 light_at_pos);
228         v3s16 getLightPosition();
229         void updateNodePos();
230
231         void step(float dtime, ClientEnvironment *env);
232
233         void processMessage(const std::string &data);
234
235         void initialize(const std::string &data);
236         
237         core::aabbox3d<f32>* getSelectionBox()
238                 {return &m_selection_box;}
239         v3f getPosition()
240                 {return pos_translator.vect_show;}
241                 //{return m_position;}
242
243 private:
244         IntervalLimiter m_attack_interval;
245         core::aabbox3d<f32> m_selection_box;
246         scene::IMeshSceneNode *m_node;
247         v3f m_position;
248         float m_yaw;
249         SmoothTranslator pos_translator;
250         float m_damage_visual_timer;
251         bool m_damage_texture_enabled;
252 };
253
254 /*
255         FireflyCAO
256 */
257
258 class FireflyCAO : public ClientActiveObject
259 {
260 public:
261         FireflyCAO();
262         virtual ~FireflyCAO();
263         
264         u8 getType() const
265         {
266                 return ACTIVEOBJECT_TYPE_FIREFLY;
267         }
268         
269         static ClientActiveObject* create();
270
271         void addToScene(scene::ISceneManager *smgr);
272         void removeFromScene();
273         void updateLight(u8 light_at_pos);
274         v3s16 getLightPosition();
275         void updateNodePos();
276
277         void step(float dtime, ClientEnvironment *env);
278
279         void processMessage(const std::string &data);
280
281         void initialize(const std::string &data);
282         
283         core::aabbox3d<f32>* getSelectionBox()
284                 {return &m_selection_box;}
285         v3f getPosition()
286                 {return m_position;}
287
288 private:
289         core::aabbox3d<f32> m_selection_box;
290         scene::IMeshSceneNode *m_node;
291         v3f m_position;
292         float m_yaw;
293         SmoothTranslator pos_translator;
294 };
295
296 /*
297         MobV2CAO
298 */
299
300 class MobV2CAO : public ClientActiveObject
301 {
302 public:
303         MobV2CAO();
304         virtual ~MobV2CAO();
305         
306         u8 getType() const
307         {
308                 return ACTIVEOBJECT_TYPE_MOBV2;
309         }
310         
311         static ClientActiveObject* create();
312
313         void addToScene(scene::ISceneManager *smgr);
314         void removeFromScene();
315         void updateLight(u8 light_at_pos);
316         v3s16 getLightPosition();
317         void updateNodePos();
318
319         void step(float dtime, ClientEnvironment *env);
320
321         void processMessage(const std::string &data);
322
323         void initialize(const std::string &data);
324         
325         core::aabbox3d<f32>* getSelectionBox()
326                 {return &m_selection_box;}
327         v3f getPosition()
328                 {return pos_translator.vect_show;}
329                 //{return m_position;}
330         bool doShowSelectionBox(){return false;}
331
332 private:
333         IntervalLimiter m_attack_interval;
334         core::aabbox3d<f32> m_selection_box;
335         scene::MyBillboardSceneNode *m_node;
336         v3f m_position;
337         float m_yaw;
338         SmoothTranslator pos_translator;
339         bool m_walking;
340         float m_walking_unset_timer;
341         float m_walk_timer;
342         int m_walk_frame;
343         float m_damage_visual_timer;
344         u8 m_last_light;
345         bool m_shooting;
346         float m_shooting_unset_timer;
347         v2f m_sprite_size;
348         v3f m_sprite_pos;
349         bool m_bright_shooting;
350         std::string m_sprite_type;
351         int m_simple_anim_frames;
352         float m_simple_anim_frametime;
353         bool m_lock_full_brightness;
354         int m_player_hit_damage;
355         float m_player_hit_distance;
356         float m_player_hit_interval;
357         float m_player_hit_timer;
358
359         Settings *m_properties;
360 };
361
362
363 #endif
364