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