Switch the license to be LGPLv2/later, with small parts still remaining as GPLv2...
[oweals/minetest.git] / src / nodetimer.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2012 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 "irrlichttypes.h"
24 #include <iostream>
25 #include <map>
26
27 /*
28         NodeTimer provides per-node timed callback functionality.
29         Can be used for:
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)
33 */
34
35 class NodeTimer
36 {
37 public:
38         NodeTimer(): duration(0.), elapsed(0.) {}
39         NodeTimer(f32 duration_, f32 elapsed_):
40                 duration(duration_), elapsed(elapsed_) {}
41         ~NodeTimer() {}
42         
43         void serialize(std::ostream &os) const;
44         void deSerialize(std::istream &is);
45         
46         f32 duration;
47         f32 elapsed;
48 };
49
50 /*
51         List of timers of all the nodes of a block
52 */
53
54 class NodeTimerList
55 {
56 public:
57         NodeTimerList() {}
58         ~NodeTimerList() {}
59         
60         void serialize(std::ostream &os) const;
61         void deSerialize(std::istream &is);
62         
63         // Get timer
64         NodeTimer get(v3s16 p){
65                 std::map<v3s16, NodeTimer>::iterator n = m_data.find(p);
66                 if(n == m_data.end())
67                         return NodeTimer();
68                 return n->second;
69         }
70         // Deletes timer
71         void remove(v3s16 p){
72                 m_data.erase(p);
73         }
74         // Deletes old timer and sets a new one
75         void set(v3s16 p, NodeTimer t){
76                 m_data[p] = t;
77         }
78         // Deletes all timers
79         void clear(){
80                 m_data.clear();
81         }
82
83         // A step in time. Returns map of elapsed timers.
84         std::map<v3s16, f32> step(float dtime);
85
86 private:
87         std::map<v3s16, NodeTimer> m_data;
88 };
89
90 #endif
91