////////////////////////////////////////////////////////////////////////
// A5 DLL interface header
// (c) Conitec / JCL 2000, 2002
// 05-2-02 last change
// Do not modify this file
///////////////////////////////////////////////////////////////////////
#ifndef A5DLL_H
#define A5DLL_H
#include "stdafx.h"

///////////////////////////////////////////////////////////////////////
#define DLLFUNC extern "C" __declspec(dllexport)

typedef unsigned char byte;
typedef long fixedPoint;	// fixedPoint point 22.10 number format used by C-Script
inline fixedPoint INT2FIX(int i) { return i<<10; }
inline int FIX2INT(fixedPoint x) { return x>>10; }
inline double FIX2FLOAT(fixedPoint x) { return ((double)x)/(1<<10); }
inline fixedPoint FLOAT2FIX(double f) { return (fixedPoint)(f*(1<<10)); }

///////////////////////////////////////////////////////////////////////
// A4 flag/mask definitions
// note: all other flag bits are used internally and must not be modified!!
///////////////////////////////////////////////////////////////////////

// trace / move mode flags
const long TRMF_IGNORE_ME 		= 1<<0;
const long TRMF_IGNORE_YOU		= 1<<1;
const long TRMF_IGNORE_PASSABLE = 1<<2;
const long TRMF_IGNORE_PASSENTS = 1<<3;
const long TRMF_IGNORE_MODELS	= 1<<4;
const long TRMF_IGNORE_SPRITES	= 1<<5;
const long TRMF_IGNORE_MAPS	= 1<<7;
const long TRMF_IGNORE_WORLD	= 1<<8;
const long TRMF_IGNORE_PUSH	= 1<<9;
const long TRMF_ACTIVATE_SHOOT	= 1<<10;
const long TRMF_ACTIVATE_SONAR	= 1<<11;
const long TRMF_ACTIVATE_TRIGGER = 1<<11;
const long TRMF_USE_BOX		= 1<<13;
const long TRMF_SCAN_TEXTURE	= 1<<16;
const long TRMF_GLIDE			= 1<<18;

// entity property flags
const long ENF_FLAG1	= 1<<0;	// FLAG1..FLAG8 for WDL use
const long ENF_FLAG2	= 1<<1;
const long ENF_FLAG3	= 1<<2;
const long ENF_FLAG4	= 1<<3;
const long ENF_FLAG5	= 1<<4;
const long ENF_FLAG6	= 1<<5;
const long ENF_FLAG7	= 1<<6;
const long ENF_FLAG8	= 1<<7;

const long ENF_INVISIBLE = 1<<8;
const long ENF_PASSABLE	= 1<<9;
const long ENF_TRANSPARENT	= 1<<10;	// translucent
const long ENF_ORIENTED	= 1<<12;	// for sprites only
const long ENF_UVSPEED	= 1<<11;	// UV parameters are used as a speed
const long ENF_OVERLAY	= 1<<12;	// for models only
const long ENF_FACING	= 1<<13;	// for sprites only
const long ENF_NEAR	= 1<<14;
const long ENF_FLARE	= 1<<15;
const long ENF_NOFILTER = 1<<16;
const long ENF_UNLIT	= 1<<17;	// no light from environment
const long ENF_SHADOW	= 1<<18;	// dynamic shadows
const long ENF_LIGHT	= 1<<19;	// lit by own light
const long ENF_NOFOG	= 1<<20;	// shines through fog (fog 25%)
const long ENF_BRIGHT	= 1<<21;	// additive blending

// Entity event enable flags
const long EVF_SURFACE	= 1<<1;
const long EVF_ENTITY	= 1<<2;
const long EVF_STUCK	= 1<<3;
const long EVF_PUSHED	= 1<<4;
const long EVF_IMPACT	= 1<<5;

const long EVF_DETECT	= 1<<7;
const long EVF_SCAN	= 1<<8;
const long EVF_SHOOT	= 1<<9;
const long EVF_TRIGGER	= 1<<10;
const long EVF_SONAR	= 1<<11;

const long EVF_TOUCH	= 1<<16;
const long EVF_RELEASE	= 1<<17;
const long EVF_CLICK	= 1<<18;
const long EVF_RIGHTCLICK	= 1<<19;

const long EVF_DISCONNECT	= 1<<24;	// Entity client has disconnected
const long EVF_RECONNECT 	= 1<<25;	// Entity client has reconnected

// particle effect flags when different from entity flags
const long EPF_STREAK	= 1<<8;
const long EPF_BEAM	= 1<<9;
const long EPF_MOVE	= 1<<11;

