Full Version : PS book and internals
xmlspawner >>Scripting Support >>PS book and internals


<< Prev | Next >>

Sunshine- 07-31-2006
I have this script I have been using and now come to find out I have alot of internals because of it...I have checked and it is archived...so not much help there


there are newer ones out that say they do not cause internals but I am afraid if I switch ..it will cause players to loose ps..soo

If you do not object I am going to post it here and ask if you know how I can correct this one so it will not cause deletion of the exsisting ps I would GREATLY appreciate it
Run UO 1 ....umm they add to internal when players drop them in the books

CODE
using System;
using Server;
using Server.Mobiles;
using Server.ContextMenus;
using Server.Targeting;
using System.Collections;
using Server.Gumps;

namespace Server.Items
{
#region Book
public class PSBook : Item
{
 private ArrayList m_Entries;
 public ArrayList Entries{get{ return m_Entries; }}

 [Constructable]
 public PSBook() : base(8793)
 {
  Weight = 1.0;
  LootType = LootType.Cursed;
  m_Entries = new ArrayList();

  Hue = 1153;
  Name = "Power Scroll Book";
 }

 public PSBook(Serial serial) : base(serial)
 {
 }

 public override void Serialize(GenericWriter writer)
 {
  base.Serialize(writer);

  writer.Write((int) 1);

  writer.WriteEncodedInt( (int) m_Entries.Count );

  for ( int i = 0; i < m_Entries.Count; ++i )
  {
   PowerScroll scroll = m_Entries[i] as PowerScroll;
   int skill = (int)scroll.Skill;
   writer.WriteEncodedInt(skill);
   double amount = scroll.Value;
   writer.Write(amount);
  }
 }

 public override void Deserialize(GenericReader reader)
 {
  base.Deserialize(reader);

  int version = reader.ReadInt();

  int count = reader.ReadEncodedInt();

  m_Entries = new ArrayList( count );

  for ( int i = 0; i < count; ++i )
  {
   SkillName skill = (SkillName)reader.ReadEncodedInt();
   double amount = reader.ReadDouble();
   PowerScroll scroll = new PowerScroll(skill,amount);
   m_Entries.Add(scroll);
  }
 }
 public override void AddNameProperties( ObjectPropertyList list )
 {
  base.AddNameProperties( list );

  list.Add(1053099 ,"{0}\t{1}","Scrolls in book: ", m_Entries.Count.ToString());
 }
 public override void OnDoubleClick( Mobile from )
 {
  if ( !IsChildOf( from.Backpack ) )
  {
   from.SendMessage( "Book must be in your backpack to use it." );
  }
  else if ( m_Entries.Count == 0 )
  {
   from.SendLocalizedMessage( 1062381 );
  }
  else if ( from is PlayerMobile )
  {
   from.CloseGump( typeof( PSBookGump ) );
   from.SendGump( new PSBookGump( from, this ) );
  }
 }
 public override bool OnDragDrop( Mobile from, Item dropped )
 {
  if ( dropped is PowerScroll )
  {
   if ( !IsChildOf( from.Backpack ) )
   {
    from.SendMessage( "Book must be in your backpack to use it." );
    return false;
   }
   else if ( m_Entries.Count < 20 )//will hold 20
   {
    PowerScroll scroll = (PowerScroll)dropped;
    this.Entries.Add(scroll);

    InvalidateProperties();

    from.SendMessage( "Scroll added to book." );

    if ( from is PlayerMobile )
    {
     from.CloseGump( typeof( PSBookGump ) );
     from.SendGump( new PSBookGump( from, this ) );
    }

    dropped.Delete();
    return true;
   }
   else
   {
    from.SendMessage( "The book is full." );
    return false;
   }
  }

  from.SendMessage( "That is not a powerscroll." );
  return false;
 }
}
#endregion
#region Gump
public class PSBookGump : Gump
{
 private int y;
 public Mobile m_From;
 public PSBook m_Book;

