Translated using Weblate (Chinese (Simplified))
[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                 // Object must be marked as gone when children try to detach
33                 active_object.second = nullptr;
34         }
35         m_active_objects.clear();
36 }
37
38 void ActiveObjectMgr::step(
39                 float dtime, const std::function<void(ClientActiveObject *)> &f)
40 {
41         g_profiler->avg("ActiveObjectMgr: CAO count [#]", m_active_objects.size());
42         for (auto &ao_it : m_active_objects) {
43                 f(ao_it.second);
44         }
45 }
46
47 // clang-format off
48 bool ActiveObjectMgr::registerObject(ClientActiveObject *obj)
49 {
50         assert(obj); // Pre-condition
51         if (obj->getId() == 0) {
52                 u16 new_id = getFreeId();
53                 if (new_id == 0) {
54                         infostream << "Client::ActiveObjectMgr::registerObject(): "
55                                         << "no free id available" << std::endl;
56
57                         delete obj;
58                         return false;
59                 }
60                 obj->setId(new_id);
61         }
62
63         if (!isFreeId(obj->getId())) {
64                 infostream << "Client::ActiveObjectMgr::registerObject(): "
65                                 << "id is not free (" << obj->getId() << ")" << std::endl;
66                 delete obj;
67                 return false;
68         }
69         infostream << "Client::ActiveObjectMgr::registerObject(): "
70                         << "added (id=" << obj->getId() << ")" << std::endl;
71         m_active_objects[obj->getId()] = obj;
72         return true;
73 }
74
75 void ActiveObjectMgr::removeObject(u16 id)
76 {
77         verbosestream << "Client::ActiveObjectMgr::removeObject(): "
78                         << "id=" << id << std::endl;
79         ClientActiveObject *obj = getActiveObject(id);
80         if (!obj) {
81                 infostream << "Client::ActiveObjectMgr::removeObject(): "
82                                 << "id=" << id << " not found" << std::endl;
83                 return;
84         }
85
86         m_active_objects.erase(id);
87
88         obj->removeFromScene(true);
89         delete obj;
90 }
91
92 // clang-format on
93 void ActiveObjectMgr::getActiveObjects(const v3f &origin, f32 max_d,
94                 std::vector<DistanceSortedActiveObject> &dest)
95 {
96         f32 max_d2 = max_d * max_d;
97         for (auto &ao_it : m_active_objects) {
98                 ClientActiveObject *obj = ao_it.second;
99
100                 f32 d2 = (obj->getPosition() - origin).getLengthSQ();
101
102                 if (d2 > max_d2)
103                         continue;
104
105                 dest.emplace_back(obj, d2);
106         }
107 }
108
109 } // namespace client