Skip to content

Commit c1b8290

Browse files
committedJun 22, 2013
Decoration: Add Schematic decoration type
1 parent b1a74df commit c1b8290

File tree

8 files changed

+589
-22
lines changed

8 files changed

+589
-22
lines changed
 

‎doc/lua_api.txt

+61-1
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,32 @@ The default value is simple, and is currently the only type supported.
421421
for example. Can also generate a decoration of random height between a specified lower and
422422
upper bound. This type of decoration is intended for placement of grass, flowers, cacti,
423423
papyrus, and so on.
424+
- schematic
425+
Copies a box of MapNodes from a specified schematic file (or raw description). Can specify a
426+
probability of a node randomly appearing when placed. This decoration type is intended to be used
427+
for multi-node sized discrete structures, such as trees, cave spikes, rocks, and so on.
428+
429+
Schematic specifier
430+
--------------------
431+
A schematic specifier identifies a schematic by either a filename to a Minetest Schematic file (.mts)
432+
or through raw data supplied through Lua, in the form of a table. This table must specify two fields:
433+
- The 'size' field is a 3d vector containing the dimensions of the provided schematic.
434+
- The 'data' field is a flat table of MapNodes making up the schematic, in the order of [z [y [x]]].
435+
In the bulk MapNode data, param1, instead of the typical light values, instead represents the
436+
probability of that node appearing in the structure. It is an integer with a value from 0-255; 0 means
437+
that node will always appear. If the probability value p is non-zero, then there is a
438+
(p / 256 * 100)% chance that node will appear when the schematic is placed on the map.
439+
Important note: Node aliases cannot be used for a raw schematic provided when registering as a decoration.
440+
441+
Schematic attributes
442+
-----------------
443+
Currently supported flags: place_center_x, place_center_y, place_center_z
444+
- place_center_x
445+
Placement of this decoration is centered along the X axis.
446+
- place_center_y
447+
Placement of this decoration is centered along the Y axis.
448+
- place_center_z
449+
Placement of this decoration is centered along the Z axis.
424450

425451
HUD element types
426452
-------------------
@@ -1018,6 +1044,7 @@ minetest.setting_get(name) -> string or nil
10181044
minetest.setting_getbool(name) -> boolean value or nil
10191045
minetest.setting_get_pos(name) -> position or nil
10201046
minetest.setting_save() -> nil, save all settings to config file
1047+
minetest.add_to_creative_inventory(itemstring)
10211048

10221049
Authentication:
10231050
minetest.notify_authentication_modified(name)
@@ -1248,6 +1275,21 @@ minetest.delete_particlespawner(id, player)
12481275
^ If playername is specified, only deletes on the player's client,
12491276
^ otherwise on all clients
12501277

1278+
Schematics:
1279+
minetest.create_schematic(p1, p2, probability_list, filename)
1280+
^ Create a schematic from the volume of map specified by the box formed by p1 and p2.
1281+
^ Apply the specified probability values to the specified nodes in probability_list.
1282+
^ probability_list is an array of tables containing two fields, pos and prob.
1283+
^ pos is the 3d vector specifying the absolute coordinates of the node being modified,
1284+
^ and prob is the integer value from 0 to 255 of the probability (see: Schematic specifier).
1285+
^ If there are two or more entries with the same pos value, the last occuring in the array is used.
1286+
^ If pos is not inside the box formed by p1 and p2, it is ignored.
1287+
^ If probability_list is nil, no probabilities are applied.
1288+
^ Saves schematic in the Minetest Schematic format to filename.
1289+
1290+
minetest.place_schematic(pos, schematic)
1291+
^ Place the schematic specified by schematic (see: Schematic specifier) at pos.
1292+
12511293
Random:
12521294
minetest.get_connected_players() -> list of ObjectRefs
12531295
minetest.hash_node_position({x=,y=,z=}) -> 48-bit integer
@@ -1292,6 +1334,15 @@ minetest.object_refs
12921334
minetest.luaentities
12931335
^ List of lua entities, indexed by active object id
12941336

