/*********************************************************************//**
*	\brief Definice formatu souboru BSP.
*
*	Format BSP v sobe obsahuje vsechny informace, ktre vedou k rekonstrukci
*	cele sceny, vcetne BSP stromu, informaci o vzajemne viditelnosti listu
*	polygonu, vertexu, hran, rovin, textur a entit. 
*
*======================================================================
*
*	Copyright (c) 1996-2002, Valve LLC. All rights reserved.
*	
*	This product contains software technology licensed from Id 
*	Software, Inc. ("Id Technology").  Id Technology (c) 1996 Id Software, Inc. 
*	All Rights Reserved.
*
*=======================================================================                                                                 
*
*	\author Michal Jirous
*	\date 4.2.2009
*	\file level_structs.h
**********************************************************************/  


/* Contents of this file was changes for proper functionality of this project. */

#ifndef __BSP_FILE_H__
#define __BSP_FILE_H__

#ifdef PHOBIA
	#include "algebraic.h"
#endif
// upper design bounds

#define	MAX_MAP_HULLS		4

#define	MAX_MAP_MODELS		400
#define	MAX_MAP_BRUSHES		4096
#define	MAX_MAP_ENTITIES	1024
#define	MAX_MAP_ENTSTRING	(128*1024)

#define	MAX_MAP_PLANES		32767
#define	MAX_MAP_NODES		32767		// because negative shorts are contents
#define	MAX_MAP_CLIPNODES	32767		//
#define	MAX_MAP_LEAFS		8192
#define	MAX_MAP_VERTS		65535
#define	MAX_MAP_FACES		65535
#define	MAX_MAP_MARKSURFACES 65535
#define	MAX_MAP_TEXINFO		8192
#define	MAX_MAP_EDGES		256000
#define	MAX_MAP_SURFEDGES	512000
#define	MAX_MAP_TEXTURES	512
#define	MAX_MAP_MIPTEX		0x200000
#define	MAX_MAP_LIGHTING	0x200000
#define	MAX_MAP_VISIBILITY	0x200000

#define	MAX_MAP_PORTALS		65536

// key / value pair sizes

#define	MAX_KEY		32
#define	MAX_VALUE	1024

//=============================================================================

#define BSPVERSION	31 /*!< @brief upravena verze pro Chronophobia. */
#define	TOOLVERSION	2

typedef unsigned char byte;

/** @brief jednotka informace v souboru BSP.
*	Pojmem LUMP se oznacuje jedna cast souboru, ktera predstavuje nejakou kolekci dat.
*	Tyto data mohou byt napriklad uzly nebo listy BSP stromu, modely, roviny,
*	body apod. Tento objekt tedy definuje pozici dat v souboru a pocet bytu,
*	ktere zabiraji.
*/
typedef struct
{
	int		fileofs,	/*!< @brief Pozice v souboru. */
			filelen;	/*!< @brief Pocet bytu dat. */
} lump_t;

#define	LUMP_ENTITIES	0		/*!< @brief Text s definici entit. */
#define	LUMP_PLANES		1		/*!< @brief Roviny -> #Plane (nebo struktura dplane_t). */
#define	LUMP_TEXTURES	2		/*!< @brief Data textur -> vycet struktur dmipmap_t. */
#define	LUMP_VERTEXES	3		/*!< @brief Body, vertexy -> #Point (nebo struktura dvertex_t) */
#define	LUMP_VISIBILITY	4		/*!< @brief VIS portaly, clustery, vzajemna viditelnost listu (pouze byty) */
#define	LUMP_NODES		5		/*!< @brief Uzly BSP stromu - dnode_t */
#define	LUMP_TEXINFO	6		/*!< @brief texinfo_t, informace o texture. */
#define	LUMP_FACES		7		/*!< @brief Faces = polygony - dface_t */
#define	LUMP_LIGHTING	8		/*!< @brief Lightmapy (data jako byty, do tohoto pole ukazuji Faces). */
#define	LUMP_CLIPNODES	9		/*!< @brief Uzly pro clipping hulls (diky nefunkcnosti (pravdepodobne nepresnosti) docela k nicemu ). */
#define	LUMP_LEAFS		10		/*!< @brief Listy stromu dleaf_t. */
#define	LUMP_MARKSURFACES 11	/*!< @brief Indexy ploch = pouzivaji je listy stromu (listy pristupuji k polygonum pres tyto indexy. */
#define	LUMP_EDGES		12		/*!< @brief Hrany -> dedge_t. */
#define	LUMP_SURFEDGES	13		/*!< @brief Indexy do edges, kde zaporne cislo, znamena opacnou orientaci hrany. */
#define	LUMP_MODELS		14		/*!< @brief Jednotlive modely: worldspawn, door atd. (entity). */
#define LUMP_FOGCOORDS	15		/*!< @brief Souradnice mlhy. */
#define LUMP_TEXTS		16

