context: using Server;
  using System;
  using Server.Mobiles;
  using System.Collections.Generic;
  using System.Linq;
  using Server.Commands;
  
  namespace Server.Items
  {
      public class MoonstonePowerGeneratorAddon : BaseAddon
      {
          public override bool ShareHue { get { return false; } }
  
          public Timer ActiveTimer { get; set; }
          public InternalComponent Activator1 { get; set; }
          public InternalComponent Activator2 { get; set; }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public bool Activated { get; set; }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public bool SetActive
          {
              get
              {
                  return Activated;
              }
              set
              {
                  if (value)
                  {
                      if (ActiveTimer != null)
                      {
                          ActiveTimer.Stop();
                          ActiveTimer = null;
                      }
  
                      Activated = true;
                      CheckNetwork();
                  }
              }
          }
  
          public bool Link { get; set; }
  
          public MoonstonePowerGenerator Generator { get; set; }
  
          [Constructable]
          public MoonstonePowerGeneratorAddon(bool link)
          {
              AddonComponent c = new AddonComponent(39759);
              c.Hue = 2955;
              AddComponent(c, 0, 0, 0);
  
              c = new AddonComponent(39759);
              c.Hue = 2955;
              AddComponent(c, -1, 0, 0);
  
              c = new AddonComponent(39759);
              c.Hue = 2955;
              AddComponent(c, 1, 0, 0);
  
              c = new AddonComponent(39759);
              c.Hue = 2955;
              AddComponent(c, 0, -1, 0);
  
              c = new AddonComponent(39759);
              c.Hue = 2955;
              AddComponent(c, 0, 1, 0);
  
              c = new AddonComponent(39818);
              c.Hue = 2955;
              AddComponent(c, -1, -1, 0);
  
              c = new AddonComponent(39818);
              c.Hue = 2955;
              AddComponent(c, -1, 1, 0);
  
              c = new AddonComponent(39818);
              c.Hue = 2955;
              AddComponent(c, 1, -1, 0);
  
              c = new AddonComponent(39818);
              c.Hue = 2955;
              AddComponent(c, 1, 1, 0);
  
              Activator1 = new InternalComponent(40158);
              Activator2 = new InternalComponent(40203);
  
              AddComponent(Activator1, 0, -1, 5);
              AddComponent(Activator2, 0, 1, 5);
  
              AddComponent(new LocalizedAddonComponent(40155, 1156623), 1, 0, 5);
              AddComponent(new LocalizedAddonComponent(40155, 1156623), -1, 0, 5);
  
              AddComponent(new LocalizedAddonComponent(40156, 1156628), -1, 0, 10);
              AddComponent(new LocalizedAddonComponent(40156, 1156628), 1, 0, 10);
  
              //AddComponent(new LocalizedAddonComponent(40147, 1124171), 0, 0, 5);
              AddComponent(new LocalizedAddonComponent(40157, 1124171), 0, 0, 20);
  
              Generator = new MoonstonePowerGenerator(this);
  
              Link = link;
  
              if (link)
                  Generators.Add(this);
          }
  
          public override void OnLocationChange(Point3D oldlocation)
          {
              base.OnLocationChange(oldlocation);
  
              if (Generator != null && !Generator.Deleted)
              {
                  Generator.MoveToWorld(new Point3D(this.X, this.Y, this.Z + 5), this.Map);
              }
          }
  
          public override void OnMapChange()
          {
              base.OnMapChange();
  
              if (Generator != null && !Generator.Deleted)
              {
                  Generator.Map = this.Map;
              }
          }
  
          public override void OnComponentUsed(AddonComponent component, Mobile from)
          {
              if (!Activated && component != null && component is InternalComponent && from.InRange(component.Location, 2))
              {
                  InternalComponent comp = component as InternalComponent;
  
                  if (!comp.Active)
                  {
                      comp.Active = true;
                      comp.WhoActivated = from;
  
                      if (Activator1.Active && Activator2.Active && Activator1.WhoActivated != Activator2.WhoActivated)
                      {
                          if (ActiveTimer != null)
                          {
                              ActiveTimer.Stop();
                              ActiveTimer = null;
                          }
  
                          Activated = true;
                          CheckNetwork();
                      }
                      else if (ActiveTimer == null)
                          ActiveTimer = Timer.DelayCall(TimeSpan.FromSeconds(1), Reset);
                  }
              }
          }
  
          private void Reset()
          {
              if (Activator1 != null)
                  Activator1.Active = false;
  
              if (Activator2 != null)
                  Activator2.Active = false;
  
              ActiveTimer = null;
          }
  
          public class InternalComponent : LocalizedAddonComponent
          {
              private bool _Active;
  
              [CommandProperty(AccessLevel.GameMaster)]
              public bool Active
              {
                  get { return _Active; }
                  set
                  {
                      if (!_Active && value)
                      {
                          ItemID = ActiveID;
                          Effects.PlaySound(this.Location, this.Map, 0x051);
                      }
                      else if (_Active && !value)
                      {
                          ItemID = InactiveID;
                          WhoActivated = null;
                          Effects.PlaySound(this.Location, this.Map, 0x051);
                      }
  
                      _Active = value;
                  }
              }
  
              public Mobile WhoActivated { get; set; }
  
              public int ActiveID { get; set; }
              public int InactiveID { get; set; }
  
              public InternalComponent(int itemid)
                  : base(itemid, 1156624)
              {
                  InactiveID = itemid;
  
                  if (itemid == 40203)
                      ActiveID = 40158;
                  else
                      ActiveID = 40203;
              }
  
              public InternalComponent(Serial serial)
                  : base(serial)
              {
              }
  
              public override void Serialize(GenericWriter writer)
              {
                  base.Serialize(writer);
                  writer.Write(0);
  
                  writer.Write(ActiveID);
                  writer.Write(InactiveID);
  
                  writer.Write(_Active);
              }
  
              public override void Deserialize(GenericReader reader)
              {
                  base.Deserialize(reader);
                  int v = reader.ReadInt();
  
                  ActiveID = reader.ReadInt();
                  InactiveID = reader.ReadInt();
  
                  Active = reader.ReadBool();
  
                  MoonstonePowerGeneratorAddon chamber = Addon as MoonstonePowerGeneratorAddon;
  
                  if (chamber != null)
                  {
                      if (chamber.Activator1 == null)
                          chamber.Activator1 = this;
                      else
                          chamber.Activator2 = this;
                  }
              }
          }
  
          public override void Delete()
          {
              base.Delete();
  
              if (Generators.Contains(this))
                  Generators.Remove(this);
          }
  
          public static void Configure()
          {
              Generators = new List<MoonstonePowerGeneratorAddon>();
          }
  
          public static void Initialize()
          {
              bool active = true;
  
              foreach (MoonstonePowerGeneratorAddon c in Generators)
              {
                  if (!c.Activated)
                  {
                      active = false;
                      break;
                  }
              }
  
              if (!active)
              {
                  ResetGenerators(true);
              }
              else if (Boss == null)
              {
                  ResetGenerators();
              }
  
              CommandSystem.Register("ActivateChambers", AccessLevel.Administrator, e =>
                  {
                      if (Boss == null)
                      {
  
gt:                         Generators.ForEach(c => c.Activated = true);