 public PSBookGump(Mobile from, PSBook book) : base( 0, 0 )
 {
  m_From = from;
  m_Book = book;

  y = (m_Book.Entries.Count -1)*20;

  Closable=true;
  Disposable=true;
  Dragable=true;
  Resizable=false;
  AddPage(0);
  AddBackground(10, 50, 230, 113+y, 9250);
  AddImageTiled( 24, 65, 200, 85+y, 2053);  
  AddAlphaRegion(24, 65, 200, 85+y);
  AddImageTiled(24, 87, 200, 10, 9264);
  AddImageTiled(24, 120, 200, 10, 9264);
  AddImageTiled(105, 95, 3, 55+y, 9264);
  AddImageTiled(179, 95, 3, 55+y, 9264);

  AddLabel(65, 65, 190, "PowerScroll Book");
  AddLabel(30, 100, 199, "Skill");
  AddLabel(110, 100, 199, "Value");
  AddLabel(185, 100, 199, "Drop");
 
  int y2 = 0;
  int butNumb = 1;
  for(int i = 0; i < m_Book.Entries.Count; i++)
  {
   PowerScroll scroll = m_Book.Entries[i] as PowerScroll;
   AddLabel(25, 130+y2, 195, scroll.Skill.ToString());
   AddLabel(110, 130+y2, 195, scroll.Value.ToString());
   AddButton(195, 133+y2, 1209, 1210, butNumb, GumpButtonType.Reply, 0);
   y2+=20;
   butNumb++;
  }
 }  
 public override void OnResponse( Server.Network.NetState sender, RelayInfo info )
 {
  int bp;//button pushed
  switch(info.ButtonID)
  {
   case 1:bp = 0;break;
   case 2:bp = 1;break;
   case 3:bp = 2;break;
   case 4:bp = 3;break;
   case 5:bp = 4;break;
   case 6:bp = 5;break;
   case 7:bp = 6;break;
   case 8:bp = 7;break;
   case 9:bp = 8;break;
   case 10:bp = 9;break;
   case 11:bp = 10;break;
   case 12:bp = 11;break;
   case 13:bp = 12;break;
   case 14:bp = 13;break;
   case 15:bp = 14;break;
   case 16:bp = 15;break;
   case 17:bp = 16;break;
   case 18:bp = 17;break;
   case 19:bp = 18;break;
   case 20:bp = 19;break;
   default:return;break;
  }

  PowerScroll scroll = m_Book.Entries[bp] as PowerScroll;
  SkillName sklnm = scroll.Skill;
  double sklval = scroll.Value;
  PowerScroll newScroll = new PowerScroll(sklnm, sklval);
  m_From.AddToBackpack(newScroll);
  m_Book.Entries.RemoveAt(bp);
  m_Book.InvalidateProperties();
 }
}
#endregion
}


ArteGordon- 07-31-2006
the problem is here

QUOTE

public override void Deserialize(GenericReader reader)
{
  base.Deserialize(reader);

  int version = reader.ReadInt();

  int count = reader.ReadEncodedInt();

  m_Entries = new ArrayList( count );

  for ( int i = 0; i < count; ++i )
  {
  SkillName skill = (SkillName)reader.ReadEncodedInt();
  double amount = reader.ReadDouble();
  PowerScroll scroll = new PowerScroll(skill,amount);
  m_Entries.Add(scroll);
  }
}


every time you restart, it will create new power scrolls to add to the book. The problem is that the previous power scrolls were saved, as all items are, and so are still around, the book just didnt keep track of them during serialization.

The correct solution is to save the list of power scrolls during serialization and then restore them to the list during deserialization.

CODE

       public override void Serialize(GenericWriter writer)
       {
           base.Serialize(writer);

           writer.Write((int)2);

           // version 2
           writer.WriteItemList(m_Entries);

       }

       public override void Deserialize(GenericReader reader)
       {
           base.Deserialize(reader);

           int version = reader.ReadInt();

           if (version > 1)
           {
               // reload the book with the saved powerscrolls
               m_Entries = reader.ReadItemList();
           }
           else
           {
               // this is the old bugged code that creates new scrolls
               int count = reader.ReadEncodedInt();

               m_Entries = new ArrayList(count);

               for (int i = 0; i < count; ++i)
               {
                   SkillName skill = (SkillName)reader.ReadEncodedInt();
                   double amount = reader.ReadDouble();
                   PowerScroll scroll = new PowerScroll(skill, amount);
                   m_Entries.Add(scroll);
               }
           }
       }


Sunshine- 07-31-2006
QUOTE (ArteGordon @ July 31, 2006 10:00 pm)
the problem is here

QUOTE