#define	HEADER_LUMPS	20	/*!< @brief Celkovy pocet LUMPs. */



/*
vec3_t	hull_size[NUM_HULLS][2] = {
{ {0, 0, 0}, {0, 0, 0} },
{ {-16,-16,-36}, {16,16,36} },// 32x32x72
{ {-32,-32,-32}, {32,32,32} }, // 64x64x64
{ {-16,-16,-18}, {16,16,18} } // 32x32x36
*/

const int HULL_REALWORLD		= 0;	/*!< @brief HULL realneho sveta, coz znamena bez rozsireni okraju. */
const int HULL_PLAYER			= 1;	/*!< @brief HULL hrace, kde vzdniklo rozsireni vsech pevnych sten o rozmery hrace 32x32x72. */
const int HULL_MODEL64			= 2;	/*!< @brief HULL rozsireny o 64x64x64. */
const int HULL_PLAYER_DUCKED	= 3;	/*!< @brief HULL rozsireny pro skrceneho hrace 32x32x36. */

const float HULL_HEIGHTS[4] = {0,72,36,64};	/*!< @brief Vysky pro vsechny typy HULLs. */
const float HULL_WIDTHS[4] = {0,32,32,64};	/*!< @brief Sirky  pro vsechny typy HULLs. */



/** @brief Jeden model, ktery definuje fyzicky solid model entity. */
typedef struct
{
#ifdef PHOBIA
	BoundingBox bounds;			/*!< @brief Bounding box obalujici vsechny body modelu (neboli= float mins[3], maxs[3]; ). */
#else
	float mins[3],maxs[3];
#endif
	float		origin[3];		/*!< @brief Stred modelu. */
	int			headnode[MAX_MAP_HULLS];	/*!< @brief Indexy do pole uzlu pro vsechny HULLs (1. je do pole dnode_t a ostatni do dclipnode_t). */
	int			visleafs;		/*!< @brief Pocet listu bez solid lustu cislo 0. (orig.: not including the solid leaf 0). */
	int			firstface,		/*!< @brief Polygony obalujici cely tento objekt (asi, protoze funkcnost jsem dosud neoveril). */
				numfaces;		/*!< @brief Pocet polygonu obalujicich tento model. */
} dmodel_t;

/** @brief Hlavicka souboru. */
typedef struct
{
	int			version;				/*!< @brief Verze BSP. */
	lump_t		lumps[HEADER_LUMPS];	/*!< @brief Seznam objektu lumo_t, kde budou ulozeny informace o vsech datech v souboru. */
} dheader_t;


/****************************************
*	LUMP_TEXTURES area
*****************************************/

/** @brief Dejme tomu, ze tohle je hlavicka dat textur, ktera obsahuje jen pocet textur (v originale tam je). */
typedef struct
{
	int			nummiptex;	/*!< @brief Pocet textur. */
	//int			dataofs[4];		// [nummiptex] = kravina, protoze tam bude urcite vice textur, je to jenom hlavicka
} dmiptexlump_t;

#define	MIPLEVELS	4	/*!< @brief Pocet urovni obrazku (pro mipmapy). */
/** @brief Objekt obsahujici informace o obrazku textury. */
typedef struct miptex_s
{
	char		name[16];	/*!< @brief Nazev textury, coz je jeji identifikator. */
	unsigned	width,		/*!< @brief Sirka textury. */
				height;		/*!< @brief Vyska textury. */
	unsigned	offsets[MIPLEVELS];	/*!< @brief Pozice dat textury. */
} miptex_t;
//=========================================

#ifdef PHOBIA
//Point je stejny jako vertex zde
typedef Point dvertex_t;	/*!< @brief Jeden vertex v souboru. */

#else
typedef struct
{
	float	point[3];
} dvertex_t;
#endif




#ifdef PHOBIA
//Definice roviny v algebraic je stejna jako typ zde
typedef Plane dplane_t;	/*!< @brief Jedna rovina v souboru. */
#else
typedef struct
{
	float	normal[3];
	float	dist;
	int		type;		// PLANE_X - PLANE_ANYZ ?remove? trivial to regenerate
} dplane_t;
#endif


#define	CONTENTS_EMPTY		-1
#define	CONTENTS_SOLID		-2
#define	CONTENTS_WATER		-3
#define	CONTENTS_SLIME		-4
#define	CONTENTS_LAVA		-5
#define	CONTENTS_SKY		-6
#define	CONTENTS_ORIGIN		-7		// removed at csg time
#define	CONTENTS_CLIP		-8		// changed to contents_solid

