Skip to content
This repository has been archived by the owner on Jan 24, 2019. It is now read-only.

Commit

Permalink
WeightedString to generic WeightectObject<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
Jofairden committed Aug 31, 2017
1 parent e971b1b commit 43a4ff0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
14 changes: 7 additions & 7 deletions NPCs/TownNPCs/ArabianMerchant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ public override string TownNPCName()

private readonly WeightedRandom<string> _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<string>("Salam aleykum! Do you need anything?"),
new WeightedObject<string>("I got some sand in my pockets. I think throwing it will hurt your eyes.", 2),
new WeightedObject<string>("My wear was absolutely white long time ago. Maybe I should wash it with this perfect yellow water?", 2),
new WeightedObject<string>("There are stories about what happened in the sands of this desert. But I won't tell you anything.", .5),
new WeightedObject<string>("In case something will happen with me... I bequeath you all my sand.", .75),
new WeightedObject<string>("The sands are telling me that... That... Ugh... That you will buy everything!", 3),
new WeightedObject<string>("The sands are moving... Be careful or you will be sucked into unknown depths!")
}.ToWeightedCollection();

public override string GetChat()
Expand Down
33 changes: 16 additions & 17 deletions TremorUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,21 @@

namespace Tremor
{
// literally just a tuple
public class WeightedString
public class WeightedObject<T>
{
public readonly string Message;
public readonly double Weight;
public T Obj;
public double Weight;

public static Tuple<string, double> Tuple(string message, double weight = 1d)
=> new Tuple<string, double>(message, weight);
public static Tuple<T, double> Tuple(T obj, double weight = 1d)
=> new Tuple<T, double>(obj, weight);

public Tuple<string, double> Tuple()
=> Tuple(Message, Weight);
public Tuple<T, double> 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;
}
}

Expand All @@ -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<string>[] ToWeightedStringCollection(this string[] strings, params double[] weights)
{
WeightedString[] chats = new WeightedString[strings.Length];
WeightedObject<string>[] chats = new WeightedObject<string>[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<string>(strings[i], i < weightCount ? weights[i] : 1d);
}
return chats;
}

public static WeightedRandom<string> ToWeightedCollection(this WeightedString[] strings)
=> new WeightedRandom<string>(strings.Select(x => new Tuple<string, double>(x.Message, x.Weight)).ToArray());
public static WeightedRandom<string> ToWeightedCollection(this WeightedObject<string>[] strings)
=> new WeightedRandom<string>(strings.Select(x => new Tuple<string, double>(x.Obj, x.Weight)).ToArray());

public static WeightedRandom<string> ToWeightedCollectionWithWeight(this string[] strings)
{
Expand All @@ -67,7 +66,7 @@ public static WeightedRandom<string> ToWeightedCollection(this string[] strings)
=> new WeightedRandom<string>(strings.Select(x => x.ToWeightedTuple()).ToArray());

public static Tuple<string, double> ToWeightedTuple(this string message, double weight = 1d)
=> WeightedString.Tuple(message, weight);
=> WeightedObject<string>.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);
Expand Down

0 comments on commit 43a4ff0

Please sign in to comment.