]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - csprogs.c
c6b38b152094497e001053ee59e549f2fdda894b
[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_render_t *entrender;
133         model_t *model;
134         matrix4x4_t tagmatrix, matrix2;
135
136         model = CL_GetModelFromEdict(ed);
137         if (!model)
138                 return false;
139
140         entrender = CL_NewTempEntity();
141         if (!entrender)
142                 return false;
143
144         entrender->model = model;
145         entrender->skinnum = (int)ed->fields.client->skin;
146         entrender->effects |= entrender->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)           entrender->alpha = val->_float;
151         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.scale)) && val->_float)           entrender->scale = scale = val->_float;
152         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.colormod)) && VectorLength2(val->vector)) VectorCopy(val->vector, entrender->colormod);
153         if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.effects)) && val->_float) entrender->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 (entrender->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         entrender->frame1 = entrender->frame2 = ed->fields.client->frame;
191         if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame2))) entrender->frame2 = val->_float;
192         if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame1time))) entrender->frame2time = val->_float;
193         if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame2time))) entrender->frame1time = val->_float;
194         if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.lerpfrac))) entrender->framelerp = val->_float;
195
196         // concat the matrices to make the entity relative to its tag
197         Matrix4x4_Concat(&entrender->matrix, &tagmatrix, &matrix2);
198
199         if(renderflags)
200         {
201                 if(renderflags & RF_VIEWMODEL)  entrender->flags |= RENDER_VIEWMODEL;
202                 if(renderflags & RF_EXTERNALMODEL)entrender->flags |= RENDER_EXTERIORMODEL;
203                 if(renderflags & RF_DEPTHHACK)  entrender->effects |= EF_NODEPTHTEST;
204                 if(renderflags & RF_ADDITIVE)           entrender->effects |= EF_ADDITIVE;
205         }
206
207         c = (int)ed->fields.client->colormap;
208         if (c <= 0)
209                 CL_SetEntityColormapColors(entrender, -1);
210         else if (c <= cl.maxclients && cl.scores != NULL)
211                 CL_SetEntityColormapColors(entrender, cl.scores[c-1].colors);
212         else
213                 CL_SetEntityColormapColors(entrender, c);
214
215         // either fullbright or lit
216         if (!(entrender->effects & EF_FULLBRIGHT) && !r_fullbright.integer)
217                 entrender->flags |= RENDER_LIGHT;
218         // hide player shadow during intermission or nehahra movie
219         if (!(entrender->effects & (EF_NOSHADOW | EF_ADDITIVE | EF_NODEPTHTEST))
220          &&  (entrender->alpha >= 1)
221          && !(entrender->flags & RENDER_VIEWMODEL)
222          && (!(entrender->flags & RENDER_EXTERIORMODEL) || (!cl.intermission && cls.protocol != PROTOCOL_NEHAHRAMOVIE && !cl_noplayershadow.integer)))
223                 entrender->flags |= RENDER_SHADOW;
224         if (entrender->flags & RENDER_VIEWMODEL)
225                 entrender->flags |= RENDER_NOSELFSHADOW;
226
227         // make the other useful stuff
228         CL_UpdateRenderEntity(entrender);
229
230         return true;
231 }
232
233 qboolean CL_VM_InputEvent (qboolean down, int key, int ascii)
234 {
235         qboolean r;
236
237         if(!cl.csqc_loaded)
238                 return false;
239
240         CSQC_BEGIN
241                 if (!prog->funcoffsets.CSQC_InputEvent)
242                         r = false;
243                 else
244                 {
245                         prog->globals.client->time = cl.time;
246                         PRVM_G_FLOAT(OFS_PARM0) = !down; // 0 is down, 1 is up
247                         PRVM_G_FLOAT(OFS_PARM1) = key;
248                         PRVM_G_FLOAT(OFS_PARM2) = ascii;
249                         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_InputEvent, "QC function CSQC_InputEvent is missing");
250                         r = CSQC_RETURNVAL;
251                 }
252         CSQC_END
253         return r;
254 }
255
256 qboolean CL_VM_UpdateView (void)
257 {
258         vec3_t emptyvector;
259         emptyvector[0] = 0;
260         emptyvector[1] = 0;
261         emptyvector[2] = 0;
262 //      vec3_t oldangles;
263         if(!cl.csqc_loaded)
264                 return false;
265         CSQC_BEGIN
266                 //VectorCopy(cl.viewangles, oldangles);
267                 prog->globals.client->time = cl.time;
268                 CSQC_SetGlobals();
269                 // clear renderable entity and light lists to prevent crashes if the
270                 // CSQC_UpdateView function does not call R_ClearScene as it should
271                 r_refdef.scene.numentities = 0;
272                 r_refdef.scene.numlights = 0;
273                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_UpdateView, "QC function CSQC_UpdateView is missing");
274                 //VectorCopy(oldangles, cl.viewangles);
275                 // Dresk : Reset Dmg Globals Here
276                 CL_VM_UpdateDmgGlobals(0, 0, emptyvector);
277         CSQC_END
278         return true;
279 }
280
281 extern sizebuf_t vm_tempstringsbuf;
282 qboolean CL_VM_ConsoleCommand (const char *cmd)
283 {
284         int restorevm_tempstringsbuf_cursize;
285         qboolean r = false;
286         if(!cl.csqc_loaded)
287                 return false;
288         CSQC_BEGIN
289         if (prog->funcoffsets.CSQC_ConsoleCommand)
290         {
291                 prog->globals.client->time = cl.time;
292                 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
293                 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(cmd);
294                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_ConsoleCommand, "QC function CSQC_ConsoleCommand is missing");
295                 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
296                 r = CSQC_RETURNVAL;
297         }
298         CSQC_END
299         return r;
300 }
301
302 qboolean CL_VM_Parse_TempEntity (void)
303 {
304         int                     t;
305         qboolean        r = false;
306         if(!cl.csqc_loaded)
307                 return false;
308         CSQC_BEGIN
309         if(prog->funcoffsets.CSQC_Parse_TempEntity)
310         {
311                 t = msg_readcount;
312                 prog->globals.client->time = cl.time;
313                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_TempEntity, "QC function CSQC_Parse_TempEntity is missing");
314                 r = CSQC_RETURNVAL;
315                 if(!r)
316                 {
317                         msg_readcount = t;
318                         msg_badread = false;
319                 }
320         }
321         CSQC_END
322         return r;
323 }
324
325 void CL_VM_Parse_StuffCmd (const char *msg)
326 {
327         int restorevm_tempstringsbuf_cursize;
328         if(msg[0] == 'c')
329         if(msg[1] == 's')
330         if(msg[2] == 'q')
331         if(msg[3] == 'c')
332         {
333                 // if this is setting a csqc variable, deprotect csqc_progcrc
334                 // temporarily so that it can be set by the cvar command,
335                 // and then reprotect it afterwards
336                 int crcflags = csqc_progcrc.flags;
337                 int sizeflags = csqc_progcrc.flags;
338                 csqc_progcrc.flags &= ~CVAR_READONLY;
339                 csqc_progsize.flags &= ~CVAR_READONLY;
340                 Cmd_ExecuteString (msg, src_command);
341                 csqc_progcrc.flags = crcflags;
342                 csqc_progsize.flags = sizeflags;
343                 return;
344         }
345         if(!cl.csqc_loaded)
346         {
347                 Cbuf_AddText(msg);
348                 return;
349         }
350         CSQC_BEGIN
351         if(prog->funcoffsets.CSQC_Parse_StuffCmd)
352         {
353                 prog->globals.client->time = cl.time;
354                 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
355                 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(msg);
356                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_StuffCmd, "QC function CSQC_Parse_StuffCmd is missing");
357                 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
358         }
359         else
360                 Cbuf_AddText(msg);
361         CSQC_END
362 }
363
364 static void CL_VM_Parse_Print (const char *msg)
365 {
366         int restorevm_tempstringsbuf_cursize;
367         prog->globals.client->time = cl.time;
368         restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
369         PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(msg);
370         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_Print, "QC function CSQC_Parse_Print is missing");
371         vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
372 }
373
374 void CSQC_AddPrintText (const char *msg)
375 {
376         size_t i;
377         if(!cl.csqc_loaded)
378         {
379                 Con_Print(msg);
380                 return;
381         }
382         CSQC_BEGIN
383         if(prog->funcoffsets.CSQC_Parse_Print)
384         {
385                 // FIXME: is this bugged?
386                 i = strlen(msg)-1;
387                 if(msg[i] != '\n' && msg[i] != '\r')
388                 {
389                         if(strlen(cl.csqc_printtextbuf)+i >= MAX_INPUTLINE)
390                         {
391                                 CL_VM_Parse_Print(cl.csqc_printtextbuf);
392                                 cl.csqc_printtextbuf[0] = 0;
393                         }
394                         else
395                                 strlcat(cl.csqc_printtextbuf, msg, MAX_INPUTLINE);
396                         return;
397                 }
398                 strlcat(cl.csqc_printtextbuf, msg, MAX_INPUTLINE);
399                 CL_VM_Parse_Print(cl.csqc_printtextbuf);
400                 cl.csqc_printtextbuf[0] = 0;
401         }
402         else
403                 Con_Print(msg);
404         CSQC_END
405 }
406
407 void CL_VM_Parse_CenterPrint (const char *msg)
408 {
409         int restorevm_tempstringsbuf_cursize;
410         if(!cl.csqc_loaded)
411         {
412                 SCR_CenterPrint((char*)msg);
413                 return;
414         }
415         CSQC_BEGIN
416         if(prog->funcoffsets.CSQC_Parse_CenterPrint)
417         {
418                 prog->globals.client->time = cl.time;
419                 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
420                 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(msg);
421                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_CenterPrint, "QC function CSQC_Parse_CenterPrint is missing");
422                 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
423         }
424         else
425                 SCR_CenterPrint((char*)msg);
426         CSQC_END
427 }
428
429 void CL_VM_UpdateIntermissionState (int intermission)
430 {
431         prvm_eval_t *val;
432         if(cl.csqc_loaded)
433         {
434                 CSQC_BEGIN
435                 val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.intermission);
436                 if(val)
437                         val->_float = intermission;
438                 CSQC_END
439         }
440 }
441 void CL_VM_UpdateShowingScoresState (int showingscores)
442 {
443         prvm_eval_t *val;
444         if(cl.csqc_loaded)
445         {
446                 CSQC_BEGIN
447                 val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.sb_showscores);
448                 if(val)
449                         val->_float = showingscores;
450                 CSQC_END
451         }
452 }
453 qboolean CL_VM_Event_Sound(int sound_num, int volume, int channel, float attenuation, int ent, vec3_t pos)
454 {
455         qboolean r = false;
456         if(cl.csqc_loaded)
457         {
458                 CSQC_BEGIN
459                 if(prog->funcoffsets.CSQC_Event_Sound)
460                 {
461                         prog->globals.client->time = cl.time;
462                         PRVM_G_FLOAT(OFS_PARM0) = ent;
463                         PRVM_G_FLOAT(OFS_PARM1) = channel;
464                         PRVM_G_INT(OFS_PARM2) = PRVM_SetTempString(cl.sound_name[sound_num] );
465                         PRVM_G_FLOAT(OFS_PARM3) = volume;
466                         PRVM_G_FLOAT(OFS_PARM4) = attenuation;
467                         VectorCopy(pos, PRVM_G_VECTOR(OFS_PARM5) );
468                         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Event_Sound, "QC function CSQC_Event_Sound is missing");
469                         r = CSQC_RETURNVAL;
470                 }
471                 CSQC_END
472         }
473
474         return r;
475 }
476 void CL_VM_UpdateCoopDeathmatchGlobals (int gametype)
477 {
478         // Avoid global names for clean(er) coding
479         int localcoop;
480         int localdeathmatch;
481
482         prvm_eval_t *val;
483         if(cl.csqc_loaded)
484         {
485                 if(gametype == GAME_COOP)
486                 {
487                         localcoop = 1;
488                         localdeathmatch = 0;
489                 }
490                 else
491                 if(gametype == GAME_DEATHMATCH)
492                 {
493                         localcoop = 0;
494                         localdeathmatch = 1;
495                 }
496                 else
497                 {
498                         // How did the ServerInfo send an unknown gametype?
499                         // Better just assign the globals as 0...
500                         localcoop = 0;
501                         localdeathmatch = 0;
502                 }
503                 CSQC_BEGIN
504                 val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.coop);
505                 if(val)
506                         val->_float = localcoop;
507                 val = PRVM_GLOBALFIELDVALUE(prog->globaloffsets.deathmatch);
508                 if(val)
509                         val->_float = localdeathmatch;
510                 CSQC_END
511         }
512 }
513 float CL_VM_Event (float event)         //[515]: needed ? I'd say "YES", but don't know for what :D
514 {
515         float r = 0;
516         if(!cl.csqc_loaded)
517                 return 0;
518         CSQC_BEGIN
519         if(prog->funcoffsets.CSQC_Event)
520         {
521                 prog->globals.client->time = cl.time;
522                 PRVM_G_FLOAT(OFS_PARM0) = event;
523                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Event, "QC function CSQC_Event is missing");
524                 r = CSQC_RETURNVAL;
525         }
526         CSQC_END
527         return r;
528 }
529
530 void CSQC_ReadEntities (void)
531 {
532         unsigned short entnum, oldself, realentnum;
533         if(!cl.csqc_loaded)
534         {
535                 Host_Error ("CSQC_ReadEntities: CSQC is not loaded");
536                 return;
537         }
538
539         CSQC_BEGIN
540                 prog->globals.client->time = cl.time;
541                 oldself = prog->globals.client->self;
542                 while(1)
543                 {
544                         entnum = MSG_ReadShort();
545                         if(!entnum || msg_badread)
546                                 return;
547                         realentnum = entnum & 0x7FFF;
548                         prog->globals.client->self = cl.csqc_server2csqcentitynumber[realentnum];
549                         if(entnum & 0x8000)
550                         {
551                                 if(prog->globals.client->self)
552                                 {
553                                         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Ent_Remove, "QC function CSQC_Ent_Remove is missing");
554                                         cl.csqc_server2csqcentitynumber[realentnum] = 0;
555                                 }
556                                 else
557                                         Con_Printf("Bad csqc_server2csqcentitynumber map\n");   //[515]: never happens ?
558                         }
559                         else
560                         {
561                                 if(!prog->globals.client->self)
562                                 {
563                                         if(!prog->funcoffsets.CSQC_Ent_Spawn)
564                                         {
565                                                 prvm_edict_t    *ed;
566                                                 ed = PRVM_ED_Alloc();
567                                                 ed->fields.client->entnum = realentnum;
568                                                 prog->globals.client->self = cl.csqc_server2csqcentitynumber[realentnum] = PRVM_EDICT_TO_PROG(ed);
569                                                 PRVM_G_FLOAT(OFS_PARM0) = 1;
570                                                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Ent_Update, "QC function CSQC_Ent_Update is missing");
571                                         }
572                                         else
573                                         {
574                                                 // entity( float entnum ) CSQC_Ent_Spawn;
575                                                 // the qc function should set entnum, too (this way it also can return world [2/1/2008 Andreas]
576                                                 PRVM_G_FLOAT(OFS_PARM0) = (float) realentnum;
577                                                 // make sure no one gets wrong ideas
578                                                 prog->globals.client->self = 0;
579                                                 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Ent_Spawn, "QC function CSQC_Ent_Spawn is missing");
580                                                 cl.csqc_server2csqcentitynumber[realentnum] = PRVM_EDICT( PRVM_G_INT( OFS_RETURN ) );
581                                         }
582                                 }
583                                 else {
584                                         PRVM_G_FLOAT(OFS_PARM0) = 0;
585                                         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Ent_Update, "QC function CSQC_Ent_Update is missing");
586                                 }
587                         }
588                 }
589                 prog->globals.client->self = oldself;
590         CSQC_END
591 }
592
593 void CL_VM_CB_BeginIncreaseEdicts(void)
594 {
595         int i;
596         prvm_edict_t *ent;
597
598         // links don't survive the transition, so unlink everything
599         for (i = 0, ent = prog->edicts;i < prog->max_edicts;i++, ent++)
600         {
601                 if (!ent->priv.server->free)
602                         World_UnlinkEdict(prog->edicts + i);
603                 memset(&ent->priv.server->areagrid, 0, sizeof(ent->priv.server->areagrid));
604         }
605         World_Clear(&cl.world);
606 }
607
608 void CL_VM_CB_EndIncreaseEdicts(void)
609 {
610         int i;
611         prvm_edict_t *ent;
612
613         // link every entity except world
614         for (i = 1, ent = prog->edicts;i < prog->max_edicts;i++, ent++)
615                 if (!ent->priv.server->free)
616                         CL_LinkEdict(ent);
617 }
618
619 void CL_VM_CB_InitEdict(prvm_edict_t *e)
620 {
621         e->priv.server->move = false; // don't move on first frame
622 }
623
624 void CL_VM_CB_FreeEdict(prvm_edict_t *ed)
625 {
626         World_UnlinkEdict(ed);
627         memset(ed->fields.client, 0, sizeof(*ed->fields.client));
628 }
629
630 void CL_VM_CB_CountEdicts(void)
631 {
632         int             i;
633         prvm_edict_t    *ent;
634         int             active = 0, models = 0, solid = 0;
635
636         for (i=0 ; i<prog->num_edicts ; i++)
637         {
638                 ent = PRVM_EDICT_NUM(i);
639                 if (ent->priv.server->free)
640                         continue;
641                 active++;
642                 if (ent->fields.client->solid)
643                         solid++;
644                 if (ent->fields.client->model)
645                         models++;
646         }
647
648         Con_Printf("num_edicts:%3i\n", prog->num_edicts);
649         Con_Printf("active    :%3i\n", active);
650         Con_Printf("view      :%3i\n", models);
651         Con_Printf("touch     :%3i\n", solid);
652 }
653
654 qboolean CL_VM_CB_LoadEdict(prvm_edict_t *ent)
655 {
656         return true;
657 }
658
659 void Cmd_ClearCsqcFuncs (void);
660
661 void CL_VM_Init (void)
662 {
663         const char* csprogsfn;
664         unsigned char *csprogsdata;
665         fs_offset_t csprogsdatasize;
666         int csprogsdatacrc, requiredcrc;
667         int requiredsize;
668         prvm_eval_t *val;
669
670         // reset csqc_progcrc after reading it, so that changing servers doesn't
671         // expect csqc on the next server
672         requiredcrc = csqc_progcrc.integer;
673         requiredsize = csqc_progsize.integer;
674         Cvar_SetValueQuick(&csqc_progcrc, -1);
675         Cvar_SetValueQuick(&csqc_progsize, -1);
676
677         // if the server is not requesting a csprogs, then we're done here
678         if (requiredcrc < 0)
679                 return;
680
681         // see if the requested csprogs.dat file matches the requested crc
682         csprogsdatacrc = -1;
683         csprogsfn = va("dlcache/%s.%i.%i", csqc_progname.string, requiredsize, requiredcrc);
684         csprogsdata = FS_LoadFile(csprogsfn, tempmempool, true, &csprogsdatasize);
685         if (!csprogsdata)
686         {
687                 csprogsfn = csqc_progname.string;
688                 csprogsdata = FS_LoadFile(csprogsfn, tempmempool, true, &csprogsdatasize);
689         }
690         if (csprogsdata)
691         {
692                 csprogsdatacrc = CRC_Block(csprogsdata, csprogsdatasize);
693                 Mem_Free(csprogsdata);
694                 if (csprogsdatacrc != requiredcrc || csprogsdatasize != requiredsize)
695                 {
696                         if (cls.demoplayback)
697                         {
698                                 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);
699                                 return;
700                         }
701                         else
702                         {
703                                 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);
704                                 CL_Disconnect();
705                                 return;
706                         }
707                 }
708         }
709         else
710         {
711                 if (requiredcrc >= 0)
712                 {
713                         if (cls.demoplayback)
714                                 Con_Printf("CL_VM_Init: demo requires CSQC, but \"%s\" wasn't found\n", csqc_progname.string);
715                         else
716                                 Con_Printf("CL_VM_Init: server requires CSQC, but \"%s\" wasn't found\n", csqc_progname.string);
717                         CL_Disconnect();
718                 }
719                 return;
720         }
721
722         PRVM_Begin;
723         PRVM_InitProg(PRVM_CLIENTPROG);
724
725         // allocate the mempools
726         prog->progs_mempool = Mem_AllocPool(csqc_progname.string, 0, NULL);
727         prog->headercrc = CL_PROGHEADER_CRC;
728         prog->edictprivate_size = 0; // no private struct used
729         prog->name = CL_NAME;
730         prog->num_edicts = 1;
731         prog->max_edicts = 512;
732         prog->limit_edicts = CL_MAX_EDICTS;
733         prog->reserved_edicts = 0;
734         prog->edictprivate_size = sizeof(edict_engineprivate_t);
735         // TODO: add a shared extension string #define and add real support for csqc extension strings [12/5/2007 Black]
736         prog->extensionstring = vm_sv_extensions;
737         prog->builtins = vm_cl_builtins;
738         prog->numbuiltins = vm_cl_numbuiltins;
739         prog->begin_increase_edicts = CL_VM_CB_BeginIncreaseEdicts;
740         prog->end_increase_edicts = CL_VM_CB_EndIncreaseEdicts;
741         prog->init_edict = CL_VM_CB_InitEdict;
742         prog->free_edict = CL_VM_CB_FreeEdict;
743         prog->count_edicts = CL_VM_CB_CountEdicts;
744         prog->load_edict = CL_VM_CB_LoadEdict;
745         prog->init_cmd = VM_CL_Cmd_Init;
746         prog->reset_cmd = VM_CL_Cmd_Reset;
747         prog->error_cmd = CL_VM_Error;
748
749         PRVM_LoadProgs(csprogsfn, cl_numrequiredfunc, cl_required_func, 0, NULL, 0, NULL);
750
751         if (!prog->loaded)
752         {
753                 CL_VM_Error("CSQC %s ^2failed to load\n", csprogsfn);
754                 if(!sv.active)
755                         CL_Disconnect();
756                 return;
757         }
758
759         Con_Printf("CSQC %s ^5loaded (crc=%i, size=%i)\n", csprogsfn, csprogsdatacrc, (int)csprogsdatasize);
760
761         // check if OP_STATE animation is possible in this dat file
762         if (prog->fieldoffsets.nextthink >= 0 && prog->fieldoffsets.frame >= 0 && prog->fieldoffsets.think >= 0 && prog->globaloffsets.self >= 0)
763                 prog->flag |= PRVM_OP_STATE;
764
765         // set time
766         prog->globals.client->time = cl.time;
767
768         prog->globals.client->mapname = PRVM_SetEngineString(cl.worldmodel->name);
769         prog->globals.client->player_localentnum = cl.playerentity;
770
771         // set map description (use world entity 0)
772         val = PRVM_EDICTFIELDVALUE(prog->edicts, prog->fieldoffsets.message);
773         if(val)
774                 val->string = PRVM_SetEngineString(cl.levelname);
775
776         // call the prog init
777         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Init, "QC function CSQC_Init is missing");
778
779         PRVM_End;
780         cl.csqc_loaded = true;
781
782         cl.csqc_vidvars.drawcrosshair = false;
783         cl.csqc_vidvars.drawenginesbar = false;
784
785         // Update Coop and Deathmatch Globals (at this point the client knows them from ServerInfo)
786         CL_VM_UpdateCoopDeathmatchGlobals(cl.gametype);
787 }
788
789 void CL_VM_ShutDown (void)
790 {
791         Cmd_ClearCsqcFuncs();
792         Cvar_SetValueQuick(&csqc_progcrc, -1);
793         Cvar_SetValueQuick(&csqc_progsize, -1);
794         if(!cl.csqc_loaded)
795                 return;
796         CSQC_BEGIN
797                 prog->globals.client->time = cl.time;
798                 if (prog->funcoffsets.CSQC_Shutdown)
799                         PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Shutdown, "QC function CSQC_Shutdown is missing");
800                 PRVM_ResetProg();
801         CSQC_END
802         Con_Print("CSQC ^1unloaded\n");
803         cl.csqc_loaded = false;
804 }