]> git.lizzy.rs Git - minetest.git/blob - src/content_cao.h
Improve LuaEntity velocity/acceleration handling (by kahrl); implement staticdata...
[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         v3f vect_show;
37         v3f vect_aim;
38         f32 anim_counter;
39         f32 anim_time;
40         f32 anim_time_counter;
41         bool aim_is_end;
42
43         SmoothTranslator():
44                 vect_old(0,0,0),
45                 vect_show(0,0,0),
46                 vect_aim(0,0,0),
47                 anim_counter(0),
48                 anim_time(0),
49                 anim_time_counter(0),
50                 aim_is_end(true)
51         {}
52
53         void init(v3f vect)
54         {
55                 vect_old = vect;
56                 vect_show = vect;
57                 vect_aim = vect;
58                 anim_counter = 0;
59                 anim_time = 0;
60                 anim_time_counter = 0;
61                 aim_is_end = true;
62         }
63
64         void sharpen()
65         {
66                 init(vect_show);
67         }
68
69         void update(v3f vect_new, bool is_end_position=false, float update_interval=-1)
70         {
71                 aim_is_end = is_end_position;
72                 vect_old = vect_show;
73                 vect_aim = vect_new;
74                 if(update_interval > 0){
75                         anim_time = update_interval;
76                 } else {
77                         if(anim_time < 0.001 || anim_time > 1.0)
78                                 anim_time = anim_time_counter;
79                         else
80                                 anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
81                 }
82                 anim_time_counter = 0;
83                 anim_counter = 0;
84         }
85
86         void translate(f32 dtime)
87         {
88                 anim_time_counter = anim_time_counter + dtime;
89                 anim_counter = anim_counter + dtime;
90                 v3f vect_move = vect_aim - vect_old;
91                 f32 moveratio = 1.0;
92                 if(anim_time > 0.001)
93                         moveratio = anim_time_counter / anim_time;
94                 // Move a bit less than should, to avoid oscillation
95                 moveratio = moveratio * 0.5;
96                 float move_end = 1.5;
97                 if(aim_is_end)
98                         move_end = 1.0;
99                 if(moveratio > move_end)
100                         moveratio = move_end;
101                 vect_show = vect_old + vect_move * moveratio;
102         }
103
104         bool is_moving()
105         {
106                 return ((anim_time_counter / anim_time) < 1.4);
107         }
108 };
109
110
111 /*
112         TestCAO
113 */
114
115 class TestCAO : public ClientActiveObject
116 {
117 public:
118         TestCAO(IGameDef *gamedef);
119         virtual ~TestCAO();
120         
121         u8 getType() const
122         {
123                 return ACTIVEOBJECT_TYPE_TEST;
124         }
125         
126         static ClientActiveObject* create(IGameDef *gamedef);
127
128         void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc);
129         void removeFromScene();
130         void updateLight(u8 light_at_pos);
131         v3s16 getLightPosition();
132         void updateNodePos();
133
134         void step(float dtime, ClientEnvironment *env);
135
136         void processMessage(const std::string &data);
137
138 private:
139         scene::IMeshSceneNode *m_node;
140         v3f m_position;
141 };
142
143 /*
144         ItemCAO
145 */
146
147 class ItemCAO : public ClientActiveObject
148 {
149 public:
150         ItemCAO(IGameDef *gamedef);
151         virtual ~ItemCAO();
152         
153         u8 getType() const
154         {
155                 return ACTIVEOBJECT_TYPE_ITEM;
156         }
157         
158         static ClientActiveObject* create(IGameDef *gamedef);
159
160         void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc);
161         void removeFromScene();
162         void updateLight(u8 light_at_pos);
163         v3s16 getLightPosition();
164         void updateNodePos();
165
166         void step(float dtime, ClientEnvironment *env);
167
168         void processMessage(const std::string &data);
169
170         void initialize(const std::string &data);
171         
172         core::aabbox3d<f32>* getSelectionBox()
173                 {return &m_selection_box;}
174         v3f getPosition()
175                 {return m_position;}
176
177 private:
178         core::aabbox3d<f32> m_selection_box;
179         scene::IMeshSceneNode *m_node;
180         v3f m_position;
181         std::string m_inventorystring;
182 };
183
184 /*
185         RatCAO
186 */
187
188 class RatCAO : public ClientActiveObject
189 {
190 public:
191         RatCAO(IGameDef *gamedef);
192         virtual ~RatCAO();
193         
194         u8 getType() const
195         {
196                 return ACTIVEOBJECT_TYPE_RAT;
197         }
198         
199         static ClientActiveObject* create(IGameDef *gamedef);
200
201         void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc);
202         void removeFromScene();
203         void updateLight(u8 light_at_pos);
204         v3s16 getLightPosition();
205         void updateNodePos();
206
207         void step(float dtime, ClientEnvironment *env);
208
209         void processMessage(const std::string &data);
210
211         void initialize(const std::string &data);
212         
213         core::aabbox3d<f32>* getSelectionBox()
214                 {return &m_selection_box;}
215         v3f getPosition()
216                 {return pos_translator.vect_show;}
217                 //{return m_position;}
218
219 private:
220         core::aabbox3d<f32> m_selection_box;
221         scene::IMeshSceneNode *m_node;
222         v3f m_position;
223         float m_yaw;
224         SmoothTranslator pos_translator;
225 };
226
227 /*
228         Oerkki1CAO
229 */
230
231 class Oerkki1CAO : public ClientActiveObject
232 {
233 public:
234         Oerkki1CAO(IGameDef *gamedef);
235         virtual ~Oerkki1CAO();
236         
237         u8 getType() const
238         {
239                 return ACTIVEOBJECT_TYPE_OERKKI1;
240         }
241         
242         static ClientActiveObject* create(IGameDef *gamedef);
243
244         void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc);
245         void removeFromScene();
246         void updateLight(u8 light_at_pos);
247         v3s16 getLightPosition();
248         void updateNodePos();
249
250         void step(float dtime, ClientEnvironment *env);
251
252         void processMessage(const std::string &data);
253
254         void initialize(const std::string &data);
255         
256         core::aabbox3d<f32>* getSelectionBox()
257                 {return &m_selection_box;}
258         v3f getPosition()
259                 {return pos_translator.vect_show;}
260                 //{return m_position;}
261
262         // If returns true, punch will not be sent to the server
263         bool directReportPunch(const std::string &toolname, v3f dir);
264
265 private:
266         IntervalLimiter m_attack_interval;
267         core::aabbox3d<f32> m_selection_box;
268         scene::IMeshSceneNode *m_node;
269         v3f m_position;
270         float m_yaw;
271         SmoothTranslator pos_translator;
272         float m_damage_visual_timer;
273         bool m_damage_texture_enabled;
274 };
275
276 /*
277         FireflyCAO
278 */
279
280 class FireflyCAO : public ClientActiveObject
281 {
282 public:
283         FireflyCAO(IGameDef *gamedef);
284         virtual ~FireflyCAO();
285         
286         u8 getType() const
287         {
288                 return ACTIVEOBJECT_TYPE_FIREFLY;
289         }
290         
291         static ClientActiveObject* create(IGameDef *gamedef);
292
293         void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc);
294         void removeFromScene();
295         void updateLight(u8 light_at_pos);
296         v3s16 getLightPosition();
297         void updateNodePos();
298
299         void step(float dtime, ClientEnvironment *env);
300
301         void processMessage(const std::string &data);
302
303         void initialize(const std::string &data);
304         
305         core::aabbox3d<f32>* getSelectionBox()
306                 {return &m_selection_box;}
307         v3f getPosition()
308                 {return m_position;}
309
310 private:
311         core::aabbox3d<f32> m_selection_box;
312         scene::IMeshSceneNode *m_node;
313         v3f m_position;
314         float m_yaw;
315         SmoothTranslator pos_translator;
316 };
317
318 /*
319         MobV2CAO
320 */
321
322 class MobV2CAO : public ClientActiveObject
323 {
324 public:
325         MobV2CAO(IGameDef *gamedef);
326         virtual ~MobV2CAO();
327         
328         u8 getType() const
329         {
330                 return ACTIVEOBJECT_TYPE_MOBV2;
331         }
332         
333         static ClientActiveObject* create(IGameDef *gamedef);
334
335         void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc);
336         void removeFromScene();
337         void updateLight(u8 light_at_pos);
338         v3s16 getLightPosition();
339         void updateNodePos();
340
341         void step(float dtime, ClientEnvironment *env);
342
343         void processMessage(const std::string &data);
344
345         void initialize(const std::string &data);
346         
347         core::aabbox3d<f32>* getSelectionBox()
348                 {return &m_selection_box;}
349         v3f getPosition()
350                 {return pos_translator.vect_show;}
351                 //{return m_position;}
352         bool doShowSelectionBox(){return false;}
353
354         // If returns true, punch will not be sent to the server
355         bool directReportPunch(const std::string &toolname, v3f dir);
356
357 private:
358         void setLooks(const std::string &looks);
359         
360         IntervalLimiter m_attack_interval;
361         core::aabbox3d<f32> m_selection_box;
362         scene::MyBillboardSceneNode *m_node;
363         v3f m_position;
364         std::string m_texture_name;
365         float m_yaw;
366         SmoothTranslator pos_translator;
367         bool m_walking;
368         float m_walking_unset_timer;
369         float m_walk_timer;
370         int m_walk_frame;
371         float m_damage_visual_timer;
372         u8 m_last_light;
373         bool m_shooting;
374         float m_shooting_unset_timer;
375         v2f m_sprite_size;
376         float m_sprite_y;
377         bool m_bright_shooting;
378         std::string m_sprite_type;
379         int m_simple_anim_frames;
380         float m_simple_anim_frametime;
381         bool m_lock_full_brightness;
382         int m_player_hit_damage;
383         float m_player_hit_distance;
384         float m_player_hit_interval;
385         float m_player_hit_timer;
386
387         Settings *m_properties;
388 };
389
390 /*
391         LuaEntityCAO
392 */
393
394 struct LuaEntityProperties;
395
396 class LuaEntityCAO : public ClientActiveObject
397 {
398 public:
399         LuaEntityCAO(IGameDef *gamedef);
400         virtual ~LuaEntityCAO();
401         
402         u8 getType() const
403         {
404                 return ACTIVEOBJECT_TYPE_LUAENTITY;
405         }
406         
407         static ClientActiveObject* create(IGameDef *gamedef);
408
409         void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc);
410         void removeFromScene();
411         void updateLight(u8 light_at_pos);
412         v3s16 getLightPosition();
413         void updateNodePos();
414
415         void step(float dtime, ClientEnvironment *env);
416
417         void processMessage(const std::string &data);
418
419         void initialize(const std::string &data);
420         
421         core::aabbox3d<f32>* getSelectionBox()
422                 {return &m_selection_box;}
423         v3f getPosition()
424                 {return pos_translator.vect_show;}
425
426 private:
427         core::aabbox3d<f32> m_selection_box;
428         scene::IMeshSceneNode *m_meshnode;
429         scene::MyBillboardSceneNode *m_spritenode;
430         v3f m_position;
431         v3f m_velocity;
432         v3f m_acceleration;
433         float m_yaw;
434         struct LuaEntityProperties *m_prop;
435         SmoothTranslator pos_translator;
436 };
437
438
439 #endif
440