Line data Source code
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 : class IGameDef;
33 :
34 : /*
35 : Minetest Schematic File Format
36 :
37 : All values are stored in big-endian byte order.
38 : [u32] signature: 'MTSM'
39 : [u16] version: 4
40 : [u16] size X
41 : [u16] size Y
42 : [u16] size Z
43 : For each Y:
44 : [u8] slice probability value
45 : [Name-ID table] Name ID Mapping Table
46 : [u16] name-id count
47 : For each name-id mapping:
48 : [u16] name length
49 : [u8[]] name
50 : ZLib deflated {
51 : For each node in schematic: (for z, y, x)
52 : [u16] content
53 : For each node in schematic:
54 : [u8] param1
55 : bit 0-6: probability
56 : bit 7: specific node force placement
57 : For each node in schematic:
58 : [u8] param2
59 : }
60 :
61 : Version changes:
62 : 1 - Initial version
63 : 2 - Fixed messy never/always place; 0 probability is now never, 0xFF is always
64 : 3 - Added y-slice probabilities; this allows for variable height structures
65 : 4 - Compressed range of node occurence prob., added per-node force placement bit
66 : */
67 :
68 : //// Schematic constants
69 : #define MTSCHEM_FILE_SIGNATURE 0x4d54534d // 'MTSM'
70 : #define MTSCHEM_FILE_VER_HIGHEST_READ 4
71 : #define MTSCHEM_FILE_VER_HIGHEST_WRITE 4
72 :
73 : #define MTSCHEM_PROB_MASK 0x7F
74 :
75 : #define MTSCHEM_PROB_NEVER 0x00
76 : #define MTSCHEM_PROB_ALWAYS 0x7F
77 : #define MTSCHEM_PROB_ALWAYS_OLD 0xFF
78 :
79 : #define MTSCHEM_FORCE_PLACE 0x80
80 :
81 : enum SchematicType
82 : {
83 : SCHEMATIC_NORMAL,
84 : };
85 :
86 : enum SchematicFormatType {
87 : SCHEM_FMT_HANDLE,
88 : SCHEM_FMT_MTS,
89 : SCHEM_FMT_LUA,
90 : };
91 :
92 : class Schematic : public ObjDef, public NodeResolver {
93 : public:
94 : Schematic();
95 : virtual ~Schematic();
96 :
97 : virtual void resolveNodeNames();
98 :
99 : bool loadSchematicFromFile(const std::string &filename, INodeDefManager *ndef,
100 : StringMap *replace_names=NULL);
101 : bool saveSchematicToFile(const std::string &filename, INodeDefManager *ndef);
102 : bool getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2);
103 :
104 : bool deserializeFromMts(std::istream *is, std::vector<std::string> *names);
105 : bool serializeToMts(std::ostream *os, const std::vector<std::string> &names);
106 : bool serializeToLua(std::ostream *os, const std::vector<std::string> &names,
107 : bool use_comments, u32 indent_spaces);
108 :
109 : void blitToVManip(v3s16 p, MMVManip *vm, Rotation rot, bool force_place);
110 : void placeStructure(Map *map, v3s16 p, u32 flags, Rotation rot, bool force_place);
111 :
112 : void applyProbabilities(v3s16 p0,
113 : std::vector<std::pair<v3s16, u8> > *plist,
114 : std::vector<std::pair<s16, u8> > *splist);
115 :
116 : std::vector<content_t> c_nodes;
117 : u32 flags;
118 : v3s16 size;
119 : MapNode *schemdata;
120 : u8 *slice_probs;
121 : };
122 :
123 : class SchematicManager : public ObjDefManager {
124 : public:
125 : SchematicManager(IGameDef *gamedef);
126 0 : virtual ~SchematicManager() {}
127 :
128 : virtual void clear();
129 :
130 0 : const char *getObjectTitle() const
131 : {
132 0 : return "schematic";
133 : }
134 :
135 0 : static Schematic *create(SchematicType type)
136 : {
137 0 : return new Schematic;
138 : }
139 :
140 : private:
141 : IGameDef *m_gamedef;
142 : };
143 :
144 : void generate_nodelist_and_update_ids(MapNode *nodes, size_t nodecount,
145 : std::vector<std::string> *usednodes, INodeDefManager *ndef);
146 :
147 : #endif
|