mg_schematic.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. Minetest
  3. Copyright (C) 2014-2018 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
  4. Copyright (C) 2015-2018 paramat
  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. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #pragma once
  18. #include <map>
  19. #include "mg_decoration.h"
  20. #include "util/string.h"
  21. class Map;
  22. class ServerMap;
  23. class Mapgen;
  24. class MMVManip;
  25. class PseudoRandom;
  26. class NodeResolver;
  27. class Server;
  28. /*
  29. Minetest Schematic File Format
  30. All values are stored in big-endian byte order.
  31. [u32] signature: 'MTSM'
  32. [u16] version: 4
  33. [u16] size X
  34. [u16] size Y
  35. [u16] size Z
  36. For each Y:
  37. [u8] slice probability value
  38. [Name-ID table] Name ID Mapping Table
  39. [u16] name-id count
  40. For each name-id mapping:
  41. [u16] name length
  42. [u8[]] name
  43. ZLib deflated {
  44. For each node in schematic: (for z, y, x)
  45. [u16] content
  46. For each node in schematic:
  47. [u8] param1
  48. bit 0-6: probability
  49. bit 7: specific node force placement
  50. For each node in schematic:
  51. [u8] param2
  52. }
  53. Version changes:
  54. 1 - Initial version
  55. 2 - Fixed messy never/always place; 0 probability is now never, 0xFF is always
  56. 3 - Added y-slice probabilities; this allows for variable height structures
  57. 4 - Compressed range of node occurence prob., added per-node force placement bit
  58. */
  59. //// Schematic constants
  60. #define MTSCHEM_FILE_SIGNATURE 0x4d54534d // 'MTSM'
  61. #define MTSCHEM_FILE_VER_HIGHEST_READ 4
  62. #define MTSCHEM_FILE_VER_HIGHEST_WRITE 4
  63. #define MTSCHEM_MAPNODE_SER_FMT_VER 28 // Fixed serialization version for schematics since these still need to use Zlib
  64. #define MTSCHEM_PROB_MASK 0x7F
  65. #define MTSCHEM_PROB_NEVER 0x00
  66. #define MTSCHEM_PROB_ALWAYS 0x7F
  67. #define MTSCHEM_PROB_ALWAYS_OLD 0xFF
  68. #define MTSCHEM_FORCE_PLACE 0x80
  69. enum SchematicType
  70. {
  71. SCHEMATIC_NORMAL,
  72. };
  73. enum SchematicFormatType {
  74. SCHEM_FMT_HANDLE,
  75. SCHEM_FMT_MTS,
  76. SCHEM_FMT_LUA,
  77. };
  78. class Schematic : public ObjDef, public NodeResolver {
  79. public:
  80. Schematic() = default;
  81. virtual ~Schematic();
  82. ObjDef *clone() const;
  83. virtual void resolveNodeNames();
  84. bool loadSchematicFromFile(const std::string &filename,
  85. const NodeDefManager *ndef, StringMap *replace_names = NULL);
  86. bool saveSchematicToFile(const std::string &filename,
  87. const NodeDefManager *ndef);
  88. bool getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2);
  89. bool deserializeFromMts(std::istream *is);
  90. bool serializeToMts(std::ostream *os) const;
  91. bool serializeToLua(std::ostream *os, bool use_comments, u32 indent_spaces) const;
  92. void blitToVManip(MMVManip *vm, v3s16 p, Rotation rot, bool force_place);
  93. bool placeOnVManip(MMVManip *vm, v3s16 p, u32 flags, Rotation rot, bool force_place);
  94. void placeOnMap(ServerMap *map, v3s16 p, u32 flags, Rotation rot, bool force_place);
  95. void applyProbabilities(v3s16 p0,
  96. std::vector<std::pair<v3s16, u8> > *plist,
  97. std::vector<std::pair<s16, u8> > *splist);
  98. std::vector<content_t> c_nodes;
  99. u32 flags = 0;
  100. v3s16 size;
  101. MapNode *schemdata = nullptr;
  102. u8 *slice_probs = nullptr;
  103. private:
  104. // Counterpart to the node resolver: Condense content_t to a sequential "m_nodenames" list
  105. void condenseContentIds();
  106. };
  107. class SchematicManager : public ObjDefManager {
  108. public:
  109. SchematicManager(Server *server);
  110. virtual ~SchematicManager() = default;
  111. SchematicManager *clone() const;
  112. virtual void clear();
  113. const char *getObjectTitle() const
  114. {
  115. return "schematic";
  116. }
  117. static Schematic *create(SchematicType type)
  118. {
  119. return new Schematic;
  120. }
  121. private:
  122. SchematicManager() {};
  123. Server *m_server;
  124. };