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