// bitmap flags
const long BMPF_ARGB	= 1<<8;	// indicates 4444 content of pixels16, otherwise 565
const long BMPF_SAVE	= 1<<9;	// bitmap was changed, save it at SAVE/LOAD
const long BMPF_MIPMAPS = 1<<10;	// Bmap has 4 mipmaps following the pixels
const long BMPF_8BIT 	= 1<<11;	// Bmap contains 8-bit pixels. Otherwise, it's 16 bit.

// text, view & panel flags
const long TPVF_OVLY		= 1<<3;	// overlay panel
const long TPVF_TRANSP		= 1<<4;	// 50% transparency
const long TPVF_D3D		= 1<<8;	// draw text/panel by D3D
const long TPVF_NARROW		= 1<<13; // draw text condensed
const long TPVF_VISIBLE	= 1<<14; // visible
const long TPVF_AUDIBLE	= 1<<15; // view has sound focus
const long TPVF_CENTER_X	= 1<<18; // center text horizontally
const long TPVF_CENTER_Y	= 1<<19; // center text vertically

///////////////////////////////////////////////////////////////////////
// internal A4 / A5 structs
// Note: some struct members are not available in engine versions older
// then 5.12 (see comments). If you use them, your DLL should check the
// engine version though the VERSION variable.

typedef struct WDL_LINK_S {
	long	index;	// internal use only
	WDL_LINK_S *next;	// internal use only
	char  *name;	// pointer to WDL name of the object (if any)
} WDL_LINK;			// WDL object header

typedef struct {
	WDL_LINK link;
	char	*modelname;	// internal use only
	fixedPoint	x,y,z;		// position of the entity
	fixedPoint	pan,tilt,roll;	// euler angles
	fixedPoint	scale_x,scale_y,scale_z;	// scale factors, 0..255
	long	flags;		// entity property flags, see ENF...
	fixedPoint	frame;		// frame number for sprites & models
	fixedPoint	nextframe;	// interpolation target frame for models
	fixedPoint	skin;		// skin texture number for models
	fixedPoint	ambient;	// -100..+100
	fixedPoint	albedo;		// 0..100, light reflectivity
	fixedPoint	alpha;		// 0..100, transparency, default 50
	fixedPoint	lightrange;	// dynamic light range in quants
	fixedPoint	blue,green,red;	// dynamic light color, 0..255
	long	emask;		// event enable flags, EVF...
	long	eflags;		// internal status flags
	fixedPoint	min[3],max[3];	// bounding box for entity->entity collisions, in quants
	fixedPoint	trigger_range;
	fixedPoint	push;
	fixedPoint	shadow_range;
	fixedPoint	floor_range;
	long 	client_id;	// client # that has created this entity
	fixedPoint	skill[48];	// entity skills
// only available in A5.51 or above
	long	smask;		// nosend_... flags
	fixedPoint	u,v;		// texture offset or speed
	long	tex_scale;	// internal use only
	char	*skinname;	// internal use only
} A4_ENTITY;

typedef struct {
	WDL_LINK link;
	char  *chars;		// pointer to null terminated string
	long	length;		// allocated length of string (NEVER exceed!)
	long	flags;		// 0 = don't save, 1 = save string at SAVE/LOAD
} A4_STRING;

typedef struct {
	void	*pd3dtex;	// pointer to the DirectDrawSurface7 (A5.25 and below)
						// pointer to the Direct3DTexture8 (A5.50 and above)
	long	finalwidth,finalheight;  // real allocated size (always a power of 2)
	byte	*pixels;	// pointer to the original pixels
	byte	*palette;	// pointer to the original palette when 8 bit
	long	bitspp;		// original bitspp - 8, 16, 24, or 32
	long	width,height;	// original size of the bitmap
	long	pitch;		// original size of a horizontal line in bytes
	long	flags;		// internal use
// only available in A5.51 or above
	long	miplevels;	// number of mipmaps (1..4). The mipmap pixels immediately follow the normal pixels.
} A4_TEX;

typedef struct {
	WDL_LINK link;
	long 	width,height; // size of the bitmap
	long	bytespp;	// internal bytes per pixel (1 or 2)
	byte	*pixels8;	// pointer to 8 bit image (color indexes)
	byte	*pixels16;	// NULL when palettized, otherwise ptr to
						// 565, 4444, 888 or 8888 coded image
	long	flags;  // see BMPF_.. flags
	A4_TEX	*tex;	// pointer to one or more textures the image is split into.
					// the last texture is followed by a NULL pointer.
} A4_BMAP;

