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;
66 qboolean csqc_loaded = false;
68 extern entity_t *cl_csqcentities;
69 extern unsigned char *cl_csqcentities_active;
70 extern int cl_num_csqcentities;
72 vec3_t csqc_origin, csqc_angles;
73 static double csqc_frametime = 0;
76 static mempool_t *csqc_mempool;
78 static void CL_VM_FindEdictFieldOffsets (void)
80 csqc_fieldoff_alpha = PRVM_ED_FindFieldOffset("alpha");
81 csqc_fieldoff_scale = PRVM_ED_FindFieldOffset("scale");
82 csqc_fieldoff_colormod = PRVM_ED_FindFieldOffset("colormod");
83 csqc_fieldoff_renderflags = PRVM_ED_FindFieldOffset("renderflags");
84 csqc_fieldoff_effects = PRVM_ED_FindFieldOffset("effects");
85 csqc_fieldoff_tag_entity = PRVM_ED_FindFieldOffset("tag_entity");
86 csqc_fieldoff_tag_index = PRVM_ED_FindFieldOffset("tag_index");
88 CSQC_Parse_TempEntity = PRVM_ED_FindFunction (CL_F_PARSE_TEMPENTITY);
89 CSQC_Parse_StuffCmd = PRVM_ED_FindFunction (CL_F_PARSE_STUFFCMD);
90 CSQC_Parse_Print = PRVM_ED_FindFunction (CL_F_PARSE_PRINT);
91 CSQC_Parse_CenterPrint = PRVM_ED_FindFunction (CL_F_PARSE_CENTERPRINT);
92 CSQC_Ent_Update = PRVM_ED_FindFunction (CL_F_ENT_UPDATE);
93 CSQC_Ent_Remove = PRVM_ED_FindFunction (CL_F_ENT_REMOVE);
94 CSQC_Event = PRVM_ED_FindFunction (CL_F_EVENT);
98 csqc_printtextbuf = Mem_Alloc(csqc_mempool, CSQC_PRINTBUFFERLEN);
99 csqc_printtextbuf[0] = 0;
103 void CL_VM_Error (const char *format, ...) //[515]: hope it will be never executed =)
105 char errorstring[4096];
108 va_start (argptr, format);
109 dpvsnprintf (errorstring, sizeof(errorstring), format, argptr);
111 // Con_Printf( "CL_VM_Error: %s\n", errorstring );
115 Mem_FreePool(&csqc_mempool);
117 Cvar_SetValueQuick(&csqc_progcrc, 0);
119 // Host_AbortCurrentFrame(); //[515]: hmmm... if server says it needs csqc then client MUST disconnect
120 Host_Error(va("CL_VM_Error: %s", errorstring));
123 //[515]: set globals before calling R_UpdateView, WEIRD CRAP
124 static void CSQC_SetGlobals (void)
126 //extern cvar_t sv_accelerate, sv_friction, sv_gravity, sv_stopspeed, sv_maxspeed;
129 *prog->time = cl.time;
130 prog->globals.client->frametime = cl.time - csqc_frametime;
131 csqc_frametime = cl.time;
132 prog->globals.client->servercommandframe = cl.servermovesequence;
133 prog->globals.client->clientcommandframe = cl.movemessages;
134 VectorCopy(cl.viewangles, prog->globals.client->input_angles);
135 VectorCopy(cl.viewangles, csqc_angles);
136 prog->globals.client->input_buttons = csqc_buttons;
137 VectorSet(prog->globals.client->input_movevalues, cl.cmd.forwardmove, cl.cmd.sidemove, cl.cmd.upmove);
138 //VectorCopy(cl.movement_origin, csqc_origin);
139 VectorCopy(cl_entities[cl.viewentity].render.origin, csqc_origin);
140 VectorCopy(csqc_origin, prog->globals.client->pmove_org);
141 prog->globals.client->maxclients = cl.maxclients;
142 //VectorCopy(cl.movement_velocity, prog->globals.client->pmove_vel);
143 VectorCopy(cl.velocity, prog->globals.client->pmove_vel);
147 static void CSQC_Predraw (prvm_edict_t *ed)
150 if(!ed->fields.client->predraw)
152 b = prog->globals.client->self;
153 prog->globals.client->self = PRVM_EDICT_TO_PROG(ed);
154 PRVM_ExecuteProgram(ed->fields.client->predraw, "CSQC_Predraw: NULL function\n");
155 prog->globals.client->self = b;
158 static void CSQC_Think (prvm_edict_t *ed)
161 if(ed->fields.client->think)
162 if(ed->fields.client->nextthink && ed->fields.client->nextthink <= *prog->time)
164 ed->fields.client->nextthink = 0;
165 b = prog->globals.client->self;
166 prog->globals.client->self = PRVM_EDICT_TO_PROG(ed);
167 PRVM_ExecuteProgram(ed->fields.client->think, "CSQC_Think: NULL function\n");
168 prog->globals.client->self = b;
173 static qboolean CSQC_EdictToEntity (prvm_edict_t *ed, entity_t *e)
178 i = ed->fields.client->modelindex;
179 e->state_current.modelindex = 0;
180 if(i >= MAX_MODELS || i <= -MAX_MODELS) //[515]: make work as error ?
182 Con_Print("CSQC_EdictToEntity: modelindex >= MAX_MODELS\n");
183 ed->fields.client->modelindex = 0;
186 e->state_current.modelindex = i;
187 if(!e->state_current.modelindex)
190 e->state_current.time = cl.time;
193 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_renderflags)) && val->_float)
196 if(i & RF_VIEWMODEL) e->state_current.flags |= RENDER_VIEWMODEL;
197 if(i & RF_EXTERNALMODEL)e->state_current.flags |= RENDER_EXTERIORMODEL;
198 if(i & RF_DEPTHHACK) e->state_current.effects |= EF_NODEPTHTEST;
199 if(i & RF_ADDITIVE) e->state_current.effects |= EF_ADDITIVE;
202 if(i & RF_USEAXIS) //FIXME!!!
203 VectorCopy(ed->fields.client->angles, e->persistent.newangles);
205 VectorCopy(ed->fields.client->angles, e->persistent.newangles);
207 VectorCopy(ed->fields.client->origin, e->persistent.neworigin);
208 VectorCopy(ed->fields.client->origin, e->state_current.origin);
209 e->state_current.colormap = ed->fields.client->colormap;
210 e->state_current.effects = ed->fields.client->effects;
211 e->state_current.frame = ed->fields.client->frame;
212 e->state_current.skin = ed->fields.client->skin;
214 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_alpha)) && val->_float) e->state_current.alpha = val->_float*255;
215 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_scale)) && val->_float) e->state_current.scale = val->_float*16;
216 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_colormod)) && VectorLength2(val->vector)) VectorScale(val->vector, 32, e->state_current.colormod);
217 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_effects)) && val->_float) e->state_current.effects = val->_float;
218 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_tag_entity)) && val->edict)
220 e->state_current.tagentity = val->edict;
221 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_tag_index)) && val->_float)
222 e->state_current.tagindex = val->_float;
228 void CSQC_ClearCSQCEntities (void)
230 memset(cl_csqcentities_active, 0, sizeof(cl_csqcentities_active));
231 cl_num_csqcentities = 0;
235 void CL_ExpandCSQCEntities (int num);
237 void CSQC_RelinkCSQCEntities (void)
243 *prog->time = cl.time;
244 for(i=1;i<prog->num_edicts;i++)
246 if(i >= cl_max_csqcentities)
247 CL_ExpandCSQCEntities(i);
249 e = &cl_csqcentities[i];
250 ed = &prog->edicts[i];
251 if(ed->priv.required->free)
253 e->state_current.active = false;
254 cl_csqcentities_active[i] = false;
257 VectorAdd(ed->fields.client->origin, ed->fields.client->mins, ed->fields.client->absmin);
258 VectorAdd(ed->fields.client->origin, ed->fields.client->maxs, ed->fields.client->absmax);
260 if(ed->priv.required->free)
262 e->state_current.active = false;
263 cl_csqcentities_active[i] = false;
267 if(ed->priv.required->free)
269 e->state_current.active = false;
270 cl_csqcentities_active[i] = false;
273 if(!cl_csqcentities_active[i])
274 if(!((int)ed->fields.client->drawmask & csqc_drawmask))
277 e->state_previous = e->state_current;
278 e->state_current = defaultstate;
279 if((cl_csqcentities_active[i] = CSQC_EdictToEntity(ed, e)))
281 if(!e->state_current.active)
283 if(!e->state_previous.active)
284 VectorCopy(ed->fields.client->origin, e->persistent.trail_origin);//[515]: hack to make good gibs =/
285 e->state_previous = e->state_current;
286 e->state_current.active = true;
288 e->persistent.lerpdeltatime = 0;//prog->globals.client->frametime;
289 cl_num_csqcentities++;
294 //[515]: omfg... it's all weird =/
295 void CSQC_AddEntity (int n)
297 cl_csqcentities_active[n] = true;
300 qboolean CL_VM_InputEvent (qboolean pressed, int key)
306 *prog->time = cl.time;
307 PRVM_G_FLOAT(OFS_PARM0) = pressed;
308 PRVM_G_FLOAT(OFS_PARM1) = key;
309 PRVM_ExecuteProgram (prog->globals.client->CSQC_InputEvent, CL_F_INPUTEVENT);
315 qboolean CL_VM_UpdateView (void)
321 //VectorCopy(cl.viewangles, oldangles);
322 *prog->time = cl.time;
325 cl_num_csqcentities = 0;
326 PRVM_ExecuteProgram (prog->globals.client->CSQC_UpdateView, CL_F_UPDATEVIEW);
327 //VectorCopy(oldangles, cl.viewangles);
333 qboolean CL_VM_ConsoleCommand (const char *cmd)
339 *prog->time = cl.time;
340 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(cmd);
341 PRVM_ExecuteProgram (prog->globals.client->CSQC_ConsoleCommand, CL_F_CONSOLECOMMAND);
347 qboolean CL_VM_Parse_TempEntity (void)
351 if(!csqc_loaded || !CSQC_Parse_TempEntity)
355 *prog->time = cl.time;
356 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_TempEntity - prog->functions), CL_F_PARSE_TEMPENTITY);
367 void CL_VM_Parse_StuffCmd (const char *msg)
369 if(!csqc_loaded) //[515]: add more here
375 Cvar_SetQuick(&csqc_progcrc, "0");
376 csqc_progcrc.flags = 0;
377 Cmd_ExecuteString (msg, src_command);
378 csqc_progcrc.flags = CVAR_READONLY;
381 if(!csqc_loaded || !CSQC_Parse_StuffCmd)
387 *prog->time = cl.time;
388 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(msg);
389 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_StuffCmd - prog->functions), CL_F_PARSE_STUFFCMD);
393 static void CL_VM_Parse_Print (const char *msg)
396 *prog->time = cl.time;
397 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(msg);
398 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_Print - prog->functions), CL_F_PARSE_PRINT);
402 void CSQC_AddPrintText (const char *msg)
405 if(!csqc_loaded || !CSQC_Parse_Print)
411 if(msg[i] != '\n' && msg[i] != '\r')
413 if(strlen(csqc_printtextbuf)+i >= CSQC_PRINTBUFFERLEN)
415 CL_VM_Parse_Print(csqc_printtextbuf);
416 csqc_printtextbuf[0] = 0;
419 strcat(csqc_printtextbuf, msg);
422 strcat(csqc_printtextbuf, msg);
423 CL_VM_Parse_Print(csqc_printtextbuf);
424 csqc_printtextbuf[0] = 0;
427 void CL_VM_Parse_CenterPrint (const char *msg)
429 if(!csqc_loaded || !CSQC_Parse_CenterPrint)
431 SCR_CenterPrint((char*)msg);
435 *prog->time = cl.time;
436 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(msg);
437 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_CenterPrint - prog->functions), CL_F_PARSE_CENTERPRINT);
441 float CL_VM_Event (float event) //[515]: needed ? I'd say "YES", but don't know for what :D
444 if(!csqc_loaded || !CSQC_Event)
447 *prog->time = cl.time;
448 PRVM_G_FLOAT(OFS_PARM0) = event;
449 PRVM_ExecuteProgram ((func_t)(CSQC_Event - prog->functions), CL_F_EVENT);
455 void CSQC_ReadEntities (void)
457 unsigned short entnum, oldself, realentnum;
459 *prog->time = cl.time;
460 oldself = prog->globals.client->self;
463 entnum = MSG_ReadShort();
466 realentnum = entnum & 0x7FFF;
467 prog->globals.client->self = csqc_sv2csqcents[realentnum];
470 if(prog->globals.client->self)
472 PRVM_ExecuteProgram((func_t)(CSQC_Ent_Remove - prog->functions), CL_F_ENT_REMOVE);
473 csqc_sv2csqcents[realentnum] = 0;
476 Con_Printf("Smth bad happens in csqc...\n"); //[515]: never happens ?
480 if(!prog->globals.client->self)
483 ed = PRVM_ED_Alloc();
484 ed->fields.client->entnum = realentnum;
485 prog->globals.client->self = csqc_sv2csqcents[realentnum] = PRVM_EDICT_TO_PROG(ed);
486 PRVM_G_FLOAT(OFS_PARM0) = 1;
489 PRVM_G_FLOAT(OFS_PARM0) = 0;
490 PRVM_ExecuteProgram((func_t)(CSQC_Ent_Update - prog->functions), CL_F_ENT_UPDATE);
493 prog->globals.client->self = oldself;
497 void Cmd_ClearCsqcFuncs (void);
499 void CL_VM_Init (void)
504 memset(cl.csqc_model_precache, 0, sizeof(cl.csqc_model_precache));
505 memset(&cl.csqc_vidvars, true, sizeof(csqc_vidvars_t));
507 if(!FS_FileExists(csqc_progname.string))
509 if(!sv.active && csqc_progcrc.integer)
511 Con_Printf("CL_VM_Init: server requires CSQC, but \"%s\" wasn't found\n", csqc_progname.string);
517 if(!sv.active && !csqc_progcrc.integer) //[515]: because cheaters may use csqc while server didn't allowed it !
519 Con_Printf("CL_VM_Init: server didn't sent CSQC crc, so CSQC is disabled\n");
524 PRVM_InitProg(PRVM_CLIENTPROG);
526 // allocate the mempools
527 prog->progs_mempool = Mem_AllocPool(csqc_progname.string, 0, NULL);
528 prog->headercrc = CL_PROGHEADER_CRC;
529 prog->edictprivate_size = 0; // no private struct used
530 prog->name = csqc_progname.string;
531 prog->num_edicts = 1;
532 prog->limit_edicts = CL_MAX_EDICTS;
533 prog->extensionstring = vm_cl_extensions;
534 prog->builtins = vm_cl_builtins;
535 prog->numbuiltins = vm_cl_numbuiltins;
536 prog->init_cmd = VM_CL_Cmd_Init;
537 prog->reset_cmd = VM_CL_Cmd_Reset;
538 prog->error_cmd = CL_VM_Error;
540 PRVM_LoadProgs(csqc_progname.string, cl_numrequiredfunc, cl_required_func, 0, NULL);
542 if(!sv.active && !cls.demoplayback && prog->filecrc != (unsigned short)csqc_progcrc.integer)
544 Con_Printf("^1Your CSQC version differs from server's one (%i!=%i)\n", prog->filecrc, csqc_progcrc.integer);
552 Cvar_SetValueQuick(&csqc_progcrc, prog->filecrc);
553 Con_Printf("CSQC ^5loaded (crc=%i)\n", csqc_progcrc.integer);
557 CL_VM_Error("CSQC ^2failed to load\n");
563 csqc_mempool = Mem_AllocPool("CSQC", 0, NULL);
565 //[515]: optional fields & funcs
566 CL_VM_FindEdictFieldOffsets();
569 *prog->time = cl.time;
572 prog->globals.client->mapname = PRVM_SetEngineString(cl.worldmodel->name);
573 prog->globals.client->player_localentnum = cl.playerentity;
575 // call the prog init
576 PRVM_ExecuteProgram((func_t) (PRVM_ED_FindFunction(CL_F_INIT) - prog->functions), CL_F_INIT);
581 csqc_sv2csqcents = Mem_Alloc(csqc_mempool, MAX_EDICTS*sizeof(unsigned short));
582 memset(csqc_sv2csqcents, 0, MAX_EDICTS*sizeof(unsigned short));
584 cl.csqc_vidvars.drawcrosshair = false;
585 cl.csqc_vidvars.drawenginesbar = false;
588 ent = &cl_csqcentities[0];
589 // entire entity array was cleared, so just fill in a few fields
590 ent->state_current.active = true;
591 ent->render.model = cl.worldmodel = cl.model_precache[1];
592 ent->render.scale = 1; // some of the renderer still relies on scale
593 ent->render.alpha = 1;
594 ent->render.flags = RENDER_SHADOW | RENDER_LIGHT;
595 Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, 0, 0, 0, 0, 0, 0, 1);
596 Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
597 CL_BoundingBoxForEntity(&ent->render);
600 void CL_VM_ShutDown (void)
602 Cmd_ClearCsqcFuncs();
603 Cvar_SetValueQuick(&csqc_progcrc, 0);
607 *prog->time = cl.time;
608 PRVM_ExecuteProgram((func_t) (PRVM_ED_FindFunction(CL_F_SHUTDOWN) - prog->functions), CL_F_SHUTDOWN);
611 Con_Print("CSQC ^1unloaded\n");
613 Mem_FreePool(&csqc_mempool);