C++11 patchset 2: remove util/cpp11.h and util/cpp11_container.h (#5821)
[oweals/minetest.git] / src / mapsector.h
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 #ifndef MAPSECTOR_HEADER
21 #define MAPSECTOR_HEADER
22
23 #include "irrlichttypes.h"
24 #include "irr_v2d.h"
25 #include "mapblock.h"
26 #include <ostream>
27 #include <map>
28 #include <vector>
29
30 class Map;
31 class IGameDef;
32
33 /*
34         This is an Y-wise stack of MapBlocks.
35 */
36
37 #define MAPSECTOR_SERVER 0
38 #define MAPSECTOR_CLIENT 1
39
40 class MapSector
41 {
42 public:
43
44         MapSector(Map *parent, v2s16 pos, IGameDef *gamedef);
45         virtual ~MapSector();
46
47         virtual u32 getId() const = 0;
48
49         void deleteBlocks();
50
51         v2s16 getPos()
52         {
53                 return m_pos;
54         }
55
56         MapBlock * getBlockNoCreateNoEx(s16 y);
57         MapBlock * createBlankBlockNoInsert(s16 y);
58         MapBlock * createBlankBlock(s16 y);
59
60         void insertBlock(MapBlock *block);
61
62         void deleteBlock(MapBlock *block);
63
64         void getBlocks(MapBlockVect &dest);
65
66         bool empty() const { return m_blocks.empty(); }
67
68         // Always false at the moment, because sector contains no metadata.
69         bool differs_from_disk;
70
71 protected:
72
73         // The pile of MapBlocks
74         std::unordered_map<s16, MapBlock*> m_blocks;
75
76         Map *m_parent;
77         // Position on parent (in MapBlock widths)
78         v2s16 m_pos;
79
80         IGameDef *m_gamedef;
81
82         // Last-used block is cached here for quicker access.
83         // Be sure to set this to NULL when the cached block is deleted
84         MapBlock *m_block_cache;
85         s16 m_block_cache_y;
86
87         /*
88                 Private methods
89         */
90         MapBlock *getBlockBuffered(s16 y);
91
92 };
93
94 class ServerMapSector : public MapSector
95 {
96 public:
97         ServerMapSector(Map *parent, v2s16 pos, IGameDef *gamedef);
98         ~ServerMapSector();
99
100         u32 getId() const
101         {
102                 return MAPSECTOR_SERVER;
103         }
104
105         /*
106                 These functions handle metadata.
107                 They do not handle blocks.
108         */
109
110         void serialize(std::ostream &os, u8 version);
111
112         static ServerMapSector* deSerialize(
113                         std::istream &is,
114                         Map *parent,
115                         v2s16 p2d,
116                         std::map<v2s16, MapSector*> & sectors,
117                         IGameDef *gamedef
118                 );
119
120 private:
121 };
122
123 #ifndef SERVER
124 class ClientMapSector : public MapSector
125 {
126 public:
127         ClientMapSector(Map *parent, v2s16 pos, IGameDef *gamedef);
128         ~ClientMapSector();
129
130         u32 getId() const
131         {
132                 return MAPSECTOR_CLIENT;
133         }
134
135 private:
136 };
137 #endif
138
139 #endif
140