]> git.lizzy.rs Git - minetest.git/blob - src/clientobject.cpp
a6aa033efcd533993ff0c1d3f01ac8d23f3d36c5
[minetest.git] / src / clientobject.cpp
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 #include "clientobject.h"
21 #include "debug.h"
22 #include "porting.h"
23 #include "constants.h"
24 #include "utility.h"
25 #include "environment.h"
26
27 core::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
28
29 ClientActiveObject::ClientActiveObject(u16 id):
30         ActiveObject(id)
31 {
32 }
33
34 ClientActiveObject::~ClientActiveObject()
35 {
36         removeFromScene();
37 }
38
39 ClientActiveObject* ClientActiveObject::create(u8 type)
40 {
41         // Find factory function
42         core::map<u16, Factory>::Node *n;
43         n = m_types.find(type);
44         if(n == NULL)
45         {
46                 // If factory is not found, just return.
47                 dstream<<"WARNING: ClientActiveObject: No factory for type="
48                                 <<type<<std::endl;
49                 return NULL;
50         }
51
52         Factory f = n->getValue();
53         ClientActiveObject *object = (*f)();
54         return object;
55 }
56
57 void ClientActiveObject::registerType(u16 type, Factory f)
58 {
59         core::map<u16, Factory>::Node *n;
60         n = m_types.find(type);
61         if(n)
62                 return;
63         m_types.insert(type, f);
64 }
65
66 /*
67         TestCAO
68 */
69
70 // Prototype
71 TestCAO proto_TestCAO;
72
73 TestCAO::TestCAO():
74         ClientActiveObject(0),
75         m_node(NULL),
76         m_position(v3f(0,10*BS,0))
77 {
78         ClientActiveObject::registerType(getType(), create);
79 }
80
81 TestCAO::~TestCAO()
82 {
83 }
84
85 ClientActiveObject* TestCAO::create()
86 {
87         return new TestCAO();
88 }
89
90 void TestCAO::addToScene(scene::ISceneManager *smgr)
91 {
92         if(m_node != NULL)
93                 return;
94         
95         video::IVideoDriver* driver = smgr->getVideoDriver();
96         
97         scene::SMesh *mesh = new scene::SMesh();
98         scene::IMeshBuffer *buf = new scene::SMeshBuffer();
99         video::SColor c(255,255,255,255);
100         video::S3DVertex vertices[4] =
101         {
102                 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
103                 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
104                 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
105                 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
106         };
107         u16 indices[] = {0,1,2,2,3,0};
108         buf->append(vertices, 4, indices, 6);
109         // Set material
110         buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
111         buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
112         buf->getMaterial().setTexture
113                         (0, driver->getTexture(porting::getDataPath("rat.png").c_str()));
114         buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
115         buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
116         buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
117         // Add to mesh
118         mesh->addMeshBuffer(buf);
119         buf->drop();
120         m_node = smgr->addMeshSceneNode(mesh, NULL);
121         mesh->drop();
122         updateNodePos();
123 }
124
125 void TestCAO::removeFromScene()
126 {
127         if(m_node == NULL)
128                 return;
129
130         m_node->remove();
131         m_node = NULL;
132 }
133
134 void TestCAO::updateLight(u8 light_at_pos)
135 {
136 }
137
138 v3s16 TestCAO::getLightPosition()
139 {
140         return floatToInt(m_position, BS);
141 }
142
143 void TestCAO::updateNodePos()
144 {
145         if(m_node == NULL)
146                 return;
147
148         m_node->setPosition(m_position);
149         //m_node->setRotation(v3f(0, 45, 0));
150 }
151
152 void TestCAO::step(float dtime, ClientEnvironment *env)
153 {
154         if(m_node)
155         {
156                 v3f rot = m_node->getRotation();
157                 //dstream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
158                 rot.Y += dtime * 180;
159                 m_node->setRotation(rot);
160         }
161 }
162
163 void TestCAO::processMessage(const std::string &data)
164 {
165         dstream<<"TestCAO: Got data: "<<data<<std::endl;
166         std::istringstream is(data, std::ios::binary);
167         u16 cmd;
168         is>>cmd;
169         if(cmd == 0)
170         {
171                 v3f newpos;
172                 is>>newpos.X;
173                 is>>newpos.Y;
174                 is>>newpos.Z;
175                 m_position = newpos;
176                 updateNodePos();
177         }
178 }
179
180 /*
181         ItemCAO
182 */
183
184 #include "inventory.h"
185
186 // Prototype
187 ItemCAO proto_ItemCAO;
188
189 ItemCAO::ItemCAO():
190         ClientActiveObject(0),
191         m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
192         m_node(NULL),
193         m_position(v3f(0,10*BS,0))
194 {
195         ClientActiveObject::registerType(getType(), create);
196 }
197
198 ItemCAO::~ItemCAO()
199 {
200 }
201
202 ClientActiveObject* ItemCAO::create()
203 {
204         return new ItemCAO();
205 }
206
207 void ItemCAO::addToScene(scene::ISceneManager *smgr)
208 {
209         if(m_node != NULL)
210                 return;
211         
212         video::IVideoDriver* driver = smgr->getVideoDriver();
213         
214         scene::SMesh *mesh = new scene::SMesh();
215         scene::IMeshBuffer *buf = new scene::SMeshBuffer();
216         video::SColor c(255,255,255,255);
217         video::S3DVertex vertices[4] =
218         {
219                 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
220                 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
221                 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
222                 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
223                 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
224                 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
225                 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
226                 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
227         };
228         u16 indices[] = {0,1,2,2,3,0};
229         buf->append(vertices, 4, indices, 6);
230         // Set material
231         buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
232         buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
233         //buf->getMaterial().setTexture(0, NULL);
234         buf->getMaterial().setTexture
235                         (0, driver->getTexture(porting::getDataPath("rat.png").c_str()));
236         buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
237         buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
238         buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
239         // Add to mesh
240         mesh->addMeshBuffer(buf);
241         buf->drop();
242         m_node = smgr->addMeshSceneNode(mesh, NULL);
243         mesh->drop();
244         // Set it to use the materials of the meshbuffers directly.
245         // This is needed for changing the texture in the future
246         m_node->setReadOnlyMaterials(true);
247         updateNodePos();
248 }
249
250 void ItemCAO::removeFromScene()
251 {
252         if(m_node == NULL)
253                 return;
254
255         m_node->remove();
256         m_node = NULL;
257 }
258
259 void ItemCAO::updateLight(u8 light_at_pos)
260 {
261 }
262
263 v3s16 ItemCAO::getLightPosition()
264 {
265         return floatToInt(m_position, BS);
266 }
267
268 void ItemCAO::updateNodePos()
269 {
270         if(m_node == NULL)
271                 return;
272
273         m_node->setPosition(m_position);
274 }
275
276 void ItemCAO::step(float dtime, ClientEnvironment *env)
277 {
278         if(m_node)
279         {
280                 /*v3f rot = m_node->getRotation();
281                 rot.Y += dtime * 120;
282                 m_node->setRotation(rot);*/
283                 LocalPlayer *player = env->getLocalPlayer();
284                 assert(player);
285                 v3f rot = m_node->getRotation();
286                 rot.Y = 180.0 - (player->getYaw());
287                 m_node->setRotation(rot);
288         }
289 }
290
291 void ItemCAO::processMessage(const std::string &data)
292 {
293         dstream<<"ItemCAO: Got data: "<<data<<std::endl;
294         std::istringstream is(data, std::ios::binary);
295         u16 cmd;
296         is>>cmd;
297         if(cmd == 0)
298         {
299                 v3f newpos;
300                 is>>newpos.X;
301                 is>>newpos.Y;
302                 is>>newpos.Z;
303                 m_position = newpos;
304                 updateNodePos();
305         }
306 }
307
308 void ItemCAO::initialize(const std::string &data)
309 {
310         dstream<<"ItemCAO: Got init data: "<<data<<std::endl;
311         
312         Strfnd fn(data);
313
314         v3f newpos;
315         newpos.X = stoi(fn.next(","));
316         newpos.Y = stoi(fn.next(","));
317         newpos.Z = stoi(fn.next(":"));
318         m_position = newpos;
319         updateNodePos();
320
321         m_inventorystring = fn.next("");
322
323         if(m_node == NULL)
324                 return;
325
326         scene::IMesh *mesh = m_node->getMesh();
327
328         if(mesh == NULL)
329                 return;
330         
331         scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
332
333         if(buf == NULL)
334                 return;
335
336         /*
337                 Create an inventory item to see what is its image
338         */
339         std::istringstream is(m_inventorystring, std::ios_base::binary);
340         video::ITexture *texture = NULL;
341         try{
342                 InventoryItem *item = NULL;
343                 item = InventoryItem::deSerialize(is);
344                 dstream<<__FUNCTION_NAME<<": m_inventorystring=\""
345                                 <<m_inventorystring<<"\" -> item="<<item
346                                 <<std::endl;
347                 if(item)
348                 {
349                         texture = item->getImage();
350                         delete item;
351                 }
352         }
353         catch(SerializationError &e)
354         {
355                 dstream<<"WARNING: "<<__FUNCTION_NAME
356                                 <<": error deSerializing inventorystring \""
357                                 <<m_inventorystring<<"\""<<std::endl;
358         }
359         
360         // Set meshbuffer texture
361         buf->getMaterial().setTexture(0, texture);
362         
363 }
364
365