/*
Copyright (C) 1996-1997 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

/*
QuakeTM
Transactional implementation of the original Quake game
Copyright (C) 2008 Barcelona Supercomputing Center
Author: Vladimir Gajinov
*/



#include "pr_comp.h"			// defs shared with qcc
#include "progdefs.h"			// generated by program cdefs

typedef union eval_s
{
	string_t		string;
	float			_float;
	float			vector[3];
	func_t			function;
	int				_int;
	int				edict;
} eval_t;	

#define	MAX_ENT_LEAFS	16
typedef struct edict_s
{
	qboolean	free;
        int             nada[16];
	link_t		area;				// linked to a division node or leaf
        int             nada2[16];
	
	int			num_leafs;
	short		leafnums[MAX_ENT_LEAFS];

	entity_state_t	baseline;
	
	float		freetime;			// sv.time when the object was freed
	entvars_t	v;					// C exported fields from progs
	// other fields from progs come immediately after
} edict_t;
#define	EDICT_FROM_AREA(l) STRUCT_FROM_LINK(l,edict_t,area)

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

extern	dprograms_t		*progs;
extern	dfunction_t		*pr_functions;
extern	char			*pr_strings;
extern	ddef_t			*pr_globaldefs;
extern	ddef_t			*pr_fielddefs;
extern	dstatement_t	*pr_statements;
//extern	globalvars_t	*pr_global_struct;
//extern	float			*pr_globals;			// same as pr_global_struct
extern int				pr_private_elements;
extern	int				pr_edict_size;	// in bytes

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

// THREADPRIVATE --------------------------------------------

#ifndef PR_GLOBAL_STRUCT
#define PR_GLOBAL_STRUCT
	extern	globalvars_t	*pr_global_struct;
	THREADPRIVATE(pr_global_struct)
#endif

// -----------------------------------------------------------

#ifndef PR_GLOBALS
#define PR_GLOBALS
	extern	float			*pr_globals;
	THREADPRIVATE(pr_globals)
#endif

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



void PR_Init (void);

TM_CALLABLE
void PR_ExecuteProgram (func_t fnum);
void PR_LoadProgs (void);

void PR_Profile_f (void);

TM_CALLABLE
edict_t *ED_Alloc (void);
TM_CALLABLE
void ED_Free (edict_t *ed);

char	*ED_NewString (char *string);
// returns a copy of the string allocated from the server's string heap

TM_PURE
void ED_Print (edict_t *ed);
char *ED_ParseEdict (char *data, edict_t *ent);

void ED_LoadFromFile (char *data);

TM_CALLABLE
edict_t *EDICT_NUM(int n);
TM_CALLABLE
int NUM_FOR_EDICT(edict_t *e);

#define	NEXT_EDICT(e) ((edict_t *)( (byte *)e + pr_edict_size))

#define	EDICT_TO_PROG(e) ((byte *)e - (byte *)sv.edicts)
#define PROG_TO_EDICT(e) ((edict_t *)((byte *)sv.edicts + e))

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

#define	G_FLOAT(o) (pr_globals[o])
#define	G_INT(o) (*(int *)&pr_globals[o])
#define	G_EDICT(o) ((edict_t *)((byte *)sv.edicts+ *(int *)&pr_globals[o]))
#define G_EDICTNUM(o) NUM_FOR_EDICT(G_EDICT(o))
#define	G_VECTOR(o) (&pr_globals[o])
#define	G_STRING(o) (PR_GetString(*(string_t *)&pr_globals[o]))
#define	G_FUNCTION(o) (*(func_t *)&pr_globals[o])

#define	E_FLOAT(e,o) (((float*)&e->v)[o])
#define	E_INT(e,o) (*(int *)&((float*)&e->v)[o])
#define	E_VECTOR(e,o) (&((float*)&e->v)[o])
#define	E_STRING(e,o) (PR_GetString(*(string_t *)&((float*)&e->v)[o]))

extern	int		type_size[8];

//TM_CALLABLE
typedef void (*builtin_t) (void);
//TM_CALLABLE
extern	builtin_t *pr_builtins;
extern int pr_numbuiltins;
// GAJA: Added from pr_cmds.c
TM_CALLABLE
void ClearTempBuffers(void);
TM_CALLABLE
void WriteTempBuffers(void);

//extern int		pr_argc;

extern	qboolean	pr_trace;
//extern	dfunction_t	*pr_xfunction[ThreadId];
//extern	int			pr_xstatement;


extern func_t SpectatorConnect;
extern func_t SpectatorThink;
extern func_t SpectatorDisconnect;

TM_PURE
void PR_RunError (char *error, ...);

TM_PURE
void ED_PrintEdicts (void);
TM_PURE
void ED_PrintNum (int ent);

TM_CALLABLE
eval_t *GetEdictFieldValue(edict_t *ed, char *field);

//
// PR STrings stuff
//
#define MAX_PRSTR 1024

extern char *pr_strtbl[MAX_PRSTR];
extern int num_prstr;

TM_CALLABLE
char *PR_GetString(int num);
TM_CALLABLE
int PR_SetString(char *s);

