Skip to content

Commit

Permalink
if a creature already exists in the library by guid, assume the wild …
Browse files Browse the repository at this point in the history
…levels are correct for easier updating it.
  • Loading branch information
cadaei committed Dec 24, 2019
1 parent d3f5572 commit 9bb67c7
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 107 deletions.
10 changes: 5 additions & 5 deletions ARKBreedingStats/CreatureInfoInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public partial class CreatureInfoInput : UserControl
public event Save2LibraryClickedEventHandler Save2Library_Clicked;
public delegate void RequestParentListEventHandler(CreatureInfoInput sender);
public event RequestParentListEventHandler ParentListRequested;
public delegate void RequestCreatureDataEventHandler(CreatureInfoInput sender, bool patternEditor);
public delegate void RequestCreatureDataEventHandler(CreatureInfoInput sender, bool patternEditor, bool showDuplicateNameWarning);
public event RequestCreatureDataEventHandler CreatureDataRequested;
public bool extractor;
private Sex sex;
Expand Down Expand Up @@ -426,25 +426,25 @@ private void btnGenerateUniqueName_Click(object sender, EventArgs e)
{
if (selectedSpecies != null)
{
CreatureDataRequested?.Invoke(this, false);
CreatureDataRequested?.Invoke(this, false, true);
}
}

private void btnGenerateUniqueName_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
CreatureDataRequested?.Invoke(this, true); // TODO doesn't get called when right-clicking. Why?
CreatureDataRequested?.Invoke(this, true, true);
}
}

/// <summary>
/// Generates a creature name with a given pattern
/// </summary>
public void generateCreatureName(Creature creature)
public void generateCreatureName(Creature creature, bool showDuplicateNameWarning)
{
setCreatureData(creature);
CreatureName = uiControls.NamePatterns.generateCreatureName(creature, _females, _males);
CreatureName = uiControls.NamePatterns.generateCreatureName(creature, _females, _males, showDuplicateNameWarning);
}

public void openNamePatternEditor(Creature creature)
Expand Down
1 change: 1 addition & 0 deletions ARKBreedingStats/Extraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public void ExtractLevels(Species species, int level, List<StatIO> statIOs, doub
// Pteranodon (Lvl 34, TE: 80%): HP: 415.9 (6, 0); St: 195 (6, 0); Ox: 240 (6, 0); Fo: 2150.4 (6, 0); We: 134.4 (6, 0); Dm: 141.6% (3, 0); Sp: 135% (0, 0); To: 358.1 (33);
// will fail the extraction with a lowerTEBound of 0.8, it only extracts with a lowerTEBound of 0.79, then displays 0.8 as result for the TE. Adding these margins make it work as expected.
lowerTEBound -= 0.0006;
if (lowerTEBound < 0) lowerTEBound = 0;
upperTEBound += 0.0006;
}

Expand Down
4 changes: 2 additions & 2 deletions ARKBreedingStats/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2787,7 +2787,7 @@ private void UpdateTempCreatureDropDown()
/// </summary>
/// <param name="sender"></param>
/// <param name="openPatternEditor"></param>
private void CreatureInfoInput_CreatureDataRequested(CreatureInfoInput sender, bool openPatternEditor)
private void CreatureInfoInput_CreatureDataRequested(CreatureInfoInput sender, bool openPatternEditor, bool showDuplicateNameWarning)
{
Creature cr = new Creature
{
Expand Down Expand Up @@ -2819,7 +2819,7 @@ private void CreatureInfoInput_CreatureDataRequested(CreatureInfoInput sender, b
if (openPatternEditor)
sender.openNamePatternEditor(cr);
else
sender.generateCreatureName(cr);
sender.generateCreatureName(cr, showDuplicateNameWarning);
}

private void ExtractionTestControl1_CopyToTester(string speciesBP, int[] wildLevels, int[] domLevels, bool postTamed, bool bred, double te, double imprintingBonus, bool gotoTester, testCases.TestCaseControl tcc)
Expand Down
97 changes: 60 additions & 37 deletions ARKBreedingStats/Form1.extractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ private void ClearAll(bool clearExtraCreatureData = true)
exportedCreatureControl = null;
creatureInfoInputExtractor.SetArkId(0, false);
}
DisplayIfCreatureAlreadyInLibrary();

creatureInfoInputExtractor.UpdateExistingCreature = false;
}

