Decoration: Add schematic rotation support

This commit is contained in:
kwolekr 2013-07-01 18:04:17 -04:00
parent 131eb56f52
commit dd6d1afd8e
5 changed files with 113 additions and 45 deletions

@ -1309,8 +1309,10 @@ minetest.create_schematic(p1, p2, probability_list, filename)
^ If probability_list is nil, no probabilities are applied. ^ If probability_list is nil, no probabilities are applied.
^ Saves schematic in the Minetest Schematic format to filename. ^ Saves schematic in the Minetest Schematic format to filename.
minetest.place_schematic(pos, schematic) minetest.place_schematic(pos, schematic, rotation)
^ Place the schematic specified by schematic (see: Schematic specifier) at pos. ^ Place the schematic specified by schematic (see: Schematic specifier) at pos.
^ Rotation can be "0", "90", "180", "270", or "random".
^ If the rotation parameter is omitted, the schematic is not rotated.
Random: Random:
minetest.get_connected_players() -> list of ObjectRefs minetest.get_connected_players() -> list of ObjectRefs
@ -2048,6 +2050,8 @@ Decoration definition (register_decoration)
^ See 'Schematic specifier' for details. ^ See 'Schematic specifier' for details.
flags = "place_center_x, place_center_z", flags = "place_center_x, place_center_z",
^ Flags for schematic decorations. See 'Schematic attributes'. ^ Flags for schematic decorations. See 'Schematic attributes'.
rotation = "90" --rotate schematic 90 degrees on placement
^ Rotation can be "0", "90", "180", "270", or "random".
} }
Chatcommand definition (register_chatcommand) Chatcommand definition (register_chatcommand)