public override void Deserialize(GenericReader reader)
{
  base.Deserialize(reader);

  int version = reader.ReadInt();

  int count = reader.ReadEncodedInt();

  m_Entries = new ArrayList( count );

  for ( int i = 0; i < count; ++i )
  {
   SkillName skill = (SkillName)reader.ReadEncodedInt();
   double amount = reader.ReadDouble();
   PowerScroll scroll = new PowerScroll(skill,amount);
   m_Entries.Add(scroll);
  }
}


every time you restart, it will create new power scrolls to add to the book. The problem is that the previous power scrolls were saved, as all items are, and so are still around, the book just didnt keep track of them during serialization.

The correct solution is to save the list of power scrolls during serialization and then restore them to the list during deserialization.

CODE

       public override void Serialize(GenericWriter writer)
       {
           base.Serialize(writer);

           writer.Write((int)2);

           // version 2
           writer.WriteItemList(m_Entries);

       }

       public override void Deserialize(GenericReader reader)
       {
           base.Deserialize(reader);

           int version = reader.ReadInt();

           if (version > 1)
           {
               // reload the book with the saved powerscrolls
               m_Entries = reader.ReadItemList();
           }
           else
           {
               // this is the old bugged code that creates new scrolls
               int count = reader.ReadEncodedInt();

               m_Entries = new ArrayList(count);

               for (int i = 0; i < count; ++i)
               {
                   SkillName skill = (SkillName)reader.ReadEncodedInt();
                   double amount = reader.ReadDouble();
                   PowerScroll scroll = new PowerScroll(skill, amount);
                   m_Entries.Add(scroll);
               }
           }
       }

Thanks hun soo I just replace the part posted with the section you posted?


I appreciate your help and understood what you were saying but not sure if I should change out just that part or what

ArteGordon- 07-31-2006
right. Replace your existing Serialize and Deserialize with what I posted.

When you restart your server, you will still end up with one last round of improperly internalized power scrolls from your previous save, but you shouldnt get any more after that.

Actually, you are also going to need to add in an override of the OnDelete method to properly remove the scrolls when you delete the book.

CODE

      public override void OnDelete()
       {
           base.OnDelete();
           foreach (PowerScroll s in m_Entries)
           {
               if(s != null)
                   s.Delete();
           }
       }

Sunshine- 08-01-2006
CODE
using System;
using Server;
using Server.Mobiles;
using Server.ContextMenus;
using Server.Targeting;
using System.Collections;
using Server.Gumps;

namespace Server.Items
{
#region Book
public class PSBook : Item
{
private ArrayList m_Entries;
public ArrayList Entries{get{ return m_Entries; }}

[Constructable]
public PSBook() : base(8793)
{
 Weight = 1.0;
 LootType = LootType.Cursed;
 m_Entries = new ArrayList();

 Hue = 1153;
 Name = "Power Scroll Book";
}

public PSBook(Serial serial) : base(serial)
{
}

public override void Serialize(GenericWriter writer)
      {
          base.Serialize(writer);

          writer.Write((int)2);

          // version 2
          writer.WriteItemList(m_Entries);

      }