typedef struct {
	WDL_LINK link;
	fixedPoint	lifespan;// time until removal in ticks; set to 0 for removing
	fixedPoint	x,y,z;	// position
	fixedPoint	vel_x,vel_y,vel_z;	// speed or beam vector
	fixedPoint	size;	// in quants
	A4_BMAP *bmap;	// bitmap, or NULL
	long	function;	// particle script function
	long	flags;	// see EPF_...
	fixedPoint	skill_x,skill_y,skill_z;
	fixedPoint	gravity;
	fixedPoint	unused;
	fixedPoint	alpha;		// opacity 0..100
	long	colorkey;	// internal use
	fixedPoint	blue,green,red;
	fixedPoint	skill[4];	// more general purpose skills
} A4_PARTICLE;

typedef struct {
	WDL_LINK link;
	A4_BMAP bmap;
	int   dx,dy;	// width/height of single character
	int   num;		// number of characters - 11, 128, or 256
	int   cpl;		// characters per bitmap line
} A4_FONT;

typedef struct {
	WDL_LINK link;
	long	type;	// internal use only
	fixedPoint	layer;	// layer number (read only)
	fixedPoint	pos_x,pos_y;	// screen position in pixels
	long	flags;	// see TPVF...
	fixedPoint	alpha;	// transparency factor
	fixedPoint	offset;	// vertical offset
	fixedPoint	lines;  // number of lines to display
	A4_FONT *font;	// font used for the text
	long	strings;	// number of strings (read only)
	A4_STRING **pstrings;  // pointer to array of string pointers
// only available in A5.14 or above
	fixedPoint	scale_x,scale_y;
} A4_TEXT;

typedef struct {
	WDL_LINK link;
	long	type;	// internal use only
	fixedPoint	layer;	// layer number (read only)
	fixedPoint	pos_x,pos_y;	// screen position in pixels
	long	flags;	// see TPVF...
	fixedPoint	alpha;	// transparency factor
	A4_BMAP *bmap;	// background bitmap
// only available in A5.14 or above
	fixedPoint	scale_x,scale_y;
} A4_PANEL;

typedef struct A4_VIEW_S {
	WDL_LINK link;
	long	type;	// internal use only
	fixedPoint	layer;	// layer number (read only)
	fixedPoint	pos_x,pos_y;	// screen position in pixels
	long	flags;	// see TPVF...
	fixedPoint	size_x,size_y;	// screen size in pixels
	fixedPoint	x,y,z;	// position of the camera
	fixedPoint	pan,tilt,roll;	// camera angles
	fixedPoint	offset_x,offset_y;	// eye offsets
	fixedPoint	arc;	// camera FOV, used for zooming
	fixedPoint	aspect;	// width to height ratio
	fixedPoint	ambient;// brightness
	fixedPoint	fog;	// fog strength (only used when fog_end == 0)
	fixedPoint	alpha;	// transparency
// only available in A5.12 or above
	fixedPoint	portal_x,portal_y,portal_z;	// portal plane vertex
	fixedPoint	pnormal_x,pnormal_y,pnormal_z;// portal plane normal
// only available in A5.14 or above
	fixedPoint	fog_start,fog_end;	// fog start/end range in D3D mode
	fixedPoint	unused;
	A4_ENTITY *genius;	// calculates valid BSP tree leaf for the view
	A4_VIEW_S *portal;	// portal view
// only available in A5.30 or above
	A4_BMAP	*bmap;		// render target
} A4_VIEW;

///////////////////////////////////////////////////////////////////////
// Access to exported A4/A5 variables and functions. The interface
// structs are initialized by the engine at game start. NEVER change or
// overwrite the structs afterwards! Some elements of the structs are
// only available in higher engine versions, so always test the engine
// version number before using them.

typedef struct {
	byte	*save_block;// pointer to block of variables for save/load (not used)
	int		save_size;	// size of block of variables for saving (not used)
	long	(*Exec)(long n,long p1,long p2,long p3);	// DLLLIB internal use only
// only available in A5.51 or above - first packet byte must be 17 (0x11) for user defined packets
	void	(*SendPacket)(long to,void *data,long size,long guaranteed);	// the send function of the engine
	void	(*ReceivePacket)(long from,void *data,long size);		// user provided function 
} ENGINE_INTERFACE;

typedef struct {	// not used - only available with A5.24 and below!
	byte	*bytes;	// pointer to frame buffer (only valid after Lock)
	int		pitch;	// size of a horizontal line in bytes (!= width!)
	int		width,height;	// width, height of the screen
	int		bpp;			// bytes per pixel of the frame buffer
	BOOL	(*Lock)(void);	// lock frame buffer before accessing it
	void	(*Unlock)(void); // unlock frame buffer after accessing it
	void	*pd3ddev;		// pointer to the IDirect3DDevice7 used by A5 prior to A5.30; NULL otherwise
	HWND	hwnd;	// handle of the engine window
	byte	*palette;	// level palette in byte[768] format
} FRAME_INTERFACE;	// not used by A5.30 and above

