reorganized a lot of stuff and modified mapgen and objects slightly while doing it
[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
27 /*
28         SmoothTranslator
29 */
30
31 struct SmoothTranslator
32 {
33         v3f vect_old;
34         f32 anim_counter;
35         f32 anim_time;
36         f32 anim_time_counter;
37         v3f vect_show;
38         v3f vect_aim;
39
40         SmoothTranslator():
41                 vect_old(0,0,0),
42                 anim_counter(0),
43                 anim_time(0),
44                 anim_time_counter(0),
45                 vect_show(0,0,0),
46                 vect_aim(0,0,0)
47         {}
48
49         void init(v3f vect)
50         {
51                 vect_old = vect;
52                 vect_show = vect;
53                 vect_aim = vect;
54         }
55
56         void update(v3f vect_new)
57         {
58                 vect_old = vect_show;
59                 vect_aim = vect_new;
60                 if(anim_time < 0.001 || anim_time > 1.0)
61                         anim_time = anim_time_counter;
62                 else
63                         anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
64                 anim_time_counter = 0;
65                 anim_counter = 0;
66         }
67
68         void translate(f32 dtime)
69         {
70                 anim_time_counter = anim_time_counter + dtime;
71                 anim_counter = anim_counter + dtime;
72                 v3f vect_move = vect_aim - vect_old;
73                 f32 moveratio = 1.0;
74                 if(anim_time > 0.001)
75                         moveratio = anim_time_counter / anim_time;
76                 // Move a bit less than should, to avoid oscillation
77                 moveratio = moveratio * 0.8;
78                 if(moveratio > 1.5)
79                         moveratio = 1.5;
80                 vect_show = vect_old + vect_move * moveratio;
81         }
82 };
83
84
85 /*
86         TestCAO
87 */
88
89 class TestCAO : public ClientActiveObject
90 {
91 public:
92         TestCAO();
93         virtual ~TestCAO();
94         
95         u8 getType() const
96         {
97                 return ACTIVEOBJECT_TYPE_TEST;
98         }
99         
100         static ClientActiveObject* create();
101
102         void addToScene(scene::ISceneManager *smgr);
103         void removeFromScene();
104         void updateLight(u8 light_at_pos);
105         v3s16 getLightPosition();
106         void updateNodePos();
107
108         void step(float dtime, ClientEnvironment *env);
109
110         void processMessage(const std::string &data);
111
112 private:
113         scene::IMeshSceneNode *m_node;
114         v3f m_position;
115 };
116
117 /*
118         ItemCAO
119 */
120
121 class ItemCAO : public ClientActiveObject
122 {
123 public:
124         ItemCAO();
125         virtual ~ItemCAO();
126         
127         u8 getType() const
128         {
129                 return ACTIVEOBJECT_TYPE_ITEM;
130         }
131         
132         static ClientActiveObject* create();
133
134         void addToScene(scene::ISceneManager *smgr);
135         void removeFromScene();
136         void updateLight(u8 light_at_pos);
137         v3s16 getLightPosition();
138         void updateNodePos();
139
140         void step(float dtime, ClientEnvironment *env);
141
142         void processMessage(const std::string &data);
143
144         void initialize(const std::string &data);
145         
146         core::aabbox3d<f32>* getSelectionBox()
147                 {return &m_selection_box;}
148         v3f getPosition()
149                 {return m_position;}
150
151 private:
152         core::aabbox3d<f32> m_selection_box;
153         scene::IMeshSceneNode *m_node;
154         v3f m_position;
155         std::string m_inventorystring;
156 };
157
158 /*
159         RatCAO
160 */
161
162 class RatCAO : public ClientActiveObject
163 {
164 public:
165         RatCAO();
166         virtual ~RatCAO();
167         
168         u8 getType() const
169         {
170                 return ACTIVEOBJECT_TYPE_RAT;
171         }
172         
173         static ClientActiveObject* create();
174
175         void addToScene(scene::ISceneManager *smgr);
176         void removeFromScene();
177         void updateLight(u8 light_at_pos);
178         v3s16 getLightPosition();
179         void updateNodePos();
180
181         void step(float dtime, ClientEnvironment *env);
182
183         void processMessage(const std::string &data);
184
185         void initialize(const std::string &data);
186         
187         core::aabbox3d<f32>* getSelectionBox()
188                 {return &m_selection_box;}
189         v3f getPosition()
190                 {return m_position;}
191
192 private:
193         core::aabbox3d<f32> m_selection_box;
194         scene::IMeshSceneNode *m_node;
195         v3f m_position;
196         float m_yaw;
197         SmoothTranslator pos_translator;
198 };
199
200 /*
201         Oerkki1CAO
202 */
203
204 class Oerkki1CAO : public ClientActiveObject
205 {
206 public:
207         Oerkki1CAO();
208         virtual ~Oerkki1CAO();
209         
210         u8 getType() const
211         {
212                 return ACTIVEOBJECT_TYPE_OERKKI1;
213         }
214         
215         static ClientActiveObject* create();
216
217         void addToScene(scene::ISceneManager *smgr);
218         void removeFromScene();
219         void updateLight(u8 light_at_pos);
220         v3s16 getLightPosition();
221         void updateNodePos();
222
223         void step(float dtime, ClientEnvironment *env);
224
225         void processMessage(const std::string &data);
226
227         void initialize(const std::string &data);
228         
229         core::aabbox3d<f32>* getSelectionBox()
230                 {return &m_selection_box;}
231         v3f getPosition()
232                 {return pos_translator.vect_show;}
233                 //{return m_position;}
234
235 private:
236         IntervalLimiter m_attack_interval;
237         core::aabbox3d<f32> m_selection_box;
238         scene::IMeshSceneNode *m_node;
239         v3f m_position;
240         float m_yaw;
241         SmoothTranslator pos_translator;
242         float m_damage_visual_timer;
243         bool m_damage_texture_enabled;
244 };
245
246
247 #endif
248