Skip to content

Commit

Permalink
fix auto avoid
Browse files Browse the repository at this point in the history
  • Loading branch information
jeannsebold6666 committed Sep 9, 2024
1 parent 3001823 commit d7b21a7
Show file tree
Hide file tree
Showing 4 changed files with 287 additions and 5 deletions.
223 changes: 223 additions & 0 deletions src/ClassicUO.Client/Game/GameObjects/PlayerMobile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1664,6 +1664,229 @@ public void CloseRangedGumps()
// #############################################

public bool Walk(Direction direction, bool run)
{
if (!ProfileManager.CurrentProfile.AutoAvoidMobiles)
{

return WalkNotAvoid(direction, run);
}

else
{

if (Walker.WalkingFailed || Walker.LastStepRequestTime > Time.Ticks || Walker.StepsCount >= Constants.MAX_STEP_COUNT || Client.Version >= ClientVersion.CV_60142 && IsParalyzed)
{
return false;
}

run |= ProfileManager.CurrentProfile.AlwaysRun;

if (SpeedMode >= CharacterSpeedType.CantRun || Stamina <= 1 && !IsDead || IsHidden && ProfileManager.CurrentProfile.AlwaysRunUnlessHidden)
{
run = false;
}

int x = X;
int y = Y;
sbyte z = Z;
Direction oldDirection = Direction;

bool emptyStack = Steps.Count == 0;

if (!emptyStack)
{
ref Step walkStep = ref Steps.Back();
x = walkStep.X;
y = walkStep.Y;
z = walkStep.Z;
oldDirection = (Direction)walkStep.Direction;
}

sbyte oldZ = z;
ushort walkTime = Constants.TURN_DELAY;

// Lógica de verificação para contornar obstáculos em direções cardeais
if (IsCardinalDirection(direction))
{
if (IsObstacle(direction, x, y, z))
{
// Tenta evitar o obstáculo
Direction newDir = TryToAvoid(direction, x, y, z);

if (!IsObstacle(newDir, x, y, z))
{
direction = newDir;
World.Player.ClearSteps();

World.Player.SetInWorldTile((ushort)x, (ushort)y, z);
//NetClient.Socket.Send_Resync();
}
else
{
return false; // Não pode evitar o obstáculo
}
}
}

if ((oldDirection & Direction.Mask) == (direction & Direction.Mask))
{
// ## BEGIN - END ## // ONCASTINGGUMP
if (GameActions.iscasting) return false;
// ## BEGIN - END ## // ONCASTINGGUMP

Direction newDir = direction;
int newX = x;
int newY = y;
sbyte newZ = z;

if (!Pathfinder.CanWalkObstacules(ref newDir, ref newX, ref newY, ref newZ))
{
return false;
}

if ((direction & Direction.Mask) != newDir)
{
direction = newDir;
}
else
{
direction = newDir;
x = newX;
y = newY;
z = newZ;

walkTime = (ushort)MovementSpeed.TimeToCompleteMovement(run, IsMounted || SpeedMode == CharacterSpeedType.FastUnmount || SpeedMode == CharacterSpeedType.FastUnmountAndCantRun || IsFlying);
}
}
else
{
Direction newDir = direction;
int newX = x;
int newY = y;
sbyte newZ = z;

if (!Pathfinder.CanWalkObstacules(ref newDir, ref newX, ref newY, ref newZ))
{
if ((oldDirection & Direction.Mask) == newDir)
{
return false;
}
}

if ((oldDirection & Direction.Mask) == newDir)
{
x = newX;
y = newY;
z = newZ;

walkTime = (ushort)MovementSpeed.TimeToCompleteMovement(run, IsMounted || SpeedMode == CharacterSpeedType.FastUnmount || SpeedMode == CharacterSpeedType.FastUnmountAndCantRun || IsFlying);
}

direction = newDir;
}

CloseBank();

if (emptyStack)
{
if (!IsWalking)
{
SetAnimation(0xFF);
}

LastStepTime = Time.Ticks;
}
if (Walker.StepsCount == -1)
{
Walker.StepsCount = 1;
}


var item = Walker.StepsCount;

ref StepInfo step = ref Walker.StepInfos[item];
step.Sequence = Walker.WalkSequence;
step.Accepted = false;
step.Running = run;
step.OldDirection = (byte)(oldDirection & Direction.Mask);
step.Direction = (byte)direction;
step.Timer = Time.Ticks;
step.X = (ushort)x;
step.Y = (ushort)y;
step.Z = z;
step.NoRotation = step.OldDirection == (byte)direction && oldZ - z >= 11;

Walker.StepsCount++;

Steps.AddToBack
(
new Step
{
X = x,
Y = y,
Z = z,
Direction = (byte)direction,
Run = run
}
);

NetClient.Socket.Send_WalkRequest(direction, Walker.WalkSequence, run, Walker.FastWalkStack.GetValue());

if (Walker.WalkSequence == 0xFF)
{
Walker.WalkSequence = 1;
}
else
{
Walker.WalkSequence++;
}

Walker.UnacceptedPacketsCount++;

AddToTile();

int nowDelta = 0;

Walker.LastStepRequestTime = Time.Ticks + walkTime - nowDelta;
GetGroupForAnimation(this, 0, true);

return true;
}

}

// Funções auxiliares para contornar obstáculos
bool IsCardinalDirection(Direction direction)
{
return direction == Direction.North || direction == Direction.South ||
direction == Direction.East || direction == Direction.West;
}

bool IsObstacle(Direction direction, int x, int y, sbyte z)
{
// Verifica se há um obstáculo usando a função de caminho
return !Pathfinder.CanWalkObstacules(ref direction, ref x, ref y, ref z);
}

