void TestAffixContainerGraph() { AffixContainer c1 = new AffixContainer(new Affix[] { AffixInfo.GenerateAffix(AffixType.Health, 4) }); AffixContainer c2 = new AffixContainer(new Affix[] { AffixInfo.GenerateAffix(AffixType.FireRate, 4) }); AffixContainer c3 = new AffixContainer(new Affix[] { AffixInfo.GenerateAffix(AffixType.Health, 20) }); AffixContainer c4 = new AffixContainer(new Affix[] { AffixInfo.GenerateAffix(AffixType.PhysDmgFlat, 4) }); print(c1); print(c2); print(c3); print(c4); //c1.AppendChild(c1); c1.AppendChild(c2); print("Appended c2 to c1"); print(c1); //c2.AppendChild(c1); c2.AppendChild(c3); print("Appended c3 to c2"); print(c1); print(c2); //c3.AppendChild(c1); //c3.AppendChild(c2); c1.AppendChild(c4); print("Appendecd c4 to c1"); print(c1); c2.DisconnectFromParent(); print("Disconnected c2 from c1"); print(c1); }
/// <summary> /// Saves the selected affix type to disk and updates the entry in the AffixInfo dictionary. /// </summary> /// <returns>Whether or not there were any problems saving. Also true if there were no changes.</returns> public async Task <bool> SaveSelectedType() { if (selectedType == AffixType.None) { return(false); } if (!AffixInfoDisplay.IsValid) { await DialogBasic.Show("Some inputs are invalid! Make sure no fields are empty.").result; return(false); } if (AffixInfoDisplay.IsChanged) { var newInfo = AffixInfoDisplay.Info; AffixInfo.Deregister(selectedType); AffixInfo.Register(newInfo); Serializer.SaveAffixInfoToDisk(newInfo); // Updates all the changed variables AffixInfoDisplay.SetType(newInfo.Type); } return(true); }
public void SetType(AffixType type) { currentInfo = AffixInfo.GetAffixInfo(type); NameDisplay.text = currentInfo.Name; DescriptionDisplay.SetText(currentInfo.Description); ValueInfoDisplay.SetInfo(currentInfo.ValueInfo); }
private double CalcAffixScore(AffixInfo x, AffixInfo y) { if (x.Ngram.Length == 0 && y.Ngram.Length == 0) { return(NormalizeScores ? 1.0 : MaxZScore(_wordCount)); } double score; if (x.Ngram.Length == 0 || y.Ngram.Length == 0) { score = NormalizeScores ? Math.Max(x.NormalizedZScore, y.NormalizedZScore) : Math.Max(x.ZScore, y.ZScore); if (WeightScores) { score *= (double)IntersectionCount(x.Stems, y.Stems) / Math.Min(x.Stems.Count, y.Stems.Count); } } else { score = NormalizeScores ? Math.Min(x.NormalizedZScore, y.NormalizedZScore) : Math.Min(x.ZScore, y.ZScore); if (WeightScores) { score *= (2.0 * IntersectionCount(x.Stems, y.Stems)) / (x.Stems.Count + y.Stems.Count); } } return(score); }
/// <summary> /// Returns a list of random affixes in this pool, making sure they are all of different types. The tiers will be determined through the provided Lottery. /// </summary> /// <param name="totalTiers">The total number of tiers of affixes to be generated</param> /// <param name="tierDistribution">The distribution of tiers</param> /// <returns></returns> public Affix[] GetUniqueRandomAffixes(int totalTiers, Lottery <int> tierDistribution, HashSet <AffixType> blacklist) { List <Affix> affixes = new List <Affix>(); lottery.StartBatchDraw(blacklist); while (totalTiers > 0) { // Get the tier of the Affix to be generated and cap it if it exceeds the total number of tiers left int tier = tierDistribution.Draw(); tier = tier > totalTiers ? totalTiers : tier; AffixType type = GetRandomType(); if (type == AffixType.None) { // We've run out of AffixTypes to draw from break; } affixes.Add(AffixInfo.GetAffixInfo(type).GenerateAffix(tier)); totalTiers -= tier; } lottery.EndBatchDraw(); return(affixes.ToArray()); }
private void AffixInfosListBox_SelectedIndexChanged(object sender, EventArgs e) { if (_last_index != -1 && AffixInfosListBox.SelectedIndex == _last_index) { return; } if (HasUnsavedChanges()) { if (MessageBox.Show("Unsaved changes will be lost. Proceed?", "Unsaved Changes", MessageBoxButtons.OKCancel) == DialogResult.Cancel) { AffixInfosListBox.SelectedIndex = _last_index; return; } } if (AffixInfosListBox.SelectedIndex == -1) { currentInfo = null; } else { currentInfo = AffixInfo.GetAffixInfo((AffixType)AffixInfosListBox.Items[AffixInfosListBox.SelectedIndex]); if (localType != currentInfo.Type) { UpdateCurrentAffixInfoDisplay(); } localType = currentInfo.Type; } _last_index = AffixInfosListBox.SelectedIndex; }
public Affix(AffixType type, AffixValue value, int tier) { Type = type; Value = value; Tier = tier; info = AffixInfo.GetAffixInfo(type); }
/// <summary> /// Saves an AffixInfo to disk at a specified location. /// </summary> /// <param name="path">The full path, including filename</param> public static void SaveAffixInfoToDisk(AffixInfo info, string path) { string data = SerializeAffixInfo(info); using (FileStream file = new FileStream(path, FileMode.Create)) using (StreamWriter writer = new StreamWriter(file)) writer.Write(data); }
/// <summary> /// Loads all affix infos from disk and registers them in the affix info dictionary /// </summary> /// <param name="folder">The path to the root data folder</param> public static void LoadAllAffixInfosFromDisk(string folder) { var texts = Resources.LoadAll <TextAsset>(folder); foreach (var text in texts) { AffixInfo.Register(DeserializeAffixInfo(text.text)); } }
/// <summary> /// Returns a list of random Affixes of types in this pool, making sure each is of a different type. All Affixes will be of the given tier. /// </summary> /// <param name="n">The number of Affixes to draw</param> /// <returns></returns> public Affix[] GetUniqueRandomAffixes(int n, int tier) { List <Affix> affixes = new List <Affix>(); AffixType[] types = GetUniqueRandomTypes(n); foreach (AffixType type in types) { affixes.Add(AffixInfo.GetAffixInfo(type).GenerateAffix(tier)); } return(affixes.ToArray()); }
public async Task RenameSelectedType() { if (selectedType == AffixType.None) { return; } if (!AffixInfoDisplay.IsValid) { DialogBasic.Show("Renaming requires that all changes are saved first, but there are invalid entries. Please fix all errors before trying again."); return; } if (AffixInfoDisplay.IsChanged) { if ((await DialogCancellable.Show("Renaming requires that all changes be saved first. Save now?").result).IsCancelled) { return; } if (await SaveSelectedType() == false) { DialogBasic.Show("There was a problem saving. Please recheck the values and try again."); return; } } var result = await DialogStringInput.ShowBlocking($"Enter a new name for '{selectedType.Name}'", name => !AffixType.Exists(name) && AffixType.IsValidName(name), ShowInvalidNameMessage).result; if (result.IsOK) { string name = result.Value; var oldInfo = AffixInfo.GetAffixInfo(selectedType); AffixInfo.Deregister(selectedType); Serializer.DeleteAffixInfo(oldInfo.Type); var newType = AffixType.Replace(selectedType.ID, name); var newInfo = new AffixInfo(newType, oldInfo.ValueInfo, oldInfo.Description); AffixInfo.Register(newInfo); Serializer.SaveAffixInfoToDisk(newInfo); var display = displays[selectedType]; displays.Remove(selectedType); displays.Add(newType, display); display.SetAffixType(newType); SelectType(newType); } }
protected static ICollection <AffixInfo> ComputeParadigm(AffixInfo mainAffix, IEnumerable <AffixInfo> affixes, int maxSearchDepth) { AffixInfo[] affixArray = affixes.ToArray(); var paradigm = new HashSet <AffixInfo>(mainAffix.ToEnumerable()); AffixInfo lastChanged = null; double paradigmScore = 0; for (int i = 0; i < maxSearchDepth; i++) { double maxScore = paradigmScore; HashSet <AffixInfo> bestParadigm = paradigm; AffixInfo bestChanged = null; foreach (AffixInfo affix in affixArray.Where(a => a != mainAffix && a != lastChanged)) { var newParadigm = new HashSet <AffixInfo>(paradigm); if (paradigm.Contains(affix)) { newParadigm.Remove(affix); } else { newParadigm.Add(affix); } double score = CalcVIScore(affixArray, newParadigm); if (score > maxScore) { maxScore = score; bestParadigm = newParadigm; bestChanged = affix; if (Math.Abs(maxScore - 1.0) < double.Epsilon) { return(bestParadigm); } } } if (bestParadigm == paradigm) { return(bestParadigm); } paradigm = bestParadigm; paradigmScore = maxScore; lastChanged = bestChanged; } return(paradigm); }
/// <summary> /// Shows a prompt for the user to enter a new name. /// </summary> public async Task CreateNew() { var result = await DialogStringInput.ShowBlocking("Enter a name:", name => !AffixType.Exists(name) && AffixType.IsValidName(name), ShowInvalidNameMessage).result; if (result.IsOK) { string name = result.Value; AffixInfo info = new AffixInfo(name, new AffixValueInfo()); AffixInfo.Register(info); AddTypeToLIst(info.Type); UpdateHeight(); SelectType(info.Type); } }
/// <summary> /// Delets the selected affix type. /// </summary> /// <returns>A task that returns whether or not the affix type was deleted.</returns> public async Task <bool> DeleteSelectedType() { if (selectedType == AffixType.None) { return(false); } if (await DialogCancellable.ShowBlocking($"Are you sure you want to delete {selectedType.Name}?").result == DialogResult.OK) { AffixInfo.Deregister(selectedType); AffixType.Remove(selectedType); Destroy(displays[selectedType].gameObject); displays.Remove(selectedType); Serializer.DeleteAffixInfo(selectedType); selectedType = AffixType.None; UpdateHeight(); return(true); } return(false); }
private void SaveAffixInfoButton_Click(object sender, EventArgs e) { AffixProgression newProgression = new AffixProgression(localProgressionFunctionName, localProgressionParameters); AffixValueInfo newValueInfo = null; if (localValueType == AffixValueType.SingleValue) { newValueInfo = new AffixValueInfo(localBaseValueMinSingle, localBaseValueMaxSingle, newProgression); } else if (localValueType == AffixValueType.Range) { newValueInfo = new AffixValueInfo(localBaseValueMinRange, localBaseValueMaxRange, newProgression); } AffixInfo newInfo = new AffixInfo(localValueType, localName, newValueInfo, localDescription); Serializer.SaveAffixInfoToDisk(newInfo); AffixInfo.Register(newInfo); currentInfo = newInfo; UpdateAllLabels(); }
/// <summary> /// Generates the Affixes and BaseAffixes for an item /// </summary> protected void GenerateAffixes(Item item) { item.Affixes.Clear(); item.BaseAffixes.Clear(); foreach (AffixType affixType in BaseAffixes) { item.BaseAffixes.Add(AffixInfo.GetAffixInfo(affixType).GenerateAffix(item.Tier)); } // Create new lottery to decide tier roll of each affix // TODO: make nicer Lottery<int> tierLottery = new Lottery<int>(); tierLottery.Enter(item.Tier, 10); if (item.Tier > 1) tierLottery.Enter(item.Tier - 1, 5); if (item.Tier > 2) tierLottery.Enter(item.Tier - 2, 2); if (item.Tier > 3) tierLottery.Enter(item.Tier - 3, 1); int quality = item.Quality; // Guaranteed affixes don't care about possible pool foreach (AffixType affixType in GuaranteedAffixes) { int tier = tierLottery.Draw(); tier = tier > quality ? quality : tier; item.Affixes.Add(AffixInfo.GetAffixInfo(affixType).GenerateAffix(tier)); quality -= tier; } // Fill up the rest of the affixes Affix[] randomAffixes = PossibleAffixes.GetUniqueRandomAffixes(quality, tierLottery, new HashSet<AffixType>(GuaranteedAffixes)); foreach (Affix affix in randomAffixes) { item.Affixes.Add(affix); } }
void GenerateTestAffixContainer() { AffixContainer container = new AffixContainer(); Affix health1 = AffixInfo.GetAffixInfo(AffixType.Health).GenerateAffix(2); container.Add(health1); container.Add(AffixInfo.GetAffixInfo(AffixType.Health).GenerateAffix(2)); container.Add(health1); container.Add(AffixInfo.GetAffixInfo(AffixType.PhysDmgFlat).GenerateAffix(3)); print(container); container.Remove(health1); print(container); Affix fireRate = AffixInfo.GetAffixInfo(AffixType.FireRate).GenerateAffix(5); container.Remove(fireRate); Affix health2 = AffixInfo.GetAffixInfo(AffixType.Health).GenerateAffix(3); container.Remove(health2); }
/// <summary> /// Takes an AffixInfo object and returns a string containing formatted XML describing it. /// </summary> /// <param name="info">The AffixInfo object to be serialized</param> /// <returns>A strong containing formatted XML</returns> public static string SerializeAffixInfo(AffixInfo info) { return(JsonConvert.SerializeObject(info)); }
protected static double CalcVpScore(ICollection <AffixInfo> paradigm, AffixInfo y) { return(paradigm.Where(x => x != y).Sum(x => CalcHScore(x, y))); }
protected static double CalcHScore(AffixInfo x, AffixInfo y) { return((double)IntersectionCount(x.Stems, y.Stems) / x.Stems.Count); }
/// <summary> /// Returns a random Affix of a type in this pool. The Affix will be of the given tier. /// </summary> /// <returns></returns> public Affix GetRandomAffix(int tier) { AffixType type = GetRandomType(); return(AffixInfo.GetAffixInfo(type).GenerateAffix(tier)); }
protected IEnumerable <AffixInfo> ComputeAffixes(ICollection <TSeq> sequences, AffixType type) { var dir = Direction.LeftToRight; switch (type) { case AffixType.Prefix: dir = Direction.LeftToRight; break; case AffixType.Suffix: dir = Direction.RightToLeft; break; } var affixFreqDist = new ConditionalFrequencyDistribution <int, Ngram <TItem> >(); var ngramFreqDist = new ConditionalFrequencyDistribution <int, Ngram <TItem> >(); var itemFreqDist = new FrequencyDistribution <TItem>(); var affixes = new Dictionary <Ngram <TItem>, AffixInfo>(); var nullAffix = new AffixInfo(sequences.Count, new Ngram <TItem>()); foreach (TSeq seq in sequences) { var wordNgram = new Ngram <TItem>(_syllablesSelector(seq).SelectMany(s => s)); nullAffix.Stems.Add(wordNgram); foreach (TItem item in wordNgram) { itemFreqDist.Increment(item); } if (wordNgram.Length <= 1) { continue; } var items = new List <TItem>(); var syllableStart = new HashSet <int>(); foreach (IEnumerable <TItem> syllable in _syllablesSelector(seq).Items(dir)) { items.AddRange(syllable.Items(dir)); syllableStart.Add(items.Count - 1); } var affix = new Ngram <TItem>(); var stem = new Ngram <TItem>(items, dir); for (int i = 0; i < Math.Min(MaxAffixLength + 1, items.Count); i++) { affix = affix.Concat(items[i], dir); affixFreqDist[affix.Length].Increment(affix); if (i < items.Count - 1 && affix.Length <= MaxAffixLength) { AffixInfo ai = affixes.GetOrCreate(affix, () => new AffixInfo(sequences.Count, affix)); stem = stem.SkipFirst(dir); ai.Stems.Add(stem); if (syllableStart.Contains(i)) { ai.SyllableBreakCount++; } } } for (int i = 0; i < items.Count; i++) { var ngram = new Ngram <TItem>(); for (int j = i; j < Math.Min(MaxAffixLength + i, items.Count); j++) { ngram = ngram.Concat(items[j], dir); ngramFreqDist[ngram.Length].Increment(ngram); } } } var itemProbDist = new MaxLikelihoodProbabilityDistribution <TItem>(itemFreqDist); var affixProbDist = new ConditionalProbabilityDistribution <int, Ngram <TItem> >(affixFreqDist, (c, fd) => { if (c == 1) { return(new MaxLikelihoodProbabilityDistribution <Ngram <TItem> >(fd)); } int binCount; try { binCount = checked ((int)Math.Pow(itemFreqDist.ObservedSamples.Count, c)); } catch (OverflowException) { binCount = int.MaxValue; } return(new WittenBellProbabilityDistribution <Ngram <TItem> >(fd, binCount)); }); var ngramProbDist = new ConditionalProbabilityDistribution <int, Ngram <TItem> >(ngramFreqDist, (c, fd) => { if (c == 1) { return(new MaxLikelihoodProbabilityDistribution <Ngram <TItem> >(fd)); } int binCount; try { binCount = checked ((int)Math.Pow(itemFreqDist.ObservedSamples.Count, c)); } catch (OverflowException) { binCount = int.MaxValue; } return(new WittenBellProbabilityDistribution <Ngram <TItem> >(fd, binCount)); }); foreach (AffixInfo affix in affixes.Values) { int freq = affixFreqDist[affix.Ngram.Length][affix.Ngram]; var maxCurveItem = itemFreqDist.ObservedSamples.Select(item => new { Item = item, Curve = (double)affixFreqDist[affix.Ngram.Length + 1][affix.Ngram.Concat(item, dir)] / freq }) .MaxBy(item => item.Curve); double curveDrop = (1 - maxCurveItem.Curve) / (1 - itemProbDist[maxCurveItem.Item]); double pw = affixProbDist[affix.Ngram.Length][affix.Ngram]; double npw = ngramProbDist[affix.Ngram.Length][affix.Ngram]; double randomAdj = npw == 0 ? 1.0 : pw / npw; double normalizedFreq = affix.Ngram.Length * Math.Log(freq); double syllableScore = AffixesOccurOnSyllableBoundaries ? (0.5 * ((double)affix.SyllableBreakCount / freq)) + 0.5 : 1.0; affix.ZScore = curveDrop * randomAdj * normalizedFreq * syllableScore; yield return(affix); } yield return(nullAffix); }
/// <summary> /// Saves info about a certain affix type to disk. /// </summary> public static void SaveAffixInfoToDisk(AffixInfo info) { SaveAffixInfoToDisk(info, GetSystemPath(GetPathFromAffixType(info.Type))); }
/// <summary> /// Registers an AffixInfo object in the affix info dictionary /// </summary> /// <param name="info"></param> public static void Register(AffixInfo info) { affixInfoDictionary[info.Type] = info; }
private void generateSampleAffixToolStripMenuItem_Click(object sender, EventArgs e) { MessageBox.Show(AffixInfo.GenerateAffix(currentInfo.Type, 1).ToString()); }
void TestAffixGeneration() { print(AffixInfo.GenerateAffix(3, 5).ToString()); }
/// <summary> /// Copies from an existing affix info object. Note that this also recreates the affix type. /// </summary> public AffixInfo(AffixInfo src) : this(src.ID, src.Name, src.ValueInfo, src.Description) { }