this.GoalType = "MOBILEMG42";
this.Version = 1;
this.DefaultPriority = 0.89;
this.MinRadius = 32;
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(30).CheckCallback(CheckMinCamp);
this.Schema.MaxCampTime = Validate.NumRange(0,9999).Default(60).CheckCallback(CheckMaxCamp);

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

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

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

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

this.UpgradeVersion = function(Props)
{
	if(Props.Version==0)
	{
		// upgrade from old waypoint format
		Props.Version = 1;
		Props.AimVectors = { ToVector(Props.Facing) };
		this.SetPosition(this.GetPosition() + Util.WaypointGoalOffset);
	}
	else if(Props.Version == 1)
	{
		this.AimVectors = Props.AimVectors;
		this.MinCampTime = Props.MinCampTime;
		this.MaxCampTime = Props.MaxCampTime;

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

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

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

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

		if(!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 (IsTable(this.AimVectors))
		{
			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;
	if (!IsInt(Map.SaveCampTime) || Map.SaveCampTime)
	{
		SaveTable.MaxCampTime = this.MaxCampTime;
		SaveTable.MinCampTime = this.MinCampTime;
	}
};

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

this.Help = function()
{
	print(this.GoalType,"goal");
	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);
};
