From 43a4ff0605530844c65fd51b7edbf63cddd53b42 Mon Sep 17 00:00:00 2001 From: Jofairden Date: Thu, 31 Aug 2017 12:45:16 +0200 Subject: [PATCH] WeightedString to generic WeightectObject --- NPCs/TownNPCs/ArabianMerchant.cs | 14 +++++++------- TremorUtils.cs | 33 ++++++++++++++++---------------- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/NPCs/TownNPCs/ArabianMerchant.cs b/NPCs/TownNPCs/ArabianMerchant.cs index ec2259d8..d2657e38 100644 --- a/NPCs/TownNPCs/ArabianMerchant.cs +++ b/NPCs/TownNPCs/ArabianMerchant.cs @@ -71,13 +71,13 @@ public override string TownNPCName() private readonly WeightedRandom _chats = new[] { - new WeightedString("Salam aleykum! Do you need anything?"), - new WeightedString("I got some sand in my pockets. I think throwing it will hurt your eyes.", 2), - new WeightedString("My wear was absolutely white long time ago. Maybe I should wash it with this perfect yellow water?", 2), - new WeightedString("There are stories about what happened in the sands of this desert. But I won't tell you anything.", .5), - new WeightedString("In case something will happen with me... I bequeath you all my sand.", .75), - new WeightedString("The sands are telling me that... That... Ugh... That you will buy everything!", 3), - new WeightedString("The sands are moving... Be careful or you will be sucked into unknown depths!") + new WeightedObject("Salam aleykum! Do you need anything?"), + new WeightedObject("I got some sand in my pockets. I think throwing it will hurt your eyes.", 2), + new WeightedObject("My wear was absolutely white long time ago. Maybe I should wash it with this perfect yellow water?", 2), + new WeightedObject("There are stories about what happened in the sands of this desert. But I won't tell you anything.", .5), + new WeightedObject("In case something will happen with me... I bequeath you all my sand.", .75), + new WeightedObject("The sands are telling me that... That... Ugh... That you will buy everything!", 3), + new WeightedObject("The sands are moving... Be careful or you will be sucked into unknown depths!") }.ToWeightedCollection(); public override string GetChat() diff --git a/TremorUtils.cs b/TremorUtils.cs index 9f1dd392..8e1b88fa 100644 --- a/TremorUtils.cs +++ b/TremorUtils.cs @@ -12,22 +12,21 @@ namespace Tremor { - // literally just a tuple - public class WeightedString + public class WeightedObject { - public readonly string Message; - public readonly double Weight; + public T Obj; + public double Weight; - public static Tuple Tuple(string message, double weight = 1d) - => new Tuple(message, weight); + public static Tuple Tuple(T obj, double weight = 1d) + => new Tuple(obj, weight); - public Tuple Tuple() - => Tuple(Message, Weight); + public Tuple Tuple() + => Tuple(Obj, Weight); - public WeightedString(string message, double weight = 1d) + public WeightedObject(T obj, double weight = 1d) { - Message = message; - Weight = weight; + this.Obj = obj; + this.Weight = weight; } } @@ -36,19 +35,19 @@ public static class TremorUtils public static string NamespaceToPath(this Type type) => type.Namespace?.Replace('.', '/'); - public static WeightedString[] ToWeightedStringCollection(this string[] strings, params double[] weights) + public static WeightedObject[] ToWeightedStringCollection(this string[] strings, params double[] weights) { - WeightedString[] chats = new WeightedString[strings.Length]; + WeightedObject[] chats = new WeightedObject[strings.Length]; int weightCount = weights.Length; for (int i = 0; i < strings.Length; i++) { - chats[i] = new WeightedString(strings[i], i < weightCount ? weights[i] : 1d); + chats[i] = new WeightedObject(strings[i], i < weightCount ? weights[i] : 1d); } return chats; } - public static WeightedRandom ToWeightedCollection(this WeightedString[] strings) - => new WeightedRandom(strings.Select(x => new Tuple(x.Message, x.Weight)).ToArray()); + public static WeightedRandom ToWeightedCollection(this WeightedObject[] strings) + => new WeightedRandom(strings.Select(x => new Tuple(x.Obj, x.Weight)).ToArray()); public static WeightedRandom ToWeightedCollectionWithWeight(this string[] strings) { @@ -67,7 +66,7 @@ public static WeightedRandom ToWeightedCollection(this string[] strings) => new WeightedRandom(strings.Select(x => x.ToWeightedTuple()).ToArray()); public static Tuple ToWeightedTuple(this string message, double weight = 1d) - => WeightedString.Tuple(message, weight); + => WeightedObject.Tuple(message, weight); public static NPC NewNPC(this ModItem item, int type, float ai0 = 0f, float ai1 = 0f, float ai2 = 0f, float ai3 = 0f, int target = 255, int start = 0, float offsetX = 0f, float offsetY = 0f) => NewNPC(item.item, type, ai0, ai1, ai2, ai3, target, start, offsetX, offsetY);