#define	CONTENTS_CURRENT_0		-9
#define	CONTENTS_CURRENT_90		-10
#define	CONTENTS_CURRENT_180	-11
#define	CONTENTS_CURRENT_270	-12
#define	CONTENTS_CURRENT_UP		-13
#define	CONTENTS_CURRENT_DOWN	-14

#define CONTENTS_TRANSLUCENT	-15




// !!! if this is changed, it must be changed in asm_i386.h too !!!
/** @brief Jeden uzel BSP stromu. */
typedef struct
{
	int			planenum;		/*!< @brief Cislo roviny, pro urceni potomku pri pruchodu. */
	short		children[2];	/*!< @brief Cisla potomku (zaporne cislo znamena list a cislo listu se zjisti jako -(index+1) ). */
	short		mins[3];		/*!< @brief Minimalni bod bounding boxu. */
	short		maxs[3];		/*!< @brief Maximalni bod bounding boxu. */
	unsigned short	firstface;	/*!< @brief Zase polygony, ale dosud nezjisteno jaky. */
	unsigned short	numfaces;	/*!< @brief Pocet polygonu (// counting both sides). */
} dnode_t;

/** @brief Uzel pro clipping hull 1 a vice. */
typedef struct
{
	int			planenum;		/*!< @brief Cislo delici roviny. */
	short		children[2];	/*!< @brief Cisla potomku (zaporne cislo znamena, ze neco obsahuje: viz vsechny typy CONTENTS). */
} dclipnode_t;

/** @brief Informace o mapovani textury na polygon. */
typedef struct texinfo_s
{
	float		vecs[2][4];		/*!< @brief Souradnice pro U,V (s,t), ze kterych se vypocitaji vysledne texturovaci souradnice. ([s/t][xyz offset] ). */
	int			miptex;			/*!< @brief Cislo miptex struktury, kde jsou informace o obrazku textury. */
	int			flags;			/*!< @brief Parametry textury (cislo 1 znamena bez lightmapy). */
} texinfo_t;
#define	TEX_SPECIAL		1		/*!< @brief  sky or slime, no lightmap or 256 subdivision. */

// note that edge 0 is never used, because negative edge nums are used for
// counterclockwise use of the edge in a face
/** @brief Hrana polygonu. */
typedef struct
{
	unsigned short	v[2];		/*!< @brief Cisla vertexu v poli dvertex_t (LUMP_VERTEXES) vertex numbers. */
} dedge_t;

#define	MAXLIGHTMAPS	4		/*!< @brief Maximalni pocet lightmap. */

/** @brief Polygon (Face). */
typedef struct
{
	short		planenum;		/*!< @brief Cislo roviny tohoto polygonu. */
	short		side;			/*!< @brief Cislo 1 znamena opacny smer vektoru roviny. */

	int			firstedge;		/*!< @brief Prvni pozice v surfedges, kde najdeme cislo hrany (zaporne cislo hrany znamena opacny smer hrany). */
	short		numedges;		/*!< @brief Poced hran. */
	short		texinfo;		/*!< @brief Index do pole informaci o texture. */

// lighting info
	byte		styles[MAXLIGHTMAPS];	/*!< @brief Osvetlovaci styly polygonu (standardne ma pouze jeden zakladni styl, tj. jednu lightmapu). */
	int			lightofs;		/*!< @brief Pozice dat prvni lightmapy (start of [numstyles*surfsize] samples). */ 
} dface_t;



#define	AMBIENT_WATER	0
#define	AMBIENT_SKY		1
#define	AMBIENT_SLIME	2
#define	AMBIENT_LAVA	3

#define	NUM_AMBIENTS			4		// automatic ambient sounds

// leaf 0 is the generic CONTENTS_SOLID leaf, used for all solid areas
// all other leafs need visibility info

/** @brief List BSP stromu. */
typedef struct
{
	int			contents;			/*!< @brief Obsah listu (viz. vsechny CONTENTS). */
	int			visofs;				/*!< @brief Pozice dat viditelnosti (VIS) (-1 znamena ze tyto informace nejsou). */

	short		mins[3];			/*!< @brief Minimalni bod bounding boxu (for frustum culling). */
	short		maxs[3];			/*!< @brief Maximalni bod bounding boxu (for frustum culling). */

	unsigned short		firstmarksurface;	/*!< @brief Prvni pozice v marksurfaces, kde jsou indexy polygonu. */
	unsigned short		nummarksurfaces;	/*!< @brief Pocet polygonu. */

	byte		ambient_level[NUM_AMBIENTS];	/*!< @brief Ambient levely (dosud neotestovano). */
} dleaf_t;


#endif

