added DP_ASINACOSATANATAN2TAN extension which adds common trig functions missing...
[xonotic/darkplaces.git] / clvm_cmds.c
index 4a4dff9..4e284d2 100644 (file)
@@ -49,6 +49,7 @@ char *vm_cl_extensions =
 "DP_MONSTERWALK "
 "DP_MOVETYPEBOUNCEMISSILE "
 "DP_MOVETYPEFOLLOW "
+"DP_QC_ASINACOSATANATAN2TAN "
 "DP_QC_CHANGEPITCH "
 "DP_QC_COPYENTITY "
 "DP_QC_CVAR_STRING "
@@ -760,15 +761,12 @@ void VM_CL_getlight (void)
 
 //============================================================================
 //[515]: SCENE MANAGER builtins
-void V_CalcRefdef (void);//view.c
-void CSQC_R_ClearScreen (void);//gl_rmain.c
-void CSQC_R_RenderScene (void);//gl_rmain.c
-void CSQC_AddEntity (int n);//csprogs.c
-void CSQC_ClearCSQCEntities (void);
+extern void V_CalcRefdef (void);//view.c
+extern qboolean CSQC_AddRenderEdict (prvm_edict_t *ed);//csprogs.c
+extern void CSQC_ClearCSQCEntities (void);//csprogs.c
 
 matrix4x4_t csqc_listenermatrix;
 qboolean csqc_usecsqclistener = false, csqc_frame = false;//[515]: per-frame
-qboolean csqc_onground;
 
 static void CSQC_R_RecalcView (void)
 {
@@ -776,7 +774,7 @@ static void CSQC_R_RecalcView (void)
        viewmodelmatrix = identitymatrix;
        r_view.matrix = identitymatrix;
        Matrix4x4_CreateFromQuakeEntity(&r_view.matrix, csqc_origin[0], csqc_origin[1], csqc_origin[2], csqc_angles[0], csqc_angles[1], csqc_angles[2], 1);
-       Matrix4x4_CreateFromQuakeEntity(&viewmodelmatrix, csqc_origin[0], csqc_origin[1], csqc_origin[2], csqc_angles[0], csqc_angles[1], csqc_angles[2], 0.3);
+       Matrix4x4_CreateFromQuakeEntity(&viewmodelmatrix, csqc_origin[0], csqc_origin[1], csqc_origin[2], csqc_angles[0], csqc_angles[1], csqc_angles[2], cl_viewmodel_scale.value);
 }
 
 //#300 void() clearscene (EXT_CSQC)
@@ -784,23 +782,46 @@ void VM_R_ClearScene (void)
 {
        VM_SAFEPARMCOUNT(0, VM_R_ClearScene);
 //     CSQC_R_RecalcView();
-       if(csqc_frame)
-               CSQC_ClearCSQCEntities();
-       CSQC_R_ClearScreen();
+       CSQC_ClearCSQCEntities();
 }
 
 //#301 void(float mask) addentities (EXT_CSQC)
+extern void CSQC_Predraw (prvm_edict_t *ed);//csprogs.c
+extern void CSQC_Think (prvm_edict_t *ed);//csprogs.c
 void VM_R_AddEntities (void)
 {
+       int                     i, drawmask;
+       prvm_edict_t *ed;
        VM_SAFEPARMCOUNT(1, VM_R_AddEntities);
-       csqc_drawmask = (int)PRVM_G_FLOAT(OFS_PARM0);
+       drawmask = (int)PRVM_G_FLOAT(OFS_PARM0);
+       CSQC_RelinkAllEntities(drawmask);
+
+       *prog->time = cl.time;
+       for(i=1;i<prog->num_edicts;i++)
+       {
+               ed = &prog->edicts[i];
+               if(ed->priv.required->free)
+                       continue;
+               VectorAdd(ed->fields.client->origin, ed->fields.client->mins, ed->fields.client->absmin);
+               VectorAdd(ed->fields.client->origin, ed->fields.client->maxs, ed->fields.client->absmax);
+               CSQC_Think(ed);
+               if(ed->priv.required->free)
+                       continue;
+               // note that for RF_USEAXIS entities, Predraw sets v_forward/v_right/v_up globals that are read by CSQC_AddRenderEdict
+               CSQC_Predraw(ed);
+               if(ed->priv.required->free)
+                       continue;
+               if(!((int)ed->fields.client->drawmask & drawmask))
+                       continue;
+               CSQC_AddRenderEdict(ed);
+       }
 }
 
 //#302 void(entity ent) addentity (EXT_CSQC)
 void VM_R_AddEntity (void)
 {
        VM_SAFEPARMCOUNT(1, VM_R_AddEntity);
-       CSQC_AddEntity(PRVM_NUM_FOR_EDICT(PRVM_G_EDICT(OFS_PARM0)));
+       CSQC_AddRenderEdict(PRVM_G_EDICT(OFS_PARM0));
 }
 
 //#303 float(float property, ...) setproperty (EXT_CSQC)
