/*
+--------+
|Controls|
+--------+-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+
| Scratch                                      Http://www.admdev.com/scratch |
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+
| Allows player movement to be tampered with and is linked with the          |
| Corporal's legs.						                       |
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+
*/

// CONTROLS.QC
// Code by KrimZon to get the player controls to prove it can be done.
// I am basically reversing the physics code to get the player's controls.
// version 0.1 - only approximate atm

.float forwardmove;
.float sidemove; //right is positive here

//call this every player command frame
void() Controls_Think =
{
	local vector delta_v, ang, fwdvec;
	local float spd;

	delta_v = self.velocity;
	delta_v_z = 0; //dont consider gravity
	spd = vlen(delta_v);
	delta_v = normalize(delta_v);
	ang = self.v_angle;
	ang_x = 0; //only consider player yaw

	makevectors(ang);

	self.forwardmove = v_forward * delta_v;
	self.sidemove = v_right * delta_v;

	self.forwardmove = self.forwardmove * spd;
	self.sidemove = self.sidemove * spd;
};

/*
============
Corporal Legs
============
*/

void() tilt;
.float legsthink_finished;

void() LegsThink =
{
	local float below, above;

	if (world.model == "maps/start.bsp")
	{
		self.skin = self.owner.skin;
		self.colormap = self.owner.colormap;
		return;
	}

	below = self.owner.angles_y - 65;
	above = self.owner.angles_y + 65;

	if (!self.owner.netname)
		remove(self);
	if (intermission_running)
	{
		setmodel(self, string_null);
		self.frame = 0;
		self.think = SUB_Null;
		self.nextthink = -1;
	}
	else
		setmodel(self, "progs/corpbot.mdl");

	// knifed death
	if (self.owner.deadflag > 0) // If dead
	{
		if (self.owner.dethtype > 0)
			remove(self);

		if (self.legsthink_finished < time)
		{
			if (self.frame < 19)
				self.frame = 19;
			else
				self.frame = self.frame + 1;
			if (self.frame > 24)
				self.frame = 24;
			self.legsthink_finished = time + 0.1; // Animate at 0.1
		}
		self.angles_y = self.owner.angles_y;
		tilt();
	}
	else // If Alive
	{
		if (!self.owner.flags & FL_ONGROUND) // fell/jumped
		{
			if (self.legsthink_finished < time) // Animate at 0.1
			{
				if (self.frame < 25) self.frame = 25;
				if (self.frame < 36) self.frame = self.frame + 1;
				self.legsthink_finished = time + 0.03; // animate fast
			}
		}

		if (self.owner.sidemove || self.owner.forwardmove) // If my owner is moving
		{
			if (self.owner.flags & FL_ONGROUND)
			{
				if (self.legsthink_finished < time) // Animate at a given time
				{
					if (self.owner.forwardmove < 50) // Strafing
					{
						if (self.owner.sidemove < -50 || self.owner.sidemove > 50) // Side strafe
						{
							if (self.frame < 12)
								self.frame = 17;
							if (self.frame > 17)
								self.frame = 12;
							if (self.owner.sidemove < -50)
								self.frame = self.frame - 1;
							if (self.owner.sidemove > 50)
								self.frame = self.frame + 1;
							self.angles_y = self.owner.angles_y;
						}
					}
					if (self.owner.forwardmove < -50 && self.owner.sidemove < 50 && self.owner.sidemove > -50) // Backpedal
					{
						if (self.frame < 12) self.frame = self.frame + 1;
						else	self.frame = 6;
					}
					else if (self.owner.forwardmove >= 50) // Forward
					{
						if (self.frame < 6) self.frame = self.frame + 1;
						else self.frame = 1;
					}

					if (self.owner.forwardmove > -50) // Given frame animation time
						self.legsthink_finished = time + 0.1;
					else
						self.legsthink_finished = time + 0.07;
	
					if (self.frame == 2 || self.frame == 4 || self.frame == 6 || self.frame == 10 || self.frame == 13)
						sound (self, CHAN_BODY, "player/step.wav", 1, ATTN_NORM);
				}
			}
			//Angle work
			if (self.owner.forwardmove < 350 && self.owner.sidemove < 50 && self.owner.sidemove > -50) // Forward
				self.angles_y = self.owner.angles_y;
			if (self.angles_y < below) // cap turning distance
				self.angles_y = below;
			else if (self.angles_y > above)
				self.angles_y = above;
		}
		else // If he's not moving
		{
			if (self.owner.flags & FL_ONGROUND)
			{
				if (self.cnt == 0)
					self.frame = 0;
				else if (self.cnt == 1) // play standing turn frame
				{
					if (self.legsthink_finished < time) // Animate at a given time
					{
						self.frame = 12;
						self.legsthink_finished = time + 0.1;
						self.cnt = 0;
					}
				}
			}

			if (self.angles_y < below || self.angles_y > above) // legs no longer turn with exact movement 
			{
				self.angles_y = self.owner.angles_y;
				self.cnt = 1; // flag to play turn animation				
			}
		}
		if (self.owner.health > 0)
		{
			if (self.owner.forwardmove > 135 && self.owner.sidemove < -50) // Strafe Up Left
				self.angles_y = self.owner.angles_y + 45;
			if (self.owner.forwardmove > 135 && self.owner.sidemove > 50) // Strafe Up Right
				self.angles_y = self.owner.angles_y - 45;
		}
	}

	self.skin = self.owner.skin;
	self.angles_x = 0;
	self.colormap = self.owner.colormap;
	self.origin = self.owner.origin;
	self.nextthink = time + 0.01;
};

void() CorpLegs =
{
	local entity legs;
		
	legs = spawn();
	legs.owner = self;
	legs.classname = "legs";
	legs.movetype  = MOVETYPE_NOCLIP;
	legs.solid     = SOLID_NOT;
	setmodel(legs, "progs/corpbot.mdl");
	setsize(legs, '0 0 0', '0 0 0');
	legs.think = LegsThink;
	legs.nextthink = time + 0.01;
	setorigin(legs, self.origin);
	legs.th_die = SUB_Remove;
	legs.angles_y = self.angles_y;
};