Skip to content

Commit 313e1a8

Browse files
committed
LGPE Flee routine is now faster. Improved overworld checker routine as well.
1 parent 5af6575 commit 313e1a8

File tree

3 files changed

+47
-48
lines changed

3 files changed

+47
-48
lines changed

SysBot.Pokemon/LGPE/BotOverworld/BotOverworld7B.cs

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public BotOverworld7B(PokeBotState cfg, PokeBotHub<PK8> hub) : base(cfg)
2727
public override async Task MainLoop(CancellationToken token)
2828
{
2929
Log("Identifying trainer data of the host console.");
30-
var sav = await IdentifyTrainer(token).ConfigureAwait(false);
30+
await IdentifyTrainer(token).ConfigureAwait(false);
3131
await InitializeHardware(Settings, token).ConfigureAwait(false);
3232

3333
try
@@ -65,11 +65,10 @@ private async Task Overworld(CancellationToken token, bool birds = false)
6565
var i = 0;
6666
var freeze = false;
6767
var searchforshiny = Settings.OnlyShiny;
68-
uint newspawn;
68+
int newspawn;
6969
uint catchcombo;
7070
uint speciescombo;
7171
bool found;
72-
int lastSpawn;
7372

7473
if (movementslist.Count > 0)
7574
Log($"{Environment.NewLine}----------------------------------------{Environment.NewLine}" +
@@ -151,36 +150,40 @@ private async Task Overworld(CancellationToken token, bool birds = false)
151150

152151
//Check if inside an unwanted encounter
153152
if (await IsInCatchScreen(token).ConfigureAwait(false))
154-
await FleeToOverworld(token).ConfigureAwait(false);
155-
156-
//Check new spawns
157-
newspawn = BitConverter.ToUInt16(await Connection.ReadBytesAsync(LastSpawn, 2, token).ConfigureAwait(false), 0);
158-
if (newspawn != 0)
159153
{
160-
//Count and log the LastSpawn
161-
encounterCount++;
162-
Settings.AddCompletedScans();
163-
lastSpawn = (int)newspawn;
164-
var msg = $"New spawn ({encounterCount}): {lastSpawn} {SpeciesName.GetSpeciesName((int)lastSpawn, 4)}";
165-
Log(msg);
166-
167-
//Set the LastSpawn to 0, so we can account multiple consecutive spawns of the same species. Thanks Anubis for the suggestion!
154+
await FleeToOverworld(token).ConfigureAwait(false);
155+
//The encounter changes the LastSpawn value.
168156
await Connection.WriteBytesAsync(new byte[] { 0x0, 0x0 }, LastSpawn, token).ConfigureAwait(false);
169-
170-
if (!searchforshiny &&
171-
((!birds && lastSpawn == (int)Settings.StopOnSpecies) ||
172-
(!birds && (int)Settings.StopOnSpecies == 0) ||
173-
(birds && (lastSpawn == 144 || lastSpawn == 145 || lastSpawn == 146))))
157+
}
158+
else
159+
{
160+
//Check new spawns
161+
newspawn = BitConverter.ToUInt16(await Connection.ReadBytesAsync(LastSpawn, 2, token).ConfigureAwait(false), 0);
162+
if (newspawn != 0)
174163
{
175-
await Click(X, 1_000, token).ConfigureAwait(false);
176-
await Click(HOME, 1_000, token).ConfigureAwait(false);
177-
178-
msg = "Stop conditions met, restart the bot(s) to search again.";
179-
if (!string.IsNullOrWhiteSpace(Hub.Config.StopConditions.MatchFoundEchoMention))
180-
msg = $"{Hub.Config.StopConditions.MatchFoundEchoMention} {msg}";
181-
Log(msg);
182-
183-
return;
164+
//Count and log the LastSpawn
165+
encounterCount++;
166+
Settings.AddCompletedScans();
167+
Log($"New spawn ({encounterCount}): {newspawn} {SpeciesName.GetSpeciesName(newspawn, 4)}");
168+
169+
//Set the LastSpawn to 0, so we can account multiple consecutive spawns of the same species. Thanks Anubis for the suggestion!
170+
await Connection.WriteBytesAsync(new byte[] { 0x0, 0x0 }, LastSpawn, token).ConfigureAwait(false);
171+
172+
if (!searchforshiny &&
173+
((!birds && newspawn == (int)Settings.StopOnSpecies) ||
174+
(!birds && (int)Settings.StopOnSpecies == 0) ||
175+
(birds && (newspawn == 144 || newspawn == 145 || newspawn == 146))))
176+
{
177+
await Click(X, 1_000, token).ConfigureAwait(false);
178+
await Click(HOME, 1_000, token).ConfigureAwait(false);
179+
180+
var msg = "Stop conditions met, restart the bot(s) to search again.";
181+
if (!string.IsNullOrWhiteSpace(Hub.Config.StopConditions.MatchFoundEchoMention))
182+
msg = $"{Hub.Config.StopConditions.MatchFoundEchoMention} {msg}";
183+
Log(msg);
184+
185+
return;
186+
}
184187
}
185188
}
186189
}
@@ -190,13 +193,12 @@ private async Task Overworld(CancellationToken token, bool birds = false)
190193

191194
await Unfreeze(token, version).ConfigureAwait(false);
192195
freeze = false;
193-
await Task.Delay(0_500, token).ConfigureAwait(false);
194-
newspawn = BitConverter.ToUInt16(await Connection.ReadBytesAsync(LastSpawn, 2, token).ConfigureAwait(false), 0);
196+
newspawn = BitConverter.ToUInt16(await Connection.ReadBytesAsync(LastSpawn_r, 2, token).ConfigureAwait(false), 0);
195197

196198
//Stop Conditions
197-
if (birds && ((int)newspawn == 144 || (int)newspawn == 145 || (int)newspawn == 146) && !token.IsCancellationRequested)
199+
if (birds && (newspawn == 144 || newspawn == 145 || newspawn == 146) && !token.IsCancellationRequested)
198200
found = true;
199-
else if ((!birds && (int)Settings.StopOnSpecies > 0 && (int)newspawn == (int)Settings.StopOnSpecies) ||
201+
else if ((!birds && (int)Settings.StopOnSpecies > 0 && newspawn == (int)Settings.StopOnSpecies) ||
200202
(!birds && (int)Settings.StopOnSpecies == 0))
201203
found = true;
202204
else
@@ -317,17 +319,10 @@ private async Task FleeToOverworld(CancellationToken token)
317319
{
318320
await ResetStick(token).ConfigureAwait(false);
319321
Log($"Unwanted encounter detected!");
320-
int i = 0;
321-
while (await IsInCatchScreen(token).ConfigureAwait(false) && !token.IsCancellationRequested)
322-
{
323-
i++;
324-
await Task.Delay(8_000, token).ConfigureAwait(false);
325-
if (i > 2)
326-
await Click(B, 1_200, token).ConfigureAwait(false);
322+
while (!await IsInConfirmDialog(token).ConfigureAwait(false) && !token.IsCancellationRequested)
327323
await Click(B, 1_200, token).ConfigureAwait(false);
328-
await Click(A, 1_000, token).ConfigureAwait(false);
329-
await Task.Delay(6_500, token).ConfigureAwait(false);
330-
}
324+
await Click(A, 1_000, token).ConfigureAwait(false);
325+
while (await IsInCatchScreen(token).ConfigureAwait(false) && !token.IsCancellationRequested) { }
331326
Log($"Exited wild encounter.");
332327
}
333328
}

SysBot.Pokemon/LGPE/Data/PokeDataOffsets7B.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ public static class PokeDataOffsets7B
44
{
55
public const int BoxFormatSlotSize = 0x104;
66
public const int TrainerSize = 0x168;
7-
public const uint FreezedValue = 0x1610EE0; //1 byte
7+
public const uint FreezedValue = 0x1610EE0; //main - 1 byte
8+
public const uint IsInConfirmationDialog = 0x1654494; //main
89
public const uint IsInOverworld = 0x163F694; //main
910
public const uint IsInBattleScenario = 0x1EE067C; //main
1011
public const uint IsInTitleScreen = 0x160D4E0; //main
@@ -13,6 +14,7 @@ public static class PokeDataOffsets7B
1314
public const uint StationaryBattleData = 0x9A118D68; //heap
1415
public const uint PokeData = 0x163EDC0; //main
1516
public const uint LastSpawn = 0x5E12B148; //heap
17+
public const uint LastSpawn_r = 0x5E12C120; //heap
1618
public const uint EShinyValue = 0x7398C4; //main
1719
public const uint PShinyValue = 0x739864; //main
1820
public const uint PGeneratingFunction = 0x7398D0; //main
@@ -26,8 +28,8 @@ public static class PokeDataOffsets7B
2628
public const uint CatchingSpecies = 0x9A264598; //heap - Thanks Lincoln-LM!
2729
public const uint CatchCombo = 0x5E1CF500; //heap - Thanks Lincoln-LM!
2830
public const uint SpeciesCombo = 0x5E1CF4F8; //heap - Thanks Lincoln-LM!
29-
public const uint LureType = 0x53405D28; //Thanks Anubis!
30-
public const uint LureCounter = 0x53405D2A; //Thanks Anubis!
31-
public const uint TextSpeedOffset = 0x53321EDC; //Thanks Anubis!
31+
public const uint LureType = 0x53405D28; // heap - Thanks Anubis!
32+
public const uint LureCounter = 0x53405D2A; //heap - Thanks Anubis!
33+
public const uint TextSpeedOffset = 0x53321EDC; //heap - Thanks Anubis!
3234
}
3335
}

SysBot.Pokemon/LGPE/PokeRoutineExecutor7B.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ public override async Task<PB7> ReadBoxPokemon(int box, int slot, CancellationTo
7979

8080
public async Task<bool> IsInCatchScreen(CancellationToken token) => (await SwitchConnection.ReadBytesMainAsync(IsInOverworld, 1, token).ConfigureAwait(false))[0] != 0;
8181

82+
public async Task<bool> IsInConfirmDialog(CancellationToken token) => (await SwitchConnection.ReadBytesMainAsync(IsInConfirmationDialog, 1, token).ConfigureAwait(false))[0] != 0;
83+
8284
public async Task<bool> IsInTrade(CancellationToken token) => (await SwitchConnection.ReadBytesMainAsync(PokeDataOffsets7B.IsInTrade, 1, token).ConfigureAwait(false))[0] != 0;
8385

8486
public async Task<bool> IsGiftFound(CancellationToken token) => (await SwitchConnection.ReadBytesMainAsync(PokeDataOffsets7B.IsGiftFound, 1, token).ConfigureAwait(false))[0] > 0;

0 commit comments

Comments
 (0)