3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
28 NodeTimer provides per-node timed callback functionality.
30 - Furnaces, to keep the fire burnin'
31 - "activated" nodes that snap back to their original state
32 after a fixed amount of time (mesecons buttons, for example)
38 NodeTimer() = default;
39 NodeTimer(const v3s16 &position_):
40 position(position_) {}
41 NodeTimer(f32 timeout_, f32 elapsed_, v3s16 position_):
42 timeout(timeout_), elapsed(elapsed_), position(position_) {}
43 ~NodeTimer() = default;
45 void serialize(std::ostream &os) const;
46 void deSerialize(std::istream &is);
54 List of timers of all the nodes of a block
60 NodeTimerList() = default;
61 ~NodeTimerList() = default;
63 void serialize(std::ostream &os, u8 map_format_version) const;
64 void deSerialize(std::istream &is, u8 map_format_version);
67 NodeTimer get(const v3s16 &p) {
68 std::map<v3s16, std::multimap<double, NodeTimer>::iterator>::iterator n =
70 if (n == m_iterators.end())
72 NodeTimer t = n->second->second;
73 t.elapsed = t.timeout - (n->second->first - m_time);
77 void remove(v3s16 p) {
78 std::map<v3s16, std::multimap<double, NodeTimer>::iterator>::iterator n =
80 if(n != m_iterators.end()) {
81 double removed_time = n->second->first;
82 m_timers.erase(n->second);
84 // Yes, this is float equality, but it is not a problem
85 // since we only test equality of floats as an ordered type
86 // and thus we never lose precision
87 if (removed_time == m_next_trigger_time) {
89 m_next_trigger_time = -1.;
91 m_next_trigger_time = m_timers.begin()->first;
95 // Undefined behaviour if there already is a timer
96 void insert(NodeTimer timer) {
97 v3s16 p = timer.position;
98 double trigger_time = m_time + (double)(timer.timeout - timer.elapsed);
99 std::multimap<double, NodeTimer>::iterator it =
100 m_timers.insert(std::pair<double, NodeTimer>(
104 std::pair<v3s16, std::multimap<double, NodeTimer>::iterator>(p, it));
105 if (m_next_trigger_time == -1. || trigger_time < m_next_trigger_time)
106 m_next_trigger_time = trigger_time;
108 // Deletes old timer and sets a new one
109 inline void set(const NodeTimer &timer) {
110 remove(timer.position);
113 // Deletes all timers
117 m_next_trigger_time = -1.;
120 // Move forward in time, returns elapsed timers
121 std::vector<NodeTimer> step(float dtime);
124 std::multimap<double, NodeTimer> m_timers;
125 std::map<v3s16, std::multimap<double, NodeTimer>::iterator> m_iterators;
126 double m_next_trigger_time = -1.0;