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