]> git.lizzy.rs Git - dragonfireclient.git/blob - src/nodetimer.h
Make node timers more efficient
[dragonfireclient.git] / src / nodetimer.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef NODETIMER_HEADER
21 #define NODETIMER_HEADER
22
23 #include "irr_v3d.h"
24 #include <iostream>
25 #include <map>
26 #include <vector>
27
28 /*
29         NodeTimer provides per-node timed callback functionality.
30         Can be used for:
31         - Furnaces, to keep the fire burnin'
32         - "activated" nodes that snap back to their original state
33           after a fixed amount of time (mesecons buttons, for example)
34 */
35
36 class NodeTimer
37 {
38 public:
39         NodeTimer(): timeout(0.), elapsed(0.) {}
40         NodeTimer(const v3s16 &position_):
41                 timeout(0.), elapsed(0.), position(position_) {}
42         NodeTimer(f32 timeout_, f32 elapsed_, v3s16 position_):
43                 timeout(timeout_), elapsed(elapsed_), position(position_) {}
44         ~NodeTimer() {}
45         
46         void serialize(std::ostream &os) const;
47         void deSerialize(std::istream &is);
48         
49         f32 timeout;
50         f32 elapsed;
51         v3s16 position;
52 };
53
54 /*
55         List of timers of all the nodes of a block
56 */
57
58 class NodeTimerList
59 {
60 public:
61         NodeTimerList(): m_next_trigger_time(-1.), m_time(0.) {}
62         ~NodeTimerList() {}
63         
64         void serialize(std::ostream &os, u8 map_format_version) const;
65         void deSerialize(std::istream &is, u8 map_format_version);
66         
67         // Get timer
68         NodeTimer get(const v3s16 &p) {
69                 std::map<v3s16, std::multimap<double, NodeTimer>::iterator>::iterator n =
70                         m_iterators.find(p);
71                 if (n == m_iterators.end())
72                         return NodeTimer();
73                 NodeTimer t = n->second->second;
74                 t.elapsed = t.timeout - (n->second->first - m_time);
75                 return t;
76         }
77         // Deletes timer
78         void remove(v3s16 p) {
79                 std::map<v3s16, std::multimap<double, NodeTimer>::iterator>::iterator n =
80                         m_iterators.find(p);
81                 if(n != m_iterators.end()) {
82                         double removed_time = n->second->first;
83                         m_timers.erase(n->second);
84                         m_iterators.erase(n);
85                         // Yes, this is float equality, but it is not a problem
86                         // since we only test equality of floats as an ordered type
87                         // and thus we never lose precision
88                         if (removed_time == m_next_trigger_time) {
89                                 if (m_timers.empty())
90                                         m_next_trigger_time = -1.;
91                                 else
92                                         m_next_trigger_time = m_timers.begin()->first;
93                         }
94                 }
95         }
96         // Undefined behaviour if there already is a timer
97         void insert(NodeTimer timer) {
98                 v3s16 p = timer.position;
99                 double trigger_time = m_time + (double)(timer.timeout - timer.elapsed);
100                 std::multimap<double, NodeTimer>::iterator it =
101                         m_timers.insert(std::pair<double, NodeTimer>(
102                                 trigger_time, timer
103                         ));
104                 m_iterators.insert(
105                         std::pair<v3s16, std::multimap<double, NodeTimer>::iterator>(p, it));
106                 if (m_next_trigger_time == -1. || trigger_time < m_next_trigger_time)
107                         m_next_trigger_time = trigger_time;
108         }
109         // Deletes old timer and sets a new one
110         inline void set(const NodeTimer &timer) {
111                 remove(timer.position);
112                 insert(timer);
113         }
114         // Deletes all timers
115         void clear() {
116                 m_timers.clear();
117                 m_iterators.clear();
118                 m_next_trigger_time = -1.;
119         }
120
121         inline double getNextTriggerTime() {
122                 return m_next_trigger_time;
123         }
124
125         // Move forward in time, returns elapsed timers
126         std::vector<NodeTimer> step(float dtime);
127
128 private:
129         std::multimap<double, NodeTimer> m_timers;
130         std::map<v3s16, std::multimap<double, NodeTimer>::iterator> m_iterators;
131         double m_next_trigger_time;
132         double m_time;
133 };
134
135 #endif
136