Exception:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.ThrowHelper.ThrowArgumentOutOfRangeException()
at System.Collections.Generic.List`1.get_Item(Int32 index)
at System.Collections.Generic.List`1.System.Collections.IList.get_Item(Int32 index)
at Server.Map.TypedEnumerator.MoveNext()
at Server.Spells.Third.WallOfStoneSpell.Target(IPoint3D p) in c:\Scripts\Spells\Third\WallOfStone.cs:line 123
at Server.Spells.Third.WallOfStoneSpell.OnPlayerCast() in c:\Scripts\Spells\Third\WallOfStone.cs:line 31
at Server.Spells.Spell.CastTimer.OnTick() in c:\Users\FUCK YOU\Desktop\servidor de Testeo\22-03-2018 servidor de testeo\Scripts\Spells\Base\Spell.cs:line 1363
at Server.Timer.Slice()
at Server.Core.Main(String[] args)


Code:
using System;
using Server.Items;
using Server.Misc;
using Server.Targeting;
using Server.Regions;

namespace Server.Spells.Third
{
    public class WallOfStoneSpell : MagerySpell
    {
        public override SpellCircle Circle { get { return SpellCircle.Third; } }
    //    public override bool DispellableFieldAttribute { get { return true; } }
        public override int Sound { get { return 0x1F6; } }
    
        public override bool CanTargetGround { get { return true; } }

        private static readonly SpellInfo m_Info = new SpellInfo(
                "Wall of Stone", "In Sanct Ylem",
                263,//269
                9011,
                Reagent.Bloodmoss,
                Reagent.Garlic
            );

        public WallOfStoneSpell( Mobile caster, Item scroll ) : base( caster, scroll, m_Info )
        {
        }

        public override void OnPlayerCast()
        {
            Target((IPoint3D)SphereSpellTarget);
        }

        public override void OnCast()
        {
            Caster.Target = new InternalTarget( this );
        }

        public void Target( IPoint3D p )
        {
            if ( !Caster.CanSee( p ) )
            {
                Caster.SendLocalizedMessage( 500237 ); // Target can not be seen.
            }
            else if (SphereSpellTarget is BaseWand)
            {
                BaseWand bw = SphereSpellTarget as BaseWand;
                bw.RechargeWand(Caster, this);
            }
            else if ( CheckSequence() )
            {

                SpellHelper.GetSurfaceTop( ref p );

                int dx = Caster.Location.X - p.X;
                int dy = Caster.Location.Y - p.Y;
                int rx = (dx - dy) * 44;
                int ry = (dx + dy) * 44;

                bool eastToWest;

                if ( rx >= 0 && ry >= 0 )
                {
                    eastToWest = false;
                }
                else if ( rx >= 0 )
                {
                    eastToWest = true;
                }
                else if ( ry >= 0 )
                {
                    eastToWest = true;
                }
                else
                {
                    eastToWest = false;
                }

                Effects.PlaySound( p, Caster.Map, Sound );

                //Loki edit: Wall length reduced
                CustomRegion cR = Caster.Region as CustomRegion;
                int wallsize;
                if (cR != null && cR.Controller.LokiPvP)
                    wallsize = 2;
                else
                    wallsize = 3;

                for (int i = -wallsize; i <= wallsize; ++i) //End Loki edit: Was: int i = -3; i <= 3; ++i
                {
                   
                    Point3D loc = new Point3D(eastToWest ? p.X + i : p.X, eastToWest ? p.Y : p.Y + i, p.Z);
                    /*bool canFit = SpellHelper.AdjustField(ref loc, Caster.Map, 22, true);

                    if (!canFit)
                        continue;*/

                    IPooledEnumerable eable = Caster.Map.GetMobilesInRange(loc, 0);
                    bool canFit = true;

                    foreach (Mobile m in eable)
                    {
                        if (m.AccessLevel != AccessLevel.Player || !m.Alive)
                            continue;
                    
                        if (m.Location.Z - loc.Z < 18 && m.Location.Z - loc.Z > -10)
                        {
                           
                            Caster.DoHarmful(m);
                           
                            canFit = false;
                            break;
                        }
                    }

                    eable.Free();

                    if (!canFit)
                        continue;

                    //remove existing wall items
                    eable = Caster.Map.GetItemsInRange(loc, 0);
                    foreach (Item item in eable)
                    {
                        if (item is InternalItem)
                            item.Delete();
                    }
                    eable.Free();

                    new InternalItem( loc, Caster.Map, Caster );
                }
            }

            FinishSequence();
        }

