]> git.lizzy.rs Git - minetest.git/blob - src/staticobject.h
Replace minetest_game with "Minetest Game" where appropriate
[minetest.git] / src / staticobject.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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 #pragma once
21
22 #include "irrlichttypes_bloated.h"
23 #include <string>
24 #include <sstream>
25 #include <vector>
26 #include <map>
27 #include "debug.h"
28
29 class ServerActiveObject;
30
31 struct StaticObject
32 {
33         u8 type = 0;
34         v3f pos;
35         std::string data;
36
37         StaticObject() = default;
38         StaticObject(const ServerActiveObject *s_obj, const v3f &pos_);
39
40         void serialize(std::ostream &os) const;
41         void deSerialize(std::istream &is, u8 version);
42 };
43
44 class StaticObjectList
45 {
46 public:
47         /*
48                 Inserts an object to the container.
49                 Id must be unique (active) or 0 (stored).
50         */
51         void insert(u16 id, const StaticObject &obj)
52         {
53                 if (id == 0) {
54                         m_stored.push_back(obj);
55                 } else {
56                         if (m_active.find(id) != m_active.end()) {
57                                 dstream << "ERROR: StaticObjectList::insert(): "
58                                                 << "id already exists" << std::endl;
59                                 FATAL_ERROR("StaticObjectList::insert()");
60                         }
61                         setActive(id, obj);
62                 }
63         }
64
65         void remove(u16 id)
66         {
67                 assert(id != 0); // Pre-condition
68                 if (m_active.find(id) == m_active.end()) {
69                         warningstream << "StaticObjectList::remove(): id=" << id << " not found"
70                                                   << std::endl;
71                         return;
72                 }
73                 m_active.erase(id);
74         }
75
76         void serialize(std::ostream &os);
77         void deSerialize(std::istream &is);
78
79         // Never permit to modify outside of here. Only this object is responsible of m_stored and m_active modifications
80         const std::vector<StaticObject>& getAllStored() const { return m_stored; }
81         const std::map<u16, StaticObject> &getAllActives() const { return m_active; }
82
83         inline void setActive(u16 id, const StaticObject &obj) { m_active[id] = obj; }
84         inline size_t getActiveSize() const { return m_active.size(); }
85         inline size_t getStoredSize() const { return m_stored.size(); }
86         inline void clearStored() { m_stored.clear(); }
87         void pushStored(const StaticObject &obj) { m_stored.push_back(obj); }
88
89         bool storeActiveObject(u16 id);
90
91         inline void clear()
92         {
93                 m_active.clear();
94                 m_stored.clear();
95         }
96
97         inline size_t size()
98         {
99                 return m_active.size() + m_stored.size();
100         }
101
102 private:
103         /*
104                 NOTE: When an object is transformed to active, it is removed
105                 from m_stored and inserted to m_active.
106         */
107         std::vector<StaticObject> m_stored;
108         std::map<u16, StaticObject> m_active;
109 };