]> git.lizzy.rs Git - dragonfireclient.git/blob - src/clientobject.cpp
901b3d072cce3039b64cc5cf736c1999213b3150
[dragonfireclient.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         if(m_node == NULL)
262                 return;
263
264         u8 li = decode_light(light_at_pos);
265         video::SColor color(255,li,li,li);
266
267         scene::IMesh *mesh = m_node->getMesh();
268         if(mesh == NULL)
269                 return;
270         
271         u16 mc = mesh->getMeshBufferCount();
272         for(u16 j=0; j<mc; j++)
273         {
274                 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
275                 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
276                 u16 vc = buf->getVertexCount();
277                 for(u16 i=0; i<vc; i++)
278                 {
279                         vertices[i].Color = color;
280                 }
281         }
282 }
283
284 v3s16 ItemCAO::getLightPosition()
285 {
286         return floatToInt(m_position, BS);
287 }
288
289 void ItemCAO::updateNodePos()
290 {
291         if(m_node == NULL)
292                 return;
293
294         m_node->setPosition(m_position);
295 }
296
297 void ItemCAO::step(float dtime, ClientEnvironment *env)
298 {
299         if(m_node)
300         {
301                 /*v3f rot = m_node->getRotation();
302                 rot.Y += dtime * 120;
303                 m_node->setRotation(rot);*/
304                 LocalPlayer *player = env->getLocalPlayer();
305                 assert(player);
306                 v3f rot = m_node->getRotation();
307                 rot.Y = 180.0 - (player->getYaw());
308                 m_node->setRotation(rot);
309         }
310 }
311
312 void ItemCAO::processMessage(const std::string &data)
313 {
314         dstream<<"ItemCAO: Got message"<<std::endl;
315         std::istringstream is(data, std::ios::binary);
316         char buf[4];
317         // command
318         is.read(buf, 1);
319         u8 cmd = buf[0];
320         if(cmd == 0)
321         {
322                 // pos
323                 is.read(buf, 4);
324                 m_position.X = (float)readS32((u8*)buf)/1000.0;
325                 is.read(buf, 4);
326                 m_position.Y = (float)readS32((u8*)buf)/1000.0;
327                 is.read(buf, 4);
328                 m_position.Z = (float)readS32((u8*)buf)/1000.0;
329                 updateNodePos();
330         }
331 }
332
333 void ItemCAO::initialize(const std::string &data)
334 {
335         dstream<<"ItemCAO: Got init data"<<std::endl;
336         
337         {
338                 std::istringstream is(data, std::ios::binary);
339                 char buf[4];
340                 // version
341                 is.read(buf, 1);
342                 u8 version = buf[0];
343                 // check version
344                 if(version != 0)
345                         return;
346                 // pos
347                 is.read(buf, 4);
348                 m_position.X = (float)readS32((u8*)buf)/1000.0;
349                 is.read(buf, 4);
350                 m_position.Y = (float)readS32((u8*)buf)/1000.0;
351                 is.read(buf, 4);
352                 m_position.Z = (float)readS32((u8*)buf)/1000.0;
353                 // inventorystring
354                 m_inventorystring = deSerializeString(is);
355         }
356         
357         updateNodePos();
358
359         /*
360                 Update image of node
361         */
362
363         if(m_node == NULL)
364                 return;
365
366         scene::IMesh *mesh = m_node->getMesh();
367
368         if(mesh == NULL)
369                 return;
370         
371         scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
372
373         if(buf == NULL)
374                 return;
375
376         // Create an inventory item to see what is its image
377         std::istringstream is(m_inventorystring, std::ios_base::binary);
378         video::ITexture *texture = NULL;
379         try{
380                 InventoryItem *item = NULL;
381                 item = InventoryItem::deSerialize(is);
382                 dstream<<__FUNCTION_NAME<<": m_inventorystring=\""
383                                 <<m_inventorystring<<"\" -> item="<<item
384                                 <<std::endl;
385                 if(item)
386                 {
387                         texture = item->getImage();
388                         delete item;
389                 }
390         }
391         catch(SerializationError &e)
392         {
393                 dstream<<"WARNING: "<<__FUNCTION_NAME
394                                 <<": error deSerializing inventorystring \""
395                                 <<m_inventorystring<<"\""<<std::endl;
396         }
397         
398         // Set meshbuffer texture
399         buf->getMaterial().setTexture(0, texture);
400         
401 }
402
403