1337+
Deprecated but defined for backwards compatibility:
1338+
minetest.digprop_constanttime(time)
1339+
minetest.digprop_stonelike(toughness)
1340+
minetest.digprop_dirtlike(toughness)
1341+
minetest.digprop_gravellike(toughness)
1342+
minetest.digprop_woodlike(toughness)
1343+
minetest.digprop_leaveslike(toughness)
1344+
minetest.digprop_glasslike(toughness)
1345+
12951346
Class reference
12961347
----------------
12971348
NodeMetaRef: Node metadata - reference extra data and functionality stored
@@ -1700,7 +1751,6 @@ Node definition (register_node)
17001751
liquid_alternative_source = "", -- Source version of flowing liquid
17011752
liquid_viscosity = 0, -- Higher viscosity = slower flow (max. 7)
17021753
liquid_renewable = true, -- Can new liquid source be created by placing
1703-
drowning = true, -- Player will drown in these
Has a conversation. Original line has a conversation.
17041754
two or more sources nearly?
17051755
light_source = 0, -- Amount of light emitted by node
17061756
damage_per_second = 0, -- If player is inside node, this damage is caused
@@ -1894,6 +1944,16 @@ Decoration definition (register_decoration)
18941944
num_spawn_by = 1,
18951945
^ Number of spawn_by nodes that must be surrounding the decoration position to occur.
18961946
^ If absent or -1, decorations occur next to any nodes.
1947+
1948+
----- Schematic-type parameters
1949+
schematic = "foobar.mts",
1950+
^ If schematic is a string, it is the filepath relative to the current working directory of the
1951+
^ specified Minetest schematic file.
1952+
^ - OR -, could instead be a table containing two fields, size and data:
1953+
schematic = {size = {x=4, y=6, z=4}, data = { {"cobble", 0, 0}, {"dirt_with_grass", 0, 0}, ...}},
1954+
^ See 'Schematic specifier' for details.
1955+
flags = "place_center_x, place_center_z",
1956+
^ Flags for schematic decorations. See 'Schematic attributes'.
18971957
}
18981958

18991959
Chatcommand definition (register_chatcommand)

‎src/mapgen.cpp

+313-6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3535
#include "treegen.h"
3636
#include "mapgen_v6.h"
3737
#include "mapgen_v7.h"
38+
#include "util/serialize.h"
3839

