Skip to content

Commit 804d88b

Browse files
committed
how the heck does this still work?
1 parent 4cdcde0 commit 804d88b

File tree

6 files changed

+102
-71
lines changed

6 files changed

+102
-71
lines changed

Helpers/AutoLegality.cs

Lines changed: 57 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
using System;
2+
using System.Text.RegularExpressions;
3+
using System.Linq;
24
using System.Threading;
35
using PKHeX.Core;
46
using PKHeX.Core.AutoMod;
7+
using Microsoft.Extensions.FileSystemGlobbing.Internal.Patterns;
8+
59
namespace CoreAPI.Helpers
610
{
711

@@ -10,7 +14,8 @@ public class AutoLegality
1014
private static PKM legalpk;
1115
private static LegalityAnalysis la;
1216
private static bool Initialized;
13-
public bool Successful = true;
17+
private readonly Random _random = new Random();
18+
public bool Successful = false;
1419
public bool Ran = true;
1520
public string Report;
1621

@@ -29,7 +34,7 @@ public static void Initalize()
2934
Legalizer.AllowBruteForce = true;
3035
}
3136

32-
public AutoLegality(PKM pk, string ver)
37+
public AutoLegality(PKM pk, string ver)
3338
{
3439
EnsureInitialized();
3540
bool valid = Enum.TryParse<GameVersion>(ver, true, out var game);
@@ -39,7 +44,7 @@ public AutoLegality(PKM pk, string ver)
3944
}
4045

