SolvedMoveTo trouble

Posts 1–11 of 11 · Page 1 of 1
MoveTo trouble
In my head I thought this would work. For MoveTo.cs you would check if it's 1 square away, then once that's completed you would check for a smaller double (ex: 0.5). Didn't go as planned, they would jitter across the screen and i'd say 75% of the time they would be 0.1 squares away from the xy coordinate. Honstly if someone had a better idea than this that would be great! PM me instead of a post so leachers don't leach (that's if you want to).

Note: the code below is messy and is not stable, don't use it.

PS. this code is just a test and the function "once" is not functional.

Code:
using Mono.Game;
using System;
using wServer.realm;

namespace wServer.logic.behaviors
{
    public class MoveTo : CycleBehavior
    {
        private readonly float speed;
        private readonly float speed2;
        private readonly float baseX;
        private readonly float baseY;
        private readonly bool isMapPosition;
        private readonly bool instant;
        private bool once;
        private bool returned;
        private float X;
        private float Y;
        
        public MoveTo(float X, float Y, double speed = 2, bool once = false, bool isMapPosition = false, bool instant=false)
        {
            this.isMapPosition = isMapPosition;
            this.speed = (float)speed;
            this.speed2 = (float)speed / 8;
            this.once = once;
            this.instant = instant;
            baseX = X;
            baseY = Y;
        }

        protected override void OnStateEntry(Entity host, RealmTime time, ref object state)
        {
            if (!isMapPosition)
            {
                X = baseX + host.X;
                Y = baseY + host.Y;
            }
            else
            {
                X = baseX;
                Y = baseY;
            }

            if (instant)
            {
                host.Move(X, Y);
                host.UpdateCount++;
            }
        }

