Schematic: Read slice probability table from schematic descriptors
authorkwolekr <kwolekr@minetest.net>
Sun, 19 Jan 2014 07:44:22 +0000 (02:44 -0500)
committerkwolekr <kwolekr@minetest.net>
Sun, 19 Jan 2014 07:44:45 +0000 (02:44 -0500)
src/mapgen.cpp
src/script/common/c_content.cpp

index 4433a2a58343b61cf50ef0429f2e53a5022ff44d..a07f24db5873f59262ecf12d6c4f9067633bdfee 100644 (file)
@@ -789,6 +789,7 @@ bool DecoSchematic::loadSchematicFile() {
        Version changes:
        1 - Initial version
        2 - Fixed messy never/always place; 0 probability is now never, 0xFF is always
+       3 - Added y-slice probabilities; this allows for variable height structures
 */
 void DecoSchematic::saveSchematicFile(INodeDefManager *ndef) {
        std::ostringstream ss(std::ios_base::binary);
index c4acb7c32e48969fc869cccb40fc9275e1fb4f8b..0d1f7aa03bccdd4b28996f8bd02a13ab2c7217f0 100644 (file)
@@ -958,6 +958,7 @@ bool read_schematic(lua_State *L, int index, DecoSchematic *dschem, Server *serv
                MapNode *schemdata = new MapNode[numnodes];
                int i = 0;
                
+               // Get schematic data
                lua_getfield(L, index, "data");
                luaL_checktype(L, -1, LUA_TTABLE);
                
@@ -986,15 +987,34 @@ bool read_schematic(lua_State *L, int index, DecoSchematic *dschem, Server *serv
                        lua_pop(L, 1);
                }
                
-               dschem->size      = size;
-               dschem->schematic = schemdata;
-               
                if (i != numnodes) {
                        errorstream << "read_schematic: incorrect number of "
                                "nodes provided in raw schematic data (got " << i <<
                                ", expected " << numnodes << ")." << std::endl;
                        return false;
                }
+
+               u8 *sliceprobs = new u8[size.Y];
+               for (i = 0; i != size.Y; i++)
+                       sliceprobs[i] = MTSCHEM_PROB_ALWAYS;
+
+               // Get Y-slice probability values (if present)
+               lua_getfield(L, index, "yslice_prob");
+               if (lua_istable(L, -1)) {
+                       lua_pushnil(L);
+                       while (lua_next(L, -2)) {
+                               if (getintfield(L, -1, "ypos", i) && i >= 0 && i < size.Y) {
+                                       sliceprobs[i] = getintfield_default(L, -1,
+                                               "prob", MTSCHEM_PROB_ALWAYS);
+                               }
+                               lua_pop(L, 1);
+                       }
+               }
+
+               dschem->size        = size;
+               dschem->schematic   = schemdata;
+               dschem->slice_probs = sliceprobs;
+
        } else if (lua_isstring(L, index)) {
                dschem->filename = std::string(lua_tostring(L, index));
        } else {