typedef struct {
	void	*pdi8;		// pointer to the DirectInput8 object used by A5.30 and above
	void	*pdi8mouse;	// pointer to the mouse DirectInputDevice8 used by A5.30 and above
	void	*pdikbd;	// not used - keyboard doesn't use DirectInput
	void	*pdijoy;	// not used  -joystick doesn't use DirectInput

	void	*pdplay8client;	// pointer to the DirectPlay4 interface used by A5.24 and below
						// pointer to the DirectPlay8Client interface used by A5.30 and above
	void	*pdplay8server;	// pointer to the DirectPlayLobby2A interface used by A5.24 and below
						// pointer to the DirectPlay8Server interface used by A5.30 and above
	DWORD	pdplayer;	// not used 
	void	*pdpguid;	// not used 

	void	*pds;		// pointer to the DirectSound interface
	void	*pdsb;		// pointer to the DirectSoundBuffer interface
// only available in A5.50 or above
	void	*pd3d8;		// pointer to the DirectDraw7 interface used by A5.24 and below; 
						// pointer to the Direct3D8 interface used by A5.50 or later; 
	void	*pd3ddev8;	// pointer to the IDirect3DDevice8 used by A5.50 and above
	HWND	hwnd;		// main app window
} DX_INTERFACE;

typedef struct {
	long (*GetObj)(char *name);	// DLLLIB internal use only
	long (*GetFunc)(char *name);	// DLLLIB internal use only
	A4_ENTITY **my,**you;	// pointer to MY, YOU entity pointers. *(a5wdl->my) accesses the MY entity.
// only available in A5.12 or above
	fixedPoint (*Action)(long,long,long,long,long);	// Call a user defined WDL action
} WDL_INTERFACE;


typedef struct {
	long dll_version;
// The version is automatically tested against A5DLL_VERSION
// on opening the DLL. DLLs work with engines with the same or a higher
// version number, but not with a lower version engine.
	WDL_INTERFACE	*wdl;	// access to WDL objects and functions
	FRAME_INTERFACE	*fb;	// only used by A5 prior to A5.30!
	DX_INTERFACE	*dx;	// access to directx pointers
// only available in A5.12 or above
	ENGINE_INTERFACE *eng;	// access to engine functions
} A5_INTERFACE;

// interface pointers provided by the library
extern A5_INTERFACE 	a5_interface,*a5;
extern FRAME_INTERFACE 	fb,*a5fb;
extern DX_INTERFACE 	dx,*a5dx;
extern WDL_INTERFACE	wdl,*a5wdl;
extern ENGINE_INTERFACE	 eng,*a5eng;

///////////////////////////////////////////////////////////////////////
// public library functions
// Note that all WDL functions must be called directly by DLL functions.
// They can not be called by timer threads or other independent threads.

// returns the address of any WDL object with the given name
long a5dll_getwdlobj(char *name);

// returns the address of a built-in WDL function
long a5dll_getwdlfunc(char *name);

// typecasts help passing function parameters
typedef fixedPoint (*wdlfunc1)(long);
typedef fixedPoint (*wdlfunc2)(long,long);
typedef fixedPoint (*wdlfunc3)(long,long,long);
typedef fixedPoint (*wdlfunc4)(long,long,long,long);

// the following functions are only available in A5.12 or above ///////

// returns the address of a user defined WDL script function or action
long a5dll_getscript(char *name);

// calls a user-defined WDL script function with given address
// parameters can be a fixedPoint point number, an array, or a pointer to a WDL object
fixedPoint a5dll_callscript(long script,long p1=0,long p2=0,long p3=0,long p4=0);

// calls a user-defined WDL script function with given name
// parameters can be a fixedPoint point number, an array, or a pointer to a WDL object
fixedPoint a5dll_callname(char *name,long p1=0,long p2=0,long p3=0,long p4=0);

// returns the texture of a sprite, model or terrain entity
// frame = frame or skin number
// texnum = subtexture number (if it is split into several subtextures)
A4_TEX *a5dll_tex4ent(A4_ENTITY *entity,int frame,int texnum=0);

// enumerates local entities
// if called with NULL, returns a pointer to the first entity in the level
// if called with a local entity pointer, returns a pointer to the next local entity
// if called with a pointer to the last entity, returns NULL
A4_ENTITY *a5dll_entnext(A4_ENTITY *entity);

// creates an Error #1527 message requester with given text
void a5dll_errormessage(char *text);

///////////////////////////////////////////////////////////////////////
#endif //A5DLL_H