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.Radius = this.MinRadius;

	if (GetEntFlags(GetLocalEntity(), ENTFLAG.CROUCHED)) {
		this.Stance = "crouch";
	}

	this.CheckPosition ();
};

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

this.UpgradeVersion = function(Props)
{
	// version 0 is string,string key/values from the old waypoint format
	if(Props.Version==0)
	{
		// Version 1 Schema, upgrade from waypoint format
		Props.Version = 1;
	}
	else if(Props.Version == this.Version)
	{
		this.Stance = Props.Stance;
		this.AimPosition = Props.AimPosition;

		this.CloseMines = {};
		tbl = {
			Name=this.GetName(),
			OnEnter = this.MineEntered,
			OnExit = this.MineExited,
			UpdateDelay = 0.5,
			CallbackObject = this,
			TriggerOnClass = CLASS.TRIPMINE,
		};

		rgnId = OnTriggerRegion(this.AimPosition, 64.0, tbl);
		//todo: attach the life of this region to the mapgoal
	}
};

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

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

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

		if(this.ExtraDebugText==null)
		{
			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, version",this.Version);
	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;
	}
};
