3 #include "clprogdefs.h"
5 #include "cl_collision.h"
7 //============================================================================
8 // Client prog handling
9 //[515]: omg !!! optimize it ! a lot of hacks here and there also :P
11 #define CSQC_RETURNVAL prog->globals.generic[OFS_RETURN]
12 #define CSQC_BEGIN csqc_tmpprog=prog;prog=0;PRVM_SetProg(PRVM_CLIENTPROG);
13 #define CSQC_END prog=csqc_tmpprog;
14 static prvm_prog_t *csqc_tmpprog;
16 //[515]: these are required funcs
17 static char *cl_required_func[] =
22 "CSQC_ConsoleCommand",
26 static int cl_numrequiredfunc = sizeof(cl_required_func) / sizeof(char*);
28 void CL_VM_Error (const char *format, ...) DP_FUNC_PRINTF(1);
29 void CL_VM_Error (const char *format, ...) //[515]: hope it will be never executed =)
31 char errorstring[4096];
34 va_start (argptr, format);
35 dpvsnprintf (errorstring, sizeof(errorstring), format, argptr);
37 // Con_Printf( "CL_VM_Error: %s\n", errorstring );
40 cl.csqc_loaded = false;
42 Cvar_SetValueQuick(&csqc_progcrc, -1);
43 Cvar_SetValueQuick(&csqc_progsize, -1);
45 // Host_AbortCurrentFrame(); //[515]: hmmm... if server says it needs csqc then client MUST disconnect
46 Host_Error(va("CL_VM_Error: %s", errorstring));
49 //[515]: set globals before calling R_UpdateView, WEIRD CRAP
50 static void CSQC_SetGlobals (void)
53 prog->globals.client->time = cl.time;
54 prog->globals.client->frametime = max(0, cl.time - cl.oldtime);
55 prog->globals.client->servercommandframe = cls.servermovesequence;
56 prog->globals.client->clientcommandframe = cl.movecmd[0].sequence;
57 VectorCopy(cl.viewangles, prog->globals.client->input_angles);
58 VectorCopy(cl.viewangles, cl.csqc_angles);
59 prog->globals.client->input_buttons = cl.movecmd[0].buttons;
60 VectorSet(prog->globals.client->input_movevalues, cl.movecmd[0].forwardmove, cl.movecmd[0].sidemove, cl.movecmd[0].upmove);
61 //VectorCopy(cl.movement_origin, cl.csqc_origin);
62 Matrix4x4_OriginFromMatrix(&cl.entities[cl.viewentity].render.matrix, cl.csqc_origin);
63 VectorCopy(cl.csqc_origin, prog->globals.client->pmove_org);
64 prog->globals.client->maxclients = cl.maxclients;
65 //VectorCopy(cl.movement_velocity, prog->globals.client->pmove_vel);
66 VectorCopy(cl.velocity, prog->globals.client->pmove_vel);
70 void CSQC_Predraw (prvm_edict_t *ed)
73 if(!ed->fields.client->predraw)
75 b = prog->globals.client->self;
76 prog->globals.client->self = PRVM_EDICT_TO_PROG(ed);
77 PRVM_ExecuteProgram(ed->fields.client->predraw, "CSQC_Predraw: NULL function\n");
78 prog->globals.client->self = b;
81 void CSQC_Think (prvm_edict_t *ed)
84 if(ed->fields.client->think)
85 if(ed->fields.client->nextthink && ed->fields.client->nextthink <= prog->globals.client->time)
87 ed->fields.client->nextthink = 0;
88 b = prog->globals.client->self;
89 prog->globals.client->self = PRVM_EDICT_TO_PROG(ed);
90 PRVM_ExecuteProgram(ed->fields.client->think, "CSQC_Think: NULL function\n");
91 prog->globals.client->self = b;
95 extern cvar_t cl_noplayershadow;
96 qboolean CSQC_AddRenderEdict(prvm_edict_t *ed)
103 matrix4x4_t tagmatrix, matrix2;
105 model = CL_GetModelFromEdict(ed);
109 e = CL_NewTempEntity();
113 e->render.model = model;
114 e->render.colormap = (int)ed->fields.client->colormap;
115 e->render.skinnum = (int)ed->fields.client->skin;
116 e->render.effects |= e->render.model->effects;
119 if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.renderflags)) && val->_float) renderflags = (int)val->_float;
120 if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.alpha)) && val->_float) e->render.alpha = val->_float;
121 if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.scale)) && val->_float) e->render.scale = scale = val->_float;
122 if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.colormod)) && VectorLength2(val->vector)) VectorCopy(val->vector, e->render.colormod);
123 if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.effects)) && val->_float) e->render.effects |= (int)val->_float;
124 if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.tag_entity)) && val->edict)
128 tagentity = val->edict;
129 if((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.tag_index)) && val->_float)
130 tagindex = (int)val->_float;
131 // FIXME: calculate tag matrix
132 Matrix4x4_CreateIdentity(&tagmatrix);
135 Matrix4x4_CreateIdentity(&tagmatrix);
137 if (renderflags & RF_USEAXIS)
140 VectorNegate(prog->globals.client->v_right, left);
141 Matrix4x4_FromVectors(&matrix2, prog->globals.client->v_forward, left, prog->globals.client->v_up, ed->fields.client->origin);
142 Matrix4x4_Scale(&matrix2, scale, 1);
147 VectorCopy(ed->fields.client->angles, angles);
148 // if model is alias, reverse pitch direction
149 if (e->render.model->type == mod_alias)
150 angles[0] = -angles[0];
152 // set up the render matrix
153 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);
156 // set up the animation data
157 // self.frame is the interpolation target (new frame)
158 // self.frame1time is the animation base time for the interpolation target
159 // self.frame2 is the interpolation start (previous frame)
160 // self.frame2time is the animation base time for the interpolation start
161 e->render.frame1 = e->render.frame2 = ed->fields.client->frame;
162 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame2))) e->render.frame2 = val->_float;
163 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame1time))) e->render.frame2time = val->_float;
164 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.frame2time))) e->render.frame1time = val->_float;
165 if ((val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.lerpfrac))) e->render.framelerp = val->_float;
167 // concat the matrices to make the entity relative to its tag
168 Matrix4x4_Concat(&e->render.matrix, &tagmatrix, &matrix2);
169 // make the other useful stuff
170 CL_UpdateRenderEntity(&e->render);
174 if(renderflags & RF_VIEWMODEL) e->render.flags |= RENDER_VIEWMODEL;
175 if(renderflags & RF_EXTERNALMODEL)e->render.flags |= RENDER_EXTERIORMODEL;
176 if(renderflags & RF_DEPTHHACK) e->render.effects |= EF_NODEPTHTEST;
177 if(renderflags & RF_ADDITIVE) e->render.effects |= EF_ADDITIVE;
180 if ((e->render.colormap > 0 && e->render.colormap <= cl.maxclients) || e->render.colormap >= 1024)
183 unsigned char *cbcolor;
185 if (e->render.colormap >= 1024)
186 palcol = (unsigned char)(e->render.colormap-1024);
188 palcol = cl.scores[e->render.colormap-1].colors;
190 cb = (palcol & 0xF) << 4;cb += (cb >= 128 && cb < 224) ? 4 : 12;
191 cbcolor = (unsigned char *) (&palette_complete[cb]);
192 e->render.colormap_pantscolor[0] = cbcolor[0] * (1.0f / 255.0f);
193 e->render.colormap_pantscolor[1] = cbcolor[1] * (1.0f / 255.0f);
194 e->render.colormap_pantscolor[2] = cbcolor[2] * (1.0f / 255.0f);
195 cb = (palcol & 0xF0);cb += (cb >= 128 && cb < 224) ? 4 : 12;
196 cbcolor = (unsigned char *) (&palette_complete[cb]);
197 e->render.colormap_shirtcolor[0] = cbcolor[0] * (1.0f / 255.0f);
198 e->render.colormap_shirtcolor[1] = cbcolor[1] * (1.0f / 255.0f);
199 e->render.colormap_shirtcolor[2] = cbcolor[2] * (1.0f / 255.0f);
202 // either fullbright or lit
203 if (!(e->render.effects & EF_FULLBRIGHT) && !r_fullbright.integer)
204 e->render.flags |= RENDER_LIGHT;
205 // hide player shadow during intermission or nehahra movie
206 if (!(e->render.effects & (EF_NOSHADOW | EF_ADDITIVE | EF_NODEPTHTEST))
207 && (e->render.alpha >= 1)
208 && !(e->render.flags & RENDER_VIEWMODEL)
209 && (!(e->render.flags & RENDER_EXTERIORMODEL) || (!cl.intermission && cls.protocol != PROTOCOL_NEHAHRAMOVIE && !cl_noplayershadow.integer)))
210 e->render.flags |= RENDER_SHADOW;
211 if (e->render.flags & RENDER_VIEWMODEL)
212 e->render.flags |= RENDER_NOSELFSHADOW;
217 qboolean CL_VM_InputEvent (qboolean pressed, int key)
223 prog->globals.client->time = cl.time;
224 PRVM_G_FLOAT(OFS_PARM0) = pressed;
225 PRVM_G_FLOAT(OFS_PARM1) = key;
226 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_InputEvent, "QC function CSQC_InputEvent is missing");
232 qboolean CL_VM_UpdateView (void)
238 //VectorCopy(cl.viewangles, oldangles);
239 prog->globals.client->time = cl.time;
241 // clear renderable entity and light lists to prevent crashes if the
242 // CSQC_UpdateView function does not call R_ClearScene as it should
243 r_refdef.numentities = 0;
244 r_refdef.numlights = 0;
245 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_UpdateView, "QC function CSQC_UpdateView is missing");
246 //VectorCopy(oldangles, cl.viewangles);
251 extern sizebuf_t vm_tempstringsbuf;
252 qboolean CL_VM_ConsoleCommand (const char *cmd)
254 int restorevm_tempstringsbuf_cursize;
259 prog->globals.client->time = cl.time;
260 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
261 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(cmd);
262 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_ConsoleCommand, "QC function CSQC_ConsoleCommand is missing");
263 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
269 qboolean CL_VM_Parse_TempEntity (void)
276 if(prog->funcoffsets.CSQC_Parse_TempEntity)
279 prog->globals.client->time = cl.time;
280 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_TempEntity, "QC function CSQC_Parse_TempEntity is missing");
292 void CL_VM_Parse_StuffCmd (const char *msg)
294 int restorevm_tempstringsbuf_cursize;
300 // if this is setting a csqc variable, deprotect csqc_progcrc
301 // temporarily so that it can be set by the cvar command,
302 // and then reprotect it afterwards
303 int crcflags = csqc_progcrc.flags;
304 int sizeflags = csqc_progcrc.flags;
305 csqc_progcrc.flags &= ~CVAR_READONLY;
306 csqc_progsize.flags &= ~CVAR_READONLY;
307 Cmd_ExecuteString (msg, src_command);
308 csqc_progcrc.flags = crcflags;
309 csqc_progsize.flags = sizeflags;
318 if(prog->funcoffsets.CSQC_Parse_StuffCmd)
320 prog->globals.client->time = cl.time;
321 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
322 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(msg);
323 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_StuffCmd, "QC function CSQC_Parse_StuffCmd is missing");
324 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
331 static void CL_VM_Parse_Print (const char *msg)
333 int restorevm_tempstringsbuf_cursize;
334 prog->globals.client->time = cl.time;
335 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
336 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(msg);
337 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_Print, "QC function CSQC_Parse_Print is missing");
338 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
341 void CSQC_AddPrintText (const char *msg)
350 if(prog->funcoffsets.CSQC_Parse_Print)
352 // FIXME: is this bugged?
354 if(msg[i] != '\n' && msg[i] != '\r')
356 if(strlen(cl.csqc_printtextbuf)+i >= MAX_INPUTLINE)
358 CL_VM_Parse_Print(cl.csqc_printtextbuf);
359 cl.csqc_printtextbuf[0] = 0;
362 strlcat(cl.csqc_printtextbuf, msg, MAX_INPUTLINE);
365 strlcat(cl.csqc_printtextbuf, msg, MAX_INPUTLINE);
366 CL_VM_Parse_Print(cl.csqc_printtextbuf);
367 cl.csqc_printtextbuf[0] = 0;
374 void CL_VM_Parse_CenterPrint (const char *msg)
376 int restorevm_tempstringsbuf_cursize;
379 SCR_CenterPrint((char*)msg);
383 if(prog->funcoffsets.CSQC_Parse_CenterPrint)
385 prog->globals.client->time = cl.time;
386 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
387 PRVM_G_INT(OFS_PARM0) = PRVM_SetTempString(msg);
388 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Parse_CenterPrint, "QC function CSQC_Parse_CenterPrint is missing");
389 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
392 SCR_CenterPrint((char*)msg);
396 float CL_VM_Event (float event) //[515]: needed ? I'd say "YES", but don't know for what :D
402 if(prog->funcoffsets.CSQC_Event)
404 prog->globals.client->time = cl.time;
405 PRVM_G_FLOAT(OFS_PARM0) = event;
406 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Event, "QC function CSQC_Event is missing");
413 void CSQC_ReadEntities (void)
415 unsigned short entnum, oldself, realentnum;
417 prog->globals.client->time = cl.time;
418 oldself = prog->globals.client->self;
421 entnum = MSG_ReadShort();
422 if(!entnum || msg_badread)
424 realentnum = entnum & 0x7FFF;
425 prog->globals.client->self = cl.csqc_server2csqcentitynumber[realentnum];
428 if(prog->globals.client->self)
430 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Ent_Remove, "QC function CSQC_Ent_Remove is missing");
431 cl.csqc_server2csqcentitynumber[realentnum] = 0;
434 Con_Printf("Smth bad happens in csqc...\n"); //[515]: never happens ?
438 if(!prog->globals.client->self)
441 ed = PRVM_ED_Alloc();
442 ed->fields.client->entnum = realentnum;
443 prog->globals.client->self = cl.csqc_server2csqcentitynumber[realentnum] = PRVM_EDICT_TO_PROG(ed);
444 PRVM_G_FLOAT(OFS_PARM0) = 1;
447 PRVM_G_FLOAT(OFS_PARM0) = 0;
448 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Ent_Update, "QC function CSQC_Ent_Update is missing");
451 prog->globals.client->self = oldself;
455 void CL_VM_CB_BeginIncreaseEdicts(void)
460 // links don't survive the transition, so unlink everything
461 for (i = 0, ent = prog->edicts;i < prog->max_edicts;i++, ent++)
463 if (!ent->priv.server->free)
464 World_UnlinkEdict(prog->edicts + i);
465 memset(&ent->priv.server->areagrid, 0, sizeof(ent->priv.server->areagrid));
467 World_Clear(&cl.world);
470 void CL_VM_CB_EndIncreaseEdicts(void)
475 // link every entity except world
476 for (i = 1, ent = prog->edicts;i < prog->max_edicts;i++, ent++)
477 if (!ent->priv.server->free)
481 void CL_VM_CB_InitEdict(prvm_edict_t *e)
483 e->priv.server->move = false; // don't move on first frame
486 void CL_VM_CB_FreeEdict(prvm_edict_t *ed)
488 World_UnlinkEdict(ed);
489 memset(ed->fields.client, 0, sizeof(*ed->fields.client));
492 void CL_VM_CB_CountEdicts(void)
496 int active = 0, models = 0, solid = 0;
498 for (i=0 ; i<prog->num_edicts ; i++)
500 ent = PRVM_EDICT_NUM(i);
501 if (ent->priv.server->free)
504 if (ent->fields.client->solid)
506 if (ent->fields.client->model)
510 Con_Printf("num_edicts:%3i\n", prog->num_edicts);
511 Con_Printf("active :%3i\n", active);
512 Con_Printf("view :%3i\n", models);
513 Con_Printf("touch :%3i\n", solid);
516 qboolean CL_VM_CB_LoadEdict(prvm_edict_t *ent)
521 void Cmd_ClearCsqcFuncs (void);
523 void CL_VM_Init (void)
525 unsigned char *csprogsdata;
526 fs_offset_t csprogsdatasize;
527 int csprogsdatacrc, requiredcrc;
530 // reset csqc_progcrc after reading it, so that changing servers doesn't
531 // expect csqc on the next server
532 requiredcrc = csqc_progcrc.integer;
533 requiredsize = csqc_progsize.integer;
534 Cvar_SetValueQuick(&csqc_progcrc, -1);
535 Cvar_SetValueQuick(&csqc_progsize, -1);
537 // if the server is not requesting a csprogs, then we're done here
541 // see if the requested csprogs.dat file matches the requested crc
543 csprogsdata = FS_LoadFile(va("dlcache/%s.%i.%i", csqc_progname.string, requiredsize, requiredcrc), tempmempool, true, &csprogsdatasize);
545 csprogsdata = FS_LoadFile(csqc_progname.string, tempmempool, true, &csprogsdatasize);
548 csprogsdatacrc = CRC_Block(csprogsdata, csprogsdatasize);
549 Mem_Free(csprogsdata);
550 if (csprogsdatacrc != requiredcrc || csprogsdatasize != requiredsize)
552 if (cls.demoplayback)
554 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);
559 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);
567 if (requiredcrc >= 0)
569 if (cls.demoplayback)
570 Con_Printf("CL_VM_Init: demo requires CSQC, but \"%s\" wasn't found\n", csqc_progname.string);
572 Con_Printf("CL_VM_Init: server requires CSQC, but \"%s\" wasn't found\n", csqc_progname.string);
579 PRVM_InitProg(PRVM_CLIENTPROG);
581 // allocate the mempools
582 prog->progs_mempool = Mem_AllocPool(csqc_progname.string, 0, NULL);
583 prog->headercrc = CL_PROGHEADER_CRC;
584 prog->edictprivate_size = 0; // no private struct used
585 prog->name = CL_NAME;
586 prog->num_edicts = 1;
587 prog->max_edicts = 512;
588 prog->limit_edicts = CL_MAX_EDICTS;
589 prog->reserved_edicts = 0;
590 prog->edictprivate_size = sizeof(edict_engineprivate_t);
591 prog->extensionstring = vm_sv_extensions;
592 prog->builtins = vm_cl_builtins;
593 prog->numbuiltins = vm_cl_numbuiltins;
594 prog->begin_increase_edicts = CL_VM_CB_BeginIncreaseEdicts;
595 prog->end_increase_edicts = CL_VM_CB_EndIncreaseEdicts;
596 prog->init_edict = CL_VM_CB_InitEdict;
597 prog->free_edict = CL_VM_CB_FreeEdict;
598 prog->count_edicts = CL_VM_CB_CountEdicts;
599 prog->load_edict = CL_VM_CB_LoadEdict;
600 prog->init_cmd = VM_CL_Cmd_Init;
601 prog->reset_cmd = VM_CL_Cmd_Reset;
602 prog->error_cmd = CL_VM_Error;
604 PRVM_LoadProgs(csqc_progname.string, cl_numrequiredfunc, cl_required_func, 0, NULL, 0, NULL);
608 CL_VM_Error("CSQC ^2failed to load\n");
614 Con_Printf("CSQC ^5loaded (crc=%i, size=%i)\n", csprogsdatacrc, (int)csprogsdatasize);
616 // check if OP_STATE animation is possible in this dat file
617 if (prog->fieldoffsets.nextthink >= 0 && prog->fieldoffsets.frame >= 0 && prog->fieldoffsets.think >= 0 && prog->globaloffsets.self >= 0)
618 prog->flag |= PRVM_OP_STATE;
621 prog->globals.client->time = cl.time;
623 prog->globals.client->mapname = PRVM_SetEngineString(cl.worldmodel->name);
624 prog->globals.client->player_localentnum = cl.playerentity;
626 // call the prog init
627 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Init, "QC function CSQC_Init is missing");
630 cl.csqc_loaded = true;
632 cl.csqc_vidvars.drawcrosshair = false;
633 cl.csqc_vidvars.drawenginesbar = false;
636 void CL_VM_ShutDown (void)
638 Cmd_ClearCsqcFuncs();
639 Cvar_SetValueQuick(&csqc_progcrc, -1);
640 Cvar_SetValueQuick(&csqc_progsize, -1);
644 prog->globals.client->time = cl.time;
645 PRVM_ExecuteProgram(prog->funcoffsets.CSQC_Shutdown, "QC function CSQC_Shutdown is missing");
648 Con_Print("CSQC ^1unloaded\n");
649 cl.csqc_loaded = false;