@@ -900,14 +921,7 @@ void VM_R_SetView (void)
 void VM_R_RenderScene (void) //#134
 {
        VM_SAFEPARMCOUNT(0, VM_R_RenderScene);
-
-       if(csqc_frame)
-       {
-               CSQC_RelinkCSQCEntities();
-               CSQC_RelinkAllEntities(csqc_drawmask);
-       }
-
-       CSQC_R_RenderScene();
+       R_RenderView();
 }
 
 //#305 void(vector org, float radius, vector lightcolours) adddynamiclight (EXT_CSQC)
@@ -1269,7 +1283,7 @@ void VM_CL_registercmd (void)
        if(!Cmd_Exists(PRVM_G_STRING(OFS_PARM0)))
        {
                size_t alloclen;
-               
+
                alloclen = strlen(PRVM_G_STRING(OFS_PARM0)) + 1;
                t = (char *)Z_Malloc(alloclen);
                memcpy(t, PRVM_G_STRING(OFS_PARM0), alloclen);
@@ -1296,7 +1310,7 @@ void VM_CL_playernum (void)
 //#355 float() cl_onground (EXT_CSQC)
 void VM_CL_onground (void)
 {
-       PRVM_G_FLOAT(OFS_RETURN) = csqc_onground;
+       PRVM_G_FLOAT(OFS_RETURN) = cl.onground;
 }
 
 //#360 float() readbyte (EXT_CSQC)
@@ -2058,16 +2072,10 @@ int CL_GetTagMatrix (matrix4x4_t *out, prvm_edict_t *ent, int tagindex)
                                val->_float = 1;
                        Matrix4x4_CreateFromQuakeEntity(&entitymatrix, ent->fields.client->origin[0], ent->fields.client->origin[1], ent->fields.client->origin[2], -ent->fields.client->angles[0], ent->fields.client->angles[1], ent->fields.client->angles[2], val->_float);
                        Matrix4x4_Concat(out, &entitymatrix, &tagmatrix);
-                       out->m[0][3] = entitymatrix.m[0][3] + val->_float*(entitymatrix.m[0][0]*tagmatrix.m[0][3] + entitymatrix.m[0][1]*tagmatrix.m[1][3] + entitymatrix.m[0][2]*tagmatrix.m[2][3]);
-                       out->m[1][3] = entitymatrix.m[1][3] + val->_float*(entitymatrix.m[1][0]*tagmatrix.m[0][3] + entitymatrix.m[1][1]*tagmatrix.m[1][3] + entitymatrix.m[1][2]*tagmatrix.m[2][3]);
-                       out->m[2][3] = entitymatrix.m[2][3] + val->_float*(entitymatrix.m[2][0]*tagmatrix.m[0][3] + entitymatrix.m[2][1]*tagmatrix.m[1][3] + entitymatrix.m[2][2]*tagmatrix.m[2][3]);
                        Matrix4x4_Copy(&tagmatrix, out);
 
                        // finally transformate by matrix of tag on parent entity
                        Matrix4x4_Concat(out, &attachmatrix, &tagmatrix);
-                       out->m[0][3] = attachmatrix.m[0][3] + attachmatrix.m[0][0]*tagmatrix.m[0][3] + attachmatrix.m[0][1]*tagmatrix.m[1][3] + attachmatrix.m[0][2]*tagmatrix.m[2][3];
-                       out->m[1][3] = attachmatrix.m[1][3] + attachmatrix.m[1][0]*tagmatrix.m[0][3] + attachmatrix.m[1][1]*tagmatrix.m[1][3] + attachmatrix.m[1][2]*tagmatrix.m[2][3];
-                       out->m[2][3] = attachmatrix.m[2][3] + attachmatrix.m[2][0]*tagmatrix.m[0][3] + attachmatrix.m[2][1]*tagmatrix.m[1][3] + attachmatrix.m[2][2]*tagmatrix.m[2][3];
                        Matrix4x4_Copy(&tagmatrix, out);
 
                        ent = attachent;
@@ -2085,9 +2093,6 @@ int CL_GetTagMatrix (matrix4x4_t *out, prvm_edict_t *ent, int tagindex)
        // Alias models have inverse pitch, bmodels can't have tags, so don't check for modeltype...
        Matrix4x4_CreateFromQuakeEntity(&entitymatrix, ent->fields.client->origin[0], ent->fields.client->origin[1], ent->fields.client->origin[2], -ent->fields.client->angles[0], ent->fields.client->angles[1], ent->fields.client->angles[2], val->_float);
        Matrix4x4_Concat(out, &entitymatrix, &tagmatrix);
-       out->m[0][3] = entitymatrix.m[0][3] + val->_float*(entitymatrix.m[0][0]*tagmatrix.m[0][3] + entitymatrix.m[0][1]*tagmatrix.m[1][3] + entitymatrix.m[0][2]*tagmatrix.m[2][3]);
-       out->m[1][3] = entitymatrix.m[1][3] + val->_float*(entitymatrix.m[1][0]*tagmatrix.m[0][3] + entitymatrix.m[1][1]*tagmatrix.m[1][3] + entitymatrix.m[1][2]*tagmatrix.m[2][3]);
-       out->m[2][3] = entitymatrix.m[2][3] + val->_float*(entitymatrix.m[2][0]*tagmatrix.m[0][3] + entitymatrix.m[2][1]*tagmatrix.m[1][3] + entitymatrix.m[2][2]*tagmatrix.m[2][3]);
 
        if ((val = PRVM_GETEDICTFIELDVALUE(ent, csqc_fieldoff_renderflags)) && (RF_VIEWMODEL & (int)val->_float))
        {// RENDER_VIEWMODEL magic
@@ -2099,9 +2104,6 @@ int CL_GetTagMatrix (matrix4x4_t *out, prvm_edict_t *ent, int tagindex)
 
                Matrix4x4_CreateFromQuakeEntity(&entitymatrix, csqc_origin[0], csqc_origin[1], csqc_origin[2], csqc_angles[0], csqc_angles[1], csqc_angles[2], val->_float);
                Matrix4x4_Concat(out, &entitymatrix, &tagmatrix);
-               out->m[0][3] = entitymatrix.m[0][3] + val->_float*(entitymatrix.m[0][0]*tagmatrix.m[0][3] + entitymatrix.m[0][1]*tagmatrix.m[1][3] + entitymatrix.m[0][2]*tagmatrix.m[2][3]);
-               out->m[1][3] = entitymatrix.m[1][3] + val->_float*(entitymatrix.m[1][0]*tagmatrix.m[0][3] + entitymatrix.m[1][1]*tagmatrix.m[1][3] + entitymatrix.m[1][2]*tagmatrix.m[2][3]);
-               out->m[2][3] = entitymatrix.m[2][3] + val->_float*(entitymatrix.m[2][0]*tagmatrix.m[0][3] + entitymatrix.m[2][1]*tagmatrix.m[1][3] + entitymatrix.m[2][2]*tagmatrix.m[2][3]);
 
                /*
                // Cl_bob, ported from rendering code
@@ -2122,7 +2124,7 @@ int CL_GetTagMatrix (matrix4x4_t *out, prvm_edict_t *ent, int tagindex)
                        // (don't count Z, or jumping messes it up)
                        bob = sqrt(ent->fields.client->velocity[0]*ent->fields.client->velocity[0] + ent->fields.client->velocity[1]*ent->fields.client->velocity[1])*cl_bob.value;
                        bob = bob*0.3 + bob*0.7*cycle;
-                       out->m[2][3] += bound(-7, bob, 4);
+                       Matrix4x4_AdjustOrigin(out, 0, 0, bound(-7, bob, 4));
                }
                */
        }
@@ -2295,7 +2297,7 @@ static int Is_Text_Color (char c, char t)
        char c2 = c - (c & 128);
        char t2 = t - (t & 128);
 
-       if(c != '^' && c2 != '^')               return 0;
+       if(c != STRING_COLOR_TAG && c2 != STRING_COLOR_TAG)             return 0;
        if(t >= '0' && t <= '9')                a = 1;
        if(t2 >= '0' && t2 <= '9')              a = 1;
 /*     if(t >= 'A' && t <= 'Z')                a = 2;
@@ -2721,7 +2723,17 @@ VM_bufstr_get,                           // #466 string(float bufhandle, float string_index) bufstr_get
 VM_bufstr_set,                         // #467 void(float bufhandle, float string_index, string str) bufstr_set (DP_QC_STRINGBUFFERS)
 VM_bufstr_add,                         // #468 float(float bufhandle, string str, float order) bufstr_add (DP_QC_STRINGBUFFERS)
 VM_bufstr_free,                                // #469 void(float bufhandle, float string_index) bufstr_free (DP_QC_STRINGBUFFERS)
-e10, e10, e10                  // #470-499 (LordHavoc)
+NULL,                                          // #470
+VM_asin,                                       // #471 float(float s) VM_asin (DP_QC_ASINACOSATANATAN2TAN)
+VM_acos,                                       // #472 float(float c) VM_acos (DP_QC_ASINACOSATANATAN2TAN)
+VM_atan,                                       // #473 float(float t) VM_atan (DP_QC_ASINACOSATANATAN2TAN)
+VM_atan2,                                      // #474 float(float c, float s) VM_atan2 (DP_QC_ASINACOSATANATAN2TAN)
+VM_tan,                                                // #475 float(float a) VM_tan (DP_QC_ASINACOSATANATAN2TAN)
+NULL,                                          // #476
+NULL,                                          // #477
+NULL,                                          // #478
+NULL,                                          // #479
+e10, e10                       // #480-499 (LordHavoc)
 };
 
 const int vm_cl_numbuiltins = sizeof(vm_cl_builtins) / sizeof(prvm_builtin_t);