Add an activeobject manager to hold active objects (#7939)
[oweals/minetest.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("Server::ActiveObjectMgr: num of objects",
48                         m_active_objects.size());
49         for (auto &ao_it : m_active_objects) {
50                 f(ao_it.second);
51         }
52 }
53
54 // clang-format off
55 bool ActiveObjectMgr::registerObject(ServerActiveObject *obj)
56 {
57         assert(obj); // Pre-condition
58         if (obj->getId() == 0) {
59                 u16 new_id = getFreeId();
60                 if (new_id == 0) {
61                         errorstream << "Server::ActiveObjectMgr::addActiveObjectRaw(): "
62                                         << "no free id available" << std::endl;
63                         if (obj->environmentDeletes())
64                                 delete obj;
65                         return false;
66                 }
67                 obj->setId(new_id);
68         } else {
69                 verbosestream << "Server::ActiveObjectMgr::addActiveObjectRaw(): "
70                                 << "supplied with id " << obj->getId() << std::endl;
71         }
72
73         if (!isFreeId(obj->getId())) {
74                 errorstream << "Server::ActiveObjectMgr::addActiveObjectRaw(): "
75                                 << "id is not free (" << obj->getId() << ")" << std::endl;
76                 if (obj->environmentDeletes())
77                         delete obj;
78                 return false;
79         }
80
81         if (objectpos_over_limit(obj->getBasePosition())) {
82                 v3f p = obj->getBasePosition();
83                 warningstream << "Server::ActiveObjectMgr::addActiveObjectRaw(): "
84                                 << "object position (" << p.X << "," << p.Y << "," << p.Z
85                                 << ") outside maximum range" << std::endl;
86                 if (obj->environmentDeletes())
87                         delete obj;
88                 return false;
89         }
90
91         m_active_objects[obj->getId()] = obj;
92
93         verbosestream << "Server::ActiveObjectMgr::addActiveObjectRaw(): "
94                         << "Added id=" << obj->getId() << "; there are now "
95                         << m_active_objects.size() << " active objects." << std::endl;
96         return true;
97 }
98
99 void ActiveObjectMgr::removeObject(u16 id)
100 {
101         verbosestream << "Server::ActiveObjectMgr::removeObject(): "
102                         << "id=" << id << std::endl;
103         ServerActiveObject *obj = getActiveObject(id);
104         if (!obj) {
105                 infostream << "Server::ActiveObjectMgr::removeObject(): "
106                                 << "id=" << id << " not found" << std::endl;
107                 return;
108         }
109
110         m_active_objects.erase(id);
111         delete obj;
112 }
113
114 // clang-format on
115 void ActiveObjectMgr::getObjectsInsideRadius(
116                 const v3f &pos, float radius, std::vector<u16> &result)
117 {
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.getDistanceFrom(pos) > radius)
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