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 unsigned int csqc_drawmask = 0;
47 static char *csqc_printtextbuf = NULL;
48 static unsigned short *csqc_sv2csqcents; //[515]: server entities numbers on client side. FIXME : make pointers instead of numbers ?
50 static mfunction_t *CSQC_Parse_TempEntity;
51 static mfunction_t *CSQC_Parse_StuffCmd;
52 static mfunction_t *CSQC_Parse_Print;
53 static mfunction_t *CSQC_Parse_CenterPrint;
54 static mfunction_t *CSQC_Ent_Update;
55 static mfunction_t *CSQC_Ent_Remove;
56 static mfunction_t *CSQC_Event;
58 static int csqc_fieldoff_alpha;
59 static int csqc_fieldoff_colormod;
60 static int csqc_fieldoff_effects;
61 int csqc_fieldoff_scale;
62 int csqc_fieldoff_renderflags;
63 int csqc_fieldoff_tag_entity;
64 int csqc_fieldoff_tag_index;
65 int csqc_fieldoff_dphitcontentsmask;
67 qboolean csqc_loaded = false;
69 vec3_t csqc_origin, csqc_angles;
70 static double csqc_frametime = 0;
73 static mempool_t *csqc_mempool;
75 static void CL_VM_FindEdictFieldOffsets (void)
77 csqc_fieldoff_alpha = PRVM_ED_FindFieldOffset("alpha");
78 csqc_fieldoff_scale = PRVM_ED_FindFieldOffset("scale");
79 csqc_fieldoff_colormod = PRVM_ED_FindFieldOffset("colormod");
80 csqc_fieldoff_renderflags = PRVM_ED_FindFieldOffset("renderflags");
81 csqc_fieldoff_effects = PRVM_ED_FindFieldOffset("effects");
82 csqc_fieldoff_tag_entity = PRVM_ED_FindFieldOffset("tag_entity");
83 csqc_fieldoff_tag_index = PRVM_ED_FindFieldOffset("tag_index");
85 CSQC_Parse_TempEntity = PRVM_ED_FindFunction (CL_F_PARSE_TEMPENTITY);
86 CSQC_Parse_StuffCmd = PRVM_ED_FindFunction (CL_F_PARSE_STUFFCMD);
87 CSQC_Parse_Print = PRVM_ED_FindFunction (CL_F_PARSE_PRINT);
88 CSQC_Parse_CenterPrint = PRVM_ED_FindFunction (CL_F_PARSE_CENTERPRINT);
89 CSQC_Ent_Update = PRVM_ED_FindFunction (CL_F_ENT_UPDATE);
90 CSQC_Ent_Remove = PRVM_ED_FindFunction (CL_F_ENT_REMOVE);
91 CSQC_Event = PRVM_ED_FindFunction (CL_F_EVENT);
95 csqc_printtextbuf = (char *)Mem_Alloc(csqc_mempool, CSQC_PRINTBUFFERLEN);
96 csqc_printtextbuf[0] = 0;
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, 0);
116 // Host_AbortCurrentFrame(); //[515]: hmmm... if server says it needs csqc then client MUST disconnect
117 Host_Error(va("CL_VM_Error: %s", errorstring));
120 //[515]: set globals before calling R_UpdateView, WEIRD CRAP
121 static void CSQC_SetGlobals (void)
123 //extern cvar_t sv_accelerate, sv_friction, sv_gravity, sv_stopspeed, sv_maxspeed;
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 VectorCopy(cl.entities[cl.viewentity].render.origin, 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);
144 static void CSQC_Predraw (prvm_edict_t *ed)
147 if(!ed->fields.client->predraw)
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;
155 static void CSQC_Think (prvm_edict_t *ed)
158 if(ed->fields.client->think)
159 if(ed->fields.client->nextthink && ed->fields.client->nextthink <= *prog->time)
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;
170 static qboolean CSQC_EdictToEntity (prvm_edict_t *ed, entity_t *e)
175 i = (int)ed->fields.client->modelindex;
176 e->state_current.modelindex = 0;
177 if(i >= MAX_MODELS || i <= -MAX_MODELS) //[515]: make work as error ?
179 Con_Print("CSQC_EdictToEntity: modelindex >= MAX_MODELS\n");
180 ed->fields.client->modelindex = 0;
183 e->state_current.modelindex = i;
184 if(!e->state_current.modelindex)
187 e->state_current.time = cl.time;
190 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_renderflags)) && val->_float)
192 i = (int)val->_float;
193 if(i & RF_VIEWMODEL) e->state_current.flags |= RENDER_VIEWMODEL;
194 if(i & RF_EXTERNALMODEL)e->state_current.flags |= RENDER_EXTERIORMODEL;
195 if(i & RF_DEPTHHACK) e->state_current.effects |= EF_NODEPTHTEST;
196 if(i & RF_ADDITIVE) e->state_current.effects |= EF_ADDITIVE;
199 if(i & RF_USEAXIS) //FIXME!!!
200 VectorCopy(ed->fields.client->angles, e->persistent.newangles);
202 VectorCopy(ed->fields.client->angles, e->persistent.newangles);
204 VectorCopy(ed->fields.client->origin, e->persistent.neworigin);
205 VectorCopy(ed->fields.client->origin, e->state_current.origin);
206 e->state_current.colormap = (int)ed->fields.client->colormap;
207 e->state_current.effects = (int)ed->fields.client->effects;
208 e->state_current.frame = (int)ed->fields.client->frame;
209 e->state_current.skin = (int)ed->fields.client->skin;
211 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_alpha)) && val->_float) e->state_current.alpha = (int)(val->_float*255);
212 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_scale)) && val->_float) e->state_current.scale = (int)(val->_float*16);
213 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_colormod)) && VectorLength2(val->vector)) {int j;for (j = 0;j < 3;j++) e->state_current.colormod[j] = (unsigned char)bound(0, val->vector[j] * 32.0f, 255);}
214 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_effects)) && val->_float) e->state_current.effects = (int)val->_float;
215 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_tag_entity)) && val->edict)
217 e->state_current.tagentity = val->edict;
218 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_tag_index)) && val->_float)
219 e->state_current.tagindex = (int)val->_float;
225 void CSQC_ClearCSQCEntities (void)
227 memset(cl.csqcentities_active, 0, sizeof(cl.csqcentities_active));
228 cl.num_csqcentities = 0;
232 void CL_ExpandCSQCEntities (int num);
234 void CSQC_RelinkCSQCEntities (void)
240 *prog->time = cl.time;
241 for(i=1;i<prog->num_edicts;i++)
243 if(i >= cl.max_csqcentities)
244 CL_ExpandCSQCEntities(i);
246 e = &cl.csqcentities[i];
247 ed = &prog->edicts[i];
248 if(ed->priv.required->free)
250 e->state_current.active = false;
251 cl.csqcentities_active[i] = false;
254 VectorAdd(ed->fields.client->origin, ed->fields.client->mins, ed->fields.client->absmin);
255 VectorAdd(ed->fields.client->origin, ed->fields.client->maxs, ed->fields.client->absmax);
257 if(ed->priv.required->free)
259 e->state_current.active = false;
260 cl.csqcentities_active[i] = false;
264 if(ed->priv.required->free)
266 e->state_current.active = false;
267 cl.csqcentities_active[i] = false;
270 if(!cl.csqcentities_active[i])
271 if(!((int)ed->fields.client->drawmask & csqc_drawmask))
274 e->state_previous = e->state_current;
275 e->state_current = defaultstate;
276 if((cl.csqcentities_active[i] = CSQC_EdictToEntity(ed, e)))
278 if(!e->state_current.active)
280 if(!e->state_previous.active)
281 VectorCopy(ed->fields.client->origin, e->persistent.trail_origin);//[515]: hack to make good gibs =/
282 e->state_previous = e->state_current;
283 e->state_current.active = true;
285 e->persistent.lerpdeltatime = 0;//prog->globals.client->frametime;
286 cl.num_csqcentities++;
291 //[515]: omfg... it's all weird =/
292 void CSQC_AddEntity (int n)
294 cl.csqcentities_active[n] = true;
297 qboolean CL_VM_InputEvent (qboolean pressed, int key)
303 *prog->time = cl.time;
304 PRVM_G_FLOAT(OFS_PARM0) = pressed;
305 PRVM_G_FLOAT(OFS_PARM1) = key;
306 PRVM_ExecuteProgram (prog->globals.client->CSQC_InputEvent, CL_F_INPUTEVENT);
312 qboolean CL_VM_UpdateView (void)
318 //VectorCopy(cl.viewangles, oldangles);
319 *prog->time = cl.time;
322 cl.num_csqcentities = 0;
323 PRVM_ExecuteProgram (prog->globals.client->CSQC_UpdateView, CL_F_UPDATEVIEW);
324 //VectorCopy(oldangles, cl.viewangles);
330 qboolean CL_VM_ConsoleCommand (const char *cmd)
336 *prog->time = cl.time;
337 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(cmd);
338 PRVM_ExecuteProgram (prog->globals.client->CSQC_ConsoleCommand, CL_F_CONSOLECOMMAND);
344 qboolean CL_VM_Parse_TempEntity (void)
348 if(!csqc_loaded || !CSQC_Parse_TempEntity)
352 *prog->time = cl.time;
353 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_TempEntity - prog->functions), CL_F_PARSE_TEMPENTITY);
364 void CL_VM_Parse_StuffCmd (const char *msg)
366 if(!csqc_loaded) //[515]: add more here
372 Cvar_SetQuick(&csqc_progcrc, "0");
373 csqc_progcrc.flags = 0;
374 Cmd_ExecuteString (msg, src_command);
375 csqc_progcrc.flags = CVAR_READONLY;
378 if(!csqc_loaded || !CSQC_Parse_StuffCmd)
384 *prog->time = cl.time;
385 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(msg);
386 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_StuffCmd - prog->functions), CL_F_PARSE_STUFFCMD);
390 static void CL_VM_Parse_Print (const char *msg)
393 *prog->time = cl.time;
394 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(msg);
395 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_Print - prog->functions), CL_F_PARSE_PRINT);
399 void CSQC_AddPrintText (const char *msg)
402 if(!csqc_loaded || !CSQC_Parse_Print)
407 // FIXME: is this bugged?
409 if(msg[i] != '\n' && msg[i] != '\r')
411 if(strlen(csqc_printtextbuf)+i >= CSQC_PRINTBUFFERLEN)
413 CL_VM_Parse_Print(csqc_printtextbuf);
414 csqc_printtextbuf[0] = 0;
417 strcat(csqc_printtextbuf, msg);
420 strcat(csqc_printtextbuf, msg);
421 CL_VM_Parse_Print(csqc_printtextbuf);
422 csqc_printtextbuf[0] = 0;
425 void CL_VM_Parse_CenterPrint (const char *msg)
427 if(!csqc_loaded || !CSQC_Parse_CenterPrint)
429 SCR_CenterPrint((char*)msg);
433 *prog->time = cl.time;
434 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(msg);
435 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_CenterPrint - prog->functions), CL_F_PARSE_CENTERPRINT);
439 float CL_VM_Event (float event) //[515]: needed ? I'd say "YES", but don't know for what :D
442 if(!csqc_loaded || !CSQC_Event)
445 *prog->time = cl.time;
446 PRVM_G_FLOAT(OFS_PARM0) = event;
447 PRVM_ExecuteProgram ((func_t)(CSQC_Event - prog->functions), CL_F_EVENT);
453 void CSQC_ReadEntities (void)
455 unsigned short entnum, oldself, realentnum;
457 *prog->time = cl.time;
458 oldself = prog->globals.client->self;
461 entnum = MSG_ReadShort();
464 realentnum = entnum & 0x7FFF;
465 prog->globals.client->self = csqc_sv2csqcents[realentnum];
468 if(prog->globals.client->self)
470 PRVM_ExecuteProgram((func_t)(CSQC_Ent_Remove - prog->functions), CL_F_ENT_REMOVE);
471 csqc_sv2csqcents[realentnum] = 0;
474 Con_Printf("Smth bad happens in csqc...\n"); //[515]: never happens ?
478 if(!prog->globals.client->self)
481 ed = PRVM_ED_Alloc();
482 ed->fields.client->entnum = realentnum;
483 prog->globals.client->self = csqc_sv2csqcents[realentnum] = PRVM_EDICT_TO_PROG(ed);
484 PRVM_G_FLOAT(OFS_PARM0) = 1;
487 PRVM_G_FLOAT(OFS_PARM0) = 0;
488 PRVM_ExecuteProgram((func_t)(CSQC_Ent_Update - prog->functions), CL_F_ENT_UPDATE);
491 prog->globals.client->self = oldself;
495 void Cmd_ClearCsqcFuncs (void);
497 void CL_VM_Init (void)
502 memset(cl.csqc_model_precache, 0, sizeof(cl.csqc_model_precache));
503 memset(&cl.csqc_vidvars, true, sizeof(csqc_vidvars_t));
505 if(!FS_FileExists(csqc_progname.string))
507 if(!sv.active && csqc_progcrc.integer)
509 Con_Printf("CL_VM_Init: server requires CSQC, but \"%s\" wasn't found\n", csqc_progname.string);
515 if(!sv.active && !csqc_progcrc.integer) //[515]: because cheaters may use csqc while server didn't allowed it !
517 Con_Printf("CL_VM_Init: server didn't sent CSQC crc, so CSQC is disabled\n");
522 PRVM_InitProg(PRVM_CLIENTPROG);
524 // allocate the mempools
525 prog->progs_mempool = Mem_AllocPool(csqc_progname.string, 0, NULL);
526 prog->headercrc = CL_PROGHEADER_CRC;
527 prog->edictprivate_size = 0; // no private struct used
528 prog->name = csqc_progname.string;
529 prog->num_edicts = 1;
530 prog->limit_edicts = CL_MAX_EDICTS;
531 prog->extensionstring = vm_cl_extensions;
532 prog->builtins = vm_cl_builtins;
533 prog->numbuiltins = vm_cl_numbuiltins;
534 prog->init_cmd = VM_CL_Cmd_Init;
535 prog->reset_cmd = VM_CL_Cmd_Reset;
536 prog->error_cmd = CL_VM_Error;
538 PRVM_LoadProgs(csqc_progname.string, cl_numrequiredfunc, cl_required_func, 0, NULL);
540 if(!sv.active && !cls.demoplayback && prog->filecrc != (unsigned short)csqc_progcrc.integer)
542 Con_Printf("^1Your CSQC version differs from server's one (%i!=%i)\n", prog->filecrc, csqc_progcrc.integer);
550 Cvar_SetValueQuick(&csqc_progcrc, prog->filecrc);
551 Con_Printf("CSQC ^5loaded (crc=%i)\n", csqc_progcrc.integer);
555 CL_VM_Error("CSQC ^2failed to load\n");
561 csqc_mempool = Mem_AllocPool("CSQC", 0, NULL);
563 //[515]: optional fields & funcs
564 CL_VM_FindEdictFieldOffsets();
567 *prog->time = cl.time;
570 prog->globals.client->mapname = PRVM_SetEngineString(cl.worldmodel->name);
571 prog->globals.client->player_localentnum = cl.playerentity;
573 // call the prog init
574 PRVM_ExecuteProgram((func_t) (PRVM_ED_FindFunction(CL_F_INIT) - prog->functions), CL_F_INIT);
579 csqc_sv2csqcents = (unsigned short *)Mem_Alloc(csqc_mempool, MAX_EDICTS*sizeof(unsigned short));
580 memset(csqc_sv2csqcents, 0, MAX_EDICTS*sizeof(unsigned short));
582 cl.csqc_vidvars.drawcrosshair = false;
583 cl.csqc_vidvars.drawenginesbar = false;
586 ent = &cl.csqcentities[0];
587 // entire entity array was cleared, so just fill in a few fields
588 ent->state_current.active = true;
589 ent->render.model = cl.worldmodel = cl.model_precache[1];
590 ent->render.scale = 1; // some of the renderer still relies on scale
591 ent->render.alpha = 1;
592 ent->render.flags = RENDER_SHADOW | RENDER_LIGHT;
593 Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, 0, 0, 0, 0, 0, 0, 1);
594 Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
595 CL_BoundingBoxForEntity(&ent->render);
598 void CL_VM_ShutDown (void)
600 Cmd_ClearCsqcFuncs();
601 Cvar_SetValueQuick(&csqc_progcrc, 0);
605 *prog->time = cl.time;
606 PRVM_ExecuteProgram((func_t) (PRVM_ED_FindFunction(CL_F_SHUTDOWN) - prog->functions), CL_F_SHUTDOWN);
609 Con_Print("CSQC ^1unloaded\n");
611 Mem_FreePool(&csqc_mempool);