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