// dkdata.h

#pragma warning( disable : 4305 )

#ifdef _WIN32
#include <windows.h>
#include <direct.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <math.h>
#include <string.h>
#include <sys/stat.h>

#include "cmdlib.h"
#include "scriplib.h"
#include "mathlib.h"
#include "trilib.h"
#include "lbmlib.h"
#include "threads.h"
#include "l3dslib.h"
#include "bspfile.h"

//
// added for daikatana
//
#include "bmp.h"
#include "lightwave.h"

void DK_Cmd_Base (void);
void DK_Cmd_Cd (void);
void DK_Cmd_LGen (char *name);
void DK_Cmd_Origin (char *x, char *y, char *z);
void DK_Cmd_ScaleUp (char *scale);
void DK_Cmd_Frame (void);
void DK_Cmd_Modelname (char *name);
void DK_Cmd_Skin (void);
void DK_Cmd_Skinsize (void);
void FinishModel (char *filename);
int GrabBase (char *name);

void DK_Cmd_Inverse16Table (void);

void DK_Cmd_SpriteName (void);
void DK_Cmd_Load (void);
void DK_Cmd_SpriteFrame (void);
void FinishSprite (void);
void DK_Cmd_Sprite (char *name);

void DK_Cmd_Grab (void);
void DK_Cmd_Raw (void);
void DK_Cmd_Mip (void);
void DK_Cmd_Environment (void);
void DK_Cmd_Colormap (char *name);

void DK_Cmd_File (void);
void DK_Cmd_Dir (void);
void DK_Cmd_StartWad (void);
void DK_Cmd_EndWad (void);
void DK_Cmd_Mippal (void);
void DK_Cmd_Mipdir (char *directory);
void DK_Cmd_Alphalight (char *filename);

void DK_Cmd_Video (void);

void RemapZero (byte *pixels, byte *palette, int width, int height);

void ReleaseFile (char *filename);

extern	byte		*byteimage, *lbmpalette;
extern	int			byteimagewidth, byteimageheight;

extern	qboolean	g_release;			// don't grab, copy output data to new tree
extern	char		g_releasedir[1024];	// c:\quake2\baseq2, etc
extern	qboolean	g_archive;			// don't grab, copy source data to new tree
extern	qboolean	do3ds;
extern	char		g_only[256];		// if set, only grab this cd
extern	qboolean	g_skipmodel;		// set true when a cd is not g_only
extern	qboolean	forceuv;
extern	qboolean	recurse;

extern	char		*trifileext;

extern int			global_argc;
extern char			**global_argv;

//===================================================
//
// Daikatana specific stuff
//
//===================================================

typedef struct
{
	int	version;
	int	num_images;
	int	num_polys;
} uvinfo_t;

typedef vec_t uv_t[2];

extern uvinfo_t	uvinfo;
extern uv_t		uvdata[MAX_TRIANGLES][3];		// each element in the array corresponds directly 
												// to each element in the triangles array

extern int			useduvchunk;				// uv chunk usage
extern extinfo_t	extrainfo;					// extra info header
extern extsurf_t	surface[MAX_SURFACES];		// surface information
extern extpoly_t	polygons[MAX_TRIANGLES];	// polygon information

//	Nelno:	added this because frames other than the base frame do not always have valid
//	surface info.  GrabFrame was overwriting the surfid's grabbed from the base frame
extern extpoly_t	base_polygons[MAX_TRIANGLES];	// polygon information

extern int			glsurfpoints[16384];		// skin per surface info - HACK, FIXME!
extern int			numsurfpoints;				// skin per surface info - HACK, FIXME!
extern lwpoly_t		controlpoly;				// to test for re-ordered polys
extern lwinfo_t		controlobject;				// the base frame information
extern qboolean		polymapcreated;				// for re-mapping polygons
extern int			polymapping[MAX_TRIANGLES];	// for re-mapping polygons

extern dmdl_t		model;
extern vec3_t		base_xyz[MAX_VERTS];
extern dstvert_t	base_st[MAX_VERTS];
extern dtriangle_t	triangles[MAX_TRIANGLES];
extern char			g_skins[MAX_MD2SKINS][64];
extern char			modelname[64];	// empty unless $modelname issued (players)

int DoDirectoryWork (char *directory, char *filespec, char *skip, int (*function)(char *));
int CheckDirectoryName (char *name);

int FindTextureIndexForPoly (int index);
int FindSurfaceFlagForPoly (int index);
void InitControlObject (void);
void CreatePolyMapping (void);
void ReOrderPolys (triangle_t *ptri, int numtriangles);
void AttachPolysToSurfaces (void);
void WriteExtraData (void);
void SwapYZ (void);
void LW_ParseChunkUVCV (byte *in, int chunklength);
void LW_ParseChunkUVIM (byte *in, int chunklength);
void LoadUVFromLWO (char *filename);
void CalcPolygonNormals (void);
int LoadLightwaveTriangles (char *filename, triangle_t **pptri, int *numtriangles);
void CreateSurfaceData (int numtri);
void LoadSurfaces (char	*filename);

void LoadUVFromFile (char *filename);
