cache and mapgen support
This commit is contained in:
parent
48aff74815
commit
e339d405a6
15
build.lua
15
build.lua
@ -157,3 +157,18 @@ function building_lib.do_build(mapblock_pos, building_name, rotation, callback)
|
|||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- mapgen build shortcut, only for 1x1x1 sized buildings
|
||||||
|
function building_lib.do_build_mapgen(mapblock_pos, building_name, rotation)
|
||||||
|
local building_def = building_lib.get_building(building_name)
|
||||||
|
local placement = building_lib.get_placement(building_def.placement)
|
||||||
|
|
||||||
|
building_lib.store:merge(mapblock_pos, {
|
||||||
|
building = {
|
||||||
|
name = building_def.name,
|
||||||
|
rotation = rotation
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
placement.place(placement, mapblock_pos, building_def, rotation)
|
||||||
|
end
|
@ -1,14 +1,30 @@
|
|||||||
|
|
||||||
building_lib.register_placement("default", {
|
building_lib.register_placement("default", {
|
||||||
place = function(self, mapblock_pos, building_def, rotation, callback)
|
place = function(self, mapblock_pos, building_def, rotation, callback)
|
||||||
|
callback = callback or function() end
|
||||||
|
|
||||||
local catalog
|
local catalog
|
||||||
local offset = {x=0, y=0, z=0}
|
local offset = {x=0, y=0, z=0}
|
||||||
|
local cache = false
|
||||||
|
|
||||||
if type(building_def.catalog) == "table" then
|
if type(building_def.catalog) == "table" then
|
||||||
catalog = mapblock_lib.get_catalog(building_def.catalog.filename)
|
catalog = mapblock_lib.get_catalog(building_def.catalog.filename)
|
||||||
offset = building_def.catalog.offset
|
offset = building_def.catalog.offset
|
||||||
|
cache = building_def.catalog.cache
|
||||||
else
|
else
|
||||||
catalog = mapblock_lib.get_catalog(building_def.catalog)
|
catalog = mapblock_lib.get_catalog(building_def.catalog)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if cache and building_def._cache and building_def._cache[rotation] then
|
||||||
|
-- rotated building already cached
|
||||||
|
building_def._cache[rotation](mapblock_pos)
|
||||||
|
callback()
|
||||||
|
return
|
||||||
|
else
|
||||||
|
-- initialize cache if not already done
|
||||||
|
building_def._cache = building_def._cache or {}
|
||||||
|
end
|
||||||
|
|
||||||
local size = self.get_size(self, mapblock_pos, building_def, 0)
|
local size = self.get_size(self, mapblock_pos, building_def, 0)
|
||||||
|
|
||||||
local catalog_pos1 = vector.add({x=0, y=0, z=0}, offset)
|
local catalog_pos1 = vector.add({x=0, y=0, z=0}, offset)
|
||||||
@ -31,7 +47,7 @@ building_lib.register_placement("default", {
|
|||||||
-- translate to world-coords
|
-- translate to world-coords
|
||||||
local world_pos = vector.add(mapblock_pos, rotated_rel_catalog_pos)
|
local world_pos = vector.add(mapblock_pos, rotated_rel_catalog_pos)
|
||||||
|
|
||||||
catalog:deserialize(catalog_pos, world_pos, {
|
local place_fn = catalog:prepare(catalog_pos, {
|
||||||
transform = {
|
transform = {
|
||||||
rotate = {
|
rotate = {
|
||||||
axis = "y",
|
axis = "y",
|
||||||
@ -41,11 +57,20 @@ building_lib.register_placement("default", {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
-- cache prepared mapblock if enabled
|
||||||
|
if cache then
|
||||||
|
-- verify size constraints for caching
|
||||||
|
assert(vector.equals(size, {x=1, y=1, z=1}))
|
||||||
|
building_def._cache[rotation] = place_fn
|
||||||
|
end
|
||||||
|
|
||||||
|
place_fn(world_pos)
|
||||||
minetest.after(0, worker)
|
minetest.after(0, worker)
|
||||||
end
|
end
|
||||||
|
|
||||||
worker()
|
worker()
|
||||||
end,
|
end,
|
||||||
|
|
||||||
get_size = function(_, _, building_def, rotation)
|
get_size = function(_, _, building_def, rotation)
|
||||||
local size
|
local size
|
||||||
if type(building_def.catalog) == "table" then
|
if type(building_def.catalog) == "table" then
|
||||||
@ -56,6 +81,7 @@ building_lib.register_placement("default", {
|
|||||||
end
|
end
|
||||||
return mapblock_lib.rotate_size(size, rotation)
|
return mapblock_lib.rotate_size(size, rotation)
|
||||||
end,
|
end,
|
||||||
|
|
||||||
validate = function(_, building_def)
|
validate = function(_, building_def)
|
||||||
local catalogfilename = building_def.catalog
|
local catalogfilename = building_def.catalog
|
||||||
if type(building_def.catalog) == "table" then
|
if type(building_def.catalog) == "table" then
|
||||||
|
@ -20,7 +20,7 @@ function building_lib.do_remove(mapblock_pos)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local mapblock_data, origin = mapblock_lib.resolve_data_link(building_lib.store, mapblock_pos)
|
local mapblock_data, origin = mapblock_lib.resolve_data_link(building_lib.store, mapblock_pos)
|
||||||
local size = mapblock_data.building.size
|
local size = mapblock_data.building.size or {x=1, y=1, z=1}
|
||||||
|
|
||||||
for x=origin.x,origin.x+size.x-1 do
|
for x=origin.x,origin.x+size.x-1 do
|
||||||
for y=origin.y,origin.y+size.y-1 do
|
for y=origin.y,origin.y+size.y-1 do
|
||||||
|
@ -146,7 +146,7 @@ minetest.register_tool("building_lib:remove", {
|
|||||||
|
|
||||||
local building_def = building_lib.get_building(building_info.name)
|
local building_def = building_lib.get_building(building_info.name)
|
||||||
|
|
||||||
local size = building_lib.get_building_size(building_def, building_info.rotation)
|
local size = building_lib.get_building_size(building_def, building_info.rotation or 0)
|
||||||
local mapblock_pos2 = vector.add(origin, vector.subtract(size, 1))
|
local mapblock_pos2 = vector.add(origin, vector.subtract(size, 1))
|
||||||
|
|
||||||
local can_remove = building_lib.can_remove(origin)
|
local can_remove = building_lib.can_remove(origin)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user