staticobject.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "irrlichttypes_bloated.h"
  18. #include <string>
  19. #include <sstream>
  20. #include <vector>
  21. #include <map>
  22. #include "debug.h"
  23. class ServerActiveObject;
  24. struct StaticObject
  25. {
  26. u8 type = 0;
  27. v3f pos;
  28. std::string data;
  29. StaticObject() = default;
  30. StaticObject(const ServerActiveObject *s_obj, const v3f &pos_);
  31. void serialize(std::ostream &os) const;
  32. void deSerialize(std::istream &is, u8 version);
  33. };
  34. class StaticObjectList
  35. {
  36. public:
  37. /*
  38. Inserts an object to the container.
  39. Id must be unique (active) or 0 (stored).
  40. */
  41. void insert(u16 id, const StaticObject &obj)
  42. {
  43. if (id == 0) {
  44. m_stored.push_back(obj);
  45. } else {
  46. if (m_active.find(id) != m_active.end()) {
  47. dstream << "ERROR: StaticObjectList::insert(): "
  48. << "id already exists" << std::endl;
  49. FATAL_ERROR("StaticObjectList::insert()");
  50. }
  51. setActive(id, obj);
  52. }
  53. }
  54. void remove(u16 id)
  55. {
  56. assert(id != 0); // Pre-condition
  57. if (m_active.find(id) == m_active.end()) {
  58. warningstream << "StaticObjectList::remove(): id=" << id << " not found"
  59. << std::endl;
  60. return;
  61. }
  62. m_active.erase(id);
  63. }
  64. void serialize(std::ostream &os);
  65. void deSerialize(std::istream &is);
  66. // Never permit to modify outside of here. Only this object is responsible of m_stored and m_active modifications
  67. const std::vector<StaticObject>& getAllStored() const { return m_stored; }
  68. const std::map<u16, StaticObject> &getAllActives() const { return m_active; }
  69. inline void setActive(u16 id, const StaticObject &obj) { m_active[id] = obj; }
  70. inline size_t getActiveSize() const { return m_active.size(); }
  71. inline size_t getStoredSize() const { return m_stored.size(); }
  72. inline void clearStored() { m_stored.clear(); }
  73. void pushStored(const StaticObject &obj) { m_stored.push_back(obj); }
  74. bool storeActiveObject(u16 id);
  75. inline void clear()
  76. {
  77. m_active.clear();
  78. m_stored.clear();
  79. }
  80. inline size_t size()
  81. {
  82. return m_active.size() + m_stored.size();
  83. }
  84. private:
  85. /*
  86. NOTE: When an object is transformed to active, it is removed
  87. from m_stored and inserted to m_active.
  88. */
  89. std::vector<StaticObject> m_stored;
  90. std::map<u16, StaticObject> m_active;
  91. };