3 Copyright (C) 2010-2012 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 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.
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.
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.
20 #include "nodetimer.h"
28 void NodeTimer::serialize(std::ostream &os) const
30 writeF1000(os, duration);
31 writeF1000(os, elapsed);
34 void NodeTimer::deSerialize(std::istream &is)
36 duration = readF1000(is);
37 elapsed = readF1000(is);
44 void NodeTimerList::serialize(std::ostream &os) const
47 Version 0 is a placeholder for "nothing to see here; go away."
50 if(m_data.size() == 0){
51 writeU8(os, 0); // version
55 writeU8(os, 1); // version
56 writeU16(os, m_data.size());
58 for(std::map<v3s16, NodeTimer>::const_iterator
60 i != m_data.end(); i++){
62 NodeTimer t = i->second;
64 u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
70 void NodeTimerList::deSerialize(std::istream &is)
74 u8 version = readU8(is);
78 throw SerializationError("unsupported NodeTimerList version");
80 u16 count = readU16(is);
82 for(u16 i=0; i<count; i++)
84 u16 p16 = readU16(is);
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;
98 infostream<<"WARNING: NodeTimerList::deSerialize(): "
99 <<"invalid data at position"
100 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
105 if(m_data.find(p) != m_data.end())
107 infostream<<"WARNING: NodeTimerList::deSerialize(): "
108 <<"already set data at position"
109 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
114 m_data.insert(std::make_pair(p, t));
118 std::map<v3s16, f32> NodeTimerList::step(float dtime)
120 std::map<v3s16, f32> elapsed_timers;
122 for(std::map<v3s16, NodeTimer>::iterator
124 i != m_data.end(); i++){
126 NodeTimer t = i->second;
128 if(t.elapsed >= t.duration)
129 elapsed_timers.insert(std::make_pair(p, t.elapsed));
133 // Delete elapsed timers
134 for(std::map<v3s16, f32>::const_iterator
135 i = elapsed_timers.begin();
136 i != elapsed_timers.end(); i++){
140 return elapsed_timers;