]> git.lizzy.rs Git - dragonfireclient.git/blob - src/nodetimer.cpp
Implement node timers
[dragonfireclient.git] / src / nodetimer.cpp
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 #include "nodetimer.h"
21 #include "log.h"
22 #include "util/serialize.h"
23 #include "constants.h" // MAP_BLOCKSIZE
24
25 /*
26         NodeTimer
27 */
28
29 void NodeTimer::serialize(std::ostream &os) const
30 {
31         writeF1000(os, timeout);
32         writeF1000(os, elapsed);
33 }
34
35 void NodeTimer::deSerialize(std::istream &is)
36 {
37         timeout = readF1000(is);
38         elapsed = readF1000(is);
39 }
40
41 /*
42         NodeTimerList
43 */
44
45 void NodeTimerList::serialize(std::ostream &os) const
46 {
47         /*
48                 Version 0 is a placeholder for "nothing to see here; go away."
49         */
50
51         if(m_data.size() == 0){
52                 writeU8(os, 0); // version
53                 return;
54         }
55
56         writeU8(os, 1); // version
57         writeU16(os, m_data.size());
58
59         for(std::map<v3s16, NodeTimer>::const_iterator
60                         i = m_data.begin();
61                         i != m_data.end(); i++){
62                 v3s16 p = i->first;
63                 NodeTimer t = i->second;
64
65                 u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
66                 writeU16(os, p16);
67                 t.serialize(os);
68         }
69 }
70
71 void NodeTimerList::deSerialize(std::istream &is)
72 {
73         m_data.clear();
74
75         u8 version = readU8(is);
76         if(version == 0)
77                 return;
78         if(version != 1)
79                 throw SerializationError("unsupported NodeTimerList version");
80
81         u16 count = readU16(is);
82
83         for(u16 i=0; i<count; i++)
84         {
85                 u16 p16 = readU16(is);
86
87                 v3s16 p(0,0,0);
88                 p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
89                 p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
90                 p.Y += p16 / MAP_BLOCKSIZE;
91                 p16 -= p.Y * MAP_BLOCKSIZE;
92                 p.X += p16;
93
94                 NodeTimer t;
95                 t.deSerialize(is);
96
97                 if(t.timeout <= 0)
98                 {
99                         infostream<<"WARNING: NodeTimerList::deSerialize(): "
100                                         <<"invalid data at position"
101                                         <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
102                                         <<std::endl;
103                         continue;
104                 }
105
106                 if(m_data.find(p) != m_data.end())
107                 {
108                         infostream<<"WARNING: NodeTimerList::deSerialize(): "
109                                         <<"already set data at position"
110                                         <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
111                                         <<std::endl;
112                         continue;
113                 }
114
115                 m_data.insert(std::make_pair(p, t));
116         }
117 }
118
119 std::map<v3s16, NodeTimer> NodeTimerList::step(float dtime)
120 {
121         std::map<v3s16, NodeTimer> elapsed_timers;
122         // Increment timers
123         for(std::map<v3s16, NodeTimer>::iterator
124                         i = m_data.begin();
125                         i != m_data.end(); i++){
126                 v3s16 p = i->first;
127                 NodeTimer t = i->second;
128                 t.elapsed += dtime;
129                 if(t.elapsed >= t.timeout)
130                         elapsed_timers.insert(std::make_pair(p, t));
131                 else
132                         i->second = t;
133         }
134         // Delete elapsed timers
135         for(std::map<v3s16, NodeTimer>::const_iterator
136                         i = elapsed_timers.begin();
137                         i != elapsed_timers.end(); i++){
138                 v3s16 p = i->first;
139                 m_data.erase(p);
140         }
141         return elapsed_timers;
142 }