Advertisement
techforce

DP_SVPLAYERPHYSICS.QC

Aug 20th, 2011
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. float lastclientthink, sv_maxspeed, sv_friction, sv_accelerate, sv_stopspeed;
  2. float sv_edgefriction, cl_rollangle, cl_divspeed;
  3.  
  4. // LordHavoc:
  5. // Highly optimized port of SV_ClientThink from engine code to QuakeC.
  6. // No behavior changes!  This code is much shorter and probably faster than
  7. // the engine code :)
  8.  
  9. // note that darkplaces engine will call this function if it finds it,
  10. // so modify for your own mods and enjoy...
  11.  
  12. // note also, this code uses some builtin functions from dpextensions.qc
  13. // (included with darkplaces engine releases)
  14.  
  15. void() SV_PlayerPhysics =
  16. {
  17.     local vector wishvel, wishdir, v;
  18.     local float wishspeed, f;
  19.  
  20.     if (self.movetype == MOVETYPE_NONE)
  21.         return;
  22.  
  23.     if (self.punchangle != '0 0 0')
  24.     {
  25.         f = vlen(self.punchangle) - 10 * frametime;
  26.         if (f > 0)
  27.             self.punchangle = normalize(self.punchangle) * f;
  28.         else
  29.             self.punchangle = '0 0 0';
  30.     }
  31.  
  32.     // if dead, behave differently
  33.     if (self.health <= 0)
  34.         return;
  35.  
  36.     if (time != lastclientthink)
  37.     {
  38.         lastclientthink = time;
  39.         sv_maxspeed = cvar("sv_maxspeed");
  40.         sv_friction = cvar("sv_friction");
  41.         sv_accelerate = cvar("sv_accelerate");
  42.         sv_stopspeed = cvar("sv_stopspeed");
  43.         sv_edgefriction = cvar("edgefriction");
  44.         // LordHavoc: this * 4 is an optimization
  45.         cl_rollangle = cvar("cl_rollangle") * 4;
  46.         // LordHavoc: this 1 / is an optimization
  47.         cl_divspeed = 1 / cvar("cl_rollspeed");
  48.     }
  49.  
  50.     // show 1/3 the pitch angle and all the roll angle
  51.     self.angles_z = bound(-1, self.velocity * v_right * cl_divspeed, 1) * cl_rollangle;
  52.     if (!self.fixangle)
  53.     {
  54.         self.angles_x = (self.v_angle_x + self.punchangle_x) * -0.333;
  55.         self.angles_y = self.v_angle_y + self.punchangle_y;
  56.     }
  57.  
  58.     if (self.flags & FL_WATERJUMP )
  59.     {
  60.         self.velocity_x = self.movedir_x;
  61.         self.velocity_y = self.movedir_y;
  62.         if (time > self.teleport_time || self.waterlevel == 0)
  63.         {
  64.             self.flags = self.flags - (self.flags & FL_WATERJUMP);
  65.             self.teleport_time = 0;
  66.         }
  67.         return;
  68.     }
  69.  
  70.     makevectors(self.v_angle);
  71.  
  72.     // swim
  73.     if (self.waterlevel >= 2)
  74.     if (self.movetype != MOVETYPE_NOCLIP)
  75.     {
  76.         self.flags = self.flags - (self.flags & FL_ONGROUND);
  77.  
  78.         if (self.movement == '0 0 0')
  79.             wishvel = '0 0 -60'; // drift towards bottom
  80.         else
  81.             wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
  82.  
  83.         wishspeed = vlen (wishvel);
  84.         if (wishspeed > sv_maxspeed)
  85.             wishspeed = sv_maxspeed;
  86.  
  87.         wishspeed = wishspeed * 0.7;
  88.  
  89.         // water friction
  90.         if (self.velocity != '0 0 0')
  91.         {
  92.             f = vlen(self.velocity) * (1 - frametime * sv_friction);
  93.             if (f > 0)
  94.                 self.velocity = normalize(self.velocity) * f;
  95.             else
  96.                 self.velocity = '0 0 0';
  97.         }
  98.         else
  99.             f = 0;
  100.  
  101.         // water acceleration
  102.         if (wishspeed <= f)
  103.             return;
  104.  
  105.         f = min(wishspeed - f, sv_accelerate * wishspeed * frametime);
  106.         self.velocity = self.velocity + normalize(wishvel) * f;
  107.         return;
  108.     }
  109.  
  110.     // hack to not let you back into teleporter
  111.     if (time < self.teleport_time && self.movement_x < 0)
  112.         wishvel = v_right * self.movement_y;
  113.     else
  114.     {
  115.         // LordHavoc: this NOCLIP check is a fix for the 'not moving while looking straight up/down when noclipping' bug in Quake
  116.         if ((self.flags & FL_ONGROUND) || (self.movetype == MOVETYPE_NOCLIP))
  117.             makevectors (self.v_angle_y * '0 1 0');
  118.  
  119.         wishvel = v_forward * self.movement_x + v_right * self.movement_y;
  120.     }
  121.  
  122.     if (self.movetype != MOVETYPE_WALK)
  123.         wishvel_z = self.movement_z;
  124.     else
  125.         wishvel_z = 0;
  126.  
  127.     wishdir = normalize(wishvel);
  128.     wishspeed = vlen(wishvel);
  129.     if (wishspeed > sv_maxspeed)
  130.         wishspeed = sv_maxspeed;
  131.  
  132.     if (self.movetype == MOVETYPE_NOCLIP) // noclip
  133.     {
  134.         self.flags = self.flags - (self.flags & FL_ONGROUND);
  135.         self.velocity = wishdir * wishspeed;
  136.     }
  137.     else if (self.flags & FL_ONGROUND) // walking
  138.     {
  139.         // friction
  140.         if (self.velocity_x || self.velocity_y)
  141.         {
  142.             v = self.velocity;
  143.             v_z = 0;
  144.             f = vlen(v);
  145.  
  146.             // if the leading edge is over a dropoff, increase friction
  147.             v = self.origin + normalize(v) * 16 + '0 0 1' * self.mins_z;
  148.  
  149.             traceline(v, v + '0 0 -34', TRUE, self);
  150.  
  151.             // apply friction
  152.             if (trace_fraction == 1.0)
  153.             {
  154.                 if (f < sv_stopspeed)
  155.                     f = 1 - frametime * (sv_stopspeed / f) * sv_friction * sv_edgefriction;
  156.                 else
  157.                     f = 1 - frametime * sv_friction * sv_edgefriction;
  158.             }
  159.             else
  160.             {
  161.                 if (f < sv_stopspeed)
  162.                     f = 1 - frametime * (sv_stopspeed / f) * sv_friction;
  163.                 else
  164.                     f = 1 - frametime * sv_friction;
  165.             }
  166.  
  167.             if (f < 0)
  168.                 self.velocity = '0 0 0';
  169.             else
  170.                 self.velocity = self.velocity * f;
  171.         }
  172.  
  173.         // acceleration
  174.         f = wishspeed - (self.velocity * wishdir);
  175.         if (f > 0)
  176.             self.velocity = self.velocity + wishdir * min(f, sv_accelerate * frametime * wishspeed);
  177.     }
  178.     else // airborn
  179.     {
  180.         if (wishspeed < 30)
  181.             f = wishspeed - (self.velocity * wishdir);
  182.         else
  183.             f = 30 - (self.velocity * wishdir);
  184.         if (f > 0)
  185.             self.velocity = self.velocity + wishdir * (min(f, sv_accelerate) * wishspeed * frametime);
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement