+ // ent is the view entity (visible when out of body)
+ ent = &cl_entities[cl.viewentity];
+ if (cl.intermission)
+ {
+ // entity is a fixed camera, just copy the matrix
+ Matrix4x4_Copy(&r_refdef.viewentitymatrix, &ent->render.matrix);
+ Matrix4x4_Copy(&viewmodelmatrix, &ent->render.matrix);
+ r_refdef.viewentitymatrix.m[2][3] += cl.stats[STAT_VIEWHEIGHT];
+ viewmodelmatrix.m[2][3] += cl.stats[STAT_VIEWHEIGHT];
+ }
+ else
+ {
+ // player can look around, so take the origin from the entity,
+ // and the angles from the input system
+ Matrix4x4_OriginFromMatrix(&ent->render.matrix, vieworg);
+ VectorCopy(cl.viewangles, viewangles);
+
+ // stair smoothing
+ //Con_Printf("cl.onground %i oldz %f newz %f\n", cl.onground, oldz, vieworg[2]);
+ if (cl.onground && oldz < vieworg[2])
+ {
+ oldz += (cl.time - cl.oldtime) * cl_stairsmoothspeed.value;
+ oldz = vieworg[2] = bound(vieworg[2] - 16, oldz, vieworg[2]);
+ }
+ else if (cl.onground && oldz > vieworg[2])
+ {
+ oldz -= (cl.time - cl.oldtime) * cl_stairsmoothspeed.value;
+ oldz = vieworg[2] = bound(vieworg[2], oldz, vieworg[2] + 16);
+ }
+ else
+ oldz = vieworg[2];
+
+ if (chase_active.value)
+ {
+ // observing entity from third person
+ vec_t camback, camup, dist, forward[3], chase_dest[3];
+
+ camback = bound(0, chase_back.value, 128);
+ if (chase_back.value != camback)
+ Cvar_SetValueQuick(&chase_back, camback);
+ camup = bound(-48, chase_up.value, 96);
+ if (chase_up.value != camup)
+ Cvar_SetValueQuick(&chase_up, camup);
+
+ // this + 22 is to match view_ofs for compatibility with older versions
+ camup += 22;
+
+ if (gamemode == GAME_GOODVSBAD2 && chase_stevie.integer)
+ {
+ // look straight down from high above
+ viewangles[0] = 90;
+ camback = 2048;
+ }
+ AngleVectors(viewangles, forward, NULL, NULL);
+
+ // trace a little further so it hits a surface more consistently (to avoid 'snapping' on the edge of the range)
+ dist = -camback - 8;
+ chase_dest[0] = vieworg[0] + forward[0] * dist;
+ chase_dest[1] = vieworg[1] + forward[1] * dist;
+ chase_dest[2] = vieworg[2] + forward[2] * dist + camup;
+ trace = CL_TraceBox(vieworg, vec3_origin, vec3_origin, chase_dest, true, NULL, SUPERCONTENTS_SOLID | SUPERCONTENTS_SKY, false);
+ VectorMAMAM(1, trace.endpos, 8, forward, 4, trace.plane.normal, vieworg);
+ }
+ else
+ {
+ // first person view from entity
+ // angles
+ if (cl.stats[STAT_HEALTH] <= 0 && gamemode != GAME_FNIGGIUM)
+ viewangles[ROLL] = 80; // dead view angle
+ VectorAdd(viewangles, cl.punchangle, viewangles);
+ viewangles[ROLL] += V_CalcRoll(cl.viewangles, cl.movement_velocity);
+ if (v_dmg_time > 0)
+ {
+ viewangles[ROLL] += v_dmg_time/v_kicktime.value*v_dmg_roll;
+ viewangles[PITCH] += v_dmg_time/v_kicktime.value*v_dmg_pitch;
+ v_dmg_time -= cl.frametime;
+ }
+ // origin
+ VectorAdd(vieworg, cl.punchvector, vieworg);
+ vieworg[2] += cl.stats[STAT_VIEWHEIGHT];
+ if (cl.stats[STAT_HEALTH] > 0 && cl_bob.value && cl_bobcycle.value)
+ {
+ double bob, cycle;
+ // LordHavoc: this code is *weird*, but not replacable (I think it
+ // should be done in QC on the server, but oh well, quake is quake)
+ // LordHavoc: figured out bobup: the time at which the sin is at 180
+ // degrees (which allows lengthening or squishing the peak or valley)
+ cycle = cl.time / cl_bobcycle.value;
+ cycle -= (int) cycle;
+ if (cycle < cl_bobup.value)
+ cycle = sin(M_PI * cycle / cl_bobup.value);
+ else
+ cycle = sin(M_PI + M_PI * (cycle-cl_bobup.value)/(1.0 - cl_bobup.value));
+ // bob is proportional to velocity in the xy plane
+ // (don't count Z, or jumping messes it up)
+ bob = sqrt(cl.movement_velocity[0]*cl.movement_velocity[0] + cl.movement_velocity[1]*cl.movement_velocity[1]) * cl_bob.value;
+ bob = bob*0.3 + bob*0.7*cycle;
+ vieworg[2] += bound(-7, bob, 4);
+ }
+ }
+ // calculate a view matrix for rendering the scene
+ if (v_idlescale.value)
+ Matrix4x4_CreateFromQuakeEntity(&r_refdef.viewentitymatrix, vieworg[0], vieworg[1], vieworg[2], viewangles[0] + v_idlescale.value * sin(cl.time*v_ipitch_cycle.value) * v_ipitch_level.value, viewangles[1] + v_idlescale.value * sin(cl.time*v_iyaw_cycle.value) * v_iyaw_level.value, viewangles[2] + v_idlescale.value * sin(cl.time*v_iroll_cycle.value) * v_iroll_level.value, 1);
+ else
+ Matrix4x4_CreateFromQuakeEntity(&r_refdef.viewentitymatrix, vieworg[0], vieworg[1], vieworg[2], viewangles[0], viewangles[1], viewangles[2] + v_idlescale.value * sin(cl.time*v_iroll_cycle.value) * v_iroll_level.value, 1);
+ // calculate a viewmodel matrix for use in view-attached entities
+ Matrix4x4_CreateFromQuakeEntity(&viewmodelmatrix, vieworg[0], vieworg[1], vieworg[2], viewangles[0], viewangles[1], viewangles[2], 0.3);
+ }