Token Requester

jayates

Citizen
Was wondering if someone could help me with this? This token requester is suppossed to have 8 charges and everytime you use it, it will say how many charges left. Well, it always says 7. Also, was wanting it to self delete after the charges are used up.
C#:
using System;
using System.Collections;                                                     
using Server;                                                                     
using Server.Items;                                                               
using Server.Mobiles;                                                             
using Server.Network;                                                             
namespace Server.Items                                                           
{                                                                               
    public class TokenRequester : GoldBracelet                                 
    {                                                                             
        private int m_Charges;                                                     
        private DateTime m_NextRecharge;                                         
        private DateTime m_NextUse;                                             
        private DateTime m_ActiveUntil;                                           

        [Constructable]                                                         
        public TokenRequester() : base()                                 
        {
            Name = "a Token Requester";
            ItemID = 12122;   //(Talisman)
            Layer = Layer.Talisman;
            m_NextRecharge = DateTime.Now + TimeSpan.FromHours( 1.0 );
            m_Charges = 8;
            m_NextUse = DateTime.Now;
        }

        public override void OnDoubleClick( Mobile from )
        {
            if ( m_NextRecharge < DateTime.Now )
            {
                this.m_Charges = 8;
                m_NextRecharge = DateTime.Now + TimeSpan.FromHours( 1.0 );
            }
            if ( (from.FindItemOnLayer( Layer.Talisman ) is TokenRequester) && m_Charges > 0 && m_NextUse < DateTime.Now )
            {
                from.SendMessage( 0, "You blow the  Token Requester and an NPC hears your call... some tokens have been bestowed upon thee! Charges left: {0}", m_Charges - 1 );
                from.AddToBackpack( new Token() );
                from.AddToBackpack( new Token() );
                from.AddToBackpack( new Token() );
                from.AddToBackpack( new Token() );
                from.AddToBackpack( new Token() );             
                from.PlaySound( 1460 );    //horn blowing

                m_NextUse = DateTime.Now + TimeSpan.FromHours( 1.0 );  //FromMinutes( 1.0 );
                m_Charges -= 1;
            }
            else if ( (from.FindItemOnLayer( Layer.Talisman ) is TokenRequester) && this.m_Charges > 0 )
                from.SendMessage( 0, "Someone hears your request! But, they are very busy ...you must wait. Time left: {0}", m_NextUse - DateTime.Now );
            else if ( from.FindItemOnLayer( Layer.Talisman ) is TokenRequester )
                from.SendMessage( 0, "The  Requester is very worn. Time left for self repair: {0}", m_NextRecharge - DateTime.Now );
            else
                from.SendMessage( 0, "That must be equipped before you can use it!" );
        }

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

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

            writer.Write( (int) 0 );
            writer.Write( (int) m_Charges );
            writer.Write( (DateTime) m_NextUse );
            writer.Write( (DateTime) m_NextRecharge );
        }


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

            int version = reader.ReadInt();
            m_Charges = reader.ReadInt();
            m_NextUse = reader.ReadDateTime();
            m_NextRecharge = reader.ReadDateTime(); 
        }
    }
}
 
Last edited by a moderator:

DragnMaw

Citizen
As far as I can see, the Charge time to regain a charge, is the same time as the Use time, so at the exact time you can use it, you should have 8 charges left, so it would drop back down to 7.
 

TheArt

Rookie
Look at your script, below this line that you have here:
m_NextUse = DateTime.Now + TimeSpan.FromHours( 1.0 ); //FromMinutes( 1.0 );


There is a peculiar way the next line looks....

m_Charges -= 1;

Is it supposed to have a - before the =?

Because if not, that might be your problem.
 

PyrO

Moderator
Well there is an logical error in there.
First of all you have 8 charges. Wich is fine

If the Item is double clicked after 1 hour, it resets its charges to 8

then if it is set to 8 the next if takes a look.

You first send the message with charges - 1 (thats your 7)

then you give the items, followed by resetting the delay for NextUse, wich is also an hour.

So the next time you want to use the bracelet it resets the charges to 8 first, and then gives the 8 as the -1

I didnt change anything on the timing for the recharge nor the use, you would need to balance that out on your own.
I made some static variables, wich makes it just one place to change each.
C#:
using System;

namespace Server.Items
{
    public class TokenRequester : GoldBracelet
    {
        private static int MaxCharges = 8;
        private static TimeSpan DelayRecharge = TimeSpan.FromHours(1);
        private static TimeSpan DelayNextUse = TimeSpan.FromHours(1);

        private int m_Charges;
        private DateTime m_NextRecharge;
        private DateTime m_NextUse;

        public override string DefaultName { get { return "a Token Requester"; } }

        [Constructable]
        public TokenRequester() : base()
        {
            ItemID = 12122;   //(Talisman)
            Layer = Layer.Talisman;
            m_Charges = MaxCharges;
            m_NextRecharge = DateTime.UtcNow + DelayRecharge;
            m_NextUse = DateTime.UtcNow;
        }

        public override void OnDoubleClick(Mobile from)
        {
            if (m_NextRecharge < DateTime.UtcNow)
            {
                m_Charges = MaxCharges;
                m_NextRecharge = DateTime.UtcNow + DelayRecharge;
            }
            if (from.FindItemOnLayer(Layer.Talisman) is TokenRequester)
            {
                if (m_Charges > 0 && m_NextUse < DateTime.UtcNow)
                {
                    from.AddToBackpack(new Token());
                    from.AddToBackpack(new Token());
                    from.AddToBackpack(new Token());
                    from.AddToBackpack(new Token());
                    from.AddToBackpack(new Token());
                    from.PlaySound(1460);    //horn blowing

                    m_NextUse = DateTime.UtcNow + DelayNextUse;  //FromMinutes( 1.0 );
                    m_Charges--;
                    from.SendMessage(0, "You blow the  Token Requester and an NPC hears your call... some tokens have been bestowed upon thee! Charges left: {0}", m_Charges);
                }
                else if (m_Charges > 0)
                    from.SendMessage(0, "Someone hears your request! But, they are very busy ...you must wait. Time left: {0}", m_NextUse - DateTime.UtcNow);
                else
                    from.SendMessage(0, "The  Requester is very worn. Time left for self repair: {0}", m_NextRecharge - DateTime.UtcNow);
            }
            else
                from.SendMessage(0, "That must be equipped before you can use it!");
        }

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

        public override void Serialize(GenericWriter writer)
        {
            base.Serialize(writer);
            writer.Write(0); // version

            writer.Write(m_Charges);
            writer.Write(m_NextUse);
            writer.Write(m_NextRecharge);
        }

        public override void Deserialize(GenericReader reader)
        {
            base.Deserialize(reader);
            int version = reader.ReadInt();

            m_Charges = reader.ReadInt();
            m_NextUse = reader.ReadDateTime();
            m_NextRecharge = reader.ReadDateTime();
        }
    }
}