]> git.lizzy.rs Git - minetest.git/blobdiff - src/client.cpp
Add shutdown hook interface to Lua API
[minetest.git] / src / client.cpp
index de3c636988aa0d49f5f37eb75120b25748bf639f..4117a91306a655f923d01703266c9e912f160784 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,10 @@ 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"
+#include "util/serialize.h"
 
 static std::string getMediaCacheDir()
 {
@@ -266,6 +267,7 @@ Client::Client(
        m_time_of_day_set(false),
        m_last_time_of_day_f(-1),
        m_time_of_day_update_timer(0),
+       m_recommended_send_interval(0.1),
        m_removed_sounds_check_timer(0)
 {
        m_packetcounter_timer = 0.0;
@@ -305,6 +307,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)
@@ -490,8 +501,9 @@ void Client::step(float dtime)
                        // [2] u8 SER_FMT_VER_HIGHEST
                        // [3] u8[20] player_name
                        // [23] u8[28] password (new in some version)
-                       // [51] u16 client network protocol version (new in some version)
-                       SharedBuffer<u8> data(2+1+PLAYERNAME_SIZE+PASSWORD_SIZE+2);
+                       // [51] u16 minimum supported network protocol version (added sometime)
+                       // [53] u16 maximum supported network protocol version (added later than the previous one)
+                       SharedBuffer<u8> data(2+1+PLAYERNAME_SIZE+PASSWORD_SIZE+2+2);
                        writeU16(&data[0], TOSERVER_INIT);
                        writeU8(&data[2], SER_FMT_VER_HIGHEST);
 
@@ -504,8 +516,8 @@ void Client::step(float dtime)
                        memset((char*)&data[23], 0, PASSWORD_SIZE);
                        snprintf((char*)&data[23], PASSWORD_SIZE, "%s", m_password.c_str());
                        
-                       // This should be incremented in each version
-                       writeU16(&data[51], PROTOCOL_VERSION);
+                       writeU16(&data[51], CLIENT_PROTOCOL_VERSION_MIN);
+                       writeU16(&data[53], CLIENT_PROTOCOL_VERSION_MAX);
 
                        // Send as unreliable
                        Send(0, data, false);
@@ -647,7 +659,7 @@ void Client::step(float dtime)
        {
                float &counter = m_playerpos_send_timer;
                counter += dtime;
-               if(counter >= 0.2)
+               if(counter >= m_recommended_send_interval)
                {
                        counter = 0.0;
                        sendPlayerPos();
@@ -813,7 +825,7 @@ bool Client::loadMedia(const std::string &data, const std::string &filename)
        if(name != "")
        {
                verbosestream<<"Client: Attempting to load image "
-                               <<"file \""<<filename<<"\""<<std::endl;
+               <<"file \""<<filename<<"\""<<std::endl;
 
                io::IFileSystem *irrfs = m_device->getFileSystem();
                video::IVideoDriver *vdrv = m_device->getVideoDriver();
@@ -847,11 +859,33 @@ bool Client::loadMedia(const std::string &data, const std::string &filename)
        if(name != "")
        {
                verbosestream<<"Client: Attempting to load sound "
-                               <<"file \""<<filename<<"\""<<std::endl;
+               <<"file \""<<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;
@@ -989,6 +1023,14 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
                        m_map_seed = readU64(&data[2+1+6]);
                        infostream<<"Client: received map seed: "<<m_map_seed<<std::endl;
                }
+
+               if(datasize >= 2+1+6+8+4)
+               {
+                       // Get map seed
+                       m_recommended_send_interval = readF1000(&data[2+1+6+8]);
+                       infostream<<"Client: received recommended send interval "
+                                       <<m_recommended_send_interval<<std::endl;
+               }
                
                // Reply to server
                u32 replysize = 2;
@@ -1639,10 +1681,10 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
                int client_id = -1;
                switch(type){
                case 0: // local
-                       client_id = m_sound->playSound(name, false, gain);
+                       client_id = m_sound->playSound(name, loop, gain);
                        break;
                case 1: // positional
-                       client_id = m_sound->playSoundAt(name, false, gain, pos);
+                       client_id = m_sound->playSoundAt(name, loop, gain, pos);
                        break;
                case 2: { // object
                        ClientActiveObject *cao = m_env.getActiveObject(object_id);
@@ -1689,6 +1731,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 +1810,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 +1834,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);
@@ -1912,7 +1995,7 @@ void Client::sendPlayerPos()
        v3s32 speed(sf.X*100, sf.Y*100, sf.Z*100);
        s32 pitch = myplayer->getPitch() * 100;
        s32 yaw = myplayer->getYaw() * 100;
-
+       u32 keyPressed=myplayer->keyPressed;
        /*
                Format:
                [0] u16 command
@@ -1920,15 +2003,15 @@ void Client::sendPlayerPos()
                [2+12] v3s32 speed*100
                [2+12+12] s32 pitch*100
                [2+12+12+4] s32 yaw*100
+               [2+12+12+4+4] u32 keyPressed
        */
-
-       SharedBuffer<u8> data(2+12+12+4+4);
+       SharedBuffer<u8> data(2+12+12+4+4+4);
        writeU16(&data[0], TOSERVER_PLAYERPOS);
        writeV3S32(&data[2], position);
        writeV3S32(&data[2+12], speed);
        writeS32(&data[2+12+12], pitch);
-       writeS32(&data[2+12+12+4], yaw);
-
+       writeS32(&data[2+12+12+4], yaw);        
+       writeU32(&data[2+12+12+4+4], keyPressed);
        // Send as unreliable
        Send(0, data, false);
 }
@@ -2068,6 +2151,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 +2438,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)