]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/serverenvironment.cpp
[CSM] Add core.get_timeofday & core.get_day_count env calls (#5401)
[dragonfireclient.git] / src / serverenvironment.cpp
index 6229e4cf16d8f0c50e1c0bf941b8af8f6af41cb9..e09c7da16b4435860454a884f908a6cf661718c5 100644 (file)
@@ -28,7 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "profiler.h"
 #include "raycast.h"
 #include "remoteplayer.h"
-#include "scripting_game.h"
+#include "serverscripting.h"
 #include "server.h"
 #include "voxelalgorithms.h"
 #include "util/serialize.h"
@@ -352,11 +352,12 @@ void ActiveBlockList::update(std::vector<v3s16> &active_positions,
 */
 
 ServerEnvironment::ServerEnvironment(ServerMap *map,
-       GameScripting *scriptIface, IGameDef *gamedef,
-       const std::string &path_world) :
+       ServerScripting *scriptIface, Server *server,
+       const std::string &path_world):
+       Environment(server),
        m_map(map),
        m_script(scriptIface),
-       m_gamedef(gamedef),
+       m_server(server),
        m_path_world(path_world),
        m_send_recommended_timer(0),
        m_active_block_interval_overload_skip(0),
@@ -487,7 +488,7 @@ void ServerEnvironment::kickAllPlayers(AccessDeniedCode reason,
        for (std::vector<RemotePlayer *>::iterator it = m_players.begin();
                it != m_players.end(); ++it) {
                RemotePlayer *player = dynamic_cast<RemotePlayer *>(*it);
-               ((Server*)m_gamedef)->DenyAccessVerCompliant(player->peer_id,
+               m_server->DenyAccessVerCompliant(player->peer_id,
                        player->protocol_version, reason, str_reason, reconnect);
        }
 }
@@ -500,8 +501,9 @@ void ServerEnvironment::saveLoadedPlayers()
        for (std::vector<RemotePlayer *>::iterator it = m_players.begin();
                it != m_players.end();
                ++it) {
-               if ((*it)->checkModified()) {
-                       (*it)->save(players_path, m_gamedef);
+               if ((*it)->checkModified() ||
+                       ((*it)->getPlayerSAO() && (*it)->getPlayerSAO()->extendedAttributesModified())) {
+                       (*it)->save(players_path, m_server);
                }
        }
 }
@@ -511,7 +513,7 @@ void ServerEnvironment::savePlayer(RemotePlayer *player)
        std::string players_path = m_path_world + DIR_DELIM "players";
        fs::CreateDir(players_path);
 
-       player->save(players_path, m_gamedef);
+       player->save(players_path, m_server);
 }
 
 RemotePlayer *ServerEnvironment::loadPlayer(const std::string &playername, PlayerSAO *sao)
@@ -523,7 +525,7 @@ RemotePlayer *ServerEnvironment::loadPlayer(const std::string &playername, Playe
 
        RemotePlayer *player = getPlayer(playername.c_str());
        if (!player) {
-               player = new RemotePlayer("", m_gamedef->idef());
+               player = new RemotePlayer("", m_server->idef());
                newplayer = true;
        }
 
@@ -632,7 +634,7 @@ void ServerEnvironment::loadMeta()
        } catch (SettingNotFoundException &e) {
                // No problem, this is expected. Just continue with an empty string
        }
-       m_lbm_mgr.loadIntroductionTimes(lbm_introduction_times, m_gamedef, m_game_time);
+       m_lbm_mgr.loadIntroductionTimes(lbm_introduction_times, m_server, m_game_time);
 
        m_day_count = args.exists("day_count") ?
                args.getU64("day_count") : 0;
@@ -640,7 +642,7 @@ void ServerEnvironment::loadMeta()
 
 void ServerEnvironment::loadDefaultMeta()
 {
-       m_lbm_mgr.loadIntroductionTimes("", m_gamedef, m_game_time);
+       m_lbm_mgr.loadIntroductionTimes("", m_server, m_game_time);
 }
 
 struct ActiveABM
@@ -770,65 +772,65 @@ class ABMHandler
 
                v3s16 p0;
                for(p0.X=0; p0.X<MAP_BLOCKSIZE; p0.X++)
-                       for(p0.Y=0; p0.Y<MAP_BLOCKSIZE; p0.Y++)
-                               for(p0.Z=0; p0.Z<MAP_BLOCKSIZE; p0.Z++)
-                               {
-                                       const MapNode &n = block->getNodeUnsafe(p0);
-                                       content_t c = n.getContent();
+               for(p0.Y=0; p0.Y<MAP_BLOCKSIZE; p0.Y++)
+               for(p0.Z=0; p0.Z<MAP_BLOCKSIZE; p0.Z++)
+               {
+                       const MapNode &n = block->getNodeUnsafe(p0);
+                       content_t c = n.getContent();
 
-                                       if (c >= m_aabms.size() || !m_aabms[c])
-                                               continue;
+                       if (c >= m_aabms.size() || !m_aabms[c])
+                               continue;
 
-                                       v3s16 p = p0 + block->getPosRelative();
-                                       for(std::vector<ActiveABM>::iterator
-                                               i = m_aabms[c]->begin(); i != m_aabms[c]->end(); ++i) {
-                                               if(myrand() % i->chance != 0)
-                                                       continue;
+                       v3s16 p = p0 + block->getPosRelative();
+                       for(std::vector<ActiveABM>::iterator
+                               i = m_aabms[c]->begin(); i != m_aabms[c]->end(); ++i) {
+                               if(myrand() % i->chance != 0)
+                                       continue;
 
-                                               // Check neighbors
-                                               if(!i->required_neighbors.empty())
-                                               {
-                                                       v3s16 p1;
-                                                       for(p1.X = p0.X-1; p1.X <= p0.X+1; p1.X++)
-                                                               for(p1.Y = p0.Y-1; p1.Y <= p0.Y+1; p1.Y++)
-                                                                       for(p1.Z = p0.Z-1; p1.Z <= p0.Z+1; p1.Z++)
-                                                                       {
-                                                                               if(p1 == p0)
-                                                                                       continue;
-                                                                               content_t c;
-                                                                               if (block->isValidPosition(p1)) {
-                                                                                       // if the neighbor is found on the same map block
-                                                                                       // get it straight from there
-                                                                                       const MapNode &n = block->getNodeUnsafe(p1);
-                                                                                       c = n.getContent();
-                                                                               } else {
-                                                                                       // otherwise consult the map
-                                                                                       MapNode n = map->getNodeNoEx(p1 + block->getPosRelative());
-                                                                                       c = n.getContent();
-                                                                               }
-                                                                               std::set<content_t>::const_iterator k;
-                                                                               k = i->required_neighbors.find(c);
-                                                                               if(k != i->required_neighbors.end()){
-                                                                                       goto neighbor_found;
-                                                                               }
-                                                                       }
-                                                       // No required neighbor found
+                               // Check neighbors
+                               if(!i->required_neighbors.empty())
+                               {
+                                       v3s16 p1;
+                                       for(p1.X = p0.X-1; p1.X <= p0.X+1; p1.X++)
+                                       for(p1.Y = p0.Y-1; p1.Y <= p0.Y+1; p1.Y++)
+                                       for(p1.Z = p0.Z-1; p1.Z <= p0.Z+1; p1.Z++)
+                                       {
+                                               if(p1 == p0)
                                                        continue;
+                                               content_t c;
+                                               if (block->isValidPosition(p1)) {
+                                                       // if the neighbor is found on the same map block
+                                                       // get it straight from there
+                                                       const MapNode &n = block->getNodeUnsafe(p1);
+                                                       c = n.getContent();
+                                               } else {
+                                                       // otherwise consult the map
+                                                       MapNode n = map->getNodeNoEx(p1 + block->getPosRelative());
+                                                       c = n.getContent();
                                                }
-                                               neighbor_found:
-
-                                               // Call all the trigger variations
-                                               i->abm->trigger(m_env, p, n);
-                                               i->abm->trigger(m_env, p, n,
-                                                       active_object_count, active_object_count_wider);
-
-                                               // Count surrounding objects again if the abms added any
-                                               if(m_env->m_added_objects > 0) {
-                                                       active_object_count = countObjects(block, map, active_object_count_wider);
-                                                       m_env->m_added_objects = 0;
+                                               std::set<content_t>::const_iterator k;
+                                               k = i->required_neighbors.find(c);
+                                               if(k != i->required_neighbors.end()){
+                                                       goto neighbor_found;
                                                }
                                        }
+                                       // No required neighbor found
+                                       continue;
+                               }
+                               neighbor_found:
+
+                               // Call all the trigger variations
+                               i->abm->trigger(m_env, p, n);
+                               i->abm->trigger(m_env, p, n,
+                                       active_object_count, active_object_count_wider);
+
+                               // Count surrounding objects again if the abms added any
+                               if(m_env->m_added_objects > 0) {
+                                       active_object_count = countObjects(block, map, active_object_count_wider);
+                                       m_env->m_added_objects = 0;
                                }
+                       }
+               }
        }
 };
 
@@ -902,7 +904,7 @@ void ServerEnvironment::addLoadingBlockModifierDef(LoadingBlockModifierDef *lbm)
 
 bool ServerEnvironment::setNode(v3s16 p, const MapNode &n)
 {
-       INodeDefManager *ndef = m_gamedef->ndef();
+       INodeDefManager *ndef = m_server->ndef();
        MapNode n_old = m_map->getNodeNoEx(p);
 
        // Call destructor
@@ -929,7 +931,7 @@ bool ServerEnvironment::setNode(v3s16 p, const MapNode &n)
 
 bool ServerEnvironment::removeNode(v3s16 p)
 {
-       INodeDefManager *ndef = m_gamedef->ndef();
+       INodeDefManager *ndef = m_server->ndef();
        MapNode n_old = m_map->getNodeNoEx(p);
 
        // Call destructor
@@ -1666,7 +1668,7 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
 
        if (objectpos_over_limit(object->getBasePosition())) {
                v3f p = object->getBasePosition();
-               errorstream << "ServerEnvironment::addActiveObjectRaw(): "
+               warningstream << "ServerEnvironment::addActiveObjectRaw(): "
                        << "object position (" << p.X << "," << p.Y << "," << p.Z
                        << ") outside maximum range" << std::endl;
                if (object->environmentDeletes())
@@ -1694,7 +1696,8 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object,
        {
                // Add static object to active static list of the block
                v3f objectpos = object->getBasePosition();
-               std::string staticdata = object->getStaticData();
+               std::string staticdata = "";
+               object->getStaticData(&staticdata);
                StaticObject s_obj(object->getType(), objectpos, staticdata);
                // Add to the block where the object is located in
                v3s16 blockpos = getNodeBlockPos(floatToInt(objectpos, BS));
@@ -1939,11 +1942,14 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s)
 
        If block wasn't generated (not in memory or on disk),
 */
-void ServerEnvironment::deactivateFarObjects(bool force_delete)
+void ServerEnvironment::deactivateFarObjects(bool _force_delete)
 {
        std::vector<u16> objects_to_remove;
        for(ActiveObjectMap::iterator i = m_active_objects.begin();
                i != m_active_objects.end(); ++i) {
+               // force_delete might be overriden per object
+               bool force_delete = _force_delete;
+
                ServerActiveObject* obj = i->second;
                assert(obj);
 
@@ -1980,7 +1986,8 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
                                        <<std::endl;
                                continue;
                        }
-                       std::string staticdata_new = obj->getStaticData();
+                       std::string staticdata_new = "";
+                       obj->getStaticData(&staticdata_new);
                        StaticObject s_obj(obj->getType(), objectpos, staticdata_new);
                        block->m_static_objects.insert(id, s_obj);
                        obj->m_static_block = blockpos_o;
@@ -2020,7 +2027,8 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete)
                if(obj->isStaticAllowed())
                {
                        // Create new static object
-                       std::string staticdata_new = obj->getStaticData();
+                       std::string staticdata_new = "";
+                       obj->getStaticData(&staticdata_new);
                        StaticObject s_obj(obj->getType(), objectpos, staticdata_new);
 
                        bool stays_in_same_block = false;