$cd id1/models/player_4
$origin 0 -6 24
$base base		
$skin skin

//
// running
//
$frame axrun1 axrun2 axrun3 axrun4 axrun5 axrun6

$frame rockrun1 rockrun2 rockrun3 rockrun4 rockrun5 rockrun6

//
// standing
//
$frame stand1 stand2 stand3 stand4 stand5

$frame axstnd1 axstnd2 axstnd3 axstnd4 axstnd5 axstnd6
$frame axstnd7 axstnd8 axstnd9 axstnd10 axstnd11 axstnd12


//
// pain
//
$frame axpain1 axpain2 axpain3 axpain4 axpain5 axpain6

$frame pain1 pain2 pain3 pain4 pain5 pain6


//
// death
//

$frame axdeth1 axdeth2 axdeth3 axdeth4 axdeth5 axdeth6
$frame axdeth7 axdeth8 axdeth9

$frame deatha1 deatha2 deatha3 deatha4 deatha5 deatha6 deatha7 deatha8
$frame deatha9 deatha10 deatha11

$frame deathb1 deathb2 deathb3 deathb4 deathb5 deathb6 deathb7 deathb8
$frame deathb9

$frame deathc1 deathc2 deathc3 deathc4 deathc5 deathc6 deathc7 deathc8
$frame deathc9 deathc10 deathc11 deathc12 deathc13 deathc14 deathc15

$frame deathd1 deathd2 deathd3 deathd4 deathd5 deathd6 deathd7
$frame deathd8 deathd9

$frame deathe1 deathe2 deathe3 deathe4 deathe5 deathe6 deathe7
$frame deathe8 deathe9

//
// attacks
//
$frame nailatt1 nailatt2

$frame light1 light2

$frame rockatt1 rockatt2 rockatt3 rockatt4 rockatt5 rockatt6

$frame shotatt1 shotatt2 shotatt3 shotatt4 shotatt5 shotatt6

$frame axatt1 axatt2 axatt3 axatt4 axatt5 axatt6

$frame axattb1 axattb2 axattb3 axattb4 axattb5 axattb6

$frame axattc1 axattc2 axattc3 axattc4 axattc5 axattc6

$frame axattd1 axattd2 axattd3 axattd4 axattd5 axattd6

$frame kick1 kick2

$frame yeah1 yeah2 yeah3 yeah4 yeah5 yeah6
//========================================================================

entity() FindGoal =
{
	local entity goal, found;
	
	goal = find(world, classname, "trigger_goal");
	while (goal)
	{
		if (goal.team_no != self.team_no)
			found = goal;
		goal = find(goal, classname, "trigger_goal");
	}
	
	return found;
};

entity() FindGoalToKeeper =
{
	local entity goal, found;
	
	goal = find(world, classname, "trigger_goal");
	while (goal)
	{
		if (goal.team_no == self.team_no)
			found = goal;
		goal = find(goal, classname, "trigger_goal");
	}
	
	return found;
};

void() B_Kick =
{
	local entity ball;
	local vector dir;
	local float r;
	
	ball = findradius(self.origin, 64);
	while (ball)
	{
		if (ball != self)
		if (ball.classname == "ball")
		if (infront(ball))
		{
			kick1 ();
			if (ball.flags & FL_ONGROUND)
				ball.flags = ball.flags - FL_ONGROUND;
			makevectors (self.angles);
			r = 700 + 400*random();
			dir = v_forward + crandom()*0.2*v_right + crandom()*0.2*v_up;
			dir = normalize(dir);
			ball.velocity = dir * r;
			ball.velocity_z = (r / 2) - 100;
			ball.avelocity_x = -400 + random()*800;
			ball.avelocity_y = -400 + random()*800;
			ball.avelocity_z = -400 + random()*800;
		}
		ball = ball.chain;
	}
};

