]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapsector.cpp
Don't ignore server disconnects in client code
[dragonfireclient.git] / src / mapsector.cpp
1 /*
2 Minetest
3 Copyright (C) 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 "mapsector.h"
21 #include "exceptions.h"
22 #include "mapblock.h"
23 #include "serialization.h"
24
25 MapSector::MapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
26                 m_parent(parent),
27                 m_pos(pos),
28                 m_gamedef(gamedef)
29 {
30 }
31
32 MapSector::~MapSector()
33 {
34         deleteBlocks();
35 }
36
37 void MapSector::deleteBlocks()
38 {
39         // Clear cache
40         m_block_cache = nullptr;
41
42         // Delete all
43         for (auto &block : m_blocks) {
44                 delete block.second;
45         }
46
47         // Clear container
48         m_blocks.clear();
49 }
50
51 MapBlock * MapSector::getBlockBuffered(s16 y)
52 {
53         MapBlock *block;
54
55         if (m_block_cache && y == m_block_cache_y) {
56                 return m_block_cache;
57         }
58
59         // If block doesn't exist, return NULL
60         std::unordered_map<s16, MapBlock*>::const_iterator n = m_blocks.find(y);
61         block = (n != m_blocks.end() ? n->second : nullptr);
62
63         // Cache the last result
64         m_block_cache_y = y;
65         m_block_cache = block;
66
67         return block;
68 }
69
70 MapBlock * MapSector::getBlockNoCreateNoEx(s16 y)
71 {
72         return getBlockBuffered(y);
73 }
74
75 MapBlock * MapSector::createBlankBlockNoInsert(s16 y)
76 {
77         assert(getBlockBuffered(y) == NULL);    // Pre-condition
78
79         v3s16 blockpos_map(m_pos.X, y, m_pos.Y);
80
81         MapBlock *block = new MapBlock(m_parent, blockpos_map, m_gamedef);
82
83         return block;
84 }
85
86 MapBlock * MapSector::createBlankBlock(s16 y)
87 {
88         MapBlock *block = createBlankBlockNoInsert(y);
89
90         m_blocks[y] = block;
91
92         return block;
93 }
94
95 void MapSector::insertBlock(MapBlock *block)
96 {
97         s16 block_y = block->getPos().Y;
98
99         MapBlock *block2 = getBlockBuffered(block_y);
100         if (block2) {
101                 throw AlreadyExistsException("Block already exists");
102         }
103
104         v2s16 p2d(block->getPos().X, block->getPos().Z);
105         assert(p2d == m_pos);
106
107         // Insert into container
108         m_blocks[block_y] = block;
109 }
110
111 void MapSector::deleteBlock(MapBlock *block)
112 {
113         s16 block_y = block->getPos().Y;
114
115         // Clear from cache
116         m_block_cache = nullptr;
117
118         // Remove from container
119         m_blocks.erase(block_y);
120
121         // Delete
122         delete block;
123 }
124
125 void MapSector::getBlocks(MapBlockVect &dest)
126 {
127         for (auto &block : m_blocks) {
128                 dest.push_back(block.second);
129         }
130 }