Add an activeobject manager to hold active objects (#7939)
[oweals/minetest.git] / src / client / 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 "profiler.h"
22 #include "activeobjectmgr.h"
23
24 namespace client
25 {
26
27 void ActiveObjectMgr::clear()
28 {
29         // delete active objects
30         for (auto &active_object : m_active_objects) {
31                 delete active_object.second;
32         }
33 }
34
35 void ActiveObjectMgr::step(
36                 float dtime, const std::function<void(ClientActiveObject *)> &f)
37 {
38         g_profiler->avg("Client::ActiveObjectMgr: num of objects",
39                         m_active_objects.size());
40         for (auto &ao_it : m_active_objects) {
41                 f(ao_it.second);
42         }
43 }
44
45 // clang-format off
46 bool ActiveObjectMgr::registerObject(ClientActiveObject *obj)
47 {
48         assert(obj); // Pre-condition
49         if (obj->getId() == 0) {
50                 u16 new_id = getFreeId();
51                 if (new_id == 0) {
52                         infostream << "Client::ActiveObjectMgr::registerObject(): "
53                                         << "no free id available" << std::endl;
54
55                         delete obj;
56                         return false;
57                 }
58                 obj->setId(new_id);
59         }
60
61         if (!isFreeId(obj->getId())) {
62                 infostream << "Client::ActiveObjectMgr::registerObject(): "
63                                 << "id is not free (" << obj->getId() << ")" << std::endl;
64                 delete obj;
65                 return false;
66         }
67         infostream << "Client::ActiveObjectMgr::registerObject(): "
68                         << "added (id=" << obj->getId() << ")" << std::endl;
69         m_active_objects[obj->getId()] = obj;
70         return true;
71 }
72
73 void ActiveObjectMgr::removeObject(u16 id)
74 {
75         verbosestream << "Client::ActiveObjectMgr::removeObject(): "
76                         << "id=" << id << std::endl;
77         ClientActiveObject *obj = getActiveObject(id);
78         if (!obj) {
79                 infostream << "Client::ActiveObjectMgr::removeObject(): "
80                                 << "id=" << id << " not found" << std::endl;
81                 return;
82         }
83
84         m_active_objects.erase(id);
85
86         obj->removeFromScene(true);
87         delete obj;
88 }
89
90 // clang-format on
91 void ActiveObjectMgr::getActiveObjects(const v3f &origin, f32 max_d,
92                 std::vector<DistanceSortedActiveObject> &dest)
93 {
94         for (auto &ao_it : m_active_objects) {
95                 ClientActiveObject *obj = ao_it.second;
96
97                 f32 d = (obj->getPosition() - origin).getLength();
98
99                 if (d > max_d)
100                         continue;
101
102                 dest.emplace_back(obj, d);
103         }
104 }
105
106 } // namespace client