        [DispellableField]
        private class InternalItem : Item
        {
            private Timer m_Timer;
            private DateTime m_End;

            //public override bool BlocksFit{ get{ return true; } }

            public InternalItem( Point3D loc, Map map, Mobile caster ) : base( 0x80 )
            {
                //Visible = false;
                Visible = true;
                Movable = false;

                MoveToWorld( loc, map );

                /*if ( caster.InLOS( this ) )
                    Visible = true;
                else
                    Delete();

                if ( Deleted )
                    return;*/

                /* edit: This was probably meant to be Utility.RandomMinMax()
                 
                int timespan = Utility.Random(60, 120); //sphere style.
                 */
                int timespan = Utility.RandomMinMax(50, 70);

                m_Timer = new InternalTimer( this, TimeSpan.FromSeconds( timespan) );
                m_Timer.Start();

                m_End = DateTime.Now + TimeSpan.FromSeconds( timespan );
            }

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

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

                writer.Write( 1 ); // version

                writer.WriteDeltaTime( m_End );
            }

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

                int version = reader.ReadInt();

                switch ( version )
                {
                    case 1:
                    {
                        m_End = reader.ReadDeltaTime();

                        m_Timer = new InternalTimer( this, m_End - DateTime.Now );
                        m_Timer.Start();

                        break;
                    }
                    case 0:
                    {
                        TimeSpan duration = TimeSpan.FromSeconds( 10.0 );

                        m_Timer = new InternalTimer( this, duration );
                        m_Timer.Start();

                        m_End = DateTime.Now + duration;

                        break;
                    }
                }
            }

            public override void OnAfterDelete()
            {
                base.OnAfterDelete();

                if ( m_Timer != null )
                    m_Timer.Stop();
            }

            private class InternalTimer : Timer
            {
                private readonly InternalItem m_Item;

                public InternalTimer( InternalItem item, TimeSpan duration ) : base( duration )
                {
                    Priority = TimerPriority.OneSecond;
                    m_Item = item;
                }

                protected override void OnTick()
                {
                    m_Item.Delete();
                }
            }
        }

        private class InternalTarget : Target
        {
            private readonly WallOfStoneSpell m_Owner;

            public InternalTarget( WallOfStoneSpell owner ) : base( 12, true, TargetFlags.None )
            {
                m_Owner = owner;
            }

            protected override void OnTarget( Mobile from, object o )
            {
                if ( o is IPoint3D )
                    m_Owner.Target( (IPoint3D)o );
            }

            protected override void OnTargetFinish( Mobile from )
            {
                m_Owner.FinishSequence();
            }
        }
    }
}

This happened on britain bridge, i removed a certain part then added the tiles and [set movable false to them, whenever someone drops an item there they fall under the bridge


this might be the issue with crashes, Voxpire asked me if i modified the map core
 
Last edited:
Don't delete items from a collection in a foreach loop that enumerates the same collection.
 
If you need to delete them, you can add them to another collection, then go through each one in a for loop in reverse order, and delete them that way. If you google delete items from an enumeration using for or foreach, you should find what you need.
 
Try this instead of the foreach loop:

Code:
for (int i = eable.Count - 1; i >= 0; --i)
    if (eable[i] is InternalItem)
        eable(i).Delete();
 
Try this instead of the foreach loop:

Code:
for (int i = eable.Count - 1; i >= 0; --i)
    if (eable[i] is InternalItem)
        eable(i).Delete();

it gives me errors:


CS0136: Line 129: A local variable named 'i' cannot be declared in this scop
e because it would give a different meaning to 'i', which is already used in a '
parent or current' scope to denote something else
CS1061: Line 129: 'Server.IPooledEnumerable' does not contain a definition f
or 'Count' and no extension method 'Count' accepting a first argument of type 'S
erver.IPooledEnumerable' could be found (are you missing a using directive or an
assembly reference?)
CS0021: Line 130: Cannot apply indexing with [] to an expression of type 'Se
rver.IPooledEnumerable'
CS0118: Line 131: 'eable' is a 'variable' but is used like a 'method'