private void buttonExtract_Click(object sender, EventArgs e)
Expand All @@ -181,7 +182,7 @@ private void buttonExtract_Click(object sender, EventArgs e)
/// <param name="autoExtraction"></param>
/// <param name="statInputsHighPrecision">Set to true if the data is from an export file which has a higher precision for stat-values so the tolerance of calculations can be smaller.</param>
/// <returns></returns>
private bool ExtractLevels(bool autoExtraction = false, bool statInputsHighPrecision = false, bool showLevelsInOverlay = false)
private bool ExtractLevels(bool autoExtraction = false, bool statInputsHighPrecision = false, bool showLevelsInOverlay = false, Creature existingCreature = null)
{
SuspendLayout();
int activeStatKeeper = activeStatIndex;
Expand Down Expand Up @@ -245,15 +246,34 @@ private bool ExtractLevels(bool autoExtraction = false, bool statInputsHighPreci
}
else if (extractor.results[s].Count > 0)
{
// choose the most probable wild-level, aka the level nearest to the mean of the wild levels.
int r = 0;
for (int b = 1; b < extractor.results[s].Count; b++)
if (existingCreature != null)
{
if (Math.Abs(meanWildLevel - extractor.results[s][b].levelWild) < Math.Abs(meanWildLevel - extractor.results[s][r].levelWild))
r = b;
// set the wild levels to the existing ones
int r = 0;
for (int b = 1; b < extractor.results[s].Count; b++)
{
if (extractor.results[s][b].levelWild == existingCreature.levelsWild[s]
&& extractor.results[s][b].levelDom >= existingCreature.levelsDom[s]
&& extractor.results[s][b].TE.Includes(existingCreature.tamingEff))
{
r = b;
break;
}
}
SetLevelCombination(s, r == -1 ? 0 : r);
}
else
{
// choose the most probable wild-level, aka the level nearest to the mean of the wild levels.
int r = 0;
for (int b = 1; b < extractor.results[s].Count; b++)
{
if (Math.Abs(meanWildLevel - extractor.results[s][b].levelWild) < Math.Abs(meanWildLevel - extractor.results[s][r].levelWild))
r = b;
}

SetLevelCombination(s, r);
SetLevelCombination(s, r);
}
if (extractor.results[s].Count > 1)
{
statIOs[s].Status = StatIOStatus.Nonunique;
Expand Down Expand Up @@ -746,26 +766,19 @@ private bool ExtractExportedFileInExtractor(string exportFile)
return false;
}

SetCreatureValuesToExtractor(cv, false);

// exported stat-files have values for all stats, so activate all stats the species uses
SetStatsActiveAccordingToUsage(cv.Species);

ExtractLevels(autoExtraction: true, statInputsHighPrecision: true);
UpdateParentListInput(creatureInfoInputExtractor); // this function is only used for single-creature extractions, e.g. LastExport
SetCreatureValuesToInfoInput(cv, creatureInfoInputExtractor);
bool creatureExists = ExtractValuesInExtractor(cv, exportFile, true);

if (Properties.Settings.Default.applyNamePatternOnImportIfEmptyName
&& string.IsNullOrEmpty(creatureInfoInputExtractor.CreatureName))
{
CreatureInfoInput_CreatureDataRequested(creatureInfoInputExtractor, false);
CreatureInfoInput_CreatureDataRequested(creatureInfoInputExtractor, false, false);
if (Properties.Settings.Default.copyNameToClipboardOnImportWhenAutoNameApplied)
Clipboard.SetText(creatureInfoInputExtractor.CreatureName);
}

tabControlMain.SelectedTab = tabPageExtractor;
SetMessageLabelText("Creature of the exported file\n" + exportFile);
return DisplayIfCreatureAlreadyInLibrary();
return creatureExists;
}

/// <summary>
Expand All @@ -778,25 +791,30 @@ private void ExtractExportedFileInExtractor(importExported.ExportedCreatureContr
if (ecc == null)
return;

SetCreatureValuesToExtractor(ecc.creatureValues, false);
bool creatureExists = ExtractValuesInExtractor(ecc.creatureValues, exportedCreatureControl.exportedFile, false);

// exported stat-files have values for all stats, so activate all stats the species uses
SetStatsActiveAccordingToUsage(ecc.creatureValues.Species);

ExtractLevels(autoExtraction: false, statInputsHighPrecision: true);
// gets deleted in extractLevels()
exportedCreatureControl = ecc;

// not for batch-extractions, only for single creatures
//if (updateParentVisuals) // TODO parents need to be updated always to be able to select the correct parent. Change GetParent from parentCombobox so that it only returns a guid?
UpdateParentListInput(creatureInfoInputExtractor);

SetCreatureValuesToInfoInput(exportedCreatureControl.creatureValues, creatureInfoInputExtractor);
if (exportedCreatureList != null && exportedCreatureList.ownerSuffix != null)
creatureInfoInputExtractor.CreatureOwner += exportedCreatureList.ownerSuffix;
}

private bool ExtractValuesInExtractor(CreatureValues cv, string filePath, bool autoExtraction)
{
SetCreatureValuesToExtractor(cv, false);

// exported stat-files have values for all stats, so activate all stats the species uses
SetStatsActiveAccordingToUsage(cv.Species);

SetMessageLabelText("Creature of the exported file\n" + exportedCreatureControl.exportedFile);
DisplayIfCreatureAlreadyInLibrary();
bool creatureExists = IsCreatureAlreadyInLibrary(cv.guid, cv.ARKID, out Creature existingCreature);

ExtractLevels(autoExtraction: autoExtraction, statInputsHighPrecision: true, existingCreature: existingCreature);
UpdateParentListInput(creatureInfoInputExtractor); // this function is only used for single-creature extractions, e.g. LastExport
SetCreatureValuesToInfoInput(cv, creatureInfoInputExtractor);
creatureInfoInputExtractor.UpdateExistingCreature = creatureExists;
SetMessageLabelText("Creature of the exported file\n" + filePath);
return creatureExists;
}

/// <summary>
Expand Down Expand Up @@ -870,14 +888,19 @@ private void SetCreatureValuesToExtractor(CreatureValues cv, bool setInfoInput =
/// Gives feedback to the user if the current creature in the extractor is already in the library.
/// This uses the ARK-ID and only works if exported creatures are imported
/// </summary>
private bool DisplayIfCreatureAlreadyInLibrary()
private bool IsCreatureAlreadyInLibrary(Guid creatureGuid, long arkId, out Creature existingCreature)
{
bool creatureAlreadyExistsInLibrary = creatureInfoInputExtractor.CreatureGuid != Guid.Empty
&& Utils.IsArkIdImported(creatureInfoInputExtractor.ArkId, creatureInfoInputExtractor.CreatureGuid)
&& creatureCollection.creatures.Any(c => c.guid == creatureInfoInputExtractor.CreatureGuid
&& !c.flags.HasFlag(CreatureFlags.Placeholder)
);
creatureInfoInputExtractor.UpdateExistingCreature = creatureAlreadyExistsInLibrary;
existingCreature = null;
bool creatureAlreadyExistsInLibrary = false;
if (creatureGuid != Guid.Empty
&& Utils.IsArkIdImported(arkId, creatureGuid))
{
existingCreature = creatureCollection.creatures.FirstOrDefault(c => c.guid == creatureGuid
&& !c.flags.HasFlag(CreatureFlags.Placeholder)
);
if (existingCreature != null)
creatureAlreadyExistsInLibrary = true;
}
return creatureAlreadyExistsInLibrary;
}
}
Expand Down
11 changes: 7 additions & 4 deletions ARKBreedingStats/Form1.importExported.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,12 @@ private void ExportedCreatureList_CopyValuesToExtractor(importExported.ExportedC
private void ImportExportedAddIfPossible_WatcherThread(string filePath)
{
// wait a moment until the file is readable. why is this necessary? blocked by fileWatcher?
System.Threading.Thread.Sleep(100);
System.Threading.Thread.Sleep(200);

// filewatcher is on another thread, invoke ui-thread to work with ui
Invoke(new Action(delegate () { ImportExportedAddIfPossible(filePath); }));
// moving to the archived folder can trigger another fileWatcherEvent, first check if the file is still there
if (File.Exists(filePath))
// filewatcher is on another thread, invoke ui-thread to work with ui
Invoke(new Action(delegate () { ImportExportedAddIfPossible(filePath); }));
}

/// <summary>
Expand All @@ -128,7 +130,8 @@ private void ImportExportedAddIfPossible(string filePath)
bool alreadyExists = ExtractExportedFileInExtractor(filePath);
bool added = false;

if (extractor.uniqueResults)
if (extractor.uniqueResults
|| (alreadyExists && extractor.validResults))
{
AddCreatureToCollection(true, goToLibraryTab: false);
added = true;
Expand Down
Loading

0 comments on commit 9bb67c7

Please sign in to comment.