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