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