10 evalc_t spawnflagscache;
14 //this is windows all files are written with this endian standard. we do this to try to get a little more speed.
21 //edictrun_t *sv_edicts;
24 //int pr_edict_size; // in bytes
25 //int pr_max_edict_size;
27 //unsigned short pr_crc;
29 fdef_t *ED_FieldAtOfs (progfuncs_t *progfuncs, unsigned int ofs);
30 pbool ED_ParseEpair (progfuncs_t *progfuncs, void *base, ddefXX_t *key, char *s, int bits);
35 #define MAX_FIELD_LEN 64
36 #define GEFV_CACHESIZE 5
40 char field[MAX_FIELD_LEN];
43 static gefv_cache gefvCache[GEFV_CACHESIZE] = {{NULL, ""}, {NULL, ""}};
50 Sets everything to NULL
53 void QC_ClearEdict (progfuncs_t *progfuncs, struct edict_s *ed)
55 edictrun_t *e = (edictrun_t *)ed;
57 memset (e->fields, 0, fields_size);
62 edictrun_t *ED_AllocIntoTable (progfuncs_t *progfuncs, int num)
66 prinst->edicttable[num] = *(struct edict_s **)&e = (void*)memalloc(externs->edictsize);
67 memset(e, 0, externs->edictsize);
68 e->fields = PRAddressableAlloc(progfuncs, fields_size);
70 QC_ClearEdict(progfuncs, (struct edict_s*)e);
79 Either finds a free edict, or allocates a new one.
80 Try to avoid reusing an entity that was recently freed, because it
81 can cause the client to think the entity morphed into something else
82 instead of being removed and recreated, which can cause interpolated
83 angles and bad trails.
86 struct edict_s *ED_Alloc (progfuncs_t *progfuncs)
91 for ( i=0 ; i<sv_num_edicts ; i++)
93 e = (edictrun_t*)EDICT_NUM(progfuncs, i);
94 // the first couple seconds of server time can involve a lot of
95 // freeing and allocating, so relax the replacement policy
96 if (!e || (e->isfree && ( e->freetime < 2 || *externs->gametime - e->freetime > 0.5 ) ))
99 e = ED_AllocIntoTable(progfuncs, i);
101 QC_ClearEdict (progfuncs, (struct edict_s*)e);
103 if (externs->entspawn)
104 externs->entspawn((struct edict_s *) e, false);
105 return (struct edict_s *)e;
109 if (i >= maxedicts-1) //try again, but use timed out ents.
111 for ( i=0 ; i<sv_num_edicts ; i++)
113 e = (edictrun_t*)EDICT_NUM(progfuncs, i);
114 // the first couple seconds of server time can involve a lot of
115 // freeing and allocating, so relax the replacement policy
116 if (!e || (e->isfree))
119 e = ED_AllocIntoTable(progfuncs, i);
121 QC_ClearEdict (progfuncs, (struct edict_s*)e);
123 if (externs->entspawn)
124 externs->entspawn((struct edict_s *) e, false);
125 return (struct edict_s *)e;
129 if (i >= maxedicts-2)
131 printf("Running out of edicts\n");
132 pr_trace = 1; //trip the debugger whilst it's still valid
134 if (i >= maxedicts-1)
138 buf = progfuncs->save_ents(progfuncs, NULL, &size, 0);
139 progfuncs->parms->WriteFile("edalloc.dump", buf, size);
140 Sys_Error ("ED_Alloc: no free edicts");
145 e = (edictrun_t*)EDICT_NUM(progfuncs, i);
148 e = ED_AllocIntoTable(progfuncs, i);
150 QC_ClearEdict (progfuncs, (struct edict_s*)e);
152 if (externs->entspawn)
153 externs->entspawn((struct edict_s *) e, false);
155 return (struct edict_s *)e;
162 Marks the edict as free
163 FIXME: walk all entities and NULL out references to this entity
166 void ED_Free (progfuncs_t *progfuncs, struct edict_s *ed)
168 edictrun_t *e = (edictrun_t *)ed;
169 // SV_UnlinkEdict (ed); // unlink from world bsp
171 if (e->isfree) //this happens on start.bsp where an onlyregistered trigger killtargets itself (when all of this sort die after 1 trigger anyway).
174 printf("Tried to free free entity within %s\n", pr_xfunction->s_name+progfuncs->stringtable);
176 printf("Engine tried to free free entity\n");
177 // if (developer.value == 1)
182 if (externs->entcanfree)
183 if (!externs->entcanfree(ed)) //can stop an ent from being freed.
187 e->freetime = (float)*externs->gametime;
191 ed->v.takedamage = 0;
192 ed->v.modelindex = 0;
196 VectorCopy (vec3_origin, ed->v.origin);
197 VectorCopy (vec3_origin, ed->v.angles);
198 ed->v.nextthink = -1;
203 //===========================================================================
210 ddef16_t *ED_GlobalAtOfs16 (progfuncs_t *progfuncs, int ofs)
215 for (i=0 ; i<pr_progs->numglobaldefs ; i++)
217 def = &pr_globaldefs16[i];
223 ddef32_t *ED_GlobalAtOfs32 (progfuncs_t *progfuncs, unsigned int ofs)
228 for (i=0 ; i<pr_progs->numglobaldefs ; i++)
230 def = &pr_globaldefs32[i];
242 fdef_t *ED_FieldAtOfs (progfuncs_t *progfuncs, unsigned int ofs)
247 for (i=0 ; i<numfields ; i++)
249 if (field[i].ofs == ofs)
259 fdef_t *ED_FindField (progfuncs_t *progfuncs, char *name)
263 for (i=0 ; i<numfields ; i++)
265 if (!strcmp(field[i].name, name) )
277 ddef16_t *ED_FindGlobal16 (progfuncs_t *progfuncs, char *name)
282 for (i=1 ; i<pr_progs->numglobaldefs ; i++)
284 def = &pr_globaldefs16[i];
285 if (!strcmp(def->s_name+progfuncs->stringtable,name) )
290 ddef32_t *ED_FindGlobal32 (progfuncs_t *progfuncs, char *name)
295 for (i=1 ; i<pr_progs->numglobaldefs ; i++)
297 def = &pr_globaldefs32[i];
298 if (!strcmp(def->s_name+progfuncs->stringtable,name) )
304 unsigned int ED_FindGlobalOfs (progfuncs_t *progfuncs, char *name)
308 switch(current_progstate->intsize)
312 d16 = ED_FindGlobal16(progfuncs, name);
313 return d16?d16->ofs:0;
315 d32 = ED_FindGlobal32(progfuncs, name);
316 return d32?d32->ofs:0;
318 Sys_Error("ED_FindGlobalOfs - bad intsize");
322 ddef16_t *ED_FindGlobalFromProgs16 (progfuncs_t *progfuncs, char *name, progsnum_t prnum)
327 for (i=1 ; i<pr_progstate[prnum].progs->numglobaldefs ; i++)
329 def = &pr_progstate[prnum].globaldefs16[i];
330 if (!strcmp(def->s_name+progfuncs->stringtable,name) )
335 ddef32_t *ED_FindGlobalFromProgs32 (progfuncs_t *progfuncs, char *name, progsnum_t prnum)
340 for (i=1 ; i<pr_progstate[prnum].progs->numglobaldefs ; i++)
342 def = &pr_progstate[prnum].globaldefs32[i];
343 if (!strcmp(def->s_name+progfuncs->stringtable,name) )
349 ddef16_t *ED_FindTypeGlobalFromProgs16 (progfuncs_t *progfuncs, char *name, progsnum_t prnum, int type)
354 for (i=1 ; i<pr_progstate[prnum].progs->numglobaldefs ; i++)
356 def = &pr_progstate[prnum].globaldefs16[i];
357 if (!strcmp(def->s_name+progfuncs->stringtable,name) )
359 if (pr_progstate[prnum].types)
361 if (pr_progstate[prnum].types[def->type&~DEF_SAVEGLOBAL].type != type)
364 else if ((def->type&(~DEF_SAVEGLOBAL)) != type)
373 ddef32_t *ED_FindTypeGlobalFromProgs32 (progfuncs_t *progfuncs, char *name, progsnum_t prnum, int type)
378 for (i=1 ; i<pr_progstate[prnum].progs->numglobaldefs ; i++)
380 def = &pr_progstate[prnum].globaldefs32[i];
381 if (!strcmp(def->s_name+progfuncs->stringtable,name) )
383 if (pr_progstate[prnum].types)
385 if (pr_progstate[prnum].types[def->type&~DEF_SAVEGLOBAL].type != type)
388 else if ((def->type&(~DEF_SAVEGLOBAL)) != (unsigned)type)
396 unsigned int *ED_FindGlobalOfsFromProgs (progfuncs_t *progfuncs, char *name, progsnum_t prnum, int type)
400 static unsigned int pos;
401 switch(pr_progstate[prnum].intsize)
405 def16 = ED_FindTypeGlobalFromProgs16(progfuncs, name, prnum, type);
411 def32 = ED_FindTypeGlobalFromProgs32(progfuncs, name, prnum, type);
416 Sys_Error("ED_FindGlobalOfsFromProgs - bad intsize");
425 dfunction_t *ED_FindFunction (progfuncs_t *progfuncs, char *name, progsnum_t *prnum, progsnum_t fromprogs)
435 sep = strchr(name, ':');
446 pnum = pr_typecurrent;
451 pnum = pr_typecurrent;
453 if ((unsigned)pnum > (unsigned)maxprogs)
455 printf("Progsnum %i out of bounds\n", pnum);
459 if (!pr_progstate[pnum].progs)
462 for (i=1 ; i<pr_progstate[pnum].progs->numfunctions ; i++)
464 func = &pr_progstate[pnum].functions[i];
465 if (!strcmp(func->s_name+progfuncs->stringtable,name) )
475 Returns a string describing *data in a type specific manner
478 char *PR_ValueString (progfuncs_t *progfuncs, etype_t type, eval_t *val)
480 static char line[256];
484 #ifdef DEF_SAVEGLOBAL
485 type &= ~DEF_SAVEGLOBAL;
489 type = pr_types[type].type;
494 sprintf (line, "struct");
497 sprintf (line, "union");
500 sprintf (line, "%s", PR_StringToNative(progfuncs, val->string));
503 sprintf (line, "entity %i", NUM_FOR_EDICT(progfuncs, (struct edict_s *)PROG_TO_EDICT(progfuncs, val->edict)) );
507 sprintf (line, "NULL function");
510 if ((val->function & 0xff000000)>>24 >= (unsigned)maxprogs || !pr_progstate[(val->function & 0xff000000)>>24].functions)
511 sprintf (line, "Bad function");
514 f = pr_progstate[(val->function & 0xff000000)>>24].functions + (val->function & ~0xff000000);
515 sprintf (line, "%i:%s()", (val->function & 0xff000000)>>24, f->s_name+progfuncs->stringtable);
520 fielddef = ED_FieldAtOfs (progfuncs, val->_int );
522 sprintf (line, ".??? (%i)", val->_int);
524 sprintf (line, ".%s (%i)", fielddef->name, val->_int);
527 sprintf (line, "void type");
530 sprintf (line, "%5.1f", val->_float);
533 sprintf (line, "%i", val->_int);
536 sprintf (line, "'%5.1f %5.1f %5.1f'", val->_vector[0], val->_vector[1], val->_vector[2]);
539 sprintf (line, "pointer");
545 sprintf (line, "NULL pointer");
549 sprintf(line, "UNKNOWN");
550 // entnum = ((qbyte *)val->edict - (qbyte *)sv_edicts) / pr_edict_size;
551 // valofs = (int *)val->edict - (int *)edvars(EDICT_NUM(progfuncs, entnum));
552 // fielddef = ED_FieldAtOfs (progfuncs, valofs );
554 // sprintf(line, "ent%i.%s", entnum, "UNKNOWN");
556 // sprintf(line, "ent%i.%s", entnum, fielddef->s_name);
560 sprintf (line, "bad type %i", type);
571 Returns a string describing *data in a type specific manner
572 Easier to parse than PR_ValueString
575 char *PR_UglyValueString (progfuncs_t *progfuncs, etype_t type, eval_t *val)
577 static char line[256];
582 #ifdef DEF_SAVEGLOBAL
583 type &= ~DEF_SAVEGLOBAL;
587 type = pr_types[type].type;
592 sprintf (line, "structures cannot yet be saved");
595 sprintf (line, "unions cannot yet be saved");
600 int outb = sizeof(line)-2;
601 char *ins = PR_StringToNative(progfuncs, val->string);
602 //markup the output string.
603 while(*ins && outb > 0)
635 sprintf (line, "%i", val->_int);
638 i = (val->function & 0xff000000)>>24; //progs number
639 if ((unsigned)i >= maxprogs || !pr_progstate[(unsigned)i].progs)
640 sprintf (line, "BAD FUNCTION INDEX: %i", val->function);
643 j = (val->function & ~0xff000000); //function number
644 if ((unsigned)j >= pr_progstate[(unsigned)i].progs->numfunctions)
645 sprintf(line, "%i:%s", i, "CORRUPT FUNCTION POINTER");
648 f = pr_progstate[(unsigned)i].functions + j;
649 sprintf (line, "%i:%s", i, f->s_name+progfuncs->stringtable);
654 fielddef = ED_FieldAtOfs (progfuncs, val->_int );
655 sprintf (line, "%s", fielddef->name);
658 sprintf (line, "void");
661 if (val->_float == (int)val->_float)
662 sprintf (line, "%i", (int)val->_float); //an attempt to cut down on the number of .000000 vars..
664 sprintf (line, "%f", val->_float);
667 sprintf (line, "%i", val->_int);
670 if (val->_vector[0] == (int)val->_vector[0] && val->_vector[1] == (int)val->_vector[1] && val->_vector[2] == (int)val->_vector[2])
671 sprintf (line, "%i %i %i", (int)val->_vector[0], (int)val->_vector[1], (int)val->_vector[2]);
673 sprintf (line, "%f %f %f", val->_vector[0], val->_vector[1], val->_vector[2]);
676 sprintf (line, "bad type %i", type);
683 //compatible with Q1 (for savegames)
684 char *PR_UglyOldValueString (progfuncs_t *progfuncs, etype_t type, eval_t *val)
686 static char line[256];
690 #ifdef DEF_SAVEGLOBAL
691 type &= ~DEF_SAVEGLOBAL;
695 type = pr_types[type].type;
700 sprintf (line, "structures cannot yet be saved");
703 sprintf (line, "unions cannot yet be saved");
706 sprintf (line, "%s", PR_StringToNative(progfuncs, val->string));
709 sprintf (line, "%i", NUM_FOR_EDICT(progfuncs, (struct edict_s *)PROG_TO_EDICT(progfuncs, val->edict)));
712 f = pr_progstate[(val->function & 0xff000000)>>24].functions + (val->function & ~0xff000000);
713 sprintf (line, "%s", f->s_name+progfuncs->stringtable);
716 fielddef = ED_FieldAtOfs (progfuncs, val->_int );
717 sprintf (line, "%s", fielddef->name);
720 sprintf (line, "void");
723 if (val->_float == (int)val->_float)
724 sprintf (line, "%i", (int)val->_float); //an attempt to cut down on the number of .000000 vars..
726 sprintf (line, "%f", val->_float);
729 sprintf (line, "%i", val->_int);
732 if (val->_vector[0] == (int)val->_vector[0] && val->_vector[1] == (int)val->_vector[1] && val->_vector[2] == (int)val->_vector[2])
733 sprintf (line, "%i %i %i", (int)val->_vector[0], (int)val->_vector[1], (int)val->_vector[2]);
735 sprintf (line, "%f %f %f", val->_vector[0], val->_vector[1], val->_vector[2]);
739 sprintf (line, "bad type %i", type);
746 char *PR_TypeString(progfuncs_t *progfuncs, etype_t type)
748 #ifdef DEF_SAVEGLOBAL
749 type &= ~DEF_SAVEGLOBAL;
753 type = pr_types[type].type;
786 Returns a string with a description and the contents of a global,
787 padded to 20 field width
790 char *PR_GlobalString (progfuncs_t *progfuncs, int ofs)
797 static char line[128];
799 switch (current_progstate->intsize)
803 val = (void *)&pr_globals[ofs];
804 def16 = ED_GlobalAtOfs16(progfuncs, ofs);
806 sprintf (line,"%i(?""?""?)", ofs);
809 s = PR_ValueString (progfuncs, def16->type, val);
810 sprintf (line,"%i(%s)%s", ofs, def16->s_name+progfuncs->stringtable, s);
819 val = (void *)&pr_globals[ofs];
820 def32 = ED_GlobalAtOfs32(progfuncs, ofs);
822 sprintf (line,"%i(?""?""?)", ofs);
825 s = PR_ValueString (progfuncs, def32->type, val);
826 sprintf (line,"%i(%s)%s", ofs, def32->s_name+progfuncs->stringtable, s);
835 Sys_Error("Bad offset size in PR_GlobalString");
839 char *PR_GlobalStringNoContents (progfuncs_t *progfuncs, int ofs)
844 static char line[128];
846 switch (current_progstate->intsize)
850 def16 = ED_GlobalAtOfs16(progfuncs, ofs);
852 sprintf (line,"%i(?""?""?)", ofs);
854 sprintf (line,"%i(%s)", ofs, def16->s_name+progfuncs->stringtable);
857 def32 = ED_GlobalAtOfs32(progfuncs, ofs);
859 sprintf (line,"%i(?""?""?)", ofs);
861 sprintf (line,"%i(%s)", ofs, def32->s_name+progfuncs->stringtable);
864 Sys_Error("Bad offset size in PR_GlobalStringNoContents");
883 void ED_Print (progfuncs_t *progfuncs, struct edict_s *ed)
888 unsigned int i;unsigned int j;
892 if (((edictrun_t *)ed)->isfree)
898 printf("\nEDICT %i:\n", NUM_FOR_EDICT(progfuncs, (struct edict_s *)ed));
899 for (i=1 ; i<numfields ; i++)
904 if (l >= 2 && name[l-2] == '_')
905 continue; // skip _x, _y, _z vars
907 v = (int *)((char *)edvars(ed) + d->ofs*4);
909 // if the value is still all 0, skip the field
910 #ifdef DEF_SAVEGLOBAL
911 type = d->type & ~DEF_SAVEGLOBAL;
916 for (j=0 ; j<type_size[type] ; j++)
919 if (j == type_size[type])
927 printf ("%s\n", PR_ValueString(progfuncs, d->type, (eval_t *)v));
931 void ED_PrintNum (progfuncs_t *progfuncs, int ent)
933 ED_Print (progfuncs, EDICT_NUM(progfuncs, ent));
940 For debugging, prints all the entities in the current server
943 void ED_PrintEdicts (progfuncs_t *progfuncs)
947 printf ("%i entities\n", sv_num_edicts);
948 for (i=0 ; i<sv_num_edicts ; i++)
949 ED_PrintNum (progfuncs, i);
959 void ED_Count (progfuncs_t *progfuncs)
963 unsigned int active, models, solid, step;
965 active = models = solid = step = 0;
966 for (i=0 ; i<sv_num_edicts ; i++)
968 ent = (edictrun_t *)EDICT_NUM(progfuncs, i);
976 // if (ent->v.movetype == MOVETYPE_STEP)
980 printf ("num_edicts:%3i\n", sv_num_edicts);
981 printf ("active :%3i\n", active);
982 // Con_Printf ("view :%3i\n", models);
983 // Con_Printf ("touch :%3i\n", solid);
984 // Con_Printf ("step :%3i\n", step);
990 //============================================================================
998 char *ED_NewString (progfuncs_t *progfuncs, char *string, int minlength)
1005 l = strlen(string) + 1;
1007 newc = PRAddressableAlloc (progfuncs, l<minlength?minlength:l);
1010 for (i=0 ; i< l ; i++)
1012 if (string[i] == '\\' && i < l-1 && string[i+1] != 0)
1015 if (string[i] == 'n')
1021 *new_p++ = string[i];
1032 Can parse either fields or globals
1033 returns false if error
1036 pbool ED_ParseEpair (progfuncs_t *progfuncs, void *base, ddefXX_t *key, char *s, int bits)
1051 d = (void *)((int *)base + ((ddef16_t*)key)->ofs);
1054 type = pr_types[((ddef16_t*)key)->type & ~DEF_SAVEGLOBAL].type;
1056 type = ((ddef16_t*)key)->type & ~DEF_SAVEGLOBAL;
1059 d = (void *)((int *)base + ((ddef32_t*)key)->ofs);
1062 type = pr_types[((ddef32_t*)key)->type & ~DEF_SAVEGLOBAL].type;
1064 type = ((ddef32_t*)key)->type & ~DEF_SAVEGLOBAL;
1067 Sys_Error("Bad bits in ED_ParseEpair");
1074 st = PR_StringToProgs(progfuncs, ED_NewString (progfuncs, s, 0));
1075 *(string_t *)d = st;
1079 *(float *)d = (float)atof (s);
1083 *(int *)d = atoi (s);
1090 for (i=0 ; i<3 ; i++)
1092 while (*v && *v != ' ')
1095 ((float *)d)[i] = (float)atof (w);
1101 *(int *)d = atoi (s);
1105 def = ED_FindField (progfuncs, s);
1108 printf ("Can't find field %s\n", s);
1111 *(int *)d = def->ofs;
1115 if (s[1]==':'&&s[2]=='\0')
1120 func = ED_FindFunction (progfuncs, s, &i, -1);
1123 printf ("Can't find function %s\n", s);
1126 *(func_t *)d = (func - pr_progstate[i].functions) | (i<<24);
1136 ====================
1139 Parses an edict out of the given string, returning the new position
1140 ed should be a properly initialized empty edict.
1141 Used for initial level load and for savegames.
1142 ====================
1145 char *ED_ParseEdict (progfuncs_t *progfuncs, char *data, edictrun_t *ent)
1157 // if (ent != (edictrun_t *)sv_edicts) // hack
1158 // memset (ent+1, 0, pr_edict_size - sizeof(edictrun_t));
1160 // go through all the dictionary pairs
1164 data = QCC_COM_Parse (data);
1165 if (qcc_token[0] == '}')
1169 printf ("ED_ParseEntity: EOF without closing brace\n");
1173 strncpy (keyname, qcc_token, sizeof(keyname)-1);
1174 keyname[sizeof(keyname)-1] = 0;
1176 // another hack to fix heynames with trailing spaces
1177 n = strlen(keyname);
1178 while (n && keyname[n-1] == ' ')
1185 data = QCC_COM_Parse (data);
1188 printf ("ED_ParseEntity: EOF without closing brace\n");
1192 if (qcc_token[0] == '}')
1194 printf ("ED_ParseEntity: closing brace without data\n");
1200 // keynames with a leading underscore are used for utility comments,
1201 // and are immediately discarded by quake
1202 if (keyname[0] == '_')
1205 key = ED_FindField (progfuncs, keyname);
1208 if (!strcmp(keyname, "angle")) //Quake anglehack - we've got to leave it in cos it doesn't work for quake otherwise, and this is a QuakeC lib!
1210 if ((key = ED_FindField (progfuncs, "angles")))
1212 sprintf (qcc_token, "0 %f 0", atof(qcc_token)); //change it from yaw to 3d angle
1216 if (!strcmp(keyname, "light")) //Quake lighthack - allows a field name and a classname to go by the same thing in the level editor
1217 if ((key = ED_FindField (progfuncs, "light_lev")))
1219 printf ("'%s' is not a field\n", keyname);
1224 if (!ED_ParseEpair (progfuncs, ent->fields, (ddefXX_t*)key, qcc_token, 32))
1227 // Sys_Error ("ED_ParseEdict: parse error on entities");
1242 The entities are directly placed in the array, rather than allocated with
1243 ED_Alloc, because otherwise an error loading the map would have entity
1244 number references out of order.
1246 Creates a server's entity / program execution context by
1247 parsing textual entity definitions out of an ent file.
1249 Used for both fresh maps and savegame loads. A fresh map would also need
1250 to call ED_CallSpawnFunctions () to let the objects initialize themselves.
1254 char *ED_WriteGlobals(progfuncs_t *progfuncs, char *buffer) //switch first.
1256 #define AddS(str) strcpy(buffer, str);buffer+=strlen(str);
1264 int curprogs = pr_typecurrent;
1266 switch(current_progstate->intsize)
1270 for (i=0 ; i<pr_progs->numglobaldefs ; i++)
1272 def16 = &pr_globaldefs16[i];
1273 name = def16->s_name + progfuncs->stringtable;
1277 if (name[len-2] == '_' && (name[len-1] == 'x' || name[len-1] == 'y' || name[len-1] == 'z'))
1278 continue; // skip _x, _y, _z vars (vector components, which are saved as one vector not 3 floats)
1282 #ifdef DEF_SAVEGLOBAL
1283 if ( !(def16->type & DEF_SAVEGLOBAL) )
1285 type &= ~DEF_SAVEGLOBAL;
1287 if (current_progstate->types)
1288 type = current_progstate->types[type].type;
1289 if (type == ev_function)
1291 v = (int *)¤t_progstate->globals[def16->ofs];
1292 if ((v[0]&0xff000000)>>24 == (unsigned)curprogs) //same progs
1294 if (!progfuncs->stringtable[current_progstate->functions[v[0]&0x00ffffff].s_name])
1296 else if (!strcmp(current_progstate->functions[v[0]&0x00ffffff].s_name+ progfuncs->stringtable, name)) //names match. Assume function is at initial value.
1301 if ((v[0]&0xff000000)>>24 == 0)
1302 if (!ED_FindFunction(progfuncs, name, NULL, curprogs)) //defined as extern
1304 if (!progfuncs->stringtable[pr_progstate[0].functions[v[0]&0x00ffffff].s_name])
1306 else if (!strcmp(pr_progstate[0].functions[v[0]&0x00ffffff].s_name + progfuncs->stringtable, name)) //same name.
1310 //else function has been redirected externally.
1313 else if (type != ev_string //anything other than these is not saved
1315 && type != ev_integer
1316 && type != ev_entity
1317 && type != ev_vector)
1320 v = (int *)¤t_progstate->globals[def16->ofs];
1322 // make sure the value is not null, where there's no point in saving
1323 for (j=0 ; j<type_size[type] ; j++)
1326 if (j == type_size[type])
1330 AddS (qcva("\"%s\" ", name));
1331 AddS (qcva("\"%s\"\n", PR_UglyValueString(progfuncs, def16->type&~DEF_SAVEGLOBAL, (eval_t *)v)));
1335 for (i=0 ; i<pr_progs->numglobaldefs ; i++)
1337 def32 = &pr_globaldefs32[i];
1338 name = def32->s_name + progfuncs->stringtable;
1339 if (name[strlen(name)-2] == '_')
1340 continue; // skip _x, _y, _z vars (vector components, which are saved as one vector not 3 floats)
1344 #ifdef DEF_SAVEGLOBAL
1345 if ( !(def32->type & DEF_SAVEGLOBAL) )
1347 type &= ~DEF_SAVEGLOBAL;
1349 if (current_progstate->types)
1350 type = current_progstate->types[type].type;
1351 if (type == ev_function)
1353 v = (int *)¤t_progstate->globals[def32->ofs];
1354 if ((v[0]&0xff000000)>>24 == (unsigned)curprogs) //same progs
1355 if (!strcmp(current_progstate->functions[v[0]&0x00ffffff].s_name+ progfuncs->stringtable, name)) //names match. Assume function is at initial value.
1359 if ((v[0]&0xff000000)>>24 == 0)
1360 if (!ED_FindFunction(progfuncs, name, NULL, curprogs)) //defined as extern
1361 if (!strcmp(pr_progstate[0].functions[v[0]&0x00ffffff].s_name+ progfuncs->stringtable, name)) //same name.
1364 //else function has been redirected externally.
1367 else if (type != ev_string //anything other than these is not saved
1369 && type != ev_integer
1370 && type != ev_entity
1371 && type != ev_vector)
1374 v = (int *)¤t_progstate->globals[def32->ofs];
1376 // make sure the value is not null, where there's no point in saving
1377 for (j=0 ; j<type_size[type] ; j++)
1380 if (j == type_size[type])
1383 AddS (qcva("\"%s\" ", name));
1384 AddS (qcva("\"%s\"\n", PR_UglyValueString(progfuncs, def32->type&~DEF_SAVEGLOBAL, (eval_t *)v)));
1388 Sys_Error("Bad number of bits in SaveEnts");
1394 char *ED_WriteEdict(progfuncs_t *progfuncs, edictrun_t *ed, char *buffer, pbool q1compatible)
1399 unsigned int i;unsigned int j;
1404 for (i=0 ; i<numfields ; i++)
1409 if (len>4 && (name[len-2] == '_' && (name[len-1] == 'x' || name[len-1] == 'y' || name[len-1] == 'z')))
1410 continue; // skip _x, _y, _z vars
1412 v = (int *)((char*)ed->fields + d->ofs*4);
1414 // if the value is still all 0, skip the field
1415 #ifdef DEF_SAVEGLOBAL
1416 type = d->type & ~DEF_SAVEGLOBAL;
1421 for (j=0 ; j<type_size[type] ; j++)
1424 if (j == type_size[type])
1427 //add it to the file
1428 AddS (qcva("\"%s\" ",name));
1429 AddS (qcva("\"%s\"\n", (q1compatible?PR_UglyOldValueString:PR_UglyValueString)(progfuncs, d->type, (eval_t *)v)));
1436 char *SaveCallStack (progfuncs_t *progfuncs, char *s)
1438 #define AddS(str) strcpy(s, str);s+=strlen(str);
1451 AddS ("<NO STACK>\n");
1455 globalbase = (int *)pr_globals + pr_xfunction->parm_start + pr_xfunction->locals;
1457 pr_stack[pr_depth].f = pr_xfunction;
1458 for (i=pr_depth ; i>0 ; i--)
1464 AddS ("<NO FUNCTION>\n");
1468 if (pr_stack[i].progsnum != progs)
1470 progs = pr_stack[i].progsnum;
1472 sprintf(buffer, "//%i %s\n", progs, pr_progstate[progs].filename);
1476 sprintf(buffer, "\t\"%i:%s\"\n", progs, f->s_name+progfuncs->stringtable);
1478 sprintf(buffer, "\t\"%i:%s\" //%s\n", progs, f->s_name+progfuncs->stringtable, f->s_file+progfuncs->stringtable);
1482 for (arg = 0; arg < f->locals; arg++)
1485 local = ED_GlobalAtOfs16(progfuncs, f->parm_start+arg);
1487 sprintf(buffer, "\t\tofs%i %i // %f\n", f->parm_start+arg, *(int *)(globalbase - f->locals+arg), *(float *)(globalbase - f->locals+arg) );
1490 if (local->type == ev_entity)
1492 sprintf(buffer, "\t\t\"%s\" \"entity %i\"\n", local->s_name+progfuncs->stringtable, ((eval_t*)(globalbase - f->locals+arg))->edict);
1495 sprintf(buffer, "\t\t\"%s\"\t\"%s\"\n", local->s_name+progfuncs->stringtable, PR_ValueString(progfuncs, local->type, (eval_t*)(globalbase - f->locals+arg)));
1497 if (local->type == ev_vector)
1505 globalbase = localstack + localstack_used - f->locals;
1507 globalbase -= f->locals;
1514 //there are two ways of saving everything.
1515 //0 is to save just the entities.
1516 //1 is to save the entites, and all the progs info so that all the variables are saved off, and it can be reloaded to exactly how it was (provided no files or data has been changed outside, like the progs.dat for example)
1517 char *SaveEnts(progfuncs_t *progfuncs, char *mem, int *len, int alldata)
1519 #define AddS(str) strcpy(s, str);s+=strlen(str);
1529 os = s = memalloc(5*1024*1024);
1532 { //special Q1 savegame compatability mode.
1533 //engine will need to store references to progs type and will need to preload the progs and inti the ents itself before loading.
1535 //Make sure there is only 1 progs loaded.
1536 for (a = 1; a < maxprogs; a++)
1538 if (pr_progstate[a].progs)
1541 if (!pr_progstate[0].progs || a != maxprogs) //the state of the progs wasn't Q1 compatible.
1550 oldprogs = pr_typecurrent;
1551 PR_SwitchProgs(progfuncs, 0);
1553 s = ED_WriteGlobals(progfuncs, s);
1555 PR_SwitchProgs(progfuncs, oldprogs);
1561 for (a = 0; a < sv_num_edicts; a++)
1563 edictrun_t *ed = (edictrun_t *)EDICT_NUM(progfuncs, a);
1568 s = ED_WriteEdict(progfuncs, ed, s, true);
1579 AddS("general {\n");
1580 AddS(qcva("\"maxprogs\" \"%i\"\n", maxprogs));
1581 // AddS(qcva("\"maxentities\" \"%i\"\n", maxedicts));
1582 // AddS(qcva("\"mem\" \"%i\"\n", hunksize));
1583 // AddS(qcva("\"crc\" \"%i\"\n", header_crc));
1584 AddS(qcva("\"numentities\" \"%i\"\n", sv_num_edicts));
1587 oldprogs = pr_typecurrent;
1589 for (a = 0; a < maxprogs; a++)
1591 if (!pr_progstate[a].progs)
1593 PR_SwitchProgs(progfuncs, a);
1595 AddS (qcva("progs %i {\n", a));
1596 AddS (qcva("\"filename\" \"%s\"\n", pr_progstate[a].filename));
1597 AddS (qcva("\"crc\" \"%i\"\n", pr_progs->crc));
1598 AddS (qcva("\"numbuiltins\" \"%i\"\n", current_progstate->numbuiltins));
1606 AddS("stacktrace {\n");
1607 s = SaveCallStack(progfuncs, s);
1611 for (a = 0; a < maxprogs; a++) //I would mix, but external functions rely on other progs being loaded
1613 if (!pr_progstate[a].progs)
1616 AddS (qcva("globals %i {\n", a));
1618 PR_SwitchProgs(progfuncs, a);
1620 s = ED_WriteGlobals(progfuncs, s);
1624 PR_SwitchProgs(progfuncs, oldprogs);
1626 for (a = 0; a < sv_num_edicts; a++)
1628 edictrun_t *ed = (edictrun_t *)EDICT_NUM(progfuncs, a);
1633 AddS (qcva("entity %i{\n", a));
1635 s = ED_WriteEdict(progfuncs, ed, s, false);
1648 //if 'general' block is found, this is a compleate state, otherwise, we should spawn entities like
1649 int LoadEnts(progfuncs_t *progfuncs, char *file, float killonspawnflags)
1651 eval_t *fulldata; //this is part of FTE_FULLSPAWNDATA
1659 edictrun_t *ed=NULL;
1662 func_t CheckSpawn=0;
1664 extern edictrun_t tempedict;
1672 if (!strncmp(file, "loadgame", 8))
1683 if (pr_typecurrent>=0)
1684 num = ED_FindGlobalOfs(progfuncs, "__fullspawndata");
1688 fulldata = (eval_t *)((int *)pr_globals + num);
1696 file = QCC_COM_Parse(file);
1698 break; //finished reading file
1699 else if (!strcmp(qcc_token, "entity"))
1701 if (entsize == 0 && resethunk) //edicts have not yet been initialized, and this is a compleate load (memsize has been set)
1703 entsize = PR_InitEnts(progfuncs, maxedicts);
1704 // sv_num_edicts = numents;
1706 for (num = 0; num < numents; num++)
1708 ed = (edictrun_t *)EDICT_NUM(progfuncs, num);
1712 ed = ED_AllocIntoTable(progfuncs, num);
1714 if (externs->entspawn)
1715 externs->entspawn((struct edict_s *) ed, true);
1720 file = QCC_COM_Parse(file);
1721 num = atoi(qcc_token);
1722 file = QCC_COM_Parse(file);
1723 if (qcc_token[0] != '{')
1724 Sys_Error("Progs loading found %s, not '{'", qcc_token);
1726 ed = (edictrun_t *)ED_Alloc(progfuncs);
1729 ed = (edictrun_t *)EDICT_NUM(progfuncs, num);
1733 Sys_Error("Edict was not allocated\n");
1734 ed = ED_AllocIntoTable(progfuncs, num);
1738 if (externs->entspawn)
1739 externs->entspawn((struct edict_s *) ed, true);
1740 file = ED_ParseEdict(progfuncs, file, ed);
1742 if (killonspawnflags)
1744 var = GetEdictFieldValue (progfuncs, (struct edict_s *)&ed, "spawnflags", &spawnflagscache);
1747 if ((int)var->_float & (int)killonspawnflags)
1758 if ((var = GetEdictFieldValue (progfuncs, (struct edict_s *)ed, "classname", NULL)))
1760 f = ED_FindFunction(progfuncs, var->string + progfuncs->stringtable, NULL, -1);
1763 var = (eval_t *)((int *)pr_globals + ED_FindGlobalOfs(progfuncs, "self"));
1764 var->edict = EDICT_TO_PROG(progfuncs, ed);
1765 PR_ExecuteProgram(progfuncs, f-pr_functions);
1770 else if (!strcmp(qcc_token, "progs"))
1772 file = QCC_COM_Parse(file);
1773 num = atoi(qcc_token);
1774 file = QCC_COM_Parse(file);
1775 if (qcc_token[0] != '{')
1776 Sys_Error("Progs loading found %s, not '{'", qcc_token);
1785 file = QCC_COM_Parse(file); //read the key
1787 Sys_Error("EOF in progs block");
1789 if (!strcmp("filename", qcc_token)) //check key get and save values
1790 {file = QCC_COM_Parse(file); strcpy(filename, qcc_token);}
1791 else if (!strcmp("crc", qcc_token))
1792 {file = QCC_COM_Parse(file); header_crc = atoi(qcc_token);}
1793 else if (!strcmp("numbuiltins", qcc_token))
1794 {file = QCC_COM_Parse(file); numbuiltins = atoi(qcc_token);}
1795 else if (qcc_token[0] == '}') //end of block
1798 Sys_Error("Bad key \"%s\" in progs block", qcc_token);
1801 PR_ReallyLoadProgs(progfuncs, filename, header_crc, &pr_progstate[num], true);
1802 if (!externs->builtinsfor)
1804 // Sys_Error("Couldn't reset the builtin functions");
1805 current_progstate->builtins = NULL; //these are specific, we assume the global ones were set via pr_configure
1806 current_progstate->numbuiltins = 0;
1810 current_progstate->builtins = externs->builtinsfor(num, header_crc);
1811 current_progstate->numbuiltins = numbuiltins;
1814 else if (!strcmp(qcc_token, "globals"))
1816 if (entsize == 0 && resethunk) //by the time we parse some globals, we MUST have loaded all progs
1818 entsize = PR_InitEnts(progfuncs, maxedicts);
1819 // sv_num_edicts = numents;
1821 for (num = 0; num < numents; num++)
1823 ed = (edictrun_t *)EDICT_NUM(progfuncs, num);
1827 ed = ED_AllocIntoTable(progfuncs, num);
1831 if (externs->entspawn)
1832 externs->entspawn((struct edict_s *) ed, true);
1836 file = QCC_COM_Parse(file);
1837 num = atoi(qcc_token);
1839 file = QCC_COM_Parse(file);
1840 if (qcc_token[0] != '{')
1841 Sys_Error("Globals loading found \'%s\', not '{'", qcc_token);
1843 PR_SwitchProgs(progfuncs, num);
1846 file = QCC_COM_Parse(file);
1847 if (qcc_token[0] == '}')
1849 else if (!qcc_token[0] || !file)
1850 Sys_Error("EOF when parsing global values");
1852 switch(current_progstate->intsize)
1856 if (!(d16 = ED_FindGlobal16(progfuncs, qcc_token)))
1858 file = QCC_COM_Parse(file);
1859 printf("global value %s not found", qcc_token);
1863 file = QCC_COM_Parse(file);
1864 ED_ParseEpair(progfuncs, pr_globals, (ddefXX_t*)d16, qcc_token, 16);
1868 if (!(d32 = ED_FindGlobal32(progfuncs, qcc_token)))
1870 file = QCC_COM_Parse(file);
1871 printf("global value %s not found", qcc_token);
1875 file = QCC_COM_Parse(file);
1876 ED_ParseEpair(progfuncs, pr_globals, (ddefXX_t*)d32, qcc_token, 32);
1880 Sys_Error("Bad intsize in LoadEnts");
1884 // file = QCC_COM_Parse(file);
1885 // if (com_token[0] != '}')
1886 // Sys_Error("Progs loading found %s, not '}'", qcc_token);
1888 else if (!strcmp(qcc_token, "general"))
1890 QC_StartShares(progfuncs);
1891 // QC_InitShares(); //forget stuff
1892 // pr_edict_size = 0;
1895 file = QCC_COM_Parse(file);
1896 if (qcc_token[0] != '{')
1897 Sys_Error("Progs loading found %s, not '{'", qcc_token);
1901 file = QCC_COM_Parse(file); //read the key
1903 Sys_Error("EOF in general block");
1905 if (!strcmp("maxprogs", qcc_token)) //check key get and save values
1906 {file = QCC_COM_Parse(file); maxprogs = atoi(qcc_token);}
1907 // else if (!strcmp("maxentities", com_token))
1908 // {file = QCC_COM_Parse(file); maxedicts = atoi(qcc_token);}
1909 // else if (!strcmp("mem", com_token))
1910 // {file = QCC_COM_Parse(file); memsize = atoi(qcc_token);}
1911 // else if (!strcmp("crc", com_token))
1912 // {file = QCC_COM_Parse(file); crc = atoi(qcc_token);}
1913 else if (!strcmp("numentities", qcc_token))
1914 {file = QCC_COM_Parse(file); numents = atoi(qcc_token);}
1915 else if (qcc_token[0] == '}') //end of block
1918 Sys_Error("Bad key \"%s\" in general block", qcc_token);
1921 PRAddressableFlush(progfuncs, -1);
1924 pr_progstate = PRHunkAlloc(progfuncs, sizeof(progstate_t) * maxprogs);
1927 sv_num_edicts = 1; //set up a safty buffer so things won't go horribly wrong too often
1928 sv_edicts=(struct edict_s *)&tempedict;
1929 prinst->edicttable = &sv_edicts;
1932 sv_num_edicts = numents; //should be fine
1934 // PR_Configure(crc, NULL, memsize, maxedicts, maxprogs);
1936 else if (!strcmp(qcc_token, "{"))
1940 if (numents == -1) //globals
1944 file = QCC_COM_Parse(file);
1945 if (qcc_token[0] == '}')
1947 else if (!qcc_token[0] || !file)
1948 Sys_Error("EOF when parsing global values");
1950 switch(current_progstate->intsize)
1954 if (!(d16 = ED_FindGlobal16(progfuncs, qcc_token)))
1956 file = QCC_COM_Parse(file);
1957 printf("global value %s not found", qcc_token);
1961 file = QCC_COM_Parse(file);
1962 ED_ParseEpair(progfuncs, pr_globals, (ddefXX_t*)d16, qcc_token, 16);
1966 if (!(d32 = ED_FindGlobal32(progfuncs, qcc_token)))
1968 file = QCC_COM_Parse(file);
1969 printf("global value %s not found", qcc_token);
1973 file = QCC_COM_Parse(file);
1974 ED_ParseEpair(progfuncs, pr_globals, (ddefXX_t*)d32, qcc_token, 32);
1978 Sys_Error("Bad intsize in LoadEnts");
1984 ed = (edictrun_t *)EDICT_NUM(progfuncs, numents);
1986 ed = ED_AllocIntoTable(progfuncs, numents);
1988 if (externs->entspawn)
1989 externs->entspawn((struct edict_s *) ed, true);
1991 sv_num_edicts = numents;
1993 file = ED_ParseEdict (progfuncs, file, ed);
1999 if (entsize == 0 && resethunk) //edicts have not yet been initialized, and this is a compleate load (memsize has been set)
2001 entsize = PR_InitEnts(progfuncs, maxedicts);
2002 // sv_num_edicts = numents;
2004 for (num = 0; num < numents; num++)
2006 ed = (edictrun_t *)EDICT_NUM(progfuncs, num);
2010 ed = ED_AllocIntoTable(progfuncs, num);
2016 if (!ed) //first entity
2017 ed = (edictrun_t *)EDICT_NUM(progfuncs, 0);
2019 ed = (edictrun_t *)ED_Alloc(progfuncs);
2021 if (externs->entspawn)
2022 externs->entspawn((struct edict_s *) ed, true);
2023 file = ED_ParseEdict(progfuncs, file, ed);
2025 if (killonspawnflags)
2027 var = GetEdictFieldValue (progfuncs, (struct edict_s *)ed, "spawnflags", &spawnflagscache);
2030 if ((int)var->_float & (int)killonspawnflags)
2044 CheckSpawn = PR_FindFunc(progfuncs, "CheckSpawn", -2);
2046 var = GetEdictFieldValue (progfuncs, (struct edict_s *)ed, "classname", NULL);
2047 if (!var || !var->string || !*PR_StringToNative(progfuncs, var->string))
2049 printf("No classname\n");
2050 ED_Free(progfuncs, (struct edict_s *)ed);
2056 //added by request of Mercury.
2057 if (fulldata) //this is a vital part of HL map support!!!
2058 { //essentually, it passes the ent's spawn info to the ent.
2059 char *nl; //otherwise it sees only the named fields of
2060 char *spawndata;//a standard quake ent.
2061 spawndata = PRHunkAlloc(progfuncs, file - datastart +1);
2062 strncpy(spawndata, datastart, file - datastart);
2063 spawndata[file - datastart] = '\0';
2064 for (nl = spawndata; *nl; nl++)
2067 fulldata->string = PR_StringToProgs(progfuncs, spawndata);
2070 selfvar = (eval_t *)((int *)pr_globals + ED_FindGlobalOfs(progfuncs, "self"));
2071 selfvar->edict = EDICT_TO_PROG(progfuncs, ed);
2073 //DP_SV_SPAWNFUNC_PREFIX support
2074 eclassname = PR_StringToNative(progfuncs, var->string);
2076 _snprintf(filename, sizeof(filename), "spawnfunc_%s", eclassname);
2077 filename[sizeof(filename)-1] = 0;
2079 snprintf(filename, sizeof(filename), "spawnfunc_%s", eclassname);
2081 f = PR_FindFunc(progfuncs, filename, PR_ANYBACK);
2083 f = PR_FindFunc(progfuncs, eclassname, PR_ANYBACK);
2088 G_INT(OFS_PARM0) = f;
2089 PR_ExecuteProgram(progfuncs, CheckSpawn);
2090 //call the spawn func or remove.
2093 PR_ExecuteProgram(progfuncs, f);
2095 else if (CheckSpawn)
2097 G_INT(OFS_PARM0) = 0;
2098 PR_ExecuteProgram(progfuncs, CheckSpawn);
2099 //the mod is responsible for freeing unrecognised ents.
2103 printf("Couldn't find spawn function %s\n", PR_StringToNative(progfuncs, var->string));
2104 ED_Free(progfuncs, (struct edict_s *)ed);
2110 Sys_Error("Command %s not recognised", qcc_token);
2115 if (externs->loadcompleate)
2116 externs->loadcompleate(entsize);
2118 sv_num_edicts = numents;
2126 return max_fields_size;
2129 #define AddS(str) strcpy(s, str);s+=strlen(str);
2131 char *SaveEnt (progfuncs_t *progfuncs, char *buf, int *size, struct edict_s *ed)
2135 unsigned int i;unsigned int j;
2147 for (i=0 ; i<numfields ; i++)
2153 len = strlen(name); // should we skip vars with no name?
2154 if (len > 2 && name[len-2] == '_')
2155 continue; // skip _x, _y, _z vars
2157 v = (int*)((edictrun_t*)ed)->fields + d->ofs;
2159 // if the value is still all 0, skip the field
2160 type = d->type & ~DEF_SAVEGLOBAL;
2161 for (j=0 ; j<type_size[type] ; j++)
2164 if (j == type_size[type])
2167 //add it to the file
2168 AddS (qcva("\"%s\" ",name));
2169 AddS (qcva("\"%s\"\n", PR_UglyValueString(progfuncs, d->type, (eval_t *)v)));
2178 struct edict_s *RestoreEnt (progfuncs_t *progfuncs, char *buf, int *size, struct edict_s *ed)
2183 buf = QCC_COM_Parse(buf); //read the key
2184 if (!buf || !*qcc_token)
2187 if (strcmp(qcc_token, "{"))
2188 Sys_Error("Restore Ent with no opening brace");
2191 ent = (edictrun_t *)ED_Alloc(progfuncs);
2193 ent = (edictrun_t *)ed;
2194 ent->isfree = false;
2196 if (externs->entspawn)
2197 externs->entspawn((struct edict_s *) ent, false);
2199 buf = ED_ParseEdict(progfuncs, buf, ent);
2201 *size = buf - start;
2203 return (struct edict_s *)ent;
2206 #define Host_Error Sys_Error
2208 //return true if pr_progs needs recompiling (source files have changed)
2209 pbool PR_TestRecompile(progfuncs_t *progfuncs)
2212 int num, found=0, lost=0, changed=0;
2213 includeddatafile_t *s;
2214 if (!pr_progs->ofsfiles)
2217 num = *(int*)((char *)pr_progs + pr_progs->ofsfiles);
2218 s = (includeddatafile_t *)((char *)pr_progs + pr_progs->ofsfiles+4);
2221 newsize = externs->FileSize(s->filename);
2222 if (newsize == -1) //ignore now missing files. - the referencer must have changed...
2224 else if (s->size != newsize) //file
2232 if (lost > found+changed)
2240 //this is for debugging.
2241 //I'm using this to detect incorrect string types while converting 32bit string pointers with bias to bound indexes.
2242 void PR_TestForWierdness(progfuncs_t *progfuncs)
2247 for (i = 0; i < pr_progs->numglobaldefs; i++)
2249 if ((pr_globaldefs16[i].type&~(DEF_SHARED|DEF_SAVEGLOBAL)) == ev_string)
2251 if (G_INT(pr_globaldefs16[i].ofs) < 0 || G_INT(pr_globaldefs16[i].ofs) >= addressableused)
2252 printf("String type irregularity on \"%s\" \"%s\"\n", pr_globaldefs16[i].s_name+progfuncs->stringtable, G_INT(pr_globaldefs16[i].ofs)+progfuncs->stringtable);
2256 for (i = 0; i < numfields; i++)
2258 if ((field[i].type&~(DEF_SHARED|DEF_SAVEGLOBAL)) == ev_string)
2260 for (e = 0; e < sv_num_edicts; e++)
2262 ed = (edictrun_t*)EDICT_NUM(progfuncs, e);
2265 if (((int *)ed->fields)[field[i].ofs] < 0 || ((int *)ed->fields)[field[i].ofs] >= addressableused)
2266 printf("String type irregularity \"%s\" \"%s\"\n", field[i].name, ((int *)ed->fields)[field[i].ofs]+progfuncs->stringtable);
2273 char *decode(int complen, int len, int method, char *info, char *buffer);
2279 int PR_ReallyLoadProgs (progfuncs_t *progfuncs, char *filename, int headercrc, progstate_t *progstate, pbool complain)
2281 unsigned int i, type;
2282 // extensionbuiltin_t *eb;
2286 // dfunction_t *f, *f2;
2294 // bool qfhack = false;
2295 pbool isfriked = false; //all imediate values were stripped, which causes problems with strings.
2296 pbool hexencalling = false; //hexen style calling convention. The opcodes themselves are used as part of passing the arguments;
2297 ddef16_t *gd16, *fld16;
2300 dstatement16_t *st16;
2302 int hmark=0xffffffff;
2304 int reorg = prinst->reorganisefields || numfields;
2308 current_progstate = progstate;
2310 strcpy(current_progstate->filename, filename);
2313 // flush the non-C variable lookup cache
2314 // for (i=0 ; i<GEFV_CACHESIZE ; i++)
2315 // gefvCache[i].field[0] = 0;
2317 memset(&spawnflagscache, 0, sizeof(evalc_t));
2319 if (externs->autocompile == PR_COMPILEALWAYS) //always compile before loading
2321 printf("Forcing compile of progs %s\n", filename);
2322 if (!CompileFile(progfuncs, filename))
2326 // CRC_Init (&pr_crc);
2329 if ((len=externs->FileSize(filename))<=0)
2331 if (externs->autocompile == PR_COMPILENEXIST || externs->autocompile == PR_COMPILECHANGED) //compile if file is not found (if 2, we have already tried, so don't bother)
2333 if (hmark==0xffffffff) //first try
2335 printf("couldn't open progs %s. Attempting to compile.\n", filename);
2336 CompileFile(progfuncs, filename);
2338 if ((len=externs->FileSize(filename))<0)
2340 printf("Couldn't find or compile file %s\n", filename);
2344 else if (externs->autocompile == PR_COMPILEIGNORE)
2348 printf("Couldn't find file %s\n", filename);
2353 hmark = PRHunkMark(progfuncs);
2354 pr_progs = PRHunkAlloc(progfuncs, len+1);
2355 if (!externs->ReadFile(filename, pr_progs, len+1))
2359 printf("Failed to open %s", filename);
2363 // for (i=0 ; i<len ; i++)
2364 // CRC_ProcessByte (&pr_crc, ((byte *)pr_progs)[i]);
2366 // byte swap the header
2368 for (i=0 ; i<standard_dprograms_t_size/sizeof(int); i++)
2369 ((int *)pr_progs)[i] = PRLittleLong ( ((int *)pr_progs)[i] );
2372 if (pr_progs->version == PROG_VERSION)
2374 // printf("Opening standard progs file \"%s\"\n", filename);
2375 current_progstate->intsize = 16;
2377 else if (pr_progs->version == PROG_EXTENDEDVERSION)
2380 for (i = standard_dprograms_t_size/sizeof(int); i < sizeof(dprograms_t)/sizeof(int); i++)
2381 ((int *)pr_progs)[i] = PRLittleLong ( ((int *)pr_progs)[i] );
2383 if (pr_progs->secondaryversion == PROG_SECONDARYVERSION16)
2385 // printf("Opening 16bit fte progs file \"%s\"\n", filename);
2386 current_progstate->intsize = 16;
2388 else if (pr_progs->secondaryversion == PROG_SECONDARYVERSION32)
2390 // printf("Opening 32bit fte progs file \"%s\"\n", filename);
2391 current_progstate->intsize = 32;
2395 // printf("Opening KK7 progs file \"%s\"\n", filename);
2396 current_progstate->intsize = 24; //KK progs. Yuck. Disabling saving would be a VERY good idea.
2397 pr_progs->version = PROG_VERSION; //not fte.
2401 printf ("Progs extensions are not compatible\nTry recompiling with the FTE compiler\n");
2409 printf ("%s has wrong version number (%i should be %i)\n", filename, pr_progs->version, PROG_VERSION);
2410 PRHunkFree(progfuncs, hmark);
2415 //progs contains enough info for use to recompile it.
2416 if (trysleft && (externs->autocompile == PR_COMPILECHANGED || externs->autocompile == PR_COMPILEEXISTANDCHANGED) && pr_progs->version == PROG_EXTENDEDVERSION)
2418 if (PR_TestRecompile(progfuncs))
2420 printf("Source file has changed\nRecompiling.\n");
2421 if (CompileFile(progfuncs, filename))
2423 PRHunkFree(progfuncs, hmark);
2431 if (!trysleft) //the progs exists, let's just be happy about it.
2432 printf("Progs is out of date and uncompilable\n");
2434 if (headercrc != -1 && headercrc != 0)
2435 if (pr_progs->crc != headercrc && pr_progs->crc != 0) //This shouldn't affect us. However, it does adjust expectations and usage of builtins.
2437 // printf ("%s system vars have been modified, progdefs.h is out of date\n", filename);
2438 PRHunkFree(progfuncs, hmark);
2443 if (pr_progs->version == PROG_EXTENDEDVERSION && pr_progs->blockscompressed && !QC_decodeMethodSupported(2))
2445 printf ("%s uses compression\n", filename);
2446 PRHunkFree(progfuncs, hmark);
2451 fnc = pr_functions = (dfunction_t *)((qbyte *)pr_progs + pr_progs->ofs_functions);
2452 pr_strings = ((char *)pr_progs + pr_progs->ofs_strings);
2453 current_progstate->globaldefs = *(void**)&gd16 = (void *)((qbyte *)pr_progs + pr_progs->ofs_globaldefs);
2454 current_progstate->fielddefs = *(void**)&fld16 = (void *)((qbyte *)pr_progs + pr_progs->ofs_fielddefs);
2455 current_progstate->statements = (void *)((qbyte *)pr_progs + pr_progs->ofs_statements);
2457 glob = pr_globals = (void *)((qbyte *)pr_progs + pr_progs->ofs_globals);
2461 if (pr_progs->version == PROG_EXTENDEDVERSION)
2463 if (pr_progs->ofslinenums)
2464 pr_linenums = (int *)((qbyte *)pr_progs + pr_progs->ofslinenums);
2465 if (pr_progs->ofs_types)
2466 pr_types = (typeinfo_t *)((qbyte *)pr_progs + pr_progs->ofs_types);
2468 //start decompressing stuff...
2469 if (pr_progs->blockscompressed & 1) //statements
2471 switch(current_progstate->intsize)
2474 len=sizeof(dstatement16_t)*pr_progs->numstatements;
2477 len=sizeof(dstatement32_t)*pr_progs->numstatements;
2480 Sys_Error("Bad intsize");
2482 s = PRHunkAlloc(progfuncs, len);
2483 QC_decode(progfuncs, PRLittleLong(*(int *)pr_statements16), len, 2, (char *)(((int *)pr_statements16)+1), s);
2485 current_progstate->statements = (dstatement16_t *)s;
2487 if (pr_progs->blockscompressed & 2) //global defs
2489 switch(current_progstate->intsize)
2492 len=sizeof(ddef16_t)*pr_progs->numglobaldefs;
2495 len=sizeof(ddef32_t)*pr_progs->numglobaldefs;
2498 Sys_Error("Bad intsize");
2500 s = PRHunkAlloc(progfuncs, len);
2501 QC_decode(progfuncs, PRLittleLong(*(int *)pr_globaldefs16), len, 2, (char *)(((int *)pr_globaldefs16)+1), s);
2503 gd16 = *(ddef16_t**)¤t_progstate->globaldefs = (ddef16_t *)s;
2505 if (pr_progs->blockscompressed & 4) //fields
2507 switch(current_progstate->intsize)
2510 len=sizeof(ddef16_t)*pr_progs->numglobaldefs;
2513 len=sizeof(ddef32_t)*pr_progs->numglobaldefs;
2516 Sys_Error("Bad intsize");
2518 s = PRHunkAlloc(progfuncs, len);
2519 QC_decode(progfuncs, PRLittleLong(*(int *)pr_fielddefs16), len, 2, (char *)(((int *)pr_fielddefs16)+1), s);
2521 *(ddef16_t**)¤t_progstate->fielddefs = (ddef16_t *)s;
2523 if (pr_progs->blockscompressed & 8) //functions
2525 len=sizeof(dfunction_t)*pr_progs->numfunctions;
2526 s = PRHunkAlloc(progfuncs, len);
2527 QC_decode(progfuncs, PRLittleLong(*(int *)pr_functions), len, 2, (char *)(((int *)pr_functions)+1), s);
2529 fnc = pr_functions = (dfunction_t *)s;
2531 if (pr_progs->blockscompressed & 16) //string table
2533 len=sizeof(char)*pr_progs->numstrings;
2534 s = PRHunkAlloc(progfuncs, len);
2535 QC_decode(progfuncs, PRLittleLong(*(int *)pr_strings), len, 2, (char *)(((int *)pr_strings)+1), s);
2537 pr_strings = (char *)s;
2539 if (pr_progs->blockscompressed & 32) //globals
2541 len=sizeof(float)*pr_progs->numglobals;
2542 s = PRHunkAlloc(progfuncs, len);
2543 QC_decode(progfuncs, PRLittleLong(*(int *)pr_globals), len, 2, (char *)(((int *)pr_globals)+1), s);
2545 glob = pr_globals = (float *)s;
2547 if (pr_linenums && pr_progs->blockscompressed & 64) //line numbers
2549 len=sizeof(int)*pr_progs->numstatements;
2550 s = PRHunkAlloc(progfuncs, len);
2551 QC_decode(progfuncs, PRLittleLong(*(int *)pr_linenums), len, 2, (char *)(((int *)pr_linenums)+1), s);
2553 pr_linenums = (int *)s;
2555 if (pr_types && pr_progs->blockscompressed & 128) //types
2557 len=sizeof(typeinfo_t)*pr_progs->numtypes;
2558 s = PRHunkAlloc(progfuncs, len);
2559 QC_decode(progfuncs, PRLittleLong(*(int *)pr_types), len, 2, (char *)(((int *)pr_types)+1), s);
2561 pr_types = (typeinfo_t *)s;
2565 len=sizeof(char)*pr_progs->numstrings;
2566 s = PRAddressableAlloc(progfuncs, len);
2567 memcpy(s, pr_strings, len);
2568 pr_strings = (char *)s;
2570 len=sizeof(float)*pr_progs->numglobals;
2571 s = PRAddressableAlloc(progfuncs, len);
2572 memcpy(s, pr_globals, len);
2573 glob = pr_globals = (float *)s;
2575 if (progfuncs->stringtable)
2576 stringadjust = pr_strings - progfuncs->stringtable;
2582 unsigned int lnotype = *(unsigned int*)"LNOF";
2583 unsigned int version = 1;
2587 ohm = PRHunkMark(progfuncs);
2589 strcpy(lnoname, filename);
2590 StripExtension(lnoname);
2591 strcat(lnoname, ".lno");
2592 if ((len=externs->FileSize(lnoname))>0)
2594 file = PRHunkAlloc(progfuncs, len+1);
2595 if (externs->ReadFile(lnoname, file, len+1))
2597 if ( file[0] != lnotype
2598 || file[1] != version
2599 || file[2] != pr_progs->numglobaldefs
2600 || file[3] != pr_progs->numglobals
2601 || file[4] != pr_progs->numfielddefs
2602 || file[5] != pr_progs->numstatements
2605 PRHunkFree(progfuncs, ohm); //whoops: old progs or incompatible
2608 pr_linenums = file + 6;
2614 // pr_strings = ((char *)pr_progs + pr_progs->ofs_strings);
2615 gd16 = *(ddef16_t**)¤t_progstate->globaldefs = (ddef16_t *)((qbyte *)pr_progs + pr_progs->ofs_globaldefs);
2616 fld16 = (ddef16_t *)((qbyte *)pr_progs + pr_progs->ofs_fielddefs);
2617 // pr_statements16 = (dstatement16_t *)((qbyte *)pr_progs + pr_progs->ofs_statements);
2619 st16 = pr_statements16;
2621 #undef pr_globaldefs16
2623 #undef pr_statements16
2624 #undef pr_fielddefs16
2627 current_progstate->edict_size = pr_progs->entityfields * 4 + externs->edictsize;
2629 // byte swap the lumps
2630 for (i=0 ; i<pr_progs->numfunctions; i++)
2633 fnc[i].first_statement = PRLittleLong (fnc[i].first_statement);
2634 fnc[i].parm_start = PRLittleLong (fnc[i].parm_start);
2635 fnc[i].s_name = (string_t)PRLittleLong ((long)fnc[i].s_name);
2636 fnc[i].s_file = (string_t)PRLittleLong ((long)fnc[i].s_file);
2637 fnc[i].numparms = PRLittleLong (fnc[i].numparms);
2638 fnc[i].locals = PRLittleLong (fnc[i].locals);
2640 /* if (!strncmp(fnc[i].s_name+pr_strings, "ext_", 4))
2642 for (eb = extensionbuiltin; eb; eb = eb->prev)
2644 if (*eb->name == '_')
2646 if (!strncmp(fnc[i].s_name+pr_strings+4, eb->name+1, strlen(eb->name+1)))
2648 fnc[i].first_statement = -0x7fffffff;
2649 *(void**)&fnc[i].profile = (void*)eb->func;
2653 else if (!strcmp(fnc[i].s_name+4, eb->name))
2655 fnc[i].first_statement = -0x7fffffff;
2656 *(void**)&fnc[i].profile = (void*)eb->func;
2662 fnc[i].s_name += stringadjust;
2663 fnc[i].s_file += stringadjust;
2666 //actual global values
2668 for (i=0 ; i<pr_progs->numglobals ; i++)
2669 ((int *)glob)[i] = PRLittleLong (((int *)glob)[i]);
2674 for (i=0 ; i<pr_progs->numtypes ; i++)
2677 pr_types[i].type = PRLittleLong(current_progstate->types[i].type);
2678 pr_types[i].next = PRLittleLong(current_progstate->types[i].next);
2679 pr_types[i].aux_type = PRLittleLong(current_progstate->types[i].aux_type);
2680 pr_types[i].num_parms = PRLittleLong(current_progstate->types[i].num_parms);
2681 pr_types[i].ofs = PRLittleLong(current_progstate->types[i].ofs);
2682 pr_types[i].size = PRLittleLong(current_progstate->types[i].size);
2683 pr_types[i].name = (char *)PRLittleLong((long)current_progstate->types[i].name);
2685 pr_types[i].name += stringadjust;
2690 reorg = (headercrc != -1);
2692 QC_FlushProgsOffsets(progfuncs);
2693 switch(current_progstate->intsize)
2697 //byteswap the globals and fix name offsets
2698 for (i=0 ; i<pr_progs->numglobaldefs ; i++)
2701 gd16[i].type = PRLittleShort (gd16[i].type);
2702 gd16[i].ofs = PRLittleShort (gd16[i].ofs);
2703 gd16[i].s_name = (string_t)PRLittleLong ((long)gd16[i].s_name);
2705 gd16[i].s_name += stringadjust;
2708 //byteswap fields and fix name offets. Also register the fields (which will result in some offset adjustments in the globals segment).
2709 for (i=0 ; i<pr_progs->numfielddefs ; i++)
2712 fld16[i].type = PRLittleShort (fld16[i].type);
2713 fld16[i].ofs = PRLittleShort (fld16[i].ofs);
2714 fld16[i].s_name = (string_t)PRLittleLong ((long)fld16[i].s_name);
2719 type = pr_types[fld16[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL)].type;
2721 type = fld16[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL);
2723 if (progfuncs->fieldadjust && !pr_typecurrent) //we need to make sure all fields appear in their original place.
2724 QC_RegisterFieldVar(progfuncs, type, fld16[i].s_name+pr_strings, 4*(fld16[i].ofs+progfuncs->fieldadjust), -1);
2725 else if (type == ev_vector) //emit vector vars early, so their fields cannot be alocated before the vector itself. (useful against scramblers)
2727 QC_RegisterFieldVar(progfuncs, type, fld16[i].s_name+pr_strings, -1, fld16[i].ofs);
2733 if (numfields+1>maxfields)
2737 nf = memalloc(sizeof(fdef_t) * maxfields);
2738 memcpy(nf, field, sizeof(fdef_t) * i);
2742 nf = &field[numfields];
2743 nf->name = fld16[i].s_name+pr_strings;
2744 nf->type = fld16[i].type;
2745 nf->progsofs = fld16[i].ofs;
2746 nf->ofs = fld16[i].ofs;
2748 if (fields_size < (nf->ofs+type_size[nf->type])*4)
2749 fields_size = (nf->ofs+type_size[nf->type])*4;
2753 fld16[i].s_name += stringadjust;
2755 if (reorg && !(progfuncs->fieldadjust && !pr_typecurrent))
2756 for (i=0 ; i<pr_progs->numfielddefs ; i++)
2759 type = pr_types[fld16[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL)].type;
2761 type = fld16[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL);
2762 if (type != ev_vector)
2763 QC_RegisterFieldVar(progfuncs, type, fld16[i].s_name+pr_strings-stringadjust, -1, fld16[i].ofs);
2768 for (i=0 ; i<pr_progs->numglobaldefs ; i++)
2771 pr_globaldefs32[i].type = PRLittleLong (pr_globaldefs32[i].type);
2772 pr_globaldefs32[i].ofs = PRLittleLong (pr_globaldefs32[i].ofs);
2773 pr_globaldefs32[i].s_name = (string_t)PRLittleLong ((long)pr_globaldefs32[i].s_name);
2775 pr_globaldefs32[i].s_name += stringadjust;
2778 for (i=0 ; i<pr_progs->numfielddefs ; i++)
2781 pr_fielddefs32[i].type = PRLittleLong (pr_fielddefs32[i].type);
2782 pr_fielddefs32[i].ofs = PRLittleLong (pr_fielddefs32[i].ofs);
2783 pr_fielddefs32[i].s_name = (string_t)PRLittleLong ((long)pr_fielddefs32[i].s_name);
2789 type = pr_types[pr_fielddefs32[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL)].type;
2791 type = pr_fielddefs32[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL);
2792 if (progfuncs->fieldadjust && !pr_typecurrent) //we need to make sure all fields appear in their original place.
2793 QC_RegisterFieldVar(progfuncs, type, fld16[i].s_name+pr_strings, 4*(fld16[i].ofs+progfuncs->fieldadjust), -1);
2794 else if (type == ev_vector)
2795 QC_RegisterFieldVar(progfuncs, type, pr_fielddefs32[i].s_name+pr_strings, -1, pr_fielddefs32[i].ofs);
2797 pr_fielddefs32[i].s_name += stringadjust;
2799 if (reorg && !(progfuncs->fieldadjust && !pr_typecurrent))
2800 for (i=0 ; i<pr_progs->numfielddefs ; i++)
2803 type = pr_types[pr_fielddefs32[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL)].type;
2805 type = pr_fielddefs32[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL);
2806 if (type != ev_vector)
2807 QC_RegisterFieldVar(progfuncs, type, pr_fielddefs32[i].s_name+pr_strings-stringadjust, -1, pr_fielddefs32[i].ofs);
2811 Sys_Error("Bad int size");
2814 //ifstring fixes arn't performed anymore.
2815 //the following switch just fixes endian and hexen2 calling conventions (by using different opcodes).
2816 switch(current_progstate->intsize)
2819 for (i=0 ; i<pr_progs->numstatements ; i++)
2822 st16[i].op = PRLittleShort(st16[i].op);
2823 st16[i].a = PRLittleShort(st16[i].a);
2824 st16[i].b = PRLittleShort(st16[i].b);
2825 st16[i].c = PRLittleShort(st16[i].c);
2827 if (st16[i].op >= OP_CALL1 && st16[i].op <= OP_CALL8)
2830 hexencalling = true;
2836 for (i=0 ; i<pr_progs->numstatements ; i++)
2838 if (st16[i].op >= OP_CALL1 && st16[i].op <= OP_CALL8)
2839 st16[i].op += OP_CALL1H - OP_CALL1;
2844 case 24: //24 sucks. Guess why.
2845 for (i=0 ; i<pr_progs->numstatements ; i++)
2848 pr_statements32[i].op = PRLittleLong(pr_statements32[i].op);
2849 pr_statements32[i].a = PRLittleLong(pr_statements32[i].a);
2850 pr_statements32[i].b = PRLittleLong(pr_statements32[i].b);
2851 pr_statements32[i].c = PRLittleLong(pr_statements32[i].c);
2853 if (pr_statements32[i].op >= OP_CALL1 && pr_statements32[i].op <= OP_CALL8)
2855 if (pr_statements32[i].b)
2856 hexencalling = true;
2862 for (i=0 ; i<pr_progs->numstatements ; i++)
2864 if (pr_statements32[i].op >= OP_CALL1 && pr_statements32[i].op <= OP_CALL8)
2865 pr_statements32[i].op += OP_CALL1H - OP_CALL1;
2870 for (i=0 ; i<pr_progs->numstatements ; i++)
2873 pr_statements32[i].op = PRLittleLong(pr_statements32[i].op);
2874 pr_statements32[i].a = PRLittleLong(pr_statements32[i].a);
2875 pr_statements32[i].b = PRLittleLong(pr_statements32[i].b);
2876 pr_statements32[i].c = PRLittleLong(pr_statements32[i].c);
2878 if (pr_statements32[i].op >= OP_CALL1 && pr_statements32[i].op <= OP_CALL8)
2880 if (pr_statements32[i].b)
2881 hexencalling = true;
2887 for (i=0 ; i<pr_progs->numstatements ; i++)
2889 if (pr_statements32[i].op >= OP_CALL1 && pr_statements32[i].op <= OP_CALL8)
2890 pr_statements32[i].op += OP_CALL1H - OP_CALL1;
2897 if (headercrc == -1)
2900 if (current_progstate->intsize != 16)
2901 Sys_Error("Decompiling a bigprogs");
2905 progstype = current_progstate-pr_progstate;
2907 // QC_StartShares(progfuncs);
2910 if (!pr_typecurrent) //progs 0 always acts as string stripped.
2911 isfriked = -1; //partly to avoid some bad progs.
2914 switch(current_progstate->intsize)
2918 for (i=0 ; i<pr_progs->numglobaldefs ; i++)
2921 type = pr_types[gd16[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL)].type;
2923 type = gd16[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL);
2925 if (gd16[i].type & DEF_SHARED)
2927 gd16[i].type &= ~DEF_SHARED;
2929 QC_AddSharedVar(progfuncs, gd16[i].ofs, pr_types[gd16[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL)].size);
2931 QC_AddSharedVar(progfuncs, gd16[i].ofs, type_size[type]);
2937 QC_AddSharedFieldVar(progfuncs, i, pr_strings - stringadjust);
2940 if (((unsigned int *)glob)[gd16[i].ofs]>=progstate->progs->numstrings)
2941 printf("Insane value\n");
2942 else if (isfriked != -1)
2944 if (pr_strings[((int *)glob)[gd16[i].ofs]]) //quakec uses string tables. 0 must remain null, or 'if (s)' can break.
2946 ((int *)glob)[gd16[i].ofs] += stringadjust;
2950 ((int *)glob)[gd16[i].ofs] = 0;
2954 if (((int *)glob)[gd16[i].ofs]) //don't change null funcs
2956 // if (fnc[((int *)glob)[gd16[i].ofs]].first_statement>=0) //this is a hack. Make all builtins switch to the main progs first. Allows builtin funcs to cache vars from just the main progs.
2957 ((int *)glob)[gd16[i].ofs] |= progstype << 24;
2964 for (i=0 ; i<pr_progs->numglobaldefs ; i++)
2967 type = pr_types[pr_globaldefs32[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL)].type;
2969 type = pr_globaldefs32[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL);
2971 if (pr_globaldefs32[i].type & DEF_SHARED)
2973 pr_globaldefs32[i].type &= ~DEF_SHARED;
2975 QC_AddSharedVar(progfuncs, pr_globaldefs32[i].ofs, pr_types[pr_globaldefs32[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL)].size);
2977 QC_AddSharedVar(progfuncs, pr_globaldefs32[i].ofs, type_size[type]);
2982 QC_AddSharedFieldVar(progfuncs, i, pr_strings - stringadjust);
2985 if (pr_strings[((int *)glob)[pr_globaldefs32[i].ofs]]) //quakec uses string tables. 0 must remain null, or 'if (s)' can break.
2987 ((int *)glob)[pr_globaldefs32[i].ofs] += stringadjust;
2992 if (((int *)glob)[pr_globaldefs32[i].ofs]) //don't change null funcs
2993 ((int *)glob)[pr_globaldefs32[i].ofs] |= progstype << 24;
2998 if (pr_progs->version == PROG_EXTENDEDVERSION && pr_progs->numbodylessfuncs)
3000 s = &((char *)pr_progs)[pr_progs->ofsbodylessfuncs];
3001 for (i = 0; i < pr_progs->numbodylessfuncs; i++)
3003 d32 = ED_FindGlobal32(progfuncs, s);
3004 d2 = ED_FindGlobalOfsFromProgs(progfuncs, s, 0, ev_function);
3006 Sys_Error("Runtime-linked function %s was not found in existing progs", s);
3008 Sys_Error("Couldn't find def for \"%s\"", s);
3009 ((int *)glob)[d32->ofs] = (*(func_t *)&pr_progstate[0].globals[*d2]);
3016 Sys_Error("Bad int size");
3019 if ((isfriked && pr_typecurrent)) //friked progs only allow one file.
3021 printf("You are trying to load a string-stripped progs as an addon.\nThis behaviour is not supported. Try removing some optimizations.");
3022 PRHunkFree(progfuncs, hmark);
3027 pr_strings+=stringadjust;
3028 if (!progfuncs->stringtable)
3029 progfuncs->stringtable = pr_strings;
3031 if (progfuncs->stringtablesize + progfuncs->stringtable < pr_strings + pr_progs->numstrings)
3032 progfuncs->stringtablesize = (pr_strings + pr_progs->numstrings) - progfuncs->stringtable;
3034 eval = PR_FindGlobal(progfuncs, "thisprogs", progstype);
3036 eval->prog = progstype;
3038 switch(current_progstate->intsize)
3041 if (pr_progs->version == PROG_EXTENDEDVERSION && pr_progs->numbodylessfuncs)
3043 s = &((char *)pr_progs)[pr_progs->ofsbodylessfuncs];
3044 for (i = 0; i < pr_progs->numbodylessfuncs; i++)
3046 d16 = ED_FindGlobal16(progfuncs, s);
3049 printf("Progs requires \"%s\" the external function \"%s\", but the definition was stripped", filename, s);
3050 PRHunkFree(progfuncs, hmark);
3055 ((int *)glob)[d16->ofs] = PR_FindFunc(progfuncs, s, PR_ANY);
3056 if (!((int *)glob)[d16->ofs])
3057 printf("Warning: Runtime-linked function %s was not found in primary progs (loading %s)", s, filename);
3059 d2 = ED_FindGlobalOfsFromProgs(progfuncs, s, 0, ev_function);
3061 Sys_Error("Runtime-linked function %s was not found in primary progs (loading %s)", s, filename);
3062 ((int *)glob)[d16->ofs] = (*(func_t *)&pr_progstate[0].globals[*d2]);
3069 break; //cannot happen anyway.
3071 if (pr_progs->version == PROG_EXTENDEDVERSION && pr_progs->numbodylessfuncs)
3073 s = &((char *)pr_progs)[pr_progs->ofsbodylessfuncs];
3074 for (i = 0; i < pr_progs->numbodylessfuncs; i++)
3076 d32 = ED_FindGlobal32(progfuncs, s);
3077 d2 = ED_FindGlobalOfsFromProgs(progfuncs, s, 0, ev_function);
3079 printf("Warning: Runtime-linked function %s was not found in existing progs", s);
3082 printf("Couldn't find def for \"%s\"", s);
3083 PRHunkFree(progfuncs, hmark);
3087 ((int *)glob)[d32->ofs] = (*(func_t *)&pr_progstate[0].globals[*d2]);
3095 eval = PR_FindGlobal(progfuncs, "__ext__fasttrackarrays", PR_CURRENT);
3096 if (eval) //we support these opcodes
3097 eval->_float = true;
3104 struct edict_s *EDICT_NUM(progfuncs_t *progfuncs, unsigned int n)
3107 Sys_Error ("QCLIB: EDICT_NUM: bad number %i", n);
3109 return prinst->edicttable[n];
3112 unsigned int NUM_FOR_EDICT(progfuncs_t *progfuncs, struct edict_s *e)
3114 edictrun_t *er = (edictrun_t*)e;
3115 if (er->entnum >= maxedicts)
3116 Sys_Error ("QCLIB: NUM_FOR_EDICT: bad pointer (%p)", e);