/*
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

#ifndef PROGS_H
#define PROGS_H

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

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 48
typedef struct edict_s {
    qboolean free;

    link_t area;		// linked to a division node or leaf

    int num_leafs;
    int leafnums[MAX_ENT_LEAFS];

    entity_state_t baseline;
    unsigned char alpha;

    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;

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

extern dprograms_t *progs;
extern dfunction_t *pr_functions;
extern char *pr_strings;
extern int pr_strings_size;
extern dstatement_t *pr_statements;
extern globalvars_t *pr_global_struct;
extern float *pr_globals;	// same as pr_global_struct

extern int pr_edict_size;	// in bytes
extern qboolean pr_alpha_supported;

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

void PR_AddCommands();
void PR_RegisterVariables();
void PR_Init();

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

void PR_Profile_f();
void PR_StackTrace();
void PR_StackTrace_Developer();

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

// returns a copy of the string allocated from the server's string heap

void ED_Print(edict_t *ed);
void ED_Write(FILE *f, edict_t *ed);
const char *ED_ParseEdict(const char *data, edict_t *ent);

void ED_WriteGlobals(FILE *f);
void ED_ParseGlobals(const char *data);

void ED_LoadFromFile(const char *data);

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

#define CHECK_EDICT_PTR(e) \
    do { (void)((typeof(e))NULL == (const edict_t *)NULL); } while (0)

#define	NEXT_EDICT(e) ({			\
	CHECK_EDICT_PTR(e);			\
	(typeof(e))((const byte *)e + pr_edict_size); })

#define EDICT_TO_PROG(e) ({			\
	CHECK_EDICT_PTR(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]))

typedef void (*builtin_t) (void);
extern builtin_t *pr_builtins;
extern int pr_numbuiltins;
extern int pr_argc;

extern qboolean pr_trace;
extern dfunction_t *pr_xfunction;
extern int pr_xstatement;

#ifdef NQ_HACK
extern unsigned short pr_crc;
#endif
#if defined(QW_HACK) && defined(SERVERONLY)
extern func_t SpectatorConnect;
extern func_t SpectatorThink;
extern func_t SpectatorDisconnect;
#endif

void PR_RunError(const char *error, ...)
    __attribute__((noreturn, format(printf,1,2)));

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

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

/*
 * PR Strings stuff
 */
void PR_InitStringTable(void);
const char *PR_GetString(int num);
int PR_SetString(const char *s);

/*
 * Somehow, I don't think this should be exposed - but better to have it here
 * than have hidden exports between .c files.
 */
void PF_changeyaw(void);

#endif /* PROGS_H */
