Minetest  5.4.0
mg_schematic.h
Go to the documentation of this file.
1 /*
2 Minetest
3 Copyright (C) 2014-2018 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2015-2018 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 #pragma once
22 
23 #include <map>
24 #include "mg_decoration.h"
25 #include "util/string.h"
26 
27 class Map;
28 class ServerMap;
29 class Mapgen;
30 class MMVManip;
31 class PseudoRandom;
32 class NodeResolver;
33 class Server;
34 
35 /*
36  Minetest Schematic File Format
37 
38  All values are stored in big-endian byte order.
39  [u32] signature: 'MTSM'
40  [u16] version: 4
41  [u16] size X
42  [u16] size Y
43  [u16] size Z
44  For each Y:
45  [u8] slice probability value
46  [Name-ID table] Name ID Mapping Table
47  [u16] name-id count
48  For each name-id mapping:
49  [u16] name length
50  [u8[]] name
51  ZLib deflated {
52  For each node in schematic: (for z, y, x)
53  [u16] content
54  For each node in schematic:
55  [u8] param1
56  bit 0-6: probability
57  bit 7: specific node force placement
58  For each node in schematic:
59  [u8] param2
60  }
61 
62  Version changes:
63  1 - Initial version
64  2 - Fixed messy never/always place; 0 probability is now never, 0xFF is always
65  3 - Added y-slice probabilities; this allows for variable height structures
66  4 - Compressed range of node occurence prob., added per-node force placement bit
67 */
68 
70 #define MTSCHEM_FILE_SIGNATURE 0x4d54534d // 'MTSM'
71 #define MTSCHEM_FILE_VER_HIGHEST_READ 4
72 #define MTSCHEM_FILE_VER_HIGHEST_WRITE 4
73 
74 #define MTSCHEM_PROB_MASK 0x7F
75 
76 #define MTSCHEM_PROB_NEVER 0x00
77 #define MTSCHEM_PROB_ALWAYS 0x7F
78 #define MTSCHEM_PROB_ALWAYS_OLD 0xFF
79 
80 #define MTSCHEM_FORCE_PLACE 0x80
81 
83 {
85 };
86 
91 };
92 
93 class Schematic : public ObjDef, public NodeResolver {
94 public:
96  virtual ~Schematic();
97 
98  ObjDef *clone() const;
99 
100  virtual void resolveNodeNames();
101 
102  bool loadSchematicFromFile(const std::string &filename,
103  const NodeDefManager *ndef, StringMap *replace_names = NULL);
104  bool saveSchematicToFile(const std::string &filename,
105  const NodeDefManager *ndef);
106  bool getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2);
107 
108  bool deserializeFromMts(std::istream *is, std::vector<std::string> *names);
109  bool serializeToMts(std::ostream *os,
110  const std::vector<std::string> &names) const;
111  bool serializeToLua(std::ostream *os, const std::vector<std::string> &names,
112  bool use_comments, u32 indent_spaces) const;
113 
114  void blitToVManip(MMVManip *vm, v3s16 p, Rotation rot, bool force_place);
115  bool placeOnVManip(MMVManip *vm, v3s16 p, u32 flags, Rotation rot, bool force_place);
116  void placeOnMap(ServerMap *map, v3s16 p, u32 flags, Rotation rot, bool force_place);
117 
118  void applyProbabilities(v3s16 p0,
119  std::vector<std::pair<v3s16, u8> > *plist,
120  std::vector<std::pair<s16, u8> > *splist);
121 
122  std::vector<content_t> c_nodes;
123  u32 flags = 0;
125  MapNode *schemdata = nullptr;
126  u8 *slice_probs = nullptr;
127 };
128 
130 public:
132  virtual ~SchematicManager() = default;
133 
134  SchematicManager *clone() const;
135 
136  virtual void clear();
137 
138  const char *getObjectTitle() const
139  {
140  return "schematic";
141  }
142 
144  {
145  return new Schematic;
146  }
147 
148 private:
150 
152 };
153 
154 void generate_nodelist_and_update_ids(MapNode *nodes, size_t nodecount,
155  std::vector<std::string> *usednodes, const NodeDefManager *ndef);
Definition: map.h:439
Definition: map.h:123
Definition: mapgen.h:156
This class is for getting the actual properties of nodes from their content ID.
Definition: nodedef.h:510
Definition: nodedef.h:792
Definition: objdef.h:70
Definition: objdef.h:44
Definition: noise.h:43
Definition: mg_schematic.h:129
SchematicManager()
Definition: mg_schematic.h:149
static Schematic * create(SchematicType type)
Definition: mg_schematic.h:143
const char * getObjectTitle() const
Definition: mg_schematic.h:138
virtual void clear()
Definition: mg_schematic.cpp:55
Server * m_server
Definition: mg_schematic.h:149
SchematicManager * clone() const
Definition: mg_schematic.cpp:46
virtual ~SchematicManager()=default
Definition: mg_schematic.h:93
bool saveSchematicToFile(const std::string &filename, const NodeDefManager *ndef)
Definition: mg_schematic.cpp:494
bool deserializeFromMts(std::istream *is, std::vector< std::string > *names)
Definition: mg_schematic.cpp:282
bool getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2)
Definition: mg_schematic.cpp:532
std::vector< content_t > c_nodes
Definition: mg_schematic.h:122
bool loadSchematicFromFile(const std::string &filename, const NodeDefManager *ndef, StringMap *replace_names=NULL)
Definition: mg_schematic.cpp:460
ObjDef * clone() const
Definition: mg_schematic.cpp:89
void applyProbabilities(v3s16 p0, std::vector< std::pair< v3s16, u8 > > *plist, std::vector< std::pair< s16, u8 > > *splist)
Definition: mg_schematic.cpp:563
bool placeOnVManip(MMVManip *vm, v3s16 p, u32 flags, Rotation rot, bool force_place)
Definition: mg_schematic.cpp:205
bool serializeToMts(std::ostream *os, const std::vector< std::string > &names) const
Definition: mg_schematic.cpp:361
u8 * slice_probs
Definition: mg_schematic.h:126
MapNode * schemdata
Definition: mg_schematic.h:125
void placeOnMap(ServerMap *map, v3s16 p, u32 flags, Rotation rot, bool force_place)
Definition: mg_schematic.cpp:232
bool serializeToLua(std::ostream *os, const std::vector< std::string > &names, bool use_comments, u32 indent_spaces) const
Definition: mg_schematic.cpp:385
virtual void resolveNodeNames()
Definition: mg_schematic.cpp:109
virtual ~Schematic()
Definition: mg_schematic.cpp:83
u32 flags
Definition: mg_schematic.h:123
void blitToVManip(MMVManip *vm, v3s16 p, Rotation rot, bool force_place)
Definition: mg_schematic.cpp:122
v3s16 size
Definition: mg_schematic.h:124
Definition: map.h:315
Definition: server.h:142
core::vector3d< s16 > v3s16
Definition: irr_v3d.h:28
Rotation
Definition: mapnode.h:82
SchematicType
Definition: mg_schematic.h:83
@ SCHEMATIC_NORMAL
Definition: mg_schematic.h:84
void generate_nodelist_and_update_ids(MapNode *nodes, size_t nodecount, std::vector< std::string > *usednodes, const NodeDefManager *ndef)
Definition: mg_schematic.cpp:587
SchematicFormatType
Definition: mg_schematic.h:87
@ SCHEM_FMT_LUA
Definition: mg_schematic.h:90
@ SCHEM_FMT_HANDLE
Definition: mg_schematic.h:88
@ SCHEM_FMT_MTS
Definition: mg_schematic.h:89
Definition: activeobjectmgr.cpp:26
std::unordered_map< std::string, std::string > StringMap
Definition: string.h:60
Definition: mapnode.h:118
std::string p(std::string path)
Definition: test_filepath.cpp:59