Try to fix a dedicated server crash by moving the two csqc_ cvar initializations
[divverent/darkplaces.git] / csprogs.c
1 #include "quakedef.h"
2 #include "progsvm.h"
3 #include "clprogdefs.h"
4 #include "csprogs.h"
5
6 //============================================================================
7 // Client prog handling
8 //[515]: omg !!! optimize it ! a lot of hacks here and there also :P
9
10 #define CSQC_RETURNVAL  prog->globals.generic[OFS_RETURN]
11 #define CSQC_BEGIN              csqc_tmpprog=prog;prog=0;PRVM_SetProg(PRVM_CLIENTPROG);
12 #define CSQC_END                prog=csqc_tmpprog;
13 static prvm_prog_t *csqc_tmpprog;
14
15 //[515]: these are required funcs
16 #define CL_F_INIT                               "CSQC_Init"
17 #define CL_F_INPUTEVENT                 "CSQC_InputEvent"
18 #define CL_F_UPDATEVIEW                 "CSQC_UpdateView"
19 #define CL_F_CONSOLECOMMAND             "CSQC_ConsoleCommand"
20 #define CL_F_SHUTDOWN                   "CSQC_Shutdown"
21
22 //[515]: these are optional
23 #define CL_F_PARSE_TEMPENTITY   "CSQC_Parse_TempEntity" //[515]: very helpfull when you want to create your own particles/decals/etc for effects that allready exist
24 #define CL_F_PARSE_STUFFCMD             "CSQC_Parse_StuffCmd"
25 #define CL_F_PARSE_PRINT                "CSQC_Parse_Print"
26 #define CL_F_PARSE_CENTERPRINT  "CSQC_Parse_CenterPrint"
27 #define CL_F_ENT_UPDATE                 "CSQC_Ent_Update"
28 #define CL_F_ENT_REMOVE                 "CSQC_Ent_Remove"
29 #define CL_F_EVENT                              "CSQC_Event"    //[515]: engine call this for its own needs
30                                                                                                 //so csqc can do some things according to what engine it's running on
31                                                                                                 //example: to say about edicts increase, whatever...
32
33 #define CSQC_PRINTBUFFERLEN             8192    //[515]: enough ?
34
35 static char *cl_required_func[] =
36 {
37         CL_F_INIT,
38         CL_F_INPUTEVENT,
39         CL_F_UPDATEVIEW,
40         CL_F_CONSOLECOMMAND,
41         CL_F_SHUTDOWN
42 };
43
44 static int cl_numrequiredfunc = sizeof(cl_required_func) / sizeof(char*);
45
46 static char                             *csqc_printtextbuf = NULL;
47 static unsigned short   *csqc_sv2csqcents;      //[515]: server entities numbers on client side. FIXME : make pointers instead of numbers ?
48
49 static mfunction_t      *CSQC_Parse_TempEntity;
50 static mfunction_t      *CSQC_Parse_StuffCmd;
51 static mfunction_t      *CSQC_Parse_Print;
52 static mfunction_t      *CSQC_Parse_CenterPrint;
53 static mfunction_t      *CSQC_Ent_Update;
54 static mfunction_t      *CSQC_Ent_Remove;
55 static mfunction_t      *CSQC_Event;
56
57 static int csqc_fieldoff_alpha;
58 static int csqc_fieldoff_colormod;
59 static int csqc_fieldoff_effects;
60 int csqc_fieldoff_scale;
61 int csqc_fieldoff_renderflags;
62 int csqc_fieldoff_tag_entity;
63 int csqc_fieldoff_tag_index;
64 int csqc_fieldoff_dphitcontentsmask;
65
66 qboolean csqc_loaded = false;
67
68 vec3_t csqc_origin, csqc_angles;
69 static double csqc_frametime = 0;
70 int csqc_buttons;
71
72 static mempool_t *csqc_mempool;
73
74 static void CL_VM_FindEdictFieldOffsets (void)
75 {
76         csqc_fieldoff_alpha                     = PRVM_ED_FindFieldOffset("alpha");
77         csqc_fieldoff_scale                     = PRVM_ED_FindFieldOffset("scale");
78         csqc_fieldoff_colormod          = PRVM_ED_FindFieldOffset("colormod");
79         csqc_fieldoff_renderflags       = PRVM_ED_FindFieldOffset("renderflags");
80         csqc_fieldoff_effects           = PRVM_ED_FindFieldOffset("effects");
81         csqc_fieldoff_tag_entity        = PRVM_ED_FindFieldOffset("tag_entity");
82         csqc_fieldoff_tag_index         = PRVM_ED_FindFieldOffset("tag_index");
83
84         CSQC_Parse_TempEntity           = PRVM_ED_FindFunction (CL_F_PARSE_TEMPENTITY);
85         CSQC_Parse_StuffCmd                     = PRVM_ED_FindFunction (CL_F_PARSE_STUFFCMD);
86         CSQC_Parse_Print                        = PRVM_ED_FindFunction (CL_F_PARSE_PRINT);
87         CSQC_Parse_CenterPrint          = PRVM_ED_FindFunction (CL_F_PARSE_CENTERPRINT);
88         CSQC_Ent_Update                         = PRVM_ED_FindFunction (CL_F_ENT_UPDATE);
89         CSQC_Ent_Remove                         = PRVM_ED_FindFunction (CL_F_ENT_REMOVE);
90         CSQC_Event                                      = PRVM_ED_FindFunction (CL_F_EVENT);
91
92         if(CSQC_Parse_Print)
93         {
94                 csqc_printtextbuf = (char *)Mem_Alloc(csqc_mempool, CSQC_PRINTBUFFERLEN);
95                 csqc_printtextbuf[0] = 0;
96         }
97 }
98
99 void CL_VM_Error (const char *format, ...)      //[515]: hope it will be never executed =)
100 {
101         char errorstring[4096];
102         va_list argptr;
103
104         va_start (argptr, format);
105         dpvsnprintf (errorstring, sizeof(errorstring), format, argptr);
106         va_end (argptr);
107 //      Con_Printf( "CL_VM_Error: %s\n", errorstring );
108
109         PRVM_Crash();
110         csqc_loaded = false;
111         Mem_FreePool(&csqc_mempool);
112
113         Cvar_SetValueQuick(&csqc_progcrc, 0);
114
115 //      Host_AbortCurrentFrame();       //[515]: hmmm... if server says it needs csqc then client MUST disconnect
116         Host_Error(va("CL_VM_Error: %s", errorstring));
117 }
118
119 //[515]: set globals before calling R_UpdateView, WEIRD CRAP
120 static void CSQC_SetGlobals (void)
121 {
122         //extern cvar_t sv_accelerate, sv_friction, sv_gravity, sv_stopspeed, sv_maxspeed;
123
124         CSQC_BEGIN
125                 *prog->time = cl.time;
126                 prog->globals.client->frametime = cl.time - csqc_frametime;
127                 csqc_frametime = cl.time;
128                 prog->globals.client->servercommandframe = cl.servermovesequence;
129                 prog->globals.client->clientcommandframe = cl.movemessages;
130                 VectorCopy(cl.viewangles, prog->globals.client->input_angles);
131                 VectorCopy(cl.viewangles, csqc_angles);
132                 prog->globals.client->input_buttons = csqc_buttons;
133                 VectorSet(prog->globals.client->input_movevalues, cl.cmd.forwardmove, cl.cmd.sidemove, cl.cmd.upmove);
134                 //VectorCopy(cl.movement_origin, csqc_origin);
135                 VectorCopy(cl.entities[cl.viewentity].render.origin, csqc_origin);
136                 VectorCopy(csqc_origin, prog->globals.client->pmove_org);
137                 prog->globals.client->maxclients = cl.maxclients;
138                 //VectorCopy(cl.movement_velocity, prog->globals.client->pmove_vel);
139                 VectorCopy(cl.velocity, prog->globals.client->pmove_vel);
140         CSQC_END
141 }
142
143 void CSQC_Predraw (prvm_edict_t *ed)
144 {
145         int b;
146         if(!ed->fields.client->predraw)
147                 return;
148         b = prog->globals.client->self;
149         prog->globals.client->self = PRVM_EDICT_TO_PROG(ed);
150         PRVM_ExecuteProgram(ed->fields.client->predraw, "CSQC_Predraw: NULL function\n");
151         prog->globals.client->self = b;
152 }
153
154 void CSQC_Think (prvm_edict_t *ed)
155 {
156         int b;
157         if(ed->fields.client->think)
158         if(ed->fields.client->nextthink && ed->fields.client->nextthink <= *prog->time)
159         {
160                 ed->fields.client->nextthink = 0;
161                 b = prog->globals.client->self;
162                 prog->globals.client->self = PRVM_EDICT_TO_PROG(ed);
163                 PRVM_ExecuteProgram(ed->fields.client->think, "CSQC_Think: NULL function\n");
164                 prog->globals.client->self = b;
165         }
166 }
167
168 extern cvar_t cl_noplayershadow;
169 qboolean CSQC_AddRenderEdict(prvm_edict_t *ed)
170 {
171         int i;
172         prvm_eval_t *val;
173         entity_t *e;
174         matrix4x4_t tagmatrix, matrix2;
175
176         e = CL_NewTempEntity();
177         if (!e)
178                 return false;
179
180         i = (int)ed->fields.client->modelindex;
181         if(i >= MAX_MODELS || i <= -MAX_MODELS) //[515]: make work as error ?
182         {
183                 Con_Print("CSQC_AddRenderEdict: modelindex >= MAX_MODELS\n");
184                 ed->fields.client->modelindex = i = 0;
185         }
186
187         // model setup and some modelflags
188         if (i < MAX_MODELS)
189                 e->render.model = cl.model_precache[e->state_current.modelindex];
190         else
191                 e->render.model = cl.csqc_model_precache[65536-e->state_current.modelindex];
192
193         if (!e->render.model)
194                 return false;
195
196         e->render.colormap = (int)ed->fields.client->colormap;
197         e->render.frame = (int)ed->fields.client->frame;
198         e->render.skinnum = (int)ed->fields.client->skin;
199         e->render.effects |= e->render.model->flags2 & (EF_FULLBRIGHT | EF_ADDITIVE);
200
201         if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_alpha)) && val->_float)             e->render.alpha = val->_float;
202         if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_scale)) && val->_float)             e->render.scale = val->_float;
203         if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_colormod)) && VectorLength2(val->vector))   VectorCopy(val->vector, e->render.colormod);
204         if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_effects)) && val->_float)   e->render.effects = (int)val->_float;
205         if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_tag_entity)) && val->edict)
206         {
207                 int tagentity;
208                 int tagindex;
209                 tagentity = val->edict;
210                 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_tag_index)) && val->_float)
211                         tagindex = (int)val->_float;
212                 // FIXME: calculate tag matrix
213                 Matrix4x4_CreateIdentity(&tagmatrix);
214         }
215         else
216                 Matrix4x4_CreateIdentity(&tagmatrix);
217
218         if(i & RF_USEAXIS)      //FIXME!!!
219         {
220                 vec3_t left;
221                 VectorNegate(prog->globals.client->v_right, left);
222                 Matrix4x4_FromVectors(&matrix2, prog->globals.client->v_forward, left, prog->globals.client->v_up, ed->fields.client->origin);
223         }
224         else
225         {
226                 vec3_t angles;
227                 VectorCopy(ed->fields.client->angles, angles);
228                 // if model is alias, reverse pitch direction
229                 if (e->render.model->type == mod_alias)
230                         angles[0] = -angles[0];
231
232                 // set up the render matrix
233                 // FIXME: e->render.scale should go away
234                 Matrix4x4_CreateFromQuakeEntity(&matrix2, ed->fields.client->origin[0], ed->fields.client->origin[1], ed->fields.client->origin[2], angles[0], angles[1], angles[2], e->render.scale);
235         }
236
237         // concat the matrices to make the entity relative to its tag
238         Matrix4x4_Concat(&e->render.matrix, &tagmatrix, &matrix2);
239         // make the other useful stuff
240         Matrix4x4_Invert_Simple(&e->render.inversematrix, &e->render.matrix);
241         CL_BoundingBoxForEntity(&e->render);
242
243         // FIXME: csqc has frame1/frame2/frame1time/frame2time/lerpfrac but this implementation's cl_entvars_t lacks those fields
244         e->render.frame1 = e->render.frame = ed->fields.client->frame;
245         e->render.frame1time = e->render.frame2time = 0;
246         e->render.framelerp = 0;
247         R_LerpAnimation(&e->render);
248
249         i = 0;
250         if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_renderflags)) && val->_float)
251         {
252                 i = (int)val->_float;
253                 if(i & RF_VIEWMODEL)    e->render.flags |= RENDER_VIEWMODEL;
254                 if(i & RF_EXTERNALMODEL)e->render.flags |= RENDER_EXTERIORMODEL;
255                 if(i & RF_DEPTHHACK)    e->render.effects |= EF_NODEPTHTEST;
256                 if(i & RF_ADDITIVE)             e->render.effects |= EF_ADDITIVE;
257         }
258
259         // transparent stuff can't be lit during the opaque stage
260         if (e->render.effects & (EF_ADDITIVE | EF_NODEPTHTEST) || e->render.alpha < 1)
261                 e->render.flags |= RENDER_TRANSPARENT;
262         // double sided rendering mode causes backfaces to be visible
263         // (mostly useful on transparent stuff)
264         if (e->render.effects & EF_DOUBLESIDED)
265                 e->render.flags |= RENDER_NOCULLFACE;
266         // either fullbright or lit
267         if (!(e->render.effects & EF_FULLBRIGHT) && !r_fullbright.integer)
268                 e->render.flags |= RENDER_LIGHT;
269         // hide player shadow during intermission or nehahra movie
270         if (!(e->render.effects & EF_NOSHADOW)
271          && !(e->render.flags & (RENDER_VIEWMODEL | RENDER_TRANSPARENT))
272          && (!(e->render.flags & RENDER_EXTERIORMODEL) || (!cl.intermission && cls.protocol != PROTOCOL_NEHAHRAMOVIE && !cl_noplayershadow.integer)))
273                 e->render.flags |= RENDER_SHADOW;
274
275         return true;
276 }
277
278 void CSQC_ClearCSQCEntities (void)
279 {
280         memset(cl.csqcentities_active, 0, sizeof(cl.csqcentities_active));
281         cl.num_csqcentities = 0;
282 }
283
284 void CL_ExpandCSQCEntities (int num);
285
286 qboolean CL_VM_InputEvent (qboolean pressed, int key)
287 {
288         qboolean r;
289         if(!csqc_loaded)
290                 return false;
291         CSQC_BEGIN
292                 *prog->time = cl.time;
293                 PRVM_G_FLOAT(OFS_PARM0) = pressed;
294                 PRVM_G_FLOAT(OFS_PARM1) = key;
295                 PRVM_ExecuteProgram (prog->globals.client->CSQC_InputEvent, CL_F_INPUTEVENT);
296                 r = CSQC_RETURNVAL;
297         CSQC_END
298         return r;
299 }
300
301 qboolean CL_VM_UpdateView (void)
302 {
303 //      vec3_t oldangles;
304         if(!csqc_loaded)
305                 return false;
306         CSQC_BEGIN
307                 //VectorCopy(cl.viewangles, oldangles);
308                 *prog->time = cl.time;
309                 CSQC_SetGlobals();
310                 cl.num_csqcentities = 0;
311                 PRVM_ExecuteProgram (prog->globals.client->CSQC_UpdateView, CL_F_UPDATEVIEW);
312                 //VectorCopy(oldangles, cl.viewangles);
313         CSQC_END
314         csqc_frame = false;
315         return true;
316 }
317
318 qboolean CL_VM_ConsoleCommand (const char *cmd)
319 {
320         qboolean r;
321         if(!csqc_loaded)
322                 return false;
323         CSQC_BEGIN
324                 *prog->time = cl.time;
325                 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(cmd);
326                 PRVM_ExecuteProgram (prog->globals.client->CSQC_ConsoleCommand, CL_F_CONSOLECOMMAND);
327                 r = CSQC_RETURNVAL;
328         CSQC_END
329         return r;
330 }
331
332 qboolean CL_VM_Parse_TempEntity (void)
333 {
334         int                     t;
335         qboolean        r;
336         if(!csqc_loaded || !CSQC_Parse_TempEntity)
337                 return false;
338         t = msg_readcount;
339         CSQC_BEGIN
340                 *prog->time = cl.time;
341                 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_TempEntity - prog->functions), CL_F_PARSE_TEMPENTITY);
342                 r = CSQC_RETURNVAL;
343         CSQC_END
344         if(!r)
345         {
346                 msg_readcount = t;
347                 msg_badread = false;
348         }
349         return r;
350 }
351
352 void CL_VM_Parse_StuffCmd (const char *msg)
353 {
354         if(!csqc_loaded)        //[515]: add more here
355         if(msg[0] == 'c')
356         if(msg[1] == 's')
357         if(msg[2] == 'q')
358         if(msg[3] == 'c')
359         {
360                 Cvar_SetQuick(&csqc_progcrc, "0");
361                 csqc_progcrc.flags = 0;
362                 Cmd_ExecuteString (msg, src_command);
363                 csqc_progcrc.flags = CVAR_READONLY;
364                 return;
365         }
366         if(!csqc_loaded || !CSQC_Parse_StuffCmd)
367         {
368                 Cbuf_AddText(msg);
369                 return;
370         }
371         CSQC_BEGIN
372                 *prog->time = cl.time;
373                 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(msg);
374                 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_StuffCmd - prog->functions), CL_F_PARSE_STUFFCMD);
375         CSQC_END
376 }
377
378 static void CL_VM_Parse_Print (const char *msg)
379 {
380         CSQC_BEGIN
381                 *prog->time = cl.time;
382                 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(msg);
383                 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_Print - prog->functions), CL_F_PARSE_PRINT);
384         CSQC_END
385 }
386
387 void CSQC_AddPrintText (const char *msg)
388 {
389         size_t i;
390         if(!csqc_loaded || !CSQC_Parse_Print)
391         {
392                 Con_Print(msg);
393                 return;
394         }
395         // FIXME: is this bugged?
396         i = strlen(msg)-1;
397         if(msg[i] != '\n' && msg[i] != '\r')
398         {
399                 if(strlen(csqc_printtextbuf)+i >= CSQC_PRINTBUFFERLEN)
400                 {
401                         CL_VM_Parse_Print(csqc_printtextbuf);
402                         csqc_printtextbuf[0] = 0;
403                 }
404                 else
405                         strlcat(csqc_printtextbuf, msg, CSQC_PRINTBUFFERLEN);
406                 return;
407         }
408         strlcat(csqc_printtextbuf, msg, CSQC_PRINTBUFFERLEN);
409         CL_VM_Parse_Print(csqc_printtextbuf);
410         csqc_printtextbuf[0] = 0;
411 }
412
413 void CL_VM_Parse_CenterPrint (const char *msg)
414 {
415         if(!csqc_loaded || !CSQC_Parse_CenterPrint)
416         {
417                 SCR_CenterPrint((char*)msg);
418                 return;
419         }
420         CSQC_BEGIN
421                 *prog->time = cl.time;
422                 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(msg);
423                 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_CenterPrint - prog->functions), CL_F_PARSE_CENTERPRINT);
424         CSQC_END
425 }
426
427 float CL_VM_Event (float event)         //[515]: needed ? I'd say "YES", but don't know for what :D
428 {
429         float r;
430         if(!csqc_loaded || !CSQC_Event)
431                 return 0;
432         CSQC_BEGIN
433                 *prog->time = cl.time;
434                 PRVM_G_FLOAT(OFS_PARM0) = event;
435                 PRVM_ExecuteProgram ((func_t)(CSQC_Event - prog->functions), CL_F_EVENT);
436                 r = CSQC_RETURNVAL;
437         CSQC_END
438         return r;
439 }
440
441 void CSQC_ReadEntities (void)
442 {
443         unsigned short entnum, oldself, realentnum;
444         CSQC_BEGIN
445                 *prog->time = cl.time;
446                 oldself = prog->globals.client->self;
447                 while(1)
448                 {
449                         entnum = MSG_ReadShort();
450                         if(!entnum)
451                                 return;
452                         realentnum = entnum & 0x7FFF;
453                         prog->globals.client->self = csqc_sv2csqcents[realentnum];
454                         if(entnum & 0x8000)
455                         {
456                                 if(prog->globals.client->self)
457                                 {
458                                         PRVM_ExecuteProgram((func_t)(CSQC_Ent_Remove - prog->functions), CL_F_ENT_REMOVE);
459                                         csqc_sv2csqcents[realentnum] = 0;
460                                 }
461                                 else
462                                         Con_Printf("Smth bad happens in csqc...\n");    //[515]: never happens ?
463                         }
464                         else
465                         {
466                                 if(!prog->globals.client->self)
467                                 {
468                                         prvm_edict_t    *ed;
469                                         ed = PRVM_ED_Alloc();
470                                         ed->fields.client->entnum = realentnum;
471                                         prog->globals.client->self = csqc_sv2csqcents[realentnum] = PRVM_EDICT_TO_PROG(ed);
472                                         PRVM_G_FLOAT(OFS_PARM0) = 1;
473                                 }
474                                 else
475                                         PRVM_G_FLOAT(OFS_PARM0) = 0;
476                                 PRVM_ExecuteProgram((func_t)(CSQC_Ent_Update - prog->functions), CL_F_ENT_UPDATE);
477                         }
478                 }
479                 prog->globals.client->self = oldself;
480         CSQC_END
481 }
482
483 void Cmd_ClearCsqcFuncs (void);
484
485 void CL_VM_Init (void)
486 {
487         entity_t *ent;
488
489         csqc_loaded = false;
490         memset(cl.csqc_model_precache, 0, sizeof(cl.csqc_model_precache));
491         memset(&cl.csqc_vidvars, true, sizeof(csqc_vidvars_t));
492
493         if(!FS_FileExists(csqc_progname.string))
494         {
495                 if(!sv.active && csqc_progcrc.integer)
496                 {
497                         Con_Printf("CL_VM_Init: server requires CSQC, but \"%s\" wasn't found\n", csqc_progname.string);
498                         CL_Disconnect();
499                 }
500                 return;
501         }
502         else
503         {
504                 if (developer.integer < 100)
505                 {
506                         Con_DPrintf("CL_VM_Init: CSQC is broken, and is not being loaded because developer is less than 100.\n");
507                         return;
508                 }
509                 if(!sv.active && !csqc_progcrc.integer) //[515]: because cheaters may use csqc while server didn't allowed it !
510                 {
511                         Con_Printf("CL_VM_Init: server didn't sent CSQC crc, so CSQC is disabled\n");
512                         return;
513                 }
514         }
515
516         PRVM_Begin;
517         PRVM_InitProg(PRVM_CLIENTPROG);
518
519         csqc_mempool = Mem_AllocPool("CSQC", 0, NULL);
520
521         // allocate the mempools
522         prog->progs_mempool = Mem_AllocPool(csqc_progname.string, 0, NULL);
523         prog->headercrc = CL_PROGHEADER_CRC;
524         prog->edictprivate_size = 0; // no private struct used
525         prog->name = CL_NAME;
526         prog->num_edicts = 1;
527         prog->limit_edicts = CL_MAX_EDICTS;
528         prog->extensionstring = vm_cl_extensions;
529         prog->builtins = vm_cl_builtins;
530         prog->numbuiltins = vm_cl_numbuiltins;
531         prog->init_cmd = VM_CL_Cmd_Init;
532         prog->reset_cmd = VM_CL_Cmd_Reset;
533         prog->error_cmd = CL_VM_Error;
534
535         PRVM_LoadProgs(csqc_progname.string, cl_numrequiredfunc, cl_required_func, 0, NULL);
536
537         if(!sv.active && !cls.demoplayback && prog->filecrc != (unsigned short)csqc_progcrc.integer)
538         {
539                 Con_Printf("^1Your %s is not the same version as the server (CRC is %i but should be %i)\n", prog->filecrc, csqc_progcrc.integer);
540                 PRVM_ResetProg();
541                 CL_Disconnect();
542                 return;
543         }
544
545         if(prog->loaded)
546         {
547                 Cvar_SetValueQuick(&csqc_progcrc, prog->filecrc);
548                 Con_Printf("CSQC ^5loaded (crc=%i)\n", csqc_progcrc.integer);
549         }
550         else
551         {
552                 CL_VM_Error("CSQC ^2failed to load\n");
553                 if(!sv.active)
554                         CL_Disconnect();
555                 return;
556         }
557
558         //[515]: optional fields & funcs
559         CL_VM_FindEdictFieldOffsets();
560
561         // set time
562         *prog->time = cl.time;
563         csqc_frametime = 0;
564
565         prog->globals.client->mapname = PRVM_SetEngineString(cl.worldmodel->name);
566         prog->globals.client->player_localentnum = cl.playerentity;
567
568         // call the prog init
569         PRVM_ExecuteProgram((func_t) (PRVM_ED_FindFunction(CL_F_INIT) - prog->functions), CL_F_INIT);
570
571         PRVM_End;
572         csqc_loaded = true;
573
574         csqc_sv2csqcents = (unsigned short *)Mem_Alloc(csqc_mempool, MAX_EDICTS*sizeof(unsigned short));
575         memset(csqc_sv2csqcents, 0, MAX_EDICTS*sizeof(unsigned short));
576
577         cl.csqc_vidvars.drawcrosshair = false;
578         cl.csqc_vidvars.drawenginesbar = false;
579
580         // local state
581         ent = &cl.csqcentities[0];
582         // entire entity array was cleared, so just fill in a few fields
583         ent->state_current.active = true;
584         ent->render.model = cl.worldmodel = cl.model_precache[1];
585         ent->render.scale = 1; // some of the renderer still relies on scale
586         ent->render.alpha = 1;
587         ent->render.flags = RENDER_SHADOW | RENDER_LIGHT;
588         Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, 0, 0, 0, 0, 0, 0, 1);
589         Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
590         CL_BoundingBoxForEntity(&ent->render);
591 }
592
593 void CL_VM_ShutDown (void)
594 {
595         Cmd_ClearCsqcFuncs();
596         Cvar_SetValueQuick(&csqc_progcrc, 0);
597         if(!csqc_loaded)
598                 return;
599         CSQC_BEGIN
600                 *prog->time = cl.time;
601                 PRVM_ExecuteProgram((func_t) (PRVM_ED_FindFunction(CL_F_SHUTDOWN) - prog->functions), CL_F_SHUTDOWN);
602                 PRVM_ResetProg();
603         CSQC_END
604         Con_Print("CSQC ^1unloaded\n");
605         csqc_loaded = false;
606         Mem_FreePool(&csqc_mempool);
607 }