this.GoalType = "MOBILEMG42";
this.Version = 1;
this.DefaultPriority = 0.89;
this.DefaultMinCampTime = 30.0;
this.DefaultMaxCamptime = 60.0;
this.RolePriorityBonus = 0.1;
this.MaxUsers_InUse(1);
this.MaxUsers_InProgress(1);

//////////////////////////////////////////////////////////////////////////
// Couple callbacks to enforce additional constraints on property values.
CheckMinCamp = function(var)
{
	if(var <= this.MaxCampTime)
	{
		return true;
	}
	// returning any string is an error string
	return "MinCampTime must be <= MaxCampTime";
};
CheckMaxCamp = function(var)
{
	if(var >= this.MinCampTime)
	{
		return true;
	}
	// returning any string is an error string
	return "MaxCampTime must be >= MinCampTime";
};

this.Schema = Schema();
this.Schema.AimVectors = Validate.TableOf("vector3").Default({});
this.Schema.MinCampTime = Validate.NumRange(0,9999).Default(this.DefaultMinCampTime).CheckCallback(CheckMinCamp);
this.Schema.MaxCampTime = Validate.NumRange(0,9999).Default(this.DefaultMaxCamptime).CheckCallback(CheckMaxCamp);

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

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

	if ( this.AimVectors ) {
		this.AimVectors[0] = GetLocalFacing();
	}
};

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

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;
		Props.AimVectors = { ToVector(Props.Facing) };

		this.Schema.Check(Props);

		this.SetPosition(this.GetPosition() + Util.WaypointGoalOffset);
	}
	else if(Props.Version == this.Version)
	{
		this.Schema.Check(Props);
		this.AimVectors = Props.AimVectors;
		this.MinCampTime = Props.MinCampTime;
		this.MaxCampTime = Props.MaxCampTime;

		// hack the orientation to work. leaving aim vectors for future conversion to script
		if ( tableCount(this.AimVectors) > 0 ) {
			this.SetFacing(this.AimVectors[0]);
		}

		if(!this.Schema.Check(this) || tableCount(this.AimVectors) < 1)
		{
			this.DisableGoal(true);
		}
	}
};

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

this.Render = function(editMode)
{
	offset2 = Vec3(0,0,64);
	stanceOffset = Vec3(0,0,-50);

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

		if(this.ExtraDebugText==null)
		{
			this.ExtraDebugText = "";
			this.ExtraDebugText += format("CampTime: %g to %g secs.",ToFloat(this.MinCampTime),ToFloat(this.MaxCampTime));
		}

		this.RenderDefault();

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

		if ( typeId(this.AimVectors) != 6 )
		{
			print(this.GetName(), "no aim vector table" );
			block(0);
		}
		else
		{
			foreach ( id and face in this.AimVectors )
			{
				DrawArrow(goalPos+stanceOffset,goalPos+stanceOffset+face*32,COLOR.BLUE,2);
				DrawText3d(goalPos+stanceOffset+face*32,ToString(id),COLOR.WHITE,2,textDrawRadius);
			}
		}

		sleep(2);
	}
};

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

this.SaveToTable = function(SaveTable)
{
	// save the goal specific properties
	SaveTable.AimVectors = this.AimVectors;
	SaveTable.MaxCampTime= this.MaxCampTime;
	SaveTable.MinCampTime = this.MinCampTime;
};

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

this.Help = function()
{
	print(this.GoalType,"goal, version",this.Version);
	print("Requires:");
	print("		facing - Direction the bot should face");
	print("Optional:");
	print("		mincamptime - minimum time a bot should camp at the goal");
	print("		maxcamptime - maximum time a bot should camp at the goal");
};

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

this.SetProperty = function(property, value)
{
	switch(property)
	{
		case "facing":
		{
			if ( value == "clearall" || value == "clear" )
			{
				tableClear(this.AimVectors);
			}
			else
			{
				i = ToInt(value);
				if ( typeId(i) == 1)
				{
					// make em add sequentially
					n = tableCount(this.AimVectors);
					if ( i < 0 || i > n )
					{
						i = n;
					}

					playerFace = GetLocalFacing();
					this.AimVectors[ i ] = playerFace;
					print("Facing",i,"set to",playerFace);
				}
				else
				{
					print(this.GoalType, ": Invalid facing index, expecting int");
				}
			}
		}
		case "mincamptime":
		{
			v = ToFloat(value);
			if(this.Schema.MinCampTime.CheckPrintErrors(this,v))
			{
				this.MinCampTime = v;
			}
		}
		case "maxcamptime":
		{
			v = ToFloat(value);
			if(this.Schema.MaxCampTime.CheckPrintErrors(this,v))
			{
				this.MaxCampTime = v;
			}
		}
		default:
		{
			print(this.GoalType, ": Invalid property:", property);
		}
	}
	this.ExtraDebugText = null;
};

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

this.HudDisplay = function(window)
{
	this.CreateGuiFromSchema(this.Schema);
};
