staticobject.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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);
  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. {
  45. m_stored.push_back(obj);
  46. }
  47. else
  48. {
  49. if(m_active.find(id) != m_active.end())
  50. {
  51. dstream<<"ERROR: StaticObjectList::insert(): "
  52. <<"id already exists"<<std::endl;
  53. FATAL_ERROR("StaticObjectList::insert()");
  54. }
  55. m_active[id] = obj;
  56. }
  57. }
  58. void remove(u16 id)
  59. {
  60. assert(id != 0); // Pre-condition
  61. if(m_active.find(id) == m_active.end())
  62. {
  63. warningstream<<"StaticObjectList::remove(): id="<<id
  64. <<" not found"<<std::endl;
  65. return;
  66. }
  67. m_active.erase(id);
  68. }
  69. void serialize(std::ostream &os);
  70. void deSerialize(std::istream &is);
  71. /*
  72. NOTE: When an object is transformed to active, it is removed
  73. from m_stored and inserted to m_active.
  74. The caller directly manipulates these containers.
  75. */
  76. std::vector<StaticObject> m_stored;
  77. std::map<u16, StaticObject> m_active;
  78. private:
  79. };