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