utility.h: Change Buffer's interface to be more compatible with SharedBuffer's interf...
[oweals/minetest.git] / src / mapsector.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 /*
21 (c) 2010 Perttu Ahola <celeron55@gmail.com>
22 */
23
24 #ifndef MAPSECTOR_HEADER
25 #define MAPSECTOR_HEADER
26
27 #include <jmutex.h>
28 #include "common_irrlicht.h"
29 #include "exceptions.h"
30 #include <ostream>
31
32 class MapBlock;
33 class Map;
34
35 /*
36         This is an Y-wise stack of MapBlocks.
37 */
38
39 #define MAPSECTOR_SERVER 0
40 #define MAPSECTOR_CLIENT 1
41
42 class MapSector
43 {
44 public:
45         
46         MapSector(Map *parent, v2s16 pos);
47         virtual ~MapSector();
48
49         virtual u32 getId() const = 0;
50
51         void deleteBlocks();
52
53         v2s16 getPos()
54         {
55                 return m_pos;
56         }
57
58         MapBlock * getBlockNoCreateNoEx(s16 y);
59         MapBlock * createBlankBlockNoInsert(s16 y);
60         MapBlock * createBlankBlock(s16 y);
61
62         void insertBlock(MapBlock *block);
63         
64         void deleteBlock(MapBlock *block);
65         
66         void getBlocks(core::list<MapBlock*> &dest);
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         core::map<s16, MapBlock*> m_blocks;
75
76         Map *m_parent;
77         // Position on parent (in MapBlock widths)
78         v2s16 m_pos;
79         
80         // Last-used block is cached here for quicker access.
81         // Be sure to set this to NULL when the cached block is deleted 
82         MapBlock *m_block_cache;
83         s16 m_block_cache_y;
84         
85         /*
86                 Private methods
87         */
88         MapBlock *getBlockBuffered(s16 y);
89
90 };
91
92 class ServerMapSector : public MapSector
93 {
94 public:
95         ServerMapSector(Map *parent, v2s16 pos);
96         ~ServerMapSector();
97         
98         u32 getId() const
99         {
100                 return MAPSECTOR_SERVER;
101         }
102
103         /*
104                 These functions handle metadata.
105                 They do not handle blocks.
106         */
107
108         void serialize(std::ostream &os, u8 version);
109         
110         static ServerMapSector* deSerialize(
111                         std::istream &is,
112                         Map *parent,
113                         v2s16 p2d,
114                         core::map<v2s16, MapSector*> & sectors
115                 );
116                 
117 private:
118 };
119
120 #ifndef SERVER
121 class ClientMapSector : public MapSector
122 {
123 public:
124         ClientMapSector(Map *parent, v2s16 pos);
125         ~ClientMapSector();
126         
127         u32 getId() const
128         {
129                 return MAPSECTOR_CLIENT;
130         }
131
132 private:
133 };
134 #endif
135         
136 #endif
137