Skip to content

Commit

Permalink
Remove DateTimeExtensions
Browse files Browse the repository at this point in the history
Use single Delay method in TrainerBot
Use 10ms target update time in EndlessGame
  • Loading branch information
ethanmoffat committed May 9, 2023
1 parent cc228dc commit 5ef4c76
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 37 deletions.
40 changes: 17 additions & 23 deletions EOBot/TrainerBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,26 +251,23 @@ protected override async Task DoWorkAsync(CancellationToken ct)
}
}

await Task.Delay(TimeSpan.FromMilliseconds(120));
_fixedTimeStepRepository.Tick(12);
await Delay(120);
}
}

private async Task Attack(IMapCellState cellState)
{
cellState.NPC.MatchSome(npc => ConsoleHelper.WriteMessage(ConsoleHelper.Type.Attack, $"{npc.Index,7} - {_npcData.Single(x => x.ID == npc.ID).Name}"));
await TrySend(_characterActions.Attack);
await Task.Delay(TimeSpan.FromMilliseconds(ATTACK_BACKOFF_MS));
_fixedTimeStepRepository.Tick(ATTACK_BACKOFF_MS / 10);
await Delay(ATTACK_BACKOFF_MS);
}

private async Task Walk()
{
var renderProps = _characterRepository.MainCharacter.RenderProperties;
ConsoleHelper.WriteMessage(ConsoleHelper.Type.Walk, $"{renderProps.GetDestinationX(),3},{renderProps.GetDestinationY(),3}");
await TrySend(_characterActions.Walk);
await Task.Delay(TimeSpan.FromMilliseconds(WALK_BACKOFF_MS));
_fixedTimeStepRepository.Tick(WALK_BACKOFF_MS / 10);
await Delay(WALK_BACKOFF_MS);
}

private async Task Face(EODirection direction)
Expand All @@ -283,8 +280,7 @@ private async Task Face(EODirection direction)
_characterRepository.MainCharacter = _characterRepository.MainCharacter
.WithRenderProperties(_characterRepository.MainCharacter.RenderProperties.WithDirection(direction));

await Task.Delay(TimeSpan.FromMilliseconds(FACE_BACKOFF_MS));
_fixedTimeStepRepository.Tick(FACE_BACKOFF_MS / 10);
await Delay(FACE_BACKOFF_MS);
}

private async Task FaceCoordinateIfNeeded(MapCoordinate originalCoord, MapCoordinate targetCoord)
Expand Down Expand Up @@ -312,8 +308,7 @@ private async Task PickUpItems(IMapCellState cellState)

if (JunkItemIds.Contains(item.ItemID))
{
await Task.Delay(TimeSpan.FromSeconds(1));
_fixedTimeStepRepository.Tick(100);
await Delay(1000);
await JunkItem(item);
}
}
Expand All @@ -330,16 +325,14 @@ await TrySend(() =>
else
ConsoleHelper.WriteMessage(ConsoleHelper.Type.Warning, $"Ignoring item {itemName} x{item.Amount} due to pickup error {pickupResult}", ConsoleColor.DarkYellow);
});
await Task.Delay(TimeSpan.FromMilliseconds(ATTACK_BACKOFF_MS));
_fixedTimeStepRepository.Tick(ATTACK_BACKOFF_MS / 10);
await Delay(ATTACK_BACKOFF_MS);
}

private async Task JunkItem(MapItem item)
{
ConsoleHelper.WriteMessage(ConsoleHelper.Type.JunkItem, $"{item.Amount,7} - {_itemData.Single(x => x.ID == item.ItemID).Name}");
await TrySend(() => _itemActions.JunkItem(item.ItemID, item.Amount));
await Task.Delay(TimeSpan.FromMilliseconds(ATTACK_BACKOFF_MS));
_fixedTimeStepRepository.Tick(ATTACK_BACKOFF_MS / 10);
await Delay(ATTACK_BACKOFF_MS);
}

private async Task CastHealSpell(IEnumerable<InventorySpell> healSpells)
Expand All @@ -353,12 +346,10 @@ private async Task CastHealSpell(IEnumerable<InventorySpell> healSpells)
ConsoleHelper.WriteMessage(ConsoleHelper.Type.Cast, $"{spellToUse.HP,4} HP - {spellToUse.Name} - TP {stats[CharacterStat.TP]}/{stats[CharacterStat.MaxTP]}");

await TrySend(() => _characterActions.PrepareCastSpell(spellToUse.ID));
await Task.Delay(spellToUse.CastTime * 480);
_fixedTimeStepRepository.Tick((uint)spellToUse.CastTime * 48);
await Delay((uint)spellToUse.CastTime * 480);

await TrySend(() => _characterActions.CastSpell(spellToUse.ID, _characterRepository.MainCharacter));
await Task.Delay(ATTACK_BACKOFF_MS); // 600ms cooldown between spell casts
_fixedTimeStepRepository.Tick(ATTACK_BACKOFF_MS / 10);
await Delay(ATTACK_BACKOFF_MS); // 600ms cooldown between spell casts
}

private async Task UseHealItem(IEnumerable<InventoryItem> healItems)
Expand All @@ -372,9 +363,7 @@ private async Task UseHealItem(IEnumerable<InventoryItem> healItems)
ConsoleHelper.WriteMessage(ConsoleHelper.Type.UseItem, $"{itemToUse.Name} - {itemToUse.HP} HP - inventory: {amount - 1} - (other heal item types: {healItems.Count() - 1})");

await TrySend(() => _itemActions.UseItem(itemToUse.ID));

await Task.Delay(ATTACK_BACKOFF_MS);
_fixedTimeStepRepository.Tick(ATTACK_BACKOFF_MS / 10);
await Delay(ATTACK_BACKOFF_MS);
}

private async Task ToggleSit()
Expand All @@ -400,10 +389,15 @@ private async Task TrySend(Action action, uint attempts = 3)
if (i == attempts)
throw;

await Task.Delay(TimeSpan.FromSeconds(i * i));
_fixedTimeStepRepository.Tick(i * i * 100);
await Delay(i * i * 1000);
}
}
}

private Task Delay(uint milliseconds)
{
_fixedTimeStepRepository.Tick(milliseconds / 10);
return Task.Delay((int)milliseconds);
}
}
}
14 changes: 1 addition & 13 deletions EOLib/misc.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Diagnostics;

namespace EOLib
namespace EOLib
{
public static class ArrayExtension
{
Expand All @@ -19,15 +16,6 @@ public static T[] SubArray<T>(this T[] arr, int offset, int count)
}
}

public static class DateTimeExtension
{
public static int ToEOTimeStamp(this Stopwatch sw)
{
var elapsedHundreths = Math.Round(sw.ElapsedTicks / (Stopwatch.Frequency / 100.0));
return (int)elapsedHundreths;
}
}

public static class Constants
{
public const int ResponseTimeout = 5000;
Expand Down
2 changes: 1 addition & 1 deletion EndlessClient/GameExecution/EndlessGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ protected override void Initialize()
IsMouseVisible = true;
IsFixedTimeStep = false;

TargetElapsedTime = TimeSpan.FromMilliseconds(12);
TargetElapsedTime = TimeSpan.FromMilliseconds(FixedTimeStepRepository.TICK_TIME_MS);

_previousKeyState = Keyboard.GetState();

Expand Down

0 comments on commit 5ef4c76

Please sign in to comment.