// This script contains functionality to allow engineers to repair MG42
this.Name = "REPAIRMG42";
this.Parent = "HighLevel";
this.SkipGetPriorityWhenActive = true;
this.LimitToWeapon(WEAPON.PLIERS);

this.Initialize = function()
{
	// check for entity status so we can finish if another player repairs it
	this.LimitTo(this.CheckRepairableState, 2, true);
};

this.CheckRepairableState = function()
{
	return !this.GoalEntity || this.Bot.IsMG42Repairable(this.GoalEntity);
};

this.GetPriority = function()
{
	Util.GetPriority(this,0x17929136 /* REPAIRMG42 */,0,0, function(goal, bot, _this)
	{
		ent = goal.GetEntity();
		if (ent && bot.IsMG42Repairable(ent)) {
			_this.GoalEntity = ent;
			return true;
		}
	});
};

this.Enter = this.MarkInProgress;

this.Update = function()
{
	Util.RouteTo(this);

	switch (this.MapGoal.Stance)
	{
		case "crouch":
		{
			this.Bot.HoldButton(BTN.CROUCH, 99);
		}
		case "prone":
		{
			this.Bot.HoldButton(BTN.PRONE, 99);
		}
	}

	this.AddAimRequest(Priority.High, "position", this.MapGoal.GetPosition());

	this.AddWeaponRequest(WEAPON.PLIERS);
	this.BlockForWeaponChange(WEAPON.PLIERS);

	if (this.MapGoal.Stance == "prone")
	{
		for (i=0; i<50 && this.Bot.DistanceTo(this.MapGoal, true) < 60; ++i)
		{
			this.Bot.HoldButton(BTN.BACKWARD, 0.1);
			sleep(0.1);
		}
	}
	
	for (timeout=0; this.Bot.IsMG42Repairable(this.GoalEntity); ++timeout)
	{
		if (timeout > 10)
		{
			this.BlackboardDelay(45);
			Util.ScriptDebugPrint(this.MapGoal.GetName(), "timedout");
			this.Finished();
		}
		if (timeout == 3 && this.Bot.IsWeaponCharged(WEAPON.DYNAMITE))
		{
			this.Bot.HoldButton(BTN.BACKWARD, 0.3);
		}
		this.Bot.HoldButton(BTN.ATTACK1, 1);
		sleep(1);
	}

	this.BlackboardDelay(5);
	this.Finished();
};

this.Exit = function()
{
	this.GoalEntity = null;
	this.Bot.ReleaseButton(BTN.CROUCH, BTN.PRONE);
};
