Update player look when using pathfinding

Document Look command in Readme
Improve UpdateLocation API
Small coding style fixes
This commit is contained in:
ORelio 2019-04-12 17:08:30 +02:00
parent ba0f51dc8c
commit d0088e0dca
4 changed files with 59 additions and 53 deletions

View file

@ -9,7 +9,7 @@ namespace MinecraftClient.Commands
public class Look : Command public class Look : Command
{ {
public override string CMDName { get { return "look"; } } public override string CMDName { get { return "look"; } }
public override string CMDDesc { get { return "look <x y z|yaw pitch|up|down|east|west|north|south|>: look direction or at block."; } } public override string CMDDesc { get { return "look <x y z|yaw pitch|up|down|east|west|north|south>: look at direction or coordinates."; } }
public override string Run(McTcpClient handler, string command) public override string Run(McTcpClient handler, string command)
{ {
@ -31,18 +31,18 @@ namespace MinecraftClient.Commands
default: return "Unknown direction '" + dirStr + "'."; default: return "Unknown direction '" + dirStr + "'.";
} }
handler.LookAtDirection(direction); handler.UpdateLocation(handler.GetCurrentLocation(), direction);
return "Looking " + dirStr; return "Looking " + dirStr;
} }
else if (args.Length == 2) else if (args.Length == 2)
{ {
try try
{ {
float yaw = Single.Parse(args[0]), float yaw = Single.Parse(args[0]);
pitch = Single.Parse(args[1]); float pitch = Single.Parse(args[1]);
handler.LookAtAngle(yaw, pitch); handler.UpdateLocation(handler.GetCurrentLocation(), yaw, pitch);
return $"Looking at YAW: {yaw} PITCH: {pitch}"; return String.Format("Looking at YAW: {0} PITCH: {1}", yaw.ToString("0.00"), pitch.ToString("0.00"));
} }
catch (FormatException) { return CMDDesc; } catch (FormatException) { return CMDDesc; }
} }
@ -50,12 +50,12 @@ namespace MinecraftClient.Commands
{ {
try try
{ {
int x = int.Parse(args[0]), int x = int.Parse(args[0]);
y = int.Parse(args[1]), int y = int.Parse(args[1]);
z = int.Parse(args[2]); int z = int.Parse(args[2]);
Location block = new Location(x, y, z); Location block = new Location(x, y, z);
handler.LookAtBlock(block); handler.UpdateLocation(handler.GetCurrentLocation(), block);
return "Looking at " + block; return "Looking at " + block;
} }

View file

@ -412,7 +412,8 @@ namespace MinecraftClient
/// or if a ChatBot whishes to update the player's location. /// or if a ChatBot whishes to update the player's location.
/// </summary> /// </summary>
/// <param name="location">The new location</param> /// <param name="location">The new location</param>
/// <param name="relative">If true, the location is relative to the current location</param> /// <param name="yaw">Yaw to look at</param>
/// <param name="pitch">Pitch to look at</param>
public void UpdateLocation(Location location, float yaw, float pitch) public void UpdateLocation(Location location, float yaw, float pitch)
{ {
this.yaw = yaw; this.yaw = yaw;
@ -421,42 +422,37 @@ namespace MinecraftClient
} }
/// <summary> /// <summary>
/// Look at specified block /// Called when the server sends a new player location,
/// or if a ChatBot whishes to update the player's location.
/// </summary> /// </summary>
/// <param name="block">The block to look at</param> /// <param name="location">The new location</param>
public void LookAtBlock(Location block) /// <param name="lookAt">Block coordinates to look at</param>
public void UpdateLocation(Location location, Location lookAtLocation)
{ {
double dx = block.X - (location.X - 0.5), double dx = lookAtLocation.X - (location.X - 0.5);
dy = block.Y - (location.Y + 1), double dy = lookAtLocation.Y - (location.Y + 1);
dz = block.Z - (location.Z - 0.5); double dz = lookAtLocation.Z - (location.Z - 0.5);
double r = Math.Sqrt(dx * dx + dy * dy + dz * dz); double r = Math.Sqrt(dx * dx + dy * dy + dz * dz);
float yaw = Convert.ToSingle(-Math.Atan2(dx, dz) / Math.PI * 180), float yaw = Convert.ToSingle(-Math.Atan2(dx, dz) / Math.PI * 180);
pitch = Convert.ToSingle(-Math.Asin(dy / r) / Math.PI * 180); float pitch = Convert.ToSingle(-Math.Asin(dy / r) / Math.PI * 180);
if (yaw < 0) yaw += 360; if (yaw < 0) yaw += 360;
UpdateLocation(location, yaw, pitch); UpdateLocation(location, yaw, pitch);
} }
/// <summary> /// <summary>
/// Look at specified angle /// Called when the server sends a new player location,
/// or if a ChatBot whishes to update the player's location.
/// </summary> /// </summary>
/// <param name="yaw">The yaw to look at</param> /// <param name="location">The new location</param>
/// <param name="pitch">The pitch to look at</param> /// <param name="direction">Direction to look at</param>
public void LookAtAngle(float yaw, float pitch) public void UpdateLocation(Location location, Direction direction)
{ {
UpdateLocation(location, yaw, pitch); float yaw = 0;
} float pitch = 0;
/// <summary>
/// Look in specified direction
/// </summary>
/// <param name="direction">The direction too look in</param>
public void LookAtDirection(Direction direction)
{
float yaw = 0,
pitch = 0;
switch (direction) switch (direction)
{ {
case Direction.Up: case Direction.Up:
@ -479,6 +475,7 @@ namespace MinecraftClient
default: default:
throw new ArgumentException("Unknown direction", "direction"); throw new ArgumentException("Unknown direction", "direction");
} }
UpdateLocation(location, yaw, pitch); UpdateLocation(location, yaw, pitch);
} }
@ -600,13 +597,24 @@ namespace MinecraftClient
if (yaw == null || pitch == null) if (yaw == null || pitch == null)
{ {
if (steps != null && steps.Count > 0) if (steps != null && steps.Count > 0)
{
location = steps.Dequeue(); location = steps.Dequeue();
}
else if (path != null && path.Count > 0) else if (path != null && path.Count > 0)
steps = Movement.Move2Steps(location, path.Dequeue(), ref motionY); {
else location = Movement.HandleGravity(world, location, ref motionY); Location next = path.Dequeue();
steps = Movement.Move2Steps(location, next, ref motionY);
UpdateLocation(location, next); // Update yaw and pitch to look at next step
}
else
{
location = Movement.HandleGravity(world, location, ref motionY);
}
} }
handler.SendLocationUpdate(location, Movement.IsOnGround(world, location), yaw, pitch); handler.SendLocationUpdate(location, Movement.IsOnGround(world, location), yaw, pitch);
} }
// First 2 updates must be player position AND look, and player must not move (to conform with vanilla)
// Once yaw and pitch have been sent, switch back to location-only updates (without yaw and pitch)
yaw = null; yaw = null;
pitch = null; pitch = null;
} }

