local closingDoor = MoveEvent()

function closingDoor.onStepOut(creature, item, position, fromPosition)
	local tile = Tile(position)
	if tile:getCreatureCount() > 0 then
		return true
	end

	local newPosition = {x = position.x + 1, y = position.y, z = position.z}
	local query = Tile(newPosition):queryAdd(creature, FLAG_IGNOREBLOCKCREATURE)
	if query ~= RETURNVALUE_NOERROR or query == RETURNVALUE_NOTENOUGHROOM then
		newPosition.x = newPosition.x - 1
		newPosition.y = newPosition.y + 1
		query = Tile(newPosition):queryAdd(creature, FLAG_IGNOREBLOCKCREATURE)
	end

	if query == RETURNVALUE_NOERROR or query ~= RETURNVALUE_NOTENOUGHROOM then
		doRelocate(position, newPosition)
	end

	local i, tileItem, tileCount = 1, true, tile:getThingCount()
	while tileItem and i < tileCount do
		tileItem = tile:getThing(i)
		if tileItem and tileItem:getUniqueId() ~= item.uid and tileItem:getType():isMovable() then
			tileItem:remove()
		else
			i = i + 1
		end
	end

	item:transform(item.itemid - 1)
	return true
end

for _, i in ipairs(openLevelDoors) do
	closingDoor:id(i)
end
for _, i in ipairs(openQuestDoors) do
	closingDoor:id(i)
end
closingDoor:register()
