]> git.lizzy.rs Git - minetest.git/blob - src/mg_schematic.h
Cpp11 patchset 11: continue working on constructor style migration (#6004)
[minetest.git] / src / mg_schematic.h
1 /*
2 Minetest
3 Copyright (C) 2014-2016 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2015-2017 paramat
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #ifndef MG_SCHEMATIC_HEADER
22 #define MG_SCHEMATIC_HEADER
23
24 #include <map>
25 #include "mg_decoration.h"
26 #include "util/string.h"
27
28 class Map;
29 class ServerMap;
30 class Mapgen;
31 class MMVManip;
32 class PseudoRandom;
33 class NodeResolver;
34 class Server;
35
36 /*
37         Minetest Schematic File Format
38
39         All values are stored in big-endian byte order.
40         [u32] signature: 'MTSM'
41         [u16] version: 4
42         [u16] size X
43         [u16] size Y
44         [u16] size Z
45         For each Y:
46                 [u8] slice probability value
47         [Name-ID table] Name ID Mapping Table
48                 [u16] name-id count
49                 For each name-id mapping:
50                         [u16] name length
51                         [u8[]] name
52         ZLib deflated {
53         For each node in schematic:  (for z, y, x)
54                 [u16] content
55         For each node in schematic:
56                 [u8] param1
57                   bit 0-6: probability
58                   bit 7:   specific node force placement
59         For each node in schematic:
60                 [u8] param2
61         }
62
63         Version changes:
64         1 - Initial version
65         2 - Fixed messy never/always place; 0 probability is now never, 0xFF is always
66         3 - Added y-slice probabilities; this allows for variable height structures
67         4 - Compressed range of node occurence prob., added per-node force placement bit
68 */
69
70 //// Schematic constants
71 #define MTSCHEM_FILE_SIGNATURE 0x4d54534d // 'MTSM'
72 #define MTSCHEM_FILE_VER_HIGHEST_READ  4
73 #define MTSCHEM_FILE_VER_HIGHEST_WRITE 4
74
75 #define MTSCHEM_PROB_MASK       0x7F
76
77 #define MTSCHEM_PROB_NEVER      0x00
78 #define MTSCHEM_PROB_ALWAYS     0x7F
79 #define MTSCHEM_PROB_ALWAYS_OLD 0xFF
80
81 #define MTSCHEM_FORCE_PLACE     0x80
82
83 enum SchematicType
84 {
85         SCHEMATIC_NORMAL,
86 };
87
88 enum SchematicFormatType {
89         SCHEM_FMT_HANDLE,
90         SCHEM_FMT_MTS,
91         SCHEM_FMT_LUA,
92 };
93
94 class Schematic : public ObjDef, public NodeResolver {
95 public:
96         Schematic();
97         virtual ~Schematic();
98
99         virtual void resolveNodeNames();
100
101         bool loadSchematicFromFile(const std::string &filename, INodeDefManager *ndef,
102                 StringMap *replace_names=NULL);
103         bool saveSchematicToFile(const std::string &filename, INodeDefManager *ndef);
104         bool getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2);
105
106         bool deserializeFromMts(std::istream *is, std::vector<std::string> *names);
107         bool serializeToMts(std::ostream *os, const std::vector<std::string> &names);
108         bool serializeToLua(std::ostream *os, const std::vector<std::string> &names,
109                 bool use_comments, u32 indent_spaces);
110
111         void blitToVManip(MMVManip *vm, v3s16 p, Rotation rot, bool force_place);
112         bool placeOnVManip(MMVManip *vm, v3s16 p, u32 flags, Rotation rot, bool force_place);
113         void placeOnMap(ServerMap *map, v3s16 p, u32 flags, Rotation rot, bool force_place);
114
115         void applyProbabilities(v3s16 p0,
116                 std::vector<std::pair<v3s16, u8> > *plist,
117                 std::vector<std::pair<s16, u8> > *splist);
118
119         std::vector<content_t> c_nodes;
120         u32 flags = 0;
121         v3s16 size;
122         MapNode *schemdata = nullptr;
123         u8 *slice_probs = nullptr;
124 };
125
126 class SchematicManager : public ObjDefManager {
127 public:
128         SchematicManager(Server *server);
129         virtual ~SchematicManager() {}
130
131         virtual void clear();
132
133         const char *getObjectTitle() const
134         {
135                 return "schematic";
136         }
137
138         static Schematic *create(SchematicType type)
139         {
140                 return new Schematic;
141         }
142
143 private:
144         Server *m_server;
145 };
146
147 void generate_nodelist_and_update_ids(MapNode *nodes, size_t nodecount,
148         std::vector<std::string> *usednodes, INodeDefManager *ndef);
149
150 #endif