View file

@ -13,7 +13,7 @@ using MinecraftClient.Mapping;
namespace MinecraftClient.Protocol.Handlers namespace MinecraftClient.Protocol.Handlers
{ {
/// <summary> /// <summary>
/// Implementation for Minecraft 1.7.X, 1.8.X, 1.9.X, 1.10.X Protocols /// Implementation for Minecraft 1.7.X+ Protocols
/// </summary> /// </summary>
class Protocol18Handler : IMinecraftCom class Protocol18Handler : IMinecraftCom
{ {
@ -1912,24 +1912,21 @@ namespace MinecraftClient.Protocol.Handlers
/// </summary> /// </summary>
/// <param name="location">The new location of the player</param> /// <param name="location">The new location of the player</param>
/// <param name="onGround">True if the player is on the ground</param> /// <param name="onGround">True if the player is on the ground</param>
/// <param name="yaw">The new yaw of the player</param> /// <param name="yaw">Optional new yaw for updating player look</param>
/// <param name="pitch">The new pitch of the player</param> /// <param name="pitch">Optional new pitch for updating player look</param>
/// <returns>True if the location update was successfully sent</returns> /// <returns>True if the location update was successfully sent</returns>
public bool SendLocationUpdate(Location location, bool onGround, float? yaw = null, float? pitch = null) public bool SendLocationUpdate(Location location, bool onGround, float? yaw = null, float? pitch = null)
{ {
if (Settings.TerrainAndMovements) if (Settings.TerrainAndMovements)
{ {
PacketOutgoingType packetType;
byte[] yawpitch = new byte[0]; byte[] yawpitch = new byte[0];
if (yaw != null && pitch != null) PacketOutgoingType packetType = PacketOutgoingType.PlayerPosition;
if (yaw.HasValue && pitch.HasValue)
{ {
yawpitch = concatBytes(getFloat((float)yaw), getFloat((float)pitch)); yawpitch = concatBytes(getFloat(yaw.Value), getFloat(pitch.Value));
packetType = PacketOutgoingType.PlayerPositionAndLook; packetType = PacketOutgoingType.PlayerPositionAndLook;
} }
else
{
packetType = PacketOutgoingType.PlayerPosition;
}
try try
{ {

View file

@ -1,6 +1,6 @@
================================================================================= ==================================================================================
Minecraft Client v1.9.0 for Minecraft 1.4.6 to 1.9.0 - By ORelio & Contributors Minecraft Client v1.13.0 for Minecraft 1.4.6 to 1.13.0 - By ORelio & Contributors
================================================================================= ==================================================================================
Thanks for dowloading Minecraft Console Client! Thanks for dowloading Minecraft Console Client!
@ -70,6 +70,7 @@ In scripts and remote control, no slash is needed to perform the command.
- set varname=value : set a value which can be used as %varname% in further commands - set varname=value : set a value which can be used as %varname% in further commands
- wait <time> : wait X ticks (10 ticks = ~1 second. Only for scripts) - wait <time> : wait X ticks (10 ticks = ~1 second. Only for scripts)
- move : used for moving when terrain and movements feature is enabled - move : used for moving when terrain and movements feature is enabled
- look : used for looking at direction when terrain and movements is enabled
- debug : toggle debug messages, useful for chatbot developers - debug : toggle debug messages, useful for chatbot developers
- help : show command help. Tip: Use "/send /help" for server help - help : show command help. Tip: Use "/send /help" for server help
@ -274,8 +275,8 @@ Bug Hunters
Code contributions Code contributions
Allyoutoo, Aragas, Bancey, bearbear12345, corbanmailloux, dbear20, dogwatch, initsuj, Allyoutoo, Aragas, Bancey, bearbear12345, corbanmailloux, dbear20, dogwatch, initsuj,
JamieSinn, justcool393, lokulin, maxpowa, medxo, Pokechu22, TheMeq, v1RuX, ZizzyDizzyMC JamieSinn, justcool393, lokulin, maxpowa, medxo, Pokechu22, repository, TheMeq, v1RuX,
ZizzyDizzyMC
Libraries Libraries
@ -306,5 +307,5 @@ Like Minecraft Console Client? You can buy me a coffee here:
Code contributions, bug reports and any kind of comments are also highly appreciated :) Code contributions, bug reports and any kind of comments are also highly appreciated :)
+-----------------------------------+ +-----------------------------------+
| © 2012-2016 ORelio & Contributors | | © 2012-2019 ORelio & Contributors |
+-----------------------------------+ +-----------------------------------+