3 Copyright (C) 2010-2018 nerzhul, Loic BLOT <loic.blot@unix-experience.fr>
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.
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.
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.
23 #include "activeobjectmgr.h"
28 void ActiveObjectMgr::clear(const std::function<bool(ServerActiveObject *, u16)> &cb)
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);
38 // Remove references from m_active_objects
39 for (u16 i : objects_to_remove) {
40 m_active_objects.erase(i);
44 void ActiveObjectMgr::step(
45 float dtime, const std::function<void(ServerActiveObject *)> &f)
47 g_profiler->avg("Server::ActiveObjectMgr: num of objects",
48 m_active_objects.size());
49 for (auto &ao_it : m_active_objects) {
55 bool ActiveObjectMgr::registerObject(ServerActiveObject *obj)
57 assert(obj); // Pre-condition
58 if (obj->getId() == 0) {
59 u16 new_id = getFreeId();
61 errorstream << "Server::ActiveObjectMgr::addActiveObjectRaw(): "
62 << "no free id available" << std::endl;
63 if (obj->environmentDeletes())
69 verbosestream << "Server::ActiveObjectMgr::addActiveObjectRaw(): "
70 << "supplied with id " << obj->getId() << std::endl;
73 if (!isFreeId(obj->getId())) {
74 errorstream << "Server::ActiveObjectMgr::addActiveObjectRaw(): "
75 << "id is not free (" << obj->getId() << ")" << std::endl;
76 if (obj->environmentDeletes())
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())
91 m_active_objects[obj->getId()] = obj;
93 verbosestream << "Server::ActiveObjectMgr::addActiveObjectRaw(): "
94 << "Added id=" << obj->getId() << "; there are now "
95 << m_active_objects.size() << " active objects." << std::endl;
99 void ActiveObjectMgr::removeObject(u16 id)
101 verbosestream << "Server::ActiveObjectMgr::removeObject(): "
102 << "id=" << id << std::endl;
103 ServerActiveObject *obj = getActiveObject(id);
105 infostream << "Server::ActiveObjectMgr::removeObject(): "
106 << "id=" << id << " not found" << std::endl;
110 m_active_objects.erase(id);
115 void ActiveObjectMgr::getObjectsInsideRadius(
116 const v3f &pos, float radius, std::vector<u16> &result)
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)
124 result.push_back(id);
128 void ActiveObjectMgr::getAddedActiveObjectsAroundPos(const v3f &player_pos, f32 radius,
129 f32 player_radius, std::set<u16> ¤t_objects,
130 std::queue<u16> &added_objects)
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
139 for (auto &ao_it : m_active_objects) {
140 u16 id = ao_it.first;
143 ServerActiveObject *object = ao_it.second;
147 if (object->isGone())
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)
155 } else if (distance_f > radius)
158 // Discard if already on current_objects
159 auto n = current_objects.find(id);
160 if (n != current_objects.end())
162 // Add to added_objects
163 added_objects.push(id);
167 } // namespace server