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