nodetimer.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #ifndef NODETIMER_HEADER
  17. #define NODETIMER_HEADER
  18. #include "irr_v3d.h"
  19. #include <iostream>
  20. #include <map>
  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(): timeout(0.), elapsed(0.) {}
  32. NodeTimer(f32 timeout_, f32 elapsed_):
  33. timeout(timeout_), elapsed(elapsed_) {}
  34. ~NodeTimer() {}
  35. void serialize(std::ostream &os) const;
  36. void deSerialize(std::istream &is);
  37. f32 timeout;
  38. f32 elapsed;
  39. };
  40. /*
  41. List of timers of all the nodes of a block
  42. */
  43. class NodeTimerList
  44. {
  45. public:
  46. NodeTimerList() {}
  47. ~NodeTimerList() {}
  48. void serialize(std::ostream &os, u8 map_format_version) const;
  49. void deSerialize(std::istream &is, u8 map_format_version);
  50. // Get timer
  51. NodeTimer get(v3s16 p){
  52. std::map<v3s16, NodeTimer>::iterator n = m_data.find(p);
  53. if(n == m_data.end())
  54. return NodeTimer();
  55. return n->second;
  56. }
  57. // Deletes timer
  58. void remove(v3s16 p){
  59. m_data.erase(p);
  60. }
  61. // Deletes old timer and sets a new one
  62. void set(v3s16 p, NodeTimer t){
  63. m_data[p] = t;
  64. }
  65. // Deletes all timers
  66. void clear(){
  67. m_data.clear();
  68. }
  69. // A step in time. Returns map of elapsed timers.
  70. std::map<v3s16, NodeTimer> step(float dtime);
  71. private:
  72. std::map<v3s16, NodeTimer> m_data;
  73. };
  74. #endif