Add scroll_container formspec element (redo) (#9101)
[oweals/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);
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                 {
55                         m_stored.push_back(obj);
56                 }
57                 else
58                 {
59                         if(m_active.find(id) != m_active.end())
60                         {
61                                 dstream<<"ERROR: StaticObjectList::insert(): "
62                                                 <<"id already exists"<<std::endl;
63                                 FATAL_ERROR("StaticObjectList::insert()");
64                         }
65                         m_active[id] = obj;
66                 }
67         }
68
69         void remove(u16 id)
70         {
71                 assert(id != 0); // Pre-condition
72                 if(m_active.find(id) == m_active.end())
73                 {
74                         warningstream<<"StaticObjectList::remove(): id="<<id
75                                         <<" not found"<<std::endl;
76                         return;
77                 }
78                 m_active.erase(id);
79         }
80
81         void serialize(std::ostream &os);
82         void deSerialize(std::istream &is);
83
84         /*
85                 NOTE: When an object is transformed to active, it is removed
86                 from m_stored and inserted to m_active.
87                 The caller directly manipulates these containers.
88         */
89         std::vector<StaticObject> m_stored;
90         std::map<u16, StaticObject> m_active;
91
92 private:
93 };