Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 350 lines (294 sloc) 7.517 kb
e84d18ea » James Mitchell
2010-11-27 Adding chunkmap.h
1 /*
2 Copyright (c) 2010, The Mineserver Project
3 All rights reserved.
4
080bd08b » deoxxa
2011-01-10 fixed formatting of license, it was annoying me to no end
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the The Mineserver Project nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
e84d18ea » James Mitchell
2010-11-27 Adding chunkmap.h
15
080bd08b » deoxxa
2011-01-10 fixed formatting of license, it was annoying me to no end
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
26 */
e84d18ea » James Mitchell
2010-11-27 Adding chunkmap.h
27
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
28 #ifndef _CHUNKMAP_H
29 #define _CHUNKMAP_H
e84d18ea » James Mitchell
2010-11-27 Adding chunkmap.h
30
31 #include <set>
32 #include <list>
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
33 #include <vector>
60b591e9 » deoxxa
2011-01-14 fixed a segfault on unloading chunks (i think?)
34 #include <iostream>
e84d18ea » James Mitchell
2010-11-27 Adding chunkmap.h
35
f47333a5 » donno
2011-01-09 Minimised Include dependices. This was done by changing applicable ca…
36 #include "packets.h"
37 #include "user.h"
38
e84d18ea » James Mitchell
2010-11-27 Adding chunkmap.h
39 class NBT_Value;
7f124dab » deoxxa
2011-01-15 killed most of the memory leaks
40
41 struct spawnedItem
42 {
43 int EID;
44 int16_t item;
45 char count;
46 int16_t health;
47 vec pos;
48 time_t spawnedAt;
49 uint32_t spawnedBy;
50
51 spawnedItem()
52 {
53 spawnedAt = time(0);
54 spawnedBy = 0;
55 }
56 };
57
58 struct chestData
59 {
60 int32_t x;
61 int32_t y;
62 int32_t z;
63 Item items[27];
64 };
65
66 struct signData
67 {
68 int32_t x;
69 int32_t y;
70 int32_t z;
71 std::string text1;
72 std::string text2;
73 std::string text3;
74 std::string text4;
75 };
76
77 struct furnaceData
78 {
79 int32_t x;
80 int32_t y;
81 int32_t z;
82 Item items[3];
83 int16_t burnTime;
84 int16_t cookTime;
85 };
e84d18ea » James Mitchell
2010-11-27 Adding chunkmap.h
86
87 struct sChunk
88 {
090ea7fe » donno
2011-01-09 Changed from using the typedef types that to use stdint.
89 uint8_t* blocks;
90 uint8_t* data;
91 uint8_t* blocklight;
92 uint8_t* skylight;
93 uint8_t* heightmap;
94 int32_t x;
95 int32_t z;
2258d30d » deoxxa
2010-12-27 fixed some warnings
96
97 int refCount;
7b8da724 » fador
2010-12-24 some changed on chunk access/save/lightgen
98 bool lightRegen;
99 bool changed;
100 time_t lastused;
101
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
102 NBT_Value* nbt;
103 std::set<User*> users;
104 std::vector<spawnedItem*> items;
e84d18ea » James Mitchell
2010-11-27 Adding chunkmap.h
105
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
106 // ToDo: clear these
107 std::vector<chestData*> chests;
108 std::vector<signData*> signs;
109 std::vector<furnaceData*> furnaces;
110
7f124dab » deoxxa
2011-01-15 killed most of the memory leaks
111 sChunk() : refCount(0),lightRegen(false),changed(false),lastused(0)
112 {
113 }
114
115 ~sChunk()
116 {
117 std::vector<chestData*>::iterator chest_it = chests.begin();
118 for (;chest_it!=chests.end();++chest_it)
119 {
120 delete *chest_it;
121 }
122 chests.clear();
123
124 std::vector<signData*>::iterator sign_it = signs.begin();
125 for (;sign_it!=signs.end();++sign_it)
126 {
127 delete *sign_it;
128 }
129 signs.clear();
130
131 std::vector<furnaceData*>::iterator furnace_it = furnaces.begin();
132 for (;furnace_it!=furnaces.end();++furnace_it)
133 {
134 delete *furnace_it;
135 }
136 furnaces.clear();
137
138 delete nbt;
139 }
140
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
141 bool hasUser(User* user)
e84d18ea » James Mitchell
2010-11-27 Adding chunkmap.h
142 {
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
143 return users.count(user) != 0;
e84d18ea » James Mitchell
2010-11-27 Adding chunkmap.h
144 }
145
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
146 void sendPacket(const Packet &packet, User* nosend = NULL)
e84d18ea » James Mitchell
2010-11-27 Adding chunkmap.h
147 {
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
148 std::set<User*>::iterator iter_a = users.begin(), iter_b = users.end();
6d4fccf5 » bakkdoor
2010-12-13 More refactorings (coding style).
149
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
150 for (;iter_a!=iter_b;++iter_a)
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
151 {
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
152 if ((*iter_a) != nosend)
153 {
154 (*iter_a)->buffer.addToWrite(packet.getWrite(), packet.getWriteLen());
155 }
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
156 }
e84d18ea » James Mitchell
2010-11-27 Adding chunkmap.h
157 }
158
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
159 static bool userBoundary(sChunk* left, std::list<User*> &lusers, sChunk* right, std::list<User*> &rusers)
e84d18ea » James Mitchell
2010-11-27 Adding chunkmap.h
160 {
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
161 bool diff = false;
162
163 std::set<User*>::iterator iter_a;
164 std::set<User*>::iterator iter_b;
165
166 iter_a = left->users.begin(), iter_b = left->users.end();
167 for(;iter_a!=iter_b;++iter_a)
168 {
169 if (!right->users.count(*iter_a))
170 {
171 lusers.push_front(*iter_a);
172 diff = true;
173 }
174 }
175
176 iter_a = right->users.begin(), iter_b = right->users.end();
177 for(;iter_a!=iter_b;++iter_a)
178 {
179 if (!left->users.count(*iter_a))
180 {
181 rusers.push_front(*iter_a);
182 diff = true;
183 }
184 }
185
186 return diff;
e84d18ea » James Mitchell
2010-11-27 Adding chunkmap.h
187 }
188 };
189
190 struct sChunkNode
191 {
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
192 sChunkNode(sChunk* _chunk, sChunkNode* _prev, sChunkNode* _next) : chunk(_chunk),prev(_prev),next(_next) {}
7f124dab » deoxxa
2011-01-15 killed most of the memory leaks
193
194 ~sChunkNode()
195 {
196 if (chunk != NULL)
197 {
198 delete chunk;
199 }
200 }
201
6d4fccf5 » bakkdoor
2010-12-13 More refactorings (coding style).
202 sChunk* chunk;
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
203 sChunkNode* prev;
728172ba » deoxxa
2011-01-13 fixed a segfault
204 sChunkNode* next;
e84d18ea » James Mitchell
2010-11-27 Adding chunkmap.h
205 };
206
207 class ChunkMap
208 {
209 public:
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
210 ChunkMap()
211 {
212 memset(m_buckets, 0, sizeof(m_buckets));
213 }
214
7f124dab » deoxxa
2011-01-15 killed most of the memory leaks
215 ~ChunkMap()
216 {
217 for (int i=0;i<441;++i)
218 {
219 sChunkNode* node = m_buckets[i];
220 sChunkNode* next = NULL;
221 if (node != NULL)
222 {
223 next = node->next;
224 delete node;
225 node = next;
226 }
227 m_buckets[i] = NULL;
228 }
229 }
230
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
231 int hash(int x, int z)
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
232 {
233 x %= 21;
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
234 if (x < 0)
235 {
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
236 x += 21;
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
237 }
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
238
239 z %= 21;
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
240 if (z < 0)
241 {
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
242 z += 21;
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
243 }
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
244
245 return x + z * 21;
246 }
247
7f124dab » deoxxa
2011-01-15 killed most of the memory leaks
248 int numChunks()
249 {
250 int num = 0;
251
252 for (int i=0;i<441;++i)
253 for (sChunkNode* node = m_buckets[i];node!=NULL;node=node->next)
254 num++;
255
256 return num;
257 }
258
728172ba » deoxxa
2011-01-13 fixed a segfault
259 sChunk* getChunk(int x, int z)
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
260 {
728172ba » deoxxa
2011-01-13 fixed a segfault
261 sChunkNode* node = NULL;
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
262
728172ba » deoxxa
2011-01-13 fixed a segfault
263 for (node=m_buckets[hash(x,z)];node!=NULL;node=node->next)
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
264 {
728172ba » deoxxa
2011-01-13 fixed a segfault
265 if ((node->chunk->x == x) && (node->chunk->z == z))
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
266 {
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
267 return node->chunk;
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
268 }
269 }
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
270
271 return NULL;
272 }
273
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
274 void unlinkChunk(int x, int z)
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
275 {
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
276 int _hash = hash(x, z);
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
277
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
278 sChunkNode* root = m_buckets[_hash];
279 sChunkNode* node = root;
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
280
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
281 // Loop until we reach the end of the chain
282 while (node != NULL)
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
283 {
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
284 // We've got the right node, time to get to work!
285 if ((node->chunk->x == x) && (node->chunk->z == z))
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
286 {
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
287 node->chunk->refCount--;
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
288
7f124dab » deoxxa
2011-01-15 killed most of the memory leaks
289 if (node->chunk->refCount == 0)
290 {
291 delete node->chunk;
292 }
293
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
294 // If we have both next and previous nodes, we need to connect them up
295 // when we remove this node because we're in the middle of the chain.
296 if (node->next != NULL && node->prev != NULL)
297 {
298 node->next->prev = node->prev;
299 node->prev->next = node->next;
300 }
301 // Otherwise we're at one of the ends of the chain, so we just need to
302 // cut it off where it is.
303 else if (node->next != NULL)
304 {
305 node->next->prev = NULL;
306 }
307 else if (node->prev != NULL)
308 {
309 node->prev->next = NULL;
310 }
311
312 // If the node we're looking at is the root node, we need to update the
313 // bucket to point at the new start of the chain.
314 if (node == root)
315 {
316 m_buckets[_hash] = node->next;
317 }
318
319 // Free up the memory we were using for this node.
320 delete node;
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
321
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
322 return;
323 }
324 // This isn't the right node, look at the next one.
325 else
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
326 {
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
327 node = node->next;
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
328 }
329 }
330 }
331
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
332 void linkChunk(sChunk* chunk, int x, int z)
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
333 {
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
334 int _hash = hash(x, z);
60b591e9 » deoxxa
2011-01-14 fixed a segfault on unloading chunks (i think?)
335
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
336 chunk->refCount++;
60b591e9 » deoxxa
2011-01-14 fixed a segfault on unloading chunks (i think?)
337
59084f96 » deoxxa
2011-01-13 refactored a couple of functions to make them clearer
338 m_buckets[_hash] = new sChunkNode(chunk, NULL, m_buckets[_hash]);
60b591e9 » deoxxa
2011-01-14 fixed a segfault on unloading chunks (i think?)
339
340 if (m_buckets[_hash]->next != NULL)
341 {
342 m_buckets[_hash]->next->prev = m_buckets[_hash];
343 }
d6ac884e » fador
2010-12-28 more warning fixes and tabs -> spaces
344 }
345
728172ba » deoxxa
2011-01-13 fixed a segfault
346 private:
347 sChunkNode* m_buckets[441];
e84d18ea » James Mitchell
2010-11-27 Adding chunkmap.h
348 };
349
bbaf5b38 » James Mitchell
2010-12-04 Some fixes
350 #endif
Something went wrong with that request. Please try again.