def save_map(map_name, base_dir=MAP_DIR, only_cached=True):
	_map_dir = os.path.join(base_dir, map_name)
	
	#if base_dir == DATA_DIR:
	#	_map_dir = os.path.join(_map_dir, map_name)

	try:
		os.makedirs(_map_dir)
	except:
		pass
		
	for light in WORLD_INFO['lights']:
		if 'los' in light:
			del light['los']
		
		if 'old_pos' in light:
			del light['old_pos']

	with open(os.path.join(_map_dir, 'world.meta'), 'w') as _map_file:
		try:
			_slices = WORLD_INFO['slices']
			_references = WORLD_INFO['references']
			_chunk_map = WORLD_INFO['chunk_map']
			_map = WORLD_INFO['map']
			_weather_light_map = None
			
			del WORLD_INFO['slices']
			del WORLD_INFO['chunk_map']
			del WORLD_INFO['references']
			del WORLD_INFO['map']
			
			WORLD_INFO['map_size'] = maputils.get_map_size(_map)
			
			if 'light_map' in WORLD_INFO['weather']:
				_weather_light_map = WORLD_INFO['weather']['light_map']
				
				del WORLD_INFO['weather']['light_map']
			
			logging.debug('Writing map metadata to disk...')
			
			_map_file.write('world_info:%s\n' % json.dumps(WORLD_INFO))
			
			for _slice in _slices.keys():
				if '_map' in _slices[_slice]:
					del _slices[_slice]['_map']
				
				_map_file.write('slice:%s:%s\n' % (_slice, json.dumps(_slices[_slice])))
			
			for _chunk_key in _chunk_map:
				_map_file.write('chunk:%s:%s\n' % (_chunk_key, json.dumps(_chunk_map[_chunk_key])))
			
			#_map_file.write('slice_map:%s' % json.dumps(_slice_map))
			
			WORLD_INFO['slices'] = _slices
			WORLD_INFO['chunk_map'] = _chunk_map
			WORLD_INFO['references'] = _references
			WORLD_INFO['map'] = _map
			#WORLD_INFO['slice_map'] = _slice_map
			
			if _weather_light_map:
				WORLD_INFO['weather']['light_map'] = _weather_light_map
			
			#logging.debug('Reloading slices...')
			#reload_slices()
			#logging.debug('Done!')
			
		except __HOLE__ as e:
			logging.critical('FATAL: Map not JSON serializable.')
			gfx.log('TypeError: Failed to save map (Map not JSON serializable).')
			
			raise e
		
	_chunk_cluster_size = WORLD_INFO['chunk_size']*10
	_map = WORLD_INFO['map']
	
	del WORLD_INFO['map']
	
	if only_cached:
		_cluster_keys = LOADED_CHUNKS
	else:
		_cluster_keys = []
		
		for y1 in range(0, MAP_SIZE[1], _chunk_cluster_size):
			for x1 in range(0, MAP_SIZE[0], _chunk_cluster_size):
				_cluster_keys.append('%s,%s' % (x1, y1))
				
	for cluster_key in _cluster_keys:
		_x1 = int(cluster_key.split(',')[0])
		_y1 = int(cluster_key.split(',')[1])
		
		with open(os.path.join(_map_dir, 'world_%s.cluster' % cluster_key.replace(',', '_')), 'w') as _cluster_file:
			for y2 in range(_y1, _y1+_chunk_cluster_size):
				for x2 in range(_x1, _x1+_chunk_cluster_size):
					_cluster_file.write(json.dumps(_map[x2][y2])+'\n')
	
	WORLD_INFO['map'] = _map
	SETTINGS['base_dir'] = _map_dir
dataset/ETHPy150Open flags/Reactor-3/maps.py/save_map