      public override void Deserialize(GenericReader reader)
      {
          base.Deserialize(reader);

          int version = reader.ReadInt();

          if (version > 1)
          {
              // reload the book with the saved powerscrolls
              m_Entries = reader.ReadItemList();
          }
          else
          {
              // this is the old bugged code that creates new scrolls
              int count = reader.ReadEncodedInt();

              m_Entries = new ArrayList(count);

              for (int i = 0; i < count; ++i)
              {
                  SkillName skill = (SkillName)reader.ReadEncodedInt();
                  double amount = reader.ReadDouble();
                  PowerScroll scroll = new PowerScroll(skill, amount);
                  m_Entries.Add(scroll);
              }
          }
      }

public override void AddNameProperties( ObjectPropertyList list )
{
 base.AddNameProperties( list );

 list.Add(1053099 ,"{0}\t{1}","Scrolls in book: ", m_Entries.Count.ToString());
}
public override void OnDoubleClick( Mobile from )
{
 if ( !IsChildOf( from.Backpack ) )
 {
  from.SendMessage( "Book must be in your backpack to use it." );
 }
 else if ( m_Entries.Count == 0 )
 {
  from.SendLocalizedMessage( 1062381 );
 }
 else if ( from is PlayerMobile )
 {
  from.CloseGump( typeof( PSBookGump ) );
  from.SendGump( new PSBookGump( from, this ) );
 }
}
public override bool OnDragDrop( Mobile from, Item dropped )
{
 if ( dropped is PowerScroll )
 {
  if ( !IsChildOf( from.Backpack ) )
  {
   from.SendMessage( "Book must be in your backpack to use it." );
   return false;
  }
  else if ( m_Entries.Count < 20 )//will hold 20
  {
   PowerScroll scroll = (PowerScroll)dropped;
   this.Entries.Add(scroll);

   InvalidateProperties();

   from.SendMessage( "Scroll added to book." );

   if ( from is PlayerMobile )
   {
    from.CloseGump( typeof( PSBookGump ) );
    from.SendGump( new PSBookGump( from, this ) );
   }

   dropped.Delete();
   return true;
  }
  else
  {
   from.SendMessage( "The book is full." );
   return false;
  }
 }

 from.SendMessage( "That is not a powerscroll." );
 return false;
}
}



public override void OnDelete()
      {
          base.OnDelete();
          foreach (PowerScroll s in m_Entries)
          {
              if(s != null)
                  s.Delete();
          }
      }

#endregion
#region Gump
public class PSBookGump : Gump
{
private int y;
public Mobile m_From;
public PSBook m_Book;

public PSBookGump(Mobile from, PSBook book) : base( 0, 0 )
{
 m_From = from;
 m_Book = book;

 y = (m_Book.Entries.Count -1)*20;

 Closable=true;
 Disposable=true;
 Dragable=true;
 Resizable=false;
 AddPage(0);
 AddBackground(10, 50, 230, 113+y, 9250);
 AddImageTiled( 24, 65, 200, 85+y, 2053);  
 AddAlphaRegion(24, 65, 200, 85+y);
 AddImageTiled(24, 87, 200, 10, 9264);
 AddImageTiled(24, 120, 200, 10, 9264);
 AddImageTiled(105, 95, 3, 55+y, 9264);
 AddImageTiled(179, 95, 3, 55+y, 9264);

 AddLabel(65, 65, 190, "PowerScroll Book");
 AddLabel(30, 100, 199, "Skill");
 AddLabel(110, 100, 199, "Value");
 AddLabel(185, 100, 199, "Drop");
 
 int y2 = 0;
 int butNumb = 1;
 for(int i = 0; i < m_Book.Entries.Count; i++)
 {
  PowerScroll scroll = m_Book.Entries[i] as PowerScroll;
  AddLabel(25, 130+y2, 195, scroll.Skill.ToString());
  AddLabel(110, 130+y2, 195, scroll.Value.ToString());
  AddButton(195, 133+y2, 1209, 1210, butNumb, GumpButtonType.Reply, 0);
  y2+=20;
  butNumb++;
 }
}  
public override void OnResponse( Server.Network.NetState sender, RelayInfo info )
{
 int bp;//button pushed
 switch(info.ButtonID)
 {
  case 1:bp = 0;break;
  case 2:bp = 1;break;
  case 3:bp = 2;break;
  case 4:bp = 3;break;
  case 5:bp = 4;break;
  case 6:bp = 5;break;
  case 7:bp = 6;break;
  case 8:bp = 7;break;
  case 9:bp = 8;break;
  case 10:bp = 9;break;
  case 11:bp = 10;break;
  case 12:bp = 11;break;
  case 13:bp = 12;break;
  case 14:bp = 13;break;
  case 15:bp = 14;break;
  case 16:bp = 15;break;
  case 17:bp = 16;break;
  case 18:bp = 17;break;
  case 19:bp = 18;break;
  case 20:bp = 19;break;
  default:return;break;
 }

 PowerScroll scroll = m_Book.Entries[bp] as PowerScroll;
 SkillName sklnm = scroll.Skill;
 double sklval = scroll.Value;
 PowerScroll newScroll = new PowerScroll(sklnm, sklval);
 m_From.AddToBackpack(newScroll);
 m_Book.Entries.RemoveAt(bp);
 m_Book.InvalidateProperties();
}
}
#endregion
}




So is this right? I tried adding in what you said where I thought it went..I am sorry to be so dumb but I do soo appreicate the help too..but you know that

ArteGordon- 08-01-2006
it's a bit hard to tell, but I dont think that you put the OnDelete override inside of the PSBook class definition.
It looks like it is after the last closing brace for the PSBook class.