mg_schematic.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. Minetest
  3. Copyright (C) 2014-2016 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
  4. Copyright (C) 2015-2017 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_PROB_MASK 0x7F
  64. #define MTSCHEM_PROB_NEVER 0x00
  65. #define MTSCHEM_PROB_ALWAYS 0x7F
  66. #define MTSCHEM_PROB_ALWAYS_OLD 0xFF
  67. #define MTSCHEM_FORCE_PLACE 0x80
  68. enum SchematicType
  69. {
  70. SCHEMATIC_NORMAL,
  71. };
  72. enum SchematicFormatType {
  73. SCHEM_FMT_HANDLE,
  74. SCHEM_FMT_MTS,
  75. SCHEM_FMT_LUA,
  76. };
  77. class Schematic : public ObjDef, public NodeResolver {
  78. public:
  79. Schematic();
  80. virtual ~Schematic();
  81. virtual void resolveNodeNames();
  82. bool loadSchematicFromFile(const std::string &filename, INodeDefManager *ndef,
  83. StringMap *replace_names=NULL);
  84. bool saveSchematicToFile(const std::string &filename, INodeDefManager *ndef);
  85. bool getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2);
  86. bool deserializeFromMts(std::istream *is, std::vector<std::string> *names);
  87. bool serializeToMts(std::ostream *os, const std::vector<std::string> &names);
  88. bool serializeToLua(std::ostream *os, const std::vector<std::string> &names,
  89. bool use_comments, u32 indent_spaces);
  90. void blitToVManip(MMVManip *vm, v3s16 p, Rotation rot, bool force_place);
  91. bool placeOnVManip(MMVManip *vm, v3s16 p, u32 flags, Rotation rot, bool force_place);
  92. void placeOnMap(ServerMap *map, v3s16 p, u32 flags, Rotation rot, bool force_place);
  93. void applyProbabilities(v3s16 p0,
  94. std::vector<std::pair<v3s16, u8> > *plist,
  95. std::vector<std::pair<s16, u8> > *splist);
  96. std::vector<content_t> c_nodes;
  97. u32 flags = 0;
  98. v3s16 size;
  99. MapNode *schemdata = nullptr;
  100. u8 *slice_probs = nullptr;
  101. };
  102. class SchematicManager : public ObjDefManager {
  103. public:
  104. SchematicManager(Server *server);
  105. virtual ~SchematicManager() = default;
  106. virtual void clear();
  107. const char *getObjectTitle() const
  108. {
  109. return "schematic";
  110. }
  111. static Schematic *create(SchematicType type)
  112. {
  113. return new Schematic;
  114. }
  115. private:
  116. Server *m_server;
  117. };
  118. void generate_nodelist_and_update_ids(MapNode *nodes, size_t nodecount,
  119. std::vector<std::string> *usednodes, INodeDefManager *ndef);