Direction TryToAvoid(Direction direction, int x, int y, sbyte z)
{
// Tenta contornar o obstáculo movendo-se lateralmente
switch (direction)
{
case Direction.North:
return IsObstacle(Direction.East, x, y, z) ? Direction.West : Direction.East;
case Direction.South:
return IsObstacle(Direction.East, x, y, z) ? Direction.West : Direction.East;
case Direction.East:
return IsObstacle(Direction.North, x, y, z) ? Direction.South : Direction.North;
case Direction.West:
return IsObstacle(Direction.North, x, y, z) ? Direction.South : Direction.North;
default:
return direction;
}
}

public bool WalkNotAvoid(Direction direction, bool run)
{
if (Walker.WalkingFailed || Walker.LastStepRequestTime > Time.Ticks || Walker.StepsCount >= Constants.MAX_STEP_COUNT || Client.Version >= ClientVersion.CV_60142 && IsParalyzed)
{
Expand Down
14 changes: 12 additions & 2 deletions src/ClassicUO.Client/Game/Managers/NameOverHeadManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
// ## BEGIN - END ## // NAMEOVERHEAD IMPROVEMENTS // PKRION
using ClassicUO.Utility;
// ## BEGIN - END ## // NAMEOVERHEAD IMPROVEMENTS // PKRION
using System.Text.RegularExpressions;

namespace ClassicUO.Game.Managers
{
Expand Down Expand Up @@ -165,8 +166,17 @@ public static NameOverheadTypeAllowed TypeAllowed
// ## BEGIN - END ## // NAMEOVERHEAD
public static string LastActiveNameOverheadOption
{
get => ProfileManager.CurrentProfile.LastActiveNameOverheadOption;
set => ProfileManager.CurrentProfile.LastActiveNameOverheadOption = value;
get
{
string value = ProfileManager.CurrentProfile.LastActiveNameOverheadOption;
value = Regex.Unescape(value);
return value;
}
set
{
string decodedValue = Regex.Unescape(value);
ProfileManager.CurrentProfile.LastActiveNameOverheadOption = decodedValue;
}
}
// ## BEGIN - END ## // NAMEOVERHEAD

Expand Down
49 changes: 49 additions & 0 deletions src/ClassicUO.Client/Game/Pathfinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,55 @@ public static bool CanWalk(ref Direction direction, ref int x, ref int y, ref sb
return passed;
}

public static bool CanWalkObstacules(ref Direction direction, ref int x, ref int y, ref sbyte z)
{
int newX = x;
int newY = y;
sbyte newZ = z;
byte newDirection = (byte)direction;
GetNewXY((byte)direction, ref newX, ref newY);
bool passed = CalculateNewZ(newX, newY, ref newZ, (byte)direction);

if ((sbyte)direction % 2 != 0)
{
if (passed)
{
for (int i = 0; i < 2 && passed; i++)
{
int testX = x;
int testY = y;
sbyte testZ = z;
byte testDir = (byte)(((byte)direction + _dirOffset[i]) % 8);
GetNewXY(testDir, ref testX, ref testY);
passed = CalculateNewZ(testX, testY, ref testZ, testDir);
}
}

if (!passed)
{
for (int i = 0; i < 2 && !passed; i++)
{
newX = x;
newY = y;
newZ = z;
newDirection = (byte)(((byte)direction + _dirOffset[i]) % 8);
GetNewXY(newDirection, ref newX, ref newY);
passed = CalculateNewZ(newX, newY, ref newZ, newDirection);
}
}
}

if (passed)
{
x = newX;
y = newY;
z = newZ;
direction = (Direction)newDirection;
}

return passed;
}

private static int GetGoalDistCost(Point point, int cost)
{
return Math.Max(Math.Abs(_endPoint.X - point.X), Math.Abs(_endPoint.Y - point.Y));
Expand Down
6 changes: 3 additions & 3 deletions src/ClassicUO.Client/Game/UI/Gumps/OptionsGump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4432,13 +4432,13 @@ private void BuildDust()
section8.Add(_ignoreCoT = AddCheckBox(null, "Enable ignorelist for circle of transparency:", _currentProfile.IgnoreCoTEnabled, startX, startY));
startY += _ignoreCoT.Height + 2;

section8.Add(_showDeathOnWorldmap = AddCheckBox(null, "Show death location on world map for 5min:", _currentProfile.ShowDeathOnWorldmap, startX, startY));
section8.Add(_showDeathOnWorldmap = AddCheckBox(null, "Show death location on world map for 5min", _currentProfile.ShowDeathOnWorldmap, startX, startY));
startY += _showDeathOnWorldmap.Height + 2;

section8.Add(_showMapCloseFriend = AddCheckBox(null, "Show closed friend in World Map:", _currentProfile.ShowMapCloseFriend, startX, startY));
section8.Add(_showMapCloseFriend = AddCheckBox(null, "Show closed friend in World Map", _currentProfile.ShowMapCloseFriend, startX, startY));
startY += _showMapCloseFriend.Height + 2;

section8.Add(_autoAvoidMobiles = AddCheckBox(null, "Auto void Mobiles:", _currentProfile.AutoAvoidMobiles, startX, startY));
section8.Add(_autoAvoidMobiles = AddCheckBox(null, "Auto void Mobiles and Obstacules", _currentProfile.AutoAvoidMobiles, startX, startY));
startY += _autoAvoidMobiles.Height + 2;

// ## BEGIN - END ## // MISC2
Expand Down

0 comments on commit d7b21a7

Please sign in to comment.