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