staticobject.h 2.2 KB

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