staticobject.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #include "staticobject.h"
  17. #include "util/serialize.h"
  18. void StaticObject::serialize(std::ostream &os)
  19. {
  20. char buf[12];
  21. // type
  22. buf[0] = type;
  23. os.write(buf, 1);
  24. // pos
  25. writeV3S32((u8*)buf, v3s32(pos.X*1000,pos.Y*1000,pos.Z*1000));
  26. os.write(buf, 12);
  27. // data
  28. os<<serializeString(data);
  29. }
  30. void StaticObject::deSerialize(std::istream &is, u8 version)
  31. {
  32. char buf[12];
  33. // type
  34. is.read(buf, 1);
  35. type = buf[0];
  36. // pos
  37. is.read(buf, 12);
  38. v3s32 intp = readV3S32((u8*)buf);
  39. pos.X = (f32)intp.X/1000;
  40. pos.Y = (f32)intp.Y/1000;
  41. pos.Z = (f32)intp.Z/1000;
  42. // data
  43. data = deSerializeString(is);
  44. }
  45. void StaticObjectList::serialize(std::ostream &os)
  46. {
  47. char buf[12];
  48. // version
  49. buf[0] = 0;
  50. os.write(buf, 1);
  51. // count
  52. u16 count = m_stored.size() + m_active.size();
  53. writeU16((u8*)buf, count);
  54. os.write(buf, 2);
  55. for(std::list<StaticObject>::iterator
  56. i = m_stored.begin();
  57. i != m_stored.end(); ++i)
  58. {
  59. StaticObject &s_obj = *i;
  60. s_obj.serialize(os);
  61. }
  62. for(std::map<u16, StaticObject>::iterator
  63. i = m_active.begin();
  64. i != m_active.end(); ++i)
  65. {
  66. StaticObject s_obj = i->second;
  67. s_obj.serialize(os);
  68. }
  69. }
  70. void StaticObjectList::deSerialize(std::istream &is)
  71. {
  72. char buf[12];
  73. // version
  74. is.read(buf, 1);
  75. u8 version = buf[0];
  76. // count
  77. is.read(buf, 2);
  78. u16 count = readU16((u8*)buf);
  79. for(u16 i=0; i<count; i++)
  80. {
  81. StaticObject s_obj;
  82. s_obj.deSerialize(is, version);
  83. m_stored.push_back(s_obj);
  84. }
  85. }