created new MSVC2008 project files and solution from scratch
[xonotic/darkplaces.git] / prvm_edict.c
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 // AK new vm
21
22 #include "quakedef.h"
23 #include "progsvm.h"
24
25 prvm_prog_t *prog;
26
27 static prvm_prog_t prog_list[PRVM_MAXPROGS];
28
29 int             prvm_type_size[8] = {1,sizeof(string_t)/4,1,3,1,1,sizeof(func_t)/4,sizeof(void *)/4};
30
31 ddef_t *PRVM_ED_FieldAtOfs(int ofs);
32 qboolean PRVM_ED_ParseEpair(prvm_edict_t *ent, ddef_t *key, const char *s, qboolean parsebackslash);
33
34 cvar_t prvm_language = {0, "prvm_language", "", "when set, loads progs.dat.LANGUAGENAME.po for string translations; when set to dump, progs.dat.dump.po is written from the strings in the progs"};
35 // LordHavoc: prints every opcode as it executes - warning: this is significant spew
36 cvar_t prvm_traceqc = {0, "prvm_traceqc", "0", "prints every QuakeC statement as it is executed (only for really thorough debugging!)"};
37 // LordHavoc: counts usage of each QuakeC statement
38 cvar_t prvm_statementprofiling = {0, "prvm_statementprofiling", "0", "counts how many times each QuakeC statement has been executed, these counts are displayed in prvm_printfunction output (if enabled)"};
39 cvar_t prvm_backtraceforwarnings = {0, "prvm_backtraceforwarnings", "0", "print a backtrace for warnings too"};
40 cvar_t prvm_leaktest = {0, "prvm_leaktest", "0", "try to detect memory leaks in strings or entities"};
41 cvar_t prvm_leaktest_ignore_classnames = {0, "prvm_leaktest_ignore_classnames", "", "classnames of entities to NOT leak check because they are found by find(world, classname, ...) but are actually spawned by QC code (NOT map entities)"};
42 cvar_t prvm_errordump = {0, "prvm_errordump", "0", "write a savegame on crash to crash-server.dmp"};
43 cvar_t prvm_reuseedicts_startuptime = {0, "prvm_reuseedicts_startuptime", "2", "allows immediate re-use of freed entity slots during start of new level (value in seconds)"};
44 cvar_t prvm_reuseedicts_neverinsameframe = {0, "prvm_reuseedicts_neverinsameframe", "1", "never allows re-use of freed entity slots during same frame"};
45
46 static double prvm_reuseedicts_always_allow = 0;
47 qboolean prvm_runawaycheck = true;
48
49 extern sizebuf_t vm_tempstringsbuf;
50
51 //============================================================================
52 // mempool handling
53
54 /*
55 ===============
56 PRVM_MEM_Alloc
57 ===============
58 */
59 void PRVM_MEM_Alloc(void)
60 {
61         int i;
62
63         // reserve space for the null entity aka world
64         // check bound of max_edicts
65         prog->max_edicts = bound(1 + prog->reserved_edicts, prog->max_edicts, prog->limit_edicts);
66         prog->num_edicts = bound(1 + prog->reserved_edicts, prog->num_edicts, prog->max_edicts);
67
68         // edictprivate_size has to be min as big prvm_edict_private_t
69         prog->edictprivate_size = max(prog->edictprivate_size,(int)sizeof(prvm_edict_private_t));
70
71         // alloc edicts
72         prog->edicts = (prvm_edict_t *)Mem_Alloc(prog->progs_mempool,prog->limit_edicts * sizeof(prvm_edict_t));
73
74         // alloc edict private space
75         prog->edictprivate = Mem_Alloc(prog->progs_mempool, prog->max_edicts * prog->edictprivate_size);
76
77         // alloc edict fields
78         prog->entityfieldsarea = prog->entityfields * prog->max_edicts;
79         prog->edictsfields = (vec_t *)Mem_Alloc(prog->progs_mempool, prog->entityfieldsarea * sizeof(vec_t));
80
81         // set edict pointers
82         for(i = 0; i < prog->max_edicts; i++)
83         {
84                 prog->edicts[i].priv.required = (prvm_edict_private_t *)((unsigned char  *)prog->edictprivate + i * prog->edictprivate_size);
85                 prog->edicts[i].fields.vp = prog->edictsfields + i * prog->entityfields;
86         }
87 }
88
89 /*
90 ===============
91 PRVM_MEM_IncreaseEdicts
92 ===============
93 */
94 void PRVM_MEM_IncreaseEdicts(void)
95 {
96         int             i;
97
98         if(prog->max_edicts >= prog->limit_edicts)
99                 return;
100
101         PRVM_GCALL(begin_increase_edicts)();
102
103         // increase edicts
104         prog->max_edicts = min(prog->max_edicts + 256, prog->limit_edicts);
105
106         prog->entityfieldsarea = prog->entityfields * prog->max_edicts;
107         prog->edictsfields = (vec_t*)Mem_Realloc(prog->progs_mempool, (void *)prog->edictsfields, prog->entityfieldsarea * sizeof(vec_t));
108         prog->edictprivate = (void *)Mem_Realloc(prog->progs_mempool, (void *)prog->edictprivate, prog->max_edicts * prog->edictprivate_size);
109
110         //set e and v pointers
111         for(i = 0; i < prog->max_edicts; i++)
112         {
113                 prog->edicts[i].priv.required  = (prvm_edict_private_t *)((unsigned char  *)prog->edictprivate + i * prog->edictprivate_size);
114                 prog->edicts[i].fields.vp = prog->edictsfields + i * prog->entityfields;
115         }
116
117         PRVM_GCALL(end_increase_edicts)();
118 }
119
120 //============================================================================
121 // normal prvm
122
123 int PRVM_ED_FindFieldOffset(const char *field)
124 {
125         ddef_t *d;
126         d = PRVM_ED_FindField(field);
127         if (!d)
128                 return -1;
129         return d->ofs;
130 }
131
132 int PRVM_ED_FindGlobalOffset(const char *global)
133 {
134         ddef_t *d;
135         d = PRVM_ED_FindGlobal(global);
136         if (!d)
137                 return -1;
138         return d->ofs;
139 }
140
141 func_t PRVM_ED_FindFunctionOffset(const char *function)
142 {
143         mfunction_t *f;
144         f = PRVM_ED_FindFunction(function);
145         if (!f)
146                 return 0;
147         return (func_t)(f - prog->functions);
148 }
149
150 qboolean PRVM_ProgLoaded(int prognr)
151 {
152         if(prognr < 0 || prognr >= PRVM_MAXPROGS)
153                 return FALSE;
154
155         return (prog_list[prognr].loaded ? TRUE : FALSE);
156 }
157
158 /*
159 =================
160 PRVM_SetProgFromString
161 =================
162 */
163 // perhaps add a return value when the str doesnt exist
164 qboolean PRVM_SetProgFromString(const char *str)
165 {
166         int i = 0;
167         for(; i < PRVM_MAXPROGS ; i++)
168                 if(prog_list[i].name && !strcmp(prog_list[i].name,str))
169                 {
170                         if(prog_list[i].loaded)
171                         {
172                                 prog = &prog_list[i];
173                                 return TRUE;
174                         }
175                         else
176                         {
177                                 Con_Printf("%s not loaded !\n",PRVM_NAME);
178                                 return FALSE;
179                         }
180                 }
181
182         Con_Printf("Invalid program name %s !\n", str);
183         return FALSE;
184 }
185
186 /*
187 =================
188 PRVM_SetProg
189 =================
190 */
191 void PRVM_SetProg(int prognr)
192 {
193         if(0 <= prognr && prognr < PRVM_MAXPROGS)
194         {
195                 if(prog_list[prognr].loaded)
196                         prog = &prog_list[prognr];
197                 else
198                         PRVM_ERROR("%i not loaded !", prognr);
199                 return;
200         }
201         PRVM_ERROR("Invalid program number %i", prognr);
202 }
203
204 /*
205 =================
206 PRVM_ED_ClearEdict
207
208 Sets everything to NULL
209 =================
210 */
211 void PRVM_ED_ClearEdict (prvm_edict_t *e)
212 {
213         memset (e->fields.vp, 0, prog->progs->entityfields * 4);
214         e->priv.required->free = false;
215
216         // AK: Let the init_edict function determine if something needs to be initialized
217         PRVM_GCALL(init_edict)(e);
218 }
219
220 const char *PRVM_AllocationOrigin(void)
221 {
222         char *buf = NULL;
223         if(prog->leaktest_active)
224         if(prog->depth > 0) // actually in QC code and not just parsing the entities block of a map/savegame
225         {
226                 buf = (char *)PRVM_Alloc(128);
227                 PRVM_ShortStackTrace(buf, 128);
228         }
229         return buf;
230 }
231
232 /*
233 =================
234 PRVM_ED_CanAlloc
235
236 Returns if this particular edict could get allocated by PRVM_ED_Alloc
237 =================
238 */
239 qboolean PRVM_ED_CanAlloc(prvm_edict_t *e)
240 {
241         if(!e->priv.required->free)
242                 return false;
243         if(prvm_reuseedicts_always_allow == realtime)
244                 return true;
245         if(realtime <= e->priv.required->freetime && prvm_reuseedicts_neverinsameframe.integer)
246                 return false; // never allow reuse in same frame (causes networking trouble)
247         if(e->priv.required->freetime < prog->starttime + prvm_reuseedicts_startuptime.value)
248                 return true;
249         if(realtime > e->priv.required->freetime + 1)
250                 return true;
251         return false; // entity slot still blocked because the entity was freed less than one second ago
252 }
253
254 /*
255 =================
256 PRVM_ED_Alloc
257
258 Either finds a free edict, or allocates a new one.
259 Try to avoid reusing an entity that was recently freed, because it
260 can cause the client to think the entity morphed into something else
261 instead of being removed and recreated, which can cause interpolated
262 angles and bad trails.
263 =================
264 */
265 prvm_edict_t *PRVM_ED_Alloc (void)
266 {
267         int                     i;
268         prvm_edict_t            *e;
269
270         // the client qc dont need maxclients
271         // thus it doesnt need to use svs.maxclients
272         // AK:  changed i=svs.maxclients+1
273         // AK:  changed so the edict 0 wont spawn -> used as reserved/world entity
274         //              although the menu/client has no world
275         for (i = prog->reserved_edicts + 1;i < prog->num_edicts;i++)
276         {
277                 e = PRVM_EDICT_NUM(i);
278                 if(PRVM_ED_CanAlloc(e))
279                 {
280                         PRVM_ED_ClearEdict (e);
281                         e->priv.required->allocation_origin = PRVM_AllocationOrigin();
282                         return e;
283                 }
284         }
285
286         if (i == prog->limit_edicts)
287                 PRVM_ERROR ("%s: PRVM_ED_Alloc: no free edicts",PRVM_NAME);
288
289         prog->num_edicts++;
290         if (prog->num_edicts >= prog->max_edicts)
291                 PRVM_MEM_IncreaseEdicts();
292
293         e = PRVM_EDICT_NUM(i);
294         PRVM_ED_ClearEdict (e);
295
296         e->priv.required->allocation_origin = PRVM_AllocationOrigin();
297
298         return e;
299 }
300
301 /*
302 =================
303 PRVM_ED_Free
304
305 Marks the edict as free
306 FIXME: walk all entities and NULL out references to this entity
307 =================
308 */
309 void PRVM_ED_Free (prvm_edict_t *ed)
310 {
311         // dont delete the null entity (world) or reserved edicts
312         if(PRVM_NUM_FOR_EDICT(ed) <= prog->reserved_edicts )
313                 return;
314
315         PRVM_GCALL(free_edict)(ed);
316
317         ed->priv.required->free = true;
318         ed->priv.required->freetime = realtime;
319         if(ed->priv.required->allocation_origin)
320         {
321                 PRVM_Free((char *)ed->priv.required->allocation_origin);
322                 ed->priv.required->allocation_origin = NULL;
323         }
324 }
325
326 //===========================================================================
327
328 /*
329 ============
330 PRVM_ED_GlobalAtOfs
331 ============
332 */
333 ddef_t *PRVM_ED_GlobalAtOfs (int ofs)
334 {
335         ddef_t          *def;
336         int                     i;
337
338         for (i=0 ; i<prog->progs->numglobaldefs ; i++)
339         {
340                 def = &prog->globaldefs[i];
341                 if (def->ofs == ofs)
342                         return def;
343         }
344         return NULL;
345 }
346
347 /*
348 ============
349 PRVM_ED_FieldAtOfs
350 ============
351 */
352 ddef_t *PRVM_ED_FieldAtOfs (int ofs)
353 {
354         ddef_t          *def;
355         int                     i;
356
357         for (i=0 ; i<prog->progs->numfielddefs ; i++)
358         {
359                 def = &prog->fielddefs[i];
360                 if (def->ofs == ofs)
361                         return def;
362         }
363         return NULL;
364 }
365
366 /*
367 ============
368 PRVM_ED_FindField
369 ============
370 */
371 ddef_t *PRVM_ED_FindField (const char *name)
372 {
373         ddef_t *def;
374         int i;
375
376         for (i=0 ; i<prog->progs->numfielddefs ; i++)
377         {
378                 def = &prog->fielddefs[i];
379                 if (!strcmp(PRVM_GetString(def->s_name), name))
380                         return def;
381         }
382         return NULL;
383 }
384
385 /*
386 ============
387 PRVM_ED_FindGlobal
388 ============
389 */
390 ddef_t *PRVM_ED_FindGlobal (const char *name)
391 {
392         ddef_t *def;
393         int i;
394
395         for (i=0 ; i<prog->progs->numglobaldefs ; i++)
396         {
397                 def = &prog->globaldefs[i];
398                 if (!strcmp(PRVM_GetString(def->s_name), name))
399                         return def;
400         }
401         return NULL;
402 }
403
404
405 /*
406 ============
407 PRVM_ED_FindFunction
408 ============
409 */
410 mfunction_t *PRVM_ED_FindFunction (const char *name)
411 {
412         mfunction_t             *func;
413         int                             i;
414
415         for (i=0 ; i<prog->progs->numfunctions ; i++)
416         {
417                 func = &prog->functions[i];
418                 if (!strcmp(PRVM_GetString(func->s_name), name))
419                         return func;
420         }
421         return NULL;
422 }
423
424
425 /*
426 ============
427 PRVM_ValueString
428
429 Returns a string describing *data in a type specific manner
430 =============
431 */
432 char *PRVM_ValueString (etype_t type, prvm_eval_t *val)
433 {
434         static char line[MAX_INPUTLINE];
435         ddef_t *def;
436         mfunction_t *f;
437         int n;
438
439         type = (etype_t)((int) type & ~DEF_SAVEGLOBAL);
440
441         switch (type)
442         {
443         case ev_string:
444                 strlcpy (line, PRVM_GetString (val->string), sizeof (line));
445                 break;
446         case ev_entity:
447                 n = val->edict;
448                 if (n < 0 || n >= prog->max_edicts)
449                         dpsnprintf (line, sizeof(line), "entity %i (invalid!)", n);
450                 else
451                         dpsnprintf (line, sizeof(line), "entity %i", n);
452                 break;
453         case ev_function:
454                 f = prog->functions + val->function;
455                 dpsnprintf (line, sizeof(line), "%s()", PRVM_GetString(f->s_name));
456                 break;
457         case ev_field:
458                 def = PRVM_ED_FieldAtOfs ( val->_int );
459                 dpsnprintf (line, sizeof(line), ".%s", PRVM_GetString(def->s_name));
460                 break;
461         case ev_void:
462                 dpsnprintf (line, sizeof(line), "void");
463                 break;
464         case ev_float:
465                 // LordHavoc: changed from %5.1f to %10.4f
466                 dpsnprintf (line, sizeof(line), "%10.4f", val->_float);
467                 break;
468         case ev_vector:
469                 // LordHavoc: changed from %5.1f to %10.4f
470                 dpsnprintf (line, sizeof(line), "'%10.4f %10.4f %10.4f'", val->vector[0], val->vector[1], val->vector[2]);
471                 break;
472         case ev_pointer:
473                 dpsnprintf (line, sizeof(line), "pointer");
474                 break;
475         default:
476                 dpsnprintf (line, sizeof(line), "bad type %i", (int) type);
477                 break;
478         }
479
480         return line;
481 }
482
483 /*
484 ============
485 PRVM_UglyValueString
486
487 Returns a string describing *data in a type specific manner
488 Easier to parse than PR_ValueString
489 =============
490 */
491 char *PRVM_UglyValueString (etype_t type, prvm_eval_t *val)
492 {
493         static char line[MAX_INPUTLINE];
494         int i;
495         const char *s;
496         ddef_t *def;
497         mfunction_t *f;
498
499         type = (etype_t)((int)type & ~DEF_SAVEGLOBAL);
500
501         switch (type)
502         {
503         case ev_string:
504                 // Parse the string a bit to turn special characters
505                 // (like newline, specifically) into escape codes,
506                 // this fixes saving games from various mods
507                 s = PRVM_GetString (val->string);
508                 for (i = 0;i < (int)sizeof(line) - 2 && *s;)
509                 {
510                         if (*s == '\n')
511                         {
512                                 line[i++] = '\\';
513                                 line[i++] = 'n';
514                         }
515                         else if (*s == '\r')
516                         {
517                                 line[i++] = '\\';
518                                 line[i++] = 'r';
519                         }
520                         else if (*s == '\\')
521                         {
522                                 line[i++] = '\\';
523                                 line[i++] = '\\';
524                         }
525                         else if (*s == '"')
526                         {
527                                 line[i++] = '\\';
528                                 line[i++] = '"';
529                         }
530                         else
531                                 line[i++] = *s;
532                         s++;
533                 }
534                 line[i] = '\0';
535                 break;
536         case ev_entity:
537                 dpsnprintf (line, sizeof (line), "%i", PRVM_NUM_FOR_EDICT(PRVM_PROG_TO_EDICT(val->edict)));
538                 break;
539         case ev_function:
540                 f = prog->functions + val->function;
541                 strlcpy (line, PRVM_GetString (f->s_name), sizeof (line));
542                 break;
543         case ev_field:
544                 def = PRVM_ED_FieldAtOfs ( val->_int );
545                 dpsnprintf (line, sizeof (line), ".%s", PRVM_GetString(def->s_name));
546                 break;
547         case ev_void:
548                 dpsnprintf (line, sizeof (line), "void");
549                 break;
550         case ev_float:
551                 dpsnprintf (line, sizeof (line), "%.9g", val->_float);
552                 break;
553         case ev_vector:
554                 dpsnprintf (line, sizeof (line), "%.9g %.9g %.9g", val->vector[0], val->vector[1], val->vector[2]);
555                 break;
556         default:
557                 dpsnprintf (line, sizeof (line), "bad type %i", type);
558                 break;
559         }
560
561         return line;
562 }
563
564 /*
565 ============
566 PRVM_GlobalString
567
568 Returns a string with a description and the contents of a global,
569 padded to 20 field width
570 ============
571 */
572 char *PRVM_GlobalString (int ofs)
573 {
574         char    *s;
575         //size_t        i;
576         ddef_t  *def;
577         void    *val;
578         static char     line[128];
579
580         val = (void *)&prog->globals.generic[ofs];
581         def = PRVM_ED_GlobalAtOfs(ofs);
582         if (!def)
583                 dpsnprintf (line, sizeof(line), "GLOBAL%i", ofs);
584         else
585         {
586                 s = PRVM_ValueString ((etype_t)def->type, (prvm_eval_t *)val);
587                 dpsnprintf (line, sizeof(line), "%s (=%s)", PRVM_GetString(def->s_name), s);
588         }
589
590         //i = strlen(line);
591         //for ( ; i<20 ; i++)
592         //      strcat (line," ");
593         //strcat (line," ");
594
595         return line;
596 }
597
598 char *PRVM_GlobalStringNoContents (int ofs)
599 {
600         //size_t        i;
601         ddef_t  *def;
602         static char     line[128];
603
604         def = PRVM_ED_GlobalAtOfs(ofs);
605         if (!def)
606                 dpsnprintf (line, sizeof(line), "GLOBAL%i", ofs);
607         else
608                 dpsnprintf (line, sizeof(line), "%s", PRVM_GetString(def->s_name));
609
610         //i = strlen(line);
611         //for ( ; i<20 ; i++)
612         //      strcat (line," ");
613         //strcat (line," ");
614
615         return line;
616 }
617
618
619 /*
620 =============
621 PRVM_ED_Print
622
623 For debugging
624 =============
625 */
626 // LordHavoc: optimized this to print out much more quickly (tempstring)
627 // LordHavoc: changed to print out every 4096 characters (incase there are a lot of fields to print)
628 void PRVM_ED_Print(prvm_edict_t *ed, const char *wildcard_fieldname)
629 {
630         size_t  l;
631         ddef_t  *d;
632         int             *v;
633         int             i, j;
634         const char      *name;
635         int             type;
636         char    tempstring[MAX_INPUTLINE], tempstring2[260]; // temporary string buffers
637
638         if (ed->priv.required->free)
639         {
640                 Con_Printf("%s: FREE\n",PRVM_NAME);
641                 return;
642         }
643
644         tempstring[0] = 0;
645         dpsnprintf(tempstring, sizeof(tempstring), "\n%s EDICT %i:\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ed));
646         for (i=1 ; i<prog->progs->numfielddefs ; i++)
647         {
648                 d = &prog->fielddefs[i];
649                 name = PRVM_GetString(d->s_name);
650                 if (name[strlen(name)-2] == '_')
651                         continue;       // skip _x, _y, _z vars
652
653                 // Check Field Name Wildcard
654                 if(wildcard_fieldname)
655                         if( !matchpattern(name, wildcard_fieldname, 1) )
656                                 // Didn't match; skip
657                                 continue;
658
659                 v = (int *)(ed->fields.vp + d->ofs);
660
661         // if the value is still all 0, skip the field
662                 type = d->type & ~DEF_SAVEGLOBAL;
663
664                 for (j=0 ; j<prvm_type_size[type] ; j++)
665                         if (v[j])
666                                 break;
667                 if (j == prvm_type_size[type])
668                         continue;
669
670                 if (strlen(name) > sizeof(tempstring2)-4)
671                 {
672                         memcpy (tempstring2, name, sizeof(tempstring2)-4);
673                         tempstring2[sizeof(tempstring2)-4] = tempstring2[sizeof(tempstring2)-3] = tempstring2[sizeof(tempstring2)-2] = '.';
674                         tempstring2[sizeof(tempstring2)-1] = 0;
675                         name = tempstring2;
676                 }
677                 strlcat(tempstring, name, sizeof(tempstring));
678                 for (l = strlen(name);l < 14;l++)
679                         strlcat(tempstring, " ", sizeof(tempstring));
680                 strlcat(tempstring, " ", sizeof(tempstring));
681
682                 name = PRVM_ValueString((etype_t)d->type, (prvm_eval_t *)v);
683                 if (strlen(name) > sizeof(tempstring2)-4)
684                 {
685                         memcpy (tempstring2, name, sizeof(tempstring2)-4);
686                         tempstring2[sizeof(tempstring2)-4] = tempstring2[sizeof(tempstring2)-3] = tempstring2[sizeof(tempstring2)-2] = '.';
687                         tempstring2[sizeof(tempstring2)-1] = 0;
688                         name = tempstring2;
689                 }
690                 strlcat(tempstring, name, sizeof(tempstring));
691                 strlcat(tempstring, "\n", sizeof(tempstring));
692                 if (strlen(tempstring) >= sizeof(tempstring)/2)
693                 {
694                         Con_Print(tempstring);
695                         tempstring[0] = 0;
696                 }
697         }
698         if (tempstring[0])
699                 Con_Print(tempstring);
700 }
701
702 /*
703 =============
704 PRVM_ED_Write
705
706 For savegames
707 =============
708 */
709 extern cvar_t developer_entityparsing;
710 void PRVM_ED_Write (qfile_t *f, prvm_edict_t *ed)
711 {
712         ddef_t  *d;
713         int             *v;
714         int             i, j;
715         const char      *name;
716         int             type;
717
718         FS_Print(f, "{\n");
719
720         if (ed->priv.required->free)
721         {
722                 FS_Print(f, "}\n");
723                 return;
724         }
725
726         for (i=1 ; i<prog->progs->numfielddefs ; i++)
727         {
728                 d = &prog->fielddefs[i];
729                 name = PRVM_GetString(d->s_name);
730
731                 if(developer_entityparsing.integer)
732                         Con_Printf("PRVM_ED_Write: at entity %d field %s\n", PRVM_NUM_FOR_EDICT(ed), name);
733
734                 if (name[strlen(name)-2] == '_')
735                         continue;       // skip _x, _y, _z vars
736
737                 v = (int *)(ed->fields.vp + d->ofs);
738
739         // if the value is still all 0, skip the field
740                 type = d->type & ~DEF_SAVEGLOBAL;
741                 for (j=0 ; j<prvm_type_size[type] ; j++)
742                         if (v[j])
743                                 break;
744                 if (j == prvm_type_size[type])
745                         continue;
746
747                 FS_Printf(f,"\"%s\" ",name);
748                 prog->statestring = va("PRVM_ED_Write, ent=%d, name=%s", i, name);
749                 FS_Printf(f,"\"%s\"\n", PRVM_UglyValueString((etype_t)d->type, (prvm_eval_t *)v));
750                 prog->statestring = NULL;
751         }
752
753         FS_Print(f, "}\n");
754 }
755
756 void PRVM_ED_PrintNum (int ent, const char *wildcard_fieldname)
757 {
758         PRVM_ED_Print(PRVM_EDICT_NUM(ent), wildcard_fieldname);
759 }
760
761 /*
762 =============
763 PRVM_ED_PrintEdicts_f
764
765 For debugging, prints all the entities in the current server
766 =============
767 */
768 void PRVM_ED_PrintEdicts_f (void)
769 {
770         int             i;
771         const char *wildcard_fieldname;
772
773         if(Cmd_Argc() < 2 || Cmd_Argc() > 3)
774         {
775                 Con_Print("prvm_edicts <program name> <optional field name wildcard>\n");
776                 return;
777         }
778
779         PRVM_Begin;
780         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
781                 return;
782
783         if( Cmd_Argc() == 3)
784                 wildcard_fieldname = Cmd_Argv(2);
785         else
786                 wildcard_fieldname = NULL;
787
788         Con_Printf("%s: %i entities\n", PRVM_NAME, prog->num_edicts);
789         for (i=0 ; i<prog->num_edicts ; i++)
790                 PRVM_ED_PrintNum (i, wildcard_fieldname);
791
792         PRVM_End;
793 }
794
795 /*
796 =============
797 PRVM_ED_PrintEdict_f
798
799 For debugging, prints a single edict
800 =============
801 */
802 void PRVM_ED_PrintEdict_f (void)
803 {
804         int             i;
805         const char      *wildcard_fieldname;
806
807         if(Cmd_Argc() < 3 || Cmd_Argc() > 4)
808         {
809                 Con_Print("prvm_edict <program name> <edict number> <optional field name wildcard>\n");
810                 return;
811         }
812
813         PRVM_Begin;
814         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
815                 return;
816
817         i = atoi (Cmd_Argv(2));
818         if (i >= prog->num_edicts)
819         {
820                 Con_Print("Bad edict number\n");
821                 PRVM_End;
822                 return;
823         }
824         if( Cmd_Argc() == 4)
825                 // Optional Wildcard Provided
826                 wildcard_fieldname = Cmd_Argv(3);
827         else
828                 // Use All
829                 wildcard_fieldname = NULL;
830         PRVM_ED_PrintNum (i, wildcard_fieldname);
831
832         PRVM_End;
833 }
834
835 /*
836 =============
837 PRVM_ED_Count
838
839 For debugging
840 =============
841 */
842 // 2 possibilities : 1. just displaying the active edict count
843 //                                       2. making a function pointer [x]
844 void PRVM_ED_Count_f (void)
845 {
846         int             i;
847         prvm_edict_t    *ent;
848         int             active;
849
850         if(Cmd_Argc() != 2)
851         {
852                 Con_Print("prvm_count <program name>\n");
853                 return;
854         }
855
856         PRVM_Begin;
857         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
858                 return;
859
860         if(prog->count_edicts)
861                 prog->count_edicts();
862         else
863         {
864                 active = 0;
865                 for (i=0 ; i<prog->num_edicts ; i++)
866                 {
867                         ent = PRVM_EDICT_NUM(i);
868                         if (ent->priv.required->free)
869                                 continue;
870                         active++;
871                 }
872
873                 Con_Printf("num_edicts:%3i\n", prog->num_edicts);
874                 Con_Printf("active    :%3i\n", active);
875         }
876
877         PRVM_End;
878 }
879
880 /*
881 ==============================================================================
882
883                                         ARCHIVING GLOBALS
884
885 FIXME: need to tag constants, doesn't really work
886 ==============================================================================
887 */
888
889 /*
890 =============
891 PRVM_ED_WriteGlobals
892 =============
893 */
894 void PRVM_ED_WriteGlobals (qfile_t *f)
895 {
896         ddef_t          *def;
897         int                     i;
898         const char              *name;
899         int                     type;
900
901         FS_Print(f,"{\n");
902         for (i=0 ; i<prog->progs->numglobaldefs ; i++)
903         {
904                 def = &prog->globaldefs[i];
905                 type = def->type;
906                 if ( !(def->type & DEF_SAVEGLOBAL) )
907                         continue;
908                 type &= ~DEF_SAVEGLOBAL;
909
910                 if (type != ev_string && type != ev_float && type != ev_entity)
911                         continue;
912
913                 name = PRVM_GetString(def->s_name);
914
915                 if(developer_entityparsing.integer)
916                         Con_Printf("PRVM_ED_WriteGlobals: at global %s\n", name);
917
918                 prog->statestring = va("PRVM_ED_WriteGlobals, name=%s", name);
919                 FS_Printf(f,"\"%s\" ", name);
920                 FS_Printf(f,"\"%s\"\n", PRVM_UglyValueString((etype_t)type, (prvm_eval_t *)&prog->globals.generic[def->ofs]));
921                 prog->statestring = NULL;
922         }
923         FS_Print(f,"}\n");
924 }
925
926 /*
927 =============
928 PRVM_ED_ParseGlobals
929 =============
930 */
931 void PRVM_ED_ParseGlobals (const char *data)
932 {
933         char keyname[MAX_INPUTLINE];
934         ddef_t *key;
935
936         while (1)
937         {
938                 // parse key
939                 if (!COM_ParseToken_Simple(&data, false, false))
940                         PRVM_ERROR ("PRVM_ED_ParseGlobals: EOF without closing brace");
941                 if (com_token[0] == '}')
942                         break;
943
944                 if (developer_entityparsing.integer)
945                         Con_Printf("Key: \"%s\"", com_token);
946
947                 strlcpy (keyname, com_token, sizeof(keyname));
948
949                 // parse value
950                 if (!COM_ParseToken_Simple(&data, false, true))
951                         PRVM_ERROR ("PRVM_ED_ParseGlobals: EOF without closing brace");
952
953                 if (developer_entityparsing.integer)
954                         Con_Printf(" \"%s\"\n", com_token);
955
956                 if (com_token[0] == '}')
957                         PRVM_ERROR ("PRVM_ED_ParseGlobals: closing brace without data");
958
959                 key = PRVM_ED_FindGlobal (keyname);
960                 if (!key)
961                 {
962                         Con_DPrintf("'%s' is not a global on %s\n", keyname, PRVM_NAME);
963                         continue;
964                 }
965
966                 if (!PRVM_ED_ParseEpair(NULL, key, com_token, true))
967                         PRVM_ERROR ("PRVM_ED_ParseGlobals: parse error");
968         }
969 }
970
971 //============================================================================
972
973
974 /*
975 =============
976 PRVM_ED_ParseEval
977
978 Can parse either fields or globals
979 returns false if error
980 =============
981 */
982 qboolean PRVM_ED_ParseEpair(prvm_edict_t *ent, ddef_t *key, const char *s, qboolean parsebackslash)
983 {
984         int i, l;
985         char *new_p;
986         ddef_t *def;
987         prvm_eval_t *val;
988         mfunction_t *func;
989
990         if (ent)
991                 val = (prvm_eval_t *)(ent->fields.vp + key->ofs);
992         else
993                 val = (prvm_eval_t *)(prog->globals.generic + key->ofs);
994         switch (key->type & ~DEF_SAVEGLOBAL)
995         {
996         case ev_string:
997                 l = (int)strlen(s) + 1;
998                 val->string = PRVM_AllocString(l, &new_p);
999                 for (i = 0;i < l;i++)
1000                 {
1001                         if (s[i] == '\\' && s[i+1] && parsebackslash)
1002                         {
1003                                 i++;
1004                                 if (s[i] == 'n')
1005                                         *new_p++ = '\n';
1006                                 else if (s[i] == 'r')
1007                                         *new_p++ = '\r';
1008                                 else
1009                                         *new_p++ = s[i];
1010                         }
1011                         else
1012                                 *new_p++ = s[i];
1013                 }
1014                 break;
1015
1016         case ev_float:
1017                 while (*s && ISWHITESPACE(*s))
1018                         s++;
1019                 val->_float = atof(s);
1020                 break;
1021
1022         case ev_vector:
1023                 for (i = 0;i < 3;i++)
1024                 {
1025                         while (*s && ISWHITESPACE(*s))
1026                                 s++;
1027                         if (!*s)
1028                                 break;
1029                         val->vector[i] = atof(s);
1030                         while (!ISWHITESPACE(*s))
1031                                 s++;
1032                         if (!*s)
1033                                 break;
1034                 }
1035                 break;
1036
1037         case ev_entity:
1038                 while (*s && ISWHITESPACE(*s))
1039                         s++;
1040                 i = atoi(s);
1041                 if (i >= prog->limit_edicts)
1042                         Con_Printf("PRVM_ED_ParseEpair: ev_entity reference too large (edict %u >= MAX_EDICTS %u) on %s\n", (unsigned int)i, prog->limit_edicts, PRVM_NAME);
1043                 while (i >= prog->max_edicts)
1044                         PRVM_MEM_IncreaseEdicts();
1045                 // if IncreaseEdicts was called the base pointer needs to be updated
1046                 if (ent)
1047                         val = (prvm_eval_t *)(ent->fields.vp + key->ofs);
1048                 val->edict = PRVM_EDICT_TO_PROG(PRVM_EDICT_NUM((int)i));
1049                 break;
1050
1051         case ev_field:
1052                 if (*s != '.')
1053                 {
1054                         Con_DPrintf("PRVM_ED_ParseEpair: Bogus field name %s in %s\n", s, PRVM_NAME);
1055                         return false;
1056                 }
1057                 def = PRVM_ED_FindField(s + 1);
1058                 if (!def)
1059                 {
1060                         Con_DPrintf("PRVM_ED_ParseEpair: Can't find field %s in %s\n", s, PRVM_NAME);
1061                         return false;
1062                 }
1063                 val->_int = def->ofs;
1064                 break;
1065
1066         case ev_function:
1067                 func = PRVM_ED_FindFunction(s);
1068                 if (!func)
1069                 {
1070                         Con_Printf("PRVM_ED_ParseEpair: Can't find function %s in %s\n", s, PRVM_NAME);
1071                         return false;
1072                 }
1073                 val->function = func - prog->functions;
1074                 break;
1075
1076         default:
1077                 Con_Printf("PRVM_ED_ParseEpair: Unknown key->type %i for key \"%s\" on %s\n", key->type, PRVM_GetString(key->s_name), PRVM_NAME);
1078                 return false;
1079         }
1080         return true;
1081 }
1082
1083 /*
1084 =============
1085 PRVM_GameCommand_f
1086
1087 Console command to send a string to QC function GameCommand of the
1088 indicated progs
1089
1090 Usage:
1091   sv_cmd adminmsg 3 "do not teamkill"
1092   cl_cmd someclientcommand
1093   menu_cmd somemenucommand
1094
1095 All progs can support this extension; sg calls it in server QC, cg in client
1096 QC, mg in menu QC.
1097 =============
1098 */
1099 void PRVM_GameCommand(const char *whichprogs, const char *whichcmd)
1100 {
1101         if(Cmd_Argc() < 1)
1102         {
1103                 Con_Printf("%s text...\n", whichcmd);
1104                 return;
1105         }
1106
1107         PRVM_Begin;
1108         if(!PRVM_SetProgFromString(whichprogs))
1109         // note: this is not PRVM_SetProg because that one aborts "hard" using PRVM_Error
1110         // also, it makes printing error messages easier!
1111         {
1112                 Con_Printf("%s program not loaded.\n", whichprogs);
1113                 return;
1114         }
1115
1116         if(!prog->funcoffsets.GameCommand)
1117         {
1118                 Con_Printf("%s program do not support GameCommand!\n", whichprogs);
1119         }
1120         else
1121         {
1122                 int restorevm_tempstringsbuf_cursize;
1123                 const char *s;
1124
1125                 s = Cmd_Args();
1126
1127                 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
1128                 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(s ? s : "");
1129                 PRVM_ExecuteProgram (prog->funcoffsets.GameCommand, "QC function GameCommand is missing");
1130                 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
1131         }
1132
1133         PRVM_End;
1134 }
1135 void PRVM_GameCommand_Server_f(void)
1136 {
1137         PRVM_GameCommand("server", "sv_cmd");
1138 }
1139 void PRVM_GameCommand_Client_f(void)
1140 {
1141         PRVM_GameCommand("client", "cl_cmd");
1142 }
1143 void PRVM_GameCommand_Menu_f(void)
1144 {
1145         PRVM_GameCommand("menu", "menu_cmd");
1146 }
1147
1148 /*
1149 =============
1150 PRVM_ED_EdictGet_f
1151
1152 Console command to load a field of a specified edict
1153 =============
1154 */
1155 void PRVM_ED_EdictGet_f(void)
1156 {
1157         prvm_edict_t *ed;
1158         ddef_t *key;
1159         const char *s;
1160         prvm_eval_t *v;
1161
1162         if(Cmd_Argc() != 4 && Cmd_Argc() != 5)
1163         {
1164                 Con_Print("prvm_edictget <program name> <edict number> <field> [<cvar>]\n");
1165                 return;
1166         }
1167
1168         PRVM_Begin;
1169         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
1170         {
1171                 Con_Printf("Wrong program name %s !\n", Cmd_Argv(1));
1172                 return;
1173         }
1174
1175         ed = PRVM_EDICT_NUM(atoi(Cmd_Argv(2)));
1176
1177         if((key = PRVM_ED_FindField(Cmd_Argv(3))) == 0)
1178         {
1179                 Con_Printf("Key %s not found !\n", Cmd_Argv(3));
1180                 goto fail;
1181         }
1182
1183         v = (prvm_eval_t *)(ed->fields.vp + key->ofs);
1184         s = PRVM_UglyValueString((etype_t)key->type, v);
1185         if(Cmd_Argc() == 5)
1186         {
1187                 cvar_t *cvar = Cvar_FindVar(Cmd_Argv(4));
1188                 if (cvar && cvar->flags & CVAR_READONLY)
1189                 {
1190                         Con_Printf("prvm_edictget: %s is read-only\n", cvar->name);
1191                         goto fail;
1192                 }
1193                 Cvar_Get(Cmd_Argv(4), s, 0, NULL);
1194         }
1195         else
1196                 Con_Printf("%s\n", s);
1197
1198 fail:
1199         PRVM_End;
1200 }
1201
1202 void PRVM_ED_GlobalGet_f(void)
1203 {
1204         ddef_t *key;
1205         const char *s;
1206         prvm_eval_t *v;
1207
1208         if(Cmd_Argc() != 3 && Cmd_Argc() != 4)
1209         {
1210                 Con_Print("prvm_globalget <program name> <global> [<cvar>]\n");
1211                 return;
1212         }
1213
1214         PRVM_Begin;
1215         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
1216         {
1217                 Con_Printf("Wrong program name %s !\n", Cmd_Argv(1));
1218                 return;
1219         }
1220
1221         key = PRVM_ED_FindGlobal(Cmd_Argv(2));
1222         if(!key)
1223         {
1224                 Con_Printf( "No global '%s' in %s!\n", Cmd_Argv(2), Cmd_Argv(1) );
1225                 goto fail;
1226         }
1227
1228         v = (prvm_eval_t *) &prog->globals.generic[key->ofs];
1229         s = PRVM_UglyValueString((etype_t)key->type, v);
1230         if(Cmd_Argc() == 4)
1231         {
1232                 cvar_t *cvar = Cvar_FindVar(Cmd_Argv(3));
1233                 if (cvar && cvar->flags & CVAR_READONLY)
1234                 {
1235                         Con_Printf("prvm_globalget: %s is read-only\n", cvar->name);
1236                         goto fail;
1237                 }
1238                 Cvar_Get(Cmd_Argv(3), s, 0, NULL);
1239         }
1240         else
1241                 Con_Printf("%s\n", s);
1242
1243 fail:
1244         PRVM_End;
1245 }
1246
1247 /*
1248 =============
1249 PRVM_ED_EdictSet_f
1250
1251 Console command to set a field of a specified edict
1252 =============
1253 */
1254 void PRVM_ED_EdictSet_f(void)
1255 {
1256         prvm_edict_t *ed;
1257         ddef_t *key;
1258
1259         if(Cmd_Argc() != 5)
1260         {
1261                 Con_Print("prvm_edictset <program name> <edict number> <field> <value>\n");
1262                 return;
1263         }
1264
1265         PRVM_Begin;
1266         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
1267         {
1268                 Con_Printf("Wrong program name %s !\n", Cmd_Argv(1));
1269                 return;
1270         }
1271
1272         ed = PRVM_EDICT_NUM(atoi(Cmd_Argv(2)));
1273
1274         if((key = PRVM_ED_FindField(Cmd_Argv(3))) == 0)
1275                 Con_Printf("Key %s not found !\n", Cmd_Argv(3));
1276         else
1277                 PRVM_ED_ParseEpair(ed, key, Cmd_Argv(4), true);
1278
1279         PRVM_End;
1280 }
1281
1282 /*
1283 ====================
1284 PRVM_ED_ParseEdict
1285
1286 Parses an edict out of the given string, returning the new position
1287 ed should be a properly initialized empty edict.
1288 Used for initial level load and for savegames.
1289 ====================
1290 */
1291 const char *PRVM_ED_ParseEdict (const char *data, prvm_edict_t *ent)
1292 {
1293         ddef_t *key;
1294         qboolean anglehack;
1295         qboolean init;
1296         char keyname[256];
1297         size_t n;
1298
1299         init = false;
1300
1301 // go through all the dictionary pairs
1302         while (1)
1303         {
1304         // parse key
1305                 if (!COM_ParseToken_Simple(&data, false, false))
1306                         PRVM_ERROR ("PRVM_ED_ParseEdict: EOF without closing brace");
1307                 if (developer_entityparsing.integer)
1308                         Con_Printf("Key: \"%s\"", com_token);
1309                 if (com_token[0] == '}')
1310                         break;
1311
1312                 // anglehack is to allow QuakeEd to write single scalar angles
1313                 // and allow them to be turned into vectors. (FIXME...)
1314                 if (!strcmp(com_token, "angle"))
1315                 {
1316                         strlcpy (com_token, "angles", sizeof(com_token));
1317                         anglehack = true;
1318                 }
1319                 else
1320                         anglehack = false;
1321
1322                 // FIXME: change light to _light to get rid of this hack
1323                 if (!strcmp(com_token, "light"))
1324                         strlcpy (com_token, "light_lev", sizeof(com_token));    // hack for single light def
1325
1326                 strlcpy (keyname, com_token, sizeof(keyname));
1327
1328                 // another hack to fix keynames with trailing spaces
1329                 n = strlen(keyname);
1330                 while (n && keyname[n-1] == ' ')
1331                 {
1332                         keyname[n-1] = 0;
1333                         n--;
1334                 }
1335
1336         // parse value
1337                 if (!COM_ParseToken_Simple(&data, false, false))
1338                         PRVM_ERROR ("PRVM_ED_ParseEdict: EOF without closing brace");
1339                 if (developer_entityparsing.integer)
1340                         Con_Printf(" \"%s\"\n", com_token);
1341
1342                 if (com_token[0] == '}')
1343                         PRVM_ERROR ("PRVM_ED_ParseEdict: closing brace without data");
1344
1345                 init = true;
1346
1347                 // ignore attempts to set key "" (this problem occurs in nehahra neh1m8.bsp)
1348                 if (!keyname[0])
1349                         continue;
1350
1351 // keynames with a leading underscore are used for utility comments,
1352 // and are immediately discarded by quake
1353                 if (keyname[0] == '_')
1354                         continue;
1355
1356                 key = PRVM_ED_FindField (keyname);
1357                 if (!key)
1358                 {
1359                         Con_DPrintf("%s: '%s' is not a field\n", PRVM_NAME, keyname);
1360                         continue;
1361                 }
1362
1363                 if (anglehack)
1364                 {
1365                         char    temp[32];
1366                         strlcpy (temp, com_token, sizeof(temp));
1367                         dpsnprintf (com_token, sizeof(com_token), "0 %s 0", temp);
1368                 }
1369
1370                 if (!PRVM_ED_ParseEpair(ent, key, com_token, strcmp(keyname, "wad") != 0))
1371                         PRVM_ERROR ("PRVM_ED_ParseEdict: parse error");
1372         }
1373
1374         if (!init)
1375                 ent->priv.required->free = true;
1376
1377         return data;
1378 }
1379
1380
1381 /*
1382 ================
1383 PRVM_ED_LoadFromFile
1384
1385 The entities are directly placed in the array, rather than allocated with
1386 PRVM_ED_Alloc, because otherwise an error loading the map would have entity
1387 number references out of order.
1388
1389 Creates a server's entity / program execution context by
1390 parsing textual entity definitions out of an ent file.
1391
1392 Used for both fresh maps and savegame loads.  A fresh map would also need
1393 to call PRVM_ED_CallSpawnFunctions () to let the objects initialize themselves.
1394 ================
1395 */
1396 void PRVM_ED_LoadFromFile (const char *data)
1397 {
1398         prvm_edict_t *ent;
1399         int parsed, inhibited, spawned, died;
1400         const char *funcname;
1401         mfunction_t *func;
1402
1403         parsed = 0;
1404         inhibited = 0;
1405         spawned = 0;
1406         died = 0;
1407
1408         prvm_reuseedicts_always_allow = realtime;
1409
1410 // parse ents
1411         while (1)
1412         {
1413 // parse the opening brace
1414                 if (!COM_ParseToken_Simple(&data, false, false))
1415                         break;
1416                 if (com_token[0] != '{')
1417                         PRVM_ERROR ("PRVM_ED_LoadFromFile: %s: found %s when expecting {", PRVM_NAME, com_token);
1418
1419                 // CHANGED: this is not conform to PR_LoadFromFile
1420                 if(prog->loadintoworld)
1421                 {
1422                         prog->loadintoworld = false;
1423                         ent = PRVM_EDICT_NUM(0);
1424                 }
1425                 else
1426                         ent = PRVM_ED_Alloc();
1427
1428                 // clear it
1429                 if (ent != prog->edicts)        // hack
1430                         memset (ent->fields.vp, 0, prog->progs->entityfields * 4);
1431
1432                 data = PRVM_ED_ParseEdict (data, ent);
1433                 parsed++;
1434
1435                 // remove the entity ?
1436                 if(prog->load_edict && !prog->load_edict(ent))
1437                 {
1438                         PRVM_ED_Free(ent);
1439                         inhibited++;
1440                         continue;
1441                 }
1442
1443                 if (prog->funcoffsets.SV_OnEntityPreSpawnFunction)
1444                 {
1445                         // self = ent
1446                         PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict = PRVM_EDICT_TO_PROG(ent);
1447                         PRVM_ExecuteProgram (prog->funcoffsets.SV_OnEntityPreSpawnFunction, "QC function SV_OnEntityPreSpawnFunction is missing");
1448                 }
1449
1450                 if(ent->priv.required->free)
1451                 {
1452                         inhibited++;
1453                         continue;
1454                 }
1455
1456 //
1457 // immediately call spawn function, but only if there is a self global and a classname
1458 //
1459                 if(!ent->priv.required->free)
1460                 if(prog->globaloffsets.self >= 0 && prog->fieldoffsets.classname >= 0)
1461                 {
1462                         string_t handle =  PRVM_EDICTFIELDVALUE(ent, prog->fieldoffsets.classname)->string;
1463                         if (!handle)
1464                         {
1465                                 Con_Print("No classname for:\n");
1466                                 PRVM_ED_Print(ent, NULL);
1467                                 PRVM_ED_Free (ent);
1468                                 continue;
1469                         }
1470
1471                         // look for the spawn function
1472                         funcname = PRVM_GetString(handle);
1473                         func = PRVM_ED_FindFunction (va("spawnfunc_%s", funcname));
1474                         if(!func)
1475                                 if(prog->globaloffsets.require_spawnfunc_prefix < 0)
1476                                         func = PRVM_ED_FindFunction (funcname);
1477
1478                         if (!func)
1479                         {
1480                                 // check for OnEntityNoSpawnFunction
1481                                 if (prog->funcoffsets.SV_OnEntityNoSpawnFunction)
1482                                 {
1483                                         // self = ent
1484                                         PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict = PRVM_EDICT_TO_PROG(ent);
1485                                         PRVM_ExecuteProgram (prog->funcoffsets.SV_OnEntityNoSpawnFunction, "QC function SV_OnEntityNoSpawnFunction is missing");
1486                                 }
1487                                 else
1488                                 {
1489                                         if (developer.integer > 0) // don't confuse non-developers with errors
1490                                         {
1491                                                 Con_Print("No spawn function for:\n");
1492                                                 PRVM_ED_Print(ent, NULL);
1493                                         }
1494                                         PRVM_ED_Free (ent);
1495                                         continue; // not included in "inhibited" count
1496                                 }
1497                         }
1498                         else
1499                         {
1500                                 // self = ent
1501                                 PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict = PRVM_EDICT_TO_PROG(ent);
1502                                 PRVM_ExecuteProgram (func - prog->functions, "");
1503                         }
1504                 }
1505
1506                 if(!ent->priv.required->free)
1507                 if (prog->funcoffsets.SV_OnEntityPostSpawnFunction)
1508                 {
1509                         // self = ent
1510                         PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict = PRVM_EDICT_TO_PROG(ent);
1511                         PRVM_ExecuteProgram (prog->funcoffsets.SV_OnEntityPostSpawnFunction, "QC function SV_OnEntityPostSpawnFunction is missing");
1512                 }
1513
1514                 spawned++;
1515                 if (ent->priv.required->free)
1516                         died++;
1517         }
1518
1519         Con_DPrintf("%s: %i new entities parsed, %i new inhibited, %i (%i new) spawned (whereas %i removed self, %i stayed)\n", PRVM_NAME, parsed, inhibited, prog->num_edicts, spawned, died, spawned - died);
1520
1521         prvm_reuseedicts_always_allow = 0;
1522 }
1523
1524 void PRVM_FindOffsets(void)
1525 {
1526         // field and global searches use -1 for NULL
1527         memset(&prog->fieldoffsets, -1, sizeof(prog->fieldoffsets));
1528         memset(&prog->globaloffsets, -1, sizeof(prog->globaloffsets));
1529         // functions use 0 for NULL
1530         memset(&prog->funcoffsets, 0, sizeof(prog->funcoffsets));
1531
1532         // server and client qc use a lot of similar fields, so this is combined
1533         prog->fieldoffsets.SendEntity                     = PRVM_ED_FindFieldOffset("SendEntity");
1534         prog->fieldoffsets.SendFlags                      = PRVM_ED_FindFieldOffset("SendFlags");
1535         prog->fieldoffsets.Version                        = PRVM_ED_FindFieldOffset("Version");
1536         prog->fieldoffsets.alpha                          = PRVM_ED_FindFieldOffset("alpha");
1537         prog->fieldoffsets.ammo_cells1                    = PRVM_ED_FindFieldOffset("ammo_cells1");
1538         prog->fieldoffsets.ammo_lava_nails                = PRVM_ED_FindFieldOffset("ammo_lava_nails");
1539         prog->fieldoffsets.ammo_multi_rockets             = PRVM_ED_FindFieldOffset("ammo_multi_rockets");
1540         prog->fieldoffsets.ammo_nails1                    = PRVM_ED_FindFieldOffset("ammo_nails1");
1541         prog->fieldoffsets.ammo_plasma                    = PRVM_ED_FindFieldOffset("ammo_plasma");
1542         prog->fieldoffsets.ammo_rockets1                  = PRVM_ED_FindFieldOffset("ammo_rockets1");
1543         prog->fieldoffsets.ammo_shells1                   = PRVM_ED_FindFieldOffset("ammo_shells1");
1544         prog->fieldoffsets.angles                         = PRVM_ED_FindFieldOffset("angles");
1545         prog->fieldoffsets.button3                        = PRVM_ED_FindFieldOffset("button3");
1546         prog->fieldoffsets.button4                        = PRVM_ED_FindFieldOffset("button4");
1547         prog->fieldoffsets.button5                        = PRVM_ED_FindFieldOffset("button5");
1548         prog->fieldoffsets.button6                        = PRVM_ED_FindFieldOffset("button6");
1549         prog->fieldoffsets.button7                        = PRVM_ED_FindFieldOffset("button7");
1550         prog->fieldoffsets.button8                        = PRVM_ED_FindFieldOffset("button8");
1551         prog->fieldoffsets.button9                        = PRVM_ED_FindFieldOffset("button9");
1552         prog->fieldoffsets.button10                       = PRVM_ED_FindFieldOffset("button10");
1553         prog->fieldoffsets.button11                       = PRVM_ED_FindFieldOffset("button11");
1554         prog->fieldoffsets.button12                       = PRVM_ED_FindFieldOffset("button12");
1555         prog->fieldoffsets.button13                       = PRVM_ED_FindFieldOffset("button13");
1556         prog->fieldoffsets.button14                       = PRVM_ED_FindFieldOffset("button14");
1557         prog->fieldoffsets.button15                       = PRVM_ED_FindFieldOffset("button15");
1558         prog->fieldoffsets.button16                       = PRVM_ED_FindFieldOffset("button16");
1559         prog->fieldoffsets.buttonchat                     = PRVM_ED_FindFieldOffset("buttonchat");
1560         prog->fieldoffsets.buttonuse                      = PRVM_ED_FindFieldOffset("buttonuse");
1561         prog->fieldoffsets.chain                          = PRVM_ED_FindFieldOffset("chain");
1562         prog->fieldoffsets.classname                      = PRVM_ED_FindFieldOffset("classname");
1563         prog->fieldoffsets.clientcamera                   = PRVM_ED_FindFieldOffset("clientcamera");
1564         prog->fieldoffsets.clientcolors                   = PRVM_ED_FindFieldOffset("clientcolors");
1565         prog->fieldoffsets.clientstatus                   = PRVM_ED_FindFieldOffset("clientstatus");
1566         prog->fieldoffsets.color                          = PRVM_ED_FindFieldOffset("color");
1567         prog->fieldoffsets.colormod                       = PRVM_ED_FindFieldOffset("colormod");
1568         prog->fieldoffsets.contentstransition             = PRVM_ED_FindFieldOffset("contentstransition");
1569         prog->fieldoffsets.cursor_active                  = PRVM_ED_FindFieldOffset("cursor_active");
1570         prog->fieldoffsets.cursor_screen                  = PRVM_ED_FindFieldOffset("cursor_screen");
1571         prog->fieldoffsets.cursor_trace_endpos            = PRVM_ED_FindFieldOffset("cursor_trace_endpos");
1572         prog->fieldoffsets.cursor_trace_ent               = PRVM_ED_FindFieldOffset("cursor_trace_ent");
1573         prog->fieldoffsets.cursor_trace_start             = PRVM_ED_FindFieldOffset("cursor_trace_start");
1574         prog->fieldoffsets.customizeentityforclient       = PRVM_ED_FindFieldOffset("customizeentityforclient");
1575         prog->fieldoffsets.dimension_hit                  = PRVM_ED_FindFieldOffset("dimension_hit");
1576         prog->fieldoffsets.dimension_solid                = PRVM_ED_FindFieldOffset("dimension_solid");
1577         prog->fieldoffsets.disableclientprediction        = PRVM_ED_FindFieldOffset("disableclientprediction");
1578         prog->fieldoffsets.discardabledemo                = PRVM_ED_FindFieldOffset("discardabledemo");
1579         prog->fieldoffsets.dphitcontentsmask              = PRVM_ED_FindFieldOffset("dphitcontentsmask");
1580         prog->fieldoffsets.drawonlytoclient               = PRVM_ED_FindFieldOffset("drawonlytoclient");
1581         prog->fieldoffsets.exteriormodeltoclient          = PRVM_ED_FindFieldOffset("exteriormodeltoclient");
1582         prog->fieldoffsets.fatness                        = PRVM_ED_FindFieldOffset("fatness");
1583         prog->fieldoffsets.forceshader                    = PRVM_ED_FindFieldOffset("forceshader");
1584         prog->fieldoffsets.frame                          = PRVM_ED_FindFieldOffset("frame");
1585         prog->fieldoffsets.frame1time                     = PRVM_ED_FindFieldOffset("frame1time");
1586         prog->fieldoffsets.frame2                         = PRVM_ED_FindFieldOffset("frame2");
1587         prog->fieldoffsets.frame2time                     = PRVM_ED_FindFieldOffset("frame2time");
1588         prog->fieldoffsets.frame3                         = PRVM_ED_FindFieldOffset("frame3");
1589         prog->fieldoffsets.frame3time                     = PRVM_ED_FindFieldOffset("frame3time");
1590         prog->fieldoffsets.frame4                         = PRVM_ED_FindFieldOffset("frame4");
1591         prog->fieldoffsets.frame4time                     = PRVM_ED_FindFieldOffset("frame4time");
1592         prog->fieldoffsets.fullbright                     = PRVM_ED_FindFieldOffset("fullbright");
1593         prog->fieldoffsets.glow_color                     = PRVM_ED_FindFieldOffset("glow_color");
1594         prog->fieldoffsets.glow_size                      = PRVM_ED_FindFieldOffset("glow_size");
1595         prog->fieldoffsets.glow_trail                     = PRVM_ED_FindFieldOffset("glow_trail");
1596         prog->fieldoffsets.glowmod                        = PRVM_ED_FindFieldOffset("glowmod");
1597         prog->fieldoffsets.gravity                        = PRVM_ED_FindFieldOffset("gravity");
1598         prog->fieldoffsets.groundentity                   = PRVM_ED_FindFieldOffset("groundentity");
1599         prog->fieldoffsets.hull                           = PRVM_ED_FindFieldOffset("hull");
1600         prog->fieldoffsets.ideal_yaw                      = PRVM_ED_FindFieldOffset("ideal_yaw");
1601         prog->fieldoffsets.idealpitch                     = PRVM_ED_FindFieldOffset("idealpitch");
1602         prog->fieldoffsets.items2                         = PRVM_ED_FindFieldOffset("items2");
1603         prog->fieldoffsets.lerpfrac                       = PRVM_ED_FindFieldOffset("lerpfrac");
1604         prog->fieldoffsets.lerpfrac3                      = PRVM_ED_FindFieldOffset("lerpfrac3");
1605         prog->fieldoffsets.lerpfrac4                      = PRVM_ED_FindFieldOffset("lerpfrac4");
1606         prog->fieldoffsets.light_lev                      = PRVM_ED_FindFieldOffset("light_lev");
1607         prog->fieldoffsets.message                        = PRVM_ED_FindFieldOffset("message");
1608         prog->fieldoffsets.modelflags                     = PRVM_ED_FindFieldOffset("modelflags");
1609         prog->fieldoffsets.movement                       = PRVM_ED_FindFieldOffset("movement");
1610         prog->fieldoffsets.movetypesteplandevent          = PRVM_ED_FindFieldOffset("movetypesteplandevent");
1611         prog->fieldoffsets.netaddress                     = PRVM_ED_FindFieldOffset("netaddress");
1612         prog->fieldoffsets.nextthink                      = PRVM_ED_FindFieldOffset("nextthink");
1613         prog->fieldoffsets.nodrawtoclient                 = PRVM_ED_FindFieldOffset("nodrawtoclient");
1614         prog->fieldoffsets.pflags                         = PRVM_ED_FindFieldOffset("pflags");
1615         prog->fieldoffsets.ping                           = PRVM_ED_FindFieldOffset("ping");
1616         prog->fieldoffsets.packetloss                     = PRVM_ED_FindFieldOffset("ping_packetloss");
1617         prog->fieldoffsets.movementloss                   = PRVM_ED_FindFieldOffset("ping_movementloss");
1618         prog->fieldoffsets.pitch_speed                    = PRVM_ED_FindFieldOffset("pitch_speed");
1619         prog->fieldoffsets.playermodel                    = PRVM_ED_FindFieldOffset("playermodel");
1620         prog->fieldoffsets.playerskin                     = PRVM_ED_FindFieldOffset("playerskin");
1621         prog->fieldoffsets.pmodel                         = PRVM_ED_FindFieldOffset("pmodel");
1622         prog->fieldoffsets.punchvector                    = PRVM_ED_FindFieldOffset("punchvector");
1623         prog->fieldoffsets.renderamt                      = PRVM_ED_FindFieldOffset("renderamt"); // HalfLife support
1624         prog->fieldoffsets.renderflags                    = PRVM_ED_FindFieldOffset("renderflags");
1625         prog->fieldoffsets.rendermode                     = PRVM_ED_FindFieldOffset("rendermode"); // HalfLife support
1626         prog->fieldoffsets.scale                          = PRVM_ED_FindFieldOffset("scale");
1627         prog->fieldoffsets.shadertime                     = PRVM_ED_FindFieldOffset("shadertime");
1628         prog->fieldoffsets.skeletonindex                  = PRVM_ED_FindFieldOffset("skeletonindex");
1629         prog->fieldoffsets.style                          = PRVM_ED_FindFieldOffset("style");
1630         prog->fieldoffsets.tag_entity                     = PRVM_ED_FindFieldOffset("tag_entity");
1631         prog->fieldoffsets.tag_index                      = PRVM_ED_FindFieldOffset("tag_index");
1632         prog->fieldoffsets.think                          = PRVM_ED_FindFieldOffset("think");
1633         prog->fieldoffsets.viewmodelforclient             = PRVM_ED_FindFieldOffset("viewmodelforclient");
1634         prog->fieldoffsets.viewzoom                       = PRVM_ED_FindFieldOffset("viewzoom");
1635         prog->fieldoffsets.yaw_speed                      = PRVM_ED_FindFieldOffset("yaw_speed");
1636         prog->fieldoffsets.bouncefactor                   = PRVM_ED_FindFieldOffset("bouncefactor");
1637         prog->fieldoffsets.bouncestop                     = PRVM_ED_FindFieldOffset("bouncestop");
1638
1639         prog->fieldoffsets.solid                          = PRVM_ED_FindFieldOffset("solid");
1640         prog->fieldoffsets.movetype                       = PRVM_ED_FindFieldOffset("movetype");
1641         prog->fieldoffsets.modelindex                     = PRVM_ED_FindFieldOffset("modelindex");
1642         prog->fieldoffsets.mins                           = PRVM_ED_FindFieldOffset("mins");
1643         prog->fieldoffsets.maxs                           = PRVM_ED_FindFieldOffset("maxs");
1644         prog->fieldoffsets.mass                           = PRVM_ED_FindFieldOffset("mass");
1645         prog->fieldoffsets.origin                         = PRVM_ED_FindFieldOffset("origin");
1646         prog->fieldoffsets.velocity                       = PRVM_ED_FindFieldOffset("velocity");
1647         //prog->fieldoffsets.axis_forward                   = PRVM_ED_FindFieldOffset("axis_forward");
1648         //prog->fieldoffsets.axis_left                      = PRVM_ED_FindFieldOffset("axis_left");
1649         //prog->fieldoffsets.axis_up                        = PRVM_ED_FindFieldOffset("axis_up");
1650         //prog->fieldoffsets.spinvelocity                   = PRVM_ED_FindFieldOffset("spinvelocity");
1651         prog->fieldoffsets.angles                         = PRVM_ED_FindFieldOffset("angles");
1652         prog->fieldoffsets.avelocity                      = PRVM_ED_FindFieldOffset("avelocity");
1653         prog->fieldoffsets.aiment                         = PRVM_ED_FindFieldOffset("aiment");
1654         prog->fieldoffsets.enemy                          = PRVM_ED_FindFieldOffset("enemy");
1655         prog->fieldoffsets.jointtype                      = PRVM_ED_FindFieldOffset("jointtype");
1656         prog->fieldoffsets.movedir                        = PRVM_ED_FindFieldOffset("movedir");
1657
1658         prog->fieldoffsets.camera_transform               = PRVM_ED_FindFieldOffset("camera_transform");
1659         prog->fieldoffsets.userwavefunc_param0            = PRVM_ED_FindFieldOffset("userwavefunc_param0");
1660         prog->fieldoffsets.userwavefunc_param1            = PRVM_ED_FindFieldOffset("userwavefunc_param1");
1661         prog->fieldoffsets.userwavefunc_param2            = PRVM_ED_FindFieldOffset("userwavefunc_param2");
1662         prog->fieldoffsets.userwavefunc_param3            = PRVM_ED_FindFieldOffset("userwavefunc_param3");
1663
1664         prog->funcoffsets.CSQC_ConsoleCommand             = PRVM_ED_FindFunctionOffset("CSQC_ConsoleCommand");
1665         prog->funcoffsets.CSQC_Ent_Remove                 = PRVM_ED_FindFunctionOffset("CSQC_Ent_Remove");
1666         prog->funcoffsets.CSQC_Ent_Spawn                  = PRVM_ED_FindFunctionOffset("CSQC_Ent_Spawn");
1667         prog->funcoffsets.CSQC_Ent_Update                 = PRVM_ED_FindFunctionOffset("CSQC_Ent_Update");
1668         prog->funcoffsets.CSQC_Event                      = PRVM_ED_FindFunctionOffset("CSQC_Event");
1669         prog->funcoffsets.CSQC_Event_Sound                = PRVM_ED_FindFunctionOffset("CSQC_Event_Sound");
1670         prog->funcoffsets.CSQC_Init                       = PRVM_ED_FindFunctionOffset("CSQC_Init");
1671         prog->funcoffsets.CSQC_InputEvent                 = PRVM_ED_FindFunctionOffset("CSQC_InputEvent");
1672         prog->funcoffsets.CSQC_Parse_CenterPrint          = PRVM_ED_FindFunctionOffset("CSQC_Parse_CenterPrint");
1673         prog->funcoffsets.CSQC_Parse_Print                = PRVM_ED_FindFunctionOffset("CSQC_Parse_Print");
1674         prog->funcoffsets.CSQC_Parse_StuffCmd             = PRVM_ED_FindFunctionOffset("CSQC_Parse_StuffCmd");
1675         prog->funcoffsets.CSQC_Parse_TempEntity           = PRVM_ED_FindFunctionOffset("CSQC_Parse_TempEntity");
1676         prog->funcoffsets.CSQC_Shutdown                   = PRVM_ED_FindFunctionOffset("CSQC_Shutdown");
1677         prog->funcoffsets.CSQC_UpdateView                 = PRVM_ED_FindFunctionOffset("CSQC_UpdateView");
1678         prog->funcoffsets.EndFrame                        = PRVM_ED_FindFunctionOffset("EndFrame");
1679         prog->funcoffsets.GameCommand                     = PRVM_ED_FindFunctionOffset("GameCommand");
1680         prog->funcoffsets.Gecko_Query                     = PRVM_ED_FindFunctionOffset("Gecko_Query");
1681         prog->funcoffsets.RestoreGame                     = PRVM_ED_FindFunctionOffset("RestoreGame");
1682         prog->funcoffsets.SV_ChangeTeam                   = PRVM_ED_FindFunctionOffset("SV_ChangeTeam");
1683         prog->funcoffsets.SV_OnEntityNoSpawnFunction      = PRVM_ED_FindFunctionOffset("SV_OnEntityNoSpawnFunction");
1684         prog->funcoffsets.SV_OnEntityPostSpawnFunction    = PRVM_ED_FindFunctionOffset("SV_OnEntityPostSpawnFunction");
1685         prog->funcoffsets.SV_OnEntityPreSpawnFunction     = PRVM_ED_FindFunctionOffset("SV_OnEntityPreSpawnFunction");
1686         prog->funcoffsets.SV_ParseClientCommand           = PRVM_ED_FindFunctionOffset("SV_ParseClientCommand");
1687         prog->funcoffsets.SV_PausedTic                    = PRVM_ED_FindFunctionOffset("SV_PausedTic");
1688         prog->funcoffsets.SV_PlayerPhysics                = PRVM_ED_FindFunctionOffset("SV_PlayerPhysics");
1689         prog->funcoffsets.SV_Shutdown                     = PRVM_ED_FindFunctionOffset("SV_Shutdown");
1690         prog->funcoffsets.URI_Get_Callback                = PRVM_ED_FindFunctionOffset("URI_Get_Callback");
1691         prog->globaloffsets.SV_InitCmd                    = PRVM_ED_FindGlobalOffset("SV_InitCmd");
1692         prog->globaloffsets.coop                          = PRVM_ED_FindGlobalOffset("coop");
1693         prog->globaloffsets.deathmatch                    = PRVM_ED_FindGlobalOffset("deathmatch");
1694         prog->globaloffsets.dmg_origin                    = PRVM_ED_FindGlobalOffset("dmg_origin");
1695         prog->globaloffsets.dmg_save                      = PRVM_ED_FindGlobalOffset("dmg_save");
1696         prog->globaloffsets.dmg_take                      = PRVM_ED_FindGlobalOffset("dmg_take");
1697         prog->globaloffsets.drawfont                      = PRVM_ED_FindGlobalOffset("drawfont");
1698         prog->globaloffsets.drawfontscale                 = PRVM_ED_FindGlobalOffset("drawfontscale");
1699         prog->globaloffsets.gettaginfo_forward            = PRVM_ED_FindGlobalOffset("gettaginfo_forward");
1700         prog->globaloffsets.gettaginfo_name               = PRVM_ED_FindGlobalOffset("gettaginfo_name");
1701         prog->globaloffsets.gettaginfo_offset             = PRVM_ED_FindGlobalOffset("gettaginfo_offset");
1702         prog->globaloffsets.gettaginfo_parent             = PRVM_ED_FindGlobalOffset("gettaginfo_parent");
1703         prog->globaloffsets.gettaginfo_right              = PRVM_ED_FindGlobalOffset("gettaginfo_right");
1704         prog->globaloffsets.gettaginfo_up                 = PRVM_ED_FindGlobalOffset("gettaginfo_up");
1705         prog->globaloffsets.transparent_offset            = PRVM_ED_FindGlobalOffset("transparent_offset");
1706         prog->globaloffsets.intermission                  = PRVM_ED_FindGlobalOffset("intermission");
1707         prog->globaloffsets.require_spawnfunc_prefix      = PRVM_ED_FindGlobalOffset("require_spawnfunc_prefix");
1708         prog->globaloffsets.sb_showscores                 = PRVM_ED_FindGlobalOffset("sb_showscores");
1709         prog->globaloffsets.self                          = PRVM_ED_FindGlobalOffset("self");
1710         prog->globaloffsets.serverdeltatime               = PRVM_ED_FindGlobalOffset("serverdeltatime");
1711         prog->globaloffsets.serverprevtime                = PRVM_ED_FindGlobalOffset("serverprevtime");
1712         prog->globaloffsets.servertime                    = PRVM_ED_FindGlobalOffset("servertime");
1713         prog->globaloffsets.time                          = PRVM_ED_FindGlobalOffset("time");
1714         prog->globaloffsets.trace_allsolid                = PRVM_ED_FindGlobalOffset("trace_allsolid");
1715         prog->globaloffsets.trace_dphitcontents           = PRVM_ED_FindGlobalOffset("trace_dphitcontents");
1716         prog->globaloffsets.trace_dphitq3surfaceflags     = PRVM_ED_FindGlobalOffset("trace_dphitq3surfaceflags");
1717         prog->globaloffsets.trace_dphittexturename        = PRVM_ED_FindGlobalOffset("trace_dphittexturename");
1718         prog->globaloffsets.trace_dpstartcontents         = PRVM_ED_FindGlobalOffset("trace_dpstartcontents");
1719         prog->globaloffsets.trace_endpos                  = PRVM_ED_FindGlobalOffset("trace_endpos");
1720         prog->globaloffsets.trace_ent                     = PRVM_ED_FindGlobalOffset("trace_ent");
1721         prog->globaloffsets.trace_fraction                = PRVM_ED_FindGlobalOffset("trace_fraction");
1722         prog->globaloffsets.trace_inopen                  = PRVM_ED_FindGlobalOffset("trace_inopen");
1723         prog->globaloffsets.trace_inwater                 = PRVM_ED_FindGlobalOffset("trace_inwater");
1724         prog->globaloffsets.trace_networkentity           = PRVM_ED_FindGlobalOffset("trace_networkentity");
1725         prog->globaloffsets.trace_plane_dist              = PRVM_ED_FindGlobalOffset("trace_plane_dist");
1726         prog->globaloffsets.trace_plane_normal            = PRVM_ED_FindGlobalOffset("trace_plane_normal");
1727         prog->globaloffsets.trace_startsolid              = PRVM_ED_FindGlobalOffset("trace_startsolid");
1728         prog->globaloffsets.v_forward                     = PRVM_ED_FindGlobalOffset("v_forward");
1729         prog->globaloffsets.v_right                       = PRVM_ED_FindGlobalOffset("v_right");
1730         prog->globaloffsets.v_up                          = PRVM_ED_FindGlobalOffset("v_up");
1731         prog->globaloffsets.view_angles                   = PRVM_ED_FindGlobalOffset("view_angles");
1732         prog->globaloffsets.worldstatus                   = PRVM_ED_FindGlobalOffset("worldstatus");
1733         prog->globaloffsets.particles_alphamin            = PRVM_ED_FindGlobalOffset("particles_alphamin");
1734         prog->globaloffsets.particles_alphamax            = PRVM_ED_FindGlobalOffset("particles_alphamax");
1735         prog->globaloffsets.particles_colormin            = PRVM_ED_FindGlobalOffset("particles_colormin");
1736         prog->globaloffsets.particles_colormax            = PRVM_ED_FindGlobalOffset("particles_colormax");
1737
1738         // menu qc only uses some functions, nothing else
1739         prog->funcoffsets.m_draw                          = PRVM_ED_FindFunctionOffset("m_draw");
1740         prog->funcoffsets.m_init                          = PRVM_ED_FindFunctionOffset("m_init");
1741         prog->funcoffsets.m_keydown                       = PRVM_ED_FindFunctionOffset("m_keydown");
1742         prog->funcoffsets.m_keyup                         = PRVM_ED_FindFunctionOffset("m_keyup");
1743         prog->funcoffsets.m_shutdown                      = PRVM_ED_FindFunctionOffset("m_shutdown");
1744         prog->funcoffsets.m_toggle                        = PRVM_ED_FindFunctionOffset("m_toggle");
1745 }
1746
1747 // not used
1748 /*
1749 typedef struct dpfield_s
1750 {
1751         int type;
1752         char *string;
1753 }
1754 dpfield_t;
1755
1756 #define DPFIELDS (sizeof(dpfields) / sizeof(dpfield_t))
1757
1758 dpfield_t dpfields[] =
1759 {
1760 };
1761 */
1762
1763 /*
1764 ===============
1765 PRVM_ResetProg
1766 ===============
1767 */
1768
1769 #define PO_HASHSIZE 16384
1770 typedef struct po_string_s
1771 {
1772         char *key, *value;
1773         struct po_string_s *nextonhashchain;
1774 }
1775 po_string_t;
1776 typedef struct po_s
1777 {
1778         po_string_t *hashtable[PO_HASHSIZE];
1779 }
1780 po_t;
1781 void PRVM_PO_UnparseString(char *out, const char *in, size_t outsize)
1782 {
1783         for(;;)
1784         {
1785                 switch(*in)
1786                 {
1787                         case 0:
1788                                 *out++ = 0;
1789                                 return;
1790                         case '\a': if(outsize >= 2) { *out++ = '\\'; *out++ = 'a'; outsize -= 2; } break;
1791                         case '\b': if(outsize >= 2) { *out++ = '\\'; *out++ = 'b'; outsize -= 2; } break;
1792                         case '\t': if(outsize >= 2) { *out++ = '\\'; *out++ = 't'; outsize -= 2; } break;
1793                         case '\r': if(outsize >= 2) { *out++ = '\\'; *out++ = 'r'; outsize -= 2; } break;
1794                         case '\n': if(outsize >= 2) { *out++ = '\\'; *out++ = 'n'; outsize -= 2; } break;
1795                         case '\\': if(outsize >= 2) { *out++ = '\\'; *out++ = '\\'; outsize -= 2; } break;
1796                         case '"': if(outsize >= 2) { *out++ = '\\'; *out++ = '"'; outsize -= 2; } break;
1797                         default:
1798                                 if(*in >= 0 && *in <= 0x1F)
1799                                 {
1800                                         if(outsize >= 4)
1801                                         {
1802                                                 *out++ = '\\';
1803                                                 *out++ = '0' + ((*in & 0700) >> 6);
1804                                                 *out++ = '0' + ((*in & 0070) >> 3);
1805                                                 *out++ = '0' + ((*in & 0007));
1806                                                 outsize -= 4;
1807                                         }
1808                                 }
1809                                 else
1810                                 {
1811                                         if(outsize >= 1)
1812                                         {
1813                                                 *out++ = *in;
1814                                                 outsize -= 1;
1815                                         }
1816                                 }
1817                                 break;
1818                 }
1819                 ++in;
1820         }
1821 }
1822 void PRVM_PO_ParseString(char *out, const char *in, size_t outsize)
1823 {
1824         for(;;)
1825         {
1826                 switch(*in)
1827                 {
1828                         case 0:
1829                                 *out++ = 0;
1830                                 return;
1831                         case '\\':
1832                                 ++in;
1833                                 switch(*in)
1834                                 {
1835                                         case 'a': if(outsize > 0) { *out++ = '\a'; --outsize; } break;
1836                                         case 'b': if(outsize > 0) { *out++ = '\b'; --outsize; } break;
1837                                         case 't': if(outsize > 0) { *out++ = '\t'; --outsize; } break;
1838                                         case 'r': if(outsize > 0) { *out++ = '\r'; --outsize; } break;
1839                                         case 'n': if(outsize > 0) { *out++ = '\n'; --outsize; } break;
1840                                         case '\\': if(outsize > 0) { *out++ = '\\'; --outsize; } break;
1841                                         case '"': if(outsize > 0) { *out++ = '"'; --outsize; } break;
1842                                         case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7':
1843                                                 if(outsize > 0) 
1844                                                         *out = *in - '0';
1845                                                 ++in;
1846                                                 if(*in >= '0' && *in <= '7')
1847                                                 {
1848                                                         if(outsize > 0)
1849                                                                 *out = (*out << 3) | (*in - '0');
1850                                                         ++in;
1851                                                 }
1852                                                 if(*in >= '0' && *in <= '7')
1853                                                 {
1854                                                         if(outsize > 0)
1855                                                                 *out = (*out << 3) | (*in - '0');
1856                                                         ++in;
1857                                                 }
1858                                                 --in;
1859                                                 if(outsize > 0)
1860                                                 {
1861                                                         ++out;
1862                                                         --outsize;
1863                                                 }
1864                                                 break;
1865                                         default:
1866                                                 if(outsize > 0) { *out++ = *in; --outsize; }
1867                                                 break;
1868                                 }
1869                                 break;
1870                         default:
1871                                 if(outsize > 0)
1872                                 {
1873                                         *out++ = *in;
1874                                         --outsize;
1875                                 }
1876                                 break;
1877                 }
1878                 ++in;
1879         }
1880 }
1881 po_t *PRVM_PO_Load(const char *filename, mempool_t *pool)
1882 {
1883         po_t *po;
1884         const char *p, *q;
1885         int mode;
1886         char inbuf[MAX_INPUTLINE];
1887         char decodedbuf[MAX_INPUTLINE];
1888         size_t decodedpos;
1889         int hashindex;
1890         po_string_t thisstr;
1891         const char *buf = (const char *) FS_LoadFile(filename, pool, true, NULL);
1892
1893         if(!buf)
1894                 return NULL;
1895
1896         memset(&thisstr, 0, sizeof(thisstr)); // hush compiler warning
1897
1898         po = (po_t *)Mem_Alloc(pool, sizeof(*po));
1899         memset(po, 0, sizeof(*po));
1900
1901         p = buf;
1902         while(*p)
1903         {
1904                 if(*p == '#')
1905                 {
1906                         // skip to newline
1907                         p = strchr(p, '\n');
1908                         if(!p)
1909                                 break;
1910                         ++p;
1911                         continue;
1912                 }
1913                 if(*p == '\r' || *p == '\n')
1914                 {
1915                         ++p;
1916                         continue;
1917                 }
1918                 if(!strncmp(p, "msgid \"", 7))
1919                 {
1920                         mode = 0;
1921                         p += 6;
1922                 }
1923                 else if(!strncmp(p, "msgstr \"", 8))
1924                 {
1925                         mode = 1;
1926                         p += 7;
1927                 }
1928                 else
1929                 {
1930                         p = strchr(p, '\n');
1931                         if(!p)
1932                                 break;
1933                         ++p;
1934                         continue;
1935                 }
1936                 decodedpos = 0;
1937                 while(*p == '"')
1938                 {
1939                         ++p;
1940                         q = strchr(p, '\n');
1941                         if(!q)
1942                                 break;
1943                         if(*(q-1) == '\r')
1944                                 --q;
1945                         if(*(q-1) != '"')
1946                                 break;
1947                         if((size_t)(q - p) >= (size_t) sizeof(inbuf))
1948                                 break;
1949                         strlcpy(inbuf, p, q - p); // not - 1, because this adds a NUL
1950                         PRVM_PO_ParseString(decodedbuf + decodedpos, inbuf, sizeof(decodedbuf) - decodedpos);
1951                         decodedpos += strlen(decodedbuf + decodedpos);
1952                         if(*q == '\r')
1953                                 ++q;
1954                         if(*q == '\n')
1955                                 ++q;
1956                         p = q;
1957                 }
1958                 if(mode == 0)
1959                 {
1960                         if(thisstr.key)
1961                                 Mem_Free(thisstr.key);
1962                         thisstr.key = (char *)Mem_Alloc(pool, decodedpos + 1);
1963                         memcpy(thisstr.key, decodedbuf, decodedpos + 1);
1964                 }
1965                 else if(decodedpos > 0 && thisstr.key) // skip empty translation results
1966                 {
1967                         thisstr.value = (char *)Mem_Alloc(pool, decodedpos + 1);
1968                         memcpy(thisstr.value, decodedbuf, decodedpos + 1);
1969                         hashindex = CRC_Block((const unsigned char *) thisstr.key, strlen(thisstr.key)) % PO_HASHSIZE;
1970                         thisstr.nextonhashchain = po->hashtable[hashindex];
1971                         po->hashtable[hashindex] = (po_string_t *)Mem_Alloc(pool, sizeof(thisstr));
1972                         memcpy(po->hashtable[hashindex], &thisstr, sizeof(thisstr));
1973                         memset(&thisstr, 0, sizeof(thisstr));
1974                 }
1975         }
1976         
1977         Mem_Free((char *) buf);
1978         return po;
1979 }
1980 const char *PRVM_PO_Lookup(po_t *po, const char *str)
1981 {
1982         int hashindex = CRC_Block((const unsigned char *) str, strlen(str)) % PO_HASHSIZE;
1983         po_string_t *p = po->hashtable[hashindex];
1984         while(p)
1985         {
1986                 if(!strcmp(str, p->key))
1987                         return p->value;
1988                 p = p->nextonhashchain;
1989         }
1990         return NULL;
1991 }
1992 void PRVM_PO_Destroy(po_t *po)
1993 {
1994         int i;
1995         for(i = 0; i < PO_HASHSIZE; ++i)
1996         {
1997                 po_string_t *p = po->hashtable[i];
1998                 while(p)
1999                 {
2000                         po_string_t *q = p;
2001                         p = p->nextonhashchain;
2002                         Mem_Free(q->key);
2003                         Mem_Free(q->value);
2004                         Mem_Free(q);
2005                 }
2006         }
2007         Mem_Free(po);
2008 }
2009
2010 void PRVM_LeakTest(void);
2011 void PRVM_ResetProg(void)
2012 {
2013         PRVM_LeakTest();
2014         PRVM_GCALL(reset_cmd)();
2015         Mem_FreePool(&prog->progs_mempool);
2016         if(prog->po)
2017                 PRVM_PO_Destroy((po_t *) prog->po);
2018         memset(prog,0,sizeof(prvm_prog_t));
2019         prog->starttime = Sys_DoubleTime();
2020 }
2021
2022 /*
2023 ===============
2024 PRVM_LoadLNO
2025 ===============
2026 */
2027 void PRVM_LoadLNO( const char *progname ) {
2028         fs_offset_t filesize;
2029         unsigned char *lno;
2030         unsigned int *header;
2031         char filename[512];
2032
2033         FS_StripExtension( progname, filename, sizeof( filename ) );
2034         strlcat( filename, ".lno", sizeof( filename ) );
2035
2036         lno = FS_LoadFile( filename, tempmempool, false, &filesize );
2037         if( !lno ) {
2038                 return;
2039         }
2040
2041 /*
2042 <Spike>    SafeWrite (h, &lnotype, sizeof(int));
2043 <Spike>    SafeWrite (h, &version, sizeof(int));
2044 <Spike>    SafeWrite (h, &numglobaldefs, sizeof(int));
2045 <Spike>    SafeWrite (h, &numpr_globals, sizeof(int));
2046 <Spike>    SafeWrite (h, &numfielddefs, sizeof(int));
2047 <Spike>    SafeWrite (h, &numstatements, sizeof(int));
2048 <Spike>    SafeWrite (h, statement_linenums, numstatements*sizeof(int));
2049 */
2050         if( (unsigned) filesize < (6 + prog->progs->numstatements) * sizeof( int ) ) {
2051                 Mem_Free(lno);
2052                 return;
2053         }
2054
2055         header = (unsigned int *) lno;
2056         if( header[ 0 ] == *(unsigned int *) "LNOF" &&
2057                 LittleLong( header[ 1 ] ) == 1 &&
2058                 (unsigned int)LittleLong( header[ 2 ] ) == (unsigned int)prog->progs->numglobaldefs &&
2059                 (unsigned int)LittleLong( header[ 3 ] ) == (unsigned int)prog->progs->numglobals &&
2060                 (unsigned int)LittleLong( header[ 4 ] ) == (unsigned int)prog->progs->numfielddefs &&
2061                 (unsigned int)LittleLong( header[ 5 ] ) == (unsigned int)prog->progs->numstatements )
2062         {
2063                 prog->statement_linenums = (int *)Mem_Alloc(prog->progs_mempool, prog->progs->numstatements * sizeof( int ) );
2064                 memcpy( prog->statement_linenums, (int *) lno + 6, prog->progs->numstatements * sizeof( int ) );
2065         }
2066         Mem_Free( lno );
2067 }
2068
2069 /*
2070 ===============
2071 PRVM_LoadProgs
2072 ===============
2073 */
2074 void PRVM_LoadProgs (const char * filename, int numrequiredfunc, char **required_func, int numrequiredfields, prvm_required_field_t *required_field, int numrequiredglobals, char **required_global)
2075 {
2076         int i;
2077         dstatement_t *st;
2078         ddef_t *infielddefs;
2079         dfunction_t *dfunctions;
2080         fs_offset_t filesize;
2081
2082         if( prog->loaded ) {
2083                 PRVM_ERROR ("PRVM_LoadProgs: there is already a %s program loaded!", PRVM_NAME );
2084         }
2085
2086         prog->progs = (dprograms_t *)FS_LoadFile (filename, prog->progs_mempool, false, &filesize);
2087         if (prog->progs == NULL || filesize < (fs_offset_t)sizeof(dprograms_t))
2088                 PRVM_ERROR ("PRVM_LoadProgs: couldn't load %s for %s", filename, PRVM_NAME);
2089         // TODO bounds check header fields (e.g. numstatements), they must never go behind end of file
2090
2091         Con_DPrintf("%s programs occupy %iK.\n", PRVM_NAME, (int)(filesize/1024));
2092
2093         prog->filecrc = CRC_Block((unsigned char *)prog->progs, filesize);
2094
2095 // byte swap the header
2096         for (i = 0;i < (int) sizeof(*prog->progs) / 4;i++)
2097                 ((int *)prog->progs)[i] = LittleLong ( ((int *)prog->progs)[i] );
2098
2099         if (prog->progs->version != PROG_VERSION)
2100                 PRVM_ERROR ("%s: %s has wrong version number (%i should be %i)", PRVM_NAME, filename, prog->progs->version, PROG_VERSION);
2101         if (prog->progs->crc != prog->headercrc && prog->progs->crc != prog->headercrc2)
2102                 PRVM_ERROR ("%s: %s system vars have been modified (CRC of progs.dat systemvars %i != engine %i), progdefs.h is out of date", PRVM_NAME, filename, prog->progs->crc, prog->headercrc);
2103
2104         //prog->functions = (dfunction_t *)((unsigned char *)progs + progs->ofs_functions);
2105         dfunctions = (dfunction_t *)((unsigned char *)prog->progs + prog->progs->ofs_functions);
2106
2107         if (prog->progs->ofs_strings + prog->progs->numstrings >= (int)filesize)
2108                 PRVM_ERROR ("%s: %s strings go past end of file", PRVM_NAME, filename);
2109         prog->strings = (char *)prog->progs + prog->progs->ofs_strings;
2110         prog->stringssize = prog->progs->numstrings;
2111
2112         prog->numknownstrings = 0;
2113         prog->maxknownstrings = 0;
2114         prog->knownstrings = NULL;
2115         prog->knownstrings_freeable = NULL;
2116
2117         Mem_ExpandableArray_NewArray(&prog->stringbuffersarray, prog->progs_mempool, sizeof(prvm_stringbuffer_t), 64);
2118
2119         prog->globaldefs = (ddef_t *)((unsigned char *)prog->progs + prog->progs->ofs_globaldefs);
2120
2121         // we need to expand the fielddefs list to include all the engine fields,
2122         // so allocate a new place for it
2123         infielddefs = (ddef_t *)((unsigned char *)prog->progs + prog->progs->ofs_fielddefs);
2124         //                                                                                              ( + DPFIELDS                       )
2125         prog->fielddefs = (ddef_t *)Mem_Alloc(prog->progs_mempool, (prog->progs->numfielddefs + numrequiredfields) * sizeof(ddef_t));
2126
2127         prog->statements = (dstatement_t *)((unsigned char *)prog->progs + prog->progs->ofs_statements);
2128
2129         prog->statement_profile = (double *)Mem_Alloc(prog->progs_mempool, prog->progs->numstatements * sizeof(*prog->statement_profile));
2130
2131         //pr_global_struct = (globalvars_t *)((unsigned char *)progs + progs->ofs_globals);
2132         prog->globals.generic = (float *)((unsigned char *)prog->progs + prog->progs->ofs_globals);
2133
2134 // byte swap the lumps
2135         for (i=0 ; i<prog->progs->numstatements ; i++)
2136         {
2137                 prog->statements[i].op = LittleShort(prog->statements[i].op);
2138                 prog->statements[i].a = LittleShort(prog->statements[i].a);
2139                 prog->statements[i].b = LittleShort(prog->statements[i].b);
2140                 prog->statements[i].c = LittleShort(prog->statements[i].c);
2141         }
2142
2143         prog->functions = (mfunction_t *)Mem_Alloc(prog->progs_mempool, sizeof(mfunction_t) * prog->progs->numfunctions);
2144         for (i = 0;i < prog->progs->numfunctions;i++)
2145         {
2146                 prog->functions[i].first_statement = LittleLong (dfunctions[i].first_statement);
2147                 prog->functions[i].parm_start = LittleLong (dfunctions[i].parm_start);
2148                 prog->functions[i].s_name = LittleLong (dfunctions[i].s_name);
2149                 prog->functions[i].s_file = LittleLong (dfunctions[i].s_file);
2150                 prog->functions[i].numparms = LittleLong (dfunctions[i].numparms);
2151                 prog->functions[i].locals = LittleLong (dfunctions[i].locals);
2152                 memcpy(prog->functions[i].parm_size, dfunctions[i].parm_size, sizeof(dfunctions[i].parm_size));
2153                 if(prog->functions[i].first_statement >= prog->progs->numstatements)
2154                         PRVM_ERROR("PRVM_LoadProgs: out of bounds function statement (function %d) in %s", i, PRVM_NAME);
2155                 // TODO bounds check parm_start, s_name, s_file, numparms, locals, parm_size
2156         }
2157
2158         for (i=0 ; i<prog->progs->numglobaldefs ; i++)
2159         {
2160                 prog->globaldefs[i].type = LittleShort (prog->globaldefs[i].type);
2161                 prog->globaldefs[i].ofs = LittleShort (prog->globaldefs[i].ofs);
2162                 prog->globaldefs[i].s_name = LittleLong (prog->globaldefs[i].s_name);
2163                 // TODO bounds check ofs, s_name
2164         }
2165
2166         // copy the progs fields to the new fields list
2167         for (i = 0;i < prog->progs->numfielddefs;i++)
2168         {
2169                 prog->fielddefs[i].type = LittleShort (infielddefs[i].type);
2170                 if (prog->fielddefs[i].type & DEF_SAVEGLOBAL)
2171                         PRVM_ERROR ("PRVM_LoadProgs: prog->fielddefs[i].type & DEF_SAVEGLOBAL in %s", PRVM_NAME);
2172                 prog->fielddefs[i].ofs = LittleShort (infielddefs[i].ofs);
2173                 prog->fielddefs[i].s_name = LittleLong (infielddefs[i].s_name);
2174                 // TODO bounds check ofs, s_name
2175         }
2176
2177         // append the required fields
2178         for (i = 0;i < (int) numrequiredfields;i++)
2179         {
2180                 prog->fielddefs[prog->progs->numfielddefs].type = required_field[i].type;
2181                 prog->fielddefs[prog->progs->numfielddefs].ofs = prog->progs->entityfields;
2182                 prog->fielddefs[prog->progs->numfielddefs].s_name = PRVM_SetEngineString(required_field[i].name);
2183                 // TODO bounds check ofs, s_name
2184                 if (prog->fielddefs[prog->progs->numfielddefs].type == ev_vector)
2185                         prog->progs->entityfields += 3;
2186                 else
2187                         prog->progs->entityfields++;
2188                 prog->progs->numfielddefs++;
2189         }
2190         prog->entityfields = prog->progs->entityfields;
2191
2192         // check required functions
2193         for(i=0 ; i < numrequiredfunc ; i++)
2194                 if(PRVM_ED_FindFunction(required_func[i]) == 0)
2195                         PRVM_ERROR("%s: %s not found in %s",PRVM_NAME, required_func[i], filename);
2196
2197         // check required globals
2198         for(i=0 ; i < numrequiredglobals ; i++)
2199                 if(PRVM_ED_FindGlobal(required_global[i]) == 0)
2200                         PRVM_ERROR("%s: %s not found in %s",PRVM_NAME, required_global[i], filename);
2201
2202         for (i=0 ; i<prog->progs->numglobals ; i++)
2203                 ((int *)prog->globals.generic)[i] = LittleLong (((int *)prog->globals.generic)[i]);
2204
2205         // LordHavoc: bounds check anything static
2206         for (i = 0,st = prog->statements;i < prog->progs->numstatements;i++,st++)
2207         {
2208                 switch (st->op)
2209                 {
2210                 case OP_IF:
2211                 case OP_IFNOT:
2212                         if ((unsigned short) st->a >= prog->progs->numglobals || st->b + i < 0 || st->b + i >= prog->progs->numstatements)
2213                                 PRVM_ERROR("PRVM_LoadProgs: out of bounds IF/IFNOT (statement %d) in %s", i, PRVM_NAME);
2214                         break;
2215                 case OP_GOTO:
2216                         if (st->a + i < 0 || st->a + i >= prog->progs->numstatements)
2217                                 PRVM_ERROR("PRVM_LoadProgs: out of bounds GOTO (statement %d) in %s", i, PRVM_NAME);
2218                         break;
2219                 // global global global
2220                 case OP_ADD_F:
2221                 case OP_ADD_V:
2222                 case OP_SUB_F:
2223                 case OP_SUB_V:
2224                 case OP_MUL_F:
2225                 case OP_MUL_V:
2226                 case OP_MUL_FV:
2227                 case OP_MUL_VF:
2228                 case OP_DIV_F:
2229                 case OP_BITAND:
2230                 case OP_BITOR:
2231                 case OP_GE:
2232                 case OP_LE:
2233                 case OP_GT:
2234                 case OP_LT:
2235                 case OP_AND:
2236                 case OP_OR:
2237                 case OP_EQ_F:
2238                 case OP_EQ_V:
2239                 case OP_EQ_S:
2240                 case OP_EQ_E:
2241                 case OP_EQ_FNC:
2242                 case OP_NE_F:
2243                 case OP_NE_V:
2244                 case OP_NE_S:
2245                 case OP_NE_E:
2246                 case OP_NE_FNC:
2247                 case OP_ADDRESS:
2248                 case OP_LOAD_F:
2249                 case OP_LOAD_FLD:
2250                 case OP_LOAD_ENT:
2251                 case OP_LOAD_S:
2252                 case OP_LOAD_FNC:
2253                 case OP_LOAD_V:
2254                         if ((unsigned short) st->a >= prog->progs->numglobals || (unsigned short) st->b >= prog->progs->numglobals || (unsigned short) st->c >= prog->progs->numglobals)
2255                                 PRVM_ERROR("PRVM_LoadProgs: out of bounds global index (statement %d)", i);
2256                         break;
2257                 // global none global
2258                 case OP_NOT_F:
2259                 case OP_NOT_V:
2260                 case OP_NOT_S:
2261                 case OP_NOT_FNC:
2262                 case OP_NOT_ENT:
2263                         if ((unsigned short) st->a >= prog->progs->numglobals || (unsigned short) st->c >= prog->progs->numglobals)
2264                                 PRVM_ERROR("PRVM_LoadProgs: out of bounds global index (statement %d) in %s", i, PRVM_NAME);
2265                         break;
2266                 // 2 globals
2267                 case OP_STOREP_F:
2268                 case OP_STOREP_ENT:
2269                 case OP_STOREP_FLD:
2270                 case OP_STOREP_S:
2271                 case OP_STOREP_FNC:
2272                 case OP_STORE_F:
2273                 case OP_STORE_ENT:
2274                 case OP_STORE_FLD:
2275                 case OP_STORE_S:
2276                 case OP_STORE_FNC:
2277                 case OP_STATE:
2278                 case OP_STOREP_V:
2279                 case OP_STORE_V:
2280                         if ((unsigned short) st->a >= prog->progs->numglobals || (unsigned short) st->b >= prog->progs->numglobals)
2281                                 PRVM_ERROR("PRVM_LoadProgs: out of bounds global index (statement %d) in %s", i, PRVM_NAME);
2282                         break;
2283                 // 1 global
2284                 case OP_CALL0:
2285                 case OP_CALL1:
2286                 case OP_CALL2:
2287                 case OP_CALL3:
2288                 case OP_CALL4:
2289                 case OP_CALL5:
2290                 case OP_CALL6:
2291                 case OP_CALL7:
2292                 case OP_CALL8:
2293                 case OP_DONE:
2294                 case OP_RETURN:
2295                         if ((unsigned short) st->a >= prog->progs->numglobals)
2296                                 PRVM_ERROR("PRVM_LoadProgs: out of bounds global index (statement %d) in %s", i, PRVM_NAME);
2297                         break;
2298                 default:
2299                         Con_DPrintf("PRVM_LoadProgs: unknown opcode %d at statement %d in %s\n", st->op, i, PRVM_NAME);
2300                         break;
2301                 }
2302         }
2303         if(prog->progs->numstatements < 1)
2304         {
2305                 PRVM_ERROR("PRVM_LoadProgs: empty program in %s", PRVM_NAME);
2306         }
2307         else switch(prog->statements[prog->progs->numstatements - 1].op)
2308         {
2309                 case OP_RETURN:
2310                 case OP_GOTO:
2311                 case OP_DONE:
2312                         break;
2313                 default:
2314                         PRVM_ERROR("PRVM_LoadProgs: program may fall off the edge (does not end with RETURN, GOTO or DONE) in %s", PRVM_NAME);
2315                         break;
2316         }
2317
2318         PRVM_LoadLNO(filename);
2319
2320         PRVM_Init_Exec();
2321
2322         if(*prvm_language.string)
2323         // in CSQC we really shouldn't be able to change how stuff works... sorry for now
2324         // later idea: include a list of authorized .po file checksums with the csprogs
2325         {
2326                 qboolean deftrans = !!strcmp(PRVM_NAME, "client");
2327                 if(deftrans) // once we have dotranslate_ strings, ALWAYS use the opt-in method!
2328                 {
2329                         for (i=0 ; i<prog->progs->numglobaldefs ; i++)
2330                         {
2331                                 const char *name;
2332                                 name = PRVM_GetString(prog->globaldefs[i].s_name);
2333                                 if((prog->globaldefs[i].type & ~DEF_SAVEGLOBAL) == ev_string)
2334                                 if(name && !strncmp(name, "dotranslate_", 12))
2335                                 {
2336                                         deftrans = false;
2337                                         break;
2338                                 }
2339                         }
2340                 }
2341                 if(!strcmp(prvm_language.string, "dump"))
2342                 {
2343                         qfile_t *f = FS_OpenRealFile(va("%s.%s.po", filename, prvm_language.string), "w", false);
2344                         Con_Printf("Dumping to %s.%s.po\n", filename, prvm_language.string);
2345                         if(f)
2346                         {
2347                                 for (i=0 ; i<prog->progs->numglobaldefs ; i++)
2348                                 {
2349                                         const char *name;
2350                                         name = PRVM_GetString(prog->globaldefs[i].s_name);
2351                                         if((prog->globaldefs[i].type & ~DEF_SAVEGLOBAL) == ev_string)
2352                                         if(deftrans ? (!name || strncmp(name, "notranslate_", 12)) : (name && !strncmp(name, "dotranslate_", 12)))
2353                                         {
2354                                                 prvm_eval_t *val = (prvm_eval_t *)(prog->globals.generic + prog->globaldefs[i].ofs);
2355                                                 const char *value = PRVM_GetString(val->string);
2356                                                 if(*value)
2357                                                 {
2358                                                         char buf[MAX_INPUTLINE];
2359                                                         PRVM_PO_UnparseString(buf, value, sizeof(buf));
2360                                                         FS_Printf(f, "msgid \"%s\"\nmsgstr \"\"\n\n", buf);
2361                                                 }
2362                                         }
2363                                 }
2364                                 FS_Close(f);
2365                         }
2366                 }
2367                 else
2368                 {
2369                         po_t *po = PRVM_PO_Load(va("%s.%s.po", filename, prvm_language.string), prog->progs_mempool);
2370                         if(po)
2371                         {
2372                                 for (i=0 ; i<prog->progs->numglobaldefs ; i++)
2373                                 {
2374                                         const char *name;
2375                                         name = PRVM_GetString(prog->globaldefs[i].s_name);
2376                                         if(deftrans ? (!name || strncmp(name, "notranslate_", 12)) : (name && !strncmp(name, "dotranslate_", 12)))
2377                                         if((prog->globaldefs[i].type & ~DEF_SAVEGLOBAL) == ev_string)
2378                                         {
2379                                                 prvm_eval_t *val = (prvm_eval_t *)(prog->globals.generic + prog->globaldefs[i].ofs);
2380                                                 const char *value = PRVM_GetString(val->string);
2381                                                 if(*value)
2382                                                 {
2383                                                         value = PRVM_PO_Lookup(po, value);
2384                                                         if(value)
2385                                                                 val->string = PRVM_SetEngineString(value);
2386                                                 }
2387                                         }
2388                                 }
2389                         }
2390                 }
2391         }
2392
2393         for (i=0 ; i<prog->progs->numglobaldefs ; i++)
2394         {
2395                 const char *name;
2396                 name = PRVM_GetString(prog->globaldefs[i].s_name);
2397                 //Con_Printf("found var %s\n", name);
2398                 if(name
2399                         && !strncmp(name, "autocvar_", 9)
2400                         && !(strlen(name) > 1 && name[strlen(name)-2] == '_' && (name[strlen(name)-1] == 'x' || name[strlen(name)-1] == 'y' || name[strlen(name)-1] == 'z'))
2401                 )
2402                 {
2403                         prvm_eval_t *val = (prvm_eval_t *)(prog->globals.generic + prog->globaldefs[i].ofs);
2404                         cvar_t *cvar = Cvar_FindVar(name + 9);
2405                         //Con_Printf("PRVM_LoadProgs: autocvar global %s in %s, processing...\n", name, PRVM_NAME);
2406                         if(!cvar)
2407                         {
2408                                 const char *value;
2409                                 char buf[64];
2410                                 Con_Printf("PRVM_LoadProgs: no cvar for autocvar global %s in %s, creating...\n", name, PRVM_NAME);
2411                                 switch(prog->globaldefs[i].type & ~DEF_SAVEGLOBAL)
2412                                 {
2413                                         case ev_float:
2414                                                 if((float)((int)(val->_float)) == val->_float)
2415                                                         dpsnprintf(buf, sizeof(buf), "%i", (int)(val->_float));
2416                                                 else
2417                                                         dpsnprintf(buf, sizeof(buf), "%.9g", val->_float);
2418                                                 value = buf;
2419                                                 break;
2420                                         case ev_vector:
2421                                                 dpsnprintf(buf, sizeof(buf), "%.9g %.9g %.9g", val->vector[0], val->vector[1], val->vector[2]); value = buf;
2422                                                 break;
2423                                         case ev_string:
2424                                                 value = PRVM_GetString(val->string);
2425                                                 break;
2426                                         default:
2427                                                 Con_Printf("PRVM_LoadProgs: invalid type of autocvar global %s in %s\n", name, PRVM_NAME);
2428                                                 goto fail;
2429                                 }
2430                                 cvar = Cvar_Get(name + 9, value, 0, NULL);
2431                                 if((prog->globaldefs[i].type & ~DEF_SAVEGLOBAL) == ev_string)
2432                                 {
2433                                         val->string = PRVM_SetEngineString(cvar->string);
2434                                         cvar->globaldefindex_stringno[prog - prog_list] = val->string;
2435                                 }
2436                                 if(!cvar)
2437                                         PRVM_ERROR("PRVM_LoadProgs: could not create cvar for autocvar global %s in %s", name, PRVM_NAME);
2438                                 cvar->globaldefindex_progid[prog - prog_list] = prog->id;
2439                                 cvar->globaldefindex[prog - prog_list] = i;
2440                         }
2441                         else if((cvar->flags & CVAR_PRIVATE) == 0)
2442                         {
2443                                 // MUST BE SYNCED WITH cvar.c Cvar_Set
2444                                 int j;
2445                                 const char *s;
2446                                 switch(prog->globaldefs[i].type & ~DEF_SAVEGLOBAL)
2447                                 {
2448                                         case ev_float:
2449                                                 val->_float = cvar->value;
2450                                                 break;
2451                                         case ev_vector:
2452                                                 s = cvar->string;
2453                                                 VectorClear(val->vector);
2454                                                 for (j = 0;j < 3;j++)
2455                                                 {
2456                                                         while (*s && ISWHITESPACE(*s))
2457                                                                 s++;
2458                                                         if (!*s)
2459                                                                 break;
2460                                                         val->vector[j] = atof(s);
2461                                                         while (!ISWHITESPACE(*s))
2462                                                                 s++;
2463                                                         if (!*s)
2464                                                                 break;
2465                                                 }
2466                                                 break;
2467                                         case ev_string:
2468                                                 val->string = PRVM_SetEngineString(cvar->string);
2469                                                 cvar->globaldefindex_stringno[prog - prog_list] = val->string;
2470                                                 break;
2471                                         default:
2472                                                 Con_Printf("PRVM_LoadProgs: invalid type of autocvar global %s in %s\n", name, PRVM_NAME);
2473                                                 goto fail;
2474                                 }
2475                                 cvar->globaldefindex_progid[prog - prog_list] = prog->id;
2476                                 cvar->globaldefindex[prog - prog_list] = i;
2477                         }
2478                         else
2479                                 Con_Printf("PRVM_LoadProgs: private cvar for autocvar global %s in %s\n", name, PRVM_NAME);
2480                 }
2481 fail:
2482                 ;
2483         }
2484
2485         prog->loaded = TRUE;
2486
2487         // set flags & ddef_ts in prog
2488
2489         prog->flag = 0;
2490
2491         PRVM_FindOffsets();
2492
2493         PRVM_GCALL(init_cmd)();
2494
2495         // init mempools
2496         PRVM_MEM_Alloc();
2497 }
2498
2499
2500 void PRVM_Fields_f (void)
2501 {
2502         int i, j, ednum, used, usedamount;
2503         int *counts;
2504         char tempstring[MAX_INPUTLINE], tempstring2[260];
2505         const char *name;
2506         prvm_edict_t *ed;
2507         ddef_t *d;
2508         int *v;
2509
2510         // TODO
2511         /*
2512         if (!sv.active)
2513         {
2514                 Con_Print("no progs loaded\n");
2515                 return;
2516         }
2517         */
2518
2519         if(Cmd_Argc() != 2)
2520         {
2521                 Con_Print("prvm_fields <program name>\n");
2522                 return;
2523         }
2524
2525         PRVM_Begin;
2526         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
2527                 return;
2528
2529         counts = (int *)Mem_Alloc(tempmempool, prog->progs->numfielddefs * sizeof(int));
2530         for (ednum = 0;ednum < prog->max_edicts;ednum++)
2531         {
2532                 ed = PRVM_EDICT_NUM(ednum);
2533                 if (ed->priv.required->free)
2534                         continue;
2535                 for (i = 1;i < prog->progs->numfielddefs;i++)
2536                 {
2537                         d = &prog->fielddefs[i];
2538                         name = PRVM_GetString(d->s_name);
2539                         if (name[strlen(name)-2] == '_')
2540                                 continue;       // skip _x, _y, _z vars
2541                         v = (int *)(ed->fields.vp + d->ofs);
2542                         // if the value is still all 0, skip the field
2543                         for (j = 0;j < prvm_type_size[d->type & ~DEF_SAVEGLOBAL];j++)
2544                         {
2545                                 if (v[j])
2546                                 {
2547                                         counts[i]++;
2548                                         break;
2549                                 }
2550                         }
2551                 }
2552         }
2553         used = 0;
2554         usedamount = 0;
2555         tempstring[0] = 0;
2556         for (i = 0;i < prog->progs->numfielddefs;i++)
2557         {
2558                 d = &prog->fielddefs[i];
2559                 name = PRVM_GetString(d->s_name);
2560                 if (name[strlen(name)-2] == '_')
2561                         continue;       // skip _x, _y, _z vars
2562                 switch(d->type & ~DEF_SAVEGLOBAL)
2563                 {
2564                 case ev_string:
2565                         strlcat(tempstring, "string   ", sizeof(tempstring));
2566                         break;
2567                 case ev_entity:
2568                         strlcat(tempstring, "entity   ", sizeof(tempstring));
2569                         break;
2570                 case ev_function:
2571                         strlcat(tempstring, "function ", sizeof(tempstring));
2572                         break;
2573                 case ev_field:
2574                         strlcat(tempstring, "field    ", sizeof(tempstring));
2575                         break;
2576                 case ev_void:
2577                         strlcat(tempstring, "void     ", sizeof(tempstring));
2578                         break;
2579                 case ev_float:
2580                         strlcat(tempstring, "float    ", sizeof(tempstring));
2581                         break;
2582                 case ev_vector:
2583                         strlcat(tempstring, "vector   ", sizeof(tempstring));
2584                         break;
2585                 case ev_pointer:
2586                         strlcat(tempstring, "pointer  ", sizeof(tempstring));
2587                         break;
2588                 default:
2589                         dpsnprintf (tempstring2, sizeof(tempstring2), "bad type %i ", d->type & ~DEF_SAVEGLOBAL);
2590                         strlcat(tempstring, tempstring2, sizeof(tempstring));
2591                         break;
2592                 }
2593                 if (strlen(name) > sizeof(tempstring2)-4)
2594                 {
2595                         memcpy (tempstring2, name, sizeof(tempstring2)-4);
2596                         tempstring2[sizeof(tempstring2)-4] = tempstring2[sizeof(tempstring2)-3] = tempstring2[sizeof(tempstring2)-2] = '.';
2597                         tempstring2[sizeof(tempstring2)-1] = 0;
2598                         name = tempstring2;
2599                 }
2600                 strlcat(tempstring, name, sizeof(tempstring));
2601                 for (j = (int)strlen(name);j < 25;j++)
2602                         strlcat(tempstring, " ", sizeof(tempstring));
2603                 dpsnprintf(tempstring2, sizeof(tempstring2), "%5d", counts[i]);
2604                 strlcat(tempstring, tempstring2, sizeof(tempstring));
2605                 strlcat(tempstring, "\n", sizeof(tempstring));
2606                 if (strlen(tempstring) >= sizeof(tempstring)/2)
2607                 {
2608                         Con_Print(tempstring);
2609                         tempstring[0] = 0;
2610                 }
2611                 if (counts[i])
2612                 {
2613                         used++;
2614                         usedamount += prvm_type_size[d->type & ~DEF_SAVEGLOBAL];
2615                 }
2616         }
2617         Mem_Free(counts);
2618         Con_Printf("%s: %i entity fields (%i in use), totalling %i bytes per edict (%i in use), %i edicts allocated, %i bytes total spent on edict fields (%i needed)\n", PRVM_NAME, prog->progs->entityfields, used, prog->progs->entityfields * 4, usedamount * 4, prog->max_edicts, prog->progs->entityfields * 4 * prog->max_edicts, usedamount * 4 * prog->max_edicts);
2619
2620         PRVM_End;
2621 }
2622
2623 void PRVM_Globals_f (void)
2624 {
2625         int i;
2626         const char *wildcard;
2627         int numculled;
2628                 numculled = 0;
2629         // TODO
2630         /*if (!sv.active)
2631         {
2632                 Con_Print("no progs loaded\n");
2633                 return;
2634         }*/
2635         if(Cmd_Argc () < 2 || Cmd_Argc() > 3)
2636         {
2637                 Con_Print("prvm_globals <program name> <optional name wildcard>\n");
2638                 return;
2639         }
2640
2641         PRVM_Begin;
2642         if(!PRVM_SetProgFromString (Cmd_Argv (1)))
2643                 return;
2644
2645         if( Cmd_Argc() == 3)
2646                 wildcard = Cmd_Argv(2);
2647         else
2648                 wildcard = NULL;
2649
2650         Con_Printf("%s :", PRVM_NAME);
2651
2652         for (i = 0;i < prog->progs->numglobaldefs;i++)
2653         {
2654                 if(wildcard)
2655                         if( !matchpattern( PRVM_GetString(prog->globaldefs[i].s_name), wildcard, 1) )
2656                         {
2657                                 numculled++;
2658                                 continue;
2659                         }
2660                 Con_Printf("%s\n", PRVM_GetString(prog->globaldefs[i].s_name));
2661         }
2662         Con_Printf("%i global variables, %i culled, totalling %i bytes\n", prog->progs->numglobals, numculled, prog->progs->numglobals * 4);
2663
2664         PRVM_End;
2665 }
2666
2667 /*
2668 ===============
2669 PRVM_Global
2670 ===============
2671 */
2672 void PRVM_Global_f(void)
2673 {
2674         ddef_t *global;
2675         if( Cmd_Argc() != 3 ) {
2676                 Con_Printf( "prvm_global <program name> <global name>\n" );
2677                 return;
2678         }
2679
2680         PRVM_Begin;
2681         if( !PRVM_SetProgFromString( Cmd_Argv(1) ) )
2682                 return;
2683
2684         global = PRVM_ED_FindGlobal( Cmd_Argv(2) );
2685         if( !global )
2686                 Con_Printf( "No global '%s' in %s!\n", Cmd_Argv(2), Cmd_Argv(1) );
2687         else
2688                 Con_Printf( "%s: %s\n", Cmd_Argv(2), PRVM_ValueString( (etype_t)global->type, (prvm_eval_t *) &prog->globals.generic[ global->ofs ] ) );
2689         PRVM_End;
2690 }
2691
2692 /*
2693 ===============
2694 PRVM_GlobalSet
2695 ===============
2696 */
2697 void PRVM_GlobalSet_f(void)
2698 {
2699         ddef_t *global;
2700         if( Cmd_Argc() != 4 ) {
2701                 Con_Printf( "prvm_globalset <program name> <global name> <value>\n" );
2702                 return;
2703         }
2704
2705         PRVM_Begin;
2706         if( !PRVM_SetProgFromString( Cmd_Argv(1) ) )
2707                 return;
2708
2709         global = PRVM_ED_FindGlobal( Cmd_Argv(2) );
2710         if( !global )
2711                 Con_Printf( "No global '%s' in %s!\n", Cmd_Argv(2), Cmd_Argv(1) );
2712         else
2713                 PRVM_ED_ParseEpair( NULL, global, Cmd_Argv(3), true );
2714         PRVM_End;
2715 }
2716
2717 /*
2718 ===============
2719 PRVM_Init
2720 ===============
2721 */
2722 void PRVM_Init (void)
2723 {
2724         Cmd_AddCommand ("prvm_edict", PRVM_ED_PrintEdict_f, "print all data about an entity number in the selected VM (server, client, menu)");
2725         Cmd_AddCommand ("prvm_edicts", PRVM_ED_PrintEdicts_f, "prints all data about all entities in the selected VM (server, client, menu)");
2726         Cmd_AddCommand ("prvm_edictcount", PRVM_ED_Count_f, "prints number of active entities in the selected VM (server, client, menu)");
2727         Cmd_AddCommand ("prvm_profile", PRVM_Profile_f, "prints execution statistics about the most used QuakeC functions in the selected VM (server, client, menu)");
2728         Cmd_AddCommand ("prvm_childprofile", PRVM_ChildProfile_f, "prints execution statistics about the most used QuakeC functions in the selected VM (server, client, menu), sorted by time taken in function with child calls");
2729         Cmd_AddCommand ("prvm_callprofile", PRVM_CallProfile_f, "prints execution statistics about the most time consuming QuakeC calls from the engine in the selected VM (server, client, menu)");
2730         Cmd_AddCommand ("prvm_fields", PRVM_Fields_f, "prints usage statistics on properties (how many entities have non-zero values) in the selected VM (server, client, menu)");
2731         Cmd_AddCommand ("prvm_globals", PRVM_Globals_f, "prints all global variables in the selected VM (server, client, menu)");
2732         Cmd_AddCommand ("prvm_global", PRVM_Global_f, "prints value of a specified global variable in the selected VM (server, client, menu)");
2733         Cmd_AddCommand ("prvm_globalset", PRVM_GlobalSet_f, "sets value of a specified global variable in the selected VM (server, client, menu)");
2734         Cmd_AddCommand ("prvm_edictset", PRVM_ED_EdictSet_f, "changes value of a specified property of a specified entity in the selected VM (server, client, menu)");
2735         Cmd_AddCommand ("prvm_edictget", PRVM_ED_EdictGet_f, "retrieves the value of a specified property of a specified entity in the selected VM (server, client menu) into a cvar or to the console");
2736         Cmd_AddCommand ("prvm_globalget", PRVM_ED_GlobalGet_f, "retrieves the value of a specified global variable in the selected VM (server, client menu) into a cvar or to the console");
2737         Cmd_AddCommand ("prvm_printfunction", PRVM_PrintFunction_f, "prints a disassembly (QuakeC instructions) of the specified function in the selected VM (server, client, menu)");
2738         Cmd_AddCommand ("cl_cmd", PRVM_GameCommand_Client_f, "calls the client QC function GameCommand with the supplied string as argument");
2739         Cmd_AddCommand ("menu_cmd", PRVM_GameCommand_Menu_f, "calls the menu QC function GameCommand with the supplied string as argument");
2740         Cmd_AddCommand ("sv_cmd", PRVM_GameCommand_Server_f, "calls the server QC function GameCommand with the supplied string as argument");
2741
2742         Cvar_RegisterVariable (&prvm_language);
2743         Cvar_RegisterVariable (&prvm_traceqc);
2744         Cvar_RegisterVariable (&prvm_statementprofiling);
2745         Cvar_RegisterVariable (&prvm_backtraceforwarnings);
2746         Cvar_RegisterVariable (&prvm_leaktest);
2747         Cvar_RegisterVariable (&prvm_leaktest_ignore_classnames);
2748         Cvar_RegisterVariable (&prvm_errordump);
2749         Cvar_RegisterVariable (&prvm_reuseedicts_startuptime);
2750         Cvar_RegisterVariable (&prvm_reuseedicts_neverinsameframe);
2751
2752         // COMMANDLINEOPTION: PRVM: -norunaway disables the runaway loop check (it might be impossible to exit DarkPlaces if used!)
2753         prvm_runawaycheck = !COM_CheckParm("-norunaway");
2754
2755         //VM_Cmd_Init();
2756 }
2757
2758 /*
2759 ===============
2760 PRVM_InitProg
2761 ===============
2762 */
2763 void PRVM_InitProg(int prognr)
2764 {
2765         static unsigned int progid = 0;
2766
2767         if(prognr < 0 || prognr >= PRVM_MAXPROGS)
2768                 Sys_Error("PRVM_InitProg: Invalid program number %i",prognr);
2769
2770         prog = &prog_list[prognr];
2771
2772         if(prog->loaded)
2773                 PRVM_ResetProg();
2774
2775         memset(prog, 0, sizeof(prvm_prog_t));
2776         prog->starttime = Sys_DoubleTime();
2777         prog->id = ++progid;
2778
2779         prog->error_cmd = Host_Error;
2780         prog->leaktest_active = prvm_leaktest.integer != 0;
2781 }
2782
2783 int PRVM_GetProgNr(void)
2784 {
2785         return prog - prog_list;
2786 }
2787
2788 void *_PRVM_Alloc(size_t buffersize, const char *filename, int fileline)
2789 {
2790         return _Mem_Alloc(prog->progs_mempool, NULL, buffersize, 16, filename, fileline);
2791 }
2792
2793 void _PRVM_Free(void *buffer, const char *filename, int fileline)
2794 {
2795         _Mem_Free(buffer, filename, fileline);
2796 }
2797
2798 void _PRVM_FreeAll(const char *filename, int fileline)
2799 {
2800         prog->progs = NULL;
2801         prog->fielddefs = NULL;
2802         prog->functions = NULL;
2803         _Mem_EmptyPool(prog->progs_mempool, filename, fileline);
2804 }
2805
2806 // LordHavoc: turned PRVM_EDICT_NUM into a #define for speed reasons
2807 unsigned int PRVM_EDICT_NUM_ERROR(unsigned int n, char *filename, int fileline)
2808 {
2809         PRVM_ERROR ("PRVM_EDICT_NUM: %s: bad number %i (called at %s:%i)", PRVM_NAME, n, filename, fileline);
2810         return 0;
2811 }
2812
2813 sizebuf_t vm_tempstringsbuf;
2814 #define PRVM_KNOWNSTRINGBASE 0x40000000
2815
2816 const char *PRVM_GetString(int num)
2817 {
2818         if (num < 0)
2819         {
2820                 // invalid
2821                 VM_Warning("PRVM_GetString: Invalid string offset (%i < 0)\n", num);
2822                 return "";
2823         }
2824         else if (num < prog->stringssize)
2825         {
2826                 // constant string from progs.dat
2827                 return prog->strings + num;
2828         }
2829         else if (num <= prog->stringssize + vm_tempstringsbuf.maxsize)
2830         {
2831                 // tempstring returned by engine to QC (becomes invalid after returning to engine)
2832                 num -= prog->stringssize;
2833                 if (num < vm_tempstringsbuf.cursize)
2834                         return (char *)vm_tempstringsbuf.data + num;
2835                 else
2836                 {
2837                         VM_Warning("PRVM_GetString: Invalid temp-string offset (%i >= %i vm_tempstringsbuf.cursize)\n", num, vm_tempstringsbuf.cursize);
2838                         return "";
2839                 }
2840         }
2841         else if (num & PRVM_KNOWNSTRINGBASE)
2842         {
2843                 // allocated string
2844                 num = num - PRVM_KNOWNSTRINGBASE;
2845                 if (num >= 0 && num < prog->numknownstrings)
2846                 {
2847                         if (!prog->knownstrings[num])
2848                         {
2849                                 VM_Warning("PRVM_GetString: Invalid zone-string offset (%i has been freed)\n", num);
2850                                 return "";
2851                         }
2852                         return prog->knownstrings[num];
2853                 }
2854                 else
2855                 {
2856                         VM_Warning("PRVM_GetString: Invalid zone-string offset (%i >= %i)\n", num, prog->numknownstrings);
2857                         return "";
2858                 }
2859         }
2860         else
2861         {
2862                 // invalid string offset
2863                 VM_Warning("PRVM_GetString: Invalid constant-string offset (%i >= %i prog->stringssize)\n", num, prog->stringssize);
2864                 return "";
2865         }
2866 }
2867
2868 const char *PRVM_ChangeEngineString(int i, const char *s)
2869 {
2870         const char *old;
2871         i = i - PRVM_KNOWNSTRINGBASE;
2872         if(i < 0 || i >= prog->numknownstrings)
2873                 PRVM_ERROR("PRVM_ChangeEngineString: s is not an engine string");
2874         old = prog->knownstrings[i];
2875         prog->knownstrings[i] = s;
2876         return old;
2877 }
2878
2879 int PRVM_SetEngineString(const char *s)
2880 {
2881         int i;
2882         if (!s)
2883                 return 0;
2884         if (s >= prog->strings && s <= prog->strings + prog->stringssize)
2885                 PRVM_ERROR("PRVM_SetEngineString: s in prog->strings area");
2886         // if it's in the tempstrings area, use a reserved range
2887         // (otherwise we'd get millions of useless string offsets cluttering the database)
2888         if (s >= (char *)vm_tempstringsbuf.data && s < (char *)vm_tempstringsbuf.data + vm_tempstringsbuf.maxsize)
2889 #if 1
2890                 return prog->stringssize + (s - (char *)vm_tempstringsbuf.data);
2891 #endif
2892         // see if it's a known string address
2893         for (i = 0;i < prog->numknownstrings;i++)
2894                 if (prog->knownstrings[i] == s)
2895                         return PRVM_KNOWNSTRINGBASE + i;
2896         // new unknown engine string
2897         if (developer_insane.integer)
2898                 Con_DPrintf("new engine string %p = \"%s\"\n", s, s);
2899         for (i = prog->firstfreeknownstring;i < prog->numknownstrings;i++)
2900                 if (!prog->knownstrings[i])
2901                         break;
2902         if (i >= prog->numknownstrings)
2903         {
2904                 if (i >= prog->maxknownstrings)
2905                 {
2906                         const char **oldstrings = prog->knownstrings;
2907                         const unsigned char *oldstrings_freeable = prog->knownstrings_freeable;
2908                         const char **oldstrings_origin = prog->knownstrings_origin;
2909                         prog->maxknownstrings += 128;
2910                         prog->knownstrings = (const char **)PRVM_Alloc(prog->maxknownstrings * sizeof(char *));
2911                         prog->knownstrings_freeable = (unsigned char *)PRVM_Alloc(prog->maxknownstrings * sizeof(unsigned char));
2912                         if(prog->leaktest_active)
2913                                 prog->knownstrings_origin = (const char **)PRVM_Alloc(prog->maxknownstrings * sizeof(char *));
2914                         if (prog->numknownstrings)
2915                         {
2916                                 memcpy((char **)prog->knownstrings, oldstrings, prog->numknownstrings * sizeof(char *));
2917                                 memcpy((char **)prog->knownstrings_freeable, oldstrings_freeable, prog->numknownstrings * sizeof(unsigned char));
2918                                 if(prog->leaktest_active)
2919                                         memcpy((char **)prog->knownstrings_origin, oldstrings_origin, prog->numknownstrings * sizeof(char *));
2920                         }
2921                 }
2922                 prog->numknownstrings++;
2923         }
2924         prog->firstfreeknownstring = i + 1;
2925         prog->knownstrings[i] = s;
2926         prog->knownstrings_freeable[i] = false;
2927         if(prog->leaktest_active)
2928                 prog->knownstrings_origin[i] = NULL;
2929         return PRVM_KNOWNSTRINGBASE + i;
2930 }
2931
2932 // temp string handling
2933
2934 // all tempstrings go into this buffer consecutively, and it is reset
2935 // whenever PRVM_ExecuteProgram returns to the engine
2936 // (technically each PRVM_ExecuteProgram call saves the cursize value and
2937 //  restores it on return, so multiple recursive calls can share the same
2938 //  buffer)
2939 // the buffer size is automatically grown as needed
2940
2941 int PRVM_SetTempString(const char *s)
2942 {
2943         int size;
2944         char *t;
2945         if (!s)
2946                 return 0;
2947         size = (int)strlen(s) + 1;
2948         if (developer_insane.integer)
2949                 Con_DPrintf("PRVM_SetTempString: cursize %i, size %i\n", vm_tempstringsbuf.cursize, size);
2950         if (vm_tempstringsbuf.maxsize < vm_tempstringsbuf.cursize + size)
2951         {
2952                 sizebuf_t old = vm_tempstringsbuf;
2953                 if (vm_tempstringsbuf.cursize + size >= 1<<28)
2954                         PRVM_ERROR("PRVM_SetTempString: ran out of tempstring memory!  (refusing to grow tempstring buffer over 256MB, cursize %i, size %i)\n", vm_tempstringsbuf.cursize, size);
2955                 vm_tempstringsbuf.maxsize = max(vm_tempstringsbuf.maxsize, 65536);
2956                 while (vm_tempstringsbuf.maxsize < vm_tempstringsbuf.cursize + size)
2957                         vm_tempstringsbuf.maxsize *= 2;
2958                 if (vm_tempstringsbuf.maxsize != old.maxsize || vm_tempstringsbuf.data == NULL)
2959                 {
2960                         Con_DPrintf("PRVM_SetTempString: enlarging tempstrings buffer (%iKB -> %iKB)\n", old.maxsize/1024, vm_tempstringsbuf.maxsize/1024);
2961                         vm_tempstringsbuf.data = (unsigned char *) Mem_Alloc(sv_mempool, vm_tempstringsbuf.maxsize);
2962                         if (old.cursize)
2963                                 memcpy(vm_tempstringsbuf.data, old.data, old.cursize);
2964                         if (old.data)
2965                                 Mem_Free(old.data);
2966                 }
2967         }
2968         t = (char *)vm_tempstringsbuf.data + vm_tempstringsbuf.cursize;
2969         memcpy(t, s, size);
2970         vm_tempstringsbuf.cursize += size;
2971         return PRVM_SetEngineString(t);
2972 }
2973
2974 int PRVM_AllocString(size_t bufferlength, char **pointer)
2975 {
2976         int i;
2977         if (!bufferlength)
2978                 return 0;
2979         for (i = prog->firstfreeknownstring;i < prog->numknownstrings;i++)
2980                 if (!prog->knownstrings[i])
2981                         break;
2982         if (i >= prog->numknownstrings)
2983         {
2984                 if (i >= prog->maxknownstrings)
2985                 {
2986                         const char **oldstrings = prog->knownstrings;
2987                         const unsigned char *oldstrings_freeable = prog->knownstrings_freeable;
2988                         const char **oldstrings_origin = prog->knownstrings_origin;
2989                         prog->maxknownstrings += 128;
2990                         prog->knownstrings = (const char **)PRVM_Alloc(prog->maxknownstrings * sizeof(char *));
2991                         prog->knownstrings_freeable = (unsigned char *)PRVM_Alloc(prog->maxknownstrings * sizeof(unsigned char));
2992                         if(prog->leaktest_active)
2993                                 prog->knownstrings_origin = (const char **)PRVM_Alloc(prog->maxknownstrings * sizeof(char *));
2994                         if (prog->numknownstrings)
2995                         {
2996                                 memcpy((char **)prog->knownstrings, oldstrings, prog->numknownstrings * sizeof(char *));
2997                                 memcpy((char **)prog->knownstrings_freeable, oldstrings_freeable, prog->numknownstrings * sizeof(unsigned char));
2998                                 if(prog->leaktest_active)
2999                                         memcpy((char **)prog->knownstrings_origin, oldstrings_origin, prog->numknownstrings * sizeof(char *));
3000                         }
3001                         if (oldstrings)
3002                                 Mem_Free((char **)oldstrings);
3003                         if (oldstrings_freeable)
3004                                 Mem_Free((unsigned char *)oldstrings_freeable);
3005                         if (oldstrings_origin)
3006                                 Mem_Free((char **)oldstrings_origin);
3007                 }
3008                 prog->numknownstrings++;
3009         }
3010         prog->firstfreeknownstring = i + 1;
3011         prog->knownstrings[i] = (char *)PRVM_Alloc(bufferlength);
3012         prog->knownstrings_freeable[i] = true;
3013         if(prog->leaktest_active)
3014                 prog->knownstrings_origin[i] = PRVM_AllocationOrigin();
3015         if (pointer)
3016                 *pointer = (char *)(prog->knownstrings[i]);
3017         return PRVM_KNOWNSTRINGBASE + i;
3018 }
3019
3020 void PRVM_FreeString(int num)
3021 {
3022         if (num == 0)
3023                 PRVM_ERROR("PRVM_FreeString: attempt to free a NULL string");
3024         else if (num >= 0 && num < prog->stringssize)
3025                 PRVM_ERROR("PRVM_FreeString: attempt to free a constant string");
3026         else if (num >= PRVM_KNOWNSTRINGBASE && num < PRVM_KNOWNSTRINGBASE + prog->numknownstrings)
3027         {
3028                 num = num - PRVM_KNOWNSTRINGBASE;
3029                 if (!prog->knownstrings[num])
3030                         PRVM_ERROR("PRVM_FreeString: attempt to free a non-existent or already freed string");
3031                 if (!prog->knownstrings_freeable[num])
3032                         PRVM_ERROR("PRVM_FreeString: attempt to free a string owned by the engine");
3033                 PRVM_Free((char *)prog->knownstrings[num]);
3034                 if(prog->leaktest_active)
3035                         if(prog->knownstrings_origin[num])
3036                                 PRVM_Free((char *)prog->knownstrings_origin[num]);
3037                 prog->knownstrings[num] = NULL;
3038                 prog->knownstrings_freeable[num] = false;
3039                 prog->firstfreeknownstring = min(prog->firstfreeknownstring, num);
3040         }
3041         else
3042                 PRVM_ERROR("PRVM_FreeString: invalid string offset %i", num);
3043 }
3044
3045 static qboolean PRVM_IsStringReferenced(string_t string)
3046 {
3047         int i, j;
3048
3049         for (i = 0;i < prog->progs->numglobaldefs;i++)
3050         {
3051                 ddef_t *d = &prog->globaldefs[i];
3052                 if((etype_t)((int) d->type & ~DEF_SAVEGLOBAL) != ev_string)
3053                         continue;
3054                 if(string == ((prvm_eval_t *) &prog->globals.generic[d->ofs])->string)
3055                         return true;
3056         }
3057
3058         for(j = 0; j < prog->num_edicts; ++j)
3059         {
3060                 prvm_edict_t *ed = PRVM_EDICT_NUM(j);
3061                 if (ed->priv.required->free)
3062                         continue;
3063                 for (i=0; i<prog->progs->numfielddefs; ++i)
3064                 {
3065                         ddef_t *d = &prog->fielddefs[i];
3066                         if((etype_t)((int) d->type & ~DEF_SAVEGLOBAL) != ev_string)
3067                                 continue;
3068                         if(string == ((prvm_eval_t *) &ed->fields.vp[d->ofs])->string)
3069                                 return true;
3070                 }
3071         }
3072
3073         return false;
3074 }
3075
3076 static qboolean PRVM_IsEdictRelevant(prvm_edict_t *edict)
3077 {
3078         if(PRVM_NUM_FOR_EDICT(edict) <= prog->reserved_edicts)
3079                 return true; // world or clients
3080         switch(prog - prog_list)
3081         {
3082                 case PRVM_SERVERPROG:
3083                         {
3084                                 entvars_t *ev = edict->fields.server;
3085                                 if(ev->solid) // can block other stuff, or is a trigger?
3086                                         return true;
3087                                 if(ev->modelindex) // visible ent?
3088                                         return true;
3089                                 if(ev->effects) // particle effect?
3090                                         return true;
3091                                 if(ev->think) // has a think function?
3092                                         if(ev->nextthink > 0) // that actually will eventually run?
3093                                                 return true;
3094                                 if(ev->takedamage)
3095                                         return true;
3096                                 if(*prvm_leaktest_ignore_classnames.string)
3097                                 {
3098                                         if(strstr(va(" %s ", prvm_leaktest_ignore_classnames.string), va(" %s ", PRVM_GetString(ev->classname))))
3099                                                 return true;
3100                                 }
3101                         }
3102                         break;
3103                 case PRVM_CLIENTPROG:
3104                         {
3105                                 // TODO someone add more stuff here
3106                                 cl_entvars_t *ev = edict->fields.client;
3107                                 if(ev->entnum) // csqc networked
3108                                         return true;
3109                                 if(ev->modelindex) // visible ent?
3110                                         return true;
3111                                 if(ev->effects) // particle effect?
3112                                         return true;
3113                                 if(ev->think) // has a think function?
3114                                         if(ev->nextthink > 0) // that actually will eventually run?
3115                                                 return true;
3116                                 if(*prvm_leaktest_ignore_classnames.string)
3117                                 {
3118                                         if(strstr(va(" %s ", prvm_leaktest_ignore_classnames.string), va(" %s ", PRVM_GetString(ev->classname))))
3119                                                 return true;
3120                                 }
3121                         }
3122                         break;
3123                 case PRVM_MENUPROG:
3124                         // menu prog does not have classnames
3125                         break;
3126         }
3127         return false;
3128 }
3129
3130 static qboolean PRVM_IsEdictReferenced(prvm_edict_t *edict, int mark)
3131 {
3132         int i, j;
3133         int edictnum = PRVM_NUM_FOR_EDICT(edict);
3134         const char *targetname = NULL;
3135
3136         switch(prog - prog_list)
3137         {
3138                 case PRVM_SERVERPROG:
3139                         targetname = PRVM_GetString(edict->fields.server->targetname);
3140                         break;
3141         }
3142
3143         if(targetname)
3144                 if(!*targetname) // ""
3145                         targetname = NULL;
3146
3147         for (i = 0;i < prog->progs->numglobaldefs;i++)
3148         {
3149                 ddef_t *d = &prog->globaldefs[i];
3150                 if((etype_t)((int) d->type & ~DEF_SAVEGLOBAL) != ev_entity)
3151                         continue;
3152                 if(edictnum == ((prvm_eval_t *) &prog->globals.generic[d->ofs])->edict)
3153                         return true;
3154         }
3155
3156         for(j = 0; j < prog->num_edicts; ++j)
3157         {
3158                 prvm_edict_t *ed = PRVM_EDICT_NUM(j);
3159                 if (ed->priv.required->mark < mark)
3160                         continue;
3161                 if(ed == edict)
3162                         continue;
3163                 if(targetname)
3164                 {
3165                         const char *target = PRVM_GetString(ed->fields.server->target);
3166                         if(target)
3167                                 if(!strcmp(target, targetname))
3168                                         return true;
3169                 }
3170                 for (i=0; i<prog->progs->numfielddefs; ++i)
3171                 {
3172                         ddef_t *d = &prog->fielddefs[i];
3173                         if((etype_t)((int) d->type & ~DEF_SAVEGLOBAL) != ev_entity)
3174                                 continue;
3175                         if(edictnum == ((prvm_eval_t *) &ed->fields.vp[d->ofs])->edict)
3176                                 return true;
3177                 }
3178         }
3179
3180         return false;
3181 }
3182
3183 static void PRVM_MarkReferencedEdicts(void)
3184 {
3185         int j;
3186         qboolean found_new;
3187         int stage;
3188
3189         for(j = 0; j < prog->num_edicts; ++j)
3190         {
3191                 prvm_edict_t *ed = PRVM_EDICT_NUM(j);
3192                 if(ed->priv.required->free)
3193                         continue;
3194                 ed->priv.required->mark = PRVM_IsEdictRelevant(ed) ? 1 : 0;
3195         }
3196
3197         stage = 1;
3198         do
3199         {
3200                 found_new = false;
3201                 for(j = 0; j < prog->num_edicts; ++j)
3202                 {
3203                         prvm_edict_t *ed = PRVM_EDICT_NUM(j);
3204                         if(ed->priv.required->free)
3205                                 continue;
3206                         if(ed->priv.required->mark)
3207                                 continue;
3208                         if(PRVM_IsEdictReferenced(ed, stage))
3209                         {
3210                                 ed->priv.required->mark = stage + 1;
3211                                 found_new = true;
3212                         }
3213                 }
3214                 ++stage;
3215         }
3216         while(found_new);
3217         Con_DPrintf("leak check used %d stages to find all references\n", stage);
3218 }
3219
3220 void PRVM_LeakTest(void)
3221 {
3222         int i, j;
3223         qboolean leaked = false;
3224
3225         if(!prog->leaktest_active)
3226                 return;
3227
3228         // 1. Strings
3229         for (i = 0; i < prog->numknownstrings; ++i)
3230         {
3231                 if(prog->knownstrings[i])
3232                 if(prog->knownstrings_freeable[i])
3233                 if(prog->knownstrings_origin[i])
3234                 if(!PRVM_IsStringReferenced(-1 - i))
3235                 {
3236                         Con_Printf("Unreferenced string found!\n  Value: %s\n  Origin: %s\n", prog->knownstrings[i], prog->knownstrings_origin[i]);
3237                         leaked = true;
3238                 }
3239         }
3240
3241         // 2. Edicts
3242         PRVM_MarkReferencedEdicts();
3243         for(j = 0; j < prog->num_edicts; ++j)
3244         {
3245                 prvm_edict_t *ed = PRVM_EDICT_NUM(j);
3246                 if(ed->priv.required->free)
3247                         continue;
3248                 if(!ed->priv.required->mark)
3249                 if(ed->priv.required->allocation_origin)
3250                 {
3251                         Con_Printf("Unreferenced edict found!\n  Allocated at: %s\n", ed->priv.required->allocation_origin);
3252                         PRVM_ED_Print(ed, NULL);
3253                         Con_Print("\n");
3254                         leaked = true;
3255                 }
3256         }
3257
3258         for (i = 0; i < (int)Mem_ExpandableArray_IndexRange(&prog->stringbuffersarray); ++i)
3259         {
3260                 prvm_stringbuffer_t *stringbuffer = (prvm_stringbuffer_t*) Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);
3261                 if(stringbuffer)
3262                 if(stringbuffer->origin)
3263                 {
3264                         Con_Printf("Open string buffer handle found!\n  Allocated at: %s\n", stringbuffer->origin);
3265                         leaked = true;
3266                 }
3267         }
3268
3269         for(i = 0; i < PRVM_MAX_OPENFILES; ++i)
3270         {
3271                 if(prog->openfiles[i])
3272                 if(prog->openfiles_origin[i])
3273                 {
3274                         Con_Printf("Open file handle found!\n  Allocated at: %s\n", prog->openfiles_origin[i]);
3275                         leaked = true;
3276                 }
3277         }
3278
3279         for(i = 0; i < PRVM_MAX_OPENSEARCHES; ++i)
3280         {
3281                 if(prog->opensearches[i])
3282                 if(prog->opensearches_origin[i])
3283                 {
3284                         Con_Printf("Open search handle found!\n  Allocated at: %s\n", prog->opensearches_origin[i]);
3285                         leaked = true;
3286                 }
3287         }
3288
3289         if(!leaked)
3290                 Con_Printf("Congratulations. No leaks found.\n");
3291 }