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.
20 #ifndef NODETIMER_HEADER
21 #define NODETIMER_HEADER
29 NodeTimer provides per-node timed callback functionality.
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)
40 NodeTimer(const v3s16 &position_):
41 position(position_) {}
42 NodeTimer(f32 timeout_, f32 elapsed_, v3s16 position_):
43 timeout(timeout_), elapsed(elapsed_), position(position_) {}
46 void serialize(std::ostream &os) const;
47 void deSerialize(std::istream &is);
55 List of timers of all the nodes of a block
64 void serialize(std::ostream &os, u8 map_format_version) const;
65 void deSerialize(std::istream &is, u8 map_format_version);
68 NodeTimer get(const v3s16 &p) {
69 std::map<v3s16, std::multimap<double, NodeTimer>::iterator>::iterator n =
71 if (n == m_iterators.end())
73 NodeTimer t = n->second->second;
74 t.elapsed = t.timeout - (n->second->first - m_time);
78 void remove(v3s16 p) {
79 std::map<v3s16, std::multimap<double, NodeTimer>::iterator>::iterator n =
81 if(n != m_iterators.end()) {
82 double removed_time = n->second->first;
83 m_timers.erase(n->second);
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) {
90 m_next_trigger_time = -1.;
92 m_next_trigger_time = m_timers.begin()->first;
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>(
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;
109 // Deletes old timer and sets a new one
110 inline void set(const NodeTimer &timer) {
111 remove(timer.position);
114 // Deletes all timers
118 m_next_trigger_time = -1.;
121 inline double getNextTriggerTime() {
122 return m_next_trigger_time;
125 // Move forward in time, returns elapsed timers
126 std::vector<NodeTimer> step(float dtime);
129 std::multimap<double, NodeTimer> m_timers;
130 std::map<v3s16, std::multimap<double, NodeTimer>::iterator> m_iterators;
131 double m_next_trigger_time = -1.0;