Write base code for mass position/orientation rewrite.

This is where the fun begins
This commit is contained in:
UnknownShadow200 2017-04-17 16:02:25 +10:00
parent 9ba531b6db
commit 706275738c
4 changed files with 106 additions and 61 deletions

View File

@ -27,7 +27,16 @@ namespace MCGalaxy.Bots {
public override bool Execute(PlayerBot bot, InstructionData data) { public override bool Execute(PlayerBot bot, InstructionData data) {
int search = 75; int search = 75;
if (data.Metadata != null) search = (ushort)data.Metadata; 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[] players = PlayerInfo.Online.Items;
Player closest = null; 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 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); int playerDist = Math.Abs(dx) + Math.Abs(dy) + Math.Abs(dz);
if (playerDist >= dist) continue; if (playerDist >= maxDist) continue;
closest = p; closest = p;
dist = playerDist; maxDist = playerDist;
} }
return closest;
if (closest == null) { bot.NextInstruction(); return false; }
bool overlapsPlayer = MoveTowards(bot, closest);
if (overlapsPlayer) { bot.NextInstruction(); return false; }
return true;
} }
static bool MoveTowards(PlayerBot bot, Player p) { static bool MoveTowards(PlayerBot bot, Player p) {
@ -126,20 +131,7 @@ namespace MCGalaxy.Bots {
public override bool Execute(PlayerBot bot, InstructionData data) { public override bool Execute(PlayerBot bot, InstructionData data) {
int search = 20000; int search = 20000;
if (data.Metadata != null) search = (ushort)data.Metadata; if (data.Metadata != null) search = (ushort)data.Metadata;
int dist = search * 32; Player closest = HuntInstruction.ClosestPlayer(bot, search);
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;
}
if (closest == null) return true; if (closest == null) return true;
FaceTowards(bot, closest); FaceTowards(bot, closest);

View File

@ -24,7 +24,7 @@ using MCGalaxy.Bots;
namespace MCGalaxy { namespace MCGalaxy {
public sealed class PlayerBot { public sealed class PlayerBot : Entity {
[Obsolete("Use PlayerBot.Bots.Items instead")] [Obsolete("Use PlayerBot.Bots.Items instead")]
public static List<PlayerBot> playerbots; public static List<PlayerBot> playerbots;
@ -73,6 +73,16 @@ namespace MCGalaxy {
rot = new byte[2] { rotx, roty }; 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) { public static void Add(PlayerBot bot, bool save = true) {
Bots.Add(bot); Bots.Add(bot);
bot.GlobalSpawn(); bot.GlobalSpawn();

View File

@ -18,42 +18,75 @@
using System; using System;
namespace MCGalaxy { namespace MCGalaxy {
public abstract class Entity { public abstract class Entity {
public Orientation Rot;
public Position Pos; // Raw orientation/position - NOT threadsafe
protected Orientation _rot;
} protected Position _pos;
/// <summary> Represents the position of an entity in the world. </summary> // Last sent orientation/position, for delta calculation
public struct Position { protected Orientation lastRot;
protected Position lastPos;
/// <summary> X fixed-point location in the world. </summary>
public int X; /// <summary> Gets or sets the orientation of this entity. </summary>
public Orientation Rot {
/// <summary> Y fixed-point location in the world. (vertical) </summary> get { return _rot; }
public int Y; set { _rot = value; OnSetRot(); }
}
/// <summary> Z fixed-point location in the world. </summary>
public int Z; /// <summary> Gets or sets the position of this entity. </summary>
public Position Pos {
/// <summary> World/block coordinate of this position. </summary> get { return _pos; }
set { _pos = value; OnSetPos(); }
}
protected virtual void OnSetPos() { }
protected virtual void OnSetRot() { }
}
/// <summary> Represents the position of an entity in the world. </summary>
public struct Position {
/// <summary> X fixed-point location in the world. </summary>
public int X;
/// <summary> Y fixed-point location in the world. (vertical) </summary>
public int Y;
/// <summary> Z fixed-point location in the world. </summary>
public int Z;
/// <summary> World/block coordinate of this position. </summary>
public Vec3S32 BlockCoords { get { return new Vec3S32(X >> 5, Y >> 5, Z >> 5); } } public Vec3S32 BlockCoords { get { return new Vec3S32(X >> 5, Y >> 5, Z >> 5); } }
} }
/// <summary> Represents orientation / rotation of an entity. </summary> /// <summary> Represents orientation / rotation of an entity. </summary>
public struct Orientation { public struct Orientation {
/// <summary> Rotation around X axis in degrees. </summary> /// <summary> Rotation around X axis in packed form. </summary>
public short RotX; public byte RotX;
/// <summary> Rotation around Y axis in degrees. (yaw) </summary> /// <summary> Rotation around Y axis in packed form. (yaw) </summary>
public short RotY; public byte RotY;
/// <summary> Rotation around Z axis in degrees. </summary> /// <summary> Rotation around Z axis in packed form. </summary>
public short RotZ; public byte RotZ;
/// <summary> Rotation of head around X axis in degrees. (pitch) </summary> /// <summary> Rotation of head around X axis in packed form. (pitch) </summary>
public short HeadX; public byte HeadX;
}
/// <summary> Converts angle in range [0, 256) into range [0, 360). </summary>
public static short PackedToDegrees(byte packed) {
return (short)(packed * 360 / 256);
}
/// <summary> Converts angle in degrees into range [0, 256) </summary>
public static byte DegreesToPacked(short degrees) {
return (byte)(degrees * 256 / 360);
}
}
} }

View File

@ -28,7 +28,7 @@ namespace MCGalaxy {
public string username { get; set; } public string username { get; set; }
} }
public sealed partial class Player : IDisposable { public sealed partial class Player : Entity, IDisposable {
static int sessionCounter; static int sessionCounter;
@ -59,6 +59,16 @@ namespace MCGalaxy {
catch ( Exception e ) { Leave("Login failed!"); Server.ErrorLog(e); } 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) { public byte GetActualHeldBlock(out byte extBlock) {
byte block = RawHeldBlock; byte block = RawHeldBlock;