Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
staticobject.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
8#include <string>
9#include <sstream>
10#include <vector>
11#include <map>
12#include "debug.h"
13
15
17{
18 u8 type = 0;
20 std::string data;
21
22 StaticObject() = default;
23 StaticObject(const ServerActiveObject *s_obj, const v3f &pos_);
24
25 void serialize(std::ostream &os) const;
26 void deSerialize(std::istream &is, u8 version);
27};
28
30{
31public:
32 /*
33 Inserts an object to the container.
34 Id must be unique (active) or 0 (stored).
35 */
36 void insert(u16 id, const StaticObject &obj)
37 {
38 if (id == 0) {
39 m_stored.push_back(obj);
40 } else {
41 if (m_active.find(id) != m_active.end()) {
42 dstream << "ERROR: StaticObjectList::insert(): "
43 << "id already exists" << std::endl;
44 FATAL_ERROR("StaticObjectList::insert()");
45 }
46 setActive(id, obj);
47 }
48 }
49
50 void remove(u16 id)
51 {
52 assert(id != 0); // Pre-condition
53 if (m_active.find(id) == m_active.end()) {
54 warningstream << "StaticObjectList::remove(): id=" << id << " not found"
55 << std::endl;
56 return;
57 }
58 m_active.erase(id);
59 }
60
61 void serialize(std::ostream &os);
62 void deSerialize(std::istream &is);
63
64 // Never permit to modify outside of here. Only this object is responsible of m_stored and m_active modifications
65 const std::vector<StaticObject>& getAllStored() const { return m_stored; }
66 const std::map<u16, StaticObject> &getAllActives() const { return m_active; }
67
68 inline void setActive(u16 id, const StaticObject &obj) { m_active[id] = obj; }
69 inline size_t getActiveSize() const { return m_active.size(); }
70 inline size_t getStoredSize() const { return m_stored.size(); }
71 inline void clearStored() { m_stored.clear(); }
72 void pushStored(const StaticObject &obj) { m_stored.push_back(obj); }
73
74 bool storeActiveObject(u16 id);
75
76 inline void clear()
77 {
78 m_active.clear();
79 m_stored.clear();
80 }
81
82 inline size_t size()
83 {
84 return m_active.size() + m_stored.size();
85 }
86
87private:
88 /*
89 NOTE: When an object is transformed to active, it is removed
90 from m_stored and inserted to m_active.
91 */
92 std::vector<StaticObject> m_stored;
93 std::map<u16, StaticObject> m_active;
94};
Definition serveractiveobject.h:41
Definition staticobject.h:30
void clear()
Definition staticobject.h:76
std::map< u16, StaticObject > m_active
Definition staticobject.h:93
void remove(u16 id)
Definition staticobject.h:50
size_t getStoredSize() const
Definition staticobject.h:70
void insert(u16 id, const StaticObject &obj)
Definition staticobject.h:36
void pushStored(const StaticObject &obj)
Definition staticobject.h:72
void setActive(u16 id, const StaticObject &obj)
Definition staticobject.h:68
bool storeActiveObject(u16 id)
Definition staticobject.cpp:111
std::vector< StaticObject > m_stored
Definition staticobject.h:92
void deSerialize(std::istream &is)
Definition staticobject.cpp:89
const std::vector< StaticObject > & getAllStored() const
Definition staticobject.h:65
void serialize(std::ostream &os)
Definition staticobject.cpp:37
size_t size()
Definition staticobject.h:82
const std::map< u16, StaticObject > & getAllActives() const
Definition staticobject.h:66
size_t getActiveSize() const
Definition staticobject.h:69
void clearStored()
Definition staticobject.h:71
#define FATAL_ERROR(msg)
Definition debug.h:33
core::vector3df v3f
Definition irr_v3d.h:11
thread_local LogStream dstream
thread_local LogStream warningstream
Definition staticobject.h:17
u8 type
Definition staticobject.h:18
void serialize(std::ostream &os) const
Definition staticobject.cpp:17
v3f pos
Definition staticobject.h:19
StaticObject()=default
void deSerialize(std::istream &is, u8 version)
Definition staticobject.cpp:27
std::string data
Definition staticobject.h:20