fixed view location bugs in csqc; it was reading the player entity's render.origin...
[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                 Matrix4x4_OriginFromMatrix(&cl.entities[cl.viewentity].render.matrix, 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         // FIXME: csqc has frame1/frame2/frame1time/frame2time/lerpfrac but this implementation's cl_entvars_t lacks those fields
238         e->render.frame1 = e->render.frame = ed->fields.client->frame;
239         e->render.frame1time = e->render.frame2time = 0;
240         e->render.framelerp = 0;
241
242         // concat the matrices to make the entity relative to its tag
243         Matrix4x4_Concat(&e->render.matrix, &tagmatrix, &matrix2);
244         // make the other useful stuff
245         CL_UpdateRenderEntity(&e->render);
246
247         i = 0;
248         if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_renderflags)) && val->_float)
249         {
250                 i = (int)val->_float;
251                 if(i & RF_VIEWMODEL)    e->render.flags |= RENDER_VIEWMODEL;
252                 if(i & RF_EXTERNALMODEL)e->render.flags |= RENDER_EXTERIORMODEL;
253                 if(i & RF_DEPTHHACK)    e->render.effects |= EF_NODEPTHTEST;
254                 if(i & RF_ADDITIVE)             e->render.effects |= EF_ADDITIVE;
255         }
256
257         // transparent stuff can't be lit during the opaque stage
258         if (e->render.effects & (EF_ADDITIVE | EF_NODEPTHTEST) || e->render.alpha < 1)
259                 e->render.flags |= RENDER_TRANSPARENT;
260         // double sided rendering mode causes backfaces to be visible
261         // (mostly useful on transparent stuff)
262         if (e->render.effects & EF_DOUBLESIDED)
263                 e->render.flags |= RENDER_NOCULLFACE;
264         // either fullbright or lit
265         if (!(e->render.effects & EF_FULLBRIGHT) && !r_fullbright.integer)
266                 e->render.flags |= RENDER_LIGHT;
267         // hide player shadow during intermission or nehahra movie
268         if (!(e->render.effects & EF_NOSHADOW)
269          && !(e->render.flags & (RENDER_VIEWMODEL | RENDER_TRANSPARENT))
270          && (!(e->render.flags & RENDER_EXTERIORMODEL) || (!cl.intermission && cls.protocol != PROTOCOL_NEHAHRAMOVIE && !cl_noplayershadow.integer)))
271                 e->render.flags |= RENDER_SHADOW;
272
273         return true;
274 }
275
276 void CSQC_ClearCSQCEntities (void)
277 {
278         memset(cl.csqcentities_active, 0, sizeof(cl.csqcentities_active));
279         cl.num_csqcentities = 0;
280 }
281
282 void CL_ExpandCSQCEntities (int num);
283
284 qboolean CL_VM_InputEvent (qboolean pressed, int key)
285 {
286         qboolean r;
287         if(!csqc_loaded)
288                 return false;
289         CSQC_BEGIN
290                 *prog->time = cl.time;
291                 PRVM_G_FLOAT(OFS_PARM0) = pressed;
292                 PRVM_G_FLOAT(OFS_PARM1) = key;
293                 PRVM_ExecuteProgram (prog->globals.client->CSQC_InputEvent, CL_F_INPUTEVENT);
294                 r = CSQC_RETURNVAL;
295         CSQC_END
296         return r;
297 }
298
299 qboolean CL_VM_UpdateView (void)
300 {
301 //      vec3_t oldangles;
302         if(!csqc_loaded)
303                 return false;
304         CSQC_BEGIN
305                 //VectorCopy(cl.viewangles, oldangles);
306                 *prog->time = cl.time;
307                 CSQC_SetGlobals();
308                 cl.num_csqcentities = 0;
309                 PRVM_ExecuteProgram (prog->globals.client->CSQC_UpdateView, CL_F_UPDATEVIEW);
310                 //VectorCopy(oldangles, cl.viewangles);
311         CSQC_END
312         csqc_frame = false;
313         return true;
314 }
315
316 qboolean CL_VM_ConsoleCommand (const char *cmd)
317 {
318         qboolean r;
319         if(!csqc_loaded)
320                 return false;
321         CSQC_BEGIN
322                 *prog->time = cl.time;
323                 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(cmd);
324                 PRVM_ExecuteProgram (prog->globals.client->CSQC_ConsoleCommand, CL_F_CONSOLECOMMAND);
325                 r = CSQC_RETURNVAL;
326         CSQC_END
327         return r;
328 }
329
330 qboolean CL_VM_Parse_TempEntity (void)
331 {
332         int                     t;
333         qboolean        r;
334         if(!csqc_loaded || !CSQC_Parse_TempEntity)
335                 return false;
336         t = msg_readcount;
337         CSQC_BEGIN
338                 *prog->time = cl.time;
339                 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_TempEntity - prog->functions), CL_F_PARSE_TEMPENTITY);
340                 r = CSQC_RETURNVAL;
341         CSQC_END
342         if(!r)
343         {
344                 msg_readcount = t;
345                 msg_badread = false;
346         }
347         return r;
348 }
349
350 void CL_VM_Parse_StuffCmd (const char *msg)
351 {
352         if(!csqc_loaded)        //[515]: add more here
353         if(msg[0] == 'c')
354         if(msg[1] == 's')
355         if(msg[2] == 'q')
356         if(msg[3] == 'c')
357         {
358                 csqc_progcrc.flags = 0;
359                 Cmd_ExecuteString (msg, src_command);
360                 csqc_progcrc.flags = CVAR_READONLY;
361                 return;
362         }
363         if(!csqc_loaded || !CSQC_Parse_StuffCmd)
364         {
365                 Cbuf_AddText(msg);
366                 return;
367         }
368         CSQC_BEGIN
369                 *prog->time = cl.time;
370                 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(msg);
371                 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_StuffCmd - prog->functions), CL_F_PARSE_STUFFCMD);
372         CSQC_END
373 }
374
375 static void CL_VM_Parse_Print (const char *msg)
376 {
377         CSQC_BEGIN
378                 *prog->time = cl.time;
379                 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(msg);
380                 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_Print - prog->functions), CL_F_PARSE_PRINT);
381         CSQC_END
382 }
383
384 void CSQC_AddPrintText (const char *msg)
385 {
386         size_t i;
387         if(!csqc_loaded || !CSQC_Parse_Print)
388         {
389                 Con_Print(msg);
390                 return;
391         }
392         // FIXME: is this bugged?
393         i = strlen(msg)-1;
394         if(msg[i] != '\n' && msg[i] != '\r')
395         {
396                 if(strlen(csqc_printtextbuf)+i >= CSQC_PRINTBUFFERLEN)
397                 {
398                         CL_VM_Parse_Print(csqc_printtextbuf);
399                         csqc_printtextbuf[0] = 0;
400                 }
401                 else
402                         strlcat(csqc_printtextbuf, msg, CSQC_PRINTBUFFERLEN);
403                 return;
404         }
405         strlcat(csqc_printtextbuf, msg, CSQC_PRINTBUFFERLEN);
406         CL_VM_Parse_Print(csqc_printtextbuf);
407         csqc_printtextbuf[0] = 0;
408 }
409
410 void CL_VM_Parse_CenterPrint (const char *msg)
411 {
412         if(!csqc_loaded || !CSQC_Parse_CenterPrint)
413         {
414                 SCR_CenterPrint((char*)msg);
415                 return;
416         }
417         CSQC_BEGIN
418                 *prog->time = cl.time;
419                 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(msg);
420                 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_CenterPrint - prog->functions), CL_F_PARSE_CENTERPRINT);
421         CSQC_END
422 }
423
424 float CL_VM_Event (float event)         //[515]: needed ? I'd say "YES", but don't know for what :D
425 {
426         float r;
427         if(!csqc_loaded || !CSQC_Event)
428                 return 0;
429         CSQC_BEGIN
430                 *prog->time = cl.time;
431                 PRVM_G_FLOAT(OFS_PARM0) = event;
432                 PRVM_ExecuteProgram ((func_t)(CSQC_Event - prog->functions), CL_F_EVENT);
433                 r = CSQC_RETURNVAL;
434         CSQC_END
435         return r;
436 }
437
438 void CSQC_ReadEntities (void)
439 {
440         unsigned short entnum, oldself, realentnum;
441         CSQC_BEGIN
442                 *prog->time = cl.time;
443                 oldself = prog->globals.client->self;
444                 while(1)
445                 {
446                         entnum = MSG_ReadShort();
447                         if(!entnum)
448                                 return;
449                         realentnum = entnum & 0x7FFF;
450                         prog->globals.client->self = csqc_sv2csqcents[realentnum];
451                         if(entnum & 0x8000)
452                         {
453                                 if(prog->globals.client->self)
454                                 {
455                                         PRVM_ExecuteProgram((func_t)(CSQC_Ent_Remove - prog->functions), CL_F_ENT_REMOVE);
456                                         csqc_sv2csqcents[realentnum] = 0;
457                                 }
458                                 else
459                                         Con_Printf("Smth bad happens in csqc...\n");    //[515]: never happens ?
460                         }
461                         else
462                         {
463                                 if(!prog->globals.client->self)
464                                 {
465                                         prvm_edict_t    *ed;
466                                         ed = PRVM_ED_Alloc();
467                                         ed->fields.client->entnum = realentnum;
468                                         prog->globals.client->self = csqc_sv2csqcents[realentnum] = PRVM_EDICT_TO_PROG(ed);
469                                         PRVM_G_FLOAT(OFS_PARM0) = 1;
470                                 }
471                                 else
472                                         PRVM_G_FLOAT(OFS_PARM0) = 0;
473                                 PRVM_ExecuteProgram((func_t)(CSQC_Ent_Update - prog->functions), CL_F_ENT_UPDATE);
474                         }
475                 }
476                 prog->globals.client->self = oldself;
477         CSQC_END
478 }
479
480 void Cmd_ClearCsqcFuncs (void);
481
482 void CL_VM_Init (void)
483 {
484         unsigned char *csprogsdata;
485         fs_offset_t csprogsdatasize;
486         unsigned int csprogsdatacrc;
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 the server is not requesting a csprogs, then we're done here
494         if (!csqc_progcrc.integer)
495                 return;
496
497         // see if there is a csprogs.dat installed, and if so, set the csqc_progcrc accordingly, this will be sent to connecting clients to tell them to only load a matching csprogs.dat file
498         csprogsdatacrc = 0;
499         csprogsdata = FS_LoadFile(csqc_progname.string, tempmempool, true, &csprogsdatasize);
500         if (csprogsdata)
501         {
502                 csprogsdatacrc = CRC_Block(csprogsdata, csprogsdatasize);
503                 Mem_Free(csprogsdata);
504                 if(csprogsdatacrc != (unsigned short)csqc_progcrc.integer && !sv.active && !cls.demoplayback)
505                 {
506                         Con_Printf("^1Your %s is not the same version as the server (CRC is %i but should be %i)\n", csqc_progname.string, prog->filecrc, csqc_progcrc.integer);
507                         PRVM_ResetProg();
508                         CL_Disconnect();
509                         return;
510                 }
511         }
512         else
513         {
514                 if(!sv.active && csqc_progcrc.integer)
515                 {
516                         Con_Printf("CL_VM_Init: server requires CSQC, but \"%s\" wasn't found\n", csqc_progname.string);
517                         CL_Disconnect();
518                 }
519                 return;
520         }
521
522         PRVM_Begin;
523         PRVM_InitProg(PRVM_CLIENTPROG);
524
525         csqc_mempool = Mem_AllocPool("CSQC", 0, NULL);
526
527         // allocate the mempools
528         prog->progs_mempool = Mem_AllocPool(csqc_progname.string, 0, NULL);
529         prog->headercrc = CL_PROGHEADER_CRC;
530         prog->edictprivate_size = 0; // no private struct used
531         prog->name = CL_NAME;
532         prog->num_edicts = 1;
533         prog->limit_edicts = CL_MAX_EDICTS;
534         prog->extensionstring = vm_cl_extensions;
535         prog->builtins = vm_cl_builtins;
536         prog->numbuiltins = vm_cl_numbuiltins;
537         prog->init_cmd = VM_CL_Cmd_Init;
538         prog->reset_cmd = VM_CL_Cmd_Reset;
539         prog->error_cmd = CL_VM_Error;
540
541         PRVM_LoadProgs(csqc_progname.string, cl_numrequiredfunc, cl_required_func, 0, NULL);
542
543         if(!sv.active && !cls.demoplayback && prog->filecrc != (unsigned short)csqc_progcrc.integer)
544         {
545                 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);
546                 PRVM_ResetProg();
547                 CL_Disconnect();
548                 return;
549         }
550
551         if(prog->loaded)
552                 Con_Printf("CSQC ^5loaded (crc=%i)\n", csqc_progcrc.integer);
553         else
554         {
555                 CL_VM_Error("CSQC ^2failed to load\n");
556                 if(!sv.active)
557                         CL_Disconnect();
558                 return;
559         }
560
561         //[515]: optional fields & funcs
562         CL_VM_FindEdictFieldOffsets();
563
564         // set time
565         *prog->time = cl.time;
566         csqc_frametime = 0;
567
568         prog->globals.client->mapname = PRVM_SetEngineString(cl.worldmodel->name);
569         prog->globals.client->player_localentnum = cl.playerentity;
570
571         // call the prog init
572         PRVM_ExecuteProgram((func_t) (PRVM_ED_FindFunction(CL_F_INIT) - prog->functions), CL_F_INIT);
573
574         PRVM_End;
575         csqc_loaded = true;
576
577         csqc_sv2csqcents = (unsigned short *)Mem_Alloc(csqc_mempool, MAX_EDICTS*sizeof(unsigned short));
578         memset(csqc_sv2csqcents, 0, MAX_EDICTS*sizeof(unsigned short));
579
580         cl.csqc_vidvars.drawcrosshair = false;
581         cl.csqc_vidvars.drawenginesbar = false;
582
583         // local state
584         ent = &cl.csqcentities[0];
585         // entire entity array was cleared, so just fill in a few fields
586         ent->state_current.active = true;
587         ent->render.model = cl.worldmodel = cl.model_precache[1];
588         ent->render.alpha = 1;
589         ent->render.flags = RENDER_SHADOW | RENDER_LIGHT;
590         Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, 0, 0, 0, 0, 0, 0, 1);
591         CL_UpdateRenderEntity(&ent->render);
592 }
593
594 void CL_VM_ShutDown (void)
595 {
596         Cmd_ClearCsqcFuncs();
597         Cvar_SetValueQuick(&csqc_progcrc, 0);
598         if(!csqc_loaded)
599                 return;
600         CSQC_BEGIN
601                 *prog->time = cl.time;
602                 PRVM_ExecuteProgram((func_t) (PRVM_ED_FindFunction(CL_F_SHUTDOWN) - prog->functions), CL_F_SHUTDOWN);
603                 PRVM_ResetProg();
604         CSQC_END
605         Con_Print("CSQC ^1unloaded\n");
606         csqc_loaded = false;
607         Mem_FreePool(&csqc_mempool);
608 }