]> git.lizzy.rs Git - minetest.git/blobdiff - src/client.cpp
Allow the LUA API to set animations to meshes as well as the animation speed. Also...
[minetest.git] / src / client.cpp
index 7f92e5176f2b76203777f2aac3ef7fb85b808d7a..7f5421747c75da1c4dc3bc1745440852dc1197de 100644 (file)
@@ -3,22 +3,21 @@ Minetest-c55
 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
 
 This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
+GNU Lesser General Public License for more details.
 
-You should have received a copy of the GNU General Public License along
+You should have received a copy of the GNU Lesser General Public License along
 with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
 #include "client.h"
-#include "utility.h"
 #include <iostream>
 #include "clientserver.h"
 #include "jmutexautolock.h"
@@ -40,8 +39,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "clientmap.h"
 #include "filecache.h"
 #include "sound.h"
-#include "utility_string.h"
+#include "util/string.h"
 #include "hex.h"
+#include "IMeshCache.h"
 
 static std::string getMediaCacheDir()
 {
@@ -305,6 +305,15 @@ Client::~Client()
                sleep_ms(100);
 
        delete m_inventory_from_server;
+
+       // Delete detached inventories
+       {
+               for(std::map<std::string, Inventory*>::iterator
+                               i = m_detached_inventories.begin();
+                               i != m_detached_inventories.end(); i++){
+                       delete i->second;
+               }
+       }
 }
 
 void Client::connect(Address address)
@@ -812,8 +821,8 @@ bool Client::loadMedia(const std::string &data, const std::string &filename)
        name = removeStringEnd(filename, image_ext);
        if(name != "")
        {
-               verbosestream<<"Client: Attempting to load image "
-                               <<"file \""<<filename<<"\""<<std::endl;
+               verbosestream<<"Client: Storing image into Irrlicht: "
+                               <<"\""<<filename<<"\""<<std::endl;
 
                io::IFileSystem *irrfs = m_device->getFileSystem();
                video::IVideoDriver *vdrv = m_device->getVideoDriver();
@@ -846,12 +855,34 @@ bool Client::loadMedia(const std::string &data, const std::string &filename)
        name = removeStringEnd(filename, sound_ext);
        if(name != "")
        {
-               verbosestream<<"Client: Attempting to load sound "
-                               <<"file \""<<filename<<"\""<<std::endl;
+               verbosestream<<"Client: Storing sound into Irrlicht: "
+                               <<"\""<<filename<<"\""<<std::endl;
                m_sound->loadSoundData(name, data);
                return true;
        }
 
+       const char *model_ext[] = {
+               ".x", ".b3d", ".md2", ".obj",
+               NULL
+       };
+       name = removeStringEnd(filename, model_ext);
+       if(name != "")
+       {
+               verbosestream<<"Client: Storing model into Irrlicht: "
+                               <<"\""<<filename<<"\""<<std::endl;
+
+               io::IFileSystem *irrfs = m_device->getFileSystem();
+               io::IReadFile *rfile = irrfs->createMemoryReadFile(
+                               *data_rw, data_rw.getSize(), filename.c_str());
+               assert(rfile);
+               
+               scene::ISceneManager *smgr = m_device->getSceneManager();
+               scene::IAnimatedMesh *mesh = smgr->getMesh(rfile);
+               smgr->getMeshCache()->addMesh(filename.c_str(), mesh);
+               
+               return true;
+       }
+
        errorstream<<"Client: Don't know how to load file \""
                        <<filename<<"\""<<std::endl;
        return false;
@@ -1689,6 +1720,34 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
                }
                infostream<<std::endl;
        }
+       else if(command == TOCLIENT_INVENTORY_FORMSPEC)
+       {
+               std::string datastring((char*)&data[2], datasize-2);
+               std::istringstream is(datastring, std::ios_base::binary);
+
+               // Store formspec in LocalPlayer
+               Player *player = m_env.getLocalPlayer();
+               assert(player != NULL);
+               player->inventory_formspec = deSerializeLongString(is);
+       }
+       else if(command == TOCLIENT_DETACHED_INVENTORY)
+       {
+               std::string datastring((char*)&data[2], datasize-2);
+               std::istringstream is(datastring, std::ios_base::binary);
+
+               std::string name = deSerializeString(is);
+               
+               infostream<<"Client: Detached inventory update: \""<<name<<"\""<<std::endl;
+
+               Inventory *inv = NULL;
+               if(m_detached_inventories.count(name) > 0)
+                       inv = m_detached_inventories[name];
+               else{
+                       inv = new Inventory(m_itemdef);
+                       m_detached_inventories[name] = inv;
+               }
+               inv->deSerialize(is);
+       }
        else
        {
                infostream<<"Client: Ignoring unknown command "
@@ -1740,33 +1799,23 @@ void Client::interact(u8 action, const PointedThing& pointed)
        Send(0, data, true);
 }
 