3940
FlagDesc flagdesc_mapgen[] = {
4041
{"trees", MG_TREES},
@@ -53,6 +54,12 @@ FlagDesc flagdesc_ore[] = {
5354
{NULL, 0}
5455
};
5556

57+
FlagDesc flagdesc_deco_schematic[] = {
58+
{"place_center_x", DECO_PLACE_CENTER_X},
59+
{"place_center_y", DECO_PLACE_CENTER_Y},
60+
{"place_center_z", DECO_PLACE_CENTER_Z},
61+
{NULL, 0}
62+
};
5663

5764
///////////////////////////////////////////////////////////////////////////////
5865

@@ -198,12 +205,15 @@ void OreSheet::generate(ManualMapVoxelManipulator *vm, int seed,
198205
}
199206

200207

208+
///////////////////////////////////////////////////////////////////////////////
209+
210+
201211
Decoration *createDecoration(DecorationType type) {
202212
switch (type) {
203213
case DECO_SIMPLE:
204214
return new DecoSimple;
205-
//case DECO_SCHEMATIC:
206-
// return new DecoSchematic;
215+
case DECO_SCHEMATIC:
216+
return new DecoSchematic;
207217
//case DECO_LSYSTEM:
208218
// return new DecoLSystem;
209219
default:
@@ -212,6 +222,14 @@ Decoration *createDecoration(DecorationType type) {
212222
}
213223

214224

225+
Decoration::Decoration() {
226+
mapseed = 0;
227+
np = NULL;
228+
fill_ratio = 0;
229+
sidelen = 1;
230+
}
231+
232+
215233
Decoration::~Decoration() {
216234
delete np;
217235
}
@@ -352,22 +370,25 @@ void Decoration::placeCutoffs(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
352370
#endif
353371

354372

373+
///////////////////////////////////////////////////////////////////////////////
374+
375+
355376
void DecoSimple::resolveNodeNames(INodeDefManager *ndef) {
356377
Decoration::resolveNodeNames(ndef);
357378

358379
if (c_deco == CONTENT_IGNORE) {
359380
c_deco = ndef->getId(deco_name);
360381
if (c_deco == CONTENT_IGNORE) {
361382
errorstream << "DecoSimple::resolveNodeNames: decoration node '"
362-
<< deco_name << "' not defined";
383+
<< deco_name << "' not defined" << std::endl;
363384
c_deco = CONTENT_AIR;
364385
}
365386
}
366387
if (c_spawnby == CONTENT_IGNORE) {
367388
c_spawnby = ndef->getId(spawnby_name);
368389
if (c_spawnby == CONTENT_IGNORE) {
369390
errorstream << "DecoSimple::resolveNodeNames: spawnby node '"
370-
<< deco_name << "' not defined";
391+
<< deco_name << "' not defined" << std::endl;
371392
nspawnby = -1;
372393
c_spawnby = CONTENT_AIR;
373394
}
@@ -380,15 +401,16 @@ void DecoSimple::resolveNodeNames(INodeDefManager *ndef) {
380401
content_t c = ndef->getId(decolist_names[i]);
381402
if (c == CONTENT_IGNORE) {
382403
errorstream << "DecoSimple::resolveNodeNames: decolist node '"
383-
<< decolist_names[i] << "' not defined";
404+
<< decolist_names[i] << "' not defined" << std::endl;
384405
c = CONTENT_AIR;
385406
}
386407
c_decolist.push_back(c);
387408
}
388409
}
389410

390411

391-
void DecoSimple::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, s16 start_y, v3s16 p) {
412+
void DecoSimple::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y,
413+
s16 start_y, v3s16 p) {
392414
ManualMapVoxelManipulator *vm = mg->vm;
393415

394416
u32 vi = vm->m_area.index(p);
@@ -454,6 +476,291 @@ std::string DecoSimple::getName() {
454476
///////////////////////////////////////////////////////////////////////////////
455477

456478

479+
DecoSchematic::DecoSchematic() {
480+
node_names = NULL;
481+
schematic = NULL;
482+
flags = 0;
483+
size = v3s16(0, 0, 0);
484+
}
485+
486+
487+
DecoSchematic::~DecoSchematic() {
488+
delete node_names;
489+
delete []schematic;
490+
}
491+
492+
493+
void DecoSchematic::resolveNodeNames(INodeDefManager *ndef) {
494+
Decoration::resolveNodeNames(ndef);
495+
496+
if (filename.empty())
497+
return;
498+
499+
if (!node_names) {
500+
errorstream << "DecoSchematic::resolveNodeNames: node name list was "
501+
"not created" << std::endl;
502+
return;
503+
}
504+
505+
for (size_t i = 0; i != node_names->size(); i++) {
506+
content_t c = ndef->getId(node_names->at(i));
507+
if (c == CONTENT_IGNORE) {
508+
errorstream << "DecoSchematic::resolveNodeNames: node '"
509+
<< node_names->at(i) << "' not defined" << std::endl;
510+
c = CONTENT_AIR;
511+
}
512+
c_nodes.push_back(c);
513+
}
514+
515+
for (int i = 0; i != size.X * size.Y * size.Z; i++)
516+
schematic[i].setContent(c_nodes[schematic[i].getContent()]);
517+
518+
delete node_names;
519+
node_names = NULL;
520+
}
521+
522+
523+
void DecoSchematic::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y,
524+
s16 start_y, v3s16 p) {
525+
ManualMapVoxelManipulator *vm = mg->vm;
526+
527+
if (flags & DECO_PLACE_CENTER_X)
528+
p.X -= (size.X + 1) / 2;
529+
if (flags & DECO_PLACE_CENTER_Y)
530+
p.Y -= (size.Y + 1) / 2;
531+
if (flags & DECO_PLACE_CENTER_Z)
532+
p.Z -= (size.Z + 1) / 2;
533+
534+
u32 vi = vm->m_area.index(p);
535+
if (vm->m_data[vi].getContent() != c_place_on &&
536+
c_place_on != CONTENT_IGNORE)
537+
return;
538+
539+
u32 i = 0;
540+
for (s16 z = 0; z != size.Z; z++)
541+
for (s16 y = 0; y != size.Y; y++) {
542+
vi = vm->m_area.index(p.X, p.Y + y, p.Z + z);
543+
for (s16 x = 0; x != size.X; x++, i++, vi++) {
544+
if (!vm->m_area.contains(vi))
545+
continue;
546+
547+
content_t c = vm->m_data[vi].getContent();
548+
if (c != CONTENT_AIR && c != CONTENT_IGNORE)
549+
continue;
550+
551+
if (schematic[i].param1 && myrand_range(1, 256) > schematic[i].param1)
552+
continue;
553+
554+
vm->m_data[vi] = schematic[i];
555+
vm->m_data[vi].param1 = 0;
556+
}
557+
}
558+
}
559+
560+
561+
int DecoSchematic::getHeight() {
562+
return size.Y;
563+
}
564+
565+
566+
std::string DecoSchematic::getName() {
567+
return filename;
568+
}
569+
570+
571+
void DecoSchematic::placeStructure(Map *map, v3s16 p) {
572+
assert(schematic != NULL);
573+
ManualMapVoxelManipulator *vm = new ManualMapVoxelManipulator(map);
574+
575+
if (flags & DECO_PLACE_CENTER_X)
576+
p.X -= (size.X + 1) / 2;
577+
if (flags & DECO_PLACE_CENTER_Y)
578+
p.Y -= (size.Y + 1) / 2;
579+
if (flags & DECO_PLACE_CENTER_Z)
580+
p.Z -= (size.Z + 1) / 2;
581+
582+
v3s16 bp1 = getNodeBlockPos(p);
583+
v3s16 bp2 = getNodeBlockPos(p + size - v3s16(1,1,1));
584+
vm->initialEmerge(bp1, bp2);
585+
586+
u32 i = 0;
587+
for (s16 z = 0; z != size.Z; z++)
588+
for (s16 y = 0; y != size.Y; y++) {
589+
u32 vi = vm->m_area.index(p.X, p.Y + y, p.Z + z);
590+
for (s16 x = 0; x != size.X; x++, i++, vi++) {
591+
if (!vm->m_area.contains(vi))
592+
continue;
593+
if (schematic[i].param1 && myrand_range(1, 256) > schematic[i].param1)
594+
continue;
595+
596+
vm->m_data[vi] = schematic[i];
597+
vm->m_data[vi].param1 = 0;
598+
}
599+
}
600+
601+
std::map<v3s16, MapBlock *> lighting_modified_blocks;
602+
std::map<v3s16, MapBlock *> modified_blocks;
603+
vm->blitBackAll(&modified_blocks);
604+
605+
// TODO: Optimize this by using Mapgen::calcLighting() instead
606+
lighting_modified_blocks.insert(modified_blocks.begin(), modified_blocks.end());
607+
map->updateLighting(lighting_modified_blocks, modified_blocks);
608+
609+
MapEditEvent event;
610+
event.type = MEET_OTHER;
611+
for (std::map<v3s16, MapBlock *>::iterator
612+
it = modified_blocks.begin();
613+
it != modified_blocks.end(); ++it)
614+
event.modified_blocks.insert(it->first);
615+
616+
map->dispatchEvent(&event);
617+
}
618+
619+
620+
bool DecoSchematic::loadSchematicFile() {
621+
std::ifstream is(filename.c_str(), std::ios_base::binary);
622+
623+
u32 signature = readU32(is);
624+
if (signature != 'MTSM') {
625+
errorstream << "loadSchematicFile: invalid schematic "
626+
"file" << std::endl;
627+
return false;
628+
}
629+
630+
u16 version = readU16(is);
631+
if (version != 1) {
632+
errorstream << "loadSchematicFile: unsupported schematic "
633+
"file version" << std::endl;
634+
return false;
635+
}
636+
637+
size = readV3S16(is);
638+
int nodecount = size.X * size.Y * size.Z;
639+
640+
u16 nidmapcount = readU16(is);
641+
642+
node_names = new std::vector<std::string>;
643+
for (int i = 0; i != nidmapcount; i++) {
644+
std::string name = deSerializeString(is);
645+
node_names->push_back(name);
646+
}
647+
648+
delete schematic;
649+
schematic = new MapNode[nodecount];
650+
MapNode::deSerializeBulk(is, SER_FMT_VER_HIGHEST, schematic,
651+
nodecount, 2, 2, true);
652+
653+
return true;
654+
}
655+
656+
657+
/*
658+
Minetest Schematic File Format
659+
660+
All values are stored in big-endian byte order.
661+
[u32] signature: 'MTSM'
662+
[u16] version: 1
663+
[u16] size X
664+
[u16] size Y
665+
[u16] size Z
666+
[Name-ID table] Name ID Mapping Table
667+
[u16] name-id count
668+
For each name-id mapping:
669+
[u16] name length
670+
[u8[]] name
671+
ZLib deflated {
672+
For each node in schematic: (for z, y, x)
673+
[u16] content
674+
For each node in schematic:
675+
[u8] probability of occurance (param1)
676+
For each node in schematic:
677+
[u8] param2
678+
}
679+
*/
680+
void DecoSchematic::saveSchematicFile(INodeDefManager *ndef) {
681+
std::ofstream os(filename.c_str(), std::ios_base::binary);
682+
683+
writeU32(os, 'MTSM'); // signature
684+
writeU16(os, 1); // version
685+
writeV3S16(os, size); // schematic size
686+
687+
std::vector<content_t> usednodes;
688+
int nodecount = size.X * size.Y * size.Z;
689+
build_nnlist_and_update_ids(schematic, nodecount, &usednodes);
690+
691+
u16 numids = usednodes.size();
692+
writeU16(os, numids); // name count
693+
for (int i = 0; i != numids; i++)
694+
os << serializeString(ndef->get(usednodes[i]).name); // node names
695+
696+
// compressed bulk node data
697+
MapNode::serializeBulk(os, SER_FMT_VER_HIGHEST, schematic,
698+
nodecount, 2, 2, true);
699+
}
700+
701+
702+
void build_nnlist_and_update_ids(MapNode *nodes, u32 nodecount,
703+
std::vector<content_t> *usednodes) {
704+
std::map<content_t, content_t> nodeidmap;
705+
content_t numids = 0;
706+
707+
for (u32 i = 0; i != nodecount; i++) {
708+
content_t id;
709+
content_t c = nodes[i].getContent();
710+
711+
std::map<content_t, content_t>::const_iterator it = nodeidmap.find(c);
712+
if (it == nodeidmap.end()) {
713+
id = numids;
714+
numids++;
715+
716+
usednodes->push_back(c);
717+
nodeidmap.insert(std::make_pair(c, id));
718+
} else {
719+
id = it->second;
720+
}
721+
nodes[i].setContent(id);
722+
}
723+
}
724+
725+
726+
bool DecoSchematic::getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2) {
727+
ManualMapVoxelManipulator *vm = new ManualMapVoxelManipulator(map);
728+
729+
v3s16 bp1 = getNodeBlockPos(p1);
730+
v3s16 bp2 = getNodeBlockPos(p2);
731+
vm->initialEmerge(bp1, bp2);
732+
733+
size = p2 - p1 + 1;
734+
schematic = new MapNode[size.X * size.Y * size.Z];
735+
736+
u32 i = 0;
737+
for (s16 z = p1.Z; z <= p2.Z; z++)
738+
for (s16 y = p1.Y; y <= p2.Y; y++) {
739+
u32 vi = vm->m_area.index(p1.X, y, z);
740+
for (s16 x = p1.X; x <= p2.X; x++, i++, vi++) {
741+
schematic[i] = vm->m_data[vi];
742+
schematic[i].param1 = 0;
743+
}
744+
}
745+
746+
delete vm;
747+
return true;
748+
}
749+
750+
751+
void DecoSchematic::applyProbabilities(std::vector<std::pair<v3s16, u8> > *plist, v3s16 p0) {
752+
for (size_t i = 0; i != plist->size(); i++) {
753+
v3s16 p = (*plist)[i].first - p0;
754+
int index = p.Z * (size.Y * size.X) + p.Y * size.X + p.X;
755+
if (index < size.Z * size.Y * size.X)
756+
schematic[index].param1 = (*plist)[i].second;
757+
}
758+
}
759+
760+
761+
///////////////////////////////////////////////////////////////////////////////
762+
763+
457764
Mapgen::Mapgen() {
458765
seed = 0;
459766
water_level = 0;

‎src/mapgen.h

+35-3
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
4545
// nodes isn't the specified node
4646
#define OREFLAG_NODEISNT 0x04 // not yet implemented
4747

48+
/////////////////// Decoration flags
49+
#define DECO_PLACE_CENTER_X 1
50+
#define DECO_PLACE_CENTER_Y 2
51+
#define DECO_PLACE_CENTER_Z 4
52+
4853
extern FlagDesc flagdesc_mapgen[];
4954
extern FlagDesc flagdesc_ore[];
55+
extern FlagDesc flagdesc_deco_schematic[];
5056

5157
class BiomeDefManager;
5258
class Biome;
@@ -57,6 +63,7 @@ class VoxelManipulator;
5763
class INodeDefManager;
5864
struct BlockMakeData;
5965
class VoxelArea;
66+
class Map;
6067

6168
struct MapgenParams {
6269
std::string mg_name;
@@ -207,6 +214,7 @@ class Decoration {
207214
//std::list<CutoffData> cutoffs;
208215
//JMutex cutoff_mutex;
209216

217+
Decoration();
210218
virtual ~Decoration();
211219

212220
virtual void resolveNodeNames(INodeDefManager *ndef);
@@ -241,12 +249,36 @@ class DecoSimple : public Decoration {
241249
virtual std::string getName();
242250
};
243251

244-
/*
245252
class DecoSchematic : public Decoration {
246253
public:
247-
virtual void generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax);
254+
std::string filename;
255+
256+
std::vector<std::string> *node_names;
257+
std::vector<content_t> c_nodes;
258+
259+
u32 flags;
260+
v3s16 size;
261+
MapNode *schematic;
262+
263+
DecoSchematic();
264+
~DecoSchematic();
265+
266+
void resolveNodeNames(INodeDefManager *ndef);
267+
virtual void generate(Mapgen *mg, PseudoRandom *pr, s16 max_y,
268+
s16 start_y, v3s16 p);
269+
virtual int getHeight();
270+
virtual std::string getName();
271+
272+
bool loadSchematicFile();
273+
void saveSchematicFile(INodeDefManager *ndef);
274+
275+
bool getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2);
276+
void placeStructure(Map *map, v3s16 p);
277+
void applyProbabilities(std::vector<std::pair<v3s16, u8> > *plist, v3s16 p0);
248278
};
249-
*/
279+
280+
void build_nnlist_and_update_ids(MapNode *nodes, u32 nodecount,
281+
std::vector<content_t> *usednodes);
250282

251283
/*
252284
class DecoLSystem : public Decoration {

‎src/script/common/c_content.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3030
#include "log.h"
3131
#include "tool.h"
3232
#include "server.h"
33+
#include "mapgen.h"
3334

3435
struct EnumString es_TileAnimationType[] =
3536
{
@@ -922,3 +923,51 @@ NoiseParams *read_noiseparams(lua_State *L, int index)
922923

923924
return np;
924925
}
926+
927+
/******************************************************************************/
928+
bool read_schematic(lua_State *L, int index, DecoSchematic *dschem, Server *server) {
929+
if (index < 0)
930+
index = lua_gettop(L) + 1 + index;
931+
932+
INodeDefManager *ndef = server->getNodeDefManager();
933+
934+
if (lua_istable(L, index)) {
935+
lua_getfield(L, index, "size");
936+
v3s16 size = read_v3s16(L, -1);
937+
lua_pop(L, 1);
938+
939+
int numnodes = size.X * size.Y * size.Z;
940+
MapNode *schemdata = new MapNode[numnodes];
941+
int i = 0;
942+
943+
lua_getfield(L, index, "data");
944+
luaL_checktype(L, -1, LUA_TTABLE);
945+
946+
lua_pushnil(L);
947+
while (lua_next(L, -2)) {
948+
if (i < numnodes)
949+
schemdata[i] = readnode(L, -1, ndef);
950+
951+
i++;
952+
lua_pop(L, 1);
953+
}
954+
955+
dschem->size = size;
956+
dschem->schematic = schemdata;
957+
958+
if (i != numnodes) {
959+
errorstream << "read_schematic: incorrect number of "
960+
"nodes provided in raw schematic data (got " << i <<
961+
", expected " << numnodes << ")." << std::endl;
962+
return false;
963+
}
964+
} else if (lua_isstring(L, index)) {
965+
dschem->filename = std::string(lua_tostring(L, index));
966+
} else {
967+
errorstream << "read_schematic: missing schematic "
968+
"filename or raw schematic data" << std::endl;
969+
return false;
970+
}
971+
972+
return true;
973+
}

‎src/script/common/c_content.h

+5
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ struct DigParams;
5757
struct HitParams;
5858
struct EnumString;
5959
struct NoiseParams;
60+
class DecoSchematic;
6061

6162

6263
ContentFeatures read_content_features (lua_State *L, int index);
@@ -139,6 +140,10 @@ bool string_to_enum (const EnumString *spec,
139140

140141
NoiseParams* read_noiseparams (lua_State *L, int index);
141142

143+
bool read_schematic (lua_State *L, int index,
144+
DecoSchematic *dschem,
145+
Server *server);
146+
142147
void luaentity_get (lua_State *L,u16 id);
143148

144149
extern struct EnumString es_TileAnimationType[];

‎src/script/lua_api/luaapi.cpp

+104-12
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ bool ModApiBasic::Initialize(lua_State* L,int top) {
101101

102102
retval &= API_FCT(register_ore);
103103
retval &= API_FCT(register_decoration);
104+
retval &= API_FCT(create_schematic);
105+
retval &= API_FCT(place_schematic);
104106

105107
return retval;
106108
}
@@ -680,7 +682,7 @@ int ModApiBasic::l_register_decoration(lua_State *L)
680682
{
681683
int index = 1;
682684
luaL_checktype(L, index, LUA_TTABLE);
683-
685+
684686
EmergeManager *emerge = getServer(L)->getEmergeManager();
685687
BiomeDefManager *bdef = emerge->biomedef;
686688

@@ -701,8 +703,14 @@ int ModApiBasic::l_register_decoration(lua_State *L)
701703

702704
deco->c_place_on = CONTENT_IGNORE;
703705
deco->place_on_name = getstringfield_default(L, index, "place_on", "ignore");
704-
deco->sidelen = getintfield_default(L, index, "sidelen", 8);
705706
deco->fill_ratio = getfloatfield_default(L, index, "fill_ratio", 0.02);
707+
deco->sidelen = getintfield_default(L, index, "sidelen", 8);
708+
if (deco->sidelen <= 0) {
709+
errorstream << "register_decoration: sidelen must be "
710+
"greater than 0" << std::endl;
711+
delete deco;
712+
return 0;
713+
}
706714

707715
lua_getfield(L, index, "noise_params");
708716
deco->np = read_noiseparams(L, -1);
@@ -743,7 +751,7 @@ int ModApiBasic::l_register_decoration(lua_State *L)
743751
lua_pop(L, 1);
744752
}
745753
} else if (lua_isstring(L, -1)) {
746-
dsimple->deco_name = std::string(lua_tostring(L, -1));
754+
dsimple->deco_name = std::string(lua_tostring(L, -1));
747755
} else {
748756
dsimple->deco_name = std::string("air");
749757
}
@@ -758,28 +766,112 @@ int ModApiBasic::l_register_decoration(lua_State *L)
758766

759767
break; }
760768
case DECO_SCHEMATIC: {
761-
//DecoSchematic *decoschematic = (DecoSchematic *)deco;
769+
DecoSchematic *dschem = (DecoSchematic *)deco;
770+
dschem->flags = getflagsfield(L, index, "flags", flagdesc_deco_schematic);
762771

772+
lua_getfield(L, index, "schematic");
773+
if (!read_schematic(L, -1, dschem, getServer(L))) {
774+
delete dschem;
775+
return 0;
776+
}
777+
lua_pop(L, -1);
778+
779+
if (!dschem->filename.empty() && !dschem->loadSchematicFile()) {
780+
errorstream << "register_decoration: failed to load schematic file '"
781+
<< dschem->filename << "'" << std::endl;
782+
delete dschem;
783+
return 0;
784+
}
763785
break; }
764786
case DECO_LSYSTEM: {
765787
//DecoLSystem *decolsystem = (DecoLSystem *)deco;
766788

767789
break; }
768790
}
769-
770-
if (deco->sidelen <= 0) {
771-
errorstream << "register_decoration: sidelen must be "
772-
"greater than 0" << std::endl;
773-
delete deco;
774-
return 0;
775-
}
776-
791+
777792
emerge->decorations.push_back(deco);
778793

779794
verbosestream << "register_decoration: decoration '" << deco->getName()
780795
<< "' registered" << std::endl;
781796
return 0;
782797
}
783798

799+
// create_schematic(p1, p2, probability_list, filename)
800+
int ModApiBasic::l_create_schematic(lua_State *L)
801+
{
802+
DecoSchematic dschem;
803+
804+
Map *map = &(getEnv(L)->getMap());
805+
INodeDefManager *ndef = getServer(L)->getNodeDefManager();
806+
807+
v3s16 p1 = read_v3s16(L, 1);
808+
v3s16 p2 = read_v3s16(L, 2);
809+
sortBoxVerticies(p1, p2);
810+
811+
std::vector<std::pair<v3s16, u8> > probability_list;
812+
if (lua_istable(L, 3)) {
813+
lua_pushnil(L);
814+
while (lua_next(L, 3)) {
815+
if (lua_istable(L, -1)) {
816+
lua_getfield(L, -1, "pos");
817+
v3s16 pos = read_v3s16(L, -1);
818+
lua_pop(L, 1);
819+
820+
int prob = getintfield_default(L, -1, "prob", 0);
821+
if (prob < 0 || prob >= UCHAR_MAX) {
822+
errorstream << "create_schematic: probability value of "
823+
<< prob << " at " << PP(pos) << " out of range" << std::endl;
824+
} else {
825+
probability_list.push_back(std::make_pair(pos, (u8)prob));
826+
}
827+
}
828+
829+
lua_pop(L, 1);
830+
}
831+
}
832+
833+
dschem.filename = std::string(lua_tostring(L, 4));
834+
835+
if (!dschem.getSchematicFromMap(map, p1, p2)) {
836+
errorstream << "create_schematic: failed to get schematic "
837+
"from map" << std::endl;
838+
return 0;
839+
}
840+
841+
dschem.applyProbabilities(&probability_list, p1);
842+
843+
dschem.saveSchematicFile(ndef);
844+
actionstream << "create_schematic: saved schematic file '" << dschem.filename << "'." << std::endl;
845+
846+
return 1;
847+
}
848+
849+
850+
// place_schematic(p, schematic)
851+
int ModApiBasic::l_place_schematic(lua_State *L)
852+
{
853+
DecoSchematic dschem;
854+
855+
Map *map = &(getEnv(L)->getMap());
856+
INodeDefManager *ndef = getServer(L)->getNodeDefManager();
857+
858+
v3s16 p = read_v3s16(L, 1);
859+
if (!read_schematic(L, 2, &dschem, getServer(L)))
860+
return 0;
861+
862+
if (!dschem.filename.empty()) {
863+
if (!dschem.loadSchematicFile()) {
864+
errorstream << "place_schematic: failed to load schematic file '"
865+
<< dschem.filename << "'" << std::endl;
866+
return 0;
867+
}
868+
dschem.resolveNodeNames(ndef);
869+
}
870+
871+
dschem.placeStructure(map, p);
872+
873+
return 1;
874+
}
875+
784876

785877
ModApiBasic modapibasic_prototype;

‎src/script/lua_api/luaapi.h

+6
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ class ModApiBasic : public ModApiBase {
132132

133133
// register_decoration(deco)
134134
static int l_register_decoration(lua_State *L);
135+
136+
// create_schematic(p1, p2, filename)
137+
static int l_create_schematic(lua_State *L);
138+
139+
// place_schematic(p, filename)
140+
static int l_place_schematic(lua_State *L);
135141

136142
static struct EnumString es_OreType[];
137143
static struct EnumString es_DecorationType[];

‎src/util/numeric.h

+16
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,22 @@ inline v3s16 arealim(v3s16 p, s16 d)
140140
return p;
141141
}
142142

143+
// The naive swap performs better than the xor version
144+
#define SWAP(t, x, y) do { \
145+
t temp = x; \
146+
x = y; \
147+
y = temp; \
148+
} while (0)
149+
150+
inline void sortBoxVerticies(v3s16 &p1, v3s16 &p2) {
151+
if (p1.X > p2.X)
152+
SWAP(s16, p1.X, p2.X);
153+
if (p1.Y > p2.Y)
154+
SWAP(s16, p1.Y, p2.Y);
155+
if (p1.Z > p2.Z)
156+
SWAP(s16, p1.Z, p2.Z);
157+
}
158+
143159

144160
/*
145161
See test.cpp for example cases.

0 commit comments

Comments
 (0)
Please sign in to comment.