c457f1384f946c6b10a97d419ef0381c16535abf
[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 struct StaticObject
30 {
31         u8 type = 0;
32         v3f pos;
33         std::string data;
34
35         StaticObject() {}
36         StaticObject(u8 type_, v3f pos_, const std::string &data_):
37                 type(type_),
38                 pos(pos_),
39                 data(data_)
40         {
41         }
42
43         void serialize(std::ostream &os);
44         void deSerialize(std::istream &is, u8 version);
45 };
46
47 class StaticObjectList
48 {
49 public:
50         /*
51                 Inserts an object to the container.
52                 Id must be unique (active) or 0 (stored).
53         */
54         void insert(u16 id, StaticObject obj)
55         {
56                 if(id == 0)
57                 {
58                         m_stored.push_back(obj);
59                 }
60                 else
61                 {
62                         if(m_active.find(id) != m_active.end())
63                         {
64                                 dstream<<"ERROR: StaticObjectList::insert(): "
65                                                 <<"id already exists"<<std::endl;
66                                 FATAL_ERROR("StaticObjectList::insert()");
67                         }
68                         m_active[id] = obj;
69                 }
70         }
71
72         void remove(u16 id)
73         {
74                 assert(id != 0); // Pre-condition
75                 if(m_active.find(id) == m_active.end())
76                 {
77                         warningstream<<"StaticObjectList::remove(): id="<<id
78                                         <<" not found"<<std::endl;
79                         return;
80                 }
81                 m_active.erase(id);
82         }
83
84         void serialize(std::ostream &os);
85         void deSerialize(std::istream &is);
86
87         /*
88                 NOTE: When an object is transformed to active, it is removed
89                 from m_stored and inserted to m_active.
90                 The caller directly manipulates these containers.
91         */
92         std::vector<StaticObject> m_stored;
93         std::map<u16, StaticObject> m_active;
94
95 private:
96 };