removed the ^7 after player names in various engine messages and made
[divverent/darkplaces.git] / csprogs.c
1 #include "quakedef.h"
2 #include "progsvm.h"
3 #include "clprogdefs.h"
4 #include "csprogs.h"
5 #include "cl_collision.h"
6
7 //============================================================================
8 // Client prog handling
9 //[515]: omg !!! optimize it ! a lot of hacks here and there also :P
10
11 #define CSQC_RETURNVAL  prog->globals.generic[OFS_RETURN]
12 #define CSQC_BEGIN              csqc_tmpprog=prog;prog=0;PRVM_SetProg(PRVM_CLIENTPROG);
13 #define CSQC_END                prog=csqc_tmpprog;
14 static prvm_prog_t *csqc_tmpprog;
15
16 //[515]: these are required funcs
17 static char *cl_required_func[] =
18 {
19         "CSQC_Init",
20         "CSQC_InputEvent",
21         "CSQC_UpdateView",
22         "CSQC_ConsoleCommand",
23         "CSQC_Shutdown"
24 };
25
26 static int cl_numrequiredfunc = sizeof(cl_required_func) / sizeof(char*);
27
28 void CL_VM_Error (const char *format, ...) DP_FUNC_PRINTF(1);
29 void CL_VM_Error (const char *format, ...)      //[515]: hope it will be never executed =)
30 {
31         char errorstring[4096];
32         va_list argptr;
33
34         va_start (argptr, format);
35         dpvsnprintf (errorstring, sizeof(errorstring), format, argptr);
36         va_end (argptr);
37 //      Con_Printf( "CL_VM_Error: %s\n", errorstring );
38
39         PRVM_Crash();
40         cl.csqc_loaded = false;
41
42         Cvar_SetValueQuick(&csqc_progcrc, -1);
43         Cvar_SetValueQuick(&csqc_progsize, -1);
44
45 //      Host_AbortCurrentFrame();       //[515]: hmmm... if server says it needs csqc then client MUST disconnect
46         Host_Error(va("CL_VM_Error: %s", errorstring));
47 }
48
49 //[515]: set globals before calling R_UpdateView, WEIRD CRAP
50 static void CSQC_SetGlobals (void)
51 {
52         CSQC_BEGIN
53                 prog->globals.client->time = cl.time;
54                 prog->globals.client->frametime = max(0, cl.time - cl.oldtime);
55                 prog->globals.client->servercommandframe = cls.servermovesequence;
56                 prog->globals.client->clientcommandframe = cl.movecmd[0].sequence;
57                 VectorCopy(cl.viewangles, prog->globals.client->input_angles);
58                 VectorCopy(cl.viewangles, cl.csqc_angles);
59                 prog->globals.client->input_buttons = cl.movecmd[0].buttons;
60                 VectorSet(prog->globals.client->input_movevalues, cl.movecmd[0].forwardmove, cl.movecmd[0].sidemove, cl.movecmd[0].upmove);
61                 //VectorCopy(cl.movement_origin, cl.csqc_origin);
62                 Matrix4x4_OriginFromMatrix(&cl.entities[cl.viewentity].render.matrix, cl.csqc_origin);
63                 VectorCopy(cl.csqc_origin, prog->globals.client->pmove_org);
64                 prog->globals.client->maxclients = cl.maxclients;
65                 //VectorCopy(cl.movement_velocity, prog->globals.client->pmove_vel);
66                 VectorCopy(cl.velocity, prog->globals.client->pmove_vel);
67         CSQC_END
68 }
69
70 void CSQC_Predraw (prvm_edict_t *ed)
71 {
72         int b;
73         if(!ed->fields.client->predraw)
74                 return;
75         b = prog->globals.client->self;
76         prog->globals.client->self = PRVM_EDICT_TO_PROG(ed);
77         PRVM_ExecuteProgram(ed->fields.client->predraw, "CSQC_Predraw: NULL function\n");
78         prog->globals.client->self = b;
79 }
80
81 void CSQC_Think (prvm_edict_t *ed)
82 {
83         int b;
84         if(ed->fields.client->think)
85         if(ed->fields.client->nextthink && ed->fields.client->nextthink <= prog->globals.client->time)
86         {
87                 ed->fields.client->nextthink = 0;
88                 b = prog->globals.client->self;
89                 prog->globals.client->self = PRVM_EDICT_TO_PROG(ed);
90                 PRVM_ExecuteProgram(ed->fields.client->think, "CSQC_Think: NULL function\n");
91                 prog->globals.client->self = b;
92         }
93 }
94
95 extern cvar_t cl_noplayershadow;
96 qboolean CSQC_AddRenderEdict(prvm_edict_t *ed)
97 {
98         int renderflags;
99         float scale;
100         prvm_eval_t *val;
101         entity_t *e;
102         model_t *model;
103         matrix4x4_t tagmatrix, matrix2;
104
105         model = CL_GetModelFromEdict(ed);
106         if (!model)
107                 return false;
108
109         e = CL_NewTempEntity();
110         if (!e)
111                 return false;
112
113         e->render.model = model;
114         e->render.colormap = (int)ed->fields.client->colormap;
115         e->render.frame = (int)ed->fields.client->frame;
116         e->render.skinnum = (int)ed->fields.client->skin;
117         e->render.effects |= e->render.model->effects;
118         scale = 1;
119         renderflags = 0;
120         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.renderflags)) && val->_float)     renderflags = (int)val->_float;
121         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.alpha)) && val->_float)           e->render.alpha = val->_float;
122         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.scale)) && val->_float)           e->render.scale = scale = val->_float;
123         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.colormod)) && VectorLength2(val->vector)) VectorCopy(val->vector, e->render.colormod);
124         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.effects)) && val->_float) e->render.effects |= (int)val->_float;
125         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.tag_entity)) && val->edict)
126         {
127                 int tagentity;
128                 int tagindex = 0;
129                 tagentity = val->edict;
130                 if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.tag_index)) && val->_float)
131                         tagindex = (int)val->_float;
132                 // FIXME: calculate tag matrix
133                 Matrix4x4_CreateIdentity(&tagmatrix);
134         }
135         else
136                 Matrix4x4_CreateIdentity(&tagmatrix);
137
138         if (renderflags & RF_USEAXIS)
139         {
140                 vec3_t left;
141                 VectorNegate(prog->globals.client->v_right, left);
142                 Matrix4x4_FromVectors(&matrix2, prog->globals.client->v_forward, left, prog->globals.client->v_up, ed->fields.client->origin);
143                 Matrix4x4_Scale(&matrix2, scale, 1);
144         }
145         else
146         {
147                 vec3_t angles;
148                 VectorCopy(ed->fields.client->angles, angles);
149                 // if model is alias, reverse pitch direction
150                 if (e->render.model->type == mod_alias)
151                         angles[0] = -angles[0];
152
153                 // set up the render matrix
154                 Matrix4x4_CreateFromQuakeEntity(&matrix2, ed->fields.client->origin[0], ed->fields.client->origin[1], ed->fields.client->origin[2], angles[0], angles[1], angles[2], scale);
155         }
156
157         // set up the animation data
158         // self.frame is the interpolation target (new frame)
159         // self.frame1time is the animation base time for the interpolation target
160         // self.frame2 is the interpolation start (previous frame)
161         // self.frame2time is the animation base time for the interpolation start
162         e->render.frame1 = e->render.frame = ed->fields.client->frame;
163         if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame2))) e->render.frame = val->_float;
164         if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame1time))) e->render.frame2time = val->_float;
165         if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame2time))) e->render.frame1time = val->_float;
166         if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.lerpfrac))) e->render.framelerp = val->_float;
167
168         // concat the matrices to make the entity relative to its tag
169         Matrix4x4_Concat(&e->render.matrix, &tagmatrix, &matrix2);
170         // make the other useful stuff
171         CL_UpdateRenderEntity(&e->render);
172
173         if(renderflags)
174         {
175                 if(renderflags & RF_VIEWMODEL)  e->render.flags |= RENDER_VIEWMODEL;
176                 if(renderflags & RF_EXTERNALMODEL)e->render.flags |= RENDER_EXTERIORMODEL;
177                 if(renderflags & RF_DEPTHHACK)  e->render.effects |= EF_NODEPTHTEST;
178                 if(renderflags & RF_ADDITIVE)           e->render.effects |= EF_ADDITIVE;
179         }
180
181         // either fullbright or lit
182         if (!(e->render.effects & EF_FULLBRIGHT) && !r_fullbright.integer)
183                 e->render.flags |= RENDER_LIGHT;
184         // hide player shadow during intermission or nehahra movie
185         if (!(e->render.effects & (EF_NOSHADOW | EF_ADDITIVE | EF_NODEPTHTEST))
186          &&  (e->render.alpha >= 1)
187          && !(e->render.flags & RENDER_VIEWMODEL)
188          && (!(e->render.flags & RENDER_EXTERIORMODEL) || (!cl.intermission && cls.protocol != PROTOCOL_NEHAHRAMOVIE && !cl_noplayershadow.integer)))
189                 e->render.flags |= RENDER_SHADOW;
190         if (e->render.flags & RENDER_VIEWMODEL)
191                 e->render.flags |= RENDER_NOSELFSHADOW;
192
193         return true;
194 }
195
196 qboolean CL_VM_InputEvent (qboolean pressed, int key)
197 {
198         qboolean r;
199         if(!cl.csqc_loaded)
200                 return false;
201         CSQC_BEGIN
202                 prog->globals.client->time = cl.time;
203                 PRVM_G_FLOAT(OFS_PARM0) = pressed;
204                 PRVM_G_FLOAT(OFS_PARM1) = key;
205                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_InputEvent, "QC function CSQC_InputEvent is missing");
206                 r = CSQC_RETURNVAL;
207         CSQC_END
208         return r;
209 }
210
211 qboolean CL_VM_UpdateView (void)
212 {
213 //      vec3_t oldangles;
214         if(!cl.csqc_loaded)
215                 return false;
216         CSQC_BEGIN
217                 //VectorCopy(cl.viewangles, oldangles);
218                 prog->globals.client->time = cl.time;
219                 CSQC_SetGlobals();
220                 // clear renderable entity and light lists to prevent crashes if the
221                 // CSQC_UpdateView function does not call R_ClearScene as it should
222                 r_refdef.numentities = 0;
223                 r_refdef.numlights = 0;
224                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_UpdateView, "QC function CSQC_UpdateView is missing");
225                 //VectorCopy(oldangles, cl.viewangles);
226         CSQC_END
227         return true;
228 }
229
230 extern sizebuf_t vm_tempstringsbuf;
231 qboolean CL_VM_ConsoleCommand (const char *cmd)
232 {
233         int restorevm_tempstringsbuf_cursize;
234         qboolean r;
235         if(!cl.csqc_loaded)
236                 return false;
237         CSQC_BEGIN
238                 prog->globals.client->time = cl.time;
239                 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
240                 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(cmd);
241                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_ConsoleCommand, "QC function CSQC_ConsoleCommand is missing");
242                 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
243                 r = CSQC_RETURNVAL;
244         CSQC_END
245         return r;
246 }
247
248 qboolean CL_VM_Parse_TempEntity (void)
249 {
250         int                     t;
251         qboolean        r = false;
252         if(!cl.csqc_loaded)
253                 return false;
254         CSQC_BEGIN
255         if(prog->funcoffsets.CSQC_Parse_TempEntity)
256         {
257                 t = msg_readcount;
258                 prog->globals.client->time = cl.time;
259                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_TempEntity, "QC function CSQC_Parse_TempEntity is missing");
260                 r = CSQC_RETURNVAL;
261                 if(!r)
262                 {
263                         msg_readcount = t;
264                         msg_badread = false;
265                 }
266         }
267         CSQC_END
268         return r;
269 }
270
271 void CL_VM_Parse_StuffCmd (const char *msg)
272 {
273         int restorevm_tempstringsbuf_cursize;
274         if(msg[0] == 'c')
275         if(msg[1] == 's')
276         if(msg[2] == 'q')
277         if(msg[3] == 'c')
278         {
279                 // if this is setting a csqc variable, deprotect csqc_progcrc
280                 // temporarily so that it can be set by the cvar command,
281                 // and then reprotect it afterwards
282                 int crcflags = csqc_progcrc.flags;
283                 int sizeflags = csqc_progcrc.flags;
284                 csqc_progcrc.flags &= ~CVAR_READONLY;
285                 csqc_progsize.flags &= ~CVAR_READONLY;
286                 Cmd_ExecuteString (msg, src_command);
287                 csqc_progcrc.flags = crcflags;
288                 csqc_progsize.flags = sizeflags;
289                 return;
290         }
291         if(!cl.csqc_loaded)
292         {
293                 Cbuf_AddText(msg);
294                 return;
295         }
296         CSQC_BEGIN
297         if(prog->funcoffsets.CSQC_Parse_StuffCmd)
298         {
299                 prog->globals.client->time = cl.time;
300                 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
301                 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(msg);
302                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_StuffCmd, "QC function CSQC_Parse_StuffCmd is missing");
303                 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
304         }
305         else
306                 Cbuf_AddText(msg);
307         CSQC_END
308 }
309
310 static void CL_VM_Parse_Print (const char *msg)
311 {
312         int restorevm_tempstringsbuf_cursize;
313         prog->globals.client->time = cl.time;
314         restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
315         PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(msg);
316         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_Print, "QC function CSQC_Parse_Print is missing");
317         vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
318 }
319
320 void CSQC_AddPrintText (const char *msg)
321 {
322         size_t i;
323         if(!cl.csqc_loaded)
324         {
325                 Con_Print(msg);
326                 return;
327         }
328         CSQC_BEGIN
329         if(prog->funcoffsets.CSQC_Parse_Print)
330         {
331                 // FIXME: is this bugged?
332                 i = strlen(msg)-1;
333                 if(msg[i] != '\n' && msg[i] != '\r')
334                 {
335                         if(strlen(cl.csqc_printtextbuf)+i >= MAX_INPUTLINE)
336                         {
337                                 CL_VM_Parse_Print(cl.csqc_printtextbuf);
338                                 cl.csqc_printtextbuf[0] = 0;
339                         }
340                         else
341                                 strlcat(cl.csqc_printtextbuf, msg, MAX_INPUTLINE);
342                         return;
343                 }
344                 strlcat(cl.csqc_printtextbuf, msg, MAX_INPUTLINE);
345                 CL_VM_Parse_Print(cl.csqc_printtextbuf);
346                 cl.csqc_printtextbuf[0] = 0;
347         }
348         else
349                 Con_Print(msg);
350         CSQC_END
351 }
352
353 void CL_VM_Parse_CenterPrint (const char *msg)
354 {
355         int restorevm_tempstringsbuf_cursize;
356         if(!cl.csqc_loaded)
357         {
358                 SCR_CenterPrint((char*)msg);
359                 return;
360         }
361         CSQC_BEGIN
362         if(prog->funcoffsets.CSQC_Parse_CenterPrint)
363         {
364                 prog->globals.client->time = cl.time;
365                 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
366                 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(msg);
367                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_CenterPrint, "QC function CSQC_Parse_CenterPrint is missing");
368                 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
369         }
370         else
371                 SCR_CenterPrint((char*)msg);
372         CSQC_END
373 }
374
375 float CL_VM_Event (float event)         //[515]: needed ? I'd say "YES", but don't know for what :D
376 {
377         float r = 0;
378         if(!cl.csqc_loaded)
379                 return 0;
380         CSQC_BEGIN
381         if(prog->funcoffsets.CSQC_Event)
382         {
383                 prog->globals.client->time = cl.time;
384                 PRVM_G_FLOAT(OFS_PARM0) = event;
385                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Event, "QC function CSQC_Event is missing");
386                 r = CSQC_RETURNVAL;
387         }
388         CSQC_END
389         return r;
390 }
391
392 void CSQC_ReadEntities (void)
393 {
394         unsigned short entnum, oldself, realentnum;
395         CSQC_BEGIN
396                 prog->globals.client->time = cl.time;
397                 oldself = prog->globals.client->self;
398                 while(1)
399                 {
400                         entnum = MSG_ReadShort();
401                         if(!entnum || msg_badread)
402                                 return;
403                         realentnum = entnum & 0x7FFF;
404                         prog->globals.client->self = cl.csqc_server2csqcentitynumber[realentnum];
405                         if(entnum & 0x8000)
406                         {
407                                 if(prog->globals.client->self)
408                                 {
409                                         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Ent_Remove, "QC function CSQC_Ent_Remove is missing");
410                                         cl.csqc_server2csqcentitynumber[realentnum] = 0;
411                                 }
412                                 else
413                                         Con_Printf("Smth bad happens in csqc...\n");    //[515]: never happens ?
414                         }
415                         else
416                         {
417                                 if(!prog->globals.client->self)
418                                 {
419                                         prvm_edict_t    *ed;
420                                         ed = PRVM_ED_Alloc();
421                                         ed->fields.client->entnum = realentnum;
422                                         prog->globals.client->self = cl.csqc_server2csqcentitynumber[realentnum] = PRVM_EDICT_TO_PROG(ed);
423                                         PRVM_G_FLOAT(OFS_PARM0) = 1;
424                                 }
425                                 else
426                                         PRVM_G_FLOAT(OFS_PARM0) = 0;
427                                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Ent_Update, "QC function CSQC_Ent_Update is missing");
428                         }
429                 }
430                 prog->globals.client->self = oldself;
431         CSQC_END
432 }
433
434 void CL_VM_CB_BeginIncreaseEdicts(void)
435 {
436         int i;
437         prvm_edict_t *ent;
438
439         // links don't survive the transition, so unlink everything
440         for (i = 0, ent = prog->edicts;i < prog->max_edicts;i++, ent++)
441         {
442                 if (!ent->priv.server->free)
443                         World_UnlinkEdict(prog->edicts + i);
444                 memset(&ent->priv.server->areagrid, 0, sizeof(ent->priv.server->areagrid));
445         }
446         World_Clear(&cl.world);
447 }
448
449 void CL_VM_CB_EndIncreaseEdicts(void)
450 {
451         int i;
452         prvm_edict_t *ent;
453
454         // link every entity except world
455         for (i = 1, ent = prog->edicts;i < prog->max_edicts;i++, ent++)
456                 if (!ent->priv.server->free)
457                         CL_LinkEdict(ent);
458 }
459
460 void CL_VM_CB_InitEdict(prvm_edict_t *e)
461 {
462         e->priv.server->move = false; // don't move on first frame
463 }
464
465 void CL_VM_CB_FreeEdict(prvm_edict_t *ed)
466 {
467         World_UnlinkEdict(ed);
468         memset(ed->fields.client, 0, sizeof(*ed->fields.client));
469 }
470
471 void CL_VM_CB_CountEdicts(void)
472 {
473         int             i;
474         prvm_edict_t    *ent;
475         int             active = 0, models = 0, solid = 0;
476
477         for (i=0 ; i<prog->num_edicts ; i++)
478         {
479                 ent = PRVM_EDICT_NUM(i);
480                 if (ent->priv.server->free)
481                         continue;
482                 active++;
483                 if (ent->fields.client->solid)
484                         solid++;
485                 if (ent->fields.client->model)
486                         models++;
487         }
488
489         Con_Printf("num_edicts:%3i\n", prog->num_edicts);
490         Con_Printf("active    :%3i\n", active);
491         Con_Printf("view      :%3i\n", models);
492         Con_Printf("touch     :%3i\n", solid);
493 }
494
495 qboolean CL_VM_CB_LoadEdict(prvm_edict_t *ent)
496 {
497         return true;
498 }
499
500 void Cmd_ClearCsqcFuncs (void);
501
502 void CL_VM_Init (void)
503 {
504         unsigned char *csprogsdata;
505         fs_offset_t csprogsdatasize;
506         int csprogsdatacrc, requiredcrc;
507         int requiredsize;
508
509         // reset csqc_progcrc after reading it, so that changing servers doesn't
510         // expect csqc on the next server
511         requiredcrc = csqc_progcrc.integer;
512         requiredsize = csqc_progsize.integer;
513         Cvar_SetValueQuick(&csqc_progcrc, -1);
514         Cvar_SetValueQuick(&csqc_progsize, -1);
515
516         // if the server is not requesting a csprogs, then we're done here
517         if (requiredcrc < 0)
518                 return;
519
520         // see if the requested csprogs.dat file matches the requested crc
521         csprogsdatacrc = -1;
522         csprogsdata = FS_LoadFile(va("dlcache/%s.%i.%i", csqc_progname.string, requiredsize, requiredcrc), tempmempool, true, &csprogsdatasize);
523         if (!csprogsdata)
524                 csprogsdata = FS_LoadFile(csqc_progname.string, tempmempool, true, &csprogsdatasize);
525         if (csprogsdata)
526         {
527                 csprogsdatacrc = CRC_Block(csprogsdata, csprogsdatasize);
528                 Mem_Free(csprogsdata);
529                 if (csprogsdatacrc != requiredcrc || csprogsdatasize != requiredsize)
530                 {
531                         if (cls.demoplayback)
532                         {
533                                 Con_Printf("^1Warning: Your %s is not the same version as the demo was recorded with (CRC/size are %i/%i but should be %i/%i)\n", csqc_progname.string, csprogsdatacrc, (int)csprogsdatasize, requiredcrc, requiredsize);
534                                 return;
535                         }
536                         else
537                         {
538                                 Con_Printf("^1Your %s is not the same version as the server (CRC is %i/%i but should be %i/%i)\n", csqc_progname.string, csprogsdatacrc, (int)csprogsdatasize, requiredcrc, requiredsize);
539                                 CL_Disconnect();
540                                 return;
541                         }
542                 }
543         }
544         else
545         {
546                 if (requiredcrc >= 0)
547                 {
548                         if (cls.demoplayback)
549                                 Con_Printf("CL_VM_Init: demo requires CSQC, but \"%s\" wasn't found\n", csqc_progname.string);
550                         else
551                                 Con_Printf("CL_VM_Init: server requires CSQC, but \"%s\" wasn't found\n", csqc_progname.string);
552                         CL_Disconnect();
553                 }
554                 return;
555         }
556
557         PRVM_Begin;
558         PRVM_InitProg(PRVM_CLIENTPROG);
559
560         // allocate the mempools
561         prog->progs_mempool = Mem_AllocPool(csqc_progname.string, 0, NULL);
562         prog->headercrc = CL_PROGHEADER_CRC;
563         prog->edictprivate_size = 0; // no private struct used
564         prog->name = CL_NAME;
565         prog->num_edicts = 1;
566         prog->max_edicts = 512;
567         prog->limit_edicts = CL_MAX_EDICTS;
568         prog->reserved_edicts = 0;
569         prog->edictprivate_size = sizeof(edict_engineprivate_t);
570         prog->extensionstring = vm_sv_extensions;
571         prog->builtins = vm_cl_builtins;
572         prog->numbuiltins = vm_cl_numbuiltins;
573         prog->begin_increase_edicts = CL_VM_CB_BeginIncreaseEdicts;
574         prog->end_increase_edicts = CL_VM_CB_EndIncreaseEdicts;
575         prog->init_edict = CL_VM_CB_InitEdict;
576         prog->free_edict = CL_VM_CB_FreeEdict;
577         prog->count_edicts = CL_VM_CB_CountEdicts;
578         prog->load_edict = CL_VM_CB_LoadEdict;
579         prog->init_cmd = VM_CL_Cmd_Init;
580         prog->reset_cmd = VM_CL_Cmd_Reset;
581         prog->error_cmd = CL_VM_Error;
582
583         PRVM_LoadProgs(csqc_progname.string, cl_numrequiredfunc, cl_required_func, 0, NULL, 0, NULL);
584
585         if (!prog->loaded)
586         {
587                 CL_VM_Error("CSQC ^2failed to load\n");
588                 if(!sv.active)
589                         CL_Disconnect();
590                 return;
591         }
592
593         Con_Printf("CSQC ^5loaded (crc=%i, size=%i)\n", csprogsdatacrc, (int)csprogsdatasize);
594
595         // check if OP_STATE animation is possible in this dat file
596         if (prog->fieldoffsets.nextthink >= 0 && prog->fieldoffsets.frame >= 0 && prog->fieldoffsets.think >= 0 && prog->globaloffsets.self >= 0)
597                 prog->flag |= PRVM_OP_STATE;
598
599         // set time
600         prog->globals.client->time = cl.time;
601
602         prog->globals.client->mapname = PRVM_SetEngineString(cl.worldmodel->name);
603         prog->globals.client->player_localentnum = cl.playerentity;
604
605         // call the prog init
606         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Init, "QC function CSQC_Init is missing");
607
608         PRVM_End;
609         cl.csqc_loaded = true;
610
611         cl.csqc_vidvars.drawcrosshair = false;
612         cl.csqc_vidvars.drawenginesbar = false;
613 }
614
615 void CL_VM_ShutDown (void)
616 {
617         Cmd_ClearCsqcFuncs();
618         Cvar_SetValueQuick(&csqc_progcrc, -1);
619         Cvar_SetValueQuick(&csqc_progsize, -1);
620         if(!cl.csqc_loaded)
621                 return;
622         CSQC_BEGIN
623                 prog->globals.client->time = cl.time;
624                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Shutdown, "QC function CSQC_Shutdown is missing");
625                 PRVM_ResetProg();
626         CSQC_END
627         Con_Print("CSQC ^1unloaded\n");
628         cl.csqc_loaded = false;
629 }