]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - csprogs.c
ada3b073b486425d18942ca5abd926bcff4f86df
[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 #include "snd_main.h"
7 #include "clvm_cmds.h"
8
9 //============================================================================
10 // Client prog handling
11 //[515]: omg !!! optimize it ! a lot of hacks here and there also :P
12
13 #define CSQC_RETURNVAL  prog->globals.generic[OFS_RETURN]
14 #define CSQC_BEGIN              csqc_tmpprog=prog;prog=0;PRVM_SetProg(PRVM_CLIENTPROG);
15 #define CSQC_END                prog=csqc_tmpprog;
16 static prvm_prog_t *csqc_tmpprog;
17
18 //[515]: these are required funcs
19 static char *cl_required_func[] =
20 {
21         "CSQC_Init",
22         "CSQC_InputEvent",
23         "CSQC_UpdateView",
24         "CSQC_ConsoleCommand",
25 };
26
27 static int cl_numrequiredfunc = sizeof(cl_required_func) / sizeof(char*);
28
29 void CL_VM_Error (const char *format, ...) DP_FUNC_PRINTF(1);
30 void CL_VM_Error (const char *format, ...)      //[515]: hope it will be never executed =)
31 {
32         char errorstring[4096];
33         va_list argptr;
34
35         va_start (argptr, format);
36         dpvsnprintf (errorstring, sizeof(errorstring), format, argptr);
37         va_end (argptr);
38 //      Con_Printf( "CL_VM_Error: %s\n", errorstring );
39
40         PRVM_Crash();
41         cl.csqc_loaded = false;
42
43         Cvar_SetValueQuick(&csqc_progcrc, -1);
44         Cvar_SetValueQuick(&csqc_progsize, -1);
45
46 //      Host_AbortCurrentFrame();       //[515]: hmmm... if server says it needs csqc then client MUST disconnect
47         Host_Error(va("CL_VM_Error: %s", errorstring));
48 }
49 void CL_VM_UpdateDmgGlobals (int dmg_take, int dmg_save, vec3_t dmg_origin)
50 {
51         prvm_eval_t *val;
52         if(cl.csqc_loaded)
53         {
54                 CSQC_BEGIN
55                 val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.dmg_take);
56                 if(val)
57                         val->_float = dmg_take;
58                 val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.dmg_save);
59                 if(val)
60                         val->_float = dmg_save;
61                 val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.dmg_origin);
62                 if(val)
63                 {
64                         val->vector[0] = dmg_origin[0];
65                         val->vector[1] = dmg_origin[1];
66                         val->vector[2] = dmg_origin[2];
67                 }
68                 CSQC_END
69         }
70 }
71 //[515]: set globals before calling R_UpdateView, WEIRD CRAP
72 static void CSQC_SetGlobals (void)
73 {
74         prvm_eval_t *val;
75         CSQC_BEGIN
76                 prog->globals.client->time = cl.time;
77                 prog->globals.client->frametime = max(0, cl.time - cl.oldtime);
78                 prog->globals.client->servercommandframe = cls.servermovesequence;
79                 prog->globals.client->clientcommandframe = cl.movecmd[0].sequence;
80                 VectorCopy(cl.viewangles, prog->globals.client->input_angles);
81                 VectorCopy(cl.viewangles, cl.csqc_angles);
82                 // // FIXME: this actually belongs into getinputstate().. [12/17/2007 Black]
83                 prog->globals.client->input_buttons = cl.movecmd[0].buttons;
84                 VectorSet(prog->globals.client->input_movevalues, cl.movecmd[0].forwardmove, cl.movecmd[0].sidemove, cl.movecmd[0].upmove);
85                 //VectorCopy(cl.movement_origin, cl.csqc_origin);
86                 Matrix4x4_OriginFromMatrix(&cl.entities[cl.viewentity].render.matrix, cl.csqc_origin);
87
88                 // LordHavoc: Spike says not to do this, but without pmove_org the
89                 // CSQC is useless as it can't alter the view origin without
90                 // completely replacing it
91                 VectorCopy(cl.csqc_origin, prog->globals.client->pmove_org);
92                 VectorCopy(cl.velocity, prog->globals.client->pmove_vel);
93
94                 if ((val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.view_angles)))
95                         VectorCopy(cl.viewangles, val->vector);
96                 prog->globals.client->maxclients = cl.maxclients;
97         CSQC_END
98 }
99
100 void CSQC_Predraw (prvm_edict_t *ed)
101 {
102         int b;
103         if(!ed->fields.client->predraw)
104                 return;
105         b = prog->globals.client->self;
106         prog->globals.client->self = PRVM_EDICT_TO_PROG(ed);
107         PRVM_ExecuteProgram(ed->fields.client->predraw, "CSQC_Predraw: NULL function\n");
108         prog->globals.client->self = b;
109 }
110
111 void CSQC_Think (prvm_edict_t *ed)
112 {
113         int b;
114         if(ed->fields.client->think)
115         if(ed->fields.client->nextthink && ed->fields.client->nextthink <= prog->globals.client->time)
116         {
117                 ed->fields.client->nextthink = 0;
118                 b = prog->globals.client->self;
119                 prog->globals.client->self = PRVM_EDICT_TO_PROG(ed);
120                 PRVM_ExecuteProgram(ed->fields.client->think, "CSQC_Think: NULL function\n");
121                 prog->globals.client->self = b;
122         }
123 }
124
125 extern cvar_t cl_noplayershadow;
126 qboolean CSQC_AddRenderEdict(prvm_edict_t *ed)
127 {
128         int renderflags;
129         int c;
130         float scale;
131         prvm_eval_t *val;
132         entity_t *e;
133         model_t *model;
134         matrix4x4_t tagmatrix, matrix2;
135
136         model = CL_GetModelFromEdict(ed);
137         if (!model)
138                 return false;
139
140         e = CL_NewTempEntity();
141         if (!e)
142                 return false;
143
144         e->render.model = model;
145         e->render.skinnum = (int)ed->fields.client->skin;
146         e->render.effects |= e->render.model->effects;
147         scale = 1;
148         renderflags = 0;
149         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.renderflags)) && val->_float)     renderflags = (int)val->_float;
150         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.alpha)) && val->_float)           e->render.alpha = val->_float;
151         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.scale)) && val->_float)           e->render.scale = scale = val->_float;
152         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.colormod)) && VectorLength2(val->vector)) VectorCopy(val->vector, e->render.colormod);
153         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.effects)) && val->_float) e->render.effects |= (int)val->_float;
154         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.tag_entity)) && val->edict)
155         {
156                 int tagentity;
157                 int tagindex = 0;
158                 tagentity = val->edict;
159                 if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.tag_index)) && val->_float)
160                         tagindex = (int)val->_float;
161                 CL_GetTagMatrix (&tagmatrix, PRVM_PROG_TO_EDICT(tagentity), tagindex);
162         }
163         else
164                 Matrix4x4_CreateIdentity(&tagmatrix);
165
166         if (renderflags & RF_USEAXIS)
167         {
168                 vec3_t left;
169                 VectorNegate(prog->globals.client->v_right, left);
170                 Matrix4x4_FromVectors(&matrix2, prog->globals.client->v_forward, left, prog->globals.client->v_up, ed->fields.client->origin);
171                 Matrix4x4_Scale(&matrix2, scale, 1);
172         }
173         else
174         {
175                 vec3_t angles;
176                 VectorCopy(ed->fields.client->angles, angles);
177                 // if model is alias, reverse pitch direction
178                 if (e->render.model->type == mod_alias)
179                         angles[0] = -angles[0];
180
181                 // set up the render matrix
182                 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);
183         }
184
185         // set up the animation data
186         // self.frame is the interpolation target (new frame)
187         // self.frame1time is the animation base time for the interpolation target
188         // self.frame2 is the interpolation start (previous frame)
189         // self.frame2time is the animation base time for the interpolation start
190         e->render.frame1 = e->render.frame2 = ed->fields.client->frame;
191         if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame2))) e->render.frame2 = val->_float;
192         if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame1time))) e->render.frame2time = val->_float;
193         if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame2time))) e->render.frame1time = val->_float;
194         if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.lerpfrac))) e->render.framelerp = val->_float;
195
196         // concat the matrices to make the entity relative to its tag
197         Matrix4x4_Concat(&e->render.matrix, &tagmatrix, &matrix2);
198
199         if(renderflags)
200         {
201                 if(renderflags & RF_VIEWMODEL)  e->render.flags |= RENDER_VIEWMODEL;
202                 if(renderflags & RF_EXTERNALMODEL)e->render.flags |= RENDER_EXTERIORMODEL;
203                 if(renderflags & RF_DEPTHHACK)  e->render.effects |= EF_NODEPTHTEST;
204                 if(renderflags & RF_ADDITIVE)           e->render.effects |= EF_ADDITIVE;
205         }
206
207         c = (int)ed->fields.client->colormap;
208         if (c <= 0)
209                 CL_SetEntityColormapColors(&e->render, -1);
210         else if (c <= cl.maxclients && cl.scores != NULL)
211                 CL_SetEntityColormapColors(&e->render, cl.scores[c-1].colors);
212         else
213                 CL_SetEntityColormapColors(&e->render, c);
214
215         // either fullbright or lit
216         if (!(e->render.effects & EF_FULLBRIGHT) && !r_fullbright.integer)
217                 e->render.flags |= RENDER_LIGHT;
218         // hide player shadow during intermission or nehahra movie
219         if (!(e->render.effects & (EF_NOSHADOW | EF_ADDITIVE | EF_NODEPTHTEST))
220          &&  (e->render.alpha >= 1)
221          && !(e->render.flags & RENDER_VIEWMODEL)
222          && (!(e->render.flags & RENDER_EXTERIORMODEL) || (!cl.intermission && cls.protocol != PROTOCOL_NEHAHRAMOVIE && !cl_noplayershadow.integer)))
223                 e->render.flags |= RENDER_SHADOW;
224         if (e->render.flags & RENDER_VIEWMODEL)
225                 e->render.flags |= RENDER_NOSELFSHADOW;
226
227         // make the other useful stuff
228         CL_UpdateRenderEntity(&e->render);
229
230         return true;
231 }
232
233 qboolean CL_VM_InputEvent (qboolean down, int key, int ascii)
234 {
235         qboolean r;
236         prvm_eval_t* val;
237         if(!cl.csqc_loaded)
238                 return false;
239         CSQC_BEGIN
240                 if (!prog->funcoffsets.CSQC_InputEvent)
241                         r = false;
242                 else
243                 {
244                         prog->globals.client->time = cl.time;
245                         PRVM_G_FLOAT(OFS_PARM0) = !down; // 0 is down, 1 is up
246                         PRVM_G_FLOAT(OFS_PARM1) = key;
247                         PRVM_G_FLOAT(OFS_PARM2) = ascii;
248                         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_InputEvent, "QC function CSQC_InputEvent is missing");
249                         r = CSQC_RETURNVAL;
250                 }
251         CSQC_END
252         return r;
253 }
254
255 qboolean CL_VM_UpdateView (void)
256 {
257         vec3_t emptyvector;
258         emptyvector[0] = 0;
259         emptyvector[1] = 0;
260         emptyvector[2] = 0;
261 //      vec3_t oldangles;
262         if(!cl.csqc_loaded)
263                 return false;
264         CSQC_BEGIN
265                 //VectorCopy(cl.viewangles, oldangles);
266                 prog->globals.client->time = cl.time;
267                 CSQC_SetGlobals();
268                 // clear renderable entity and light lists to prevent crashes if the
269                 // CSQC_UpdateView function does not call R_ClearScene as it should
270                 r_refdef.numentities = 0;
271                 r_refdef.numlights = 0;
272                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_UpdateView, "QC function CSQC_UpdateView is missing");
273                 //VectorCopy(oldangles, cl.viewangles);
274                 // Dresk : Reset Dmg Globals Here
275                 CL_VM_UpdateDmgGlobals(0, 0, emptyvector);
276         CSQC_END
277         return true;
278 }
279
280 extern sizebuf_t vm_tempstringsbuf;
281 qboolean CL_VM_ConsoleCommand (const char *cmd)
282 {
283         int restorevm_tempstringsbuf_cursize;
284         qboolean r = false;
285         if(!cl.csqc_loaded)
286                 return false;
287         CSQC_BEGIN
288         if (prog->funcoffsets.CSQC_ConsoleCommand)
289         {
290                 prog->globals.client->time = cl.time;
291                 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
292                 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(cmd);
293                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_ConsoleCommand, "QC function CSQC_ConsoleCommand is missing");
294                 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
295                 r = CSQC_RETURNVAL;
296         }
297         CSQC_END
298         return r;
299 }
300
301 qboolean CL_VM_Parse_TempEntity (void)
302 {
303         int                     t;
304         qboolean        r = false;
305         if(!cl.csqc_loaded)
306                 return false;
307         CSQC_BEGIN
308         if(prog->funcoffsets.CSQC_Parse_TempEntity)
309         {
310                 t = msg_readcount;
311                 prog->globals.client->time = cl.time;
312                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_TempEntity, "QC function CSQC_Parse_TempEntity is missing");
313                 r = CSQC_RETURNVAL;
314                 if(!r)
315                 {
316                         msg_readcount = t;
317                         msg_badread = false;
318                 }
319         }
320         CSQC_END
321         return r;
322 }
323
324 void CL_VM_Parse_StuffCmd (const char *msg)
325 {
326         int restorevm_tempstringsbuf_cursize;
327         if(msg[0] == 'c')
328         if(msg[1] == 's')
329         if(msg[2] == 'q')
330         if(msg[3] == 'c')
331         {
332                 // if this is setting a csqc variable, deprotect csqc_progcrc
333                 // temporarily so that it can be set by the cvar command,
334                 // and then reprotect it afterwards
335                 int crcflags = csqc_progcrc.flags;
336                 int sizeflags = csqc_progcrc.flags;
337                 csqc_progcrc.flags &= ~CVAR_READONLY;
338                 csqc_progsize.flags &= ~CVAR_READONLY;
339                 Cmd_ExecuteString (msg, src_command);
340                 csqc_progcrc.flags = crcflags;
341                 csqc_progsize.flags = sizeflags;
342                 return;
343         }
344         if(!cl.csqc_loaded)
345         {
346                 Cbuf_AddText(msg);
347                 return;
348         }
349         CSQC_BEGIN
350         if(prog->funcoffsets.CSQC_Parse_StuffCmd)
351         {
352                 prog->globals.client->time = cl.time;
353                 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
354                 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(msg);
355                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_StuffCmd, "QC function CSQC_Parse_StuffCmd is missing");
356                 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
357         }
358         else
359                 Cbuf_AddText(msg);
360         CSQC_END
361 }
362
363 static void CL_VM_Parse_Print (const char *msg)
364 {
365         int restorevm_tempstringsbuf_cursize;
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_Print, "QC function CSQC_Parse_Print is missing");
370         vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
371 }
372
373 void CSQC_AddPrintText (const char *msg)
374 {
375         size_t i;
376         if(!cl.csqc_loaded)
377         {
378                 Con_Print(msg);
379                 return;
380         }
381         CSQC_BEGIN
382         if(prog->funcoffsets.CSQC_Parse_Print)
383         {
384                 // FIXME: is this bugged?
385                 i = strlen(msg)-1;
386                 if(msg[i] != '\n' && msg[i] != '\r')
387                 {
388                         if(strlen(cl.csqc_printtextbuf)+i >= MAX_INPUTLINE)
389                         {
390                                 CL_VM_Parse_Print(cl.csqc_printtextbuf);
391                                 cl.csqc_printtextbuf[0] = 0;
392                         }
393                         else
394                                 strlcat(cl.csqc_printtextbuf, msg, MAX_INPUTLINE);
395                         return;
396                 }
397                 strlcat(cl.csqc_printtextbuf, msg, MAX_INPUTLINE);
398                 CL_VM_Parse_Print(cl.csqc_printtextbuf);
399                 cl.csqc_printtextbuf[0] = 0;
400         }
401         else
402                 Con_Print(msg);
403         CSQC_END
404 }
405
406 void CL_VM_Parse_CenterPrint (const char *msg)
407 {
408         int restorevm_tempstringsbuf_cursize;
409         if(!cl.csqc_loaded)
410         {
411                 SCR_CenterPrint((char*)msg);
412                 return;
413         }
414         CSQC_BEGIN
415         if(prog->funcoffsets.CSQC_Parse_CenterPrint)
416         {
417                 prog->globals.client->time = cl.time;
418                 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
419                 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(msg);
420                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_CenterPrint, "QC function CSQC_Parse_CenterPrint is missing");
421                 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
422         }
423         else
424                 SCR_CenterPrint((char*)msg);
425         CSQC_END
426 }
427
428 void CL_VM_UpdateIntermissionState (int intermission)
429 {
430         prvm_eval_t *val;
431         if(cl.csqc_loaded)
432         {
433                 CSQC_BEGIN
434                 val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.intermission);
435                 if(val)
436                         val->_float = intermission;
437                 CSQC_END
438         }
439 }
440 void CL_VM_UpdateShowingScoresState (int showingscores)
441 {
442         prvm_eval_t *val;
443         if(cl.csqc_loaded)
444         {
445                 CSQC_BEGIN
446                 val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.sb_showscores);
447                 if(val)
448                         val->_float = showingscores;
449                 CSQC_END
450         }
451 }
452 qboolean CL_VM_Event_Sound(int sound_num, int volume, int channel, float attenuation, int ent, vec3_t pos)
453 {
454         qboolean r = false;
455         if(cl.csqc_loaded)
456         {
457                 CSQC_BEGIN
458                 if(prog->funcoffsets.CSQC_Event_Sound)
459                 {
460                         prog->globals.client->time = cl.time;
461                         PRVM_G_FLOAT(OFS_PARM0) = ent;
462                         PRVM_G_FLOAT(OFS_PARM1) = channel;
463                         PRVM_G_INT(OFS_PARM2) = PRVM_SetTempString(cl.sound_name[sound_num] );
464                         PRVM_G_FLOAT(OFS_PARM3) = volume;
465                         PRVM_G_FLOAT(OFS_PARM4) = attenuation;
466                         VectorCopy(pos, PRVM_G_VECTOR(OFS_PARM5) );
467                         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Event_Sound, "QC function CSQC_Event_Sound is missing");
468                         r = CSQC_RETURNVAL;
469                 }
470                 CSQC_END
471         }
472
473         return r;
474 }
475 void CL_VM_UpdateCoopDeathmatchGlobals (int gametype)
476 {
477         // Avoid global names for clean(er) coding
478         int localcoop;
479         int localdeathmatch;
480
481         prvm_eval_t *val;
482         if(cl.csqc_loaded)
483         {
484                 if(gametype == GAME_COOP)
485                 {
486                         localcoop = 1;
487                         localdeathmatch = 0;
488                 }
489                 else
490                 if(gametype == GAME_DEATHMATCH)
491                 {
492                         localcoop = 0;
493                         localdeathmatch = 1;
494                 }
495                 else
496                 {
497                         // How did the ServerInfo send an unknown gametype?
498                         // Better just assign the globals as 0...
499                         localcoop = 0;
500                         localdeathmatch = 0;
501                 }
502                 CSQC_BEGIN
503                 val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.coop);
504                 if(val)
505                         val->_float = localcoop;
506                 val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.deathmatch);
507                 if(val)
508                         val->_float = localdeathmatch;
509                 CSQC_END
510         }
511 }
512 float CL_VM_Event (float event)         //[515]: needed ? I'd say "YES", but don't know for what :D
513 {
514         float r = 0;
515         if(!cl.csqc_loaded)
516                 return 0;
517         CSQC_BEGIN
518         if(prog->funcoffsets.CSQC_Event)
519         {
520                 prog->globals.client->time = cl.time;
521                 PRVM_G_FLOAT(OFS_PARM0) = event;
522                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Event, "QC function CSQC_Event is missing");
523                 r = CSQC_RETURNVAL;
524         }
525         CSQC_END
526         return r;
527 }
528
529 void CSQC_ReadEntities (void)
530 {
531         unsigned short entnum, oldself, realentnum;
532         if(!cl.csqc_loaded)
533         {
534                 Host_Error ("CSQC_ReadEntities: CSQC is not loaded");
535                 return;
536         }
537
538         CSQC_BEGIN
539                 prog->globals.client->time = cl.time;
540                 oldself = prog->globals.client->self;
541                 while(1)
542                 {
543                         entnum = MSG_ReadShort();
544                         if(!entnum || msg_badread)
545                                 return;
546                         realentnum = entnum & 0x7FFF;
547                         prog->globals.client->self = cl.csqc_server2csqcentitynumber[realentnum];
548                         if(entnum & 0x8000)
549                         {
550                                 if(prog->globals.client->self)
551                                 {
552                                         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Ent_Remove, "QC function CSQC_Ent_Remove is missing");
553                                         cl.csqc_server2csqcentitynumber[realentnum] = 0;
554                                 }
555                                 else
556                                         Con_Printf("Smth bad happens in csqc...\n");    //[515]: never happens ?
557                         }
558                         else
559                         {
560                                 if(!prog->globals.client->self)
561                                 {
562                                         prvm_edict_t    *ed;
563                                         ed = PRVM_ED_Alloc();
564                                         ed->fields.client->entnum = realentnum;
565                                         prog->globals.client->self = cl.csqc_server2csqcentitynumber[realentnum] = PRVM_EDICT_TO_PROG(ed);
566                                         PRVM_G_FLOAT(OFS_PARM0) = 1;
567                                 }
568                                 else
569                                         PRVM_G_FLOAT(OFS_PARM0) = 0;
570                                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Ent_Update, "QC function CSQC_Ent_Update is missing");
571                         }
572                 }
573                 prog->globals.client->self = oldself;
574         CSQC_END
575 }
576
577 void CL_VM_CB_BeginIncreaseEdicts(void)
578 {
579         int i;
580         prvm_edict_t *ent;
581
582         // links don't survive the transition, so unlink everything
583         for (i = 0, ent = prog->edicts;i < prog->max_edicts;i++, ent++)
584         {
585                 if (!ent->priv.server->free)
586                         World_UnlinkEdict(prog->edicts + i);
587                 memset(&ent->priv.server->areagrid, 0, sizeof(ent->priv.server->areagrid));
588         }
589         World_Clear(&cl.world);
590 }
591
592 void CL_VM_CB_EndIncreaseEdicts(void)
593 {
594         int i;
595         prvm_edict_t *ent;
596
597         // link every entity except world
598         for (i = 1, ent = prog->edicts;i < prog->max_edicts;i++, ent++)
599                 if (!ent->priv.server->free)
600                         CL_LinkEdict(ent);
601 }
602
603 void CL_VM_CB_InitEdict(prvm_edict_t *e)
604 {
605         e->priv.server->move = false; // don't move on first frame
606 }
607
608 void CL_VM_CB_FreeEdict(prvm_edict_t *ed)
609 {
610         World_UnlinkEdict(ed);
611         memset(ed->fields.client, 0, sizeof(*ed->fields.client));
612 }
613
614 void CL_VM_CB_CountEdicts(void)
615 {
616         int             i;
617         prvm_edict_t    *ent;
618         int             active = 0, models = 0, solid = 0;
619
620         for (i=0 ; i<prog->num_edicts ; i++)
621         {
622                 ent = PRVM_EDICT_NUM(i);
623                 if (ent->priv.server->free)
624                         continue;
625                 active++;
626                 if (ent->fields.client->solid)
627                         solid++;
628                 if (ent->fields.client->model)
629                         models++;
630         }
631
632         Con_Printf("num_edicts:%3i\n", prog->num_edicts);
633         Con_Printf("active    :%3i\n", active);
634         Con_Printf("view      :%3i\n", models);
635         Con_Printf("touch     :%3i\n", solid);
636 }
637
638 qboolean CL_VM_CB_LoadEdict(prvm_edict_t *ent)
639 {
640         return true;
641 }
642
643 void Cmd_ClearCsqcFuncs (void);
644
645 void CL_VM_Init (void)
646 {
647         const char* csprogsfn;
648         unsigned char *csprogsdata;
649         fs_offset_t csprogsdatasize;
650         int csprogsdatacrc, requiredcrc;
651         int requiredsize;
652         prvm_eval_t *val;
653
654         // reset csqc_progcrc after reading it, so that changing servers doesn't
655         // expect csqc on the next server
656         requiredcrc = csqc_progcrc.integer;
657         requiredsize = csqc_progsize.integer;
658         Cvar_SetValueQuick(&csqc_progcrc, -1);
659         Cvar_SetValueQuick(&csqc_progsize, -1);
660
661         // if the server is not requesting a csprogs, then we're done here
662         if (requiredcrc < 0)
663                 return;
664
665         // see if the requested csprogs.dat file matches the requested crc
666         csprogsdatacrc = -1;
667         csprogsfn = va("dlcache/%s.%i.%i", csqc_progname.string, requiredsize, requiredcrc);
668         csprogsdata = FS_LoadFile(csprogsfn, tempmempool, true, &csprogsdatasize);
669         if (!csprogsdata)
670         {
671                 csprogsfn = csqc_progname.string;
672                 csprogsdata = FS_LoadFile(csprogsfn, tempmempool, true, &csprogsdatasize);
673         }
674         if (csprogsdata)
675         {
676                 csprogsdatacrc = CRC_Block(csprogsdata, csprogsdatasize);
677                 Mem_Free(csprogsdata);
678                 if (csprogsdatacrc != requiredcrc || csprogsdatasize != requiredsize)
679                 {
680                         if (cls.demoplayback)
681                         {
682                                 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);
683                                 return;
684                         }
685                         else
686                         {
687                                 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);
688                                 CL_Disconnect();
689                                 return;
690                         }
691                 }
692         }
693         else
694         {
695                 if (requiredcrc >= 0)
696                 {
697                         if (cls.demoplayback)
698                                 Con_Printf("CL_VM_Init: demo requires CSQC, but \"%s\" wasn't found\n", csqc_progname.string);
699                         else
700                                 Con_Printf("CL_VM_Init: server requires CSQC, but \"%s\" wasn't found\n", csqc_progname.string);
701                         CL_Disconnect();
702                 }
703                 return;
704         }
705
706         PRVM_Begin;
707         PRVM_InitProg(PRVM_CLIENTPROG);
708
709         // allocate the mempools
710         prog->progs_mempool = Mem_AllocPool(csqc_progname.string, 0, NULL);
711         prog->headercrc = CL_PROGHEADER_CRC;
712         prog->edictprivate_size = 0; // no private struct used
713         prog->name = CL_NAME;
714         prog->num_edicts = 1;
715         prog->max_edicts = 512;
716         prog->limit_edicts = CL_MAX_EDICTS;
717         prog->reserved_edicts = 0;
718         prog->edictprivate_size = sizeof(edict_engineprivate_t);
719         // TODO: add a shared extension string #define and add real support for csqc extension strings [12/5/2007 Black]
720         prog->extensionstring = vm_sv_extensions;
721         prog->builtins = vm_cl_builtins;
722         prog->numbuiltins = vm_cl_numbuiltins;
723         prog->begin_increase_edicts = CL_VM_CB_BeginIncreaseEdicts;
724         prog->end_increase_edicts = CL_VM_CB_EndIncreaseEdicts;
725         prog->init_edict = CL_VM_CB_InitEdict;
726         prog->free_edict = CL_VM_CB_FreeEdict;
727         prog->count_edicts = CL_VM_CB_CountEdicts;
728         prog->load_edict = CL_VM_CB_LoadEdict;
729         prog->init_cmd = VM_CL_Cmd_Init;
730         prog->reset_cmd = VM_CL_Cmd_Reset;
731         prog->error_cmd = CL_VM_Error;
732
733         PRVM_LoadProgs(csprogsfn, cl_numrequiredfunc, cl_required_func, 0, NULL, 0, NULL);
734
735         if (!prog->loaded)
736         {
737                 CL_VM_Error("CSQC %s ^2failed to load\n", csprogsfn);
738                 if(!sv.active)
739                         CL_Disconnect();
740                 return;
741         }
742
743         Con_Printf("CSQC %s ^5loaded (crc=%i, size=%i)\n", csprogsfn, csprogsdatacrc, (int)csprogsdatasize);
744
745         // check if OP_STATE animation is possible in this dat file
746         if (prog->fieldoffsets.nextthink >= 0 && prog->fieldoffsets.frame >= 0 && prog->fieldoffsets.think >= 0 && prog->globaloffsets.self >= 0)
747                 prog->flag |= PRVM_OP_STATE;
748
749         // set time
750         prog->globals.client->time = cl.time;
751
752         prog->globals.client->mapname = PRVM_SetEngineString(cl.worldmodel->name);
753         prog->globals.client->player_localentnum = cl.playerentity;
754
755         // set map description (use world entity 0)
756         val = PRVM_EDICTFIELDVALUE(prog->edicts, prog->fieldoffsets.message);
757         if(val)
758                 val->string = PRVM_SetEngineString(cl.levelname);
759
760         // call the prog init
761         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Init, "QC function CSQC_Init is missing");
762
763         PRVM_End;
764         cl.csqc_loaded = true;
765
766         cl.csqc_vidvars.drawcrosshair = false;
767         cl.csqc_vidvars.drawenginesbar = false;
768
769         // Update Coop and Deathmatch Globals (at this point the client knows them from ServerInfo)
770         CL_VM_UpdateCoopDeathmatchGlobals(cl.gametype);
771 }
772
773 void CL_VM_ShutDown (void)
774 {
775         Cmd_ClearCsqcFuncs();
776         Cvar_SetValueQuick(&csqc_progcrc, -1);
777         Cvar_SetValueQuick(&csqc_progsize, -1);
778         if(!cl.csqc_loaded)
779                 return;
780         CSQC_BEGIN
781                 prog->globals.client->time = cl.time;
782                 if (prog->funcoffsets.CSQC_Shutdown)
783                         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Shutdown, "QC function CSQC_Shutdown is missing");
784                 PRVM_ResetProg();
785         CSQC_END
786         Con_Print("CSQC ^1unloaded\n");
787         cl.csqc_loaded = false;
788 }