Skip to content

Commit

Permalink
Fix plando issues and add error messages for users
Browse files Browse the repository at this point in the history
  • Loading branch information
bartz24games@gmail.com committed Dec 26, 2020
1 parent ac65cda commit 48e42e8
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 39 deletions.
46 changes: 45 additions & 1 deletion Data/Ability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,58 @@ public bool HasCharacter(string character)
return false;
}

public string GetCharacters()
{
return string.Join("", list.SelectMany(a => a.Characters.ToCharArray()).Distinct().OrderBy(c => "lsvzhf".ToCharArray().ToList().IndexOf(c)).Select(c =>
{
switch (c)
{
case 'l':
return "L";
case 's':
return "Sn";
case 'v':
return "V";
case 'z':
return "Sz";
case 'h':
return "H";
case 'f':
return "F";
default:
return "";
}
}));
}

public string GetAbility(string character)
{
for (int i = list.Count - 1; i >= 0; i--)
{
if (list[i].HasCharacter(character))
return list[i].ID;
}
throw new InvalidCastException("Character does not have the ability!");
throw new Exception($"{GetName(character)} cannot have the {Name} ability!");
}

private Character GetName(string character)
{
switch (character)
{
case "l":
return Character.Lightning;
case "s":
return Character.Snow;
case "v":
return Character.Vanille;
case "z":
return Character.Sazh;
case "h":
return Character.Hope;
case "f":
return Character.Fang;
}
return Character.Lightning;
}

public Ability SetStarting()
Expand Down
41 changes: 27 additions & 14 deletions FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace FF13Randomizer
{
public partial class FormMain : Form
{
public static string Version { get; set; } = "1.8.0.Pre";
public static string Version { get; set; } = "1.8.0.Pre-2";

public static bool PlandoModified { get; set; } = false;

Expand Down Expand Up @@ -714,6 +714,7 @@ private void button9_Click(object sender, EventArgs e)
{
flag.ResetRandom(seed);
}
RandomNum.ClearRand();

foreach(string path in fileNamesModified)
{
Expand All @@ -732,24 +733,34 @@ private void button9_Click(object sender, EventArgs e)
randomizers.Add(new RandoMusic(this, randomizers));
randomizers.Add(new RandoRunSpeed(this, randomizers));

new ProgressForm("Loading data...", bw => LoadRandos(randomizers, bw)).ShowDialog();

foreach(Randomizer rando in randomizers)
try
{
new ProgressForm(rando.GetProgressMessage(), bw => rando.Randomize(bw)).ShowDialog();
}
new ProgressForm("Loading data...", bw => LoadRandos(randomizers, bw)).ShowDialog();
foreach (Randomizer rando in randomizers)
{
new ProgressForm(rando.GetProgressMessage(), bw => rando.Randomize(bw)).ShowDialog();
}
new ProgressForm("Saving data...", bw => SaveRandos(randomizers, bw)).ShowDialog();

new ProgressForm("Saving data...", bw => SaveRandos(randomizers, bw)).ShowDialog();

new ProgressForm("Inserting files...", bw => insertFiles(bw, true)).ShowDialog();

new ProgressForm("Inserting files...", bw => insertFiles(bw, true)).ShowDialog();
UserFlagsSeed.Export(RandoPath, textBoxSeed.Text.Trim(), Version);
addHistory();

UserFlagsSeed.Export(RandoPath, textBoxSeed.Text.Trim(), Version);
addHistory();

//UserFlagsSeed.Export("logs", textBoxSeed.Text.Trim(), version);
//UserFlagsSeed.Export("logs", textBoxSeed.Text.Trim(), version);

MessageBox.Show("Complete! Ready to play! Whenever you need to uninstall the rando, come back to this program and go to the Uninstall tab!");
MessageBox.Show("Complete! Ready to play! Whenever you need to uninstall the rando, come back to this program and go to the Uninstall tab!");
} catch(Exception ex)
{
Exception innerMost = ex;
while(innerMost.InnerException != null)
{
innerMost = innerMost.InnerException;
}
MessageBox.Show("Randomizer encountered an error:\n" + innerMost.Message, "Rando failed");
}
}

public void LoadRandos(List<Randomizer> randomizers, BackgroundWorker worker)
Expand Down Expand Up @@ -1102,8 +1113,9 @@ private void button15_Click(object sender, EventArgs e)
{
PlandoFile.Write(dialog.FileName, this);
Process.Start(Path.GetDirectoryName(dialog.FileName));
MessageBox.Show("Plando exported! Opened location where file has been saved.");
PlandoModified = false;
}
PlandoModified = false;
}

