3 #include "clprogdefs.h"
6 //============================================================================
7 // Client prog handling
8 //[515]: omg !!! optimize it ! a lot of hacks here and there also :P
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;
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"
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...
33 #define CSQC_PRINTBUFFERLEN 8192 //[515]: enough ?
35 static char *cl_required_func[] =
44 static int cl_numrequiredfunc = sizeof(cl_required_func) / sizeof(char*);
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 ?
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;
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;
66 qboolean csqc_loaded = false;
68 vec3_t csqc_origin, csqc_angles;
69 static double csqc_frametime = 0;
72 static mempool_t *csqc_mempool;
74 static void CL_VM_FindEdictFieldOffsets (void)
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");
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);
94 csqc_printtextbuf = (char *)Mem_Alloc(csqc_mempool, CSQC_PRINTBUFFERLEN);
95 csqc_printtextbuf[0] = 0;
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 =)
102 char errorstring[4096];
105 va_start (argptr, format);
106 dpvsnprintf (errorstring, sizeof(errorstring), format, argptr);
108 // Con_Printf( "CL_VM_Error: %s\n", errorstring );
112 Mem_FreePool(&csqc_mempool);
114 Cvar_SetValueQuick(&csqc_progcrc, -1);
115 Cvar_SetValueQuick(&csqc_progsize, -1);
117 // Host_AbortCurrentFrame(); //[515]: hmmm... if server says it needs csqc then client MUST disconnect
118 Host_Error(va("CL_VM_Error: %s", errorstring));
121 //[515]: set globals before calling R_UpdateView, WEIRD CRAP
122 static void CSQC_SetGlobals (void)
124 //extern cvar_t sv_accelerate, sv_friction, sv_gravity, sv_stopspeed, sv_maxspeed;
127 *prog->time = cl.time;
128 prog->globals.client->frametime = cl.time - csqc_frametime;
129 csqc_frametime = cl.time;
130 prog->globals.client->servercommandframe = cl.servermovesequence;
131 prog->globals.client->clientcommandframe = cl.movemessages;
132 VectorCopy(cl.viewangles, prog->globals.client->input_angles);
133 VectorCopy(cl.viewangles, csqc_angles);
134 prog->globals.client->input_buttons = csqc_buttons;
135 VectorSet(prog->globals.client->input_movevalues, cl.cmd.forwardmove, cl.cmd.sidemove, cl.cmd.upmove);
136 //VectorCopy(cl.movement_origin, csqc_origin);
137 Matrix4x4_OriginFromMatrix(&cl.entities[cl.viewentity].render.matrix, csqc_origin);
138 VectorCopy(csqc_origin, prog->globals.client->pmove_org);
139 prog->globals.client->maxclients = cl.maxclients;
140 //VectorCopy(cl.movement_velocity, prog->globals.client->pmove_vel);
141 VectorCopy(cl.velocity, prog->globals.client->pmove_vel);
145 void CSQC_Predraw (prvm_edict_t *ed)
148 if(!ed->fields.client->predraw)
150 b = prog->globals.client->self;
151 prog->globals.client->self = PRVM_EDICT_TO_PROG(ed);
152 PRVM_ExecuteProgram(ed->fields.client->predraw, "CSQC_Predraw: NULL function\n");
153 prog->globals.client->self = b;
156 void CSQC_Think (prvm_edict_t *ed)
159 if(ed->fields.client->think)
160 if(ed->fields.client->nextthink && ed->fields.client->nextthink <= *prog->time)
162 ed->fields.client->nextthink = 0;
163 b = prog->globals.client->self;
164 prog->globals.client->self = PRVM_EDICT_TO_PROG(ed);
165 PRVM_ExecuteProgram(ed->fields.client->think, "CSQC_Think: NULL function\n");
166 prog->globals.client->self = b;
170 extern cvar_t cl_noplayershadow;
171 qboolean CSQC_AddRenderEdict(prvm_edict_t *ed)
176 matrix4x4_t tagmatrix, matrix2;
178 e = CL_NewTempEntity();
182 i = (int)ed->fields.client->modelindex;
183 if(i >= MAX_MODELS || i <= -MAX_MODELS) //[515]: make work as error ?
185 Con_Print("CSQC_AddRenderEdict: modelindex >= MAX_MODELS\n");
186 ed->fields.client->modelindex = i = 0;
189 // model setup and some modelflags
191 e->render.model = cl.model_precache[e->state_current.modelindex];
193 e->render.model = cl.csqc_model_precache[65536-e->state_current.modelindex];
195 if (!e->render.model)
198 e->render.colormap = (int)ed->fields.client->colormap;
199 e->render.frame = (int)ed->fields.client->frame;
200 e->render.skinnum = (int)ed->fields.client->skin;
201 e->render.effects |= e->render.model->flags2 & (EF_FULLBRIGHT | EF_ADDITIVE);
203 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_alpha)) && val->_float) e->render.alpha = val->_float;
204 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_scale)) && val->_float) e->render.scale = val->_float;
205 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_colormod)) && VectorLength2(val->vector)) VectorCopy(val->vector, e->render.colormod);
206 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_effects)) && val->_float) e->render.effects = (int)val->_float;
207 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_tag_entity)) && val->edict)
211 tagentity = val->edict;
212 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_tag_index)) && val->_float)
213 tagindex = (int)val->_float;
214 // FIXME: calculate tag matrix
215 Matrix4x4_CreateIdentity(&tagmatrix);
218 Matrix4x4_CreateIdentity(&tagmatrix);
220 if(i & RF_USEAXIS) //FIXME!!!
223 VectorNegate(prog->globals.client->v_right, left);
224 Matrix4x4_FromVectors(&matrix2, prog->globals.client->v_forward, left, prog->globals.client->v_up, ed->fields.client->origin);
229 VectorCopy(ed->fields.client->angles, angles);
230 // if model is alias, reverse pitch direction
231 if (e->render.model->type == mod_alias)
232 angles[0] = -angles[0];
234 // set up the render matrix
235 // FIXME: e->render.scale should go away
236 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);
239 // FIXME: csqc has frame1/frame2/frame1time/frame2time/lerpfrac but this implementation's cl_entvars_t lacks those fields
240 e->render.frame1 = e->render.frame = ed->fields.client->frame;
241 e->render.frame1time = e->render.frame2time = 0;
242 e->render.framelerp = 0;
244 // concat the matrices to make the entity relative to its tag
245 Matrix4x4_Concat(&e->render.matrix, &tagmatrix, &matrix2);
246 // make the other useful stuff
247 CL_UpdateRenderEntity(&e->render);
250 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_renderflags)) && val->_float)
252 i = (int)val->_float;
253 if(i & RF_VIEWMODEL) e->render.flags |= RENDER_VIEWMODEL;
254 if(i & RF_EXTERNALMODEL)e->render.flags |= RENDER_EXTERIORMODEL;
255 if(i & RF_DEPTHHACK) e->render.effects |= EF_NODEPTHTEST;
256 if(i & RF_ADDITIVE) e->render.effects |= EF_ADDITIVE;
259 // transparent stuff can't be lit during the opaque stage
260 if (e->render.effects & (EF_ADDITIVE | EF_NODEPTHTEST) || e->render.alpha < 1)
261 e->render.flags |= RENDER_TRANSPARENT;
262 // double sided rendering mode causes backfaces to be visible
263 // (mostly useful on transparent stuff)
264 if (e->render.effects & EF_DOUBLESIDED)
265 e->render.flags |= RENDER_NOCULLFACE;
266 // either fullbright or lit
267 if (!(e->render.effects & EF_FULLBRIGHT) && !r_fullbright.integer)
268 e->render.flags |= RENDER_LIGHT;
269 // hide player shadow during intermission or nehahra movie
270 if (!(e->render.effects & EF_NOSHADOW)
271 && !(e->render.flags & (RENDER_VIEWMODEL | RENDER_TRANSPARENT))
272 && (!(e->render.flags & RENDER_EXTERIORMODEL) || (!cl.intermission && cls.protocol != PROTOCOL_NEHAHRAMOVIE && !cl_noplayershadow.integer)))
273 e->render.flags |= RENDER_SHADOW;
278 void CSQC_ClearCSQCEntities (void)
280 memset(cl.csqcentities_active, 0, sizeof(cl.csqcentities_active));
281 cl.num_csqcentities = 0;
284 void CL_ExpandCSQCEntities (int num);
286 qboolean CL_VM_InputEvent (qboolean pressed, int key)
292 *prog->time = cl.time;
293 PRVM_G_FLOAT(OFS_PARM0) = pressed;
294 PRVM_G_FLOAT(OFS_PARM1) = key;
295 PRVM_ExecuteProgram (prog->globals.client->CSQC_InputEvent, CL_F_INPUTEVENT);
301 qboolean CL_VM_UpdateView (void)
307 //VectorCopy(cl.viewangles, oldangles);
308 *prog->time = cl.time;
310 cl.num_csqcentities = 0;
311 PRVM_ExecuteProgram (prog->globals.client->CSQC_UpdateView, CL_F_UPDATEVIEW);
312 //VectorCopy(oldangles, cl.viewangles);
317 qboolean CL_VM_ConsoleCommand (const char *cmd)
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);
331 qboolean CL_VM_Parse_TempEntity (void)
335 if(!csqc_loaded || !CSQC_Parse_TempEntity)
339 *prog->time = cl.time;
340 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_TempEntity - prog->functions), CL_F_PARSE_TEMPENTITY);
351 void CL_VM_Parse_StuffCmd (const char *msg)
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 crcflags = csqc_progcrc.flags;
362 int sizeflags = csqc_progcrc.flags;
363 csqc_progcrc.flags &= ~CVAR_READONLY;
364 csqc_progsize.flags &= ~CVAR_READONLY;
365 Cmd_ExecuteString (msg, src_command);
366 csqc_progcrc.flags = crcflags;
367 csqc_progsize.flags = sizeflags;
370 if(!csqc_loaded || !CSQC_Parse_StuffCmd)
376 *prog->time = cl.time;
377 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(msg);
378 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_StuffCmd - prog->functions), CL_F_PARSE_STUFFCMD);
382 static void CL_VM_Parse_Print (const char *msg)
385 *prog->time = cl.time;
386 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(msg);
387 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_Print - prog->functions), CL_F_PARSE_PRINT);
391 void CSQC_AddPrintText (const char *msg)
394 if(!csqc_loaded || !CSQC_Parse_Print)
399 // FIXME: is this bugged?
401 if(msg[i] != '\n' && msg[i] != '\r')
403 if(strlen(csqc_printtextbuf)+i >= CSQC_PRINTBUFFERLEN)
405 CL_VM_Parse_Print(csqc_printtextbuf);
406 csqc_printtextbuf[0] = 0;
409 strlcat(csqc_printtextbuf, msg, CSQC_PRINTBUFFERLEN);
412 strlcat(csqc_printtextbuf, msg, CSQC_PRINTBUFFERLEN);
413 CL_VM_Parse_Print(csqc_printtextbuf);
414 csqc_printtextbuf[0] = 0;
417 void CL_VM_Parse_CenterPrint (const char *msg)
419 if(!csqc_loaded || !CSQC_Parse_CenterPrint)
421 SCR_CenterPrint((char*)msg);
425 *prog->time = cl.time;
426 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(msg);
427 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_CenterPrint - prog->functions), CL_F_PARSE_CENTERPRINT);
431 float CL_VM_Event (float event) //[515]: needed ? I'd say "YES", but don't know for what :D
434 if(!csqc_loaded || !CSQC_Event)
437 *prog->time = cl.time;
438 PRVM_G_FLOAT(OFS_PARM0) = event;
439 PRVM_ExecuteProgram ((func_t)(CSQC_Event - prog->functions), CL_F_EVENT);
445 void CSQC_ReadEntities (void)
447 unsigned short entnum, oldself, realentnum;
449 *prog->time = cl.time;
450 oldself = prog->globals.client->self;
453 entnum = MSG_ReadShort();
456 realentnum = entnum & 0x7FFF;
457 prog->globals.client->self = csqc_sv2csqcents[realentnum];
460 if(prog->globals.client->self)
462 PRVM_ExecuteProgram((func_t)(CSQC_Ent_Remove - prog->functions), CL_F_ENT_REMOVE);
463 csqc_sv2csqcents[realentnum] = 0;
466 Con_Printf("Smth bad happens in csqc...\n"); //[515]: never happens ?
470 if(!prog->globals.client->self)
473 ed = PRVM_ED_Alloc();
474 ed->fields.client->entnum = realentnum;
475 prog->globals.client->self = csqc_sv2csqcents[realentnum] = PRVM_EDICT_TO_PROG(ed);
476 PRVM_G_FLOAT(OFS_PARM0) = 1;
479 PRVM_G_FLOAT(OFS_PARM0) = 0;
480 PRVM_ExecuteProgram((func_t)(CSQC_Ent_Update - prog->functions), CL_F_ENT_UPDATE);
483 prog->globals.client->self = oldself;
487 void Cmd_ClearCsqcFuncs (void);
489 void CL_VM_Init (void)
491 unsigned char *csprogsdata;
492 fs_offset_t csprogsdatasize;
493 int csprogsdatacrc, requiredcrc;
497 // reset csqc_progcrc after reading it, so that changing servers doesn't
498 // expect csqc on the next server
499 requiredcrc = csqc_progcrc.integer;
500 requiredsize = csqc_progsize.integer;
501 Cvar_SetValueQuick(&csqc_progcrc, -1);
502 Cvar_SetValueQuick(&csqc_progsize, -1);
505 memset(cl.csqc_model_precache, 0, sizeof(cl.csqc_model_precache));
506 memset(&cl.csqc_vidvars, true, sizeof(csqc_vidvars_t));
508 // if the server is not requesting a csprogs, then we're done here
512 // see if the requested csprogs.dat file matches the requested crc
514 csprogsdata = FS_LoadFile(va("dlcache/%s.%i.%i", csqc_progname.string, requiredsize, requiredcrc), tempmempool, true, &csprogsdatasize);
516 csprogsdata = FS_LoadFile(csqc_progname.string, tempmempool, true, &csprogsdatasize);
519 csprogsdatacrc = CRC_Block(csprogsdata, csprogsdatasize);
520 Mem_Free(csprogsdata);
521 if (csprogsdatacrc != requiredcrc || csprogsdatasize != requiredsize)
523 if (cls.demoplayback)
525 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);
530 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);
538 if (requiredcrc >= 0)
540 if (cls.demoplayback)
541 Con_Printf("CL_VM_Init: demo requires CSQC, but \"%s\" wasn't found\n", csqc_progname.string);
543 Con_Printf("CL_VM_Init: server requires CSQC, but \"%s\" wasn't found\n", csqc_progname.string);
550 PRVM_InitProg(PRVM_CLIENTPROG);
552 csqc_mempool = Mem_AllocPool("CSQC", 0, NULL);
554 // allocate the mempools
555 prog->progs_mempool = Mem_AllocPool(csqc_progname.string, 0, NULL);
556 prog->headercrc = CL_PROGHEADER_CRC;
557 prog->edictprivate_size = 0; // no private struct used
558 prog->name = CL_NAME;
559 prog->num_edicts = 1;
560 prog->limit_edicts = CL_MAX_EDICTS;
561 prog->extensionstring = vm_cl_extensions;
562 prog->builtins = vm_cl_builtins;
563 prog->numbuiltins = vm_cl_numbuiltins;
564 prog->init_cmd = VM_CL_Cmd_Init;
565 prog->reset_cmd = VM_CL_Cmd_Reset;
566 prog->error_cmd = CL_VM_Error;
568 PRVM_LoadProgs(csqc_progname.string, cl_numrequiredfunc, cl_required_func, 0, NULL);
571 Con_Printf("CSQC ^5loaded (crc=%i, size=%i)\n", csprogsdatacrc, (int)csprogsdatasize);
574 CL_VM_Error("CSQC ^2failed to load\n");
580 //[515]: optional fields & funcs
581 CL_VM_FindEdictFieldOffsets();
584 *prog->time = cl.time;
587 prog->globals.client->mapname = PRVM_SetEngineString(cl.worldmodel->name);
588 prog->globals.client->player_localentnum = cl.playerentity;
590 // call the prog init
591 PRVM_ExecuteProgram((func_t) (PRVM_ED_FindFunction(CL_F_INIT) - prog->functions), CL_F_INIT);
596 csqc_sv2csqcents = (unsigned short *)Mem_Alloc(csqc_mempool, MAX_EDICTS*sizeof(unsigned short));
597 memset(csqc_sv2csqcents, 0, MAX_EDICTS*sizeof(unsigned short));
599 cl.csqc_vidvars.drawcrosshair = false;
600 cl.csqc_vidvars.drawenginesbar = false;
603 ent = &cl.csqcentities[0];
604 // entire entity array was cleared, so just fill in a few fields
605 ent->state_current.active = true;
606 ent->render.model = cl.worldmodel = cl.model_precache[1];
607 ent->render.alpha = 1;
608 ent->render.flags = RENDER_SHADOW | RENDER_LIGHT;
609 Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, 0, 0, 0, 0, 0, 0, 1);
610 CL_UpdateRenderEntity(&ent->render);
613 void CL_VM_ShutDown (void)
615 Cmd_ClearCsqcFuncs();
616 Cvar_SetValueQuick(&csqc_progcrc, -1);
617 Cvar_SetValueQuick(&csqc_progsize, -1);
621 *prog->time = cl.time;
622 PRVM_ExecuteProgram((func_t) (PRVM_ED_FindFunction(CL_F_SHUTDOWN) - prog->functions), CL_F_SHUTDOWN);
625 Con_Print("CSQC ^1unloaded\n");
627 Mem_FreePool(&csqc_mempool);