]> git.lizzy.rs Git - dragonfireclient.git/blob - src/server/activeobjectmgr.cpp
Overall improvements to log messages (#9598)
[dragonfireclient.git] / src / server / activeobjectmgr.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2018 nerzhul, Loic BLOT <loic.blot@unix-experience.fr>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser 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 <log.h>
21 #include "mapblock.h"
22 #include "profiler.h"
23 #include "activeobjectmgr.h"
24
25 namespace server
26 {
27
28 void ActiveObjectMgr::clear(const std::function<bool(ServerActiveObject *, u16)> &cb)
29 {
30         std::vector<u16> objects_to_remove;
31         for (auto &it : m_active_objects) {
32                 if (cb(it.second, it.first)) {
33                         // Id to be removed from m_active_objects
34                         objects_to_remove.push_back(it.first);
35                 }
36         }
37
38         // Remove references from m_active_objects
39         for (u16 i : objects_to_remove) {
40                 m_active_objects.erase(i);
41         }
42 }
43
44 void ActiveObjectMgr::step(
45                 float dtime, const std::function<void(ServerActiveObject *)> &f)
46 {
47         g_profiler->avg("ActiveObjectMgr: SAO count [#]", m_active_objects.size());
48         for (auto &ao_it : m_active_objects) {
49                 f(ao_it.second);
50         }
51 }
52
53 // clang-format off
54 bool ActiveObjectMgr::registerObject(ServerActiveObject *obj)
55 {
56         assert(obj); // Pre-condition
57         if (obj->getId() == 0) {
58                 u16 new_id = getFreeId();
59                 if (new_id == 0) {
60                         errorstream << "Server::ActiveObjectMgr::addActiveObjectRaw(): "
61                                         << "no free id available" << std::endl;
62                         if (obj->environmentDeletes())
63                                 delete obj;
64                         return false;
65                 }
66                 obj->setId(new_id);
67         } else {
68                 verbosestream << "Server::ActiveObjectMgr::addActiveObjectRaw(): "
69                                 << "supplied with id " << obj->getId() << std::endl;
70         }
71
72         if (!isFreeId(obj->getId())) {
73                 errorstream << "Server::ActiveObjectMgr::addActiveObjectRaw(): "
74                                 << "id is not free (" << obj->getId() << ")" << std::endl;
75                 if (obj->environmentDeletes())
76                         delete obj;
77                 return false;
78         }
79
80         if (objectpos_over_limit(obj->getBasePosition())) {
81                 v3f p = obj->getBasePosition();
82                 warningstream << "Server::ActiveObjectMgr::addActiveObjectRaw(): "
83                                 << "object position (" << p.X << "," << p.Y << "," << p.Z
84                                 << ") outside maximum range" << std::endl;
85                 if (obj->environmentDeletes())
86                         delete obj;
87                 return false;
88         }
89
90         m_active_objects[obj->getId()] = obj;
91
92         verbosestream << "Server::ActiveObjectMgr::addActiveObjectRaw(): "
93                         << "Added id=" << obj->getId() << "; there are now "
94                         << m_active_objects.size() << " active objects." << std::endl;
95         return true;
96 }
97
98 void ActiveObjectMgr::removeObject(u16 id)
99 {
100         verbosestream << "Server::ActiveObjectMgr::removeObject(): "
101                         << "id=" << id << std::endl;
102         ServerActiveObject *obj = getActiveObject(id);
103         if (!obj) {
104                 infostream << "Server::ActiveObjectMgr::removeObject(): "
105                                 << "id=" << id << " not found" << std::endl;
106                 return;
107         }
108
109         m_active_objects.erase(id);
110         delete obj;
111 }
112
113 // clang-format on
114 void ActiveObjectMgr::getObjectsInsideRadius(
115                 const v3f &pos, float radius, std::vector<u16> &result)
116 {
117         float r2 = radius * radius;
118         for (auto &activeObject : m_active_objects) {
119                 ServerActiveObject *obj = activeObject.second;
120                 u16 id = activeObject.first;
121                 const v3f &objectpos = obj->getBasePosition();
122                 if (objectpos.getDistanceFromSQ(pos) > r2)
123                         continue;
124                 result.push_back(id);
125         }
126 }
127
128 void ActiveObjectMgr::getAddedActiveObjectsAroundPos(const v3f &player_pos, f32 radius,
129                 f32 player_radius, std::set<u16> &current_objects,
130                 std::queue<u16> &added_objects)
131 {
132         /*
133                 Go through the object list,
134                 - discard removed/deactivated objects,
135                 - discard objects that are too far away,
136                 - discard objects that are found in current_objects.
137                 - add remaining objects to added_objects
138         */
139         for (auto &ao_it : m_active_objects) {
140                 u16 id = ao_it.first;
141
142                 // Get object
143                 ServerActiveObject *object = ao_it.second;
144                 if (!object)
145                         continue;
146
147                 if (object->isGone())
148                         continue;
149
150                 f32 distance_f = object->getBasePosition().getDistanceFrom(player_pos);
151                 if (object->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
152                         // Discard if too far
153                         if (distance_f > player_radius && player_radius != 0)
154                                 continue;
155                 } else if (distance_f > radius)
156                         continue;
157
158                 // Discard if already on current_objects
159                 auto n = current_objects.find(id);
160                 if (n != current_objects.end())
161                         continue;
162                 // Add to added_objects
163                 added_objects.push(id);
164         }
165 }
166
167 } // namespace server