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 #include "nodetimer.h"
22 #include "serialization.h"
23 #include "util/serialize.h"
24 #include "constants.h" // MAP_BLOCKSIZE
30 void NodeTimer::serialize(std::ostream &os) const
32 writeF1000(os, timeout);
33 writeF1000(os, elapsed);
36 void NodeTimer::deSerialize(std::istream &is)
38 timeout = readF1000(is);
39 elapsed = readF1000(is);
46 void NodeTimerList::serialize(std::ostream &os, u8 map_format_version) const
48 if (map_format_version == 24) {
49 // Version 0 is a placeholder for "nothing to see here; go away."
50 if (m_timers.empty()) {
51 writeU8(os, 0); // version
54 writeU8(os, 1); // version
55 writeU16(os, m_timers.size());
58 if (map_format_version >= 25) {
59 writeU8(os, 2 + 4 + 4); // length of the data for a single timer
60 writeU16(os, m_timers.size());
63 for (std::multimap<double, NodeTimer>::const_iterator
65 i != m_timers.end(); ++i) {
66 NodeTimer t = i->second;
67 NodeTimer nt = NodeTimer(t.timeout,
68 t.timeout - (f32)(i->first - m_time), t.position);
71 u16 p16 = p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE + p.Y * MAP_BLOCKSIZE + p.X;
77 void NodeTimerList::deSerialize(std::istream &is, u8 map_format_version)
81 if (map_format_version == 24) {
82 u8 timer_version = readU8(is);
83 if(timer_version == 0)
85 if(timer_version != 1)
86 throw SerializationError("unsupported NodeTimerList version");
89 if (map_format_version >= 25) {
90 u8 timer_data_len = readU8(is);
91 if(timer_data_len != 2+4+4)
92 throw SerializationError("unsupported NodeTimer data length");
95 u16 count = readU16(is);
97 for (u16 i = 0; i < count; i++) {
98 u16 p16 = readU16(is);
101 p.Z = p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
102 p16 &= MAP_BLOCKSIZE * MAP_BLOCKSIZE - 1;
103 p.Y = p16 / MAP_BLOCKSIZE;
104 p16 &= MAP_BLOCKSIZE - 1;
110 if (t.timeout <= 0) {
111 warningstream<<"NodeTimerList::deSerialize(): "
112 <<"invalid data at position"
113 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
118 if (m_iterators.find(p) != m_iterators.end()) {
119 warningstream<<"NodeTimerList::deSerialize(): "
120 <<"already set data at position"
121 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
130 std::vector<NodeTimer> NodeTimerList::step(float dtime)
132 std::vector<NodeTimer> elapsed_timers;
134 if (m_next_trigger_time == -1. || m_time < m_next_trigger_time) {
135 return elapsed_timers;
137 std::multimap<double, NodeTimer>::iterator i = m_timers.begin();
139 for (; i != m_timers.end() && i->first <= m_time; ++i) {
140 NodeTimer t = i->second;
141 t.elapsed = t.timeout + (f32)(m_time - i->first);
142 elapsed_timers.push_back(t);
143 m_iterators.erase(t.position);
145 // Delete elapsed timers
146 m_timers.erase(m_timers.begin(), i);
147 if (m_timers.empty())
148 m_next_trigger_time = -1.;
150 m_next_trigger_time = m_timers.begin()->first;
151 return elapsed_timers;