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 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 = Mem_Alloc(csqc_mempool, CSQC_PRINTBUFFERLEN);
95 csqc_printtextbuf[0] = 0;
99 void CL_VM_Error (const char *format, ...) //[515]: hope it will be never executed =)
101 char errorstring[4096];
104 va_start (argptr, format);
105 dpvsnprintf (errorstring, sizeof(errorstring), format, argptr);
107 // Con_Printf( "CL_VM_Error: %s\n", errorstring );
111 Mem_FreePool(&csqc_mempool);
113 Cvar_SetValueQuick(&csqc_progcrc, 0);
115 // Host_AbortCurrentFrame(); //[515]: hmmm... if server says it needs csqc then client MUST disconnect
116 Host_Error(va("CL_VM_Error: %s", errorstring));
119 //[515]: set globals before calling R_UpdateView, WEIRD CRAP
120 static void CSQC_SetGlobals (void)
122 //extern cvar_t sv_accelerate, sv_friction, sv_gravity, sv_stopspeed, sv_maxspeed;
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 VectorCopy(cl.entities[cl.viewentity].render.origin, 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);
143 static void CSQC_Predraw (prvm_edict_t *ed)
146 if(!ed->fields.client->predraw)
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;
154 static void CSQC_Think (prvm_edict_t *ed)
157 if(ed->fields.client->think)
158 if(ed->fields.client->nextthink && ed->fields.client->nextthink <= *prog->time)
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;
169 static qboolean CSQC_EdictToEntity (prvm_edict_t *ed, entity_t *e)
174 i = ed->fields.client->modelindex;
175 e->state_current.modelindex = 0;
176 if(i >= MAX_MODELS || i <= -MAX_MODELS) //[515]: make work as error ?
178 Con_Print("CSQC_EdictToEntity: modelindex >= MAX_MODELS\n");
179 ed->fields.client->modelindex = 0;
182 e->state_current.modelindex = i;
183 if(!e->state_current.modelindex)
186 e->state_current.time = cl.time;
189 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_renderflags)) && val->_float)
192 if(i & RF_VIEWMODEL) e->state_current.flags |= RENDER_VIEWMODEL;
193 if(i & RF_EXTERNALMODEL)e->state_current.flags |= RENDER_EXTERIORMODEL;
194 if(i & RF_DEPTHHACK) e->state_current.effects |= EF_NODEPTHTEST;
195 if(i & RF_ADDITIVE) e->state_current.effects |= EF_ADDITIVE;
198 if(i & RF_USEAXIS) //FIXME!!!
199 VectorCopy(ed->fields.client->angles, e->persistent.newangles);
201 VectorCopy(ed->fields.client->angles, e->persistent.newangles);
203 VectorCopy(ed->fields.client->origin, e->persistent.neworigin);
204 VectorCopy(ed->fields.client->origin, e->state_current.origin);
205 e->state_current.colormap = ed->fields.client->colormap;
206 e->state_current.effects = ed->fields.client->effects;
207 e->state_current.frame = ed->fields.client->frame;
208 e->state_current.skin = ed->fields.client->skin;
210 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_alpha)) && val->_float) e->state_current.alpha = val->_float*255;
211 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_scale)) && val->_float) e->state_current.scale = val->_float*16;
212 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_colormod)) && VectorLength2(val->vector)) VectorScale(val->vector, 32, e->state_current.colormod);
213 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_effects)) && val->_float) e->state_current.effects = val->_float;
214 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_tag_entity)) && val->edict)
216 e->state_current.tagentity = val->edict;
217 if((val = PRVM_GETEDICTFIELDVALUE(ed, csqc_fieldoff_tag_index)) && val->_float)
218 e->state_current.tagindex = val->_float;
224 void CSQC_ClearCSQCEntities (void)
226 memset(cl.csqcentities_active, 0, sizeof(cl.csqcentities_active));
227 cl.num_csqcentities = 0;
231 void CL_ExpandCSQCEntities (int num);
233 void CSQC_RelinkCSQCEntities (void)
239 *prog->time = cl.time;
240 for(i=1;i<prog->num_edicts;i++)
242 if(i >= cl.max_csqcentities)
243 CL_ExpandCSQCEntities(i);
245 e = &cl.csqcentities[i];
246 ed = &prog->edicts[i];
247 if(ed->priv.required->free)
249 e->state_current.active = false;
250 cl.csqcentities_active[i] = false;
253 VectorAdd(ed->fields.client->origin, ed->fields.client->mins, ed->fields.client->absmin);
254 VectorAdd(ed->fields.client->origin, ed->fields.client->maxs, ed->fields.client->absmax);
256 if(ed->priv.required->free)
258 e->state_current.active = false;
259 cl.csqcentities_active[i] = false;
263 if(ed->priv.required->free)
265 e->state_current.active = false;
266 cl.csqcentities_active[i] = false;
269 if(!cl.csqcentities_active[i])
270 if(!((int)ed->fields.client->drawmask & csqc_drawmask))
273 e->state_previous = e->state_current;
274 e->state_current = defaultstate;
275 if((cl.csqcentities_active[i] = CSQC_EdictToEntity(ed, e)))
277 if(!e->state_current.active)
279 if(!e->state_previous.active)
280 VectorCopy(ed->fields.client->origin, e->persistent.trail_origin);//[515]: hack to make good gibs =/
281 e->state_previous = e->state_current;
282 e->state_current.active = true;
284 e->persistent.lerpdeltatime = 0;//prog->globals.client->frametime;
285 cl.num_csqcentities++;
290 //[515]: omfg... it's all weird =/
291 void CSQC_AddEntity (int n)
293 cl.csqcentities_active[n] = true;
296 qboolean CL_VM_InputEvent (qboolean pressed, int key)
302 *prog->time = cl.time;
303 PRVM_G_FLOAT(OFS_PARM0) = pressed;
304 PRVM_G_FLOAT(OFS_PARM1) = key;
305 PRVM_ExecuteProgram (prog->globals.client->CSQC_InputEvent, CL_F_INPUTEVENT);
311 qboolean CL_VM_UpdateView (void)
317 //VectorCopy(cl.viewangles, oldangles);
318 *prog->time = cl.time;
321 cl.num_csqcentities = 0;
322 PRVM_ExecuteProgram (prog->globals.client->CSQC_UpdateView, CL_F_UPDATEVIEW);
323 //VectorCopy(oldangles, cl.viewangles);
329 qboolean CL_VM_ConsoleCommand (const char *cmd)
335 *prog->time = cl.time;
336 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(cmd);
337 PRVM_ExecuteProgram (prog->globals.client->CSQC_ConsoleCommand, CL_F_CONSOLECOMMAND);
343 qboolean CL_VM_Parse_TempEntity (void)
347 if(!csqc_loaded || !CSQC_Parse_TempEntity)
351 *prog->time = cl.time;
352 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_TempEntity - prog->functions), CL_F_PARSE_TEMPENTITY);
363 void CL_VM_Parse_StuffCmd (const char *msg)
365 if(!csqc_loaded) //[515]: add more here
371 Cvar_SetQuick(&csqc_progcrc, "0");
372 csqc_progcrc.flags = 0;
373 Cmd_ExecuteString (msg, src_command);
374 csqc_progcrc.flags = CVAR_READONLY;
377 if(!csqc_loaded || !CSQC_Parse_StuffCmd)
383 *prog->time = cl.time;
384 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(msg);
385 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_StuffCmd - prog->functions), CL_F_PARSE_STUFFCMD);
389 static void CL_VM_Parse_Print (const char *msg)
392 *prog->time = cl.time;
393 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(msg);
394 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_Print - prog->functions), CL_F_PARSE_PRINT);
398 void CSQC_AddPrintText (const char *msg)
401 if(!csqc_loaded || !CSQC_Parse_Print)
407 if(msg[i] != '\n' && msg[i] != '\r')
409 if(strlen(csqc_printtextbuf)+i >= CSQC_PRINTBUFFERLEN)
411 CL_VM_Parse_Print(csqc_printtextbuf);
412 csqc_printtextbuf[0] = 0;
415 strcat(csqc_printtextbuf, msg);
418 strcat(csqc_printtextbuf, msg);
419 CL_VM_Parse_Print(csqc_printtextbuf);
420 csqc_printtextbuf[0] = 0;
423 void CL_VM_Parse_CenterPrint (const char *msg)
425 if(!csqc_loaded || !CSQC_Parse_CenterPrint)
427 SCR_CenterPrint((char*)msg);
431 *prog->time = cl.time;
432 PRVM_G_INT(OFS_PARM0) = PRVM_SetEngineString(msg);
433 PRVM_ExecuteProgram ((func_t)(CSQC_Parse_CenterPrint - prog->functions), CL_F_PARSE_CENTERPRINT);
437 float CL_VM_Event (float event) //[515]: needed ? I'd say "YES", but don't know for what :D
440 if(!csqc_loaded || !CSQC_Event)
443 *prog->time = cl.time;
444 PRVM_G_FLOAT(OFS_PARM0) = event;
445 PRVM_ExecuteProgram ((func_t)(CSQC_Event - prog->functions), CL_F_EVENT);
451 void CSQC_ReadEntities (void)
453 unsigned short entnum, oldself, realentnum;
455 *prog->time = cl.time;
456 oldself = prog->globals.client->self;
459 entnum = MSG_ReadShort();
462 realentnum = entnum & 0x7FFF;
463 prog->globals.client->self = csqc_sv2csqcents[realentnum];
466 if(prog->globals.client->self)
468 PRVM_ExecuteProgram((func_t)(CSQC_Ent_Remove - prog->functions), CL_F_ENT_REMOVE);
469 csqc_sv2csqcents[realentnum] = 0;
472 Con_Printf("Smth bad happens in csqc...\n"); //[515]: never happens ?
476 if(!prog->globals.client->self)
479 ed = PRVM_ED_Alloc();
480 ed->fields.client->entnum = realentnum;
481 prog->globals.client->self = csqc_sv2csqcents[realentnum] = PRVM_EDICT_TO_PROG(ed);
482 PRVM_G_FLOAT(OFS_PARM0) = 1;
485 PRVM_G_FLOAT(OFS_PARM0) = 0;
486 PRVM_ExecuteProgram((func_t)(CSQC_Ent_Update - prog->functions), CL_F_ENT_UPDATE);
489 prog->globals.client->self = oldself;
493 void Cmd_ClearCsqcFuncs (void);
495 void CL_VM_Init (void)
500 memset(cl.csqc_model_precache, 0, sizeof(cl.csqc_model_precache));
501 memset(&cl.csqc_vidvars, true, sizeof(csqc_vidvars_t));
503 if(!FS_FileExists(csqc_progname.string))
505 if(!sv.active && csqc_progcrc.integer)
507 Con_Printf("CL_VM_Init: server requires CSQC, but \"%s\" wasn't found\n", csqc_progname.string);
513 if(!sv.active && !csqc_progcrc.integer) //[515]: because cheaters may use csqc while server didn't allowed it !
515 Con_Printf("CL_VM_Init: server didn't sent CSQC crc, so CSQC is disabled\n");
520 PRVM_InitProg(PRVM_CLIENTPROG);
522 // allocate the mempools
523 prog->progs_mempool = Mem_AllocPool(csqc_progname.string, 0, NULL);
524 prog->headercrc = CL_PROGHEADER_CRC;
525 prog->edictprivate_size = 0; // no private struct used
526 prog->name = csqc_progname.string;
527 prog->num_edicts = 1;
528 prog->limit_edicts = CL_MAX_EDICTS;
529 prog->extensionstring = vm_cl_extensions;
530 prog->builtins = vm_cl_builtins;
531 prog->numbuiltins = vm_cl_numbuiltins;
532 prog->init_cmd = VM_CL_Cmd_Init;
533 prog->reset_cmd = VM_CL_Cmd_Reset;
534 prog->error_cmd = CL_VM_Error;
536 PRVM_LoadProgs(csqc_progname.string, cl_numrequiredfunc, cl_required_func, 0, NULL);
538 if(!sv.active && !cls.demoplayback && prog->filecrc != (unsigned short)csqc_progcrc.integer)
540 Con_Printf("^1Your CSQC version differs from server's one (%i!=%i)\n", prog->filecrc, csqc_progcrc.integer);
548 Cvar_SetValueQuick(&csqc_progcrc, prog->filecrc);
549 Con_Printf("CSQC ^5loaded (crc=%i)\n", csqc_progcrc.integer);
553 CL_VM_Error("CSQC ^2failed to load\n");
559 csqc_mempool = Mem_AllocPool("CSQC", 0, NULL);
561 //[515]: optional fields & funcs
562 CL_VM_FindEdictFieldOffsets();
565 *prog->time = cl.time;
568 prog->globals.client->mapname = PRVM_SetEngineString(cl.worldmodel->name);
569 prog->globals.client->player_localentnum = cl.playerentity;
571 // call the prog init
572 PRVM_ExecuteProgram((func_t) (PRVM_ED_FindFunction(CL_F_INIT) - prog->functions), CL_F_INIT);
577 csqc_sv2csqcents = Mem_Alloc(csqc_mempool, MAX_EDICTS*sizeof(unsigned short));
578 memset(csqc_sv2csqcents, 0, MAX_EDICTS*sizeof(unsigned short));
580 cl.csqc_vidvars.drawcrosshair = false;
581 cl.csqc_vidvars.drawenginesbar = false;
584 ent = &cl.csqcentities[0];
585 // entire entity array was cleared, so just fill in a few fields
586 ent->state_current.active = true;
587 ent->render.model = cl.worldmodel = cl.model_precache[1];
588 ent->render.scale = 1; // some of the renderer still relies on scale
589 ent->render.alpha = 1;
590 ent->render.flags = RENDER_SHADOW | RENDER_LIGHT;
591 Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, 0, 0, 0, 0, 0, 0, 1);
592 Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
593 CL_BoundingBoxForEntity(&ent->render);
596 void CL_VM_ShutDown (void)
598 Cmd_ClearCsqcFuncs();
599 Cvar_SetValueQuick(&csqc_progcrc, 0);
603 *prog->time = cl.time;
604 PRVM_ExecuteProgram((func_t) (PRVM_ED_FindFunction(CL_F_SHUTDOWN) - prog->functions), CL_F_SHUTDOWN);
607 Con_Print("CSQC ^1unloaded\n");
609 Mem_FreePool(&csqc_mempool);