private void button14_Click(object sender, EventArgs e)
Expand All @@ -1125,8 +1137,9 @@ private void button14_Click(object sender, EventArgs e)
if (dialog.ShowDialog() == DialogResult.OK)
{
PlandoFile.Read(dialog.FileName, this);
MessageBox.Show("Plando imported!");
PlandoModified = false;
}
PlandoModified = false;
}

private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
Expand Down
18 changes: 15 additions & 3 deletions Plandos/CrystariumPlando.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private void AddEntries()
dataGridView1.Columns.Add(column2);

DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn();
column.Items.AddRange(new string[] { "???" }.Concat(Abilities.abilities.Select(a => a.Name).ToList()).ToArray());
column.Items.AddRange(GetAbilityDropdownNames());
column.HeaderText = "New Ability";
column.DisplayMember = "New Ability";
column.ValueMember = "New Ability";
Expand All @@ -87,6 +87,11 @@ private void AddEntries()
dataGridView1.DataSource = dataTable;
}

private static string[] GetAbilityDropdownNames()
{
return new string[] { "???" }.Concat(Abilities.abilities.Select(a => a.Name + (a.GetCharacters() != "LSnVSzHF" ? $" ({a.GetCharacters()})" : "")).ToList()).ToArray();
}

private void AddEntry(DataStoreIDCrystarium crystId, string charName, string role, string locationName)
{
dataTable.Rows.Add(crystId.ID, charName, role, crystId.Stage, locationName, "Unknown", "???", "???", 0);
Expand Down Expand Up @@ -188,7 +193,7 @@ private void dataGridView1_CellValidating(object sender, DataGridViewCellValidat
string nodeId = row.Field<string>(0);
string name = row.Field<string>(1);
CrystariumType type = row.Field<string>(6) == "???" ? CrystariumType.Unknown : ((CrystariumType[])Enum.GetValues(typeof(CrystariumType)))[Enum.GetNames(typeof(CrystariumType)).ToList().IndexOf(row.Field<string>(6).Replace(" ", ""))];
Ability ability = Abilities.abilities.Find(a => a.Name == row.Field<string>(7));
Ability ability = Abilities.abilities.Find(a => a.Name == GetAbilityName(row.Field<string>(7)));
int statAmount = row.Field<int>(8);
if (!(type == CrystariumType.Unknown && ability == null))
{
Expand All @@ -209,6 +214,11 @@ private void dataGridView1_CellValidating(object sender, DataGridViewCellValidat
return dict;
}

private string GetAbilityName(string name)
{
return name.Contains("(") ? name.Substring(0, name.IndexOf("(") - 1).Trim() : name;
}

public class JSONPlandoCrystarium
{
public string ID { get; set; }
Expand Down Expand Up @@ -240,7 +250,9 @@ public void LoadJSONPlando(List<JSONPlandoCrystarium> list)
{
JSONPlandoCrystarium json = list.Find(j => j.ID == row.Field<string>(0));
row.SetField<string>(6, json.Type);
row.SetField<string>(7, json.AbilityName);

row.SetField<string>(7, GetAbilityDropdownNames().Where(s => s == json.AbilityName || s.StartsWith(json.AbilityName)).FirstOrDefault());

row.SetField<int>(8, json.Value);
}
}
Expand Down
9 changes: 5 additions & 4 deletions Plandos/ShopPlando.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,20 @@ private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
}
}

public Dictionary<Shop, List<Item>> GetShops()
public Dictionary<Tuple<Shop, int>, List<Item>> GetShops()
{
Dictionary<Shop, List<Item>> dict = new Dictionary<Shop, List<Item>>();
Shops.shops.ForEach(s => dict.Add(s, new List<Item>()));
Dictionary<Tuple<Shop, int>, List<Item>> dict = new Dictionary<Tuple<Shop, int>, List<Item>>();
Shops.shops.ForEach(s => Enumerable.Range(0, s.Tiers).ForEach(tier => dict.Add(new Tuple<Shop, int>(s, tier), new List<Item>())));
foreach (DataRow row in dataTable.Rows)
{
Shop shop = Shops.shops.Find(s => s.GetAllIds().Contains(row.Field<string>(0)));
int tier = shop.GetAllIds().IndexOf(row.Field<string>(0));
Item item = Items.items.Find(i => i.Name == row.Field<string>(3) || i.ID == row.Field<string>(3));
if (row.Field<string>(3) == "None")
item = Items.Gil;
if (item != null)
{
dict[shop].Add(item);
dict[new Tuple<Shop, int>(shop, tier)].Add(item);
}
}
return dict;
Expand Down
2 changes: 2 additions & 0 deletions ProgressForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEve

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
throw new Exception("Randomizer Error", e.Error);
this.Close();
}
}
Expand Down
67 changes: 63 additions & 4 deletions Randomizers/RandoCrystarium.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public override void Randomize(BackgroundWorker backgroundWorker)
backgroundWorker.ReportProgress(10);
if (Flags.CrystariumFlags.RandStats)
RandomizeStats(plando, statAverages);

