9 //this is windows all files are written with this endian standard. we do this to try to get a little more speed.
16 fdef_t *ED_FieldAtOfs (progfuncs_t *progfuncs, unsigned int ofs);
17 pbool ED_ParseEpair (progfuncs_t *progfuncs, int qcptr, unsigned int fldofs, int fldtype, char *s);
23 Sets everything to NULL
26 void QC_ClearEdict (progfuncs_t *progfuncs, struct edict_s *ed)
28 edictrun_t *e = (edictrun_t *)ed;
30 memset (e->fields, 0, fields_size);
35 edictrun_t *ED_AllocIntoTable (progfuncs_t *progfuncs, int num)
39 prinst->edicttable[num] = *(struct edict_s **)&e = (void*)memalloc(externs->edictsize);
40 memset(e, 0, externs->edictsize);
41 e->fields = PRAddressableExtend(progfuncs, fields_size);
43 QC_ClearEdict(progfuncs, (struct edict_s*)e);
52 Either finds a free edict, or allocates a new one.
53 Try to avoid reusing an entity that was recently freed, because it
54 can cause the client to think the entity morphed into something else
55 instead of being removed and recreated, which can cause interpolated
56 angles and bad trails.
59 struct edict_s *ED_Alloc (progfuncs_t *progfuncs)
64 for ( i=0 ; i<sv_num_edicts ; i++)
66 e = (edictrun_t*)EDICT_NUM(progfuncs, i);
67 // the first couple seconds of server time can involve a lot of
68 // freeing and allocating, so relax the replacement policy
69 if (!e || (e->isfree && ( e->freetime < 2 || *externs->gametime - e->freetime > 0.5 ) ))
72 e = ED_AllocIntoTable(progfuncs, i);
74 QC_ClearEdict (progfuncs, (struct edict_s*)e);
76 if (externs->entspawn)
77 externs->entspawn((struct edict_s *) e, false);
78 return (struct edict_s *)e;
82 if (i >= maxedicts-1) //try again, but use timed out ents.
84 for ( i=0 ; i<sv_num_edicts ; i++)
86 e = (edictrun_t*)EDICT_NUM(progfuncs, i);
87 // the first couple seconds of server time can involve a lot of
88 // freeing and allocating, so relax the replacement policy
89 if (!e || (e->isfree))
92 e = ED_AllocIntoTable(progfuncs, i);
94 QC_ClearEdict (progfuncs, (struct edict_s*)e);
96 if (externs->entspawn)
97 externs->entspawn((struct edict_s *) e, false);
98 return (struct edict_s *)e;
102 if (i >= maxedicts-2)
104 printf("Running out of edicts\n");
105 pr_trace = 1; //trip the debugger whilst it's still valid
107 if (i >= maxedicts-1)
111 buf = progfuncs->save_ents(progfuncs, NULL, &size, 0);
112 progfuncs->parms->WriteFile("edalloc.dump", buf, size);
113 Sys_Error ("ED_Alloc: no free edicts");
118 e = (edictrun_t*)EDICT_NUM(progfuncs, i);
121 e = ED_AllocIntoTable(progfuncs, i);
123 QC_ClearEdict (progfuncs, (struct edict_s*)e);
125 if (externs->entspawn)
126 externs->entspawn((struct edict_s *) e, false);
128 return (struct edict_s *)e;
135 Marks the edict as free
136 FIXME: walk all entities and NULL out references to this entity
139 void ED_Free (progfuncs_t *progfuncs, struct edict_s *ed)
141 edictrun_t *e = (edictrun_t *)ed;
142 // SV_UnlinkEdict (ed); // unlink from world bsp
144 if (e->isfree) //this happens on start.bsp where an onlyregistered trigger killtargets itself (when all of this sort die after 1 trigger anyway).
147 printf("Tried to free free entity within %s\n", pr_xfunction->s_name+progfuncs->stringtable);
149 printf("Engine tried to free free entity\n");
150 // if (developer.value == 1)
155 if (externs->entcanfree)
156 if (!externs->entcanfree(ed)) //can stop an ent from being freed.
160 e->freetime = (float)*externs->gametime;
164 ed->v.takedamage = 0;
165 ed->v.modelindex = 0;
169 VectorCopy (vec3_origin, ed->v.origin);
170 VectorCopy (vec3_origin, ed->v.angles);
171 ed->v.nextthink = -1;
176 //===========================================================================
183 ddef16_t *ED_GlobalAtOfs16 (progfuncs_t *progfuncs, int ofs)
188 for (i=0 ; i<pr_progs->numglobaldefs ; i++)
190 def = &pr_globaldefs16[i];
196 ddef32_t *ED_GlobalAtOfs32 (progfuncs_t *progfuncs, unsigned int ofs)
201 for (i=0 ; i<pr_progs->numglobaldefs ; i++)
203 def = &pr_globaldefs32[i];
215 fdef_t *ED_FieldAtOfs (progfuncs_t *progfuncs, unsigned int ofs)
220 for (i=0 ; i<numfields ; i++)
222 if (field[i].ofs == ofs)
232 fdef_t *ED_FindField (progfuncs_t *progfuncs, char *name)
236 for (i=0 ; i<numfields ; i++)
238 if (!strcmp(field[i].name, name) )
250 ddef16_t *ED_FindGlobal16 (progfuncs_t *progfuncs, char *name)
255 for (i=1 ; i<pr_progs->numglobaldefs ; i++)
257 def = &pr_globaldefs16[i];
258 if (!strcmp(def->s_name+progfuncs->stringtable,name) )
263 ddef32_t *ED_FindGlobal32 (progfuncs_t *progfuncs, char *name)
268 for (i=1 ; i<pr_progs->numglobaldefs ; i++)
270 def = &pr_globaldefs32[i];
271 if (!strcmp(def->s_name+progfuncs->stringtable,name) )
277 unsigned int ED_FindGlobalOfs (progfuncs_t *progfuncs, char *name)
281 switch(current_progstate->structtype)
285 d16 = ED_FindGlobal16(progfuncs, name);
286 return d16?d16->ofs:0;
289 d32 = ED_FindGlobal32(progfuncs, name);
290 return d32?d32->ofs:0;
292 Sys_Error("ED_FindGlobalOfs - bad struct type");
296 ddef16_t *ED_FindGlobalFromProgs16 (progfuncs_t *progfuncs, char *name, progsnum_t prnum)
301 for (i=1 ; i<pr_progstate[prnum].progs->numglobaldefs ; i++)
303 def = &pr_progstate[prnum].globaldefs16[i];
304 if (!strcmp(def->s_name+progfuncs->stringtable,name) )
309 ddef32_t *ED_FindGlobalFromProgs32 (progfuncs_t *progfuncs, char *name, progsnum_t prnum)
314 for (i=1 ; i<pr_progstate[prnum].progs->numglobaldefs ; i++)
316 def = &pr_progstate[prnum].globaldefs32[i];
317 if (!strcmp(def->s_name+progfuncs->stringtable,name) )
323 ddef16_t *ED_FindTypeGlobalFromProgs16 (progfuncs_t *progfuncs, char *name, progsnum_t prnum, int type)
328 for (i=1 ; i<pr_progstate[prnum].progs->numglobaldefs ; i++)
330 def = &pr_progstate[prnum].globaldefs16[i];
331 if (!strcmp(def->s_name+progfuncs->stringtable,name) )
333 if (pr_progstate[prnum].types)
335 if (pr_progstate[prnum].types[def->type&~DEF_SAVEGLOBAL].type != type)
338 else if ((def->type&(~DEF_SAVEGLOBAL)) != type)
347 ddef32_t *ED_FindTypeGlobalFromProgs32 (progfuncs_t *progfuncs, char *name, progsnum_t prnum, int type)
352 for (i=1 ; i<pr_progstate[prnum].progs->numglobaldefs ; i++)
354 def = &pr_progstate[prnum].globaldefs32[i];
355 if (!strcmp(def->s_name+progfuncs->stringtable,name) )
357 if (pr_progstate[prnum].types)
359 if (pr_progstate[prnum].types[def->type&~DEF_SAVEGLOBAL].type != type)
362 else if ((def->type&(~DEF_SAVEGLOBAL)) != (unsigned)type)
370 unsigned int *ED_FindGlobalOfsFromProgs (progfuncs_t *progfuncs, char *name, progsnum_t prnum, int type)
374 static unsigned int pos;
375 switch(pr_progstate[prnum].structtype)
379 def16 = ED_FindTypeGlobalFromProgs16(progfuncs, name, prnum, type);
386 def32 = ED_FindTypeGlobalFromProgs32(progfuncs, name, prnum, type);
391 Sys_Error("ED_FindGlobalOfsFromProgs - bad struct type");
400 dfunction_t *ED_FindFunction (progfuncs_t *progfuncs, char *name, progsnum_t *prnum, progsnum_t fromprogs)
410 sep = strchr(name, ':');
421 pnum = pr_typecurrent;
426 pnum = pr_typecurrent;
428 if ((unsigned)pnum > (unsigned)maxprogs)
430 printf("Progsnum %i out of bounds\n", pnum);
434 if (!pr_progstate[pnum].progs)
437 for (i=1 ; i<pr_progstate[pnum].progs->numfunctions ; i++)
439 func = &pr_progstate[pnum].functions[i];
440 if (!strcmp(func->s_name+progfuncs->stringtable,name) )
450 Returns a string describing *data in a type specific manner
453 char *PR_ValueString (progfuncs_t *progfuncs, etype_t type, eval_t *val)
455 static char line[256];
459 #ifdef DEF_SAVEGLOBAL
460 type &= ~DEF_SAVEGLOBAL;
463 if (current_progstate && pr_types)
464 type = pr_types[type].type;
469 sprintf (line, "struct");
472 sprintf (line, "union");
475 sprintf (line, "%s", PR_StringToNative(progfuncs, val->string));
478 fielddef = ED_FindField(progfuncs, "classname");
479 if (fielddef && val->edict < sv_num_edicts)
483 ed = (edictrun_t *)EDICT_NUM(progfuncs, val->edict);
484 v = (string_t *)((char *)edvars(ed) + fielddef->ofs*4);
485 sprintf (line, "entity %i(%s)", val->edict, PR_StringToNative(progfuncs, *v));
488 sprintf (line, "entity %i", val->edict);
492 sprintf (line, "NULL function");
495 if ((val->function & 0xff000000)>>24 >= (unsigned)maxprogs || !pr_progstate[(val->function & 0xff000000)>>24].functions)
496 sprintf (line, "Bad function");
499 if ((val->function &~0xff000000) >= pr_progs->numfunctions)
500 sprintf(line, "bad function %i:%i\n", (val->function & 0xff000000)>>24, val->function & ~0xff000000);
503 f = pr_progstate[(val->function & 0xff000000)>>24].functions + (val->function & ~0xff000000);
504 sprintf (line, "%i:%s()", (val->function & 0xff000000)>>24, f->s_name+progfuncs->stringtable);
510 fielddef = ED_FieldAtOfs (progfuncs, val->_int );
512 sprintf (line, ".??? (%i)", val->_int);
514 sprintf (line, ".%s (%i)", fielddef->name, val->_int);
517 sprintf (line, "void type");
520 sprintf (line, "%5.1f", val->_float);
523 sprintf (line, "%i", val->_int);
526 sprintf (line, "'%5.1f %5.1f %5.1f'", val->_vector[0], val->_vector[1], val->_vector[2]);
529 sprintf (line, "pointer");
535 sprintf (line, "NULL pointer");
539 sprintf(line, "UNKNOWN");
540 // entnum = ((qbyte *)val->edict - (qbyte *)sv_edicts) / pr_edict_size;
541 // valofs = (int *)val->edict - (int *)edvars(EDICT_NUM(progfuncs, entnum));
542 // fielddef = ED_FieldAtOfs (progfuncs, valofs );
544 // sprintf(line, "ent%i.%s", entnum, "UNKNOWN");
546 // sprintf(line, "ent%i.%s", entnum, fielddef->s_name);
550 sprintf (line, "bad type %i", type);
561 Returns a string describing *data in a type specific manner
562 Easier to parse than PR_ValueString
565 char *PR_UglyValueString (progfuncs_t *progfuncs, etype_t type, eval_t *val)
567 static char line[256];
572 #ifdef DEF_SAVEGLOBAL
573 type &= ~DEF_SAVEGLOBAL;
577 // type = pr_types[type].type;
582 sprintf (line, "structures cannot yet be saved");
585 sprintf (line, "unions cannot yet be saved");
590 int outb = sizeof(line)-2;
591 char *ins = PR_StringToNative(progfuncs, val->string);
592 //markup the output string.
593 while(*ins && outb > 0)
625 sprintf (line, "%i", val->_int);
628 i = (val->function & 0xff000000)>>24; //progs number
629 if ((unsigned)i >= maxprogs || !pr_progstate[(unsigned)i].progs)
630 sprintf (line, "BAD FUNCTION INDEX: %i", val->function);
633 j = (val->function & ~0xff000000); //function number
634 if ((unsigned)j >= pr_progstate[(unsigned)i].progs->numfunctions)
635 sprintf(line, "%i:%s", i, "CORRUPT FUNCTION POINTER");
638 f = pr_progstate[(unsigned)i].functions + j;
639 sprintf (line, "%i:%s", i, f->s_name+progfuncs->stringtable);
644 fielddef = ED_FieldAtOfs (progfuncs, val->_int );
645 sprintf (line, "%s", fielddef->name);
648 sprintf (line, "void");
651 if (val->_float == (int)val->_float)
652 sprintf (line, "%i", (int)val->_float); //an attempt to cut down on the number of .000000 vars..
654 sprintf (line, "%f", val->_float);
657 sprintf (line, "%i", val->_int);
660 if (val->_vector[0] == (int)val->_vector[0] && val->_vector[1] == (int)val->_vector[1] && val->_vector[2] == (int)val->_vector[2])
661 sprintf (line, "%i %i %i", (int)val->_vector[0], (int)val->_vector[1], (int)val->_vector[2]);
663 sprintf (line, "%f %f %f", val->_vector[0], val->_vector[1], val->_vector[2]);
666 sprintf (line, "bad type %i", type);
673 //compatible with Q1 (for savegames)
674 char *PR_UglyOldValueString (progfuncs_t *progfuncs, etype_t type, eval_t *val)
676 static char line[256];
680 #ifdef DEF_SAVEGLOBAL
681 type &= ~DEF_SAVEGLOBAL;
685 type = pr_types[type].type;
690 sprintf (line, "structures cannot yet be saved");
693 sprintf (line, "unions cannot yet be saved");
696 sprintf (line, "%s", PR_StringToNative(progfuncs, val->string));
699 sprintf (line, "%i", NUM_FOR_EDICT(progfuncs, (struct edict_s *)PROG_TO_EDICT(progfuncs, val->edict)));
702 f = pr_progstate[(val->function & 0xff000000)>>24].functions + (val->function & ~0xff000000);
703 sprintf (line, "%s", f->s_name+progfuncs->stringtable);
706 fielddef = ED_FieldAtOfs (progfuncs, val->_int );
707 sprintf (line, "%s", fielddef->name);
710 sprintf (line, "void");
713 if (val->_float == (int)val->_float)
714 sprintf (line, "%i", (int)val->_float); //an attempt to cut down on the number of .000000 vars..
716 sprintf (line, "%f", val->_float);
719 sprintf (line, "%i", val->_int);
722 if (val->_vector[0] == (int)val->_vector[0] && val->_vector[1] == (int)val->_vector[1] && val->_vector[2] == (int)val->_vector[2])
723 sprintf (line, "%i %i %i", (int)val->_vector[0], (int)val->_vector[1], (int)val->_vector[2]);
725 sprintf (line, "%f %f %f", val->_vector[0], val->_vector[1], val->_vector[2]);
729 sprintf (line, "bad type %i", type);
736 char *PR_TypeString(progfuncs_t *progfuncs, etype_t type)
738 #ifdef DEF_SAVEGLOBAL
739 type &= ~DEF_SAVEGLOBAL;
743 type = pr_types[type].type;
776 Returns a string with a description and the contents of a global,
777 padded to 20 field width
780 char *PR_GlobalString (progfuncs_t *progfuncs, int ofs)
787 static char line[128];
789 switch (current_progstate->structtype)
793 val = (void *)&pr_globals[ofs];
794 def16 = ED_GlobalAtOfs16(progfuncs, ofs);
796 sprintf (line,"%i(?""?""?)", ofs);
799 s = PR_ValueString (progfuncs, def16->type, val);
800 sprintf (line,"%i(%s)%s", ofs, def16->s_name+progfuncs->stringtable, s);
810 val = (void *)&pr_globals[ofs];
811 def32 = ED_GlobalAtOfs32(progfuncs, ofs);
813 sprintf (line,"%i(?""?""?)", ofs);
816 s = PR_ValueString (progfuncs, def32->type, val);
817 sprintf (line,"%i(%s)%s", ofs, def32->s_name+progfuncs->stringtable, s);
826 Sys_Error("Bad struct type in PR_GlobalString");
830 char *PR_GlobalStringNoContents (progfuncs_t *progfuncs, int ofs)
835 static char line[128];
837 switch (current_progstate->structtype)
841 def16 = ED_GlobalAtOfs16(progfuncs, ofs);
843 sprintf (line,"%i(?""?""?)", ofs);
845 sprintf (line,"%i(%s)", ofs, def16->s_name+progfuncs->stringtable);
849 def32 = ED_GlobalAtOfs32(progfuncs, ofs);
851 sprintf (line,"%i(?""?""?)", ofs);
853 sprintf (line,"%i(%s)", ofs, def32->s_name+progfuncs->stringtable);
856 Sys_Error("Bad struct type in PR_GlobalStringNoContents");
875 void ED_Print (progfuncs_t *progfuncs, struct edict_s *ed)
880 unsigned int i;unsigned int j;
884 if (((edictrun_t *)ed)->isfree)
890 printf("\nEDICT %i:\n", NUM_FOR_EDICT(progfuncs, (struct edict_s *)ed));
891 for (i=1 ; i<numfields ; i++)
896 if (l >= 2 && name[l-2] == '_')
897 continue; // skip _x, _y, _z vars
899 v = (int *)((char *)edvars(ed) + d->ofs*4);
901 // if the value is still all 0, skip the field
902 #ifdef DEF_SAVEGLOBAL
903 type = d->type & ~DEF_SAVEGLOBAL;
908 for (j=0 ; j<type_size[type] ; j++)
911 if (j == type_size[type])
919 printf ("%s\n", PR_ValueString(progfuncs, d->type, (eval_t *)v));
923 void ED_PrintNum (progfuncs_t *progfuncs, int ent)
925 ED_Print (progfuncs, EDICT_NUM(progfuncs, ent));
932 For debugging, prints all the entities in the current server
935 void ED_PrintEdicts (progfuncs_t *progfuncs)
939 printf ("%i entities\n", sv_num_edicts);
940 for (i=0 ; i<sv_num_edicts ; i++)
941 ED_PrintNum (progfuncs, i);
951 void ED_Count (progfuncs_t *progfuncs)
955 unsigned int active, models, solid, step;
957 active = models = solid = step = 0;
958 for (i=0 ; i<sv_num_edicts ; i++)
960 ent = (edictrun_t *)EDICT_NUM(progfuncs, i);
968 // if (ent->v.movetype == MOVETYPE_STEP)
972 printf ("num_edicts:%3i\n", sv_num_edicts);
973 printf ("active :%3i\n", active);
974 // Con_Printf ("view :%3i\n", models);
975 // Con_Printf ("touch :%3i\n", solid);
976 // Con_Printf ("step :%3i\n", step);
982 //============================================================================
990 char *ED_NewString (progfuncs_t *progfuncs, char *string, int minlength)
997 l = strlen(string) + 1;
999 newc = progfuncs->AddressableAlloc (progfuncs, l<minlength?minlength:l);
1001 return progfuncs->stringtable;
1005 for (i=0 ; i< l ; i++)
1007 if (string[i] == '\\' && i < l-1 && string[i+1] != 0)
1010 if (string[i] == 'n')
1016 *new_p++ = string[i];
1027 Can parse either fields or globals
1028 returns false if error
1031 pbool ED_ParseEpair (progfuncs_t *progfuncs, int qcptr, unsigned int fldofs, int fldtype, char *s)
1039 int type = fldtype & ~DEF_SAVEGLOBAL;
1040 qcptr += fldofs*sizeof(int);
1045 st = PR_StringToProgs(progfuncs, ED_NewString (progfuncs, s, 0));
1046 *(string_t *)(progfuncs->stringtable + qcptr) = st;
1050 *(float *)(progfuncs->stringtable + qcptr) = (float)atof (s);
1054 *(int *)(progfuncs->stringtable + qcptr) = atoi (s);
1061 for (i=0 ; i<3 ; i++)
1063 while (*v && *v != ' ')
1067 ((float *)(progfuncs->stringtable + qcptr))[i] = (float)atof (w);
1073 ((float *)(progfuncs->stringtable + qcptr))[i] = (float)atof (w);
1080 *(int *)(progfuncs->stringtable + qcptr) = atoi (s);
1084 def = ED_FindField (progfuncs, s);
1087 printf ("Can't find field %s\n", s);
1090 *(int *)(progfuncs->stringtable + qcptr) = def->ofs;
1094 if (s[1]==':'&&s[2]=='\0')
1096 *(func_t *)(progfuncs->stringtable + qcptr) = 0;
1099 func = ED_FindFunction (progfuncs, s, &i, -1);
1102 printf ("Can't find function %s\n", s);
1105 *(func_t *)(progfuncs->stringtable + qcptr) = (func - pr_progstate[i].functions) | (i<<24);
1115 ====================
1118 Parses an edict out of the given string, returning the new position
1119 ed should be a properly initialized empty edict.
1120 Used for initial level load and for savegames.
1121 ====================
1124 char *ED_ParseEdict (progfuncs_t *progfuncs, char *data, edictrun_t *ent)
1137 // if (ent != (edictrun_t *)sv_edicts) // hack
1138 // memset (ent+1, 0, pr_edict_size - sizeof(edictrun_t));
1140 // go through all the dictionary pairs
1144 data = QCC_COM_Parse (data);
1145 if (qcc_token[0] == '}')
1151 if (qcc_token[0] == '{' && !qcc_token[1])
1155 printf ("ED_ParseEntity: EOF without closing brace\n");
1161 strncpy (keyname, qcc_token, sizeof(keyname)-1);
1162 keyname[sizeof(keyname)-1] = 0;
1164 // another hack to fix heynames with trailing spaces
1165 n = strlen(keyname);
1166 while (n && keyname[n-1] == ' ')
1173 data = QCC_COM_Parse (data);
1176 printf ("ED_ParseEntity: EOF without closing brace\n");
1180 if (qcc_token[0] == '}')
1182 printf ("ED_ParseEntity: closing brace without data\n");
1188 // keynames with a leading underscore are used for utility comments,
1189 // and are immediately discarded by quake
1190 if (keyname[0] == '_')
1193 key = ED_FindField (progfuncs, keyname);
1196 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!
1198 if ((key = ED_FindField (progfuncs, "angles")))
1200 sprintf (qcc_token, "0 %f 0", atof(qcc_token)); //change it from yaw to 3d angle
1204 if (!strcmp(keyname, "light")) //Quake lighthack - allows a field name and a classname to go by the same thing in the level editor
1205 if ((key = ED_FindField (progfuncs, "light_lev")))
1207 if (externs->badfield && externs->badfield(progfuncs, (struct edict_s*)ent, keyname, qcc_token))
1209 printf ("'%s' is not a field\n", keyname);
1214 if (!ED_ParseEpair (progfuncs, (char*)ent->fields - progfuncs->stringtable, key->ofs, key->type, qcc_token))
1217 // Sys_Error ("ED_ParseEdict: parse error on entities");
1232 The entities are directly placed in the array, rather than allocated with
1233 ED_Alloc, because otherwise an error loading the map would have entity
1234 number references out of order.
1236 Creates a server's entity / program execution context by
1237 parsing textual entity definitions out of an ent file.
1239 Used for both fresh maps and savegame loads. A fresh map would also need
1240 to call ED_CallSpawnFunctions () to let the objects initialize themselves.
1244 char *ED_WriteGlobals(progfuncs_t *progfuncs, char *buffer) //switch first.
1246 #define AddS(str) strcpy(buffer, str);buffer+=strlen(str);
1254 int curprogs = pr_typecurrent;
1256 switch(current_progstate->structtype)
1260 for (i=0 ; i<pr_progs->numglobaldefs ; i++)
1262 def16 = &pr_globaldefs16[i];
1263 name = def16->s_name + progfuncs->stringtable;
1267 if (name[len-2] == '_' && (name[len-1] == 'x' || name[len-1] == 'y' || name[len-1] == 'z'))
1268 continue; // skip _x, _y, _z vars (vector components, which are saved as one vector not 3 floats)
1272 #ifdef DEF_SAVEGLOBAL
1273 if ( !(def16->type & DEF_SAVEGLOBAL) )
1275 type &= ~DEF_SAVEGLOBAL;
1277 if (current_progstate->types)
1278 type = current_progstate->types[type].type;
1279 if (type == ev_function)
1281 v = (int *)¤t_progstate->globals[def16->ofs];
1282 if ((v[0]&0xff000000)>>24 == (unsigned)curprogs) //same progs
1284 if (!progfuncs->stringtable[current_progstate->functions[v[0]&0x00ffffff].s_name])
1286 else if (!strcmp(current_progstate->functions[v[0]&0x00ffffff].s_name+ progfuncs->stringtable, name)) //names match. Assume function is at initial value.
1291 if ((v[0]&0xff000000)>>24 == 0)
1292 if (!ED_FindFunction(progfuncs, name, NULL, curprogs)) //defined as extern
1294 if (!progfuncs->stringtable[pr_progstate[0].functions[v[0]&0x00ffffff].s_name])
1296 else if (!strcmp(pr_progstate[0].functions[v[0]&0x00ffffff].s_name + progfuncs->stringtable, name)) //same name.
1300 //else function has been redirected externally.
1303 else if (type != ev_string //anything other than these is not saved
1305 && type != ev_integer
1306 && type != ev_entity
1307 && type != ev_vector)
1310 v = (int *)¤t_progstate->globals[def16->ofs];
1312 // make sure the value is not null, where there's no point in saving
1313 for (j=0 ; j<type_size[type] ; j++)
1316 if (j == type_size[type])
1320 AddS (qcva("\"%s\" ", name));
1321 AddS (qcva("\"%s\"\n", PR_UglyValueString(progfuncs, def16->type&~DEF_SAVEGLOBAL, (eval_t *)v)));
1326 for (i=0 ; i<pr_progs->numglobaldefs ; i++)
1328 def32 = &pr_globaldefs32[i];
1329 name = def32->s_name + progfuncs->stringtable;
1330 if (name[strlen(name)-2] == '_')
1331 continue; // skip _x, _y, _z vars (vector components, which are saved as one vector not 3 floats)
1335 #ifdef DEF_SAVEGLOBAL
1336 if ( !(def32->type & DEF_SAVEGLOBAL) )
1338 type &= ~DEF_SAVEGLOBAL;
1340 if (current_progstate->types)
1341 type = current_progstate->types[type].type;
1342 if (type == ev_function)
1344 v = (int *)¤t_progstate->globals[def32->ofs];
1345 if ((v[0]&0xff000000)>>24 == (unsigned)curprogs) //same progs
1346 if (!strcmp(current_progstate->functions[v[0]&0x00ffffff].s_name+ progfuncs->stringtable, name)) //names match. Assume function is at initial value.
1350 if ((v[0]&0xff000000)>>24 == 0)
1351 if (!ED_FindFunction(progfuncs, name, NULL, curprogs)) //defined as extern
1352 if (!strcmp(pr_progstate[0].functions[v[0]&0x00ffffff].s_name+ progfuncs->stringtable, name)) //same name.
1355 //else function has been redirected externally.
1358 else if (type != ev_string //anything other than these is not saved
1360 && type != ev_integer
1361 && type != ev_entity
1362 && type != ev_vector)
1365 v = (int *)¤t_progstate->globals[def32->ofs];
1367 // make sure the value is not null, where there's no point in saving
1368 for (j=0 ; j<type_size[type] ; j++)
1371 if (j == type_size[type])
1374 AddS (qcva("\"%s\" ", name));
1375 AddS (qcva("\"%s\"\n", PR_UglyValueString(progfuncs, def32->type&~DEF_SAVEGLOBAL, (eval_t *)v)));
1379 Sys_Error("Bad struct type in SaveEnts");
1385 char *ED_WriteEdict(progfuncs_t *progfuncs, edictrun_t *ed, char *buffer, pbool q1compatible)
1390 unsigned int i;unsigned int j;
1395 for (i=0 ; i<numfields ; i++)
1400 if (len>4 && (name[len-2] == '_' && (name[len-1] == 'x' || name[len-1] == 'y' || name[len-1] == 'z')))
1401 continue; // skip _x, _y, _z vars
1403 v = (int *)((char*)ed->fields + d->ofs*4);
1405 // if the value is still all 0, skip the field
1406 #ifdef DEF_SAVEGLOBAL
1407 type = d->type & ~DEF_SAVEGLOBAL;
1412 for (j=0 ; j<type_size[type] ; j++)
1415 if (j == type_size[type])
1418 //add it to the file
1419 AddS (qcva("\"%s\" ",name));
1420 AddS (qcva("\"%s\"\n", (q1compatible?PR_UglyOldValueString:PR_UglyValueString)(progfuncs, d->type, (eval_t *)v)));
1427 char *SaveCallStack (progfuncs_t *progfuncs, char *s)
1429 #define AddS(str) strcpy(s, str);s+=strlen(str);
1442 AddS ("<NO STACK>\n");
1446 globalbase = (int *)pr_globals + pr_xfunction->parm_start + pr_xfunction->locals;
1448 pr_stack[pr_depth].f = pr_xfunction;
1449 for (i=pr_depth ; i>0 ; i--)
1455 AddS ("<NO FUNCTION>\n");
1459 if (pr_stack[i].progsnum != progs)
1461 progs = pr_stack[i].progsnum;
1463 sprintf(buffer, "//%i %s\n", progs, pr_progstate[progs].filename);
1467 sprintf(buffer, "\t\"%i:%s\"\n", progs, f->s_name+progfuncs->stringtable);
1469 sprintf(buffer, "\t\"%i:%s\" //%s\n", progs, f->s_name+progfuncs->stringtable, f->s_file+progfuncs->stringtable);
1473 for (arg = 0; arg < f->locals; arg++)
1476 local = ED_GlobalAtOfs16(progfuncs, f->parm_start+arg);
1478 sprintf(buffer, "\t\tofs%i %i // %f\n", f->parm_start+arg, *(int *)(globalbase - f->locals+arg), *(float *)(globalbase - f->locals+arg) );
1481 if (local->type == ev_entity)
1483 sprintf(buffer, "\t\t\"%s\" \"entity %i\"\n", local->s_name+progfuncs->stringtable, ((eval_t*)(globalbase - f->locals+arg))->edict);
1486 sprintf(buffer, "\t\t\"%s\"\t\"%s\"\n", local->s_name+progfuncs->stringtable, PR_ValueString(progfuncs, local->type, (eval_t*)(globalbase - f->locals+arg)));
1488 if (local->type == ev_vector)
1496 globalbase = localstack + localstack_used - f->locals;
1498 globalbase -= f->locals;
1505 //there are two ways of saving everything.
1506 //0 is to save just the entities.
1507 //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)
1508 char *SaveEnts(progfuncs_t *progfuncs, char *mem, int *len, int alldata)
1510 #define AddS(str) strcpy(s, str);s+=strlen(str);
1520 os = s = memalloc(5*1024*1024);
1523 { //special Q1 savegame compatability mode.
1524 //engine will need to store references to progs type and will need to preload the progs and inti the ents itself before loading.
1526 //Make sure there is only 1 progs loaded.
1527 for (a = 1; a < maxprogs; a++)
1529 if (pr_progstate[a].progs)
1532 if (!pr_progstate[0].progs || a != maxprogs) //the state of the progs wasn't Q1 compatible.
1541 oldprogs = pr_typecurrent;
1542 PR_SwitchProgs(progfuncs, 0);
1544 s = ED_WriteGlobals(progfuncs, s);
1546 PR_SwitchProgs(progfuncs, oldprogs);
1552 for (a = 0; a < sv_num_edicts; a++)
1554 edictrun_t *ed = (edictrun_t *)EDICT_NUM(progfuncs, a);
1559 s = ED_WriteEdict(progfuncs, ed, s, true);
1570 AddS("general {\n");
1571 AddS(qcva("\"maxprogs\" \"%i\"\n", maxprogs));
1572 // AddS(qcva("\"maxentities\" \"%i\"\n", maxedicts));
1573 // AddS(qcva("\"mem\" \"%i\"\n", hunksize));
1574 // AddS(qcva("\"crc\" \"%i\"\n", header_crc));
1575 AddS(qcva("\"numentities\" \"%i\"\n", sv_num_edicts));
1578 oldprogs = pr_typecurrent;
1580 for (a = 0; a < maxprogs; a++)
1582 if (!pr_progstate[a].progs)
1584 PR_SwitchProgs(progfuncs, a);
1586 AddS (qcva("progs %i {\n", a));
1587 AddS (qcva("\"filename\" \"%s\"\n", pr_progstate[a].filename));
1588 AddS (qcva("\"crc\" \"%i\"\n", pr_progs->crc));
1589 AddS (qcva("\"numbuiltins\" \"%i\"\n", current_progstate->numbuiltins));
1597 AddS("stacktrace {\n");
1598 s = SaveCallStack(progfuncs, s);
1602 for (a = 0; a < maxprogs; a++) //I would mix, but external functions rely on other progs being loaded
1604 if (!pr_progstate[a].progs)
1607 AddS (qcva("globals %i {\n", a));
1609 PR_SwitchProgs(progfuncs, a);
1611 s = ED_WriteGlobals(progfuncs, s);
1615 PR_SwitchProgs(progfuncs, oldprogs);
1617 for (a = 0; a < sv_num_edicts; a++)
1619 edictrun_t *ed = (edictrun_t *)EDICT_NUM(progfuncs, a);
1624 AddS (qcva("entity %i{\n", a));
1626 s = ED_WriteEdict(progfuncs, ed, s, false);
1639 //if 'general' block is found, this is a compleate state, otherwise, we should spawn entities like
1640 int LoadEnts(progfuncs_t *progfuncs, char *file, float killonspawnflags)
1642 eval_t *fulldata; //this is part of FTE_FULLSPAWNDATA
1645 eval_t *selfvar = NULL;
1651 edictrun_t *ed=NULL;
1654 func_t CheckSpawn=0;
1655 void *oldglobals = NULL;
1656 int oldglobalssize = 0;
1658 extern edictrun_t tempedict;
1666 if (!strncmp(file, "loadgame", 8))
1677 fulldata = PR_FindGlobal(progfuncs, "__fullspawndata", PR_ANY, NULL);
1683 file = QCC_COM_Parse(file);
1685 break; //finished reading file
1686 else if (!strcmp(qcc_token, "Version"))
1688 file = QCC_COM_Parse(file);
1689 //qcc_token is a version number
1691 else if (!strcmp(qcc_token, "entity"))
1693 if (entsize == 0 && resethunk) //edicts have not yet been initialized, and this is a compleate load (memsize has been set)
1695 entsize = PR_InitEnts(progfuncs, maxedicts);
1696 // sv_num_edicts = numents;
1698 for (num = 0; num < numents; num++)
1700 ed = (edictrun_t *)EDICT_NUM(progfuncs, num);
1704 ed = ED_AllocIntoTable(progfuncs, num);
1706 if (externs->entspawn)
1707 externs->entspawn((struct edict_s *) ed, true);
1712 file = QCC_COM_Parse(file);
1713 num = atoi(qcc_token);
1714 file = QCC_COM_Parse(file);
1715 if (qcc_token[0] != '{')
1716 Sys_Error("Progs loading found %s, not '{'", qcc_token);
1718 ed = (edictrun_t *)ED_Alloc(progfuncs);
1721 ed = (edictrun_t *)EDICT_NUM(progfuncs, num);
1725 Sys_Error("Edict was not allocated\n");
1726 ed = ED_AllocIntoTable(progfuncs, num);
1730 if (externs->entspawn)
1731 externs->entspawn((struct edict_s *) ed, true);
1732 file = ED_ParseEdict(progfuncs, file, ed);
1734 if (killonspawnflags)
1736 var = GetEdictFieldValue (progfuncs, (struct edict_s *)&ed, "spawnflags", &spawnflagscache);
1739 if ((int)var->_float & (int)killonspawnflags)
1750 if ((var = GetEdictFieldValue (progfuncs, (struct edict_s *)ed, "classname", NULL)))
1752 f = ED_FindFunction(progfuncs, var->string + progfuncs->stringtable, NULL, -1);
1755 var = (eval_t *)((int *)pr_globals + ED_FindGlobalOfs(progfuncs, "self"));
1756 var->edict = EDICT_TO_PROG(progfuncs, ed);
1757 PR_ExecuteProgram(progfuncs, f-pr_functions);
1762 else if (!strcmp(qcc_token, "progs"))
1764 file = QCC_COM_Parse(file);
1765 num = atoi(qcc_token);
1766 file = QCC_COM_Parse(file);
1767 if (qcc_token[0] != '{')
1768 Sys_Error("Progs loading found %s, not '{'", qcc_token);
1777 file = QCC_COM_Parse(file); //read the key
1779 Sys_Error("EOF in progs block");
1781 if (!strcmp("filename", qcc_token)) //check key get and save values
1782 {file = QCC_COM_Parse(file); strcpy(filename, qcc_token);}
1783 else if (!strcmp("crc", qcc_token))
1784 {file = QCC_COM_Parse(file); header_crc = atoi(qcc_token);}
1785 else if (!strcmp("numbuiltins", qcc_token))
1786 {file = QCC_COM_Parse(file); numbuiltins = atoi(qcc_token);}
1787 else if (qcc_token[0] == '}') //end of block
1790 Sys_Error("Bad key \"%s\" in progs block", qcc_token);
1793 PR_ReallyLoadProgs(progfuncs, filename, header_crc, &pr_progstate[num], true);
1794 if (!externs->builtinsfor)
1796 // Sys_Error("Couldn't reset the builtin functions");
1797 current_progstate->builtins = NULL; //these are specific, we assume the global ones were set via pr_configure
1798 current_progstate->numbuiltins = 0;
1802 current_progstate->builtins = externs->builtinsfor(num, header_crc);
1803 current_progstate->numbuiltins = numbuiltins;
1806 if (num == 0 && oldglobals)
1808 if (pr_progstate[0].globals_size == oldglobalssize)
1809 memcpy(pr_progstate[0].globals, oldglobals, pr_progstate[0].globals_size);
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->structtype)
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, (char*)pr_globals - progfuncs->stringtable, d16->ofs, d16->type, qcc_token);
1869 if (!(d32 = ED_FindGlobal32(progfuncs, qcc_token)))
1871 file = QCC_COM_Parse(file);
1872 printf("global value %s not found", qcc_token);
1876 file = QCC_COM_Parse(file);
1877 ED_ParseEpair(progfuncs, (char*)pr_globals - progfuncs->stringtable, d32->ofs, d32->type, qcc_token);
1881 Sys_Error("Bad struct type in LoadEnts");
1885 // file = QCC_COM_Parse(file);
1886 // if (com_token[0] != '}')
1887 // Sys_Error("Progs loading found %s, not '}'", qcc_token);
1889 else if (!strcmp(qcc_token, "general"))
1891 QC_StartShares(progfuncs);
1892 // QC_InitShares(); //forget stuff
1893 // pr_edict_size = 0;
1896 file = QCC_COM_Parse(file);
1897 if (qcc_token[0] != '{')
1898 Sys_Error("Progs loading found %s, not '{'", qcc_token);
1902 file = QCC_COM_Parse(file); //read the key
1904 Sys_Error("EOF in general block");
1906 if (!strcmp("maxprogs", qcc_token)) //check key get and save values
1907 {file = QCC_COM_Parse(file); maxprogs = atoi(qcc_token);}
1908 // else if (!strcmp("maxentities", com_token))
1909 // {file = QCC_COM_Parse(file); maxedicts = atoi(qcc_token);}
1910 // else if (!strcmp("mem", com_token))
1911 // {file = QCC_COM_Parse(file); memsize = atoi(qcc_token);}
1912 // else if (!strcmp("crc", com_token))
1913 // {file = QCC_COM_Parse(file); crc = atoi(qcc_token);}
1914 else if (!strcmp("numentities", qcc_token))
1915 {file = QCC_COM_Parse(file); numents = atoi(qcc_token);}
1916 else if (qcc_token[0] == '}') //end of block
1919 Sys_Error("Bad key \"%s\" in general block", qcc_token);
1925 if (pr_progstate[0].globals_size)
1927 oldglobals = malloc(pr_progstate[0].globals_size);
1930 oldglobalssize = pr_progstate[0].globals_size;
1931 memcpy(oldglobals, pr_progstate[0].globals, oldglobalssize);
1934 printf("Unable to alloc %i bytes\n", pr_progstate[0].globals_size);
1937 PRAddressableFlush(progfuncs, -1);
1940 pr_progstate = PRHunkAlloc(progfuncs, sizeof(progstate_t) * maxprogs);
1943 sv_num_edicts = 1; //set up a safty buffer so things won't go horribly wrong too often
1944 sv_edicts=(struct edict_s *)&tempedict;
1945 prinst->edicttable = &sv_edicts;
1948 sv_num_edicts = numents; //should be fine
1950 // PR_Configure(crc, NULL, memsize, maxedicts, maxprogs);
1952 else if (!strcmp(qcc_token, "{"))
1956 if (numents == -1) //globals
1960 file = QCC_COM_Parse(file);
1961 if (qcc_token[0] == '}')
1963 else if (!qcc_token[0] || !file)
1964 Sys_Error("EOF when parsing global values");
1966 switch(current_progstate->structtype)
1970 if (!(d16 = ED_FindGlobal16(progfuncs, qcc_token)))
1972 file = QCC_COM_Parse(file);
1973 printf("global value %s not found", qcc_token);
1977 file = QCC_COM_Parse(file);
1978 ED_ParseEpair(progfuncs, (char*)pr_globals - progfuncs->stringtable, d16->ofs, d16->type, qcc_token);
1983 if (!(d32 = ED_FindGlobal32(progfuncs, qcc_token)))
1985 file = QCC_COM_Parse(file);
1986 printf("global value %s not found", qcc_token);
1990 file = QCC_COM_Parse(file);
1991 ED_ParseEpair(progfuncs, (char*)pr_globals - progfuncs->stringtable, d32->ofs, d32->type, qcc_token);
1995 Sys_Error("Bad struct type in LoadEnts");
2001 ed = (edictrun_t *)EDICT_NUM(progfuncs, numents);
2003 ed = ED_AllocIntoTable(progfuncs, numents);
2005 if (externs->entspawn)
2006 externs->entspawn((struct edict_s *) ed, true);
2008 sv_num_edicts = numents;
2010 file = ED_ParseEdict (progfuncs, file, ed);
2016 if (entsize == 0 && resethunk) //edicts have not yet been initialized, and this is a compleate load (memsize has been set)
2018 entsize = PR_InitEnts(progfuncs, maxedicts);
2019 // sv_num_edicts = numents;
2021 for (num = 0; num < numents; num++)
2023 ed = (edictrun_t *)EDICT_NUM(progfuncs, num);
2027 ed = ED_AllocIntoTable(progfuncs, num);
2033 if (!ed) //first entity
2034 ed = (edictrun_t *)EDICT_NUM(progfuncs, 0);
2036 ed = (edictrun_t *)ED_Alloc(progfuncs);
2038 if (externs->entspawn)
2039 externs->entspawn((struct edict_s *) ed, true);
2040 file = ED_ParseEdict(progfuncs, file, ed);
2042 if (killonspawnflags)
2044 var = GetEdictFieldValue (progfuncs, (struct edict_s *)ed, "spawnflags", &spawnflagscache);
2047 if ((int)var->_float & (int)killonspawnflags)
2061 CheckSpawn = PR_FindFunc(progfuncs, "CheckSpawn", -2);
2063 var = GetEdictFieldValue (progfuncs, (struct edict_s *)ed, "classname", NULL);
2064 if (!var || !var->string || !*PR_StringToNative(progfuncs, var->string))
2066 printf("No classname\n");
2067 ED_Free(progfuncs, (struct edict_s *)ed);
2071 //added by request of Mercury.
2072 if (fulldata) //this is a vital part of HL map support!!!
2073 { //essentually, it passes the ent's spawn info to the ent.
2074 char *nl; //otherwise it sees only the named fields of
2075 char *spawndata;//a standard quake ent.
2076 spawndata = PRHunkAlloc(progfuncs, file - datastart +1);
2077 strncpy(spawndata, datastart, file - datastart);
2078 spawndata[file - datastart] = '\0';
2079 for (nl = spawndata; *nl; nl++)
2082 fulldata->string = PR_StringToProgs(progfuncs, spawndata);
2086 selfvar = PR_FindGlobal(progfuncs, "self", PR_ANY, NULL);
2088 selfvar->edict = EDICT_TO_PROG(progfuncs, ed);
2090 //DP_SV_SPAWNFUNC_PREFIX support
2091 eclassname = PR_StringToNative(progfuncs, var->string);
2093 _snprintf(filename, sizeof(filename), "spawnfunc_%s", eclassname);
2094 filename[sizeof(filename)-1] = 0;
2096 snprintf(filename, sizeof(filename), "spawnfunc_%s", eclassname);
2098 f = PR_FindFunc(progfuncs, filename, PR_ANYBACK);
2100 f = PR_FindFunc(progfuncs, eclassname, PR_ANYBACK);
2105 G_INT(OFS_PARM0) = f;
2106 PR_ExecuteProgram(progfuncs, CheckSpawn);
2107 //call the spawn func or remove.
2110 PR_ExecuteProgram(progfuncs, f);
2112 else if (CheckSpawn)
2114 G_INT(OFS_PARM0) = 0;
2115 PR_ExecuteProgram(progfuncs, CheckSpawn);
2116 //the mod is responsible for freeing unrecognised ents.
2120 printf("Couldn't find spawn function %s\n", PR_StringToNative(progfuncs, var->string));
2121 ED_Free(progfuncs, (struct edict_s *)ed);
2127 Sys_Error("Command %s not recognised", qcc_token);
2132 if (externs->loadcompleate)
2133 externs->loadcompleate(entsize);
2135 sv_num_edicts = numents;
2147 return max_fields_size;
2150 #define AddS(str) strcpy(s, str);s+=strlen(str);
2152 char *SaveEnt (progfuncs_t *progfuncs, char *buf, int *size, struct edict_s *ed)
2156 unsigned int i;unsigned int j;
2168 for (i=0 ; i<numfields ; i++)
2174 len = strlen(name); // should we skip vars with no name?
2175 if (len > 2 && name[len-2] == '_')
2176 continue; // skip _x, _y, _z vars
2178 v = (int*)((edictrun_t*)ed)->fields + d->ofs;
2180 // if the value is still all 0, skip the field
2181 type = d->type & ~DEF_SAVEGLOBAL;
2182 for (j=0 ; j<type_size[type] ; j++)
2185 if (j == type_size[type])
2188 //add it to the file
2189 AddS (qcva("\"%s\" ",name));
2190 AddS (qcva("\"%s\"\n", PR_UglyValueString(progfuncs, d->type, (eval_t *)v)));
2199 struct edict_s *RestoreEnt (progfuncs_t *progfuncs, char *buf, int *size, struct edict_s *ed)
2204 buf = QCC_COM_Parse(buf); //read the key
2205 if (!buf || !*qcc_token)
2208 if (strcmp(qcc_token, "{"))
2209 Sys_Error("Restore Ent with no opening brace");
2212 ent = (edictrun_t *)ED_Alloc(progfuncs);
2214 ent = (edictrun_t *)ed;
2215 ent->isfree = false;
2217 if (externs->entspawn)
2218 externs->entspawn((struct edict_s *) ent, false);
2220 buf = ED_ParseEdict(progfuncs, buf, ent);
2222 *size = buf - start;
2224 return (struct edict_s *)ent;
2227 #define Host_Error Sys_Error
2229 //return true if pr_progs needs recompiling (source files have changed)
2230 pbool PR_TestRecompile(progfuncs_t *progfuncs)
2233 int num, found=0, lost=0, changed=0;
2234 includeddatafile_t *s;
2235 if (!pr_progs->ofsfiles)
2238 num = *(int*)((char *)pr_progs + pr_progs->ofsfiles);
2239 s = (includeddatafile_t *)((char *)pr_progs + pr_progs->ofsfiles+4);
2242 newsize = externs->FileSize(s->filename);
2243 if (newsize == -1) //ignore now missing files. - the referencer must have changed...
2245 else if (s->size != newsize) //file
2253 if (lost > found+changed)
2261 //this is for debugging.
2262 //I'm using this to detect incorrect string types while converting 32bit string pointers with bias to bound indexes.
2263 void PR_TestForWierdness(progfuncs_t *progfuncs)
2268 for (i = 0; i < pr_progs->numglobaldefs; i++)
2270 if ((pr_globaldefs16[i].type&~(DEF_SHARED|DEF_SAVEGLOBAL)) == ev_string)
2272 if (G_INT(pr_globaldefs16[i].ofs) < 0 || G_INT(pr_globaldefs16[i].ofs) >= addressableused)
2273 printf("String type irregularity on \"%s\" \"%s\"\n", pr_globaldefs16[i].s_name+progfuncs->stringtable, G_INT(pr_globaldefs16[i].ofs)+progfuncs->stringtable);
2277 for (i = 0; i < numfields; i++)
2279 if ((field[i].type&~(DEF_SHARED|DEF_SAVEGLOBAL)) == ev_string)
2281 for (e = 0; e < sv_num_edicts; e++)
2283 ed = (edictrun_t*)EDICT_NUM(progfuncs, e);
2286 if (((int *)ed->fields)[field[i].ofs] < 0 || ((int *)ed->fields)[field[i].ofs] >= addressableused)
2287 printf("String type irregularity \"%s\" \"%s\"\n", field[i].name, ((int *)ed->fields)[field[i].ofs]+progfuncs->stringtable);
2294 char *decode(int complen, int len, int method, char *info, char *buffer);
2300 int PR_ReallyLoadProgs (progfuncs_t *progfuncs, char *filename, int headercrc, progstate_t *progstate, pbool complain)
2302 unsigned int i, type;
2303 // extensionbuiltin_t *eb;
2307 // dfunction_t *f, *f2;
2315 // bool qfhack = false;
2316 pbool isfriked = false; //all imediate values were stripped, which causes problems with strings.
2317 pbool hexencalling = false; //hexen style calling convention. The opcodes themselves are used as part of passing the arguments;
2318 ddef16_t *gd16, *fld16;
2321 dstatement16_t *st16;
2323 int hmark=0xffffffff;
2325 int reorg = prinst->reorganisefields || numfields;
2329 current_progstate = progstate;
2331 strcpy(current_progstate->filename, filename);
2334 // flush the non-C variable lookup cache
2335 // for (i=0 ; i<GEFV_CACHESIZE ; i++)
2336 // gefvCache[i].field[0] = 0;
2338 memset(&spawnflagscache, 0, sizeof(evalc_t));
2340 if (externs->autocompile == PR_COMPILEALWAYS) //always compile before loading
2342 printf("Forcing compile of progs %s\n", filename);
2343 if (!CompileFile(progfuncs, filename))
2347 // CRC_Init (&pr_crc);
2350 if ((len=externs->FileSize(filename))<=0)
2352 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)
2354 if (hmark==0xffffffff) //first try
2356 printf("couldn't open progs %s. Attempting to compile.\n", filename);
2357 CompileFile(progfuncs, filename);
2359 if ((len=externs->FileSize(filename))<0)
2361 printf("Couldn't find or compile file %s\n", filename);
2365 else if (externs->autocompile == PR_COMPILEIGNORE)
2369 printf("Couldn't find file %s\n", filename);
2374 hmark = PRHunkMark(progfuncs);
2375 pr_progs = PRHunkAlloc(progfuncs, len+1);
2376 if (!externs->ReadFile(filename, pr_progs, len+1))
2380 printf("Failed to open %s", filename);
2384 // for (i=0 ; i<len ; i++)
2385 // CRC_ProcessByte (&pr_crc, ((byte *)pr_progs)[i]);
2387 // byte swap the header
2389 for (i=0 ; i<standard_dprograms_t_size/sizeof(int); i++)
2390 ((int *)pr_progs)[i] = PRLittleLong ( ((int *)pr_progs)[i] );
2393 if (pr_progs->version == PROG_VERSION)
2395 // printf("Opening standard progs file \"%s\"\n", filename);
2396 current_progstate->structtype = PST_DEFAULT;
2398 else if (pr_progs->version == PROG_QTESTVERSION)
2400 current_progstate->structtype = PST_QTEST;
2402 else if (pr_progs->version == PROG_EXTENDEDVERSION)
2405 for (i = standard_dprograms_t_size/sizeof(int); i < sizeof(dprograms_t)/sizeof(int); i++)
2406 ((int *)pr_progs)[i] = PRLittleLong ( ((int *)pr_progs)[i] );
2408 if (pr_progs->secondaryversion == PROG_SECONDARYVERSION16)
2410 // printf("Opening 16bit fte progs file \"%s\"\n", filename);
2411 current_progstate->structtype = PST_DEFAULT;
2413 else if (pr_progs->secondaryversion == PROG_SECONDARYVERSION32)
2415 // printf("Opening 32bit fte progs file \"%s\"\n", filename);
2416 current_progstate->structtype = PST_FTE32;
2420 // printf("Opening KK7 progs file \"%s\"\n", filename);
2421 current_progstate->structtype = PST_KKQWSV; //KK progs. Yuck. Disabling saving would be a VERY good idea.
2422 pr_progs->version = PROG_VERSION; //not fte.
2426 printf ("Progs extensions are not compatible\nTry recompiling with the FTE compiler\n");
2434 printf ("%s has wrong version number (%i should be %i)\n", filename, pr_progs->version, PROG_VERSION);
2435 PRHunkFree(progfuncs, hmark);
2440 //progs contains enough info for use to recompile it.
2441 if (trysleft && (externs->autocompile == PR_COMPILECHANGED || externs->autocompile == PR_COMPILEEXISTANDCHANGED) && pr_progs->version == PROG_EXTENDEDVERSION)
2443 if (PR_TestRecompile(progfuncs))
2445 printf("Source file has changed\nRecompiling.\n");
2446 if (CompileFile(progfuncs, filename))
2448 PRHunkFree(progfuncs, hmark);
2456 if (!trysleft) //the progs exists, let's just be happy about it.
2457 printf("Progs is out of date and uncompilable\n");
2459 if (headercrc != -1 && headercrc != 0)
2460 if (pr_progs->crc != headercrc && pr_progs->crc != 0) //This shouldn't affect us. However, it does adjust expectations and usage of builtins.
2462 // printf ("%s system vars have been modified, progdefs.h is out of date\n", filename);
2463 PRHunkFree(progfuncs, hmark);
2468 if (pr_progs->version == PROG_EXTENDEDVERSION && pr_progs->blockscompressed && !QC_decodeMethodSupported(2))
2470 printf ("%s uses compression\n", filename);
2471 PRHunkFree(progfuncs, hmark);
2476 fnc = pr_functions = (dfunction_t *)((qbyte *)pr_progs + pr_progs->ofs_functions);
2477 pr_strings = ((char *)pr_progs + pr_progs->ofs_strings);
2478 current_progstate->globaldefs = *(void**)&gd16 = (void *)((qbyte *)pr_progs + pr_progs->ofs_globaldefs);
2479 current_progstate->fielddefs = *(void**)&fld16 = (void *)((qbyte *)pr_progs + pr_progs->ofs_fielddefs);
2480 current_progstate->statements = (void *)((qbyte *)pr_progs + pr_progs->ofs_statements);
2482 glob = pr_globals = (void *)((qbyte *)pr_progs + pr_progs->ofs_globals);
2483 current_progstate->globals_size = pr_progs->numglobals*sizeof(*pr_globals);
2487 if (pr_progs->version == PROG_EXTENDEDVERSION)
2489 if (pr_progs->ofslinenums)
2490 pr_linenums = (int *)((qbyte *)pr_progs + pr_progs->ofslinenums);
2491 if (pr_progs->ofs_types)
2492 pr_types = (typeinfo_t *)((qbyte *)pr_progs + pr_progs->ofs_types);
2494 //start decompressing stuff...
2495 if (pr_progs->blockscompressed & 1) //statements
2497 switch(current_progstate->structtype)
2500 len=sizeof(dstatement16_t)*pr_progs->numstatements;
2503 len=sizeof(dstatement32_t)*pr_progs->numstatements;
2506 Sys_Error("Bad struct type");
2508 s = PRHunkAlloc(progfuncs, len);
2509 QC_decode(progfuncs, PRLittleLong(*(int *)pr_statements16), len, 2, (char *)(((int *)pr_statements16)+1), s);
2511 current_progstate->statements = (dstatement16_t *)s;
2513 if (pr_progs->blockscompressed & 2) //global defs
2515 switch(current_progstate->structtype)
2518 len=sizeof(ddef16_t)*pr_progs->numglobaldefs;
2521 len=sizeof(ddef32_t)*pr_progs->numglobaldefs;
2524 Sys_Error("Bad struct type");
2526 s = PRHunkAlloc(progfuncs, len);
2527 QC_decode(progfuncs, PRLittleLong(*(int *)pr_globaldefs16), len, 2, (char *)(((int *)pr_globaldefs16)+1), s);
2529 gd16 = *(ddef16_t**)¤t_progstate->globaldefs = (ddef16_t *)s;
2531 if (pr_progs->blockscompressed & 4) //fields
2533 switch(current_progstate->structtype)
2536 len=sizeof(ddef16_t)*pr_progs->numglobaldefs;
2539 len=sizeof(ddef32_t)*pr_progs->numglobaldefs;
2542 Sys_Error("Bad struct type");
2544 s = PRHunkAlloc(progfuncs, len);
2545 QC_decode(progfuncs, PRLittleLong(*(int *)pr_fielddefs16), len, 2, (char *)(((int *)pr_fielddefs16)+1), s);
2547 *(ddef16_t**)¤t_progstate->fielddefs = (ddef16_t *)s;
2549 if (pr_progs->blockscompressed & 8) //functions
2551 len=sizeof(dfunction_t)*pr_progs->numfunctions;
2552 s = PRHunkAlloc(progfuncs, len);
2553 QC_decode(progfuncs, PRLittleLong(*(int *)pr_functions), len, 2, (char *)(((int *)pr_functions)+1), s);
2555 fnc = pr_functions = (dfunction_t *)s;
2557 if (pr_progs->blockscompressed & 16) //string table
2559 len=sizeof(char)*pr_progs->numstrings;
2560 s = PRHunkAlloc(progfuncs, len);
2561 QC_decode(progfuncs, PRLittleLong(*(int *)pr_strings), len, 2, (char *)(((int *)pr_strings)+1), s);
2563 pr_strings = (char *)s;
2565 if (pr_progs->blockscompressed & 32) //globals
2567 len=sizeof(float)*pr_progs->numglobals;
2568 s = PRHunkAlloc(progfuncs, len);
2569 QC_decode(progfuncs, PRLittleLong(*(int *)pr_globals), len, 2, (char *)(((int *)pr_globals)+1), s);
2571 glob = pr_globals = (float *)s;
2573 if (pr_linenums && pr_progs->blockscompressed & 64) //line numbers
2575 len=sizeof(int)*pr_progs->numstatements;
2576 s = PRHunkAlloc(progfuncs, len);
2577 QC_decode(progfuncs, PRLittleLong(*(int *)pr_linenums), len, 2, (char *)(((int *)pr_linenums)+1), s);
2579 pr_linenums = (int *)s;
2581 if (pr_types && pr_progs->blockscompressed & 128) //types
2583 len=sizeof(typeinfo_t)*pr_progs->numtypes;
2584 s = PRHunkAlloc(progfuncs, len);
2585 QC_decode(progfuncs, PRLittleLong(*(int *)pr_types), len, 2, (char *)(((int *)pr_types)+1), s);
2587 pr_types = (typeinfo_t *)s;
2591 len=sizeof(char)*pr_progs->numstrings;
2592 s = PRAddressableExtend(progfuncs, len);
2593 memcpy(s, pr_strings, len);
2594 pr_strings = (char *)s;
2596 len=sizeof(float)*pr_progs->numglobals;
2597 s = PRAddressableExtend(progfuncs, len);
2598 memcpy(s, pr_globals, len);
2599 glob = pr_globals = (float *)s;
2601 if (progfuncs->stringtable)
2602 stringadjust = pr_strings - progfuncs->stringtable;
2608 unsigned int lnotype = *(unsigned int*)"LNOF";
2609 unsigned int version = 1;
2613 ohm = PRHunkMark(progfuncs);
2615 strcpy(lnoname, filename);
2616 StripExtension(lnoname);
2617 strcat(lnoname, ".lno");
2618 if ((len=externs->FileSize(lnoname))>0)
2620 file = PRHunkAlloc(progfuncs, len+1);
2621 if (externs->ReadFile(lnoname, file, len+1))
2623 if ( file[0] != lnotype
2624 || file[1] != version
2625 || file[2] != pr_progs->numglobaldefs
2626 || file[3] != pr_progs->numglobals
2627 || file[4] != pr_progs->numfielddefs
2628 || file[5] != pr_progs->numstatements
2631 PRHunkFree(progfuncs, ohm); //whoops: old progs or incompatible
2634 pr_linenums = file + 6;
2640 // pr_strings = ((char *)pr_progs + pr_progs->ofs_strings);
2641 gd16 = *(ddef16_t**)¤t_progstate->globaldefs = (ddef16_t *)((qbyte *)pr_progs + pr_progs->ofs_globaldefs);
2642 fld16 = (ddef16_t *)((qbyte *)pr_progs + pr_progs->ofs_fielddefs);
2643 // pr_statements16 = (dstatement16_t *)((qbyte *)pr_progs + pr_progs->ofs_statements);
2645 st16 = pr_statements16;
2647 #undef pr_globaldefs16
2649 #undef pr_statements16
2650 #undef pr_fielddefs16
2653 current_progstate->edict_size = pr_progs->entityfields * 4 + externs->edictsize;
2655 // byte swap the lumps
2656 switch(current_progstate->structtype)
2659 // qtest needs a struct remap
2660 for (i=0 ; i<pr_progs->numfunctions; i++)
2663 qtest_function_t qtfunc = ((qtest_function_t*)fnc)[i];
2665 fnc[i].first_statement = PRLittleLong (qtfunc.first_statement);
2666 fnc[i].parm_start = PRLittleLong (qtfunc.parm_start);
2667 fnc[i].s_name = (string_t)PRLittleLong (qtfunc.s_name);
2668 fnc[i].s_file = (string_t)PRLittleLong (qtfunc.s_file);
2669 fnc[i].numparms = PRLittleLong (qtfunc.numparms);
2670 fnc[i].locals = PRLittleLong (qtfunc.locals);
2672 for (j=0; j<MAX_PARMS;j++)
2673 fnc[i].parm_size[j] = PRLittleLong (qtfunc.parm_size[j]);
2675 fnc[i].s_name += stringadjust;
2676 fnc[i].s_file += stringadjust;
2682 for (i=0 ; i<pr_progs->numfunctions; i++)
2685 fnc[i].first_statement = PRLittleLong (fnc[i].first_statement);
2686 fnc[i].parm_start = PRLittleLong (fnc[i].parm_start);
2687 fnc[i].s_name = (string_t)PRLittleLong ((long)fnc[i].s_name);
2688 fnc[i].s_file = (string_t)PRLittleLong ((long)fnc[i].s_file);
2689 fnc[i].numparms = PRLittleLong (fnc[i].numparms);
2690 fnc[i].locals = PRLittleLong (fnc[i].locals);
2692 fnc[i].s_name += stringadjust;
2693 fnc[i].s_file += stringadjust;
2697 Sys_Error("Bad struct type");
2700 //actual global values
2702 for (i=0 ; i<pr_progs->numglobals ; i++)
2703 ((int *)glob)[i] = PRLittleLong (((int *)glob)[i]);
2708 for (i=0 ; i<pr_progs->numtypes ; i++)
2711 pr_types[i].type = PRLittleLong(current_progstate->types[i].type);
2712 pr_types[i].next = PRLittleLong(current_progstate->types[i].next);
2713 pr_types[i].aux_type = PRLittleLong(current_progstate->types[i].aux_type);
2714 pr_types[i].num_parms = PRLittleLong(current_progstate->types[i].num_parms);
2715 pr_types[i].ofs = PRLittleLong(current_progstate->types[i].ofs);
2716 pr_types[i].size = PRLittleLong(current_progstate->types[i].size);
2717 pr_types[i].name = PRLittleLong(current_progstate->types[i].name);
2719 pr_types[i].name += stringadjust;
2724 reorg = (headercrc != -1);
2726 QC_FlushProgsOffsets(progfuncs);
2727 switch(current_progstate->structtype)
2731 //byteswap the globals and fix name offsets
2732 for (i=0 ; i<pr_progs->numglobaldefs ; i++)
2735 gd16[i].type = PRLittleShort (gd16[i].type);
2736 gd16[i].ofs = PRLittleShort (gd16[i].ofs);
2737 gd16[i].s_name = (string_t)PRLittleLong ((long)gd16[i].s_name);
2739 gd16[i].s_name += stringadjust;
2742 //byteswap fields and fix name offets. Also register the fields (which will result in some offset adjustments in the globals segment).
2743 for (i=0 ; i<pr_progs->numfielddefs ; i++)
2746 fld16[i].type = PRLittleShort (fld16[i].type);
2747 fld16[i].ofs = PRLittleShort (fld16[i].ofs);
2748 fld16[i].s_name = (string_t)PRLittleLong ((long)fld16[i].s_name);
2753 type = pr_types[fld16[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL)].type;
2755 type = fld16[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL);
2757 if (progfuncs->fieldadjust && !pr_typecurrent) //we need to make sure all fields appear in their original place.
2758 QC_RegisterFieldVar(progfuncs, type, fld16[i].s_name+pr_strings, 4*(fld16[i].ofs+progfuncs->fieldadjust), -1);
2759 else if (type == ev_vector) //emit vector vars early, so their fields cannot be alocated before the vector itself. (useful against scramblers)
2761 QC_RegisterFieldVar(progfuncs, type, fld16[i].s_name+pr_strings, -1, fld16[i].ofs);
2767 if (numfields+1>maxfields)
2771 nf = memalloc(sizeof(fdef_t) * maxfields);
2772 memcpy(nf, field, sizeof(fdef_t) * i);
2776 nf = &field[numfields];
2777 nf->name = fld16[i].s_name+pr_strings;
2778 nf->type = fld16[i].type;
2779 nf->progsofs = fld16[i].ofs;
2780 nf->ofs = fld16[i].ofs;
2782 if (fields_size < (nf->ofs+type_size[nf->type])*4)
2783 fields_size = (nf->ofs+type_size[nf->type])*4;
2787 fld16[i].s_name += stringadjust;
2789 if (reorg && !(progfuncs->fieldadjust && !pr_typecurrent))
2790 for (i=0 ; i<pr_progs->numfielddefs ; i++)
2793 type = pr_types[fld16[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL)].type;
2795 type = fld16[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL);
2796 if (type != ev_vector)
2797 QC_RegisterFieldVar(progfuncs, type, fld16[i].s_name+pr_strings-stringadjust, -1, fld16[i].ofs);
2802 // qtest needs a struct remap
2803 for (i=0 ; i<pr_progs->numglobaldefs ; i++)
2805 qtest_def_t qtdef = ((qtest_def_t *)pr_globaldefs32)[i];
2807 pr_globaldefs32[i].type = qtdef.type;
2808 pr_globaldefs32[i].s_name = qtdef.s_name;
2809 pr_globaldefs32[i].ofs = qtdef.ofs;
2811 for (i=0 ; i<pr_progs->numfielddefs ; i++)
2813 qtest_def_t qtdef = ((qtest_def_t *)pr_fielddefs32)[i];
2815 pr_fielddefs32[i].type = qtdef.type;
2816 pr_fielddefs32[i].s_name = qtdef.s_name;
2817 pr_fielddefs32[i].ofs = qtdef.ofs;
2821 for (i=0 ; i<pr_progs->numglobaldefs ; i++)
2824 pr_globaldefs32[i].type = PRLittleLong (pr_globaldefs32[i].type);
2825 pr_globaldefs32[i].ofs = PRLittleLong (pr_globaldefs32[i].ofs);
2826 pr_globaldefs32[i].s_name = (string_t)PRLittleLong ((long)pr_globaldefs32[i].s_name);
2828 pr_globaldefs32[i].s_name += stringadjust;
2831 for (i=0 ; i<pr_progs->numfielddefs ; i++)
2834 pr_fielddefs32[i].type = PRLittleLong (pr_fielddefs32[i].type);
2835 pr_fielddefs32[i].ofs = PRLittleLong (pr_fielddefs32[i].ofs);
2836 pr_fielddefs32[i].s_name = (string_t)PRLittleLong ((long)pr_fielddefs32[i].s_name);
2842 type = pr_types[pr_fielddefs32[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL)].type;
2844 type = pr_fielddefs32[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL);
2845 if (progfuncs->fieldadjust && !pr_typecurrent) //we need to make sure all fields appear in their original place.
2846 QC_RegisterFieldVar(progfuncs, type, pr_fielddefs32[i].s_name+pr_strings, 4*(pr_fielddefs32[i].ofs+progfuncs->fieldadjust), -1);
2847 else if (type == ev_vector)
2848 QC_RegisterFieldVar(progfuncs, type, pr_fielddefs32[i].s_name+pr_strings, -1, pr_fielddefs32[i].ofs);
2850 pr_fielddefs32[i].s_name += stringadjust;
2852 if (reorg && !(progfuncs->fieldadjust && !pr_typecurrent))
2853 for (i=0 ; i<pr_progs->numfielddefs ; i++)
2856 type = pr_types[pr_fielddefs32[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL)].type;
2858 type = pr_fielddefs32[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL);
2859 if (type != ev_vector)
2860 QC_RegisterFieldVar(progfuncs, type, pr_fielddefs32[i].s_name+pr_strings-stringadjust, -1, pr_fielddefs32[i].ofs);
2864 Sys_Error("Bad struct type");
2867 //ifstring fixes arn't performed anymore.
2868 //the following switch just fixes endian and hexen2 calling conventions (by using different opcodes).
2869 switch(current_progstate->structtype)
2872 for (i=0 ; i<pr_progs->numstatements ; i++)
2874 qtest_statement_t qtst = ((qtest_statement_t*)st16)[i];
2876 st16[i].op = PRLittleShort(qtst.op);
2877 st16[i].a = PRLittleShort(qtst.a);
2878 st16[i].b = PRLittleShort(qtst.b);
2879 st16[i].c = PRLittleShort(qtst.c);
2880 // could use the line info as lno information maybe? is it really worth it?
2881 // also never assuming h2 calling mechanism
2885 for (i=0 ; i<pr_progs->numstatements ; i++)
2888 st16[i].op = PRLittleShort(st16[i].op);
2889 st16[i].a = PRLittleShort(st16[i].a);
2890 st16[i].b = PRLittleShort(st16[i].b);
2891 st16[i].c = PRLittleShort(st16[i].c);
2893 if (st16[i].op >= OP_CALL1 && st16[i].op <= OP_CALL8)
2897 hexencalling = true;
2904 for (i=0 ; i<pr_progs->numstatements ; i++)
2906 if (st16[i].op >= OP_CALL1 && st16[i].op <= OP_CALL8)
2907 st16[i].op += OP_CALL1H - OP_CALL1;
2908 if (st16[i].op >= OP_RAND0 && st16[i].op <= OP_RANDV2)
2910 st16[i].c = OFS_RETURN;
2915 case PST_KKQWSV: //24 sucks. Guess why.
2917 for (i=0 ; i<pr_progs->numstatements ; i++)
2920 pr_statements32[i].op = PRLittleLong(pr_statements32[i].op);
2921 pr_statements32[i].a = PRLittleLong(pr_statements32[i].a);
2922 pr_statements32[i].b = PRLittleLong(pr_statements32[i].b);
2923 pr_statements32[i].c = PRLittleLong(pr_statements32[i].c);
2925 if (pr_statements32[i].op >= OP_CALL1 && pr_statements32[i].op <= OP_CALL8)
2927 if (pr_statements32[i].b)
2929 hexencalling = true;
2936 for (i=0 ; i<pr_progs->numstatements ; i++)
2938 if (pr_statements32[i].op >= OP_CALL1 && pr_statements32[i].op <= OP_CALL8)
2939 pr_statements32[i].op += OP_CALL1H - OP_CALL1;
2940 if (pr_statements32[i].op >= OP_RAND0 && pr_statements32[i].op <= OP_RANDV2)
2941 if (!pr_statements32[i].c)
2942 pr_statements32[i].c = OFS_RETURN;
2949 if (headercrc == -1)
2952 if (current_progstate->structtype != PST_DEFAULT)
2953 Sys_Error("Decompiling a bigprogs");
2957 progstype = current_progstate-pr_progstate;
2959 // QC_StartShares(progfuncs);
2962 if (!pr_typecurrent) //progs 0 always acts as string stripped.
2963 isfriked = -1; //partly to avoid some bad progs.
2966 switch(current_progstate->structtype)
2970 for (i=0 ; i<pr_progs->numglobaldefs ; i++)
2973 type = pr_types[gd16[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL)].type;
2975 type = gd16[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL);
2977 if (gd16[i].type & DEF_SHARED)
2979 gd16[i].type &= ~DEF_SHARED;
2981 QC_AddSharedVar(progfuncs, gd16[i].ofs, pr_types[gd16[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL)].size);
2983 QC_AddSharedVar(progfuncs, gd16[i].ofs, type_size[type]);
2989 QC_AddSharedFieldVar(progfuncs, i, pr_strings - stringadjust);
2992 if (((unsigned int *)glob)[gd16[i].ofs]>=progstate->progs->numstrings)
2993 printf("Insane value\n");
2994 else if (isfriked != -1)
2996 if (pr_strings[((int *)glob)[gd16[i].ofs]]) //quakec uses string tables. 0 must remain null, or 'if (s)' can break.
2998 ((int *)glob)[gd16[i].ofs] += stringadjust;
3002 ((int *)glob)[gd16[i].ofs] = 0;
3006 if (((int *)glob)[gd16[i].ofs]) //don't change null funcs
3008 // 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.
3009 ((int *)glob)[gd16[i].ofs] |= progstype << 24;
3017 for (i=0 ; i<pr_progs->numglobaldefs ; i++)
3020 type = pr_types[pr_globaldefs32[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL)].type;
3022 type = pr_globaldefs32[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL);
3024 if (pr_globaldefs32[i].type & DEF_SHARED)
3026 pr_globaldefs32[i].type &= ~DEF_SHARED;
3028 QC_AddSharedVar(progfuncs, pr_globaldefs32[i].ofs, pr_types[pr_globaldefs32[i].type & ~(DEF_SHARED|DEF_SAVEGLOBAL)].size);
3030 QC_AddSharedVar(progfuncs, pr_globaldefs32[i].ofs, type_size[type]);
3035 QC_AddSharedFieldVar(progfuncs, i, pr_strings - stringadjust);
3038 if (pr_strings[((int *)glob)[pr_globaldefs32[i].ofs]]) //quakec uses string tables. 0 must remain null, or 'if (s)' can break.
3040 ((int *)glob)[pr_globaldefs32[i].ofs] += stringadjust;
3045 if (((int *)glob)[pr_globaldefs32[i].ofs]) //don't change null funcs
3046 ((int *)glob)[pr_globaldefs32[i].ofs] |= progstype << 24;
3051 if (pr_progs->version == PROG_EXTENDEDVERSION && pr_progs->numbodylessfuncs)
3053 s = &((char *)pr_progs)[pr_progs->ofsbodylessfuncs];
3054 for (i = 0; i < pr_progs->numbodylessfuncs; i++)
3056 d32 = ED_FindGlobal32(progfuncs, s);
3057 d2 = ED_FindGlobalOfsFromProgs(progfuncs, s, 0, ev_function);
3059 Sys_Error("Runtime-linked function %s was not found in existing progs", s);
3061 Sys_Error("Couldn't find def for \"%s\"", s);
3062 ((int *)glob)[d32->ofs] = (*(func_t *)&pr_progstate[0].globals[*d2]);
3069 Sys_Error("Bad struct type");
3072 if ((isfriked && pr_typecurrent)) //friked progs only allow one file.
3074 printf("You are trying to load a string-stripped progs as an addon.\nThis behaviour is not supported. Try removing some optimizations.");
3075 PRHunkFree(progfuncs, hmark);
3080 pr_strings+=stringadjust;
3081 if (!progfuncs->stringtable)
3082 progfuncs->stringtable = pr_strings;
3084 if (progfuncs->stringtablesize + progfuncs->stringtable < pr_strings + pr_progs->numstrings)
3085 progfuncs->stringtablesize = (pr_strings + pr_progs->numstrings) - progfuncs->stringtable;
3087 eval = PR_FindGlobal(progfuncs, "thisprogs", progstype, NULL);
3089 eval->prog = progstype;
3091 switch(current_progstate->structtype)
3094 if (pr_progs->version == PROG_EXTENDEDVERSION && pr_progs->numbodylessfuncs)
3096 s = &((char *)pr_progs)[pr_progs->ofsbodylessfuncs];
3097 for (i = 0; i < pr_progs->numbodylessfuncs; i++)
3099 d16 = ED_FindGlobal16(progfuncs, s);
3102 printf("Progs requires \"%s\" the external function \"%s\", but the definition was stripped\n", filename, s);
3103 PRHunkFree(progfuncs, hmark);
3108 ((int *)glob)[d16->ofs] = PR_FindFunc(progfuncs, s, PR_ANY);
3109 if (!((int *)glob)[d16->ofs])
3110 printf("Warning: Runtime-linked function %s could not be found (loading %s)\n", s, filename);
3112 d2 = ED_FindGlobalOfsFromProgs(progfuncs, s, 0, ev_function);
3114 Sys_Error("Runtime-linked function %s was not found in primary progs (loading %s)", s, filename);
3115 ((int *)glob)[d16->ofs] = (*(func_t *)&pr_progstate[0].globals[*d2]);
3123 break; //cannot happen anyway.
3125 if (pr_progs->version == PROG_EXTENDEDVERSION && pr_progs->numbodylessfuncs)
3127 s = &((char *)pr_progs)[pr_progs->ofsbodylessfuncs];
3128 for (i = 0; i < pr_progs->numbodylessfuncs; i++)
3130 d32 = ED_FindGlobal32(progfuncs, s);
3131 d2 = ED_FindGlobalOfsFromProgs(progfuncs, s, 0, ev_function);
3133 printf("Warning: Runtime-linked function %s was not found in existing progs", s);
3136 printf("Couldn't find def for \"%s\"", s);
3137 PRHunkFree(progfuncs, hmark);
3141 ((int *)glob)[d32->ofs] = (*(func_t *)&pr_progstate[0].globals[*d2]);
3149 eval = PR_FindGlobal(progfuncs, "__ext__fasttrackarrays", PR_CURRENT, NULL);
3150 if (eval) //we support these opcodes
3151 eval->_float = true;
3158 struct edict_s *EDICT_NUM(progfuncs_t *progfuncs, unsigned int n)
3161 Sys_Error ("QCLIB: EDICT_NUM: bad number %i", n);
3163 return prinst->edicttable[n];
3166 unsigned int NUM_FOR_EDICT(progfuncs_t *progfuncs, struct edict_s *e)
3168 edictrun_t *er = (edictrun_t*)e;
3169 if (er->entnum >= maxedicts)
3170 Sys_Error ("QCLIB: NUM_FOR_EDICT: bad pointer (%p)", e);