void() bot_ai =
{
	local vector src, dir;
	local float dist;
	local entity keeper;
	
	self.nextthink = time + 0.1;
	
	if (self.walkframe >= 6)
		self.walkframe = 0;
	self.frame = $axrun1 + self.walkframe;
	self.walkframe = self.walkframe + 1;
	
	self.gol = FindGoal ();
	self.goalentity = self.bol;
	
	src = self.origin - '0 0 20';
	dist = vlen(self.bol.origin - src);
	
	dir = normalize(self.bol.origin - src);
	
	traceline (src, src + dir*dist, FALSE, self);
	
	if (walkmove(self.angles_y, 14 + 8*random()) && trace_fraction > 0.8)
		self.angles_y = vectoyaw(self.bol.origin - self.origin);
	else
		movetogoal (14 + 8*random());
	
	if (vlen(self.bol.origin - self.origin) < 140 && self.kick_time < time)
	{
		if (self.bol.origin_z > self.origin_z + 40)
		{
			if (self.flags & FL_ONGROUND)
			{
				self.flags = self.flags - FL_ONGROUND;
				makevectors (self.angles);
				self.velocity = v_forward * (320 + random()*130);
				self.velocity_z = self.velocity_z + 350;
			}
		}
	}
	
	/*
		makevectors (self.angles);
		traceline (self.origin, self.origin + v_forward*10000, FALSE, self);
		
		keeper = findradius(trace_endpos, 300);
		while (keeper)
		{
			if (keeper != self)
			if (keeper.team_no != self.team_no)
			if (keeper.classname == "bot")
			if (keeper.is_keeper)
				B_Kick ();
			keeper = keeper.chain;
		}
	*/
};

string() Names =
{
	local float r;
	
	r = floor(random()*35);
	if (r == 0)
		return "Defederico";
	if (r == 1)
		return "Douglas";
	if (r == 2)
		return "Diego Souza";
	if (r == 3)
		return "Felipe";
	if (r == 4)
		return "Rogerio Ceni";
	if (r == 5)
		return "Hugo";
	if (r == 6)
		return "Ronaldo";
	if (r == 7)
		return "Richarlyson";
	if (r == 8)
		return "Obina";
	if (r == 9)
		return "Figueroa";
	if (r == 10)
		return "Armero";
	if (r == 11)
		return "Cleiton Xavier";
	if (r == 12)
		return "Vagner Love";
	if (r == 13)
		return "Souza";
	if (r == 14)
		return "Jumar";
	if (r == 15)
		return "Lenny";
	if (r == 16)
		return "Marcos";
	if (r == 17)
		return "Mauricio Ramos";
	if (r == 18)
		return "Sandro Silva";
	if (r == 19)
		return "Edmilson";
	if (r == 20)
		return "Valdivia";
	if (r == 21)
		return "Adriano";
	if (r == 22)
		return "Marlos";
	if (r == 23)
		return "Miranda";
	if (r == 24)
		return "Washington";
	if (r == 25)
		return "Marquinhos";
	if (r == 26)
		return "Deyvid Saconni";
	if (r == 27)
		return "Deola";
	if (r == 28)
		return "Bruno";
	if (r == 29)
		return "Diego Cavalieri";
	if (r == 30)
		return "Dagoberto";
	if (r == 31)
		return "Borges";
	if (r == 32)
		return "Madson";
	if (r == 33)
		return "Ibson";
	if (r == 34)
		return "Nilmar";
	
	return "player";
};

float(string name) UsedName =
{
	local entity e;
	
	e = find(world, classname, "bot");
	while (e)
	{
		if (e != self && e.netname == name)
			return TRUE;
		e = find(e, classname, "bot");
	}
	
	return FALSE;
};

void() GetName =
{
	do
	{
		self.netname = Names ();
	} while (UsedName(self.netname));
};

