]> git.lizzy.rs Git - minetest.git/blob - src/mg_schematic.h
Schematics: Reorganize (de)serialization and add Lua serialization API
[minetest.git] / src / mg_schematic.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
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 MG_SCHEMATIC_HEADER
21 #define MG_SCHEMATIC_HEADER
22
23 #include <map>
24 #include "mg_decoration.h"
25 #include "util/string.h"
26
27 class Map;
28 class Mapgen;
29 class MMVManip;
30 class PseudoRandom;
31 class NodeResolver;
32
33 /*
34         Minetest Schematic File Format
35
36         All values are stored in big-endian byte order.
37         [u32] signature: 'MTSM'
38         [u16] version: 3
39         [u16] size X
40         [u16] size Y
41         [u16] size Z
42         For each Y:
43                 [u8] slice probability value
44         [Name-ID table] Name ID Mapping Table
45                 [u16] name-id count
46                 For each name-id mapping:
47                         [u16] name length
48                         [u8[]] name
49         ZLib deflated {
50         For each node in schematic:  (for z, y, x)
51                 [u16] content
52         For each node in schematic:
53                 [u8] probability of occurance (param1)
54         For each node in schematic:
55                 [u8] param2
56         }
57
58         Version changes:
59         1 - Initial version
60         2 - Fixed messy never/always place; 0 probability is now never, 0xFF is always
61         3 - Added y-slice probabilities; this allows for variable height structures
62 */
63
64 /////////////////// Schematic flags
65 #define SCHEM_CIDS_UPDATED 0x08
66
67 #define MTSCHEM_FILE_SIGNATURE 0x4d54534d // 'MTSM'
68 #define MTSCHEM_FILE_VER_HIGHEST_READ  3
69 #define MTSCHEM_FILE_VER_HIGHEST_WRITE 3
70
71 #define MTSCHEM_PROB_NEVER  0x00
72 #define MTSCHEM_PROB_ALWAYS 0xFF
73
74 enum SchematicType
75 {
76         SCHEMATIC_NORMAL,
77 };
78
79 enum SchematicFormatType {
80         SCHEM_FMT_HANDLE,
81         SCHEM_FMT_MTS,
82         SCHEM_FMT_LUA,
83 };
84
85 class Schematic : public ObjDef, public NodeResolver {
86 public:
87         std::vector<content_t> c_nodes;
88
89         u32 flags;
90         v3s16 size;
91         MapNode *schemdata;
92         u8 *slice_probs;
93
94         Schematic();
95         virtual ~Schematic();
96
97         virtual void resolveNodeNames(NodeResolveInfo *nri);
98
99         void updateContentIds();
100
101         void blitToVManip(v3s16 p, MMVManip *vm,
102                 Rotation rot, bool force_placement, INodeDefManager *ndef);
103
104         bool loadSchematicFromFile(const char *filename, INodeDefManager *ndef,
105                 StringMap *replace_names);
106         bool saveSchematicToFile(const char *filename, INodeDefManager *ndef);
107         bool getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2);
108
109         bool deserializeFromMts(std::istream *is, INodeDefManager *ndef,
110                 std::vector<std::string> *names);
111         bool serializeToMts(std::ostream *os, INodeDefManager *ndef);
112         bool serializeToLua(std::ostream *os,
113                 INodeDefManager *ndef, bool use_comments);
114
115
116         void placeStructure(Map *map, v3s16 p, u32 flags,
117                 Rotation rot, bool force_placement, INodeDefManager *nef);
118         void applyProbabilities(v3s16 p0,
119                 std::vector<std::pair<v3s16, u8> > *plist,
120                 std::vector<std::pair<s16, u8> > *splist);
121
122         std::string getAsLuaTable(INodeDefManager *ndef, bool use_comments);
123 };
124
125 class SchematicManager : public ObjDefManager {
126 public:
127         SchematicManager(IGameDef *gamedef);
128         ~SchematicManager() {}
129
130         const char *getObjectTitle() const
131         {
132                 return "schematic";
133         }
134
135         static Schematic *create(SchematicType type)
136         {
137                 return new Schematic;
138         }
139 };
140
141 void build_nnlist_and_update_ids(MapNode *nodes, u32 nodecount,
142         std::vector<content_t> *usednodes);
143
144
145 #endif