context: /*
   * DAWN OF LIGHT - The first free open source DAoC server emulator
   * 
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License
   * as published by the Free Software Foundation; either version 2
   * of the License, or (at your option) any later version.
   * 
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   * 
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   *
   */
  using System;
  using System.Collections;
  using System.Collections.Generic;
  using DOL.AI.Brain;
  using DOL.Database;
  using DOL.Events;
  using DOL.GS.Keeps;
  using DOL.GS.PacketHandler;
  using DOL.GS.Movement;
  using DOL.Language;
  
  namespace DOL.GS.Commands
  {
  	/// <summary>
  	/// Various keep guard commands
  	/// </summary>
  	[CmdAttribute(
  		"&keepguard",
  		ePrivLevel.GM,
  		"GMCommands.KeepGuard.Description",
  		"GMCommands.KeepGuard.Information",
  		"GMCommands.KeepGuard.Usage.Create",
  		"GMCommands.KeepGuard.Usage.Position.Add",
  		"GMCommands.KeepGuard.Usage.Position.Remove",
  		"GMCommands.KeepGuard.Usage.Path.Create",
  		"GMCommands.KeepGuard.Usage.Path.Add",
  		"GMCommands.KeepGuard.Usage.Path.Save")]
  	public class KeepGuardCommandHandler : AbstractCommandHandler, ICommandHandler
  	{
  		/// <summary>
  		/// The command handler itself
  		/// </summary>
  		/// <param name="client">The client using the command</param>
  		/// <param name="args">The command arguments</param>
  		public void OnCommand(GameClient client, string[] args)
  		{
  			if (args.Length == 1)
  			{
  				DisplaySyntax(client);
  				return;
  			}
  
  			switch (args[1].ToLower())
  			{
  				#region Create
  				case "create":
  					{
  						GameKeepGuard guard = null;
  						if (args.Length < 3)
  						{
  							DisplaySyntax(client);
  							return;
  						}
  						
  						switch (args[2].ToLower())
  						{
  							#region Lord
  							case "lord":
  								{
  									guard = new GuardLord();
  									break;
  								}
  							#endregion Lord
  							#region Fighter
  							case "fighter":
  								{
  									guard = new GuardFighter();
  									break;
  								}
  							#endregion Fighter
  							#region Archer
  							case "archer":
  								{
  									if (args.Length > 3)
  										guard = new GuardStaticArcher();
  									else
  										guard = new GuardArcher();
  									break;
  								}
  							#endregion Archer
  							#region Healer
  							case "healer":
  								{
  									guard = new GuardHealer();
  									break;
  								}
  							#endregion Healer
  							#region Stealther
  							case "stealther":
  								{
  									guard = new GuardStealther();
  									break;
  								}
  							#endregion Stealther
  							#region Caster
  							case "caster":
  								{
  									if (args.Length > 3)
  										guard = new GuardStaticCaster();
  									else
  										guard = new GuardCaster();
  									break;
  								}
  							#endregion Caster
  							#region Hastener
  							case "hastener":
  								{
  									guard = new FrontierHastener();
  									break;
  								}
  							#endregion Hastener
  							#region Mission
  							case "mission":
  								{
  									guard = new MissionMaster();
  									break;
  								}
  							#endregion Mission
  							#region Patrol
  							case "patrol":
  								{
  									if (args.Length < 4)
  									{
  										DisplayMessage(client, "You need to provide a name for this patrol.");
  										return;
  									}
  
  									AbstractGameKeep.eKeepType keepType = AbstractGameKeep.eKeepType.Any;
  
  									if (args.Length < 5)
  									{
  										DisplayMessage(client, "You need to provide the type of keep this patrol works with.");
  										int i = 0;
  										foreach (string str in Enum.GetNames(typeof(Keeps.AbstractGameKeep.eKeepType)))
  										{
  											DisplayMessage(client, "#" + i + ": " + str);
  											i++;
  										}
  										return;
  									}
  
  									try
  									{
  										keepType = (AbstractGameKeep.eKeepType)Convert.ToInt32(args[4]);
  									}
  									catch
  									{
  										DisplayMessage(client, "Type of keep specified was not valid.");
  										return;
  									}
  
  
  									if (client.Player.TargetObject is GameKeepComponent == false)
  									{
  										DisplayMessage(client, LanguageMgr.GetTranslation(client.Account.Language, "GMCommands.KeepGuard.Create.NoKCompTarget"));
  										return;
  									}
  									GameKeepComponent c = client.Player.TargetObject as GameKeepComponent;;
  									Patrol p = new Patrol(c);
  									p.PatrolID = args[3];
  									p.KeepType = keepType;
  									p.SpawnPosition = PositionMgr.CreatePatrolPosition(p.PatrolID, c, client.Player, keepType);
  									p.PatrolID = p.SpawnPosition.TemplateID;
  									p.InitialiseGuards();
  									DisplayMessage(client, "Patrol created for Keep Type " + Enum.GetName(typeof(AbstractGameKeep.eKeepType), keepType));
  									return;
  								}
  							#endregion Patrol
  						}
  
  						if (guard == null)
  						{
  							DisplaySyntax(client);
  							return;
  						}
  
  						GameKeepComponent component = client.Player.TargetObject as GameKeepComponent;
  						if (component != null)
  						{
  							int height = component.Height;
  							if (args.Length > 4)
  								int.TryParse(args[4], out height);
  
  							DBKeepPosition pos = PositionMgr.CreatePosition(guard.GetType(), height, client.Player, Guid.NewGuid().ToString(), component);
  							//PositionMgr.AddPosition(pos);
  							//PositionMgr.FillPositions();
  							DBKeepPosition[] list = component.Positions[pos.TemplateID] as DBKeepPosition[];
  							if (list == null)
  							{
  								list = new DBKeepPosition[4];
  								component.Positions[pos.TemplateID] = list;
  							}
  								
  							list[pos.Height] = pos;
  							component.LoadPositions();
  							component.FillPositions();
  						}
  						else
  						{
  							guard.CurrentRegion = client.Player.CurrentRegion;
  							guard.X = client.Player.X;
  							guard.Y = client.Player.Y;
  							guard.Z = client.Player.Z;
  							guard.Heading = client.Player.Heading;
  							guard.Realm = guard.CurrentZone.Realm;
                              guard.LoadedFromScript = false;
                              guard.SaveIntoDatabase();
  							
  							foreach (AbstractArea area in guard.CurrentAreas)
  							{
  								if (area is KeepArea)
  								{
  									AbstractGameKeep keep = (area as KeepArea).Keep;
  									guard.Component = new GameKeepComponent();
  									guard.Component.Keep = keep;
  									break;
  								}
  							}
  
  							guard.RefreshTemplate();
  							guard.AddToWorld();
  
  
gt: 							if (guard.Component != null && guard.Component.Keep != null)