void(float t) AddBot =
{
	local entity bot, spot, oself;
	
	spot = FindTeamSpawn (t);
	
	bot = spawn ();
	bot.classname = "bot";
	
	oself = self;
	self = bot;
	GetName ();
	self = oself;
	
	bot.touch = player_touch;
	bot.health = 100;
	bot.movetype = MOVETYPE_STEP;
	bot.solid = SOLID_SLIDEBOX;
	bot.team_no = bot.skin = t;
	
	bot.bol = find(world, classname, "ball");
	
	bot.origin = bot.oldorigin = spot.origin + '0 0 1';
	bot.angles = spot.angles;
	bot.yaw_speed = 120;
	bot.ideal_yaw = bot.angles * '0 1 0';
	
	bot.nextthink = time + 0.1;
	bot.think = bot_ai;
	
	setmodel (bot, "progs/player.mdl");
	setsize (bot, '-16 -16 -24', '16 16 32');
};


void() tackle1 = [$deathd1, tackle2] {self.nextthink = time + 0.05;};
void() tackle2 = [$deathd2, tackle3] {self.nextthink = time + 0.05;};
void() tackle3 = [$deathd3, tackle4] {self.nextthink = time + 0.05;};
void() tackle4 = [$deathd4, tackle5] {self.nextthink = time + 0.05;};
void() tackle5 = [$deathd5, tackle6] {self.nextthink = time + 0.05;};
void() tackle6 = [$deathd6, tackle7] {self.nextthink = time + 0.05;};
void() tackle7 = [$deathd7, tackle8] {self.nextthink = time + 0.05;};
void() tackle8 = [$deathd8, tackle9] {self.nextthink = time + 0.05;};
void() tackle9 = [$deathd9, getup1] {self.nextthink = time + 0.05;};
	
void() getup1 = [$deathd8, getup2] {self.nextthink = time + 0.03;};
void() getup2 = [$deathd7, getup3] {self.nextthink = time + 0.03;};
void() getup3 = [$deathd6, getup4] {self.nextthink = time + 0.03;};
void() getup4 = [$deathd5, getup5] {self.nextthink = time + 0.03;};
void() getup5 = [$deathd4, getup6] {self.nextthink = time + 0.03;};
void() getup6 = [$deathd3, getup7] {self.nextthink = time + 0.03;};
void() getup7 = [$deathd2, getup8] {self.nextthink = time + 0.03;};
void() getup8 = [$deathd1, keeper_ai] {self.nextthink = time + 0.03;};

void() yeah1 = [$yeah1, yeah2]
{
	if (self.flags & FL_ONGROUND)
	{
		self.flags = self.flags - FL_ONGROUND;
		self.velocity_z = 350;
	}
};
void() yeah2 = [$yeah2, yeah3] {};
void() yeah3 = [$yeah3, yeah4] {};
void() yeah4 = [$yeah4, yeah5] {};
void() yeah5 = [$yeah5, yeah6] {};
void() yeah6 = [$yeah6, yeah7] {};
void() yeah7 = [$yeah5, yeah8] {};
void() yeah8 = [$yeah4, yeah9] {};
void() yeah9 = [$yeah3, yeah10] {};
void() yeah10 = [$yeah2, yeah10]
{
	if (self.is_keeper)
		self.think = keeper_ai;
	else
		self.think = bot_ai;
};

void() CheckLedge =
{
	local vector spot;
	
	if (!(self.flags & FL_ONGROUND))
		return;
	
	if (random() < 0.8)
		return;
	
	makevectors (self.angles);
	spot = self.origin + v_forward*60;
	spot = spot - '0 0 35';
	if (pointcontents(spot) == CONTENT_EMPTY)
	{
		self.flags = self.flags - FL_ONGROUND;
		self.velocity = v_forward * 320;
		self.velocity_z = self.velocity_z + 270;
	}
};

float(entity e) SomeonesComing =
{
	local entity head, selected;
	
	head = findradius(e.origin, 600);
	while (head)
	{
		if (head != e)
		if (head.classname == "player" || head.classname == "bot")
		if (head.team_no != e.team_no)
		if (head.kick_time > time)	// probably has the ball
			selected = head;
		head = head.chain;
	}
	
	if (selected)
		return TRUE;
	
	return FALSE;
};

