---
--@type building
--@field #table area two positions marking the size of the building
--@field #table anchor the anchor point of the schematic
local building = {}

fn building.new(o) {
	return setmetatable(o or {}, {__index = building})
}

-- #table all buildings that ever were in the world
local building_store
building_store = (() => {
	local file_name = minetest.get_worldpath() .. "/building_signs"

	minetest.register_on_shutdown(() => {
		local file = io.open(file_name, "w")
		file:write(minetest.serialize(building_store))
		file:close()
	})

	local file = io.open(file_name, "r")
	if file ~= nil {
		local data = file:read("*a")
		file:close()
		local ret = {}
		for i,v in pairs(minetest.deserialize(data)) {
		  ret[i] = building.new(v)
		}
		return ret
	}
	return {}
}) ()

fn building_sign.get(pos, create) {
	local poshash = minetest.hash_node_position(pos)
	if building_sign.building_store[poshash] == nil and create {
		building_sign.building_store[poshash] = building.new()
	}
	return building_sign.building_store[poshash]
}

building_sign.building = building
building_sign.building_store = building_store

return building_store
