]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/server.cpp
Limit speed in collisionMoveResult for avoiding hangs
[dragonfireclient.git] / src / server.cpp
index d699dc9d2ab633c0baf0792ee9f5eba21718a947..c4dd0ab0fabd1652ba6a23930305167d967f1eb4 100644 (file)
@@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "server.h"
 #include <iostream>
 #include <queue>
+#include <algorithm>
 #include "clientserver.h"
 #include "map.h"
 #include "jmutexautolock.h"
@@ -58,6 +59,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "util/mathconstants.h"
 #include "rollback.h"
 #include "util/serialize.h"
+#include "defaultsettings.h"
 
 void * ServerThread::Thread()
 {
@@ -651,7 +653,6 @@ Server::Server(
        m_craftdef(createCraftDefManager()),
        m_event(new EventManager()),
        m_thread(this),
-       //m_emergethread(this),
        m_time_of_day_send_timer(0),
        m_uptime(0),
        m_shutdown_requested(false),
@@ -687,9 +688,19 @@ Server::Server(
        infostream<<"- config: "<<m_path_config<<std::endl;
        infostream<<"- game:   "<<m_gamespec.path<<std::endl;
 
+       // Initialize default settings and override defaults with those provided
+       // by the game
+       set_default_settings(g_settings);
+       Settings gamedefaults;
+       getGameMinetestConfig(gamespec.path, gamedefaults);
+       override_default_settings(g_settings, &gamedefaults);
+
        // Create biome definition manager
        m_biomedef = new BiomeDefManager(this);
-
+       
+       // Create emerge manager
+       m_emerge = new EmergeManager(this, m_biomedef);
+       
        // Create rollback manager
        std::string rollback_path = m_path_world+DIR_DELIM+"rollback.txt";
        m_rollback = createRollbackManager(rollback_path, this);
@@ -736,10 +747,10 @@ Server::Server(
        }
        // complain about mods declared to be loaded, but not found
        for(std::vector<ModSpec>::iterator it = m_mods.begin();
-               it != m_mods.end(); ++it)
+                       it != m_mods.end(); ++it)
                load_mod_names.erase((*it).name);
        for(std::list<ModSpec>::iterator it = unsatisfied_mods.begin();
-               it != unsatisfied_mods.end(); ++it)
+                       it != unsatisfied_mods.end(); ++it)
                load_mod_names.erase((*it).name);
        if(!load_mod_names.empty())
        {               
@@ -805,9 +816,6 @@ Server::Server(
        // Add default biomes after nodedef had its aliases added
        m_biomedef->addDefaultBiomes();
 
-       // Create emerge manager
-       m_emerge = new EmergeManager(this, m_biomedef);
-
        // Initialize Environment
        ServerMap *servermap = new ServerMap(path_world, this, m_emerge);
        m_env = new ServerEnvironment(servermap, m_lua, this, this);
@@ -1235,7 +1243,7 @@ void Server::AsyncRunStep()
                float &counter = m_masterserver_timer;
                if((!counter || counter >= 300.0) && g_settings->getBool("server_announce") == true)
                {
-                       ServerList::sendAnnounce(!counter ? "start" : "update", m_clients_number);
+                       ServerList::sendAnnounce(!counter ? "start" : "update", m_clients_number, m_uptime.get(), m_gamespec.id);
                        counter = 0.01;
                }
                counter += dtime;
@@ -2072,7 +2080,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                SendMovement(m_con, peer_id);
 
                // Send item definitions
-               SendItemDef(m_con, peer_id, m_itemdef);
+               SendItemDef(m_con, peer_id, m_itemdef, client->net_proto_version);
 
                // Send node definitions
                SendNodeDef(m_con, peer_id, m_nodedef, client->net_proto_version);
@@ -3334,7 +3342,7 @@ void Server::SendDeathscreen(con::Connection &con, u16 peer_id,
 }
 
 void Server::SendItemDef(con::Connection &con, u16 peer_id,
-               IItemDefManager *itemdef)
+               IItemDefManager *itemdef, u16 protocol_version)
 {
        DSTACK(__FUNCTION_NAME);
        std::ostringstream os(std::ios_base::binary);
@@ -3346,7 +3354,7 @@ void Server::SendItemDef(con::Connection &con, u16 peer_id,
        */
        writeU16(os, TOCLIENT_ITEMDEF);
        std::ostringstream tmp_os(std::ios::binary);
-       itemdef->serialize(tmp_os);
+       itemdef->serialize(tmp_os, protocol_version);
        std::ostringstream tmp_os2(std::ios::binary);
        compressZlib(tmp_os.str(), tmp_os2);
        os<<serializeLongString(tmp_os2.str());
@@ -3466,6 +3474,132 @@ void Server::SendShowFormspecMessage(u16 peer_id, const std::string formspec, co
        m_con.Send(peer_id, 0, data, true);
 }
 
+// Spawns a particle on peer with peer_id
+void Server::SendSpawnParticle(u16 peer_id, v3f pos, v3f velocity, v3f acceleration, float expirationtime, float size, bool collisiondetection, std::string texture)
+{
+       DSTACK(__FUNCTION_NAME);
+
+       std::ostringstream os(std::ios_base::binary);
+       writeU16(os, TOCLIENT_SPAWN_PARTICLE);
+       writeV3F1000(os, pos);
+       writeV3F1000(os, velocity);
+       writeV3F1000(os, acceleration);
+       writeF1000(os, expirationtime);
+       writeF1000(os, size);
+       writeU8(os,  collisiondetection);
+       os<<serializeLongString(texture);
+
+       // Make data buffer
+       std::string s = os.str();
+       SharedBuffer<u8> data((u8*)s.c_str(), s.size());
+       // Send as reliable
+       m_con.Send(peer_id, 0, data, true);
+}
+
+// Spawns a particle on all peers
+void Server::SendSpawnParticleAll(v3f pos, v3f velocity, v3f acceleration, float expirationtime, float size, bool collisiondetection, std::string texture)
+{
+       for(std::map<u16, RemoteClient*>::iterator
+               i = m_clients.begin();
+               i != m_clients.end(); i++)
+       {
+               // Get client and check that it is valid
+               RemoteClient *client = i->second;
+               assert(client->peer_id == i->first);
+               if(client->serialization_version == SER_FMT_VER_INVALID)
+                       continue;
+
+               SendSpawnParticle(client->peer_id, pos, velocity, acceleration,
+                       expirationtime, size, collisiondetection, texture);
+       }
+}
+
+// Adds a ParticleSpawner on peer with peer_id
+void Server::SendAddParticleSpawner(u16 peer_id, u16 amount, float spawntime, v3f minpos, v3f maxpos,
+       v3f minvel, v3f maxvel, v3f minacc, v3f maxacc, float minexptime, float maxexptime,
+       float minsize, float maxsize, bool collisiondetection, std::string texture, u32 id)
+{
+       DSTACK(__FUNCTION_NAME);
+
+       std::ostringstream os(std::ios_base::binary);
+       writeU16(os, TOCLIENT_ADD_PARTICLESPAWNER);
+
+       writeU16(os, amount);
+       writeF1000(os, spawntime);
+       writeV3F1000(os, minpos);
+       writeV3F1000(os, maxpos);
+       writeV3F1000(os, minvel);
+       writeV3F1000(os, maxvel);
+       writeV3F1000(os, minacc);
+       writeV3F1000(os, maxacc);
+       writeF1000(os, minexptime);
+       writeF1000(os, maxexptime);
+       writeF1000(os, minsize);
+       writeF1000(os, maxsize);
+       writeU8(os,  collisiondetection);
+       os<<serializeLongString(texture);
+       writeU32(os, id);
+
+       // Make data buffer
+       std::string s = os.str();
+       SharedBuffer<u8> data((u8*)s.c_str(), s.size());
+       // Send as reliable
+       m_con.Send(peer_id, 0, data, true);
+}
+
+// Adds a ParticleSpawner on all peers
+void Server::SendAddParticleSpawnerAll(u16 amount, float spawntime, v3f minpos, v3f maxpos,
+       v3f minvel, v3f maxvel, v3f minacc, v3f maxacc, float minexptime, float maxexptime,
+       float minsize, float maxsize, bool collisiondetection, std::string texture, u32 id)
+{
+       for(std::map<u16, RemoteClient*>::iterator
+               i = m_clients.begin();
+               i != m_clients.end(); i++)
+       {
+               // Get client and check that it is valid
+               RemoteClient *client = i->second;
+               assert(client->peer_id == i->first);
+               if(client->serialization_version == SER_FMT_VER_INVALID)
+                       continue;
+
+               SendAddParticleSpawner(client->peer_id, amount, spawntime,
+                       minpos, maxpos, minvel, maxvel, minacc, maxacc,
+                       minexptime, maxexptime, minsize, maxsize, collisiondetection, texture, id);
+       }
+}
+
+void Server::SendDeleteParticleSpawner(u16 peer_id, u32 id)
+{
+       DSTACK(__FUNCTION_NAME);
+
+       std::ostringstream os(std::ios_base::binary);
+       writeU16(os, TOCLIENT_DELETE_PARTICLESPAWNER);
+
+       writeU16(os, id);
+
+       // Make data buffer
+       std::string s = os.str();
+       SharedBuffer<u8> data((u8*)s.c_str(), s.size());
+       // Send as reliable
+       m_con.Send(peer_id, 0, data, true);
+}
+
+void Server::SendDeleteParticleSpawnerAll(u32 id)
+{
+       for(std::map<u16, RemoteClient*>::iterator
+               i = m_clients.begin();
+               i != m_clients.end(); i++)
+       {
+               // Get client and check that it is valid
+               RemoteClient *client = i->second;
+               assert(client->peer_id == i->first);
+               if(client->serialization_version == SER_FMT_VER_INVALID)
+                       continue;
+
+               SendDeleteParticleSpawner(client->peer_id, id);
+       }
+}
+
 void Server::BroadcastChatMessage(const std::wstring &message)
 {
        for(std::map<u16, RemoteClient*>::iterator
@@ -4424,6 +4558,111 @@ void Server::notifyPlayers(const std::wstring msg)
        BroadcastChatMessage(msg);
 }
 
+void Server::spawnParticle(const char *playername, v3f pos,
+               v3f velocity, v3f acceleration,
+               float expirationtime, float size, bool
+               collisiondetection, std::string texture)
+{
+       Player *player = m_env->getPlayer(playername);
+       if(!player)
+               return;
+       SendSpawnParticle(player->peer_id, pos, velocity, acceleration,
+                       expirationtime, size, collisiondetection, texture);
+}
+
+void Server::spawnParticleAll(v3f pos, v3f velocity, v3f acceleration,
+               float expirationtime, float size,
+               bool collisiondetection, std::string texture)
+{
+       SendSpawnParticleAll(pos, velocity, acceleration,
+                       expirationtime, size, collisiondetection, texture);
+}
+
+u32 Server::addParticleSpawner(const char *playername,
+               u16 amount, float spawntime,
+               v3f minpos, v3f maxpos,
+               v3f minvel, v3f maxvel,
+               v3f minacc, v3f maxacc,
+               float minexptime, float maxexptime,
+               float minsize, float maxsize,
+               bool collisiondetection, std::string texture)
+{
+       Player *player = m_env->getPlayer(playername);
+       if(!player)
+               return -1;
+
+       u32 id = 0;
+       for(;;) // look for unused particlespawner id
+       {
+               id++;
+               if (std::find(m_particlespawner_ids.begin(),
+                               m_particlespawner_ids.end(), id)
+                               == m_particlespawner_ids.end())
+               {
+                       m_particlespawner_ids.push_back(id);
+                       break;
+               }
+       }
+
+       SendAddParticleSpawner(player->peer_id, amount, spawntime,
+               minpos, maxpos, minvel, maxvel, minacc, maxacc,
+               minexptime, maxexptime, minsize, maxsize,
+               collisiondetection, texture, id);
+
+       return id;
+}
+
+u32 Server::addParticleSpawnerAll(u16 amount, float spawntime,
+               v3f minpos, v3f maxpos,
+               v3f minvel, v3f maxvel,
+               v3f minacc, v3f maxacc,
+               float minexptime, float maxexptime,
+               float minsize, float maxsize,
+               bool collisiondetection, std::string texture)
+{
+       u32 id = 0;
+       for(;;) // look for unused particlespawner id
+       {
+               id++;
+               if (std::find(m_particlespawner_ids.begin(),
+                               m_particlespawner_ids.end(), id)
+                               == m_particlespawner_ids.end())
+               {
+                       m_particlespawner_ids.push_back(id);
+                       break;
+               }
+       }
+
+       SendAddParticleSpawnerAll(amount, spawntime,
+               minpos, maxpos, minvel, maxvel, minacc, maxacc,
+               minexptime, maxexptime, minsize, maxsize,
+               collisiondetection, texture, id);
+
+       return id;
+}
+
+void Server::deleteParticleSpawner(const char *playername, u32 id)
+{
+       Player *player = m_env->getPlayer(playername);
+       if(!player)
+               return;
+
+       m_particlespawner_ids.erase(
+                       std::remove(m_particlespawner_ids.begin(),
+                       m_particlespawner_ids.end(), id),
+                       m_particlespawner_ids.end());
+       SendDeleteParticleSpawner(player->peer_id, id);
+}
+
+void Server::deleteParticleSpawnerAll(u32 id)
+{
+       m_particlespawner_ids.erase(
+                       std::remove(m_particlespawner_ids.begin(),
+                       m_particlespawner_ids.end(), id),
+                       m_particlespawner_ids.end());
+       SendDeleteParticleSpawnerAll(id);
+}
+
 void Server::queueBlockEmerge(v3s16 blockpos, bool allow_generate)
 {
        m_emerge->enqueueBlockEmerge(PEER_ID_INEXISTENT, blockpos, allow_generate);