void() keeper_ai =
{
	local vector dir, org, ang;
	local float vel, yaw;
	
	self.nextthink = time + 0.1;
	
	CheckLedge ();
	
	ang = vectoangles(self.gol.movedir);
	makevectors (ang);
	org = self.gol.real_origin + v_forward*150;
	
	if (vlen(org - self.origin) > 450)
	{
		if (self.walkframe >= 6)
			self.walkframe = 0;
		self.frame = $axrun1 + self.walkframe;
		self.walkframe = self.walkframe + 1;
		yaw = vectoyaw (org - self.origin);
		if (!walkmove (yaw, 14 + 8*random()))
		{
			if (!walkmove(self.angles_y, 14 + 8*random()))
				self.angles_y = self.angles_y - 45;
		}
		return;
	}
	
	if (vlen(self.bol.origin - self.origin) > 350 || self.kick_time > time)
	{
		if (vlen(org - self.origin) > 64)
		{
			if (self.walkframe >= 6)
				self.walkframe = 0;
			self.frame = $axrun1 + self.walkframe;
			self.walkframe = self.walkframe + 1;
			walkmove (vectoyaw(org - self.origin), 14 + 8*random());
		}
		else
		{
			if (self.walkframe >= 12)
				self.walkframe = 0;
			self.frame = $axstnd1 + self.walkframe;
			self.walkframe = self.walkframe + 1;	
		}
	}
	else
	{
		if (self.walkframe >= 6)
			self.walkframe = 0;
		self.frame = $axrun1 + self.walkframe;
		self.walkframe = self.walkframe + 1;
		
		self.angles_y = vectoyaw(self.bol.origin - self.origin);
		walkmove (self.angles_y, 14 + 8*random());
		
		if (vlen(self.bol.origin - self.origin) < 320)
		{
			if (self.kick_time < time)
			{
				if (self.flags & FL_ONGROUND)
				{
					self.flags = self.flags - FL_ONGROUND;
					if (vlen(self.bol.origin - self.origin) < 320 && vlen(self.bol.velocity) > 825)
					{
						tackle1 ();
						self.angles_y = vectoyaw(self.bol.origin - self.origin);
						makevectors (self.angles);
						vel = self.bol.velocity*v_right;
						if (self.bol.velocity*v_right > 0)
							self.velocity = v_right * vel;
						else
							self.velocity = v_right * vel;
						self.velocity_z = self.velocity_z + 300 + 100*random();
					}
					else
					{
						if (vlen(self.bol.origin - self.origin) < 320 && self.bol.origin_z > self.origin_z + 55)
						{
							self.angles_y = vectoyaw(self.bol.origin - self.origin);
							self.velocity = normalize(self.bol.origin - self.origin);
							self.velocity = self.velocity * 410;
							self.velocity_z = self.velocity_z + 300;
						}
					}
				}
			}
		}
	}
};

void(float t) SpawnKeeper =
{
	local entity bot, spot, os;
	
	bot = spawn ();
	bot.classname = "bot";
	
	os = self;
	self = bot;
	GetName ();
	self = os;
	
	bot.touch = player_touch;
	bot.health = 100;
	bot.movetype = MOVETYPE_STEP;
	bot.solid = SOLID_SLIDEBOX;
	bot.team_no = bot.skin = t;
	bot.is_keeper = 1;
	
	os = self;
	self = bot;
	spot = self.gol = FindGoalToKeeper ();
	self.goal2 = FindGoal ();
	self = os;
	
	bot.origin = bot.oldorigin = spot.real_origin + spot.movedir*48 + '0 0 1';
	bot.angles = vectoangles(spot.movedir);
	
	bot.bol = find(world, classname, "ball");
	
	bot.yaw_speed = 120;
	bot.ideal_yaw = bot.angles * '0 1 0';
	
	bot.nextthink = time + 0.1;
	bot.think = keeper_ai;
	
	setmodel (bot, "progs/player.mdl");
	setsize (bot, '-16 -16 -24', '16 16 32');
};