Re-add jungles, apple trees
[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 #ifndef STATICOBJECT_HEADER
21 #define STATICOBJECT_HEADER
22
23 #include "irrlichttypes_bloated.h"
24 #include <string>
25 #include <sstream>
26 #include <list>
27 #include <map>
28 #include "debug.h"
29
30 struct StaticObject
31 {
32         u8 type;
33         v3f pos;
34         std::string data;
35
36         StaticObject():
37                 type(0),
38                 pos(0,0,0)
39         {
40         }
41         StaticObject(u8 type_, v3f pos_, const std::string &data_):
42                 type(type_),
43                 pos(pos_),
44                 data(data_)
45         {
46         }
47
48         void serialize(std::ostream &os);
49         void deSerialize(std::istream &is, u8 version);
50 };
51
52 class StaticObjectList
53 {
54 public:
55         /*
56                 Inserts an object to the container.
57                 Id must be unique or 0.
58         */
59         void insert(u16 id, StaticObject obj)
60         {
61                 if(id == 0)
62                 {
63                         m_stored.push_back(obj);
64                 }
65                 else
66                 {
67                         if(m_active.find(id) != m_active.end())
68                         {
69                                 dstream<<"ERROR: StaticObjectList::insert(): "
70                                                 <<"id already exists"<<std::endl;
71                                 assert(0);
72                                 return;
73                         }
74                         m_active[id] = obj;
75                 }
76         }
77
78         void remove(u16 id)
79         {
80                 assert(id != 0);
81                 if(m_active.find(id) == m_active.end())
82                 {
83                         dstream<<"WARNING: StaticObjectList::remove(): id="<<id
84                                         <<" not found"<<std::endl;
85                         return;
86                 }
87                 m_active.erase(id);
88         }
89
90         void serialize(std::ostream &os);
91         void deSerialize(std::istream &is);
92         
93         /*
94                 NOTE: When an object is transformed to active, it is removed
95                 from m_stored and inserted to m_active.
96                 The caller directly manipulates these containers.
97         */
98         std::list<StaticObject> m_stored;
99         std::map<u16, StaticObject> m_active;
100
101 private:
102 };
103
104 #endif
105