Skip to content

Commit

Permalink
Fix infinite loop using heal items, add more output when attacking stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanmoffat committed May 10, 2021
1 parent b9ff4cf commit 8db36a2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
15 changes: 14 additions & 1 deletion EOBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using EOLib.Domain.Map;
using EOLib.Domain.Notifiers;
using EOLib.Domain.NPC;
using EOLib.IO.Repositories;
using System;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -19,14 +20,26 @@ static class Program
class NpcWalkNotifier : INPCActionNotifier
{
private readonly ICurrentMapStateRepository _currentMapStateRepository;
private readonly ICharacterProvider _characterProvider;
private readonly IENFFileProvider _enfFileProvider;

public NpcWalkNotifier(ICurrentMapStateRepository currentMapStateRepository)
public NpcWalkNotifier(ICurrentMapStateRepository currentMapStateRepository,
ICharacterProvider characterProvider,
IENFFileProvider enfFileProvider)
{
_currentMapStateRepository = currentMapStateRepository;
_characterProvider = characterProvider;
_enfFileProvider = enfFileProvider;
}

public void NPCTakeDamage(short npcIndex, int fromPlayerId, int damageToNpc, short npcPctHealth, EOLib.Optional<int> spellId)
{
if (fromPlayerId != _characterProvider.MainCharacter.ID)
return;

var npc = _currentMapStateRepository.NPCs.SingleOrDefault(x => x.Index == npcIndex);
var npcName = _enfFileProvider.ENFFile.Data.SingleOrDefault(x => npc != null && npc.ID == x.ID)?.Name;
Console.WriteLine($"[DMG ] {damageToNpc,7} - {npcPctHealth}% {npcIndex} - {npcName ?? string.Empty}");
}

public void RemoveNPCFromView(int npcIndex, int playerId, EOLib.Optional<short> spellId, EOLib.Optional<int> damage, bool showDeathAnimation)
Expand Down
36 changes: 21 additions & 15 deletions EOBot/TrainerBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,14 @@ protected override async Task DoWorkAsync(CancellationToken ct)
}
else if (healItems.Any() && character.Stats[CharacterStat.HP] < character.Stats[CharacterStat.MaxHP] * .3)
{
await UseHealItem(healItems, targetHealthPercent: .6);
var stats = _characterRepository.MainCharacter.Stats;
while (!TerminationRequested && stats[CharacterStat.HP] < stats[CharacterStat.MaxHP] * .6)
{
await UseHealItem(healItems);
handler.PollForPacketsAndHandle();
stats = _characterRepository.MainCharacter.Stats;
}

action_taken = true;
}
else if (healSpells.Any() && character.Stats[CharacterStat.HP] < character.Stats[CharacterStat.MaxHP]
Expand Down Expand Up @@ -235,7 +242,8 @@ private async Task Walk()

private async Task Face(EODirection direction)
{
Console.WriteLine($"[FACE] {Enum.GetName(typeof(EODirection), direction),7}");
var rp = _characterRepository.MainCharacter.RenderProperties;
Console.WriteLine($"[FACE] {Enum.GetName(typeof(EODirection), direction),7} - at {rp.MapX},{rp.MapY}");
await TrySend(() => _characterActions.Face(direction));

// todo: character actions Face() should also change the character's direction instead of relying on client to update it separately
Expand Down Expand Up @@ -280,7 +288,8 @@ private async Task CastHealSpell(IEnumerable<IInventorySpell> healSpells)
.OrderByDescending(x => x.HP)
.First();

Console.WriteLine($"[CAST] {spellToUse.HP,4} HP - {spellToUse.Name}");
var stats = _characterRepository.MainCharacter.Stats.Stats;
Console.WriteLine($"[CAST] {spellToUse.HP,4} HP - {spellToUse.Name} - TP {stats[CharacterStat.TP]}/{stats[CharacterStat.MaxTP]}");

await TrySend(() => _characterActions.PrepareCastSpell(spellToUse.ID));
await Task.Delay((int)Math.Round(spellToUse.CastTime / 2.0 * 950)); // ?
Expand All @@ -289,22 +298,19 @@ private async Task CastHealSpell(IEnumerable<IInventorySpell> healSpells)
await Task.Delay((int)Math.Round(spellToUse.CastTime / 2.0 * 950)); // ?
}

private async Task UseHealItem(IEnumerable<IInventoryItem> healItems, double targetHealthPercent)
private async Task UseHealItem(IEnumerable<IInventoryItem> healItems)
{
while (_characterRepository.MainCharacter.Stats[CharacterStat.HP] < _characterRepository.MainCharacter.Stats[CharacterStat.MaxHP] * targetHealthPercent)
{
var itemToUse = _itemData.Data
.Where(x => healItems.Any(y => y.ItemID == x.ID))
.OrderBy(x => x.HP)
.First();
var amount = healItems.Single(x => x.ItemID == itemToUse.ID).Amount;
var itemToUse = _itemData.Data
.Where(x => healItems.Any(y => y.ItemID == x.ID))
.OrderBy(x => x.HP)
.First();
var amount = healItems.Single(x => x.ItemID == itemToUse.ID).Amount;

Console.WriteLine($"[USE ] {itemToUse.Name} - {itemToUse.HP} HP - inventory: {amount - 1} - (other heal item types: {healItems.Count() - 1})");
Console.WriteLine($"[USE ] {itemToUse.Name} - {itemToUse.HP} HP - inventory: {amount - 1} - (other heal item types: {healItems.Count() - 1})");

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

await Task.Delay(ATTACK_BACKOFF_MS);
}
await Task.Delay(ATTACK_BACKOFF_MS);
}

private async Task SitDown()
Expand Down

0 comments on commit 8db36a2

Please sign in to comment.