tried to change ''i'' to ''e'' and got different errors:


CS1061: Line 129: 'Server.IPooledEnumerable' does not contain a definition f
or 'Count' and no extension method 'Count' accepting a first argument of type 'S
erver.IPooledEnumerable' could be found (are you missing a using directive or an
assembly reference?)
CS0021: Line 130: Cannot apply indexing with [] to an expression of type 'Se
rver.IPooledEnumerable'
CS0118: Line 131: 'eable' is a 'variable' but is used like a 'method'
 
Last edited:
OK, try this one then:

Code:
                    //remove existing wall items
                    eable = Caster.Map.GetItemsInRange(loc, 0);
                    List<Item> itemsToSearch = eable.ToList();
                    for (int i = itemsToSearch.Count - 1; i >= 0; --i)
                        if (itemsToSearch[i] is InternalItem)
                            itemsToSearch(i).Delete();
                    eable.Free();

I did not remember that GetItemsInRange was using the IEnumerable type, which cannot be Counted. So, what we are doing here is copying it to a List<> first.

In order to use this code, which uses Linq, add this line to the top of the script:

Code:
using System.Linq;
 
OK, try this one then:

Code:
                    //remove existing wall items
                    eable = Caster.Map.GetItemsInRange(loc, 0);
                    List<Item> itemsToSearch = eable.ToList();
                    for (int i = itemsToSearch.Count - 1; i >= 0; --i)
                        if (itemsToSearch[i] is InternalItem)
                            itemsToSearch(i).Delete();
                    eable.Free();

I did not remember that GetItemsInRange was using the IEnumerable type, which cannot be Counted. So, what we are doing here is copying it to a List<> first.

In order to use this code, which uses Linq, add this line to the top of the script:

Code:
using System.Linq;

im being annoying i know, it still showing errors:


im using runuo 2.2 maybe that helps


CS0246: Line 133: The type or namespace name 'List' could not be found (are
you missing a using directive or an assembly reference?)
CS1061: Line 133: 'Server.IPooledEnumerable' does not contain a definition f
or 'ToList' and no extension method 'ToList' accepting a first argument of type
'Server.IPooledEnumerable' could be found (are you missing a using directive or
an assembly reference?)
CS0136: Line 134: A local variable named 'i' cannot be declared in this scop
e because it would give a different meaning to 'i', which is already used in a '
parent or current' scope to denote something else
 
OK, sorry, to get List<> to work, you need to add this to the top also:

Code:
using System.Collections.Generic;

I think that is right, it's off the top of my head.
 
need to say sorry! i added that,still got errors



CS1061: Line 135: 'Server.IPooledEnumerable' does not contain a definition f
or 'ToList' and no extension method 'ToList' accepting a first argument of type
'Server.IPooledEnumerable' could be found (are you missing a using directive or
an assembly reference?)
CS0136: Line 136: A local variable named 'i' cannot be declared in this scop
e because it would give a different meaning to 'i', which is already used in a '
parent or current' scope to denote something else
CS0118: Line 138: 'itemsToSearch' is a 'variable' but is used like a 'method
 
No problem. This is a process. Go ahead and post the full script again. Let's have another look.
 
ok :)


Code:
using System;
using Server.Items;
using Server.Misc;
using Server.Targeting;
using Server.Regions;
using System.Linq;
using System.Collections.Generic;
using System.Collections;