@ -535,28 +535,10 @@ void DecoSchematic::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p) {
c_place_on != CONTENT_IGNORE) c_place_on != CONTENT_IGNORE)
return; return;
u32 i = 0; Rotation rot = (rotation == ROTATE_RAND) ?
for (s16 z = 0; z != size.Z; z++) (Rotation)pr->range(ROTATE_0, ROTATE_270) : rotation;
for (s16 y = 0; y != size.Y; y++) {
vi = vm->m_area.index(p.X, p.Y + y, p.Z + z);
for (s16 x = 0; x != size.X; x++, i++, vi++) {
if (!vm->m_area.contains(vi))
continue;
if (schematic[i].getContent() == CONTENT_IGNORE) blitToVManip(p, vm, rot, false);
continue;
content_t c = vm->m_data[vi].getContent();
if (c != CONTENT_AIR && c != CONTENT_IGNORE)
continue;
if (schematic[i].param1 && myrand_range(1, 256) > schematic[i].param1)
continue;
vm->m_data[vi] = schematic[i];
vm->m_data[vi].param1 = 0;
}
}
} }
@ -570,32 +552,58 @@ std::string DecoSchematic::getName() {
} }
void DecoSchematic::placeStructure(Map *map, v3s16 p) { void DecoSchematic::blitToVManip(v3s16 p, ManualMapVoxelManipulator *vm,
assert(schematic != NULL); int rot, bool force_placement) {
ManualMapVoxelManipulator *vm = new ManualMapVoxelManipulator(map); int xstride = 1;
int ystride = size.X;
int zstride = size.X * size.Y;
if (flags & DECO_PLACE_CENTER_X) s16 sx = size.X;
p.X -= (size.X + 1) / 2; s16 sy = size.Y;
if (flags & DECO_PLACE_CENTER_Y) s16 sz = size.Z;
p.Y -= (size.Y + 1) / 2;
if (flags & DECO_PLACE_CENTER_Z)
p.Z -= (size.Z + 1) / 2;
v3s16 bp1 = getNodeBlockPos(p); int i_start, i_step_x, i_step_z;
v3s16 bp2 = getNodeBlockPos(p + size - v3s16(1,1,1)); switch (rot) {
vm->initialEmerge(bp1, bp2); case ROTATE_90:
i_start = sx - 1;
i_step_x = zstride;
i_step_z = -xstride;
SWAP(s16, sx, sz);
break;
case ROTATE_180:
i_start = zstride * (sz - 1) + sx - 1;
i_step_x = -xstride;
i_step_z = -zstride;
break;
case ROTATE_270:
i_start = zstride * (sz - 1);
i_step_x = -zstride;
i_step_z = xstride;
SWAP(s16, sx, sz);
break;
default:
i_start = 0;
i_step_x = xstride;
i_step_z = zstride;
}
u32 i = 0; for (s16 z = 0; z != sz; z++)
for (s16 z = 0; z != size.Z; z++) for (s16 y = 0; y != sy; y++) {
for (s16 y = 0; y != size.Y; y++) { u32 i = z * i_step_z + y * ystride + i_start;
u32 vi = vm->m_area.index(p.X, p.Y + y, p.Z + z); for (s16 x = 0; x != sx; x++, i += i_step_x) {
for (s16 x = 0; x != size.X; x++, i++, vi++) { u32 vi = vm->m_area.index(p.X + x, p.Y + y, p.Z + z);
if (!vm->m_area.contains(vi)) if (!vm->m_area.contains(vi))
continue; continue;
if (schematic[i].getContent() == CONTENT_IGNORE) if (schematic[i].getContent() == CONTENT_IGNORE)
continue; continue;
if (!force_placement) {
content_t c = vm->m_data[vi].getContent();
if (c != CONTENT_AIR && c != CONTENT_IGNORE)
continue;
}
if (schematic[i].param1 && myrand_range(1, 256) > schematic[i].param1) if (schematic[i].param1 && myrand_range(1, 256) > schematic[i].param1)
continue; continue;
@ -603,6 +611,31 @@ void DecoSchematic::placeStructure(Map *map, v3s16 p) {
vm->m_data[vi].param1 = 0; vm->m_data[vi].param1 = 0;
} }
} }
}
void DecoSchematic::placeStructure(Map *map, v3s16 p) {
assert(schematic != NULL);
ManualMapVoxelManipulator *vm = new ManualMapVoxelManipulator(map);
Rotation rot = (rotation == ROTATE_RAND) ?
(Rotation)myrand_range(ROTATE_0, ROTATE_270) : rotation;
v3s16 s = (rot == ROTATE_90 || rot == ROTATE_270) ?
v3s16(size.Z, size.Y, size.X) : size;
if (flags & DECO_PLACE_CENTER_X)
p.X -= (s.X + 1) / 2;
if (flags & DECO_PLACE_CENTER_Y)
p.Y -= (s.Y + 1) / 2;
if (flags & DECO_PLACE_CENTER_Z)
p.Z -= (s.Z + 1) / 2;
v3s16 bp1 = getNodeBlockPos(p);
v3s16 bp2 = getNodeBlockPos(p + s - v3s16(1,1,1));
vm->initialEmerge(bp1, bp2);
blitToVManip(p, vm, rot, true);
std::map<v3s16, MapBlock *> lighting_modified_blocks; std::map<v3s16, MapBlock *> lighting_modified_blocks;
std::map<v3s16, MapBlock *> modified_blocks; std::map<v3s16, MapBlock *> modified_blocks;

@ -258,6 +258,14 @@ public:
virtual std::string getName(); virtual std::string getName();
}; };
enum Rotation {
ROTATE_0,
ROTATE_90,
ROTATE_180,
ROTATE_270,
ROTATE_RAND,
};
class DecoSchematic : public Decoration { class DecoSchematic : public Decoration {
public: public:
std::string filename; std::string filename;
@ -266,6 +274,7 @@ public:
std::vector<content_t> c_nodes; std::vector<content_t> c_nodes;
u32 flags; u32 flags;
Rotation rotation;
v3s16 size; v3s16 size;
MapNode *schematic; MapNode *schematic;
@ -277,6 +286,9 @@ public:
virtual int getHeight(); virtual int getHeight();
virtual std::string getName(); virtual std::string getName();
void blitToVManip(v3s16 p, ManualMapVoxelManipulator *vm,
int rot, bool force_placement);
bool loadSchematicFile(); bool loadSchematicFile();
void saveSchematicFile(INodeDefManager *ndef); void saveSchematicFile(INodeDefManager *ndef);

@ -51,6 +51,16 @@ struct EnumString ModApiBasic::es_DecorationType[] =
{0, NULL}, {0, NULL},
}; };
struct EnumString ModApiBasic::es_Rotation[] =
{
{ROTATE_0, "0"},
{ROTATE_90, "90"},
{ROTATE_180, "180"},
{ROTATE_270, "270"},
{ROTATE_RAND, "random"},
{0, NULL},
};
ModApiBasic::ModApiBasic() : ModApiBase() { ModApiBasic::ModApiBasic() : ModApiBase() {
} }
@ -768,6 +778,8 @@ int ModApiBasic::l_register_decoration(lua_State *L)
case DECO_SCHEMATIC: { case DECO_SCHEMATIC: {
DecoSchematic *dschem = (DecoSchematic *)deco; DecoSchematic *dschem = (DecoSchematic *)deco;
dschem->flags = getflagsfield(L, index, "flags", flagdesc_deco_schematic); dschem->flags = getflagsfield(L, index, "flags", flagdesc_deco_schematic);
dschem->rotation = (Rotation)getenumfield(L, index,
"rotation", es_Rotation, ROTATE_0);
lua_getfield(L, index, "schematic"); lua_getfield(L, index, "schematic");
if (!read_schematic(L, -1, dschem, getServer(L))) { if (!read_schematic(L, -1, dschem, getServer(L))) {
@ -848,7 +860,7 @@ int ModApiBasic::l_create_schematic(lua_State *L)
} }
// place_schematic(p, schematic) // place_schematic(p, schematic, rotation)
int ModApiBasic::l_place_schematic(lua_State *L) int ModApiBasic::l_place_schematic(lua_State *L)
{ {
DecoSchematic dschem; DecoSchematic dschem;
@ -860,6 +872,12 @@ int ModApiBasic::l_place_schematic(lua_State *L)
if (!read_schematic(L, 2, &dschem, getServer(L))) if (!read_schematic(L, 2, &dschem, getServer(L)))
return 0; return 0;
Rotation rot = ROTATE_0;
if (lua_isstring(L, 3))
string_to_enum(es_Rotation, (int &)rot, std::string(lua_tostring(L, 3)));
dschem.rotation = rot;
if (!dschem.filename.empty()) { if (!dschem.filename.empty()) {
if (!dschem.loadSchematicFile()) { if (!dschem.loadSchematicFile()) {
errorstream << "place_schematic: failed to load schematic file '" errorstream << "place_schematic: failed to load schematic file '"

@ -136,11 +136,12 @@ private:
// create_schematic(p1, p2, filename) // create_schematic(p1, p2, filename)
static int l_create_schematic(lua_State *L); static int l_create_schematic(lua_State *L);
// place_schematic(p, filename) // place_schematic(p, filename, rotation)
static int l_place_schematic(lua_State *L); static int l_place_schematic(lua_State *L);
static struct EnumString es_OreType[]; static struct EnumString es_OreType[];
static struct EnumString es_DecorationType[]; static struct EnumString es_DecorationType[];
static struct EnumString es_Rotation[];
}; };