-void Client::sendSignNodeText(v3s16 p, std::string text)
+void Client::sendNodemetaFields(v3s16 p, const std::string &formname,
+               const std::map<std::string, std::string> &fields)
 {
-       /*
-               u16 command
-               v3s16 p
-               u16 textlen
-               textdata
-       */
        std::ostringstream os(std::ios_base::binary);
-       u8 buf[12];
-       
-       // Write command
-       writeU16(buf, TOSERVER_SIGNNODETEXT);
-       os.write((char*)buf, 2);
-       
-       // Write p
-       writeV3S16(buf, p);
-       os.write((char*)buf, 6);
 
-       u16 textlen = text.size();
-       // Write text length
-       writeS16(buf, textlen);
-       os.write((char*)buf, 2);
+       writeU16(os, TOSERVER_NODEMETA_FIELDS);
+       writeV3S16(os, p);
+       os<<serializeString(formname);
+       writeU16(os, fields.size());
+       for(std::map<std::string, std::string>::const_iterator
+                       i = fields.begin(); i != fields.end(); i++){
+               const std::string &name = i->first;
+               const std::string &value = i->second;
+               os<<serializeString(name);
+               os<<serializeLongString(value);
+       }
 
-       // Write text
-       os.write((char*)text.c_str(), textlen);
-       
        // Make data buffer
        std::string s = os.str();
        SharedBuffer<u8> data((u8*)s.c_str(), s.size());
@@ -1774,6 +1823,29 @@ void Client::sendSignNodeText(v3s16 p, std::string text)
        Send(0, data, true);
 }
        
+void Client::sendInventoryFields(const std::string &formname, 
+               const std::map<std::string, std::string> &fields)
+{
+       std::ostringstream os(std::ios_base::binary);
+
+       writeU16(os, TOSERVER_INVENTORY_FIELDS);
+       os<<serializeString(formname);
+       writeU16(os, fields.size());
+       for(std::map<std::string, std::string>::const_iterator
+                       i = fields.begin(); i != fields.end(); i++){
+               const std::string &name = i->first;
+               const std::string &value = i->second;
+               os<<serializeString(name);
+               os<<serializeLongString(value);
+       }
+
+       // Make data buffer
+       std::string s = os.str();
+       SharedBuffer<u8> data((u8*)s.c_str(), s.size());
+       // Send as reliable
+       Send(0, data, true);
+}
+
 void Client::sendInventoryAction(InventoryAction *a)
 {
        std::ostringstream os(std::ios_base::binary);
@@ -2068,6 +2140,13 @@ Inventory* Client::getInventory(const InventoryLocation &loc)
                return meta->getInventory();
        }
        break;
+       case InventoryLocation::DETACHED:
+       {
+               if(m_detached_inventories.count(loc.name) == 0)
+                       return NULL;
+               return m_detached_inventories[loc.name];
+       }
+       break;
        default:
                assert(0);
        }
@@ -2348,32 +2427,41 @@ ClientEvent Client::getClientEvent()
 
 void Client::afterContentReceived()
 {
+       verbosestream<<"Client::afterContentReceived() started"<<std::endl;
        assert(m_itemdef_received);
        assert(m_nodedef_received);
        assert(m_media_received);
-
+       
        // remove the information about which checksum each texture
        // ought to have
        m_media_name_sha1_map.clear();
 
        // Rebuild inherited images and recreate textures
+       verbosestream<<"Rebuilding images and textures"<<std::endl;
        m_tsrc->rebuildImagesAndTextures();
 
        // Update texture atlas
+       verbosestream<<"Updating texture atlas"<<std::endl;
        if(g_settings->getBool("enable_texture_atlas"))
                m_tsrc->buildMainAtlas(this);
 
        // Update node aliases
+       verbosestream<<"Updating node aliases"<<std::endl;
        m_nodedef->updateAliases(m_itemdef);
 
        // Update node textures
+       verbosestream<<"Updating node textures"<<std::endl;
        m_nodedef->updateTextures(m_tsrc);
 
        // Update item textures and meshes
+       verbosestream<<"Updating item textures and meshes"<<std::endl;
        m_itemdef->updateTexturesAndMeshes(this);
 
        // Start mesh update thread after setting up content definitions
+       verbosestream<<"Starting mesh update thread"<<std::endl;
        m_mesh_update_thread.Start();
+       
+       verbosestream<<"Client::afterContentReceived() done"<<std::endl;
 }
 
 float Client::getRTT(void)