// This script contains functionality to allow medic bots to dispense health at spawn

this.Name = "TestBot";	
this.Parent = "HighLevel";
this.AlwaysRecieveEvents = true;	// Always recieve events
this.AutoAdd = false;			// Don't add it to everyone automatically.

this.Initialize = function()
{
	// Internal variables
	this.Debug = true;

	//print(this.Name, " Initialized");
	this.PersistentPriority = 2;
};

this.GetPriority = function()
{
	this.Priority = this.PersistentPriority;
	block(0);
};

this.Enter = function()
{
	this.Stop();
	//this.AddAimRequest(Priority.Medium, "movedirection");
	//this.AddWeaponRequest(Priority.Medium, this.Bot.GetBestWeapon());
};

this.Exit = function()
{
	this.ReleaseAimRequest();
	this.ReleaseWeaponRequest();
};

this.Update = function()
{
	while(true)
	{
		sleep(3);
	}
};

/////////////////////////
this.Events[EVENT.FEEL_PAIN] = function(Inflictor)
{
	if(Inflictor)
	{
		who = GetEntName(Inflictor);
		this.Bot.Say("Hurt by ", who);
	}
};

this.Events[EVENT.PLAYER_USE] = function(Who)
{
	p = GetEntName(Who);
	this.Bot.Say("Used by ", p);
};

this.Events[EVENT.HEAR_SOUND] = function(Source, Origin, SoundId, SoundName)
{
	this.Bot.Say("Heard Sound ", SoundName);
};

this.Events[EVENT.DEATH] = function(Inflictor, MeansOfDeath)
{
	who = GetEntName(Inflictor);
	if(who)
	{
		this.Bot.Say(who, " killed me with ", MeansOfDeath);
	}
	else
	{
		this.Bot.Say("I died from ", MeansOfDeath);
	}
};

this.Events[EVENT.WEAPON_FIRE] = function(WeaponId, Projectile)
{
	this.Bot.Say("Fired Weapon ", WeaponId, " projectile: ", Projectile);

	// if(Projectile)
	// {
		// endTime = GetTime() + 5000;
		// dowhile(GetTime() < endTime)
		// {
			// projVel = GetEntVelocity(Projectile);
			// print("Projectile Velocity ", projVel.Length());
			// yield();
		// }
	// }
};

this.Events[EVENT.KILLEDSOMEONE] = function(Victim, MeansOfDeath)
{
	who = GetEntName(Victim);
	this.Bot.Say("I killed ", Victim, " with ", MeansOfDeath);
};

this.Events[EVENT.DISGUISING] = function(AsTeam, AsClass)
{
	this.Bot.Say("Disguising as team ", AsTeam, " and class ", AsClass);
};

this.Events[EVENT.DISGUISED] = function(AsTeam, AsClass)
{
	this.Bot.Say("Disguised as team ", AsTeam, " and class ", AsClass);
};

this.Events[EVENT.DISGUISE_LOST] = function()
{
	this.Bot.Say("Lost disguise!");
};

this.Events[EVENT.GOT_ENGY_ARMOR] = function(who)
{
	if(GetEntFlags(who, ENTFLAG.HUMANCONTROLLED))
	{
		this.Bot.Say("Player Armored " + this.Bot.Name);
	}
	else
	{
		this.Bot.Say("Bot Armored " + this.Bot.Name);
	}
};

this.Events[EVENT.GOT_MEDIC_HEALTH] = function(who)
{
	if(GetEntFlags(who, ENTFLAG.HUMANCONTROLLED))
	{
		this.Bot.Say("Player Healed " + this.Bot.Name);
	}
	else
	{
		this.Bot.Say("Bot Healed " + this.Bot.Name);
	}
};

this.Events[EVENT.DISPENSER_BUILT] = function(disp)
{
	this.Bot.Say(this.Bot.Name + " Dispenser Built");
};

this.Events[EVENT.SENTRY_BUILT] = function(sg)
{
	this.Bot.Say(this.Bot.Name + " Sentry Built");
};

this.Events[EVENT.SENTRY_UPGRADED] = function(level)
{
	this.Bot.Say(this.Bot.Name + " Sentry Upgraded Level " + level);
};

