/*- BSP.H ------------------------------------------------------------------*/
/*
 * Modified 2010/08/04 by Christopher Bazley
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include <math.h>
#include <limits.h>
#include <assert.h>

#if defined(MSDOS) || defined(__MSDOS__)
#include <dos.h>
#endif
#ifdef __TURBOC__
#include <alloc.h>
#endif

#ifdef FORTIFY
#include "fortify.h"
#define GetMemory(size) GetMemoryF(size, __FILE__, __LINE__);
#define ResizeMemory(old, size) ResizeMemoryF(old, size, __FILE__, __LINE__);
#else /* FORTIFY */
#define Fortify_CheckAllMemory()
#endif /* FORTIFY */

/* cph - from FreeBSD math.h: */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

#define NELEMS(x) (sizeof(x) / sizeof((x)[0]))

/*- boolean constants ------------------------------------------------------*/

#define TRUE			1
#define FALSE			0

/*- The function prototypes ------------------------------------------------*/

#undef max
#define max(a,b) (((a)>(b))?(a):(b))

/*- print a resource name by printing first 8 characters --*/

#define Printname(dir) printf("%-8.8s",(dir)->name)

/*- Global functions & variables ------------------------------------------*/

/* blockmap.c */
extern data_callback CreateBlockmap_old;
extern data_callback CreateBlockmap_compressed;
extern data_callback *CreateBlockmap;
extern int noreject;

/* bsp.c */
void progress(void);
void FindLimits(struct Seg *, bbox_t box);
int SplitDist(struct Seg *ts);

extern const char *unlinkwad;

struct lumplist *FindDir(const char *);

/*- report an error reading a lump from the input file ---*/
void read_error(const struct lumplist *lump);

/*- seek the start of a lump in the input file -----------*/
void seek_lump(const struct lumplist *lump);

/*- read a signed 16 bit integer from the input file -----*/
int read_int16(int16_t *s);

/*- read an unsigned 16 bit integer from the input file --*/
int read_uint16(uint16_t *s);

/*- read an unsigned 32 bit integer from the input file --*/
int read_uint32(uint32_t *s);

/*- read bytes from the input file -----------------------*/
size_t read_chars(char s[], size_t n);

/*- report an error writing a lump to the output file ----*/
void write_error(const struct lumplist *lump);

/*- write a signed 16 bit integer to the output file -----*/
int write_int16(const int16_t *s);

/*- write an unsigned 16 bit integer to the output file --*/
int write_uint16(const uint16_t *s);

/*- write an unsigned 32 bit integer to the output file --*/
int write_uint32(const uint32_t *s);

/*- write bytes to the output file -----------------------*/
size_t write_chars(const char s[], size_t n);

void add_lump(const char *name, data_callback *write_data, void *data);

/* funcs.c */
extern int verbosity;
void Verbose(const char*, ...)  __attribute__((format(printf,1,2)));
void ProgError(const char *, ...) __attribute__((format(printf,1,2), noreturn));
#ifdef FORTIFY
void* GetMemoryF(size_t size, const char *file, unsigned long line);
void* ResizeMemoryF(void *old, size_t size, const char *file, unsigned long line);
#else /* FORTIFY */
void* GetMemory(size_t) __attribute__((warn_unused_result, malloc));
void* ResizeMemory(void *, size_t) __attribute__((warn_unused_result));
#endif /* FORTIFY */
/* level.c */

void DoLevel(void);
void LevelFinished(void);

extern struct Vertex *vertices;
extern long num_verts;

extern struct LineDef *linedefs;
extern long num_lines;

extern struct SideDef *sidedefs;
extern long num_sides;

extern struct Sector *sectors;
extern long num_sects;

extern struct SSector *ssectors;
extern long num_ssectors;

extern struct Pseg *psegs;
extern long num_psegs;

extern long num_nodes;

extern unsigned char *SectorHits;

extern long psx,psy,pex,pey,pdx,pdy;
extern long lsx,lsy,lex,ley;

/* makenode.c */
struct Node *CreateNode(struct Seg *, const bbox_t bbox);
unsigned ComputeAngle(int,int);

/* picknode.c */
extern int factor;

struct Seg *PickNode_traditional(struct Seg *, const bbox_t bbox);
struct Seg *PickNode_visplane(struct Seg *, const bbox_t bbox);
extern struct Seg *(*PickNode)(struct Seg *, const bbox_t bbox);
int DoLinesIntersect(void);
void ComputeIntersection(short int *outx,short int *outy);

/* malloc edbugging with dmalloc */
#ifdef WITH_DMALLOC
#include <dmalloc.h>

#define GetMemory malloc
#define ResizeMemory realloc

#endif

/*------------------------------- end of file ------------------------------*/
