/*
* region.h: Functions for parsing Minecraft region files.
*
* Copyright (c) 2013, Benjamin Johnson.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the original developer nor the names of any
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef LIBANVIL_REGION_H
#define LIBANVIL_REGION_H
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#include <math.h> /* for floorf() */
#include "cNBT/nbt.h"
/* The number of chunks in any dimension in a given region */
#define CHUNKS_PER_REGION 32
/*
* Convert chunk to region coordinates.
*/
#define C2R(coord) ((int) floorf((coord) / (float)CHUNKS_PER_REGION))
/*
* Open a file to read region data.
* The x and z parameters are in region coordinates.
*/
FILE *region_open_read(const char *base_dir, int x, int z);
/*
* Read the region header from an open file.
*/
char *region_read_header(FILE *fp);
/*
* Return the parsed NBT data for the chunk at (x, z).
* The x and z parameters are in region coordinates.
*/
nbt_node *region_get_chunk(FILE *region_file,
char *header, int x, int z);
/*
* Open a file to write region data.
* The x and z parameters are in region coordinates.
*/
FILE *region_open_write(const char *base_dir, int x, int z);
/*
* Open a file to both read and write region data.
* The x and z parameters are in region coordinates.
*/
FILE *region_open_read_write(const char *base_dir, int x, int z);
/*
* Return a new, empty region header.
*/
char *region_create_header(void);
/*
* Free a region header.
*/
void region_free_header(char *header);
/*
* Write a chunk to the specified region file.
* The x and z parameters are in chunk coordinates.
*
* Existing chunk data is NOT overwritten in place, to avoid corrupting
* other chunks if the new data is larger than the existing data. Instead,
* the region header is simply updated to point to the new data, which is
* written to the end of the region file. This is harmless and will not
* affect game performance, though repeated calls will result in gradually
* increasing region file sizes.
*
* You can remove old chunk data from region files using the program mctrim.c
* included in the libanvil distribution.
*/
bool region_write_chunk(FILE *region_file,
char *header, nbt_node *chunk_data, int x, int z);
/*
* Close an open region file.
*/
void region_close(FILE *region_file);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* LIBANVIL_REGION_H */