this.GoalType = "TRIPMINE";
this.Version = 1;
this.DefaultPriority = 0.81;
this.MinRadius = 32;
this.RolePriorityBonus = 0.1;
this.MaxUsers_InUse(1);
this.MaxUsers_InProgress(1);

//////////////////////////////////////////////////////////////////////////

this.Schema = Schema();
this.Schema.Stance = Validate.Enum("stand","crouch").Default("crouch");

//////////////////////////////////////////////////////////////////////////

this.CheckPosition = function()
{
	aimpos = ETUtil.CheckTripminePosition();

	if (aimpos) {
		this.AimPosition = aimpos;
	}
	else {
		// make this obnoxious so they see it ..
		print("^1INVALID POSITION! use /bot setproperty aim");
	}

	return aimpos;
};

//////////////////////////////////////////////////////////////////////////

this.InitNewGoal = function()
{
	this.Schema.Check(this);
	this.CheckPosition();
};

//////////////////////////////////////////////////////////////////////////

this.UpgradeVersion = function(Props)
{
	if(Props.Version == 1)
	{
		this.Stance = Props.Stance;
		this.AimPosition = Props.AimPosition;

		this.CloseMines = {};

		if (!this.Schema.Check(this) || !IsVec3(this.AimPosition)) {
			this.DisableGoal(true);
			return;
		}
		OnTriggerRegion(this.AimPosition, 64, {
			Name = this.GetName(),
			OnEnter = this.MineEntered,
			OnExit = this.MineExited,
			CallbackObject = this,
			TriggerOnClass = CLASS.TRIPMINE,
		});
		//todo: attach the life of this region to the mapgoal
	}
};

//////////////////////////////////////////////////////////////////////////

this.Render = function()
{
	offset = Vec3(0,0,32);
	offset2 = Vec3(0,0,64);

	while(this.RenderGoal)
	{
		goalBasePos = this.GetPosition();
		goalPos = goalBasePos + offset2;
		aimLine = goalBasePos + offset;

		if(!this.ExtraDebugText)
		{
			this.ExtraDebugText = format("Stance: %s\n",this.Stance);
		}

		this.RenderDefault();

		if(this.IsDisabled())
		{
			DrawLine(goalPos,goalBasePos,COLOR.RED,2);
		}
		else
		{
			if ( this.InUse ) {
				DrawLine(goalPos,goalBasePos,COLOR.ORANGE,2);
			}
			else {
				DrawLine(goalPos,goalBasePos,COLOR.GREEN,2);
			}
		}

		if ( this.AimPosition && !this.AimPosition.IsZero() ) {
			DrawArrow(aimLine,this.AimPosition,COLOR.YELLOW,2);
		}

		sleep(2);
	}
};

//////////////////////////////////////////////////////////////////////////

this.SaveToTable = function(SaveTable)
{
	// save the goal specific properties
	SaveTable.Stance = this.Stance;
	SaveTable.AimPosition = this.AimPosition;
};

//////////////////////////////////////////////////////////////////////////

this.Help = function()
{
	print(this.GoalType,"goal");
	print("Required:");
	print("		aim - traces to find an aim position");
	print("Optional:");
	print("		stance - stand, crouch");
};

//////////////////////////////////////////////////////////////////////////

this.SetProperty = function(property, value)
{
	switch(property)
	{
		case "aim":
		{
			// move the goal
			ExecCommand("goal_move toplayer");
			aim = this.CheckPosition();
			if ( aim ) {
				print( "aim set to " + aim );
			}
		}
		case "stance": // crouch, stand, default stand
		{
			if(this.Schema.Stance.CheckPrintErrors(this,value))
			{
				this.Stance = value;
			}
		}
		default:
		{
			print(this.GoalType, ": Invalid property:", property);
		}
	}

	this.ExtraDebugText = null;
};

//////////////////////////////////////////////////////////////////////////

this.MineEntered = function(ent)
{
	this.CloseMines[ent] = 1;
	this.InUse = true;
};
this.MineExited = function(ent)
{
	this.CloseMines[ent] = null;
	if ( tableCount(this.CloseMines) == 0 )
	{
		this.InUse = false;
	}
};
