remove_detached_inventory: Fix segfault during mod load
[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 "serialization.h"
23 #include "util/serialize.h"
24 #include "constants.h" // MAP_BLOCKSIZE
25
26 /*
27         NodeTimer
28 */
29
30 void NodeTimer::serialize(std::ostream &os) const
31 {
32         writeF1000(os, timeout);
33         writeF1000(os, elapsed);
34 }
35
36 void NodeTimer::deSerialize(std::istream &is)
37 {
38         timeout = readF1000(is);
39         elapsed = readF1000(is);
40 }
41
42 /*
43         NodeTimerList
44 */
45
46 void NodeTimerList::serialize(std::ostream &os, u8 map_format_version) const
47 {
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
52                         return;
53                 }
54                 writeU8(os, 1); // version
55                 writeU16(os, m_timers.size());
56         }
57
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());
61         }
62
63         for (const auto &timer : m_timers) {
64                 NodeTimer t = timer.second;
65                 NodeTimer nt = NodeTimer(t.timeout,
66                         t.timeout - (f32)(timer.first - m_time), t.position);
67                 v3s16 p = t.position;
68
69                 u16 p16 = p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE + p.Y * MAP_BLOCKSIZE + p.X;
70                 writeU16(os, p16);
71                 nt.serialize(os);
72         }
73 }
74
75 void NodeTimerList::deSerialize(std::istream &is, u8 map_format_version)
76 {
77         clear();
78
79         if (map_format_version == 24) {
80                 u8 timer_version = readU8(is);
81                 if(timer_version == 0)
82                         return;
83                 if(timer_version != 1)
84                         throw SerializationError("unsupported NodeTimerList version");
85         }
86
87         if (map_format_version >= 25) {
88                 u8 timer_data_len = readU8(is);
89                 if(timer_data_len != 2+4+4)
90                         throw SerializationError("unsupported NodeTimer data length");
91         }
92
93         u16 count = readU16(is);
94
95         for (u16 i = 0; i < count; i++) {
96                 u16 p16 = readU16(is);
97
98                 v3s16 p;
99                 p.Z = p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
100                 p16 &= MAP_BLOCKSIZE * MAP_BLOCKSIZE - 1;
101                 p.Y = p16 / MAP_BLOCKSIZE;
102                 p16 &= MAP_BLOCKSIZE - 1;
103                 p.X = p16;
104
105                 NodeTimer t(p);
106                 t.deSerialize(is);
107
108                 if (t.timeout <= 0) {
109                         warningstream<<"NodeTimerList::deSerialize(): "
110                                         <<"invalid data at position"
111                                         <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
112                                         <<std::endl;
113                         continue;
114                 }
115
116                 if (m_iterators.find(p) != m_iterators.end()) {
117                         warningstream<<"NodeTimerList::deSerialize(): "
118                                         <<"already set data at position"
119                                         <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
120                                         <<std::endl;
121                         continue;
122                 }
123
124                 insert(t);
125         }
126 }
127
128 std::vector<NodeTimer> NodeTimerList::step(float dtime)
129 {
130         std::vector<NodeTimer> elapsed_timers;
131         m_time += dtime;
132         if (m_next_trigger_time == -1. || m_time < m_next_trigger_time) {
133                 return elapsed_timers;
134         }
135         std::multimap<double, NodeTimer>::iterator i = m_timers.begin();
136         // Process timers
137         for (; i != m_timers.end() && i->first <= m_time; ++i) {
138                 NodeTimer t = i->second;
139                 t.elapsed = t.timeout + (f32)(m_time - i->first);
140                 elapsed_timers.push_back(t);
141                 m_iterators.erase(t.position);
142         }
143         // Delete elapsed timers
144         m_timers.erase(m_timers.begin(), i);
145         if (m_timers.empty())
146                 m_next_trigger_time = -1.;
147         else
148                 m_next_trigger_time = m_timers.begin()->first;
149         return elapsed_timers;
150 }