namespace Server.Spells.Third
{
    public class WallOfStoneSpell : MagerySpell
    {
        public override SpellCircle Circle { get { return SpellCircle.Third; } }
    //    public override bool DispellableFieldAttribute { get { return true; } }
        public override int Sound { get { return 0x1F6; } }
       
        public override bool CanTargetGround { get { return true; } }

        private static readonly SpellInfo m_Info = new SpellInfo(
                "Wall of Stone", "In Sanct Ylem",
                263,//269
                9011,
                Reagent.Bloodmoss,
                Reagent.Garlic
            );

        public WallOfStoneSpell( Mobile caster, Item scroll ) : base( caster, scroll, m_Info )
        {
        }

        public override void OnPlayerCast()
        {
            Target((IPoint3D)SphereSpellTarget);
        }

        public override void OnCast()
        {
            Caster.Target = new InternalTarget( this );
        }

        public void Target( IPoint3D p )
        {
            if ( !Caster.CanSee( p ) )
            {
                Caster.SendLocalizedMessage( 500237 ); // Target can not be seen.
            }
            else if (SphereSpellTarget is BaseWand)
            {
                BaseWand bw = SphereSpellTarget as BaseWand;
                bw.RechargeWand(Caster, this);
            }
            else if ( CheckSequence() )
            {

                SpellHelper.GetSurfaceTop( ref p );

                int dx = Caster.Location.X - p.X;
                int dy = Caster.Location.Y - p.Y;
                int rx = (dx - dy) * 44;
                int ry = (dx + dy) * 44;

                bool eastToWest;

                if ( rx >= 0 && ry >= 0 )
                {
                    eastToWest = false;
                }
                else if ( rx >= 0 )
                {
                    eastToWest = true;
                }
                else if ( ry >= 0 )
                {
                    eastToWest = true;
                }
                else
                {
                    eastToWest = false;
                }

                Effects.PlaySound( p, Caster.Map, Sound );

               //edit: Wall length reduced
                CustomRegion cR = Caster.Region as CustomRegion;
                int wallsize;
                if (cR != null && cR.Controller.LokiPvP)
                    wallsize = 2;
                else
                    wallsize = 3;

                for (int i = -wallsize; i <= wallsize; ++i) //End Loki edit: Was: int i = -3; i <= 3; ++i
                {
                    // Always fit
                    Point3D loc = new Point3D(eastToWest ? p.X + i : p.X, eastToWest ? p.Y : p.Y + i, p.Z);
                    /*bool canFit = SpellHelper.AdjustField(ref loc, Caster.Map, 22, true);

                    if (!canFit)
                        continue;*/

                    IPooledEnumerable eable = Caster.Map.GetMobilesInRange(loc, 0);
                    bool canFit = true;

                    foreach (Mobile m in eable)
                    {
                        if (m.AccessLevel != AccessLevel.Player || !m.Alive)
                            continue;
                       
                        if (m.Location.Z - loc.Z < 18 && m.Location.Z - loc.Z > -10)
                        {
                            //The whole field counts as a harmful action, not just the target
                            Caster.DoHarmful(m);
                            //Make a hole in the wall if a mobile is there
                            canFit = false;
                            break;
                        }
                    }

                    eable.Free();

                    if (!canFit)
                        continue;

                    //edit: Delete existing wall items
                    /*eable = Caster.Map.GetItemsInRange(loc, 0);
                    foreach (Item item in eable)
                    {
                        if (item is InternalItem)
                            item.Delete();
                    }
                    eable.Free();*/
                   
      //remove existing wall items
                    eable = Caster.Map.GetItemsInRange(loc, 0);
                    List<Item> itemsToSearch = eable.ToList();
                    for (int i = itemsToSearch.Count - 1; i >= 0; --i)
                        if (itemsToSearch[i] is InternalItem)
                            itemsToSearch(i).Delete();
                    eable.Free();

                    new InternalItem( loc, Caster.Map, Caster );
                }
            }

            FinishSequence();
        }

        [DispellableField]
        private class InternalItem : Item
        {
            private Timer m_Timer;
            private DateTime m_End;

            //public override bool BlocksFit{ get{ return true; } }

