nodetimer.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 "nodetimer.h"
  17. #include "log.h"
  18. #include "serialization.h"
  19. #include "util/serialize.h"
  20. #include "constants.h" // MAP_BLOCKSIZE
  21. /*
  22. NodeTimer
  23. */
  24. void NodeTimer::serialize(std::ostream &os) const
  25. {
  26. writeF1000(os, timeout);
  27. writeF1000(os, elapsed);
  28. }
  29. void NodeTimer::deSerialize(std::istream &is)
  30. {
  31. timeout = readF1000(is);
  32. elapsed = readF1000(is);
  33. }
  34. /*
  35. NodeTimerList
  36. */
  37. void NodeTimerList::serialize(std::ostream &os, u8 map_format_version) const
  38. {
  39. if(map_format_version == 24){
  40. // Version 0 is a placeholder for "nothing to see here; go away."
  41. if(m_data.size() == 0){
  42. writeU8(os, 0); // version
  43. return;
  44. }
  45. writeU8(os, 1); // version
  46. writeU16(os, m_data.size());
  47. }
  48. if(map_format_version >= 25){
  49. writeU8(os, 2+4+4);
  50. writeU16(os, m_data.size());
  51. }
  52. for(std::map<v3s16, NodeTimer>::const_iterator
  53. i = m_data.begin();
  54. i != m_data.end(); i++){
  55. v3s16 p = i->first;
  56. NodeTimer t = i->second;
  57. u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
  58. writeU16(os, p16);
  59. t.serialize(os);
  60. }
  61. }
  62. void NodeTimerList::deSerialize(std::istream &is, u8 map_format_version)
  63. {
  64. m_data.clear();
  65. if(map_format_version == 24){
  66. u8 timer_version = readU8(is);
  67. if(timer_version == 0)
  68. return;
  69. if(timer_version != 1)
  70. throw SerializationError("unsupported NodeTimerList version");
  71. }
  72. if(map_format_version >= 25){
  73. u8 timer_data_len = readU8(is);
  74. if(timer_data_len != 2+4+4)
  75. throw SerializationError("unsupported NodeTimer data length");
  76. }
  77. u16 count = readU16(is);
  78. for(u16 i=0; i<count; i++)
  79. {
  80. u16 p16 = readU16(is);
  81. v3s16 p(0,0,0);
  82. p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
  83. p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
  84. p.Y += p16 / MAP_BLOCKSIZE;
  85. p16 -= p.Y * MAP_BLOCKSIZE;
  86. p.X += p16;
  87. NodeTimer t;
  88. t.deSerialize(is);
  89. if(t.timeout <= 0)
  90. {
  91. infostream<<"WARNING: NodeTimerList::deSerialize(): "
  92. <<"invalid data at position"
  93. <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
  94. <<std::endl;
  95. continue;
  96. }
  97. if(m_data.find(p) != m_data.end())
  98. {
  99. infostream<<"WARNING: NodeTimerList::deSerialize(): "
  100. <<"already set data at position"
  101. <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
  102. <<std::endl;
  103. continue;
  104. }
  105. m_data.insert(std::make_pair(p, t));
  106. }
  107. }
  108. std::map<v3s16, NodeTimer> NodeTimerList::step(float dtime)
  109. {
  110. std::map<v3s16, NodeTimer> elapsed_timers;
  111. // Increment timers
  112. for(std::map<v3s16, NodeTimer>::iterator
  113. i = m_data.begin();
  114. i != m_data.end(); i++){
  115. v3s16 p = i->first;
  116. NodeTimer t = i->second;
  117. t.elapsed += dtime;
  118. if(t.elapsed >= t.timeout)
  119. elapsed_timers.insert(std::make_pair(p, t));
  120. else
  121. i->second = t;
  122. }
  123. // Delete elapsed timers
  124. for(std::map<v3s16, NodeTimer>::const_iterator
  125. i = elapsed_timers.begin();
  126. i != elapsed_timers.end(); i++){
  127. v3s16 p = i->first;
  128. m_data.erase(p);
  129. }
  130. return elapsed_timers;
  131. }