nodetimer.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_timers.empty()) {
  42. writeU8(os, 0); // version
  43. return;
  44. }
  45. writeU8(os, 1); // version
  46. writeU16(os, m_timers.size());
  47. }
  48. if (map_format_version >= 25) {
  49. writeU8(os, 2 + 4 + 4); // length of the data for a single timer
  50. writeU16(os, m_timers.size());
  51. }
  52. for (const auto &timer : m_timers) {
  53. NodeTimer t = timer.second;
  54. NodeTimer nt = NodeTimer(t.timeout,
  55. t.timeout - (f32)(timer.first - m_time), t.position);
  56. v3s16 p = t.position;
  57. u16 p16 = p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE + p.Y * MAP_BLOCKSIZE + p.X;
  58. writeU16(os, p16);
  59. nt.serialize(os);
  60. }
  61. }
  62. void NodeTimerList::deSerialize(std::istream &is, u8 map_format_version)
  63. {
  64. 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. u16 p16 = readU16(is);
  80. v3s16 p;
  81. p.Z = p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
  82. p16 &= MAP_BLOCKSIZE * MAP_BLOCKSIZE - 1;
  83. p.Y = p16 / MAP_BLOCKSIZE;
  84. p16 &= MAP_BLOCKSIZE - 1;
  85. p.X = p16;
  86. NodeTimer t(p);
  87. t.deSerialize(is);
  88. if (t.timeout <= 0) {
  89. warningstream<<"NodeTimerList::deSerialize(): "
  90. <<"invalid data at position"
  91. <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
  92. <<std::endl;
  93. continue;
  94. }
  95. if (m_iterators.find(p) != m_iterators.end()) {
  96. warningstream<<"NodeTimerList::deSerialize(): "
  97. <<"already set data at position"
  98. <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
  99. <<std::endl;
  100. continue;
  101. }
  102. insert(t);
  103. }
  104. }
  105. std::vector<NodeTimer> NodeTimerList::step(float dtime)
  106. {
  107. std::vector<NodeTimer> elapsed_timers;
  108. m_time += dtime;
  109. if (m_next_trigger_time == -1. || m_time < m_next_trigger_time) {
  110. return elapsed_timers;
  111. }
  112. std::multimap<double, NodeTimer>::iterator i = m_timers.begin();
  113. // Process timers
  114. for (; i != m_timers.end() && i->first <= m_time; ++i) {
  115. NodeTimer t = i->second;
  116. t.elapsed = t.timeout + (f32)(m_time - i->first);
  117. elapsed_timers.push_back(t);
  118. m_iterators.erase(t.position);
  119. }
  120. // Delete elapsed timers
  121. m_timers.erase(m_timers.begin(), i);
  122. if (m_timers.empty())
  123. m_next_trigger_time = -1.;
  124. else
  125. m_next_trigger_time = m_timers.begin()->first;
  126. return elapsed_timers;
  127. }