        protected override void TickCore(Entity host, RealmTime time, ref object state)
        {
            if (instant) return;
            if (!returned)
            {
                if (host.HasConditionEffect(ConditionEffectIndex.Paralyzed)) return;
                var spd = host.GetSpeed(speed) * (time.thisTickTimes / 1000f);
                var spd2 = host.GetSpeed(speed2) * (time.thisTickTimes / 1000f);

                if (Math.Abs(X - host.X) > 0.5 || Math.Abs(Y - host.Y) > 0.5)
                {
                    Vector2 vect = new Vector2(X, Y) - new Vector2(host.X, host.Y);
                    vect.Normalize();
                    vect *= spd;
                    host.ValidateAndMove(host.X + vect.X, host.Y + vect.Y);
                    host.UpdateCount++;

                    if (host.X == X && host.Y == Y)
                    {
                        if (Math.Abs(X - host.X) > 0.4 || Math.Abs(Y - host.Y) > 0.4)
                        {
                            Vector2 vect0 = new Vector2(X, Y) - new Vector2(host.X, host.Y);
                            vect0.Normalize();
                            vect0 *= spd2;
                            host.ValidateAndMove(host.X + vect0.X, host.Y + vect0.Y);
                            host.UpdateCount++;

                            if (host.X == X && host.Y == Y)
                            {
                                if (Math.Abs(X - host.X) > 0.3 || Math.Abs(Y - host.Y) > 0.3)
                                {
                                    Vector2 vect1 = new Vector2(X, Y) - new Vector2(host.X, host.Y);
                                    vect1.Normalize();
                                    vect1 *= spd2;
                                    host.ValidateAndMove(host.X + vect1.X, host.Y + vect1.Y);
                                    host.UpdateCount++;

                                    if (host.X == X && host.Y == Y)
                                    {
                                        if (Math.Abs(X - host.X) > 0.2 || Math.Abs(Y - host.Y) > 0.2)
                                        {
                                            Vector2 vect2 = new Vector2(X, Y) - new Vector2(host.X, host.Y);
                                            vect2.Normalize();
                                            vect2 *= spd2;
                                            host.ValidateAndMove(host.X + vect2.X, host.Y + vect2.Y);
                                            host.UpdateCount++;

                                            if (host.X == X && host.Y == Y)
                                            {
                                                if (Math.Abs(X - host.X) > 0.1 || Math.Abs(Y - host.Y) > 0.1)
                                                {
                                                    Vector2 vect3 = new Vector2(X, Y) - new Vector2(host.X, host.Y);
                                                    vect3.Normalize();
                                                    vect3 *= spd2;
                                                    host.ValidateAndMove(host.X + vect3.X, host.Y + vect3.Y);
                                                    host.UpdateCount++;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
You should probably explain what you're trying to do.
Quote Originally Posted by CrazyJani View Post
You should probably explain what you're trying to do.
Make the enemy move to a certain X Y position precisely. Not that inaccurate crap.
put mapposition to true, and make X and Y the place u want it to go, lets say if ur standing on a tile, and does /p (the position command) it will say like X: 50, Y: 100, so if mapposition is set to true, then just put X as 50.5f, and Y as 100.5f. Then it will go ahead and move to that exact point.
Quote Originally Posted by MikeRaarupBirk View Post
put mapposition to true, and make X and Y the place u want it to go, lets say if ur standing on a tile, and does /p (the position command) it will say like X: 50, Y: 100, so if mapposition is set to true, then just put X as 50.5f, and Y as 100.5f. Then it will go ahead and move to that exact point.
You forgot the part about speed. The whole reason why it's not precise is because it's not an instant thing. The problem is that once it gets around 0.75 tiles away from it's destination it will stop there or sometimes it will miss it completely.
Quote Originally Posted by toby2449 View Post
You forgot the part about speed. The whole reason why it's not precise is because it's not an instant thing. The problem is that once it gets around 0.75 tiles away from it's destination it will stop there or sometimes it will miss it completely.
That's FSoD for you.
Quote Originally Posted by Invader_Zim View Post
That's FSoD for you.
Its not just fsod. Every source with the returntospawn behavior is like that.
I still need help...
bump......
Bump

/2short
Quote Originally Posted by toby2449 View Post
In my head I thought this would work. For MoveTo.cs you would check if it's 1 square away, then once that's completed you would check for a smaller double (ex: 0.5). Didn't go as planned, they would jitter across the screen and i'd say 75% of the time they would be 0.1 squares away from the xy coordinate. Honstly if someone had a better idea than this that would be great! PM me instead of a post so leachers don't leach (that's if you want to).

Note: the code below is messy and is not stable, don't use it.

PS. this code is just a test and the function "once" is not functional.

Code:
using Mono.Game;
using System;
using wServer.realm;

namespace wServer.logic.behaviors
{
    public class MoveTo : CycleBehavior
    {
        private readonly float speed;
        private readonly float speed2;
        private readonly float baseX;
        private readonly float baseY;
        private readonly bool isMapPosition;
        private readonly bool instant;
        private bool once;
        private bool returned;
        private float X;
        private float Y;
        
        public MoveTo(float X, float Y, double speed = 2, bool once = false, bool isMapPosition = false, bool instant=false)
        {
            this.isMapPosition = isMapPosition;
            this.speed = (float)speed;
            this.speed2 = (float)speed / 8;
            this.once = once;
            this.instant = instant;
            baseX = X;
            baseY = Y;
        }

        protected override void OnStateEntry(Entity host, RealmTime time, ref object state)
        {
            if (!isMapPosition)
            {
                X = baseX + host.X;
                Y = baseY + host.Y;
            }
            else
            {
                X = baseX;
                Y = baseY;
            }

            if (instant)
            {
                host.Move(X, Y);
                host.UpdateCount++;
            }
        }

        protected override void TickCore(Entity host, RealmTime time, ref object state)
        {
            if (instant) return;
            if (!returned)
            {
                if (host.HasConditionEffect(ConditionEffectIndex.Paralyzed)) return;
                var spd = host.GetSpeed(speed) * (time.thisTickTimes / 1000f);
                var spd2 = host.GetSpeed(speed2) * (time.thisTickTimes / 1000f);

                if (Math.Abs(X - host.X) > 0.5 || Math.Abs(Y - host.Y) > 0.5)
                {
                    Vector2 vect = new Vector2(X, Y) - new Vector2(host.X, host.Y);
                    vect.Normalize();
                    vect *= spd;
                    host.ValidateAndMove(host.X + vect.X, host.Y + vect.Y);
                    host.UpdateCount++;

                    if (host.X == X && host.Y == Y)
                    {
                        if (Math.Abs(X - host.X) > 0.4 || Math.Abs(Y - host.Y) > 0.4)
                        {
                            Vector2 vect0 = new Vector2(X, Y) - new Vector2(host.X, host.Y);
                            vect0.Normalize();
                            vect0 *= spd2;
                            host.ValidateAndMove(host.X + vect0.X, host.Y + vect0.Y);
                            host.UpdateCount++;

                            if (host.X == X && host.Y == Y)
                            {
                                if (Math.Abs(X - host.X) > 0.3 || Math.Abs(Y - host.Y) > 0.3)
                                {
                                    Vector2 vect1 = new Vector2(X, Y) - new Vector2(host.X, host.Y);
                                    vect1.Normalize();
                                    vect1 *= spd2;
                                    host.ValidateAndMove(host.X + vect1.X, host.Y + vect1.Y);
                                    host.UpdateCount++;

                                    if (host.X == X && host.Y == Y)
                                    {
                                        if (Math.Abs(X - host.X) > 0.2 || Math.Abs(Y - host.Y) > 0.2)
                                        {
                                            Vector2 vect2 = new Vector2(X, Y) - new Vector2(host.X, host.Y);
                                            vect2.Normalize();
                                            vect2 *= spd2;
                                            host.ValidateAndMove(host.X + vect2.X, host.Y + vect2.Y);
                                            host.UpdateCount++;

                                            if (host.X == X && host.Y == Y)
                                            {
                                                if (Math.Abs(X - host.X) > 0.1 || Math.Abs(Y - host.Y) > 0.1)
                                                {
                                                    Vector2 vect3 = new Vector2(X, Y) - new Vector2(host.X, host.Y);
                                                    vect3.Normalize();
                                                    vect3 *= spd2;
                                                    host.ValidateAndMove(host.X + vect3.X, host.Y + vect3.Y);
                                                    host.UpdateCount++;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
you should use statestorage to store the variables, rather than the class
Posts 1–11 of 11 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us