2016-06-20 04:22:43 +00:00
|
|
|
|
using System;
|
2016-09-04 06:14:05 +00:00
|
|
|
|
using System.Collections.Generic;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2017-01-08 07:54:09 +00:00
|
|
|
|
namespace PKHeX.Core
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2017-10-24 06:12:58 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Logic for exporting and importing <see cref="PKM"/> data in Pokémon Showdown's text format.
|
|
|
|
|
/// </summary>
|
2016-06-20 04:22:43 +00:00
|
|
|
|
public class ShowdownSet
|
|
|
|
|
{
|
|
|
|
|
// String to Values
|
2016-09-20 05:59:15 +00:00
|
|
|
|
private static readonly string[] StatNames = { "HP", "Atk", "Def", "SpA", "SpD", "Spe" };
|
2017-12-27 00:29:35 +00:00
|
|
|
|
private static readonly string[] genders = {"M", "F", ""};
|
2018-01-30 01:20:12 +00:00
|
|
|
|
private static readonly string[] genderForms = {"", "F", ""};
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private const string Language = "en";
|
|
|
|
|
private static readonly string[] types = Util.GetTypesList(Language);
|
|
|
|
|
private static readonly string[] forms = Util.GetFormsList(Language);
|
|
|
|
|
private static readonly string[] species = Util.GetSpeciesList(Language);
|
|
|
|
|
private static readonly string[] items = Util.GetItemsList(Language);
|
|
|
|
|
private static readonly string[] natures = Util.GetNaturesList(Language);
|
|
|
|
|
private static readonly string[] moves = Util.GetMovesList(Language);
|
|
|
|
|
private static readonly string[] abilities = Util.GetAbilitiesList(Language);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
private static readonly string[] hptypes = types.Skip(1).ToArray();
|
2017-12-27 00:29:35 +00:00
|
|
|
|
private static int MAX_SPECIES => species.Length-1;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
|
|
|
|
|
// Default Set Data
|
2016-09-20 05:59:15 +00:00
|
|
|
|
public string Nickname { get; set; }
|
|
|
|
|
public int Species { get; private set; } = -1;
|
|
|
|
|
public string Form { get; private set; }
|
|
|
|
|
public string Gender { get; private set; }
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public int HeldItem { get; private set; }
|
2016-09-20 05:59:15 +00:00
|
|
|
|
public int Ability { get; private set; }
|
|
|
|
|
public int Level { get; private set; } = 100;
|
|
|
|
|
public bool Shiny { get; private set; }
|
|
|
|
|
public int Friendship { get; private set; } = 255;
|
|
|
|
|
public int Nature { get; private set; }
|
2018-01-30 01:20:12 +00:00
|
|
|
|
public int FormIndex { get; private set; }
|
2016-09-20 05:59:15 +00:00
|
|
|
|
public int[] EVs { get; private set; } = {00, 00, 00, 00, 00, 00};
|
|
|
|
|
public int[] IVs { get; private set; } = {31, 31, 31, 31, 31, 31};
|
|
|
|
|
public int[] Moves { get; private set; } = {0, 0, 0, 0};
|
2016-09-10 02:13:48 +00:00
|
|
|
|
public readonly List<string> InvalidLines = new List<string>();
|
2016-06-20 04:22:43 +00:00
|
|
|
|
|
2016-09-20 05:59:15 +00:00
|
|
|
|
private int[] IVsSpeedFirst => new[] {IVs[0], IVs[1], IVs[2], IVs[5], IVs[3], IVs[4]};
|
|
|
|
|
private int[] IVsSpeedLast => new[] {IVs[0], IVs[1], IVs[2], IVs[4], IVs[5], IVs[3]};
|
|
|
|
|
private int[] EVsSpeedFirst => new[] {EVs[0], EVs[1], EVs[2], EVs[5], EVs[3], EVs[4]};
|
|
|
|
|
private int[] EVsSpeedLast => new[] {EVs[0], EVs[1], EVs[2], EVs[4], EVs[5], EVs[3]};
|
2016-09-18 05:10:27 +00:00
|
|
|
|
|
2016-06-20 04:22:43 +00:00
|
|
|
|
// Parsing Utility
|
|
|
|
|
public ShowdownSet(string input = null)
|
|
|
|
|
{
|
|
|
|
|
if (input == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
string[] lines = input.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
|
|
|
|
|
for (int i = 0; i < lines.Length; i++) lines[i] = lines[i].Replace("'", "’").Trim(); // Sanitize apostrophes
|
|
|
|
|
|
2016-09-20 05:59:15 +00:00
|
|
|
|
lines = lines.Where(line => line.Length > 2).ToArray();
|
|
|
|
|
|
2017-11-18 06:19:23 +00:00
|
|
|
|
if (lines.Length < 3)
|
|
|
|
|
return;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
|
|
|
|
|
// Seek for start of set
|
2016-09-20 05:59:15 +00:00
|
|
|
|
int start = Array.FindIndex(lines, line => line.Contains(" @ "));
|
2017-09-02 15:26:51 +00:00
|
|
|
|
|
2016-09-20 05:59:15 +00:00
|
|
|
|
if (start != -1) // Has Item -- skip to start.
|
|
|
|
|
lines = lines.Skip(start).Take(lines.Length - start).ToArray();
|
|
|
|
|
else // Has no Item -- try parsing the first line anyway.
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
ParseFirstLine(lines[0]);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
if (Species < -1)
|
2016-09-20 05:59:15 +00:00
|
|
|
|
return; // Abort if no text is found
|
|
|
|
|
|
2016-06-20 04:22:43 +00:00
|
|
|
|
lines = lines.Skip(1).Take(lines.Length - 1).ToArray();
|
|
|
|
|
}
|
|
|
|
|
int movectr = 0;
|
|
|
|
|
// Detect relevant data
|
|
|
|
|
foreach (string line in lines)
|
|
|
|
|
{
|
2016-09-20 05:59:15 +00:00
|
|
|
|
if (line.StartsWith("-"))
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
string moveString = ParseLineMove(line);
|
2016-09-18 05:10:27 +00:00
|
|
|
|
int move = Array.IndexOf(moves, moveString);
|
|
|
|
|
if (move < 0)
|
|
|
|
|
InvalidLines.Add($"Unknown Move: {moveString}");
|
|
|
|
|
else
|
|
|
|
|
Moves[movectr++] = move;
|
|
|
|
|
|
2016-06-20 04:22:43 +00:00
|
|
|
|
if (movectr == 4)
|
|
|
|
|
break; // End of moves
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string[] brokenline = line.Split(new[] { ": " }, StringSplitOptions.None);
|
2017-01-29 08:32:02 +00:00
|
|
|
|
if (brokenline.Length == 1)
|
2017-11-18 06:19:23 +00:00
|
|
|
|
brokenline = new[] {brokenline[0], string.Empty};
|
2016-06-20 04:22:43 +00:00
|
|
|
|
switch (brokenline[0])
|
|
|
|
|
{
|
|
|
|
|
case "Trait":
|
2016-09-20 05:59:15 +00:00
|
|
|
|
case "Ability": { Ability = Array.IndexOf(abilities, brokenline[1].Trim()); break; }
|
2017-09-02 15:26:51 +00:00
|
|
|
|
case "Level": { if (int.TryParse(brokenline[1].Trim(), out int val)) Level = val; else InvalidLines.Add(line); break; }
|
2016-09-20 05:59:15 +00:00
|
|
|
|
case "Shiny": { Shiny = brokenline[1].Trim() == "Yes"; break; }
|
2017-09-02 15:26:51 +00:00
|
|
|
|
case "Happiness": { if (int.TryParse(brokenline[1].Trim(), out int val)) Friendship = val; else InvalidLines.Add(line); break; }
|
2016-09-20 05:59:15 +00:00
|
|
|
|
case "Nature": { Nature = Array.IndexOf(natures, brokenline[1].Trim()); break; }
|
|
|
|
|
case "EV":
|
2017-06-18 01:37:19 +00:00
|
|
|
|
case "EVs": { ParseLineEVs(brokenline[1].Trim()); break; }
|
2016-09-20 05:59:15 +00:00
|
|
|
|
case "IV":
|
2017-06-18 01:37:19 +00:00
|
|
|
|
case "IVs": { ParseLineIVs(brokenline[1].Trim()); break; }
|
2016-12-17 17:54:22 +00:00
|
|
|
|
case "Type": { brokenline = new[] {line}; goto default; } // Type: Null edge case
|
2016-09-20 05:59:15 +00:00
|
|
|
|
default:
|
|
|
|
|
{
|
|
|
|
|
// Either Nature or Gender ItemSpecies
|
|
|
|
|
if (brokenline[0].Contains(" @ "))
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2016-09-20 05:59:15 +00:00
|
|
|
|
string[] pieces = line.Split(new[] {" @ "}, StringSplitOptions.None);
|
|
|
|
|
string itemstr = pieces.Last().Trim();
|
|
|
|
|
int item = Array.IndexOf(items, itemstr);
|
|
|
|
|
if (item < 0)
|
|
|
|
|
InvalidLines.Add($"Unknown Item: {itemstr}");
|
|
|
|
|
else
|
2017-06-18 01:37:19 +00:00
|
|
|
|
HeldItem = item;
|
2016-09-20 05:59:15 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
ParseFirstLine(pieces[0]);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
2016-09-20 05:59:15 +00:00
|
|
|
|
else if (brokenline[0].Contains("Nature"))
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2016-09-20 05:59:15 +00:00
|
|
|
|
string naturestr = line.Split(' ')[0].Trim();
|
|
|
|
|
int nature = Array.IndexOf(natures, naturestr);
|
2016-11-27 03:23:19 +00:00
|
|
|
|
if (nature < 0)
|
2016-09-20 05:59:15 +00:00
|
|
|
|
InvalidLines.Add($"Unknown Nature: {naturestr}");
|
|
|
|
|
else
|
|
|
|
|
Nature = nature;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
2016-09-20 05:59:15 +00:00
|
|
|
|
else // Fallback
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2016-09-20 05:59:15 +00:00
|
|
|
|
string speciesstr = line.Split('(')[0].Trim();
|
|
|
|
|
int spec = Array.IndexOf(species, speciesstr);
|
|
|
|
|
if (spec < 1)
|
|
|
|
|
InvalidLines.Add(speciesstr);
|
|
|
|
|
else
|
|
|
|
|
Species = spec;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
2016-09-20 05:59:15 +00:00
|
|
|
|
}
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-18 05:10:27 +00:00
|
|
|
|
|
2016-12-23 23:37:56 +00:00
|
|
|
|
// Showdown Quirks
|
2017-09-02 15:26:51 +00:00
|
|
|
|
Form = ConvertFormFromShowdown(Form, Species, Ability);
|
2018-01-30 01:20:12 +00:00
|
|
|
|
// Set Form
|
|
|
|
|
string[] formStrings = PKX.GetFormList(Species, types, forms, genderForms);
|
|
|
|
|
FormIndex = Math.Max(0, Array.FindIndex(formStrings, z => z.Contains(Form ?? "")));
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
|
|
|
|
|
public string Text => GetText();
|
|
|
|
|
private string GetText()
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2016-09-20 05:59:15 +00:00
|
|
|
|
if (Species == 0 || Species > MAX_SPECIES)
|
2017-11-18 06:19:23 +00:00
|
|
|
|
return string.Empty;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
|
2017-06-18 20:02:02 +00:00
|
|
|
|
var result = new List<string>();
|
|
|
|
|
|
|
|
|
|
// First Line: Name, Nickname, Gender, Item
|
2017-09-02 15:26:51 +00:00
|
|
|
|
string form = ConvertFormToShowdown(Form, Species);
|
2017-06-18 20:02:02 +00:00
|
|
|
|
result.Add(GetStringFirstLine(form));
|
|
|
|
|
|
|
|
|
|
// IVs
|
|
|
|
|
if (GetStringStats(out IEnumerable<string> ivstr, IVsSpeedLast, 31))
|
|
|
|
|
result.Add($"IVs: {string.Join(" / ", ivstr)}");
|
|
|
|
|
|
|
|
|
|
// EVs
|
|
|
|
|
if (GetStringStats(out IEnumerable<string> evstr, EVsSpeedLast, 0))
|
|
|
|
|
result.Add($"EVs: {string.Join(" / ", evstr)}");
|
|
|
|
|
|
|
|
|
|
// Secondary Stats
|
|
|
|
|
if (Ability > -1 && Ability < abilities.Length)
|
|
|
|
|
result.Add($"Ability: {abilities[Ability]}");
|
|
|
|
|
result.Add($"Level: {Level}");
|
|
|
|
|
if (Shiny)
|
|
|
|
|
result.Add("Shiny: Yes");
|
|
|
|
|
|
|
|
|
|
if (Nature > -1)
|
|
|
|
|
result.Add($"{natures[Nature]} Nature");
|
|
|
|
|
|
|
|
|
|
// Moves
|
|
|
|
|
result.AddRange(GetStringMoves());
|
|
|
|
|
|
|
|
|
|
return string.Join(Environment.NewLine, result);
|
|
|
|
|
}
|
|
|
|
|
private string GetStringFirstLine(string form)
|
|
|
|
|
{
|
2016-09-18 05:10:27 +00:00
|
|
|
|
string specForm = species[Species];
|
2016-12-23 23:37:56 +00:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(form))
|
2017-09-30 05:58:25 +00:00
|
|
|
|
specForm += $"-{form.Replace("Mega ", "Mega-")}";
|
2016-10-04 02:24:46 +00:00
|
|
|
|
|
2017-06-18 20:02:02 +00:00
|
|
|
|
string result = Nickname != null && species[Species] != Nickname ? $"{Nickname} ({specForm})" : $"{specForm}";
|
2016-09-18 05:10:27 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(Gender))
|
|
|
|
|
result += $" ({Gender})";
|
2017-06-18 01:37:19 +00:00
|
|
|
|
if (HeldItem > 0 && HeldItem < items.Length)
|
2017-09-30 05:58:25 +00:00
|
|
|
|
result += $" @ {items[HeldItem]}";
|
2017-06-18 20:02:02 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
private static bool GetStringStats(out IEnumerable<string> result, int[] stats, int ignore)
|
|
|
|
|
{
|
|
|
|
|
var list = new List<string>();
|
|
|
|
|
for (int i = 0; i < stats.Length; i++)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2017-06-18 20:02:02 +00:00
|
|
|
|
if (stats[i] == ignore) continue; // ignore unused EVs
|
|
|
|
|
list.Add($"{stats[i]} {StatNames[i]}");
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
2017-06-18 20:02:02 +00:00
|
|
|
|
result = list;
|
|
|
|
|
return list.Count > 0;
|
|
|
|
|
}
|
|
|
|
|
private IEnumerable<string> GetStringMoves()
|
|
|
|
|
{
|
2016-06-20 04:22:43 +00:00
|
|
|
|
foreach (int move in Moves.Where(move => move != 0 && move < moves.Length))
|
|
|
|
|
{
|
2017-06-29 05:09:26 +00:00
|
|
|
|
var str = $"- {moves[move]}";
|
2016-06-28 05:26:39 +00:00
|
|
|
|
if (move == 237) // Hidden Power
|
|
|
|
|
{
|
2018-02-08 04:59:36 +00:00
|
|
|
|
int hp = GetHiddenPowerType(IVs);
|
2017-06-18 20:02:02 +00:00
|
|
|
|
str += $" [{hptypes[hp]}]";
|
2016-06-28 05:26:39 +00:00
|
|
|
|
}
|
2017-06-29 05:09:26 +00:00
|
|
|
|
yield return str;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-18 20:02:02 +00:00
|
|
|
|
|
2017-12-27 00:29:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts the <see cref="PKM"/> data into an importable set format for Pokémon Showdown.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pkm">PKM to convert to string</param>
|
|
|
|
|
/// <returns>Multi line set data</returns>
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static string GetShowdownText(PKM pkm)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2017-11-18 06:19:23 +00:00
|
|
|
|
if (pkm.Species == 0)
|
|
|
|
|
return string.Empty;
|
2016-09-18 05:10:27 +00:00
|
|
|
|
|
2018-01-30 01:20:12 +00:00
|
|
|
|
string[] Forms = PKX.GetFormList(pkm.Species, types, forms, genderForms, pkm.Format);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
ShowdownSet Set = new ShowdownSet
|
|
|
|
|
{
|
|
|
|
|
Nickname = pkm.Nickname,
|
|
|
|
|
Species = pkm.Species,
|
2017-06-18 01:37:19 +00:00
|
|
|
|
HeldItem = pkm.HeldItem,
|
2016-06-20 04:22:43 +00:00
|
|
|
|
Ability = pkm.Ability,
|
|
|
|
|
EVs = pkm.EVs,
|
|
|
|
|
IVs = pkm.IVs,
|
|
|
|
|
Moves = pkm.Moves,
|
|
|
|
|
Nature = pkm.Nature,
|
2017-12-27 00:29:35 +00:00
|
|
|
|
Gender = genders[pkm.Gender < 2 ? pkm.Gender : 2],
|
2016-06-20 04:22:43 +00:00
|
|
|
|
Friendship = pkm.CurrentFriendship,
|
2017-06-18 01:37:19 +00:00
|
|
|
|
Level = PKX.GetLevel(pkm.Species, pkm.EXP),
|
2016-06-20 04:22:43 +00:00
|
|
|
|
Shiny = pkm.IsShiny,
|
2018-01-30 01:20:12 +00:00
|
|
|
|
FormIndex = pkm.AltForm,
|
2017-11-18 06:19:23 +00:00
|
|
|
|
Form = pkm.AltForm > 0 && pkm.AltForm < Forms.Length ? Forms[pkm.AltForm] : string.Empty,
|
2016-06-20 04:22:43 +00:00
|
|
|
|
};
|
2017-02-03 00:58:22 +00:00
|
|
|
|
|
2017-09-02 15:26:51 +00:00
|
|
|
|
if (Set.Form == "F")
|
2017-11-18 06:19:23 +00:00
|
|
|
|
Set.Gender = string.Empty;
|
2017-02-03 00:58:22 +00:00
|
|
|
|
|
2017-06-19 05:27:40 +00:00
|
|
|
|
return Set.Text;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void ParseFirstLine(string line)
|
2016-09-20 05:59:15 +00:00
|
|
|
|
{
|
|
|
|
|
// Gender Detection
|
|
|
|
|
string last3 = line.Substring(line.Length - 3);
|
|
|
|
|
if (last3 == "(M)" || last3 == "(F)")
|
|
|
|
|
{
|
|
|
|
|
Gender = last3.Substring(1, 1);
|
|
|
|
|
line = line.Substring(0, line.Length - 3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Nickname Detection
|
|
|
|
|
string spec = line;
|
|
|
|
|
if (spec.Contains("(") && spec.Contains(")"))
|
2017-06-18 01:37:19 +00:00
|
|
|
|
ParseSpeciesNickname(ref spec);
|
2016-09-20 05:59:15 +00:00
|
|
|
|
|
|
|
|
|
spec = spec.Trim();
|
|
|
|
|
if ((Species = Array.IndexOf(species, spec)) >= 0) // success, nothing else!
|
|
|
|
|
return;
|
|
|
|
|
|
2018-01-31 00:18:39 +00:00
|
|
|
|
// Forme string present.
|
|
|
|
|
int end = spec.LastIndexOf('-');
|
|
|
|
|
if (end < 0)
|
2016-09-20 05:59:15 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2018-01-31 00:18:39 +00:00
|
|
|
|
Species = Array.IndexOf(species, spec.Substring(0, end).Trim());
|
2018-02-04 17:44:19 +00:00
|
|
|
|
Form = spec.Substring(end + 1);
|
2017-12-01 02:24:31 +00:00
|
|
|
|
|
|
|
|
|
if (Species < 0) // failure to parse, check edge cases
|
|
|
|
|
{
|
|
|
|
|
var edge = new[] {784, 250}; // all species with dashes in English Name (Kommo-o & Ho-Oh)
|
|
|
|
|
foreach (var e in edge)
|
|
|
|
|
{
|
|
|
|
|
if (!spec.StartsWith(species[e]))
|
|
|
|
|
continue;
|
|
|
|
|
Species = e;
|
2018-01-31 00:18:39 +00:00
|
|
|
|
Form = spec.Substring(species[e].Length);
|
2017-12-01 02:24:31 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-20 05:59:15 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void ParseSpeciesNickname(ref string line)
|
2016-09-20 05:59:15 +00:00
|
|
|
|
{
|
|
|
|
|
int index = line.LastIndexOf("(", StringComparison.Ordinal);
|
|
|
|
|
string n1, n2;
|
2017-03-20 07:03:31 +00:00
|
|
|
|
if (index > 1) // correct format
|
2016-09-20 05:59:15 +00:00
|
|
|
|
{
|
|
|
|
|
n1 = line.Substring(0, index - 1);
|
|
|
|
|
n2 = line.Substring(index).Trim();
|
2017-10-24 06:12:58 +00:00
|
|
|
|
n2 = ReplaceAll(n2, string.Empty, "[", "]", "(", ")"); // Trim out excess data
|
2016-09-20 05:59:15 +00:00
|
|
|
|
}
|
|
|
|
|
else // nickname first (manually created set, incorrect)
|
|
|
|
|
{
|
|
|
|
|
int end = line.IndexOf(")", StringComparison.Ordinal);
|
|
|
|
|
n2 = line.Substring(index + 1, end - 1);
|
|
|
|
|
n1 = line.Substring(end + 2);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-31 00:18:39 +00:00
|
|
|
|
int dash = n2.LastIndexOf('-');
|
|
|
|
|
if (dash < 0) dash = n2.Length;
|
|
|
|
|
bool inverted = Array.IndexOf(species, n2.Replace(" ", string.Empty)) > -1
|
|
|
|
|
|| (Species = Array.IndexOf(species, n2.Substring(0, dash))) > 0;
|
2016-09-20 05:59:15 +00:00
|
|
|
|
line = inverted ? n2 : n1;
|
|
|
|
|
Nickname = inverted ? n1 : n2;
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private string ParseLineMove(string line)
|
2016-09-20 05:59:15 +00:00
|
|
|
|
{
|
|
|
|
|
string moveString = line.Substring(line[1] == ' ' ? 2 : 1);
|
|
|
|
|
if (!moveString.Contains("Hidden Power"))
|
|
|
|
|
return moveString;
|
|
|
|
|
|
|
|
|
|
// Defined Hidden Power
|
|
|
|
|
if (moveString.Length > 13)
|
|
|
|
|
{
|
|
|
|
|
string type = moveString.Remove(0, 13);
|
2017-10-24 06:12:58 +00:00
|
|
|
|
type = ReplaceAll(type, string.Empty, "[", "]", "(", ")"); // Trim out excess data
|
2016-09-20 05:59:15 +00:00
|
|
|
|
int hpVal = Array.IndexOf(hptypes, type); // Get HP Type
|
2018-01-28 04:18:38 +00:00
|
|
|
|
|
|
|
|
|
if (IVs.Any(z => z != 31))
|
2018-02-08 02:28:56 +00:00
|
|
|
|
{
|
|
|
|
|
if (!SetIVsForHiddenPower(hpVal, IVs))
|
|
|
|
|
InvalidLines.Add($"Invalid IVs for Hidden Power Type: {type}");
|
|
|
|
|
}
|
2018-01-28 04:18:38 +00:00
|
|
|
|
else if (hpVal >= 0)
|
2017-06-18 01:37:19 +00:00
|
|
|
|
IVs = PKX.SetHPIVs(hpVal, IVs); // Get IVs
|
2016-09-20 05:59:15 +00:00
|
|
|
|
else
|
|
|
|
|
InvalidLines.Add($"Invalid Hidden Power Type: {type}");
|
|
|
|
|
}
|
|
|
|
|
moveString = "Hidden Power";
|
|
|
|
|
return moveString;
|
|
|
|
|
}
|
2018-02-08 02:28:56 +00:00
|
|
|
|
private static int GetHiddenPowerType(IReadOnlyList<int> IVs)
|
|
|
|
|
{
|
|
|
|
|
int hp = 0;
|
|
|
|
|
for (int i = 0; i < 6; i++)
|
|
|
|
|
hp |= (IVs[i] & 1) << i;
|
|
|
|
|
hp *= 0xF;
|
|
|
|
|
hp /= 0x3F;
|
|
|
|
|
return hp;
|
|
|
|
|
}
|
|
|
|
|
private static bool SetIVsForHiddenPower(int hpVal, int[] IVs)
|
|
|
|
|
{
|
|
|
|
|
if (IVs.All(z => z == 31))
|
|
|
|
|
{
|
|
|
|
|
PKX.SetHPIVs(hpVal, IVs); // Get IVs
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int current = GetHiddenPowerType(IVs);
|
|
|
|
|
if (current == hpVal)
|
|
|
|
|
return true; // no mods necessary
|
|
|
|
|
|
|
|
|
|
// Required HP type doesn't match IVs. Make currently-flawless IVs flawed.
|
2018-02-10 05:27:57 +00:00
|
|
|
|
int[] best = GetSuggestedHiddenPowerIVs(hpVal, IVs);
|
|
|
|
|
if (best == null)
|
|
|
|
|
return false; // can't force hidden power?
|
|
|
|
|
|
|
|
|
|
// set IVs back to array
|
|
|
|
|
for (int i = 0; i < IVs.Length; i++)
|
|
|
|
|
IVs[i] = best[i];
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
private static int[] GetSuggestedHiddenPowerIVs(int hpVal, int[] IVs)
|
|
|
|
|
{
|
2018-02-08 02:28:56 +00:00
|
|
|
|
var flawless = IVs.Select((v, i) => v == 31 ? i : -1).Where(v => v != -1).ToArray();
|
|
|
|
|
var permutations = GetPermutations(flawless, flawless.Length);
|
2018-02-10 05:27:57 +00:00
|
|
|
|
int flawedCount = 0;
|
|
|
|
|
int[] best = null;
|
2018-02-08 02:28:56 +00:00
|
|
|
|
foreach (var permute in permutations)
|
|
|
|
|
{
|
2018-02-10 05:27:57 +00:00
|
|
|
|
var ivs = (int[])IVs.Clone();
|
|
|
|
|
foreach (var item in permute)
|
2018-02-08 02:28:56 +00:00
|
|
|
|
{
|
2018-02-10 05:27:57 +00:00
|
|
|
|
ivs[item] ^= 1;
|
|
|
|
|
if (hpVal != GetHiddenPowerType(ivs))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
int ct = ivs.Count(z => z == 31);
|
|
|
|
|
if (ct <= flawedCount)
|
|
|
|
|
break; // any further flaws are always worse
|
2018-02-08 02:28:56 +00:00
|
|
|
|
|
2018-02-10 05:27:57 +00:00
|
|
|
|
flawedCount = ct;
|
|
|
|
|
best = ivs;
|
|
|
|
|
break; // any further flaws are always worse
|
|
|
|
|
}
|
2018-02-08 02:28:56 +00:00
|
|
|
|
}
|
2018-02-10 05:27:57 +00:00
|
|
|
|
return best;
|
2018-02-08 02:28:56 +00:00
|
|
|
|
}
|
|
|
|
|
private static IEnumerable<IEnumerable<T>> GetPermutations<T>(IList<T> list, int length)
|
|
|
|
|
{
|
|
|
|
|
// https://stackoverflow.com/a/10630026
|
|
|
|
|
if (length == 1)
|
|
|
|
|
return list.Select(t => new[] { t });
|
|
|
|
|
|
|
|
|
|
return GetPermutations(list, length - 1)
|
|
|
|
|
.SelectMany(t => list.Where(e => !t.Contains(e)),
|
|
|
|
|
(t1, t2) => t1.Concat(new[] { t2 }));
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void ParseLineEVs(string line)
|
2016-09-20 05:59:15 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
string[] evlist = SplitLineStats(line);
|
2017-01-29 08:32:02 +00:00
|
|
|
|
if (evlist.Length == 1)
|
|
|
|
|
InvalidLines.Add("Unknown EV input.");
|
2016-09-20 05:59:15 +00:00
|
|
|
|
for (int i = 0; i < evlist.Length / 2; i++)
|
|
|
|
|
{
|
2017-06-30 02:32:29 +00:00
|
|
|
|
bool valid = ushort.TryParse(evlist[i * 2 + 0], out ushort EV);
|
2016-09-20 05:59:15 +00:00
|
|
|
|
int index = Array.IndexOf(StatNames, evlist[i * 2 + 1]);
|
2017-06-18 20:02:02 +00:00
|
|
|
|
if (valid && index > -1)
|
2016-09-20 05:59:15 +00:00
|
|
|
|
EVs[index] = EV;
|
|
|
|
|
else
|
|
|
|
|
InvalidLines.Add($"Unknown EV Type input: {evlist[i * 2]}");
|
|
|
|
|
}
|
2018-02-08 02:28:56 +00:00
|
|
|
|
EVs = EVsSpeedFirst;
|
2016-09-20 05:59:15 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void ParseLineIVs(string line)
|
2016-09-20 05:59:15 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
string[] ivlist = SplitLineStats(line);
|
2017-01-29 08:32:02 +00:00
|
|
|
|
if (ivlist.Length == 1)
|
|
|
|
|
InvalidLines.Add("Unknown IV input.");
|
2016-09-20 05:59:15 +00:00
|
|
|
|
for (int i = 0; i < ivlist.Length / 2; i++)
|
|
|
|
|
{
|
2017-06-18 20:02:02 +00:00
|
|
|
|
bool valid = byte.TryParse(ivlist[i * 2 + 0], out byte IV);
|
2016-09-20 05:59:15 +00:00
|
|
|
|
int index = Array.IndexOf(StatNames, ivlist[i * 2 + 1]);
|
2017-06-18 20:02:02 +00:00
|
|
|
|
if (valid && index > -1)
|
2016-09-20 05:59:15 +00:00
|
|
|
|
IVs[index] = IV;
|
|
|
|
|
else
|
|
|
|
|
InvalidLines.Add($"Unknown IV Type input: {ivlist[i * 2]}");
|
|
|
|
|
}
|
2018-02-08 02:28:56 +00:00
|
|
|
|
IVs = IVsSpeedFirst;
|
2016-09-20 05:59:15 +00:00
|
|
|
|
}
|
2017-09-02 15:26:51 +00:00
|
|
|
|
private static string ConvertFormToShowdown(string form, int spec)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(form))
|
|
|
|
|
{
|
|
|
|
|
if (spec == 774) // Minior
|
|
|
|
|
form = "Meteor";
|
|
|
|
|
return form;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (spec)
|
|
|
|
|
{
|
|
|
|
|
case 550 when form == "Blue":
|
|
|
|
|
return "Blue Striped";
|
|
|
|
|
case 666 when form == "Poké Ball":
|
|
|
|
|
return "Pokeball"; // Vivillon
|
|
|
|
|
case 718: // Zygarde
|
2017-11-18 03:14:27 +00:00
|
|
|
|
form = form.Replace("-C", string.Empty);
|
|
|
|
|
form = form.Replace("50%", string.Empty);
|
2017-09-02 15:26:51 +00:00
|
|
|
|
return form.Replace("100%", "Complete");
|
|
|
|
|
case 774: // Minior
|
|
|
|
|
if (form.StartsWith("M-"))
|
|
|
|
|
return "Meteor";
|
2017-11-18 03:14:27 +00:00
|
|
|
|
return form.Replace("C-", string.Empty);
|
|
|
|
|
case 800 when form == "Dusk": // Necrozma
|
|
|
|
|
return $"{form}-Mane";
|
|
|
|
|
case 800 when form == "Dawn": // Necrozma
|
|
|
|
|
return $"{form}-Wings";
|
2017-09-02 15:26:51 +00:00
|
|
|
|
|
2017-11-18 03:14:27 +00:00
|
|
|
|
case 676: // Furfrou
|
|
|
|
|
case 658: // Greninja
|
|
|
|
|
case 744: // Rockruff
|
|
|
|
|
return string.Empty;
|
2017-09-02 15:26:51 +00:00
|
|
|
|
default:
|
2017-11-18 03:14:27 +00:00
|
|
|
|
if (Legal.Totem_USUM.Contains(spec) && form == "Large")
|
2017-11-18 04:38:43 +00:00
|
|
|
|
return Legal.Totem_Alolan.Contains(spec) ? "Alola-Totem" : "Totem";
|
2017-09-02 15:26:51 +00:00
|
|
|
|
return form;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private static string ConvertFormFromShowdown(string form, int spec, int ability)
|
|
|
|
|
{
|
|
|
|
|
switch (spec)
|
|
|
|
|
{
|
|
|
|
|
case 550 when form == "Blue Striped": // Basculin
|
|
|
|
|
return "Blue";
|
|
|
|
|
case 658 when ability == 210: // Greninja
|
|
|
|
|
return "Ash"; // Battle Bond
|
|
|
|
|
case 666 when form == "Pokeball": // Vivillon
|
|
|
|
|
return "Poké Ball";
|
|
|
|
|
|
|
|
|
|
// Zygarde
|
2017-10-18 18:45:45 +00:00
|
|
|
|
case 718 when string.IsNullOrWhiteSpace(form) && ability == 211:
|
|
|
|
|
return "50%-C";
|
2017-09-02 15:26:51 +00:00
|
|
|
|
case 718 when string.IsNullOrWhiteSpace(form):
|
|
|
|
|
return "50%";
|
|
|
|
|
case 718 when form == "Complete":
|
|
|
|
|
return "100%";
|
|
|
|
|
case 718 when ability == 211:
|
|
|
|
|
return "-C"; // Power Construct
|
|
|
|
|
|
2018-01-06 17:40:30 +00:00
|
|
|
|
case 741 when form == "Pom Pom":
|
|
|
|
|
return "Pom-Pom";
|
2017-11-18 03:14:27 +00:00
|
|
|
|
case 744 when ability == 020: // Rockruff-1
|
|
|
|
|
return "Dusk";
|
|
|
|
|
|
2017-09-02 15:26:51 +00:00
|
|
|
|
// Minior
|
|
|
|
|
case 774 when !string.IsNullOrWhiteSpace(form) && form != "Meteor":
|
2017-09-30 05:58:25 +00:00
|
|
|
|
return $"C-{form}";
|
2017-09-02 15:26:51 +00:00
|
|
|
|
|
2017-11-18 03:14:27 +00:00
|
|
|
|
// Necrozma
|
|
|
|
|
case 800 when form == "Dusk Mane":
|
|
|
|
|
return "Dusk";
|
|
|
|
|
case 800 when form == "Dawn Wings":
|
|
|
|
|
return "Dawn";
|
|
|
|
|
|
2017-09-02 15:26:51 +00:00
|
|
|
|
default:
|
2017-11-19 22:16:28 +00:00
|
|
|
|
if (form == null)
|
|
|
|
|
return form;
|
2017-11-18 04:38:43 +00:00
|
|
|
|
if (Legal.Totem_USUM.Contains(spec) && form.EndsWith("Totem"))
|
|
|
|
|
return "Large";
|
2017-09-02 15:26:51 +00:00
|
|
|
|
return form;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-20 05:59:15 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private static string[] SplitLineStats(string line)
|
2016-09-20 05:59:15 +00:00
|
|
|
|
{
|
|
|
|
|
// Because people think they can type sets out...
|
|
|
|
|
return line
|
|
|
|
|
.Replace("SAtk", "SpA").Replace("Sp Atk", "SpA")
|
|
|
|
|
.Replace("SDef", "SpD").Replace("Sp Def", "SpD")
|
|
|
|
|
.Replace("Spd", "Spe").Replace("Speed", "Spe").Split(new[] { " / ", " " }, StringSplitOptions.None);
|
|
|
|
|
}
|
2017-10-24 06:12:58 +00:00
|
|
|
|
private static string ReplaceAll(string original, string to, params string[] toBeReplaced)
|
2016-09-20 05:59:15 +00:00
|
|
|
|
{
|
2017-10-24 06:12:58 +00:00
|
|
|
|
return toBeReplaced.Aggregate(original, (current, v) => current.Replace(v, to));
|
2016-09-20 05:59:15 +00:00
|
|
|
|
}
|
2017-12-27 00:29:35 +00:00
|
|
|
|
|
|
|
|
|
public void ApplyToPKM(PKM pkm)
|
|
|
|
|
{
|
|
|
|
|
if (Species < 0)
|
|
|
|
|
return;
|
|
|
|
|
pkm.Species = Species;
|
|
|
|
|
|
|
|
|
|
if (Nickname != null && Nickname.Length <= pkm.NickLength)
|
|
|
|
|
pkm.Nickname = Nickname;
|
|
|
|
|
else
|
|
|
|
|
pkm.Nickname = PKX.GetSpeciesName(pkm.Species, pkm.Language);
|
|
|
|
|
|
|
|
|
|
int gender = PKX.GetGenderFromString(Gender);
|
|
|
|
|
pkm.Gender = Math.Max(0, gender);
|
|
|
|
|
|
|
|
|
|
var list = PKX.GetFormList(pkm.Species, types, forms, genders);
|
|
|
|
|
int formnum = Array.IndexOf(list, Form);
|
|
|
|
|
pkm.AltForm = Math.Max(0, formnum);
|
|
|
|
|
|
|
|
|
|
var abils = pkm.PersonalInfo.Abilities;
|
|
|
|
|
int index = Array.IndexOf(abils, Ability);
|
|
|
|
|
pkm.RefreshAbility(Math.Max(0, Math.Min(2, index)));
|
|
|
|
|
|
|
|
|
|
if (Shiny && !pkm.IsShiny)
|
|
|
|
|
pkm.SetShinyPID();
|
|
|
|
|
else if (!Shiny && pkm.IsShiny)
|
|
|
|
|
pkm.PID = Util.Rand32();
|
|
|
|
|
|
|
|
|
|
pkm.CurrentLevel = Level;
|
|
|
|
|
pkm.HeldItem = Math.Max(0, HeldItem);
|
|
|
|
|
pkm.CurrentFriendship = Friendship;
|
|
|
|
|
pkm.Nature = Nature;
|
|
|
|
|
pkm.EVs = EVs;
|
|
|
|
|
pkm.IVs = IVs;
|
|
|
|
|
pkm.Moves = Moves;
|
|
|
|
|
}
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|