this.Events[EVENT.TEAM_CHAT_MSG] = function(who, msg)
{
	if(msg && msg.Find("bot") == 0)
	{
		cmd = msg.RightAt(4);
		print("got msg", cmd);
		if(cmd == "go")
		{
			this.Bot.Say("free roam.");

			this.PersistentPriority = 0;
			this.Exit();
			this.Finished();
		}
		else if(cmd == "dyno")
		{
			if ( this.Bot.HasWeapon(WEAPON.DYNAMITE) )
			{
				this.AddWeaponRequest(Priority.High, WEAPON.DYNAMITE);
				this.BlockForWeaponChange(WEAPON.DYNAMITE);
				this.Bot.HoldButton(BTN.ATTACK1, 1);
				sleep(2);
				ent = this.Bot.GetNearest(CAT.AVOID, CLASS.DYNAMITE_ENT);
				if ( ent ) {
					this.Bot.Say("i see dyno");
				} else {
					this.Bot.Say("i'm blind!");
				}
			}
		}
		else if(cmd == "searchdyno")
		{
			this.SearchDyno = !this.SearchDyno;
			while( this.SearchDyno ) {
				ent = this.Bot.GetNearest(CAT.AVOID, CLASS.DYNAMITE_ENT);
				if ( ent ) {
					this.Bot.Say("Found it!");
					break;
				}
				this.Bot.Say("still searching");
				sleep(1);
			}
		}		
		else if(cmd == "stop")
		{
			this.Bot.Say("stopping.");

			this.PersistentPriority = 2;
			this.Initialize();
			this.Enter();
			print("this.Name", this.Name);
			print("this.Parent", this.Parent);
			print("this.AlwaysRecieveEvents", this.AlwaysRecieveEvents);
		}
		else if(cmd == "shootoff")
		{
			this.Bot.Say("not shooting.");

			this.AddAimRequest(Priority.Medium, "movedirection");
			this.AddWeaponRequest(Priority.Medium, this.Bot.SelectBestWeapon());
		}
		else if(cmd == "shooton")
		{
			this.Bot.Say("shooting.");

			this.ReleaseAimRequest();
			this.ReleaseWeaponRequest();
		}
		else if(cmd == "jump")
		{
			this.Bot.PressButton(BTN.JUMP);
		}
		else if(cmd.Find("selectweapon")==0)
		{
			wpn = cmd.RightAt(13);
			wpn = Util.WeaponNameToId(wpn);
			this.Bot.Say("switching to " + Util.GetTableKey(WEAPON,wpn));
			this.AddWeaponRequest(Priority.Medium, ToInt(wpn));
		}
		else if(cmd == "reload")
		{
			this.Bot.PressButton(BTN.RELOAD);
		}
		else if(cmd == "shoot")
		{
			this.Bot.Say("shoot");
			this.Bot.HoldButton(BTN.ATTACK1, 5);
		}
		else if(cmd == "shoot2")
		{
			this.Bot.Say("shoot2");
			this.Bot.HoldButton(BTN.ATTACK2, 5);
		}
		else if(cmd == "crouch")
		{
			this.Bot.HoldButton(BTN.CROUCH, 5);
		}
		else if(cmd == "holdcrouch")
		{
			this.Bot.HoldButton(BTN.CROUCH, -1);
		}
		else if(cmd == "releasecrouch")
		{
			this.Bot.ReleaseButton(BTN.CROUCH);
		}
		else if(cmd == "holdprone")
		{
			this.Bot.HoldButton(BTN.PRONE, -1);
		}
		else if(cmd == "releaseprone")
		{
			this.Bot.ReleaseButton(BTN.PRONE);
		}		
		else if(cmd == "cloak")
		{
			this.Bot.Cloak();
		}
		else if(cmd == "scloak")
		{
			this.Bot.Cloak(true);
		}
		else if(cmd == "spin")
		{
			testfunc = function()
			{
				while(true)
				{
					// and back again
					entEyePos = this.Bot.GetEyePosition(ent);

					sFacing = Vector3(GetTime() * 2,0,1);
					entFace = Vec3FromSpherical(sFacing);

					this.AimVector = entFace;

					// draw a debug aid
					startPos = (entEyePos + Vector3(0,0,100));
					endPos = startPos + entFace * 64;
					DrawDebugLine(startPos, endPos, COLOR.RED, 0.1);
					DrawDebugLine(startPos + Vector3(0,0,32), startPos - Vector3(0,0,32), COLOR.GREEN, 0.1);

					yield();
				}
			};

			if(this.TestHeadingThread)
			{
				this.ReleaseAimRequest();
				threadKill(this.TestHeadingThread);
				this.TestHeadingThread = null;
			}
			else
			{
				this.AddAimRequest(Priority.High, "facing", Vec3FromSpherical(0.f));
				this.TestHeadingThread = this:thread(testfunc, 1);
			}

		}
		else if(cmd == "come")
		{
			this.Bot.Say("omw.");

			vPos = GetEntPosition(who);
			vFace = GetEntFacing(who);

			res = this.Goto(vPos, 32);
			if(this.Bot){
				if(res == EVENT.PATH_FAILED)
					{ sleep(2); this.Bot.Say("Can't do it!"); }
				else
					{ sleep(2); this.Bot.Say("Did it!"); }
			}
		}
		else if(cmd == "buildsg")
		{
			// this.Say("Going to build.");
			// sc = this.IsScriptControlled();
			// wsc = this.IsScriptControlledWeapons();

			// vPos = GetEntPosition(1);
			// vFace = GetEntFacing(1);

			// this.BuildSentry(vPos, vFace);
			// res = block(EVENT.GOAL_SUCCESS, EVENT.GOAL_FAILED);

			// if(res == EVENT.GOAL_FAILED)
				// { this.Say("Can't do it!"); }
			// else
				// { this.Say("Did it!"); }

			// this.SetScriptControlled(sc);
			// this.SetScriptControlledWeapons(wsc);
		}
		else if(cmd == "upgradesg")
		{
			// this.Say("Upgrading.");
			// sc = this.IsScriptControlled();
			// wsc = this.IsScriptControlledWeapons();

			// this.UpgradeSentry(vPos, vFace);
			// res = block(EVENT.GOAL_SUCCESS, EVENT.GOAL_FAILED);

			// if(res == EVENT.GOAL_FAILED)
				// { this.Say("Can't do it!"); }
			// else
				// { this.Say("Did it!"); }

			// this.SetScriptControlled(sc);
			// this.SetScriptControlledWeapons(wsc);
		}
		else if(cmd == "builddisp")
		{
			// this.Say("Going to build.");
			// sc = this.IsScriptControlled();
			// wsc = this.IsScriptControlledWeapons();

			// vPos = GetEntPosition(1);
			// vFace = GetEntFacing(1);

			// this.BuildDispenser(vPos, vFace);
			// res = block(EVENT.GOAL_SUCCESS, EVENT.GOAL_FAILED);

			// if(res == EVENT.GOAL_FAILED)
				// { this.Say("Can't do it!"); }
			// else
				// { this.Say("Did it!"); }

			// this.SetScriptControlled(sc);
			// this.SetScriptControlledWeapons(wsc);
		}
		else if(cmd == "builddetpack")
		{
			// this.Say("Going to build.");
			// sc = this.IsScriptControlled();
			// wsc = this.IsScriptControlledWeapons();

			// vPos = GetEntPosition(1);
			// vFace = GetEntFacing(1);

			// this.BuildDetpack(vPos, vFace);
			// res = block(EVENT.GOAL_SUCCESS, EVENT.GOAL_FAILED);

			// if(res == EVENT.GOAL_FAILED)
				// { this.Say("Can't do it!"); }
			// else
				// { this.Say("Did it!"); }

			// this.SetScriptControlled(sc);
			// this.SetScriptControlledWeapons(wsc);
		}
		else if(cmd == "menu")
		{
			menuOptions =
			{
				{ },
				{ },
				{ },
				{ },
				{ },
				{ },
				{ },
				{ },
				{ },
			};

			//menuOptions = table();
			t = menuOptions[0]; t["Build Sentry"] = "say bot buildsg";
			t = menuOptions[1]; t["Upgrade Sentry"] = "say bot upgradesg";
			t = menuOptions[2]; t["Build Dispenser"] = "say bot builddisp";
			t = menuOptions[3]; t["Build Detpack"] = "say bot builddetpack";

			HudMenu(1,
					"Bot Action Menu Hit Esc...",
					"Omni-bot Options",
					"Choose an option.",
					100,
					10,
					COLOR.WHITE,
					menuOptions);
		}
		else if(cmd == "wait")
		{
			this.Goto(this.Bot.GetPosition());
		}
		else if(cmd.Left(5) == "goto ")
		{
			sPos = cmd.RightAt(5);
			vPos = ToVector(sPos);
			if(IsVec3(vPos)){
				if(this.Goto(vPos) == EVENT.PATH_FAILED)
					{ this.Bot.Say("Path failed!"); }
				else
					{ this.Bot.Say("Path succeeded!"); }
			}else{
				print("invalid vector " + sPos);
			}
		}
		else
		{
			this.Bot.Say("Command: ", cmd);
			this.Bot.ExecCommand(cmd);
		}
	}
};

this.Events[EVENT.GLOBAL_CHAT_MSG] = this.Events[EVENT.TEAM_CHAT_MSG];
