From 3fda15a2202739df836975928f51f823bca9a6be Mon Sep 17 00:00:00 2001 From: Sfan5 Date: Sun, 22 Sep 2013 18:03:27 +0200 Subject: [PATCH] Allow WorldEdit schematics to be used as buildings --- villages.lua | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/villages.lua b/villages.lua index df788dd..784a0c7 100644 --- a/villages.lua +++ b/villages.lua @@ -289,7 +289,81 @@ end local function generate_building(pos, minp, maxp, data, a, pr, extranodes) local binfo = buildings[pos.btype] - local scm = rotate(binfo.scm, pos.brotate) + local scm + if type(binfo.scm) == "string" then + local f, err = io.open(minetest.get_modpath("mg").."/schems/"..binfo.scm..".we", "r") + if not f then + error("Could not open schematic '" .. binfo.scm .. ".we': " .. err) + end + value = f:read("*a") + f:close() + value = value:gsub("return%s*{", "", 1):gsub("}%s*$", "", 1) + local escaped = value:gsub("\\\\", "@@"):gsub("\\\"", "@@"):gsub("(\"[^\"]*\")", function(s) return string.rep("@", #s) end) + local startpos, startpos1, endpos = 1, 1 + local nodes = {} + while true do + startpos, endpos = escaped:find("},%s*{", startpos) + if not startpos then + break + end + local current = value:sub(startpos1, startpos) + table.insert(nodes, minetest.deserialize("return " .. current)) + startpos, startpos1 = endpos, endpos + end + table.insert(nodes, minetest.deserialize("return " .. value:sub(startpos1))) + scm = {} + local maxx, maxy, maxz = -1, -1, -1 + for i = 1, #nodes do + local ent = nodes[i] + if ent.x > maxx then + maxx = ent.x + end + if ent.y > maxy then + maxy = ent.y + end + if ent.z > maxz then + maxz = ent.z + end + + if scm[ent.y] == nil then + scm[ent.y] = {} + end + if scm[ent.y][ent.x] == nil then + scm[ent.y][ent.x] = {} + end + if ent.param2 == nil then + ent.param2 = 0 + end + if ent.meta == nil then + ent.meta = {fields={}, inventory={}} + end + + if ent.param2 == 0 and #ent.meta.fields == 0 and #ent.meta.inventory == 0 then + scm[ent.y][ent.x][ent.z] = minetest.get_content_id(ent.name) + else + scm[ent.y][ent.x][ent.z] = {node={name=ent.name, param2=ent.param2}, meta=ent.meta} + end + end + local c_ignore = minetest.get_content_id("ignore") + for x = 1, maxx do + for y = 1, maxy do + for z = 1, maxz do + if scm[ent.y] == nil then + scm[ent.y] = {} + end + if scm[ent.y][ent.x] == nil then + scm[ent.y][ent.x] = {} + end + if scm[ent.y][ent.x][ent.z] == nil then + scm[ent.y][ent.x][ent.z] = c_ignore + end + end + end + end + else + scm = binfo.scm + end + scm = rotate(scm, pos.brotate) local t for x = 0, pos.bsizex-1 do for y = 0, binfo.ysize-1 do