1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 
13 See the GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 
19 */
20 
21 #ifndef __PROGS_H__
22 #define __PROGS_H__
23 
24 #include "pr_comp.h" // defs shared with qcc
25 #include "progdefs.h" // generated by program cdefs
26 
27 typedef union eval_s
28 {
29 	string_t	string;
30 	float		_float;
31 	float		vector[3];
32 	func_t		function;
33 	int		_int;
34 	int		edict;
35 } eval_t;
36 
37 struct edict_s; // forward referecnce for link_t
38 
39 typedef struct link_s
40 {
41 	struct edict_s *ed;
42 
43 	struct link_s	*prev, *next;
44 } link_t;
45 
46 #define	EDICT_FROM_AREA(l)	((l)->ed)
47 
48 #define	MAX_ENT_LEAFS	16
49 
50 typedef struct sv_edict_s
51 {
52 	qbool		free;
53 	link_t		area;			// linked to a division node or leaf
54 
55 	int         entnum;
56 
57 	int			num_leafs;
58 	short		leafnums[MAX_ENT_LEAFS];
59 
60 	entity_state_t	baseline;
61 
62 	float		freetime;		// sv.time when the object was freed
63 	double		lastruntime;	// sv.time when SV_RunEntity was last called for this edict (Tonik)
64 } sv_edict_t;
65 
66 typedef struct edict_s
67 {
68 	sv_edict_t	*e;			// server side part of the edict_t,
69 							// basically we can get rid of this pointer at all, since we can access it via sv.sv_edicts[num]
70 							// but this way it more friendly, I think.
71 
72 	entvars_t	v;			// C exported fields from progs
73 	// other fields from progs come immediately after
74 } edict_t;
75 
76 //============================================================================
77 
78 extern	dprograms_t	*progs;
79 extern	dfunction_t	*pr_functions;
80 extern	char		*pr_strings;
81 extern	ddef_t		*pr_globaldefs;
82 extern	ddef_t		*pr_fielddefs;
83 extern	dstatement_t	*pr_statements;
84 extern	globalvars_t	*pr_global_struct;
85 extern	float		*pr_globals;	// same as pr_global_struct
86 
87 extern	int         pr_edict_size;	// in bytes
88 extern	cvar_t      sv_progsname;
89 #ifdef WITH_NQPROGS
90 extern	cvar_t      sv_forcenqprogs;
91 #endif
92 
93 //============================================================================
94 
95 #ifdef WITH_NQPROGS
96 
97 extern	qbool			pr_nqprogs;
98 
99 extern	int pr_fieldoffsetpatch[106];
100 extern	int pr_globaloffsetpatch[62];
101 
102 #define PR_FIELDOFS(i) ((unsigned int)(i) > 105 ? (i) : pr_fieldoffsetpatch[i])
103 #define PR_GLOBAL(field) (((globalvars_t *)((byte *)pr_global_struct + \
104 	pr_globaloffsetpatch[((int *)&((globalvars_t *)0)->field - (int *)0) - 28]))->field)
105 
106 void NQP_Reset (void);
107 
108 #else	// !WITH_NQPROGS
109 
110 #define pr_nqprogs 0
111 #define PR_FIELDOFS(i) (i)
112 #define PR_GLOBAL(field) pr_global_struct->field
113 #define NQP_Reset()
114 
115 #endif
116 
117 //============================================================================
118 
119 void PR_Init (void);
120 
121 void PR_ExecuteProgram (func_t fnum);
122 void PR_InitPatchTables (void);	// NQ progs support
123 
124 void PR_Profile_f (void);
125 
126 void ED_ClearEdict (edict_t *e);
127 edict_t *ED_Alloc (void);
128 void ED_Free (edict_t *ed);
129 
130 char *ED_NewString (char *string);
131 // returns a copy of the string allocated from the server's string heap
132 
133 void ED_Print (edict_t *ed);
134 void ED_Write (FILE *f, edict_t *ed);
135 const char *ED_ParseEdict (const char *data, edict_t *ent);
136 
137 void ED_WriteGlobals (FILE *f);
138 void ED_ParseGlobals (const char *data);
139 
140 void ED_LoadFromFile (const char *data);
141 
142 edict_t *EDICT_NUM(int n);
143 int NUM_FOR_EDICT(edict_t *e);
144 
145 #define	NEXT_EDICT(e) ((edict_t *)( (byte *)e + pr_edict_size))
146 
147 #define	EDICT_TO_PROG(e) ((byte *)e - (byte *)sv.edicts)
148 #define PROG_TO_EDICT(e) ((edict_t *)((byte *)sv.edicts + e))
149 
150 //============================================================================
151 
152 #define	G_FLOAT(o) (pr_globals[o])
153 #define	G_INT(o) (*(int *)&pr_globals[o])
154 #define	G_EDICT(o) ((edict_t *)((byte *)sv.edicts+ *(int *)&pr_globals[o]))
155 #define G_EDICTNUM(o) NUM_FOR_EDICT(G_EDICT(o))
156 #define	G_VECTOR(o) (&pr_globals[o])
157 #define	G_STRING(o) (PR1_GetString(*(string_t *)&pr_globals[o]))
158 #define	G_FUNCTION(o) (*(func_t *)&pr_globals[o])
159 
160 #define	E_FLOAT(e,o) (((float*)&e->v)[o])
161 #define	E_INT(e,o) (*(int *)&((float*)&e->v)[o])
162 #define	E_VECTOR(e,o) (&((float*)&e->v)[o])
163 #define	E_STRING(e,o) (PR1_GetString(*(string_t *)&((float*)&e->v)[PR_FIELDOFS(o)]))
164 
165 typedef void		(*builtin_t) (void);
166 extern	builtin_t	*pr_builtins;
167 extern	int		pr_numbuiltins;
168 
169 extern	int		pr_argc;
170 
171 extern	qbool	pr_trace;
172 extern	dfunction_t	*pr_xfunction;
173 extern	int		pr_xstatement;
174 
175 extern func_t mod_ConsoleCmd, mod_UserCmd;
176 extern func_t mod_UserInfo_Changed, mod_localinfoChanged;
177 extern func_t mod_ChatMessage;
178 extern func_t mod_SpectatorConnect, mod_SpectatorDisconnect, mod_SpectatorThink;
179 extern func_t GE_ClientCommand, GE_PausedTic, GE_ShouldPause;
180 
181 extern int fofs_items2; // ZQ_ITEMS2 extension
182 extern int fofs_vw_index;	// ZQ_VWEP
183 extern int fofs_movement;
184 extern int fofs_gravity, fofs_maxspeed;
185 extern int fofs_hideentity;
186 extern int fofs_trackent;
187 extern int fofs_visibility;
188 extern int fofs_hide_players;
189 extern int fofs_teleported;
190 
191 #define EdictFieldFloat(ed, fieldoffset) ((eval_t *)((byte *)&(ed)->v + (fieldoffset)))->_float
192 #define EdictFieldVector(ed, fieldoffset) ((eval_t *)((byte *)&(ed)->v + (fieldoffset)))->vector
193 
194 void PR_RunError (char *error, ...);
195 
196 void ED_PrintEdicts (void);
197 void ED_PrintNum (int ent);
198 
199 eval_t *PR1_GetEdictFieldValue(edict_t *ed, char *field);
200 
201 int ED1_FindFieldOffset (char *field);
202 
203 //
204 // PR Strings stuff
205 //
206 #define MAX_PRSTR 1024
207 
208 extern char *pr_strtbl[MAX_PRSTR];
209 extern char *pr_newstrtbl[MAX_PRSTR];
210 extern int num_prstr;
211 
212 char *PR1_GetString(int num);
213 void PR1_SetString(string_t* address, char* s);
214 void PR_SetTmpString(string_t* address, const char *s);
215 
216 void PR1_LoadProgs (void);
217 void PR1_InitProg(void);
218 void PR1_Init(void);
219 
220 #define PR1_GameShutDown()	// PR1 does not really have it.
221 void PR1_UnLoadProgs(void);
222 
223 void PR1_GameClientDisconnect(int spec);
224 void PR1_GameClientConnect(int spec);
225 void PR1_GamePutClientInServer(int spec);
226 void PR1_GameClientPreThink(int spec);
227 void PR1_GameClientPostThink(int spec);
228 qbool PR1_ClientSay(int isTeamSay, char *message);
229 void PR1_PausedTic(float duration);
230 qbool PR1_ClientCmd(void);
231 
232 #define PR1_GameSetChangeParms() PR_ExecuteProgram(PR_GLOBAL(SetChangeParms))
233 #define PR1_GameSetNewParms() PR_ExecuteProgram(PR_GLOBAL(SetNewParms))
234 #define PR1_GameStartFrame() PR_ExecuteProgram (PR_GLOBAL(StartFrame))
235 #define PR1_ClientKill() PR_ExecuteProgram (PR_GLOBAL(ClientKill))
236 #define PR1_UserInfoChanged() (0) // PR1 does not really have it,
237                                   // we have mod_UserInfo_Changed but it is slightly different.
238 #define PR1_LoadEnts ED_LoadFromFile
239 #define PR1_EdictThink PR_ExecuteProgram
240 #define PR1_EdictTouch PR_ExecuteProgram
241 #define PR1_EdictBlocked PR_ExecuteProgram
242 
243 #ifndef USE_PR2
244 	#define PR_LoadProgs PR1_LoadProgs
245 	#define PR_InitProg PR1_InitProg
246 	#define PR_GameShutDown PR1_GameShutDown
247 	#define PR_UnLoadProgs PR1_UnLoadProgs
248 
249 	#define PR_Init PR1_Init
250 	//#define PR_GetString PR1_GetString
251 	//#define PR_SetString PR1_SetString
252 	#define PR_GetEntityString PR1_GetString
253 	#define PR_SetEntityString(ent, target, value) PR1_SetString(&target, value)
254 	#define PR_SetGlobalString(target, value) PR1_SetString(&target, value)
255 	#define ED_FindFieldOffset ED1_FindFieldOffset
256 	#define PR_GetEdictFieldValue PR1_GetEdictFieldValue
257 
258 	#define PR_GameClientDisconnect PR1_GameClientDisconnect
259 	#define PR_GameClientConnect PR1_GameClientConnect
260 	#define PR_GamePutClientInServer PR1_GamePutClientInServer
261 	#define PR_GameClientPreThink PR1_GameClientPreThink
262 	#define PR_GameClientPostThink PR1_GameClientPostThink
263 	#define PR_ClientSay PR1_ClientSay
264 	#define PR_PausedTic PR1_PausedTic
265 	#define PR_ClientCmd PR1_ClientCmd
266 
267 	#define PR_GameSetChangeParms PR1_GameSetChangeParms
268 	#define PR_GameSetNewParms PR1_GameSetNewParms
269 	#define PR_GameStartFrame(isBotFrame) { if (!isBotFrame) { PR1_GameStartFrame(); } }
270 	#define PR_ClientKill PR1_ClientKill
271 	#define PR_UserInfoChanged PR1_UserInfoChanged
272 	#define PR_LoadEnts PR1_LoadEnts
273 	#define PR_EdictThink PR1_EdictThink
274 	#define PR_EdictTouch PR1_EdictTouch
275 	#define PR_EdictBlocked PR1_EdictBlocked
276 
277 	#define PR_ClearEdict(ent)
278 #endif
279 
280 // pr_cmds.c
281 void PR_InitBuiltins (void);
282 
283 #endif /* !__PROGS_H__ */
284