From 706275738c26854ff1d9e0628b72aaaccca9f76d Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 17 Apr 2017 16:02:25 +1000 Subject: [PATCH] Write base code for mass position/orientation rewrite. This is where the fun begins --- .../Bots/Instructions/HunterInstructions.cs | 36 +++--- MCGalaxy/Bots/PlayerBot.cs | 12 +- MCGalaxy/Entity/Entity.cs | 107 ++++++++++++------ MCGalaxy/Player/Player.cs | 12 +- 4 files changed, 106 insertions(+), 61 deletions(-) diff --git a/MCGalaxy/Bots/Instructions/HunterInstructions.cs b/MCGalaxy/Bots/Instructions/HunterInstructions.cs index ce0611913..63087297a 100644 --- a/MCGalaxy/Bots/Instructions/HunterInstructions.cs +++ b/MCGalaxy/Bots/Instructions/HunterInstructions.cs @@ -27,7 +27,16 @@ namespace MCGalaxy.Bots { public override bool Execute(PlayerBot bot, InstructionData data) { int search = 75; if (data.Metadata != null) search = (ushort)data.Metadata; - int dist = search * 32; + Player closest = ClosestPlayer(bot, search); + + if (closest == null) { bot.NextInstruction(); return false; } + bool overlapsPlayer = MoveTowards(bot, closest); + if (overlapsPlayer) { bot.NextInstruction(); return false; } + return true; + } + + internal static Player ClosestPlayer(PlayerBot bot, int search) { + int maxDist = search * 32; Player[] players = PlayerInfo.Online.Items; Player closest = null; @@ -36,16 +45,12 @@ namespace MCGalaxy.Bots { int dx = p.pos[0] - bot.pos[0], dy = p.pos[1] - bot.pos[1], dz = p.pos[2] - bot.pos[2]; int playerDist = Math.Abs(dx) + Math.Abs(dy) + Math.Abs(dz); - if (playerDist >= dist) continue; + if (playerDist >= maxDist) continue; closest = p; - dist = playerDist; + maxDist = playerDist; } - - if (closest == null) { bot.NextInstruction(); return false; } - bool overlapsPlayer = MoveTowards(bot, closest); - if (overlapsPlayer) { bot.NextInstruction(); return false; } - return true; + return closest; } static bool MoveTowards(PlayerBot bot, Player p) { @@ -126,20 +131,7 @@ namespace MCGalaxy.Bots { public override bool Execute(PlayerBot bot, InstructionData data) { int search = 20000; if (data.Metadata != null) search = (ushort)data.Metadata; - int dist = search * 32; - Player[] players = PlayerInfo.Online.Items; - Player closest = null; - - foreach (Player p in players) { - if (p.level != bot.level || p.hidden) continue; - - int dx = p.pos[0] - bot.pos[0], dy = p.pos[1] - bot.pos[1], dz = p.pos[2] - bot.pos[2]; - int playerDist = Math.Abs(dx) + Math.Abs(dy) + Math.Abs(dz); - if (playerDist >= dist) continue; - - closest = p; - dist = playerDist; - } + Player closest = HuntInstruction.ClosestPlayer(bot, search); if (closest == null) return true; FaceTowards(bot, closest); diff --git a/MCGalaxy/Bots/PlayerBot.cs b/MCGalaxy/Bots/PlayerBot.cs index 6dca74889..bb8d5a175 100644 --- a/MCGalaxy/Bots/PlayerBot.cs +++ b/MCGalaxy/Bots/PlayerBot.cs @@ -24,7 +24,7 @@ using MCGalaxy.Bots; namespace MCGalaxy { - public sealed class PlayerBot { + public sealed class PlayerBot : Entity { [Obsolete("Use PlayerBot.Bots.Items instead")] public static List playerbots; @@ -73,6 +73,16 @@ namespace MCGalaxy { rot = new byte[2] { rotx, roty }; } + protected override void OnSetPos() { + Position p = Pos; + pos[0] = (ushort)p.X; pos[1] = (ushort)p.Y; pos[2] = (ushort)p.Z; + } + + protected override void OnSetRot() { + Orientation r = Rot; + rot[0] = r.RotY; rot[1] = r.HeadX; + } + public static void Add(PlayerBot bot, bool save = true) { Bots.Add(bot); bot.GlobalSpawn(); diff --git a/MCGalaxy/Entity/Entity.cs b/MCGalaxy/Entity/Entity.cs index 7687672a0..acf66dd9e 100644 --- a/MCGalaxy/Entity/Entity.cs +++ b/MCGalaxy/Entity/Entity.cs @@ -18,42 +18,75 @@ using System; namespace MCGalaxy { - - public abstract class Entity { - public Orientation Rot; - public Position Pos; - - } - - /// Represents the position of an entity in the world. - public struct Position { - - /// X fixed-point location in the world. - public int X; - - /// Y fixed-point location in the world. (vertical) - public int Y; - - /// Z fixed-point location in the world. - public int Z; - - /// World/block coordinate of this position. + + public abstract class Entity { + + // Raw orientation/position - NOT threadsafe + protected Orientation _rot; + protected Position _pos; + + // Last sent orientation/position, for delta calculation + protected Orientation lastRot; + protected Position lastPos; + + /// Gets or sets the orientation of this entity. + public Orientation Rot { + get { return _rot; } + set { _rot = value; OnSetRot(); } + } + + /// Gets or sets the position of this entity. + public Position Pos { + get { return _pos; } + set { _pos = value; OnSetPos(); } + } + + protected virtual void OnSetPos() { } + + protected virtual void OnSetRot() { } + + } + + /// Represents the position of an entity in the world. + public struct Position { + + /// X fixed-point location in the world. + public int X; + + /// Y fixed-point location in the world. (vertical) + public int Y; + + /// Z fixed-point location in the world. + public int Z; + + /// World/block coordinate of this position. public Vec3S32 BlockCoords { get { return new Vec3S32(X >> 5, Y >> 5, Z >> 5); } } - } - - /// Represents orientation / rotation of an entity. - public struct Orientation { - - /// Rotation around X axis in degrees. - public short RotX; - - /// Rotation around Y axis in degrees. (yaw) - public short RotY; - - /// Rotation around Z axis in degrees. - public short RotZ; - - /// Rotation of head around X axis in degrees. (pitch) - public short HeadX; - } + } + + /// Represents orientation / rotation of an entity. + public struct Orientation { + + /// Rotation around X axis in packed form. + public byte RotX; + + /// Rotation around Y axis in packed form. (yaw) + public byte RotY; + + /// Rotation around Z axis in packed form. + public byte RotZ; + + /// Rotation of head around X axis in packed form. (pitch) + public byte HeadX; + + + /// Converts angle in range [0, 256) into range [0, 360). + public static short PackedToDegrees(byte packed) { + return (short)(packed * 360 / 256); + } + + /// Converts angle in degrees into range [0, 256) + public static byte DegreesToPacked(short degrees) { + return (byte)(degrees * 256 / 360); + } + } } diff --git a/MCGalaxy/Player/Player.cs b/MCGalaxy/Player/Player.cs index db063d9df..00ef72f5e 100644 --- a/MCGalaxy/Player/Player.cs +++ b/MCGalaxy/Player/Player.cs @@ -28,7 +28,7 @@ namespace MCGalaxy { public string username { get; set; } } - public sealed partial class Player : IDisposable { + public sealed partial class Player : Entity, IDisposable { static int sessionCounter; @@ -59,6 +59,16 @@ namespace MCGalaxy { catch ( Exception e ) { Leave("Login failed!"); Server.ErrorLog(e); } } + protected override void OnSetPos() { + Position p = Pos; + pos[0] = (ushort)p.X; pos[1] = (ushort)p.Y; pos[2] = (ushort)p.Z; + } + + protected override void OnSetRot() { + Orientation r = Rot; + rot[0] = r.RotY; rot[1] = r.HeadX; + } + public byte GetActualHeldBlock(out byte extBlock) { byte block = RawHeldBlock;