[Help] Calculation of coordinates in dungeons

04/17/2018 20:42 torstmn#1
Hey guys,
it has been long since I was active for the last time but now I try to continue developing my bot (RSBot). During development I had the following issue:

I have a class "Postion" that tries to accomplish the task of calculating wether offsets or "real" coordinates. It does that very well but as soon as I enter a dungeon the calculation fu**ed up a little. This is not caused by missinterpreted packetsbut I think it's the calculation of the XCoordinate (absolute position).
Is there a different formular for dungeon coordinates I don't know about maybe?

Since im no genius in math, it would be great if you guys could help me a little.

I will attach the Position.cs class to this post.

In addition to that, I want to give you a little code example:

Step 1: Reading the packet
Code:
                result.Destination = new Position
                {
                    XSector = packet.ReadByte(),
                    YSector = packet.ReadByte(),
                };

                if (result.Destination.RegionID < short.MaxValue)
                {
                    result.Destination.XOffset = packet.ReadShort();
                    result.Destination.YOffset = packet.ReadShort();
                    result.Destination.ZOffset = packet.ReadShort();
                }
                else
                {
                    //The player is in a dungeon
                    result.Destination.XOffset = packet.ReadShort() - packet.ReadShort();
                    result.Destination.YOffset = packet.ReadShort() - packet.ReadShort();
                    result.Destination.ZOffset = packet.ReadShort() - packet.ReadShort();
                }
Step 2: Calculating the absolute X,Y Position:

Code:
        /// <summary>
        /// Gets or sets the x coordinate.
        /// </summary>
        /// <value>
        /// The x coordinate.
        /// </value>
        public float XCoordinate
        {
            get
            {
                return (XSector - 135) * 192 + (XOffset / 10);
            }
            set
            {
                XSector = XSectorFromX(value);
                XOffset = XOffsetFromX(value);
            }
        }

        /// <summary>
        /// Gets or sets the y coordinate.
        /// </summary>
        /// <value>
        /// The y coordinate.
        /// </value>
        public float YCoordinate
        {
            get
            {
                return (YSector - 92) * 192 + (ZOffset / 10);
            }
            set
            {
                YSector = YSectorFromY(value);
                ZOffset = YOffsetFromY(value);
            }
        }
Step 3: Failing
I actually have checked up the absolute positions inside a cave with phBot. Result are the following:
Pos-X: -24196.2
Pos-Y: 641.2

While my positions look like this:
Pos-X: -25540
Pos-Y: 7553

Here is the paste of the complete Position.cs file:
[Only registered and activated users can see links. Click Here To Register...]

So yeah, I realy could need your help here...

Thanks in advance
04/17/2018 21:15 qoaway#2
there isnt any diference on calculating gameworld coordinates but only x y z coordinates are in double-word (just read it as 32bit dont do readshort() - readshort())

edit:
also my advices to you are:
1-Do not split region to x sector & y sector. Store them as Region.
2-Change your coordinate namings: Y is used for depth, X Z is for 2D coordinate projection. (in packet order X-Y-Z)
3-"public bool IsInDungeon => YSector == 0x80;" dungeons are not identified by ysector == 128. Its identified by MSB of region. So you must use "public bool IsInDungeon => Region & 0x8000 != 0"
4-Don't use "class" for Position, use struct instead. In .NET classes are allocated with gcnew but structs are not if you don't do it especially. This decreases your performance.