nodetimer.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 "irr_v3d.h"
  18. #include <iostream>
  19. #include <map>
  20. #include <vector>
  21. /*
  22. NodeTimer provides per-node timed callback functionality.
  23. Can be used for:
  24. - Furnaces, to keep the fire burnin'
  25. - "activated" nodes that snap back to their original state
  26. after a fixed amount of time (mesecons buttons, for example)
  27. */
  28. class NodeTimer
  29. {
  30. public:
  31. NodeTimer() = default;
  32. NodeTimer(const v3s16 &position_):
  33. position(position_) {}
  34. NodeTimer(f32 timeout_, f32 elapsed_, v3s16 position_):
  35. timeout(timeout_), elapsed(elapsed_), position(position_) {}
  36. ~NodeTimer() = default;
  37. void serialize(std::ostream &os) const;
  38. void deSerialize(std::istream &is);
  39. f32 timeout = 0.0f;
  40. f32 elapsed = 0.0f;
  41. v3s16 position;
  42. };
  43. /*
  44. List of timers of all the nodes of a block
  45. */
  46. class NodeTimerList
  47. {
  48. public:
  49. NodeTimerList() = default;
  50. ~NodeTimerList() = default;
  51. void serialize(std::ostream &os, u8 map_format_version) const;
  52. void deSerialize(std::istream &is, u8 map_format_version);
  53. // Get timer
  54. NodeTimer get(const v3s16 &p) {
  55. std::map<v3s16, std::multimap<double, NodeTimer>::iterator>::iterator n =
  56. m_iterators.find(p);
  57. if (n == m_iterators.end())
  58. return NodeTimer();
  59. NodeTimer t = n->second->second;
  60. t.elapsed = t.timeout - (n->second->first - m_time);
  61. return t;
  62. }
  63. // Deletes timer
  64. void remove(v3s16 p) {
  65. std::map<v3s16, std::multimap<double, NodeTimer>::iterator>::iterator n =
  66. m_iterators.find(p);
  67. if(n != m_iterators.end()) {
  68. double removed_time = n->second->first;
  69. m_timers.erase(n->second);
  70. m_iterators.erase(n);
  71. // Yes, this is float equality, but it is not a problem
  72. // since we only test equality of floats as an ordered type
  73. // and thus we never lose precision
  74. if (removed_time == m_next_trigger_time) {
  75. if (m_timers.empty())
  76. m_next_trigger_time = -1.;
  77. else
  78. m_next_trigger_time = m_timers.begin()->first;
  79. }
  80. }
  81. }
  82. // Undefined behavior if there already is a timer
  83. void insert(const NodeTimer &timer) {
  84. v3s16 p = timer.position;
  85. double trigger_time = m_time + (double)(timer.timeout - timer.elapsed);
  86. std::multimap<double, NodeTimer>::iterator it = m_timers.emplace(trigger_time, timer);
  87. m_iterators.emplace(p, it);
  88. if (m_next_trigger_time == -1. || trigger_time < m_next_trigger_time)
  89. m_next_trigger_time = trigger_time;
  90. }
  91. // Deletes old timer and sets a new one
  92. inline void set(const NodeTimer &timer) {
  93. remove(timer.position);
  94. insert(timer);
  95. }
  96. // Deletes all timers
  97. void clear() {
  98. m_timers.clear();
  99. m_iterators.clear();
  100. m_next_trigger_time = -1.;
  101. }
  102. // Move forward in time, returns elapsed timers
  103. std::vector<NodeTimer> step(float dtime);
  104. private:
  105. std::multimap<double, NodeTimer> m_timers;
  106. std::map<v3s16, std::multimap<double, NodeTimer>::iterator> m_iterators;
  107. double m_next_trigger_time = -1.0;
  108. double m_time = 0.0;
  109. };