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