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) {
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);

View File

@ -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<PlayerBot> 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();

View File

@ -20,8 +20,30 @@ using System;
namespace MCGalaxy {
public abstract class Entity {
public Orientation Rot;
public Position Pos;
// Raw orientation/position - NOT threadsafe
protected Orientation _rot;
protected Position _pos;
// Last sent orientation/position, for delta calculation
protected Orientation lastRot;
protected Position lastPos;
/// <summary> Gets or sets the orientation of this entity. </summary>
public Orientation Rot {
get { return _rot; }
set { _rot = value; OnSetRot(); }
}
/// <summary> Gets or sets the position of this entity. </summary>
public Position Pos {
get { return _pos; }
set { _pos = value; OnSetPos(); }
}
protected virtual void OnSetPos() { }
protected virtual void OnSetRot() { }
}
@ -44,16 +66,27 @@ namespace MCGalaxy {
/// <summary> Represents orientation / rotation of an entity. </summary>
public struct Orientation {
/// <summary> Rotation around X axis in degrees. </summary>
public short RotX;
/// <summary> Rotation around X axis in packed form. </summary>
public byte RotX;
/// <summary> Rotation around Y axis in degrees. (yaw) </summary>
public short RotY;
/// <summary> Rotation around Y axis in packed form. (yaw) </summary>
public byte RotY;
/// <summary> Rotation around Z axis in degrees. </summary>
public short RotZ;
/// <summary> Rotation around Z axis in packed form. </summary>
public byte RotZ;
/// <summary> Rotation of head around X axis in degrees. (pitch) </summary>
public short HeadX;
/// <summary> Rotation of head around X axis in packed form. (pitch) </summary>
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 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;