4146
private void ProcessALM(PKM pkm, GameVersion ver = GameVersion.GP)
42-
{
47+
{
4348
la = new LegalityAnalysis(pkm);
4449
if (la.Valid)
4550
{
@@ -48,12 +53,12 @@ private void ProcessALM(PKM pkm, GameVersion ver = GameVersion.GP)
4853
Report = la.Report();
4954
return;
5055
}
51-
if (la.Report().ToLower().Contains("invalid move")){
56+
/*if (la.Report().ToLower().Contains("invalid move")){
5257
Ran = true; // because piepie62 and griffin wanted to make my program a liar. GG guys GG.
5358
Successful = false;
5459
Report = la.Report();
5560
return;
56-
}
61+
}*/
5762
legalpk = Legalize(pkm, ver);
5863
}
5964

@@ -71,85 +76,68 @@ private SimpleTrainerInfo getInfo(PKM pk, GameVersion ver)
7176
return info;
7277
}
7378

74-
private PKM Legalize(PKM pk, GameVersion ver)
79+
private int ChooseRandomMove(int[] moves)
7580
{
76-
var KeepOriginalData = true;
77-
Successful = false;
78-
SimpleTrainerInfo info = getInfo(pk, ver);
79-
80-
if (la.Report().ToLower().Contains("wordfilter") || la.Report().Contains("SID") || la.Report().Contains("TID"))
81+
var mvs = la.AllSuggestedMovesAndRelearn().Where(move => !moves.Contains(move));
82+
if(mvs.Count() == 0)
8183
{
82-
KeepOriginalData = false;
84+
return 0;
8385
}
86+
return mvs.ElementAt(Rand.RandomNum() % mvs.Count());
87+
}
88+
private PKM Legalize(PKM pk, GameVersion ver)
89+
{
90+
Report = la.Report();
91+
var sav = SaveUtil.GetBlankSAV(ver, pk.OT_Name);
92+
sav.TID = pk.TID;
93+
sav.SID = pk.SID;
94+
sav.Language = pk.Language;
8495
Legalizer.AllowBruteForce = true;
96+
Legalizer.EnableEasterEggs = false;
8597
Legalizer.AllowAPI = true;
86-
var timeout = TimeSpan.FromSeconds(5);
87-
var started = DateTime.UtcNow;
88-
PKM updated;
89-
var thread = new Thread(() => {
90-
if (KeepOriginalData)
91-
{
92-
updated = Legalizer.Legalize(pk);
93-
var report = la.Report().ToLower();
94-
if(!report.Contains("handling trainer"))
95-
{
96-
if (!report.Contains("untraded"))
97-
{
98-
info.ApplyToPKM(updated);
99-
if (!report.Contains("memory"))
100-
updated.HT_Memory = pk.HT_Memory;
101-
if (!report.Contains("affection"))
102-
updated.HT_Affection = pk.HT_Affection;
103-
if (!report.Contains("feeling"))
104-
updated.HT_Feeling = pk.HT_Feeling;
105-
if (!report.Contains("friendship"))
106-
updated.HT_Friendship = pk.HT_Friendship;
107-
if (!report.Contains("intensity"))
108-
updated.HT_Intensity = pk.HT_Intensity;
109-
if (!report.Contains("trash"))
110-
updated.HT_Trash = pk.HT_Trash;
111-
updated.HT_Gender = pk.HT_Gender;
112-
updated.HT_Name = pk.HT_Name;
113-
updated.HT_TextVar = pk.HT_TextVar;
114-
}
115-
} else
116-
{
117-
updated.OT_Memory = pk.OT_Memory;
118-
updated.OT_Friendship = pk.OT_Friendship;
119-
updated.OT_Name = pk.OT_Name;
120-
updated.OT_Affection = pk.OT_Affection;
121-
updated.OT_Feeling = pk.OT_Feeling;
122-
updated.OT_Gender = pk.OT_Gender;
123-
updated.OT_Intensity = pk.OT_Intensity;
124-
updated.OT_TextVar = pk.OT_TextVar;
125-
updated.OT_Trash = pk.OT_Trash;
126-
}
127-
} else
98+
APILegality.PrioritizeGame = true;
99+
APILegality.UseTrainerData = false;
100+
var r = new Regex(@"invalid move ([1-4]):", RegexOptions.IgnoreCase);
101+
var matches = r.Matches(la.Report());
102+
foreach (Match match in matches)
103+
{
104+
int movePos;
105+
if (int.TryParse(match.Groups[1].Value, out movePos))
128106
{
129-
updated = Legalizer.Legalize(pk);
107+
var mvs = pk.Moves;
108+
mvs[movePos - 1] = ChooseRandomMove(pk.Moves);
109+
pk.Moves = mvs;
130110
}
131-
if (new LegalityAnalysis(updated).Valid)
111+
}
112+
113+
PKM upd = sav.Legalize(pk.Clone());
114+
upd.SetTrainerData(getInfo(pk, ver));
115+
la = new LegalityAnalysis(upd);
116+
if(la.Valid)
117+
{
118+
legalpk = upd;
119+
Successful = true;
120+
//Report = la.Report();
121+
}
122+
else
123+
{
124+
upd = sav.Legalize(pk.Clone());
125+
la = new LegalityAnalysis(upd);
126+
if (la.Valid)
132127
{
133-
legalpk = updated;
128+
legalpk = upd;
134129
Successful = true;
135-
Report = la.Report();
130+
} else
131+
{
132+
Console.WriteLine(la.Report());
136133
}
137-
});
138-
thread.Start();
139-
while (thread.IsAlive && DateTime.UtcNow - started < timeout)
140-
{
141-
Thread.Sleep(100);
142134
}
143-
if (thread.IsAlive)
144-
thread.Abort();
145-
135+
146136
if (Successful)
147137
{
148138
return legalpk;
149-
} else
150-
{
151-
Report = la.Report();
152139
}
140+
153141
return null;
154142
}
155143
public PKM GetLegalPKM()

Helpers/Rand.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
7+
namespace CoreAPI.Helpers
8+
{
9+
public static class Rand
10+
{
11+
static int seed = Environment.TickCount;
12+
13+
static readonly ThreadLocal<Random> random =
14+
new ThreadLocal<Random>(() => new Random(Interlocked.Increment(ref seed)));
15+
16+
public static int RandomNum()
17+
{
18+
return random.Value.Next();
19+
}
20+
}
21+
}

Helpers/Utils.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,9 @@ public static string GetPokeSprite(int pokemonNum, string pokemonName, string po
269269
{
270270
formSet = true;
271271
}
272-
pokemonName = pokemonName.Replace("'", "").Replace("é", "e").Replace("’", "");
272+
pokemonName = pokemonName.Replace("'", "").Replace("é", "e").Replace("’", "").Replace(" ", "-");
273273
form = form.Replace("%-C", "").Replace("%", "").Replace("é", "e");
274-
var url = "https://sprites.fm1337.com/";
274+
var url = "http://server.charizard-is.best/"; // god fucking dammit I forgot the slash earlier and they're not using HTTPS. Fuck me.
275275
if (generation == "LGPE")
276276
{
277277
generation = "7";

Moves.csv

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,3 +796,25 @@ Index,Type
796796
794,1
797797
795,15
798798
796,8
799+
797,13
800+
798,8
801+
799,15
802+
800,5
803+
801,3
804+
802,17
805+
803,11
806+
804,12
807+
805,0
808+
806,6
809+
807,9
810+
808,16
811+
809,7
812+
810,3
813+
811,1
814+
812,10
815+
813,14
816+
814,2
817+
815,4
818+
816,11
819+
817,16
820+
818,10

deps/PKHeX.Core.AutoMod.dll

6 KB
Binary file not shown.

deps/PKHeX.Core.dll

26 KB
Binary file not shown.

0 commit comments

Comments
 (0)