staticobject.h 2.2 KB

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