            public InternalItem( Point3D loc, Map map, Mobile caster ) : base( 0x80 )
            {
                //Visible = false;
                Visible = true;
                Movable = false;

                MoveToWorld( loc, map );

                /*if ( caster.InLOS( this ) )
                    Visible = true;
                else
                    Delete();

                if ( Deleted )
                    return;*/

            
                
                int timespan = Utility.RandomMinMax(50, 70);

                m_Timer = new InternalTimer( this, TimeSpan.FromSeconds( timespan) );
                m_Timer.Start();

                m_End = DateTime.Now + TimeSpan.FromSeconds( timespan );
            }

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

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

                writer.Write( 1 ); // version

                writer.WriteDeltaTime( m_End );
            }

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

                int version = reader.ReadInt();

                switch ( version )
                {
                    case 1:
                    {
                        m_End = reader.ReadDeltaTime();

                        m_Timer = new InternalTimer( this, m_End - DateTime.Now );
                        m_Timer.Start();

                        break;
                    }
                    case 0:
                    {
                        TimeSpan duration = TimeSpan.FromSeconds( 10.0 );

                        m_Timer = new InternalTimer( this, duration );
                        m_Timer.Start();

                        m_End = DateTime.Now + duration;

                        break;
                    }
                }
            }

            public override void OnAfterDelete()
            {
                base.OnAfterDelete();

                if ( m_Timer != null )
                    m_Timer.Stop();
            }

            private class InternalTimer : Timer
            {
                private readonly InternalItem m_Item;

                public InternalTimer( InternalItem item, TimeSpan duration ) : base( duration )
                {
                    Priority = TimerPriority.OneSecond;
                    m_Item = item;
                }

                protected override void OnTick()
                {
                    m_Item.Delete();
                }
            }
        }

        private class InternalTarget : Target
        {
            private readonly WallOfStoneSpell m_Owner;

            public InternalTarget( WallOfStoneSpell owner ) : base( 12, true, TargetFlags.None )
            {
                m_Owner = owner;
            }

            protected override void OnTarget( Mobile from, object o )
            {
                if ( o is IPoint3D )
                    m_Owner.Target( (IPoint3D)o );
            }

            protected override void OnTargetFinish( Mobile from )
            {
                m_Owner.FinishSequence();
            }
        }
    }
}
 

Attachments

  • Wallofstone.cs
    6.8 KB · Views: 2
Code:
    CS1061: Line 135: 'System.Collections.IEnumerable' does not contain a defini
tion for 'ToList' and no extension method 'ToList' accepting a first argument of
type 'System.Collections.IEnumerable' could be found (are you missing a using d
irective or an assembly reference?)
    CS0118: Line 138: 'itemsToSearch' is a 'variable' but is used like a 'method

damn it
 
that one went away!

theres one remaining

line
Code:
itemsToSearch(j).Delete();

CS0118: Line 139: 'itemsToSearch' is a 'variable' but is used like a 'method
 
Oops. That was a typo. Wrong type of bracket. Should be this:

Code:
itemsToSearch[j].Delete();
 
I cast a wall and it crash :D


Exception:
System.InvalidCastException: Unable to cast object of type 'PooledEnumerable' to type 'System.Collections.Generic.IEnumerable`1[Server.Item]'.
at Server.Spells.Third.WallOfStoneSpell.Target(IPoint3D p) in c:\Scripts\Spells\Third\WallOfStone.cs:line 136
at Server.Spells.Third.WallOfStoneSpell.OnPlayerCast() in c:\Scripts\Spells\Third\WallOfStone.cs:line 35
at Server.Spells.Spell.CastTimer.OnTick() in c:\\Scripts\Spells\Base\Spell.cs:line 1363
at Server.Timer.Slice()
at Server.Core.Main(String[] args)
 
If you need to delete them, you can add them to another collection, then go through each one in a for loop in reverse order, and delete them that way. If you google delete items from an enumeration using for or foreach, you should find what you need.

I should have listened to my first instinct. I was trying to be too fancy with Linq and all. Oh well.
 
Back