Upgrade GetLookingBlock

This commit is contained in:
BruceChen 2022-10-02 13:49:36 +08:00 committed by GitHub
parent ba6a954f45
commit 4aa6c1c99f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 213 additions and 74 deletions

View file

@ -203,7 +203,7 @@ namespace MinecraftClient.ChatBots
public override void OnEntityDespawn(Entity entity)
{
if (entity.Type == EntityType.FishingBobber && entity.ID == fishingBobber!.ID)
if (entity != null && entity.Type == EntityType.FishingBobber && entity.ID == fishingBobber!.ID)
{
if (Settings.AutoFishing_LogFishingBobber)
LogToConsole(string.Format("FishingBobber despawn at {0}", entity.Location));
@ -228,7 +228,7 @@ namespace MinecraftClient.ChatBots
public override void OnEntityMove(Entity entity)
{
if (isFishing && fishingBobber!.ID == entity.ID)
if (isFishing && entity != null && fishingBobber!.ID == entity.ID)
{
Location Pos = entity.Location;
double Dx = LastPos.X - Pos.X;

View file

@ -17,10 +17,20 @@ namespace MinecraftClient.Commands
if (!handler.GetTerrainEnabled())
return Translations.Get("extra.terrainandmovement_required");
if (hasArg(command))
{
string[] args = getArgs(command);
if (args.Length == 3)
if (args.Length == 0)
{
(bool hasBlock, Location blockLoc, Block block) = RaycastHelper.RaycastBlock(handler, 4.5, false);
if (!hasBlock)
return Translations.Get("cmd.dig.too_far");
else if (block.Type == Material.Air)
return Translations.Get("cmd.dig.no_block");
else if (handler.DigBlock(blockLoc, lookAtBlock: false))
return Translations.Get("cmd.dig.dig", blockLoc.X, blockLoc.Y, blockLoc.Z, block.Type);
else
return Translations.Get("cmd.dig.fail");
}
else if (args.Length == 3)
{
try
{
@ -28,17 +38,20 @@ namespace MinecraftClient.Commands
Location blockToBreak = Location.Parse(current, args[0], args[1], args[2]);
if (blockToBreak.DistanceSquared(current.EyesLocation()) > 25)
return Translations.Get("cmd.dig.too_far");
if (handler.GetWorld().GetBlock(blockToBreak).Type == Material.Air)
Block block = handler.GetWorld().GetBlock(blockToBreak);
if (block.Type == Material.Air)
return Translations.Get("cmd.dig.no_block");
if (handler.DigBlock(blockToBreak))
return Translations.Get("cmd.dig.dig", blockToBreak.X, blockToBreak.Y, blockToBreak.Z);
else return "cmd.dig.fail";
else if (handler.DigBlock(blockToBreak))
return Translations.Get("cmd.dig.dig", blockToBreak.X, blockToBreak.Y, blockToBreak.Z, block.Type);
else
return Translations.Get("cmd.dig.fail");
}
catch (FormatException) { return GetCmdDescTranslated(); }
}
else return GetCmdDescTranslated();
else
{
return GetCmdDescTranslated();
}
else return GetCmdDescTranslated();
}
}
}

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using MinecraftClient.Mapping;
@ -17,7 +18,20 @@ namespace MinecraftClient.Commands
if (handler.GetTerrainEnabled())
{
string[] args = getArgs(command);
if (args.Length == 1)
if (args.Length == 0)
{
const double maxDistance = 8.0;
(bool hasBlock, Location target, Block block) = RaycastHelper.RaycastBlock(handler, maxDistance, false);
if (!hasBlock)
return Translations.Get("cmd.look.noinspection", maxDistance);
else
{
Location current = handler.GetCurrentLocation(), target_center = target.ToCenter();
return Translations.Get("cmd.look.inspection", block.Type, target.X, target.Y, target.Z,
current.Distance(target_center), current.EyesLocation().Distance(target_center));
}
}
else if (args.Length == 1)
{
string dirStr = getArg(command).Trim().ToLower();
Direction direction;

View file

@ -12,6 +12,8 @@ namespace MinecraftClient.Mapping
/// </summary>
public struct Block
{
public static readonly Block Air = new(0);
/// <summary>
/// Get or set global block ID to Material mapping
/// The global Palette is a concept introduced with Minecraft 1.13

View file

@ -11,6 +11,8 @@ namespace MinecraftClient.Mapping
/// </summary>
public struct Location
{
public static readonly Location Zero = new(0, 0, 0);
/// <summary>
/// The X Coordinate
/// </summary>
@ -26,17 +28,6 @@ namespace MinecraftClient.Mapping
/// </summary>
public double Z;
/// <summary>
/// Get location with zeroed coordinates
/// </summary>
public static Location Zero
{
get
{
return new Location(0, 0, 0);
}
}
/// <summary>
/// Create a new location
/// </summary>

View file

@ -0,0 +1,135 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using MinecraftClient.Protocol;
namespace MinecraftClient.Mapping
{
public static class RaycastHelper
{
public static Tuple<bool, Location, Block> RaycastBlock(McClient handler, double maxDistance, bool includeFluids)
{
return RaycastBlock(handler.GetWorld(), handler.GetCurrentLocation(), handler.GetYaw(), handler.GetPitch(), maxDistance, includeFluids);
}
public static Tuple<bool, Location, Block> RaycastBlock(World world, Location playerLocation, float yaw, float pitch, double maxDistance, bool includeFluids)
{
Location camera = playerLocation.EyesLocation();
Location rotation = MathHelper.GetRotationVector(yaw, pitch);
Location end = camera + (rotation * maxDistance);
return Raycast(world, camera, end, includeFluids);
}
public static Tuple<bool, Location, Entity> RaycastEntity(McClient handler, double maxDistance)
{
throw new NotImplementedException();
}
private static Block CheckRaycastResult(World world, Location location, bool includeFluids)
{
Block block = world.GetBlock(location);
if (!includeFluids && MaterialExtensions.IsLiquid(block.Type))
return Block.Air;
else
return block;
}
public static Tuple<bool, Location, Block> Raycast(World world, Location start, Location end, bool includeFluids)
{
if (start == end)
return new(false, Location.Zero, Block.Air);
double start_x = MathHelper.Lerp(-1.0E-7, start.X, end.X);
double start_y = MathHelper.Lerp(-1.0E-7, start.Y, end.Y);
double start_z = MathHelper.Lerp(-1.0E-7, start.Z, end.Z);
double end_x = MathHelper.Lerp(-1.0E-7, end.X, start.X);
double end_y = MathHelper.Lerp(-1.0E-7, end.Y, start.Y);
double end_z = MathHelper.Lerp(-1.0E-7, end.Z, start.Z);
Location res_location = new(Math.Floor(start_x), Math.Floor(start_y), Math.Floor(start_z));
Block block = CheckRaycastResult(world, res_location, includeFluids);
if (block.Type != Material.Air)
return new(true, res_location, block);
double dx = end_x - start_x;
double dy = end_y - start_y;
double dz = end_z - start_z;
int dx_sign = Math.Sign(dx);
int dy_sign = Math.Sign(dy);
int dz_sign = Math.Sign(dz);
double x_step = dx_sign == 0 ? double.MaxValue : (double)dx_sign / dx;
double y_step = dy_sign == 0 ? double.MaxValue : (double)dy_sign / dy;
double z_step = dz_sign == 0 ? double.MaxValue : (double)dz_sign / dz;
double x_frac = x_step * (dx_sign > 0 ? 1.0 - MathHelper.FractionalPart(start_x) : MathHelper.FractionalPart(start_x));
double y_frac = y_step * (dy_sign > 0 ? 1.0 - MathHelper.FractionalPart(start_y) : MathHelper.FractionalPart(start_y));
double z_frac = z_step * (dz_sign > 0 ? 1.0 - MathHelper.FractionalPart(start_z) : MathHelper.FractionalPart(start_z));
while (x_frac <= 1.0 || y_frac <= 1.0 || z_frac <= 1.0)
{
if (x_frac < y_frac)
{
if (x_frac < z_frac)
{
res_location.X += dx_sign;
x_frac += x_step;
}
else
{
res_location.Z += dz_sign;
z_frac += z_step;
}
}
else if (y_frac < z_frac)
{
res_location.Y += dy_sign;
y_frac += y_step;
}
else
{
res_location.Z += dz_sign;
z_frac += z_step;
}
block = CheckRaycastResult(world, res_location, includeFluids);
if (block.Type != Material.Air)
return new(true, res_location, block);
}
return new(false, Location.Zero, Block.Air);
}
}
public static class MathHelper
{
public static Location GetRotationVector(float yaw, float pitch)
{
float yaw_rad = -yaw * (MathF.PI / 180);
(float yaw_sin, float yaw_cos) = MathF.SinCos(yaw_rad);
float pitch_rad = pitch * (MathF.PI / 180);
(float pitch_sin, float pitch_cos) = MathF.SinCos(pitch_rad);
return new(yaw_sin * pitch_cos, -pitch_sin, yaw_cos * pitch_cos);
}
public static double Lerp(double delta, double start, double end)
{
return start + delta * (end - start);
}
public static long Lfloor(double value)
{
long l = (long)value;
return value < (double)l ? l - 1L : l;
}
public static double FractionalPart(double value)
{
return value - Lfloor(value);
}
}
}

View file

@ -144,7 +144,7 @@ namespace MinecraftClient.Mapping
if (chunk != null)
return chunk.GetBlock(location);
}
return new Block(0); //Air
return Block.Air;
}
/// <summary>
@ -218,36 +218,5 @@ namespace MinecraftClient.Mapping
chunkCnt = 0;
chunkLoadNotCompleted = 0;
}
/// <summary>
/// Get the location of block of the entity is looking
/// </summary>
/// <param name="location">Location of the entity</param>
/// <param name="yaw">Yaw of the entity</param>
/// <param name="pitch">Pitch of the entity</param>
/// <returns>Location of the block or empty Location if no block was found</returns>
public Location GetLookingBlockLocation(Location location, double yaw, double pitch)
{
double rotX = (Math.PI / 180) * yaw;
double rotY = (Math.PI / 180) * pitch;
double x = -Math.Cos(rotY) * Math.Sin(rotX);
double y = -Math.Sin(rotY);
double z = Math.Cos(rotY) * Math.Cos(rotX);
Location vector = new Location(x, y, z);
for (int i = 0; i < 5; i++)
{
Location newVector = vector * i;
Location blockLocation = location.EyesLocation() + new Location(newVector.X, newVector.Y, newVector.Z);
blockLocation.X = Math.Floor(blockLocation.X);
blockLocation.Y = Math.Floor(blockLocation.Y);
blockLocation.Z = Math.Floor(blockLocation.Z);
Block b = GetBlock(blockLocation);
if (b.Type != Material.Air)
{
return blockLocation;
}
}
return new Location();
}
}
}

View file

@ -2902,9 +2902,9 @@ namespace MinecraftClient
{
foreach (int a in Entities)
{
if (entities.ContainsKey(a))
if (entities.TryGetValue(a, out Entity? entity))
{
DispatchBotEvent(bot => bot.OnEntityDespawn(entities[a]));
DispatchBotEvent(bot => bot.OnEntityDespawn(entity));
entities.Remove(a);
}
}

View file

@ -255,7 +255,7 @@ cmd.debug.state_off=Debug messages are now OFF
cmd.dig.desc=Attempt to break a block
cmd.dig.too_far=You are too far away from this block.
cmd.dig.no_block=No block at this location (Air)
cmd.dig.dig=Attempting to dig block at {0} {1} {2}
cmd.dig.dig=Attempting to dig block at {0} {1} {2} ({3})
cmd.dig.fail=Failed to start digging block.
# Entitycmd
@ -368,8 +368,10 @@ cmd.log.desc=log some text to the console.
# Look
cmd.look.desc=look at direction or coordinates.
cmd.look.unknown=Unknown direction '{0}'
cmd.look.at=Looking at YAW: {0} PITCH: {1}
cmd.look.at=Looking at yaw: {0} pitch: {1}
cmd.look.block=Looking at {0}
cmd.look.inspection=The block currently intersecting the line of sight is {0} ({1:0}, {2:0}, {3:0}), distance is {4:0.0}({5:0.0}).
cmd.look.noinspection=There is no block within {0} meters that intersects with the line of sight.
# Move
cmd.move.desc=walk or start walking.

View file

@ -234,7 +234,7 @@ cmd.debug.state_off=调试消息现在已关闭
cmd.dig.desc=试图破坏一个方块
cmd.dig.too_far=你离这个方块太远了。
cmd.dig.no_block=这个地方没有方块 (空气)
cmd.dig.dig=尝试挖掘位于{0} {1} {2}的方块
cmd.dig.dig=尝试挖掘位于({0}, {1}, {2})的方块({3})。
cmd.dig.fail=无法开始挖掘方块。
# Entitycmd
@ -311,9 +311,11 @@ cmd.log.desc=将文本记录到控制台。
# Look
cmd.look.desc=查看方向或坐标。
cmd.look.unknown=未知方向 '{0}'
cmd.look.at=正在看向偏航角:{0} 俯仰角:{1}
cmd.look.block=正在看向{0}
cmd.look.unknown=未知方向 '{0}'。
cmd.look.at=当前视角 偏航角:{0} 俯仰角:{1}。
cmd.look.block=正看向位于 {0} 的方块。
cmd.look.inspection=与视线相交的第一个方块是 {0} ({1:0}, {2:0}, {3:0}),距离玩家 {4:0.0}({5:0.0})。
cmd.look.noinspection=在 {0} 米内没有任何方块与视线相交。
# Move
cmd.move.desc=移动或开始移动。

View file

@ -1033,6 +1033,17 @@ namespace MinecraftClient
Handler.UpdateLocation(Handler.GetCurrentLocation(), yaw, pitch);
}
/// <summary>
/// Find the block on the line of sight.
/// </summary>
/// <param name="maxDistance">Maximum distance from sight</param>
/// <param name="includeFluids">Whether to detect fluid</param>
/// <returns>Position of the block</returns>
protected Tuple<bool, Location, Block> GetLookingBlock(double maxDistance = 4.5, bool includeFluids = false)
{
return RaycastHelper.RaycastBlock(Handler, maxDistance, includeFluids);
}
/// <summary>
/// Get a Y-M-D h:m:s timestamp representing the current system date and time
/// </summary>