backgroundWorker.ReportProgress(20);

if (Tweaks.Boosts.ScaledCPCost)
Expand All @@ -74,6 +75,17 @@ public override void Randomize(BackgroundWorker backgroundWorker)
backgroundWorker.ReportProgress(40);

RandomizeAbilities(plando);

foreach (string name in CharNames)
{
foreach (DataStoreIDCrystarium id in crystariums[name].IdList.Where(id => !id.ID.StartsWith("!")))
{
DataStoreCrystarium c = crystariums[name][id.ID];
if (c.Type == CrystariumType.Unknown)
throw new Exception($"{name.Substring(0, 1).ToUpper() + name.Substring(1)} {c.Role} Stage {c.Stage} Node {Crystarium.GetDisplayNames(crystariums[name])[id]} is invalid.");
}
}

backgroundWorker.ReportProgress(100);
}

Expand Down Expand Up @@ -110,7 +122,7 @@ public void ApplyHalfCPCosts()
Dictionary<Role, List<Ability>> abilities = new Dictionary<Role, List<Ability>>();
foreach (Role role in Enum.GetValues(typeof(Role)))
{
abilities.Add(role, crystariums[name].DataList.Where(c => c.Role == role && c.Type == CrystariumType.Ability).Select(c => Abilities.GetAbility(name, c)).ToList());
abilities.Add(role, crystariums[name].DataList.Where(c => c.Role == role && c.Type == CrystariumType.Ability && c.CPCost > 0 && (c.Stage > 1 || primaryRoles[name].Contains(c.Role))).Select(c => Abilities.GetAbility(name, c)).ToList());
}
dict.Add(name, abilities);
}
Expand Down Expand Up @@ -189,6 +201,30 @@ public void ApplyHalfCPCosts()
return statAverages;
}

public void SetRemaining(List<DataStoreCrystarium> list)
{
foreach (string name in CharNames)
{
DataStoreWDB<DataStoreCrystarium, DataStoreIDCrystarium> crystarium = crystariums[name];
foreach (DataStoreCrystarium c in list)
{
if (!(c.Type == CrystariumType.HP || c.Type == CrystariumType.Strength || c.Type == CrystariumType.Magic))
{
int stage = c.Stage;
List<DataStoreCrystarium> others;
do
{
others = new CrystariumType[] { CrystariumType.HP, CrystariumType.Strength, CrystariumType.Magic }.SelectMany(t => crystarium.DataList.Where(o => o.Role == c.Role && o.Stage == c.Stage && o.Type == t)).ToList();
stage--;
} while (others.Count == 0);
DataStoreCrystarium other = others[RandomNum.RandInt(0, others.Count - 1)];
c.Type = other.Type;
c.Value = other.Value;
}
}
}
}

