Minetest 5.9.0-dev
 
Loading...
Searching...
No Matches
staticobject.h
Go to the documentation of this file.
1/*
2Minetest
3Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU Lesser General Public License as published by
7the Free Software Foundation; either version 2.1 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public License along
16with this program; if not, write to the Free Software Foundation, Inc.,
1751 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18*/
19
20#pragma once
21
23#include <string>
24#include <sstream>
25#include <vector>
26#include <map>
27#include "debug.h"
28
30
32{
33 u8 type = 0;
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
45{
46public:
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
102private:
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};
Definition: serveractiveobject.h:55
Definition: staticobject.h:45
void clear()
Definition: staticobject.h:91
std::map< u16, StaticObject > m_active
Definition: staticobject.h:108
void remove(u16 id)
Definition: staticobject.h:65
size_t getStoredSize() const
Definition: staticobject.h:85
void insert(u16 id, const StaticObject &obj)
Definition: staticobject.h:51
void pushStored(const StaticObject &obj)
Definition: staticobject.h:87
void setActive(u16 id, const StaticObject &obj)
Definition: staticobject.h:83
bool storeActiveObject(u16 id)
Definition: staticobject.cpp:126
std::vector< StaticObject > m_stored
Definition: staticobject.h:107
void deSerialize(std::istream &is)
Definition: staticobject.cpp:104
const std::vector< StaticObject > & getAllStored() const
Definition: staticobject.h:80
void serialize(std::ostream &os)
Definition: staticobject.cpp:52
size_t size()
Definition: staticobject.h:97
const std::map< u16, StaticObject > & getAllActives() const
Definition: staticobject.h:81
size_t getActiveSize() const
Definition: staticobject.h:84
void clearStored()
Definition: staticobject.h:86
#define FATAL_ERROR(msg)
Definition: debug.h:48
core::vector3df v3f
Definition: irr_v3d.h:26
thread_local LogStream dstream
thread_local LogStream warningstream
Definition: staticobject.h:32
u8 type
Definition: staticobject.h:33
void serialize(std::ostream &os) const
Definition: staticobject.cpp:32
v3f pos
Definition: staticobject.h:34
StaticObject()=default
void deSerialize(std::istream &is, u8 version)
Definition: staticobject.cpp:42
std::string data
Definition: staticobject.h:35