public void RandomizeStats(Dictionary<string, Dictionary<string, Tuple<CrystariumType, Ability, int>>> plando, Dictionary<int, Dictionary<CrystariumType, List<int>>> statAverages)
{
Flags.CrystariumFlags.RandStats.SetRand();
Expand Down Expand Up @@ -288,7 +324,7 @@ public void PlaceNodes(Dictionary<string, Dictionary<string, Tuple<CrystariumTyp

foreach (string name in CharNames)
{
crystariums[name].DataList.Where(c => c.CPCost > 0 && (c.Stage > 1 || primaryRoles[name].Contains(c.Role))).ForEach(c => c.Type = CrystariumType.Unknown);
List<DataStoreCrystarium> unknowns = crystariums[name].DataList.Where(c => c.CPCost > 0 && (c.Stage > 1 || primaryRoles[name].Contains(c.Role))).ToList();

foreach (string id in plando[name].Keys)
{
Expand All @@ -310,6 +346,7 @@ public void PlaceNodes(Dictionary<string, Dictionary<string, Tuple<CrystariumTyp
cryst.Type = plando[name][id].Item1;
cryst.Value = (ushort)plando[name][id].Item3;
}
unknowns.Remove(cryst);
}

List<DataStoreCrystarium> possible;
Expand All @@ -320,12 +357,13 @@ public void PlaceNodes(Dictionary<string, Dictionary<string, Tuple<CrystariumTyp
id => !id.ID.StartsWith("!") && !plando[name].Keys.Contains(id.ID)).Select(id => crystariums[name][id.ID]).Where(
c => c.Role == role &&
c.CPCost > 0 && (c.Stage > 1 || primaryRoles[name].Contains(role))
&& c.Type == CrystariumType.Unknown).ToList();
&& unknowns.Contains(c)).ToList();
for (int i = 0; i < nodes[name][role].Count; i++)
{
int select = RandomNum.RandInt(0, possible.Count - 1);
possible[select].Type = CrystariumType.Ability;
possible[select].AbilityName = nodes[name][role][i].GetAbility(Abilities.GetCharID(name));
unknowns.Remove(possible[select]);
possible.RemoveAt(select);
}

Expand All @@ -334,19 +372,21 @@ public void PlaceNodes(Dictionary<string, Dictionary<string, Tuple<CrystariumTyp
{
int select = RandomNum.RandInt(0, possible.Count - 1);
possible[select].Type = CrystariumType.RoleLevel;
unknowns.Remove(possible[select]);
possible.RemoveAt(select);
}
}

possible = crystariums[name].IdList.Where(
id => !id.ID.StartsWith("!") && !plando[name].Keys.Contains(id.ID)).Select(id => crystariums[name][id.ID]).Where(
c => c.CPCost > 0 && (c.Stage > 1 || primaryRoles[name].Contains(c.Role)) && c.Type == CrystariumType.Unknown).ToList();
c => c.CPCost > 0 && (c.Stage > 1 || primaryRoles[name].Contains(c.Role)) && unknowns.Contains(c)).ToList();

int atbLevelCount = crystariums[name].DataList.Where(c => c.Type == CrystariumType.ATBLevel).Count();
if (atbLevelCount == 0)
{
int select = RandomNum.RandInt(0, possible.Count - 1);
possible[select].Type = CrystariumType.ATBLevel;
unknowns.Remove(possible[select]);
possible.RemoveAt(select);
}

Expand All @@ -355,8 +395,27 @@ public void PlaceNodes(Dictionary<string, Dictionary<string, Tuple<CrystariumTyp
{
int select = RandomNum.RandInt(0, possible.Count - 1);
possible[select].Type = CrystariumType.Accessory;
unknowns.Remove(possible[select]);
possible.RemoveAt(select);
}

SetRemaining(unknowns);
}

foreach (string name in CharNames)
{
for (Role role = (Role)1; role <= (Role)6; role++)
{
for (int stage = 1; stage <= 10; stage++)
{
List<DataStoreCrystarium> shuffle = crystariums[name].IdList.Where(
id => !id.ID.StartsWith("!") && !plando[name].Keys.Contains(id.ID)).Select(id => crystariums[name][id.ID]).Where(
c => c.Role == role &&
c.Stage == stage &&
c.CPCost > 0 && (c.Stage > 1 || primaryRoles[name].Contains(role))).ToList();
shuffle.Shuffle((a, b) => a.SwapStatsAbilities(b));
}
}
}

RandomNum.ClearRand();
Expand Down
Loading

0 comments on commit 48e42e8

Please sign in to comment.